]> The Tcpdump Group git mirrors - tcpdump/blob - print-dccp.c
626853c2d7944053b0f8e758083e6cad8ec61b5d
[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_response - Conection initiation response header
80 *
81 * @dccph_resp_ack - 48 bit ack number, contains GSR
82 * @dccph_resp_service - Echoes the Service Code on a received DCCP-Request
83 */
84 struct dccp_hdr_response {
85 u_int8_t dccph_resp_ack[8]; /* always 8 bytes */
86 u_int32_t dccph_resp_service;
87 } UNALIGNED;
88
89 /**
90 * struct dccp_hdr_reset - Unconditionally shut down a connection
91 *
92 * @dccph_resp_ack - 48 bit ack number
93 * @dccph_reset_service - Echoes the Service Code on a received DCCP-Request
94 */
95 struct dccp_hdr_reset {
96 u_int8_t dccph_reset_ack[8]; /* always 8 bytes */
97 u_int8_t dccph_reset_code,
98 dccph_reset_data[3];
99 } UNALIGNED;
100
101 enum dccp_pkt_type {
102 DCCP_PKT_REQUEST = 0,
103 DCCP_PKT_RESPONSE,
104 DCCP_PKT_DATA,
105 DCCP_PKT_ACK,
106 DCCP_PKT_DATAACK,
107 DCCP_PKT_CLOSEREQ,
108 DCCP_PKT_CLOSE,
109 DCCP_PKT_RESET,
110 DCCP_PKT_SYNC,
111 DCCP_PKT_SYNCACK,
112 DCCP_PKT_INVALID
113 };
114
115 enum dccp_reset_codes {
116 DCCP_RESET_CODE_UNSPECIFIED = 0,
117 DCCP_RESET_CODE_CLOSED,
118 DCCP_RESET_CODE_ABORTED,
119 DCCP_RESET_CODE_NO_CONNECTION,
120 DCCP_RESET_CODE_PACKET_ERROR,
121 DCCP_RESET_CODE_OPTION_ERROR,
122 DCCP_RESET_CODE_MANDATORY_ERROR,
123 DCCP_RESET_CODE_CONNECTION_REFUSED,
124 DCCP_RESET_CODE_BAD_SERVICE_CODE,
125 DCCP_RESET_CODE_TOO_BUSY,
126 DCCP_RESET_CODE_BAD_INIT_COOKIE,
127 DCCP_RESET_CODE_AGGRESSION_PENALTY,
128 __DCCP_RESET_CODE_LAST
129 };
130
131 static const char tstr[] = "[|dccp]";
132
133 static const char *dccp_reset_codes[] = {
134 "unspecified",
135 "closed",
136 "aborted",
137 "no_connection",
138 "packet_error",
139 "option_error",
140 "mandatory_error",
141 "connection_refused",
142 "bad_service_code",
143 "too_busy",
144 "bad_init_cookie",
145 "aggression_penalty",
146 };
147
148 static const char *dccp_feature_nums[] = {
149 "reserved",
150 "ccid",
151 "allow_short_seqno",
152 "sequence_window",
153 "ecn_incapable",
154 "ack_ratio",
155 "send_ack_vector",
156 "send_ndp_count",
157 "minimum checksum coverage",
158 "check data checksum",
159 };
160
161 static inline u_int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
162 {
163 u_int cov;
164
165 if (DCCPH_CSCOV(dh) == 0)
166 return len;
167 cov = (dh->dccph_doff + DCCPH_CSCOV(dh) - 1) * sizeof(u_int32_t);
168 return (cov > len)? len : cov;
169 }
170
171 static int dccp_cksum(const struct ip *ip,
172 const struct dccp_hdr *dh, u_int len)
173 {
174 return nextproto4_cksum(ip, (const u_int8_t *)(void *)dh, len,
175 dccp_csum_coverage(dh, len), IPPROTO_DCCP);
176 }
177
178 #ifdef INET6
179 static int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len)
180 {
181 return nextproto6_cksum(ip6, (const u_int8_t *)(void *)dh,
182 dccp_csum_coverage(dh, len), IPPROTO_DCCP);
183 }
184 #endif
185
186 static const char *dccp_reset_code(u_int8_t code)
187 {
188 if (code >= __DCCP_RESET_CODE_LAST)
189 return "invalid";
190 return dccp_reset_codes[code];
191 }
192
193 static u_int64_t dccp_seqno(const struct dccp_hdr *dh)
194 {
195 u_int32_t seq_high = DCCPH_SEQ(dh);
196 u_int64_t seqno = EXTRACT_24BITS(&seq_high) & 0xFFFFFF;
197
198 if (DCCPH_X(dh) != 0) {
199 const struct dccp_hdr_ext *dhx = (void *)(dh + 1);
200 u_int32_t seq_low = dhx->dccph_seq_low;
201 seqno &= 0x00FFFF; /* clear reserved field */
202 seqno = (seqno << 32) + EXTRACT_32BITS(&seq_low);
203 }
204
205 return seqno;
206 }
207
208 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh)
209 {
210 return sizeof(*dh) + (DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : 0);
211 }
212
213 static void dccp_print_ack_no(const u_char *bp)
214 {
215 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
216 const u_char *ackp = bp + dccp_basic_hdr_len(dh);
217 u_int64_t ackno;
218
219 if (DCCPH_X(dh) != 0) {
220 TCHECK2(*ackp, 8);
221 ackno = EXTRACT_48BITS(ackp + 2);
222 } else {
223 TCHECK2(*ackp, 4);
224 ackno = EXTRACT_24BITS(ackp + 1);
225 }
226
227 (void)printf("(ack=%" PRIu64 ") ", ackno);
228 trunc:
229 return;
230 }
231
232 static int dccp_print_option(const u_char *option);
233
234 /**
235 * dccp_print - show dccp packet
236 * @bp - beginning of dccp packet
237 * @data2 - beginning of enclosing
238 * @len - lenght of ip packet
239 */
240 void dccp_print(const u_char *bp, const u_char *data2, u_int len)
241 {
242 const struct dccp_hdr *dh;
243 const struct ip *ip;
244 #ifdef INET6
245 const struct ip6_hdr *ip6;
246 #endif
247 const u_char *cp;
248 u_short sport, dport;
249 u_int hlen;
250 u_int extlen = 0;
251
252 dh = (const struct dccp_hdr *)bp;
253
254 ip = (struct ip *)data2;
255 #ifdef INET6
256 if (IP_V(ip) == 6)
257 ip6 = (const struct ip6_hdr *)data2;
258 else
259 ip6 = NULL;
260 #endif /*INET6*/
261 cp = (const u_char *)(dh + 1);
262 if (cp > snapend) {
263 printf("[Invalid packet|dccp]");
264 return;
265 }
266
267 if (len < sizeof(struct dccp_hdr)) {
268 printf("truncated-dccp - %ld bytes missing!",
269 (long)len - sizeof(struct dccp_hdr));
270 return;
271 }
272
273 sport = EXTRACT_16BITS(&dh->dccph_sport);
274 dport = EXTRACT_16BITS(&dh->dccph_dport);
275 hlen = dh->dccph_doff * 4;
276
277 #ifdef INET6
278 if (ip6) {
279 (void)printf("%s.%d > %s.%d: ",
280 ip6addr_string(&ip6->ip6_src), sport,
281 ip6addr_string(&ip6->ip6_dst), dport);
282 } else
283 #endif /*INET6*/
284 {
285 (void)printf("%s.%d > %s.%d: ",
286 ipaddr_string(&ip->ip_src), sport,
287 ipaddr_string(&ip->ip_dst), dport);
288 }
289 fflush(stdout);
290
291 if (qflag) {
292 (void)printf(" %d", len - hlen);
293 if (hlen > len) {
294 (void)printf("dccp [bad hdr length %u - too long, > %u]",
295 hlen, len);
296 }
297 return;
298 }
299
300 /* other variables in generic header */
301 if (vflag) {
302 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh));
303 }
304
305 /* checksum calculation */
306 if (vflag && TTEST2(bp[0], len)) {
307 u_int16_t sum = 0, dccp_sum;
308
309 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
310 (void)printf("cksum 0x%04x ", dccp_sum);
311 if (IP_V(ip) == 4)
312 sum = dccp_cksum(ip, dh, len);
313 #ifdef INET6
314 else if (IP_V(ip) == 6)
315 sum = dccp6_cksum(ip6, dh, len);
316 #endif
317 if (sum != 0)
318 (void)printf("(incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
319 else
320 (void)printf("(correct), ");
321 }
322
323 switch (DCCPH_TYPE(dh)) {
324 case DCCP_PKT_REQUEST: {
325 struct dccp_hdr_request *dhr =
326 (struct dccp_hdr_request *)(bp + dccp_basic_hdr_len(dh));
327 TCHECK(*dhr);
328 (void)printf("request (service=%d) ",
329 EXTRACT_32BITS(&dhr->dccph_req_service));
330 extlen += 4;
331 break;
332 }
333 case DCCP_PKT_RESPONSE: {
334 struct dccp_hdr_response *dhr =
335 (struct dccp_hdr_response *)(bp + dccp_basic_hdr_len(dh));
336 TCHECK(*dhr);
337 (void)printf("response (service=%d) ",
338 EXTRACT_32BITS(&dhr->dccph_resp_service));
339 extlen += 12;
340 break;
341 }
342 case DCCP_PKT_DATA:
343 (void)printf("data ");
344 break;
345 case DCCP_PKT_ACK: {
346 (void)printf("ack ");
347 extlen += 8;
348 break;
349 }
350 case DCCP_PKT_DATAACK: {
351 (void)printf("dataack ");
352 extlen += 8;
353 break;
354 }
355 case DCCP_PKT_CLOSEREQ:
356 (void)printf("closereq ");
357 extlen += 8;
358 break;
359 case DCCP_PKT_CLOSE:
360 (void)printf("close ");
361 extlen += 8;
362 break;
363 case DCCP_PKT_RESET: {
364 struct dccp_hdr_reset *dhr =
365 (struct dccp_hdr_reset *)(bp + dccp_basic_hdr_len(dh));
366 TCHECK(*dhr);
367 (void)printf("reset (code=%s) ",
368 dccp_reset_code(dhr->dccph_reset_code));
369 extlen += 12;
370 break;
371 }
372 case DCCP_PKT_SYNC:
373 (void)printf("sync ");
374 extlen += 8;
375 break;
376 case DCCP_PKT_SYNCACK:
377 (void)printf("syncack ");
378 extlen += 8;
379 break;
380 default:
381 (void)printf("invalid ");
382 break;
383 }
384
385 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
386 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
387 dccp_print_ack_no(bp);
388
389 if (vflag < 2)
390 return;
391
392 (void)printf("seq %" PRIu64, dccp_seqno(dh));
393
394 /* process options */
395 if (hlen > dccp_basic_hdr_len(dh) + extlen){
396 const u_char *cp;
397 u_int optlen;
398 cp = bp + dccp_basic_hdr_len(dh) + extlen;
399 printf(" <");
400
401 hlen -= dccp_basic_hdr_len(dh) + extlen;
402 while(1){
403 TCHECK(*cp);
404 optlen = dccp_print_option(cp);
405 if (!optlen) goto trunc2;
406 if (hlen <= optlen) break;
407 hlen -= optlen;
408 cp += optlen;
409 printf(", ");
410 }
411 printf(">");
412 }
413 return;
414 trunc:
415 printf("%s", tstr);
416 trunc2:
417 return;
418 }
419
420 static int dccp_print_option(const u_char *option)
421 {
422 u_int8_t optlen, i;
423
424 TCHECK(*option);
425
426 if (*option >= 32) {
427 TCHECK(*(option+1));
428 optlen = *(option +1);
429 if (optlen < 2) {
430 printf("Option %d optlen too short",*option);
431 return 1;
432 }
433 } else optlen = 1;
434
435 TCHECK2(*option,optlen);
436
437 switch (*option){
438 case 0:
439 printf("nop");
440 break;
441 case 1:
442 printf("mandatory");
443 break;
444 case 2:
445 printf("slowreceiver");
446 break;
447 case 32:
448 printf("change_l");
449 if (*(option +2) < 10){
450 printf(" %s", dccp_feature_nums[*(option +2)]);
451 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
452 }
453 break;
454 case 33:
455 printf("confirm_l");
456 if (*(option +2) < 10){
457 printf(" %s", dccp_feature_nums[*(option +2)]);
458 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
459 }
460 break;
461 case 34:
462 printf("change_r");
463 if (*(option +2) < 10){
464 printf(" %s", dccp_feature_nums[*(option +2)]);
465 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
466 }
467 break;
468 case 35:
469 printf("confirm_r");
470 if (*(option +2) < 10){
471 printf(" %s", dccp_feature_nums[*(option +2)]);
472 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
473 }
474 break;
475 case 36:
476 printf("initcookie 0x");
477 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
478 break;
479 case 37:
480 printf("ndp_count");
481 for (i = 0; i < optlen -2; i ++) printf(" %d", *(option +2 + i));
482 break;
483 case 38:
484 printf("ack_vector0 0x");
485 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
486 break;
487 case 39:
488 printf("ack_vector1 0x");
489 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
490 break;
491 case 40:
492 printf("data_dropped 0x");
493 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
494 break;
495 case 41:
496 printf("timestamp %u", EXTRACT_32BITS(option + 2));
497 break;
498 case 42:
499 printf("timestamp_echo %u", EXTRACT_32BITS(option + 2));
500 break;
501 case 43:
502 printf("elapsed_time ");
503 if (optlen == 6)
504 printf("%u", EXTRACT_32BITS(option + 2));
505 else
506 printf("%u", EXTRACT_16BITS(option + 2));
507 break;
508 case 44:
509 printf("data_checksum ");
510 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
511 break;
512 default :
513 if (*option >= 128) {
514 printf("CCID option %d",*option);
515 switch (optlen) {
516 case 4:
517 printf(" %u", EXTRACT_16BITS(option + 2));
518 break;
519 case 6:
520 printf(" %u", EXTRACT_32BITS(option + 2));
521 break;
522 default:
523 break;
524 }
525 break;
526 }
527
528 printf("unknown_opt %d", *option);
529 break;
530 }
531
532 return optlen;
533 trunc:
534 printf("%s", tstr);
535 return 0;
536 }