2 /* $KAME: ndp.c,v 1.104 2003/06/27 07:48:39 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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
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 * 4. 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
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>
79 #include <sys/ioctl.h>
80 #include <sys/socket.h>
81 #include <sys/sysctl.h>
83 #include <sys/queue.h>
86 #include <net/if_var.h>
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/if_ether.h>
94 #include <netinet/icmp6.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet6/nd6.h>
98 #include <arpa/inet.h>
111 #include "gmt2local.h"
113 /* packing rule for routing socket */
115 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
116 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
121 static int32_t thiszone; /* time difference with gmt */
123 static int repeat = 0;
125 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
126 char host_buf[NI_MAXHOST]; /* getnameinfo() */
127 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
129 int main __P((int, char **));
130 int file __P((char *));
131 void getsocket __P((void));
132 int set __P((int, char **));
133 void get __P((char *));
134 int delete __P((char *));
135 void dump __P((struct in6_addr *, int));
136 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *, int, int));
137 static char *ether_str __P((struct sockaddr_dl *));
138 int ndp_ether_aton __P((char *, u_char *));
139 void usage __P((void));
140 int rtmsg __P((int));
141 void ifinfo __P((char *, int, char **));
142 void rtrlist __P((void));
143 void plist __P((void));
144 void pfx_flush __P((void));
145 void rtr_flush __P((void));
146 void harmonize_rtr __P((void));
147 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
148 static void getdefif __P((void));
149 static void setdefif __P((char *));
151 static char *sec2str __P((time_t));
152 static char *ether_str __P((struct sockaddr_dl *));
153 static void ts_print __P((const struct timeval *));
155 #ifdef ICMPV6CTL_ND6_DRLIST
156 static char *rtpref_str[] = {
175 thiszone = gmt2local(0);
176 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
216 repeat = atoi(optarg);
236 dump(0, mode == 'c');
246 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
250 } else if (argc == 1) {
251 if (strcmp(*argv, "delete") == 0 ||
252 if_nametoindex(*argv))
255 errx(1, "invalid interface %s", *argv);
257 getdefif(); /* always call it to print the result */
260 errx(1, "not supported yet");
271 ifinfo(arg, argc, argv);
281 if (argc < 2 || argc > 4)
283 exit(set(argc, argv) ? 1 : 0);
317 * Process a file to set standard ndp entries
325 char line[100], arg[5][50], *args[5];
327 if ((fp = fopen(name, "r")) == NULL) {
328 fprintf(stderr, "ndp: cannot open %s\n", name);
331 args[0] = &arg[0][0];
332 args[1] = &arg[1][0];
333 args[2] = &arg[2][0];
334 args[3] = &arg[3][0];
335 args[4] = &arg[4][0];
337 while (fgets(line, 100, fp) != NULL) {
338 i = sscanf(line, "%49s %49s %49s %49s %49s",
339 arg[0], arg[1], arg[2], arg[3], arg[4]);
341 fprintf(stderr, "ndp: bad line: %s\n", line);
356 s = socket(PF_ROUTE, SOCK_RAW, 0);
364 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
365 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
366 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
367 int expire_time, flags, found_entry;
369 struct rt_msghdr m_rtm;
374 * Set an individual neighbor cache entry
381 register struct sockaddr_in6 *sin = &sin_m;
382 register struct sockaddr_dl *sdl;
383 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
384 struct addrinfo hints, *res;
387 char *host = argv[0], *eaddr = argv[1];
395 bzero(&hints, sizeof(hints));
396 hints.ai_family = AF_INET6;
397 gai_error = getaddrinfo(host, NULL, &hints, &res);
399 fprintf(stderr, "ndp: %s: %s\n", host,
400 gai_strerror(gai_error));
403 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
405 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
406 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
407 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
410 ea = (u_char *)LLADDR(&sdl_m);
411 if (ndp_ether_aton(eaddr, ea) == 0)
413 flags = expire_time = 0;
415 if (strncmp(argv[0], "temp", 4) == 0) {
418 gettimeofday(&time, 0);
419 expire_time = time.tv_sec + 20 * 60;
420 } else if (strncmp(argv[0], "proxy", 5) == 0)
421 flags |= RTF_ANNOUNCE;
424 if (rtmsg(RTM_GET) < 0) {
425 errx(1, "RTM_GET(%s) failed", host);
428 sin = (struct sockaddr_in6 *)(rtm + 1);
429 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
430 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
431 if (sdl->sdl_family == AF_LINK &&
432 (rtm->rtm_flags & RTF_LLINFO) &&
433 !(rtm->rtm_flags & RTF_GATEWAY)) {
434 switch (sdl->sdl_type) {
435 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
436 case IFT_ISO88024: case IFT_ISO88025:
441 * IPv4 arp command retries with sin_other = SIN_PROXY here.
443 fprintf(stderr, "set: cannot configure a new entry\n");
448 if (sdl->sdl_family != AF_LINK) {
449 printf("cannot intuit interface index and type for %s\n", host);
452 sdl_m.sdl_type = sdl->sdl_type;
453 sdl_m.sdl_index = sdl->sdl_index;
454 return (rtmsg(RTM_ADD));
458 * Display an individual neighbor cache entry
464 struct sockaddr_in6 *sin = &sin_m;
465 struct addrinfo hints, *res;
469 bzero(&hints, sizeof(hints));
470 hints.ai_family = AF_INET6;
471 gai_error = getaddrinfo(host, NULL, &hints, &res);
473 fprintf(stderr, "ndp: %s: %s\n", host,
474 gai_strerror(gai_error));
477 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
479 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
480 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
481 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
484 dump(&sin->sin6_addr, 0);
485 if (found_entry == 0) {
486 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
487 sizeof(host_buf), NULL ,0,
488 (nflag ? NI_NUMERICHOST : 0));
489 printf("%s (%s) -- no entry\n", host, host_buf);
495 * Delete a neighbor cache entry
501 struct sockaddr_in6 *sin = &sin_m;
502 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
503 struct sockaddr_dl *sdl;
504 struct addrinfo hints, *res;
510 bzero(&hints, sizeof(hints));
511 hints.ai_family = AF_INET6;
512 gai_error = getaddrinfo(host, NULL, &hints, &res);
514 fprintf(stderr, "ndp: %s: %s\n", host,
515 gai_strerror(gai_error));
518 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
520 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
521 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
522 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
525 if (rtmsg(RTM_GET) < 0) {
526 errx(1, "RTM_GET(%s) failed", host);
529 sin = (struct sockaddr_in6 *)(rtm + 1);
530 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
531 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
532 if (sdl->sdl_family == AF_LINK &&
533 (rtm->rtm_flags & RTF_LLINFO) &&
534 !(rtm->rtm_flags & RTF_GATEWAY)) {
538 * IPv4 arp command retries with sin_other = SIN_PROXY here.
540 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
545 if (sdl->sdl_family != AF_LINK) {
546 printf("cannot locate %s\n", host);
549 if (rtmsg(RTM_DELETE) == 0) {
550 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
553 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
554 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
555 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
558 getnameinfo((struct sockaddr *)&s6,
559 s6.sin6_len, host_buf,
560 sizeof(host_buf), NULL, 0,
561 (nflag ? NI_NUMERICHOST : 0));
562 printf("%s (%s) deleted\n", host, host_buf);
573 * Dump the entire neighbor cache
577 struct in6_addr *addr;
582 char *lim, *buf, *next;
583 struct rt_msghdr *rtm;
584 struct sockaddr_in6 *sin;
585 struct sockaddr_dl *sdl;
587 struct in6_nbrinfo *nbi;
596 if (!tflag && !cflag)
597 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
598 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
599 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
606 mib[4] = NET_RT_FLAGS;
608 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
609 err(1, "sysctl(PF_ROUTE estimate)");
611 if ((buf = malloc(needed)) == NULL)
613 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
614 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
619 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
620 int isrouter = 0, prbs = 0;
622 rtm = (struct rt_msghdr *)next;
623 sin = (struct sockaddr_in6 *)(rtm + 1);
624 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
627 * Some OSes can produce a route that has the LINK flag but
628 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
629 * and BSD/OS, where xx is not the interface identifier on
630 * lo0). Such routes entry would annoy getnbrinfo() below,
632 * XXX: such routes should have the GATEWAY flag, not the
633 * LINK flag. However, there is rotten routing software
634 * that advertises all routes that have the GATEWAY flag.
635 * Thus, KAME kernel intentionally does not set the LINK flag.
636 * What is to be fixed is not ndp, but such routing software
637 * (and the kernel workaround)...
639 if (sdl->sdl_family != AF_LINK)
642 if (!(rtm->rtm_flags & RTF_HOST))
646 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
649 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
651 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
652 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
653 /* XXX: should scope id be filled in the kernel? */
654 if (sin->sin6_scope_id == 0)
655 sin->sin6_scope_id = sdl->sdl_index;
657 /* KAME specific hack; removed the embedded id */
658 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
661 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
662 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
665 if (rtm->rtm_flags & RTF_WASCLONED)
667 #elif defined(RTF_CLONED)
668 if (rtm->rtm_flags & RTF_CLONED)
675 gettimeofday(&time, 0);
679 addrwidth = strlen(host_buf);
680 if (addrwidth < W_ADDR)
682 llwidth = strlen(ether_str(sdl));
683 if (W_ADDR + W_LL - addrwidth > llwidth)
684 llwidth = W_ADDR + W_LL - addrwidth;
685 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
688 ifwidth = strlen(ifname);
689 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
690 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
692 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
693 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
695 /* Print neighbor discovery specific informations */
696 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
698 if (nbi->expire > time.tv_sec) {
700 sec2str(nbi->expire - time.tv_sec));
701 } else if (nbi->expire == 0)
702 printf(" %-9.9s", "permanent");
704 printf(" %-9.9s", "expired");
706 switch (nbi->state) {
707 case ND6_LLINFO_NOSTATE:
710 #ifdef ND6_LLINFO_WAITDELETE
711 case ND6_LLINFO_WAITDELETE:
715 case ND6_LLINFO_INCOMPLETE:
718 case ND6_LLINFO_REACHABLE:
721 case ND6_LLINFO_STALE:
724 case ND6_LLINFO_DELAY:
727 case ND6_LLINFO_PROBE:
735 isrouter = nbi->isrouter;
738 warnx("failed to get neighbor information");
743 * other flags. R: router, P: proxy, W: ??
745 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
746 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
748 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
750 sin = (struct sockaddr_in6 *)
751 (sdl->sdl_len + (char *)sdl);
752 #if 0 /* W and P are mystery even for us */
753 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
755 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
756 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
757 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
759 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
761 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
764 printf(" %s", flgbuf);
782 static struct in6_nbrinfo *
783 getnbrinfo(addr, ifindex, warning)
784 struct in6_addr *addr;
788 static struct in6_nbrinfo nbi;
791 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
794 bzero(&nbi, sizeof(nbi));
795 if_indextoname(ifindex, nbi.ifname);
797 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
799 warn("ioctl(SIOCGNBRINFO_IN6)");
810 struct sockaddr_dl *sdl;
812 static char hbuf[NI_MAXHOST];
816 cp = (u_char *)LLADDR(sdl);
817 snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x",
818 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
820 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
832 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
833 &o[3], &o[4], &o[5]);
835 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
838 for (i = 0; i < 6; i++)
846 printf("usage: ndp [-nt] hostname\n");
847 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
848 printf(" ndp [-nt] -A wait\n");
849 printf(" ndp [-nt] -d hostname\n");
850 printf(" ndp [-nt] -f filename\n");
851 printf(" ndp [-nt] -i interface [flags...]\n");
852 #ifdef SIOCSDEFIFACE_IN6
853 printf(" ndp [-nt] -I [interface|delete]\n");
855 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
865 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
866 register char *cp = m_rtmsg.m_space;
870 if (cmd == RTM_DELETE)
872 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
873 rtm->rtm_flags = flags;
874 rtm->rtm_version = RTM_VERSION;
878 fprintf(stderr, "ndp: internal wrong cmd\n");
881 rtm->rtm_addrs |= RTA_GATEWAY;
883 rtm->rtm_rmx.rmx_expire = expire_time;
884 rtm->rtm_inits = RTV_EXPIRE;
886 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
887 #if 0 /* we don't support ipv6addr/128 type proxying */
888 if (rtm->rtm_flags & RTF_ANNOUNCE) {
889 rtm->rtm_flags &= ~RTF_HOST;
890 rtm->rtm_addrs |= RTA_NETMASK;
895 rtm->rtm_addrs |= RTA_DST;
897 #define NEXTADDR(w, s) \
898 if (rtm->rtm_addrs & (w)) { \
899 bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
901 NEXTADDR(RTA_DST, sin_m);
902 NEXTADDR(RTA_GATEWAY, sdl_m);
903 #if 0 /* we don't support ipv6addr/128 type proxying */
904 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
905 NEXTADDR(RTA_NETMASK, so_mask);
908 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
911 rtm->rtm_seq = ++seq;
913 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
914 if (errno != ESRCH || cmd != RTM_DELETE) {
915 err(1, "writing to routing socket");
920 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
921 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
923 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
929 ifinfo(ifname, argc, argv)
934 struct in6_ndireq nd;
937 #ifdef IPV6CTL_USETEMPADDR
941 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
945 bzero(&nd, sizeof(nd));
946 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
947 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
948 err(1, "ioctl(SIOCGIFINFO_IN6)");
953 for (i = 0; i < argc; i++) {
962 #define SETFLAG(s, f) \
964 if (strcmp(cp, (s)) == 0) {\
972 * XXX: this macro is not 100% correct, in that it matches "nud" against
973 * "nudbogus". But we just let it go since this is minor.
975 #define SETVALUE(f, v) \
978 unsigned long newval; \
979 v = 0; /* unspecified */ \
980 if (strncmp(cp, f, strlen(f)) == 0) { \
981 valptr = strchr(cp, '='); \
982 if (valptr == NULL) \
983 err(1, "syntax error in %s field", (f)); \
985 newval = strtoul(++valptr, NULL, 0); \
987 err(1, "syntax error in %s's value", (f)); \
992 SETFLAG("disabled", ND6_IFF_IFDISABLED);
993 SETFLAG("nud", ND6_IFF_PERFORMNUD);
994 #ifdef ND6_IFF_ACCEPT_RTADV
995 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
997 #ifdef ND6_IFF_PREFER_SOURCE
998 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
1000 SETVALUE("basereachable", ND.basereachable);
1001 SETVALUE("retrans", ND.retrans);
1002 SETVALUE("curhlim", ND.chlim);
1004 ND.flags = newflags;
1005 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
1006 err(1, "ioctl(SIOCSIFINFO_IN6)");
1013 if (!ND.initialized) {
1014 errx(1, "%s: not initialized yet", ifname);
1018 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
1019 err(1, "ioctl(SIOCGIFINFO_IN6)");
1022 printf("linkmtu=%d", ND.linkmtu);
1023 printf(", maxmtu=%d", ND.maxmtu);
1024 printf(", curhlim=%d", ND.chlim);
1025 printf(", basereachable=%ds%dms",
1026 ND.basereachable / 1000, ND.basereachable % 1000);
1027 printf(", reachable=%ds", ND.reachable);
1028 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1029 #ifdef IPV6CTL_USETEMPADDR
1030 memset(nullbuf, 0, sizeof(nullbuf));
1031 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1035 for (i = 0; i < 3; i++) {
1038 printf("\nRandom seed(0): ");
1039 rbuf = ND.randomseed0;
1042 printf("\nRandom seed(1): ");
1043 rbuf = ND.randomseed1;
1046 printf("\nRandom ID: ");
1050 errx(1, "impossible case for tempaddr display");
1052 for (j = 0; j < 8; j++)
1053 printf("%02x", rbuf[j]);
1058 printf("\nFlags: ");
1059 #ifdef ND6_IFF_IFDISABLED
1060 if ((ND.flags & ND6_IFF_IFDISABLED))
1061 printf("disabled ");
1063 if ((ND.flags & ND6_IFF_PERFORMNUD))
1065 #ifdef ND6_IFF_ACCEPT_RTADV
1066 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1067 printf("accept_rtadv ");
1069 #ifdef ND6_IFF_PREFER_SOURCE
1070 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1071 printf("prefer_source ");
1080 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1081 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1087 #ifdef ICMPV6CTL_ND6_DRLIST
1088 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1090 struct in6_defrouter *p, *ep;
1092 struct timeval time;
1094 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1095 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1105 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1106 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1110 ep = (struct in6_defrouter *)(buf + l);
1111 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1114 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1115 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1116 (nflag ? NI_NUMERICHOST : 0)) != 0)
1117 strlcpy(host_buf, "?", sizeof(host_buf));
1119 printf("%s if=%s", host_buf,
1120 if_indextoname(p->if_index, ifix_buf));
1121 printf(", flags=%s%s",
1122 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1123 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1124 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1125 printf(", pref=%s", rtpref_str[rtpref]);
1127 gettimeofday(&time, 0);
1129 printf(", expire=Never\n");
1131 printf(", expire=%s\n",
1132 sec2str(p->expire - time.tv_sec));
1136 struct in6_drlist dr;
1138 struct timeval time;
1140 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1144 bzero(&dr, sizeof(dr));
1145 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1146 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1147 err(1, "ioctl(SIOCGDRLST_IN6)");
1150 #define DR dr.defrouter[i]
1151 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1152 struct sockaddr_in6 sin6;
1154 bzero(&sin6, sizeof(sin6));
1155 sin6.sin6_family = AF_INET6;
1156 sin6.sin6_len = sizeof(sin6);
1157 sin6.sin6_addr = DR.rtaddr;
1158 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1159 sizeof(host_buf), NULL, 0,
1160 (nflag ? NI_NUMERICHOST : 0));
1162 printf("%s if=%s", host_buf,
1163 if_indextoname(DR.if_index, ifix_buf));
1164 printf(", flags=%s%s",
1165 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1166 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1167 gettimeofday(&time, 0);
1169 printf(", expire=Never\n");
1171 printf(", expire=%s\n",
1172 sec2str(DR.expire - time.tv_sec));
1182 #ifdef ICMPV6CTL_ND6_PRLIST
1183 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1185 struct in6_prefix *p, *ep, *n;
1186 struct sockaddr_in6 *advrtr;
1188 struct timeval time;
1189 const int niflags = NI_NUMERICHOST;
1190 int ninflags = nflag ? NI_NUMERICHOST : 0;
1191 char namebuf[NI_MAXHOST];
1193 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1194 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1202 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1203 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1207 ep = (struct in6_prefix *)(buf + l);
1208 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1209 advrtr = (struct sockaddr_in6 *)(p + 1);
1210 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1212 if (getnameinfo((struct sockaddr *)&p->prefix,
1213 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1214 NULL, 0, niflags) != 0)
1215 strlcpy(namebuf, "?", sizeof(namebuf));
1216 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1217 if_indextoname(p->if_index, ifix_buf));
1219 gettimeofday(&time, 0);
1221 * meaning of fields, especially flags, is very different
1222 * by origin. notify the difference to the users.
1224 printf("flags=%s%s%s%s%s",
1225 p->raflags.onlink ? "L" : "",
1226 p->raflags.autonomous ? "A" : "",
1227 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1228 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1230 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1235 if (p->vltime == ND6_INFINITE_LIFETIME)
1236 printf(" vltime=infinity");
1238 printf(" vltime=%lu", (unsigned long)p->vltime);
1239 if (p->pltime == ND6_INFINITE_LIFETIME)
1240 printf(", pltime=infinity");
1242 printf(", pltime=%lu", (unsigned long)p->pltime);
1244 printf(", expire=Never");
1245 else if (p->expire >= time.tv_sec)
1246 printf(", expire=%s",
1247 sec2str(p->expire - time.tv_sec));
1249 printf(", expired");
1250 printf(", ref=%d", p->refcnt);
1253 * "advertising router" list is meaningful only if the prefix
1254 * information is from RA.
1258 struct sockaddr_in6 *sin6;
1261 printf(" advertised by\n");
1262 for (j = 0; j < p->advrtrs; j++) {
1263 struct in6_nbrinfo *nbi;
1265 if (getnameinfo((struct sockaddr *)sin6,
1266 sin6->sin6_len, namebuf, sizeof(namebuf),
1267 NULL, 0, ninflags) != 0)
1268 strlcpy(namebuf, "?", sizeof(namebuf));
1269 printf(" %s", namebuf);
1271 nbi = getnbrinfo(&sin6->sin6_addr,
1274 switch (nbi->state) {
1275 case ND6_LLINFO_REACHABLE:
1276 case ND6_LLINFO_STALE:
1277 case ND6_LLINFO_DELAY:
1278 case ND6_LLINFO_PROBE:
1279 printf(" (reachable)\n");
1282 printf(" (unreachable)\n");
1285 printf(" (no neighbor state)\n");
1289 printf(" No advertising router\n");
1293 struct in6_prlist pr;
1295 struct timeval time;
1297 gettimeofday(&time, 0);
1299 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1303 bzero(&pr, sizeof(pr));
1304 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1305 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1306 err(1, "ioctl(SIOCGPRLST_IN6)");
1309 #define PR pr.prefix[i]
1310 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1311 struct sockaddr_in6 p6;
1312 char namebuf[NI_MAXHOST];
1318 memset(&p6, 0, sizeof(p6));
1319 p6.sin6_family = AF_INET6;
1320 p6.sin6_len = sizeof(p6);
1321 p6.sin6_addr = PR.prefix;
1325 * copy link index to sin6_scope_id field.
1326 * XXX: KAME specific.
1328 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1331 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1333 linkid = ntohs(linkid);
1334 p6.sin6_scope_id = linkid;
1335 p6.sin6_addr.s6_addr[2] = 0;
1336 p6.sin6_addr.s6_addr[3] = 0;
1339 niflags = NI_NUMERICHOST;
1340 if (getnameinfo((struct sockaddr *)&p6,
1341 sizeof(p6), namebuf, sizeof(namebuf),
1342 NULL, 0, niflags)) {
1343 warnx("getnameinfo failed");
1346 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1347 if_indextoname(PR.if_index, ifix_buf));
1349 gettimeofday(&time, 0);
1351 * meaning of fields, especially flags, is very different
1352 * by origin. notify the difference to the users.
1356 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1359 printf("flags=%s%s%s%s%s",
1360 PR.raflags.onlink ? "L" : "",
1361 PR.raflags.autonomous ? "A" : "",
1362 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1363 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1365 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1371 printf("flags=%s%s",
1372 PR.raflags.onlink ? "L" : "",
1373 PR.raflags.autonomous ? "A" : "");
1375 if (PR.vltime == ND6_INFINITE_LIFETIME)
1376 printf(" vltime=infinity");
1378 printf(" vltime=%lu", PR.vltime);
1379 if (PR.pltime == ND6_INFINITE_LIFETIME)
1380 printf(", pltime=infinity");
1382 printf(", pltime=%lu", PR.pltime);
1384 printf(", expire=Never");
1385 else if (PR.expire >= time.tv_sec)
1386 printf(", expire=%s",
1387 sec2str(PR.expire - time.tv_sec));
1389 printf(", expired");
1391 printf(", ref=%d", PR.refcnt);
1394 switch (PR.origin) {
1396 printf(", origin=RA");
1399 printf(", origin=RR");
1401 case PR_ORIG_STATIC:
1402 printf(", origin=static");
1404 case PR_ORIG_KERNEL:
1405 printf(", origin=kernel");
1408 printf(", origin=?");
1414 * "advertising router" list is meaningful only if the prefix
1415 * information is from RA.
1417 if (0 && /* prefix origin is almost obsolted */
1418 PR.origin != PR_ORIG_RA)
1420 else if (PR.advrtrs) {
1422 printf(" advertised by\n");
1423 for (j = 0; j < PR.advrtrs; j++) {
1424 struct sockaddr_in6 sin6;
1425 struct in6_nbrinfo *nbi;
1427 bzero(&sin6, sizeof(sin6));
1428 sin6.sin6_family = AF_INET6;
1429 sin6.sin6_len = sizeof(sin6);
1430 sin6.sin6_addr = PR.advrtr[j];
1431 sin6.sin6_scope_id = PR.if_index; /* XXX */
1432 getnameinfo((struct sockaddr *)&sin6,
1433 sin6.sin6_len, host_buf,
1434 sizeof(host_buf), NULL, 0,
1435 (nflag ? NI_NUMERICHOST : 0));
1436 printf(" %s", host_buf);
1438 nbi = getnbrinfo(&sin6.sin6_addr,
1441 switch (nbi->state) {
1442 case ND6_LLINFO_REACHABLE:
1443 case ND6_LLINFO_STALE:
1444 case ND6_LLINFO_DELAY:
1445 case ND6_LLINFO_PROBE:
1446 printf(" (reachable)\n");
1449 printf(" (unreachable)\n");
1452 printf(" (no neighbor state)\n");
1454 if (PR.advrtrs > DRLSTSIZ)
1455 printf(" and %d routers\n",
1456 PR.advrtrs - DRLSTSIZ);
1458 printf(" No advertising router\n");
1468 char dummyif[IFNAMSIZ+8];
1471 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1473 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1474 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1475 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1481 char dummyif[IFNAMSIZ+8];
1484 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1486 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1487 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1488 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1496 char dummyif[IFNAMSIZ+8];
1499 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1501 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1502 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1503 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1508 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1513 struct in6_ndifreq ndifreq;
1514 unsigned int ifindex;
1516 if (strcasecmp(ifname, "delete") == 0)
1519 if ((ifindex = if_nametoindex(ifname)) == 0)
1520 err(1, "failed to resolve i/f index for %s", ifname);
1523 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1526 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1527 ndifreq.ifindex = ifindex;
1529 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1530 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1538 struct in6_ndifreq ndifreq;
1539 char ifname[IFNAMSIZ+8];
1541 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1544 memset(&ndifreq, 0, sizeof(ndifreq));
1545 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1547 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1548 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1550 if (ndifreq.ifindex == 0)
1551 printf("No default interface.\n");
1553 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1554 err(1, "failed to resolve ifname for index %lu",
1556 printf("ND default interface = %s\n", ifname);
1567 static char result[256];
1568 int days, hours, mins, secs;
1571 char *ep = &result[sizeof(result)];
1574 days = total / 3600 / 24;
1575 hours = (total / 3600) % 24;
1576 mins = (total / 60) % 60;
1581 n = snprintf(p, ep - p, "%dd", days);
1582 if (n < 0 || n >= ep - p)
1586 if (!first || hours) {
1588 n = snprintf(p, ep - p, "%dh", hours);
1589 if (n < 0 || n >= ep - p)
1593 if (!first || mins) {
1595 n = snprintf(p, ep - p, "%dm", mins);
1596 if (n < 0 || n >= ep - p)
1600 snprintf(p, ep - p, "%ds", secs);
1606 * Print the timestamp
1607 * from tcpdump/util.c
1611 const struct timeval *tvp;
1616 s = (tvp->tv_sec + thiszone) % 86400;
1617 (void)printf("%02d:%02d:%02d.%06u ",
1618 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);