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,
158 __DCCP_RESET_CODE_LAST
162 static const char *dccp_reset_codes[] = {
170 "connection_refused",
174 "aggression_penalty",
177 static const char *dccp_feature_nums[] = {
186 "minimum checksum coverage",
187 "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);
216 static const char *dccp_reset_code(uint8_t code)
218 if (code >= __DCCP_RESET_CODE_LAST)
220 return dccp_reset_codes[code];
224 dccp_seqno(netdissect_options *ndo, const u_char *bp)
226 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
229 if (DCCPH_X(dh) != 0) {
230 const struct dccp_hdr_ext *dhx = (const struct dccp_hdr_ext *)bp;
231 seqno = GET_BE_U_6(dhx->dccph_seq);
233 seqno = GET_BE_U_3(dh->dccph_seq);
240 dccp_basic_hdr_len(netdissect_options *ndo, const struct dccp_hdr *dh)
242 return DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : sizeof(struct dccp_hdr);
245 static void dccp_print_ack_no(netdissect_options *ndo, const u_char *bp)
247 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
248 const u_char *ackp = bp + dccp_basic_hdr_len(ndo, dh);
251 if (DCCPH_X(dh) != 0) {
252 ackno = GET_BE_U_6(ackp + 2);
254 ackno = GET_BE_U_3(ackp + 1);
257 ND_PRINT("(ack=%" PRIu64 ") ", ackno);
260 static u_int dccp_print_option(netdissect_options *, const u_char *, u_int);
263 * dccp_print - show dccp packet
264 * @bp - beginning of dccp packet
265 * @data2 - beginning of enclosing
266 * @len - length of ip packet
269 dccp_print(netdissect_options *ndo, const u_char *bp, const u_char *data2,
272 const struct dccp_hdr *dh;
274 const struct ip6_hdr *ip6;
276 u_short sport, dport;
281 ndo->ndo_protocol = "dccp";
282 dh = (const struct dccp_hdr *)bp;
284 ip = (const struct ip *)data2;
286 ip6 = (const struct ip6_hdr *)data2;
290 /* make sure we have enough data to look at the X bit */
291 cp = (const u_char *)(dh + 1);
292 if (cp > ndo->ndo_snapend)
294 if (len < sizeof(struct dccp_hdr)) {
295 ND_PRINT("truncated-dccp - %zu bytes missing!",
296 sizeof(struct dccp_hdr) - len);
300 /* get the length of the generic header */
301 fixed_hdrlen = dccp_basic_hdr_len(ndo, dh);
302 if (len < fixed_hdrlen) {
303 ND_PRINT("truncated-dccp - %u bytes missing!",
307 ND_TCHECK_LEN(dh, fixed_hdrlen);
309 sport = GET_BE_U_2(dh->dccph_sport);
310 dport = GET_BE_U_2(dh->dccph_dport);
311 hlen = GET_U_1(dh->dccph_doff) * 4;
314 ND_PRINT("%s.%u > %s.%u: ",
315 GET_IP6ADDR_STRING(ip6->ip6_src), sport,
316 GET_IP6ADDR_STRING(ip6->ip6_dst), dport);
318 ND_PRINT("%s.%u > %s.%u: ",
319 GET_IPADDR_STRING(ip->ip_src), sport,
320 GET_IPADDR_STRING(ip->ip_dst), dport);
323 nd_print_protocol_caps(ndo);
325 if (ndo->ndo_qflag) {
326 ND_PRINT(" %u", len - hlen);
328 ND_PRINT(" [bad hdr length %u - too long, > %u]",
334 /* other variables in generic header */
335 if (ndo->ndo_vflag) {
336 ND_PRINT(" (CCVal %u, CsCov %u", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh));
339 /* checksum calculation */
340 if (ndo->ndo_vflag && ND_TTEST_LEN(bp, len)) {
341 uint16_t sum = 0, dccp_sum;
343 dccp_sum = GET_BE_U_2(dh->dccph_checksum);
344 ND_PRINT(", cksum 0x%04x ", dccp_sum);
346 sum = dccp_cksum(ndo, ip, dh, len);
347 else if (IP_V(ip) == 6)
348 sum = dccp6_cksum(ndo, ip6, dh, len);
350 ND_PRINT("(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum, sum));
352 ND_PRINT("(correct)");
359 dccph_type = DCCPH_TYPE(dh);
360 switch (dccph_type) {
361 case DCCP_PKT_REQUEST: {
362 const struct dccp_hdr_request *dhr =
363 (const struct dccp_hdr_request *)(bp + fixed_hdrlen);
365 if (len < fixed_hdrlen) {
366 ND_PRINT("truncated-%s - %u bytes missing!",
367 tok2str(dccp_pkt_type_str, "", dccph_type),
372 ND_PRINT("%s (service=%u) ",
373 tok2str(dccp_pkt_type_str, "", dccph_type),
374 GET_BE_U_4(dhr->dccph_req_service));
377 case DCCP_PKT_RESPONSE: {
378 const struct dccp_hdr_response *dhr =
379 (const struct dccp_hdr_response *)(bp + fixed_hdrlen);
381 if (len < fixed_hdrlen) {
382 ND_PRINT("truncated-%s - %u bytes missing!",
383 tok2str(dccp_pkt_type_str, "", dccph_type),
388 ND_PRINT("%s (service=%u) ",
389 tok2str(dccp_pkt_type_str, "", dccph_type),
390 GET_BE_U_4(dhr->dccph_resp_service));
394 ND_PRINT("%s ", tok2str(dccp_pkt_type_str, "", dccph_type));
398 if (len < fixed_hdrlen) {
399 ND_PRINT("truncated-%s - %u bytes missing!",
400 tok2str(dccp_pkt_type_str, "", dccph_type),
404 ND_PRINT("%s ", tok2str(dccp_pkt_type_str, "", dccph_type));
407 case DCCP_PKT_DATAACK: {
409 if (len < fixed_hdrlen) {
410 ND_PRINT("truncated-%s - %u bytes missing!",
411 tok2str(dccp_pkt_type_str, "", dccph_type),
415 ND_PRINT("%s ", tok2str(dccp_pkt_type_str, "", dccph_type));
418 case DCCP_PKT_CLOSEREQ:
420 if (len < fixed_hdrlen) {
421 ND_PRINT("truncated-%s - %u bytes missing!",
422 tok2str(dccp_pkt_type_str, "", dccph_type),
426 ND_PRINT("%s ", tok2str(dccp_pkt_type_str, "", dccph_type));
430 if (len < fixed_hdrlen) {
431 ND_PRINT("truncated-%s - %u bytes missing!",
432 tok2str(dccp_pkt_type_str, "", dccph_type),
436 ND_PRINT("%s ", tok2str(dccp_pkt_type_str, "", dccph_type));
438 case DCCP_PKT_RESET: {
439 const struct dccp_hdr_reset *dhr =
440 (const struct dccp_hdr_reset *)(bp + fixed_hdrlen);
442 if (len < fixed_hdrlen) {
443 ND_PRINT("truncated-%s - %u bytes missing!",
444 tok2str(dccp_pkt_type_str, "", dccph_type),
449 ND_PRINT("%s (code=%s) ",
450 tok2str(dccp_pkt_type_str, "", dccph_type),
451 dccp_reset_code(GET_U_1(dhr->dccph_reset_code)));
456 if (len < fixed_hdrlen) {
457 ND_PRINT("truncated-%s - %u bytes missing!",
458 tok2str(dccp_pkt_type_str, "", dccph_type),
462 ND_PRINT("%s ", tok2str(dccp_pkt_type_str, "", dccph_type));
464 case DCCP_PKT_SYNCACK:
466 if (len < fixed_hdrlen) {
467 ND_PRINT("truncated-%s - %u bytes missing!",
468 tok2str(dccp_pkt_type_str, "", dccph_type),
472 ND_PRINT("%s ", tok2str(dccp_pkt_type_str, "", dccph_type));
475 ND_PRINT("%s ", tok2str(dccp_pkt_type_str, "unknown-type-%u", dccph_type));
479 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
480 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
481 dccp_print_ack_no(ndo, bp);
483 if (ndo->ndo_vflag < 2)
486 ND_PRINT("seq %" PRIu64, dccp_seqno(ndo, bp));
488 /* process options */
489 if (hlen > fixed_hdrlen){
491 cp = bp + fixed_hdrlen;
494 hlen -= fixed_hdrlen;
496 optlen = dccp_print_option(ndo, cp, hlen);
512 static const struct tok dccp_option_values[] = {
515 { 2, "slowreceiver" },
520 { 36, "initcookie" },
522 { 38, "ack_vector0" },
523 { 39, "ack_vector1" },
524 { 40, "data_dropped" },
526 { 42, "timestamp_echo" },
527 { 43, "elapsed_time" },
528 { 44, "data_checksum" },
533 dccp_print_option(netdissect_options *ndo, const u_char *option, u_int hlen)
537 if (GET_U_1(option) >= 32) {
538 optlen = GET_U_1(option + 1);
540 if (GET_U_1(option) >= 128)
541 ND_PRINT("CCID option %u optlen too short",
544 ND_PRINT("%s optlen too short",
545 tok2str(dccp_option_values, "Option %u", GET_U_1(option)));
552 if (GET_U_1(option) >= 128)
553 ND_PRINT("CCID option %u optlen goes past header length",
556 ND_PRINT("%s optlen goes past header length",
557 tok2str(dccp_option_values, "Option %u", GET_U_1(option)));
560 ND_TCHECK_LEN(option, optlen);
562 if (GET_U_1(option) >= 128) {
563 ND_PRINT("CCID option %u", GET_U_1(option));
566 ND_PRINT(" %u", GET_BE_U_2(option + 2));
569 ND_PRINT(" %u", GET_BE_U_4(option + 2));
576 tok2str(dccp_option_values, "Option %u", GET_U_1(option)));
577 switch (GET_U_1(option)) {
583 ND_PRINT(" optlen too short");
586 if (GET_U_1(option + 2) < 10){
588 dccp_feature_nums[GET_U_1(option + 2)]);
589 for (i = 0; i < optlen - 3; i++)
591 GET_U_1(option + 3 + i));
597 for (i = 0; i < optlen - 2; i++)
599 GET_U_1(option + 2 + i));
603 for (i = 0; i < optlen - 2; i++)
604 ND_PRINT(" %u", GET_U_1(option + 2 + i));
609 for (i = 0; i < optlen - 2; i++)
611 GET_U_1(option + 2 + i));
617 for (i = 0; i < optlen - 2; i++)
619 GET_U_1(option + 2 + i));
625 for (i = 0; i < optlen - 2; i++)
627 GET_U_1(option + 2 + i));
632 * 13.1. Timestamp Option
634 * +--------+--------+--------+--------+--------+--------+
635 * |00101001|00000110| Timestamp Value |
636 * +--------+--------+--------+--------+--------+--------+
640 ND_PRINT(" %u", GET_BE_U_4(option + 2));
642 ND_PRINT(" [optlen != 6]");
646 * 13.3. Timestamp Echo Option
648 * +--------+--------+--------+--------+--------+--------+
649 * |00101010|00000110| Timestamp Echo |
650 * +--------+--------+--------+--------+--------+--------+
653 * +--------+--------+------- ... -------+--------+--------+
654 * |00101010|00001000| Timestamp Echo | Elapsed Time |
655 * +--------+--------+------- ... -------+--------+--------+
656 * Type=42 Len=8 (4 bytes)
658 * +--------+--------+------- ... -------+------- ... -------+
659 * |00101010|00001010| Timestamp Echo | Elapsed Time |
660 * +--------+--------+------- ... -------+------- ... -------+
661 * Type=42 Len=10 (4 bytes) (4 bytes)
665 ND_PRINT(" %u", GET_BE_U_4(option + 2));
668 ND_PRINT(" %u", GET_BE_U_4(option + 2));
669 ND_PRINT(" (elapsed time %u)",
670 GET_BE_U_2(option + 6));
673 ND_PRINT(" %u", GET_BE_U_4(option + 2));
674 ND_PRINT(" (elapsed time %u)",
675 GET_BE_U_4(option + 6));
678 ND_PRINT(" [optlen != 6 or 8 or 10]");
684 ND_PRINT(" %u", GET_BE_U_4(option + 2));
685 else if (optlen == 4)
686 ND_PRINT(" %u", GET_BE_U_2(option + 2));
688 ND_PRINT(" [optlen != 4 or 6]");
693 for (i = 0; i < optlen - 2; i++)
695 GET_U_1(option + 2 + i));