Drop main() prototype. Syncs with NetBSD-8
[minix.git] / usr.sbin / ndp / ndp.c
blobfc1da82b1b5aede61d4cf3a6bc154670b8ee4bb1
1 /* $NetBSD: ndp.c,v 1.45 2015/08/03 09:51:40 ozaki-r Exp $ */
2 /* $KAME: ndp.c,v 1.121 2005/07/13 11:30:13 keiichi Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6 * All rights reserved.
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.
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.
33 * Copyright (c) 1984, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * Sun Microsystems, Inc.
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.
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.
65 * Based on:
66 * "@(#) Copyright (c) 1984, 1993\n\
67 * The Regents of the University of California. All rights reserved.\n";
69 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
73 * ndp - display, set, delete and flush neighbor cache
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>
84 #include <net/if.h>
85 #include <net/if_dl.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
89 #include <netinet/in.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet6/nd6.h>
95 #include <arpa/inet.h>
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>
108 #include "gmt2local.h"
109 #include "prog_ops.h"
111 static pid_t pid;
112 static int nflag;
113 static int tflag;
114 static int32_t thiszone; /* time difference with gmt */
115 static int my_s = -1;
116 static unsigned int repeat = 0;
119 static char host_buf[NI_MAXHOST]; /* getnameinfo() */
120 static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
122 static void getsocket(void);
123 static int set(int, char **);
124 static void get(char *);
125 static int delete(char *);
126 static void dump(struct in6_addr *, int);
127 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, unsigned int, int);
128 static char *ether_str(struct sockaddr_dl *);
129 static int ndp_ether_aton(char *, u_char *);
130 __dead static void usage(void);
131 static int rtmsg(int);
132 static void ifinfo(char *, int, char **);
133 static void rtrlist(void);
134 static void plist(void);
135 static void pfx_flush(void);
136 static void rtrlist(void);
137 static void rtr_flush(void);
138 static void harmonize_rtr(void);
139 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
140 static void getdefif(void);
141 static void setdefif(char *);
142 #endif
143 static const char *sec2str(time_t);
144 static char *ether_str(struct sockaddr_dl *);
145 static void ts_print(const struct timeval *);
147 #ifdef ICMPV6CTL_ND6_DRLIST
148 static const char *rtpref_str[] = {
149 "medium", /* 00 */
150 "high", /* 01 */
151 "rsv", /* 10 */
152 "low" /* 11 */
154 #endif
156 static int mode = 0;
157 static char *arg = NULL;
160 main(int argc, char **argv)
162 int ch;
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*/
179 mode = ch;
180 arg = NULL;
181 break;
182 case 'd':
183 case 'f':
184 case 'i' :
185 if (mode) {
186 usage();
187 /*NOTREACHED*/
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*/
203 mode = 'a';
204 repeat = atoi(optarg);
205 break;
206 default:
207 usage();
210 argc -= optind;
211 argv += optind;
213 if (prog_init && prog_init() == -1)
214 err(1, "init failed");
216 pid = prog_getpid();
217 thiszone = gmt2local(0L);
219 switch (mode) {
220 case 'a':
221 case 'c':
222 if (argc != 0) {
223 usage();
224 /*NOTREACHED*/
226 dump(0, mode == 'c');
227 break;
228 case 'd':
229 if (argc != 0) {
230 usage();
231 /*NOTREACHED*/
233 (void)delete(arg);
234 break;
235 case 'I':
236 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
237 if (argc > 1) {
238 usage();
239 /*NOTREACHED*/
240 } else if (argc == 1) {
241 if (strcmp(*argv, "delete") == 0 ||
242 if_nametoindex(*argv))
243 setdefif(*argv);
244 else
245 errx(1, "invalid interface %s", *argv);
247 getdefif(); /* always call it to print the result */
248 break;
249 #else
250 errx(1, "not supported yet");
251 /*NOTREACHED*/
252 #endif
253 case 'p':
254 if (argc != 0) {
255 usage();
256 /*NOTREACHED*/
258 plist();
259 break;
260 case 'i':
261 ifinfo(arg, argc, argv);
262 break;
263 case 'r':
264 if (argc != 0) {
265 usage();
266 /*NOTREACHED*/
268 rtrlist();
269 break;
270 case 's':
271 if (argc < 2 || argc > 4)
272 usage();
273 return(set(argc, argv) ? 1 : 0);
274 case 'H':
275 if (argc != 0) {
276 usage();
277 /*NOTREACHED*/
279 harmonize_rtr();
280 break;
281 case 'P':
282 if (argc != 0) {
283 usage();
284 /*NOTREACHED*/
286 pfx_flush();
287 break;
288 case 'R':
289 if (argc != 0) {
290 usage();
291 /*NOTREACHED*/
293 rtr_flush();
294 break;
295 case 0:
296 if (argc != 1) {
297 usage();
298 /*NOTREACHED*/
300 get(argv[0]);
301 break;
303 return(0);
306 static void
307 getsocket(void)
309 if (my_s < 0) {
310 my_s = prog_socket(PF_ROUTE, SOCK_RAW, 0);
311 if (my_s < 0)
312 err(1, "socket");
316 #ifdef notdef
317 static struct sockaddr_in6 so_mask = {
318 .sin6_len = sizeof(so_mask),
319 .sin6_family = AF_INET6
321 #endif
322 static struct sockaddr_in6 blank_sin = {
323 .sin6_len = sizeof(blank_sin),
324 .sin6_family = AF_INET6
326 static struct sockaddr_in6 sin_m;
327 static struct sockaddr_dl blank_sdl = {
328 .sdl_len = sizeof(blank_sdl),
329 .sdl_family = AF_LINK,
331 static struct sockaddr_dl sdl_m;
332 static int expire_time, flags, found_entry;
333 static struct {
334 struct rt_msghdr m_rtm;
335 char m_space[512];
336 } m_rtmsg;
339 * Set an individual neighbor cache entry
341 static int
342 set(int argc, char **argv)
344 register struct sockaddr_in6 *mysin = &sin_m;
345 register struct sockaddr_dl *sdl;
346 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
347 struct addrinfo hints, *res;
348 int gai_error;
349 u_char *ea;
350 char *host = argv[0], *eaddr = argv[1];
352 getsocket();
353 argc -= 2;
354 argv += 2;
355 sdl_m = blank_sdl;
356 sin_m = blank_sin;
358 (void)memset(&hints, 0, sizeof(hints));
359 hints.ai_family = AF_INET6;
360 gai_error = getaddrinfo(host, NULL, &hints, &res);
361 if (gai_error) {
362 warnx("%s: %s", host, gai_strerror(gai_error));
363 return 1;
365 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
366 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
367 ea = (u_char *)LLADDR(&sdl_m);
368 if (ndp_ether_aton(eaddr, ea) == 0)
369 sdl_m.sdl_alen = 6;
370 flags = expire_time = 0;
371 while (argc-- > 0) {
372 if (strncmp(argv[0], "temp", 4) == 0) {
373 struct timeval tim;
375 (void)gettimeofday(&tim, 0);
376 expire_time = tim.tv_sec + 20 * 60;
377 } else if (strncmp(argv[0], "proxy", 5) == 0)
378 flags |= RTF_ANNOUNCE;
379 argv++;
381 if (rtmsg(RTM_GET) < 0) {
382 errx(1, "RTM_GET(%s) failed", host);
383 /* NOTREACHED */
385 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
386 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) + (char *)(void *)mysin);
387 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) {
388 if (sdl->sdl_family == AF_LINK &&
389 (rtm->rtm_flags & RTF_LLINFO) &&
390 !(rtm->rtm_flags & RTF_GATEWAY)) {
391 switch (sdl->sdl_type) {
392 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
393 case IFT_ISO88024: case IFT_ISO88025:
394 goto overwrite;
398 * IPv4 arp command retries with sin_other = SIN_PROXY here.
400 (void)fprintf(stderr, "set: cannot configure a new entry\n");
401 return 1;
404 overwrite:
405 if (sdl->sdl_family != AF_LINK) {
406 warnx("cannot intuit interface index and type for %s", host);
407 return (1);
409 sdl_m.sdl_type = sdl->sdl_type;
410 sdl_m.sdl_index = sdl->sdl_index;
411 return (rtmsg(RTM_ADD));
415 * Display an individual neighbor cache entry
417 static void
418 get(char *host)
420 struct sockaddr_in6 *mysin = &sin_m;
421 struct addrinfo hints, *res;
422 int gai_error;
424 sin_m = blank_sin;
425 (void)memset(&hints, 0, sizeof(hints));
426 hints.ai_family = AF_INET6;
427 gai_error = getaddrinfo(host, NULL, &hints, &res);
428 if (gai_error) {
429 warnx("%s: %s", host, gai_strerror(gai_error));
430 return;
432 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
433 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
434 dump(&mysin->sin6_addr, 0);
435 if (found_entry == 0) {
436 (void)getnameinfo((struct sockaddr *)(void *)mysin,
437 (socklen_t)mysin->sin6_len,
438 host_buf, sizeof(host_buf), NULL ,0,
439 (nflag ? NI_NUMERICHOST : 0));
440 errx(1, "%s (%s) -- no entry", host, host_buf);
445 * Delete a neighbor cache entry
447 static int
448 delete(char *host)
450 struct sockaddr_in6 *mysin = &sin_m;
451 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
452 struct sockaddr_dl *sdl;
453 struct addrinfo hints, *res;
454 int gai_error;
456 getsocket();
457 sin_m = blank_sin;
459 bzero(&hints, sizeof(hints));
460 hints.ai_family = AF_INET6;
461 gai_error = getaddrinfo(host, NULL, &hints, &res);
462 if (gai_error) {
463 warnx("%s: %s", host, gai_strerror(gai_error));
464 return 1;
466 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
467 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
468 if (rtmsg(RTM_GET) < 0)
469 errx(1, "RTM_GET(%s) failed", host);
470 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
471 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) +
472 (char *)(void *)mysin);
473 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) {
474 if (sdl->sdl_family == AF_LINK &&
475 (rtm->rtm_flags & RTF_LLINFO) &&
476 !(rtm->rtm_flags & RTF_GATEWAY)) {
477 goto delete;
480 * IPv4 arp command retries with sin_other = SIN_PROXY here.
482 warnx("delete: cannot delete non-NDP entry");
483 return 1;
486 delete:
487 if (sdl->sdl_family != AF_LINK) {
488 (void)printf("cannot locate %s\n", host);
489 return (1);
491 if (rtmsg(RTM_DELETE) == 0) {
492 struct sockaddr_in6 s6 = *mysin; /* XXX: for safety */
494 mysin->sin6_scope_id = 0;
495 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL);
496 (void)getnameinfo((struct sockaddr *)(void *)&s6,
497 (socklen_t)s6.sin6_len, host_buf,
498 sizeof(host_buf), NULL, 0,
499 (nflag ? NI_NUMERICHOST : 0));
500 (void)printf("%s (%s) deleted\n", host, host_buf);
503 return 0;
506 #define W_ADDR 36
507 #define W_LL 17
508 #define W_IF 6
511 * Dump the entire neighbor cache
513 static void
514 dump(struct in6_addr *addr, int cflag)
516 int mib[6];
517 size_t needed;
518 char *lim, *buf, *next;
519 struct rt_msghdr *rtm;
520 struct sockaddr_in6 *mysin;
521 struct sockaddr_dl *sdl;
522 struct in6_nbrinfo *nbi;
523 struct timeval tim;
524 int addrwidth;
525 int llwidth;
526 int ifwidth;
527 char flgbuf[8];
528 const char *ifname;
530 /* Print header */
531 if (!tflag && !cflag)
532 (void)printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
533 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
534 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
536 again:;
537 mib[0] = CTL_NET;
538 mib[1] = PF_ROUTE;
539 mib[2] = 0;
540 mib[3] = AF_INET6;
541 mib[4] = NET_RT_FLAGS;
542 mib[5] = RTF_LLINFO;
543 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
544 err(1, "sysctl(PF_ROUTE estimate)");
545 if (needed > 0) {
546 if ((buf = malloc(needed)) == NULL)
547 err(1, "malloc");
548 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
549 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
550 lim = buf + needed;
551 } else
552 buf = lim = NULL;
554 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
555 int isrouter = 0, prbs = 0;
557 rtm = (struct rt_msghdr *)(void *)next;
558 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
559 sdl = (struct sockaddr_dl *)(void *)((char *)(void *)mysin + RT_ROUNDUP(mysin->sin6_len));
562 * Some OSes can produce a route that has the LINK flag but
563 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
564 * and BSD/OS, where xx is not the interface identifier on
565 * lo0). Such routes entry would annoy getnbrinfo() below,
566 * so we skip them.
567 * XXX: such routes should have the GATEWAY flag, not the
568 * LINK flag. However, there is rotten routing software
569 * that advertises all routes that have the GATEWAY flag.
570 * Thus, KAME kernel intentionally does not set the LINK flag.
571 * What is to be fixed is not ndp, but such routing software
572 * (and the kernel workaround)...
574 if (sdl->sdl_family != AF_LINK)
575 continue;
577 if (!(rtm->rtm_flags & RTF_HOST))
578 continue;
580 if (addr) {
581 if (!IN6_ARE_ADDR_EQUAL(addr, &mysin->sin6_addr))
582 continue;
583 found_entry = 1;
584 } else if (IN6_IS_ADDR_MULTICAST(&mysin->sin6_addr))
585 continue;
586 if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr) ||
587 IN6_IS_ADDR_MC_LINKLOCAL(&mysin->sin6_addr)) {
588 uint16_t scopeid = mysin->sin6_scope_id;
589 inet6_getscopeid(mysin, INET6_IS_ADDR_LINKLOCAL|
590 INET6_IS_ADDR_MC_LINKLOCAL);
591 if (scopeid == 0)
592 mysin->sin6_scope_id = sdl->sdl_index;
594 (void)getnameinfo((struct sockaddr *)(void *)mysin,
595 (socklen_t)mysin->sin6_len,
596 host_buf, sizeof(host_buf), NULL, 0,
597 (nflag ? NI_NUMERICHOST : 0));
598 if (cflag) {
599 #ifdef RTF_WASCLONED
600 if (rtm->rtm_flags & RTF_WASCLONED)
601 (void)delete(host_buf);
602 #elif defined(RTF_CLONED)
603 if (rtm->rtm_flags & RTF_CLONED)
604 (void)delete(host_buf);
605 #else
606 (void)delete(host_buf);
607 #endif
608 continue;
610 (void)gettimeofday(&tim, 0);
611 if (tflag)
612 ts_print(&tim);
614 addrwidth = strlen(host_buf);
615 if (addrwidth < W_ADDR)
616 addrwidth = W_ADDR;
617 llwidth = strlen(ether_str(sdl));
618 if (W_ADDR + W_LL - addrwidth > llwidth)
619 llwidth = W_ADDR + W_LL - addrwidth;
620 ifname = if_indextoname((unsigned int)sdl->sdl_index, ifix_buf);
621 if (!ifname)
622 ifname = "?";
623 ifwidth = strlen(ifname);
624 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
625 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
627 (void)printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth,
628 host_buf, llwidth, llwidth, ether_str(sdl), ifwidth,
629 ifwidth, ifname);
631 /* Print neighbor discovery specific informations */
632 nbi = getnbrinfo(&mysin->sin6_addr,
633 (unsigned int)sdl->sdl_index, 1);
634 if (nbi) {
635 if (nbi->expire > tim.tv_sec) {
636 (void)printf(" %-9.9s",
637 sec2str(nbi->expire - tim.tv_sec));
638 } else if (nbi->expire == 0)
639 (void)printf(" %-9.9s", "permanent");
640 else
641 (void)printf(" %-9.9s", "expired");
643 switch (nbi->state) {
644 case ND6_LLINFO_NOSTATE:
645 (void)printf(" N");
646 break;
647 #ifdef ND6_LLINFO_WAITDELETE
648 case ND6_LLINFO_WAITDELETE:
649 (void)printf(" W");
650 break;
651 #endif
652 case ND6_LLINFO_INCOMPLETE:
653 (void)printf(" I");
654 break;
655 case ND6_LLINFO_REACHABLE:
656 (void)printf(" R");
657 break;
658 case ND6_LLINFO_STALE:
659 (void)printf(" S");
660 break;
661 case ND6_LLINFO_DELAY:
662 (void)printf(" D");
663 break;
664 case ND6_LLINFO_PROBE:
665 (void)printf(" P");
666 break;
667 default:
668 (void)printf(" ?");
669 break;
672 isrouter = nbi->isrouter;
673 prbs = nbi->asked;
674 } else {
675 warnx("failed to get neighbor information");
676 (void)printf(" ");
680 * other flags. R: router, P: proxy, W: ??
682 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
683 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s",
684 isrouter ? "R" : "",
685 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
686 } else {
687 mysin = (struct sockaddr_in6 *)(void *)
688 (sdl->sdl_len + (char *)(void *)sdl);
689 #if 0 /* W and P are mystery even for us */
690 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
691 isrouter ? "R" : "",
692 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
693 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
694 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
695 #else
696 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s",
697 isrouter ? "R" : "",
698 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
699 #endif
701 (void)printf(" %s", flgbuf);
703 if (prbs)
704 (void)printf(" %d", prbs);
706 (void)printf("\n");
708 if (buf != NULL)
709 free(buf);
711 if (repeat) {
712 (void)printf("\n");
713 (void)fflush(stdout);
714 (void)sleep(repeat);
715 goto again;
719 static struct in6_nbrinfo *
720 getnbrinfo(struct in6_addr *addr, unsigned int ifindex, int warning)
722 static struct in6_nbrinfo nbi;
723 int s;
725 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
726 err(1, "socket");
728 (void)memset(&nbi, 0, sizeof(nbi));
729 (void)if_indextoname(ifindex, nbi.ifname);
730 nbi.addr = *addr;
731 if (prog_ioctl(s, SIOCGNBRINFO_IN6, &nbi) < 0) {
732 if (warning)
733 warn("ioctl(SIOCGNBRINFO_IN6)");
734 (void)prog_close(s);
735 return(NULL);
738 (void)prog_close(s);
739 return(&nbi);
742 static char *
743 ether_str(struct sockaddr_dl *sdl)
745 static char hbuf[NI_MAXHOST];
747 if (sdl->sdl_alen) {
748 if (getnameinfo((struct sockaddr *)(void *)sdl,
749 (socklen_t)sdl->sdl_len,
750 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
751 (void)snprintf(hbuf, sizeof(hbuf), "<invalid>");
752 } else
753 (void)snprintf(hbuf, sizeof(hbuf), "(incomplete)");
755 return(hbuf);
758 static int
759 ndp_ether_aton(char *a, u_char *n)
761 int i, o[6];
763 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
764 &o[3], &o[4], &o[5]);
765 if (i != 6) {
766 warnx("invalid Ethernet address '%s'", a);
767 return (1);
769 for (i = 0; i < 6; i++)
770 n[i] = o[i];
771 return (0);
774 static void
775 usage(void)
777 const char *pn = getprogname();
779 (void)fprintf(stderr, "Usage: %s [-nt] hostname\n", pn);
780 (void)fprintf(stderr,
781 " %s [-nt] -a | -c | -p | -r | -H | -P | -R\n", pn);
782 (void)fprintf(stderr, " %s [-nt] -A wait\n", pn);
783 (void)fprintf(stderr, " %s [-nt] -d hostname\n", pn);
784 (void)fprintf(stderr, " %s [-nt] -f filename\n", pn);
785 (void)fprintf(stderr, " %s [-nt] -i interface [flags...]\n", pn);
786 #ifdef SIOCSDEFIFACE_IN6
787 (void)fprintf(stderr, " %s [-nt] -I [interface|delete]\n", pn);
788 #endif
789 (void)fprintf(stderr,
790 " %s [-nt] -s nodename etheraddr [temp] [proxy]\n", pn);
791 exit(1);
794 static int
795 rtmsg(int cmd)
797 static int seq;
798 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
799 register char *cp = m_rtmsg.m_space;
800 register int l;
802 errno = 0;
803 if (cmd == RTM_DELETE)
804 goto doit;
805 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
806 rtm->rtm_flags = flags;
807 rtm->rtm_version = RTM_VERSION;
809 switch (cmd) {
810 default:
811 errx(1, "internal wrong cmd");
812 /*NOTREACHED*/
813 case RTM_ADD:
814 rtm->rtm_addrs |= RTA_GATEWAY;
815 if (expire_time) {
816 rtm->rtm_rmx.rmx_expire = expire_time;
817 rtm->rtm_inits = RTV_EXPIRE;
819 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
820 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */
821 if (rtm->rtm_flags & RTF_ANNOUNCE) {
822 rtm->rtm_flags &= ~RTF_HOST;
823 rtm->rtm_addrs |= RTA_NETMASK;
825 #endif
826 /* FALLTHROUGH */
827 case RTM_GET:
828 rtm->rtm_addrs |= RTA_DST;
830 #define NEXTADDR(w, s) \
831 if (rtm->rtm_addrs & (w)) { \
832 (void)memcpy(cp, &s, sizeof(s)); \
833 RT_ADVANCE(cp, (struct sockaddr *)(void *)&s); \
836 NEXTADDR(RTA_DST, sin_m);
837 NEXTADDR(RTA_GATEWAY, sdl_m);
838 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */
839 (void)memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
840 NEXTADDR(RTA_NETMASK, so_mask);
841 #endif
843 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
844 doit:
845 l = rtm->rtm_msglen;
846 rtm->rtm_seq = ++seq;
847 rtm->rtm_type = cmd;
848 #ifdef __minix
850 * Borrow from the future by setting the "this is a link-local request"
851 * flag on all routing socket requests. IMPORTANT: this change may be
852 * dropped with the resync to NetBSD 8 as it will do the same thing,
853 * although slightly differently (and hence may not create a conflict).
855 rtm->rtm_flags |= RTF_LLDATA;
856 #endif /* __minix */
857 if (prog_write(my_s, &m_rtmsg, (size_t)l) == -1) {
858 if (errno != ESRCH || cmd != RTM_DELETE)
859 err(1, "writing to routing socket");
861 do {
862 l = prog_read(my_s, &m_rtmsg, sizeof(m_rtmsg));
863 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
864 if (l < 0)
865 warn("read from routing socket");
866 return (0);
869 static void
870 ifinfo(char *ifname, int argc, char **argv)
872 struct in6_ndireq nd;
873 int i, s;
874 u_int32_t newflags;
875 #ifdef IPV6CTL_USETEMPADDR
876 u_int8_t nullbuf[8];
877 #endif
879 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
880 err(1, "socket");
881 (void)memset(&nd, 0, sizeof(nd));
882 (void)strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
883 if (prog_ioctl(s, SIOCGIFINFO_IN6, &nd) < 0)
884 err(1, "ioctl(SIOCGIFINFO_IN6)");
885 #define ND nd.ndi
886 newflags = ND.flags;
887 for (i = 0; i < argc; i++) {
888 int clear = 0;
889 char *cp = argv[i];
891 if (*cp == '-') {
892 clear = 1;
893 cp++;
896 #define SETFLAG(s, f) \
897 do {\
898 if (strcmp(cp, (s)) == 0) {\
899 if (clear)\
900 newflags &= ~(f);\
901 else\
902 newflags |= (f);\
904 } while (/*CONSTCOND*/0)
906 * XXX: this macro is not 100% correct, in that it matches "nud" against
907 * "nudbogus". But we just let it go since this is minor.
909 #define SETVALUE(f, v) \
910 do { \
911 char *valptr; \
912 unsigned long newval; \
913 v = 0; /* unspecified */ \
914 if (strncmp(cp, f, strlen(f)) == 0) { \
915 valptr = strchr(cp, '='); \
916 if (valptr == NULL) \
917 err(1, "syntax error in %s field", (f)); \
918 errno = 0; \
919 newval = strtoul(++valptr, NULL, 0); \
920 if (errno) \
921 err(1, "syntax error in %s's value", (f)); \
922 v = newval; \
924 } while (/*CONSTCOND*/0)
926 #ifdef ND6_IFF_IFDISABLED
927 SETFLAG("disabled", ND6_IFF_IFDISABLED);
928 #endif
929 SETFLAG("nud", ND6_IFF_PERFORMNUD);
930 #ifdef ND6_IFF_ACCEPT_RTADV
931 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
932 #endif
933 #ifdef ND6_IFF_OVERRIDE_RTADV
934 SETFLAG("override_rtadv", ND6_IFF_OVERRIDE_RTADV);
935 #endif
936 #ifdef ND6_IFF_AUTO_LINKLOCAL
937 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
938 #endif
939 #ifdef ND6_IFF_PREFER_SOURCE
940 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
941 #endif
942 #ifdef ND6_IFF_DONT_SET_IFROUTE
943 SETFLAG("dont_set_ifroute", ND6_IFF_DONT_SET_IFROUTE);
944 #endif
945 SETVALUE("basereachable", ND.basereachable);
946 SETVALUE("retrans", ND.retrans);
947 SETVALUE("curhlim", ND.chlim);
949 ND.flags = newflags;
950 #ifdef SIOCSIFINFO_IN6
951 if (prog_ioctl(s, SIOCSIFINFO_IN6, &nd) < 0)
952 err(1, "ioctl(SIOCSIFINFO_IN6)");
953 #else
954 if (prog_ioctl(s, SIOCSIFINFO_FLAGS, &nd) < 0)
955 err(1, "ioctl(SIOCSIFINFO_FLAGS)");
956 #endif
957 #undef SETFLAG
958 #undef SETVALUE
961 if (!ND.initialized)
962 errx(1, "%s: not initialized yet", ifname);
964 if (prog_ioctl(s, SIOCGIFINFO_IN6, &nd) < 0)
965 err(1, "ioctl(SIOCGIFINFO_IN6)");
966 (void)printf("linkmtu=%d", ND.linkmtu);
967 (void)printf(", maxmtu=%d", ND.maxmtu);
968 (void)printf(", curhlim=%d", ND.chlim);
969 (void)printf(", basereachable=%ds%dms",
970 ND.basereachable / 1000, ND.basereachable % 1000);
971 (void)printf(", reachable=%ds", ND.reachable);
972 (void)printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
973 #ifdef IPV6CTL_USETEMPADDR
974 (void)memset(nullbuf, 0, sizeof(nullbuf));
975 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
976 int j;
977 u_int8_t *rbuf;
979 for (i = 0; i < 3; i++) {
980 switch (i) {
981 case 0:
982 (void)printf("\nRandom seed(0): ");
983 rbuf = ND.randomseed0;
984 break;
985 case 1:
986 (void)printf("\nRandom seed(1): ");
987 rbuf = ND.randomseed1;
988 break;
989 case 2:
990 (void)printf("\nRandom ID: ");
991 rbuf = ND.randomid;
992 break;
993 default:
994 errx(1, "impossible case for tempaddr display");
996 for (j = 0; j < 8; j++)
997 (void)printf("%02x", rbuf[j]);
1000 #endif
1001 if (ND.flags) {
1002 (void)printf("\nFlags: ");
1003 if ((ND.flags & ND6_IFF_PERFORMNUD))
1004 (void)printf("nud ");
1005 #ifdef ND6_IFF_IFDISABLED
1006 if ((ND.flags & ND6_IFF_IFDISABLED))
1007 (void)printf("disabled ");
1008 #endif
1009 #ifdef ND6_IFF_ACCEPT_RTADV
1010 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1011 (void)printf("accept_rtadv ");
1012 #endif
1013 #ifdef ND6_IFF_OVERRIDE_RTADV
1014 if ((ND.flags & ND6_IFF_OVERRIDE_RTADV))
1015 (void)printf("override_rtadv ");
1016 #endif
1017 #ifdef ND6_IFF_AUTO_LINKLOCAL
1018 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1019 (void)printf("auto_linklocal ");
1020 #endif
1021 #ifdef ND6_IFF_PREFER_SOURCE
1022 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1023 (void)printf("prefer_source ");
1024 #endif
1026 (void)putc('\n', stdout);
1027 #undef ND
1029 (void)prog_close(s);
1032 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1033 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1034 #endif
1036 static void
1037 rtrlist(void)
1039 #ifdef ICMPV6CTL_ND6_DRLIST
1040 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1041 char *buf;
1042 struct in6_defrouter *p, *ep;
1043 size_t l;
1044 struct timeval tim;
1046 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1047 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1048 /*NOTREACHED*/
1050 if (l == 0)
1051 return;
1052 buf = malloc(l);
1053 if (!buf) {
1054 err(1, "malloc");
1055 /*NOTREACHED*/
1057 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1058 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1059 /*NOTREACHED*/
1062 ep = (struct in6_defrouter *)(void *)(buf + l);
1063 for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) {
1064 int rtpref;
1066 if (getnameinfo((struct sockaddr *)(void *)&p->rtaddr,
1067 (socklen_t)p->rtaddr.sin6_len, host_buf, sizeof(host_buf),
1068 NULL, 0, (nflag ? NI_NUMERICHOST : 0)) != 0)
1069 (void)strlcpy(host_buf, "?", sizeof(host_buf));
1071 (void)printf("%s if=%s", host_buf,
1072 if_indextoname((unsigned int)p->if_index, ifix_buf));
1073 (void)printf(", flags=%s%s",
1074 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1075 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1076 rtpref = ((uint32_t)(p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1077 (void)printf(", pref=%s", rtpref_str[rtpref]);
1079 (void)gettimeofday(&tim, 0);
1080 if (p->expire == 0)
1081 (void)printf(", expire=Never\n");
1082 else
1083 (void)printf(", expire=%s\n",
1084 sec2str((time_t)(p->expire - tim.tv_sec)));
1086 free(buf);
1087 #else
1088 struct in6_drlist dr;
1089 int s, i;
1090 struct timeval time;
1092 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1093 err(1, "socket");
1094 /* NOTREACHED */
1096 (void)memset(&dr, 0, sizeof(dr));
1097 (void)strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1098 if (prog_ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1099 err(1, "ioctl(SIOCGDRLST_IN6)");
1100 /* NOTREACHED */
1102 #define DR dr.defrouter[i]
1103 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1104 struct sockaddr_in6 sin6;
1106 (void)memset(&sin6, 0, sizeof(sin6));
1107 sin6.sin6_family = AF_INET6;
1108 sin6.sin6_len = sizeof(sin6);
1109 sin6.sin6_addr = DR.rtaddr;
1110 (void)getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1111 host_buf, sizeof(host_buf), NULL, 0,
1112 (nflag ? NI_NUMERICHOST : 0));
1114 (void)printf("%s if=%s", host_buf,
1115 if_indextoname(DR.if_index, ifix_buf));
1116 (void)printf(", flags=%s%s",
1117 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1118 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1119 gettimeofday(&time, 0);
1120 if (DR.expire == 0)
1121 (void)printf(", expire=Never\n");
1122 else
1123 (void)printf(", expire=%s\n",
1124 sec2str(DR.expire - time.tv_sec));
1126 #undef DR
1127 (void)prog_close(s);
1128 #endif
1131 static void
1132 plist(void)
1134 #ifdef ICMPV6CTL_ND6_PRLIST
1135 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1136 char *buf, *p, *ep;
1137 struct in6_prefix pfx;
1138 size_t l;
1139 struct timeval tim;
1140 const int niflags = NI_NUMERICHOST;
1141 int ninflags = nflag ? NI_NUMERICHOST : 0;
1142 char namebuf[NI_MAXHOST];
1144 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1145 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1146 /*NOTREACHED*/
1148 buf = malloc(l);
1149 if (!buf) {
1150 err(1, "malloc");
1151 /*NOTREACHED*/
1153 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1154 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1155 /*NOTREACHED*/
1158 ep = buf + l;
1159 for (p = buf; p < ep; ) {
1160 memcpy(&pfx, p, sizeof(pfx));
1161 p += sizeof(pfx);
1163 if (getnameinfo((struct sockaddr*)&pfx.prefix,
1164 (socklen_t)pfx.prefix.sin6_len, namebuf, sizeof(namebuf),
1165 NULL, 0, niflags) != 0)
1166 (void)strlcpy(namebuf, "?", sizeof(namebuf));
1167 (void)printf("%s/%d if=%s\n", namebuf, pfx.prefixlen,
1168 if_indextoname((unsigned int)pfx.if_index, ifix_buf));
1170 (void)gettimeofday(&tim, 0);
1172 * meaning of fields, especially flags, is very different
1173 * by origin. notify the difference to the users.
1175 (void)printf("flags=%s%s%s%s%s",
1176 pfx.raflags.onlink ? "L" : "",
1177 pfx.raflags.autonomous ? "A" : "",
1178 (pfx.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1179 (pfx.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1180 #ifdef NDPRF_HOME
1181 (pfx.flags & NDPRF_HOME) != 0 ? "H" : ""
1182 #else
1184 #endif
1186 if (pfx.vltime == ND6_INFINITE_LIFETIME)
1187 (void)printf(" vltime=infinity");
1188 else
1189 (void)printf(" vltime=%lu", (unsigned long)pfx.vltime);
1190 if (pfx.pltime == ND6_INFINITE_LIFETIME)
1191 (void)printf(", pltime=infinity");
1192 else
1193 (void)printf(", pltime=%lu", (unsigned long)pfx.pltime);
1194 if (pfx.expire == 0)
1195 (void)printf(", expire=Never");
1196 else if (pfx.expire >= tim.tv_sec)
1197 (void)printf(", expire=%s",
1198 sec2str(pfx.expire - tim.tv_sec));
1199 else
1200 (void)printf(", expired");
1201 (void)printf(", ref=%d", pfx.refcnt);
1202 (void)printf("\n");
1204 * "advertising router" list is meaningful only if the prefix
1205 * information is from RA.
1207 if (pfx.advrtrs) {
1208 int j;
1209 struct sockaddr_in6 sin6;
1211 (void)printf(" advertised by\n");
1212 for (j = 0; j < pfx.advrtrs && p <= ep; j++) {
1213 struct in6_nbrinfo *nbi;
1215 memcpy(&sin6, p, sizeof(sin6));
1216 p += sizeof(sin6);
1218 if (getnameinfo((struct sockaddr *)&sin6,
1219 (socklen_t)sin6.sin6_len, namebuf,
1220 sizeof(namebuf), NULL, 0, ninflags) != 0)
1221 (void)strlcpy(namebuf, "?", sizeof(namebuf));
1222 (void)printf(" %s", namebuf);
1224 nbi = getnbrinfo(&sin6.sin6_addr,
1225 (unsigned int)pfx.if_index, 0);
1226 if (nbi) {
1227 switch (nbi->state) {
1228 case ND6_LLINFO_REACHABLE:
1229 case ND6_LLINFO_STALE:
1230 case ND6_LLINFO_DELAY:
1231 case ND6_LLINFO_PROBE:
1232 (void)printf(" (reachable)\n");
1233 break;
1234 default:
1235 (void)printf(" (unreachable)\n");
1237 } else
1238 (void)printf(" (no neighbor state)\n");
1240 } else
1241 (void)printf(" No advertising router\n");
1243 free(buf);
1244 #else
1245 struct in6_prlist pr;
1246 int s, i;
1247 struct timeval time;
1249 (void)gettimeofday(&time, 0);
1251 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1252 err(1, "socket");
1253 /* NOTREACHED */
1255 (void)memset(&pr, 0, sizeof(pr));
1256 (void)strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1257 if (prog_ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1258 err(1, "ioctl(SIOCGPRLST_IN6)");
1259 /* NOTREACHED */
1261 #define PR pr.prefix[i]
1262 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1263 struct sockaddr_in6 p6;
1264 char namebuf[NI_MAXHOST];
1265 int niflags;
1267 #ifdef NDPRF_ONLINK
1268 p6 = PR.prefix;
1269 #else
1270 (void)memset(&p6, 0, sizeof(p6));
1271 p6.sin6_family = AF_INET6;
1272 p6.sin6_len = sizeof(p6);
1273 p6.sin6_addr = PR.prefix;
1274 #endif
1276 niflags = NI_NUMERICHOST;
1277 if (getnameinfo((struct sockaddr *)&p6,
1278 sizeof(p6), namebuf, sizeof(namebuf),
1279 NULL, 0, niflags)) {
1280 warnx("getnameinfo failed");
1281 continue;
1283 (void)printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1284 if_indextoname(PR.if_index, ifix_buf));
1286 (void)gettimeofday(&time, 0);
1288 * meaning of fields, especially flags, is very different
1289 * by origin. notify the difference to the users.
1291 #if 0
1292 (void)printf(" %s",
1293 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1294 #endif
1295 #ifdef NDPRF_ONLINK
1296 (void)printf("flags=%s%s%s%s%s",
1297 PR.raflags.onlink ? "L" : "",
1298 PR.raflags.autonomous ? "A" : "",
1299 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1300 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1301 #ifdef NDPRF_HOME
1302 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1303 #else
1305 #endif
1307 #else
1308 (void)printf("flags=%s%s",
1309 PR.raflags.onlink ? "L" : "",
1310 PR.raflags.autonomous ? "A" : "");
1311 #endif
1312 if (PR.vltime == ND6_INFINITE_LIFETIME)
1313 (void)printf(" vltime=infinity");
1314 else
1315 (void)printf(" vltime=%lu", PR.vltime);
1316 if (PR.pltime == ND6_INFINITE_LIFETIME)
1317 (void)printf(", pltime=infinity");
1318 else
1319 (void)printf(", pltime=%lu", PR.pltime);
1320 if (PR.expire == 0)
1321 (void)printf(", expire=Never");
1322 else if (PR.expire >= time.tv_sec)
1323 (void)printf(", expire=%s",
1324 sec2str(PR.expire - time.tv_sec));
1325 else
1326 (void)printf(", expired");
1327 #ifdef NDPRF_ONLINK
1328 (void)printf(", ref=%d", PR.refcnt);
1329 #endif
1330 #if 0
1331 switch (PR.origin) {
1332 case PR_ORIG_RA:
1333 (void)printf(", origin=RA");
1334 break;
1335 case PR_ORIG_RR:
1336 (void)printf(", origin=RR");
1337 break;
1338 case PR_ORIG_STATIC:
1339 (void)printf(", origin=static");
1340 break;
1341 case PR_ORIG_KERNEL:
1342 (void)printf(", origin=kernel");
1343 break;
1344 default:
1345 (void)printf(", origin=?");
1346 break;
1348 #endif
1349 (void)printf("\n");
1351 * "advertising router" list is meaningful only if the prefix
1352 * information is from RA.
1354 if (0 && /* prefix origin is almost obsolted */
1355 PR.origin != PR_ORIG_RA)
1357 else if (PR.advrtrs) {
1358 int j;
1359 (void)printf(" advertised by\n");
1360 for (j = 0; j < PR.advrtrs; j++) {
1361 struct sockaddr_in6 sin6;
1362 struct in6_nbrinfo *nbi;
1364 bzero(&sin6, sizeof(sin6));
1365 sin6.sin6_family = AF_INET6;
1366 sin6.sin6_len = sizeof(sin6);
1367 sin6.sin6_addr = PR.advrtr[j];
1368 sin6.sin6_scope_id = PR.if_index; /* XXX */
1369 (void)getnameinfo((struct sockaddr *)&sin6,
1370 sin6.sin6_len, host_buf,
1371 sizeof(host_buf), NULL, 0,
1372 (nflag ? NI_NUMERICHOST : 0));
1373 (void)printf(" %s", host_buf);
1375 nbi = getnbrinfo(&sin6.sin6_addr,
1376 PR.if_index, 0);
1377 if (nbi) {
1378 switch (nbi->state) {
1379 case ND6_LLINFO_REACHABLE:
1380 case ND6_LLINFO_STALE:
1381 case ND6_LLINFO_DELAY:
1382 case ND6_LLINFO_PROBE:
1383 (void)printf(" (reachable)\n");
1384 break;
1385 default:
1386 (void)printf(" (unreachable)\n");
1388 } else
1389 (void)printf(" (no neighbor state)\n");
1391 if (PR.advrtrs > DRLSTSIZ)
1392 (void)printf(" and %d routers\n",
1393 PR.advrtrs - DRLSTSIZ);
1394 } else
1395 (void)printf(" No advertising router\n");
1397 #undef PR
1398 (void)prog_close(s);
1399 #endif
1402 static void
1403 pfx_flush(void)
1405 char dummyif[IFNAMSIZ+8];
1406 int s;
1408 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1409 err(1, "socket");
1410 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1411 if (prog_ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1412 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1413 (void)prog_close(s);
1416 static void
1417 rtr_flush(void)
1419 char dummyif[IFNAMSIZ+8];
1420 int s;
1422 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1423 err(1, "socket");
1424 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1425 if (prog_ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1426 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1428 (void)prog_close(s);
1431 static void
1432 harmonize_rtr(void)
1434 char dummyif[IFNAMSIZ+8];
1435 int s;
1437 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1438 err(1, "socket");
1439 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1440 if (prog_ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1441 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1443 (void)prog_close(s);
1446 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1447 static void
1448 setdefif(char *ifname)
1450 struct in6_ndifreq ndifreq;
1451 unsigned int ifindex;
1452 int s;
1454 if (strcasecmp(ifname, "delete") == 0)
1455 ifindex = 0;
1456 else {
1457 if ((ifindex = if_nametoindex(ifname)) == 0)
1458 err(1, "failed to resolve i/f index for %s", ifname);
1461 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1462 err(1, "socket");
1464 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1465 ndifreq.ifindex = ifindex;
1467 if (prog_ioctl(s, SIOCSDEFIFACE_IN6, &ndifreq) < 0)
1468 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1470 (void)prog_close(s);
1473 static void
1474 getdefif(void)
1476 struct in6_ndifreq ndifreq;
1477 char ifname[IFNAMSIZ+8];
1478 int s;
1480 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1481 err(1, "socket");
1483 (void)memset(&ndifreq, 0, sizeof(ndifreq));
1484 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1486 if (prog_ioctl(s, SIOCGDEFIFACE_IN6, &ndifreq) < 0)
1487 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1489 if (ndifreq.ifindex == 0)
1490 (void)printf("No default interface.\n");
1491 else {
1492 if ((if_indextoname((unsigned int)ndifreq.ifindex, ifname)) == NULL)
1493 err(1, "failed to resolve ifname for index %lu",
1494 ndifreq.ifindex);
1495 (void)printf("ND default interface = %s\n", ifname);
1498 (void)prog_close(s);
1500 #endif
1502 static const char *
1503 sec2str(time_t total)
1505 static char result[256];
1506 int days, hours, mins, secs;
1507 int first = 1;
1508 char *p = result;
1509 char *ep = &result[sizeof(result)];
1510 int n;
1512 days = total / 3600 / 24;
1513 hours = (total / 3600) % 24;
1514 mins = (total / 60) % 60;
1515 secs = total % 60;
1517 if (days) {
1518 first = 0;
1519 n = snprintf(p, (size_t)(ep - p), "%dd", days);
1520 if (n < 0 || n >= ep - p)
1521 return "?";
1522 p += n;
1524 if (!first || hours) {
1525 first = 0;
1526 n = snprintf(p, (size_t)(ep - p), "%dh", hours);
1527 if (n < 0 || n >= ep - p)
1528 return "?";
1529 p += n;
1531 if (!first || mins) {
1532 first = 0;
1533 n = snprintf(p, (size_t)(ep - p), "%dm", mins);
1534 if (n < 0 || n >= ep - p)
1535 return "?";
1536 p += n;
1538 (void)snprintf(p, (size_t)(ep - p), "%ds", secs);
1540 return(result);
1544 * Print the timestamp
1545 * from tcpdump/util.c
1547 static void
1548 ts_print(const struct timeval *tvp)
1550 int s;
1552 /* Default */
1553 s = (tvp->tv_sec + thiszone) % 86400;
1554 (void)printf("%02d:%02d:%02d.%06u ",
1555 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);