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
14 #include <tcpdump-stdinc.h>
19 #include "interface.h"
20 #include "addrtoname.h"
21 #include "extract.h" /* must come after interface.h */
29 * struct dccp_hdr - generic part of DCCP packet header
31 * @dccph_sport - Relevant port on the endpoint that sent this packet
32 * @dccph_dport - Relevant port on the other endpoint
33 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
34 * @dccph_ccval - Used by the HC-Sender CCID
35 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
36 * @dccph_checksum - Internet checksum, depends on dccph_cscov
37 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
38 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
39 * @dccph_seq - sequence number high or low order 24 bits, depends on dccph_x
42 u_int16_t dccph_sport
,
45 u_int8_t dccph_ccval_cscov
;
46 u_int16_t dccph_checksum
;
53 #define DCCPH_CCVAL(dh) (((dh)->dccph_ccval_cscov >> 4) & 0xF)
54 #define DCCPH_CSCOV(dh) (((dh)->dccph_ccval_cscov) & 0xF)
56 #define DCCPH_X(dh) ((dh)->dccph_xtrs.dccph_xtr & 1)
57 #define DCCPH_TYPE(dh) (((dh)->dccph_xtrs.dccph_xtr >> 1) & 0xF)
58 #define DCCPH_SEQ(dh) (((dh)->dccph_xtrs.dccph_seq) >> 8)
61 * struct dccp_hdr_ext - the low bits of a 48 bit seq packet
63 * @dccph_seq_low - low 24 bits of a 48 bit seq packet
66 u_int32_t dccph_seq_low
;
70 * struct dccp_hdr_request - Conection initiation request header
72 * @dccph_req_service - Service to which the client app wants to connect
74 struct dccp_hdr_request
{
75 u_int32_t dccph_req_service
;
79 * struct dccp_hdr_response - Conection initiation response header
81 * @dccph_resp_ack - 48 bit ack number, contains GSR
82 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
84 struct dccp_hdr_response
{
85 u_int8_t dccph_resp_ack
[8]; /* always 8 bytes */
86 u_int32_t dccph_resp_service
;
90 * struct dccp_hdr_reset - Unconditionally shut down a connection
92 * @dccph_resp_ack - 48 bit ack number
93 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
95 struct dccp_hdr_reset
{
96 u_int8_t dccph_reset_ack
[8]; /* always 8 bytes */
97 u_int8_t dccph_reset_code
,
102 DCCP_PKT_REQUEST
= 0,
115 enum dccp_reset_codes
{
116 DCCP_RESET_CODE_UNSPECIFIED
= 0,
117 DCCP_RESET_CODE_CLOSED
,
118 DCCP_RESET_CODE_ABORTED
,
119 DCCP_RESET_CODE_NO_CONNECTION
,
120 DCCP_RESET_CODE_PACKET_ERROR
,
121 DCCP_RESET_CODE_OPTION_ERROR
,
122 DCCP_RESET_CODE_MANDATORY_ERROR
,
123 DCCP_RESET_CODE_CONNECTION_REFUSED
,
124 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
125 DCCP_RESET_CODE_TOO_BUSY
,
126 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
127 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
128 __DCCP_RESET_CODE_LAST
131 static const char tstr
[] = "[|dccp]";
133 static const char *dccp_reset_codes
[] = {
141 "connection_refused",
145 "aggression_penalty",
148 static const char *dccp_feature_nums
[] = {
157 "minimum checksum coverage",
158 "check data checksum",
161 static inline u_int
dccp_csum_coverage(const struct dccp_hdr
* dh
, u_int len
)
165 if (DCCPH_CSCOV(dh
) == 0)
167 cov
= (dh
->dccph_doff
+ DCCPH_CSCOV(dh
) - 1) * sizeof(u_int32_t
);
168 return (cov
> len
)? len
: cov
;
171 static int dccp_cksum(const struct ip
*ip
,
172 const struct dccp_hdr
*dh
, u_int len
)
174 return nextproto4_cksum(ip
, (const u_int8_t
*)(void *)dh
, len
,
175 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
179 static int dccp6_cksum(const struct ip6_hdr
*ip6
, const struct dccp_hdr
*dh
, u_int len
)
181 return nextproto6_cksum(ip6
, (const u_int8_t
*)(void *)dh
,
182 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
186 static const char *dccp_reset_code(u_int8_t code
)
188 if (code
>= __DCCP_RESET_CODE_LAST
)
190 return dccp_reset_codes
[code
];
193 static u_int64_t
dccp_seqno(const struct dccp_hdr
*dh
)
195 u_int32_t seq_high
= DCCPH_SEQ(dh
);
196 u_int64_t seqno
= EXTRACT_24BITS(&seq_high
) & 0xFFFFFF;
198 if (DCCPH_X(dh
) != 0) {
199 const struct dccp_hdr_ext
*dhx
= (void *)(dh
+ 1);
200 u_int32_t seq_low
= dhx
->dccph_seq_low
;
201 seqno
&= 0x00FFFF; /* clear reserved field */
202 seqno
= (seqno
<< 32) + EXTRACT_32BITS(&seq_low
);
208 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
210 return sizeof(*dh
) + (DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : 0);
213 static void dccp_print_ack_no(const u_char
*bp
)
215 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
216 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(dh
);
219 if (DCCPH_X(dh
) != 0) {
221 ackno
= EXTRACT_48BITS(ackp
+ 2);
224 ackno
= EXTRACT_24BITS(ackp
+ 1);
227 (void)printf("(ack=%" PRIu64
") ", ackno
);
232 static int dccp_print_option(const u_char
*option
);
235 * dccp_print - show dccp packet
236 * @bp - beginning of dccp packet
237 * @data2 - beginning of enclosing
238 * @len - lenght of ip packet
240 void dccp_print(const u_char
*bp
, const u_char
*data2
, u_int len
)
242 const struct dccp_hdr
*dh
;
245 const struct ip6_hdr
*ip6
;
248 u_short sport
, dport
;
252 dh
= (const struct dccp_hdr
*)bp
;
254 ip
= (struct ip
*)data2
;
257 ip6
= (const struct ip6_hdr
*)data2
;
261 cp
= (const u_char
*)(dh
+ 1);
263 printf("[Invalid packet|dccp]");
267 if (len
< sizeof(struct dccp_hdr
)) {
268 printf("truncated-dccp - %ld bytes missing!",
269 (long)len
- sizeof(struct dccp_hdr
));
273 sport
= EXTRACT_16BITS(&dh
->dccph_sport
);
274 dport
= EXTRACT_16BITS(&dh
->dccph_dport
);
275 hlen
= dh
->dccph_doff
* 4;
279 (void)printf("%s.%d > %s.%d: ",
280 ip6addr_string(&ip6
->ip6_src
), sport
,
281 ip6addr_string(&ip6
->ip6_dst
), dport
);
285 (void)printf("%s.%d > %s.%d: ",
286 ipaddr_string(&ip
->ip_src
), sport
,
287 ipaddr_string(&ip
->ip_dst
), dport
);
292 (void)printf(" %d", len
- hlen
);
294 (void)printf("dccp [bad hdr length %u - too long, > %u]",
300 /* other variables in generic header */
302 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
305 /* checksum calculation */
306 if (vflag
&& TTEST2(bp
[0], len
)) {
307 u_int16_t sum
= 0, dccp_sum
;
309 dccp_sum
= EXTRACT_16BITS(&dh
->dccph_checksum
);
310 (void)printf("cksum 0x%04x ", dccp_sum
);
312 sum
= dccp_cksum(ip
, dh
, len
);
314 else if (IP_V(ip
) == 6)
315 sum
= dccp6_cksum(ip6
, dh
, len
);
318 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum
, sum
));
320 (void)printf("(correct), ");
323 switch (DCCPH_TYPE(dh
)) {
324 case DCCP_PKT_REQUEST
: {
325 struct dccp_hdr_request
*dhr
=
326 (struct dccp_hdr_request
*)(bp
+ dccp_basic_hdr_len(dh
));
328 (void)printf("request (service=%d) ",
329 EXTRACT_32BITS(&dhr
->dccph_req_service
));
333 case DCCP_PKT_RESPONSE
: {
334 struct dccp_hdr_response
*dhr
=
335 (struct dccp_hdr_response
*)(bp
+ dccp_basic_hdr_len(dh
));
337 (void)printf("response (service=%d) ",
338 EXTRACT_32BITS(&dhr
->dccph_resp_service
));
343 (void)printf("data ");
346 (void)printf("ack ");
350 case DCCP_PKT_DATAACK
: {
351 (void)printf("dataack ");
355 case DCCP_PKT_CLOSEREQ
:
356 (void)printf("closereq ");
360 (void)printf("close ");
363 case DCCP_PKT_RESET
: {
364 struct dccp_hdr_reset
*dhr
=
365 (struct dccp_hdr_reset
*)(bp
+ dccp_basic_hdr_len(dh
));
367 (void)printf("reset (code=%s) ",
368 dccp_reset_code(dhr
->dccph_reset_code
));
373 (void)printf("sync ");
376 case DCCP_PKT_SYNCACK
:
377 (void)printf("syncack ");
381 (void)printf("invalid ");
385 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
386 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
387 dccp_print_ack_no(bp
);
392 (void)printf("seq %" PRIu64
, dccp_seqno(dh
));
394 /* process options */
395 if (hlen
> dccp_basic_hdr_len(dh
) + extlen
){
398 cp
= bp
+ dccp_basic_hdr_len(dh
) + extlen
;
401 hlen
-= dccp_basic_hdr_len(dh
) + extlen
;
404 optlen
= dccp_print_option(cp
);
405 if (!optlen
) goto trunc2
;
406 if (hlen
<= optlen
) break;
420 static int dccp_print_option(const u_char
*option
)
428 optlen
= *(option
+1);
430 printf("Option %d optlen too short",*option
);
435 TCHECK2(*option
,optlen
);
445 printf("slowreceiver");
449 if (*(option
+2) < 10){
450 printf(" %s", dccp_feature_nums
[*(option
+2)]);
451 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
456 if (*(option
+2) < 10){
457 printf(" %s", dccp_feature_nums
[*(option
+2)]);
458 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
463 if (*(option
+2) < 10){
464 printf(" %s", dccp_feature_nums
[*(option
+2)]);
465 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
470 if (*(option
+2) < 10){
471 printf(" %s", dccp_feature_nums
[*(option
+2)]);
472 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
476 printf("initcookie 0x");
477 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
481 for (i
= 0; i
< optlen
-2; i
++) printf(" %d", *(option
+2 + i
));
484 printf("ack_vector0 0x");
485 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
488 printf("ack_vector1 0x");
489 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
492 printf("data_dropped 0x");
493 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
496 printf("timestamp %u", EXTRACT_32BITS(option
+ 2));
499 printf("timestamp_echo %u", EXTRACT_32BITS(option
+ 2));
502 printf("elapsed_time ");
504 printf("%u", EXTRACT_32BITS(option
+ 2));
506 printf("%u", EXTRACT_16BITS(option
+ 2));
509 printf("data_checksum ");
510 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
513 if (*option
>= 128) {
514 printf("CCID option %d",*option
);
517 printf(" %u", EXTRACT_16BITS(option
+ 2));
520 printf(" %u", EXTRACT_32BITS(option
+ 2));
528 printf("unknown_opt %d", *option
);