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.7.2.1 2007-11-09 00:45:16 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 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 int cov
= dccp_csum_coverage(dh
, len
);
91 phu
.ph
.len
= htons(len
);
92 phu
.ph
.proto
= IPPROTO_DCCP
;
93 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
95 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
97 phu
.ph
.dst
= ip_finddst(ip
);
100 return in_cksum((u_short
*)dh
, cov
, sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5]);
104 static int dccp6_cksum(const struct ip6_hdr
*ip6
, const struct dccp_hdr
*dh
, u_int len
)
108 int cov
= dccp_csum_coverage(dh
, len
);
111 struct in6_addr ph_src
;
112 struct in6_addr ph_dst
;
121 memset(&phu
, 0, sizeof(phu
));
122 phu
.ph
.ph_src
= ip6
->ip6_src
;
123 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
124 phu
.ph
.ph_len
= htonl(len
);
125 phu
.ph
.ph_nxt
= IPPROTO_DCCP
;
127 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
130 return in_cksum((u_short
*)dh
, cov
, sum
);
134 static const char *dccp_reset_code(u_int8_t code
)
136 if (code
>= __DCCP_RESET_CODE_LAST
)
138 return dccp_reset_codes
[code
];
141 static u_int64_t
dccp_seqno(const struct dccp_hdr
*dh
)
143 u_int32_t seq_high
= DCCPH_SEQ(dh
);
144 u_int64_t seqno
= EXTRACT_24BITS(&seq_high
) & 0xFFFFFF;
146 if (DCCPH_X(dh
) != 0) {
147 const struct dccp_hdr_ext
*dhx
= (void *)(dh
+ 1);
148 u_int32_t seq_low
= dhx
->dccph_seq_low
;
149 seqno
&= 0x00FFFF; /* clear reserved field */
150 seqno
= (seqno
<< 32) + EXTRACT_32BITS(&seq_low
);
156 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
158 return sizeof(*dh
) + (DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : 0);
161 static void dccp_print_ack_no(const u_char
*bp
)
163 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
164 const struct dccp_hdr_ack_bits
*dh_ack
=
165 (struct dccp_hdr_ack_bits
*)(bp
+ dccp_basic_hdr_len(dh
));
170 ack_high
= DCCPH_ACK(dh_ack
);
171 ackno
= EXTRACT_24BITS(&ack_high
) & 0xFFFFFF;
173 if (DCCPH_X(dh
) != 0) {
177 ack_low
= dh_ack
->dccph_ack_nr_low
;
179 ackno
&= 0x00FFFF; /* clear reserved field */
180 ackno
= (ackno
<< 32) + EXTRACT_32BITS(&ack_low
);
183 (void)printf("(ack=%" PRIu64
") ", ackno
);
188 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type
)
190 if (type
== DCCP_PKT_DATA
)
192 if (type
== DCCP_PKT_DATAACK
||
193 type
== DCCP_PKT_ACK
||
194 type
== DCCP_PKT_SYNC
||
195 type
== DCCP_PKT_SYNCACK
||
196 type
== DCCP_PKT_CLOSE
||
197 type
== DCCP_PKT_CLOSEREQ
)
198 return sizeof(struct dccp_hdr_ack_bits
);
199 if (type
== DCCP_PKT_REQUEST
)
200 return sizeof(struct dccp_hdr_request
);
201 if (type
== DCCP_PKT_RESPONSE
)
202 return sizeof(struct dccp_hdr_response
);
203 return sizeof(struct dccp_hdr_reset
);
206 static int dccp_print_option(const u_char
*option
);
209 * dccp_print - show dccp packet
210 * @bp - beginning of dccp packet
211 * @data2 - beginning of enclosing
212 * @len - lenght of ip packet
214 void dccp_print(const u_char
*bp
, const u_char
*data2
, u_int len
)
216 const struct dccp_hdr
*dh
;
219 const struct ip6_hdr
*ip6
;
222 u_short sport
, dport
;
226 dh
= (const struct dccp_hdr
*)bp
;
228 ip
= (struct ip
*)data2
;
231 ip6
= (const struct ip6_hdr
*)data2
;
235 cp
= (const u_char
*)(dh
+ 1);
237 printf("[Invalid packet|dccp]");
241 if (len
< sizeof(struct dccp_hdr
)) {
242 printf("truncated-dccp - %ld bytes missing!",
243 (long)len
- sizeof(struct dccp_hdr
));
247 sport
= EXTRACT_16BITS(&dh
->dccph_sport
);
248 dport
= EXTRACT_16BITS(&dh
->dccph_dport
);
249 hlen
= dh
->dccph_doff
* 4;
253 (void)printf("%s.%d > %s.%d: ",
254 ip6addr_string(&ip6
->ip6_src
), sport
,
255 ip6addr_string(&ip6
->ip6_dst
), dport
);
259 (void)printf("%s.%d > %s.%d: ",
260 ipaddr_string(&ip
->ip_src
), sport
,
261 ipaddr_string(&ip
->ip_dst
), dport
);
266 (void)printf(" %d", len
- hlen
);
268 (void)printf("dccp [bad hdr length %u - too long, > %u]",
274 /* other variables in generic header */
276 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
279 /* checksum calculation */
280 if (vflag
&& TTEST2(bp
[0], len
)) {
281 u_int16_t sum
= 0, dccp_sum
;
283 dccp_sum
= EXTRACT_16BITS(&dh
->dccph_checksum
);
284 (void)printf("cksum 0x%04x ", dccp_sum
);
286 sum
= dccp_cksum(ip
, dh
, len
);
288 else if (IP_V(ip
) == 6)
289 sum
= dccp6_cksum(ip6
, dh
, len
);
292 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum
, sum
));
294 (void)printf("(correct), ");
297 switch (DCCPH_TYPE(dh
)) {
298 case DCCP_PKT_REQUEST
: {
299 struct dccp_hdr_request
*dhr
=
300 (struct dccp_hdr_request
*)(bp
+ dccp_basic_hdr_len(dh
));
302 (void)printf("request (service=%d) ",
303 EXTRACT_32BITS(&dhr
->dccph_req_service
));
307 case DCCP_PKT_RESPONSE
: {
308 struct dccp_hdr_response
*dhr
=
309 (struct dccp_hdr_response
*)(bp
+ dccp_basic_hdr_len(dh
));
311 (void)printf("response (service=%d) ",
312 EXTRACT_32BITS(&dhr
->dccph_resp_service
));
317 (void)printf("data ");
320 (void)printf("ack ");
324 case DCCP_PKT_DATAACK
: {
325 (void)printf("dataack ");
329 case DCCP_PKT_CLOSEREQ
:
330 (void)printf("closereq ");
334 (void)printf("close ");
337 case DCCP_PKT_RESET
: {
338 struct dccp_hdr_reset
*dhr
=
339 (struct dccp_hdr_reset
*)(bp
+ dccp_basic_hdr_len(dh
));
341 (void)printf("reset (code=%s) ",
342 dccp_reset_code(dhr
->dccph_reset_code
));
347 (void)printf("sync ");
350 case DCCP_PKT_SYNCACK
:
351 (void)printf("syncack ");
355 (void)printf("invalid ");
359 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
360 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
361 dccp_print_ack_no(bp
);
366 (void)printf("seq %" PRIu64
, dccp_seqno(dh
));
368 /* process options */
369 if (hlen
> dccp_basic_hdr_len(dh
) + extlen
){
372 cp
= bp
+ dccp_basic_hdr_len(dh
) + extlen
;
375 hlen
-= dccp_basic_hdr_len(dh
) + extlen
;
378 optlen
= dccp_print_option(cp
);
379 if (!optlen
) goto trunc2
;
380 if (hlen
<= optlen
) break;
394 static int dccp_print_option(const u_char
*option
)
405 optlen
= *(option
+1);
407 printf("Option %d optlen too short",*option
);
412 TCHECK2(*option
,optlen
);
422 printf("slowreceiver");
426 if (*(option
+2) < 10){
427 printf(" %s", dccp_feature_nums
[*(option
+2)]);
428 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
433 if (*(option
+2) < 10){
434 printf(" %s", dccp_feature_nums
[*(option
+2)]);
435 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
440 if (*(option
+2) < 10){
441 printf(" %s", dccp_feature_nums
[*(option
+2)]);
442 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
447 if (*(option
+2) < 10){
448 printf(" %s", dccp_feature_nums
[*(option
+2)]);
449 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
453 printf("initcookie 0x");
454 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
458 for (i
= 0; i
< optlen
-2; i
++) printf(" %d", *(option
+2 + i
));
461 printf("ack_vector0 0x");
462 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
465 printf("ack_vector1 0x");
466 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
469 printf("data_dropped 0x");
470 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
473 ts
= (u_int32_t
*)(option
+ 2);
474 printf("timestamp %u", (u_int32_t
)ntohl(*ts
));
477 ts
= (u_int32_t
*)(option
+ 2);
478 printf("timestamp_echo %u", (u_int32_t
)ntohl(*ts
));
481 printf("elapsed_time ");
483 ts
= (u_int32_t
*)(option
+ 2);
484 printf("%u", (u_int32_t
)ntohl(*ts
));
486 var16
= (u_int16_t
*)(option
+ 2);
487 printf("%u", ntohs(*var16
));
491 printf("data_checksum ");
492 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
495 if (*option
>= 128) {
496 printf("CCID option %d",*option
);
499 var16
= (u_int16_t
*)(option
+ 2);
500 printf(" %u",ntohs(*var16
));
503 var32
= (u_int32_t
*)(option
+ 2);
504 printf(" %u",(u_int32_t
)ntohl(*var32
));
512 printf("unknown_opt %d", *option
);