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 #define ND_LONGJMP_FROM_TCHECK
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
45 nd_uint16_t dccph_sport
,
47 nd_uint8_t dccph_doff
;
48 nd_uint8_t dccph_ccval_cscov
;
49 nd_uint16_t dccph_checksum
;
51 nd_uint24_t dccph_seq
;
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
69 nd_uint16_t dccph_sport
,
71 nd_uint8_t dccph_doff
;
72 nd_uint8_t dccph_ccval_cscov
;
73 nd_uint16_t dccph_checksum
;
76 nd_uint48_t dccph_seq
;
79 #define DCCPH_CCVAL(dh) ((GET_U_1((dh)->dccph_ccval_cscov) >> 4) & 0xF)
80 #define DCCPH_CSCOV(dh) (GET_U_1((dh)->dccph_ccval_cscov) & 0xF)
82 #define DCCPH_X(dh) (GET_U_1((dh)->dccph_xtr) & 1)
83 #define DCCPH_TYPE(dh) ((GET_U_1((dh)->dccph_xtr) >> 1) & 0xF)
86 * struct dccp_hdr_request - Connection initiation request header
88 * @dccph_req_service - Service to which the client app wants to connect
90 struct dccp_hdr_request
{
91 nd_uint32_t dccph_req_service
;
95 * struct dccp_hdr_response - Connection 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 nd_uint64_t dccph_resp_ack
; /* always 8 bytes, first 2 reserved */
102 nd_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 nd_uint64_t dccph_reset_ack
; /* always 8 bytes, first 2 reserved */
113 nd_uint8_t dccph_reset_code
;
114 nd_uint8_t dccph_reset_data1
;
115 nd_uint8_t dccph_reset_data2
;
116 nd_uint8_t dccph_reset_data3
;
120 DCCP_PKT_REQUEST
= 0,
132 static const struct tok dccp_pkt_type_str
[] = {
133 { DCCP_PKT_REQUEST
, "DCCP-Request" },
134 { DCCP_PKT_RESPONSE
, "DCCP-Response" },
135 { DCCP_PKT_DATA
, "DCCP-Data" },
136 { DCCP_PKT_ACK
, "DCCP-Ack" },
137 { DCCP_PKT_DATAACK
, "DCCP-DataAck" },
138 { DCCP_PKT_CLOSEREQ
, "DCCP-CloseReq" },
139 { DCCP_PKT_CLOSE
, "DCCP-Close" },
140 { DCCP_PKT_RESET
, "DCCP-Reset" },
141 { DCCP_PKT_SYNC
, "DCCP-Sync" },
142 { DCCP_PKT_SYNCACK
, "DCCP-SyncAck" },
146 enum dccp_reset_codes
{
147 DCCP_RESET_CODE_UNSPECIFIED
= 0,
148 DCCP_RESET_CODE_CLOSED
,
149 DCCP_RESET_CODE_ABORTED
,
150 DCCP_RESET_CODE_NO_CONNECTION
,
151 DCCP_RESET_CODE_PACKET_ERROR
,
152 DCCP_RESET_CODE_OPTION_ERROR
,
153 DCCP_RESET_CODE_MANDATORY_ERROR
,
154 DCCP_RESET_CODE_CONNECTION_REFUSED
,
155 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
156 DCCP_RESET_CODE_TOO_BUSY
,
157 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
158 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
161 static const struct tok dccp_reset_code_str
[] = {
162 { DCCP_RESET_CODE_UNSPECIFIED
, "unspecified" },
163 { DCCP_RESET_CODE_CLOSED
, "closed" },
164 { DCCP_RESET_CODE_ABORTED
, "aborted" },
165 { DCCP_RESET_CODE_NO_CONNECTION
, "no_connection" },
166 { DCCP_RESET_CODE_PACKET_ERROR
, "packet_error" },
167 { DCCP_RESET_CODE_OPTION_ERROR
, "option_error" },
168 { DCCP_RESET_CODE_MANDATORY_ERROR
, "mandatory_error" },
169 { DCCP_RESET_CODE_CONNECTION_REFUSED
, "connection_refused" },
170 { DCCP_RESET_CODE_BAD_SERVICE_CODE
, "bad_service_code" },
171 { DCCP_RESET_CODE_TOO_BUSY
, "too_busy" },
172 { DCCP_RESET_CODE_BAD_INIT_COOKIE
, "bad_init_cookie" },
173 { DCCP_RESET_CODE_AGGRESSION_PENALTY
, "aggression_penalty" },
177 static const struct tok dccp_feature_num_str
[] = {
180 { 2, "allow_short_seqno" },
181 { 3, "sequence_window" },
182 { 4, "ecn_incapable" },
184 { 6, "send_ack_vector" },
185 { 7, "send_ndp_count" },
186 { 8, "minimum_checksum_coverage" },
187 { 9, "check_data_checksum" },
192 dccp_csum_coverage(netdissect_options
*ndo
,
193 const struct dccp_hdr
*dh
, u_int len
)
197 if (DCCPH_CSCOV(dh
) == 0)
199 cov
= (GET_U_1(dh
->dccph_doff
) + DCCPH_CSCOV(dh
) - 1) * sizeof(uint32_t);
200 return (cov
> len
)? len
: cov
;
203 static uint16_t dccp_cksum(netdissect_options
*ndo
, const struct ip
*ip
,
204 const struct dccp_hdr
*dh
, u_int len
)
206 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)(const void *)dh
, len
,
207 dccp_csum_coverage(ndo
, dh
, len
), IPPROTO_DCCP
);
210 static uint16_t dccp6_cksum(netdissect_options
*ndo
, const struct ip6_hdr
*ip6
,
211 const struct dccp_hdr
*dh
, u_int len
)
213 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)(const void *)dh
, len
,
214 dccp_csum_coverage(ndo
, dh
, len
), IPPROTO_DCCP
);
218 dccp_seqno(netdissect_options
*ndo
, const u_char
*bp
)
220 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
223 if (DCCPH_X(dh
) != 0) {
224 const struct dccp_hdr_ext
*dhx
= (const struct dccp_hdr_ext
*)bp
;
225 seqno
= GET_BE_U_6(dhx
->dccph_seq
);
227 seqno
= GET_BE_U_3(dh
->dccph_seq
);
234 dccp_basic_hdr_len(netdissect_options
*ndo
, const struct dccp_hdr
*dh
)
236 return DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : sizeof(struct dccp_hdr
);
239 static void dccp_print_ack_no(netdissect_options
*ndo
, const u_char
*bp
)
241 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
242 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(ndo
, dh
);
245 if (DCCPH_X(dh
) != 0) {
246 ackno
= GET_BE_U_6(ackp
+ 2);
248 ackno
= GET_BE_U_3(ackp
+ 1);
251 ND_PRINT("(ack=%" PRIu64
") ", ackno
);
254 static u_int
dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
257 * dccp_print - show dccp packet
258 * @bp - beginning of dccp packet
259 * @data2 - beginning of enclosing
260 * @len - length of ip packet
263 dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
266 const struct dccp_hdr
*dh
;
268 const struct ip6_hdr
*ip6
;
270 u_short sport
, dport
;
275 ndo
->ndo_protocol
= "dccp";
276 dh
= (const struct dccp_hdr
*)bp
;
278 ip
= (const struct ip
*)data2
;
280 ip6
= (const struct ip6_hdr
*)data2
;
284 cp
= (const u_char
*)(dh
+ 1);
285 ND_ICHECK_ZU(length
, <, sizeof(struct dccp_hdr
));
287 /* get the length of the generic header */
288 fixed_hdrlen
= dccp_basic_hdr_len(ndo
, dh
);
289 ND_ICHECK_U(length
, <, fixed_hdrlen
);
290 ND_TCHECK_LEN(dh
, fixed_hdrlen
);
292 sport
= GET_BE_U_2(dh
->dccph_sport
);
293 dport
= GET_BE_U_2(dh
->dccph_dport
);
294 hlen
= GET_U_1(dh
->dccph_doff
) * 4;
297 ND_PRINT("%s.%u > %s.%u: ",
298 GET_IP6ADDR_STRING(ip6
->ip6_src
), sport
,
299 GET_IP6ADDR_STRING(ip6
->ip6_dst
), dport
);
301 ND_PRINT("%s.%u > %s.%u: ",
302 GET_IPADDR_STRING(ip
->ip_src
), sport
,
303 GET_IPADDR_STRING(ip
->ip_dst
), dport
);
306 nd_print_protocol_caps(ndo
);
308 if (ndo
->ndo_qflag
) {
309 ND_ICHECK_U(length
, <, hlen
);
310 ND_PRINT(" %u", length
- hlen
);
314 /* other variables in generic header */
315 if (ndo
->ndo_vflag
) {
316 ND_PRINT(" (CCVal %u, CsCov %u", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
317 /* checksum calculation */
318 if (ND_TTEST_LEN(bp
, length
)) {
319 uint16_t sum
= 0, dccp_sum
;
321 dccp_sum
= GET_BE_U_2(dh
->dccph_checksum
);
322 ND_PRINT(", cksum 0x%04x ", dccp_sum
);
324 sum
= dccp_cksum(ndo
, ip
, dh
, length
);
325 else if (IP_V(ip
) == 6)
326 sum
= dccp6_cksum(ndo
, ip6
, dh
, length
);
328 ND_PRINT("(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum
, sum
));
330 ND_PRINT("(correct)");
335 dccph_type
= DCCPH_TYPE(dh
);
336 ND_PRINT(" %s ", tok2str(dccp_pkt_type_str
, "packet-type-%u",
338 switch (dccph_type
) {
339 case DCCP_PKT_REQUEST
: {
340 const struct dccp_hdr_request
*dhr
=
341 (const struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
343 ND_ICHECK_U(length
, <, fixed_hdrlen
);
344 ND_PRINT("(service=%u) ", GET_BE_U_4(dhr
->dccph_req_service
));
347 case DCCP_PKT_RESPONSE
: {
348 const struct dccp_hdr_response
*dhr
=
349 (const struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
351 ND_ICHECK_U(length
, <, fixed_hdrlen
);
352 ND_PRINT("(service=%u) ", GET_BE_U_4(dhr
->dccph_resp_service
));
359 ND_ICHECK_U(length
, <, fixed_hdrlen
);
362 case DCCP_PKT_DATAACK
: {
364 ND_ICHECK_U(length
, <, fixed_hdrlen
);
367 case DCCP_PKT_CLOSEREQ
:
369 ND_ICHECK_U(length
, <, fixed_hdrlen
);
373 ND_ICHECK_U(length
, <, fixed_hdrlen
);
375 case DCCP_PKT_RESET
: {
376 const struct dccp_hdr_reset
*dhr
=
377 (const struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
379 ND_ICHECK_U(length
, <, fixed_hdrlen
);
381 ND_PRINT("(code=%s) ", tok2str(dccp_reset_code_str
,
382 "reset-code-%u (invalid)", GET_U_1(dhr
->dccph_reset_code
)));
387 ND_ICHECK_U(length
, <, fixed_hdrlen
);
389 case DCCP_PKT_SYNCACK
:
391 ND_ICHECK_U(length
, <, fixed_hdrlen
);
398 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
399 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
400 dccp_print_ack_no(ndo
, bp
);
402 if (ndo
->ndo_vflag
< 2)
405 ND_PRINT("seq %" PRIu64
, dccp_seqno(ndo
, bp
));
407 /* process options */
408 if (hlen
> fixed_hdrlen
){
410 cp
= bp
+ fixed_hdrlen
;
413 hlen
-= fixed_hdrlen
;
415 optlen
= dccp_print_option(ndo
, cp
, hlen
);
428 nd_print_invalid(ndo
);
431 enum dccp_option_type
{
432 DCCP_OPTION_PADDING
= 0,
433 DCCP_OPTION_MANDATORY
= 1,
434 DCCP_OPTION_SLOW_RECEIVER
= 2,
435 DCCP_OPTION_CHANGE_L
= 32,
436 DCCP_OPTION_CONFIRM_L
= 33,
437 DCCP_OPTION_CHANGE_R
= 34,
438 DCCP_OPTION_CONFIRM_R
= 35,
439 DCCP_OPTION_INIT_COOKIE
= 36,
440 DCCP_OPTION_NDP_COUNT
= 37,
441 DCCP_OPTION_ACK_VECTOR_NONCE_0
= 38,
442 DCCP_OPTION_ACK_VECTOR_NONCE_1
= 39,
443 DCCP_OPTION_DATA_DROPPED
= 40,
444 DCCP_OPTION_TIMESTAMP
= 41,
445 DCCP_OPTION_TIMESTAMP_ECHO
= 42,
446 DCCP_OPTION_ELAPSED_TIME
= 43,
447 DCCP_OPTION_DATA_CHECKSUM
= 44
450 static const struct tok dccp_option_values
[] = {
451 { DCCP_OPTION_PADDING
, "nop" },
452 { DCCP_OPTION_MANDATORY
, "mandatory" },
453 { DCCP_OPTION_SLOW_RECEIVER
, "slowreceiver" },
454 { DCCP_OPTION_CHANGE_L
, "change_l" },
455 { DCCP_OPTION_CONFIRM_L
, "confirm_l" },
456 { DCCP_OPTION_CHANGE_R
, "change_r" },
457 { DCCP_OPTION_CONFIRM_R
, "confirm_r" },
458 { DCCP_OPTION_INIT_COOKIE
, "initcookie" },
459 { DCCP_OPTION_NDP_COUNT
, "ndp_count" },
460 { DCCP_OPTION_ACK_VECTOR_NONCE_0
, "ack_vector0" },
461 { DCCP_OPTION_ACK_VECTOR_NONCE_1
, "ack_vector1" },
462 { DCCP_OPTION_DATA_DROPPED
, "data_dropped" },
463 { DCCP_OPTION_TIMESTAMP
, "timestamp" },
464 { DCCP_OPTION_TIMESTAMP_ECHO
, "timestamp_echo" },
465 { DCCP_OPTION_ELAPSED_TIME
, "elapsed_time" },
466 { DCCP_OPTION_DATA_CHECKSUM
, "data_checksum" },
471 dccp_print_option(netdissect_options
*ndo
, const u_char
*bp
, u_int hlen
)
473 uint8_t option
, optlen
, i
;
475 option
= GET_U_1(bp
);
477 ND_PRINT("CCID option %u", option
);
480 tok2str(dccp_option_values
, "option-type-%u", option
));
482 optlen
= GET_U_1(bp
+ 1);
483 ND_ICHECK_U(optlen
, <, 2);
487 ND_ICHECKMSG_U("remaining length", hlen
, <, optlen
);
492 ND_PRINT(" %u", GET_BE_U_2(bp
+ 2));
495 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
499 for (i
= 0; i
< optlen
- 2; i
++)
500 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));
505 case DCCP_OPTION_PADDING
:
506 case DCCP_OPTION_MANDATORY
:
507 case DCCP_OPTION_SLOW_RECEIVER
:
510 case DCCP_OPTION_CHANGE_L
:
511 case DCCP_OPTION_CHANGE_R
:
512 ND_ICHECK_U(optlen
, <, 4);
513 ND_PRINT(" %s", tok2str(dccp_feature_num_str
,
514 "feature-number-%u (invalid)", GET_U_1(bp
+ 2)));
515 for (i
= 0; i
< optlen
- 3; i
++)
516 ND_PRINT(" %u", GET_U_1(bp
+ 3 + i
));
518 case DCCP_OPTION_CONFIRM_L
:
519 case DCCP_OPTION_CONFIRM_R
:
520 ND_ICHECK_U(optlen
, <, 3);
521 ND_PRINT(" %s", tok2str(dccp_feature_num_str
,
522 "feature-number-%u (invalid)", GET_U_1(bp
+ 2)));
523 for (i
= 0; i
< optlen
- 3; i
++)
524 ND_PRINT(" %u", GET_U_1(bp
+ 3 + i
));
526 case DCCP_OPTION_INIT_COOKIE
:
527 ND_ICHECK_U(optlen
, <, 3);
529 for (i
= 0; i
< optlen
- 2; i
++)
530 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));
532 case DCCP_OPTION_NDP_COUNT
:
533 ND_ICHECK_U(optlen
, <, 3);
534 ND_ICHECK_U(optlen
, >, 8);
535 for (i
= 0; i
< optlen
- 2; i
++)
536 ND_PRINT(" %u", GET_U_1(bp
+ 2 + i
));
538 case DCCP_OPTION_ACK_VECTOR_NONCE_0
:
539 case DCCP_OPTION_ACK_VECTOR_NONCE_1
:
540 ND_ICHECK_U(optlen
, <, 3);
542 for (i
= 0; i
< optlen
- 2; i
++)
543 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));
545 case DCCP_OPTION_DATA_DROPPED
:
546 ND_ICHECK_U(optlen
, <, 3);
548 for (i
= 0; i
< optlen
- 2; i
++)
549 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));
551 case DCCP_OPTION_TIMESTAMP
:
553 * 13.1. Timestamp Option
555 * +--------+--------+--------+--------+--------+--------+
556 * |00101001|00000110| Timestamp Value |
557 * +--------+--------+--------+--------+--------+--------+
560 ND_ICHECK_U(optlen
, !=, 6);
561 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
563 case DCCP_OPTION_TIMESTAMP_ECHO
:
565 * 13.3. Timestamp Echo Option
567 * +--------+--------+--------+--------+--------+--------+
568 * |00101010|00000110| Timestamp Echo |
569 * +--------+--------+--------+--------+--------+--------+
572 * +--------+--------+------- ... -------+--------+--------+
573 * |00101010|00001000| Timestamp Echo | Elapsed Time |
574 * +--------+--------+------- ... -------+--------+--------+
575 * Type=42 Len=8 (4 bytes)
577 * +--------+--------+------- ... -------+------- ... -------+
578 * |00101010|00001010| Timestamp Echo | Elapsed Time |
579 * +--------+--------+------- ... -------+------- ... -------+
580 * Type=42 Len=10 (4 bytes) (4 bytes)
584 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
587 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
588 ND_PRINT(" (elapsed time %u)",
592 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
593 ND_PRINT(" (elapsed time %u)",
597 ND_PRINT(" [optlen != 6 or 8 or 10]");
602 case DCCP_OPTION_ELAPSED_TIME
:
605 ND_PRINT(" %u", GET_BE_U_2(bp
+ 2));
608 ND_PRINT(" %u", GET_BE_U_4(bp
+ 2));
611 ND_PRINT(" [optlen != 4 or 6]");
615 case DCCP_OPTION_DATA_CHECKSUM
:
616 ND_ICHECK_U(optlen
, !=, 6);
618 for (i
= 0; i
< optlen
- 2; i
++)
619 ND_PRINT("%02x", GET_U_1(bp
+ 2 + i
));