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"
23 #include "netdissect.h"
24 #include "addrtoname.h"
30 /* RFC4340: Datagram Congestion Control Protocol (DCCP) */
33 * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
36 * @dccph_sport - Relevant port on the endpoint that sent this packet
37 * @dccph_dport - Relevant port on the other endpoint
38 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
39 * @dccph_ccval - Used by the HC-Sender CCID
40 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
41 * @dccph_checksum - Internet checksum, depends on dccph_cscov
42 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
43 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
44 * @dccph_seq - 24-bit sequence number
47 nd_uint16_t dccph_sport
,
49 nd_uint8_t dccph_doff
;
50 nd_uint8_t dccph_ccval_cscov
;
51 nd_uint16_t dccph_checksum
;
53 nd_uint24_t dccph_seq
;
57 * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
60 * @dccph_sport - Relevant port on the endpoint that sent this packet
61 * @dccph_dport - Relevant port on the other endpoint
62 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
63 * @dccph_ccval - Used by the HC-Sender CCID
64 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
65 * @dccph_checksum - Internet checksum, depends on dccph_cscov
66 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
67 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
68 * @dccph_seq - 48-bit sequence number
71 nd_uint16_t dccph_sport
,
73 nd_uint8_t dccph_doff
;
74 nd_uint8_t dccph_ccval_cscov
;
75 nd_uint16_t dccph_checksum
;
78 nd_uint48_t dccph_seq
;
81 #define DCCPH_CCVAL(dh) ((EXTRACT_U_1((dh)->dccph_ccval_cscov) >> 4) & 0xF)
82 #define DCCPH_CSCOV(dh) (EXTRACT_U_1((dh)->dccph_ccval_cscov) & 0xF)
84 #define DCCPH_X(dh) (EXTRACT_U_1((dh)->dccph_xtr) & 1)
85 #define DCCPH_TYPE(dh) ((EXTRACT_U_1((dh)->dccph_xtr) >> 1) & 0xF)
88 * struct dccp_hdr_request - Conection initiation request header
90 * @dccph_req_service - Service to which the client app wants to connect
92 struct dccp_hdr_request
{
93 nd_uint32_t dccph_req_service
;
97 * struct dccp_hdr_response - Conection initiation response header
99 * @dccph_resp_ack - 48 bit ack number, contains GSR
100 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
102 struct dccp_hdr_response
{
103 nd_uint64_t dccph_resp_ack
; /* always 8 bytes, first 2 reserved */
104 nd_uint32_t dccph_resp_service
;
108 * struct dccp_hdr_reset - Unconditionally shut down a connection
110 * @dccph_resp_ack - 48 bit ack number
111 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
113 struct dccp_hdr_reset
{
114 nd_uint64_t dccph_reset_ack
; /* always 8 bytes, first 2 reserved */
115 nd_uint8_t dccph_reset_code
;
116 nd_uint8_t dccph_reset_data1
;
117 nd_uint8_t dccph_reset_data2
;
118 nd_uint8_t dccph_reset_data3
;
122 DCCP_PKT_REQUEST
= 0,
134 static const struct tok dccp_pkt_type_str
[] = {
135 { DCCP_PKT_REQUEST
, "DCCP-Request" },
136 { DCCP_PKT_RESPONSE
, "DCCP-Response" },
137 { DCCP_PKT_DATA
, "DCCP-Data" },
138 { DCCP_PKT_ACK
, "DCCP-Ack" },
139 { DCCP_PKT_DATAACK
, "DCCP-DataAck" },
140 { DCCP_PKT_CLOSEREQ
, "DCCP-CloseReq" },
141 { DCCP_PKT_CLOSE
, "DCCP-Close" },
142 { DCCP_PKT_RESET
, "DCCP-Reset" },
143 { DCCP_PKT_SYNC
, "DCCP-Sync" },
144 { DCCP_PKT_SYNCACK
, "DCCP-SyncAck" },
148 enum dccp_reset_codes
{
149 DCCP_RESET_CODE_UNSPECIFIED
= 0,
150 DCCP_RESET_CODE_CLOSED
,
151 DCCP_RESET_CODE_ABORTED
,
152 DCCP_RESET_CODE_NO_CONNECTION
,
153 DCCP_RESET_CODE_PACKET_ERROR
,
154 DCCP_RESET_CODE_OPTION_ERROR
,
155 DCCP_RESET_CODE_MANDATORY_ERROR
,
156 DCCP_RESET_CODE_CONNECTION_REFUSED
,
157 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
158 DCCP_RESET_CODE_TOO_BUSY
,
159 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
160 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
161 __DCCP_RESET_CODE_LAST
164 static const char tstr
[] = "[|dccp]";
166 static const char *dccp_reset_codes
[] = {
174 "connection_refused",
178 "aggression_penalty",
181 static const char *dccp_feature_nums
[] = {
190 "minimum checksum coverage",
191 "check data checksum",
195 dccp_csum_coverage(const struct dccp_hdr
* dh
, u_int len
)
199 if (DCCPH_CSCOV(dh
) == 0)
201 cov
= (EXTRACT_U_1(dh
->dccph_doff
) + DCCPH_CSCOV(dh
) - 1) * sizeof(uint32_t);
202 return (cov
> len
)? len
: cov
;
205 static int dccp_cksum(netdissect_options
*ndo
, const struct ip
*ip
,
206 const struct dccp_hdr
*dh
, u_int len
)
208 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)(const void *)dh
, len
,
209 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
212 static int dccp6_cksum(netdissect_options
*ndo
, const struct ip6_hdr
*ip6
,
213 const struct dccp_hdr
*dh
, u_int len
)
215 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)(const void *)dh
, len
,
216 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
219 static const char *dccp_reset_code(uint8_t code
)
221 if (code
>= __DCCP_RESET_CODE_LAST
)
223 return dccp_reset_codes
[code
];
226 static uint64_t dccp_seqno(const u_char
*bp
)
228 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
231 if (DCCPH_X(dh
) != 0) {
232 const struct dccp_hdr_ext
*dhx
= (const struct dccp_hdr_ext
*)bp
;
233 seqno
= EXTRACT_BE_U_6(dhx
->dccph_seq
);
235 seqno
= EXTRACT_BE_U_3(dh
->dccph_seq
);
242 dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
244 return DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : sizeof(struct dccp_hdr
);
247 static void dccp_print_ack_no(netdissect_options
*ndo
, const u_char
*bp
)
249 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
250 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(dh
);
253 if (DCCPH_X(dh
) != 0) {
255 ackno
= EXTRACT_BE_U_6(ackp
+ 2);
258 ackno
= EXTRACT_BE_U_3(ackp
+ 1);
261 ND_PRINT("(ack=%" PRIu64
") ", ackno
);
266 static u_int
dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
269 * dccp_print - show dccp packet
270 * @bp - beginning of dccp packet
271 * @data2 - beginning of enclosing
272 * @len - lenght of ip packet
275 dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
278 const struct dccp_hdr
*dh
;
280 const struct ip6_hdr
*ip6
;
282 u_short sport
, dport
;
287 dh
= (const struct dccp_hdr
*)bp
;
289 ip
= (const struct ip
*)data2
;
291 ip6
= (const struct ip6_hdr
*)data2
;
295 /* make sure we have enough data to look at the X bit */
296 cp
= (const u_char
*)(dh
+ 1);
297 if (cp
> ndo
->ndo_snapend
) {
298 ND_PRINT("[Invalid packet|dccp]");
301 if (len
< sizeof(struct dccp_hdr
)) {
302 ND_PRINT("truncated-dccp - %u bytes missing!",
303 len
- (u_int
)sizeof(struct dccp_hdr
));
307 /* get the length of the generic header */
308 fixed_hdrlen
= dccp_basic_hdr_len(dh
);
309 if (len
< fixed_hdrlen
) {
310 ND_PRINT("truncated-dccp - %u bytes missing!",
314 ND_TCHECK_LEN(dh
, fixed_hdrlen
);
316 sport
= EXTRACT_BE_U_2(dh
->dccph_sport
);
317 dport
= EXTRACT_BE_U_2(dh
->dccph_dport
);
318 hlen
= EXTRACT_U_1(dh
->dccph_doff
) * 4;
321 ND_PRINT("%s.%u > %s.%u: ",
322 ip6addr_string(ndo
, ip6
->ip6_src
), sport
,
323 ip6addr_string(ndo
, ip6
->ip6_dst
), dport
);
325 ND_PRINT("%s.%u > %s.%u: ",
326 ipaddr_string(ndo
, ip
->ip_src
), sport
,
327 ipaddr_string(ndo
, ip
->ip_dst
), dport
);
332 if (ndo
->ndo_qflag
) {
333 ND_PRINT(" %u", len
- hlen
);
335 ND_PRINT(" [bad hdr length %u - too long, > %u]",
341 /* other variables in generic header */
342 if (ndo
->ndo_vflag
) {
343 ND_PRINT(" (CCVal %u, CsCov %u, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
346 /* checksum calculation */
347 if (ndo
->ndo_vflag
&& ND_TTEST_LEN(bp
, len
)) {
348 uint16_t sum
= 0, dccp_sum
;
350 dccp_sum
= EXTRACT_BE_U_2(dh
->dccph_checksum
);
351 ND_PRINT("cksum 0x%04x ", dccp_sum
);
353 sum
= dccp_cksum(ndo
, ip
, dh
, len
);
354 else if (IP_V(ip
) == 6)
355 sum
= dccp6_cksum(ndo
, ip6
, dh
, len
);
357 ND_PRINT("(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum
, sum
));
359 ND_PRINT("(correct)");
366 dccph_type
= DCCPH_TYPE(dh
);
367 switch (dccph_type
) {
368 case DCCP_PKT_REQUEST
: {
369 const struct dccp_hdr_request
*dhr
=
370 (const struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
372 if (len
< fixed_hdrlen
) {
373 ND_PRINT("truncated-%s - %u bytes missing!",
374 tok2str(dccp_pkt_type_str
, "", dccph_type
),
379 ND_PRINT("%s (service=%u) ",
380 tok2str(dccp_pkt_type_str
, "", dccph_type
),
381 EXTRACT_BE_U_4(dhr
->dccph_req_service
));
384 case DCCP_PKT_RESPONSE
: {
385 const struct dccp_hdr_response
*dhr
=
386 (const struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
388 if (len
< fixed_hdrlen
) {
389 ND_PRINT("truncated-%s - %u bytes missing!",
390 tok2str(dccp_pkt_type_str
, "", dccph_type
),
395 ND_PRINT("%s (service=%u) ",
396 tok2str(dccp_pkt_type_str
, "", dccph_type
),
397 EXTRACT_BE_U_4(dhr
->dccph_resp_service
));
401 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
405 if (len
< fixed_hdrlen
) {
406 ND_PRINT("truncated-%s - %u bytes missing!",
407 tok2str(dccp_pkt_type_str
, "", dccph_type
),
411 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
414 case DCCP_PKT_DATAACK
: {
416 if (len
< fixed_hdrlen
) {
417 ND_PRINT("truncated-%s - %u bytes missing!",
418 tok2str(dccp_pkt_type_str
, "", dccph_type
),
422 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
425 case DCCP_PKT_CLOSEREQ
:
427 if (len
< fixed_hdrlen
) {
428 ND_PRINT("truncated-%s - %u bytes missing!",
429 tok2str(dccp_pkt_type_str
, "", dccph_type
),
433 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
437 if (len
< fixed_hdrlen
) {
438 ND_PRINT("truncated-%s - %u bytes missing!",
439 tok2str(dccp_pkt_type_str
, "", dccph_type
),
443 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
445 case DCCP_PKT_RESET
: {
446 const struct dccp_hdr_reset
*dhr
=
447 (const struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
449 if (len
< fixed_hdrlen
) {
450 ND_PRINT("truncated-%s - %u bytes missing!",
451 tok2str(dccp_pkt_type_str
, "", dccph_type
),
456 ND_PRINT("%s (code=%s) ",
457 tok2str(dccp_pkt_type_str
, "", dccph_type
),
458 dccp_reset_code(EXTRACT_U_1(dhr
->dccph_reset_code
)));
463 if (len
< fixed_hdrlen
) {
464 ND_PRINT("truncated-%s - %u bytes missing!",
465 tok2str(dccp_pkt_type_str
, "", dccph_type
),
469 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
471 case DCCP_PKT_SYNCACK
:
473 if (len
< fixed_hdrlen
) {
474 ND_PRINT("truncated-%s - %u bytes missing!",
475 tok2str(dccp_pkt_type_str
, "", dccph_type
),
479 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
482 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "unknown-type-%u", dccph_type
));
486 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
487 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
488 dccp_print_ack_no(ndo
, bp
);
490 if (ndo
->ndo_vflag
< 2)
493 ND_PRINT("seq %" PRIu64
, dccp_seqno(bp
));
495 /* process options */
496 if (hlen
> fixed_hdrlen
){
498 cp
= bp
+ fixed_hdrlen
;
501 hlen
-= fixed_hdrlen
;
503 optlen
= dccp_print_option(ndo
, cp
, hlen
);
516 ND_PRINT("%s", tstr
);
520 static const struct tok dccp_option_values
[] = {
523 { 2, "slowreceiver" },
528 { 36, "initcookie" },
530 { 38, "ack_vector0" },
531 { 39, "ack_vector1" },
532 { 40, "data_dropped" },
534 { 42, "timestamp_echo" },
535 { 43, "elapsed_time" },
536 { 44, "data_checksum" },
540 static u_int
dccp_print_option(netdissect_options
*ndo
, const u_char
*option
, u_int hlen
)
546 if (EXTRACT_U_1(option
) >= 32) {
547 ND_TCHECK_1(option
+ 1);
548 optlen
= EXTRACT_U_1(option
+ 1);
550 if (EXTRACT_U_1(option
) >= 128)
551 ND_PRINT("CCID option %u optlen too short", EXTRACT_U_1(option
));
553 ND_PRINT("%s optlen too short",
554 tok2str(dccp_option_values
, "Option %u", EXTRACT_U_1(option
)));
561 if (EXTRACT_U_1(option
) >= 128)
562 ND_PRINT("CCID option %u optlen goes past header length",
563 EXTRACT_U_1(option
));
565 ND_PRINT("%s optlen goes past header length",
566 tok2str(dccp_option_values
, "Option %u", EXTRACT_U_1(option
)));
569 ND_TCHECK_LEN(option
, optlen
);
571 if (EXTRACT_U_1(option
) >= 128) {
572 ND_PRINT("CCID option %u", EXTRACT_U_1(option
));
575 ND_PRINT(" %u", EXTRACT_BE_U_2(option
+ 2));
578 ND_PRINT(" %u", EXTRACT_BE_U_4(option
+ 2));
584 ND_PRINT("%s", tok2str(dccp_option_values
, "Option %u", EXTRACT_U_1(option
)));
585 switch (EXTRACT_U_1(option
)) {
591 ND_PRINT(" optlen too short");
594 if (EXTRACT_U_1(option
+ 2) < 10){
595 ND_PRINT(" %s", dccp_feature_nums
[EXTRACT_U_1(option
+ 2)]);
596 for (i
= 0; i
< optlen
- 3; i
++)
597 ND_PRINT(" %u", EXTRACT_U_1(option
+ 3 + i
));
603 for (i
= 0; i
< optlen
- 2; i
++)
604 ND_PRINT("%02x", EXTRACT_U_1(option
+ 2 + i
));
608 for (i
= 0; i
< optlen
- 2; i
++)
609 ND_PRINT(" %u", EXTRACT_U_1(option
+ 2 + i
));
614 for (i
= 0; i
< optlen
- 2; i
++)
615 ND_PRINT("%02x", EXTRACT_U_1(option
+ 2 + i
));
621 for (i
= 0; i
< optlen
- 2; i
++)
622 ND_PRINT("%02x", EXTRACT_U_1(option
+ 2 + i
));
628 for (i
= 0; i
< optlen
- 2; i
++)
629 ND_PRINT("%02x", EXTRACT_U_1(option
+ 2 + i
));
634 ND_PRINT(" %u", EXTRACT_BE_U_4(option
+ 2));
636 ND_PRINT(" optlen != 4");
640 ND_PRINT(" %u", EXTRACT_BE_U_4(option
+ 2));
642 ND_PRINT(" optlen != 4");
646 ND_PRINT(" %u", EXTRACT_BE_U_4(option
+ 2));
647 else if (optlen
== 4)
648 ND_PRINT(" %u", EXTRACT_BE_U_2(option
+ 2));
650 ND_PRINT(" optlen != 4 or 6");
655 for (i
= 0; i
< optlen
- 2; i
++)
656 ND_PRINT("%02x", EXTRACT_U_1(option
+ 2 + i
));
664 ND_PRINT("%s", tstr
);