4 * Copyright (C) Arnaldo Carvalho de Melo 2004
5 * Copyright (C) Ian McDonald 2005
6 * Copyright (C) Yoshifumi Nishida 2005
8 * This software may be distributed either under the terms of the
9 * BSD-style license that accompanies tcpdump or the GNU GPL version 2
12 #include <sys/cdefs.h>
15 static const char rcsid
[] _U_
=
16 "@(#) Header: /tcpdump/master/tcpdump/print-dccp.c,v 1.1.2.6 2006/02/19 05:08:44 guy Exp (LBL)";
18 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
26 #include <tcpdump-stdinc.h>
33 #include "interface.h"
34 #include "addrtoname.h"
35 #include "extract.h" /* must come after interface.h */
42 static const char *dccp_reset_codes
[] = {
57 static const char *dccp_feature_nums
[] = {
66 "minimum checksum coverage",
67 "check data checksum",
70 static int dccp_cksum(const struct ip
*ip
,
71 const struct dccp_hdr
*dh
, u_int len
)
87 phu
.ph
.len
= htons(len
);
88 phu
.ph
.proto
= IPPROTO_DCCP
;
89 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
91 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
93 phu
.ph
.dst
= ip_finddst(ip
);
96 return in_cksum((u_short
*)dh
, len
, sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
100 static int dccp6_cksum(const struct ip6_hdr
*ip6
, const struct dccp_hdr
*dh
, u_int len
)
107 struct in6_addr ph_src
;
108 struct in6_addr ph_dst
;
117 memset(&phu
, 0, sizeof(phu
));
118 phu
.ph
.ph_src
= ip6
->ip6_src
;
119 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
120 phu
.ph
.ph_len
= htonl(len
);
121 phu
.ph
.ph_nxt
= IPPROTO_DCCP
;
124 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
127 sp
= (const u_int16_t
*)dh
;
129 for (i
= 0; i
< (len
& ~1); i
+= 2)
133 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
136 sum
= (sum
& 0xffff) + (sum
>> 16);
143 static const char *dccp_reset_code(u_int8_t code
)
145 if (code
>= __DCCP_RESET_CODE_LAST
)
147 return dccp_reset_codes
[code
];
150 static u_int64_t
dccp_seqno(const struct dccp_hdr
*dh
)
152 u_int32_t seq_high
= DCCPH_SEQ(dh
);
153 u_int64_t seqno
= EXTRACT_24BITS(&seq_high
) & 0xFFFFFF;
155 if (DCCPH_X(dh
) != 0) {
156 const struct dccp_hdr_ext
*dhx
= (void *)(dh
+ 1);
157 u_int32_t seq_low
= dhx
->dccph_seq_low
;
158 seqno
&= 0x00FFFF; /* clear reserved field */
159 seqno
= (seqno
<< 32) + EXTRACT_32BITS(&seq_low
);
165 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
167 return sizeof(*dh
) + (DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : 0);
170 static void dccp_print_ack_no(const u_char
*bp
)
172 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
173 const struct dccp_hdr_ack_bits
*dh_ack
=
174 (struct dccp_hdr_ack_bits
*)(bp
+ dccp_basic_hdr_len(dh
));
179 ack_high
= DCCPH_ACK(dh_ack
);
180 ackno
= EXTRACT_24BITS(&ack_high
) & 0xFFFFFF;
182 if (DCCPH_X(dh
) != 0) {
186 ack_low
= dh_ack
->dccph_ack_nr_low
;
188 ackno
&= 0x00FFFF; /* clear reserved field */
189 ackno
= (ackno
<< 32) + EXTRACT_32BITS(&ack_low
);
192 (void)printf("(ack=%" PRIu64
") ", ackno
);
197 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type
)
199 if (type
== DCCP_PKT_DATA
)
201 if (type
== DCCP_PKT_DATAACK
||
202 type
== DCCP_PKT_ACK
||
203 type
== DCCP_PKT_SYNC
||
204 type
== DCCP_PKT_SYNCACK
||
205 type
== DCCP_PKT_CLOSE
||
206 type
== DCCP_PKT_CLOSEREQ
)
207 return sizeof(struct dccp_hdr_ack_bits
);
208 if (type
== DCCP_PKT_REQUEST
)
209 return sizeof(struct dccp_hdr_request
);
210 if (type
== DCCP_PKT_RESPONSE
)
211 return sizeof(struct dccp_hdr_response
);
212 return sizeof(struct dccp_hdr_reset
);
215 static int dccp_print_option(const u_char
*option
);
218 * dccp_print - show dccp packet
219 * @bp - beginning of dccp packet
220 * @data2 - beginning of enclosing
221 * @len - lenght of ip packet
223 void dccp_print(const u_char
*bp
, const u_char
*data2
, u_int len
)
225 const struct dccp_hdr
*dh
;
228 const struct ip6_hdr
*ip6
;
231 u_short sport
, dport
;
235 dh
= (const struct dccp_hdr
*)bp
;
237 ip
= (struct ip
*)data2
;
240 ip6
= (const struct ip6_hdr
*)data2
;
244 cp
= (const u_char
*)(dh
+ 1);
246 printf("[Invalid packet|dccp]");
250 if (len
< sizeof(struct dccp_hdr
)) {
251 printf("truncated-dccp - %ld bytes missing!",
252 (long)len
- sizeof(struct dccp_hdr
));
256 sport
= EXTRACT_16BITS(&dh
->dccph_sport
);
257 dport
= EXTRACT_16BITS(&dh
->dccph_dport
);
258 hlen
= dh
->dccph_doff
* 4;
262 (void)printf("%s.%d > %s.%d: ",
263 ip6addr_string(&ip6
->ip6_src
), sport
,
264 ip6addr_string(&ip6
->ip6_dst
), dport
);
268 (void)printf("%s.%d > %s.%d: ",
269 ipaddr_string(&ip
->ip_src
), sport
,
270 ipaddr_string(&ip
->ip_dst
), dport
);
275 (void)printf(" %d", len
- hlen
);
277 (void)printf("dccp [bad hdr length %u - too long, > %u]",
283 /* other variables in generic header */
285 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
288 /* checksum calculation */
291 if (ip6
->ip6_plen
&& vflag
) {
292 u_int16_t sum
, dccp_sum
;
294 sum
= dccp6_cksum(ip6
, dh
, len
);
295 dccp_sum
= EXTRACT_16BITS(&dh
->dccph_checksum
);
296 printf("cksum 0x%04x", dccp_sum
);
298 (void)printf(" (incorrect (-> 0x%04x), ",in_cksum_shouldbe(dccp_sum
, sum
));
300 (void)printf(" (correct), ");
306 u_int16_t sum
, dccp_sum
;
308 sum
= dccp_cksum(ip
, dh
, len
);
309 dccp_sum
= EXTRACT_16BITS(&dh
->dccph_checksum
);
310 printf("cksum 0x%04x", dccp_sum
);
312 (void)printf(" (incorrect (-> 0x%04x), ",in_cksum_shouldbe(dccp_sum
, sum
));
314 (void)printf(" (correct), ");
317 switch (DCCPH_TYPE(dh
)) {
318 case DCCP_PKT_REQUEST
: {
319 struct dccp_hdr_request
*dhr
=
320 (struct dccp_hdr_request
*)(bp
+ dccp_basic_hdr_len(dh
));
322 (void)printf("request (service=%d) ",
323 EXTRACT_32BITS(&dhr
->dccph_req_service
));
327 case DCCP_PKT_RESPONSE
: {
328 struct dccp_hdr_response
*dhr
=
329 (struct dccp_hdr_response
*)(bp
+ dccp_basic_hdr_len(dh
));
331 (void)printf("response (service=%d) ",
332 EXTRACT_32BITS(&dhr
->dccph_resp_service
));
337 (void)printf("data ");
340 (void)printf("ack ");
344 case DCCP_PKT_DATAACK
: {
345 (void)printf("dataack ");
349 case DCCP_PKT_CLOSEREQ
:
350 (void)printf("closereq ");
354 (void)printf("close ");
357 case DCCP_PKT_RESET
: {
358 struct dccp_hdr_reset
*dhr
=
359 (struct dccp_hdr_reset
*)(bp
+ dccp_basic_hdr_len(dh
));
361 (void)printf("reset (code=%s) ",
362 dccp_reset_code(dhr
->dccph_reset_code
));
367 (void)printf("sync ");
370 case DCCP_PKT_SYNCACK
:
371 (void)printf("syncack ");
375 (void)printf("invalid ");
379 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
380 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
381 dccp_print_ack_no(bp
);
386 (void)printf("seq %" PRIu64
, dccp_seqno(dh
));
388 /* process options */
389 if (hlen
> dccp_basic_hdr_len(dh
) + extlen
){
392 cp
= bp
+ dccp_basic_hdr_len(dh
) + extlen
;
395 hlen
-= dccp_basic_hdr_len(dh
) + extlen
;
398 optlen
= dccp_print_option(cp
);
399 if (!optlen
) goto trunc2
;
400 if (hlen
<= optlen
) break;
414 static int dccp_print_option(const u_char
*option
)
425 optlen
= *(option
+1);
427 printf("Option %d optlen too short",*option
);
432 TCHECK2(*option
,optlen
);
442 printf("slowreceiver");
446 if (*(option
+2) < 10){
447 printf(" %s", dccp_feature_nums
[*(option
+2)]);
448 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
453 if (*(option
+2) < 10){
454 printf(" %s", dccp_feature_nums
[*(option
+2)]);
455 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
460 if (*(option
+2) < 10){
461 printf(" %s", dccp_feature_nums
[*(option
+2)]);
462 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
467 if (*(option
+2) < 10){
468 printf(" %s", dccp_feature_nums
[*(option
+2)]);
469 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
473 printf("initcookie 0x");
474 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
478 for (i
= 0; i
< optlen
-2; i
++) printf(" %d", *(option
+2 + i
));
481 printf("ack_vector0 0x");
482 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
485 printf("ack_vector1 0x");
486 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
489 printf("data_dropped 0x");
490 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
493 ts
= (u_int32_t
*)(option
+ 2);
494 printf("timestamp %u", (u_int32_t
)ntohl(*ts
));
497 ts
= (u_int32_t
*)(option
+ 2);
498 printf("timestamp_echo %u", (u_int32_t
)ntohl(*ts
));
501 printf("elapsed_time ");
503 ts
= (u_int32_t
*)(option
+ 2);
504 printf("%u", (u_int32_t
)ntohl(*ts
));
506 var16
= (u_int16_t
*)(option
+ 2);
507 printf("%u", ntohs(*var16
));
511 printf("data_checksum ");
512 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
515 if (*option
>= 128) {
516 printf("CCID option %d",*option
);
519 var16
= (u_int16_t
*)(option
+ 2);
520 printf(" %u",ntohs(*var16
));
523 var32
= (u_int32_t
*)(option
+ 2);
524 printf(" %u",(u_int32_t
)ntohl(*var32
));
532 printf("unknown_opt %d", *option
);