No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / ndp / ndp.c
blobdf9201941630c5fc3cc3eb361291cc2a6c2433e2
1 /* $NetBSD: ndp.c,v 1.37 2007/01/16 17:32:04 hubertf 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>
107 #include "gmt2local.h"
109 /* packing rule for routing socket */
110 #define ROUNDUP(a) \
111 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
112 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
114 static pid_t pid;
115 static int nflag;
116 static int tflag;
117 static int32_t thiszone; /* time difference with gmt */
118 static int my_s = -1;
119 static unsigned int repeat = 0;
122 static char host_buf[NI_MAXHOST]; /* getnameinfo() */
123 static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
125 static void getsocket(void);
126 static int set(int, char **);
127 static void get(char *);
128 static int delete(char *);
129 static void dump(struct in6_addr *, int);
130 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, unsigned int, int);
131 static char *ether_str(struct sockaddr_dl *);
132 static int ndp_ether_aton(char *, u_char *);
133 static void usage(void);
134 static int rtmsg(int);
135 static void ifinfo(char *, int, char **);
136 static void rtrlist(void);
137 static void plist(void);
138 static void pfx_flush(void);
139 static void rtrlist(void);
140 static void rtr_flush(void);
141 static void harmonize_rtr(void);
142 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
143 static void getdefif(void);
144 static void setdefif(char *);
145 #endif
146 static const char *sec2str(time_t);
147 static char *ether_str(struct sockaddr_dl *);
148 static void ts_print(const struct timeval *);
150 #ifdef ICMPV6CTL_ND6_DRLIST
151 static const char *rtpref_str[] = {
152 "medium", /* 00 */
153 "high", /* 01 */
154 "rsv", /* 10 */
155 "low" /* 11 */
157 #endif
159 static int mode = 0;
160 static char *arg = NULL;
163 main(int argc, char **argv)
165 int ch;
167 pid = getpid();
168 thiszone = gmt2local(0L);
169 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
170 switch (ch) {
171 case 'a':
172 case 'c':
173 case 'p':
174 case 'r':
175 case 'H':
176 case 'P':
177 case 'R':
178 case 's':
179 case 'I':
180 if (mode) {
181 usage();
182 /*NOTREACHED*/
184 mode = ch;
185 arg = NULL;
186 break;
187 case 'd':
188 case 'f':
189 case 'i' :
190 if (mode) {
191 usage();
192 /*NOTREACHED*/
194 mode = ch;
195 arg = optarg;
196 break;
197 case 'n':
198 nflag = 1;
199 break;
200 case 't':
201 tflag = 1;
202 break;
203 case 'A':
204 if (mode) {
205 usage();
206 /*NOTREACHED*/
208 mode = 'a';
209 repeat = atoi(optarg);
210 break;
211 default:
212 usage();
215 argc -= optind;
216 argv += optind;
218 switch (mode) {
219 case 'a':
220 case 'c':
221 if (argc != 0) {
222 usage();
223 /*NOTREACHED*/
225 dump(0, mode == 'c');
226 break;
227 case 'd':
228 if (argc != 0) {
229 usage();
230 /*NOTREACHED*/
232 (void)delete(arg);
233 break;
234 case 'I':
235 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
236 if (argc > 1) {
237 usage();
238 /*NOTREACHED*/
239 } else if (argc == 1) {
240 if (strcmp(*argv, "delete") == 0 ||
241 if_nametoindex(*argv))
242 setdefif(*argv);
243 else
244 errx(1, "invalid interface %s", *argv);
246 getdefif(); /* always call it to print the result */
247 break;
248 #else
249 errx(1, "not supported yet");
250 /*NOTREACHED*/
251 #endif
252 case 'p':
253 if (argc != 0) {
254 usage();
255 /*NOTREACHED*/
257 plist();
258 break;
259 case 'i':
260 ifinfo(arg, argc, argv);
261 break;
262 case 'r':
263 if (argc != 0) {
264 usage();
265 /*NOTREACHED*/
267 rtrlist();
268 break;
269 case 's':
270 if (argc < 2 || argc > 4)
271 usage();
272 return(set(argc, argv) ? 1 : 0);
273 case 'H':
274 if (argc != 0) {
275 usage();
276 /*NOTREACHED*/
278 harmonize_rtr();
279 break;
280 case 'P':
281 if (argc != 0) {
282 usage();
283 /*NOTREACHED*/
285 pfx_flush();
286 break;
287 case 'R':
288 if (argc != 0) {
289 usage();
290 /*NOTREACHED*/
292 rtr_flush();
293 break;
294 case 0:
295 if (argc != 1) {
296 usage();
297 /*NOTREACHED*/
299 get(argv[0]);
300 break;
302 return(0);
305 static void
306 getsocket(void)
308 if (my_s < 0) {
309 my_s = socket(PF_ROUTE, SOCK_RAW, 0);
310 if (my_s < 0)
311 err(1, "socket");
315 #ifdef notdef
316 static struct sockaddr_in6 so_mask = {
317 .sin6_len = sizeof(so_mask),
318 .sin6_family = AF_INET6
320 #endif
321 static struct sockaddr_in6 blank_sin = {
322 .sin6_len = sizeof(blank_sin),
323 .sin6_family = AF_INET6
325 static struct sockaddr_in6 sin_m;
326 static struct sockaddr_dl blank_sdl = {
327 .sdl_len = sizeof(blank_sdl),
328 .sdl_family = AF_LINK,
330 static struct sockaddr_dl sdl_m;
331 static int expire_time, flags, found_entry;
332 static struct {
333 struct rt_msghdr m_rtm;
334 char m_space[512];
335 } m_rtmsg;
338 * Set an individual neighbor cache entry
340 static int
341 set(int argc, char **argv)
343 register struct sockaddr_in6 *mysin = &sin_m;
344 register struct sockaddr_dl *sdl;
345 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
346 struct addrinfo hints, *res;
347 int gai_error;
348 u_char *ea;
349 char *host = argv[0], *eaddr = argv[1];
351 getsocket();
352 argc -= 2;
353 argv += 2;
354 sdl_m = blank_sdl;
355 sin_m = blank_sin;
357 (void)memset(&hints, 0, sizeof(hints));
358 hints.ai_family = AF_INET6;
359 gai_error = getaddrinfo(host, NULL, &hints, &res);
360 if (gai_error) {
361 warnx("%s: %s\n", host, gai_strerror(gai_error));
362 return 1;
364 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
365 #ifdef __KAME__
366 if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr)) {
367 *(u_int16_t *)(void *)&mysin->sin6_addr.s6_addr[2] =
368 htons(((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_scope_id);
370 #endif
371 ea = (u_char *)LLADDR(&sdl_m);
372 if (ndp_ether_aton(eaddr, ea) == 0)
373 sdl_m.sdl_alen = 6;
374 flags = expire_time = 0;
375 while (argc-- > 0) {
376 if (strncmp(argv[0], "temp", 4) == 0) {
377 struct timeval tim;
379 (void)gettimeofday(&tim, 0);
380 expire_time = tim.tv_sec + 20 * 60;
381 } else if (strncmp(argv[0], "proxy", 5) == 0)
382 flags |= RTF_ANNOUNCE;
383 argv++;
385 if (rtmsg(RTM_GET) < 0) {
386 errx(1, "RTM_GET(%s) failed", host);
387 /* NOTREACHED */
389 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
390 sdl = (struct sockaddr_dl *)(void *)(ROUNDUP(mysin->sin6_len) + (char *)(void *)mysin);
391 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) {
392 if (sdl->sdl_family == AF_LINK &&
393 (rtm->rtm_flags & RTF_LLINFO) &&
394 !(rtm->rtm_flags & RTF_GATEWAY)) {
395 switch (sdl->sdl_type) {
396 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
397 case IFT_ISO88024: case IFT_ISO88025:
398 goto overwrite;
402 * IPv4 arp command retries with sin_other = SIN_PROXY here.
404 (void)fprintf(stderr, "set: cannot configure a new entry\n");
405 return 1;
408 overwrite:
409 if (sdl->sdl_family != AF_LINK) {
410 warnx("cannot intuit interface index and type for %s", host);
411 return (1);
413 sdl_m.sdl_type = sdl->sdl_type;
414 sdl_m.sdl_index = sdl->sdl_index;
415 return (rtmsg(RTM_ADD));
419 * Display an individual neighbor cache entry
421 static void
422 get(char *host)
424 struct sockaddr_in6 *mysin = &sin_m;
425 struct addrinfo hints, *res;
426 int gai_error;
428 sin_m = blank_sin;
429 (void)memset(&hints, 0, sizeof(hints));
430 hints.ai_family = AF_INET6;
431 gai_error = getaddrinfo(host, NULL, &hints, &res);
432 if (gai_error) {
433 warnx("%s: %s\n", host, gai_strerror(gai_error));
434 return;
436 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
437 #ifdef __KAME__
438 if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr)) {
439 *(u_int16_t *)(void *)&mysin->sin6_addr.s6_addr[2] =
440 htons(((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_scope_id);
442 #endif
443 dump(&mysin->sin6_addr, 0);
444 if (found_entry == 0) {
445 (void)getnameinfo((struct sockaddr *)(void *)mysin,
446 (socklen_t)mysin->sin6_len,
447 host_buf, sizeof(host_buf), NULL ,0,
448 (nflag ? NI_NUMERICHOST : 0));
449 errx(1, "%s (%s) -- no entry", host, host_buf);
454 * Delete a neighbor cache entry
456 static int
457 delete(char *host)
459 struct sockaddr_in6 *mysin = &sin_m;
460 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
461 struct sockaddr_dl *sdl;
462 struct addrinfo hints, *res;
463 int gai_error;
465 getsocket();
466 sin_m = blank_sin;
468 bzero(&hints, sizeof(hints));
469 hints.ai_family = AF_INET6;
470 gai_error = getaddrinfo(host, NULL, &hints, &res);
471 if (gai_error) {
472 warnx("%s: %s\n", host, gai_strerror(gai_error));
473 return 1;
475 mysin->sin6_addr = ((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_addr;
476 #ifdef __KAME__
477 if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr)) {
478 *(u_int16_t *)(void *)&mysin->sin6_addr.s6_addr[2] =
479 htons(((struct sockaddr_in6 *)(void *)res->ai_addr)->sin6_scope_id);
481 #endif
482 if (rtmsg(RTM_GET) < 0)
483 errx(1, "RTM_GET(%s) failed", host);
484 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
485 sdl = (struct sockaddr_dl *)(void *)(ROUNDUP(mysin->sin6_len) +
486 (char *)(void *)mysin);
487 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) {
488 if (sdl->sdl_family == AF_LINK &&
489 (rtm->rtm_flags & RTF_LLINFO) &&
490 !(rtm->rtm_flags & RTF_GATEWAY)) {
491 goto delete;
494 * IPv4 arp command retries with sin_other = SIN_PROXY here.
496 warnx("delete: cannot delete non-NDP entry");
497 return 1;
500 delete:
501 if (sdl->sdl_family != AF_LINK) {
502 (void)printf("cannot locate %s\n", host);
503 return (1);
505 if (rtmsg(RTM_DELETE) == 0) {
506 struct sockaddr_in6 s6 = *mysin; /* XXX: for safety */
508 #ifdef __KAME__
509 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
510 s6.sin6_scope_id = ntohs(*(u_int16_t *)(void *)&s6.sin6_addr.s6_addr[2]);
511 *(u_int16_t *)(void *)&s6.sin6_addr.s6_addr[2] = 0;
513 #endif
514 (void)getnameinfo((struct sockaddr *)(void *)&s6,
515 (socklen_t)s6.sin6_len, host_buf,
516 sizeof(host_buf), NULL, 0,
517 (nflag ? NI_NUMERICHOST : 0));
518 (void)printf("%s (%s) deleted\n", host, host_buf);
521 return 0;
524 #define W_ADDR 36
525 #define W_LL 17
526 #define W_IF 6
529 * Dump the entire neighbor cache
531 static void
532 dump(struct in6_addr *addr, int cflag)
534 int mib[6];
535 size_t needed;
536 char *lim, *buf, *next;
537 struct rt_msghdr *rtm;
538 struct sockaddr_in6 *mysin;
539 struct sockaddr_dl *sdl;
540 struct in6_nbrinfo *nbi;
541 struct timeval tim;
542 int addrwidth;
543 int llwidth;
544 int ifwidth;
545 char flgbuf[8];
546 const char *ifname;
548 /* Print header */
549 if (!tflag && !cflag)
550 (void)printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
551 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
552 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
554 again:;
555 mib[0] = CTL_NET;
556 mib[1] = PF_ROUTE;
557 mib[2] = 0;
558 mib[3] = AF_INET6;
559 mib[4] = NET_RT_FLAGS;
560 mib[5] = RTF_LLINFO;
561 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
562 err(1, "sysctl(PF_ROUTE estimate)");
563 if (needed > 0) {
564 if ((buf = malloc(needed)) == NULL)
565 err(1, "malloc");
566 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
567 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
568 lim = buf + needed;
569 } else
570 buf = lim = NULL;
572 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
573 int isrouter = 0, prbs = 0;
575 rtm = (struct rt_msghdr *)(void *)next;
576 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1);
577 sdl = (struct sockaddr_dl *)(void *)((char *)(void *)mysin + ROUNDUP(mysin->sin6_len));
580 * Some OSes can produce a route that has the LINK flag but
581 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
582 * and BSD/OS, where xx is not the interface identifier on
583 * lo0). Such routes entry would annoy getnbrinfo() below,
584 * so we skip them.
585 * XXX: such routes should have the GATEWAY flag, not the
586 * LINK flag. However, there is rotten routing software
587 * that advertises all routes that have the GATEWAY flag.
588 * Thus, KAME kernel intentionally does not set the LINK flag.
589 * What is to be fixed is not ndp, but such routing software
590 * (and the kernel workaround)...
592 if (sdl->sdl_family != AF_LINK)
593 continue;
595 if (!(rtm->rtm_flags & RTF_HOST))
596 continue;
598 if (addr) {
599 if (!IN6_ARE_ADDR_EQUAL(addr, &mysin->sin6_addr))
600 continue;
601 found_entry = 1;
602 } else if (IN6_IS_ADDR_MULTICAST(&mysin->sin6_addr))
603 continue;
604 if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr) ||
605 IN6_IS_ADDR_MC_LINKLOCAL(&mysin->sin6_addr)) {
606 /* XXX: should scope id be filled in the kernel? */
607 if (mysin->sin6_scope_id == 0)
608 mysin->sin6_scope_id = sdl->sdl_index;
609 #ifdef __KAME__
610 /* KAME specific hack; removed the embedded id */
611 *(u_int16_t *)(void *)&mysin->sin6_addr.s6_addr[2] = 0;
612 #endif
614 (void)getnameinfo((struct sockaddr *)(void *)mysin,
615 (socklen_t)mysin->sin6_len,
616 host_buf, sizeof(host_buf), NULL, 0,
617 (nflag ? NI_NUMERICHOST : 0));
618 if (cflag) {
619 #ifdef RTF_WASCLONED
620 if (rtm->rtm_flags & RTF_WASCLONED)
621 (void)delete(host_buf);
622 #elif defined(RTF_CLONED)
623 if (rtm->rtm_flags & RTF_CLONED)
624 (void)delete(host_buf);
625 #else
626 (void)delete(host_buf);
627 #endif
628 continue;
630 (void)gettimeofday(&tim, 0);
631 if (tflag)
632 ts_print(&tim);
634 addrwidth = strlen(host_buf);
635 if (addrwidth < W_ADDR)
636 addrwidth = W_ADDR;
637 llwidth = strlen(ether_str(sdl));
638 if (W_ADDR + W_LL - addrwidth > llwidth)
639 llwidth = W_ADDR + W_LL - addrwidth;
640 ifname = if_indextoname((unsigned int)sdl->sdl_index, ifix_buf);
641 if (!ifname)
642 ifname = "?";
643 ifwidth = strlen(ifname);
644 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
645 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
647 (void)printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth,
648 host_buf, llwidth, llwidth, ether_str(sdl), ifwidth,
649 ifwidth, ifname);
651 /* Print neighbor discovery specific informations */
652 nbi = getnbrinfo(&mysin->sin6_addr,
653 (unsigned int)sdl->sdl_index, 1);
654 if (nbi) {
655 if (nbi->expire > tim.tv_sec) {
656 (void)printf(" %-9.9s",
657 sec2str(nbi->expire - tim.tv_sec));
658 } else if (nbi->expire == 0)
659 (void)printf(" %-9.9s", "permanent");
660 else
661 (void)printf(" %-9.9s", "expired");
663 switch (nbi->state) {
664 case ND6_LLINFO_NOSTATE:
665 (void)printf(" N");
666 break;
667 #ifdef ND6_LLINFO_WAITDELETE
668 case ND6_LLINFO_WAITDELETE:
669 (void)printf(" W");
670 break;
671 #endif
672 case ND6_LLINFO_INCOMPLETE:
673 (void)printf(" I");
674 break;
675 case ND6_LLINFO_REACHABLE:
676 (void)printf(" R");
677 break;
678 case ND6_LLINFO_STALE:
679 (void)printf(" S");
680 break;
681 case ND6_LLINFO_DELAY:
682 (void)printf(" D");
683 break;
684 case ND6_LLINFO_PROBE:
685 (void)printf(" P");
686 break;
687 default:
688 (void)printf(" ?");
689 break;
692 isrouter = nbi->isrouter;
693 prbs = nbi->asked;
694 } else {
695 warnx("failed to get neighbor information");
696 (void)printf(" ");
700 * other flags. R: router, P: proxy, W: ??
702 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
703 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s",
704 isrouter ? "R" : "",
705 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
706 } else {
707 mysin = (struct sockaddr_in6 *)(void *)
708 (sdl->sdl_len + (char *)(void *)sdl);
709 #if 0 /* W and P are mystery even for us */
710 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
711 isrouter ? "R" : "",
712 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
713 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
714 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
715 #else
716 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s",
717 isrouter ? "R" : "",
718 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
719 #endif
721 (void)printf(" %s", flgbuf);
723 if (prbs)
724 (void)printf(" %d", prbs);
726 (void)printf("\n");
728 if (buf != NULL)
729 free(buf);
731 if (repeat) {
732 (void)printf("\n");
733 (void)fflush(stdout);
734 (void)sleep(repeat);
735 goto again;
739 static struct in6_nbrinfo *
740 getnbrinfo(struct in6_addr *addr, unsigned int ifindex, int warning)
742 static struct in6_nbrinfo nbi;
743 int s;
745 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
746 err(1, "socket");
748 (void)memset(&nbi, 0, sizeof(nbi));
749 (void)if_indextoname(ifindex, nbi.ifname);
750 nbi.addr = *addr;
751 if (ioctl(s, SIOCGNBRINFO_IN6, &nbi) < 0) {
752 if (warning)
753 warn("ioctl(SIOCGNBRINFO_IN6)");
754 (void)close(s);
755 return(NULL);
758 (void)close(s);
759 return(&nbi);
762 static char *
763 ether_str(struct sockaddr_dl *sdl)
765 static char hbuf[NI_MAXHOST];
767 if (sdl->sdl_alen) {
768 if (getnameinfo((struct sockaddr *)(void *)sdl,
769 (socklen_t)sdl->sdl_len,
770 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
771 (void)snprintf(hbuf, sizeof(hbuf), "<invalid>");
772 } else
773 (void)snprintf(hbuf, sizeof(hbuf), "(incomplete)");
775 return(hbuf);
778 static int
779 ndp_ether_aton(char *a, u_char *n)
781 int i, o[6];
783 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
784 &o[3], &o[4], &o[5]);
785 if (i != 6) {
786 warnx("invalid Ethernet address '%s'", a);
787 return (1);
789 for (i = 0; i < 6; i++)
790 n[i] = o[i];
791 return (0);
794 static void
795 usage(void)
797 const char *pn = getprogname();
799 (void)fprintf(stderr, "Usage: %s [-nt] hostname\n", pn);
800 (void)fprintf(stderr,
801 " %s [-nt] -a | -c | -p | -r | -H | -P | -R\n", pn);
802 (void)fprintf(stderr, " %s [-nt] -A wait\n", pn);
803 (void)fprintf(stderr, " %s [-nt] -d hostname\n", pn);
804 (void)fprintf(stderr, " %s [-nt] -f filename\n", pn);
805 (void)fprintf(stderr, " %s [-nt] -i interface [flags...]\n", pn);
806 #ifdef SIOCSDEFIFACE_IN6
807 (void)fprintf(stderr, " %s [-nt] -I [interface|delete]\n", pn);
808 #endif
809 (void)fprintf(stderr,
810 " %s [-nt] -s nodename etheraddr [temp] [proxy]\n", pn);
811 exit(1);
814 static int
815 rtmsg(int cmd)
817 static int seq;
818 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
819 register char *cp = m_rtmsg.m_space;
820 register int l;
822 errno = 0;
823 if (cmd == RTM_DELETE)
824 goto doit;
825 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
826 rtm->rtm_flags = flags;
827 rtm->rtm_version = RTM_VERSION;
829 switch (cmd) {
830 default:
831 errx(1, "internal wrong cmd");
832 /*NOTREACHED*/
833 case RTM_ADD:
834 rtm->rtm_addrs |= RTA_GATEWAY;
835 if (expire_time) {
836 rtm->rtm_rmx.rmx_expire = expire_time;
837 rtm->rtm_inits = RTV_EXPIRE;
839 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
840 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */
841 if (rtm->rtm_flags & RTF_ANNOUNCE) {
842 rtm->rtm_flags &= ~RTF_HOST;
843 rtm->rtm_addrs |= RTA_NETMASK;
845 #endif
846 /* FALLTHROUGH */
847 case RTM_GET:
848 rtm->rtm_addrs |= RTA_DST;
850 #define NEXTADDR(w, s) \
851 if (rtm->rtm_addrs & (w)) { \
852 (void)memcpy(cp, &s, sizeof(s)); \
853 ADVANCE(cp, (struct sockaddr *)(void *)&s); \
856 NEXTADDR(RTA_DST, sin_m);
857 NEXTADDR(RTA_GATEWAY, sdl_m);
858 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */
859 (void)memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
860 NEXTADDR(RTA_NETMASK, so_mask);
861 #endif
863 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
864 doit:
865 l = rtm->rtm_msglen;
866 rtm->rtm_seq = ++seq;
867 rtm->rtm_type = cmd;
868 if (write(my_s, &m_rtmsg, (size_t)l) == -1) {
869 if (errno != ESRCH || cmd != RTM_DELETE)
870 err(1, "writing to routing socket");
872 do {
873 l = read(my_s, &m_rtmsg, sizeof(m_rtmsg));
874 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
875 if (l < 0)
876 warn("read from routing socket");
877 return (0);
880 static void
881 ifinfo(char *ifname, int argc, char **argv)
883 struct in6_ndireq nd;
884 int i, s;
885 u_int32_t newflags;
886 #ifdef IPV6CTL_USETEMPADDR
887 u_int8_t nullbuf[8];
888 #endif
890 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
891 err(1, "socket");
892 (void)memset(&nd, 0, sizeof(nd));
893 (void)strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
894 if (ioctl(s, SIOCGIFINFO_IN6, &nd) < 0)
895 err(1, "ioctl(SIOCGIFINFO_IN6)");
896 #define ND nd.ndi
897 newflags = ND.flags;
898 for (i = 0; i < argc; i++) {
899 int clear = 0;
900 char *cp = argv[i];
902 if (*cp == '-') {
903 clear = 1;
904 cp++;
907 #define SETFLAG(s, f) \
908 do {\
909 if (strcmp(cp, (s)) == 0) {\
910 if (clear)\
911 newflags &= ~(f);\
912 else\
913 newflags |= (f);\
915 } while (/*CONSTCOND*/0)
917 * XXX: this macro is not 100% correct, in that it matches "nud" against
918 * "nudbogus". But we just let it go since this is minor.
920 #define SETVALUE(f, v) \
921 do { \
922 char *valptr; \
923 unsigned long newval; \
924 v = 0; /* unspecified */ \
925 if (strncmp(cp, f, strlen(f)) == 0) { \
926 valptr = strchr(cp, '='); \
927 if (valptr == NULL) \
928 err(1, "syntax error in %s field", (f)); \
929 errno = 0; \
930 newval = strtoul(++valptr, NULL, 0); \
931 if (errno) \
932 err(1, "syntax error in %s's value", (f)); \
933 v = newval; \
935 } while (/*CONSTCOND*/0)
937 #ifdef ND6_IFF_IFDISABLED
938 SETFLAG("disabled", ND6_IFF_IFDISABLED);
939 #endif
940 SETFLAG("nud", ND6_IFF_PERFORMNUD);
941 #ifdef ND6_IFF_ACCEPT_RTADV
942 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
943 #endif
944 #ifdef ND6_IFF_OVERRIDE_RTADV
945 SETFLAG("override_rtadv", ND6_IFF_OVERRIDE_RTADV);
946 #endif
947 #ifdef ND6_IFF_PREFER_SOURCE
948 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
949 #endif
950 #ifdef ND6_IFF_DONT_SET_IFROUTE
951 SETFLAG("dont_set_ifroute", ND6_IFF_DONT_SET_IFROUTE);
952 #endif
953 SETVALUE("basereachable", ND.basereachable);
954 SETVALUE("retrans", ND.retrans);
955 SETVALUE("curhlim", ND.chlim);
957 ND.flags = newflags;
958 #ifdef SIOCSIFINFO_IN6
959 if (ioctl(s, SIOCSIFINFO_IN6, &nd) < 0)
960 err(1, "ioctl(SIOCSIFINFO_IN6)");
961 #else
962 if (ioctl(s, SIOCSIFINFO_FLAGS, &nd) < 0)
963 err(1, "ioctl(SIOCSIFINFO_FLAGS)");
964 #endif
965 #undef SETFLAG
966 #undef SETVALUE
969 if (!ND.initialized)
970 errx(1, "%s: not initialized yet", ifname);
972 if (ioctl(s, SIOCGIFINFO_IN6, &nd) < 0)
973 err(1, "ioctl(SIOCGIFINFO_IN6)");
974 (void)printf("linkmtu=%d", ND.linkmtu);
975 (void)printf(", maxmtu=%d", ND.maxmtu);
976 (void)printf(", curhlim=%d", ND.chlim);
977 (void)printf(", basereachable=%ds%dms",
978 ND.basereachable / 1000, ND.basereachable % 1000);
979 (void)printf(", reachable=%ds", ND.reachable);
980 (void)printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
981 #ifdef IPV6CTL_USETEMPADDR
982 (void)memset(nullbuf, 0, sizeof(nullbuf));
983 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
984 int j;
985 u_int8_t *rbuf;
987 for (i = 0; i < 3; i++) {
988 switch (i) {
989 case 0:
990 (void)printf("\nRandom seed(0): ");
991 rbuf = ND.randomseed0;
992 break;
993 case 1:
994 (void)printf("\nRandom seed(1): ");
995 rbuf = ND.randomseed1;
996 break;
997 case 2:
998 (void)printf("\nRandom ID: ");
999 rbuf = ND.randomid;
1000 break;
1001 default:
1002 errx(1, "impossible case for tempaddr display");
1004 for (j = 0; j < 8; j++)
1005 (void)printf("%02x", rbuf[j]);
1008 #endif
1009 if (ND.flags) {
1010 (void)printf("\nFlags: ");
1011 if ((ND.flags & ND6_IFF_PERFORMNUD))
1012 (void)printf("nud ");
1013 #ifdef ND6_IFF_IFDISABLED
1014 if ((ND.flags & ND6_IFF_IFDISABLED))
1015 (void)printf("disabled ");
1016 #endif
1017 #ifdef ND6_IFF_ACCEPT_RTADV
1018 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1019 (void)printf("accept_rtadv ");
1020 #endif
1021 #ifdef ND6_IFF_OVERRIDE_RTADV
1022 if ((ND.flags & ND6_IFF_OVERRIDE_RTADV))
1023 (void)printf("override_rtadv ");
1024 #endif
1025 #ifdef ND6_IFF_PREFER_SOURCE
1026 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1027 (void)printf("prefer_source ");
1028 #endif
1030 (void)putc('\n', stdout);
1031 #undef ND
1033 (void)close(s);
1036 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1037 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1038 #endif
1040 static void
1041 rtrlist(void)
1043 #ifdef ICMPV6CTL_ND6_DRLIST
1044 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1045 char *buf;
1046 struct in6_defrouter *p, *ep;
1047 size_t l;
1048 struct timeval tim;
1050 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1051 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1052 /*NOTREACHED*/
1054 if (l == 0)
1055 return;
1056 buf = malloc(l);
1057 if (!buf) {
1058 err(1, "malloc");
1059 /*NOTREACHED*/
1061 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1062 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1063 /*NOTREACHED*/
1066 ep = (struct in6_defrouter *)(void *)(buf + l);
1067 for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) {
1068 int rtpref;
1070 if (getnameinfo((struct sockaddr *)(void *)&p->rtaddr,
1071 (socklen_t)p->rtaddr.sin6_len, host_buf, sizeof(host_buf),
1072 NULL, 0, (nflag ? NI_NUMERICHOST : 0)) != 0)
1073 (void)strlcpy(host_buf, "?", sizeof(host_buf));
1075 (void)printf("%s if=%s", host_buf,
1076 if_indextoname((unsigned int)p->if_index, ifix_buf));
1077 (void)printf(", flags=%s%s",
1078 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1079 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1080 rtpref = ((uint32_t)(p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1081 (void)printf(", pref=%s", rtpref_str[rtpref]);
1083 (void)gettimeofday(&tim, 0);
1084 if (p->expire == 0)
1085 (void)printf(", expire=Never\n");
1086 else
1087 (void)printf(", expire=%s\n",
1088 sec2str((time_t)(p->expire - tim.tv_sec)));
1090 free(buf);
1091 #else
1092 struct in6_drlist dr;
1093 int s, i;
1094 struct timeval time;
1096 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1097 err(1, "socket");
1098 /* NOTREACHED */
1100 (void)memset(&dr, 0, sizeof(dr));
1101 (void)strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1102 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1103 err(1, "ioctl(SIOCGDRLST_IN6)");
1104 /* NOTREACHED */
1106 #define DR dr.defrouter[i]
1107 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1108 struct sockaddr_in6 sin6;
1110 (void)memset(&sin6, 0, sizeof(sin6));
1111 sin6.sin6_family = AF_INET6;
1112 sin6.sin6_len = sizeof(sin6);
1113 sin6.sin6_addr = DR.rtaddr;
1114 (void)getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1115 host_buf, sizeof(host_buf), NULL, 0,
1116 (nflag ? NI_NUMERICHOST : 0));
1118 (void)printf("%s if=%s", host_buf,
1119 if_indextoname(DR.if_index, ifix_buf));
1120 (void)printf(", flags=%s%s",
1121 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1122 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1123 gettimeofday(&time, 0);
1124 if (DR.expire == 0)
1125 (void)printf(", expire=Never\n");
1126 else
1127 (void)printf(", expire=%s\n",
1128 sec2str(DR.expire - time.tv_sec));
1130 #undef DR
1131 (void)close(s);
1132 #endif
1135 static void
1136 plist(void)
1138 #ifdef ICMPV6CTL_ND6_PRLIST
1139 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1140 char *buf;
1141 struct in6_prefix *p, *ep, *n;
1142 struct sockaddr_in6 *advrtr;
1143 size_t l;
1144 struct timeval tim;
1145 const int niflags = NI_NUMERICHOST;
1146 int ninflags = nflag ? NI_NUMERICHOST : 0;
1147 char namebuf[NI_MAXHOST];
1149 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1150 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1151 /*NOTREACHED*/
1153 buf = malloc(l);
1154 if (!buf) {
1155 err(1, "malloc");
1156 /*NOTREACHED*/
1158 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1159 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1160 /*NOTREACHED*/
1163 ep = (struct in6_prefix *)(void *)(buf + l);
1164 for (p = (struct in6_prefix *)(void *)buf; p < ep; p = n) {
1165 advrtr = (struct sockaddr_in6 *)(void *)(p + 1);
1166 n = (struct in6_prefix *)(void *)&advrtr[p->advrtrs];
1168 if (getnameinfo((struct sockaddr *)(void *)&p->prefix,
1169 (socklen_t)p->prefix.sin6_len, namebuf, sizeof(namebuf),
1170 NULL, 0, niflags) != 0)
1171 (void)strlcpy(namebuf, "?", sizeof(namebuf));
1172 (void)printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1173 if_indextoname((unsigned int)p->if_index, ifix_buf));
1175 (void)gettimeofday(&tim, 0);
1177 * meaning of fields, especially flags, is very different
1178 * by origin. notify the difference to the users.
1180 (void)printf("flags=%s%s%s%s%s",
1181 p->raflags.onlink ? "L" : "",
1182 p->raflags.autonomous ? "A" : "",
1183 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1184 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1185 #ifdef NDPRF_HOME
1186 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1187 #else
1189 #endif
1191 if (p->vltime == ND6_INFINITE_LIFETIME)
1192 (void)printf(" vltime=infinity");
1193 else
1194 (void)printf(" vltime=%lu", (unsigned long)p->vltime);
1195 if (p->pltime == ND6_INFINITE_LIFETIME)
1196 (void)printf(", pltime=infinity");
1197 else
1198 (void)printf(", pltime=%lu", (unsigned long)p->pltime);
1199 if (p->expire == 0)
1200 (void)printf(", expire=Never");
1201 else if (p->expire >= tim.tv_sec)
1202 (void)printf(", expire=%s",
1203 sec2str(p->expire - tim.tv_sec));
1204 else
1205 (void)printf(", expired");
1206 (void)printf(", ref=%d", p->refcnt);
1207 (void)printf("\n");
1209 * "advertising router" list is meaningful only if the prefix
1210 * information is from RA.
1212 if (p->advrtrs) {
1213 int j;
1214 struct sockaddr_in6 *sin6;
1216 sin6 = advrtr;
1217 (void)printf(" advertised by\n");
1218 for (j = 0; j < p->advrtrs; j++) {
1219 struct in6_nbrinfo *nbi;
1221 if (getnameinfo((struct sockaddr *)(void *)sin6,
1222 (socklen_t)sin6->sin6_len, namebuf,
1223 sizeof(namebuf), NULL, 0, ninflags) != 0)
1224 (void)strlcpy(namebuf, "?", sizeof(namebuf));
1225 (void)printf(" %s", namebuf);
1227 nbi = getnbrinfo(&sin6->sin6_addr,
1228 (unsigned int)p->if_index, 0);
1229 if (nbi) {
1230 switch (nbi->state) {
1231 case ND6_LLINFO_REACHABLE:
1232 case ND6_LLINFO_STALE:
1233 case ND6_LLINFO_DELAY:
1234 case ND6_LLINFO_PROBE:
1235 (void)printf(" (reachable)\n");
1236 break;
1237 default:
1238 (void)printf(" (unreachable)\n");
1240 } else
1241 (void)printf(" (no neighbor state)\n");
1242 sin6++;
1244 } else
1245 (void)printf(" No advertising router\n");
1247 free(buf);
1248 #else
1249 struct in6_prlist pr;
1250 int s, i;
1251 struct timeval time;
1253 (void)gettimeofday(&time, 0);
1255 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1256 err(1, "socket");
1257 /* NOTREACHED */
1259 (void)memset(&pr, 0, sizeof(pr));
1260 (void)strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1261 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1262 err(1, "ioctl(SIOCGPRLST_IN6)");
1263 /* NOTREACHED */
1265 #define PR pr.prefix[i]
1266 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1267 struct sockaddr_in6 p6;
1268 char namebuf[NI_MAXHOST];
1269 int niflags;
1271 #ifdef NDPRF_ONLINK
1272 p6 = PR.prefix;
1273 #else
1274 (void)memset(&p6, 0, sizeof(p6));
1275 p6.sin6_family = AF_INET6;
1276 p6.sin6_len = sizeof(p6);
1277 p6.sin6_addr = PR.prefix;
1278 #endif
1280 niflags = NI_NUMERICHOST;
1281 if (getnameinfo((struct sockaddr *)&p6,
1282 sizeof(p6), namebuf, sizeof(namebuf),
1283 NULL, 0, niflags)) {
1284 warnx("getnameinfo failed");
1285 continue;
1287 (void)printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1288 if_indextoname(PR.if_index, ifix_buf));
1290 (void)gettimeofday(&time, 0);
1292 * meaning of fields, especially flags, is very different
1293 * by origin. notify the difference to the users.
1295 #if 0
1296 (void)printf(" %s",
1297 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1298 #endif
1299 #ifdef NDPRF_ONLINK
1300 (void)printf("flags=%s%s%s%s%s",
1301 PR.raflags.onlink ? "L" : "",
1302 PR.raflags.autonomous ? "A" : "",
1303 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1304 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1305 #ifdef NDPRF_HOME
1306 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1307 #else
1309 #endif
1311 #else
1312 (void)printf("flags=%s%s",
1313 PR.raflags.onlink ? "L" : "",
1314 PR.raflags.autonomous ? "A" : "");
1315 #endif
1316 if (PR.vltime == ND6_INFINITE_LIFETIME)
1317 (void)printf(" vltime=infinity");
1318 else
1319 (void)printf(" vltime=%lu", PR.vltime);
1320 if (PR.pltime == ND6_INFINITE_LIFETIME)
1321 (void)printf(", pltime=infinity");
1322 else
1323 (void)printf(", pltime=%lu", PR.pltime);
1324 if (PR.expire == 0)
1325 (void)printf(", expire=Never");
1326 else if (PR.expire >= time.tv_sec)
1327 (void)printf(", expire=%s",
1328 sec2str(PR.expire - time.tv_sec));
1329 else
1330 (void)printf(", expired");
1331 #ifdef NDPRF_ONLINK
1332 (void)printf(", ref=%d", PR.refcnt);
1333 #endif
1334 #if 0
1335 switch (PR.origin) {
1336 case PR_ORIG_RA:
1337 (void)printf(", origin=RA");
1338 break;
1339 case PR_ORIG_RR:
1340 (void)printf(", origin=RR");
1341 break;
1342 case PR_ORIG_STATIC:
1343 (void)printf(", origin=static");
1344 break;
1345 case PR_ORIG_KERNEL:
1346 (void)printf(", origin=kernel");
1347 break;
1348 default:
1349 (void)printf(", origin=?");
1350 break;
1352 #endif
1353 (void)printf("\n");
1355 * "advertising router" list is meaningful only if the prefix
1356 * information is from RA.
1358 if (0 && /* prefix origin is almost obsolted */
1359 PR.origin != PR_ORIG_RA)
1361 else if (PR.advrtrs) {
1362 int j;
1363 (void)printf(" advertised by\n");
1364 for (j = 0; j < PR.advrtrs; j++) {
1365 struct sockaddr_in6 sin6;
1366 struct in6_nbrinfo *nbi;
1368 bzero(&sin6, sizeof(sin6));
1369 sin6.sin6_family = AF_INET6;
1370 sin6.sin6_len = sizeof(sin6);
1371 sin6.sin6_addr = PR.advrtr[j];
1372 sin6.sin6_scope_id = PR.if_index; /* XXX */
1373 (void)getnameinfo((struct sockaddr *)&sin6,
1374 sin6.sin6_len, host_buf,
1375 sizeof(host_buf), NULL, 0,
1376 (nflag ? NI_NUMERICHOST : 0));
1377 (void)printf(" %s", host_buf);
1379 nbi = getnbrinfo(&sin6.sin6_addr,
1380 PR.if_index, 0);
1381 if (nbi) {
1382 switch (nbi->state) {
1383 case ND6_LLINFO_REACHABLE:
1384 case ND6_LLINFO_STALE:
1385 case ND6_LLINFO_DELAY:
1386 case ND6_LLINFO_PROBE:
1387 (void)printf(" (reachable)\n");
1388 break;
1389 default:
1390 (void)printf(" (unreachable)\n");
1392 } else
1393 (void)printf(" (no neighbor state)\n");
1395 if (PR.advrtrs > DRLSTSIZ)
1396 (void)printf(" and %d routers\n",
1397 PR.advrtrs - DRLSTSIZ);
1398 } else
1399 (void)printf(" No advertising router\n");
1401 #undef PR
1402 (void)close(s);
1403 #endif
1406 static void
1407 pfx_flush(void)
1409 char dummyif[IFNAMSIZ+8];
1410 int s;
1412 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1413 err(1, "socket");
1414 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1415 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1416 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1417 (void)close(s);
1420 static void
1421 rtr_flush(void)
1423 char dummyif[IFNAMSIZ+8];
1424 int s;
1426 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1427 err(1, "socket");
1428 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1429 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1430 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1432 (void)close(s);
1435 static void
1436 harmonize_rtr(void)
1438 char dummyif[IFNAMSIZ+8];
1439 int s;
1441 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1442 err(1, "socket");
1443 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1444 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1445 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1447 (void)close(s);
1450 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1451 static void
1452 setdefif(char *ifname)
1454 struct in6_ndifreq ndifreq;
1455 unsigned int ifindex;
1456 int s;
1458 if (strcasecmp(ifname, "delete") == 0)
1459 ifindex = 0;
1460 else {
1461 if ((ifindex = if_nametoindex(ifname)) == 0)
1462 err(1, "failed to resolve i/f index for %s", ifname);
1465 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1466 err(1, "socket");
1468 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1469 ndifreq.ifindex = ifindex;
1471 if (ioctl(s, SIOCSDEFIFACE_IN6, &ndifreq) < 0)
1472 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1474 (void)close(s);
1477 static void
1478 getdefif(void)
1480 struct in6_ndifreq ndifreq;
1481 char ifname[IFNAMSIZ+8];
1482 int s;
1484 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1485 err(1, "socket");
1487 (void)memset(&ndifreq, 0, sizeof(ndifreq));
1488 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1490 if (ioctl(s, SIOCGDEFIFACE_IN6, &ndifreq) < 0)
1491 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1493 if (ndifreq.ifindex == 0)
1494 (void)printf("No default interface.\n");
1495 else {
1496 if ((if_indextoname((unsigned int)ndifreq.ifindex, ifname)) == NULL)
1497 err(1, "failed to resolve ifname for index %lu",
1498 ndifreq.ifindex);
1499 (void)printf("ND default interface = %s\n", ifname);
1502 (void)close(s);
1504 #endif
1506 static const char *
1507 sec2str(time_t total)
1509 static char result[256];
1510 int days, hours, mins, secs;
1511 int first = 1;
1512 char *p = result;
1513 char *ep = &result[sizeof(result)];
1514 int n;
1516 days = total / 3600 / 24;
1517 hours = (total / 3600) % 24;
1518 mins = (total / 60) % 60;
1519 secs = total % 60;
1521 if (days) {
1522 first = 0;
1523 n = snprintf(p, (size_t)(ep - p), "%dd", days);
1524 if (n < 0 || n >= ep - p)
1525 return "?";
1526 p += n;
1528 if (!first || hours) {
1529 first = 0;
1530 n = snprintf(p, (size_t)(ep - p), "%dh", hours);
1531 if (n < 0 || n >= ep - p)
1532 return "?";
1533 p += n;
1535 if (!first || mins) {
1536 first = 0;
1537 n = snprintf(p, (size_t)(ep - p), "%dm", mins);
1538 if (n < 0 || n >= ep - p)
1539 return "?";
1540 p += n;
1542 (void)snprintf(p, (size_t)(ep - p), "%ds", secs);
1544 return(result);
1548 * Print the timestamp
1549 * from tcpdump/util.c
1551 static void
1552 ts_print(const struct timeval *tvp)
1554 int s;
1556 /* Default */
1557 s = (tvp->tv_sec + thiszone) % 86400;
1558 (void)printf("%02d:%02d:%02d.%06u ",
1559 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);