]> The Tcpdump Group git mirrors - tcpdump/blob - print-dccp.c
Mark structures with UNALIGNED.
[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
30 *
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
40 */
41 struct dccp_hdr {
42 u_int16_t dccph_sport,
43 dccph_dport;
44 u_int8_t dccph_doff;
45 u_int8_t dccph_ccval_cscov;
46 u_int16_t dccph_checksum;
47 union {
48 u_int8_t dccph_xtr;
49 u_int32_t dccph_seq;
50 } dccph_xtrs;
51 } UNALIGNED;
52
53 #define DCCPH_CCVAL(dh) (((dh)->dccph_ccval_cscov >> 4) & 0xF)
54 #define DCCPH_CSCOV(dh) (((dh)->dccph_ccval_cscov) & 0xF)
55
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)
59
60 /**
61 * struct dccp_hdr_ext - the low bits of a 48 bit seq packet
62 *
63 * @dccph_seq_low - low 24 bits of a 48 bit seq packet
64 */
65 struct dccp_hdr_ext {
66 u_int32_t dccph_seq_low;
67 } UNALIGNED;
68
69 /**
70 * struct dccp_hdr_request - Conection initiation request header
71 *
72 * @dccph_req_service - Service to which the client app wants to connect
73 */
74 struct dccp_hdr_request {
75 u_int32_t dccph_req_service;
76 } UNALIGNED;
77
78 /**
79 * struct dccp_hdr_ack_bits - acknowledgment bits common to most packets
80 *
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
83 */
84 struct dccp_hdr_ack_bits {
85 u_int32_t dccph_ra;
86 u_int32_t dccph_ack_nr_low;
87 } UNALIGNED;
88
89 #define DCCPH_ACK(dh_ack) ((dh_ack)->dccph_ra >> 8)
90
91 /**
92 * struct dccp_hdr_response - Conection initiation response header
93 *
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
97 */
98 struct dccp_hdr_response {
99 struct dccp_hdr_ack_bits dccph_resp_ack;
100 u_int32_t dccph_resp_service;
101 } UNALIGNED;
102
103 /**
104 * struct dccp_hdr_reset - Unconditionally shut down a connection
105 *
106 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
107 */
108 struct dccp_hdr_reset {
109 struct dccp_hdr_ack_bits dccph_reset_ack;
110 u_int8_t dccph_reset_code,
111 dccph_reset_data[3];
112 } UNALIGNED;
113
114 enum dccp_pkt_type {
115 DCCP_PKT_REQUEST = 0,
116 DCCP_PKT_RESPONSE,
117 DCCP_PKT_DATA,
118 DCCP_PKT_ACK,
119 DCCP_PKT_DATAACK,
120 DCCP_PKT_CLOSEREQ,
121 DCCP_PKT_CLOSE,
122 DCCP_PKT_RESET,
123 DCCP_PKT_SYNC,
124 DCCP_PKT_SYNCACK,
125 DCCP_PKT_INVALID
126 };
127
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
142 };
143
144 static const char tstr[] = "[|dccp]";
145
146 static const char *dccp_reset_codes[] = {
147 "unspecified",
148 "closed",
149 "aborted",
150 "no_connection",
151 "packet_error",
152 "option_error",
153 "mandatory_error",
154 "connection_refused",
155 "bad_service_code",
156 "too_busy",
157 "bad_init_cookie",
158 "aggression_penalty",
159 };
160
161 static const char *dccp_feature_nums[] = {
162 "reserved",
163 "ccid",
164 "allow_short_seqno",
165 "sequence_window",
166 "ecn_incapable",
167 "ack_ratio",
168 "send_ack_vector",
169 "send_ndp_count",
170 "minimum checksum coverage",
171 "check data checksum",
172 };
173
174 static inline u_int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
175 {
176 u_int cov;
177
178 if (DCCPH_CSCOV(dh) == 0)
179 return len;
180 cov = (dh->dccph_doff + DCCPH_CSCOV(dh) - 1) * sizeof(u_int32_t);
181 return (cov > len)? len : cov;
182 }
183
184 static int dccp_cksum(const struct ip *ip,
185 const struct dccp_hdr *dh, u_int len)
186 {
187 return nextproto4_cksum(ip, (const u_int8_t *)(void *)dh, len,
188 dccp_csum_coverage(dh, len), IPPROTO_DCCP);
189 }
190
191 #ifdef INET6
192 static int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len)
193 {
194 return nextproto6_cksum(ip6, (const u_int8_t *)(void *)dh,
195 dccp_csum_coverage(dh, len), IPPROTO_DCCP);
196 }
197 #endif
198
199 static const char *dccp_reset_code(u_int8_t code)
200 {
201 if (code >= __DCCP_RESET_CODE_LAST)
202 return "invalid";
203 return dccp_reset_codes[code];
204 }
205
206 static u_int64_t dccp_seqno(const struct dccp_hdr *dh)
207 {
208 u_int32_t seq_high = DCCPH_SEQ(dh);
209 u_int64_t seqno = EXTRACT_24BITS(&seq_high) & 0xFFFFFF;
210
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);
216 }
217
218 return seqno;
219 }
220
221 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh)
222 {
223 return sizeof(*dh) + (DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : 0);
224 }
225
226 static void dccp_print_ack_no(const u_char *bp)
227 {
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));
231 u_int32_t ack_high;
232 u_int64_t ackno;
233
234 TCHECK2(*dh_ack,4);
235 ack_high = DCCPH_ACK(dh_ack);
236 ackno = EXTRACT_24BITS(&ack_high) & 0xFFFFFF;
237
238 if (DCCPH_X(dh) != 0) {
239 u_int32_t ack_low;
240
241 TCHECK2(*dh_ack,8);
242 ack_low = dh_ack->dccph_ack_nr_low;
243
244 ackno &= 0x00FFFF; /* clear reserved field */
245 ackno = (ackno << 32) + EXTRACT_32BITS(&ack_low);
246 }
247
248 (void)printf("(ack=%" PRIu64 ") ", ackno);
249 trunc:
250 return;
251 }
252
253 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type)
254 {
255 if (type == DCCP_PKT_DATA)
256 return 0;
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);
269 }
270
271 static int dccp_print_option(const u_char *option);
272
273 /**
274 * dccp_print - show dccp packet
275 * @bp - beginning of dccp packet
276 * @data2 - beginning of enclosing
277 * @len - lenght of ip packet
278 */
279 void dccp_print(const u_char *bp, const u_char *data2, u_int len)
280 {
281 const struct dccp_hdr *dh;
282 const struct ip *ip;
283 #ifdef INET6
284 const struct ip6_hdr *ip6;
285 #endif
286 const u_char *cp;
287 u_short sport, dport;
288 u_int hlen;
289 u_int extlen = 0;
290
291 dh = (const struct dccp_hdr *)bp;
292
293 ip = (struct ip *)data2;
294 #ifdef INET6
295 if (IP_V(ip) == 6)
296 ip6 = (const struct ip6_hdr *)data2;
297 else
298 ip6 = NULL;
299 #endif /*INET6*/
300 cp = (const u_char *)(dh + 1);
301 if (cp > snapend) {
302 printf("[Invalid packet|dccp]");
303 return;
304 }
305
306 if (len < sizeof(struct dccp_hdr)) {
307 printf("truncated-dccp - %ld bytes missing!",
308 (long)len - sizeof(struct dccp_hdr));
309 return;
310 }
311
312 sport = EXTRACT_16BITS(&dh->dccph_sport);
313 dport = EXTRACT_16BITS(&dh->dccph_dport);
314 hlen = dh->dccph_doff * 4;
315
316 #ifdef INET6
317 if (ip6) {
318 (void)printf("%s.%d > %s.%d: ",
319 ip6addr_string(&ip6->ip6_src), sport,
320 ip6addr_string(&ip6->ip6_dst), dport);
321 } else
322 #endif /*INET6*/
323 {
324 (void)printf("%s.%d > %s.%d: ",
325 ipaddr_string(&ip->ip_src), sport,
326 ipaddr_string(&ip->ip_dst), dport);
327 }
328 fflush(stdout);
329
330 if (qflag) {
331 (void)printf(" %d", len - hlen);
332 if (hlen > len) {
333 (void)printf("dccp [bad hdr length %u - too long, > %u]",
334 hlen, len);
335 }
336 return;
337 }
338
339 /* other variables in generic header */
340 if (vflag) {
341 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh));
342 }
343
344 /* checksum calculation */
345 if (vflag && TTEST2(bp[0], len)) {
346 u_int16_t sum = 0, dccp_sum;
347
348 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
349 (void)printf("cksum 0x%04x ", dccp_sum);
350 if (IP_V(ip) == 4)
351 sum = dccp_cksum(ip, dh, len);
352 #ifdef INET6
353 else if (IP_V(ip) == 6)
354 sum = dccp6_cksum(ip6, dh, len);
355 #endif
356 if (sum != 0)
357 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
358 else
359 (void)printf("(correct), ");
360 }
361
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));
366 TCHECK(*dhr);
367 (void)printf("request (service=%d) ",
368 EXTRACT_32BITS(&dhr->dccph_req_service));
369 extlen += 4;
370 break;
371 }
372 case DCCP_PKT_RESPONSE: {
373 struct dccp_hdr_response *dhr =
374 (struct dccp_hdr_response *)(bp + dccp_basic_hdr_len(dh));
375 TCHECK(*dhr);
376 (void)printf("response (service=%d) ",
377 EXTRACT_32BITS(&dhr->dccph_resp_service));
378 extlen += 12;
379 break;
380 }
381 case DCCP_PKT_DATA:
382 (void)printf("data ");
383 break;
384 case DCCP_PKT_ACK: {
385 (void)printf("ack ");
386 extlen += 8;
387 break;
388 }
389 case DCCP_PKT_DATAACK: {
390 (void)printf("dataack ");
391 extlen += 8;
392 break;
393 }
394 case DCCP_PKT_CLOSEREQ:
395 (void)printf("closereq ");
396 extlen += 8;
397 break;
398 case DCCP_PKT_CLOSE:
399 (void)printf("close ");
400 extlen += 8;
401 break;
402 case DCCP_PKT_RESET: {
403 struct dccp_hdr_reset *dhr =
404 (struct dccp_hdr_reset *)(bp + dccp_basic_hdr_len(dh));
405 TCHECK(*dhr);
406 (void)printf("reset (code=%s) ",
407 dccp_reset_code(dhr->dccph_reset_code));
408 extlen += 12;
409 break;
410 }
411 case DCCP_PKT_SYNC:
412 (void)printf("sync ");
413 extlen += 8;
414 break;
415 case DCCP_PKT_SYNCACK:
416 (void)printf("syncack ");
417 extlen += 8;
418 break;
419 default:
420 (void)printf("invalid ");
421 break;
422 }
423
424 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
425 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
426 dccp_print_ack_no(bp);
427
428 if (vflag < 2)
429 return;
430
431 (void)printf("seq %" PRIu64, dccp_seqno(dh));
432
433 /* process options */
434 if (hlen > dccp_basic_hdr_len(dh) + extlen){
435 const u_char *cp;
436 u_int optlen;
437 cp = bp + dccp_basic_hdr_len(dh) + extlen;
438 printf(" <");
439
440 hlen -= dccp_basic_hdr_len(dh) + extlen;
441 while(1){
442 TCHECK(*cp);
443 optlen = dccp_print_option(cp);
444 if (!optlen) goto trunc2;
445 if (hlen <= optlen) break;
446 hlen -= optlen;
447 cp += optlen;
448 printf(", ");
449 }
450 printf(">");
451 }
452 return;
453 trunc:
454 printf("%s", tstr);
455 trunc2:
456 return;
457 }
458
459 static int dccp_print_option(const u_char *option)
460 {
461 u_int8_t optlen, i;
462
463 TCHECK(*option);
464
465 if (*option >= 32) {
466 TCHECK(*(option+1));
467 optlen = *(option +1);
468 if (optlen < 2) {
469 printf("Option %d optlen too short",*option);
470 return 1;
471 }
472 } else optlen = 1;
473
474 TCHECK2(*option,optlen);
475
476 switch (*option){
477 case 0:
478 printf("nop");
479 break;
480 case 1:
481 printf("mandatory");
482 break;
483 case 2:
484 printf("slowreceiver");
485 break;
486 case 32:
487 printf("change_l");
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));
491 }
492 break;
493 case 33:
494 printf("confirm_l");
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));
498 }
499 break;
500 case 34:
501 printf("change_r");
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));
505 }
506 break;
507 case 35:
508 printf("confirm_r");
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));
512 }
513 break;
514 case 36:
515 printf("initcookie 0x");
516 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
517 break;
518 case 37:
519 printf("ndp_count");
520 for (i = 0; i < optlen -2; i ++) printf(" %d", *(option +2 + i));
521 break;
522 case 38:
523 printf("ack_vector0 0x");
524 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
525 break;
526 case 39:
527 printf("ack_vector1 0x");
528 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
529 break;
530 case 40:
531 printf("data_dropped 0x");
532 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
533 break;
534 case 41:
535 printf("timestamp %u", EXTRACT_32BITS(option + 2));
536 break;
537 case 42:
538 printf("timestamp_echo %u", EXTRACT_32BITS(option + 2));
539 break;
540 case 43:
541 printf("elapsed_time ");
542 if (optlen == 6)
543 printf("%u", EXTRACT_32BITS(option + 2));
544 else
545 printf("%u", EXTRACT_16BITS(option + 2));
546 break;
547 case 44:
548 printf("data_checksum ");
549 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
550 break;
551 default :
552 if (*option >= 128) {
553 printf("CCID option %d",*option);
554 switch (optlen) {
555 case 4:
556 printf(" %u", EXTRACT_16BITS(option + 2));
557 break;
558 case 6:
559 printf(" %u", EXTRACT_32BITS(option + 2));
560 break;
561 default:
562 break;
563 }
564 break;
565 }
566
567 printf("unknown_opt %d", *option);
568 break;
569 }
570
571 return optlen;
572 trunc:
573 printf("%s", tstr);
574 return 0;
575 }