ndp.c revision 1.42 1 /* $NetBSD: ndp.c,v 1.42 2013/12/17 20:26:46 martin Exp $ */
2 /* $KAME: ndp.c,v 1.121 2005/07/13 11:30:13 keiichi Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32 /*
33 * Copyright (c) 1984, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Sun Microsystems, Inc.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 /*
65 * Based on:
66 * "@(#) Copyright (c) 1984, 1993\n\
67 * The Regents of the University of California. All rights reserved.\n";
68 *
69 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
70 */
71
72 /*
73 * ndp - display, set, delete and flush neighbor cache
74 */
75
76
77 #include <sys/param.h>
78 #include <sys/file.h>
79 #include <sys/ioctl.h>
80 #include <sys/socket.h>
81 #include <sys/sysctl.h>
82 #include <sys/time.h>
83
84 #include <net/if.h>
85 #include <net/if_dl.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88
89 #include <netinet/in.h>
90
91 #include <netinet/icmp6.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/nd6.h>
94
95 #include <arpa/inet.h>
96
97 #include <netdb.h>
98 #include <errno.h>
99 #include <nlist.h>
100 #include <stdio.h>
101 #include <string.h>
102 #include <paths.h>
103 #include <err.h>
104 #include <stdlib.h>
105 #include <fcntl.h>
106 #include <unistd.h>
107 #include "gmt2local.h"
108
109 static pid_t pid;
110 static int nflag;
111 static int tflag;
112 static int32_t thiszone; /* time difference with gmt */
113 static int my_s = -1;
114 static unsigned int repeat = 0;
115
116
117 static char host_buf[NI_MAXHOST]; /* getnameinfo() */
118 static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
119
120 static void getsocket(void);
121 static int set(int, char **);
122 static void get(char *);
123 static int delete(char *);
124 static void dump(struct in6_addr *, int);
125 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, unsigned int, int);
126 static char *ether_str(struct sockaddr_dl *);
127 static int ndp_ether_aton(char *, u_char *);
128 __dead static void usage(void);
129 static int rtmsg(int);
130 static void ifinfo(char *, int, char **);
131 static void rtrlist(void);
132 static void plist(void);
133 static void pfx_flush(void);
134 static void rtrlist(void);
135 static void rtr_flush(void);
136 static void harmonize_rtr(void);
137 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
138 static void getdefif(void);
139 static void setdefif(char *);
140 #endif
141 static const char *sec2str(time_t);
142 static char *ether_str(struct sockaddr_dl *);
143 static void ts_print(const struct timeval *);
144
145 #ifdef ICMPV6CTL_ND6_DRLIST
146 static const char *rtpref_str[] = {
147 "medium", /* 00 */
148 "high", /* 01 */
149 "rsv", /* 10 */
150 "low" /* 11 */
151 };
152 #endif
153
154 static int mode = 0;
155 static char *arg = NULL;
156
157 int
158 main(int argc, char **argv)
159 {
160 int ch;
161
162 pid = getpid();
163 thiszone = gmt2local(0L);
164 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
165 switch (ch) {
166 case 'a':
167 case 'c':
168 case 'p':
169 case 'r':
170 case 'H':
171 case 'P':
172 case 'R':
173 case 's':
174 case 'I':
175 if (mode) {
176 usage();
177 /*NOTREACHED*/
178 }
179 mode = ch;
180 arg = NULL;
181 break;
182 case 'd':
183 case 'f':
184 case 'i' :
185 if (mode) {
186 usage();
187 /*NOTREACHED*/
188 }
189 mode = ch;
190 arg = optarg;
191 break;
192 case 'n':
193 nflag = 1;
194 break;
195 case 't':
196 tflag = 1;
197 break;
198 case 'A':
199 if (mode) {
200 usage();
201 /*NOTREACHED*/
202 }
203 mode = 'a';
204 repeat = atoi(optarg);
205 break;
206 default:
207 usage();
208 }
209
210 argc -= optind;
211 argv += optind;
212
213 switch (mode) {
214 case 'a':
215 case 'c':
216 if (argc != 0) {
217 usage();
218 /*NOTREACHED*/
219 }
220 dump(0, mode == 'c');
221 break;
222 case 'd':
223 if (argc != 0) {
224 usage();
225 /*NOTREACHED*/
226 }
227 (void)delete(arg);
228 break;
229 case 'I':
230 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
231 if (argc > 1) {
232 usage();
233 /*NOTREACHED*/
234 } else if (argc == 1) {
235 if (strcmp(*argv, "delete") == 0 ||
236 if_nametoindex(*argv))
237 setdefif(*argv);
238 else
239 errx(1, "invalid interface %s", *argv);
240 }
241 getdefif(); /* always call it to print the result */
242 break;
243 #else
244 errx(1, "not supported yet");
245 /*NOTREACHED*/
246 #endif
247 case 'p':
248 if (argc != 0) {
249 usage();
250 /*NOTREACHED*/
251 }
252 plist();
253 break;
254 case 'i':
255 ifinfo(arg, argc, argv);
256 break;
257 case 'r':
258 if (argc != 0) {
259 usage();
260 /*NOTREACHED*/
261 }
262 rtrlist();
263 break;
264 case 's':
265 if (argc < 2 || argc > 4)
266 usage();
267 return(set(argc, argv) ? 1 : 0);
268 case 'H':
269 if (argc != 0) {
270 usage();
271 /*NOTREACHED*/
272 }
273 harmonize_rtr();
274 break;
275 case 'P':
276 if (argc != 0) {
277 usage();
278 /*NOTREACHED*/
279 }
280 pfx_flush();
281 break;
282 case 'R':
283 if (argc != 0) {
284 usage();
285 /*NOTREACHED*/
286 }
287 rtr_flush();
288 break;
289 case 0:
290 if (argc != 1) {
291 usage();
292 /*NOTREACHED*/
293 }
294 get(argv[0]);
295 break;
296 }
297 return(0);
298 }
299
300 static void
301 getsocket(void)
302 {
303 if (my_s < 0) {
304 my_s = socket(PF_ROUTE, SOCK_RAW, 0);
305 if (my_s < 0)
306 err(1, "socket");
307 }
308 }
309
310 #ifdef notdef
311 static struct sockaddr_in6 so_mask = {
312 .sin6_len = sizeof(so_mask),
313 .sin6_family = AF_INET6
314 };
315 #endif
316 static struct sockaddr_in6 blank_sin = {
317 .sin6_len = sizeof(blank_sin),
318 .sin6_family = AF_INET6
319 };
320 static struct sockaddr_in6 sin_m;
321 static struct sockaddr_dl blank_sdl = {
322 .sdl_len = sizeof(blank_sdl),
323 .sdl_family = AF_LINK,
324 };
325 static struct sockaddr_dl sdl_m;
326 static int expire_time, flags, found_entry;
327 static struct {
328 struct rt_msghdr m_rtm;
329 char m_space[512];
330 } m_rtmsg;
331
332 /*
333 * Set an individual neighbor cache entry
334 */
335 static int
336 set(int argc, char **argv)
337 {
338 register struct sockaddr_in6 *mysin = &sin_m;
339 register struct sockaddr_dl *sdl;
340 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
341 struct addrinfo hints, *res;
342 int gai_error;
343 u_char *ea;
344 char *host = argv[0], *eaddr = argv[1];
345
346 getsocket();
347 argc -= 2;
348 argv += 2;
349 sdl_m = blank_sdl;
350 sin_m = blank_sin;
351
352 (void)memset(&hints, 0, sizeof(hints));
353 hints.ai_family = AF_INET6;
354 gai_error = getaddrinfo(host, NULL, &hints, &res);
355 if (gai_error) {
356 warnx("%s: %s\n", host, gai_strerror(gai_error));
357 return 1;
358 }
359 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
360 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
361 ea = (u_char *)LLADDR(&sdl_m);
362 if (ndp_ether_aton(eaddr, ea) == 0)
363 sdl_m.sdl_alen = 6;
364 flags = expire_time = 0;
365 while (argc-- > 0) {
366 if (strncmp(argv[0], "temp", 4) == 0) {
367 struct timeval tim;
368
369 (void)gettimeofday(&tim, 0);
370 expire_time = tim.tv_sec + 20 * 60;
371 } else if (strncmp(argv[0], "proxy", 5) == 0)
372 flags |= RTF_ANNOUNCE;
373 argv++;
374 }
375 if (rtmsg(RTM_GET) < 0) {
376 errx(1, "RTM_GET(%s) failed", host);
377 /* NOTREACHED */
378 }
379 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
380 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) + (char *)(void *)mysin);
381 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) {
382 if (sdl->sdl_family == AF_LINK &&
383 (rtm->rtm_flags & RTF_LLINFO) &&
384 !(rtm->rtm_flags & RTF_GATEWAY)) {
385 switch (sdl->sdl_type) {
386 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
387 case IFT_ISO88024: case IFT_ISO88025:
388 goto overwrite;
389 }
390 }
391 /*
392 * IPv4 arp command retries with sin_other = SIN_PROXY here.
393 */
394 (void)fprintf(stderr, "set: cannot configure a new entry\n");
395 return 1;
396 }
397
398 overwrite:
399 if (sdl->sdl_family != AF_LINK) {
400 warnx("cannot intuit interface index and type for %s", host);
401 return (1);
402 }
403 sdl_m.sdl_type = sdl->sdl_type;
404 sdl_m.sdl_index = sdl->sdl_index;
405 return (rtmsg(RTM_ADD));
406 }
407
408 /*
409 * Display an individual neighbor cache entry
410 */
411 static void
412 get(char *host)
413 {
414 struct sockaddr_in6 *mysin = &sin_m;
415 struct addrinfo hints, *res;
416 int gai_error;
417
418 sin_m = blank_sin;
419 (void)memset(&hints, 0, sizeof(hints));
420 hints.ai_family = AF_INET6;
421 gai_error = getaddrinfo(host, NULL, &hints, &res);
422 if (gai_error) {
423 warnx("%s: %s\n", host, gai_strerror(gai_error));
424 return;
425 }
426 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
427 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
428 dump(&mysin->sin6_addr, 0);
429 if (found_entry == 0) {
430 (void)getnameinfo((struct sockaddr *)(void *)mysin,
431 (socklen_t)mysin->sin6_len,
432 host_buf, sizeof(host_buf), NULL ,0,
433 (nflag ? NI_NUMERICHOST : 0));
434 errx(1, "%s (%s) -- no entry", host, host_buf);
435 }
436 }
437
438 /*
439 * Delete a neighbor cache entry
440 */
441 static int
442 delete(char *host)
443 {
444 struct sockaddr_in6 *mysin = &sin_m;
445 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
446 struct sockaddr_dl *sdl;
447 struct addrinfo hints, *res;
448 int gai_error;
449
450 getsocket();
451 sin_m = blank_sin;
452
453 bzero(&hints, sizeof(hints));
454 hints.ai_family = AF_INET6;
455 gai_error = getaddrinfo(host, NULL, &hints, &res);
456 if (gai_error) {
457 warnx("%s: %s\n", host, gai_strerror(gai_error));
458 return 1;
459 }
460 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
461 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
462 if (rtmsg(RTM_GET) < 0)
463 errx(1, "RTM_GET(%s) failed", host);
464 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
465 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) +
466 (char *)(void *)mysin);
467 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) {
468 if (sdl->sdl_family == AF_LINK &&
469 (rtm->rtm_flags & RTF_LLINFO) &&
470 !(rtm->rtm_flags & RTF_GATEWAY)) {
471 goto delete;
472 }
473 /*
474 * IPv4 arp command retries with sin_other = SIN_PROXY here.
475 */
476 warnx("delete: cannot delete non-NDP entry");
477 return 1;
478 }
479
480 delete:
481 if (sdl->sdl_family != AF_LINK) {
482 (void)printf("cannot locate %s\n", host);
483 return (1);
484 }
485 if (rtmsg(RTM_DELETE) == 0) {
486 struct sockaddr_in6 s6 = *mysin; /* XXX: for safety */
487
488 mysin->sin6_scope_id = 0;
489 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
490 (void)getnameinfo((struct sockaddr *)(void *)&s6,
491 (socklen_t)s6.sin6_len, host_buf,
492 sizeof(host_buf), NULL, 0,
493 (nflag ? NI_NUMERICHOST : 0));
494 (void)printf("%s (%s) deleted\n", host, host_buf);
495 }
496
497 return 0;
498 }
499
500 #define W_ADDR 36
501 #define W_LL 17
502 #define W_IF 6
503
504 /*
505 * Dump the entire neighbor cache
506 */
507 static void
508 dump(struct in6_addr *addr, int cflag)
509 {
510 int mib[6];
511 size_t needed;
512 char *lim, *buf, *next;
513 struct rt_msghdr *rtm;
514 struct sockaddr_in6 *mysin;
515 struct sockaddr_dl *sdl;
516 struct in6_nbrinfo *nbi;
517 struct timeval tim;
518 int addrwidth;
519 int llwidth;
520 int ifwidth;
521 char flgbuf[8];
522 const char *ifname;
523
524 /* Print header */
525 if (!tflag && !cflag)
526 (void)printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
527 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
528 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
529
530 again:;
531 mib[0] = CTL_NET;
532 mib[1] = PF_ROUTE;
533 mib[2] = 0;
534 mib[3] = AF_INET6;
535 mib[4] = NET_RT_FLAGS;
536 mib[5] = RTF_LLINFO;
537 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
538 err(1, "sysctl(PF_ROUTE estimate)");
539 if (needed > 0) {
540 if ((buf = malloc(needed)) == NULL)
541 err(1, "malloc");
542 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
543 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
544 lim = buf + needed;
545 } else
546 buf = lim = NULL;
547
548 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
549 int isrouter = 0, prbs = 0;
550
551 rtm = (struct rt_msghdr *)(void *)next;
552 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
553 sdl = (struct sockaddr_dl *)(void *)((char *)(void *)mysin + RT_ROUNDUP(mysin->sin6_len));
554
555 /*
556 * Some OSes can produce a route that has the LINK flag but
557 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
558 * and BSD/OS, where xx is not the interface identifier on
559 * lo0). Such routes entry would annoy getnbrinfo() below,
560 * so we skip them.
561 * XXX: such routes should have the GATEWAY flag, not the
562 * LINK flag. However, there is rotten routing software
563 * that advertises all routes that have the GATEWAY flag.
564 * Thus, KAME kernel intentionally does not set the LINK flag.
565 * What is to be fixed is not ndp, but such routing software
566 * (and the kernel workaround)...
567 */
568 if (sdl->sdl_family != AF_LINK)
569 continue;
570
571 if (!(rtm->rtm_flags & RTF_HOST))
572 continue;
573
574 if (addr) {
575 if (!IN6_ARE_ADDR_EQUAL(addr, &mysin->sin6_addr))
576 continue;
577 found_entry = 1;
578 } else if (IN6_IS_ADDR_MULTICAST(&mysin->sin6_addr))
579 continue;
580 if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr) ||
581 IN6_IS_ADDR_MC_LINKLOCAL(&mysin->sin6_addr)) {
582 uint16_t scopeid = mysin->sin6_scope_id;
583 inet6_getscopeid(mysin, INET6_IS_ADDR_LINKLOCAL|
584 INET6_IS_ADDR_MC_LINKLOCAL);
585 if (scopeid == 0)
586 mysin->sin6_scope_id = sdl->sdl_index;
587 }
588 (void)getnameinfo((struct sockaddr *)(void *)mysin,
589 (socklen_t)mysin->sin6_len,
590 host_buf, sizeof(host_buf), NULL, 0,
591 (nflag ? NI_NUMERICHOST : 0));
592 if (cflag) {
593 #ifdef RTF_WASCLONED
594 if (rtm->rtm_flags & RTF_WASCLONED)
595 (void)delete(host_buf);
596 #elif defined(RTF_CLONED)
597 if (rtm->rtm_flags & RTF_CLONED)
598 (void)delete(host_buf);
599 #else
600 (void)delete(host_buf);
601 #endif
602 continue;
603 }
604 (void)gettimeofday(&tim, 0);
605 if (tflag)
606 ts_print(&tim);
607
608 addrwidth = strlen(host_buf);
609 if (addrwidth < W_ADDR)
610 addrwidth = W_ADDR;
611 llwidth = strlen(ether_str(sdl));
612 if (W_ADDR + W_LL - addrwidth > llwidth)
613 llwidth = W_ADDR + W_LL - addrwidth;
614 ifname = if_indextoname((unsigned int)sdl->sdl_index, ifix_buf);
615 if (!ifname)
616 ifname = "?";
617 ifwidth = strlen(ifname);
618 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
619 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
620
621 (void)printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth,
622 host_buf, llwidth, llwidth, ether_str(sdl), ifwidth,
623 ifwidth, ifname);
624
625 /* Print neighbor discovery specific informations */
626 nbi = getnbrinfo(&mysin->sin6_addr,
627 (unsigned int)sdl->sdl_index, 1);
628 if (nbi) {
629 if (nbi->expire > tim.tv_sec) {
630 (void)printf(" %-9.9s",
631 sec2str(nbi->expire - tim.tv_sec));
632 } else if (nbi->expire == 0)
633 (void)printf(" %-9.9s", "permanent");
634 else
635 (void)printf(" %-9.9s", "expired");
636
637 switch (nbi->state) {
638 case ND6_LLINFO_NOSTATE:
639 (void)printf(" N");
640 break;
641 #ifdef ND6_LLINFO_WAITDELETE
642 case ND6_LLINFO_WAITDELETE:
643 (void)printf(" W");
644 break;
645 #endif
646 case ND6_LLINFO_INCOMPLETE:
647 (void)printf(" I");
648 break;
649 case ND6_LLINFO_REACHABLE:
650 (void)printf(" R");
651 break;
652 case ND6_LLINFO_STALE:
653 (void)printf(" S");
654 break;
655 case ND6_LLINFO_DELAY:
656 (void)printf(" D");
657 break;
658 case ND6_LLINFO_PROBE:
659 (void)printf(" P");
660 break;
661 default:
662 (void)printf(" ?");
663 break;
664 }
665
666 isrouter = nbi->isrouter;
667 prbs = nbi->asked;
668 } else {
669 warnx("failed to get neighbor information");
670 (void)printf(" ");
671 }
672
673 /*
674 * other flags. R: router, P: proxy, W: ??
675 */
676 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
677 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s",
678 isrouter ? "R" : "",
679 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
680 } else {
681 mysin = (struct sockaddr_in6 *)(void *)
682 (sdl->sdl_len + (char *)(void *)sdl);
683 #if 0 /* W and P are mystery even for us */
684 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
685 isrouter ? "R" : "",
686 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
687 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
688 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
689 #else
690 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s",
691 isrouter ? "R" : "",
692 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
693 #endif
694 }
695 (void)printf(" %s", flgbuf);
696
697 if (prbs)
698 (void)printf(" %d", prbs);
699
700 (void)printf("\n");
701 }
702 if (buf != NULL)
703 free(buf);
704
705 if (repeat) {
706 (void)printf("\n");
707 (void)fflush(stdout);
708 (void)sleep(repeat);
709 goto again;
710 }
711 }
712
713 static struct in6_nbrinfo *
714 getnbrinfo(struct in6_addr *addr, unsigned int ifindex, int warning)
715 {
716 static struct in6_nbrinfo nbi;
717 int s;
718
719 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
720 err(1, "socket");
721
722 (void)memset(&nbi, 0, sizeof(nbi));
723 (void)if_indextoname(ifindex, nbi.ifname);
724 nbi.addr = *addr;
725 if (ioctl(s, SIOCGNBRINFO_IN6, &nbi) < 0) {
726 if (warning)
727 warn("ioctl(SIOCGNBRINFO_IN6)");
728 (void)close(s);
729 return(NULL);
730 }
731
732 (void)close(s);
733 return(&nbi);
734 }
735
736 static char *
737 ether_str(struct sockaddr_dl *sdl)
738 {
739 static char hbuf[NI_MAXHOST];
740
741 if (sdl->sdl_alen) {
742 if (getnameinfo((struct sockaddr *)(void *)sdl,
743 (socklen_t)sdl->sdl_len,
744 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
745 (void)snprintf(hbuf, sizeof(hbuf), "<invalid>");
746 } else
747 (void)snprintf(hbuf, sizeof(hbuf), "(incomplete)");
748
749 return(hbuf);
750 }
751
752 static int
753 ndp_ether_aton(char *a, u_char *n)
754 {
755 int i, o[6];
756
757 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
758 &o[3], &o[4], &o[5]);
759 if (i != 6) {
760 warnx("invalid Ethernet address '%s'", a);
761 return (1);
762 }
763 for (i = 0; i < 6; i++)
764 n[i] = o[i];
765 return (0);
766 }
767
768 static void
769 usage(void)
770 {
771 const char *pn = getprogname();
772
773 (void)fprintf(stderr, "Usage: %s [-nt] hostname\n", pn);
774 (void)fprintf(stderr,
775 " %s [-nt] -a | -c | -p | -r | -H | -P | -R\n", pn);
776 (void)fprintf(stderr, " %s [-nt] -A wait\n", pn);
777 (void)fprintf(stderr, " %s [-nt] -d hostname\n", pn);
778 (void)fprintf(stderr, " %s [-nt] -f filename\n", pn);
779 (void)fprintf(stderr, " %s [-nt] -i interface [flags...]\n", pn);
780 #ifdef SIOCSDEFIFACE_IN6
781 (void)fprintf(stderr, " %s [-nt] -I [interface|delete]\n", pn);
782 #endif
783 (void)fprintf(stderr,
784 " %s [-nt] -s nodename etheraddr [temp] [proxy]\n", pn);
785 exit(1);
786 }
787
788 static int
789 rtmsg(int cmd)
790 {
791 static int seq;
792 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
793 register char *cp = m_rtmsg.m_space;
794 register int l;
795
796 errno = 0;
797 if (cmd == RTM_DELETE)
798 goto doit;
799 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
800 rtm->rtm_flags = flags;
801 rtm->rtm_version = RTM_VERSION;
802
803 switch (cmd) {
804 default:
805 errx(1, "internal wrong cmd");
806 /*NOTREACHED*/
807 case RTM_ADD:
808 rtm->rtm_addrs |= RTA_GATEWAY;
809 if (expire_time) {
810 rtm->rtm_rmx.rmx_expire = expire_time;
811 rtm->rtm_inits = RTV_EXPIRE;
812 }
813 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
814 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */
815 if (rtm->rtm_flags & RTF_ANNOUNCE) {
816 rtm->rtm_flags &= ~RTF_HOST;
817 rtm->rtm_addrs |= RTA_NETMASK;
818 }
819 #endif
820 /* FALLTHROUGH */
821 case RTM_GET:
822 rtm->rtm_addrs |= RTA_DST;
823 }
824 #define NEXTADDR(w, s) \
825 if (rtm->rtm_addrs & (w)) { \
826 (void)memcpy(cp, &s, sizeof(s)); \
827 RT_ADVANCE(cp, (struct sockaddr *)(void *)&s); \
828 }
829
830 NEXTADDR(RTA_DST, sin_m);
831 NEXTADDR(RTA_GATEWAY, sdl_m);
832 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */
833 (void)memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
834 NEXTADDR(RTA_NETMASK, so_mask);
835 #endif
836
837 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
838 doit:
839 l = rtm->rtm_msglen;
840 rtm->rtm_seq = ++seq;
841 rtm->rtm_type = cmd;
842 if (write(my_s, &m_rtmsg, (size_t)l) == -1) {
843 if (errno != ESRCH || cmd != RTM_DELETE)
844 err(1, "writing to routing socket");
845 }
846 do {
847 l = read(my_s, &m_rtmsg, sizeof(m_rtmsg));
848 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
849 if (l < 0)
850 warn("read from routing socket");
851 return (0);
852 }
853
854 static void
855 ifinfo(char *ifname, int argc, char **argv)
856 {
857 struct in6_ndireq nd;
858 int i, s;
859 u_int32_t newflags;
860 #ifdef IPV6CTL_USETEMPADDR
861 u_int8_t nullbuf[8];
862 #endif
863
864 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
865 err(1, "socket");
866 (void)memset(&nd, 0, sizeof(nd));
867 (void)strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
868 if (ioctl(s, SIOCGIFINFO_IN6, &nd) < 0)
869 err(1, "ioctl(SIOCGIFINFO_IN6)");
870 #define ND nd.ndi
871 newflags = ND.flags;
872 for (i = 0; i < argc; i++) {
873 int clear = 0;
874 char *cp = argv[i];
875
876 if (*cp == '-') {
877 clear = 1;
878 cp++;
879 }
880
881 #define SETFLAG(s, f) \
882 do {\
883 if (strcmp(cp, (s)) == 0) {\
884 if (clear)\
885 newflags &= ~(f);\
886 else\
887 newflags |= (f);\
888 }\
889 } while (/*CONSTCOND*/0)
890 /*
891 * XXX: this macro is not 100% correct, in that it matches "nud" against
892 * "nudbogus". But we just let it go since this is minor.
893 */
894 #define SETVALUE(f, v) \
895 do { \
896 char *valptr; \
897 unsigned long newval; \
898 v = 0; /* unspecified */ \
899 if (strncmp(cp, f, strlen(f)) == 0) { \
900 valptr = strchr(cp, '='); \
901 if (valptr == NULL) \
902 err(1, "syntax error in %s field", (f)); \
903 errno = 0; \
904 newval = strtoul(++valptr, NULL, 0); \
905 if (errno) \
906 err(1, "syntax error in %s's value", (f)); \
907 v = newval; \
908 } \
909 } while (/*CONSTCOND*/0)
910
911 #ifdef ND6_IFF_IFDISABLED
912 SETFLAG("disabled", ND6_IFF_IFDISABLED);
913 #endif
914 SETFLAG("nud", ND6_IFF_PERFORMNUD);
915 #ifdef ND6_IFF_ACCEPT_RTADV
916 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
917 #endif
918 #ifdef ND6_IFF_OVERRIDE_RTADV
919 SETFLAG("override_rtadv", ND6_IFF_OVERRIDE_RTADV);
920 #endif
921 #ifdef ND6_IFF_PREFER_SOURCE
922 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
923 #endif
924 #ifdef ND6_IFF_DONT_SET_IFROUTE
925 SETFLAG("dont_set_ifroute", ND6_IFF_DONT_SET_IFROUTE);
926 #endif
927 SETVALUE("basereachable", ND.basereachable);
928 SETVALUE("retrans", ND.retrans);
929 SETVALUE("curhlim", ND.chlim);
930
931 ND.flags = newflags;
932 #ifdef SIOCSIFINFO_IN6
933 if (ioctl(s, SIOCSIFINFO_IN6, &nd) < 0)
934 err(1, "ioctl(SIOCSIFINFO_IN6)");
935 #else
936 if (ioctl(s, SIOCSIFINFO_FLAGS, &nd) < 0)
937 err(1, "ioctl(SIOCSIFINFO_FLAGS)");
938 #endif
939 #undef SETFLAG
940 #undef SETVALUE
941 }
942
943 if (!ND.initialized)
944 errx(1, "%s: not initialized yet", ifname);
945
946 if (ioctl(s, SIOCGIFINFO_IN6, &nd) < 0)
947 err(1, "ioctl(SIOCGIFINFO_IN6)");
948 (void)printf("linkmtu=%d", ND.linkmtu);
949 (void)printf(", maxmtu=%d", ND.maxmtu);
950 (void)printf(", curhlim=%d", ND.chlim);
951 (void)printf(", basereachable=%ds%dms",
952 ND.basereachable / 1000, ND.basereachable % 1000);
953 (void)printf(", reachable=%ds", ND.reachable);
954 (void)printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
955 #ifdef IPV6CTL_USETEMPADDR
956 (void)memset(nullbuf, 0, sizeof(nullbuf));
957 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
958 int j;
959 u_int8_t *rbuf;
960
961 for (i = 0; i < 3; i++) {
962 switch (i) {
963 case 0:
964 (void)printf("\nRandom seed(0): ");
965 rbuf = ND.randomseed0;
966 break;
967 case 1:
968 (void)printf("\nRandom seed(1): ");
969 rbuf = ND.randomseed1;
970 break;
971 case 2:
972 (void)printf("\nRandom ID: ");
973 rbuf = ND.randomid;
974 break;
975 default:
976 errx(1, "impossible case for tempaddr display");
977 }
978 for (j = 0; j < 8; j++)
979 (void)printf("%02x", rbuf[j]);
980 }
981 }
982 #endif
983 if (ND.flags) {
984 (void)printf("\nFlags: ");
985 if ((ND.flags & ND6_IFF_PERFORMNUD))
986 (void)printf("nud ");
987 #ifdef ND6_IFF_IFDISABLED
988 if ((ND.flags & ND6_IFF_IFDISABLED))
989 (void)printf("disabled ");
990 #endif
991 #ifdef ND6_IFF_ACCEPT_RTADV
992 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
993 (void)printf("accept_rtadv ");
994 #endif
995 #ifdef ND6_IFF_OVERRIDE_RTADV
996 if ((ND.flags & ND6_IFF_OVERRIDE_RTADV))
997 (void)printf("override_rtadv ");
998 #endif
999 #ifdef ND6_IFF_PREFER_SOURCE
1000 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1001 (void)printf("prefer_source ");
1002 #endif
1003 }
1004 (void)putc('\n', stdout);
1005 #undef ND
1006
1007 (void)close(s);
1008 }
1009
1010 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1011 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1012 #endif
1013
1014 static void
1015 rtrlist(void)
1016 {
1017 #ifdef ICMPV6CTL_ND6_DRLIST
1018 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1019 char *buf;
1020 struct in6_defrouter *p, *ep;
1021 size_t l;
1022 struct timeval tim;
1023
1024 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1025 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1026 /*NOTREACHED*/
1027 }
1028 if (l == 0)
1029 return;
1030 buf = malloc(l);
1031 if (!buf) {
1032 err(1, "malloc");
1033 /*NOTREACHED*/
1034 }
1035 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1036 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1037 /*NOTREACHED*/
1038 }
1039
1040 ep = (struct in6_defrouter *)(void *)(buf + l);
1041 for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) {
1042 int rtpref;
1043
1044 if (getnameinfo((struct sockaddr *)(void *)&p->rtaddr,
1045 (socklen_t)p->rtaddr.sin6_len, host_buf, sizeof(host_buf),
1046 NULL, 0, (nflag ? NI_NUMERICHOST : 0)) != 0)
1047 (void)strlcpy(host_buf, "?", sizeof(host_buf));
1048
1049 (void)printf("%s if=%s", host_buf,
1050 if_indextoname((unsigned int)p->if_index, ifix_buf));
1051 (void)printf(", flags=%s%s",
1052 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1053 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1054 rtpref = ((uint32_t)(p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1055 (void)printf(", pref=%s", rtpref_str[rtpref]);
1056
1057 (void)gettimeofday(&tim, 0);
1058 if (p->expire == 0)
1059 (void)printf(", expire=Never\n");
1060 else
1061 (void)printf(", expire=%s\n",
1062 sec2str((time_t)(p->expire - tim.tv_sec)));
1063 }
1064 free(buf);
1065 #else
1066 struct in6_drlist dr;
1067 int s, i;
1068 struct timeval time;
1069
1070 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1071 err(1, "socket");
1072 /* NOTREACHED */
1073 }
1074 (void)memset(&dr, 0, sizeof(dr));
1075 (void)strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1076 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1077 err(1, "ioctl(SIOCGDRLST_IN6)");
1078 /* NOTREACHED */
1079 }
1080 #define DR dr.defrouter[i]
1081 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1082 struct sockaddr_in6 sin6;
1083
1084 (void)memset(&sin6, 0, sizeof(sin6));
1085 sin6.sin6_family = AF_INET6;
1086 sin6.sin6_len = sizeof(sin6);
1087 sin6.sin6_addr = DR.rtaddr;
1088 (void)getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1089 host_buf, sizeof(host_buf), NULL, 0,
1090 (nflag ? NI_NUMERICHOST : 0));
1091
1092 (void)printf("%s if=%s", host_buf,
1093 if_indextoname(DR.if_index, ifix_buf));
1094 (void)printf(", flags=%s%s",
1095 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1096 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1097 gettimeofday(&time, 0);
1098 if (DR.expire == 0)
1099 (void)printf(", expire=Never\n");
1100 else
1101 (void)printf(", expire=%s\n",
1102 sec2str(DR.expire - time.tv_sec));
1103 }
1104 #undef DR
1105 (void)close(s);
1106 #endif
1107 }
1108
1109 static void
1110 plist(void)
1111 {
1112 #ifdef ICMPV6CTL_ND6_PRLIST
1113 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1114 char *buf, *p, *ep;
1115 struct in6_prefix pfx;
1116 size_t l;
1117 struct timeval tim;
1118 const int niflags = NI_NUMERICHOST;
1119 int ninflags = nflag ? NI_NUMERICHOST : 0;
1120 char namebuf[NI_MAXHOST];
1121
1122 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1123 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1124 /*NOTREACHED*/
1125 }
1126 buf = malloc(l);
1127 if (!buf) {
1128 err(1, "malloc");
1129 /*NOTREACHED*/
1130 }
1131 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1132 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1133 /*NOTREACHED*/
1134 }
1135
1136 ep = buf + l;
1137 for (p = buf; p < ep; ) {
1138 memcpy(&pfx, p, sizeof(pfx));
1139 p += sizeof(pfx);
1140
1141 if (getnameinfo((struct sockaddr*)&pfx.prefix,
1142 (socklen_t)pfx.prefix.sin6_len, namebuf, sizeof(namebuf),
1143 NULL, 0, niflags) != 0)
1144 (void)strlcpy(namebuf, "?", sizeof(namebuf));
1145 (void)printf("%s/%d if=%s\n", namebuf, pfx.prefixlen,
1146 if_indextoname((unsigned int)pfx.if_index, ifix_buf));
1147
1148 (void)gettimeofday(&tim, 0);
1149 /*
1150 * meaning of fields, especially flags, is very different
1151 * by origin. notify the difference to the users.
1152 */
1153 (void)printf("flags=%s%s%s%s%s",
1154 pfx.raflags.onlink ? "L" : "",
1155 pfx.raflags.autonomous ? "A" : "",
1156 (pfx.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1157 (pfx.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1158 #ifdef NDPRF_HOME
1159 (pfx.flags & NDPRF_HOME) != 0 ? "H" : ""
1160 #else
1161 ""
1162 #endif
1163 );
1164 if (pfx.vltime == ND6_INFINITE_LIFETIME)
1165 (void)printf(" vltime=infinity");
1166 else
1167 (void)printf(" vltime=%lu", (unsigned long)pfx.vltime);
1168 if (pfx.pltime == ND6_INFINITE_LIFETIME)
1169 (void)printf(", pltime=infinity");
1170 else
1171 (void)printf(", pltime=%lu", (unsigned long)pfx.pltime);
1172 if (pfx.expire == 0)
1173 (void)printf(", expire=Never");
1174 else if (pfx.expire >= tim.tv_sec)
1175 (void)printf(", expire=%s",
1176 sec2str(pfx.expire - tim.tv_sec));
1177 else
1178 (void)printf(", expired");
1179 (void)printf(", ref=%d", pfx.refcnt);
1180 (void)printf("\n");
1181 /*
1182 * "advertising router" list is meaningful only if the prefix
1183 * information is from RA.
1184 */
1185 if (pfx.advrtrs) {
1186 int j;
1187 struct sockaddr_in6 sin6;
1188
1189 (void)printf(" advertised by\n");
1190 for (j = 0; j < pfx.advrtrs && p <= ep; j++) {
1191 struct in6_nbrinfo *nbi;
1192
1193 memcpy(&sin6, p, sizeof(sin6));
1194 p += sizeof(sin6);
1195
1196 if (getnameinfo((struct sockaddr *)&sin6,
1197 (socklen_t)sin6.sin6_len, namebuf,
1198 sizeof(namebuf), NULL, 0, ninflags) != 0)
1199 (void)strlcpy(namebuf, "?", sizeof(namebuf));
1200 (void)printf(" %s", namebuf);
1201
1202 nbi = getnbrinfo(&sin6.sin6_addr,
1203 (unsigned int)pfx.if_index, 0);
1204 if (nbi) {
1205 switch (nbi->state) {
1206 case ND6_LLINFO_REACHABLE:
1207 case ND6_LLINFO_STALE:
1208 case ND6_LLINFO_DELAY:
1209 case ND6_LLINFO_PROBE:
1210 (void)printf(" (reachable)\n");
1211 break;
1212 default:
1213 (void)printf(" (unreachable)\n");
1214 }
1215 } else
1216 (void)printf(" (no neighbor state)\n");
1217 }
1218 } else
1219 (void)printf(" No advertising router\n");
1220 }
1221 free(buf);
1222 #else
1223 struct in6_prlist pr;
1224 int s, i;
1225 struct timeval time;
1226
1227 (void)gettimeofday(&time, 0);
1228
1229 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1230 err(1, "socket");
1231 /* NOTREACHED */
1232 }
1233 (void)memset(&pr, 0, sizeof(pr));
1234 (void)strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1235 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1236 err(1, "ioctl(SIOCGPRLST_IN6)");
1237 /* NOTREACHED */
1238 }
1239 #define PR pr.prefix[i]
1240 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1241 struct sockaddr_in6 p6;
1242 char namebuf[NI_MAXHOST];
1243 int niflags;
1244
1245 #ifdef NDPRF_ONLINK
1246 p6 = PR.prefix;
1247 #else
1248 (void)memset(&p6, 0, sizeof(p6));
1249 p6.sin6_family = AF_INET6;
1250 p6.sin6_len = sizeof(p6);
1251 p6.sin6_addr = PR.prefix;
1252 #endif
1253
1254 niflags = NI_NUMERICHOST;
1255 if (getnameinfo((struct sockaddr *)&p6,
1256 sizeof(p6), namebuf, sizeof(namebuf),
1257 NULL, 0, niflags)) {
1258 warnx("getnameinfo failed");
1259 continue;
1260 }
1261 (void)printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1262 if_indextoname(PR.if_index, ifix_buf));
1263
1264 (void)gettimeofday(&time, 0);
1265 /*
1266 * meaning of fields, especially flags, is very different
1267 * by origin. notify the difference to the users.
1268 */
1269 #if 0
1270 (void)printf(" %s",
1271 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1272 #endif
1273 #ifdef NDPRF_ONLINK
1274 (void)printf("flags=%s%s%s%s%s",
1275 PR.raflags.onlink ? "L" : "",
1276 PR.raflags.autonomous ? "A" : "",
1277 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1278 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1279 #ifdef NDPRF_HOME
1280 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1281 #else
1282 ""
1283 #endif
1284 );
1285 #else
1286 (void)printf("flags=%s%s",
1287 PR.raflags.onlink ? "L" : "",
1288 PR.raflags.autonomous ? "A" : "");
1289 #endif
1290 if (PR.vltime == ND6_INFINITE_LIFETIME)
1291 (void)printf(" vltime=infinity");
1292 else
1293 (void)printf(" vltime=%lu", PR.vltime);
1294 if (PR.pltime == ND6_INFINITE_LIFETIME)
1295 (void)printf(", pltime=infinity");
1296 else
1297 (void)printf(", pltime=%lu", PR.pltime);
1298 if (PR.expire == 0)
1299 (void)printf(", expire=Never");
1300 else if (PR.expire >= time.tv_sec)
1301 (void)printf(", expire=%s",
1302 sec2str(PR.expire - time.tv_sec));
1303 else
1304 (void)printf(", expired");
1305 #ifdef NDPRF_ONLINK
1306 (void)printf(", ref=%d", PR.refcnt);
1307 #endif
1308 #if 0
1309 switch (PR.origin) {
1310 case PR_ORIG_RA:
1311 (void)printf(", origin=RA");
1312 break;
1313 case PR_ORIG_RR:
1314 (void)printf(", origin=RR");
1315 break;
1316 case PR_ORIG_STATIC:
1317 (void)printf(", origin=static");
1318 break;
1319 case PR_ORIG_KERNEL:
1320 (void)printf(", origin=kernel");
1321 break;
1322 default:
1323 (void)printf(", origin=?");
1324 break;
1325 }
1326 #endif
1327 (void)printf("\n");
1328 /*
1329 * "advertising router" list is meaningful only if the prefix
1330 * information is from RA.
1331 */
1332 if (0 && /* prefix origin is almost obsolted */
1333 PR.origin != PR_ORIG_RA)
1334 ;
1335 else if (PR.advrtrs) {
1336 int j;
1337 (void)printf(" advertised by\n");
1338 for (j = 0; j < PR.advrtrs; j++) {
1339 struct sockaddr_in6 sin6;
1340 struct in6_nbrinfo *nbi;
1341
1342 bzero(&sin6, sizeof(sin6));
1343 sin6.sin6_family = AF_INET6;
1344 sin6.sin6_len = sizeof(sin6);
1345 sin6.sin6_addr = PR.advrtr[j];
1346 sin6.sin6_scope_id = PR.if_index; /* XXX */
1347 (void)getnameinfo((struct sockaddr *)&sin6,
1348 sin6.sin6_len, host_buf,
1349 sizeof(host_buf), NULL, 0,
1350 (nflag ? NI_NUMERICHOST : 0));
1351 (void)printf(" %s", host_buf);
1352
1353 nbi = getnbrinfo(&sin6.sin6_addr,
1354 PR.if_index, 0);
1355 if (nbi) {
1356 switch (nbi->state) {
1357 case ND6_LLINFO_REACHABLE:
1358 case ND6_LLINFO_STALE:
1359 case ND6_LLINFO_DELAY:
1360 case ND6_LLINFO_PROBE:
1361 (void)printf(" (reachable)\n");
1362 break;
1363 default:
1364 (void)printf(" (unreachable)\n");
1365 }
1366 } else
1367 (void)printf(" (no neighbor state)\n");
1368 }
1369 if (PR.advrtrs > DRLSTSIZ)
1370 (void)printf(" and %d routers\n",
1371 PR.advrtrs - DRLSTSIZ);
1372 } else
1373 (void)printf(" No advertising router\n");
1374 }
1375 #undef PR
1376 (void)close(s);
1377 #endif
1378 }
1379
1380 static void
1381 pfx_flush(void)
1382 {
1383 char dummyif[IFNAMSIZ+8];
1384 int s;
1385
1386 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1387 err(1, "socket");
1388 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1389 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1390 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1391 (void)close(s);
1392 }
1393
1394 static void
1395 rtr_flush(void)
1396 {
1397 char dummyif[IFNAMSIZ+8];
1398 int s;
1399
1400 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1401 err(1, "socket");
1402 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1403 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1404 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1405
1406 (void)close(s);
1407 }
1408
1409 static void
1410 harmonize_rtr(void)
1411 {
1412 char dummyif[IFNAMSIZ+8];
1413 int s;
1414
1415 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1416 err(1, "socket");
1417 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1418 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1419 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1420
1421 (void)close(s);
1422 }
1423
1424 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1425 static void
1426 setdefif(char *ifname)
1427 {
1428 struct in6_ndifreq ndifreq;
1429 unsigned int ifindex;
1430 int s;
1431
1432 if (strcasecmp(ifname, "delete") == 0)
1433 ifindex = 0;
1434 else {
1435 if ((ifindex = if_nametoindex(ifname)) == 0)
1436 err(1, "failed to resolve i/f index for %s", ifname);
1437 }
1438
1439 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1440 err(1, "socket");
1441
1442 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1443 ndifreq.ifindex = ifindex;
1444
1445 if (ioctl(s, SIOCSDEFIFACE_IN6, &ndifreq) < 0)
1446 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1447
1448 (void)close(s);
1449 }
1450
1451 static void
1452 getdefif(void)
1453 {
1454 struct in6_ndifreq ndifreq;
1455 char ifname[IFNAMSIZ+8];
1456 int s;
1457
1458 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1459 err(1, "socket");
1460
1461 (void)memset(&ndifreq, 0, sizeof(ndifreq));
1462 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1463
1464 if (ioctl(s, SIOCGDEFIFACE_IN6, &ndifreq) < 0)
1465 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1466
1467 if (ndifreq.ifindex == 0)
1468 (void)printf("No default interface.\n");
1469 else {
1470 if ((if_indextoname((unsigned int)ndifreq.ifindex, ifname)) == NULL)
1471 err(1, "failed to resolve ifname for index %lu",
1472 ndifreq.ifindex);
1473 (void)printf("ND default interface = %s\n", ifname);
1474 }
1475
1476 (void)close(s);
1477 }
1478 #endif
1479
1480 static const char *
1481 sec2str(time_t total)
1482 {
1483 static char result[256];
1484 int days, hours, mins, secs;
1485 int first = 1;
1486 char *p = result;
1487 char *ep = &result[sizeof(result)];
1488 int n;
1489
1490 days = total / 3600 / 24;
1491 hours = (total / 3600) % 24;
1492 mins = (total / 60) % 60;
1493 secs = total % 60;
1494
1495 if (days) {
1496 first = 0;
1497 n = snprintf(p, (size_t)(ep - p), "%dd", days);
1498 if (n < 0 || n >= ep - p)
1499 return "?";
1500 p += n;
1501 }
1502 if (!first || hours) {
1503 first = 0;
1504 n = snprintf(p, (size_t)(ep - p), "%dh", hours);
1505 if (n < 0 || n >= ep - p)
1506 return "?";
1507 p += n;
1508 }
1509 if (!first || mins) {
1510 first = 0;
1511 n = snprintf(p, (size_t)(ep - p), "%dm", mins);
1512 if (n < 0 || n >= ep - p)
1513 return "?";
1514 p += n;
1515 }
1516 (void)snprintf(p, (size_t)(ep - p), "%ds", secs);
1517
1518 return(result);
1519 }
1520
1521 /*
1522 * Print the timestamp
1523 * from tcpdump/util.c
1524 */
1525 static void
1526 ts_print(const struct timeval *tvp)
1527 {
1528 int s;
1529
1530 /* Default */
1531 s = (tvp->tv_sec + thiszone) % 86400;
1532 (void)printf("%02d:%02d:%02d.%06u ",
1533 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1534 }
1535