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"
22 #include "netdissect.h"
23 #include "addrtoname.h"
29 /* RFC4340: Datagram Congestion Control Protocol (DCCP) */
32 * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
35 * @dccph_sport - Relevant port on the endpoint that sent this packet
36 * @dccph_dport - Relevant port on the other endpoint
37 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
38 * @dccph_ccval - Used by the HC-Sender CCID
39 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
40 * @dccph_checksum - Internet checksum, depends on dccph_cscov
41 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
42 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
43 * @dccph_seq - 24-bit sequence number
46 nd_uint16_t dccph_sport
,
48 nd_uint8_t dccph_doff
;
49 nd_uint8_t dccph_ccval_cscov
;
50 nd_uint16_t dccph_checksum
;
52 nd_uint24_t dccph_seq
;
56 * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
59 * @dccph_sport - Relevant port on the endpoint that sent this packet
60 * @dccph_dport - Relevant port on the other endpoint
61 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
62 * @dccph_ccval - Used by the HC-Sender CCID
63 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
64 * @dccph_checksum - Internet checksum, depends on dccph_cscov
65 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
66 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
67 * @dccph_seq - 48-bit sequence number
70 nd_uint16_t dccph_sport
,
72 nd_uint8_t dccph_doff
;
73 nd_uint8_t dccph_ccval_cscov
;
74 nd_uint16_t dccph_checksum
;
77 nd_uint48_t dccph_seq
;
80 #define DCCPH_CCVAL(dh) ((GET_U_1((dh)->dccph_ccval_cscov) >> 4) & 0xF)
81 #define DCCPH_CSCOV(dh) (GET_U_1((dh)->dccph_ccval_cscov) & 0xF)
83 #define DCCPH_X(dh) (GET_U_1((dh)->dccph_xtr) & 1)
84 #define DCCPH_TYPE(dh) ((GET_U_1((dh)->dccph_xtr) >> 1) & 0xF)
87 * struct dccp_hdr_request - Connection initiation request header
89 * @dccph_req_service - Service to which the client app wants to connect
91 struct dccp_hdr_request
{
92 nd_uint32_t dccph_req_service
;
96 * struct dccp_hdr_response - Connection initiation response header
98 * @dccph_resp_ack - 48 bit ack number, contains GSR
99 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
101 struct dccp_hdr_response
{
102 nd_uint64_t dccph_resp_ack
; /* always 8 bytes, first 2 reserved */
103 nd_uint32_t dccph_resp_service
;
107 * struct dccp_hdr_reset - Unconditionally shut down a connection
109 * @dccph_resp_ack - 48 bit ack number
110 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
112 struct dccp_hdr_reset
{
113 nd_uint64_t dccph_reset_ack
; /* always 8 bytes, first 2 reserved */
114 nd_uint8_t dccph_reset_code
;
115 nd_uint8_t dccph_reset_data1
;
116 nd_uint8_t dccph_reset_data2
;
117 nd_uint8_t dccph_reset_data3
;
121 DCCP_PKT_REQUEST
= 0,
133 static const struct tok dccp_pkt_type_str
[] = {
134 { DCCP_PKT_REQUEST
, "DCCP-Request" },
135 { DCCP_PKT_RESPONSE
, "DCCP-Response" },
136 { DCCP_PKT_DATA
, "DCCP-Data" },
137 { DCCP_PKT_ACK
, "DCCP-Ack" },
138 { DCCP_PKT_DATAACK
, "DCCP-DataAck" },
139 { DCCP_PKT_CLOSEREQ
, "DCCP-CloseReq" },
140 { DCCP_PKT_CLOSE
, "DCCP-Close" },
141 { DCCP_PKT_RESET
, "DCCP-Reset" },
142 { DCCP_PKT_SYNC
, "DCCP-Sync" },
143 { DCCP_PKT_SYNCACK
, "DCCP-SyncAck" },
147 enum dccp_reset_codes
{
148 DCCP_RESET_CODE_UNSPECIFIED
= 0,
149 DCCP_RESET_CODE_CLOSED
,
150 DCCP_RESET_CODE_ABORTED
,
151 DCCP_RESET_CODE_NO_CONNECTION
,
152 DCCP_RESET_CODE_PACKET_ERROR
,
153 DCCP_RESET_CODE_OPTION_ERROR
,
154 DCCP_RESET_CODE_MANDATORY_ERROR
,
155 DCCP_RESET_CODE_CONNECTION_REFUSED
,
156 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
157 DCCP_RESET_CODE_TOO_BUSY
,
158 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
159 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
160 __DCCP_RESET_CODE_LAST
164 static const char *dccp_reset_codes
[] = {
172 "connection_refused",
176 "aggression_penalty",
179 static const char *dccp_feature_nums
[] = {
188 "minimum checksum coverage",
189 "check data checksum",
193 dccp_csum_coverage(netdissect_options
*ndo
,
194 const struct dccp_hdr
* dh
, u_int len
)
198 if (DCCPH_CSCOV(dh
) == 0)
200 cov
= (GET_U_1(dh
->dccph_doff
) + DCCPH_CSCOV(dh
) - 1) * sizeof(uint32_t);
201 return (cov
> len
)? len
: cov
;
204 static uint16_t dccp_cksum(netdissect_options
*ndo
, const struct ip
*ip
,
205 const struct dccp_hdr
*dh
, u_int len
)
207 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)(const void *)dh
, len
,
208 dccp_csum_coverage(ndo
, dh
, len
), IPPROTO_DCCP
);
211 static uint16_t dccp6_cksum(netdissect_options
*ndo
, const struct ip6_hdr
*ip6
,
212 const struct dccp_hdr
*dh
, u_int len
)
214 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)(const void *)dh
, len
,
215 dccp_csum_coverage(ndo
, dh
, len
), IPPROTO_DCCP
);
218 static const char *dccp_reset_code(uint8_t code
)
220 if (code
>= __DCCP_RESET_CODE_LAST
)
222 return dccp_reset_codes
[code
];
226 dccp_seqno(netdissect_options
*ndo
, 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
= GET_BE_U_6(dhx
->dccph_seq
);
235 seqno
= GET_BE_U_3(dh
->dccph_seq
);
242 dccp_basic_hdr_len(netdissect_options
*ndo
, 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(ndo
, dh
);
253 if (DCCPH_X(dh
) != 0) {
254 ackno
= GET_BE_U_6(ackp
+ 2);
256 ackno
= GET_BE_U_3(ackp
+ 1);
259 ND_PRINT("(ack=%" PRIu64
") ", ackno
);
262 static u_int
dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
265 * dccp_print - show dccp packet
266 * @bp - beginning of dccp packet
267 * @data2 - beginning of enclosing
268 * @len - length of ip packet
271 dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
274 const struct dccp_hdr
*dh
;
276 const struct ip6_hdr
*ip6
;
278 u_short sport
, dport
;
283 ndo
->ndo_protocol
= "dccp";
284 dh
= (const struct dccp_hdr
*)bp
;
286 ip
= (const struct ip
*)data2
;
288 ip6
= (const struct ip6_hdr
*)data2
;
292 /* make sure we have enough data to look at the X bit */
293 cp
= (const u_char
*)(dh
+ 1);
294 if (cp
> ndo
->ndo_snapend
)
296 if (len
< sizeof(struct dccp_hdr
)) {
297 ND_PRINT("truncated-dccp - %u bytes missing!",
298 (u_int
)sizeof(struct dccp_hdr
) - len
);
302 /* get the length of the generic header */
303 fixed_hdrlen
= dccp_basic_hdr_len(ndo
, dh
);
304 if (len
< fixed_hdrlen
) {
305 ND_PRINT("truncated-dccp - %u bytes missing!",
309 ND_TCHECK_LEN(dh
, fixed_hdrlen
);
311 sport
= GET_BE_U_2(dh
->dccph_sport
);
312 dport
= GET_BE_U_2(dh
->dccph_dport
);
313 hlen
= GET_U_1(dh
->dccph_doff
) * 4;
316 ND_PRINT("%s.%u > %s.%u: ",
317 GET_IP6ADDR_STRING(ip6
->ip6_src
), sport
,
318 GET_IP6ADDR_STRING(ip6
->ip6_dst
), dport
);
320 ND_PRINT("%s.%u > %s.%u: ",
321 GET_IPADDR_STRING(ip
->ip_src
), sport
,
322 GET_IPADDR_STRING(ip
->ip_dst
), dport
);
325 nd_print_protocol_caps(ndo
);
327 if (ndo
->ndo_qflag
) {
328 ND_PRINT(" %u", len
- hlen
);
330 ND_PRINT(" [bad hdr length %u - too long, > %u]",
336 /* other variables in generic header */
337 if (ndo
->ndo_vflag
) {
338 ND_PRINT(" (CCVal %u, CsCov %u, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
341 /* checksum calculation */
342 if (ndo
->ndo_vflag
&& ND_TTEST_LEN(bp
, len
)) {
343 uint16_t sum
= 0, dccp_sum
;
345 dccp_sum
= GET_BE_U_2(dh
->dccph_checksum
);
346 ND_PRINT("cksum 0x%04x ", dccp_sum
);
348 sum
= dccp_cksum(ndo
, ip
, dh
, len
);
349 else if (IP_V(ip
) == 6)
350 sum
= dccp6_cksum(ndo
, ip6
, dh
, len
);
352 ND_PRINT("(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum
, sum
));
354 ND_PRINT("(correct)");
361 dccph_type
= DCCPH_TYPE(dh
);
362 switch (dccph_type
) {
363 case DCCP_PKT_REQUEST
: {
364 const struct dccp_hdr_request
*dhr
=
365 (const struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
367 if (len
< fixed_hdrlen
) {
368 ND_PRINT("truncated-%s - %u bytes missing!",
369 tok2str(dccp_pkt_type_str
, "", dccph_type
),
374 ND_PRINT("%s (service=%u) ",
375 tok2str(dccp_pkt_type_str
, "", dccph_type
),
376 GET_BE_U_4(dhr
->dccph_req_service
));
379 case DCCP_PKT_RESPONSE
: {
380 const struct dccp_hdr_response
*dhr
=
381 (const struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
383 if (len
< fixed_hdrlen
) {
384 ND_PRINT("truncated-%s - %u bytes missing!",
385 tok2str(dccp_pkt_type_str
, "", dccph_type
),
390 ND_PRINT("%s (service=%u) ",
391 tok2str(dccp_pkt_type_str
, "", dccph_type
),
392 GET_BE_U_4(dhr
->dccph_resp_service
));
396 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
400 if (len
< fixed_hdrlen
) {
401 ND_PRINT("truncated-%s - %u bytes missing!",
402 tok2str(dccp_pkt_type_str
, "", dccph_type
),
406 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
409 case DCCP_PKT_DATAACK
: {
411 if (len
< fixed_hdrlen
) {
412 ND_PRINT("truncated-%s - %u bytes missing!",
413 tok2str(dccp_pkt_type_str
, "", dccph_type
),
417 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
420 case DCCP_PKT_CLOSEREQ
:
422 if (len
< fixed_hdrlen
) {
423 ND_PRINT("truncated-%s - %u bytes missing!",
424 tok2str(dccp_pkt_type_str
, "", dccph_type
),
428 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
432 if (len
< fixed_hdrlen
) {
433 ND_PRINT("truncated-%s - %u bytes missing!",
434 tok2str(dccp_pkt_type_str
, "", dccph_type
),
438 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
440 case DCCP_PKT_RESET
: {
441 const struct dccp_hdr_reset
*dhr
=
442 (const struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
444 if (len
< fixed_hdrlen
) {
445 ND_PRINT("truncated-%s - %u bytes missing!",
446 tok2str(dccp_pkt_type_str
, "", dccph_type
),
451 ND_PRINT("%s (code=%s) ",
452 tok2str(dccp_pkt_type_str
, "", dccph_type
),
453 dccp_reset_code(GET_U_1(dhr
->dccph_reset_code
)));
458 if (len
< fixed_hdrlen
) {
459 ND_PRINT("truncated-%s - %u bytes missing!",
460 tok2str(dccp_pkt_type_str
, "", dccph_type
),
464 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
466 case DCCP_PKT_SYNCACK
:
468 if (len
< fixed_hdrlen
) {
469 ND_PRINT("truncated-%s - %u bytes missing!",
470 tok2str(dccp_pkt_type_str
, "", dccph_type
),
474 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
));
477 ND_PRINT("%s ", tok2str(dccp_pkt_type_str
, "unknown-type-%u", dccph_type
));
481 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
482 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
483 dccp_print_ack_no(ndo
, bp
);
485 if (ndo
->ndo_vflag
< 2)
488 ND_PRINT("seq %" PRIu64
, dccp_seqno(ndo
, bp
));
490 /* process options */
491 if (hlen
> fixed_hdrlen
){
493 cp
= bp
+ fixed_hdrlen
;
496 hlen
-= fixed_hdrlen
;
498 optlen
= dccp_print_option(ndo
, cp
, hlen
);
514 static const struct tok dccp_option_values
[] = {
517 { 2, "slowreceiver" },
522 { 36, "initcookie" },
524 { 38, "ack_vector0" },
525 { 39, "ack_vector1" },
526 { 40, "data_dropped" },
528 { 42, "timestamp_echo" },
529 { 43, "elapsed_time" },
530 { 44, "data_checksum" },
535 dccp_print_option(netdissect_options
*ndo
, const u_char
*option
, u_int hlen
)
539 if (GET_U_1(option
) >= 32) {
540 optlen
= GET_U_1(option
+ 1);
542 if (GET_U_1(option
) >= 128)
543 ND_PRINT("CCID option %u optlen too short",
546 ND_PRINT("%s optlen too short",
547 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
554 if (GET_U_1(option
) >= 128)
555 ND_PRINT("CCID option %u optlen goes past header length",
558 ND_PRINT("%s optlen goes past header length",
559 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
562 ND_TCHECK_LEN(option
, optlen
);
564 if (GET_U_1(option
) >= 128) {
565 ND_PRINT("CCID option %u", GET_U_1(option
));
568 ND_PRINT(" %u", GET_BE_U_2(option
+ 2));
571 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
578 tok2str(dccp_option_values
, "Option %u", GET_U_1(option
)));
579 switch (GET_U_1(option
)) {
585 ND_PRINT(" optlen too short");
588 if (GET_U_1(option
+ 2) < 10){
590 dccp_feature_nums
[GET_U_1(option
+ 2)]);
591 for (i
= 0; i
< optlen
- 3; i
++)
593 GET_U_1(option
+ 3 + i
));
599 for (i
= 0; i
< optlen
- 2; i
++)
601 GET_U_1(option
+ 2 + i
));
605 for (i
= 0; i
< optlen
- 2; i
++)
606 ND_PRINT(" %u", GET_U_1(option
+ 2 + i
));
611 for (i
= 0; i
< optlen
- 2; i
++)
613 GET_U_1(option
+ 2 + i
));
619 for (i
= 0; i
< optlen
- 2; i
++)
621 GET_U_1(option
+ 2 + i
));
627 for (i
= 0; i
< optlen
- 2; i
++)
629 GET_U_1(option
+ 2 + i
));
634 * 13.1. Timestamp Option
636 * +--------+--------+--------+--------+--------+--------+
637 * |00101001|00000110| Timestamp Value |
638 * +--------+--------+--------+--------+--------+--------+
642 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
644 ND_PRINT(" [optlen != 6]");
648 * 13.3. Timestamp Echo Option
650 * +--------+--------+--------+--------+--------+--------+
651 * |00101010|00000110| Timestamp Echo |
652 * +--------+--------+--------+--------+--------+--------+
655 * +--------+--------+------- ... -------+--------+--------+
656 * |00101010|00001000| Timestamp Echo | Elapsed Time |
657 * +--------+--------+------- ... -------+--------+--------+
658 * Type=42 Len=8 (4 bytes)
660 * +--------+--------+------- ... -------+------- ... -------+
661 * |00101010|00001010| Timestamp Echo | Elapsed Time |
662 * +--------+--------+------- ... -------+------- ... -------+
663 * Type=42 Len=10 (4 bytes) (4 bytes)
667 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
670 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
671 ND_PRINT(" (elapsed time %u)",
672 GET_BE_U_2(option
+ 6));
675 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
676 ND_PRINT(" (elapsed time %u)",
677 GET_BE_U_4(option
+ 6));
680 ND_PRINT(" [optlen != 6 or 8 or 10]");
686 ND_PRINT(" %u", GET_BE_U_4(option
+ 2));
687 else if (optlen
== 4)
688 ND_PRINT(" %u", GET_BE_U_2(option
+ 2));
690 ND_PRINT(" [optlen != 4 or 6]");
695 for (i
= 0; i
< optlen
- 2; i
++)
697 GET_U_1(option
+ 2 + i
));