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 #define ND_LONGJMP_FROM_TCHECK
19 #include "netdissect.h"
20 #include "addrtoname.h"
26 /* RFC4340: Datagram Congestion Control Protocol (DCCP) */
29 * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
32 * @dccph_sport - Relevant port on the endpoint that sent this packet
33 * @dccph_dport - Relevant port on the other endpoint
34 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
35 * @dccph_ccval - Used by the HC-Sender CCID
36 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
37 * @dccph_checksum - Internet checksum, depends on dccph_cscov
38 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
39 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
40 * @dccph_seq - 24-bit sequence number
43 nd_uint16_t dccph_sport
,
45 nd_uint8_t dccph_doff
;
46 nd_uint8_t dccph_ccval_cscov
;
47 nd_uint16_t dccph_checksum
;
49 nd_uint24_t dccph_seq
;
53 * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
56 * @dccph_sport - Relevant port on the endpoint that sent this packet
57 * @dccph_dport - Relevant port on the other endpoint
58 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
59 * @dccph_ccval - Used by the HC-Sender CCID
60 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
61 * @dccph_checksum - Internet checksum, depends on dccph_cscov
62 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
63 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
64 * @dccph_seq - 48-bit sequence number
67 nd_uint16_t dccph_sport
,
69 nd_uint8_t dccph_doff
;
70 nd_uint8_t dccph_ccval_cscov
;
71 nd_uint16_t dccph_checksum
;
74 nd_uint48_t dccph_seq
;
77 #define DCCPH_CCVAL(dh) ((GET_U_1((dh)->dccph_ccval_cscov) >> 4) & 0xF)
78 #define DCCPH_CSCOV(dh) (GET_U_1((dh)->dccph_ccval_cscov) & 0xF)
80 #define DCCPH_X(dh) (GET_U_1((dh)->dccph_xtr) & 1)
81 #define DCCPH_TYPE(dh) ((GET_U_1((dh)->dccph_xtr) >> 1) & 0xF)
84 * struct dccp_hdr_request - Connection initiation request header
86 * @dccph_req_service - Service to which the client app wants to connect
88 struct dccp_hdr_request
{
89 nd_uint32_t dccph_req_service
;
93 * struct dccp_hdr_response - Connection initiation response header
95 * @dccph_resp_ack - 48 bit ack number, contains GSR
96 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
98 struct dccp_hdr_response
{
99 nd_uint64_t dccph_resp_ack
; /* always 8 bytes, first 2 reserved */
100 nd_uint32_t dccph_resp_service
;
104 * struct dccp_hdr_reset - Unconditionally shut down a connection
106 * @dccph_resp_ack - 48 bit ack number
107 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
109 struct dccp_hdr_reset
{
110 nd_uint64_t dccph_reset_ack
; /* always 8 bytes, first 2 reserved */
111 nd_uint8_t dccph_reset_code
;
112 nd_uint8_t dccph_reset_data1
;
113 nd_uint8_t dccph_reset_data2
;
114 nd_uint8_t dccph_reset_data3
;
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
,
159 static const struct tok dccp_reset_code_str
[] = {
160 { DCCP_RESET_CODE_UNSPECIFIED
, "unspecified" },
161 { DCCP_RESET_CODE_CLOSED
, "closed" },
162 { DCCP_RESET_CODE_ABORTED
, "aborted" },
163 { DCCP_RESET_CODE_NO_CONNECTION
, "no_connection" },
164 { DCCP_RESET_CODE_PACKET_ERROR
, "packet_error" },
165 { DCCP_RESET_CODE_OPTION_ERROR
, "option_error" },
166 { DCCP_RESET_CODE_MANDATORY_ERROR
, "mandatory_error" },
167 { DCCP_RESET_CODE_CONNECTION_REFUSED
, "connection_refused" },
168 { DCCP_RESET_CODE_BAD_SERVICE_CODE
, "bad_service_code" },
169 { DCCP_RESET_CODE_TOO_BUSY
, "too_busy" },
170 { DCCP_RESET_CODE_BAD_INIT_COOKIE
, "bad_init_cookie" },
171 { DCCP_RESET_CODE_AGGRESSION_PENALTY
, "aggression_penalty" },
175 static const struct tok dccp_feature_num_str
[] = {
178 { 2, "allow_short_seqno" },
179 { 3, "sequence_window" },
180 { 4, "ecn_incapable" },
182 { 6, "send_ack_vector" },
183 { 7, "send_ndp_count" },
184 { 8, "minimum_checksum_coverage" },
185 { 9, "check_data_checksum" },
190 dccp_csum_coverage(netdissect_options
*ndo
,
191 const struct dccp_hdr
*dh
, u_int len
)
195 if (DCCPH_CSCOV(dh
) == 0)
197 cov
= (GET_U_1(dh
->dccph_doff
) + DCCPH_CSCOV(dh
) - 1) * sizeof(uint32_t);
198 return (cov
> len
)? len
: cov
;
201 static uint16_t dccp_cksum(netdissect_options
*ndo
, const struct ip
*ip
,
202 const struct dccp_hdr
*dh
, u_int len
)
204 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)(const void *)dh
, len
,
205 dccp_csum_coverage(ndo
, dh
, len
), IPPROTO_DCCP
);
208 static uint16_t dccp6_cksum(netdissect_options
*ndo
, const struct ip6_hdr
*ip6
,
209 const struct dccp_hdr
*dh
, u_int len
)
211 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)(const void *)dh
, len
,
212 dccp_csum_coverage(ndo
, dh
, len
), IPPROTO_DCCP
);
216 dccp_seqno(netdissect_options
*ndo
, const u_char
*bp
)
218 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
221 if (DCCPH_X(dh
) != 0) {
222 const struct dccp_hdr_ext
*dhx
= (const struct dccp_hdr_ext
*)bp
;
223 seqno
= GET_BE_U_6(dhx
->dccph_seq
);
225 seqno
= GET_BE_U_3(dh
->dccph_seq
);
232 dccp_basic_hdr_len(netdissect_options
*ndo
, const struct dccp_hdr
*dh
)
234 return DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : sizeof(struct dccp_hdr
);
237 static void dccp_print_ack_no(netdissect_options
*ndo
, const u_char
*bp
)
239 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
240 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(ndo
, dh
);
243 if (DCCPH_X(dh
) != 0) {
244 ackno
= GET_BE_U_6(ackp
+ 2);
246 ackno
= GET_BE_U_3(ackp
+ 1);
249 ND_PRINT("(ack=%" PRIu64
") ", ackno
);
252 static u_int
dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
255 * dccp_print - show dccp packet
256 * @bp - beginning of dccp packet
257 * @data2 - beginning of enclosing
258 * @len - length of ip packet
261 dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
264 const struct dccp_hdr
*dh
;
266 const struct ip6_hdr
*ip6
;
268 u_short sport
, dport
;
273 ndo
->ndo_protocol
= "dccp";
274 dh
= (const struct dccp_hdr
*)bp
;
276 ip
= (const struct ip
*)data2
;
278 ip6
= (const struct ip6_hdr
*)data2
;
282 cp
= (const u_char
*)(dh
+ 1);
283 ND_ICHECK_ZU(length
, <, sizeof(struct dccp_hdr
));
285 /* get the length of the generic header */
286 fixed_hdrlen
= dccp_basic_hdr_len(ndo
, dh
);
287 ND_ICHECK_U(length
, <, fixed_hdrlen
);
288 ND_TCHECK_LEN(dh
, fixed_hdrlen
);
290 sport
= GET_BE_U_2(dh
->dccph_sport
);
291 dport
= GET_BE_U_2(dh
->dccph_dport
);
292 hlen
= GET_U_1(dh
->dccph_doff
) * 4;
295 ND_PRINT("%s.%u > %s.%u: ",
296 GET_IP6ADDR_STRING(ip6
->ip6_src
), sport
,
297 GET_IP6ADDR_STRING(ip6
->ip6_dst
), dport
);
299 ND_PRINT("%s.%u > %s.%u: ",
300 GET_IPADDR_STRING(ip
->ip_src
), sport
,
301 GET_IPADDR_STRING(ip
->ip_dst
), dport
);
304 nd_print_protocol_caps(ndo
);
306 if (ndo
->ndo_qflag
) {
307 ND_ICHECK_U(length
, <, hlen
);
308 ND_PRINT(" %u", length
- hlen
);
312 /* other variables in generic header */
313 if (ndo
->ndo_vflag
) {
314 ND_PRINT(" (CCVal %u, CsCov %u", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
315 /* checksum calculation */
316 if (ND_TTEST_LEN(bp
, length
)) {
317 uint16_t sum
= 0, dccp_sum
;
319 dccp_sum
= GET_BE_U_2(dh
->dccph_checksum
);
320 ND_PRINT(", cksum 0x%04x ", dccp_sum
);
322 sum
= dccp_cksum(ndo
, ip
, dh
, length
);
323 else if (IP_V(ip
) == 6)
324 sum
= dccp6_cksum(ndo
, ip6
, dh
, length
);
326 ND_PRINT("(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum
, sum
));
328 ND_PRINT("(correct)");
333 dccph_type
= DCCPH_TYPE(dh
);
334 ND_PRINT(" %s ", tok2str(dccp_pkt_type_str
, "packet-type-%u",
336 switch (dccph_type
) {
337 case DCCP_PKT_REQUEST
: {
338 const struct dccp_hdr_request
*dhr
=
339 (const struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
341 ND_ICHECK_U(length
, <, fixed_hdrlen
);
342 ND_PRINT("(service=%u) ", GET_BE_U_4(dhr
->dccph_req_service
));
345 case DCCP_PKT_RESPONSE
: {
346 const struct dccp_hdr_response
*dhr
=
347 (const struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
349 ND_ICHECK_U(length
, <, fixed_hdrlen
);
350 ND_PRINT("(service=%u) ", GET_BE_U_4(dhr
->dccph_resp_service
));
357 ND_ICHECK_U(length
, <, fixed_hdrlen
);
360 case DCCP_PKT_DATAACK
: {
362 ND_ICHECK_U(length
, <, fixed_hdrlen
);
365 case DCCP_PKT_CLOSEREQ
:
367 ND_ICHECK_U(length
, <, fixed_hdrlen
);
371 ND_ICHECK_U(length
, <, fixed_hdrlen
);
373 case DCCP_PKT_RESET
: {
374 const struct dccp_hdr_reset
*dhr
=
375 (const struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
377 ND_ICHECK_U(length
, <, fixed_hdrlen
);
379 ND_PRINT("(code=%s) ", tok2str(dccp_reset_code_str
,
380 "reset-code-%u (invalid)", GET_U_1(dhr
->dccph_reset_code
)));
385 ND_ICHECK_U(length
, <, fixed_hdrlen
);
387 case DCCP_PKT_SYNCACK
:
389 ND_ICHECK_U(length
, <, fixed_hdrlen
);
395 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
396 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
397 dccp_print_ack_no(ndo
, bp
);
399 if (ndo
->ndo_vflag
< 2)
402 ND_PRINT("seq %" PRIu64
, dccp_seqno(ndo
, bp
));
404 /* process options */
405 if (hlen
> fixed_hdrlen
){
407 cp
= bp
+ fixed_hdrlen
;
410 hlen
-= fixed_hdrlen
;
412 optlen
= dccp_print_option(ndo
, cp
, hlen
);
425 nd_print_invalid(ndo
);
428 enum dccp_option_type
{
429 DCCP_OPTION_PADDING
= 0,
430 DCCP_OPTION_MANDATORY
= 1,
431 DCCP_OPTION_SLOW_RECEIVER
= 2,
432 DCCP_OPTION_CHANGE_L
= 32,
433 DCCP_OPTION_CONFIRM_L
= 33,
434 DCCP_OPTION_CHANGE_R
= 34,
435 DCCP_OPTION_CONFIRM_R
= 35,
436 DCCP_OPTION_INIT_COOKIE
= 36,
437 DCCP_OPTION_NDP_COUNT
= 37,
438 DCCP_OPTION_ACK_VECTOR_NONCE_0
= 38,
439 DCCP_OPTION_ACK_VECTOR_NONCE_1
= 39,
440 DCCP_OPTION_DATA_DROPPED
= 40,
441 DCCP_OPTION_TIMESTAMP
= 41,
442 DCCP_OPTION_TIMESTAMP_ECHO
= 42,
443 DCCP_OPTION_ELAPSED_TIME
= 43,
444 DCCP_OPTION_DATA_CHECKSUM
= 44
447 static const struct tok dccp_option_values
[] = {
448 { DCCP_OPTION_PADDING
, "nop" },
449 { DCCP_OPTION_MANDATORY
, "mandatory" },
450 { DCCP_OPTION_SLOW_RECEIVER
, "slowreceiver" },
451 { DCCP_OPTION_CHANGE_L
, "change_l" },
452 { DCCP_OPTION_CONFIRM_L
, "confirm_l" },
453 { DCCP_OPTION_CHANGE_R
, "change_r" },
454 { DCCP_OPTION_CONFIRM_R
, "confirm_r" },
455 { DCCP_OPTION_INIT_COOKIE
, "initcookie" },
456 { DCCP_OPTION_NDP_COUNT
, "ndp_count" },
457 { DCCP_OPTION_ACK_VECTOR_NONCE_0
, "ack_vector0" },
458 { DCCP_OPTION_ACK_VECTOR_NONCE_1
, "ack_vector1" },
459 { DCCP_OPTION_DATA_DROPPED
, "data_dropped" },
460 { DCCP_OPTION_TIMESTAMP
, "timestamp" },
461 { DCCP_OPTION_TIMESTAMP_ECHO
, "timestamp_echo" },
462 { DCCP_OPTION_ELAPSED_TIME
, "elapsed_time" },
463 { DCCP_OPTION_DATA_CHECKSUM
, "data_checksum" },
468 dccp_print_option(netdissect_options
*ndo
, const u_char
*bp
, u_int hlen
)
470 uint8_t option
, optlen
, i
;
472 option
= GET_U_1(bp
);
474 ND_PRINT("CCID option %u", option
);
477 tok2str(dccp_option_values
, "option-type-%u", option
));
479 optlen
= GET_U_1(bp
+ 1);
480 ND_ICHECK_U(optlen
, <, 2);
484 ND_ICHECKMSG_U("remaining length", hlen
, <, optlen
);
489 ND_PRINT(" %u", GET_BE_U_2(bp
+ 2));
492 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
496 for (i
= 0; i
< optlen
- 2; i
++)
497 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));
502 case DCCP_OPTION_PADDING
:
503 case DCCP_OPTION_MANDATORY
:
504 case DCCP_OPTION_SLOW_RECEIVER
:
507 case DCCP_OPTION_CHANGE_L
:
508 case DCCP_OPTION_CHANGE_R
:
509 ND_ICHECK_U(optlen
, <, 4);
510 ND_PRINT(" %s", tok2str(dccp_feature_num_str
,
511 "feature-number-%u (invalid)", GET_U_1(bp
+ 2)));
512 for (i
= 0; i
< optlen
- 3; i
++)
513 ND_PRINT(" %u", GET_U_1(bp
+ 3 + i
));
515 case DCCP_OPTION_CONFIRM_L
:
516 case DCCP_OPTION_CONFIRM_R
:
517 ND_ICHECK_U(optlen
, <, 3);
518 ND_PRINT(" %s", tok2str(dccp_feature_num_str
,
519 "feature-number-%u (invalid)", GET_U_1(bp
+ 2)));
520 for (i
= 0; i
< optlen
- 3; i
++)
521 ND_PRINT(" %u", GET_U_1(bp
+ 3 + i
));
523 case DCCP_OPTION_INIT_COOKIE
:
524 ND_ICHECK_U(optlen
, <, 3);
526 for (i
= 0; i
< optlen
- 2; i
++)
527 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));
529 case DCCP_OPTION_NDP_COUNT
:
530 ND_ICHECK_U(optlen
, <, 3);
531 ND_ICHECK_U(optlen
, >, 8);
532 for (i
= 0; i
< optlen
- 2; i
++)
533 ND_PRINT(" %u", GET_U_1(bp
+ 2 + i
));
535 case DCCP_OPTION_ACK_VECTOR_NONCE_0
:
536 case DCCP_OPTION_ACK_VECTOR_NONCE_1
:
537 ND_ICHECK_U(optlen
, <, 3);
539 for (i
= 0; i
< optlen
- 2; i
++)
540 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));
542 case DCCP_OPTION_DATA_DROPPED
:
543 ND_ICHECK_U(optlen
, <, 3);
545 for (i
= 0; i
< optlen
- 2; i
++)
546 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));
548 case DCCP_OPTION_TIMESTAMP
:
550 * 13.1. Timestamp Option
552 * +--------+--------+--------+--------+--------+--------+
553 * |00101001|00000110| Timestamp Value |
554 * +--------+--------+--------+--------+--------+--------+
557 ND_ICHECK_U(optlen
, !=, 6);
558 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
560 case DCCP_OPTION_TIMESTAMP_ECHO
:
562 * 13.3. Timestamp Echo Option
564 * +--------+--------+--------+--------+--------+--------+
565 * |00101010|00000110| Timestamp Echo |
566 * +--------+--------+--------+--------+--------+--------+
569 * +--------+--------+------- ... -------+--------+--------+
570 * |00101010|00001000| Timestamp Echo | Elapsed Time |
571 * +--------+--------+------- ... -------+--------+--------+
572 * Type=42 Len=8 (4 bytes)
574 * +--------+--------+------- ... -------+------- ... -------+
575 * |00101010|00001010| Timestamp Echo | Elapsed Time |
576 * +--------+--------+------- ... -------+------- ... -------+
577 * Type=42 Len=10 (4 bytes) (4 bytes)
581 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
584 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
585 ND_PRINT(" (elapsed time %u)",
589 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
590 ND_PRINT(" (elapsed time %u)",
594 ND_PRINT(" [optlen != 6 or 8 or 10]");
598 case DCCP_OPTION_ELAPSED_TIME
:
601 ND_PRINT(" %u", GET_BE_U_2(bp
+ 2));
604 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
607 ND_PRINT(" [optlen != 4 or 6]");
611 case DCCP_OPTION_DATA_CHECKSUM
:
612 ND_ICHECK_U(optlen
, !=, 6);
614 for (i
= 0; i
< optlen
- 2; i
++)
615 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));