]> The Tcpdump Group git mirrors - tcpdump/blob - print-dccp.c
Add {40,48,56}-bit big-endian extract macros and use them for DCCP ACKs.
[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 u_char *ackp = bp + dccp_basic_hdr_len(dh);
230 u_int64_t ackno;
231
232 if (DCCPH_X(dh) != 0) {
233 TCHECK2(*ackp, 8);
234 ackno = EXTRACT_48BITS(ackp + 2);
235 } else {
236 TCHECK2(*ackp, 4);
237 ackno = EXTRACT_24BITS(ackp + 1);
238 }
239
240 (void)printf("(ack=%" PRIu64 ") ", ackno);
241 trunc:
242 return;
243 }
244
245 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type)
246 {
247 if (type == DCCP_PKT_DATA)
248 return 0;
249 if (type == DCCP_PKT_DATAACK ||
250 type == DCCP_PKT_ACK ||
251 type == DCCP_PKT_SYNC ||
252 type == DCCP_PKT_SYNCACK ||
253 type == DCCP_PKT_CLOSE ||
254 type == DCCP_PKT_CLOSEREQ)
255 return sizeof(struct dccp_hdr_ack_bits);
256 if (type == DCCP_PKT_REQUEST)
257 return sizeof(struct dccp_hdr_request);
258 if (type == DCCP_PKT_RESPONSE)
259 return sizeof(struct dccp_hdr_response);
260 return sizeof(struct dccp_hdr_reset);
261 }
262
263 static int dccp_print_option(const u_char *option);
264
265 /**
266 * dccp_print - show dccp packet
267 * @bp - beginning of dccp packet
268 * @data2 - beginning of enclosing
269 * @len - lenght of ip packet
270 */
271 void dccp_print(const u_char *bp, const u_char *data2, u_int len)
272 {
273 const struct dccp_hdr *dh;
274 const struct ip *ip;
275 #ifdef INET6
276 const struct ip6_hdr *ip6;
277 #endif
278 const u_char *cp;
279 u_short sport, dport;
280 u_int hlen;
281 u_int extlen = 0;
282
283 dh = (const struct dccp_hdr *)bp;
284
285 ip = (struct ip *)data2;
286 #ifdef INET6
287 if (IP_V(ip) == 6)
288 ip6 = (const struct ip6_hdr *)data2;
289 else
290 ip6 = NULL;
291 #endif /*INET6*/
292 cp = (const u_char *)(dh + 1);
293 if (cp > snapend) {
294 printf("[Invalid packet|dccp]");
295 return;
296 }
297
298 if (len < sizeof(struct dccp_hdr)) {
299 printf("truncated-dccp - %ld bytes missing!",
300 (long)len - sizeof(struct dccp_hdr));
301 return;
302 }
303
304 sport = EXTRACT_16BITS(&dh->dccph_sport);
305 dport = EXTRACT_16BITS(&dh->dccph_dport);
306 hlen = dh->dccph_doff * 4;
307
308 #ifdef INET6
309 if (ip6) {
310 (void)printf("%s.%d > %s.%d: ",
311 ip6addr_string(&ip6->ip6_src), sport,
312 ip6addr_string(&ip6->ip6_dst), dport);
313 } else
314 #endif /*INET6*/
315 {
316 (void)printf("%s.%d > %s.%d: ",
317 ipaddr_string(&ip->ip_src), sport,
318 ipaddr_string(&ip->ip_dst), dport);
319 }
320 fflush(stdout);
321
322 if (qflag) {
323 (void)printf(" %d", len - hlen);
324 if (hlen > len) {
325 (void)printf("dccp [bad hdr length %u - too long, > %u]",
326 hlen, len);
327 }
328 return;
329 }
330
331 /* other variables in generic header */
332 if (vflag) {
333 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh));
334 }
335
336 /* checksum calculation */
337 if (vflag && TTEST2(bp[0], len)) {
338 u_int16_t sum = 0, dccp_sum;
339
340 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
341 (void)printf("cksum 0x%04x ", dccp_sum);
342 if (IP_V(ip) == 4)
343 sum = dccp_cksum(ip, dh, len);
344 #ifdef INET6
345 else if (IP_V(ip) == 6)
346 sum = dccp6_cksum(ip6, dh, len);
347 #endif
348 if (sum != 0)
349 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
350 else
351 (void)printf("(correct), ");
352 }
353
354 switch (DCCPH_TYPE(dh)) {
355 case DCCP_PKT_REQUEST: {
356 struct dccp_hdr_request *dhr =
357 (struct dccp_hdr_request *)(bp + dccp_basic_hdr_len(dh));
358 TCHECK(*dhr);
359 (void)printf("request (service=%d) ",
360 EXTRACT_32BITS(&dhr->dccph_req_service));
361 extlen += 4;
362 break;
363 }
364 case DCCP_PKT_RESPONSE: {
365 struct dccp_hdr_response *dhr =
366 (struct dccp_hdr_response *)(bp + dccp_basic_hdr_len(dh));
367 TCHECK(*dhr);
368 (void)printf("response (service=%d) ",
369 EXTRACT_32BITS(&dhr->dccph_resp_service));
370 extlen += 12;
371 break;
372 }
373 case DCCP_PKT_DATA:
374 (void)printf("data ");
375 break;
376 case DCCP_PKT_ACK: {
377 (void)printf("ack ");
378 extlen += 8;
379 break;
380 }
381 case DCCP_PKT_DATAACK: {
382 (void)printf("dataack ");
383 extlen += 8;
384 break;
385 }
386 case DCCP_PKT_CLOSEREQ:
387 (void)printf("closereq ");
388 extlen += 8;
389 break;
390 case DCCP_PKT_CLOSE:
391 (void)printf("close ");
392 extlen += 8;
393 break;
394 case DCCP_PKT_RESET: {
395 struct dccp_hdr_reset *dhr =
396 (struct dccp_hdr_reset *)(bp + dccp_basic_hdr_len(dh));
397 TCHECK(*dhr);
398 (void)printf("reset (code=%s) ",
399 dccp_reset_code(dhr->dccph_reset_code));
400 extlen += 12;
401 break;
402 }
403 case DCCP_PKT_SYNC:
404 (void)printf("sync ");
405 extlen += 8;
406 break;
407 case DCCP_PKT_SYNCACK:
408 (void)printf("syncack ");
409 extlen += 8;
410 break;
411 default:
412 (void)printf("invalid ");
413 break;
414 }
415
416 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
417 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
418 dccp_print_ack_no(bp);
419
420 if (vflag < 2)
421 return;
422
423 (void)printf("seq %" PRIu64, dccp_seqno(dh));
424
425 /* process options */
426 if (hlen > dccp_basic_hdr_len(dh) + extlen){
427 const u_char *cp;
428 u_int optlen;
429 cp = bp + dccp_basic_hdr_len(dh) + extlen;
430 printf(" <");
431
432 hlen -= dccp_basic_hdr_len(dh) + extlen;
433 while(1){
434 TCHECK(*cp);
435 optlen = dccp_print_option(cp);
436 if (!optlen) goto trunc2;
437 if (hlen <= optlen) break;
438 hlen -= optlen;
439 cp += optlen;
440 printf(", ");
441 }
442 printf(">");
443 }
444 return;
445 trunc:
446 printf("%s", tstr);
447 trunc2:
448 return;
449 }
450
451 static int dccp_print_option(const u_char *option)
452 {
453 u_int8_t optlen, i;
454
455 TCHECK(*option);
456
457 if (*option >= 32) {
458 TCHECK(*(option+1));
459 optlen = *(option +1);
460 if (optlen < 2) {
461 printf("Option %d optlen too short",*option);
462 return 1;
463 }
464 } else optlen = 1;
465
466 TCHECK2(*option,optlen);
467
468 switch (*option){
469 case 0:
470 printf("nop");
471 break;
472 case 1:
473 printf("mandatory");
474 break;
475 case 2:
476 printf("slowreceiver");
477 break;
478 case 32:
479 printf("change_l");
480 if (*(option +2) < 10){
481 printf(" %s", dccp_feature_nums[*(option +2)]);
482 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
483 }
484 break;
485 case 33:
486 printf("confirm_l");
487 if (*(option +2) < 10){
488 printf(" %s", dccp_feature_nums[*(option +2)]);
489 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
490 }
491 break;
492 case 34:
493 printf("change_r");
494 if (*(option +2) < 10){
495 printf(" %s", dccp_feature_nums[*(option +2)]);
496 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
497 }
498 break;
499 case 35:
500 printf("confirm_r");
501 if (*(option +2) < 10){
502 printf(" %s", dccp_feature_nums[*(option +2)]);
503 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
504 }
505 break;
506 case 36:
507 printf("initcookie 0x");
508 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
509 break;
510 case 37:
511 printf("ndp_count");
512 for (i = 0; i < optlen -2; i ++) printf(" %d", *(option +2 + i));
513 break;
514 case 38:
515 printf("ack_vector0 0x");
516 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
517 break;
518 case 39:
519 printf("ack_vector1 0x");
520 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
521 break;
522 case 40:
523 printf("data_dropped 0x");
524 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
525 break;
526 case 41:
527 printf("timestamp %u", EXTRACT_32BITS(option + 2));
528 break;
529 case 42:
530 printf("timestamp_echo %u", EXTRACT_32BITS(option + 2));
531 break;
532 case 43:
533 printf("elapsed_time ");
534 if (optlen == 6)
535 printf("%u", EXTRACT_32BITS(option + 2));
536 else
537 printf("%u", EXTRACT_16BITS(option + 2));
538 break;
539 case 44:
540 printf("data_checksum ");
541 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
542 break;
543 default :
544 if (*option >= 128) {
545 printf("CCID option %d",*option);
546 switch (optlen) {
547 case 4:
548 printf(" %u", EXTRACT_16BITS(option + 2));
549 break;
550 case 6:
551 printf(" %u", EXTRACT_32BITS(option + 2));
552 break;
553 default:
554 break;
555 }
556 break;
557 }
558
559 printf("unknown_opt %d", *option);
560 break;
561 }
562
563 return optlen;
564 trunc:
565 printf("%s", tstr);
566 return 0;
567 }