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
14 #include <netdissect-stdinc.h>
19 #include "netdissect.h"
20 #include "addrtoname.h"
26 /* RFC4340: Datagram Congestion Control Protocol (DCCP) */
29 * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
32 * @dccph_sport - Relevant port on the endpoint that sent this packet
33 * @dccph_dport - Relevant port on the other endpoint
34 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
35 * @dccph_ccval - Used by the HC-Sender CCID
36 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
37 * @dccph_checksum - Internet checksum, depends on dccph_cscov
38 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
39 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
40 * @dccph_seq - 24-bit sequence number
46 uint8_t dccph_ccval_cscov
;
47 uint16_t dccph_checksum
;
53 * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
56 * @dccph_sport - Relevant port on the endpoint that sent this packet
57 * @dccph_dport - Relevant port on the other endpoint
58 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
59 * @dccph_ccval - Used by the HC-Sender CCID
60 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
61 * @dccph_checksum - Internet checksum, depends on dccph_cscov
62 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
63 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
64 * @dccph_seq - 48-bit sequence number
70 uint8_t dccph_ccval_cscov
;
71 uint16_t dccph_checksum
;
77 #define DCCPH_CCVAL(dh) (((dh)->dccph_ccval_cscov >> 4) & 0xF)
78 #define DCCPH_CSCOV(dh) (((dh)->dccph_ccval_cscov) & 0xF)
80 #define DCCPH_X(dh) ((dh)->dccph_xtr & 1)
81 #define DCCPH_TYPE(dh) (((dh)->dccph_xtr >> 1) & 0xF)
84 * struct dccp_hdr_request - Conection initiation request header
86 * @dccph_req_service - Service to which the client app wants to connect
88 struct dccp_hdr_request
{
89 uint32_t dccph_req_service
;
93 * struct dccp_hdr_response - Conection initiation response header
95 * @dccph_resp_ack - 48 bit ack number, contains GSR
96 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
98 struct dccp_hdr_response
{
99 uint8_t dccph_resp_ack
[8]; /* always 8 bytes */
100 uint32_t dccph_resp_service
;
104 * struct dccp_hdr_reset - Unconditionally shut down a connection
106 * @dccph_resp_ack - 48 bit ack number
107 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
109 struct dccp_hdr_reset
{
110 uint8_t dccph_reset_ack
[8]; /* always 8 bytes */
111 uint8_t dccph_reset_code
,
116 DCCP_PKT_REQUEST
= 0,
128 static const struct tok dccp_pkt_type_str
[] = {
129 { DCCP_PKT_REQUEST
, "DCCP-Request" },
130 { DCCP_PKT_RESPONSE
, "DCCP-Response" },
131 { DCCP_PKT_DATA
, "DCCP-Data" },
132 { DCCP_PKT_ACK
, "DCCP-Ack" },
133 { DCCP_PKT_DATAACK
, "DCCP-DataAck" },
134 { DCCP_PKT_CLOSEREQ
, "DCCP-CloseReq" },
135 { DCCP_PKT_CLOSE
, "DCCP-Close" },
136 { DCCP_PKT_RESET
, "DCCP-Reset" },
137 { DCCP_PKT_SYNC
, "DCCP-Sync" },
138 { DCCP_PKT_SYNCACK
, "DCCP-SyncAck" },
142 enum dccp_reset_codes
{
143 DCCP_RESET_CODE_UNSPECIFIED
= 0,
144 DCCP_RESET_CODE_CLOSED
,
145 DCCP_RESET_CODE_ABORTED
,
146 DCCP_RESET_CODE_NO_CONNECTION
,
147 DCCP_RESET_CODE_PACKET_ERROR
,
148 DCCP_RESET_CODE_OPTION_ERROR
,
149 DCCP_RESET_CODE_MANDATORY_ERROR
,
150 DCCP_RESET_CODE_CONNECTION_REFUSED
,
151 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
152 DCCP_RESET_CODE_TOO_BUSY
,
153 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
154 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
155 __DCCP_RESET_CODE_LAST
158 static const char tstr
[] = "[|dccp]";
160 static const char *dccp_reset_codes
[] = {
168 "connection_refused",
172 "aggression_penalty",
175 static const char *dccp_feature_nums
[] = {
184 "minimum checksum coverage",
185 "check data checksum",
188 static inline u_int
dccp_csum_coverage(const struct dccp_hdr
* dh
, u_int len
)
192 if (DCCPH_CSCOV(dh
) == 0)
194 cov
= (dh
->dccph_doff
+ DCCPH_CSCOV(dh
) - 1) * sizeof(uint32_t);
195 return (cov
> len
)? len
: cov
;
198 static int dccp_cksum(netdissect_options
*ndo
, const struct ip
*ip
,
199 const struct dccp_hdr
*dh
, u_int len
)
201 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)(const void *)dh
, len
,
202 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
205 static int dccp6_cksum(const struct ip6_hdr
*ip6
, const struct dccp_hdr
*dh
, u_int len
)
207 return nextproto6_cksum(ip6
, (const uint8_t *)(const void *)dh
, len
,
208 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
211 static const char *dccp_reset_code(uint8_t code
)
213 if (code
>= __DCCP_RESET_CODE_LAST
)
215 return dccp_reset_codes
[code
];
218 static uint64_t dccp_seqno(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
= EXTRACT_48BITS(dhx
->dccph_seq
);
227 seqno
= EXTRACT_24BITS(dh
->dccph_seq
);
233 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
235 return DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : sizeof(struct dccp_hdr
);
238 static void dccp_print_ack_no(netdissect_options
*ndo
, const u_char
*bp
)
240 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
241 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(dh
);
244 if (DCCPH_X(dh
) != 0) {
245 ND_TCHECK2(*ackp
, 8);
246 ackno
= EXTRACT_48BITS(ackp
+ 2);
248 ND_TCHECK2(*ackp
, 4);
249 ackno
= EXTRACT_24BITS(ackp
+ 1);
252 ND_PRINT((ndo
, "(ack=%" PRIu64
") ", ackno
));
257 static int dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
260 * dccp_print - show dccp packet
261 * @bp - beginning of dccp packet
262 * @data2 - beginning of enclosing
263 * @len - lenght of ip packet
265 void dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
268 const struct dccp_hdr
*dh
;
270 const struct ip6_hdr
*ip6
;
272 u_short sport
, dport
;
277 dh
= (const struct dccp_hdr
*)bp
;
279 ip
= (const struct ip
*)data2
;
281 ip6
= (const struct ip6_hdr
*)data2
;
285 /* make sure we have enough data to look at the X bit */
286 cp
= (const u_char
*)(dh
+ 1);
287 if (cp
> ndo
->ndo_snapend
) {
288 ND_PRINT((ndo
, "[Invalid packet|dccp]"));
291 if (len
< sizeof(struct dccp_hdr
)) {
292 ND_PRINT((ndo
, "truncated-dccp - %u bytes missing!",
293 len
- (u_int
)sizeof(struct dccp_hdr
)));
297 /* get the length of the generic header */
298 fixed_hdrlen
= dccp_basic_hdr_len(dh
);
299 if (len
< fixed_hdrlen
) {
300 ND_PRINT((ndo
, "truncated-dccp - %u bytes missing!",
301 len
- fixed_hdrlen
));
304 ND_TCHECK2(*dh
, fixed_hdrlen
);
306 sport
= EXTRACT_16BITS(&dh
->dccph_sport
);
307 dport
= EXTRACT_16BITS(&dh
->dccph_dport
);
308 hlen
= dh
->dccph_doff
* 4;
311 ND_PRINT((ndo
, "%s.%d > %s.%d: ",
312 ip6addr_string(ndo
, &ip6
->ip6_src
), sport
,
313 ip6addr_string(ndo
, &ip6
->ip6_dst
), dport
));
315 ND_PRINT((ndo
, "%s.%d > %s.%d: ",
316 ipaddr_string(ndo
, &ip
->ip_src
), sport
,
317 ipaddr_string(ndo
, &ip
->ip_dst
), dport
));
320 ND_PRINT((ndo
, "DCCP"));
322 if (ndo
->ndo_qflag
) {
323 ND_PRINT((ndo
, " %d", len
- hlen
));
325 ND_PRINT((ndo
, " [bad hdr length %u - too long, > %u]",
331 /* other variables in generic header */
332 if (ndo
->ndo_vflag
) {
333 ND_PRINT((ndo
, " (CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
)));
336 /* checksum calculation */
337 if (ndo
->ndo_vflag
&& ND_TTEST2(bp
[0], len
)) {
338 uint16_t sum
= 0, dccp_sum
;
340 dccp_sum
= EXTRACT_16BITS(&dh
->dccph_checksum
);
341 ND_PRINT((ndo
, "cksum 0x%04x ", dccp_sum
));
343 sum
= dccp_cksum(ndo
, ip
, dh
, len
);
344 else if (IP_V(ip
) == 6)
345 sum
= dccp6_cksum(ip6
, dh
, len
);
347 ND_PRINT((ndo
, "(incorrect -> 0x%04x)",in_cksum_shouldbe(dccp_sum
, sum
)));
349 ND_PRINT((ndo
, "(correct)"));
353 ND_PRINT((ndo
, ")"));
354 ND_PRINT((ndo
, " "));
356 dccph_type
= DCCPH_TYPE(dh
);
357 switch (dccph_type
) {
358 case DCCP_PKT_REQUEST
: {
359 const struct dccp_hdr_request
*dhr
=
360 (const struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
362 if (len
< fixed_hdrlen
) {
363 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
364 tok2str(dccp_pkt_type_str
, "", dccph_type
),
365 len
- fixed_hdrlen
));
369 ND_PRINT((ndo
, "%s (service=%d) ",
370 tok2str(dccp_pkt_type_str
, "", dccph_type
),
371 EXTRACT_32BITS(&dhr
->dccph_req_service
)));
374 case DCCP_PKT_RESPONSE
: {
375 const struct dccp_hdr_response
*dhr
=
376 (const struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
378 if (len
< fixed_hdrlen
) {
379 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
380 tok2str(dccp_pkt_type_str
, "", dccph_type
),
381 len
- fixed_hdrlen
));
385 ND_PRINT((ndo
, "%s (service=%d) ",
386 tok2str(dccp_pkt_type_str
, "", dccph_type
),
387 EXTRACT_32BITS(&dhr
->dccph_resp_service
)));
391 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
395 if (len
< fixed_hdrlen
) {
396 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
397 tok2str(dccp_pkt_type_str
, "", dccph_type
),
398 len
- fixed_hdrlen
));
401 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
404 case DCCP_PKT_DATAACK
: {
406 if (len
< fixed_hdrlen
) {
407 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
408 tok2str(dccp_pkt_type_str
, "", dccph_type
),
409 len
- fixed_hdrlen
));
412 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
415 case DCCP_PKT_CLOSEREQ
:
417 if (len
< fixed_hdrlen
) {
418 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
419 tok2str(dccp_pkt_type_str
, "", dccph_type
),
420 len
- fixed_hdrlen
));
423 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
427 if (len
< fixed_hdrlen
) {
428 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
429 tok2str(dccp_pkt_type_str
, "", dccph_type
),
430 len
- fixed_hdrlen
));
433 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
435 case DCCP_PKT_RESET
: {
436 const struct dccp_hdr_reset
*dhr
=
437 (const struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
439 if (len
< fixed_hdrlen
) {
440 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
441 tok2str(dccp_pkt_type_str
, "", dccph_type
),
442 len
- fixed_hdrlen
));
446 ND_PRINT((ndo
, "%s (code=%s) ",
447 tok2str(dccp_pkt_type_str
, "", dccph_type
),
448 dccp_reset_code(dhr
->dccph_reset_code
)));
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
));
459 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
461 case DCCP_PKT_SYNCACK
:
463 if (len
< fixed_hdrlen
) {
464 ND_PRINT((ndo
, "truncated-%s - %u bytes missing!",
465 tok2str(dccp_pkt_type_str
, "", dccph_type
),
466 len
- fixed_hdrlen
));
469 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "", dccph_type
)));
472 ND_PRINT((ndo
, "%s ", tok2str(dccp_pkt_type_str
, "unknown-type-%u", dccph_type
)));
476 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
477 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
478 dccp_print_ack_no(ndo
, bp
);
480 if (ndo
->ndo_vflag
< 2)
483 ND_PRINT((ndo
, "seq %" PRIu64
, dccp_seqno(bp
)));
485 /* process options */
486 if (hlen
> fixed_hdrlen
){
488 cp
= bp
+ fixed_hdrlen
;
489 ND_PRINT((ndo
, " <"));
491 hlen
-= fixed_hdrlen
;
493 optlen
= dccp_print_option(ndo
, cp
, hlen
);
500 ND_PRINT((ndo
, ", "));
502 ND_PRINT((ndo
, ">"));
506 ND_PRINT((ndo
, "%s", tstr
));
510 static const struct tok dccp_option_values
[] = {
513 { 2, "slowreceiver" },
518 { 36, "initcookie" },
520 { 38, "ack_vector0" },
521 { 39, "ack_vector1" },
522 { 40, "data_dropped" },
524 { 42, "timestamp_echo" },
525 { 43, "elapsed_time" },
526 { 44, "data_checksum" },
530 static int dccp_print_option(netdissect_options
*ndo
, const u_char
*option
, u_int hlen
)
537 ND_TCHECK(*(option
+1));
538 optlen
= *(option
+1);
541 ND_PRINT((ndo
, "CCID option %u optlen too short", *option
));
543 ND_PRINT((ndo
, "%s optlen too short",
544 tok2str(dccp_option_values
, "Option %u", *option
)));
552 ND_PRINT((ndo
, "CCID option %u optlen goes past header length",
555 ND_PRINT((ndo
, "%s optlen goes past header length",
556 tok2str(dccp_option_values
, "Option %u", *option
)));
559 ND_TCHECK2(*option
, optlen
);
561 if (*option
>= 128) {
562 ND_PRINT((ndo
, "CCID option %d", *option
));
565 ND_PRINT((ndo
, " %u", EXTRACT_16BITS(option
+ 2)));
568 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(option
+ 2)));
574 ND_PRINT((ndo
, "%s", tok2str(dccp_option_values
, "Option %u", *option
)));
581 ND_PRINT((ndo
, " optlen too short"));
584 if (*(option
+ 2) < 10){
585 ND_PRINT((ndo
, " %s", dccp_feature_nums
[*(option
+ 2)]));
586 for (i
= 0; i
< optlen
- 3; i
++)
587 ND_PRINT((ndo
, " %d", *(option
+ 3 + i
)));
592 ND_PRINT((ndo
, " 0x"));
593 for (i
= 0; i
< optlen
- 2; i
++)
594 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
598 for (i
= 0; i
< optlen
- 2; i
++)
599 ND_PRINT((ndo
, " %d", *(option
+ 2 + i
)));
603 ND_PRINT((ndo
, " 0x"));
604 for (i
= 0; i
< optlen
- 2; i
++)
605 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
610 ND_PRINT((ndo
, " 0x"));
611 for (i
= 0; i
< optlen
- 2; i
++)
612 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
617 ND_PRINT((ndo
, " 0x"));
618 for (i
= 0; i
< optlen
- 2; i
++)
619 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
624 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(option
+ 2)));
626 ND_PRINT((ndo
, " optlen != 4"));
630 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(option
+ 2)));
632 ND_PRINT((ndo
, " optlen != 4"));
636 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(option
+ 2)));
637 else if (optlen
== 4)
638 ND_PRINT((ndo
, " %u", EXTRACT_16BITS(option
+ 2)));
640 ND_PRINT((ndo
, " optlen != 4 or 6"));
644 ND_PRINT((ndo
, " "));
645 for (i
= 0; i
< optlen
- 2; i
++)
646 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
654 ND_PRINT((ndo
, "%s", tstr
));