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
31 * @dccph_sport - Relevant port on the endpoint that sent this packet
32 * @dccph_dport - Relevant port on the other endpoint
33 * @dccph_doff - Data Offset from the start of the DCCP header, in 32-bit words
34 * @dccph_ccval - Used by the HC-Sender CCID
35 * @dccph_cscov - Parts of the packet that are covered by the Checksum field
36 * @dccph_checksum - Internet checksum, depends on dccph_cscov
37 * @dccph_x - 0 = 24 bit sequence number, 1 = 48
38 * @dccph_type - packet type, see DCCP_PKT_ prefixed macros
39 * @dccph_seq - sequence number high or low order 24 bits, depends on dccph_x
42 u_int16_t dccph_sport
,
45 u_int8_t dccph_ccval_cscov
;
46 u_int16_t dccph_checksum
;
53 #define DCCPH_CCVAL(dh) (((dh)->dccph_ccval_cscov >> 4) & 0xF)
54 #define DCCPH_CSCOV(dh) (((dh)->dccph_ccval_cscov) & 0xF)
56 #define DCCPH_X(dh) ((dh)->dccph_xtrs.dccph_xtr & 1)
57 #define DCCPH_TYPE(dh) (((dh)->dccph_xtrs.dccph_xtr >> 1) & 0xF)
58 #define DCCPH_SEQ(dh) (((dh)->dccph_xtrs.dccph_seq) >> 8)
61 * struct dccp_hdr_ext - the low bits of a 48 bit seq packet
63 * @dccph_seq_low - low 24 bits of a 48 bit seq packet
66 u_int32_t dccph_seq_low
;
70 * struct dccp_hdr_request - Conection initiation request header
72 * @dccph_req_service - Service to which the client app wants to connect
74 struct dccp_hdr_request
{
75 u_int32_t dccph_req_service
;
79 * struct dccp_hdr_ack_bits - acknowledgment bits common to most packets
81 * @dccph_resp_ack_nr_high - 48 bit ack number high order bits, contains GSR
82 * @dccph_resp_ack_nr_low - 48 bit ack number low order bits, contains GSR
84 struct dccp_hdr_ack_bits
{
86 u_int32_t dccph_ack_nr_low
;
89 #define DCCPH_ACK(dh_ack) ((dh_ack)->dccph_ra >> 8)
92 * struct dccp_hdr_response - Conection initiation response header
94 * @dccph_resp_ack_nr_high - 48 bit ack number high order bits, contains GSR
95 * @dccph_resp_ack_nr_low - 48 bit ack number low order bits, contains GSR
96 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
98 struct dccp_hdr_response
{
99 struct dccp_hdr_ack_bits dccph_resp_ack
;
100 u_int32_t dccph_resp_service
;
104 * struct dccp_hdr_reset - Unconditionally shut down a connection
106 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
108 struct dccp_hdr_reset
{
109 struct dccp_hdr_ack_bits dccph_reset_ack
;
110 u_int8_t dccph_reset_code
,
115 DCCP_PKT_REQUEST
= 0,
128 enum dccp_reset_codes
{
129 DCCP_RESET_CODE_UNSPECIFIED
= 0,
130 DCCP_RESET_CODE_CLOSED
,
131 DCCP_RESET_CODE_ABORTED
,
132 DCCP_RESET_CODE_NO_CONNECTION
,
133 DCCP_RESET_CODE_PACKET_ERROR
,
134 DCCP_RESET_CODE_OPTION_ERROR
,
135 DCCP_RESET_CODE_MANDATORY_ERROR
,
136 DCCP_RESET_CODE_CONNECTION_REFUSED
,
137 DCCP_RESET_CODE_BAD_SERVICE_CODE
,
138 DCCP_RESET_CODE_TOO_BUSY
,
139 DCCP_RESET_CODE_BAD_INIT_COOKIE
,
140 DCCP_RESET_CODE_AGGRESSION_PENALTY
,
141 __DCCP_RESET_CODE_LAST
144 static const char tstr
[] = "[|dccp]";
146 static const char *dccp_reset_codes
[] = {
154 "connection_refused",
158 "aggression_penalty",
161 static const char *dccp_feature_nums
[] = {
170 "minimum checksum coverage",
171 "check data checksum",
174 static inline u_int
dccp_csum_coverage(const struct dccp_hdr
* dh
, u_int len
)
178 if (DCCPH_CSCOV(dh
) == 0)
180 cov
= (dh
->dccph_doff
+ DCCPH_CSCOV(dh
) - 1) * sizeof(u_int32_t
);
181 return (cov
> len
)? len
: cov
;
184 static int dccp_cksum(const struct ip
*ip
,
185 const struct dccp_hdr
*dh
, u_int len
)
187 return nextproto4_cksum(ip
, (const u_int8_t
*)(void *)dh
, len
,
188 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
192 static int dccp6_cksum(const struct ip6_hdr
*ip6
, const struct dccp_hdr
*dh
, u_int len
)
194 return nextproto6_cksum(ip6
, (const u_int8_t
*)(void *)dh
,
195 dccp_csum_coverage(dh
, len
), IPPROTO_DCCP
);
199 static const char *dccp_reset_code(u_int8_t code
)
201 if (code
>= __DCCP_RESET_CODE_LAST
)
203 return dccp_reset_codes
[code
];
206 static u_int64_t
dccp_seqno(const struct dccp_hdr
*dh
)
208 u_int32_t seq_high
= DCCPH_SEQ(dh
);
209 u_int64_t seqno
= EXTRACT_24BITS(&seq_high
) & 0xFFFFFF;
211 if (DCCPH_X(dh
) != 0) {
212 const struct dccp_hdr_ext
*dhx
= (void *)(dh
+ 1);
213 u_int32_t seq_low
= dhx
->dccph_seq_low
;
214 seqno
&= 0x00FFFF; /* clear reserved field */
215 seqno
= (seqno
<< 32) + EXTRACT_32BITS(&seq_low
);
221 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr
*dh
)
223 return sizeof(*dh
) + (DCCPH_X(dh
) ? sizeof(struct dccp_hdr_ext
) : 0);
226 static void dccp_print_ack_no(const u_char
*bp
)
228 const struct dccp_hdr
*dh
= (const struct dccp_hdr
*)bp
;
229 const struct dccp_hdr_ack_bits
*dh_ack
=
230 (struct dccp_hdr_ack_bits
*)(bp
+ dccp_basic_hdr_len(dh
));
235 ack_high
= DCCPH_ACK(dh_ack
);
236 ackno
= EXTRACT_24BITS(&ack_high
) & 0xFFFFFF;
238 if (DCCPH_X(dh
) != 0) {
242 ack_low
= dh_ack
->dccph_ack_nr_low
;
244 ackno
&= 0x00FFFF; /* clear reserved field */
245 ackno
= (ackno
<< 32) + EXTRACT_32BITS(&ack_low
);
248 (void)printf("(ack=%" PRIu64
") ", ackno
);
253 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type
)
255 if (type
== DCCP_PKT_DATA
)
257 if (type
== DCCP_PKT_DATAACK
||
258 type
== DCCP_PKT_ACK
||
259 type
== DCCP_PKT_SYNC
||
260 type
== DCCP_PKT_SYNCACK
||
261 type
== DCCP_PKT_CLOSE
||
262 type
== DCCP_PKT_CLOSEREQ
)
263 return sizeof(struct dccp_hdr_ack_bits
);
264 if (type
== DCCP_PKT_REQUEST
)
265 return sizeof(struct dccp_hdr_request
);
266 if (type
== DCCP_PKT_RESPONSE
)
267 return sizeof(struct dccp_hdr_response
);
268 return sizeof(struct dccp_hdr_reset
);
271 static int dccp_print_option(const u_char
*option
);
274 * dccp_print - show dccp packet
275 * @bp - beginning of dccp packet
276 * @data2 - beginning of enclosing
277 * @len - lenght of ip packet
279 void dccp_print(const u_char
*bp
, const u_char
*data2
, u_int len
)
281 const struct dccp_hdr
*dh
;
284 const struct ip6_hdr
*ip6
;
287 u_short sport
, dport
;
291 dh
= (const struct dccp_hdr
*)bp
;
293 ip
= (struct ip
*)data2
;
296 ip6
= (const struct ip6_hdr
*)data2
;
300 cp
= (const u_char
*)(dh
+ 1);
302 printf("[Invalid packet|dccp]");
306 if (len
< sizeof(struct dccp_hdr
)) {
307 printf("truncated-dccp - %ld bytes missing!",
308 (long)len
- sizeof(struct dccp_hdr
));
312 sport
= EXTRACT_16BITS(&dh
->dccph_sport
);
313 dport
= EXTRACT_16BITS(&dh
->dccph_dport
);
314 hlen
= dh
->dccph_doff
* 4;
318 (void)printf("%s.%d > %s.%d: ",
319 ip6addr_string(&ip6
->ip6_src
), sport
,
320 ip6addr_string(&ip6
->ip6_dst
), dport
);
324 (void)printf("%s.%d > %s.%d: ",
325 ipaddr_string(&ip
->ip_src
), sport
,
326 ipaddr_string(&ip
->ip_dst
), dport
);
331 (void)printf(" %d", len
- hlen
);
333 (void)printf("dccp [bad hdr length %u - too long, > %u]",
339 /* other variables in generic header */
341 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh
), DCCPH_CSCOV(dh
));
344 /* checksum calculation */
345 if (vflag
&& TTEST2(bp
[0], len
)) {
346 u_int16_t sum
= 0, dccp_sum
;
348 dccp_sum
= EXTRACT_16BITS(&dh
->dccph_checksum
);
349 (void)printf("cksum 0x%04x ", dccp_sum
);
351 sum
= dccp_cksum(ip
, dh
, len
);
353 else if (IP_V(ip
) == 6)
354 sum
= dccp6_cksum(ip6
, dh
, len
);
357 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum
, sum
));
359 (void)printf("(correct), ");
362 switch (DCCPH_TYPE(dh
)) {
363 case DCCP_PKT_REQUEST
: {
364 struct dccp_hdr_request
*dhr
=
365 (struct dccp_hdr_request
*)(bp
+ dccp_basic_hdr_len(dh
));
367 (void)printf("request (service=%d) ",
368 EXTRACT_32BITS(&dhr
->dccph_req_service
));
372 case DCCP_PKT_RESPONSE
: {
373 struct dccp_hdr_response
*dhr
=
374 (struct dccp_hdr_response
*)(bp
+ dccp_basic_hdr_len(dh
));
376 (void)printf("response (service=%d) ",
377 EXTRACT_32BITS(&dhr
->dccph_resp_service
));
382 (void)printf("data ");
385 (void)printf("ack ");
389 case DCCP_PKT_DATAACK
: {
390 (void)printf("dataack ");
394 case DCCP_PKT_CLOSEREQ
:
395 (void)printf("closereq ");
399 (void)printf("close ");
402 case DCCP_PKT_RESET
: {
403 struct dccp_hdr_reset
*dhr
=
404 (struct dccp_hdr_reset
*)(bp
+ dccp_basic_hdr_len(dh
));
406 (void)printf("reset (code=%s) ",
407 dccp_reset_code(dhr
->dccph_reset_code
));
412 (void)printf("sync ");
415 case DCCP_PKT_SYNCACK
:
416 (void)printf("syncack ");
420 (void)printf("invalid ");
424 if ((DCCPH_TYPE(dh
) != DCCP_PKT_DATA
) &&
425 (DCCPH_TYPE(dh
) != DCCP_PKT_REQUEST
))
426 dccp_print_ack_no(bp
);
431 (void)printf("seq %" PRIu64
, dccp_seqno(dh
));
433 /* process options */
434 if (hlen
> dccp_basic_hdr_len(dh
) + extlen
){
437 cp
= bp
+ dccp_basic_hdr_len(dh
) + extlen
;
440 hlen
-= dccp_basic_hdr_len(dh
) + extlen
;
443 optlen
= dccp_print_option(cp
);
444 if (!optlen
) goto trunc2
;
445 if (hlen
<= optlen
) break;
459 static int dccp_print_option(const u_char
*option
)
467 optlen
= *(option
+1);
469 printf("Option %d optlen too short",*option
);
474 TCHECK2(*option
,optlen
);
484 printf("slowreceiver");
488 if (*(option
+2) < 10){
489 printf(" %s", dccp_feature_nums
[*(option
+2)]);
490 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
495 if (*(option
+2) < 10){
496 printf(" %s", dccp_feature_nums
[*(option
+2)]);
497 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
502 if (*(option
+2) < 10){
503 printf(" %s", dccp_feature_nums
[*(option
+2)]);
504 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
509 if (*(option
+2) < 10){
510 printf(" %s", dccp_feature_nums
[*(option
+2)]);
511 for (i
= 0; i
< optlen
-3; i
++) printf(" %d", *(option
+3 + i
));
515 printf("initcookie 0x");
516 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
520 for (i
= 0; i
< optlen
-2; i
++) printf(" %d", *(option
+2 + i
));
523 printf("ack_vector0 0x");
524 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
527 printf("ack_vector1 0x");
528 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
531 printf("data_dropped 0x");
532 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
535 printf("timestamp %u", EXTRACT_32BITS(option
+ 2));
538 printf("timestamp_echo %u", EXTRACT_32BITS(option
+ 2));
541 printf("elapsed_time ");
543 printf("%u", EXTRACT_32BITS(option
+ 2));
545 printf("%u", EXTRACT_16BITS(option
+ 2));
548 printf("data_checksum ");
549 for (i
= 0; i
< optlen
-2; i
++) printf("%02x", *(option
+2 + i
));
552 if (*option
>= 128) {
553 printf("CCID option %d",*option
);
556 printf(" %u", EXTRACT_16BITS(option
+ 2));
559 printf(" %u", EXTRACT_32BITS(option
+ 2));
567 printf("unknown_opt %d", *option
);