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 <tcpdump-stdinc.h>
19 #include "interface.h"
20 #include "addrtoname.h"
21 #include "extract.h" /* must come after interface.h */
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
43 u_int16_t dccph_sport
,
46 u_int8_t dccph_ccval_cscov
;
47 u_int16_t dccph_checksum
;
49 u_int8_t dccph_seq
[3];
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
67 u_int16_t dccph_sport
,
70 u_int8_t dccph_ccval_cscov
;
71 u_int16_t dccph_checksum
;
74 u_int8_t dccph_seq
[6];
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 u_int32_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 u_int8_t dccph_resp_ack
[8]; /* always 8 bytes */
100 u_int32_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 u_int8_t dccph_reset_ack
[8]; /* always 8 bytes */
111 u_int8_t dccph_reset_code
,
116 DCCP_PKT_REQUEST
= 0,
129 enum dccp_reset_codes
{
130 DCCP_RESET_CODE_UNSPECIFIED
= 0,
131 DCCP_RESET_CODE_CLOSED
,
132 DCCP_RESET_CODE_ABORTED
,
133 DCCP_RESET_CODE_NO_CONNECTION
,
134 DCCP_RESET_CODE_PACKET_ERROR
,
135 DCCP_RESET_CODE_OPTION_ERROR
,
136 DCCP_RESET_CODE_MANDATORY_ERROR
,
137 DCCP_RESET_CODE_CONNECTION_REFUSED
,
138 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
139 DCCP_RESET_CODE_TOO_BUSY
,
140 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
141 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
142 __DCCP_RESET_CODE_LAST
145 static const char tstr
[] = "[|dccp]";
147 static const char *dccp_reset_codes
[] = {
155 "connection_refused",
159 "aggression_penalty",
162 static const char *dccp_feature_nums
[] = {
171 "minimum checksum coverage",
172 "check data checksum",
175 static inline u_int
dccp_csum_coverage(const struct dccp_hdr
* dh
, u_int len
)
179 if (DCCPH_CSCOV(dh
) == 0)
181 cov
= (dh
->dccph_doff
+ DCCPH_CSCOV(dh
) - 1) * sizeof(u_int32_t
);
182 return (cov
> len
)? len
: cov
;
185 static int dccp_cksum(const struct ip
*ip
,
186 const struct dccp_hdr
*dh
, u_int len
)
188 return nextproto4_cksum(ip
, (const u_int8_t
*)(void *)dh
, len
,
189 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
193 static int dccp6_cksum(const struct ip6_hdr
*ip6
, const struct dccp_hdr
*dh
, u_int len
)
195 return nextproto6_cksum(ip6
, (const u_int8_t
*)(void *)dh
, len
,
196 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
200 static const char *dccp_reset_code(u_int8_t code
)
202 if (code
>= __DCCP_RESET_CODE_LAST
)
204 return dccp_reset_codes
[code
];
207 static u_int64_t
dccp_seqno(const u_char
*bp
)
209 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
212 if (DCCPH_X(dh
) != 0) {
213 const struct dccp_hdr_ext
*dhx
= (const struct dccp_hdr_ext
*)bp
;
214 seqno
= EXTRACT_48BITS(dhx
->dccph_seq
);
216 seqno
= EXTRACT_24BITS(dh
->dccph_seq
);
222 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
224 return DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : sizeof(struct dccp_hdr
);
227 static void dccp_print_ack_no(netdissect_options
*ndo
, const u_char
*bp
)
229 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
230 const u_char
*ackp
= bp
+ dccp_basic_hdr_len(dh
);
233 if (DCCPH_X(dh
) != 0) {
234 ND_TCHECK2(*ackp
, 8);
235 ackno
= EXTRACT_48BITS(ackp
+ 2);
237 ND_TCHECK2(*ackp
, 4);
238 ackno
= EXTRACT_24BITS(ackp
+ 1);
241 ND_PRINT((ndo
, "(ack=%" PRIu64
") ", ackno
));
246 static int dccp_print_option(netdissect_options
*, const u_char
*, u_int
);
249 * dccp_print - show dccp packet
250 * @bp - beginning of dccp packet
251 * @data2 - beginning of enclosing
252 * @len - lenght of ip packet
254 void dccp_print(netdissect_options
*ndo
, const u_char
*bp
, const u_char
*data2
,
257 const struct dccp_hdr
*dh
;
260 const struct ip6_hdr
*ip6
;
263 u_short sport
, dport
;
267 dh
= (const struct dccp_hdr
*)bp
;
269 ip
= (struct ip
*)data2
;
272 ip6
= (const struct ip6_hdr
*)data2
;
277 /* make sure we have enough data to look at the X bit */
278 cp
= (const u_char
*)(dh
+ 1);
279 if (cp
> ndo
->ndo_snapend
) {
280 ND_PRINT((ndo
, "[Invalid packet|dccp]"));
283 if (len
< sizeof(struct dccp_hdr
)) {
284 ND_PRINT((ndo
, "truncated-dccp - %u bytes missing!",
285 len
- (u_int
)sizeof(struct dccp_hdr
)));
289 /* get the length of the generic header */
290 fixed_hdrlen
= dccp_basic_hdr_len(dh
);
291 if (len
< fixed_hdrlen
) {
292 ND_PRINT((ndo
, "truncated-dccp - %u bytes missing!",
293 len
- fixed_hdrlen
));
296 ND_TCHECK2(*dh
, fixed_hdrlen
);
298 sport
= EXTRACT_16BITS(&dh
->dccph_sport
);
299 dport
= EXTRACT_16BITS(&dh
->dccph_dport
);
300 hlen
= dh
->dccph_doff
* 4;
304 ND_PRINT((ndo
, "%s.%d > %s.%d: ",
305 ip6addr_string(&ip6
->ip6_src
), sport
,
306 ip6addr_string(&ip6
->ip6_dst
), dport
));
310 ND_PRINT((ndo
, "%s.%d > %s.%d: ",
311 ipaddr_string(&ip
->ip_src
), sport
,
312 ipaddr_string(&ip
->ip_dst
), dport
));
316 if (ndo
->ndo_qflag
) {
317 ND_PRINT((ndo
, " %d", len
- hlen
));
319 ND_PRINT((ndo
, "dccp [bad hdr length %u - too long, > %u]",
325 /* other variables in generic header */
326 if (ndo
->ndo_vflag
) {
327 ND_PRINT((ndo
, "CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
)));
330 /* checksum calculation */
331 if (ndo
->ndo_vflag
&& ND_TTEST2(bp
[0], len
)) {
332 u_int16_t sum
= 0, dccp_sum
;
334 dccp_sum
= EXTRACT_16BITS(&dh
->dccph_checksum
);
335 ND_PRINT((ndo
, "cksum 0x%04x ", dccp_sum
));
337 sum
= dccp_cksum(ip
, dh
, len
);
339 else if (IP_V(ip
) == 6)
340 sum
= dccp6_cksum(ip6
, dh
, len
);
343 ND_PRINT((ndo
, "(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum
, sum
)));
345 ND_PRINT((ndo
, "(correct), "));
348 switch (DCCPH_TYPE(dh
)) {
349 case DCCP_PKT_REQUEST
: {
350 struct dccp_hdr_request
*dhr
=
351 (struct dccp_hdr_request
*)(bp
+ fixed_hdrlen
);
353 if (len
< fixed_hdrlen
) {
354 ND_PRINT((ndo
, "truncated-dccp request - %u bytes missing!",
355 len
- fixed_hdrlen
));
359 ND_PRINT((ndo
, "request (service=%d) ",
360 EXTRACT_32BITS(&dhr
->dccph_req_service
)));
363 case DCCP_PKT_RESPONSE
: {
364 struct dccp_hdr_response
*dhr
=
365 (struct dccp_hdr_response
*)(bp
+ fixed_hdrlen
);
367 if (len
< fixed_hdrlen
) {
368 ND_PRINT((ndo
, "truncated-dccp response - %u bytes missing!",
369 len
- fixed_hdrlen
));
373 ND_PRINT((ndo
, "response (service=%d) ",
374 EXTRACT_32BITS(&dhr
->dccph_resp_service
)));
378 ND_PRINT((ndo
, "data "));
382 if (len
< fixed_hdrlen
) {
383 ND_PRINT((ndo
, "truncated-dccp ack - %u bytes missing!",
384 len
- fixed_hdrlen
));
387 ND_PRINT((ndo
, "ack "));
390 case DCCP_PKT_DATAACK
: {
392 if (len
< fixed_hdrlen
) {
393 ND_PRINT((ndo
, "truncated-dccp dataack - %u bytes missing!",
394 len
- fixed_hdrlen
));
397 ND_PRINT((ndo
, "dataack "));
400 case DCCP_PKT_CLOSEREQ
:
402 if (len
< fixed_hdrlen
) {
403 ND_PRINT((ndo
, "truncated-dccp closereq - %u bytes missing!",
404 len
- fixed_hdrlen
));
407 ND_PRINT((ndo
, "closereq "));
411 if (len
< fixed_hdrlen
) {
412 ND_PRINT((ndo
, "truncated-dccp close - %u bytes missing!",
413 len
- fixed_hdrlen
));
416 ND_PRINT((ndo
, "close "));
418 case DCCP_PKT_RESET
: {
419 struct dccp_hdr_reset
*dhr
=
420 (struct dccp_hdr_reset
*)(bp
+ fixed_hdrlen
);
422 if (len
< fixed_hdrlen
) {
423 ND_PRINT((ndo
, "truncated-dccp reset - %u bytes missing!",
424 len
- fixed_hdrlen
));
428 ND_PRINT((ndo
, "reset (code=%s) ",
429 dccp_reset_code(dhr
->dccph_reset_code
)));
434 if (len
< fixed_hdrlen
) {
435 ND_PRINT((ndo
, "truncated-dccp sync - %u bytes missing!",
436 len
- fixed_hdrlen
));
439 ND_PRINT((ndo
, "sync "));
441 case DCCP_PKT_SYNCACK
:
443 if (len
< fixed_hdrlen
) {
444 ND_PRINT((ndo
, "truncated-dccp syncack - %u bytes missing!",
445 len
- fixed_hdrlen
));
448 ND_PRINT((ndo
, "syncack "));
451 ND_PRINT((ndo
, "invalid "));
455 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
456 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
457 dccp_print_ack_no(ndo
, bp
);
459 if (ndo
->ndo_vflag
< 2)
462 ND_PRINT((ndo
, "seq %" PRIu64
, dccp_seqno(bp
)));
464 /* process options */
465 if (hlen
> fixed_hdrlen
){
468 cp
= bp
+ fixed_hdrlen
;
469 ND_PRINT((ndo
, " <"));
471 hlen
-= fixed_hdrlen
;
473 optlen
= dccp_print_option(ndo
, cp
, hlen
);
480 ND_PRINT((ndo
, ", "));
482 ND_PRINT((ndo
, ">"));
486 ND_PRINT((ndo
, "%s", tstr
));
490 static const struct tok dccp_option_values
[] = {
493 { 2, "slowreceiver" },
498 { 36, "initcookie" },
500 { 38, "ack_vector0" },
501 { 39, "ack_vector1" },
502 { 40, "data_dropped" },
504 { 42, "timestamp_echo" },
505 { 43, "elapsed_time" },
506 { 44, "data_checksum" },
510 static int dccp_print_option(netdissect_options
*ndo
, const u_char
*option
, u_int hlen
)
517 ND_TCHECK(*(option
+1));
518 optlen
= *(option
+1);
521 ND_PRINT((ndo
, "CCID option %u optlen too short", *option
));
523 ND_PRINT((ndo
, "%s optlen too short",
524 tok2str(dccp_option_values
, "Option %u", *option
)));
532 ND_PRINT((ndo
, "CCID option %u optlen goes past header length",
535 ND_PRINT((ndo
, "%s optlen goes past header length",
536 tok2str(dccp_option_values
, "Option %u", *option
)));
539 ND_TCHECK2(*option
, optlen
);
541 if (*option
>= 128) {
542 ND_PRINT((ndo
, "CCID option %d", *option
));
545 ND_PRINT((ndo
, " %u", EXTRACT_16BITS(option
+ 2)));
548 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(option
+ 2)));
554 ND_PRINT((ndo
, "%s", tok2str(dccp_option_values
, "Option %u", *option
)));
561 ND_PRINT((ndo
, " optlen too short"));
564 if (*(option
+ 2) < 10){
565 ND_PRINT((ndo
, " %s", dccp_feature_nums
[*(option
+ 2)]));
566 for (i
= 0; i
< optlen
- 3; i
++)
567 ND_PRINT((ndo
, " %d", *(option
+ 3 + i
)));
572 ND_PRINT((ndo
, " 0x"));
573 for (i
= 0; i
< optlen
- 2; i
++)
574 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
578 for (i
= 0; i
< optlen
- 2; i
++)
579 ND_PRINT((ndo
, " %d", *(option
+ 2 + i
)));
583 ND_PRINT((ndo
, " 0x"));
584 for (i
= 0; i
< optlen
- 2; i
++)
585 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
590 ND_PRINT((ndo
, " 0x"));
591 for (i
= 0; i
< optlen
- 2; i
++)
592 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
597 ND_PRINT((ndo
, " 0x"));
598 for (i
= 0; i
< optlen
- 2; i
++)
599 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
604 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(option
+ 2)));
606 ND_PRINT((ndo
, " optlen != 4"));
610 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(option
+ 2)));
612 ND_PRINT((ndo
, " optlen != 4"));
616 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(option
+ 2)));
617 else if (optlen
== 4)
618 ND_PRINT((ndo
, " %u", EXTRACT_16BITS(option
+ 2)));
620 ND_PRINT((ndo
, " optlen != 4 or 6"));
624 ND_PRINT((ndo
, " "));
625 for (i
= 0; i
< optlen
- 2; i
++)
626 ND_PRINT((ndo
, "%02x", *(option
+ 2 + i
)));
634 ND_PRINT((ndo
, "%s", tstr
));