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