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 */
12 /* specification: RFC 4340 */
16 #include "netdissect-stdinc.h"
18 #include "netdissect.h"
19 #include "addrtoname.h"
25 /* RFC4340: Datagram Congestion Control Protocol (DCCP) */
28 * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
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 - 24-bit sequence number
42 nd_uint16_t dccph_sport
,
44 nd_uint8_t dccph_doff
;
45 nd_uint8_t dccph_ccval_cscov
;
46 nd_uint16_t dccph_checksum
;
48 nd_uint24_t dccph_seq
;
52 * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
55 * @dccph_sport - Relevant port on the endpoint that sent this packet
56 * @dccph_dport - Relevant port on the other endpoint
57 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
58 * @dccph_ccval - Used by the HC-Sender CCID
59 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
60 * @dccph_checksum - Internet checksum, depends on dccph_cscov
61 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
62 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
63 * @dccph_seq - 48-bit sequence number
66 nd_uint16_t dccph_sport
,
68 nd_uint8_t dccph_doff
;
69 nd_uint8_t dccph_ccval_cscov
;
70 nd_uint16_t dccph_checksum
;
73 nd_uint48_t dccph_seq
;
76 #define DCCPH_CCVAL(dh) ((GET_U_1((dh)->dccph_ccval_cscov) >> 4) & 0xF)
77 #define DCCPH_CSCOV(dh) (GET_U_1((dh)->dccph_ccval_cscov) & 0xF)
79 #define DCCPH_X(dh) (GET_U_1((dh)->dccph_xtr) & 1)
80 #define DCCPH_TYPE(dh) ((GET_U_1((dh)->dccph_xtr) >> 1) & 0xF)
83 * struct dccp_hdr_request - Connection initiation request header
85 * @dccph_req_service - Service to which the client app wants to connect
87 struct dccp_hdr_request
{
88 nd_uint32_t dccph_req_service
;
92 * struct dccp_hdr_response - Connection initiation response header
94 * @dccph_resp_ack - 48 bit ack number, contains GSR
95 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
97 struct dccp_hdr_response
{
98 nd_uint64_t dccph_resp_ack
; /* always 8 bytes, first 2 reserved */
99 nd_uint32_t dccph_resp_service
;
103 * struct dccp_hdr_reset - Unconditionally shut down a connection
105 * @dccph_resp_ack - 48 bit ack number
106 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
108 struct dccp_hdr_reset
{
109 nd_uint64_t dccph_reset_ack
; /* always 8 bytes, first 2 reserved */
110 nd_uint8_t dccph_reset_code
;
111 nd_uint8_t dccph_reset_data1
;
112 nd_uint8_t dccph_reset_data2
;
113 nd_uint8_t dccph_reset_data3
;
117 DCCP_PKT_REQUEST
= 0,
129 static const struct tok dccp_pkt_type_str
[] = {
130 { DCCP_PKT_REQUEST
, "DCCP-Request" },
131 { DCCP_PKT_RESPONSE
, "DCCP-Response" },
132 { DCCP_PKT_DATA
, "DCCP-Data" },
133 { DCCP_PKT_ACK
, "DCCP-Ack" },
134 { DCCP_PKT_DATAACK
, "DCCP-DataAck" },
135 { DCCP_PKT_CLOSEREQ
, "DCCP-CloseReq" },
136 { DCCP_PKT_CLOSE
, "DCCP-Close" },
137 { DCCP_PKT_RESET
, "DCCP-Reset" },
138 { DCCP_PKT_SYNC
, "DCCP-Sync" },
139 { DCCP_PKT_SYNCACK
, "DCCP-SyncAck" },
143 enum dccp_reset_codes
{
144 DCCP_RESET_CODE_UNSPECIFIED
= 0,
145 DCCP_RESET_CODE_CLOSED
,
146 DCCP_RESET_CODE_ABORTED
,
147 DCCP_RESET_CODE_NO_CONNECTION
,
148 DCCP_RESET_CODE_PACKET_ERROR
,
149 DCCP_RESET_CODE_OPTION_ERROR
,
150 DCCP_RESET_CODE_MANDATORY_ERROR
,
151 DCCP_RESET_CODE_CONNECTION_REFUSED
,
152 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
153 DCCP_RESET_CODE_TOO_BUSY
,
154 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
155 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
156 __DCCP_RESET_CODE_LAST
160 static const char *dccp_reset_codes
[] = {
168 "connection_refused",
172 "aggression_penalty",
175 static const char *dccp_feature_nums
[] = {
184 "minimum checksum coverage",
185 "check data checksum",
189 dccp_csum_coverage(netdissect_options
*ndo
,
190 const struct dccp_hdr
*dh
, u_int len
)
194 if (DCCPH_CSCOV(dh
) == 0)
196 cov
= (GET_U_1(dh
->dccph_doff
) + DCCPH_CSCOV(dh
) - 1) * sizeof(uint32_t);
197 return (cov
> len
)? len
: cov
;
200 static uint16_t 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(ndo
, dh
, len
), IPPROTO_DCCP
);
207 static uint16_t 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(ndo
, 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
];
222 dccp_seqno(netdissect_options
*ndo
, const u_char
*bp
)
224 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
227 if (DCCPH_X(dh
) != 0) {
228 const struct dccp_hdr_ext
*dhx
= (const struct dccp_hdr_ext
*)bp
;
229 seqno
= GET_BE_U_6(dhx
->dccph_seq
);
231 seqno
= GET_BE_U_3(dh
->dccph_seq
);
238 dccp_basic_hdr_len(netdissect_options
*ndo
, const struct dccp_hdr
*dh
)
240 return DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : sizeof(struct dccp_hdr
);
243 static void dccp_print_ack_no(netdissect_options
*ndo
, const u_char
*bp
)
245 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
246 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(ndo
, dh
);
249 if (DCCPH_X(dh
) != 0) {
250 ackno
= GET_BE_U_6(ackp
+ 2);
252 ackno
= GET_BE_U_3(ackp
+ 1);
255 ND_PRINT("(ack=%" PRIu64
") ", ackno
);
258 static u_int
dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
261 * dccp_print - show dccp packet
262 * @bp - beginning of dccp packet
263 * @data2 - beginning of enclosing
264 * @len - length of ip packet
267 dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
270 const struct dccp_hdr
*dh
;
272 const struct ip6_hdr
*ip6
;
274 u_short sport
, dport
;
279 ndo
->ndo_protocol
= "dccp";
280 dh
= (const struct dccp_hdr
*)bp
;
282 ip
= (const struct ip
*)data2
;
284 ip6
= (const struct ip6_hdr
*)data2
;
288 /* make sure we have enough data to look at the X bit */
289 cp
= (const u_char
*)(dh
+ 1);
290 if (cp
> ndo
->ndo_snapend
)
292 if (len
< sizeof(struct dccp_hdr
)) {
293 ND_PRINT("truncated-dccp - %zu bytes missing!",
294 sizeof(struct dccp_hdr
) - len
);
298 /* get the length of the generic header */
299 fixed_hdrlen
= dccp_basic_hdr_len(ndo
, dh
);
300 if (len
< fixed_hdrlen
) {
301 ND_PRINT("truncated-dccp - %u bytes missing!",
305 ND_TCHECK_LEN(dh
, fixed_hdrlen
);
307 sport
= GET_BE_U_2(dh
->dccph_sport
);
308 dport
= GET_BE_U_2(dh
->dccph_dport
);
309 hlen
= GET_U_1(dh
->dccph_doff
) * 4;
312 ND_PRINT("%s.%u > %s.%u: ",
313 GET_IP6ADDR_STRING(ip6
->ip6_src
), sport
,
314 GET_IP6ADDR_STRING(ip6
->ip6_dst
), dport
);
316 ND_PRINT("%s.%u > %s.%u: ",
317 GET_IPADDR_STRING(ip
->ip_src
), sport
,
318 GET_IPADDR_STRING(ip
->ip_dst
), dport
);
321 nd_print_protocol_caps(ndo
);
323 if (ndo
->ndo_qflag
) {
324 ND_PRINT(" %u", len
- hlen
);
326 ND_PRINT(" [bad hdr length %u - too long, > %u]",
332 /* other variables in generic header */
333 if (ndo
->ndo_vflag
) {
334 ND_PRINT(" (CCVal %u, CsCov %u", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
337 /* checksum calculation */
338 if (ndo
->ndo_vflag
&& ND_TTEST_LEN(bp
, len
)) {
339 uint16_t sum
= 0, dccp_sum
;
341 dccp_sum
= GET_BE_U_2(dh
->dccph_checksum
);
342 ND_PRINT(", cksum 0x%04x ", dccp_sum
);
344 sum
= dccp_cksum(ndo
, ip
, dh
, len
);
345 else if (IP_V(ip
) == 6)
346 sum
= dccp6_cksum(ndo
, ip6
, dh
, len
);
348 ND_PRINT("(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum
, sum
));
350 ND_PRINT("(correct)");
357 dccph_type
= DCCPH_TYPE(dh
);
358 switch (dccph_type
) {
359 case DCCP_PKT_REQUEST
: {
360 const struct dccp_hdr_request
*dhr
=
361 (const struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
363 if (len
< fixed_hdrlen
) {
364 ND_PRINT("truncated-%s - %u bytes missing!",
365 tok2str(dccp_pkt_type_str
, "", dccph_type
),
370 ND_PRINT("%s (service=%u) ",
371 tok2str(dccp_pkt_type_str
, "", dccph_type
),
372 GET_BE_U_4(dhr
->dccph_req_service
));
375 case DCCP_PKT_RESPONSE
: {
376 const struct dccp_hdr_response
*dhr
=
377 (const struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
379 if (len
< fixed_hdrlen
) {
380 ND_PRINT("truncated-%s - %u bytes missing!",
381 tok2str(dccp_pkt_type_str
, "", dccph_type
),
386 ND_PRINT("%s (service=%u) ",
387 tok2str(dccp_pkt_type_str
, "", dccph_type
),
388 GET_BE_U_4(dhr
->dccph_resp_service
));
392 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
396 if (len
< fixed_hdrlen
) {
397 ND_PRINT("truncated-%s - %u bytes missing!",
398 tok2str(dccp_pkt_type_str
, "", dccph_type
),
402 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
405 case DCCP_PKT_DATAACK
: {
407 if (len
< fixed_hdrlen
) {
408 ND_PRINT("truncated-%s - %u bytes missing!",
409 tok2str(dccp_pkt_type_str
, "", dccph_type
),
413 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
416 case DCCP_PKT_CLOSEREQ
:
418 if (len
< fixed_hdrlen
) {
419 ND_PRINT("truncated-%s - %u bytes missing!",
420 tok2str(dccp_pkt_type_str
, "", dccph_type
),
424 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
428 if (len
< fixed_hdrlen
) {
429 ND_PRINT("truncated-%s - %u bytes missing!",
430 tok2str(dccp_pkt_type_str
, "", dccph_type
),
434 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
436 case DCCP_PKT_RESET
: {
437 const struct dccp_hdr_reset
*dhr
=
438 (const struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
440 if (len
< fixed_hdrlen
) {
441 ND_PRINT("truncated-%s - %u bytes missing!",
442 tok2str(dccp_pkt_type_str
, "", dccph_type
),
447 ND_PRINT("%s (code=%s) ",
448 tok2str(dccp_pkt_type_str
, "", dccph_type
),
449 dccp_reset_code(GET_U_1(dhr
->dccph_reset_code
)));
454 if (len
< fixed_hdrlen
) {
455 ND_PRINT("truncated-%s - %u bytes missing!",
456 tok2str(dccp_pkt_type_str
, "", dccph_type
),
460 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
462 case DCCP_PKT_SYNCACK
:
464 if (len
< fixed_hdrlen
) {
465 ND_PRINT("truncated-%s - %u bytes missing!",
466 tok2str(dccp_pkt_type_str
, "", dccph_type
),
470 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
473 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "unknown-type-%u", dccph_type
));
477 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
478 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
479 dccp_print_ack_no(ndo
, bp
);
481 if (ndo
->ndo_vflag
< 2)
484 ND_PRINT("seq %" PRIu64
, dccp_seqno(ndo
, bp
));
486 /* process options */
487 if (hlen
> fixed_hdrlen
){
489 cp
= bp
+ fixed_hdrlen
;
492 hlen
-= fixed_hdrlen
;
494 optlen
= dccp_print_option(ndo
, cp
, hlen
);
510 static const struct tok dccp_option_values
[] = {
513 { 2, "slowreceiver" },
518 { 36, "initcookie" },
520 { 38, "ack_vector0" },
521 { 39, "ack_vector1" },
522 { 40, "data_dropped" },
524 { 42, "timestamp_echo" },
525 { 43, "elapsed_time" },
526 { 44, "data_checksum" },
531 dccp_print_option(netdissect_options
*ndo
, const u_char
*option
, u_int hlen
)
535 if (GET_U_1(option
) >= 32) {
536 optlen
= GET_U_1(option
+ 1);
538 if (GET_U_1(option
) >= 128)
539 ND_PRINT("CCID option %u optlen too short",
542 ND_PRINT("%s optlen too short",
543 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
550 if (GET_U_1(option
) >= 128)
551 ND_PRINT("CCID option %u optlen goes past header length",
554 ND_PRINT("%s optlen goes past header length",
555 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
558 ND_TCHECK_LEN(option
, optlen
);
560 if (GET_U_1(option
) >= 128) {
561 ND_PRINT("CCID option %u", GET_U_1(option
));
564 ND_PRINT(" %u", GET_BE_U_2(option
+ 2));
567 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
574 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
575 switch (GET_U_1(option
)) {
581 ND_PRINT(" optlen too short");
584 if (GET_U_1(option
+ 2) < 10){
586 dccp_feature_nums
[GET_U_1(option
+ 2)]);
587 for (i
= 0; i
< optlen
- 3; i
++)
589 GET_U_1(option
+ 3 + i
));
595 for (i
= 0; i
< optlen
- 2; i
++)
597 GET_U_1(option
+ 2 + i
));
601 for (i
= 0; i
< optlen
- 2; i
++)
602 ND_PRINT(" %u", GET_U_1(option
+ 2 + i
));
607 for (i
= 0; i
< optlen
- 2; i
++)
609 GET_U_1(option
+ 2 + i
));
615 for (i
= 0; i
< optlen
- 2; i
++)
617 GET_U_1(option
+ 2 + i
));
623 for (i
= 0; i
< optlen
- 2; i
++)
625 GET_U_1(option
+ 2 + i
));
630 * 13.1. Timestamp Option
632 * +--------+--------+--------+--------+--------+--------+
633 * |00101001|00000110| Timestamp Value |
634 * +--------+--------+--------+--------+--------+--------+
638 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
640 ND_PRINT(" [optlen != 6]");
644 * 13.3. Timestamp Echo Option
646 * +--------+--------+--------+--------+--------+--------+
647 * |00101010|00000110| Timestamp Echo |
648 * +--------+--------+--------+--------+--------+--------+
651 * +--------+--------+------- ... -------+--------+--------+
652 * |00101010|00001000| Timestamp Echo | Elapsed Time |
653 * +--------+--------+------- ... -------+--------+--------+
654 * Type=42 Len=8 (4 bytes)
656 * +--------+--------+------- ... -------+------- ... -------+
657 * |00101010|00001010| Timestamp Echo | Elapsed Time |
658 * +--------+--------+------- ... -------+------- ... -------+
659 * Type=42 Len=10 (4 bytes) (4 bytes)
663 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
666 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
667 ND_PRINT(" (elapsed time %u)",
668 GET_BE_U_2(option
+ 6));
671 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
672 ND_PRINT(" (elapsed time %u)",
673 GET_BE_U_4(option
+ 6));
676 ND_PRINT(" [optlen != 6 or 8 or 10]");
682 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
683 else if (optlen
== 4)
684 ND_PRINT(" %u", GET_BE_U_2(option
+ 2));
686 ND_PRINT(" [optlen != 4 or 6]");
691 for (i
= 0; i
< optlen
- 2; i
++)
693 GET_U_1(option
+ 2 + i
));