sync
[bitrig.git] / usr.sbin / ndp / ndp.c
blob28aed85098b09695ec5820c9ae67888370ddcd90
1 /* $OpenBSD: ndp.c,v 1.47 2013/03/21 04:43:17 deraadt Exp $ */
2 /* $KAME: ndp.c,v 1.101 2002/07/17 08:46:33 itojun 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>
83 #include <sys/queue.h>
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet6/nd6.h>
96 #include <arpa/inet.h>
98 #include <stdio.h>
99 #include <errno.h>
100 #include <fcntl.h>
101 #include <netdb.h>
102 #include <paths.h>
103 #include <stdlib.h>
104 #include <string.h>
105 #include <unistd.h>
106 #include <err.h>
108 #include "gmt2local.h"
110 /* packing rule for routing socket */
111 #define ROUNDUP(a) \
112 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
113 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
115 static pid_t pid;
116 static int nflag;
117 static int tflag;
118 static int32_t thiszone; /* time difference with gmt */
119 static int s = -1;
120 static int repeat = 0;
122 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
123 char host_buf[NI_MAXHOST]; /* getnameinfo() */
124 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
126 int file(char *);
127 void getsocket(void);
128 int set(int, char **);
129 void get(char *);
130 int delete(char *);
131 void dump(struct in6_addr *, int);
132 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
133 static char *ether_str(struct sockaddr_dl *);
134 int ndp_ether_aton(char *, u_char *);
135 void usage(void);
136 int rtmsg(int);
137 void ifinfo(char *, int, char **);
138 void rtrlist(void);
139 void plist(void);
140 void pfx_flush(void);
141 void rtrlist(void);
142 void rtr_flush(void);
143 void harmonize_rtr(void);
144 static char *sec2str(time_t);
145 static char *ether_str(struct sockaddr_dl *);
146 static void ts_print(const struct timeval *);
148 #ifdef ICMPV6CTL_ND6_DRLIST
149 static char *rtpref_str[] = {
150 "medium", /* 00 */
151 "high", /* 01 */
152 "rsv", /* 10 */
153 "low" /* 11 */
155 #endif
157 int mode = 0;
158 char *arg = NULL;
161 main(int argc, char *argv[])
163 int ch;
165 pid = getpid();
166 thiszone = gmt2local(0);
167 while ((ch = getopt(argc, argv, "acd:f:i:nprstA:HPR")) != -1)
168 switch (ch) {
169 case 'a':
170 case 'c':
171 case 'p':
172 case 'r':
173 case 'H':
174 case 'P':
175 case 'R':
176 case 's':
177 if (mode) {
178 usage();
179 /*NOTREACHED*/
181 mode = ch;
182 arg = NULL;
183 break;
184 case 'd':
185 case 'f':
186 case 'i' :
187 if (mode) {
188 usage();
189 /*NOTREACHED*/
191 mode = ch;
192 arg = optarg;
193 break;
194 case 'n':
195 nflag = 1;
196 break;
197 case 't':
198 tflag = 1;
199 break;
200 case 'A':
201 if (mode) {
202 usage();
203 /*NOTREACHED*/
205 mode = 'a';
206 repeat = atoi(optarg);
207 if (repeat < 0) {
208 usage();
209 /*NOTREACHED*/
211 break;
212 default:
213 usage();
216 argc -= optind;
217 argv += optind;
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 delete(arg);
234 break;
235 case 'p':
236 if (argc != 0) {
237 usage();
238 /*NOTREACHED*/
240 plist();
241 break;
242 case 'i':
243 ifinfo(arg, argc, argv);
244 break;
245 case 'r':
246 if (argc != 0) {
247 usage();
248 /*NOTREACHED*/
250 rtrlist();
251 break;
252 case 's':
253 if (argc < 2 || argc > 4)
254 usage();
255 exit(set(argc, argv) ? 1 : 0);
256 case 'H':
257 if (argc != 0) {
258 usage();
259 /*NOTREACHED*/
261 harmonize_rtr();
262 break;
263 case 'P':
264 if (argc != 0) {
265 usage();
266 /*NOTREACHED*/
268 pfx_flush();
269 break;
270 case 'R':
271 if (argc != 0) {
272 usage();
273 /*NOTREACHED*/
275 rtr_flush();
276 break;
277 case 0:
278 if (argc != 1) {
279 usage();
280 /*NOTREACHED*/
282 get(argv[0]);
283 break;
285 exit(0);
289 * Process a file to set standard ndp entries
292 file(char *name)
294 FILE *fp;
295 int i, retval;
296 char line[100], arg[5][50], *args[5];
298 if ((fp = fopen(name, "r")) == NULL) {
299 fprintf(stderr, "ndp: cannot open %s\n", name);
300 exit(1);
302 args[0] = &arg[0][0];
303 args[1] = &arg[1][0];
304 args[2] = &arg[2][0];
305 args[3] = &arg[3][0];
306 args[4] = &arg[4][0];
307 retval = 0;
308 while (fgets(line, sizeof(line), fp) != NULL) {
309 i = sscanf(line, "%49s %49s %49s %49s %49s",
310 arg[0], arg[1], arg[2], arg[3], arg[4]);
311 if (i < 2) {
312 fprintf(stderr, "ndp: bad line: %s\n", line);
313 retval = 1;
314 continue;
316 if (set(i, args))
317 retval = 1;
319 fclose(fp);
320 return (retval);
323 void
324 getsocket(void)
326 if (s < 0) {
327 s = socket(PF_ROUTE, SOCK_RAW, 0);
328 if (s < 0) {
329 err(1, "socket");
330 /* NOTREACHED */
335 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
336 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
337 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
338 int expire_time, flags, found_entry;
339 struct {
340 struct rt_msghdr m_rtm;
341 char m_space[512];
342 } m_rtmsg;
345 * Set an individual neighbor cache entry
348 set(int argc, char **argv)
350 struct sockaddr_in6 *sin = &sin_m;
351 struct sockaddr_dl *sdl;
352 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
353 struct addrinfo hints, *res;
354 int gai_error;
355 u_char *ea;
356 char *host = argv[0], *eaddr = argv[1];
358 getsocket();
359 argc -= 2;
360 argv += 2;
361 sdl_m = blank_sdl;
362 sin_m = blank_sin;
364 bzero(&hints, sizeof(hints));
365 hints.ai_family = AF_INET6;
366 gai_error = getaddrinfo(host, NULL, &hints, &res);
367 if (gai_error) {
368 fprintf(stderr, "ndp: %s: %s\n", host,
369 gai_strerror(gai_error));
370 return 1;
372 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
373 #ifdef __KAME__
374 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
375 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
376 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
378 #endif
379 ea = (u_char *)LLADDR(&sdl_m);
380 if (ndp_ether_aton(eaddr, ea) == 0)
381 sdl_m.sdl_alen = 6;
382 flags = expire_time = 0;
383 while (argc-- > 0) {
384 if (strncmp(argv[0], "temp", 4) == 0) {
385 struct timeval time;
387 gettimeofday(&time, 0);
388 expire_time = time.tv_sec + 20 * 60;
389 } else if (strncmp(argv[0], "proxy", 5) == 0)
390 flags |= RTF_ANNOUNCE;
391 argv++;
393 if (rtmsg(RTM_GET) < 0) {
394 errx(1, "RTM_GET(%s) failed", host);
395 /* NOTREACHED */
397 sin = (struct sockaddr_in6 *)((char *)rtm + rtm->rtm_hdrlen);
398 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
399 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
400 if (sdl->sdl_family == AF_LINK &&
401 (rtm->rtm_flags & RTF_LLINFO) &&
402 !(rtm->rtm_flags & RTF_GATEWAY)) {
403 switch (sdl->sdl_type) {
404 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
405 case IFT_ISO88024: case IFT_ISO88025:
406 goto overwrite;
410 * IPv4 arp command retries with sin_other = SIN_PROXY here.
412 fprintf(stderr, "set: cannot configure a new entry\n");
413 return 1;
416 overwrite:
417 if (sdl->sdl_family != AF_LINK) {
418 printf("cannot intuit interface index and type for %s\n", host);
419 return (1);
421 sdl_m.sdl_type = sdl->sdl_type;
422 sdl_m.sdl_index = sdl->sdl_index;
423 return (rtmsg(RTM_ADD));
427 * Display an individual neighbor cache entry
429 void
430 get(char *host)
432 struct sockaddr_in6 *sin = &sin_m;
433 struct addrinfo hints, *res;
434 int gai_error;
436 sin_m = blank_sin;
437 bzero(&hints, sizeof(hints));
438 hints.ai_family = AF_INET6;
439 gai_error = getaddrinfo(host, NULL, &hints, &res);
440 if (gai_error) {
441 fprintf(stderr, "ndp: %s: %s\n", host,
442 gai_strerror(gai_error));
443 return;
445 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
446 #ifdef __KAME__
447 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
448 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
449 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
451 #endif
452 dump(&sin->sin6_addr, 0);
453 if (found_entry == 0) {
454 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
455 sizeof(host_buf), NULL ,0,
456 (nflag ? NI_NUMERICHOST : 0));
457 printf("%s (%s) -- no entry\n", host, host_buf);
458 exit(1);
463 * Delete a neighbor cache entry
466 delete(char *host)
468 struct sockaddr_in6 *sin = &sin_m;
469 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
470 struct sockaddr_dl *sdl;
471 struct addrinfo hints, *res;
472 int gai_error;
474 getsocket();
475 sin_m = blank_sin;
477 bzero(&hints, sizeof(hints));
478 hints.ai_family = AF_INET6;
479 gai_error = getaddrinfo(host, NULL, &hints, &res);
480 if (gai_error) {
481 fprintf(stderr, "ndp: %s: %s\n", host,
482 gai_strerror(gai_error));
483 return 1;
485 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
486 #ifdef __KAME__
487 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
488 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
489 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
491 #endif
492 if (rtmsg(RTM_GET) < 0) {
493 errx(1, "RTM_GET(%s) failed", host);
494 /* NOTREACHED */
496 sin = (struct sockaddr_in6 *)((char *)rtm + rtm->rtm_hdrlen);
497 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
498 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
499 if (sdl->sdl_family == AF_LINK &&
500 (rtm->rtm_flags & RTF_LLINFO) &&
501 !(rtm->rtm_flags & RTF_GATEWAY)) {
502 goto delete;
505 * IPv4 arp command retries with sin_other = SIN_PROXY here.
507 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
508 return 1;
511 delete:
512 if (sdl->sdl_family != AF_LINK) {
513 printf("cannot locate %s\n", host);
514 return (1);
516 if (rtmsg(RTM_DELETE) == 0) {
517 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
519 #ifdef __KAME__
520 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
521 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
522 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
524 #endif
525 getnameinfo((struct sockaddr *)&s6,
526 s6.sin6_len, host_buf,
527 sizeof(host_buf), NULL, 0,
528 (nflag ? NI_NUMERICHOST : 0));
529 printf("%s (%s) deleted\n", host, host_buf);
532 return 0;
535 #define W_ADDR 36
536 #define W_LL 17
537 #define W_IF 6
540 * Dump the entire neighbor cache
542 void
543 dump(struct in6_addr *addr, int cflag)
545 int mib[6];
546 size_t needed;
547 char *lim, *buf = NULL, *next;
548 struct rt_msghdr *rtm;
549 struct sockaddr_in6 *sin;
550 struct sockaddr_dl *sdl;
551 struct in6_nbrinfo *nbi;
552 struct timeval time;
553 int addrwidth;
554 int llwidth;
555 int ifwidth;
556 char flgbuf[8];
557 char *ifname;
559 /* Print header */
560 if (!tflag && !cflag)
561 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
562 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
563 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
565 again:;
566 mib[0] = CTL_NET;
567 mib[1] = PF_ROUTE;
568 mib[2] = 0;
569 mib[3] = AF_INET6;
570 mib[4] = NET_RT_FLAGS;
571 mib[5] = RTF_LLINFO;
572 while (1) {
573 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
574 err(1, "sysctl(PF_ROUTE estimate)");
575 if (needed == 0)
576 break;
577 if ((buf = realloc(buf, needed)) == NULL)
578 err(1, "realloc");
579 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
580 if (errno == ENOMEM)
581 continue;
582 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
584 lim = buf + needed;
585 break;
588 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
589 int isrouter = 0, prbs = 0;
591 rtm = (struct rt_msghdr *)next;
592 if (rtm->rtm_version != RTM_VERSION)
593 continue;
594 sin = (struct sockaddr_in6 *)(next + rtm->rtm_hdrlen);
595 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
598 * Some OSes can produce a route that has the LINK flag but
599 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
600 * and BSD/OS, where xx is not the interface identifier on
601 * lo0). Such routes entry would annoy getnbrinfo() below,
602 * so we skip them.
603 * XXX: such routes should have the GATEWAY flag, not the
604 * LINK flag. However, there is rotten routing software
605 * that advertises all routes that have the GATEWAY flag.
606 * Thus, KAME kernel intentionally does not set the LINK flag.
607 * What is to be fixed is not ndp, but such routing software
608 * (and the kernel workaround)...
610 if (sdl->sdl_family != AF_LINK)
611 continue;
613 if (!(rtm->rtm_flags & RTF_HOST))
614 continue;
616 if (addr) {
617 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
618 continue;
619 found_entry = 1;
620 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
621 continue;
622 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
623 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
624 /* XXX: should scope id be filled in the kernel? */
625 if (sin->sin6_scope_id == 0)
626 sin->sin6_scope_id = sdl->sdl_index;
627 #ifdef __KAME__
628 /* KAME specific hack; removed the embedded id */
629 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
630 #endif
632 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
633 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
634 if (cflag) {
635 #ifdef RTF_WASCLONED
636 if (rtm->rtm_flags & RTF_WASCLONED)
637 delete(host_buf);
638 #elif defined(RTF_CLONED)
639 if (rtm->rtm_flags & RTF_CLONED)
640 delete(host_buf);
641 #else
642 delete(host_buf);
643 #endif
644 continue;
646 gettimeofday(&time, 0);
647 if (tflag)
648 ts_print(&time);
650 addrwidth = strlen(host_buf);
651 if (addrwidth < W_ADDR)
652 addrwidth = W_ADDR;
653 llwidth = strlen(ether_str(sdl));
654 if (W_ADDR + W_LL - addrwidth > llwidth)
655 llwidth = W_ADDR + W_LL - addrwidth;
656 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
657 if (!ifname)
658 ifname = "?";
659 ifwidth = strlen(ifname);
660 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
661 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
663 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
664 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
666 /* Print neighbor discovery specific informations */
667 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
668 if (nbi) {
669 if (nbi->expire > time.tv_sec) {
670 printf(" %-9.9s",
671 sec2str(nbi->expire - time.tv_sec));
672 } else if (nbi->expire == 0)
673 printf(" %-9.9s", "permanent");
674 else
675 printf(" %-9.9s", "expired");
677 switch (nbi->state) {
678 case ND6_LLINFO_NOSTATE:
679 printf(" N");
680 break;
681 #ifdef ND6_LLINFO_WAITDELETE
682 case ND6_LLINFO_WAITDELETE:
683 printf(" W");
684 break;
685 #endif
686 case ND6_LLINFO_INCOMPLETE:
687 printf(" I");
688 break;
689 case ND6_LLINFO_REACHABLE:
690 printf(" R");
691 break;
692 case ND6_LLINFO_STALE:
693 printf(" S");
694 break;
695 case ND6_LLINFO_DELAY:
696 printf(" D");
697 break;
698 case ND6_LLINFO_PROBE:
699 printf(" P");
700 break;
701 default:
702 printf(" ?");
703 break;
706 isrouter = nbi->isrouter;
707 prbs = nbi->asked;
708 } else {
709 warnx("failed to get neighbor information");
710 printf(" ");
714 * other flags. R: router, P: proxy, W: ??
716 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
717 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
718 isrouter ? "R" : "",
719 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
720 } else {
721 sin = (struct sockaddr_in6 *)
722 (sdl->sdl_len + (char *)sdl);
723 #if 0 /* W and P are mystery even for us */
724 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
725 isrouter ? "R" : "",
726 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
727 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
728 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
729 #else
730 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
731 isrouter ? "R" : "",
732 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
733 #endif
735 printf(" %s", flgbuf);
737 if (prbs)
738 printf(" %d", prbs);
740 printf("\n");
742 if (buf != NULL)
743 free(buf);
745 if (repeat) {
746 printf("\n");
747 fflush(stdout);
748 sleep(repeat);
749 goto again;
753 static struct in6_nbrinfo *
754 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
756 static struct in6_nbrinfo nbi;
757 int s;
759 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
760 err(1, "socket");
762 bzero(&nbi, sizeof(nbi));
763 if_indextoname(ifindex, nbi.ifname);
764 nbi.addr = *addr;
765 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
766 if (warning)
767 warn("ioctl(SIOCGNBRINFO_IN6)");
768 close(s);
769 return(NULL);
772 close(s);
773 return(&nbi);
776 static char *
777 ether_str(struct sockaddr_dl *sdl)
779 static char hbuf[NI_MAXHOST];
780 u_char *cp;
782 if (sdl->sdl_alen) {
783 cp = (u_char *)LLADDR(sdl);
784 snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x",
785 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
786 } else
787 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
789 return(hbuf);
793 ndp_ether_aton(char *a, u_char *n)
795 int i, o[6];
797 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
798 &o[3], &o[4], &o[5]);
799 if (i != 6) {
800 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
801 return (1);
803 for (i = 0; i < 6; i++)
804 n[i] = o[i];
805 return (0);
808 void
809 usage(void)
811 printf("usage: ndp [-nrt] [-a | -c | -p] [-H | -P | -R] ");
812 printf("[-A wait] [-d hostname]\n");
813 printf("\t[-f filename] ");
814 printf("[-i interface [flag ...]]\n");
815 printf("\t[-s nodename etheraddr [temp] [proxy]] [hostname]\n");
816 exit(1);
820 rtmsg(int cmd)
822 static int seq;
823 int rlen;
824 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
825 char *cp = m_rtmsg.m_space;
826 int l;
828 errno = 0;
829 if (cmd == RTM_DELETE)
830 goto doit;
831 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
832 rtm->rtm_flags = flags;
833 rtm->rtm_version = RTM_VERSION;
835 switch (cmd) {
836 default:
837 fprintf(stderr, "ndp: internal wrong cmd\n");
838 exit(1);
839 case RTM_ADD:
840 rtm->rtm_addrs |= RTA_GATEWAY;
841 if (expire_time) {
842 rtm->rtm_rmx.rmx_expire = expire_time;
843 rtm->rtm_inits = RTV_EXPIRE;
845 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
846 #if 0 /* we don't support ipv6addr/128 type proxying. */
847 if (rtm->rtm_flags & RTF_ANNOUNCE) {
848 rtm->rtm_flags &= ~RTF_HOST;
849 rtm->rtm_addrs |= RTA_NETMASK;
851 #endif
852 /* FALLTHROUGH */
853 case RTM_GET:
854 rtm->rtm_addrs |= RTA_DST;
856 #define NEXTADDR(w, s) \
857 if (rtm->rtm_addrs & (w)) { \
858 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
860 NEXTADDR(RTA_DST, sin_m);
861 NEXTADDR(RTA_GATEWAY, sdl_m);
862 #if 0 /* we don't support ipv6addr/128 type proxying. */
863 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
864 NEXTADDR(RTA_NETMASK, so_mask);
865 #endif
867 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
868 doit:
869 l = rtm->rtm_msglen;
870 rtm->rtm_seq = ++seq;
871 rtm->rtm_type = cmd;
872 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
873 if (errno != ESRCH || cmd != RTM_DELETE) {
874 err(1, "writing to routing socket");
875 /* NOTREACHED */
878 do {
879 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
880 } while (l > 0 && (rtm->rtm_version != RTM_VERSION ||
881 rtm->rtm_seq != seq || rtm->rtm_pid != pid));
882 if (l < 0)
883 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
884 strerror(errno));
885 return (0);
888 void
889 ifinfo(char *ifname, int argc, char **argv)
891 struct in6_ndireq nd;
892 int i, s;
893 u_int32_t newflags;
894 #ifdef IPV6CTL_USETEMPADDR
895 u_int8_t nullbuf[8];
896 #endif
898 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
899 err(1, "socket");
900 /* NOTREACHED */
902 bzero(&nd, sizeof(nd));
903 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
904 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
905 err(1, "ioctl(SIOCGIFINFO_IN6)");
906 /* NOTREACHED */
908 #define ND nd.ndi
909 newflags = ND.flags;
910 for (i = 0; i < argc; i++) {
911 int clear = 0;
912 char *cp = argv[i];
914 if (*cp == '-') {
915 clear = 1;
916 cp++;
919 #define SETFLAG(s, f) \
920 do {\
921 if (strcmp(cp, (s)) == 0) {\
922 if (clear)\
923 newflags &= ~(f);\
924 else\
925 newflags |= (f);\
927 } while (0)
928 SETFLAG("nud", ND6_IFF_PERFORMNUD);
929 #ifdef ND6_IFF_ACCEPT_RTADV
930 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
931 #endif
932 #ifdef ND6_IFF_PREFER_SOURCE
933 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
934 #endif
936 ND.flags = newflags;
937 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
938 err(1, "ioctl(SIOCSIFINFO_FLAGS)");
939 /* NOTREACHED */
941 #undef SETFLAG
944 if (!ND.initialized) {
945 errx(1, "%s: not initialized yet", ifname);
946 /* NOTREACHED */
949 printf("linkmtu=%d", ND.linkmtu);
950 printf(", curhlim=%d", ND.chlim);
951 printf(", basereachable=%ds%dms",
952 ND.basereachable / 1000, ND.basereachable % 1000);
953 printf(", reachable=%ds", ND.reachable);
954 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
955 #ifdef IPV6CTL_USETEMPADDR
956 memset(nullbuf, 0, sizeof(nullbuf));
957 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
958 int j;
959 u_int8_t *rbuf;
961 for (i = 0; i < 3; i++) {
962 switch (i) {
963 case 0:
964 printf("\nRandom seed(0): ");
965 rbuf = ND.randomseed0;
966 break;
967 case 1:
968 printf("\nRandom seed(1): ");
969 rbuf = ND.randomseed1;
970 break;
971 case 2:
972 printf("\nRandom ID: ");
973 rbuf = ND.randomid;
974 break;
976 for (j = 0; j < 8; j++)
977 printf("%02x", rbuf[j]);
980 #endif
981 if (ND.flags) {
982 printf("\nFlags: ");
983 if ((ND.flags & ND6_IFF_PERFORMNUD))
984 printf("nud ");
985 #ifdef ND6_IFF_ACCEPT_RTADV
986 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
987 printf("accept_rtadv ");
988 #endif
989 #ifdef ND6_IFF_PREFER_SOURCE
990 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
991 printf("prefer_source ");
992 #endif
994 putc('\n', stdout);
995 #undef ND
997 close(s);
1000 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1001 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1002 #endif
1004 void
1005 rtrlist(void)
1007 #ifdef ICMPV6CTL_ND6_DRLIST
1008 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1009 char *buf;
1010 struct in6_defrouter *p, *ep;
1011 size_t l;
1012 struct timeval time;
1014 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1015 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1016 /*NOTREACHED*/
1018 if (l == 0)
1019 return;
1020 buf = malloc(l);
1021 if (buf == NULL) {
1022 err(1, "malloc");
1023 /*NOTREACHED*/
1025 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1026 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1027 /*NOTREACHED*/
1030 ep = (struct in6_defrouter *)(buf + l);
1031 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1032 int rtpref;
1034 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1035 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1036 (nflag ? NI_NUMERICHOST : 0)) != 0)
1037 strlcpy(host_buf, "?", sizeof(host_buf));
1039 printf("%s if=%s", host_buf,
1040 if_indextoname(p->if_index, ifix_buf));
1041 printf(", flags=%s%s",
1042 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1043 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1044 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1045 printf(", pref=%s", rtpref_str[rtpref]);
1047 gettimeofday(&time, 0);
1048 if (p->expire == 0)
1049 printf(", expire=Never\n");
1050 else
1051 printf(", expire=%s\n",
1052 sec2str(p->expire - time.tv_sec));
1054 free(buf);
1055 #else
1056 struct in6_drlist dr;
1057 int s, i;
1058 struct timeval time;
1060 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1061 err(1, "socket");
1062 /* NOTREACHED */
1064 bzero(&dr, sizeof(dr));
1065 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1066 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1067 err(1, "ioctl(SIOCGDRLST_IN6)");
1068 /* NOTREACHED */
1070 #define DR dr.defrouter[i]
1071 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1072 struct sockaddr_in6 sin6;
1074 bzero(&sin6, sizeof(sin6));
1075 sin6.sin6_family = AF_INET6;
1076 sin6.sin6_len = sizeof(sin6);
1077 sin6.sin6_addr = DR.rtaddr;
1078 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1079 sizeof(host_buf), NULL, 0,
1080 (nflag ? NI_NUMERICHOST : 0));
1082 printf("%s if=%s", host_buf,
1083 if_indextoname(DR.if_index, ifix_buf));
1084 printf(", flags=%s%s",
1085 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1086 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1087 gettimeofday(&time, 0);
1088 if (DR.expire == 0)
1089 printf(", expire=Never\n");
1090 else
1091 printf(", expire=%s\n",
1092 sec2str(DR.expire - time.tv_sec));
1094 #undef DR
1095 close(s);
1096 #endif
1099 void
1100 plist(void)
1102 #ifdef ICMPV6CTL_ND6_PRLIST
1103 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1104 char *buf;
1105 struct in6_prefix *p, *ep, *n;
1106 struct sockaddr_in6 *advrtr;
1107 size_t l;
1108 struct timeval time;
1109 const int niflags = NI_NUMERICHOST;
1110 int ninflags = nflag ? NI_NUMERICHOST : 0;
1111 char namebuf[NI_MAXHOST];
1113 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1114 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1115 /*NOTREACHED*/
1117 buf = malloc(l);
1118 if (buf == NULL) {
1119 err(1, "malloc");
1120 /*NOTREACHED*/
1122 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1123 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1124 /*NOTREACHED*/
1127 ep = (struct in6_prefix *)(buf + l);
1128 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1129 advrtr = (struct sockaddr_in6 *)(p + 1);
1130 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1132 if (getnameinfo((struct sockaddr *)&p->prefix,
1133 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1134 NULL, 0, niflags) != 0)
1135 strlcpy(namebuf, "?", sizeof(namebuf));
1136 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1137 if_indextoname(p->if_index, ifix_buf));
1139 gettimeofday(&time, 0);
1141 * meaning of fields, especially flags, is very different
1142 * by origin. notify the difference to the users.
1144 printf("flags=%s%s%s%s%s",
1145 p->raflags.onlink ? "L" : "",
1146 p->raflags.autonomous ? "A" : "",
1147 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1148 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1149 #ifdef NDPRF_HOME
1150 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1151 #else
1153 #endif
1155 if (p->vltime == ND6_INFINITE_LIFETIME)
1156 printf(" vltime=infinity");
1157 else
1158 printf(" vltime=%lu", (unsigned long)p->vltime);
1159 if (p->pltime == ND6_INFINITE_LIFETIME)
1160 printf(", pltime=infinity");
1161 else
1162 printf(", pltime=%lu", (unsigned long)p->pltime);
1163 if (p->expire == 0)
1164 printf(", expire=Never");
1165 else if (p->expire >= time.tv_sec)
1166 printf(", expire=%s",
1167 sec2str(p->expire - time.tv_sec));
1168 else
1169 printf(", expired");
1170 printf(", ref=%d", p->refcnt);
1171 printf("\n");
1173 * "advertising router" list is meaningful only if the prefix
1174 * information is from RA.
1176 if (p->advrtrs) {
1177 int j;
1178 struct sockaddr_in6 *sin6;
1180 sin6 = advrtr;
1181 printf(" advertised by\n");
1182 for (j = 0; j < p->advrtrs; j++) {
1183 struct in6_nbrinfo *nbi;
1185 if (getnameinfo((struct sockaddr *)sin6,
1186 sin6->sin6_len, namebuf, sizeof(namebuf),
1187 NULL, 0, ninflags) != 0)
1188 strlcpy(namebuf, "?", sizeof(namebuf));
1189 printf(" %s", namebuf);
1191 nbi = getnbrinfo(&sin6->sin6_addr,
1192 p->if_index, 0);
1193 if (nbi) {
1194 switch (nbi->state) {
1195 case ND6_LLINFO_REACHABLE:
1196 case ND6_LLINFO_STALE:
1197 case ND6_LLINFO_DELAY:
1198 case ND6_LLINFO_PROBE:
1199 printf(" (reachable)\n");
1200 break;
1201 default:
1202 printf(" (unreachable)\n");
1204 } else
1205 printf(" (no neighbor state)\n");
1206 sin6++;
1208 } else
1209 printf(" No advertising router\n");
1211 free(buf);
1212 #else
1213 struct in6_prlist pr;
1214 int s, i;
1215 struct timeval time;
1217 gettimeofday(&time, 0);
1219 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1220 err(1, "socket");
1221 /* NOTREACHED */
1223 bzero(&pr, sizeof(pr));
1224 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1225 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1226 err(1, "ioctl(SIOCGPRLST_IN6)");
1227 /* NOTREACHED */
1229 #define PR pr.prefix[i]
1230 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1231 struct sockaddr_in6 p6;
1232 char namebuf[NI_MAXHOST];
1233 int niflags;
1235 #ifdef NDPRF_ONLINK
1236 p6 = PR.prefix;
1237 #else
1238 memset(&p6, 0, sizeof(p6));
1239 p6.sin6_family = AF_INET6;
1240 p6.sin6_len = sizeof(p6);
1241 p6.sin6_addr = PR.prefix;
1242 #endif
1245 * copy link index to sin6_scope_id field.
1246 * XXX: KAME specific.
1248 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1249 u_int16_t linkid;
1251 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1252 sizeof(linkid));
1253 linkid = ntohs(linkid);
1254 p6.sin6_scope_id = linkid;
1255 p6.sin6_addr.s6_addr[2] = 0;
1256 p6.sin6_addr.s6_addr[3] = 0;
1259 niflags = NI_NUMERICHOST;
1260 if (getnameinfo((struct sockaddr *)&p6,
1261 sizeof(p6), namebuf, sizeof(namebuf),
1262 NULL, 0, niflags)) {
1263 warnx("getnameinfo failed");
1264 continue;
1266 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1267 if_indextoname(PR.if_index, ifix_buf));
1269 gettimeofday(&time, 0);
1271 * meaning of fields, especially flags, is very different
1272 * by origin. notify the difference to the users.
1274 #if 0
1275 printf(" %s",
1276 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1277 #endif
1278 #ifdef NDPRF_ONLINK
1279 printf("flags=%s%s%s%s%s",
1280 PR.raflags.onlink ? "L" : "",
1281 PR.raflags.autonomous ? "A" : "",
1282 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1283 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1284 #ifdef NDPRF_HOME
1285 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1286 #else
1288 #endif
1290 #else
1291 printf("flags=%s%s",
1292 PR.raflags.onlink ? "L" : "",
1293 PR.raflags.autonomous ? "A" : "");
1294 #endif
1295 if (PR.vltime == ND6_INFINITE_LIFETIME)
1296 printf(" vltime=infinity");
1297 else
1298 printf(" vltime=%lu", PR.vltime);
1299 if (PR.pltime == ND6_INFINITE_LIFETIME)
1300 printf(", pltime=infinity");
1301 else
1302 printf(", pltime=%lu", PR.pltime);
1303 if (PR.expire == 0)
1304 printf(", expire=Never");
1305 else if (PR.expire >= time.tv_sec)
1306 printf(", expire=%s",
1307 sec2str(PR.expire - time.tv_sec));
1308 else
1309 printf(", expired");
1310 #ifdef NDPRF_ONLINK
1311 printf(", ref=%d", PR.refcnt);
1312 #endif
1313 #if 0
1314 switch (PR.origin) {
1315 case PR_ORIG_RA:
1316 printf(", origin=RA");
1317 break;
1318 case PR_ORIG_RR:
1319 printf(", origin=RR");
1320 break;
1321 case PR_ORIG_STATIC:
1322 printf(", origin=static");
1323 break;
1324 case PR_ORIG_KERNEL:
1325 printf(", origin=kernel");
1326 break;
1327 default:
1328 printf(", origin=?");
1329 break;
1331 #endif
1332 printf("\n");
1334 * "advertising router" list is meaningful only if the prefix
1335 * information is from RA.
1337 if (0 && /* prefix origin is almost obsolted */
1338 PR.origin != PR_ORIG_RA)
1340 else if (PR.advrtrs) {
1341 int j;
1342 printf(" advertised by\n");
1343 for (j = 0; j < PR.advrtrs; j++) {
1344 struct sockaddr_in6 sin6;
1345 struct in6_nbrinfo *nbi;
1347 bzero(&sin6, sizeof(sin6));
1348 sin6.sin6_family = AF_INET6;
1349 sin6.sin6_len = sizeof(sin6);
1350 sin6.sin6_addr = PR.advrtr[j];
1351 sin6.sin6_scope_id = PR.if_index; /* XXX */
1352 getnameinfo((struct sockaddr *)&sin6,
1353 sin6.sin6_len, host_buf,
1354 sizeof(host_buf), NULL, 0,
1355 (nflag ? NI_NUMERICHOST : 0));
1356 printf(" %s", host_buf);
1358 nbi = getnbrinfo(&sin6.sin6_addr,
1359 PR.if_index, 0);
1360 if (nbi) {
1361 switch (nbi->state) {
1362 case ND6_LLINFO_REACHABLE:
1363 case ND6_LLINFO_STALE:
1364 case ND6_LLINFO_DELAY:
1365 case ND6_LLINFO_PROBE:
1366 printf(" (reachable)\n");
1367 break;
1368 default:
1369 printf(" (unreachable)\n");
1371 } else
1372 printf(" (no neighbor state)\n");
1374 if (PR.advrtrs > DRLSTSIZ)
1375 printf(" and %d routers\n",
1376 PR.advrtrs - DRLSTSIZ);
1377 } else
1378 printf(" No advertising router\n");
1380 #undef PR
1381 close(s);
1382 #endif
1385 void
1386 pfx_flush(void)
1388 char dummyif[IFNAMSIZ+8];
1389 int s;
1391 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1392 err(1, "socket");
1393 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1394 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1395 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1396 close(s);
1399 void
1400 rtr_flush(void)
1402 char dummyif[IFNAMSIZ+8];
1403 int s;
1405 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1406 err(1, "socket");
1407 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1408 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1409 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1411 close(s);
1414 void
1415 harmonize_rtr(void)
1417 char dummyif[IFNAMSIZ+8];
1418 int s;
1420 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1421 err(1, "socket");
1422 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1423 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1424 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1426 close(s);
1429 static char *
1430 sec2str(time_t total)
1432 static char result[256];
1433 int days, hours, mins, secs;
1434 int first = 1;
1435 char *p = result;
1436 char *ep = &result[sizeof(result)];
1437 int n;
1439 days = total / 3600 / 24;
1440 hours = (total / 3600) % 24;
1441 mins = (total / 60) % 60;
1442 secs = total % 60;
1444 if (days) {
1445 first = 0;
1446 n = snprintf(p, ep - p, "%dd", days);
1447 if (n < 0 || n >= ep - p)
1448 return "?";
1449 p += n;
1451 if (!first || hours) {
1452 first = 0;
1453 n = snprintf(p, ep - p, "%dh", hours);
1454 if (n < 0 || n >= ep - p)
1455 return "?";
1456 p += n;
1458 if (!first || mins) {
1459 first = 0;
1460 n = snprintf(p, ep - p, "%dm", mins);
1461 if (n < 0 || n >= ep - p)
1462 return "?";
1463 p += n;
1465 snprintf(p, ep - p, "%ds", secs);
1467 return(result);
1471 * Print the timestamp
1472 * from tcpdump/util.c
1474 static void
1475 ts_print(const struct timeval *tvp)
1477 int s;
1479 /* Default */
1480 s = (tvp->tv_sec + thiszone) % 86400;
1481 (void)printf("%02d:%02d:%02d.%06u ",
1482 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);