]> The Tcpdump Group git mirrors - tcpdump/blob - print-icmp6.c
cleanup. do not use recurse for nd option priting.
[tcpdump] / print-icmp6.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-icmp6.c,v 1.51 2001-06-01 03:49:02 itojun Exp $";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef INET6
32
33 #include <ctype.h>
34
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39
40
41 #include <netinet/in.h>
42
43 #include <arpa/inet.h>
44
45 #include <stdio.h>
46 #include <netdb.h>
47
48 #include "ip6.h"
49 #include "icmp6.h"
50
51 #include "interface.h"
52 #include "addrtoname.h"
53
54 #include "udp.h"
55 #include "ah.h"
56
57 static const char *get_rtpref(u_int);
58 static const char *get_lifetime(u_int32_t);
59 void icmp6_opt_print(const u_char *, int);
60 void mld6_print(const u_char *);
61 static struct udphdr *get_upperlayer(u_char *, int *);
62 static void dnsname_print(const u_char *, const u_char *);
63 void icmp6_nodeinfo_print(int, const u_char *, const u_char *);
64 void icmp6_rrenum_print(int, const u_char *, const u_char *);
65
66 #ifndef abs
67 #define abs(a) ((0 < (a)) ? (a) : -(a))
68 #endif
69
70 #ifndef ND_RA_FLAG_RTPREF_MASK
71 #define ND_RA_FLAG_RTPREF_MASK 0x18
72 #endif
73
74 static const char *
75 get_rtpref(u_int v)
76 {
77 static const char *rtpref_str[] = {
78 "medium", /* 00 */
79 "high", /* 01 */
80 "rsv", /* 10 */
81 "low" /* 11 */
82 };
83
84 return rtpref_str[((v & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff];
85 }
86
87 static const char *
88 get_lifetime(u_int32_t v)
89 {
90 static char buf[20];
91
92 if (v == (u_int32_t)~0UL)
93 return "infinity";
94 else {
95 snprintf(buf, sizeof(buf), "%u", v);
96 return buf;
97 }
98 }
99
100 void
101 icmp6_print(const u_char *bp, const u_char *bp2)
102 {
103 const struct icmp6_hdr *dp;
104 const struct ip6_hdr *ip;
105 const char *str;
106 const struct ip6_hdr *oip;
107 const struct udphdr *ouh;
108 int dport;
109 const u_char *ep;
110 char buf[256];
111 int icmp6len, prot;
112
113 #if 0
114 #define TCHECK(var) if ((u_char *)&(var) > ep - sizeof(var)) goto trunc
115 #endif
116
117 dp = (struct icmp6_hdr *)bp;
118 ip = (struct ip6_hdr *)bp2;
119 oip = (struct ip6_hdr *)(dp + 1);
120 str = buf;
121 /* 'ep' points to the end of available data. */
122 ep = snapend;
123 if (ip->ip6_plen)
124 icmp6len = (ntohs(ip->ip6_plen) + sizeof(struct ip6_hdr) -
125 (bp - bp2));
126 else /* XXX: jumbo payload case... */
127 icmp6len = snapend - bp;
128
129 #if 0
130 (void)printf("%s > %s: ",
131 ip6addr_string(&ip->ip6_src),
132 ip6addr_string(&ip->ip6_dst));
133 #endif
134
135 TCHECK(dp->icmp6_code);
136 switch (dp->icmp6_type) {
137 case ICMP6_DST_UNREACH:
138 TCHECK(oip->ip6_dst);
139 switch (dp->icmp6_code) {
140 case ICMP6_DST_UNREACH_NOROUTE:
141 printf("icmp6: %s unreachable route",
142 ip6addr_string(&oip->ip6_dst));
143 break;
144 case ICMP6_DST_UNREACH_ADMIN:
145 printf("icmp6: %s unreachable prohibited",
146 ip6addr_string(&oip->ip6_dst));
147 break;
148 case ICMP6_DST_UNREACH_BEYONDSCOPE:
149 printf("icmp6: %s beyond scope of source address %s",
150 ip6addr_string(&oip->ip6_dst),
151 ip6addr_string(&oip->ip6_src));
152 break;
153 case ICMP6_DST_UNREACH_ADDR:
154 printf("icmp6: %s unreachable address",
155 ip6addr_string(&oip->ip6_dst));
156 break;
157 case ICMP6_DST_UNREACH_NOPORT:
158 if ((ouh = get_upperlayer((u_char *)oip, &prot))
159 == NULL)
160 goto trunc;
161
162 dport = ntohs(ouh->uh_dport);
163 switch (prot) {
164 case IPPROTO_TCP:
165 printf("icmp6: %s tcp port %s unreachable",
166 ip6addr_string(&oip->ip6_dst),
167 tcpport_string(dport));
168 break;
169 case IPPROTO_UDP:
170 printf("icmp6: %s udp port %s unreachable",
171 ip6addr_string(&oip->ip6_dst),
172 udpport_string(dport));
173 break;
174 default:
175 printf("icmp6: %s protocol %d port %d unreachable",
176 ip6addr_string(&oip->ip6_dst),
177 oip->ip6_nxt, dport);
178 break;
179 }
180 break;
181 default:
182 printf("icmp6: %s unreachable code-#%d",
183 ip6addr_string(&oip->ip6_dst),
184 dp->icmp6_code);
185 break;
186 }
187 break;
188 case ICMP6_PACKET_TOO_BIG:
189 TCHECK(dp->icmp6_mtu);
190 printf("icmp6: too big %u", (u_int32_t)ntohl(dp->icmp6_mtu));
191 break;
192 case ICMP6_TIME_EXCEEDED:
193 TCHECK(oip->ip6_dst);
194 switch (dp->icmp6_code) {
195 case ICMP6_TIME_EXCEED_TRANSIT:
196 printf("icmp6: time exceeded in-transit for %s",
197 ip6addr_string(&oip->ip6_dst));
198 break;
199 case ICMP6_TIME_EXCEED_REASSEMBLY:
200 printf("icmp6: ip6 reassembly time exceeded");
201 break;
202 default:
203 printf("icmp6: time exceeded code-#%d",
204 dp->icmp6_code);
205 break;
206 }
207 break;
208 case ICMP6_PARAM_PROB:
209 TCHECK(oip->ip6_dst);
210 switch (dp->icmp6_code) {
211 case ICMP6_PARAMPROB_HEADER:
212 printf("icmp6: parameter problem errorneous - octet %u",
213 (u_int32_t)ntohl(dp->icmp6_pptr));
214 break;
215 case ICMP6_PARAMPROB_NEXTHEADER:
216 printf("icmp6: parameter problem next header - octet %u",
217 (u_int32_t)ntohl(dp->icmp6_pptr));
218 break;
219 case ICMP6_PARAMPROB_OPTION:
220 printf("icmp6: parameter problem option - octet %u",
221 (u_int32_t)ntohl(dp->icmp6_pptr));
222 break;
223 default:
224 printf("icmp6: parameter problem code-#%d",
225 dp->icmp6_code);
226 break;
227 }
228 break;
229 case ICMP6_ECHO_REQUEST:
230 printf("icmp6: echo request");
231 break;
232 case ICMP6_ECHO_REPLY:
233 printf("icmp6: echo reply");
234 break;
235 case ICMP6_MEMBERSHIP_QUERY:
236 printf("icmp6: multicast listener query ");
237 mld6_print((const u_char *)dp);
238 break;
239 case ICMP6_MEMBERSHIP_REPORT:
240 printf("icmp6: multicast listener report ");
241 mld6_print((const u_char *)dp);
242 break;
243 case ICMP6_MEMBERSHIP_REDUCTION:
244 printf("icmp6: multicast listener done ");
245 mld6_print((const u_char *)dp);
246 break;
247 case ND_ROUTER_SOLICIT:
248 printf("icmp6: router solicitation ");
249 if (vflag) {
250 #define RTSOLLEN 8
251 icmp6_opt_print((const u_char *)dp + RTSOLLEN,
252 icmp6len - RTSOLLEN);
253 }
254 break;
255 case ND_ROUTER_ADVERT:
256 printf("icmp6: router advertisement");
257 if (vflag) {
258 struct nd_router_advert *p;
259
260 p = (struct nd_router_advert *)dp;
261 TCHECK(p->nd_ra_retransmit);
262 printf("(chlim=%d, ", (int)p->nd_ra_curhoplimit);
263 if (p->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED)
264 printf("M");
265 if (p->nd_ra_flags_reserved & ND_RA_FLAG_OTHER)
266 printf("O");
267 #ifndef ND_RA_FLAG_HA
268 #define ND_RA_FLAG_HA 0x20
269 #endif
270 if (p->nd_ra_flags_reserved & ND_RA_FLAG_HA)
271 printf("H");
272
273 if ((p->nd_ra_flags_reserved & ~ND_RA_FLAG_RTPREF_MASK)
274 != 0)
275 printf(" ");
276
277 printf("pref=%s, ",
278 get_rtpref(p->nd_ra_flags_reserved));
279
280 printf("router_ltime=%d, ", ntohs(p->nd_ra_router_lifetime));
281 printf("reachable_time=%u, ",
282 (u_int32_t)ntohl(p->nd_ra_reachable));
283 printf("retrans_time=%u)",
284 (u_int32_t)ntohl(p->nd_ra_retransmit));
285 #define RTADVLEN 16
286 icmp6_opt_print((const u_char *)dp + RTADVLEN,
287 icmp6len - RTADVLEN);
288 }
289 break;
290 case ND_NEIGHBOR_SOLICIT:
291 {
292 struct nd_neighbor_solicit *p;
293 p = (struct nd_neighbor_solicit *)dp;
294 TCHECK(p->nd_ns_target);
295 printf("icmp6: neighbor sol: who has %s",
296 ip6addr_string(&p->nd_ns_target));
297 if (vflag) {
298 #define NDSOLLEN 24
299 icmp6_opt_print((const u_char *)dp + NDSOLLEN,
300 icmp6len - NDSOLLEN);
301 }
302 }
303 break;
304 case ND_NEIGHBOR_ADVERT:
305 {
306 struct nd_neighbor_advert *p;
307
308 p = (struct nd_neighbor_advert *)dp;
309 TCHECK(p->nd_na_target);
310 printf("icmp6: neighbor adv: tgt is %s",
311 ip6addr_string(&p->nd_na_target));
312 if (vflag) {
313 #define ND_NA_FLAG_ALL \
314 (ND_NA_FLAG_ROUTER|ND_NA_FLAG_SOLICITED|ND_NA_FLAG_OVERRIDE)
315 /* we don't need ntohl() here. see advanced-api-04. */
316 if (p->nd_na_flags_reserved & ND_NA_FLAG_ALL) {
317 #undef ND_NA_FLAG_ALL
318 u_int32_t flags;
319
320 flags = p->nd_na_flags_reserved;
321 printf("(");
322 if (flags & ND_NA_FLAG_ROUTER)
323 printf("R");
324 if (flags & ND_NA_FLAG_SOLICITED)
325 printf("S");
326 if (flags & ND_NA_FLAG_OVERRIDE)
327 printf("O");
328 printf(")");
329 }
330 #define NDADVLEN 24
331 icmp6_opt_print((const u_char *)dp + NDADVLEN,
332 icmp6len - NDADVLEN);
333 #undef NDADVLEN
334 }
335 }
336 break;
337 case ND_REDIRECT:
338 #define RDR(i) ((struct nd_redirect *)(i))
339 TCHECK(RDR(dp)->nd_rd_dst);
340 printf("icmp6: redirect %s",
341 getname6((const u_char *)&RDR(dp)->nd_rd_dst));
342 printf(" to %s",
343 getname6((const u_char*)&RDR(dp)->nd_rd_target));
344 #define REDIRECTLEN 40
345 if (vflag) {
346 icmp6_opt_print((const u_char *)dp + REDIRECTLEN,
347 icmp6len - REDIRECTLEN);
348 }
349 break;
350 #undef REDIRECTLEN
351 #undef RDR
352 case ICMP6_ROUTER_RENUMBERING:
353 icmp6_rrenum_print(icmp6len, bp, ep);
354 break;
355 case ICMP6_NI_QUERY:
356 case ICMP6_NI_REPLY:
357 icmp6_nodeinfo_print(icmp6len, bp, ep);
358 break;
359 default:
360 printf("icmp6: type-#%d", dp->icmp6_type);
361 break;
362 }
363 return;
364 trunc:
365 fputs("[|icmp6]", stdout);
366 #if 0
367 #undef TCHECK
368 #endif
369 }
370
371 static struct udphdr *
372 get_upperlayer(u_char *bp, int *prot)
373 {
374 const u_char *ep;
375 struct ip6_hdr *ip6 = (struct ip6_hdr *)bp;
376 struct udphdr *uh;
377 struct ip6_hbh *hbh;
378 struct ip6_frag *fragh;
379 struct ah *ah;
380 int nh, hlen;
381
382 /* 'ep' points to the end of available data. */
383 ep = snapend;
384
385 if (TTEST(ip6->ip6_nxt) == 0)
386 return NULL;
387
388 nh = ip6->ip6_nxt;
389 hlen = sizeof(struct ip6_hdr);
390
391 while (bp < snapend) {
392 bp += hlen;
393
394 switch(nh) {
395 case IPPROTO_UDP:
396 case IPPROTO_TCP:
397 uh = (struct udphdr *)bp;
398 if (TTEST(uh->uh_dport)) {
399 *prot = nh;
400 return(uh);
401 }
402 else
403 return(NULL);
404 /* NOTREACHED */
405
406 case IPPROTO_HOPOPTS:
407 case IPPROTO_DSTOPTS:
408 case IPPROTO_ROUTING:
409 hbh = (struct ip6_hbh *)bp;
410 if (TTEST(hbh->ip6h_len) == 0)
411 return(NULL);
412 nh = hbh->ip6h_nxt;
413 hlen = (hbh->ip6h_len + 1) << 3;
414 break;
415
416 case IPPROTO_FRAGMENT: /* this should be odd, but try anyway */
417 fragh = (struct ip6_frag *)bp;
418 if (TTEST(fragh->ip6f_offlg) == 0)
419 return(NULL);
420 /* fragments with non-zero offset are meaningless */
421 if ((fragh->ip6f_offlg & IP6F_OFF_MASK) != 0)
422 return(NULL);
423 nh = fragh->ip6f_nxt;
424 hlen = sizeof(struct ip6_frag);
425 break;
426
427 case IPPROTO_AH:
428 ah = (struct ah *)bp;
429 if (TTEST(ah->ah_len) == 0)
430 return(NULL);
431 nh = ah->ah_nxt;
432 hlen = (ah->ah_len + 2) << 2;
433 break;
434
435 default: /* unknown or undecodable header */
436 *prot = nh; /* meaningless, but set here anyway */
437 return(NULL);
438 }
439 }
440
441 return(NULL); /* should be notreached, though */
442 }
443
444 void
445 icmp6_opt_print(const u_char *bp, int resid)
446 {
447 const struct nd_opt_hdr *op;
448 const struct nd_opt_hdr *opl; /* why there's no struct? */
449 const struct nd_opt_prefix_info *opp;
450 const struct icmp6_opts_redirect *opr;
451 const struct nd_opt_mtu *opm;
452 const struct nd_opt_advinterval *opa;
453 const struct nd_opt_route_info *opri;
454 const u_char *cp, *ep;
455 int opts_len;
456 struct in6_addr in6, *in6p;
457
458 #define ECHECK(var) if ((u_char *)&(var) > ep - sizeof(var)) return
459
460 cp = bp;
461 /* 'ep' points to the end of available data. */
462 ep = snapend;
463
464 while (cp < ep) {
465 op = (struct nd_opt_hdr *)cp;
466
467 ECHECK(op->nd_opt_len);
468 if (resid <= 0)
469 return;
470 if (op->nd_opt_len == 0)
471 goto trunc;
472 if (cp + (op->nd_opt_len << 3) > ep)
473 goto trunc;
474
475 switch (op->nd_opt_type) {
476 case ND_OPT_SOURCE_LINKADDR:
477 opl = (struct nd_opt_hdr *)op;
478 printf("(src lladdr: %s", /*)*/
479 etheraddr_string((u_char *)(opl + 1)));
480 if (opl->nd_opt_len != 1)
481 printf("!");
482 /*(*/
483 printf(")");
484 break;
485 case ND_OPT_TARGET_LINKADDR:
486 opl = (struct nd_opt_hdr *)op;
487 printf("(tgt lladdr: %s", /*)*/
488 etheraddr_string((u_char *)(opl + 1)));
489 if (opl->nd_opt_len != 1)
490 printf("!");
491 /*(*/
492 printf(")");
493 break;
494 case ND_OPT_PREFIX_INFORMATION:
495 opp = (struct nd_opt_prefix_info *)op;
496 TCHECK(opp->nd_opt_pi_prefix);
497 printf("(prefix info: "); /*)*/
498 if (opp->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
499 printf("L");
500 if (opp->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO)
501 printf("A");
502 if (opp->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ROUTER)
503 printf("R");
504 if (opp->nd_opt_pi_flags_reserved)
505 printf(" ");
506 printf("valid_ltime=%s,",
507 get_lifetime((u_int32_t)ntohl(opp->nd_opt_pi_valid_time)));
508 printf("preferred_ltime=%s,",
509 get_lifetime((u_int32_t)ntohl(opp->nd_opt_pi_preferred_time)));
510 printf("prefix=%s/%d", ip6addr_string(&opp->nd_opt_pi_prefix),
511 opp->nd_opt_pi_prefix_len);
512 if (opp->nd_opt_pi_len != 4)
513 printf("!");
514 /*(*/
515 printf(")");
516 break;
517 case ND_OPT_REDIRECTED_HEADER:
518 opr = (struct icmp6_opts_redirect *)op;
519 printf("(redirect)");
520 /* xxx */
521 break;
522 case ND_OPT_MTU:
523 opm = (struct nd_opt_mtu *)op;
524 TCHECK(opm->nd_opt_mtu_mtu);
525 printf("(mtu:"); /*)*/
526 printf(" mtu=%u", (u_int32_t)ntohl(opm->nd_opt_mtu_mtu));
527 if (opm->nd_opt_mtu_len != 1)
528 printf("!");
529 printf(")");
530 break;
531 case ND_OPT_ADVINTERVAL:
532 opa = (struct nd_opt_advinterval *)op;
533 TCHECK(opa->nd_opt_adv_interval);
534 printf("(advint:"); /*)*/
535 printf(" advint=%u",
536 (u_int32_t)ntohl(opa->nd_opt_adv_interval));
537 /*(*/
538 printf(")");
539 break;
540 case ND_OPT_ROUTE_INFO:
541 opri = (struct nd_opt_route_info *)op;
542 TCHECK(opri->nd_opt_rti_lifetime);
543 memset(&in6, 0, sizeof(in6));
544 in6p = (struct in6_addr *)(opri + 1);
545 switch (op->nd_opt_len) {
546 case 1:
547 break;
548 case 2:
549 TCHECK2(*in6p, 8);
550 memcpy(&in6, opri + 1, 8);
551 break;
552 case 3:
553 TCHECK(*in6p);
554 memcpy(&in6, opri + 1, sizeof(in6));
555 break;
556 default:
557 goto trunc;
558 }
559 printf("(rtinfo:"); /*)*/
560 printf(" %s/%u", ip6addr_string(&in6),
561 opri->nd_opt_rti_prefixlen);
562 printf(", pref=%s", get_rtpref(opri->nd_opt_rti_flags));
563 printf(", lifetime=%s",
564 get_lifetime((u_int32_t)ntohl(opri->nd_opt_rti_lifetime)));
565 /*(*/
566 printf(")");
567 break;
568 default:
569 opts_len = op->nd_opt_len;
570 printf("(unknwon opt_type=%d, opt_len=%d)",
571 op->nd_opt_type, opts_len);
572 if (opts_len == 0)
573 opts_len = 1; /* XXX */
574 break;
575 }
576
577 cp += op->nd_opt_len << 3;
578 resid -= op->nd_opt_len << 3;
579 }
580 return;
581
582 trunc:
583 fputs("[ndp opt]", stdout);
584 return;
585 #undef ECHECK
586 }
587
588 void
589 mld6_print(const u_char *bp)
590 {
591 struct mld6_hdr *mp = (struct mld6_hdr *)bp;
592 const u_char *ep;
593
594 /* 'ep' points to the end of available data. */
595 ep = snapend;
596
597 if ((u_char *)mp + sizeof(*mp) > ep)
598 return;
599
600 printf("max resp delay: %d ", ntohs(mp->mld6_maxdelay));
601 printf("addr: %s", ip6addr_string(&mp->mld6_addr));
602 }
603
604 static void
605 dnsname_print(const u_char *cp, const u_char *ep)
606 {
607 int i;
608
609 /* DNS name decoding - no decompression */
610 printf(", \"");
611 while (cp < ep) {
612 i = *cp++;
613 if (i) {
614 if (i > ep - cp) {
615 printf("???");
616 break;
617 }
618 while (i-- && cp < ep) {
619 safeputchar(*cp);
620 cp++;
621 }
622 if (cp + 1 < ep && *cp)
623 printf(".");
624 } else {
625 if (cp == ep) {
626 /* FQDN */
627 printf(".");
628 } else if (cp + 1 == ep && *cp == '\0') {
629 /* truncated */
630 } else {
631 /* invalid */
632 printf("???");
633 }
634 break;
635 }
636 }
637 printf("\"");
638 }
639
640 void
641 icmp6_nodeinfo_print(int icmp6len, const u_char *bp, const u_char *ep)
642 {
643 struct icmp6_nodeinfo *ni6;
644 struct icmp6_hdr *dp;
645 const u_char *cp;
646 int siz, i;
647 int needcomma;
648
649 dp = (struct icmp6_hdr *)bp;
650 ni6 = (struct icmp6_nodeinfo *)bp;
651 siz = ep - bp;
652
653 switch (ni6->ni_type) {
654 case ICMP6_NI_QUERY:
655 if (siz == sizeof(*dp) + 4) {
656 /* KAME who-are-you */
657 printf("icmp6: who-are-you request");
658 break;
659 }
660 printf("icmp6: node information query");
661
662 TCHECK2(*dp, sizeof(*ni6));
663 ni6 = (struct icmp6_nodeinfo *)dp;
664 printf(" ("); /*)*/
665 switch (ntohs(ni6->ni_qtype)) {
666 case NI_QTYPE_NOOP:
667 printf("noop");
668 break;
669 case NI_QTYPE_SUPTYPES:
670 printf("supported qtypes");
671 i = ntohs(ni6->ni_flags);
672 if (i)
673 printf(" [%s]", (i & 0x01) ? "C" : "");
674 break;
675 break;
676 case NI_QTYPE_FQDN:
677 printf("DNS name");
678 break;
679 case NI_QTYPE_NODEADDR:
680 printf("node addresses");
681 i = ni6->ni_flags;
682 if (!i)
683 break;
684 /* NI_NODEADDR_FLAG_TRUNCATE undefined for query */
685 printf(" [%s%s%s%s%s%s]",
686 (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
687 (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
688 (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
689 (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
690 (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
691 (i & NI_NODEADDR_FLAG_ALL) ? "A" : "");
692 break;
693 default:
694 printf("unknown");
695 break;
696 }
697
698 if (ni6->ni_qtype == NI_QTYPE_NOOP ||
699 ni6->ni_qtype == NI_QTYPE_SUPTYPES) {
700 if (siz != sizeof(*ni6))
701 if (vflag)
702 printf(", invalid len");
703 /*(*/
704 printf(")");
705 break;
706 }
707
708
709 /* XXX backward compat, icmp-name-lookup-03 */
710 if (siz == sizeof(*ni6)) {
711 printf(", 03 draft");
712 /*(*/
713 printf(")");
714 break;
715 }
716
717 switch (ni6->ni_code) {
718 case ICMP6_NI_SUBJ_IPV6:
719 if (!TTEST2(*dp,
720 sizeof(*ni6) + sizeof(struct in6_addr)))
721 break;
722 if (siz != sizeof(*ni6) + sizeof(struct in6_addr)) {
723 if (vflag)
724 printf(", invalid subject len");
725 break;
726 }
727 printf(", subject=%s",
728 getname6((const u_char *)(ni6 + 1)));
729 break;
730 case ICMP6_NI_SUBJ_FQDN:
731 printf(", subject=DNS name");
732 cp = (const u_char *)(ni6 + 1);
733 if (cp[0] == ep - cp - 1) {
734 /* icmp-name-lookup-03, pascal string */
735 if (vflag)
736 printf(", 03 draft");
737 cp++;
738 printf(", \"");
739 while (cp < ep) {
740 safeputchar(*cp);
741 cp++;
742 }
743 printf("\"");
744 } else
745 dnsname_print(cp, ep);
746 break;
747 case ICMP6_NI_SUBJ_IPV4:
748 if (!TTEST2(*dp, sizeof(*ni6) + sizeof(struct in_addr)))
749 break;
750 if (siz != sizeof(*ni6) + sizeof(struct in_addr)) {
751 if (vflag)
752 printf(", invalid subject len");
753 break;
754 }
755 printf(", subject=%s",
756 getname((const u_char *)(ni6 + 1)));
757 break;
758 default:
759 printf(", unknown subject");
760 break;
761 }
762
763 /*(*/
764 printf(")");
765 break;
766
767 case ICMP6_NI_REPLY:
768 if (icmp6len > siz) {
769 printf("[|icmp6: node information reply]");
770 break;
771 }
772
773 needcomma = 0;
774
775 ni6 = (struct icmp6_nodeinfo *)dp;
776 printf("icmp6: node information reply");
777 printf(" ("); /*)*/
778 switch (ni6->ni_code) {
779 case ICMP6_NI_SUCCESS:
780 if (vflag) {
781 printf("success");
782 needcomma++;
783 }
784 break;
785 case ICMP6_NI_REFUSED:
786 printf("refused");
787 needcomma++;
788 if (siz != sizeof(*ni6))
789 if (vflag)
790 printf(", invalid length");
791 break;
792 case ICMP6_NI_UNKNOWN:
793 printf("unknown");
794 needcomma++;
795 if (siz != sizeof(*ni6))
796 if (vflag)
797 printf(", invalid length");
798 break;
799 }
800
801 if (ni6->ni_code != ICMP6_NI_SUCCESS) {
802 /*(*/
803 printf(")");
804 break;
805 }
806
807 switch (ntohs(ni6->ni_qtype)) {
808 case NI_QTYPE_NOOP:
809 if (needcomma)
810 printf(", ");
811 printf("noop");
812 if (siz != sizeof(*ni6))
813 if (vflag)
814 printf(", invalid length");
815 break;
816 case NI_QTYPE_SUPTYPES:
817 if (needcomma)
818 printf(", ");
819 printf("supported qtypes");
820 i = ntohs(ni6->ni_flags);
821 if (i)
822 printf(" [%s]", (i & 0x01) ? "C" : "");
823 break;
824 case NI_QTYPE_FQDN:
825 if (needcomma)
826 printf(", ");
827 printf("DNS name");
828 cp = (const u_char *)(ni6 + 1) + 4;
829 if (cp[0] == ep - cp - 1) {
830 /* icmp-name-lookup-03, pascal string */
831 if (vflag)
832 printf(", 03 draft");
833 cp++;
834 printf(", \"");
835 while (cp < ep) {
836 safeputchar(*cp);
837 cp++;
838 }
839 printf("\"");
840 } else
841 dnsname_print(cp, ep);
842 if ((ntohs(ni6->ni_flags) & 0x01) != 0)
843 printf(" [TTL=%u]", *(u_int32_t *)(ni6 + 1));
844 break;
845 case NI_QTYPE_NODEADDR:
846 if (needcomma)
847 printf(", ");
848 printf("node addresses");
849 i = sizeof(*ni6);
850 while (i < siz) {
851 if (i + sizeof(struct in6_addr) + sizeof(int32_t) > siz)
852 break;
853 printf(" %s", getname6(bp + i));
854 i += sizeof(struct in6_addr);
855 printf("(%d)", (int32_t)ntohl(*(int32_t *)(bp + i)));
856 i += sizeof(int32_t);
857 }
858 i = ni6->ni_flags;
859 if (!i)
860 break;
861 printf(" [%s%s%s%s%s%s%s]",
862 (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
863 (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
864 (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
865 (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
866 (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
867 (i & NI_NODEADDR_FLAG_ALL) ? "A" : "",
868 (i & NI_NODEADDR_FLAG_TRUNCATE) ? "T" : "");
869 break;
870 default:
871 if (needcomma)
872 printf(", ");
873 printf("unknown");
874 break;
875 }
876
877 /*(*/
878 printf(")");
879 break;
880 }
881 return;
882
883 trunc:
884 fputs("[|icmp6]", stdout);
885 }
886
887 void
888 icmp6_rrenum_print(int icmp6len, const u_char *bp, const u_char *ep)
889 {
890 struct icmp6_router_renum *rr6;
891 struct icmp6_hdr *dp;
892 size_t siz;
893 const char *cp;
894 struct rr_pco_match *match;
895 struct rr_pco_use *use;
896 char hbuf[NI_MAXHOST];
897 int n;
898
899 dp = (struct icmp6_hdr *)bp;
900 rr6 = (struct icmp6_router_renum *)bp;
901 siz = ep - bp;
902 cp = (const char *)(rr6 + 1);
903
904 TCHECK(rr6->rr_reserved);
905 switch (rr6->rr_code) {
906 case ICMP6_ROUTER_RENUMBERING_COMMAND:
907 printf("router renum: command");
908 break;
909 case ICMP6_ROUTER_RENUMBERING_RESULT:
910 printf("router renum: result");
911 break;
912 case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET:
913 printf("router renum: sequence number reset");
914 break;
915 default:
916 printf("router renum: code-#%d", rr6->rr_code);
917 break;
918 }
919
920 printf(", seq=%u", (u_int32_t)ntohl(rr6->rr_seqnum));
921
922 if (vflag) {
923 #define F(x, y) ((rr6->rr_flags) & (x) ? (y) : "")
924 printf("["); /*]*/
925 if (rr6->rr_flags) {
926 printf("%s%s%s%s%s,", F(ICMP6_RR_FLAGS_TEST, "T"),
927 F(ICMP6_RR_FLAGS_REQRESULT, "R"),
928 F(ICMP6_RR_FLAGS_FORCEAPPLY, "A"),
929 F(ICMP6_RR_FLAGS_SPECSITE, "S"),
930 F(ICMP6_RR_FLAGS_PREVDONE, "P"));
931 }
932 printf("seg=%u,", rr6->rr_segnum);
933 printf("maxdelay=%u", rr6->rr_maxdelay);
934 if (rr6->rr_reserved)
935 printf("rsvd=0x%x", (u_int16_t)ntohs(rr6->rr_reserved));
936 /*[*/
937 printf("]");
938 #undef F
939 }
940
941 if (rr6->rr_code == ICMP6_ROUTER_RENUMBERING_COMMAND) {
942 match = (struct rr_pco_match *)cp;
943 cp = (const char *)(match + 1);
944
945 TCHECK(match->rpm_prefix);
946
947 if (vflag > 1)
948 printf("\n\t");
949 else
950 printf(" ");
951 printf("match("); /*)*/
952 switch (match->rpm_code) {
953 case RPM_PCO_ADD: printf("add"); break;
954 case RPM_PCO_CHANGE: printf("change"); break;
955 case RPM_PCO_SETGLOBAL: printf("setglobal"); break;
956 default: printf("#%u", match->rpm_code); break;
957 }
958
959 if (vflag) {
960 printf(",ord=%u", match->rpm_ordinal);
961 printf(",min=%u", match->rpm_minlen);
962 printf(",max=%u", match->rpm_maxlen);
963 }
964 if (inet_ntop(AF_INET6, &match->rpm_prefix, hbuf, sizeof(hbuf)))
965 printf(",%s/%u", hbuf, match->rpm_matchlen);
966 else
967 printf(",?/%u", match->rpm_matchlen);
968 /*(*/
969 printf(")");
970
971 n = match->rpm_len - 3;
972 if (n % 4)
973 goto trunc;
974 n /= 4;
975 while (n-- > 0) {
976 use = (struct rr_pco_use *)cp;
977 cp = (const char *)(use + 1);
978
979 TCHECK(use->rpu_prefix);
980
981 if (vflag > 1)
982 printf("\n\t");
983 else
984 printf(" ");
985 printf("use("); /*)*/
986 if (use->rpu_flags) {
987 #define F(x, y) ((use->rpu_flags) & (x) ? (y) : "")
988 printf("%s%s,",
989 F(ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME, "V"),
990 F(ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME, "P"));
991 #undef F
992 }
993 if (vflag) {
994 printf("mask=0x%x,", use->rpu_ramask);
995 printf("raflags=0x%x,", use->rpu_raflags);
996 if (~use->rpu_vltime == 0)
997 printf("vltime=infty,");
998 else
999 printf("vltime=%u,",
1000 (u_int32_t)ntohl(use->rpu_vltime));
1001 if (~use->rpu_pltime == 0)
1002 printf("pltime=infty,");
1003 else
1004 printf("pltime=%u,",
1005 (u_int32_t)ntohl(use->rpu_pltime));
1006 }
1007 if (inet_ntop(AF_INET6, &use->rpu_prefix, hbuf,
1008 sizeof(hbuf)))
1009 printf("%s/%u/%u", hbuf, use->rpu_uselen,
1010 use->rpu_keeplen);
1011 else
1012 printf("?/%u/%u", use->rpu_uselen,
1013 use->rpu_keeplen);
1014 /*(*/
1015 printf(")");
1016 }
1017 }
1018
1019 return;
1020
1021 trunc:
1022 fputs("[|icmp6]", stdout);
1023 }
1024
1025 #endif /* INET6 */