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