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