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
10 /* \summary: Datagram Congestion Control Protocol (DCCP) printer */
16 #include <netdissect-stdinc.h>
21 #include "netdissect.h"
22 #include "addrtoname.h"
28 /* RFC4340: Datagram Congestion Control Protocol (DCCP) */
31 * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
34 * @dccph_sport - Relevant port on the endpoint that sent this packet
35 * @dccph_dport - Relevant port on the other endpoint
36 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
37 * @dccph_ccval - Used by the HC-Sender CCID
38 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
39 * @dccph_checksum - Internet checksum, depends on dccph_cscov
40 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
41 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
42 * @dccph_seq - 24-bit sequence number
48 uint8_t dccph_ccval_cscov
;
49 uint16_t dccph_checksum
;
55 * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
58 * @dccph_sport - Relevant port on the endpoint that sent this packet
59 * @dccph_dport - Relevant port on the other endpoint
60 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
61 * @dccph_ccval - Used by the HC-Sender CCID
62 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
63 * @dccph_checksum - Internet checksum, depends on dccph_cscov
64 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
65 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
66 * @dccph_seq - 48-bit sequence number
72 uint8_t dccph_ccval_cscov
;
73 uint16_t dccph_checksum
;
79 #define DCCPH_CCVAL(dh) (((dh)->dccph_ccval_cscov >> 4) & 0xF)
80 #define DCCPH_CSCOV(dh) (((dh)->dccph_ccval_cscov) & 0xF)
82 #define DCCPH_X(dh) ((dh)->dccph_xtr & 1)
83 #define DCCPH_TYPE(dh) (((dh)->dccph_xtr >> 1) & 0xF)
86 * struct dccp_hdr_request - Conection initiation request header
88 * @dccph_req_service - Service to which the client app wants to connect
90 struct dccp_hdr_request
{
91 uint32_t dccph_req_service
;
95 * struct dccp_hdr_response - Conection initiation response header
97 * @dccph_resp_ack - 48 bit ack number, contains GSR
98 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
100 struct dccp_hdr_response
{
101 uint8_t dccph_resp_ack
[8]; /* always 8 bytes */
102 uint32_t dccph_resp_service
;
106 * struct dccp_hdr_reset - Unconditionally shut down a connection
108 * @dccph_resp_ack - 48 bit ack number
109 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
111 struct dccp_hdr_reset
{
112 uint8_t dccph_reset_ack
[8]; /* always 8 bytes */
113 uint8_t dccph_reset_code
,
118 DCCP_PKT_REQUEST
= 0,
130 static const struct tok dccp_pkt_type_str
[] = {
131 { DCCP_PKT_REQUEST
, "DCCP-Request" },
132 { DCCP_PKT_RESPONSE
, "DCCP-Response" },
133 { DCCP_PKT_DATA
, "DCCP-Data" },
134 { DCCP_PKT_ACK
, "DCCP-Ack" },
135 { DCCP_PKT_DATAACK
, "DCCP-DataAck" },
136 { DCCP_PKT_CLOSEREQ
, "DCCP-CloseReq" },
137 { DCCP_PKT_CLOSE
, "DCCP-Close" },
138 { DCCP_PKT_RESET
, "DCCP-Reset" },
139 { DCCP_PKT_SYNC
, "DCCP-Sync" },
140 { DCCP_PKT_SYNCACK
, "DCCP-SyncAck" },
144 enum dccp_reset_codes
{
145 DCCP_RESET_CODE_UNSPECIFIED
= 0,
146 DCCP_RESET_CODE_CLOSED
,
147 DCCP_RESET_CODE_ABORTED
,
148 DCCP_RESET_CODE_NO_CONNECTION
,
149 DCCP_RESET_CODE_PACKET_ERROR
,
150 DCCP_RESET_CODE_OPTION_ERROR
,
151 DCCP_RESET_CODE_MANDATORY_ERROR
,
152 DCCP_RESET_CODE_CONNECTION_REFUSED
,
153 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
154 DCCP_RESET_CODE_TOO_BUSY
,
155 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
156 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
157 __DCCP_RESET_CODE_LAST
160 static const char tstr
[] = "[|dccp]";
162 static const char *dccp_reset_codes
[] = {
170 "connection_refused",
174 "aggression_penalty",
177 static const char *dccp_feature_nums
[] = {
186 "minimum checksum coverage",
187 "check data checksum",
190 static inline u_int
dccp_csum_coverage(const struct dccp_hdr
* dh
, u_int len
)
194 if (DCCPH_CSCOV(dh
) == 0)
196 cov
= (dh
->dccph_doff
+ DCCPH_CSCOV(dh
) - 1) * sizeof(uint32_t);
197 return (cov
> len
)? len
: cov
;
200 static int dccp_cksum(netdissect_options
*ndo
, const struct ip
*ip
,
201 const struct dccp_hdr
*dh
, u_int len
)
203 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)(const void *)dh
, len
,
204 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
207 static int dccp6_cksum(netdissect_options
*ndo
, const struct ip6_hdr
*ip6
,
208 const struct dccp_hdr
*dh
, u_int len
)
210 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)(const void *)dh
, len
,
211 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
214 static const char *dccp_reset_code(uint8_t code
)
216 if (code
>= __DCCP_RESET_CODE_LAST
)
218 return dccp_reset_codes
[code
];
221 static uint64_t dccp_seqno(const u_char
*bp
)
223 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
226 if (DCCPH_X(dh
) != 0) {
227 const struct dccp_hdr_ext
*dhx
= (const struct dccp_hdr_ext
*)bp
;
228 seqno
= EXTRACT_BE_U_6(dhx
->dccph_seq
);
230 seqno
= EXTRACT_BE_U_3(dh
->dccph_seq
);
236 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
238 return DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : sizeof(struct dccp_hdr
);
241 static void dccp_print_ack_no(netdissect_options
*ndo
, const u_char
*bp
)
243 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
244 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(dh
);
247 if (DCCPH_X(dh
) != 0) {
249 ackno
= EXTRACT_BE_U_6(ackp
+ 2);
252 ackno
= EXTRACT_BE_U_3(ackp
+ 1);
255 ND_PRINT((ndo
, "(ack=%" PRIu64
") ", ackno
));
260 static int dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
263 * dccp_print - show dccp packet
264 * @bp - beginning of dccp packet
265 * @data2 - beginning of enclosing
266 * @len - lenght of ip packet
269 dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
272 const struct dccp_hdr
*dh
;
274 const struct ip6_hdr
*ip6
;
276 u_short sport
, dport
;
281 dh
= (const struct dccp_hdr
*)bp
;
283 ip
= (const struct ip
*)data2
;
285 ip6
= (const struct ip6_hdr
*)data2
;
289 /* make sure we have enough data to look at the X bit */
290 cp
= (const u_char
*)(dh
+ 1);
291 if (cp
> ndo
->ndo_snapend
) {
292 ND_PRINT((ndo
, "[Invalid packet|dccp]"));
295 if (len
< sizeof(struct dccp_hdr
)) {
296 ND_PRINT((ndo
, "truncated-dccp - %u bytes missing!",
297 len
- (u_int
)sizeof(struct dccp_hdr
)));
301 /* get the length of the generic header */
302 fixed_hdrlen
= dccp_basic_hdr_len(dh
);
303 if (len
< fixed_hdrlen
) {
304 ND_PRINT((ndo
, "truncated-dccp - %u bytes missing!",
305 len
- fixed_hdrlen
));
308 ND_TCHECK2(*dh
, fixed_hdrlen
);
310 sport
= EXTRACT_BE_U_2(&dh
->dccph_sport
);
311 dport
= EXTRACT_BE_U_2(&dh
->dccph_dport
);
312 hlen
= dh
->dccph_doff
* 4;
315 ND_PRINT((ndo
, "%s.%d > %s.%d: ",
316 ip6addr_string(ndo
, &ip6
->ip6_src
), sport
,
317 ip6addr_string(ndo
, &ip6
->ip6_dst
), dport
));
319 ND_PRINT((ndo
, "%s.%d > %s.%d: ",
320 ipaddr_string(ndo
, &ip
->ip_src
), sport
,
321 ipaddr_string(ndo
, &ip
->ip_dst
), dport
));
324 ND_PRINT((ndo
, "DCCP"));
326 if (ndo
->ndo_qflag
) {
327 ND_PRINT((ndo
, " %d", len
- hlen
));
329 ND_PRINT((ndo
, " [bad hdr length %u - too long, > %u]",
335 /* other variables in generic header */
336 if (ndo
->ndo_vflag
) {
337 ND_PRINT((ndo
, " (CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
)));
340 /* checksum calculation */
341 if (ndo
->ndo_vflag
&& ND_TTEST2(bp
[0], len
)) {
342 uint16_t sum
= 0, dccp_sum
;
344 dccp_sum
= EXTRACT_BE_U_2(&dh
->dccph_checksum
);
345 ND_PRINT((ndo
, "cksum 0x%04x ", dccp_sum
));
347 sum
= dccp_cksum(ndo
, ip
, dh
, len
);
348 else if (IP_V(ip
) == 6)
349 sum
= dccp6_cksum(ndo
, ip6
, dh
, len
);
351 ND_PRINT((ndo
, "(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum
, sum
)));
353 ND_PRINT((ndo
, "(correct)"));
357 ND_PRINT((ndo
, ")"));
358 ND_PRINT((ndo
, " "));
360 dccph_type
= DCCPH_TYPE(dh
);
361 switch (dccph_type
) {
362 case DCCP_PKT_REQUEST
: {
363 const struct dccp_hdr_request
*dhr
=
364 (const struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
366 if (len
< fixed_hdrlen
) {
367 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
368 tok2str(dccp_pkt_type_str
, "", dccph_type
),
369 len
- fixed_hdrlen
));
373 ND_PRINT((ndo
, "%s (service=%d) ",
374 tok2str(dccp_pkt_type_str
, "", dccph_type
),
375 EXTRACT_BE_U_4(&dhr
->dccph_req_service
)));
378 case DCCP_PKT_RESPONSE
: {
379 const struct dccp_hdr_response
*dhr
=
380 (const struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
382 if (len
< fixed_hdrlen
) {
383 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
384 tok2str(dccp_pkt_type_str
, "", dccph_type
),
385 len
- fixed_hdrlen
));
389 ND_PRINT((ndo
, "%s (service=%d) ",
390 tok2str(dccp_pkt_type_str
, "", dccph_type
),
391 EXTRACT_BE_U_4(&dhr
->dccph_resp_service
)));
395 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
399 if (len
< fixed_hdrlen
) {
400 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
401 tok2str(dccp_pkt_type_str
, "", dccph_type
),
402 len
- fixed_hdrlen
));
405 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
408 case DCCP_PKT_DATAACK
: {
410 if (len
< fixed_hdrlen
) {
411 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
412 tok2str(dccp_pkt_type_str
, "", dccph_type
),
413 len
- fixed_hdrlen
));
416 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
419 case DCCP_PKT_CLOSEREQ
:
421 if (len
< fixed_hdrlen
) {
422 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
423 tok2str(dccp_pkt_type_str
, "", dccph_type
),
424 len
- fixed_hdrlen
));
427 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
431 if (len
< fixed_hdrlen
) {
432 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
433 tok2str(dccp_pkt_type_str
, "", dccph_type
),
434 len
- fixed_hdrlen
));
437 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
439 case DCCP_PKT_RESET
: {
440 const struct dccp_hdr_reset
*dhr
=
441 (const struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
443 if (len
< fixed_hdrlen
) {
444 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
445 tok2str(dccp_pkt_type_str
, "", dccph_type
),
446 len
- fixed_hdrlen
));
450 ND_PRINT((ndo
, "%s (code=%s) ",
451 tok2str(dccp_pkt_type_str
, "", dccph_type
),
452 dccp_reset_code(dhr
->dccph_reset_code
)));
457 if (len
< fixed_hdrlen
) {
458 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
459 tok2str(dccp_pkt_type_str
, "", dccph_type
),
460 len
- fixed_hdrlen
));
463 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
465 case DCCP_PKT_SYNCACK
:
467 if (len
< fixed_hdrlen
) {
468 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
469 tok2str(dccp_pkt_type_str
, "", dccph_type
),
470 len
- fixed_hdrlen
));
473 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
476 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "unknown-type-%u", dccph_type
)));
480 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
481 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
482 dccp_print_ack_no(ndo
, bp
);
484 if (ndo
->ndo_vflag
< 2)
487 ND_PRINT((ndo
, "seq %" PRIu64
, dccp_seqno(bp
)));
489 /* process options */
490 if (hlen
> fixed_hdrlen
){
492 cp
= bp
+ fixed_hdrlen
;
493 ND_PRINT((ndo
, " <"));
495 hlen
-= fixed_hdrlen
;
497 optlen
= dccp_print_option(ndo
, cp
, hlen
);
504 ND_PRINT((ndo
, ", "));
506 ND_PRINT((ndo
, ">"));
510 ND_PRINT((ndo
, "%s", tstr
));
514 static const struct tok dccp_option_values
[] = {
517 { 2, "slowreceiver" },
522 { 36, "initcookie" },
524 { 38, "ack_vector0" },
525 { 39, "ack_vector1" },
526 { 40, "data_dropped" },
528 { 42, "timestamp_echo" },
529 { 43, "elapsed_time" },
530 { 44, "data_checksum" },
534 static int dccp_print_option(netdissect_options
*ndo
, const u_char
*option
, u_int hlen
)
541 ND_TCHECK(*(option
+1));
542 optlen
= EXTRACT_U_1(option
+ 1);
545 ND_PRINT((ndo
, "CCID option %u optlen too short", EXTRACT_U_1(option
)));
547 ND_PRINT((ndo
, "%s optlen too short",
548 tok2str(dccp_option_values
, "Option %u", EXTRACT_U_1(option
))));
556 ND_PRINT((ndo
, "CCID option %u optlen goes past header length",
557 EXTRACT_U_1(option
)));
559 ND_PRINT((ndo
, "%s optlen goes past header length",
560 tok2str(dccp_option_values
, "Option %u", EXTRACT_U_1(option
))));
563 ND_TCHECK2(*option
, optlen
);
565 if (*option
>= 128) {
566 ND_PRINT((ndo
, "CCID option %d", EXTRACT_U_1(option
)));
569 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_2(option
+ 2)));
572 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(option
+ 2)));
578 ND_PRINT((ndo
, "%s", tok2str(dccp_option_values
, "Option %u", EXTRACT_U_1(option
))));
579 switch (EXTRACT_U_1(option
)) {
585 ND_PRINT((ndo
, " optlen too short"));
588 if (*(option
+ 2) < 10){
589 ND_PRINT((ndo
, " %s", dccp_feature_nums
[EXTRACT_U_1(option
+ 2)]));
590 for (i
= 0; i
< optlen
- 3; i
++)
591 ND_PRINT((ndo
, " %d", EXTRACT_U_1(option
+ 3 + i
)));
596 ND_PRINT((ndo
, " 0x"));
597 for (i
= 0; i
< optlen
- 2; i
++)
598 ND_PRINT((ndo
, "%02x", EXTRACT_U_1(option
+ 2 + i
)));
602 for (i
= 0; i
< optlen
- 2; i
++)
603 ND_PRINT((ndo
, " %d", EXTRACT_U_1(option
+ 2 + i
)));
607 ND_PRINT((ndo
, " 0x"));
608 for (i
= 0; i
< optlen
- 2; i
++)
609 ND_PRINT((ndo
, "%02x", EXTRACT_U_1(option
+ 2 + i
)));
614 ND_PRINT((ndo
, " 0x"));
615 for (i
= 0; i
< optlen
- 2; i
++)
616 ND_PRINT((ndo
, "%02x", EXTRACT_U_1(option
+ 2 + i
)));
621 ND_PRINT((ndo
, " 0x"));
622 for (i
= 0; i
< optlen
- 2; i
++)
623 ND_PRINT((ndo
, "%02x", EXTRACT_U_1(option
+ 2 + i
)));
628 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(option
+ 2)));
630 ND_PRINT((ndo
, " optlen != 4"));
634 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(option
+ 2)));
636 ND_PRINT((ndo
, " optlen != 4"));
640 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_4(option
+ 2)));
641 else if (optlen
== 4)
642 ND_PRINT((ndo
, " %u", EXTRACT_BE_U_2(option
+ 2)));
644 ND_PRINT((ndo
, " optlen != 4 or 6"));
648 ND_PRINT((ndo
, " "));
649 for (i
= 0; i
< optlen
- 2; i
++)
650 ND_PRINT((ndo
, "%02x", EXTRACT_U_1(option
+ 2 + i
)));
658 ND_PRINT((ndo
, "%s", tstr
));