Drop ".i0", it is an undefined macro.
[netbsd-mini2440.git] / dist / tcpdump / print-icmp6.c
blob5550c0b7bc6c5fdd601600034bd2244c7528d654
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 #ifndef lint
26 #if 0
27 static const char rcsid[] _U_ =
28 "@(#) Header: /tcpdump/master/tcpdump/print-icmp6.c,v 1.79.2.6 2005/09/05 09:29:28 guy Exp";
29 #else
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
31 #endif
32 #endif
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #ifdef INET6
40 #include <tcpdump-stdinc.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "extract.h"
49 #include "ip6.h"
50 #include "icmp6.h"
51 #include "ipproto.h"
53 #include "udp.h"
54 #include "ah.h"
56 static const char *get_rtpref(u_int);
57 static const char *get_lifetime(u_int32_t);
58 static void print_lladdr(const u_char *, size_t);
59 static void icmp6_opt_print(const u_char *, int);
60 static void mld6_print(const u_char *);
61 static void mldv2_report_print(const u_char *, u_int);
62 static void mldv2_query_print(const u_char *, u_int);
63 static struct udphdr *get_upperlayer(u_char *, u_int *);
64 static void dnsname_print(const u_char *, const u_char *);
65 static void icmp6_nodeinfo_print(u_int, const u_char *, const u_char *);
66 static void icmp6_rrenum_print(const u_char *, const u_char *);
68 #ifndef abs
69 #define abs(a) ((0 < (a)) ? (a) : -(a))
70 #endif
72 static struct tok icmp6_type_values[] = {
73 { ICMP6_DST_UNREACH, "destination unreachable"},
74 { ICMP6_PACKET_TOO_BIG, "packet too big"},
75 { ICMP6_TIME_EXCEEDED, "time exceeded in-transit"},
76 { ICMP6_PARAM_PROB, "parameter problem"},
77 { ICMP6_ECHO_REQUEST, "echo request"},
78 { ICMP6_ECHO_REPLY, "echo reply"},
79 { MLD6_LISTENER_QUERY, "multicast listener query"},
80 { MLD6_LISTENER_REPORT, "multicast listener report"},
81 { MLD6_LISTENER_DONE, "multicast listener done"},
82 { ND_ROUTER_SOLICIT, "router solicitation"},
83 { ND_ROUTER_ADVERT, "router advertisement"},
84 { ND_NEIGHBOR_SOLICIT, "neighbor solicitation"},
85 { ND_NEIGHBOR_ADVERT, "neighbor advertisement"},
86 { ND_REDIRECT, "redirect"},
87 { ICMP6_ROUTER_RENUMBERING, "router renumbering"},
88 { IND_SOLICIT, "inverse neighbor solicitation"},
89 { IND_ADVERT, "inverse neighbor advertisement"},
90 { MLDV2_LISTENER_REPORT, "multicast listener report v2"},
91 { ICMP6_HADISCOV_REQUEST, "ha discovery request"},
92 { ICMP6_HADISCOV_REPLY, "ha discovery reply"},
93 { ICMP6_MOBILEPREFIX_SOLICIT, "mobile router solicitation"},
94 { ICMP6_MOBILEPREFIX_ADVERT, "mobile router advertisement"},
95 { ICMP6_WRUREQUEST, "who-are-you request"},
96 { ICMP6_WRUREPLY, "who-are-you reply"},
97 { ICMP6_NI_QUERY, "node information query"},
98 { ICMP6_NI_REPLY, "node information reply"},
99 { MLD6_MTRACE, "mtrace message"},
100 { MLD6_MTRACE_RESP, "mtrace response"},
101 { 0, NULL }
104 static struct tok icmp6_dst_unreach_code_values[] = {
105 { ICMP6_DST_UNREACH_NOROUTE, "unreachable route" },
106 { ICMP6_DST_UNREACH_ADMIN, " unreachable prohibited"},
107 { ICMP6_DST_UNREACH_BEYONDSCOPE, "beyond scope"},
108 { ICMP6_DST_UNREACH_ADDR, "unreachable address"},
109 { ICMP6_DST_UNREACH_NOPORT, "unreachable port"},
110 { 0, NULL }
113 static struct tok icmp6_opt_pi_flag_values[] = {
114 { ND_OPT_PI_FLAG_ONLINK, "onlink" },
115 { ND_OPT_PI_FLAG_AUTO, "auto" },
116 { ND_OPT_PI_FLAG_ROUTER, "router" },
117 { 0, NULL }
120 static struct tok icmp6_opt_ra_flag_values[] = {
121 { ND_RA_FLAG_MANAGED, "managed" },
122 { ND_RA_FLAG_OTHER, "other stateful"},
123 { ND_RA_FLAG_HOME_AGENT, "home agent"},
124 { 0, NULL }
127 static struct tok icmp6_nd_na_flag_values[] = {
128 { ND_NA_FLAG_ROUTER, "router" },
129 { ND_NA_FLAG_SOLICITED, "solicited" },
130 { ND_NA_FLAG_OVERRIDE, "override" },
131 { 0, NULL }
135 static struct tok icmp6_opt_values[] = {
136 { ND_OPT_SOURCE_LINKADDR, "source link-address"},
137 { ND_OPT_TARGET_LINKADDR, "destination link-address"},
138 { ND_OPT_PREFIX_INFORMATION, "prefix info"},
139 { ND_OPT_REDIRECTED_HEADER, "redirected header"},
140 { ND_OPT_MTU, "mtu"},
141 { ND_OPT_ADVINTERVAL, "advertisement interval"},
142 { ND_OPT_HOMEAGENT_INFO, "homeagent information"},
143 { ND_OPT_ROUTE_INFO, "route info"},
144 { 0, NULL }
147 /* mldv2 report types */
148 static struct tok mldv2report2str[] = {
149 { 1, "is_in" },
150 { 2, "is_ex" },
151 { 3, "to_in" },
152 { 4, "to_ex" },
153 { 5, "allow" },
154 { 6, "block" },
155 { 0, NULL }
158 static const char *
159 get_rtpref(u_int v)
161 static const char *rtpref_str[] = {
162 "medium", /* 00 */
163 "high", /* 01 */
164 "rsv", /* 10 */
165 "low" /* 11 */
168 return rtpref_str[((v & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff];
171 static const char *
172 get_lifetime(u_int32_t v)
174 static char buf[20];
176 if (v == (u_int32_t)~0UL)
177 return "infinity";
178 else {
179 snprintf(buf, sizeof(buf), "%u", v);
180 return buf;
184 static void
185 print_lladdr(const u_int8_t *p, size_t l)
187 const u_int8_t *ep, *q;
189 q = p;
190 ep = p + l;
191 while (l > 0 && q < ep) {
192 if (q > p)
193 printf(":");
194 printf("%02x", *q++);
195 l--;
199 static int icmp6_cksum(const struct ip6_hdr *ip6, const struct icmp6_hdr *icp,
200 u_int len)
202 size_t i;
203 register const u_int16_t *sp;
204 u_int32_t sum;
205 union {
206 struct {
207 struct in6_addr ph_src;
208 struct in6_addr ph_dst;
209 u_int32_t ph_len;
210 u_int8_t ph_zero[3];
211 u_int8_t ph_nxt;
212 } ph;
213 u_int16_t pa[20];
214 } phu;
216 /* pseudo-header */
217 memset(&phu, 0, sizeof(phu));
218 phu.ph.ph_src = ip6->ip6_src;
219 phu.ph.ph_dst = ip6->ip6_dst;
220 phu.ph.ph_len = htonl(len);
221 phu.ph.ph_nxt = IPPROTO_ICMPV6;
223 sum = 0;
224 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
225 sum += phu.pa[i];
227 sp = (const u_int16_t *)icp;
229 for (i = 0; i < (len & ~1); i += 2)
230 sum += *sp++;
232 if (len & 1)
233 sum += htons((*(const u_int8_t *)sp) << 8);
235 while (sum > 0xffff)
236 sum = (sum & 0xffff) + (sum >> 16);
237 sum = ~sum & 0xffff;
239 return (sum);
242 void
243 icmp6_print(const u_char *bp, u_int length, const u_char *bp2, int fragmented)
245 const struct icmp6_hdr *dp;
246 const struct ip6_hdr *ip;
247 const struct ip6_hdr *oip;
248 const struct udphdr *ouh;
249 int dport;
250 const u_char *ep;
251 u_int prot;
253 dp = (struct icmp6_hdr *)bp;
254 ip = (struct ip6_hdr *)bp2;
255 oip = (struct ip6_hdr *)(dp + 1);
256 /* 'ep' points to the end of available data. */
257 ep = snapend;
259 TCHECK(dp->icmp6_cksum);
261 if (vflag && !fragmented) {
262 int sum = dp->icmp6_cksum;
264 if (TTEST2(bp[0], length)) {
265 sum = icmp6_cksum(ip, dp, length);
266 if (sum != 0)
267 (void)printf("[bad icmp6 cksum %x!] ", sum);
268 else
269 (void)printf("[icmp6 sum ok] ");
273 printf("ICMP6, %s", tok2str(icmp6_type_values,"unknown icmp6 type (%u)",dp->icmp6_type));
275 /* display cosmetics: print the packet length for printer that use the vflag now */
276 if (vflag && (dp->icmp6_type ==
277 ND_ROUTER_SOLICIT ||
278 ND_ROUTER_ADVERT ||
279 ND_NEIGHBOR_ADVERT ||
280 ND_NEIGHBOR_SOLICIT ||
281 ND_REDIRECT ||
282 ICMP6_HADISCOV_REPLY ||
283 ICMP6_MOBILEPREFIX_ADVERT ))
284 printf(", length %u", length);
286 switch (dp->icmp6_type) {
287 case ICMP6_DST_UNREACH:
288 TCHECK(oip->ip6_dst);
289 printf(", %s", tok2str(icmp6_dst_unreach_code_values,"unknown unreach code (%u)",dp->icmp6_code));
290 switch (dp->icmp6_code) {
292 case ICMP6_DST_UNREACH_NOROUTE: /* fall through */
293 case ICMP6_DST_UNREACH_ADMIN:
294 case ICMP6_DST_UNREACH_ADDR:
295 printf(" %s",ip6addr_string(&oip->ip6_dst));
296 break;
297 case ICMP6_DST_UNREACH_BEYONDSCOPE:
298 printf(" %s, source address %s",
299 ip6addr_string(&oip->ip6_dst),
300 ip6addr_string(&oip->ip6_src));
301 break;
302 case ICMP6_DST_UNREACH_NOPORT:
303 if ((ouh = get_upperlayer((u_char *)oip, &prot))
304 == NULL)
305 goto trunc;
307 dport = EXTRACT_16BITS(&ouh->uh_dport);
308 switch (prot) {
309 case IPPROTO_TCP:
310 printf(", %s tcp port %s",
311 ip6addr_string(&oip->ip6_dst),
312 tcpport_string(dport));
313 break;
314 case IPPROTO_UDP:
315 printf(", %s udp port %s",
316 ip6addr_string(&oip->ip6_dst),
317 udpport_string(dport));
318 break;
319 default:
320 printf(", %s protocol %d port %d unreachable",
321 ip6addr_string(&oip->ip6_dst),
322 oip->ip6_nxt, dport);
323 break;
325 break;
326 default:
327 if (vflag <= 1) {
328 print_unknown_data(bp,"\n\t",length);
329 return;
331 break;
333 break;
334 case ICMP6_PACKET_TOO_BIG:
335 TCHECK(dp->icmp6_mtu);
336 printf(", mtu %u", EXTRACT_32BITS(&dp->icmp6_mtu));
337 break;
338 case ICMP6_TIME_EXCEEDED:
339 TCHECK(oip->ip6_dst);
340 switch (dp->icmp6_code) {
341 case ICMP6_TIME_EXCEED_TRANSIT:
342 printf(" for %s",
343 ip6addr_string(&oip->ip6_dst));
344 break;
345 case ICMP6_TIME_EXCEED_REASSEMBLY:
346 printf(" (reassembly)");
347 break;
348 default:
349 printf(", unknown code (%u)", dp->icmp6_code);
350 break;
352 break;
353 case ICMP6_PARAM_PROB:
354 TCHECK(oip->ip6_dst);
355 switch (dp->icmp6_code) {
356 case ICMP6_PARAMPROB_HEADER:
357 printf(", errorneous - octet %u", EXTRACT_32BITS(&dp->icmp6_pptr));
358 break;
359 case ICMP6_PARAMPROB_NEXTHEADER:
360 printf(", next header - octet %u", EXTRACT_32BITS(&dp->icmp6_pptr));
361 break;
362 case ICMP6_PARAMPROB_OPTION:
363 printf(", option - octet %u", EXTRACT_32BITS(&dp->icmp6_pptr));
364 break;
365 default:
366 printf(", code-#%d",
367 dp->icmp6_code);
368 break;
370 break;
371 case ICMP6_ECHO_REQUEST:
372 case ICMP6_ECHO_REPLY:
373 TCHECK(dp->icmp6_seq);
374 printf(", seq %u", EXTRACT_16BITS(&dp->icmp6_seq));
375 break;
376 case ICMP6_MEMBERSHIP_QUERY:
377 if (length == MLD_MINLEN) {
378 mld6_print((const u_char *)dp);
379 } else if (length >= MLDV2_MINLEN) {
380 printf("v2 ");
381 mldv2_query_print((const u_char *)dp, length);
382 } else {
383 printf(" unknown-version (len %u) ", length);
385 break;
386 case ICMP6_MEMBERSHIP_REPORT:
387 mld6_print((const u_char *)dp);
388 break;
389 case ICMP6_MEMBERSHIP_REDUCTION:
390 mld6_print((const u_char *)dp);
391 break;
392 case ND_ROUTER_SOLICIT:
393 #define RTSOLLEN 8
394 if (vflag) {
395 icmp6_opt_print((const u_char *)dp + RTSOLLEN,
396 length - RTSOLLEN);
398 break;
399 case ND_ROUTER_ADVERT:
400 #define RTADVLEN 16
401 if (vflag) {
402 struct nd_router_advert *p;
404 p = (struct nd_router_advert *)dp;
405 TCHECK(p->nd_ra_retransmit);
406 printf("\n\thop limit %u, Flags [%s]" \
407 ", pref %s, router lifetime %us, reachable time %us, retrans time %us",
408 (u_int)p->nd_ra_curhoplimit,
409 bittok2str(icmp6_opt_ra_flag_values,"none",(p->nd_ra_flags_reserved)),
410 get_rtpref(p->nd_ra_flags_reserved),
411 EXTRACT_16BITS(&p->nd_ra_router_lifetime),
412 EXTRACT_32BITS(&p->nd_ra_reachable),
413 EXTRACT_32BITS(&p->nd_ra_retransmit));
415 icmp6_opt_print((const u_char *)dp + RTADVLEN,
416 length - RTADVLEN);
418 break;
419 case ND_NEIGHBOR_SOLICIT:
421 struct nd_neighbor_solicit *p;
422 p = (struct nd_neighbor_solicit *)dp;
423 TCHECK(p->nd_ns_target);
424 printf(", who has %s", ip6addr_string(&p->nd_ns_target));
425 if (vflag) {
426 #define NDSOLLEN 24
427 icmp6_opt_print((const u_char *)dp + NDSOLLEN,
428 length - NDSOLLEN);
431 break;
432 case ND_NEIGHBOR_ADVERT:
434 struct nd_neighbor_advert *p;
436 p = (struct nd_neighbor_advert *)dp;
437 TCHECK(p->nd_na_target);
438 printf(", tgt is %s",
439 ip6addr_string(&p->nd_na_target));
440 if (vflag) {
441 printf(", Flags [%s]",
442 bittok2str(icmp6_nd_na_flag_values,
443 "none",
444 EXTRACT_32BITS(&p->nd_na_flags_reserved)));
445 #define NDADVLEN 24
446 icmp6_opt_print((const u_char *)dp + NDADVLEN,
447 length - NDADVLEN);
448 #undef NDADVLEN
451 break;
452 case ND_REDIRECT:
453 #define RDR(i) ((struct nd_redirect *)(i))
454 TCHECK(RDR(dp)->nd_rd_dst);
455 printf(", %s", getname6((const u_char *)&RDR(dp)->nd_rd_dst));
456 TCHECK(RDR(dp)->nd_rd_target);
457 printf(" to %s",
458 getname6((const u_char*)&RDR(dp)->nd_rd_target));
459 #define REDIRECTLEN 40
460 if (vflag) {
461 icmp6_opt_print((const u_char *)dp + REDIRECTLEN,
462 length - REDIRECTLEN);
464 break;
465 #undef REDIRECTLEN
466 #undef RDR
467 case ICMP6_ROUTER_RENUMBERING:
468 icmp6_rrenum_print(bp, ep);
469 break;
470 case ICMP6_NI_QUERY:
471 case ICMP6_NI_REPLY:
472 icmp6_nodeinfo_print(length, bp, ep);
473 break;
474 case IND_SOLICIT:
475 case IND_ADVERT:
476 break;
477 case ICMP6_V2_MEMBERSHIP_REPORT:
478 mldv2_report_print((const u_char *) dp, length);
479 break;
480 case ICMP6_MOBILEPREFIX_SOLICIT: /* fall through */
481 case ICMP6_HADISCOV_REQUEST:
482 TCHECK(dp->icmp6_data16[0]);
483 printf(", id 0x%04x", EXTRACT_16BITS(&dp->icmp6_data16[0]));
484 break;
485 case ICMP6_HADISCOV_REPLY:
486 if (vflag) {
487 struct in6_addr *in6;
488 u_char *cp;
490 TCHECK(dp->icmp6_data16[0]);
491 printf(", id 0x%04x", EXTRACT_16BITS(&dp->icmp6_data16[0]));
492 cp = (u_char *)dp + length;
493 in6 = (struct in6_addr *)(dp + 1);
494 for (; (u_char *)in6 < cp; in6++) {
495 TCHECK(*in6);
496 printf(", %s", ip6addr_string(in6));
499 break;
500 case ICMP6_MOBILEPREFIX_ADVERT:
501 if (vflag) {
502 TCHECK(dp->icmp6_data16[0]);
503 printf(", id 0x%04x", EXTRACT_16BITS(&dp->icmp6_data16[0]));
504 if (dp->icmp6_data16[1] & 0xc0)
505 printf(" ");
506 if (dp->icmp6_data16[1] & 0x80)
507 printf("M");
508 if (dp->icmp6_data16[1] & 0x40)
509 printf("O");
510 #define MPADVLEN 8
511 icmp6_opt_print((const u_char *)dp + MPADVLEN,
512 length - MPADVLEN);
514 break;
515 default:
516 printf(", length %u", length);
517 if (vflag <= 1)
518 print_unknown_data(bp,"\n\t", length);
519 return;
521 if (!vflag)
522 printf(", length %u", length);
523 return;
524 trunc:
525 fputs("[|icmp6]", stdout);
528 static struct udphdr *
529 get_upperlayer(u_char *bp, u_int *prot)
531 const u_char *ep;
532 struct ip6_hdr *ip6 = (struct ip6_hdr *)bp;
533 struct udphdr *uh;
534 struct ip6_hbh *hbh;
535 struct ip6_frag *fragh;
536 struct ah *ah;
537 u_int nh;
538 int hlen;
540 /* 'ep' points to the end of available data. */
541 ep = snapend;
543 if (!TTEST(ip6->ip6_nxt))
544 return NULL;
546 nh = ip6->ip6_nxt;
547 hlen = sizeof(struct ip6_hdr);
549 while (bp < ep) {
550 bp += hlen;
552 switch(nh) {
553 case IPPROTO_UDP:
554 case IPPROTO_TCP:
555 uh = (struct udphdr *)bp;
556 if (TTEST(uh->uh_dport)) {
557 *prot = nh;
558 return(uh);
560 else
561 return(NULL);
562 /* NOTREACHED */
564 case IPPROTO_HOPOPTS:
565 case IPPROTO_DSTOPTS:
566 case IPPROTO_ROUTING:
567 hbh = (struct ip6_hbh *)bp;
568 if (!TTEST(hbh->ip6h_len))
569 return(NULL);
570 nh = hbh->ip6h_nxt;
571 hlen = (hbh->ip6h_len + 1) << 3;
572 break;
574 case IPPROTO_FRAGMENT: /* this should be odd, but try anyway */
575 fragh = (struct ip6_frag *)bp;
576 if (!TTEST(fragh->ip6f_offlg))
577 return(NULL);
578 /* fragments with non-zero offset are meaningless */
579 if ((EXTRACT_16BITS(&fragh->ip6f_offlg) & IP6F_OFF_MASK) != 0)
580 return(NULL);
581 nh = fragh->ip6f_nxt;
582 hlen = sizeof(struct ip6_frag);
583 break;
585 case IPPROTO_AH:
586 ah = (struct ah *)bp;
587 if (!TTEST(ah->ah_len))
588 return(NULL);
589 nh = ah->ah_nxt;
590 hlen = (ah->ah_len + 2) << 2;
591 break;
593 default: /* unknown or undecodable header */
594 *prot = nh; /* meaningless, but set here anyway */
595 return(NULL);
599 return(NULL); /* should be notreached, though */
602 static void
603 icmp6_opt_print(const u_char *bp, int resid)
605 const struct nd_opt_hdr *op;
606 const struct nd_opt_hdr *opl; /* why there's no struct? */
607 const struct nd_opt_prefix_info *opp;
608 const struct icmp6_opts_redirect *opr;
609 const struct nd_opt_mtu *opm;
610 const struct nd_opt_advinterval *opa;
611 const struct nd_opt_homeagent_info *oph;
612 const struct nd_opt_route_info *opri;
613 const u_char *cp, *ep;
614 struct in6_addr in6, *in6p;
615 size_t l;
617 #define ECHECK(var) if ((u_char *)&(var) > ep - sizeof(var)) return
619 cp = bp;
620 /* 'ep' points to the end of available data. */
621 ep = snapend;
623 while (cp < ep) {
624 op = (struct nd_opt_hdr *)cp;
626 ECHECK(op->nd_opt_len);
627 if (resid <= 0)
628 return;
629 if (op->nd_opt_len == 0)
630 goto trunc;
631 if (cp + (op->nd_opt_len << 3) > ep)
632 goto trunc;
634 printf("\n\t %s option (%u), length %u (%u): ",
635 tok2str(icmp6_opt_values, "unknown", op->nd_opt_type),
636 op->nd_opt_type,
637 op->nd_opt_len << 3,
638 op->nd_opt_len);
640 switch (op->nd_opt_type) {
641 case ND_OPT_SOURCE_LINKADDR:
642 opl = (struct nd_opt_hdr *)op;
643 l = (op->nd_opt_len << 3) - 2;
644 print_lladdr(cp + 2, l);
645 break;
646 case ND_OPT_TARGET_LINKADDR:
647 opl = (struct nd_opt_hdr *)op;
648 l = (op->nd_opt_len << 3) - 2;
649 print_lladdr(cp + 2, l);
650 break;
651 case ND_OPT_PREFIX_INFORMATION:
652 opp = (struct nd_opt_prefix_info *)op;
653 TCHECK(opp->nd_opt_pi_prefix);
654 printf("%s/%u%s, Flags [%s], valid time %ss",
655 ip6addr_string(&opp->nd_opt_pi_prefix),
656 opp->nd_opt_pi_prefix_len,
657 (op->nd_opt_len != 4) ? "badlen" : "",
658 bittok2str(icmp6_opt_pi_flag_values, "none", opp->nd_opt_pi_flags_reserved),
659 get_lifetime(EXTRACT_32BITS(&opp->nd_opt_pi_valid_time)));
660 printf(", pref. time %ss", get_lifetime(EXTRACT_32BITS(&opp->nd_opt_pi_preferred_time)));
661 break;
662 case ND_OPT_REDIRECTED_HEADER:
663 opr = (struct icmp6_opts_redirect *)op;
664 print_unknown_data(bp,"\n\t ",op->nd_opt_len<<3);
665 /* xxx */
666 break;
667 case ND_OPT_MTU:
668 opm = (struct nd_opt_mtu *)op;
669 TCHECK(opm->nd_opt_mtu_mtu);
670 printf(" %u%s",
671 EXTRACT_32BITS(&opm->nd_opt_mtu_mtu),
672 (op->nd_opt_len != 1) ? "bad option length" : "" );
673 break;
674 case ND_OPT_ADVINTERVAL:
675 opa = (struct nd_opt_advinterval *)op;
676 TCHECK(opa->nd_opt_adv_interval);
677 printf(" %us", EXTRACT_32BITS(&opa->nd_opt_adv_interval));
678 break;
679 case ND_OPT_HOMEAGENT_INFO:
680 oph = (struct nd_opt_homeagent_info *)op;
681 TCHECK(oph->nd_opt_hai_lifetime);
682 printf(" preference %u, lifetime %u",
683 EXTRACT_16BITS(&oph->nd_opt_hai_preference),
684 EXTRACT_16BITS(&oph->nd_opt_hai_lifetime));
685 break;
686 case ND_OPT_ROUTE_INFO:
687 opri = (struct nd_opt_route_info *)op;
688 TCHECK(opri->nd_opt_rti_lifetime);
689 memset(&in6, 0, sizeof(in6));
690 in6p = (struct in6_addr *)(opri + 1);
691 switch (op->nd_opt_len) {
692 case 1:
693 break;
694 case 2:
695 TCHECK2(*in6p, 8);
696 memcpy(&in6, opri + 1, 8);
697 break;
698 case 3:
699 TCHECK(*in6p);
700 memcpy(&in6, opri + 1, sizeof(in6));
701 break;
702 default:
703 goto trunc;
705 printf(" %s/%u", ip6addr_string(&in6),
706 opri->nd_opt_rti_prefixlen);
707 printf(", pref=%s", get_rtpref(opri->nd_opt_rti_flags));
708 printf(", lifetime=%s",
709 get_lifetime(EXTRACT_32BITS(&opri->nd_opt_rti_lifetime)));
710 break;
711 default:
712 if (vflag <= 1) {
713 print_unknown_data(cp+2,"\n\t ", (op->nd_opt_len << 3) - 2); /* skip option header */
714 return;
716 break;
718 /* do we want to see an additional hexdump ? */
719 if (vflag> 1)
720 print_unknown_data(cp+2,"\n\t ", (op->nd_opt_len << 3) - 2); /* skip option header */
722 cp += op->nd_opt_len << 3;
723 resid -= op->nd_opt_len << 3;
725 return;
727 trunc:
728 fputs("[ndp opt]", stdout);
729 return;
730 #undef ECHECK
733 static void
734 mld6_print(const u_char *bp)
736 struct mld6_hdr *mp = (struct mld6_hdr *)bp;
737 const u_char *ep;
739 /* 'ep' points to the end of available data. */
740 ep = snapend;
742 if ((u_char *)mp + sizeof(*mp) > ep)
743 return;
745 printf("max resp delay: %d ", EXTRACT_16BITS(&mp->mld6_maxdelay));
746 printf("addr: %s", ip6addr_string(&mp->mld6_addr));
749 static void
750 mldv2_report_print(const u_char *bp, u_int len)
752 struct icmp6_hdr *icp = (struct icmp6_hdr *) bp;
753 u_int group, nsrcs, ngroups;
754 u_int i, j;
756 /* Minimum len is 8 */
757 if (len < 8) {
758 printf(" [invalid len %d]", len);
759 return;
762 TCHECK(icp->icmp6_data16[1]);
763 ngroups = ntohs(icp->icmp6_data16[1]);
764 printf(", %d group record(s)", ngroups);
765 if (vflag > 0) {
766 /* Print the group records */
767 group = 8;
768 for (i = 0; i < ngroups; i++) {
769 /* type(1) + auxlen(1) + numsrc(2) + grp(16) */
770 if (len < group + 20) {
771 printf(" [invalid number of groups]");
772 return;
774 TCHECK2(bp[group + 4], sizeof(struct in6_addr));
775 printf(" [gaddr %s", ip6addr_string(&bp[group + 4]));
776 printf(" %s", tok2str(mldv2report2str, " [v2-report-#%d]",
777 bp[group]));
778 nsrcs = (bp[group + 2] << 8) + bp[group + 3];
779 /* Check the number of sources and print them */
780 if (len < group + 20 + (nsrcs * sizeof(struct in6_addr))) {
781 printf(" [invalid number of sources %d]", nsrcs);
782 return;
784 if (vflag == 1)
785 printf(", %d source(s)", nsrcs);
786 else {
787 /* Print the sources */
788 (void)printf(" {");
789 for (j = 0; j < nsrcs; j++) {
790 TCHECK2(bp[group + 20 + j * sizeof(struct in6_addr)],
791 sizeof(struct in6_addr));
792 printf(" %s", ip6addr_string(&bp[group + 20 + j * sizeof(struct in6_addr)]));
794 (void)printf(" }");
796 /* Next group record */
797 group += 20 + nsrcs * sizeof(struct in6_addr);
798 printf("]");
801 return;
802 trunc:
803 (void)printf("[|icmp6]");
804 return;
807 static void
808 mldv2_query_print(const u_char *bp, u_int len)
810 struct icmp6_hdr *icp = (struct icmp6_hdr *) bp;
811 u_int mrc;
812 int mrt, qqi;
813 u_int nsrcs;
814 register u_int i;
816 /* Minimum len is 28 */
817 if (len < 28) {
818 printf(" [invalid len %d]", len);
819 return;
821 TCHECK(icp->icmp6_data16[0]);
822 mrc = ntohs(icp->icmp6_data16[0]);
823 if (mrc < 32768) {
824 mrt = mrc;
825 } else {
826 mrt = ((mrc & 0x0fff) | 0x1000) << (((mrc & 0x7000) >> 12) + 3);
828 if (vflag) {
829 (void)printf(" [max resp delay=%d]", mrt);
831 TCHECK2(bp[8], sizeof(struct in6_addr));
832 printf(" [gaddr %s", ip6addr_string(&bp[8]));
834 if (vflag) {
835 TCHECK(bp[25]);
836 if (bp[24] & 0x08) {
837 printf(" sflag");
839 if (bp[24] & 0x07) {
840 printf(" robustness=%d", bp[24] & 0x07);
842 if (bp[25] < 128) {
843 qqi = bp[25];
844 } else {
845 qqi = ((bp[25] & 0x0f) | 0x10) << (((bp[25] & 0x70) >> 4) + 3);
847 printf(" qqi=%d", qqi);
850 TCHECK2(bp[26], 2);
851 nsrcs = ntohs(*(u_short *)&bp[26]);
852 if (nsrcs > 0) {
853 if (len < 28 + nsrcs * sizeof(struct in6_addr))
854 printf(" [invalid number of sources]");
855 else if (vflag > 1) {
856 printf(" {");
857 for (i = 0; i < nsrcs; i++) {
858 TCHECK2(bp[28 + i * sizeof(struct in6_addr)],
859 sizeof(struct in6_addr));
860 printf(" %s", ip6addr_string(&bp[28 + i * sizeof(struct in6_addr)]));
862 printf(" }");
863 } else
864 printf(", %d source(s)", nsrcs);
866 printf("]");
867 return;
868 trunc:
869 (void)printf("[|icmp6]");
870 return;
873 void
874 dnsname_print(const u_char *cp, const u_char *ep)
876 int i;
878 /* DNS name decoding - no decompression */
879 printf(", \"");
880 while (cp < ep) {
881 i = *cp++;
882 if (i) {
883 if (i > ep - cp) {
884 printf("???");
885 break;
887 while (i-- && cp < ep) {
888 safeputchar(*cp);
889 cp++;
891 if (cp + 1 < ep && *cp)
892 printf(".");
893 } else {
894 if (cp == ep) {
895 /* FQDN */
896 printf(".");
897 } else if (cp + 1 == ep && *cp == '\0') {
898 /* truncated */
899 } else {
900 /* invalid */
901 printf("???");
903 break;
906 printf("\"");
909 static void
910 icmp6_nodeinfo_print(u_int icmp6len, const u_char *bp, const u_char *ep)
912 struct icmp6_nodeinfo *ni6;
913 struct icmp6_hdr *dp;
914 const u_char *cp;
915 size_t siz, i;
916 int needcomma;
918 if (ep < bp)
919 return;
920 dp = (struct icmp6_hdr *)bp;
921 ni6 = (struct icmp6_nodeinfo *)bp;
922 siz = ep - bp;
924 switch (ni6->ni_type) {
925 case ICMP6_NI_QUERY:
926 if (siz == sizeof(*dp) + 4) {
927 /* KAME who-are-you */
928 printf(" who-are-you request");
929 break;
931 printf(" node information query");
933 TCHECK2(*dp, sizeof(*ni6));
934 ni6 = (struct icmp6_nodeinfo *)dp;
935 printf(" ("); /*)*/
936 switch (EXTRACT_16BITS(&ni6->ni_qtype)) {
937 case NI_QTYPE_NOOP:
938 printf("noop");
939 break;
940 case NI_QTYPE_SUPTYPES:
941 printf("supported qtypes");
942 i = EXTRACT_16BITS(&ni6->ni_flags);
943 if (i)
944 printf(" [%s]", (i & 0x01) ? "C" : "");
945 break;
946 break;
947 case NI_QTYPE_FQDN:
948 printf("DNS name");
949 break;
950 case NI_QTYPE_NODEADDR:
951 printf("node addresses");
952 i = ni6->ni_flags;
953 if (!i)
954 break;
955 /* NI_NODEADDR_FLAG_TRUNCATE undefined for query */
956 printf(" [%s%s%s%s%s%s]",
957 (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
958 (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
959 (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
960 (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
961 (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
962 (i & NI_NODEADDR_FLAG_ALL) ? "A" : "");
963 break;
964 default:
965 printf("unknown");
966 break;
969 if (ni6->ni_qtype == NI_QTYPE_NOOP ||
970 ni6->ni_qtype == NI_QTYPE_SUPTYPES) {
971 if (siz != sizeof(*ni6))
972 if (vflag)
973 printf(", invalid len");
974 /*(*/
975 printf(")");
976 break;
980 /* XXX backward compat, icmp-name-lookup-03 */
981 if (siz == sizeof(*ni6)) {
982 printf(", 03 draft");
983 /*(*/
984 printf(")");
985 break;
988 switch (ni6->ni_code) {
989 case ICMP6_NI_SUBJ_IPV6:
990 if (!TTEST2(*dp,
991 sizeof(*ni6) + sizeof(struct in6_addr)))
992 break;
993 if (siz != sizeof(*ni6) + sizeof(struct in6_addr)) {
994 if (vflag)
995 printf(", invalid subject len");
996 break;
998 printf(", subject=%s",
999 getname6((const u_char *)(ni6 + 1)));
1000 break;
1001 case ICMP6_NI_SUBJ_FQDN:
1002 printf(", subject=DNS name");
1003 cp = (const u_char *)(ni6 + 1);
1004 if (cp[0] == ep - cp - 1) {
1005 /* icmp-name-lookup-03, pascal string */
1006 if (vflag)
1007 printf(", 03 draft");
1008 cp++;
1009 printf(", \"");
1010 while (cp < ep) {
1011 safeputchar(*cp);
1012 cp++;
1014 printf("\"");
1015 } else
1016 dnsname_print(cp, ep);
1017 break;
1018 case ICMP6_NI_SUBJ_IPV4:
1019 if (!TTEST2(*dp, sizeof(*ni6) + sizeof(struct in_addr)))
1020 break;
1021 if (siz != sizeof(*ni6) + sizeof(struct in_addr)) {
1022 if (vflag)
1023 printf(", invalid subject len");
1024 break;
1026 printf(", subject=%s",
1027 getname((const u_char *)(ni6 + 1)));
1028 break;
1029 default:
1030 printf(", unknown subject");
1031 break;
1034 /*(*/
1035 printf(")");
1036 break;
1038 case ICMP6_NI_REPLY:
1039 if (icmp6len > siz) {
1040 printf("[|icmp6: node information reply]");
1041 break;
1044 needcomma = 0;
1046 ni6 = (struct icmp6_nodeinfo *)dp;
1047 printf(" node information reply");
1048 printf(" ("); /*)*/
1049 switch (ni6->ni_code) {
1050 case ICMP6_NI_SUCCESS:
1051 if (vflag) {
1052 printf("success");
1053 needcomma++;
1055 break;
1056 case ICMP6_NI_REFUSED:
1057 printf("refused");
1058 needcomma++;
1059 if (siz != sizeof(*ni6))
1060 if (vflag)
1061 printf(", invalid length");
1062 break;
1063 case ICMP6_NI_UNKNOWN:
1064 printf("unknown");
1065 needcomma++;
1066 if (siz != sizeof(*ni6))
1067 if (vflag)
1068 printf(", invalid length");
1069 break;
1072 if (ni6->ni_code != ICMP6_NI_SUCCESS) {
1073 /*(*/
1074 printf(")");
1075 break;
1078 switch (EXTRACT_16BITS(&ni6->ni_qtype)) {
1079 case NI_QTYPE_NOOP:
1080 if (needcomma)
1081 printf(", ");
1082 printf("noop");
1083 if (siz != sizeof(*ni6))
1084 if (vflag)
1085 printf(", invalid length");
1086 break;
1087 case NI_QTYPE_SUPTYPES:
1088 if (needcomma)
1089 printf(", ");
1090 printf("supported qtypes");
1091 i = EXTRACT_16BITS(&ni6->ni_flags);
1092 if (i)
1093 printf(" [%s]", (i & 0x01) ? "C" : "");
1094 break;
1095 case NI_QTYPE_FQDN:
1096 if (needcomma)
1097 printf(", ");
1098 printf("DNS name");
1099 cp = (const u_char *)(ni6 + 1) + 4;
1100 if (cp[0] == ep - cp - 1) {
1101 /* icmp-name-lookup-03, pascal string */
1102 if (vflag)
1103 printf(", 03 draft");
1104 cp++;
1105 printf(", \"");
1106 while (cp < ep) {
1107 safeputchar(*cp);
1108 cp++;
1110 printf("\"");
1111 } else
1112 dnsname_print(cp, ep);
1113 if ((EXTRACT_16BITS(&ni6->ni_flags) & 0x01) != 0)
1114 printf(" [TTL=%u]", *(u_int32_t *)(ni6 + 1));
1115 break;
1116 case NI_QTYPE_NODEADDR:
1117 if (needcomma)
1118 printf(", ");
1119 printf("node addresses");
1120 i = sizeof(*ni6);
1121 while (i < siz) {
1122 if (i + sizeof(struct in6_addr) + sizeof(int32_t) > siz)
1123 break;
1124 printf(" %s", getname6(bp + i));
1125 i += sizeof(struct in6_addr);
1126 printf("(%d)", (int32_t)EXTRACT_32BITS(bp + i));
1127 i += sizeof(int32_t);
1129 i = ni6->ni_flags;
1130 if (!i)
1131 break;
1132 printf(" [%s%s%s%s%s%s%s]",
1133 (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
1134 (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
1135 (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
1136 (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
1137 (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
1138 (i & NI_NODEADDR_FLAG_ALL) ? "A" : "",
1139 (i & NI_NODEADDR_FLAG_TRUNCATE) ? "T" : "");
1140 break;
1141 default:
1142 if (needcomma)
1143 printf(", ");
1144 printf("unknown");
1145 break;
1148 /*(*/
1149 printf(")");
1150 break;
1152 return;
1154 trunc:
1155 fputs("[|icmp6]", stdout);
1158 static void
1159 icmp6_rrenum_print(const u_char *bp, const u_char *ep)
1161 struct icmp6_router_renum *rr6;
1162 const char *cp;
1163 struct rr_pco_match *match;
1164 struct rr_pco_use *use;
1165 char hbuf[NI_MAXHOST];
1166 int n;
1168 if (ep < bp)
1169 return;
1170 rr6 = (struct icmp6_router_renum *)bp;
1171 cp = (const char *)(rr6 + 1);
1173 TCHECK(rr6->rr_reserved);
1174 switch (rr6->rr_code) {
1175 case ICMP6_ROUTER_RENUMBERING_COMMAND:
1176 printf("router renum: command");
1177 break;
1178 case ICMP6_ROUTER_RENUMBERING_RESULT:
1179 printf("router renum: result");
1180 break;
1181 case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET:
1182 printf("router renum: sequence number reset");
1183 break;
1184 default:
1185 printf("router renum: code-#%d", rr6->rr_code);
1186 break;
1189 printf(", seq=%u", EXTRACT_32BITS(&rr6->rr_seqnum));
1191 if (vflag) {
1192 #define F(x, y) ((rr6->rr_flags) & (x) ? (y) : "")
1193 printf("["); /*]*/
1194 if (rr6->rr_flags) {
1195 printf("%s%s%s%s%s,", F(ICMP6_RR_FLAGS_TEST, "T"),
1196 F(ICMP6_RR_FLAGS_REQRESULT, "R"),
1197 F(ICMP6_RR_FLAGS_FORCEAPPLY, "A"),
1198 F(ICMP6_RR_FLAGS_SPECSITE, "S"),
1199 F(ICMP6_RR_FLAGS_PREVDONE, "P"));
1201 printf("seg=%u,", rr6->rr_segnum);
1202 printf("maxdelay=%u", rr6->rr_maxdelay);
1203 if (rr6->rr_reserved)
1204 printf("rsvd=0x%x", EXTRACT_16BITS(&rr6->rr_reserved));
1205 /*[*/
1206 printf("]");
1207 #undef F
1210 if (rr6->rr_code == ICMP6_ROUTER_RENUMBERING_COMMAND) {
1211 match = (struct rr_pco_match *)cp;
1212 cp = (const char *)(match + 1);
1214 TCHECK(match->rpm_prefix);
1216 if (vflag > 1)
1217 printf("\n\t");
1218 else
1219 printf(" ");
1220 printf("match("); /*)*/
1221 switch (match->rpm_code) {
1222 case RPM_PCO_ADD: printf("add"); break;
1223 case RPM_PCO_CHANGE: printf("change"); break;
1224 case RPM_PCO_SETGLOBAL: printf("setglobal"); break;
1225 default: printf("#%u", match->rpm_code); break;
1228 if (vflag) {
1229 printf(",ord=%u", match->rpm_ordinal);
1230 printf(",min=%u", match->rpm_minlen);
1231 printf(",max=%u", match->rpm_maxlen);
1233 if (inet_ntop(AF_INET6, &match->rpm_prefix, hbuf, sizeof(hbuf)))
1234 printf(",%s/%u", hbuf, match->rpm_matchlen);
1235 else
1236 printf(",?/%u", match->rpm_matchlen);
1237 /*(*/
1238 printf(")");
1240 n = match->rpm_len - 3;
1241 if (n % 4)
1242 goto trunc;
1243 n /= 4;
1244 while (n-- > 0) {
1245 use = (struct rr_pco_use *)cp;
1246 cp = (const char *)(use + 1);
1248 TCHECK(use->rpu_prefix);
1250 if (vflag > 1)
1251 printf("\n\t");
1252 else
1253 printf(" ");
1254 printf("use("); /*)*/
1255 if (use->rpu_flags) {
1256 #define F(x, y) ((use->rpu_flags) & (x) ? (y) : "")
1257 printf("%s%s,",
1258 F(ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME, "V"),
1259 F(ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME, "P"));
1260 #undef F
1262 if (vflag) {
1263 printf("mask=0x%x,", use->rpu_ramask);
1264 printf("raflags=0x%x,", use->rpu_raflags);
1265 if (~use->rpu_vltime == 0)
1266 printf("vltime=infty,");
1267 else
1268 printf("vltime=%u,",
1269 EXTRACT_32BITS(&use->rpu_vltime));
1270 if (~use->rpu_pltime == 0)
1271 printf("pltime=infty,");
1272 else
1273 printf("pltime=%u,",
1274 EXTRACT_32BITS(&use->rpu_pltime));
1276 if (inet_ntop(AF_INET6, &use->rpu_prefix, hbuf,
1277 sizeof(hbuf)))
1278 printf("%s/%u/%u", hbuf, use->rpu_uselen,
1279 use->rpu_keeplen);
1280 else
1281 printf("?/%u/%u", use->rpu_uselen,
1282 use->rpu_keeplen);
1283 /*(*/
1284 printf(")");
1288 return;
1290 trunc:
1291 fputs("[|icmp6]", stdout);
1294 #endif /* INET6 */