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))
118 #define NEXTADDR(w, s) \
119 if (rtm->rtm_addrs & (w)) { \
120 bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
126 static int32_t thiszone; /* time difference with gmt */
128 static int repeat = 0;
130 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
131 char host_buf[NI_MAXHOST]; /* getnameinfo() */
132 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
134 int main(int, char **);
135 static int file(char *);
136 void getsocket(void);
137 int set(int, char **);
140 void dump(struct in6_addr *, int);
141 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
142 static char *ether_str(struct sockaddr_dl *);
143 int ndp_ether_aton(char *, u_char *);
146 void ifinfo(char *, int, char **);
149 void pfx_flush(void);
150 void rtr_flush(void);
151 void harmonize_rtr(void);
152 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
153 static void getdefif(void);
154 static void setdefif(char *);
156 static char *sec2str(time_t);
157 static void ts_print(const struct timeval *);
159 #ifdef ICMPV6CTL_ND6_DRLIST
160 static char *rtpref_str[] = {
179 thiszone = gmt2local(0);
180 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
199 exit(file(optarg) ? 1 : 0);
221 repeat = atoi(optarg);
241 dump(0, mode == 'c');
251 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
255 } else if (argc == 1) {
256 if (strcmp(*argv, "delete") == 0 ||
257 if_nametoindex(*argv))
260 errx(1, "invalid interface %s", *argv);
262 getdefif(); /* always call it to print the result */
265 errx(1, "not supported yet");
276 ifinfo(arg, argc, argv);
286 if (argc < 2 || argc > 4)
288 exit(set(argc, argv) ? 1 : 0);
322 * Process a file to set standard ndp entries
330 char line[100], arg[5][50], *args[5], *p;
332 if ((fp = fopen(name, "r")) == NULL)
333 err(1, "cannot open %s", name);
334 args[0] = &arg[0][0];
335 args[1] = &arg[1][0];
336 args[2] = &arg[2][0];
337 args[3] = &arg[3][0];
338 args[4] = &arg[4][0];
340 while (fgets(line, sizeof(line), fp) != NULL) {
341 if ((p = strchr(line, '#')) != NULL)
343 for (p = line; isblank(*p); p++);
344 if (*p == '\n' || *p == '\0')
346 i = sscanf(line, "%49s %49s %49s %49s %49s",
347 arg[0], arg[1], arg[2], arg[3], arg[4]);
349 warnx("bad line: %s", line);
364 s = socket(PF_ROUTE, SOCK_RAW, 0);
372 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
373 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
374 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
376 int flags, found_entry;
378 struct rt_msghdr m_rtm;
383 * Set an individual neighbor cache entry
390 register struct sockaddr_in6 *sin = &sin_m;
391 register struct sockaddr_dl *sdl;
392 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
393 struct addrinfo hints, *res;
396 char *host = argv[0], *eaddr = argv[1];
404 bzero(&hints, sizeof(hints));
405 hints.ai_family = AF_INET6;
406 gai_error = getaddrinfo(host, NULL, &hints, &res);
408 fprintf(stderr, "ndp: %s: %s\n", host,
409 gai_strerror(gai_error));
412 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
414 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
415 ea = (u_char *)LLADDR(&sdl_m);
416 if (ndp_ether_aton(eaddr, ea) == 0)
418 flags = expire_time = 0;
420 if (strncmp(argv[0], "temp", 4) == 0) {
423 gettimeofday(&now, 0);
424 expire_time = now.tv_sec + 20 * 60;
425 } else if (strncmp(argv[0], "proxy", 5) == 0)
426 flags |= RTF_ANNOUNCE;
429 if (rtmsg(RTM_GET) < 0) {
430 errx(1, "RTM_GET(%s) failed", host);
433 sin = (struct sockaddr_in6 *)(rtm + 1);
434 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
435 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
436 if (sdl->sdl_family == AF_LINK &&
437 !(rtm->rtm_flags & RTF_GATEWAY)) {
438 switch (sdl->sdl_type) {
439 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
440 case IFT_ISO88024: case IFT_ISO88025:
441 case IFT_L2VLAN: case IFT_BRIDGE:
445 fprintf(stderr, "set: cannot configure a new entry\n");
450 if (sdl->sdl_family != AF_LINK) {
451 printf("cannot intuit interface index and type for %s\n", host);
454 sdl_m.sdl_type = sdl->sdl_type;
455 sdl_m.sdl_index = sdl->sdl_index;
456 return (rtmsg(RTM_ADD));
460 * Display an individual neighbor cache entry
466 struct sockaddr_in6 *sin = &sin_m;
467 struct addrinfo hints, *res;
471 bzero(&hints, sizeof(hints));
472 hints.ai_family = AF_INET6;
473 gai_error = getaddrinfo(host, NULL, &hints, &res);
475 fprintf(stderr, "ndp: %s: %s\n", host,
476 gai_strerror(gai_error));
479 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
480 dump(&sin->sin6_addr, 0);
481 if (found_entry == 0) {
482 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
483 sizeof(host_buf), NULL ,0,
484 (nflag ? NI_NUMERICHOST : 0));
485 printf("%s (%s) -- no entry\n", host, host_buf);
491 * Delete a neighbor cache entry
497 struct sockaddr_in6 *sin = &sin_m;
498 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
499 register char *cp = m_rtmsg.m_space;
500 struct sockaddr_dl *sdl;
501 struct addrinfo hints, *res;
507 bzero(&hints, sizeof(hints));
508 hints.ai_family = AF_INET6;
509 gai_error = getaddrinfo(host, NULL, &hints, &res);
511 fprintf(stderr, "ndp: %s: %s\n", host,
512 gai_strerror(gai_error));
515 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
517 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
518 if (rtmsg(RTM_GET) < 0) {
519 errx(1, "RTM_GET(%s) failed", host);
522 sin = (struct sockaddr_in6 *)(rtm + 1);
523 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
524 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
525 if (sdl->sdl_family == AF_LINK &&
526 !(rtm->rtm_flags & RTF_GATEWAY)) {
529 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
534 if (sdl->sdl_family != AF_LINK) {
535 printf("cannot locate %s\n", host);
539 * need to reinit the field because it has rt_key
540 * but we want the actual address
542 NEXTADDR(RTA_DST, sin_m);
543 rtm->rtm_flags |= RTF_LLDATA;
544 if (rtmsg(RTM_DELETE) == 0) {
545 getnameinfo((struct sockaddr *)sin,
546 sin->sin6_len, host_buf,
547 sizeof(host_buf), NULL, 0,
548 (nflag ? NI_NUMERICHOST : 0));
549 printf("%s (%s) deleted\n", host, host_buf);
560 * Dump the entire neighbor cache
564 struct in6_addr *addr;
569 char *lim, *buf, *next;
570 struct rt_msghdr *rtm;
571 struct sockaddr_in6 *sin;
572 struct sockaddr_dl *sdl;
574 struct in6_nbrinfo *nbi;
583 if (!tflag && !cflag)
584 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
585 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
586 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
593 mib[4] = NET_RT_FLAGS;
599 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
600 err(1, "sysctl(PF_ROUTE estimate)");
602 if ((buf = malloc(needed)) == NULL)
604 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
605 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
610 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
611 int isrouter = 0, prbs = 0;
613 rtm = (struct rt_msghdr *)next;
614 sin = (struct sockaddr_in6 *)(rtm + 1);
615 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
618 * Some OSes can produce a route that has the LINK flag but
619 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
620 * and BSD/OS, where xx is not the interface identifier on
621 * lo0). Such routes entry would annoy getnbrinfo() below,
623 * XXX: such routes should have the GATEWAY flag, not the
624 * LINK flag. However, there is rotten routing software
625 * that advertises all routes that have the GATEWAY flag.
626 * Thus, KAME kernel intentionally does not set the LINK flag.
627 * What is to be fixed is not ndp, but such routing software
628 * (and the kernel workaround)...
630 if (sdl->sdl_family != AF_LINK)
633 if (!(rtm->rtm_flags & RTF_HOST))
637 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
640 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
642 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
643 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
644 /* XXX: should scope id be filled in the kernel? */
645 if (sin->sin6_scope_id == 0)
646 sin->sin6_scope_id = sdl->sdl_index;
648 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
649 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
652 if (rtm->rtm_flags & RTF_WASCLONED)
654 #elif defined(RTF_CLONED)
655 if (rtm->rtm_flags & RTF_CLONED)
662 gettimeofday(&now, 0);
666 addrwidth = strlen(host_buf);
667 if (addrwidth < W_ADDR)
669 llwidth = strlen(ether_str(sdl));
670 if (W_ADDR + W_LL - addrwidth > llwidth)
671 llwidth = W_ADDR + W_LL - addrwidth;
672 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
675 ifwidth = strlen(ifname);
676 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
677 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
679 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
680 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
682 /* Print neighbor discovery specific informations */
683 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
685 if (nbi->expire > now.tv_sec) {
687 sec2str(nbi->expire - now.tv_sec));
688 } else if (nbi->expire == 0)
689 printf(" %-9.9s", "permanent");
691 printf(" %-9.9s", "expired");
693 switch (nbi->state) {
694 case ND6_LLINFO_NOSTATE:
697 #ifdef ND6_LLINFO_WAITDELETE
698 case ND6_LLINFO_WAITDELETE:
702 case ND6_LLINFO_INCOMPLETE:
705 case ND6_LLINFO_REACHABLE:
708 case ND6_LLINFO_STALE:
711 case ND6_LLINFO_DELAY:
714 case ND6_LLINFO_PROBE:
722 isrouter = nbi->isrouter;
725 warnx("failed to get neighbor information");
730 * other flags. R: router, P: proxy, W: ??
732 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
733 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
735 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
737 sin = (struct sockaddr_in6 *)
738 (sdl->sdl_len + (char *)sdl);
739 #if 0 /* W and P are mystery even for us */
740 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
742 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
743 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
744 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
746 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
748 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
751 printf(" %s", flgbuf);
769 static struct in6_nbrinfo *
770 getnbrinfo(addr, ifindex, warning)
771 struct in6_addr *addr;
775 static struct in6_nbrinfo nbi;
778 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
781 bzero(&nbi, sizeof(nbi));
782 if_indextoname(ifindex, nbi.ifname);
784 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
786 warn("ioctl(SIOCGNBRINFO_IN6)");
796 ether_str(struct sockaddr_dl *sdl)
798 static char hbuf[NI_MAXHOST];
801 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
802 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
804 } else if (sdl->sdl_alen) {
805 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
806 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
808 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
820 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
821 &o[3], &o[4], &o[5]);
823 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
826 for (i = 0; i < 6; i++)
834 printf("usage: ndp [-nt] hostname\n");
835 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
836 printf(" ndp [-nt] -A wait\n");
837 printf(" ndp [-nt] -d hostname\n");
838 printf(" ndp [-nt] -f filename\n");
839 printf(" ndp [-nt] -i interface [flags...]\n");
840 #ifdef SIOCSDEFIFACE_IN6
841 printf(" ndp [-nt] -I [interface|delete]\n");
843 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
853 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
854 register char *cp = m_rtmsg.m_space;
858 if (cmd == RTM_DELETE)
860 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
861 rtm->rtm_flags = flags;
862 rtm->rtm_version = RTM_VERSION;
866 fprintf(stderr, "ndp: internal wrong cmd\n");
869 rtm->rtm_addrs |= RTA_GATEWAY;
871 rtm->rtm_rmx.rmx_expire = expire_time;
872 rtm->rtm_inits = RTV_EXPIRE;
874 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
875 #if 0 /* we don't support ipv6addr/128 type proxying */
876 if (rtm->rtm_flags & RTF_ANNOUNCE) {
877 rtm->rtm_flags &= ~RTF_HOST;
878 rtm->rtm_addrs |= RTA_NETMASK;
883 rtm->rtm_addrs |= RTA_DST;
886 NEXTADDR(RTA_DST, sin_m);
887 NEXTADDR(RTA_GATEWAY, sdl_m);
888 #if 0 /* we don't support ipv6addr/128 type proxying */
889 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
890 NEXTADDR(RTA_NETMASK, so_mask);
893 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
896 rtm->rtm_seq = ++seq;
898 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
899 if (errno != ESRCH || cmd != RTM_DELETE) {
900 err(1, "writing to routing socket");
905 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
906 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
908 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
914 ifinfo(ifname, argc, argv)
919 struct in6_ndireq nd;
922 #ifdef IPV6CTL_USETEMPADDR
926 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
930 bzero(&nd, sizeof(nd));
931 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
932 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
933 err(1, "ioctl(SIOCGIFINFO_IN6)");
938 for (i = 0; i < argc; i++) {
947 #define SETFLAG(s, f) \
949 if (strcmp(cp, (s)) == 0) {\
957 * XXX: this macro is not 100% correct, in that it matches "nud" against
958 * "nudbogus". But we just let it go since this is minor.
960 #define SETVALUE(f, v) \
963 unsigned long newval; \
964 v = 0; /* unspecified */ \
965 if (strncmp(cp, f, strlen(f)) == 0) { \
966 valptr = strchr(cp, '='); \
967 if (valptr == NULL) \
968 err(1, "syntax error in %s field", (f)); \
970 newval = strtoul(++valptr, NULL, 0); \
972 err(1, "syntax error in %s's value", (f)); \
977 SETFLAG("disabled", ND6_IFF_IFDISABLED);
978 SETFLAG("nud", ND6_IFF_PERFORMNUD);
979 #ifdef ND6_IFF_ACCEPT_RTADV
980 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
982 #ifdef ND6_IFF_AUTO_LINKLOCAL
983 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
985 #ifdef ND6_IFF_NO_PREFER_IFACE
986 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
988 SETVALUE("basereachable", ND.basereachable);
989 SETVALUE("retrans", ND.retrans);
990 SETVALUE("curhlim", ND.chlim);
993 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
994 err(1, "ioctl(SIOCSIFINFO_IN6)");
1001 if (!ND.initialized) {
1002 errx(1, "%s: not initialized yet", ifname);
1006 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
1007 err(1, "ioctl(SIOCGIFINFO_IN6)");
1010 printf("linkmtu=%d", ND.linkmtu);
1011 printf(", maxmtu=%d", ND.maxmtu);
1012 printf(", curhlim=%d", ND.chlim);
1013 printf(", basereachable=%ds%dms",
1014 ND.basereachable / 1000, ND.basereachable % 1000);
1015 printf(", reachable=%ds", ND.reachable);
1016 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1017 #ifdef IPV6CTL_USETEMPADDR
1018 memset(nullbuf, 0, sizeof(nullbuf));
1019 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1023 for (i = 0; i < 3; i++) {
1026 printf("\nRandom seed(0): ");
1027 rbuf = ND.randomseed0;
1030 printf("\nRandom seed(1): ");
1031 rbuf = ND.randomseed1;
1034 printf("\nRandom ID: ");
1038 errx(1, "impossible case for tempaddr display");
1040 for (j = 0; j < 8; j++)
1041 printf("%02x", rbuf[j]);
1046 printf("\nFlags: ");
1047 #ifdef ND6_IFF_IFDISABLED
1048 if ((ND.flags & ND6_IFF_IFDISABLED))
1049 printf("disabled ");
1051 if ((ND.flags & ND6_IFF_PERFORMNUD))
1053 #ifdef ND6_IFF_ACCEPT_RTADV
1054 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1055 printf("accept_rtadv ");
1057 #ifdef ND6_IFF_AUTO_LINKLOCAL
1058 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1059 printf("auto_linklocal ");
1061 #ifdef ND6_IFF_NO_PREFER_IFACE
1062 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1063 printf("no_prefer_iface ");
1072 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1073 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1079 #ifdef ICMPV6CTL_ND6_DRLIST
1080 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1082 struct in6_defrouter *p, *ep;
1086 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1087 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1097 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1098 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1102 ep = (struct in6_defrouter *)(buf + l);
1103 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1106 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1107 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1108 (nflag ? NI_NUMERICHOST : 0)) != 0)
1109 strlcpy(host_buf, "?", sizeof(host_buf));
1111 printf("%s if=%s", host_buf,
1112 if_indextoname(p->if_index, ifix_buf));
1113 printf(", flags=%s%s",
1114 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1115 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1116 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1117 printf(", pref=%s", rtpref_str[rtpref]);
1119 gettimeofday(&now, 0);
1121 printf(", expire=Never\n");
1123 printf(", expire=%s\n",
1124 sec2str(p->expire - now.tv_sec));
1128 struct in6_drlist dr;
1132 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1136 bzero(&dr, sizeof(dr));
1137 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1138 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1139 err(1, "ioctl(SIOCGDRLST_IN6)");
1142 #define DR dr.defrouter[i]
1143 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1144 struct sockaddr_in6 sin6;
1146 bzero(&sin6, sizeof(sin6));
1147 sin6.sin6_family = AF_INET6;
1148 sin6.sin6_len = sizeof(sin6);
1149 sin6.sin6_addr = DR.rtaddr;
1150 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1151 sizeof(host_buf), NULL, 0,
1152 (nflag ? NI_NUMERICHOST : 0));
1154 printf("%s if=%s", host_buf,
1155 if_indextoname(DR.if_index, ifix_buf));
1156 printf(", flags=%s%s",
1157 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1158 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1159 gettimeofday(&now, 0);
1161 printf(", expire=Never\n");
1163 printf(", expire=%s\n",
1164 sec2str(DR.expire - now.tv_sec));
1174 #ifdef ICMPV6CTL_ND6_PRLIST
1175 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1177 struct in6_prefix *p, *ep, *n;
1178 struct sockaddr_in6 *advrtr;
1181 const int niflags = NI_NUMERICHOST;
1182 int ninflags = nflag ? NI_NUMERICHOST : 0;
1183 char namebuf[NI_MAXHOST];
1185 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1186 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1194 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1195 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1199 ep = (struct in6_prefix *)(buf + l);
1200 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1201 advrtr = (struct sockaddr_in6 *)(p + 1);
1202 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1204 if (getnameinfo((struct sockaddr *)&p->prefix,
1205 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1206 NULL, 0, niflags) != 0)
1207 strlcpy(namebuf, "?", sizeof(namebuf));
1208 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1209 if_indextoname(p->if_index, ifix_buf));
1211 gettimeofday(&now, 0);
1213 * meaning of fields, especially flags, is very different
1214 * by origin. notify the difference to the users.
1216 printf("flags=%s%s%s%s%s",
1217 p->raflags.onlink ? "L" : "",
1218 p->raflags.autonomous ? "A" : "",
1219 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1220 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1222 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1227 if (p->vltime == ND6_INFINITE_LIFETIME)
1228 printf(" vltime=infinity");
1230 printf(" vltime=%lu", (unsigned long)p->vltime);
1231 if (p->pltime == ND6_INFINITE_LIFETIME)
1232 printf(", pltime=infinity");
1234 printf(", pltime=%lu", (unsigned long)p->pltime);
1236 printf(", expire=Never");
1237 else if (p->expire >= now.tv_sec)
1238 printf(", expire=%s",
1239 sec2str(p->expire - now.tv_sec));
1241 printf(", expired");
1242 printf(", ref=%d", p->refcnt);
1245 * "advertising router" list is meaningful only if the prefix
1246 * information is from RA.
1250 struct sockaddr_in6 *sin6;
1253 printf(" advertised by\n");
1254 for (j = 0; j < p->advrtrs; j++) {
1255 struct in6_nbrinfo *nbi;
1257 if (getnameinfo((struct sockaddr *)sin6,
1258 sin6->sin6_len, namebuf, sizeof(namebuf),
1259 NULL, 0, ninflags) != 0)
1260 strlcpy(namebuf, "?", sizeof(namebuf));
1261 printf(" %s", namebuf);
1263 nbi = getnbrinfo(&sin6->sin6_addr,
1266 switch (nbi->state) {
1267 case ND6_LLINFO_REACHABLE:
1268 case ND6_LLINFO_STALE:
1269 case ND6_LLINFO_DELAY:
1270 case ND6_LLINFO_PROBE:
1271 printf(" (reachable)\n");
1274 printf(" (unreachable)\n");
1277 printf(" (no neighbor state)\n");
1281 printf(" No advertising router\n");
1285 struct in6_prlist pr;
1289 gettimeofday(&now, 0);
1291 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1295 bzero(&pr, sizeof(pr));
1296 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1297 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1298 err(1, "ioctl(SIOCGPRLST_IN6)");
1301 #define PR pr.prefix[i]
1302 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1303 struct sockaddr_in6 p6;
1304 char namebuf[NI_MAXHOST];
1310 memset(&p6, 0, sizeof(p6));
1311 p6.sin6_family = AF_INET6;
1312 p6.sin6_len = sizeof(p6);
1313 p6.sin6_addr = PR.prefix;
1315 niflags = NI_NUMERICHOST;
1316 if (getnameinfo((struct sockaddr *)&p6,
1317 sizeof(p6), namebuf, sizeof(namebuf),
1318 NULL, 0, niflags)) {
1319 warnx("getnameinfo failed");
1322 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1323 if_indextoname(PR.if_index, ifix_buf));
1325 gettimeofday(&now, 0);
1327 * meaning of fields, especially flags, is very different
1328 * by origin. notify the difference to the users.
1332 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1335 printf("flags=%s%s%s%s%s",
1336 PR.raflags.onlink ? "L" : "",
1337 PR.raflags.autonomous ? "A" : "",
1338 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1339 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1341 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1347 printf("flags=%s%s",
1348 PR.raflags.onlink ? "L" : "",
1349 PR.raflags.autonomous ? "A" : "");
1351 if (PR.vltime == ND6_INFINITE_LIFETIME)
1352 printf(" vltime=infinity");
1354 printf(" vltime=%lu", PR.vltime);
1355 if (PR.pltime == ND6_INFINITE_LIFETIME)
1356 printf(", pltime=infinity");
1358 printf(", pltime=%lu", PR.pltime);
1360 printf(", expire=Never");
1361 else if (PR.expire >= now.tv_sec)
1362 printf(", expire=%s",
1363 sec2str(PR.expire - now.tv_sec));
1365 printf(", expired");
1367 printf(", ref=%d", PR.refcnt);
1370 switch (PR.origin) {
1372 printf(", origin=RA");
1375 printf(", origin=RR");
1377 case PR_ORIG_STATIC:
1378 printf(", origin=static");
1380 case PR_ORIG_KERNEL:
1381 printf(", origin=kernel");
1384 printf(", origin=?");
1390 * "advertising router" list is meaningful only if the prefix
1391 * information is from RA.
1393 if (0 && /* prefix origin is almost obsolted */
1394 PR.origin != PR_ORIG_RA)
1396 else if (PR.advrtrs) {
1398 printf(" advertised by\n");
1399 for (j = 0; j < PR.advrtrs; j++) {
1400 struct sockaddr_in6 sin6;
1401 struct in6_nbrinfo *nbi;
1403 bzero(&sin6, sizeof(sin6));
1404 sin6.sin6_family = AF_INET6;
1405 sin6.sin6_len = sizeof(sin6);
1406 sin6.sin6_addr = PR.advrtr[j];
1407 sin6.sin6_scope_id = PR.if_index; /* XXX */
1408 getnameinfo((struct sockaddr *)&sin6,
1409 sin6.sin6_len, host_buf,
1410 sizeof(host_buf), NULL, 0,
1411 (nflag ? NI_NUMERICHOST : 0));
1412 printf(" %s", host_buf);
1414 nbi = getnbrinfo(&sin6.sin6_addr,
1417 switch (nbi->state) {
1418 case ND6_LLINFO_REACHABLE:
1419 case ND6_LLINFO_STALE:
1420 case ND6_LLINFO_DELAY:
1421 case ND6_LLINFO_PROBE:
1422 printf(" (reachable)\n");
1425 printf(" (unreachable)\n");
1428 printf(" (no neighbor state)\n");
1430 if (PR.advrtrs > DRLSTSIZ)
1431 printf(" and %d routers\n",
1432 PR.advrtrs - DRLSTSIZ);
1434 printf(" No advertising router\n");
1444 char dummyif[IFNAMSIZ+8];
1447 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1449 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1450 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1451 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1457 char dummyif[IFNAMSIZ+8];
1460 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1462 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1463 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1464 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1472 char dummyif[IFNAMSIZ+8];
1475 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1477 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1478 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1479 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1484 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1489 struct in6_ndifreq ndifreq;
1490 unsigned int ifindex;
1492 if (strcasecmp(ifname, "delete") == 0)
1495 if ((ifindex = if_nametoindex(ifname)) == 0)
1496 err(1, "failed to resolve i/f index for %s", ifname);
1499 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1502 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1503 ndifreq.ifindex = ifindex;
1505 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1506 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1514 struct in6_ndifreq ndifreq;
1515 char ifname[IFNAMSIZ+8];
1517 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1520 memset(&ndifreq, 0, sizeof(ndifreq));
1521 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1523 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1524 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1526 if (ndifreq.ifindex == 0)
1527 printf("No default interface.\n");
1529 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1530 err(1, "failed to resolve ifname for index %lu",
1532 printf("ND default interface = %s\n", ifname);
1543 static char result[256];
1544 int days, hours, mins, secs;
1547 char *ep = &result[sizeof(result)];
1550 days = total / 3600 / 24;
1551 hours = (total / 3600) % 24;
1552 mins = (total / 60) % 60;
1557 n = snprintf(p, ep - p, "%dd", days);
1558 if (n < 0 || n >= ep - p)
1562 if (!first || hours) {
1564 n = snprintf(p, ep - p, "%dh", hours);
1565 if (n < 0 || n >= ep - p)
1569 if (!first || mins) {
1571 n = snprintf(p, ep - p, "%dm", mins);
1572 if (n < 0 || n >= ep - p)
1576 snprintf(p, ep - p, "%ds", secs);
1582 * Print the timestamp
1583 * from tcpdump/util.c
1587 const struct timeval *tvp;
1592 s = (tvp->tv_sec + thiszone) % 86400;
1593 (void)printf("%02d:%02d:%02d.%06u ",
1594 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);