]> The Tcpdump Group git mirrors - tcpdump/blob - print-dccp.c
3d996fb7930638e9b586de531fd3f262749740fd
[tcpdump] / print-dccp.c
1 /*
2 * Copyright (C) Arnaldo Carvalho de Melo 2004
3 * Copyright (C) Ian McDonald 2005
4 * Copyright (C) Yoshifumi Nishida 2005
5 *
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
8 */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <tcpdump-stdinc.h>
15
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "interface.h"
20 #include "addrtoname.h"
21 #include "extract.h" /* must come after interface.h */
22 #include "ip.h"
23 #ifdef INET6
24 #include "ip6.h"
25 #endif
26 #include "ipproto.h"
27
28 /**
29 * struct dccp_hdr - generic part of DCCP packet header, with a 24-bit
30 * sequence number
31 *
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
41 */
42 struct dccp_hdr {
43 u_int16_t dccph_sport,
44 dccph_dport;
45 u_int8_t dccph_doff;
46 u_int8_t dccph_ccval_cscov;
47 u_int16_t dccph_checksum;
48 u_int8_t dccph_xtr;
49 u_int8_t dccph_seq[3];
50 } UNALIGNED;
51
52 /**
53 * struct dccp_hdr_ext - generic part of DCCP packet header, with a 48-bit
54 * sequence number
55 *
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
65 */
66 struct dccp_hdr_ext {
67 u_int16_t dccph_sport,
68 dccph_dport;
69 u_int8_t dccph_doff;
70 u_int8_t dccph_ccval_cscov;
71 u_int16_t dccph_checksum;
72 u_int8_t dccph_xtr;
73 u_int8_t reserved;
74 u_int8_t dccph_seq[6];
75 } UNALIGNED;
76
77 #define DCCPH_CCVAL(dh) (((dh)->dccph_ccval_cscov >> 4) & 0xF)
78 #define DCCPH_CSCOV(dh) (((dh)->dccph_ccval_cscov) & 0xF)
79
80 #define DCCPH_X(dh) ((dh)->dccph_xtr & 1)
81 #define DCCPH_TYPE(dh) (((dh)->dccph_xtr >> 1) & 0xF)
82
83 /**
84 * struct dccp_hdr_request - Conection initiation request header
85 *
86 * @dccph_req_service - Service to which the client app wants to connect
87 */
88 struct dccp_hdr_request {
89 u_int32_t dccph_req_service;
90 } UNALIGNED;
91
92 /**
93 * struct dccp_hdr_response - Conection initiation response header
94 *
95 * @dccph_resp_ack - 48 bit ack number, contains GSR
96 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
97 */
98 struct dccp_hdr_response {
99 u_int8_t dccph_resp_ack[8]; /* always 8 bytes */
100 u_int32_t dccph_resp_service;
101 } UNALIGNED;
102
103 /**
104 * struct dccp_hdr_reset - Unconditionally shut down a connection
105 *
106 * @dccph_resp_ack - 48 bit ack number
107 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
108 */
109 struct dccp_hdr_reset {
110 u_int8_t dccph_reset_ack[8]; /* always 8 bytes */
111 u_int8_t dccph_reset_code,
112 dccph_reset_data[3];
113 } UNALIGNED;
114
115 enum dccp_pkt_type {
116 DCCP_PKT_REQUEST = 0,
117 DCCP_PKT_RESPONSE,
118 DCCP_PKT_DATA,
119 DCCP_PKT_ACK,
120 DCCP_PKT_DATAACK,
121 DCCP_PKT_CLOSEREQ,
122 DCCP_PKT_CLOSE,
123 DCCP_PKT_RESET,
124 DCCP_PKT_SYNC,
125 DCCP_PKT_SYNCACK,
126 DCCP_PKT_INVALID
127 };
128
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
143 };
144
145 static const char tstr[] = "[|dccp]";
146
147 static const char *dccp_reset_codes[] = {
148 "unspecified",
149 "closed",
150 "aborted",
151 "no_connection",
152 "packet_error",
153 "option_error",
154 "mandatory_error",
155 "connection_refused",
156 "bad_service_code",
157 "too_busy",
158 "bad_init_cookie",
159 "aggression_penalty",
160 };
161
162 static const char *dccp_feature_nums[] = {
163 "reserved",
164 "ccid",
165 "allow_short_seqno",
166 "sequence_window",
167 "ecn_incapable",
168 "ack_ratio",
169 "send_ack_vector",
170 "send_ndp_count",
171 "minimum checksum coverage",
172 "check data checksum",
173 };
174
175 static inline u_int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
176 {
177 u_int cov;
178
179 if (DCCPH_CSCOV(dh) == 0)
180 return len;
181 cov = (dh->dccph_doff + DCCPH_CSCOV(dh) - 1) * sizeof(u_int32_t);
182 return (cov > len)? len : cov;
183 }
184
185 static int dccp_cksum(const struct ip *ip,
186 const struct dccp_hdr *dh, u_int len)
187 {
188 return nextproto4_cksum(ip, (const u_int8_t *)(void *)dh, len,
189 dccp_csum_coverage(dh, len), IPPROTO_DCCP);
190 }
191
192 #ifdef INET6
193 static int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len)
194 {
195 return nextproto6_cksum(ip6, (const u_int8_t *)(void *)dh, len,
196 dccp_csum_coverage(dh, len), IPPROTO_DCCP);
197 }
198 #endif
199
200 static const char *dccp_reset_code(u_int8_t code)
201 {
202 if (code >= __DCCP_RESET_CODE_LAST)
203 return "invalid";
204 return dccp_reset_codes[code];
205 }
206
207 static u_int64_t dccp_seqno(const u_char *bp)
208 {
209 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
210 u_int64_t seqno;
211
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);
215 } else {
216 seqno = EXTRACT_24BITS(dh->dccph_seq);
217 }
218
219 return seqno;
220 }
221
222 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh)
223 {
224 return DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : sizeof(struct dccp_hdr);
225 }
226
227 static void dccp_print_ack_no(const u_char *bp)
228 {
229 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
230 const u_char *ackp = bp + dccp_basic_hdr_len(dh);
231 u_int64_t ackno;
232
233 if (DCCPH_X(dh) != 0) {
234 TCHECK2(*ackp, 8);
235 ackno = EXTRACT_48BITS(ackp + 2);
236 } else {
237 TCHECK2(*ackp, 4);
238 ackno = EXTRACT_24BITS(ackp + 1);
239 }
240
241 (void)printf("(ack=%" PRIu64 ") ", ackno);
242 trunc:
243 return;
244 }
245
246 static int dccp_print_option(const u_char *option, u_int hlen);
247
248 /**
249 * dccp_print - show dccp packet
250 * @bp - beginning of dccp packet
251 * @data2 - beginning of enclosing
252 * @len - lenght of ip packet
253 */
254 void dccp_print(const u_char *bp, const u_char *data2, u_int len)
255 {
256 const struct dccp_hdr *dh;
257 const struct ip *ip;
258 #ifdef INET6
259 const struct ip6_hdr *ip6;
260 #endif
261 const u_char *cp;
262 u_short sport, dport;
263 u_int hlen;
264 u_int fixed_hdrlen;
265
266 dh = (const struct dccp_hdr *)bp;
267
268 ip = (struct ip *)data2;
269 #ifdef INET6
270 if (IP_V(ip) == 6)
271 ip6 = (const struct ip6_hdr *)data2;
272 else
273 ip6 = NULL;
274 #endif /*INET6*/
275
276 /* make sure we have enough data to look at the X bit */
277 cp = (const u_char *)(dh + 1);
278 if (cp > snapend) {
279 printf("[Invalid packet|dccp]");
280 return;
281 }
282 if (len < sizeof(struct dccp_hdr)) {
283 printf("truncated-dccp - %u bytes missing!",
284 len - (u_int)sizeof(struct dccp_hdr));
285 return;
286 }
287
288 /* get the length of the generic header */
289 fixed_hdrlen = dccp_basic_hdr_len(dh);
290 if (len < fixed_hdrlen) {
291 printf("truncated-dccp - %u bytes missing!",
292 len - fixed_hdrlen);
293 return;
294 }
295 TCHECK2(*dh, fixed_hdrlen);
296
297 sport = EXTRACT_16BITS(&dh->dccph_sport);
298 dport = EXTRACT_16BITS(&dh->dccph_dport);
299 hlen = dh->dccph_doff * 4;
300
301 #ifdef INET6
302 if (ip6) {
303 (void)printf("%s.%d > %s.%d: ",
304 ip6addr_string(&ip6->ip6_src), sport,
305 ip6addr_string(&ip6->ip6_dst), dport);
306 } else
307 #endif /*INET6*/
308 {
309 (void)printf("%s.%d > %s.%d: ",
310 ipaddr_string(&ip->ip_src), sport,
311 ipaddr_string(&ip->ip_dst), dport);
312 }
313 fflush(stdout);
314
315 if (qflag) {
316 (void)printf(" %d", len - hlen);
317 if (hlen > len) {
318 (void)printf("dccp [bad hdr length %u - too long, > %u]",
319 hlen, len);
320 }
321 return;
322 }
323
324 /* other variables in generic header */
325 if (vflag) {
326 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh));
327 }
328
329 /* checksum calculation */
330 if (vflag && TTEST2(bp[0], len)) {
331 u_int16_t sum = 0, dccp_sum;
332
333 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
334 (void)printf("cksum 0x%04x ", dccp_sum);
335 if (IP_V(ip) == 4)
336 sum = dccp_cksum(ip, dh, len);
337 #ifdef INET6
338 else if (IP_V(ip) == 6)
339 sum = dccp6_cksum(ip6, dh, len);
340 #endif
341 if (sum != 0)
342 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
343 else
344 (void)printf("(correct), ");
345 }
346
347 switch (DCCPH_TYPE(dh)) {
348 case DCCP_PKT_REQUEST: {
349 struct dccp_hdr_request *dhr =
350 (struct dccp_hdr_request *)(bp + fixed_hdrlen);
351 fixed_hdrlen += 4;
352 if (len < fixed_hdrlen) {
353 printf("truncated-dccp request - %u bytes missing!",
354 len - fixed_hdrlen);
355 return;
356 }
357 TCHECK(*dhr);
358 (void)printf("request (service=%d) ",
359 EXTRACT_32BITS(&dhr->dccph_req_service));
360 break;
361 }
362 case DCCP_PKT_RESPONSE: {
363 struct dccp_hdr_response *dhr =
364 (struct dccp_hdr_response *)(bp + fixed_hdrlen);
365 fixed_hdrlen += 12;
366 if (len < fixed_hdrlen) {
367 printf("truncated-dccp response - %u bytes missing!",
368 len - fixed_hdrlen);
369 return;
370 }
371 TCHECK(*dhr);
372 (void)printf("response (service=%d) ",
373 EXTRACT_32BITS(&dhr->dccph_resp_service));
374 break;
375 }
376 case DCCP_PKT_DATA:
377 (void)printf("data ");
378 break;
379 case DCCP_PKT_ACK: {
380 fixed_hdrlen += 8;
381 if (len < fixed_hdrlen) {
382 printf("truncated-dccp ack - %u bytes missing!",
383 len - fixed_hdrlen);
384 return;
385 }
386 (void)printf("ack ");
387 break;
388 }
389 case DCCP_PKT_DATAACK: {
390 fixed_hdrlen += 8;
391 if (len < fixed_hdrlen) {
392 printf("truncated-dccp dataack - %u bytes missing!",
393 len - fixed_hdrlen);
394 return;
395 }
396 (void)printf("dataack ");
397 break;
398 }
399 case DCCP_PKT_CLOSEREQ:
400 fixed_hdrlen += 8;
401 if (len < fixed_hdrlen) {
402 printf("truncated-dccp closereq - %u bytes missing!",
403 len - fixed_hdrlen);
404 return;
405 }
406 (void)printf("closereq ");
407 break;
408 case DCCP_PKT_CLOSE:
409 fixed_hdrlen += 8;
410 if (len < fixed_hdrlen) {
411 printf("truncated-dccp close - %u bytes missing!",
412 len - fixed_hdrlen);
413 return;
414 }
415 (void)printf("close ");
416 break;
417 case DCCP_PKT_RESET: {
418 struct dccp_hdr_reset *dhr =
419 (struct dccp_hdr_reset *)(bp + fixed_hdrlen);
420 fixed_hdrlen += 12;
421 if (len < fixed_hdrlen) {
422 printf("truncated-dccp reset - %u bytes missing!",
423 len - fixed_hdrlen);
424 return;
425 }
426 TCHECK(*dhr);
427 (void)printf("reset (code=%s) ",
428 dccp_reset_code(dhr->dccph_reset_code));
429 break;
430 }
431 case DCCP_PKT_SYNC:
432 fixed_hdrlen += 8;
433 if (len < fixed_hdrlen) {
434 printf("truncated-dccp sync - %u bytes missing!",
435 len - fixed_hdrlen);
436 return;
437 }
438 (void)printf("sync ");
439 break;
440 case DCCP_PKT_SYNCACK:
441 fixed_hdrlen += 8;
442 if (len < fixed_hdrlen) {
443 printf("truncated-dccp syncack - %u bytes missing!",
444 len - fixed_hdrlen);
445 return;
446 }
447 (void)printf("syncack ");
448 break;
449 default:
450 (void)printf("invalid ");
451 break;
452 }
453
454 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
455 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
456 dccp_print_ack_no(bp);
457
458 if (vflag < 2)
459 return;
460
461 (void)printf("seq %" PRIu64, dccp_seqno(bp));
462
463 /* process options */
464 if (hlen > fixed_hdrlen){
465 const u_char *cp;
466 u_int optlen;
467 cp = bp + fixed_hdrlen;
468 printf(" <");
469
470 hlen -= fixed_hdrlen;
471 while(1){
472 optlen = dccp_print_option(cp, hlen);
473 if (!optlen)
474 break;
475 if (hlen <= optlen)
476 break;
477 hlen -= optlen;
478 cp += optlen;
479 printf(", ");
480 }
481 printf(">");
482 }
483 return;
484 trunc:
485 printf("%s", tstr);
486 return;
487 }
488
489 static const struct tok dccp_option_values[] = {
490 { 0, "nop" },
491 { 1, "mandatory" },
492 { 2, "slowreceiver" },
493 { 32, "change_l" },
494 { 33, "confirm_l" },
495 { 34, "change_r" },
496 { 35, "confirm_r" },
497 { 36, "initcookie" },
498 { 37, "ndp_count" },
499 { 38, "ack_vector0" },
500 { 39, "ack_vector1" },
501 { 40, "data_dropped" },
502 { 41, "timestamp" },
503 { 42, "timestamp_echo" },
504 { 43, "elapsed_time" },
505 { 44, "data_checksum" },
506 { 0, NULL }
507 };
508
509 static int dccp_print_option(const u_char *option, u_int hlen)
510 {
511 u_int8_t optlen, i;
512
513 TCHECK(*option);
514
515 if (*option >= 32) {
516 TCHECK(*(option+1));
517 optlen = *(option +1);
518 if (optlen < 2) {
519 if (*option >= 128)
520 printf("CCID option %u optlen too short", *option);
521 else
522 printf("%s optlen too short",
523 tok2str(dccp_option_values, "Option %u", *option));
524 return 0;
525 }
526 } else
527 optlen = 1;
528
529 if (hlen < optlen) {
530 if (*option >= 128)
531 printf("CCID option %u optlen goes past header length",
532 *option);
533 else
534 printf("%s optlen goes past header length",
535 tok2str(dccp_option_values, "Option %u", *option));
536 return 0;
537 }
538 TCHECK2(*option, optlen);
539
540 if (*option >= 128) {
541 printf("CCID option %d", *option);
542 switch (optlen) {
543 case 4:
544 printf(" %u", EXTRACT_16BITS(option + 2));
545 break;
546 case 6:
547 printf(" %u", EXTRACT_32BITS(option + 2));
548 break;
549 default:
550 break;
551 }
552 } else {
553 printf("%s", tok2str(dccp_option_values, "Option %u", *option));
554 switch (*option) {
555 case 32:
556 case 33:
557 case 34:
558 case 35:
559 if (optlen < 3) {
560 printf(" optlen too short");
561 return optlen;
562 }
563 if (*(option + 2) < 10){
564 printf(" %s", dccp_feature_nums[*(option + 2)]);
565 for (i = 0; i < optlen - 3; i++)
566 printf(" %d", *(option + 3 + i));
567 }
568 break;
569 case 36:
570 if (optlen > 2) {
571 printf(" 0x");
572 for (i = 0; i < optlen - 2; i++)
573 printf("%02x", *(option + 2 + i));
574 }
575 break;
576 case 37:
577 for (i = 0; i < optlen - 2; i++)
578 printf(" %d", *(option + 2 + i));
579 break;
580 case 38:
581 if (optlen > 2) {
582 printf(" 0x");
583 for (i = 0; i < optlen - 2; i++)
584 printf("%02x", *(option + 2 + i));
585 }
586 break;
587 case 39:
588 if (optlen > 2) {
589 printf(" 0x");
590 for (i = 0; i < optlen - 2; i++)
591 printf("%02x", *(option + 2 + i));
592 }
593 break;
594 case 40:
595 if (optlen > 2) {
596 printf(" 0x");
597 for (i = 0; i < optlen - 2; i++)
598 printf("%02x", *(option + 2 + i));
599 }
600 break;
601 case 41:
602 if (optlen == 4)
603 printf(" %u", EXTRACT_32BITS(option + 2));
604 else
605 printf(" optlen != 4");
606 break;
607 case 42:
608 if (optlen == 4)
609 printf(" %u", EXTRACT_32BITS(option + 2));
610 else
611 printf(" optlen != 4");
612 break;
613 case 43:
614 if (optlen == 6)
615 printf(" %u", EXTRACT_32BITS(option + 2));
616 else if (optlen == 4)
617 printf(" %u", EXTRACT_16BITS(option + 2));
618 else
619 printf(" optlen != 4 or 6");
620 break;
621 case 44:
622 if (optlen > 2) {
623 printf(" ");
624 for (i = 0; i < optlen - 2; i++)
625 printf("%02x", *(option + 2 + i));
626 }
627 break;
628 }
629 }
630
631 return optlen;
632 trunc:
633 printf("%s", tstr);
634 return 0;
635 }