]> The Tcpdump Group git mirrors - tcpdump/blob - print-dccp.c
change 802.1ag pre-standard codepoint to standard codepoint
[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 #ifndef lint
11 static const char rcsid[] _U_ =
12 "@(#) $Header: /tcpdump/master/tcpdump/print-dccp.c,v 1.7 2006-11-02 09:05:23 hannes Exp $ (LBL)";
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include <tcpdump-stdinc.h>
20
21 #include "dccp.h"
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "interface.h"
27 #include "addrtoname.h"
28 #include "extract.h" /* must come after interface.h */
29 #include "ip.h"
30 #ifdef INET6
31 #include "ip6.h"
32 #endif
33 #include "ipproto.h"
34
35 static const char *dccp_reset_codes[] = {
36 "unspecified",
37 "closed",
38 "aborted",
39 "no_connection",
40 "packet_error",
41 "option_error",
42 "mandatory_error",
43 "connection_refused",
44 "bad_service_code",
45 "too_busy",
46 "bad_init_cookie",
47 "aggression_penalty",
48 };
49
50 static const char *dccp_feature_nums[] = {
51 "reserved",
52 "ccid",
53 "allow_short_seqno",
54 "sequence_window",
55 "ecn_incapable",
56 "ack_ratio",
57 "send_ack_vector",
58 "send_ndp_count",
59 "minimum checksum coverage",
60 "check data checksum",
61 };
62
63 static inline int dccp_csum_coverage(const struct dccp_hdr* dh, u_int len)
64 {
65 u_int cov;
66
67 if (DCCPH_CSCOV(dh) == 0)
68 return len;
69 cov = (dh->dccph_doff + DCCPH_CSCOV(dh) - 1) * sizeof(u_int32_t);
70 return (cov > len)? len : cov;
71 }
72
73 static int dccp_cksum(const struct ip *ip,
74 const struct dccp_hdr *dh, u_int len)
75 {
76 int cov = dccp_csum_coverage(dh, len);
77 union phu {
78 struct phdr {
79 u_int32_t src;
80 u_int32_t dst;
81 u_char mbz;
82 u_char proto;
83 u_int16_t len;
84 } ph;
85 u_int16_t pa[6];
86 } phu;
87 const u_int16_t *sp;
88
89 /* pseudo-header.. */
90 phu.ph.mbz = 0;
91 phu.ph.len = htons(len);
92 phu.ph.proto = IPPROTO_DCCP;
93 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
94 if (IP_HL(ip) == 5)
95 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
96 else
97 phu.ph.dst = ip_finddst(ip);
98
99 sp = &phu.pa[0];
100 return in_cksum((u_short *)dh, cov, sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
101 }
102
103 #ifdef INET6
104 static int dccp6_cksum(const struct ip6_hdr *ip6, const struct dccp_hdr *dh, u_int len)
105 {
106 size_t i;
107 u_int32_t sum = 0;
108 int cov = dccp_csum_coverage(dh, len);
109 union {
110 struct {
111 struct in6_addr ph_src;
112 struct in6_addr ph_dst;
113 u_int32_t ph_len;
114 u_int8_t ph_zero[3];
115 u_int8_t ph_nxt;
116 } ph;
117 u_int16_t pa[20];
118 } phu;
119
120 /* pseudo-header */
121 memset(&phu, 0, sizeof(phu));
122 phu.ph.ph_src = ip6->ip6_src;
123 phu.ph.ph_dst = ip6->ip6_dst;
124 phu.ph.ph_len = htonl(len);
125 phu.ph.ph_nxt = IPPROTO_DCCP;
126
127 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
128 sum += phu.pa[i];
129
130 return in_cksum((u_short *)dh, cov, sum);
131 }
132 #endif
133
134 static const char *dccp_reset_code(u_int8_t code)
135 {
136 if (code >= __DCCP_RESET_CODE_LAST)
137 return "invalid";
138 return dccp_reset_codes[code];
139 }
140
141 static u_int64_t dccp_seqno(const struct dccp_hdr *dh)
142 {
143 u_int32_t seq_high = DCCPH_SEQ(dh);
144 u_int64_t seqno = EXTRACT_24BITS(&seq_high) & 0xFFFFFF;
145
146 if (DCCPH_X(dh) != 0) {
147 const struct dccp_hdr_ext *dhx = (void *)(dh + 1);
148 u_int32_t seq_low = dhx->dccph_seq_low;
149 seqno &= 0x00FFFF; /* clear reserved field */
150 seqno = (seqno << 32) + EXTRACT_32BITS(&seq_low);
151 }
152
153 return seqno;
154 }
155
156 static inline unsigned int dccp_basic_hdr_len(const struct dccp_hdr *dh)
157 {
158 return sizeof(*dh) + (DCCPH_X(dh) ? sizeof(struct dccp_hdr_ext) : 0);
159 }
160
161 static void dccp_print_ack_no(const u_char *bp)
162 {
163 const struct dccp_hdr *dh = (const struct dccp_hdr *)bp;
164 const struct dccp_hdr_ack_bits *dh_ack =
165 (struct dccp_hdr_ack_bits *)(bp + dccp_basic_hdr_len(dh));
166 u_int32_t ack_high;
167 u_int64_t ackno;
168
169 TCHECK2(*dh_ack,4);
170 ack_high = DCCPH_ACK(dh_ack);
171 ackno = EXTRACT_24BITS(&ack_high) & 0xFFFFFF;
172
173 if (DCCPH_X(dh) != 0) {
174 u_int32_t ack_low;
175
176 TCHECK2(*dh_ack,8);
177 ack_low = dh_ack->dccph_ack_nr_low;
178
179 ackno &= 0x00FFFF; /* clear reserved field */
180 ackno = (ackno << 32) + EXTRACT_32BITS(&ack_low);
181 }
182
183 (void)printf("(ack=%" PRIu64 ") ", ackno);
184 trunc:
185 return;
186 }
187
188 static inline unsigned int dccp_packet_hdr_len(const u_int8_t type)
189 {
190 if (type == DCCP_PKT_DATA)
191 return 0;
192 if (type == DCCP_PKT_DATAACK ||
193 type == DCCP_PKT_ACK ||
194 type == DCCP_PKT_SYNC ||
195 type == DCCP_PKT_SYNCACK ||
196 type == DCCP_PKT_CLOSE ||
197 type == DCCP_PKT_CLOSEREQ)
198 return sizeof(struct dccp_hdr_ack_bits);
199 if (type == DCCP_PKT_REQUEST)
200 return sizeof(struct dccp_hdr_request);
201 if (type == DCCP_PKT_RESPONSE)
202 return sizeof(struct dccp_hdr_response);
203 return sizeof(struct dccp_hdr_reset);
204 }
205
206 static int dccp_print_option(const u_char *option);
207
208 /**
209 * dccp_print - show dccp packet
210 * @bp - beginning of dccp packet
211 * @data2 - beginning of enclosing
212 * @len - lenght of ip packet
213 */
214 void dccp_print(const u_char *bp, const u_char *data2, u_int len)
215 {
216 const struct dccp_hdr *dh;
217 const struct ip *ip;
218 #ifdef INET6
219 const struct ip6_hdr *ip6;
220 #endif
221 const u_char *cp;
222 u_short sport, dport;
223 u_int hlen;
224 u_int extlen = 0;
225
226 dh = (const struct dccp_hdr *)bp;
227
228 ip = (struct ip *)data2;
229 #ifdef INET6
230 if (IP_V(ip) == 6)
231 ip6 = (const struct ip6_hdr *)data2;
232 else
233 ip6 = NULL;
234 #endif /*INET6*/
235 cp = (const u_char *)(dh + 1);
236 if (cp > snapend) {
237 printf("[Invalid packet|dccp]");
238 return;
239 }
240
241 if (len < sizeof(struct dccp_hdr)) {
242 printf("truncated-dccp - %ld bytes missing!",
243 (long)len - sizeof(struct dccp_hdr));
244 return;
245 }
246
247 sport = EXTRACT_16BITS(&dh->dccph_sport);
248 dport = EXTRACT_16BITS(&dh->dccph_dport);
249 hlen = dh->dccph_doff * 4;
250
251 #ifdef INET6
252 if (ip6) {
253 (void)printf("%s.%d > %s.%d: ",
254 ip6addr_string(&ip6->ip6_src), sport,
255 ip6addr_string(&ip6->ip6_dst), dport);
256 } else
257 #endif /*INET6*/
258 {
259 (void)printf("%s.%d > %s.%d: ",
260 ipaddr_string(&ip->ip_src), sport,
261 ipaddr_string(&ip->ip_dst), dport);
262 }
263 fflush(stdout);
264
265 if (qflag) {
266 (void)printf(" %d", len - hlen);
267 if (hlen > len) {
268 (void)printf("dccp [bad hdr length %u - too long, > %u]",
269 hlen, len);
270 }
271 return;
272 }
273
274 /* other variables in generic header */
275 if (vflag) {
276 (void)printf("CCVal %d, CsCov %d, ", DCCPH_CCVAL(dh), DCCPH_CSCOV(dh));
277 }
278
279 /* checksum calculation */
280 #ifdef INET6
281 if (ip6) {
282 if (ip6->ip6_plen && vflag) {
283 u_int16_t sum, dccp_sum;
284
285 sum = dccp6_cksum(ip6, dh, len);
286 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
287 printf("cksum 0x%04x", dccp_sum);
288 if (sum != 0) {
289 (void)printf(" (incorrect -> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
290 } else
291 (void)printf(" (correct), ");
292 }
293 } else
294 #endif /* INET6 */
295 if (vflag)
296 {
297 u_int16_t sum, dccp_sum;
298
299 sum = dccp_cksum(ip, dh, len);
300 dccp_sum = EXTRACT_16BITS(&dh->dccph_checksum);
301 printf("cksum 0x%04x", dccp_sum);
302 if (sum != 0) {
303 (void)printf(" (incorrect (-> 0x%04x), ",in_cksum_shouldbe(dccp_sum, sum));
304 } else
305 (void)printf(" (correct), ");
306 }
307
308 switch (DCCPH_TYPE(dh)) {
309 case DCCP_PKT_REQUEST: {
310 struct dccp_hdr_request *dhr =
311 (struct dccp_hdr_request *)(bp + dccp_basic_hdr_len(dh));
312 TCHECK(*dhr);
313 (void)printf("request (service=%d) ",
314 EXTRACT_32BITS(&dhr->dccph_req_service));
315 extlen += 4;
316 break;
317 }
318 case DCCP_PKT_RESPONSE: {
319 struct dccp_hdr_response *dhr =
320 (struct dccp_hdr_response *)(bp + dccp_basic_hdr_len(dh));
321 TCHECK(*dhr);
322 (void)printf("response (service=%d) ",
323 EXTRACT_32BITS(&dhr->dccph_resp_service));
324 extlen += 12;
325 break;
326 }
327 case DCCP_PKT_DATA:
328 (void)printf("data ");
329 break;
330 case DCCP_PKT_ACK: {
331 (void)printf("ack ");
332 extlen += 8;
333 break;
334 }
335 case DCCP_PKT_DATAACK: {
336 (void)printf("dataack ");
337 extlen += 8;
338 break;
339 }
340 case DCCP_PKT_CLOSEREQ:
341 (void)printf("closereq ");
342 extlen += 8;
343 break;
344 case DCCP_PKT_CLOSE:
345 (void)printf("close ");
346 extlen += 8;
347 break;
348 case DCCP_PKT_RESET: {
349 struct dccp_hdr_reset *dhr =
350 (struct dccp_hdr_reset *)(bp + dccp_basic_hdr_len(dh));
351 TCHECK(*dhr);
352 (void)printf("reset (code=%s) ",
353 dccp_reset_code(dhr->dccph_reset_code));
354 extlen += 12;
355 break;
356 }
357 case DCCP_PKT_SYNC:
358 (void)printf("sync ");
359 extlen += 8;
360 break;
361 case DCCP_PKT_SYNCACK:
362 (void)printf("syncack ");
363 extlen += 8;
364 break;
365 default:
366 (void)printf("invalid ");
367 break;
368 }
369
370 if ((DCCPH_TYPE(dh) != DCCP_PKT_DATA) &&
371 (DCCPH_TYPE(dh) != DCCP_PKT_REQUEST))
372 dccp_print_ack_no(bp);
373
374 if (vflag < 2)
375 return;
376
377 (void)printf("seq %" PRIu64, dccp_seqno(dh));
378
379 /* process options */
380 if (hlen > dccp_basic_hdr_len(dh) + extlen){
381 const u_char *cp;
382 u_int optlen;
383 cp = bp + dccp_basic_hdr_len(dh) + extlen;
384 printf(" <");
385
386 hlen -= dccp_basic_hdr_len(dh) + extlen;
387 while(1){
388 TCHECK(*cp);
389 optlen = dccp_print_option(cp);
390 if (!optlen) goto trunc2;
391 if (hlen <= optlen) break;
392 hlen -= optlen;
393 cp += optlen;
394 printf(", ");
395 }
396 printf(">");
397 }
398 return;
399 trunc:
400 printf("[|dccp]");
401 trunc2:
402 return;
403 }
404
405 static int dccp_print_option(const u_char *option)
406 {
407 u_int8_t optlen, i;
408 u_int32_t *ts;
409 u_int16_t *var16;
410 u_int32_t *var32;
411
412 TCHECK(*option);
413
414 if (*option >= 32) {
415 TCHECK(*(option+1));
416 optlen = *(option +1);
417 if (optlen < 2) {
418 printf("Option %d optlen too short",*option);
419 return 1;
420 }
421 } else optlen = 1;
422
423 TCHECK2(*option,optlen);
424
425 switch (*option){
426 case 0:
427 printf("nop");
428 break;
429 case 1:
430 printf("mandatory");
431 break;
432 case 2:
433 printf("slowreceiver");
434 break;
435 case 32:
436 printf("change_l");
437 if (*(option +2) < 10){
438 printf(" %s", dccp_feature_nums[*(option +2)]);
439 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
440 }
441 break;
442 case 33:
443 printf("confirm_l");
444 if (*(option +2) < 10){
445 printf(" %s", dccp_feature_nums[*(option +2)]);
446 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
447 }
448 break;
449 case 34:
450 printf("change_r");
451 if (*(option +2) < 10){
452 printf(" %s", dccp_feature_nums[*(option +2)]);
453 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
454 }
455 break;
456 case 35:
457 printf("confirm_r");
458 if (*(option +2) < 10){
459 printf(" %s", dccp_feature_nums[*(option +2)]);
460 for (i = 0; i < optlen -3; i ++) printf(" %d", *(option +3 + i));
461 }
462 break;
463 case 36:
464 printf("initcookie 0x");
465 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
466 break;
467 case 37:
468 printf("ndp_count");
469 for (i = 0; i < optlen -2; i ++) printf(" %d", *(option +2 + i));
470 break;
471 case 38:
472 printf("ack_vector0 0x");
473 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
474 break;
475 case 39:
476 printf("ack_vector1 0x");
477 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
478 break;
479 case 40:
480 printf("data_dropped 0x");
481 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
482 break;
483 case 41:
484 ts = (u_int32_t *)(option + 2);
485 printf("timestamp %u", (u_int32_t)ntohl(*ts));
486 break;
487 case 42:
488 ts = (u_int32_t *)(option + 2);
489 printf("timestamp_echo %u", (u_int32_t)ntohl(*ts));
490 break;
491 case 43:
492 printf("elapsed_time ");
493 if (optlen == 6){
494 ts = (u_int32_t *)(option + 2);
495 printf("%u", (u_int32_t)ntohl(*ts));
496 } else {
497 var16 = (u_int16_t *)(option + 2);
498 printf("%u", ntohs(*var16));
499 }
500 break;
501 case 44:
502 printf("data_checksum ");
503 for (i = 0; i < optlen -2; i ++) printf("%02x", *(option +2 + i));
504 break;
505 default :
506 if (*option >= 128) {
507 printf("CCID option %d",*option);
508 switch (optlen) {
509 case 4:
510 var16 = (u_int16_t *)(option + 2);
511 printf(" %u",ntohs(*var16));
512 break;
513 case 6:
514 var32 = (u_int32_t *)(option + 2);
515 printf(" %u",(u_int32_t)ntohl(*var32));
516 break;
517 default:
518 break;
519 }
520 break;
521 }
522
523 printf("unknown_opt %d", *option);
524 break;
525 }
526
527 return optlen;
528 trunc:
529 printf("[|dccp]");
530 return 0;
531 }