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>
110 #include "gmt2local.h"
112 /* packing rule for routing socket */
114 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
115 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
120 static int32_t thiszone; /* time difference with gmt */
122 static int repeat = 0;
124 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
125 char host_buf[NI_MAXHOST]; /* getnameinfo() */
126 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
128 int main __P((int, char **));
129 int file __P((char *));
130 void getsocket __P((void));
131 int set __P((int, char **));
132 void get __P((char *));
133 int delete __P((char *));
134 void dump __P((struct in6_addr *, int));
135 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *, int, int));
136 static char *ether_str __P((struct sockaddr_dl *));
137 int ndp_ether_aton __P((char *, u_char *));
138 void usage __P((void));
139 int rtmsg __P((int));
140 void ifinfo __P((char *, int, char **));
141 void rtrlist __P((void));
142 void plist __P((void));
143 void pfx_flush __P((void));
144 void rtr_flush __P((void));
145 void harmonize_rtr __P((void));
146 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
147 static void getdefif __P((void));
148 static void setdefif __P((char *));
150 static char *sec2str __P((time_t));
151 static char *ether_str __P((struct sockaddr_dl *));
152 static void ts_print __P((const struct timeval *));
154 #ifdef ICMPV6CTL_ND6_DRLIST
155 static char *rtpref_str[] = {
174 thiszone = gmt2local(0);
175 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
215 repeat = atoi(optarg);
235 dump(0, mode == 'c');
245 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
249 } else if (argc == 1) {
250 if (strcmp(*argv, "delete") == 0 ||
251 if_nametoindex(*argv))
254 errx(1, "invalid interface %s", *argv);
256 getdefif(); /* always call it to print the result */
259 errx(1, "not supported yet");
270 ifinfo(arg, argc, argv);
280 if (argc < 2 || argc > 4)
282 exit(set(argc, argv) ? 1 : 0);
316 * Process a file to set standard ndp entries
324 char line[100], arg[5][50], *args[5];
326 if ((fp = fopen(name, "r")) == NULL) {
327 fprintf(stderr, "ndp: cannot open %s\n", name);
330 args[0] = &arg[0][0];
331 args[1] = &arg[1][0];
332 args[2] = &arg[2][0];
333 args[3] = &arg[3][0];
334 args[4] = &arg[4][0];
336 while (fgets(line, sizeof(line), fp) != NULL) {
337 i = sscanf(line, "%49s %49s %49s %49s %49s",
338 arg[0], arg[1], arg[2], arg[3], arg[4]);
340 fprintf(stderr, "ndp: bad line: %s\n", line);
355 s = socket(PF_ROUTE, SOCK_RAW, 0);
363 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
364 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
365 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
366 int expire_time, flags, found_entry;
368 struct rt_msghdr m_rtm;
373 * Set an individual neighbor cache entry
380 register struct sockaddr_in6 *sin = &sin_m;
381 register struct sockaddr_dl *sdl;
382 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
383 struct addrinfo hints, *res;
386 char *host = argv[0], *eaddr = argv[1];
394 bzero(&hints, sizeof(hints));
395 hints.ai_family = AF_INET6;
396 gai_error = getaddrinfo(host, NULL, &hints, &res);
398 fprintf(stderr, "ndp: %s: %s\n", host,
399 gai_strerror(gai_error));
402 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
404 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
405 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
406 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
409 ea = (u_char *)LLADDR(&sdl_m);
410 if (ndp_ether_aton(eaddr, ea) == 0)
412 flags = expire_time = 0;
414 if (strncmp(argv[0], "temp", 4) == 0) {
417 gettimeofday(&time, 0);
418 expire_time = time.tv_sec + 20 * 60;
419 } else if (strncmp(argv[0], "proxy", 5) == 0)
420 flags |= RTF_ANNOUNCE;
423 if (rtmsg(RTM_GET) < 0) {
424 errx(1, "RTM_GET(%s) failed", host);
427 sin = (struct sockaddr_in6 *)(rtm + 1);
428 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
429 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
430 if (sdl->sdl_family == AF_LINK &&
431 (rtm->rtm_flags & RTF_LLINFO) &&
432 !(rtm->rtm_flags & RTF_GATEWAY)) {
433 switch (sdl->sdl_type) {
434 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
435 case IFT_ISO88024: case IFT_ISO88025:
440 * IPv4 arp command retries with sin_other = SIN_PROXY here.
442 fprintf(stderr, "set: cannot configure a new entry\n");
447 if (sdl->sdl_family != AF_LINK) {
448 printf("cannot intuit interface index and type for %s\n", host);
451 sdl_m.sdl_type = sdl->sdl_type;
452 sdl_m.sdl_index = sdl->sdl_index;
453 return (rtmsg(RTM_ADD));
457 * Display an individual neighbor cache entry
463 struct sockaddr_in6 *sin = &sin_m;
464 struct addrinfo hints, *res;
468 bzero(&hints, sizeof(hints));
469 hints.ai_family = AF_INET6;
470 gai_error = getaddrinfo(host, NULL, &hints, &res);
472 fprintf(stderr, "ndp: %s: %s\n", host,
473 gai_strerror(gai_error));
476 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
478 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
479 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
480 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
483 dump(&sin->sin6_addr, 0);
484 if (found_entry == 0) {
485 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
486 sizeof(host_buf), NULL ,0,
487 (nflag ? NI_NUMERICHOST : 0));
488 printf("%s (%s) -- no entry\n", host, host_buf);
494 * Delete a neighbor cache entry
500 struct sockaddr_in6 *sin = &sin_m;
501 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
502 struct sockaddr_dl *sdl;
503 struct addrinfo hints, *res;
509 bzero(&hints, sizeof(hints));
510 hints.ai_family = AF_INET6;
511 gai_error = getaddrinfo(host, NULL, &hints, &res);
513 fprintf(stderr, "ndp: %s: %s\n", host,
514 gai_strerror(gai_error));
517 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
519 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
520 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
521 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
524 if (rtmsg(RTM_GET) < 0) {
525 errx(1, "RTM_GET(%s) failed", host);
528 sin = (struct sockaddr_in6 *)(rtm + 1);
529 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
530 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
531 if (sdl->sdl_family == AF_LINK &&
532 (rtm->rtm_flags & RTF_LLINFO) &&
533 !(rtm->rtm_flags & RTF_GATEWAY)) {
537 * IPv4 arp command retries with sin_other = SIN_PROXY here.
539 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
544 if (sdl->sdl_family != AF_LINK) {
545 printf("cannot locate %s\n", host);
548 if (rtmsg(RTM_DELETE) == 0) {
549 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
552 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
553 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
554 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
557 getnameinfo((struct sockaddr *)&s6,
558 s6.sin6_len, host_buf,
559 sizeof(host_buf), NULL, 0,
560 (nflag ? NI_NUMERICHOST : 0));
561 printf("%s (%s) deleted\n", host, host_buf);
572 * Dump the entire neighbor cache
576 struct in6_addr *addr;
581 char *lim, *buf, *next;
582 struct rt_msghdr *rtm;
583 struct sockaddr_in6 *sin;
584 struct sockaddr_dl *sdl;
586 struct in6_nbrinfo *nbi;
595 if (!tflag && !cflag)
596 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
597 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
598 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
605 mib[4] = NET_RT_FLAGS;
607 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
608 err(1, "sysctl(PF_ROUTE estimate)");
610 if ((buf = malloc(needed)) == NULL)
612 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
613 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
618 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
619 int isrouter = 0, prbs = 0;
621 rtm = (struct rt_msghdr *)next;
622 sin = (struct sockaddr_in6 *)(rtm + 1);
623 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
626 * Some OSes can produce a route that has the LINK flag but
627 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
628 * and BSD/OS, where xx is not the interface identifier on
629 * lo0). Such routes entry would annoy getnbrinfo() below,
631 * XXX: such routes should have the GATEWAY flag, not the
632 * LINK flag. However, there is rotten routing software
633 * that advertises all routes that have the GATEWAY flag.
634 * Thus, KAME kernel intentionally does not set the LINK flag.
635 * What is to be fixed is not ndp, but such routing software
636 * (and the kernel workaround)...
638 if (sdl->sdl_family != AF_LINK)
641 if (!(rtm->rtm_flags & RTF_HOST))
645 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
648 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
650 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
651 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
652 /* XXX: should scope id be filled in the kernel? */
653 if (sin->sin6_scope_id == 0)
654 sin->sin6_scope_id = sdl->sdl_index;
656 /* KAME specific hack; removed the embedded id */
657 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
660 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
661 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
664 if (rtm->rtm_flags & RTF_WASCLONED)
666 #elif defined(RTF_CLONED)
667 if (rtm->rtm_flags & RTF_CLONED)
674 gettimeofday(&time, 0);
678 addrwidth = strlen(host_buf);
679 if (addrwidth < W_ADDR)
681 llwidth = strlen(ether_str(sdl));
682 if (W_ADDR + W_LL - addrwidth > llwidth)
683 llwidth = W_ADDR + W_LL - addrwidth;
684 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
687 ifwidth = strlen(ifname);
688 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
689 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
691 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
692 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
694 /* Print neighbor discovery specific informations */
695 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
697 if (nbi->expire > time.tv_sec) {
699 sec2str(nbi->expire - time.tv_sec));
700 } else if (nbi->expire == 0)
701 printf(" %-9.9s", "permanent");
703 printf(" %-9.9s", "expired");
705 switch (nbi->state) {
706 case ND6_LLINFO_NOSTATE:
709 #ifdef ND6_LLINFO_WAITDELETE
710 case ND6_LLINFO_WAITDELETE:
714 case ND6_LLINFO_INCOMPLETE:
717 case ND6_LLINFO_REACHABLE:
720 case ND6_LLINFO_STALE:
723 case ND6_LLINFO_DELAY:
726 case ND6_LLINFO_PROBE:
734 isrouter = nbi->isrouter;
737 warnx("failed to get neighbor information");
742 * other flags. R: router, P: proxy, W: ??
744 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
745 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
747 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
749 sin = (struct sockaddr_in6 *)
750 (sdl->sdl_len + (char *)sdl);
751 #if 0 /* W and P are mystery even for us */
752 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
754 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
755 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
756 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
758 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
760 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
763 printf(" %s", flgbuf);
781 static struct in6_nbrinfo *
782 getnbrinfo(addr, ifindex, warning)
783 struct in6_addr *addr;
787 static struct in6_nbrinfo nbi;
790 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
793 bzero(&nbi, sizeof(nbi));
794 if_indextoname(ifindex, nbi.ifname);
796 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
798 warn("ioctl(SIOCGNBRINFO_IN6)");
809 struct sockaddr_dl *sdl;
811 static char hbuf[NI_MAXHOST];
815 cp = (u_char *)LLADDR(sdl);
816 snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x",
817 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
819 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
831 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
832 &o[3], &o[4], &o[5]);
834 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
837 for (i = 0; i < 6; i++)
845 printf("usage: ndp [-nt] hostname\n");
846 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
847 printf(" ndp [-nt] -A wait\n");
848 printf(" ndp [-nt] -d hostname\n");
849 printf(" ndp [-nt] -f filename\n");
850 printf(" ndp [-nt] -i interface [flags...]\n");
851 #ifdef SIOCSDEFIFACE_IN6
852 printf(" ndp [-nt] -I [interface|delete]\n");
854 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
864 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
865 register char *cp = m_rtmsg.m_space;
869 if (cmd == RTM_DELETE)
871 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
872 rtm->rtm_flags = flags;
873 rtm->rtm_version = RTM_VERSION;
877 fprintf(stderr, "ndp: internal wrong cmd\n");
880 rtm->rtm_addrs |= RTA_GATEWAY;
882 rtm->rtm_rmx.rmx_expire = expire_time;
883 rtm->rtm_inits = RTV_EXPIRE;
885 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
886 #if 0 /* we don't support ipv6addr/128 type proxying */
887 if (rtm->rtm_flags & RTF_ANNOUNCE) {
888 rtm->rtm_flags &= ~RTF_HOST;
889 rtm->rtm_addrs |= RTA_NETMASK;
894 rtm->rtm_addrs |= RTA_DST;
896 #define NEXTADDR(w, s) \
897 if (rtm->rtm_addrs & (w)) { \
898 bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
900 NEXTADDR(RTA_DST, sin_m);
901 NEXTADDR(RTA_GATEWAY, sdl_m);
902 #if 0 /* we don't support ipv6addr/128 type proxying */
903 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
904 NEXTADDR(RTA_NETMASK, so_mask);
907 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
910 rtm->rtm_seq = ++seq;
912 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
913 if (errno != ESRCH || cmd != RTM_DELETE) {
914 err(1, "writing to routing socket");
919 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
920 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
922 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
928 ifinfo(ifname, argc, argv)
933 struct in6_ndireq nd;
936 #ifdef IPV6CTL_USETEMPADDR
940 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
944 bzero(&nd, sizeof(nd));
945 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
946 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
947 err(1, "ioctl(SIOCGIFINFO_IN6)");
952 for (i = 0; i < argc; i++) {
961 #define SETFLAG(s, f) \
963 if (strcmp(cp, (s)) == 0) {\
971 * XXX: this macro is not 100% correct, in that it matches "nud" against
972 * "nudbogus". But we just let it go since this is minor.
974 #define SETVALUE(f, v) \
977 unsigned long newval; \
978 v = 0; /* unspecified */ \
979 if (strncmp(cp, f, strlen(f)) == 0) { \
980 valptr = strchr(cp, '='); \
981 if (valptr == NULL) \
982 err(1, "syntax error in %s field", (f)); \
984 newval = strtoul(++valptr, NULL, 0); \
986 err(1, "syntax error in %s's value", (f)); \
991 SETFLAG("disabled", ND6_IFF_IFDISABLED);
992 SETFLAG("nud", ND6_IFF_PERFORMNUD);
993 #ifdef ND6_IFF_ACCEPT_RTADV
994 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
996 #ifdef ND6_IFF_PREFER_SOURCE
997 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
999 SETVALUE("basereachable", ND.basereachable);
1000 SETVALUE("retrans", ND.retrans);
1001 SETVALUE("curhlim", ND.chlim);
1003 ND.flags = newflags;
1004 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
1005 err(1, "ioctl(SIOCSIFINFO_IN6)");
1012 if (!ND.initialized) {
1013 errx(1, "%s: not initialized yet", ifname);
1017 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
1018 err(1, "ioctl(SIOCGIFINFO_IN6)");
1021 printf("linkmtu=%d", ND.linkmtu);
1022 printf(", maxmtu=%d", ND.maxmtu);
1023 printf(", curhlim=%d", ND.chlim);
1024 printf(", basereachable=%ds%dms",
1025 ND.basereachable / 1000, ND.basereachable % 1000);
1026 printf(", reachable=%ds", ND.reachable);
1027 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1028 #ifdef IPV6CTL_USETEMPADDR
1029 memset(nullbuf, 0, sizeof(nullbuf));
1030 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1034 for (i = 0; i < 3; i++) {
1037 printf("\nRandom seed(0): ");
1038 rbuf = ND.randomseed0;
1041 printf("\nRandom seed(1): ");
1042 rbuf = ND.randomseed1;
1045 printf("\nRandom ID: ");
1049 errx(1, "impossible case for tempaddr display");
1051 for (j = 0; j < 8; j++)
1052 printf("%02x", rbuf[j]);
1057 printf("\nFlags: ");
1058 #ifdef ND6_IFF_IFDISABLED
1059 if ((ND.flags & ND6_IFF_IFDISABLED))
1060 printf("disabled ");
1062 if ((ND.flags & ND6_IFF_PERFORMNUD))
1064 #ifdef ND6_IFF_ACCEPT_RTADV
1065 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1066 printf("accept_rtadv ");
1068 #ifdef ND6_IFF_PREFER_SOURCE
1069 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1070 printf("prefer_source ");
1079 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1080 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1086 #ifdef ICMPV6CTL_ND6_DRLIST
1087 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1089 struct in6_defrouter *p, *ep;
1091 struct timeval time;
1093 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1094 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1104 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1105 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1109 ep = (struct in6_defrouter *)(buf + l);
1110 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1113 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1114 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1115 (nflag ? NI_NUMERICHOST : 0)) != 0)
1116 strlcpy(host_buf, "?", sizeof(host_buf));
1118 printf("%s if=%s", host_buf,
1119 if_indextoname(p->if_index, ifix_buf));
1120 printf(", flags=%s%s",
1121 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1122 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1123 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1124 printf(", pref=%s", rtpref_str[rtpref]);
1126 gettimeofday(&time, 0);
1128 printf(", expire=Never\n");
1130 printf(", expire=%s\n",
1131 sec2str(p->expire - time.tv_sec));
1135 struct in6_drlist dr;
1137 struct timeval time;
1139 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1143 bzero(&dr, sizeof(dr));
1144 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1145 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1146 err(1, "ioctl(SIOCGDRLST_IN6)");
1149 #define DR dr.defrouter[i]
1150 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1151 struct sockaddr_in6 sin6;
1153 bzero(&sin6, sizeof(sin6));
1154 sin6.sin6_family = AF_INET6;
1155 sin6.sin6_len = sizeof(sin6);
1156 sin6.sin6_addr = DR.rtaddr;
1157 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1158 sizeof(host_buf), NULL, 0,
1159 (nflag ? NI_NUMERICHOST : 0));
1161 printf("%s if=%s", host_buf,
1162 if_indextoname(DR.if_index, ifix_buf));
1163 printf(", flags=%s%s",
1164 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1165 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1166 gettimeofday(&time, 0);
1168 printf(", expire=Never\n");
1170 printf(", expire=%s\n",
1171 sec2str(DR.expire - time.tv_sec));
1181 #ifdef ICMPV6CTL_ND6_PRLIST
1182 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1184 struct in6_prefix *p, *ep, *n;
1185 struct sockaddr_in6 *advrtr;
1187 struct timeval time;
1188 const int niflags = NI_NUMERICHOST;
1189 int ninflags = nflag ? NI_NUMERICHOST : 0;
1190 char namebuf[NI_MAXHOST];
1192 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1193 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1201 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1202 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1206 ep = (struct in6_prefix *)(buf + l);
1207 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1208 advrtr = (struct sockaddr_in6 *)(p + 1);
1209 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1211 if (getnameinfo((struct sockaddr *)&p->prefix,
1212 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1213 NULL, 0, niflags) != 0)
1214 strlcpy(namebuf, "?", sizeof(namebuf));
1215 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1216 if_indextoname(p->if_index, ifix_buf));
1218 gettimeofday(&time, 0);
1220 * meaning of fields, especially flags, is very different
1221 * by origin. notify the difference to the users.
1223 printf("flags=%s%s%s%s%s",
1224 p->raflags.onlink ? "L" : "",
1225 p->raflags.autonomous ? "A" : "",
1226 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1227 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1229 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1234 if (p->vltime == ND6_INFINITE_LIFETIME)
1235 printf(" vltime=infinity");
1237 printf(" vltime=%lu", (unsigned long)p->vltime);
1238 if (p->pltime == ND6_INFINITE_LIFETIME)
1239 printf(", pltime=infinity");
1241 printf(", pltime=%lu", (unsigned long)p->pltime);
1243 printf(", expire=Never");
1244 else if (p->expire >= time.tv_sec)
1245 printf(", expire=%s",
1246 sec2str(p->expire - time.tv_sec));
1248 printf(", expired");
1249 printf(", ref=%d", p->refcnt);
1252 * "advertising router" list is meaningful only if the prefix
1253 * information is from RA.
1257 struct sockaddr_in6 *sin6;
1260 printf(" advertised by\n");
1261 for (j = 0; j < p->advrtrs; j++) {
1262 struct in6_nbrinfo *nbi;
1264 if (getnameinfo((struct sockaddr *)sin6,
1265 sin6->sin6_len, namebuf, sizeof(namebuf),
1266 NULL, 0, ninflags) != 0)
1267 strlcpy(namebuf, "?", sizeof(namebuf));
1268 printf(" %s", namebuf);
1270 nbi = getnbrinfo(&sin6->sin6_addr,
1273 switch (nbi->state) {
1274 case ND6_LLINFO_REACHABLE:
1275 case ND6_LLINFO_STALE:
1276 case ND6_LLINFO_DELAY:
1277 case ND6_LLINFO_PROBE:
1278 printf(" (reachable)\n");
1281 printf(" (unreachable)\n");
1284 printf(" (no neighbor state)\n");
1288 printf(" No advertising router\n");
1292 struct in6_prlist pr;
1294 struct timeval time;
1296 gettimeofday(&time, 0);
1298 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1302 bzero(&pr, sizeof(pr));
1303 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1304 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1305 err(1, "ioctl(SIOCGPRLST_IN6)");
1308 #define PR pr.prefix[i]
1309 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1310 struct sockaddr_in6 p6;
1311 char namebuf[NI_MAXHOST];
1317 memset(&p6, 0, sizeof(p6));
1318 p6.sin6_family = AF_INET6;
1319 p6.sin6_len = sizeof(p6);
1320 p6.sin6_addr = PR.prefix;
1324 * copy link index to sin6_scope_id field.
1325 * XXX: KAME specific.
1327 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1330 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1332 linkid = ntohs(linkid);
1333 p6.sin6_scope_id = linkid;
1334 p6.sin6_addr.s6_addr[2] = 0;
1335 p6.sin6_addr.s6_addr[3] = 0;
1338 niflags = NI_NUMERICHOST;
1339 if (getnameinfo((struct sockaddr *)&p6,
1340 sizeof(p6), namebuf, sizeof(namebuf),
1341 NULL, 0, niflags)) {
1342 warnx("getnameinfo failed");
1345 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1346 if_indextoname(PR.if_index, ifix_buf));
1348 gettimeofday(&time, 0);
1350 * meaning of fields, especially flags, is very different
1351 * by origin. notify the difference to the users.
1355 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1358 printf("flags=%s%s%s%s%s",
1359 PR.raflags.onlink ? "L" : "",
1360 PR.raflags.autonomous ? "A" : "",
1361 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1362 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1364 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1370 printf("flags=%s%s",
1371 PR.raflags.onlink ? "L" : "",
1372 PR.raflags.autonomous ? "A" : "");
1374 if (PR.vltime == ND6_INFINITE_LIFETIME)
1375 printf(" vltime=infinity");
1377 printf(" vltime=%lu", PR.vltime);
1378 if (PR.pltime == ND6_INFINITE_LIFETIME)
1379 printf(", pltime=infinity");
1381 printf(", pltime=%lu", PR.pltime);
1383 printf(", expire=Never");
1384 else if (PR.expire >= time.tv_sec)
1385 printf(", expire=%s",
1386 sec2str(PR.expire - time.tv_sec));
1388 printf(", expired");
1390 printf(", ref=%d", PR.refcnt);
1393 switch (PR.origin) {
1395 printf(", origin=RA");
1398 printf(", origin=RR");
1400 case PR_ORIG_STATIC:
1401 printf(", origin=static");
1403 case PR_ORIG_KERNEL:
1404 printf(", origin=kernel");
1407 printf(", origin=?");
1413 * "advertising router" list is meaningful only if the prefix
1414 * information is from RA.
1416 if (0 && /* prefix origin is almost obsolted */
1417 PR.origin != PR_ORIG_RA)
1419 else if (PR.advrtrs) {
1421 printf(" advertised by\n");
1422 for (j = 0; j < PR.advrtrs; j++) {
1423 struct sockaddr_in6 sin6;
1424 struct in6_nbrinfo *nbi;
1426 bzero(&sin6, sizeof(sin6));
1427 sin6.sin6_family = AF_INET6;
1428 sin6.sin6_len = sizeof(sin6);
1429 sin6.sin6_addr = PR.advrtr[j];
1430 sin6.sin6_scope_id = PR.if_index; /* XXX */
1431 getnameinfo((struct sockaddr *)&sin6,
1432 sin6.sin6_len, host_buf,
1433 sizeof(host_buf), NULL, 0,
1434 (nflag ? NI_NUMERICHOST : 0));
1435 printf(" %s", host_buf);
1437 nbi = getnbrinfo(&sin6.sin6_addr,
1440 switch (nbi->state) {
1441 case ND6_LLINFO_REACHABLE:
1442 case ND6_LLINFO_STALE:
1443 case ND6_LLINFO_DELAY:
1444 case ND6_LLINFO_PROBE:
1445 printf(" (reachable)\n");
1448 printf(" (unreachable)\n");
1451 printf(" (no neighbor state)\n");
1453 if (PR.advrtrs > DRLSTSIZ)
1454 printf(" and %d routers\n",
1455 PR.advrtrs - DRLSTSIZ);
1457 printf(" No advertising router\n");
1467 char dummyif[IFNAMSIZ+8];
1470 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1472 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1473 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1474 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1480 char dummyif[IFNAMSIZ+8];
1483 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1485 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1486 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1487 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1495 char dummyif[IFNAMSIZ+8];
1498 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1500 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1501 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1502 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1507 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1512 struct in6_ndifreq ndifreq;
1513 unsigned int ifindex;
1515 if (strcasecmp(ifname, "delete") == 0)
1518 if ((ifindex = if_nametoindex(ifname)) == 0)
1519 err(1, "failed to resolve i/f index for %s", ifname);
1522 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1525 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1526 ndifreq.ifindex = ifindex;
1528 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1529 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1537 struct in6_ndifreq ndifreq;
1538 char ifname[IFNAMSIZ+8];
1540 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1543 memset(&ndifreq, 0, sizeof(ndifreq));
1544 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1546 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1547 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1549 if (ndifreq.ifindex == 0)
1550 printf("No default interface.\n");
1552 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1553 err(1, "failed to resolve ifname for index %lu",
1555 printf("ND default interface = %s\n", ifname);
1566 static char result[256];
1567 int days, hours, mins, secs;
1570 char *ep = &result[sizeof(result)];
1573 days = total / 3600 / 24;
1574 hours = (total / 3600) % 24;
1575 mins = (total / 60) % 60;
1580 n = snprintf(p, ep - p, "%dd", days);
1581 if (n < 0 || n >= ep - p)
1585 if (!first || hours) {
1587 n = snprintf(p, ep - p, "%dh", hours);
1588 if (n < 0 || n >= ep - p)
1592 if (!first || mins) {
1594 n = snprintf(p, ep - p, "%dm", mins);
1595 if (n < 0 || n >= ep - p)
1599 snprintf(p, ep - p, "%ds", secs);
1605 * Print the timestamp
1606 * from tcpdump/util.c
1610 const struct timeval *tvp;
1615 s = (tvp->tv_sec + thiszone) % 86400;
1616 (void)printf("%02d:%02d:%02d.%06u ",
1617 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);