]> The Tcpdump Group git mirrors - tcpdump/blob - print-icmp6.c
initial checkin
[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.65 2002-09-05 00:52:30 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 if (vflag) {
377 TCHECK(dp->icmp6_data16[0]);
378 printf("(id=%d)", ntohs(dp->icmp6_data16[0]));
379 }
380 break;
381 case ICMP6_MOBILEPREFIX_ADVERT:
382 printf("icmp6: mobile router advertisement");
383 if (vflag) {
384 TCHECK(dp->icmp6_data16[0]);
385 printf("(id=%d)", ntohs(dp->icmp6_data16[0]));
386 #define MPADVLEN 6
387 icmp6_opt_print((const u_char *)dp + MPADVLEN,
388 icmp6len - MPADVLEN);
389 }
390 break;
391 default:
392 printf("icmp6: type-#%d", dp->icmp6_type);
393 break;
394 }
395 return;
396 trunc:
397 fputs("[|icmp6]", stdout);
398 }
399
400 static struct udphdr *
401 get_upperlayer(u_char *bp, int *prot)
402 {
403 const u_char *ep;
404 struct ip6_hdr *ip6 = (struct ip6_hdr *)bp;
405 struct udphdr *uh;
406 struct ip6_hbh *hbh;
407 struct ip6_frag *fragh;
408 struct ah *ah;
409 int nh, hlen;
410
411 /* 'ep' points to the end of available data. */
412 ep = snapend;
413
414 if (TTEST(ip6->ip6_nxt) == 0)
415 return NULL;
416
417 nh = ip6->ip6_nxt;
418 hlen = sizeof(struct ip6_hdr);
419
420 while (bp < snapend) {
421 bp += hlen;
422
423 switch(nh) {
424 case IPPROTO_UDP:
425 case IPPROTO_TCP:
426 uh = (struct udphdr *)bp;
427 if (TTEST(uh->uh_dport)) {
428 *prot = nh;
429 return(uh);
430 }
431 else
432 return(NULL);
433 /* NOTREACHED */
434
435 case IPPROTO_HOPOPTS:
436 case IPPROTO_DSTOPTS:
437 case IPPROTO_ROUTING:
438 hbh = (struct ip6_hbh *)bp;
439 if (TTEST(hbh->ip6h_len) == 0)
440 return(NULL);
441 nh = hbh->ip6h_nxt;
442 hlen = (hbh->ip6h_len + 1) << 3;
443 break;
444
445 case IPPROTO_FRAGMENT: /* this should be odd, but try anyway */
446 fragh = (struct ip6_frag *)bp;
447 if (TTEST(fragh->ip6f_offlg) == 0)
448 return(NULL);
449 /* fragments with non-zero offset are meaningless */
450 if ((fragh->ip6f_offlg & IP6F_OFF_MASK) != 0)
451 return(NULL);
452 nh = fragh->ip6f_nxt;
453 hlen = sizeof(struct ip6_frag);
454 break;
455
456 case IPPROTO_AH:
457 ah = (struct ah *)bp;
458 if (TTEST(ah->ah_len) == 0)
459 return(NULL);
460 nh = ah->ah_nxt;
461 hlen = (ah->ah_len + 2) << 2;
462 break;
463
464 default: /* unknown or undecodable header */
465 *prot = nh; /* meaningless, but set here anyway */
466 return(NULL);
467 }
468 }
469
470 return(NULL); /* should be notreached, though */
471 }
472
473 void
474 icmp6_opt_print(const u_char *bp, int resid)
475 {
476 const struct nd_opt_hdr *op;
477 const struct nd_opt_hdr *opl; /* why there's no struct? */
478 const struct nd_opt_prefix_info *opp;
479 const struct icmp6_opts_redirect *opr;
480 const struct nd_opt_mtu *opm;
481 const struct nd_opt_advinterval *opa;
482 const struct nd_opt_homeagent_info *oph;
483 const struct nd_opt_route_info *opri;
484 const u_char *cp, *ep;
485 struct in6_addr in6, *in6p;
486 size_t l;
487
488 #define ECHECK(var) if ((u_char *)&(var) > ep - sizeof(var)) return
489
490 cp = bp;
491 /* 'ep' points to the end of available data. */
492 ep = snapend;
493
494 while (cp < ep) {
495 op = (struct nd_opt_hdr *)cp;
496
497 ECHECK(op->nd_opt_len);
498 if (resid <= 0)
499 return;
500 if (op->nd_opt_len == 0)
501 goto trunc;
502 if (cp + (op->nd_opt_len << 3) > ep)
503 goto trunc;
504
505 switch (op->nd_opt_type) {
506 case ND_OPT_SOURCE_LINKADDR:
507 opl = (struct nd_opt_hdr *)op;
508 printf("(src lladdr: ");
509 l = (op->nd_opt_len << 3) - 2;
510 print_lladdr(cp + 2, l);
511 /*(*/
512 printf(")");
513 break;
514 case ND_OPT_TARGET_LINKADDR:
515 opl = (struct nd_opt_hdr *)op;
516 printf("(tgt lladdr: ");
517 l = (op->nd_opt_len << 3) - 2;
518 print_lladdr(cp + 2, l);
519 /*(*/
520 printf(")");
521 break;
522 case ND_OPT_PREFIX_INFORMATION:
523 opp = (struct nd_opt_prefix_info *)op;
524 TCHECK(opp->nd_opt_pi_prefix);
525 printf("(prefix info: "); /*)*/
526 if (op->nd_opt_len != 4) {
527 printf("badlen");
528 /*(*/
529 printf(")");
530 break;
531 }
532 if (opp->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
533 printf("L");
534 if (opp->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO)
535 printf("A");
536 if (opp->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ROUTER)
537 printf("R");
538 if (opp->nd_opt_pi_flags_reserved)
539 printf(" ");
540 printf("valid_ltime=%s,",
541 get_lifetime((u_int32_t)ntohl(opp->nd_opt_pi_valid_time)));
542 printf("preferred_ltime=%s,",
543 get_lifetime((u_int32_t)ntohl(opp->nd_opt_pi_preferred_time)));
544 printf("prefix=%s/%d",
545 ip6addr_string(&opp->nd_opt_pi_prefix),
546 opp->nd_opt_pi_prefix_len);
547 if (opp->nd_opt_pi_len != 4)
548 printf("!");
549 /*(*/
550 printf(")");
551 break;
552 case ND_OPT_REDIRECTED_HEADER:
553 opr = (struct icmp6_opts_redirect *)op;
554 printf("(redirect)");
555 /* xxx */
556 break;
557 case ND_OPT_MTU:
558 opm = (struct nd_opt_mtu *)op;
559 TCHECK(opm->nd_opt_mtu_mtu);
560 printf("(mtu:"); /*)*/
561 if (op->nd_opt_len != 1) {
562 printf("badlen");
563 /*(*/
564 printf(")");
565 break;
566 }
567 printf(" mtu=%u", (u_int32_t)ntohl(opm->nd_opt_mtu_mtu));
568 if (opm->nd_opt_mtu_len != 1)
569 printf("!");
570 printf(")");
571 break;
572 case ND_OPT_ADVINTERVAL:
573 opa = (struct nd_opt_advinterval *)op;
574 TCHECK(opa->nd_opt_adv_interval);
575 printf("(advint:"); /*)*/
576 printf(" advint=%u",
577 (u_int32_t)ntohl(opa->nd_opt_adv_interval));
578 /*(*/
579 printf(")");
580 break;
581 case ND_OPT_HOMEAGENT_INFO:
582 oph = (struct nd_opt_homeagent_info *)op;
583 TCHECK(oph->nd_opt_hai_lifetime);
584 printf("(ha info:"); /*)*/
585 printf(" pref=%d", ntohs(oph->nd_opt_hai_preference));
586 printf(", lifetime=%u", ntohs(oph->nd_opt_hai_lifetime));
587 printf(")");
588 break;
589 case ND_OPT_ROUTE_INFO:
590 opri = (struct nd_opt_route_info *)op;
591 TCHECK(opri->nd_opt_rti_lifetime);
592 memset(&in6, 0, sizeof(in6));
593 in6p = (struct in6_addr *)(opri + 1);
594 switch (op->nd_opt_len) {
595 case 1:
596 break;
597 case 2:
598 TCHECK2(*in6p, 8);
599 memcpy(&in6, opri + 1, 8);
600 break;
601 case 3:
602 TCHECK(*in6p);
603 memcpy(&in6, opri + 1, sizeof(in6));
604 break;
605 default:
606 goto trunc;
607 }
608 printf("(rtinfo:"); /*)*/
609 printf(" %s/%u", ip6addr_string(&in6),
610 opri->nd_opt_rti_prefixlen);
611 printf(", pref=%s", get_rtpref(opri->nd_opt_rti_flags));
612 printf(", lifetime=%s",
613 get_lifetime((u_int32_t)ntohl(opri->nd_opt_rti_lifetime)));
614 /*(*/
615 printf(")");
616 break;
617 default:
618 printf("(unknown opt_type=%d, opt_len=%d)",
619 op->nd_opt_type, op->nd_opt_len);
620 break;
621 }
622
623 cp += op->nd_opt_len << 3;
624 resid -= op->nd_opt_len << 3;
625 }
626 return;
627
628 trunc:
629 fputs("[ndp opt]", stdout);
630 return;
631 #undef ECHECK
632 }
633
634 void
635 mld6_print(const u_char *bp)
636 {
637 struct mld6_hdr *mp = (struct mld6_hdr *)bp;
638 const u_char *ep;
639
640 /* 'ep' points to the end of available data. */
641 ep = snapend;
642
643 if ((u_char *)mp + sizeof(*mp) > ep)
644 return;
645
646 printf("max resp delay: %d ", ntohs(mp->mld6_maxdelay));
647 printf("addr: %s", ip6addr_string(&mp->mld6_addr));
648 }
649
650 static void
651 dnsname_print(const u_char *cp, const u_char *ep)
652 {
653 int i;
654
655 /* DNS name decoding - no decompression */
656 printf(", \"");
657 while (cp < ep) {
658 i = *cp++;
659 if (i) {
660 if (i > ep - cp) {
661 printf("???");
662 break;
663 }
664 while (i-- && cp < ep) {
665 safeputchar(*cp);
666 cp++;
667 }
668 if (cp + 1 < ep && *cp)
669 printf(".");
670 } else {
671 if (cp == ep) {
672 /* FQDN */
673 printf(".");
674 } else if (cp + 1 == ep && *cp == '\0') {
675 /* truncated */
676 } else {
677 /* invalid */
678 printf("???");
679 }
680 break;
681 }
682 }
683 printf("\"");
684 }
685
686 void
687 icmp6_nodeinfo_print(int icmp6len, const u_char *bp, const u_char *ep)
688 {
689 struct icmp6_nodeinfo *ni6;
690 struct icmp6_hdr *dp;
691 const u_char *cp;
692 int siz, i;
693 int needcomma;
694
695 dp = (struct icmp6_hdr *)bp;
696 ni6 = (struct icmp6_nodeinfo *)bp;
697 siz = ep - bp;
698
699 switch (ni6->ni_type) {
700 case ICMP6_NI_QUERY:
701 if (siz == sizeof(*dp) + 4) {
702 /* KAME who-are-you */
703 printf("icmp6: who-are-you request");
704 break;
705 }
706 printf("icmp6: node information query");
707
708 TCHECK2(*dp, sizeof(*ni6));
709 ni6 = (struct icmp6_nodeinfo *)dp;
710 printf(" ("); /*)*/
711 switch (ntohs(ni6->ni_qtype)) {
712 case NI_QTYPE_NOOP:
713 printf("noop");
714 break;
715 case NI_QTYPE_SUPTYPES:
716 printf("supported qtypes");
717 i = ntohs(ni6->ni_flags);
718 if (i)
719 printf(" [%s]", (i & 0x01) ? "C" : "");
720 break;
721 break;
722 case NI_QTYPE_FQDN:
723 printf("DNS name");
724 break;
725 case NI_QTYPE_NODEADDR:
726 printf("node addresses");
727 i = ni6->ni_flags;
728 if (!i)
729 break;
730 /* NI_NODEADDR_FLAG_TRUNCATE undefined for query */
731 printf(" [%s%s%s%s%s%s]",
732 (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
733 (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
734 (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
735 (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
736 (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
737 (i & NI_NODEADDR_FLAG_ALL) ? "A" : "");
738 break;
739 default:
740 printf("unknown");
741 break;
742 }
743
744 if (ni6->ni_qtype == NI_QTYPE_NOOP ||
745 ni6->ni_qtype == NI_QTYPE_SUPTYPES) {
746 if (siz != sizeof(*ni6))
747 if (vflag)
748 printf(", invalid len");
749 /*(*/
750 printf(")");
751 break;
752 }
753
754
755 /* XXX backward compat, icmp-name-lookup-03 */
756 if (siz == sizeof(*ni6)) {
757 printf(", 03 draft");
758 /*(*/
759 printf(")");
760 break;
761 }
762
763 switch (ni6->ni_code) {
764 case ICMP6_NI_SUBJ_IPV6:
765 if (!TTEST2(*dp,
766 sizeof(*ni6) + sizeof(struct in6_addr)))
767 break;
768 if (siz != sizeof(*ni6) + sizeof(struct in6_addr)) {
769 if (vflag)
770 printf(", invalid subject len");
771 break;
772 }
773 printf(", subject=%s",
774 getname6((const u_char *)(ni6 + 1)));
775 break;
776 case ICMP6_NI_SUBJ_FQDN:
777 printf(", subject=DNS name");
778 cp = (const u_char *)(ni6 + 1);
779 if (cp[0] == ep - cp - 1) {
780 /* icmp-name-lookup-03, pascal string */
781 if (vflag)
782 printf(", 03 draft");
783 cp++;
784 printf(", \"");
785 while (cp < ep) {
786 safeputchar(*cp);
787 cp++;
788 }
789 printf("\"");
790 } else
791 dnsname_print(cp, ep);
792 break;
793 case ICMP6_NI_SUBJ_IPV4:
794 if (!TTEST2(*dp, sizeof(*ni6) + sizeof(struct in_addr)))
795 break;
796 if (siz != sizeof(*ni6) + sizeof(struct in_addr)) {
797 if (vflag)
798 printf(", invalid subject len");
799 break;
800 }
801 printf(", subject=%s",
802 getname((const u_char *)(ni6 + 1)));
803 break;
804 default:
805 printf(", unknown subject");
806 break;
807 }
808
809 /*(*/
810 printf(")");
811 break;
812
813 case ICMP6_NI_REPLY:
814 if (icmp6len > siz) {
815 printf("[|icmp6: node information reply]");
816 break;
817 }
818
819 needcomma = 0;
820
821 ni6 = (struct icmp6_nodeinfo *)dp;
822 printf("icmp6: node information reply");
823 printf(" ("); /*)*/
824 switch (ni6->ni_code) {
825 case ICMP6_NI_SUCCESS:
826 if (vflag) {
827 printf("success");
828 needcomma++;
829 }
830 break;
831 case ICMP6_NI_REFUSED:
832 printf("refused");
833 needcomma++;
834 if (siz != sizeof(*ni6))
835 if (vflag)
836 printf(", invalid length");
837 break;
838 case ICMP6_NI_UNKNOWN:
839 printf("unknown");
840 needcomma++;
841 if (siz != sizeof(*ni6))
842 if (vflag)
843 printf(", invalid length");
844 break;
845 }
846
847 if (ni6->ni_code != ICMP6_NI_SUCCESS) {
848 /*(*/
849 printf(")");
850 break;
851 }
852
853 switch (ntohs(ni6->ni_qtype)) {
854 case NI_QTYPE_NOOP:
855 if (needcomma)
856 printf(", ");
857 printf("noop");
858 if (siz != sizeof(*ni6))
859 if (vflag)
860 printf(", invalid length");
861 break;
862 case NI_QTYPE_SUPTYPES:
863 if (needcomma)
864 printf(", ");
865 printf("supported qtypes");
866 i = ntohs(ni6->ni_flags);
867 if (i)
868 printf(" [%s]", (i & 0x01) ? "C" : "");
869 break;
870 case NI_QTYPE_FQDN:
871 if (needcomma)
872 printf(", ");
873 printf("DNS name");
874 cp = (const u_char *)(ni6 + 1) + 4;
875 if (cp[0] == ep - cp - 1) {
876 /* icmp-name-lookup-03, pascal string */
877 if (vflag)
878 printf(", 03 draft");
879 cp++;
880 printf(", \"");
881 while (cp < ep) {
882 safeputchar(*cp);
883 cp++;
884 }
885 printf("\"");
886 } else
887 dnsname_print(cp, ep);
888 if ((ntohs(ni6->ni_flags) & 0x01) != 0)
889 printf(" [TTL=%u]", *(u_int32_t *)(ni6 + 1));
890 break;
891 case NI_QTYPE_NODEADDR:
892 if (needcomma)
893 printf(", ");
894 printf("node addresses");
895 i = sizeof(*ni6);
896 while (i < siz) {
897 if (i + sizeof(struct in6_addr) + sizeof(int32_t) > siz)
898 break;
899 printf(" %s", getname6(bp + i));
900 i += sizeof(struct in6_addr);
901 printf("(%d)", (int32_t)ntohl(*(int32_t *)(bp + i)));
902 i += sizeof(int32_t);
903 }
904 i = ni6->ni_flags;
905 if (!i)
906 break;
907 printf(" [%s%s%s%s%s%s%s]",
908 (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
909 (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
910 (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
911 (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
912 (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
913 (i & NI_NODEADDR_FLAG_ALL) ? "A" : "",
914 (i & NI_NODEADDR_FLAG_TRUNCATE) ? "T" : "");
915 break;
916 default:
917 if (needcomma)
918 printf(", ");
919 printf("unknown");
920 break;
921 }
922
923 /*(*/
924 printf(")");
925 break;
926 }
927 return;
928
929 trunc:
930 fputs("[|icmp6]", stdout);
931 }
932
933 void
934 icmp6_rrenum_print(int icmp6len, const u_char *bp, const u_char *ep)
935 {
936 struct icmp6_router_renum *rr6;
937 struct icmp6_hdr *dp;
938 size_t siz;
939 const char *cp;
940 struct rr_pco_match *match;
941 struct rr_pco_use *use;
942 char hbuf[NI_MAXHOST];
943 int n;
944
945 dp = (struct icmp6_hdr *)bp;
946 rr6 = (struct icmp6_router_renum *)bp;
947 siz = ep - bp;
948 cp = (const char *)(rr6 + 1);
949
950 TCHECK(rr6->rr_reserved);
951 switch (rr6->rr_code) {
952 case ICMP6_ROUTER_RENUMBERING_COMMAND:
953 printf("router renum: command");
954 break;
955 case ICMP6_ROUTER_RENUMBERING_RESULT:
956 printf("router renum: result");
957 break;
958 case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET:
959 printf("router renum: sequence number reset");
960 break;
961 default:
962 printf("router renum: code-#%d", rr6->rr_code);
963 break;
964 }
965
966 printf(", seq=%u", (u_int32_t)ntohl(rr6->rr_seqnum));
967
968 if (vflag) {
969 #define F(x, y) ((rr6->rr_flags) & (x) ? (y) : "")
970 printf("["); /*]*/
971 if (rr6->rr_flags) {
972 printf("%s%s%s%s%s,", F(ICMP6_RR_FLAGS_TEST, "T"),
973 F(ICMP6_RR_FLAGS_REQRESULT, "R"),
974 F(ICMP6_RR_FLAGS_FORCEAPPLY, "A"),
975 F(ICMP6_RR_FLAGS_SPECSITE, "S"),
976 F(ICMP6_RR_FLAGS_PREVDONE, "P"));
977 }
978 printf("seg=%u,", rr6->rr_segnum);
979 printf("maxdelay=%u", rr6->rr_maxdelay);
980 if (rr6->rr_reserved)
981 printf("rsvd=0x%x", (u_int16_t)ntohs(rr6->rr_reserved));
982 /*[*/
983 printf("]");
984 #undef F
985 }
986
987 if (rr6->rr_code == ICMP6_ROUTER_RENUMBERING_COMMAND) {
988 match = (struct rr_pco_match *)cp;
989 cp = (const char *)(match + 1);
990
991 TCHECK(match->rpm_prefix);
992
993 if (vflag > 1)
994 printf("\n\t");
995 else
996 printf(" ");
997 printf("match("); /*)*/
998 switch (match->rpm_code) {
999 case RPM_PCO_ADD: printf("add"); break;
1000 case RPM_PCO_CHANGE: printf("change"); break;
1001 case RPM_PCO_SETGLOBAL: printf("setglobal"); break;
1002 default: printf("#%u", match->rpm_code); break;
1003 }
1004
1005 if (vflag) {
1006 printf(",ord=%u", match->rpm_ordinal);
1007 printf(",min=%u", match->rpm_minlen);
1008 printf(",max=%u", match->rpm_maxlen);
1009 }
1010 if (inet_ntop(AF_INET6, &match->rpm_prefix, hbuf, sizeof(hbuf)))
1011 printf(",%s/%u", hbuf, match->rpm_matchlen);
1012 else
1013 printf(",?/%u", match->rpm_matchlen);
1014 /*(*/
1015 printf(")");
1016
1017 n = match->rpm_len - 3;
1018 if (n % 4)
1019 goto trunc;
1020 n /= 4;
1021 while (n-- > 0) {
1022 use = (struct rr_pco_use *)cp;
1023 cp = (const char *)(use + 1);
1024
1025 TCHECK(use->rpu_prefix);
1026
1027 if (vflag > 1)
1028 printf("\n\t");
1029 else
1030 printf(" ");
1031 printf("use("); /*)*/
1032 if (use->rpu_flags) {
1033 #define F(x, y) ((use->rpu_flags) & (x) ? (y) : "")
1034 printf("%s%s,",
1035 F(ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME, "V"),
1036 F(ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME, "P"));
1037 #undef F
1038 }
1039 if (vflag) {
1040 printf("mask=0x%x,", use->rpu_ramask);
1041 printf("raflags=0x%x,", use->rpu_raflags);
1042 if (~use->rpu_vltime == 0)
1043 printf("vltime=infty,");
1044 else
1045 printf("vltime=%u,",
1046 (u_int32_t)ntohl(use->rpu_vltime));
1047 if (~use->rpu_pltime == 0)
1048 printf("pltime=infty,");
1049 else
1050 printf("pltime=%u,",
1051 (u_int32_t)ntohl(use->rpu_pltime));
1052 }
1053 if (inet_ntop(AF_INET6, &use->rpu_prefix, hbuf,
1054 sizeof(hbuf)))
1055 printf("%s/%u/%u", hbuf, use->rpu_uselen,
1056 use->rpu_keeplen);
1057 else
1058 printf("?/%u/%u", use->rpu_uselen,
1059 use->rpu_keeplen);
1060 /*(*/
1061 printf(")");
1062 }
1063 }
1064
1065 return;
1066
1067 trunc:
1068 fputs("[|icmp6]", stdout);
1069 }
1070
1071 #endif /* INET6 */