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