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 */
18 #include "netdissect-stdinc.h"
20 #include "netdissect.h"
21 #include "addrtoname.h"
27 /* RFC4340: Datagram Congestion Control Protocol (DCCP) */
30 * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
33 * @dccph_sport - Relevant port on the endpoint that sent this packet
34 * @dccph_dport - Relevant port on the other endpoint
35 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
36 * @dccph_ccval - Used by the HC-Sender CCID
37 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
38 * @dccph_checksum - Internet checksum, depends on dccph_cscov
39 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
40 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
41 * @dccph_seq - 24-bit sequence number
44 nd_uint16_t dccph_sport
,
46 nd_uint8_t dccph_doff
;
47 nd_uint8_t dccph_ccval_cscov
;
48 nd_uint16_t dccph_checksum
;
50 nd_uint24_t dccph_seq
;
54 * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
57 * @dccph_sport - Relevant port on the endpoint that sent this packet
58 * @dccph_dport - Relevant port on the other endpoint
59 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
60 * @dccph_ccval - Used by the HC-Sender CCID
61 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
62 * @dccph_checksum - Internet checksum, depends on dccph_cscov
63 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
64 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
65 * @dccph_seq - 48-bit sequence number
68 nd_uint16_t dccph_sport
,
70 nd_uint8_t dccph_doff
;
71 nd_uint8_t dccph_ccval_cscov
;
72 nd_uint16_t dccph_checksum
;
75 nd_uint48_t dccph_seq
;
78 #define DCCPH_CCVAL(dh) ((GET_U_1((dh)->dccph_ccval_cscov) >> 4) & 0xF)
79 #define DCCPH_CSCOV(dh) (GET_U_1((dh)->dccph_ccval_cscov) & 0xF)
81 #define DCCPH_X(dh) (GET_U_1((dh)->dccph_xtr) & 1)
82 #define DCCPH_TYPE(dh) ((GET_U_1((dh)->dccph_xtr) >> 1) & 0xF)
85 * struct dccp_hdr_request - Connection initiation request header
87 * @dccph_req_service - Service to which the client app wants to connect
89 struct dccp_hdr_request
{
90 nd_uint32_t dccph_req_service
;
94 * struct dccp_hdr_response - Connection initiation response header
96 * @dccph_resp_ack - 48 bit ack number, contains GSR
97 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
99 struct dccp_hdr_response
{
100 nd_uint64_t dccph_resp_ack
; /* always 8 bytes, first 2 reserved */
101 nd_uint32_t dccph_resp_service
;
105 * struct dccp_hdr_reset - Unconditionally shut down a connection
107 * @dccph_resp_ack - 48 bit ack number
108 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
110 struct dccp_hdr_reset
{
111 nd_uint64_t dccph_reset_ack
; /* always 8 bytes, first 2 reserved */
112 nd_uint8_t dccph_reset_code
;
113 nd_uint8_t dccph_reset_data1
;
114 nd_uint8_t dccph_reset_data2
;
115 nd_uint8_t dccph_reset_data3
;
119 DCCP_PKT_REQUEST
= 0,
131 static const struct tok dccp_pkt_type_str
[] = {
132 { DCCP_PKT_REQUEST
, "DCCP-Request" },
133 { DCCP_PKT_RESPONSE
, "DCCP-Response" },
134 { DCCP_PKT_DATA
, "DCCP-Data" },
135 { DCCP_PKT_ACK
, "DCCP-Ack" },
136 { DCCP_PKT_DATAACK
, "DCCP-DataAck" },
137 { DCCP_PKT_CLOSEREQ
, "DCCP-CloseReq" },
138 { DCCP_PKT_CLOSE
, "DCCP-Close" },
139 { DCCP_PKT_RESET
, "DCCP-Reset" },
140 { DCCP_PKT_SYNC
, "DCCP-Sync" },
141 { DCCP_PKT_SYNCACK
, "DCCP-SyncAck" },
145 enum dccp_reset_codes
{
146 DCCP_RESET_CODE_UNSPECIFIED
= 0,
147 DCCP_RESET_CODE_CLOSED
,
148 DCCP_RESET_CODE_ABORTED
,
149 DCCP_RESET_CODE_NO_CONNECTION
,
150 DCCP_RESET_CODE_PACKET_ERROR
,
151 DCCP_RESET_CODE_OPTION_ERROR
,
152 DCCP_RESET_CODE_MANDATORY_ERROR
,
153 DCCP_RESET_CODE_CONNECTION_REFUSED
,
154 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
155 DCCP_RESET_CODE_TOO_BUSY
,
156 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
157 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
160 static const struct tok dccp_reset_code_str
[] = {
161 { DCCP_RESET_CODE_UNSPECIFIED
, "unspecified" },
162 { DCCP_RESET_CODE_CLOSED
, "closed" },
163 { DCCP_RESET_CODE_ABORTED
, "aborted" },
164 { DCCP_RESET_CODE_NO_CONNECTION
, "no_connection" },
165 { DCCP_RESET_CODE_PACKET_ERROR
, "packet_error" },
166 { DCCP_RESET_CODE_OPTION_ERROR
, "option_error" },
167 { DCCP_RESET_CODE_MANDATORY_ERROR
, "mandatory_error" },
168 { DCCP_RESET_CODE_CONNECTION_REFUSED
, "connection_refused" },
169 { DCCP_RESET_CODE_BAD_SERVICE_CODE
, "bad_service_code" },
170 { DCCP_RESET_CODE_TOO_BUSY
, "too_busy" },
171 { DCCP_RESET_CODE_BAD_INIT_COOKIE
, "bad_init_cookie" },
172 { DCCP_RESET_CODE_AGGRESSION_PENALTY
, "aggression_penalty" },
176 static const struct tok dccp_feature_num_str
[] = {
179 { 2, "allow_short_seqno" },
180 { 3, "sequence_window" },
181 { 4, "ecn_incapable" },
183 { 6, "send_ack_vector" },
184 { 7, "send_ndp_count" },
185 { 8, "minimum checksum coverage" },
186 { 9, "check data checksum" },
191 dccp_csum_coverage(netdissect_options
*ndo
,
192 const struct dccp_hdr
* dh
, u_int len
)
196 if (DCCPH_CSCOV(dh
) == 0)
198 cov
= (GET_U_1(dh
->dccph_doff
) + DCCPH_CSCOV(dh
) - 1) * sizeof(uint32_t);
199 return (cov
> len
)? len
: cov
;
202 static uint16_t dccp_cksum(netdissect_options
*ndo
, const struct ip
*ip
,
203 const struct dccp_hdr
*dh
, u_int len
)
205 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)(const void *)dh
, len
,
206 dccp_csum_coverage(ndo
, dh
, len
), IPPROTO_DCCP
);
209 static uint16_t dccp6_cksum(netdissect_options
*ndo
, const struct ip6_hdr
*ip6
,
210 const struct dccp_hdr
*dh
, u_int len
)
212 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)(const void *)dh
, len
,
213 dccp_csum_coverage(ndo
, dh
, len
), IPPROTO_DCCP
);
217 dccp_seqno(netdissect_options
*ndo
, const u_char
*bp
)
219 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
222 if (DCCPH_X(dh
) != 0) {
223 const struct dccp_hdr_ext
*dhx
= (const struct dccp_hdr_ext
*)bp
;
224 seqno
= GET_BE_U_6(dhx
->dccph_seq
);
226 seqno
= GET_BE_U_3(dh
->dccph_seq
);
233 dccp_basic_hdr_len(netdissect_options
*ndo
, const struct dccp_hdr
*dh
)
235 return DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : sizeof(struct dccp_hdr
);
238 static void dccp_print_ack_no(netdissect_options
*ndo
, const u_char
*bp
)
240 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
241 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(ndo
, dh
);
244 if (DCCPH_X(dh
) != 0) {
245 ackno
= GET_BE_U_6(ackp
+ 2);
247 ackno
= GET_BE_U_3(ackp
+ 1);
250 ND_PRINT("(ack=%" PRIu64
") ", ackno
);
253 static u_int
dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
256 * dccp_print - show dccp packet
257 * @bp - beginning of dccp packet
258 * @data2 - beginning of enclosing
259 * @len - length of ip packet
262 dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
265 const struct dccp_hdr
*dh
;
267 const struct ip6_hdr
*ip6
;
269 u_short sport
, dport
;
274 ndo
->ndo_protocol
= "dccp";
275 dh
= (const struct dccp_hdr
*)bp
;
277 ip
= (const struct ip
*)data2
;
279 ip6
= (const struct ip6_hdr
*)data2
;
283 /* make sure we have enough data to look at the X bit */
284 cp
= (const u_char
*)(dh
+ 1);
285 if (cp
> ndo
->ndo_snapend
)
287 if (len
< sizeof(struct dccp_hdr
)) {
288 ND_PRINT("truncated-dccp - %zu bytes missing!",
289 sizeof(struct dccp_hdr
) - len
);
293 /* get the length of the generic header */
294 fixed_hdrlen
= dccp_basic_hdr_len(ndo
, dh
);
295 if (len
< fixed_hdrlen
) {
296 ND_PRINT("truncated-dccp - %u bytes missing!",
300 ND_TCHECK_LEN(dh
, fixed_hdrlen
);
302 sport
= GET_BE_U_2(dh
->dccph_sport
);
303 dport
= GET_BE_U_2(dh
->dccph_dport
);
304 hlen
= GET_U_1(dh
->dccph_doff
) * 4;
307 ND_PRINT("%s.%u > %s.%u: ",
308 GET_IP6ADDR_STRING(ip6
->ip6_src
), sport
,
309 GET_IP6ADDR_STRING(ip6
->ip6_dst
), dport
);
311 ND_PRINT("%s.%u > %s.%u: ",
312 GET_IPADDR_STRING(ip
->ip_src
), sport
,
313 GET_IPADDR_STRING(ip
->ip_dst
), dport
);
316 nd_print_protocol_caps(ndo
);
318 if (ndo
->ndo_qflag
) {
319 ND_PRINT(" %u", len
- hlen
);
321 ND_PRINT(" [bad hdr length %u - too long, > %u]",
327 /* other variables in generic header */
328 if (ndo
->ndo_vflag
) {
329 ND_PRINT(" (CCVal %u, CsCov %u, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
332 /* checksum calculation */
333 if (ndo
->ndo_vflag
&& ND_TTEST_LEN(bp
, len
)) {
334 uint16_t sum
= 0, dccp_sum
;
336 dccp_sum
= GET_BE_U_2(dh
->dccph_checksum
);
337 ND_PRINT("cksum 0x%04x ", dccp_sum
);
339 sum
= dccp_cksum(ndo
, ip
, dh
, len
);
340 else if (IP_V(ip
) == 6)
341 sum
= dccp6_cksum(ndo
, ip6
, dh
, len
);
343 ND_PRINT("(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum
, sum
));
345 ND_PRINT("(correct)");
352 dccph_type
= DCCPH_TYPE(dh
);
353 switch (dccph_type
) {
354 case DCCP_PKT_REQUEST
: {
355 const struct dccp_hdr_request
*dhr
=
356 (const struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
358 if (len
< fixed_hdrlen
) {
359 ND_PRINT("truncated-%s - %u bytes missing!",
360 tok2str(dccp_pkt_type_str
, "", dccph_type
),
365 ND_PRINT("%s (service=%u) ",
366 tok2str(dccp_pkt_type_str
, "", dccph_type
),
367 GET_BE_U_4(dhr
->dccph_req_service
));
370 case DCCP_PKT_RESPONSE
: {
371 const struct dccp_hdr_response
*dhr
=
372 (const struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
374 if (len
< fixed_hdrlen
) {
375 ND_PRINT("truncated-%s - %u bytes missing!",
376 tok2str(dccp_pkt_type_str
, "", dccph_type
),
381 ND_PRINT("%s (service=%u) ",
382 tok2str(dccp_pkt_type_str
, "", dccph_type
),
383 GET_BE_U_4(dhr
->dccph_resp_service
));
387 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
391 if (len
< fixed_hdrlen
) {
392 ND_PRINT("truncated-%s - %u bytes missing!",
393 tok2str(dccp_pkt_type_str
, "", dccph_type
),
397 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
400 case DCCP_PKT_DATAACK
: {
402 if (len
< fixed_hdrlen
) {
403 ND_PRINT("truncated-%s - %u bytes missing!",
404 tok2str(dccp_pkt_type_str
, "", dccph_type
),
408 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
411 case DCCP_PKT_CLOSEREQ
:
413 if (len
< fixed_hdrlen
) {
414 ND_PRINT("truncated-%s - %u bytes missing!",
415 tok2str(dccp_pkt_type_str
, "", dccph_type
),
419 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
423 if (len
< fixed_hdrlen
) {
424 ND_PRINT("truncated-%s - %u bytes missing!",
425 tok2str(dccp_pkt_type_str
, "", dccph_type
),
429 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
431 case DCCP_PKT_RESET
: {
432 const struct dccp_hdr_reset
*dhr
=
433 (const struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
435 if (len
< fixed_hdrlen
) {
436 ND_PRINT("truncated-%s - %u bytes missing!",
437 tok2str(dccp_pkt_type_str
, "", dccph_type
),
442 ND_PRINT("%s (code=%s) ",
443 tok2str(dccp_pkt_type_str
, "", dccph_type
),
444 tok2str(dccp_reset_code_str
, "invalid", GET_U_1(dhr
->dccph_reset_code
)));
449 if (len
< fixed_hdrlen
) {
450 ND_PRINT("truncated-%s - %u bytes missing!",
451 tok2str(dccp_pkt_type_str
, "", dccph_type
),
455 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
457 case DCCP_PKT_SYNCACK
:
459 if (len
< fixed_hdrlen
) {
460 ND_PRINT("truncated-%s - %u bytes missing!",
461 tok2str(dccp_pkt_type_str
, "", dccph_type
),
465 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
468 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "unknown-type-%u", dccph_type
));
472 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
473 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
474 dccp_print_ack_no(ndo
, bp
);
476 if (ndo
->ndo_vflag
< 2)
479 ND_PRINT("seq %" PRIu64
, dccp_seqno(ndo
, bp
));
481 /* process options */
482 if (hlen
> fixed_hdrlen
){
484 cp
= bp
+ fixed_hdrlen
;
487 hlen
-= fixed_hdrlen
;
489 optlen
= dccp_print_option(ndo
, cp
, hlen
);
505 enum dccp_option_type
{
506 DCCP_OPTION_PADDING
= 0,
507 DCCP_OPTION_MANDATORY
= 1,
508 DCCP_OPTION_SLOW_RECEIVER
= 2,
509 DCCP_OPTION_CHANGE_L
= 32,
510 DCCP_OPTION_CONFIRM_L
= 33,
511 DCCP_OPTION_CHANGE_R
= 34,
512 DCCP_OPTION_CONFIRM_R
= 35,
513 DCCP_OPTION_INIT_COOKIE
= 36,
514 DCCP_OPTION_NDP_COUNT
= 37,
515 DCCP_OPTION_ACK_VECTOR_NONCE_0
= 38,
516 DCCP_OPTION_ACK_VECTOR_NONCE_1
= 39,
517 DCCP_OPTION_DATA_DROPPED
= 40,
518 DCCP_OPTION_TIMESTAMP
= 41,
519 DCCP_OPTION_TIMESTAMP_ECHO
= 42,
520 DCCP_OPTION_ELAPSED_TIME
= 43,
521 DCCP_OPTION_DATA_CHECKSUM
= 44
524 static const struct tok dccp_option_values
[] = {
525 { DCCP_OPTION_PADDING
, "nop" },
526 { DCCP_OPTION_MANDATORY
, "mandatory" },
527 { DCCP_OPTION_SLOW_RECEIVER
, "slowreceiver" },
528 { DCCP_OPTION_CHANGE_L
, "change_l" },
529 { DCCP_OPTION_CONFIRM_L
, "confirm_l" },
530 { DCCP_OPTION_CHANGE_R
, "change_r" },
531 { DCCP_OPTION_CONFIRM_R
, "confirm_r" },
532 { DCCP_OPTION_INIT_COOKIE
, "initcookie" },
533 { DCCP_OPTION_NDP_COUNT
, "ndp_count" },
534 { DCCP_OPTION_ACK_VECTOR_NONCE_0
, "ack_vector0" },
535 { DCCP_OPTION_ACK_VECTOR_NONCE_1
, "ack_vector1" },
536 { DCCP_OPTION_DATA_DROPPED
, "data_dropped" },
537 { DCCP_OPTION_TIMESTAMP
, "timestamp" },
538 { DCCP_OPTION_TIMESTAMP_ECHO
, "timestamp_echo" },
539 { DCCP_OPTION_ELAPSED_TIME
, "elapsed_time" },
540 { DCCP_OPTION_DATA_CHECKSUM
, "data_checksum" },
545 dccp_print_option(netdissect_options
*ndo
, const u_char
*option
, u_int hlen
)
549 if (GET_U_1(option
) >= 32) {
550 optlen
= GET_U_1(option
+ 1);
552 if (GET_U_1(option
) >= 128)
553 ND_PRINT("CCID option %u optlen too short",
556 ND_PRINT("%s optlen too short",
557 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
564 if (GET_U_1(option
) >= 128)
565 ND_PRINT("CCID option %u optlen goes past header length",
568 ND_PRINT("%s optlen goes past header length",
569 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
572 ND_TCHECK_LEN(option
, optlen
);
574 if (GET_U_1(option
) >= 128) {
575 ND_PRINT("CCID option %u", GET_U_1(option
));
578 ND_PRINT(" %u", GET_BE_U_2(option
+ 2));
581 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
588 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
589 switch (GET_U_1(option
)) {
590 case DCCP_OPTION_CHANGE_L
:
591 case DCCP_OPTION_CONFIRM_L
:
592 case DCCP_OPTION_CHANGE_R
:
593 case DCCP_OPTION_CONFIRM_R
:
595 ND_PRINT(" optlen too short");
598 ND_PRINT(" %s", tok2str(dccp_feature_num_str
,
599 "invalid (%u)", GET_U_1(option
+ 2)));
600 for (i
= 0; i
< optlen
- 3; i
++)
601 ND_PRINT(" %u", GET_U_1(option
+ 3 + i
));
603 case DCCP_OPTION_INIT_COOKIE
:
606 for (i
= 0; i
< optlen
- 2; i
++)
608 GET_U_1(option
+ 2 + i
));
611 case DCCP_OPTION_NDP_COUNT
:
612 for (i
= 0; i
< optlen
- 2; i
++)
613 ND_PRINT(" %u", GET_U_1(option
+ 2 + i
));
615 case DCCP_OPTION_ACK_VECTOR_NONCE_0
:
618 for (i
= 0; i
< optlen
- 2; i
++)
620 GET_U_1(option
+ 2 + i
));
623 case DCCP_OPTION_ACK_VECTOR_NONCE_1
:
626 for (i
= 0; i
< optlen
- 2; i
++)
628 GET_U_1(option
+ 2 + i
));
631 case DCCP_OPTION_DATA_DROPPED
:
634 for (i
= 0; i
< optlen
- 2; i
++)
636 GET_U_1(option
+ 2 + i
));
639 case DCCP_OPTION_TIMESTAMP
:
641 * 13.1. Timestamp Option
643 * +--------+--------+--------+--------+--------+--------+
644 * |00101001|00000110| Timestamp Value |
645 * +--------+--------+--------+--------+--------+--------+
649 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
651 ND_PRINT(" [optlen != 6]");
653 case DCCP_OPTION_TIMESTAMP_ECHO
:
655 * 13.3. Timestamp Echo Option
657 * +--------+--------+--------+--------+--------+--------+
658 * |00101010|00000110| Timestamp Echo |
659 * +--------+--------+--------+--------+--------+--------+
662 * +--------+--------+------- ... -------+--------+--------+
663 * |00101010|00001000| Timestamp Echo | Elapsed Time |
664 * +--------+--------+------- ... -------+--------+--------+
665 * Type=42 Len=8 (4 bytes)
667 * +--------+--------+------- ... -------+------- ... -------+
668 * |00101010|00001010| Timestamp Echo | Elapsed Time |
669 * +--------+--------+------- ... -------+------- ... -------+
670 * Type=42 Len=10 (4 bytes) (4 bytes)
674 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
677 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
678 ND_PRINT(" (elapsed time %u)",
679 GET_BE_U_2(option
+ 6));
682 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
683 ND_PRINT(" (elapsed time %u)",
684 GET_BE_U_4(option
+ 6));
687 ND_PRINT(" [optlen != 6 or 8 or 10]");
691 case DCCP_OPTION_ELAPSED_TIME
:
693 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
694 else if (optlen
== 4)
695 ND_PRINT(" %u", GET_BE_U_2(option
+ 2));
697 ND_PRINT(" [optlen != 4 or 6]");
699 case DCCP_OPTION_DATA_CHECKSUM
:
702 for (i
= 0; i
< optlen
- 2; i
++)
704 GET_U_1(option
+ 2 + i
));