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 #define NEXTADDR(w, s) \
113 if (rtm->rtm_addrs & (w)) { \
114 bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
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(int, char **);
130 void getsocket(void);
131 int set(int, char **);
134 void dump(struct sockaddr_in6 *, int);
135 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
136 static char *ether_str(struct sockaddr_dl *);
137 int ndp_ether_aton(char *, u_char *);
140 void ifinfo(char *, int, char **);
143 void pfx_flush(void);
144 void rtr_flush(void);
145 void harmonize_rtr(void);
146 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
147 static void getdefif(void);
148 static void setdefif(char *);
150 static char *sec2str(time_t);
151 static void ts_print(const struct timeval *);
153 #ifdef ICMPV6CTL_ND6_DRLIST
154 static char *rtpref_str[] = {
166 main(int argc, char **argv)
171 thiszone = gmt2local(0);
172 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
212 repeat = atoi(optarg);
232 dump(0, mode == 'c');
242 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
246 } else if (argc == 1) {
247 if (strcmp(*argv, "delete") == 0 ||
248 if_nametoindex(*argv))
251 errx(1, "invalid interface %s", *argv);
253 getdefif(); /* always call it to print the result */
256 errx(1, "not supported yet");
267 ifinfo(arg, argc, argv);
277 if (argc < 2 || argc > 4)
279 exit(set(argc, argv) ? 1 : 0);
313 * Process a file to set standard ndp entries
320 char line[100], arg[5][50], *args[5];
322 if ((fp = fopen(name, "r")) == NULL) {
323 fprintf(stderr, "ndp: cannot open %s\n", name);
326 args[0] = &arg[0][0];
327 args[1] = &arg[1][0];
328 args[2] = &arg[2][0];
329 args[3] = &arg[3][0];
330 args[4] = &arg[4][0];
332 while (fgets(line, sizeof(line), fp) != NULL) {
333 i = sscanf(line, "%49s %49s %49s %49s %49s",
334 arg[0], arg[1], arg[2], arg[3], arg[4]);
336 fprintf(stderr, "ndp: bad line: %s\n", line);
351 s = socket(PF_ROUTE, SOCK_RAW, 0);
359 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
360 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
361 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
363 int flags, found_entry;
365 struct rt_msghdr m_rtm;
370 * Set an individual neighbor cache entry
373 set(int argc, char **argv)
375 register struct sockaddr_in6 *sin = &sin_m;
376 register struct sockaddr_dl *sdl;
377 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
378 struct addrinfo hints, *res;
381 char *host = argv[0], *eaddr = argv[1];
389 bzero(&hints, sizeof(hints));
390 hints.ai_family = AF_INET6;
391 gai_error = getaddrinfo(host, NULL, &hints, &res);
393 fprintf(stderr, "ndp: %s: %s\n", host,
394 gai_strerror(gai_error));
397 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
399 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
400 ea = (u_char *)LLADDR(&sdl_m);
401 if (ndp_ether_aton(eaddr, ea) == 0)
403 flags = expire_time = 0;
405 if (strncmp(argv[0], "temp", 4) == 0) {
408 gettimeofday(&now, 0);
409 expire_time = now.tv_sec + 20 * 60;
410 } else if (strncmp(argv[0], "proxy", 5) == 0)
411 flags |= RTF_ANNOUNCE;
414 if (rtmsg(RTM_GET) < 0) {
415 errx(1, "RTM_GET(%s) failed", host);
418 sin = (struct sockaddr_in6 *)(rtm + 1);
419 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
420 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
421 if (sdl->sdl_family == AF_LINK &&
422 !(rtm->rtm_flags & RTF_GATEWAY)) {
423 switch (sdl->sdl_type) {
424 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
425 case IFT_ISO88024: case IFT_ISO88025:
426 case IFT_L2VLAN: case IFT_BRIDGE:
430 fprintf(stderr, "set: cannot configure a new entry\n");
435 if (sdl->sdl_family != AF_LINK) {
436 printf("cannot intuit interface index and type for %s\n", host);
439 sdl_m.sdl_type = sdl->sdl_type;
440 sdl_m.sdl_index = sdl->sdl_index;
441 return (rtmsg(RTM_ADD));
445 * Display an individual neighbor cache entry
450 struct sockaddr_in6 *sin = &sin_m;
451 struct addrinfo hints, *res;
455 bzero(&hints, sizeof(hints));
456 hints.ai_family = AF_INET6;
457 gai_error = getaddrinfo(host, NULL, &hints, &res);
459 fprintf(stderr, "ndp: %s: %s\n", host,
460 gai_strerror(gai_error));
463 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
465 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
467 if (found_entry == 0) {
468 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
469 sizeof(host_buf), NULL ,0,
470 (nflag ? NI_NUMERICHOST : 0));
471 printf("%s (%s) -- no entry\n", host, host_buf);
477 * Delete a neighbor cache entry
482 struct sockaddr_in6 *sin = &sin_m;
483 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
484 register char *cp = m_rtmsg.m_space;
485 struct sockaddr_dl *sdl;
486 struct addrinfo hints, *res;
492 bzero(&hints, sizeof(hints));
493 hints.ai_family = AF_INET6;
494 gai_error = getaddrinfo(host, NULL, &hints, &res);
496 fprintf(stderr, "ndp: %s: %s\n", host,
497 gai_strerror(gai_error));
500 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
502 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
503 if (rtmsg(RTM_GET) < 0) {
504 errx(1, "RTM_GET(%s) failed", host);
507 sin = (struct sockaddr_in6 *)(rtm + 1);
508 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
509 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
510 if (sdl->sdl_family == AF_LINK &&
511 !(rtm->rtm_flags & RTF_GATEWAY)) {
514 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
519 if (sdl->sdl_family != AF_LINK) {
520 printf("cannot locate %s\n", host);
524 * need to reinit the field because it has rt_key
525 * but we want the actual address
527 NEXTADDR(RTA_DST, sin_m);
528 rtm->rtm_flags |= RTF_LLDATA;
529 if (rtmsg(RTM_DELETE) == 0) {
530 getnameinfo((struct sockaddr *)sin,
531 sin->sin6_len, host_buf,
532 sizeof(host_buf), NULL, 0,
533 (nflag ? NI_NUMERICHOST : 0));
534 printf("%s (%s) deleted\n", host, host_buf);
545 * Dump the entire neighbor cache
548 dump(struct sockaddr_in6 *addr, int cflag)
552 char *lim, *buf, *next;
553 struct rt_msghdr *rtm;
554 struct sockaddr_in6 *sin;
555 struct sockaddr_dl *sdl;
557 struct in6_nbrinfo *nbi;
566 if (!tflag && !cflag)
567 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
568 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
569 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
576 mib[4] = NET_RT_FLAGS;
582 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
583 err(1, "sysctl(PF_ROUTE estimate)");
585 if ((buf = malloc(needed)) == NULL)
587 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
588 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
593 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
594 int isrouter = 0, prbs = 0;
596 rtm = (struct rt_msghdr *)next;
597 sin = (struct sockaddr_in6 *)(rtm + 1);
598 sdl = (struct sockaddr_dl *)((char *)sin + ALIGN(sin->sin6_len));
601 * Some OSes can produce a route that has the LINK flag but
602 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
603 * and BSD/OS, where xx is not the interface identifier on
604 * lo0). Such routes entry would annoy getnbrinfo() below,
606 * XXX: such routes should have the GATEWAY flag, not the
607 * LINK flag. However, there is rotten routing software
608 * that advertises all routes that have the GATEWAY flag.
609 * Thus, KAME kernel intentionally does not set the LINK flag.
610 * What is to be fixed is not ndp, but such routing software
611 * (and the kernel workaround)...
613 if (sdl->sdl_family != AF_LINK)
616 if (!(rtm->rtm_flags & RTF_HOST))
620 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
621 &sin->sin6_addr) == 0 ||
622 addr->sin6_scope_id != sin->sin6_scope_id)
625 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
627 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
628 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
629 /* XXX: should scope id be filled in the kernel? */
630 if (sin->sin6_scope_id == 0)
631 sin->sin6_scope_id = sdl->sdl_index;
633 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
634 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
637 if (rtm->rtm_flags & RTF_WASCLONED)
639 #elif defined(RTF_CLONED)
640 if (rtm->rtm_flags & RTF_CLONED)
647 gettimeofday(&now, 0);
651 addrwidth = strlen(host_buf);
652 if (addrwidth < W_ADDR)
654 llwidth = strlen(ether_str(sdl));
655 if (W_ADDR + W_LL - addrwidth > llwidth)
656 llwidth = W_ADDR + W_LL - addrwidth;
657 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
660 ifwidth = strlen(ifname);
661 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
662 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
664 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
665 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
667 /* Print neighbor discovery specific informations */
668 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
670 if (nbi->expire > now.tv_sec) {
672 sec2str(nbi->expire - now.tv_sec));
673 } else if (nbi->expire == 0)
674 printf(" %-9.9s", "permanent");
676 printf(" %-9.9s", "expired");
678 switch (nbi->state) {
679 case ND6_LLINFO_NOSTATE:
682 #ifdef ND6_LLINFO_WAITDELETE
683 case ND6_LLINFO_WAITDELETE:
687 case ND6_LLINFO_INCOMPLETE:
690 case ND6_LLINFO_REACHABLE:
693 case ND6_LLINFO_STALE:
696 case ND6_LLINFO_DELAY:
699 case ND6_LLINFO_PROBE:
707 isrouter = nbi->isrouter;
710 warnx("failed to get neighbor information");
715 * other flags. R: router, P: proxy, W: ??
717 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
718 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
720 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
722 sin = (struct sockaddr_in6 *)
723 (sdl->sdl_len + (char *)sdl);
724 #if 0 /* W and P are mystery even for us */
725 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
727 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
728 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
729 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
731 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
733 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
736 printf(" %s", flgbuf);
754 static struct in6_nbrinfo *
755 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
757 static struct in6_nbrinfo nbi;
760 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
763 bzero(&nbi, sizeof(nbi));
764 if_indextoname(ifindex, nbi.ifname);
766 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
768 warn("ioctl(SIOCGNBRINFO_IN6)");
778 ether_str(struct sockaddr_dl *sdl)
780 static char hbuf[NI_MAXHOST];
783 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
784 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
786 } else if (sdl->sdl_alen) {
787 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
788 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
790 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
796 ndp_ether_aton(char *a, u_char *n)
800 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
801 &o[3], &o[4], &o[5]);
803 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
806 for (i = 0; i < 6; i++)
814 printf("usage: ndp [-nt] hostname\n");
815 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
816 printf(" ndp [-nt] -A wait\n");
817 printf(" ndp [-nt] -d hostname\n");
818 printf(" ndp [-nt] -f filename\n");
819 printf(" ndp [-nt] -i interface [flags...]\n");
820 #ifdef SIOCSDEFIFACE_IN6
821 printf(" ndp [-nt] -I [interface|delete]\n");
823 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
832 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
833 register char *cp = m_rtmsg.m_space;
837 if (cmd == RTM_DELETE)
839 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
840 rtm->rtm_flags = flags;
841 rtm->rtm_version = RTM_VERSION;
845 fprintf(stderr, "ndp: internal wrong cmd\n");
848 rtm->rtm_addrs |= RTA_GATEWAY;
850 rtm->rtm_rmx.rmx_expire = expire_time;
851 rtm->rtm_inits = RTV_EXPIRE;
853 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
854 #if 0 /* we don't support ipv6addr/128 type proxying */
855 if (rtm->rtm_flags & RTF_ANNOUNCE) {
856 rtm->rtm_flags &= ~RTF_HOST;
857 rtm->rtm_addrs |= RTA_NETMASK;
862 rtm->rtm_addrs |= RTA_DST;
865 NEXTADDR(RTA_DST, sin_m);
866 NEXTADDR(RTA_GATEWAY, sdl_m);
867 #if 0 /* we don't support ipv6addr/128 type proxying */
868 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
869 NEXTADDR(RTA_NETMASK, so_mask);
872 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
875 rtm->rtm_seq = ++seq;
877 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
878 if (errno != ESRCH || cmd != RTM_DELETE) {
879 err(1, "writing to routing socket");
884 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
885 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
887 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
893 ifinfo(char *ifname, int argc, char **argv)
895 struct in6_ndireq nd;
898 #ifdef IPV6CTL_USETEMPADDR
902 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
906 bzero(&nd, sizeof(nd));
907 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
908 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
909 err(1, "ioctl(SIOCGIFINFO_IN6)");
914 for (i = 0; i < argc; i++) {
923 #define SETFLAG(s, f) \
925 if (strcmp(cp, (s)) == 0) {\
933 * XXX: this macro is not 100% correct, in that it matches "nud" against
934 * "nudbogus". But we just let it go since this is minor.
936 #define SETVALUE(f, v) \
939 unsigned long newval; \
940 v = 0; /* unspecified */ \
941 if (strncmp(cp, f, strlen(f)) == 0) { \
942 valptr = strchr(cp, '='); \
943 if (valptr == NULL) \
944 err(1, "syntax error in %s field", (f)); \
946 newval = strtoul(++valptr, NULL, 0); \
948 err(1, "syntax error in %s's value", (f)); \
953 SETFLAG("disabled", ND6_IFF_IFDISABLED);
954 SETFLAG("nud", ND6_IFF_PERFORMNUD);
955 #ifdef ND6_IFF_ACCEPT_RTADV
956 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
958 #ifdef ND6_IFF_AUTO_LINKLOCAL
959 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
961 #ifdef ND6_IFF_NO_PREFER_IFACE
962 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
964 SETVALUE("basereachable", ND.basereachable);
965 SETVALUE("retrans", ND.retrans);
966 SETVALUE("curhlim", ND.chlim);
969 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
970 err(1, "ioctl(SIOCSIFINFO_IN6)");
977 if (!ND.initialized) {
978 errx(1, "%s: not initialized yet", ifname);
982 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
983 err(1, "ioctl(SIOCGIFINFO_IN6)");
986 printf("linkmtu=%d", ND.linkmtu);
987 printf(", maxmtu=%d", ND.maxmtu);
988 printf(", curhlim=%d", ND.chlim);
989 printf(", basereachable=%ds%dms",
990 ND.basereachable / 1000, ND.basereachable % 1000);
991 printf(", reachable=%ds", ND.reachable);
992 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
993 #ifdef IPV6CTL_USETEMPADDR
994 memset(nullbuf, 0, sizeof(nullbuf));
995 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
999 for (i = 0; i < 3; i++) {
1002 printf("\nRandom seed(0): ");
1003 rbuf = ND.randomseed0;
1006 printf("\nRandom seed(1): ");
1007 rbuf = ND.randomseed1;
1010 printf("\nRandom ID: ");
1014 errx(1, "impossible case for tempaddr display");
1016 for (j = 0; j < 8; j++)
1017 printf("%02x", rbuf[j]);
1022 printf("\nFlags: ");
1023 #ifdef ND6_IFF_IFDISABLED
1024 if ((ND.flags & ND6_IFF_IFDISABLED))
1025 printf("disabled ");
1027 if ((ND.flags & ND6_IFF_PERFORMNUD))
1029 #ifdef ND6_IFF_ACCEPT_RTADV
1030 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1031 printf("accept_rtadv ");
1033 #ifdef ND6_IFF_AUTO_LINKLOCAL
1034 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1035 printf("auto_linklocal ");
1037 #ifdef ND6_IFF_NO_PREFER_IFACE
1038 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1039 printf("no_prefer_iface ");
1048 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1049 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1055 #ifdef ICMPV6CTL_ND6_DRLIST
1056 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1058 struct in6_defrouter *p, *ep;
1062 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1063 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1073 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1074 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1078 ep = (struct in6_defrouter *)(buf + l);
1079 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1082 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1083 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1084 (nflag ? NI_NUMERICHOST : 0)) != 0)
1085 strlcpy(host_buf, "?", sizeof(host_buf));
1087 printf("%s if=%s", host_buf,
1088 if_indextoname(p->if_index, ifix_buf));
1089 printf(", flags=%s%s",
1090 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1091 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1092 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1093 printf(", pref=%s", rtpref_str[rtpref]);
1095 gettimeofday(&now, 0);
1097 printf(", expire=Never\n");
1099 printf(", expire=%s\n",
1100 sec2str(p->expire - now.tv_sec));
1104 struct in6_drlist dr;
1108 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1112 bzero(&dr, sizeof(dr));
1113 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1114 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1115 err(1, "ioctl(SIOCGDRLST_IN6)");
1118 #define DR dr.defrouter[i]
1119 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1120 struct sockaddr_in6 sin6;
1122 bzero(&sin6, sizeof(sin6));
1123 sin6.sin6_family = AF_INET6;
1124 sin6.sin6_len = sizeof(sin6);
1125 sin6.sin6_addr = DR.rtaddr;
1126 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1127 sizeof(host_buf), NULL, 0,
1128 (nflag ? NI_NUMERICHOST : 0));
1130 printf("%s if=%s", host_buf,
1131 if_indextoname(DR.if_index, ifix_buf));
1132 printf(", flags=%s%s",
1133 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1134 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1135 gettimeofday(&now, 0);
1137 printf(", expire=Never\n");
1139 printf(", expire=%s\n",
1140 sec2str(DR.expire - now.tv_sec));
1150 #ifdef ICMPV6CTL_ND6_PRLIST
1151 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1153 struct in6_prefix *p, *ep, *n;
1154 struct sockaddr_in6 *advrtr;
1157 const int niflags = NI_NUMERICHOST;
1158 int ninflags = nflag ? NI_NUMERICHOST : 0;
1159 char namebuf[NI_MAXHOST];
1161 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1162 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1170 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1171 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1175 ep = (struct in6_prefix *)(buf + l);
1176 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1177 advrtr = (struct sockaddr_in6 *)(p + 1);
1178 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1180 if (getnameinfo((struct sockaddr *)&p->prefix,
1181 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1182 NULL, 0, niflags) != 0)
1183 strlcpy(namebuf, "?", sizeof(namebuf));
1184 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1185 if_indextoname(p->if_index, ifix_buf));
1187 gettimeofday(&now, 0);
1189 * meaning of fields, especially flags, is very different
1190 * by origin. notify the difference to the users.
1192 printf("flags=%s%s%s%s%s",
1193 p->raflags.onlink ? "L" : "",
1194 p->raflags.autonomous ? "A" : "",
1195 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1196 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1198 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1203 if (p->vltime == ND6_INFINITE_LIFETIME)
1204 printf(" vltime=infinity");
1206 printf(" vltime=%lu", (unsigned long)p->vltime);
1207 if (p->pltime == ND6_INFINITE_LIFETIME)
1208 printf(", pltime=infinity");
1210 printf(", pltime=%lu", (unsigned long)p->pltime);
1212 printf(", expire=Never");
1213 else if (p->expire >= now.tv_sec)
1214 printf(", expire=%s",
1215 sec2str(p->expire - now.tv_sec));
1217 printf(", expired");
1218 printf(", ref=%d", p->refcnt);
1221 * "advertising router" list is meaningful only if the prefix
1222 * information is from RA.
1226 struct sockaddr_in6 *sin6;
1229 printf(" advertised by\n");
1230 for (j = 0; j < p->advrtrs; j++) {
1231 struct in6_nbrinfo *nbi;
1233 if (getnameinfo((struct sockaddr *)sin6,
1234 sin6->sin6_len, namebuf, sizeof(namebuf),
1235 NULL, 0, ninflags) != 0)
1236 strlcpy(namebuf, "?", sizeof(namebuf));
1237 printf(" %s", namebuf);
1239 nbi = getnbrinfo(&sin6->sin6_addr,
1242 switch (nbi->state) {
1243 case ND6_LLINFO_REACHABLE:
1244 case ND6_LLINFO_STALE:
1245 case ND6_LLINFO_DELAY:
1246 case ND6_LLINFO_PROBE:
1247 printf(" (reachable)\n");
1250 printf(" (unreachable)\n");
1253 printf(" (no neighbor state)\n");
1257 printf(" No advertising router\n");
1261 struct in6_prlist pr;
1265 gettimeofday(&now, 0);
1267 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1271 bzero(&pr, sizeof(pr));
1272 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1273 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1274 err(1, "ioctl(SIOCGPRLST_IN6)");
1277 #define PR pr.prefix[i]
1278 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1279 struct sockaddr_in6 p6;
1280 char namebuf[NI_MAXHOST];
1286 memset(&p6, 0, sizeof(p6));
1287 p6.sin6_family = AF_INET6;
1288 p6.sin6_len = sizeof(p6);
1289 p6.sin6_addr = PR.prefix;
1291 niflags = NI_NUMERICHOST;
1292 if (getnameinfo((struct sockaddr *)&p6,
1293 sizeof(p6), namebuf, sizeof(namebuf),
1294 NULL, 0, niflags)) {
1295 warnx("getnameinfo failed");
1298 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1299 if_indextoname(PR.if_index, ifix_buf));
1301 gettimeofday(&now, 0);
1303 * meaning of fields, especially flags, is very different
1304 * by origin. notify the difference to the users.
1308 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1311 printf("flags=%s%s%s%s%s",
1312 PR.raflags.onlink ? "L" : "",
1313 PR.raflags.autonomous ? "A" : "",
1314 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1315 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1317 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1323 printf("flags=%s%s",
1324 PR.raflags.onlink ? "L" : "",
1325 PR.raflags.autonomous ? "A" : "");
1327 if (PR.vltime == ND6_INFINITE_LIFETIME)
1328 printf(" vltime=infinity");
1330 printf(" vltime=%lu", PR.vltime);
1331 if (PR.pltime == ND6_INFINITE_LIFETIME)
1332 printf(", pltime=infinity");
1334 printf(", pltime=%lu", PR.pltime);
1336 printf(", expire=Never");
1337 else if (PR.expire >= now.tv_sec)
1338 printf(", expire=%s",
1339 sec2str(PR.expire - now.tv_sec));
1341 printf(", expired");
1343 printf(", ref=%d", PR.refcnt);
1346 switch (PR.origin) {
1348 printf(", origin=RA");
1351 printf(", origin=RR");
1353 case PR_ORIG_STATIC:
1354 printf(", origin=static");
1356 case PR_ORIG_KERNEL:
1357 printf(", origin=kernel");
1360 printf(", origin=?");
1366 * "advertising router" list is meaningful only if the prefix
1367 * information is from RA.
1369 if (0 && /* prefix origin is almost obsolted */
1370 PR.origin != PR_ORIG_RA)
1372 else if (PR.advrtrs) {
1374 printf(" advertised by\n");
1375 for (j = 0; j < PR.advrtrs; j++) {
1376 struct sockaddr_in6 sin6;
1377 struct in6_nbrinfo *nbi;
1379 bzero(&sin6, sizeof(sin6));
1380 sin6.sin6_family = AF_INET6;
1381 sin6.sin6_len = sizeof(sin6);
1382 sin6.sin6_addr = PR.advrtr[j];
1383 sin6.sin6_scope_id = PR.if_index; /* XXX */
1384 getnameinfo((struct sockaddr *)&sin6,
1385 sin6.sin6_len, host_buf,
1386 sizeof(host_buf), NULL, 0,
1387 (nflag ? NI_NUMERICHOST : 0));
1388 printf(" %s", host_buf);
1390 nbi = getnbrinfo(&sin6.sin6_addr,
1393 switch (nbi->state) {
1394 case ND6_LLINFO_REACHABLE:
1395 case ND6_LLINFO_STALE:
1396 case ND6_LLINFO_DELAY:
1397 case ND6_LLINFO_PROBE:
1398 printf(" (reachable)\n");
1401 printf(" (unreachable)\n");
1404 printf(" (no neighbor state)\n");
1406 if (PR.advrtrs > DRLSTSIZ)
1407 printf(" and %d routers\n",
1408 PR.advrtrs - DRLSTSIZ);
1410 printf(" No advertising router\n");
1420 char dummyif[IFNAMSIZ+8];
1423 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1425 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1426 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1427 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1433 char dummyif[IFNAMSIZ+8];
1436 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1438 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1439 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1440 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1448 char dummyif[IFNAMSIZ+8];
1451 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1453 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1454 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1455 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1460 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1462 setdefif(char *ifname)
1464 struct in6_ndifreq ndifreq;
1465 unsigned int ifindex;
1467 if (strcasecmp(ifname, "delete") == 0)
1470 if ((ifindex = if_nametoindex(ifname)) == 0)
1471 err(1, "failed to resolve i/f index for %s", ifname);
1474 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1477 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1478 ndifreq.ifindex = ifindex;
1480 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1481 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1489 struct in6_ndifreq ndifreq;
1490 char ifname[IFNAMSIZ+8];
1492 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1495 memset(&ndifreq, 0, sizeof(ndifreq));
1496 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1498 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1499 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1501 if (ndifreq.ifindex == 0)
1502 printf("No default interface.\n");
1504 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1505 err(1, "failed to resolve ifname for index %lu",
1507 printf("ND default interface = %s\n", ifname);
1515 sec2str(time_t total)
1517 static char result[256];
1518 int days, hours, mins, secs;
1521 char *ep = &result[sizeof(result)];
1524 days = total / 3600 / 24;
1525 hours = (total / 3600) % 24;
1526 mins = (total / 60) % 60;
1531 n = snprintf(p, ep - p, "%dd", days);
1532 if (n < 0 || n >= ep - p)
1536 if (!first || hours) {
1538 n = snprintf(p, ep - p, "%dh", hours);
1539 if (n < 0 || n >= ep - p)
1543 if (!first || mins) {
1545 n = snprintf(p, ep - p, "%dm", mins);
1546 if (n < 0 || n >= ep - p)
1550 snprintf(p, ep - p, "%ds", secs);
1556 * Print the timestamp
1557 * from tcpdump/util.c
1560 ts_print(const struct timeval *tvp)
1565 s = (tvp->tv_sec + thiszone) % 86400;
1566 (void)printf("%02d:%02d:%02d.%06u ",
1567 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);