2 * Copyright (C) Arnaldo Carvalho de Melo 2004
3 * Copyright (C) Ian McDonald 2005
4 * Copyright (C) Yoshifumi Nishida 2005
6 * This software may be distributed either under the terms of the
7 * BSD-style license that accompanies tcpdump or the GNU GPL version 2
11 static const char rcsid
[] _U_
=
12 "@(#) $Header: /tcpdump/master/tcpdump/print-dccp.c,v 1.8 2007-11-09 00:44:09 guy Exp $ (LBL)";
19 #include <tcpdump-stdinc.h>
26 #include "interface.h"
27 #include "addrtoname.h"
28 #include "extract.h" /* must come after interface.h */
35 static const char *dccp_reset_codes
[] = {
50 static const char *dccp_feature_nums
[] = {
59 "minimum checksum coverage",
60 "check data checksum",
63 static inline u_int
dccp_csum_coverage(const struct dccp_hdr
* dh
, u_int len
)
67 if (DCCPH_CSCOV(dh
) == 0)
69 cov
= (dh
->dccph_doff
+ DCCPH_CSCOV(dh
) - 1) * sizeof(u_int32_t
);
70 return (cov
> len
)? len
: cov
;
73 static int dccp_cksum(const struct ip
*ip
,
74 const struct dccp_hdr
*dh
, u_int len
)
76 return nextproto4_cksum(ip
, (const u_int8_t
*)(void *)dh
,
77 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
81 static int dccp6_cksum(const struct ip6_hdr
*ip6
, const struct dccp_hdr
*dh
, u_int len
)
83 return nextproto6_cksum(ip6
, (const u_int8_t
*)(void *)dh
,
84 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
88 static const char *dccp_reset_code(u_int8_t code
)
90 if (code
>= __DCCP_RESET_CODE_LAST
)
92 return dccp_reset_codes
[code
];
95 static u_int64_t
dccp_seqno(const struct dccp_hdr
*dh
)
97 u_int32_t seq_high
= DCCPH_SEQ(dh
);
98 u_int64_t seqno
= EXTRACT_24BITS(&seq_high
) & 0xFFFFFF;
100 if (DCCPH_X(dh
) != 0) {
101 const struct dccp_hdr_ext
*dhx
= (void *)(dh
+ 1);
102 u_int32_t seq_low
= dhx
->dccph_seq_low
;
103 seqno
&= 0x00FFFF; /* clear reserved field */
104 seqno
= (seqno
<< 32) + EXTRACT_32BITS(&seq_low
);
110 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
112 return sizeof(*dh
) + (DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : 0);
115 static void dccp_print_ack_no(const u_char
*bp
)
117 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
118 const struct dccp_hdr_ack_bits
*dh_ack
=
119 (struct dccp_hdr_ack_bits
*)(bp
+ dccp_basic_hdr_len(dh
));
124 ack_high
= DCCPH_ACK(dh_ack
);
125 ackno
= EXTRACT_24BITS(&ack_high
) & 0xFFFFFF;
127 if (DCCPH_X(dh
) != 0) {
131 ack_low
= dh_ack
->dccph_ack_nr_low
;
133 ackno
&= 0x00FFFF; /* clear reserved field */
134 ackno
= (ackno
<< 32) + EXTRACT_32BITS(&ack_low
);
137 (void)printf("(ack=%" PRIu64
") ", ackno
);
142 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type
)
144 if (type
== DCCP_PKT_DATA
)
146 if (type
== DCCP_PKT_DATAACK
||
147 type
== DCCP_PKT_ACK
||
148 type
== DCCP_PKT_SYNC
||
149 type
== DCCP_PKT_SYNCACK
||
150 type
== DCCP_PKT_CLOSE
||
151 type
== DCCP_PKT_CLOSEREQ
)
152 return sizeof(struct dccp_hdr_ack_bits
);
153 if (type
== DCCP_PKT_REQUEST
)
154 return sizeof(struct dccp_hdr_request
);
155 if (type
== DCCP_PKT_RESPONSE
)
156 return sizeof(struct dccp_hdr_response
);
157 return sizeof(struct dccp_hdr_reset
);
160 static int dccp_print_option(const u_char
*option
);
163 * dccp_print - show dccp packet
164 * @bp - beginning of dccp packet
165 * @data2 - beginning of enclosing
166 * @len - lenght of ip packet
168 void dccp_print(const u_char
*bp
, const u_char
*data2
, u_int len
)
170 const struct dccp_hdr
*dh
;
173 const struct ip6_hdr
*ip6
;
176 u_short sport
, dport
;
180 dh
= (const struct dccp_hdr
*)bp
;
182 ip
= (struct ip
*)data2
;
185 ip6
= (const struct ip6_hdr
*)data2
;
189 cp
= (const u_char
*)(dh
+ 1);
191 printf("[Invalid packet|dccp]");
195 if (len
< sizeof(struct dccp_hdr
)) {
196 printf("truncated-dccp - %ld bytes missing!",
197 (long)len
- sizeof(struct dccp_hdr
));
201 sport
= EXTRACT_16BITS(&dh
->dccph_sport
);
202 dport
= EXTRACT_16BITS(&dh
->dccph_dport
);
203 hlen
= dh
->dccph_doff
* 4;
207 (void)printf("%s.%d > %s.%d: ",
208 ip6addr_string(&ip6
->ip6_src
), sport
,
209 ip6addr_string(&ip6
->ip6_dst
), dport
);
213 (void)printf("%s.%d > %s.%d: ",
214 ipaddr_string(&ip
->ip_src
), sport
,
215 ipaddr_string(&ip
->ip_dst
), dport
);
220 (void)printf(" %d", len
- hlen
);
222 (void)printf("dccp [bad hdr length %u - too long, > %u]",
228 /* other variables in generic header */
230 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
233 /* checksum calculation */
234 if (vflag
&& TTEST2(bp
[0], len
)) {
235 u_int16_t sum
= 0, dccp_sum
;
237 dccp_sum
= EXTRACT_16BITS(&dh
->dccph_checksum
);
238 (void)printf("cksum 0x%04x ", dccp_sum
);
240 sum
= dccp_cksum(ip
, dh
, len
);
242 else if (IP_V(ip
) == 6)
243 sum
= dccp6_cksum(ip6
, dh
, len
);
246 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum
, sum
));
248 (void)printf("(correct), ");
251 switch (DCCPH_TYPE(dh
)) {
252 case DCCP_PKT_REQUEST
: {
253 struct dccp_hdr_request
*dhr
=
254 (struct dccp_hdr_request
*)(bp
+ dccp_basic_hdr_len(dh
));
256 (void)printf("request (service=%d) ",
257 EXTRACT_32BITS(&dhr
->dccph_req_service
));
261 case DCCP_PKT_RESPONSE
: {
262 struct dccp_hdr_response
*dhr
=
263 (struct dccp_hdr_response
*)(bp
+ dccp_basic_hdr_len(dh
));
265 (void)printf("response (service=%d) ",
266 EXTRACT_32BITS(&dhr
->dccph_resp_service
));
271 (void)printf("data ");
274 (void)printf("ack ");
278 case DCCP_PKT_DATAACK
: {
279 (void)printf("dataack ");
283 case DCCP_PKT_CLOSEREQ
:
284 (void)printf("closereq ");
288 (void)printf("close ");
291 case DCCP_PKT_RESET
: {
292 struct dccp_hdr_reset
*dhr
=
293 (struct dccp_hdr_reset
*)(bp
+ dccp_basic_hdr_len(dh
));
295 (void)printf("reset (code=%s) ",
296 dccp_reset_code(dhr
->dccph_reset_code
));
301 (void)printf("sync ");
304 case DCCP_PKT_SYNCACK
:
305 (void)printf("syncack ");
309 (void)printf("invalid ");
313 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
314 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
315 dccp_print_ack_no(bp
);
320 (void)printf("seq %" PRIu64
, dccp_seqno(dh
));
322 /* process options */
323 if (hlen
> dccp_basic_hdr_len(dh
) + extlen
){
326 cp
= bp
+ dccp_basic_hdr_len(dh
) + extlen
;
329 hlen
-= dccp_basic_hdr_len(dh
) + extlen
;
332 optlen
= dccp_print_option(cp
);
333 if (!optlen
) goto trunc2
;
334 if (hlen
<= optlen
) break;
348 static int dccp_print_option(const u_char
*option
)
356 optlen
= *(option
+1);
358 printf("Option %d optlen too short",*option
);
363 TCHECK2(*option
,optlen
);
373 printf("slowreceiver");
377 if (*(option
+2) < 10){
378 printf(" %s", dccp_feature_nums
[*(option
+2)]);
379 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
384 if (*(option
+2) < 10){
385 printf(" %s", dccp_feature_nums
[*(option
+2)]);
386 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
391 if (*(option
+2) < 10){
392 printf(" %s", dccp_feature_nums
[*(option
+2)]);
393 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
398 if (*(option
+2) < 10){
399 printf(" %s", dccp_feature_nums
[*(option
+2)]);
400 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
404 printf("initcookie 0x");
405 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
409 for (i
= 0; i
< optlen
-2; i
++) printf(" %d", *(option
+2 + i
));
412 printf("ack_vector0 0x");
413 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
416 printf("ack_vector1 0x");
417 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
420 printf("data_dropped 0x");
421 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
424 printf("timestamp %u", EXTRACT_32BITS(option
+ 2));
427 printf("timestamp_echo %u", EXTRACT_32BITS(option
+ 2));
430 printf("elapsed_time ");
432 printf("%u", EXTRACT_32BITS(option
+ 2));
434 printf("%u", EXTRACT_16BITS(option
+ 2));
437 printf("data_checksum ");
438 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
441 if (*option
>= 128) {
442 printf("CCID option %d",*option
);
445 printf(" %u", EXTRACT_16BITS(option
+ 2));
448 printf(" %u", EXTRACT_32BITS(option
+ 2));
456 printf("unknown_opt %d", *option
);