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