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 #define NETDISSECT_REWORKED
15 #include <tcpdump-stdinc.h>
20 #include "interface.h"
21 #include "addrtoname.h"
22 #include "extract.h" /* must come after interface.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
49 uint8_t dccph_ccval_cscov;
50 uint16_t dccph_checksum;
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
73 uint8_t dccph_ccval_cscov;
74 uint16_t dccph_checksum;
80 #define DCCPH_CCVAL(dh) (((dh)->dccph_ccval_cscov >> 4) & 0xF)
81 #define DCCPH_CSCOV(dh) (((dh)->dccph_ccval_cscov) & 0xF)
83 #define DCCPH_X(dh) ((dh)->dccph_xtr & 1)
84 #define DCCPH_TYPE(dh) (((dh)->dccph_xtr >> 1) & 0xF)
87 * struct dccp_hdr_request - Conection initiation request header
89 * @dccph_req_service - Service to which the client app wants to connect
91 struct dccp_hdr_request {
92 uint32_t dccph_req_service;
96 * struct dccp_hdr_response - Conection 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 uint8_t dccph_resp_ack[8]; /* always 8 bytes */
103 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 uint8_t dccph_reset_ack[8]; /* always 8 bytes */
114 uint8_t dccph_reset_code,
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
161 static const char tstr[] = "[|dccp]";
163 static const char *dccp_reset_codes[] = {
171 "connection_refused",
175 "aggression_penalty",
178 static const char *dccp_feature_nums[] = {
187 "minimum checksum coverage",
188 "check data checksum",
191 static inline u_int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
195 if (DCCPH_CSCOV(dh) == 0)
197 cov = (dh->dccph_doff + DCCPH_CSCOV(dh) - 1) * sizeof(uint32_t);
198 return (cov > len)? len : cov;
201 static int 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 *)(void *)dh, len,
205 dccp_csum_coverage(dh, len), IPPROTO_DCCP);
209 static int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len)
211 return nextproto6_cksum(ip6, (const uint8_t *)(void *)dh, len,
212 dccp_csum_coverage(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];
223 static uint64_t dccp_seqno(const u_char *bp)
225 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
228 if (DCCPH_X(dh) != 0) {
229 const struct dccp_hdr_ext *dhx = (const struct dccp_hdr_ext *)bp;
230 seqno = EXTRACT_48BITS(dhx->dccph_seq);
232 seqno = EXTRACT_24BITS(dh->dccph_seq);
238 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh)
240 return DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : sizeof(struct dccp_hdr);
243 static void dccp_print_ack_no(netdissect_options *ndo, const u_char *bp)
245 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
246 const u_char *ackp = bp + dccp_basic_hdr_len(dh);
249 if (DCCPH_X(dh) != 0) {
250 ND_TCHECK2(*ackp, 8);
251 ackno = EXTRACT_48BITS(ackp + 2);
253 ND_TCHECK2(*ackp, 4);
254 ackno = EXTRACT_24BITS(ackp + 1);
257 ND_PRINT((ndo, "(ack=%" PRIu64 ") ", ackno));
262 static 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 - lenght of ip packet
270 void dccp_print(netdissect_options *ndo, const u_char *bp, const u_char *data2,
273 const struct dccp_hdr *dh;
276 const struct ip6_hdr *ip6;
279 u_short sport, dport;
284 dh = (const struct dccp_hdr *)bp;
286 ip = (struct ip *)data2;
289 ip6 = (const struct ip6_hdr *)data2;
294 /* make sure we have enough data to look at the X bit */
295 cp = (const u_char *)(dh + 1);
296 if (cp > ndo->ndo_snapend) {
297 ND_PRINT((ndo, "[Invalid packet|dccp]"));
300 if (len < sizeof(struct dccp_hdr)) {
301 ND_PRINT((ndo, "truncated-dccp - %u bytes missing!",
302 len - (u_int)sizeof(struct dccp_hdr)));
306 /* get the length of the generic header */
307 fixed_hdrlen = dccp_basic_hdr_len(dh);
308 if (len < fixed_hdrlen) {
309 ND_PRINT((ndo, "truncated-dccp - %u bytes missing!",
310 len - fixed_hdrlen));
313 ND_TCHECK2(*dh, fixed_hdrlen);
315 sport = EXTRACT_16BITS(&dh->dccph_sport);
316 dport = EXTRACT_16BITS(&dh->dccph_dport);
317 hlen = dh->dccph_doff * 4;
321 ND_PRINT((ndo, "%s.%d > %s.%d: ",
322 ip6addr_string(ndo, &ip6->ip6_src), sport,
323 ip6addr_string(ndo, &ip6->ip6_dst), dport));
327 ND_PRINT((ndo, "%s.%d > %s.%d: ",
328 ipaddr_string(ndo, &ip->ip_src), sport,
329 ipaddr_string(ndo, &ip->ip_dst), dport));
332 ND_PRINT((ndo, "DCCP"));
334 if (ndo->ndo_qflag) {
335 ND_PRINT((ndo, " %d", len - hlen));
337 ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]",
343 /* other variables in generic header */
344 if (ndo->ndo_vflag) {
345 ND_PRINT((ndo, " (CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh)));
348 /* checksum calculation */
349 if (ndo->ndo_vflag && ND_TTEST2(bp[0], len)) {
350 uint16_t sum = 0, dccp_sum;
352 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
353 ND_PRINT((ndo, "cksum 0x%04x ", dccp_sum));
355 sum = dccp_cksum(ndo, ip, dh, len);
357 else if (IP_V(ip) == 6)
358 sum = dccp6_cksum(ip6, dh, len);
361 ND_PRINT((ndo, "(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum, sum)));
363 ND_PRINT((ndo, "(correct)"));
367 ND_PRINT((ndo, ")"));
368 ND_PRINT((ndo, " "));
370 dccph_type = DCCPH_TYPE(dh);
371 switch (dccph_type) {
372 case DCCP_PKT_REQUEST: {
373 struct dccp_hdr_request *dhr =
374 (struct dccp_hdr_request *)(bp + fixed_hdrlen);
376 if (len < fixed_hdrlen) {
377 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
378 tok2str(dccp_pkt_type_str, "", dccph_type),
379 len - fixed_hdrlen));
383 ND_PRINT((ndo, "%s (service=%d) ",
384 tok2str(dccp_pkt_type_str, "", dccph_type),
385 EXTRACT_32BITS(&dhr->dccph_req_service)));
388 case DCCP_PKT_RESPONSE: {
389 struct dccp_hdr_response *dhr =
390 (struct dccp_hdr_response *)(bp + fixed_hdrlen);
392 if (len < fixed_hdrlen) {
393 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
394 tok2str(dccp_pkt_type_str, "", dccph_type),
395 len - fixed_hdrlen));
399 ND_PRINT((ndo, "%s (service=%d) ",
400 tok2str(dccp_pkt_type_str, "", dccph_type),
401 EXTRACT_32BITS(&dhr->dccph_resp_service)));
405 ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
409 if (len < fixed_hdrlen) {
410 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
411 tok2str(dccp_pkt_type_str, "", dccph_type),
412 len - fixed_hdrlen));
415 ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
418 case DCCP_PKT_DATAACK: {
420 if (len < fixed_hdrlen) {
421 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
422 tok2str(dccp_pkt_type_str, "", dccph_type),
423 len - fixed_hdrlen));
426 ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
429 case DCCP_PKT_CLOSEREQ:
431 if (len < fixed_hdrlen) {
432 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
433 tok2str(dccp_pkt_type_str, "", dccph_type),
434 len - fixed_hdrlen));
437 ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
441 if (len < fixed_hdrlen) {
442 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
443 tok2str(dccp_pkt_type_str, "", dccph_type),
444 len - fixed_hdrlen));
447 ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
449 case DCCP_PKT_RESET: {
450 struct dccp_hdr_reset *dhr =
451 (struct dccp_hdr_reset *)(bp + fixed_hdrlen);
453 if (len < fixed_hdrlen) {
454 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
455 tok2str(dccp_pkt_type_str, "", dccph_type),
456 len - fixed_hdrlen));
460 ND_PRINT((ndo, "%s (code=%s) ",
461 tok2str(dccp_pkt_type_str, "", dccph_type),
462 dccp_reset_code(dhr->dccph_reset_code)));
467 if (len < fixed_hdrlen) {
468 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
469 tok2str(dccp_pkt_type_str, "", dccph_type),
470 len - fixed_hdrlen));
473 ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
475 case DCCP_PKT_SYNCACK:
477 if (len < fixed_hdrlen) {
478 ND_PRINT((ndo, "truncated-%s - %u bytes missing!",
479 tok2str(dccp_pkt_type_str, "", dccph_type),
480 len - fixed_hdrlen));
483 ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "", dccph_type)));
486 ND_PRINT((ndo, "%s ", tok2str(dccp_pkt_type_str, "unknown-type-%u", dccph_type)));
490 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
491 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
492 dccp_print_ack_no(ndo, bp);
494 if (ndo->ndo_vflag < 2)
497 ND_PRINT((ndo, "seq %" PRIu64, dccp_seqno(bp)));
499 /* process options */
500 if (hlen > fixed_hdrlen){
503 cp = bp + fixed_hdrlen;
504 ND_PRINT((ndo, " <"));
506 hlen -= fixed_hdrlen;
508 optlen = dccp_print_option(ndo, cp, hlen);
515 ND_PRINT((ndo, ", "));
517 ND_PRINT((ndo, ">"));
521 ND_PRINT((ndo, "%s", tstr));
525 static const struct tok dccp_option_values[] = {
528 { 2, "slowreceiver" },
533 { 36, "initcookie" },
535 { 38, "ack_vector0" },
536 { 39, "ack_vector1" },
537 { 40, "data_dropped" },
539 { 42, "timestamp_echo" },
540 { 43, "elapsed_time" },
541 { 44, "data_checksum" },
545 static int dccp_print_option(netdissect_options *ndo, const u_char *option, u_int hlen)
552 ND_TCHECK(*(option+1));
553 optlen = *(option +1);
556 ND_PRINT((ndo, "CCID option %u optlen too short", *option));
558 ND_PRINT((ndo, "%s optlen too short",
559 tok2str(dccp_option_values, "Option %u", *option)));
567 ND_PRINT((ndo, "CCID option %u optlen goes past header length",
570 ND_PRINT((ndo, "%s optlen goes past header length",
571 tok2str(dccp_option_values, "Option %u", *option)));
574 ND_TCHECK2(*option, optlen);
576 if (*option >= 128) {
577 ND_PRINT((ndo, "CCID option %d", *option));
580 ND_PRINT((ndo, " %u", EXTRACT_16BITS(option + 2)));
583 ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
589 ND_PRINT((ndo, "%s", tok2str(dccp_option_values, "Option %u", *option)));
596 ND_PRINT((ndo, " optlen too short"));
599 if (*(option + 2) < 10){
600 ND_PRINT((ndo, " %s", dccp_feature_nums[*(option + 2)]));
601 for (i = 0; i < optlen - 3; i++)
602 ND_PRINT((ndo, " %d", *(option + 3 + i)));
607 ND_PRINT((ndo, " 0x"));
608 for (i = 0; i < optlen - 2; i++)
609 ND_PRINT((ndo, "%02x", *(option + 2 + i)));
613 for (i = 0; i < optlen - 2; i++)
614 ND_PRINT((ndo, " %d", *(option + 2 + i)));
618 ND_PRINT((ndo, " 0x"));
619 for (i = 0; i < optlen - 2; i++)
620 ND_PRINT((ndo, "%02x", *(option + 2 + i)));
625 ND_PRINT((ndo, " 0x"));
626 for (i = 0; i < optlen - 2; i++)
627 ND_PRINT((ndo, "%02x", *(option + 2 + i)));
632 ND_PRINT((ndo, " 0x"));
633 for (i = 0; i < optlen - 2; i++)
634 ND_PRINT((ndo, "%02x", *(option + 2 + i)));
639 ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
641 ND_PRINT((ndo, " optlen != 4"));
645 ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
647 ND_PRINT((ndo, " optlen != 4"));
651 ND_PRINT((ndo, " %u", EXTRACT_32BITS(option + 2)));
652 else if (optlen == 4)
653 ND_PRINT((ndo, " %u", EXTRACT_16BITS(option + 2)));
655 ND_PRINT((ndo, " optlen != 4 or 6"));
659 ND_PRINT((ndo, " "));
660 for (i = 0; i < optlen - 2; i++)
661 ND_PRINT((ndo, "%02x", *(option + 2 + i)));
669 ND_PRINT((ndo, "%s", tstr));