]> The Tcpdump Group git mirrors - tcpdump/blob - print-icmp6.c
put stripped-down version of ip6.h and icmp6.h into tcpdump tree.
[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.34 2000-10-07 05:53:10 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
47 #include "ip6.h"
48 #include "icmp6.h"
49
50 #include "interface.h"
51 #include "addrtoname.h"
52
53 #include "udp.h"
54 #include "ah.h"
55
56 void icmp6_opt_print(const u_char *, int);
57 void mld6_print(const u_char *);
58 static struct udphdr *get_upperlayer(u_char *, int *);
59 #ifdef HAVE_STRUCT_ICMP6_NODEINFO
60 static void dnsname_print(const u_char *, const u_char *);
61 void icmp6_nodeinfo_print(int, const u_char *, const u_char *);
62 #endif
63
64 #ifndef abs
65 #define abs(a) ((0 < (a)) ? (a) : -(a))
66 #endif
67
68 #ifndef HAVE_STRUCT_ICMP6_NODEINFO
69 struct icmp6_nodeinfo {
70 struct icmp6_hdr icmp6_ni_hdr;
71 u_int8_t icmp6_ni_nonce[8];
72 /* could be followed by reply data */
73 };
74
75 #define ni_type icmp6_ni_hdr.icmp6_type
76 #define ni_code icmp6_ni_hdr.icmp6_code
77 #define ni_cksum icmp6_ni_hdr.icmp6_cksum
78 #define ni_qtype icmp6_ni_hdr.icmp6_data16[0]
79 #define ni_flags icmp6_ni_hdr.icmp6_data16[1]
80 #endif
81
82 /* cope with past KAME typo - shipped with freebsd4[01] and openbsd27 */
83 #if defined(ICMP6_NI_SUCESS) && !defined(ICMP6_NI_SUCCESS)
84 #define ICMP6_NI_SUCCESS ICMP6_NI_SUCESS
85 #endif
86 #ifndef ICMP6_NI_SUCCESS
87 #define ICMP6_NI_SUCCESS 0 /* node information successful reply */
88 #define ICMP6_NI_REFUSED 1 /* node information request is refused */
89 #define ICMP6_NI_UNKNOWN 2 /* unknown Qtype */
90 #endif
91
92 #ifdef NI_QTYPE_NOOP
93 #define NI_QTYPE_NOOP 0 /* NOOP */
94 #define NI_QTYPE_SUPTYPES 1 /* Supported Qtypes */
95 #define NI_QTYPE_FQDN 2 /* FQDN */
96 #define NI_QTYPE_NODEADDR 3 /* Node Addresses. XXX: spec says 2, but it may be a typo... */
97 #endif
98
99 #ifndef ICMP6_NI_SUBJ_IPV6
100 #define ICMP6_NI_SUBJ_IPV6 0 /* Query Subject is an IPv6 address */
101 #define ICMP6_NI_SUBJ_FQDN 1 /* Query Subject is a Domain name */
102 #define ICMP6_NI_SUBJ_IPV4 2 /* Query Subject is an IPv4 address */
103 #endif
104
105 void
106 icmp6_print(register const u_char *bp, register const u_char *bp2)
107 {
108 const struct icmp6_hdr *dp;
109 register const struct ip6_hdr *ip;
110 register const char *str;
111 register const struct ip6_hdr *oip;
112 register const struct udphdr *ouh;
113 register int dport;
114 register const u_char *ep;
115 char buf[256];
116 int icmp6len, prot;
117
118 #if 0
119 #define TCHECK(var) if ((u_char *)&(var) > ep - sizeof(var)) goto trunc
120 #endif
121
122 dp = (struct icmp6_hdr *)bp;
123 ip = (struct ip6_hdr *)bp2;
124 oip = (struct ip6_hdr *)(dp + 1);
125 str = buf;
126 /* 'ep' points to the end of available data. */
127 ep = snapend;
128 if (ip->ip6_plen)
129 icmp6len = (ntohs(ip->ip6_plen) + sizeof(struct ip6_hdr) -
130 (bp - bp2));
131 else /* XXX: jumbo payload case... */
132 icmp6len = snapend - bp;
133
134 #if 0
135 (void)printf("%s > %s: ",
136 ip6addr_string(&ip->ip6_src),
137 ip6addr_string(&ip->ip6_dst));
138 #endif
139
140 TCHECK(dp->icmp6_code);
141 switch (dp->icmp6_type) {
142 case ICMP6_DST_UNREACH:
143 TCHECK(oip->ip6_dst);
144 switch (dp->icmp6_code) {
145 case ICMP6_DST_UNREACH_NOROUTE:
146 printf("icmp6: %s unreachable route",
147 ip6addr_string(&oip->ip6_dst));
148 break;
149 case ICMP6_DST_UNREACH_ADMIN:
150 printf("icmp6: %s unreachable prohibited",
151 ip6addr_string(&oip->ip6_dst));
152 break;
153 #ifdef ICMP6_DST_UNREACH_BEYONDSCOPE
154 case ICMP6_DST_UNREACH_BEYONDSCOPE:
155 #else
156 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
157 #endif
158 printf("icmp6: %s beyond scope of source address %s",
159 ip6addr_string(&oip->ip6_dst),
160 ip6addr_string(&oip->ip6_src));
161 break;
162 case ICMP6_DST_UNREACH_ADDR:
163 printf("icmp6: %s unreachable address",
164 ip6addr_string(&oip->ip6_dst));
165 break;
166 case ICMP6_DST_UNREACH_NOPORT:
167 if ((ouh = get_upperlayer((u_char *)oip, &prot))
168 == NULL)
169 goto trunc;
170
171 dport = ntohs(ouh->uh_dport);
172 switch (prot) {
173 case IPPROTO_TCP:
174 printf("icmp6: %s tcp port %s unreachable",
175 ip6addr_string(&oip->ip6_dst),
176 tcpport_string(dport));
177 break;
178 case IPPROTO_UDP:
179 printf("icmp6: %s udp port %s unreachable",
180 ip6addr_string(&oip->ip6_dst),
181 udpport_string(dport));
182 break;
183 default:
184 printf("icmp6: %s protocol %d port %d unreachable",
185 ip6addr_string(&oip->ip6_dst),
186 oip->ip6_nxt, dport);
187 break;
188 }
189 break;
190 default:
191 printf("icmp6: %s unreachable code-#%d",
192 ip6addr_string(&oip->ip6_dst),
193 dp->icmp6_code);
194 break;
195 }
196 break;
197 case ICMP6_PACKET_TOO_BIG:
198 TCHECK(dp->icmp6_mtu);
199 printf("icmp6: too big %u\n", (u_int32_t)ntohl(dp->icmp6_mtu));
200 break;
201 case ICMP6_TIME_EXCEEDED:
202 TCHECK(oip->ip6_dst);
203 switch (dp->icmp6_code) {
204 case ICMP6_TIME_EXCEED_TRANSIT:
205 printf("icmp6: time exceeded in-transit for %s",
206 ip6addr_string(&oip->ip6_dst));
207 break;
208 case ICMP6_TIME_EXCEED_REASSEMBLY:
209 printf("icmp6: ip6 reassembly time exceeded");
210 break;
211 default:
212 printf("icmp6: time exceeded code-#%d",
213 dp->icmp6_code);
214 break;
215 }
216 break;
217 case ICMP6_PARAM_PROB:
218 TCHECK(oip->ip6_dst);
219 switch (dp->icmp6_code) {
220 case ICMP6_PARAMPROB_HEADER:
221 printf("icmp6: parameter problem errorneous - octet %u\n",
222 (u_int32_t)ntohl(dp->icmp6_pptr));
223 break;
224 case ICMP6_PARAMPROB_NEXTHEADER:
225 printf("icmp6: parameter problem next header - octet %u\n",
226 (u_int32_t)ntohl(dp->icmp6_pptr));
227 break;
228 case ICMP6_PARAMPROB_OPTION:
229 printf("icmp6: parameter problem option - octet %u\n",
230 (u_int32_t)ntohl(dp->icmp6_pptr));
231 break;
232 default:
233 printf("icmp6: parameter problem code-#%d",
234 dp->icmp6_code);
235 break;
236 }
237 break;
238 case ICMP6_ECHO_REQUEST:
239 printf("icmp6: echo request");
240 break;
241 case ICMP6_ECHO_REPLY:
242 printf("icmp6: echo reply");
243 break;
244 #if defined(HAVE_STRUCT_MLD6_HDR) || defined(HAVE_STRUCT_ICMP6_MLD)
245 case ICMP6_MEMBERSHIP_QUERY:
246 printf("icmp6: multicast listener query ");
247 mld6_print((const u_char *)dp);
248 break;
249 case ICMP6_MEMBERSHIP_REPORT:
250 printf("icmp6: multicast listener report ");
251 mld6_print((const u_char *)dp);
252 break;
253 case ICMP6_MEMBERSHIP_REDUCTION:
254 printf("icmp6: multicast listener done ");
255 mld6_print((const u_char *)dp);
256 break;
257 #endif
258 case ND_ROUTER_SOLICIT:
259 printf("icmp6: router solicitation ");
260 if (vflag) {
261 #define RTSOLLEN 8
262 icmp6_opt_print((const u_char *)dp + RTSOLLEN,
263 icmp6len - RTSOLLEN);
264 }
265 break;
266 case ND_ROUTER_ADVERT:
267 printf("icmp6: router advertisement");
268 if (vflag) {
269 struct nd_router_advert *p;
270
271 p = (struct nd_router_advert *)dp;
272 TCHECK(p->nd_ra_retransmit);
273 printf("(chlim=%d, ", (int)p->nd_ra_curhoplimit);
274 if (p->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED)
275 printf("M");
276 if (p->nd_ra_flags_reserved & ND_RA_FLAG_OTHER)
277 printf("O");
278 #ifndef ND_RA_FLAG_HA
279 #define ND_RA_FLAG_HA 0x20
280 #endif
281 if (p->nd_ra_flags_reserved & ND_RA_FLAG_HA)
282 printf("H");
283 if (p->nd_ra_flags_reserved != 0)
284 printf(" ");
285 printf("router_ltime=%d, ", ntohs(p->nd_ra_router_lifetime));
286 printf("reachable_time=%u, ",
287 (u_int32_t)ntohl(p->nd_ra_reachable));
288 printf("retrans_time=%u)",
289 (u_int32_t)ntohl(p->nd_ra_retransmit));
290 #define RTADVLEN 16
291 icmp6_opt_print((const u_char *)dp + RTADVLEN,
292 icmp6len - RTADVLEN);
293 }
294 break;
295 case ND_NEIGHBOR_SOLICIT:
296 {
297 struct nd_neighbor_solicit *p;
298 p = (struct nd_neighbor_solicit *)dp;
299 TCHECK(p->nd_ns_target);
300 printf("icmp6: neighbor sol: who has %s",
301 ip6addr_string(&p->nd_ns_target));
302 if (vflag) {
303 #define NDSOLLEN 24
304 icmp6_opt_print((const u_char *)dp + NDSOLLEN,
305 icmp6len - NDSOLLEN);
306 }
307 }
308 break;
309 case ND_NEIGHBOR_ADVERT:
310 {
311 struct nd_neighbor_advert *p;
312
313 p = (struct nd_neighbor_advert *)dp;
314 TCHECK(p->nd_na_target);
315 printf("icmp6: neighbor adv: tgt is %s",
316 ip6addr_string(&p->nd_na_target));
317 if (vflag) {
318 #define ND_NA_FLAG_ALL \
319 (ND_NA_FLAG_ROUTER|ND_NA_FLAG_SOLICITED|ND_NA_FLAG_OVERRIDE)
320 /* we don't need ntohl() here. see advanced-api-04. */
321 if (p->nd_na_flags_reserved & ND_NA_FLAG_ALL) {
322 #undef ND_NA_FLAG_ALL
323 u_int32_t flags;
324
325 flags = p->nd_na_flags_reserved;
326 printf("(");
327 if (flags & ND_NA_FLAG_ROUTER)
328 printf("R");
329 if (flags & ND_NA_FLAG_SOLICITED)
330 printf("S");
331 if (flags & ND_NA_FLAG_OVERRIDE)
332 printf("O");
333 printf(")");
334 }
335 #define NDADVLEN 24
336 icmp6_opt_print((const u_char *)dp + NDADVLEN,
337 icmp6len - NDADVLEN);
338 #undef NDADVLEN
339 }
340 }
341 break;
342 case ND_REDIRECT:
343 #define RDR(i) ((struct nd_redirect *)(i))
344 TCHECK(RDR(dp)->nd_rd_dst);
345 printf("icmp6: redirect %s",
346 getname6((const u_char *)&RDR(dp)->nd_rd_dst));
347 printf(" to %s",
348 getname6((const u_char*)&RDR(dp)->nd_rd_target));
349 #define REDIRECTLEN 40
350 if (vflag) {
351 icmp6_opt_print((const u_char *)dp + REDIRECTLEN,
352 icmp6len - REDIRECTLEN);
353 }
354 break;
355 #undef REDIRECTLEN
356 #undef RDR
357 #ifndef ICMP6_ROUTER_RENUMBERING
358 #define ICMP6_ROUTER_RENUMBERING 138 /* router renumbering */
359 #endif
360 case ICMP6_ROUTER_RENUMBERING:
361 switch (dp->icmp6_code) {
362 #ifndef ICMP6_ROUTER_RENUMBERING_COMMAND
363 #define ICMP6_ROUTER_RENUMBERING_COMMAND 0 /* rr command */
364 #endif
365 case ICMP6_ROUTER_RENUMBERING_COMMAND:
366 printf("icmp6: router renum command");
367 break;
368 #ifndef ICMP6_ROUTER_RENUMBERING_RESULT
369 #define ICMP6_ROUTER_RENUMBERING_RESULT 1 /* rr result */
370 #endif
371 case ICMP6_ROUTER_RENUMBERING_RESULT:
372 printf("icmp6: router renum result");
373 break;
374 default:
375 printf("icmp6: router renum code-#%d", dp->icmp6_code);
376 break;
377 }
378 break;
379 #ifdef HAVE_STRUCT_ICMP6_NODEINFO
380 #ifndef ICMP6_NI_QUERY
381 #define ICMP6_NI_QUERY 139
382 #endif
383 case ICMP6_NI_QUERY:
384 icmp6_nodeinfo_print(icmp6len, bp, ep);
385 break;
386 #ifndef ICMP6_NI_REPLY
387 #define ICMP6_NI_REPLY 140
388 #endif
389 case ICMP6_NI_REPLY:
390 icmp6_nodeinfo_print(icmp6len, bp, ep);
391 break;
392 #endif
393 default:
394 printf("icmp6: type-#%d", dp->icmp6_type);
395 break;
396 }
397 return;
398 trunc:
399 fputs("[|icmp6]", stdout);
400 #if 0
401 #undef TCHECK
402 #endif
403 }
404
405 static struct udphdr *
406 get_upperlayer(register u_char *bp, int *prot)
407 {
408 register 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(register const u_char *bp, int resid)
480 {
481 register const struct nd_opt_hdr *op;
482 register const struct nd_opt_hdr *opl; /* why there's no struct? */
483 register const struct nd_opt_prefix_info *opp;
484 register const struct icmp6_opts_redirect *opr;
485 register const struct nd_opt_mtu *opm;
486 register const u_char *ep;
487 int opts_len;
488 #if 0
489 register const struct ip6_hdr *ip;
490 register const char *str;
491 register const struct ip6_hdr *oip;
492 register const struct udphdr *ouh;
493 register int hlen, dport;
494 char buf[256];
495 #endif
496
497 #define ECHECK(var) if ((u_char *)&(var) > ep - sizeof(var)) return
498
499 op = (struct nd_opt_hdr *)bp;
500 #if 0
501 ip = (struct ip6_hdr *)bp2;
502 oip = &dp->icmp6_ip6;
503 str = buf;
504 #endif
505 /* 'ep' points to the end of available data. */
506 ep = snapend;
507
508 ECHECK(op->nd_opt_len);
509 if (resid <= 0)
510 return;
511 switch (op->nd_opt_type) {
512 case ND_OPT_SOURCE_LINKADDR:
513 opl = (struct nd_opt_hdr *)op;
514 #if 1
515 if ((u_char *)opl + (opl->nd_opt_len << 3) > ep)
516 goto trunc;
517 #else
518 TCHECK((u_char *)opl + (opl->nd_opt_len << 3) - 1);
519 #endif
520 printf("(src lladdr: %s", /*)*/
521 etheraddr_string((u_char *)(opl + 1)));
522 if (opl->nd_opt_len != 1)
523 printf("!");
524 /*(*/
525 printf(")");
526 icmp6_opt_print((const u_char *)op + (op->nd_opt_len << 3),
527 resid - (op->nd_opt_len << 3));
528 break;
529 case ND_OPT_TARGET_LINKADDR:
530 opl = (struct nd_opt_hdr *)op;
531 #if 1
532 if ((u_char *)opl + (opl->nd_opt_len << 3) > ep)
533 goto trunc;
534 #else
535 TCHECK((u_char *)opl + (opl->nd_opt_len << 3) - 1);
536 #endif
537 printf("(tgt lladdr: %s", /*)*/
538 etheraddr_string((u_char *)(opl + 1)));
539 if (opl->nd_opt_len != 1)
540 printf("!");
541 /*(*/
542 printf(")");
543 icmp6_opt_print((const u_char *)op + (op->nd_opt_len << 3),
544 resid - (op->nd_opt_len << 3));
545 break;
546 case ND_OPT_PREFIX_INFORMATION:
547 opp = (struct nd_opt_prefix_info *)op;
548 TCHECK(opp->nd_opt_pi_prefix);
549 printf("(prefix info: "); /*)*/
550 if (opp->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
551 printf("L");
552 if (opp->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO)
553 printf("A");
554 if (opp->nd_opt_pi_flags_reserved)
555 printf(" ");
556 printf("valid_ltime=");
557 if ((u_int32_t)ntohl(opp->nd_opt_pi_valid_time) == ~0U)
558 printf("infinity");
559 else {
560 printf("%u", (u_int32_t)ntohl(opp->nd_opt_pi_valid_time));
561 }
562 printf(", ");
563 printf("preffered_ltime=");
564 if ((u_int32_t)ntohl(opp->nd_opt_pi_preferred_time) == ~0U)
565 printf("infinity");
566 else {
567 printf("%u", (u_int32_t)ntohl(opp->nd_opt_pi_preferred_time));
568 }
569 printf(", ");
570 printf("prefix=%s/%d", ip6addr_string(&opp->nd_opt_pi_prefix),
571 opp->nd_opt_pi_prefix_len);
572 if (opp->nd_opt_pi_len != 4)
573 printf("!");
574 /*(*/
575 printf(")");
576 icmp6_opt_print((const u_char *)op + (op->nd_opt_len << 3),
577 resid - (op->nd_opt_len << 3));
578 break;
579 case ND_OPT_REDIRECTED_HEADER:
580 opr = (struct icmp6_opts_redirect *)op;
581 printf("(redirect)");
582 /* xxx */
583 icmp6_opt_print((const u_char *)op + (op->nd_opt_len << 3),
584 resid - (op->nd_opt_len << 3));
585 break;
586 case ND_OPT_MTU:
587 opm = (struct nd_opt_mtu *)op;
588 TCHECK(opm->nd_opt_mtu_mtu);
589 printf("(mtu: "); /*)*/
590 printf("mtu=%u", (u_int32_t)ntohl(opm->nd_opt_mtu_mtu));
591 if (opm->nd_opt_mtu_len != 1)
592 printf("!");
593 printf(")");
594 icmp6_opt_print((const u_char *)op + (op->nd_opt_len << 3),
595 resid - (op->nd_opt_len << 3));
596 break;
597 default:
598 opts_len = op->nd_opt_len;
599 printf("(unknwon opt_type=%d, opt_len=%d)",
600 op->nd_opt_type, opts_len);
601 if (opts_len == 0)
602 opts_len = 1; /* XXX */
603 icmp6_opt_print((const u_char *)op + (opts_len << 3),
604 resid - (opts_len << 3));
605 break;
606 }
607 return;
608 trunc:
609 fputs("[ndp opt]", stdout);
610 return;
611 #undef ECHECK
612 }
613
614 #if defined(HAVE_STRUCT_MLD6_HDR)
615 void
616 mld6_print(register const u_char *bp)
617 {
618 register struct mld6_hdr *mp = (struct mld6_hdr *)bp;
619 register const u_char *ep;
620
621 /* 'ep' points to the end of available data. */
622 ep = snapend;
623
624 if ((u_char *)mp + sizeof(*mp) > ep)
625 return;
626
627 printf("max resp delay: %d ", ntohs(mp->mld6_maxdelay));
628 printf("addr: %s", ip6addr_string(&mp->mld6_addr));
629 }
630
631 #elif defined(HAVE_STRUCT_ICMP6_MLD)
632
633 void
634 mld6_print(register const u_char *bp)
635 {
636 register struct icmp6_mld *mp = (struct icmp6_mld *)bp;
637 register const u_char *ep;
638
639 /* 'ep' points to the end of available data. */
640 ep = snapend;
641
642 if ((u_char *)mp + sizeof(*mp) > ep)
643 return;
644
645 printf("max resp delay: %d ", ntohs(mp->icmp6m_hdr.icmp6_maxdelay));
646 printf("addr: %s", ip6addr_string(&mp->icmp6m_group));
647 }
648
649 #endif
650
651 #ifdef HAVE_STRUCT_ICMP6_NODEINFO
652 static void
653 dnsname_print(const u_char *cp, const u_char *ep)
654 {
655 int i;
656
657 /* DNS name decoding - no decompression */
658 printf(", \"");
659 while (cp < ep) {
660 i = *cp++;
661 if (i) {
662 if (i > ep - cp) {
663 printf("???");
664 break;
665 }
666 while (i-- && cp < ep) {
667 safeputchar(*cp);
668 cp++;
669 }
670 if (cp + 1 < ep && *cp)
671 printf(".");
672 } else {
673 if (cp == ep) {
674 /* FQDN */
675 printf(".");
676 } else if (cp + 1 == ep && *cp == '\0') {
677 /* truncated */
678 } else {
679 /* invalid */
680 printf("???");
681 }
682 break;
683 }
684 }
685 printf("\"");
686 }
687
688 void
689 icmp6_nodeinfo_print(int icmp6len, const u_char *bp, const u_char *ep)
690 {
691 struct icmp6_nodeinfo *ni6;
692 struct icmp6_hdr *dp;
693 const u_char *cp;
694 int siz, i;
695 int needcomma;
696
697 dp = (struct icmp6_hdr *)bp;
698 ni6 = (struct icmp6_nodeinfo *)bp;
699 siz = ep - bp;
700
701 switch (ni6->ni_type) {
702 case ICMP6_NI_QUERY:
703 if (siz == sizeof(*dp) + 4) {
704 /* KAME who-are-you */
705 printf("icmp6: who-are-you request");
706 break;
707 }
708 printf("icmp6: node information query");
709
710 TCHECK2(*dp, sizeof(*ni6));
711 ni6 = (struct icmp6_nodeinfo *)dp;
712 printf(" ("); /*)*/
713 switch (ntohs(ni6->ni_qtype)) {
714 case NI_QTYPE_NOOP:
715 printf("noop");
716 break;
717 case NI_QTYPE_SUPTYPES:
718 printf("supported qtypes");
719 i = ntohs(ni6->ni_flags);
720 if (i)
721 printf(" [%s]", (i & 0x01) ? "C" : "");
722 break;
723 break;
724 case NI_QTYPE_FQDN:
725 printf("DNS name");
726 break;
727 case NI_QTYPE_NODEADDR:
728 printf("node addresses");
729 i = ni6->ni_flags;
730 if (!i)
731 break;
732 /* NI_NODEADDR_FLAG_TRUNCATE undefined for query */
733 printf(" [%s%s%s%s%s%s]",
734 (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
735 (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
736 (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
737 (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
738 (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
739 (i & NI_NODEADDR_FLAG_ALL) ? "A" : "");
740 break;
741 default:
742 printf("unknown");
743 break;
744 }
745
746 if (ni6->ni_qtype == NI_QTYPE_NOOP ||
747 ni6->ni_qtype == NI_QTYPE_SUPTYPES) {
748 if (siz != sizeof(*ni6))
749 if (vflag)
750 printf(", invalid len");
751 /*(*/
752 printf(")");
753 break;
754 }
755
756
757 /* XXX backward compat, icmp-name-lookup-03 */
758 if (siz == sizeof(*ni6)) {
759 printf(", 03 draft");
760 /*(*/
761 printf(")");
762 break;
763 }
764
765 switch (ni6->ni_code) {
766 case ICMP6_NI_SUBJ_IPV6:
767 if (!TTEST2(*dp,
768 sizeof(*ni6) + sizeof(struct in6_addr)))
769 break;
770 if (siz != sizeof(*ni6) + sizeof(struct in6_addr)) {
771 if (vflag)
772 printf(", invalid subject len");
773 break;
774 }
775 printf(", subject=%s",
776 getname6((const u_char *)(ni6 + 1)));
777 break;
778 case ICMP6_NI_SUBJ_FQDN:
779 printf(", subject=DNS name");
780 cp = (const u_char *)(ni6 + 1);
781 if (cp[0] == ep - cp - 1) {
782 /* icmp-name-lookup-03, pascal string */
783 if (vflag)
784 printf(", 03 draft");
785 cp++;
786 printf(", \"");
787 while (cp < ep) {
788 safeputchar(*cp);
789 cp++;
790 }
791 printf("\"");
792 } else
793 dnsname_print(cp, ep);
794 break;
795 case ICMP6_NI_SUBJ_IPV4:
796 if (!TTEST2(*dp, sizeof(*ni6) + sizeof(struct in_addr)))
797 break;
798 if (siz != sizeof(*ni6) + sizeof(struct in_addr)) {
799 if (vflag)
800 printf(", invalid subject len");
801 break;
802 }
803 printf(", subject=%s",
804 getname((const u_char *)(ni6 + 1)));
805 break;
806 default:
807 printf(", unknown subject");
808 break;
809 }
810
811 /*(*/
812 printf(")");
813 break;
814
815 case ICMP6_NI_REPLY:
816 if (icmp6len > siz) {
817 printf("[|icmp6: node information reply]");
818 break;
819 }
820
821 needcomma = 0;
822
823 ni6 = (struct icmp6_nodeinfo *)dp;
824 printf("icmp6: node information reply");
825 printf(" ("); /*)*/
826 switch (ni6->ni_code) {
827 case ICMP6_NI_SUCCESS:
828 if (vflag) {
829 printf("success");
830 needcomma++;
831 }
832 break;
833 case ICMP6_NI_REFUSED:
834 printf("refused");
835 needcomma++;
836 if (siz != sizeof(*ni6))
837 if (vflag)
838 printf(", invalid length");
839 break;
840 case ICMP6_NI_UNKNOWN:
841 printf("unknown");
842 needcomma++;
843 if (siz != sizeof(*ni6))
844 if (vflag)
845 printf(", invalid length");
846 break;
847 }
848
849 if (ni6->ni_code != ICMP6_NI_SUCCESS) {
850 /*(*/
851 printf(")");
852 break;
853 }
854
855 switch (ntohs(ni6->ni_qtype)) {
856 case NI_QTYPE_NOOP:
857 if (needcomma)
858 printf(", ");
859 printf("noop");
860 if (siz != sizeof(*ni6))
861 if (vflag)
862 printf(", invalid length");
863 break;
864 case NI_QTYPE_SUPTYPES:
865 if (needcomma)
866 printf(", ");
867 printf("supported qtypes");
868 i = ntohs(ni6->ni_flags);
869 if (i)
870 printf(" [%s]", (i & 0x01) ? "C" : "");
871 break;
872 case NI_QTYPE_FQDN:
873 if (needcomma)
874 printf(", ");
875 printf("DNS name");
876 cp = (const u_char *)(ni6 + 1) + 4;
877 if (cp[0] == ep - cp - 1) {
878 /* icmp-name-lookup-03, pascal string */
879 if (vflag)
880 printf(", 03 draft");
881 cp++;
882 printf(", \"");
883 while (cp < ep) {
884 safeputchar(*cp);
885 cp++;
886 }
887 printf("\"");
888 } else
889 dnsname_print(cp, ep);
890 if ((ntohs(ni6->ni_flags) & 0x01) != 0)
891 printf(" [TTL=%u]", *(u_int32_t *)(ni6 + 1));
892 break;
893 case NI_QTYPE_NODEADDR:
894 if (needcomma)
895 printf(", ");
896 printf("node addresses");
897 i = sizeof(*ni6);
898 while (i < siz) {
899 if (i + sizeof(struct in6_addr) + sizeof(int32_t) > siz)
900 break;
901 printf(" %s", getname6(bp + i));
902 i += sizeof(struct in6_addr);
903 printf("(%d)", ntohl(*(int32_t *)(bp + i)));
904 i += sizeof(int32_t);
905 }
906 i = ni6->ni_flags;
907 if (!i)
908 break;
909 printf(" [%s%s%s%s%s%s%s]",
910 (i & NI_NODEADDR_FLAG_ANYCAST) ? "a" : "",
911 (i & NI_NODEADDR_FLAG_GLOBAL) ? "G" : "",
912 (i & NI_NODEADDR_FLAG_SITELOCAL) ? "S" : "",
913 (i & NI_NODEADDR_FLAG_LINKLOCAL) ? "L" : "",
914 (i & NI_NODEADDR_FLAG_COMPAT) ? "C" : "",
915 (i & NI_NODEADDR_FLAG_ALL) ? "A" : "",
916 (i & NI_NODEADDR_FLAG_TRUNCATE) ? "T" : "");
917 break;
918 default:
919 if (needcomma)
920 printf(", ");
921 printf("unknown");
922 break;
923 }
924
925 /*(*/
926 printf(")");
927 break;
928 }
929 return;
930
931 trunc:
932 fputs("[|icmp6]", stdout);
933 }
934 #endif
935
936 #endif /* INET6 */