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_dl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/if_ether.h>
93 #include <netinet/icmp6.h>
94 #include <netinet6/in6_var.h>
95 #include <netinet6/nd6.h>
97 #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 static char host_buf[NI_MAXHOST]; /* getnameinfo() */
125 static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
127 static int file(char *);
128 static void getsocket(void);
129 static int set(int, char **);
130 static void get(char *);
131 static int delete(char *);
132 static void dump(struct sockaddr_in6 *, int);
133 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
134 static char *ether_str(struct sockaddr_dl *);
135 static int ndp_ether_aton(char *, u_char *);
136 static void usage(void);
137 static int rtmsg(int);
138 static void ifinfo(char *, int, char **);
139 static void rtrlist(void);
140 static void plist(void);
141 static void pfx_flush(void);
142 static void rtr_flush(void);
143 static void harmonize_rtr(void);
144 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
145 static void getdefif(void);
146 static void setdefif(char *);
148 static char *sec2str(time_t);
149 static void ts_print(const struct timeval *);
151 static char *rtpref_str[] = {
159 main(int argc, char **argv)
165 thiszone = gmt2local(0);
166 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
185 exit(file(optarg) ? 1 : 0);
207 repeat = atoi(optarg);
227 dump(0, mode == 'c');
237 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
241 } else if (argc == 1) {
242 if (strcmp(*argv, "delete") == 0 ||
243 if_nametoindex(*argv))
246 errx(1, "invalid interface %s", *argv);
248 getdefif(); /* always call it to print the result */
251 errx(1, "not supported yet");
262 ifinfo(arg, argc, argv);
272 if (argc < 2 || argc > 4)
274 exit(set(argc, argv) ? 1 : 0);
308 * Process a file to set standard ndp entries
315 char line[100], arg[5][50], *args[5], *p;
317 if ((fp = fopen(name, "r")) == NULL)
318 err(1, "cannot open %s", name);
319 args[0] = &arg[0][0];
320 args[1] = &arg[1][0];
321 args[2] = &arg[2][0];
322 args[3] = &arg[3][0];
323 args[4] = &arg[4][0];
325 while (fgets(line, sizeof(line), fp) != NULL) {
326 if ((p = strchr(line, '#')) != NULL)
328 for (p = line; isblank(*p); p++);
329 if (*p == '\n' || *p == '\0')
331 i = sscanf(line, "%49s %49s %49s %49s %49s",
332 arg[0], arg[1], arg[2], arg[3], arg[4]);
334 warnx("bad line: %s", line);
349 s = socket(PF_ROUTE, SOCK_RAW, 0);
357 static struct sockaddr_in6 so_mask = {
358 .sin6_len = sizeof(so_mask),
359 .sin6_family = AF_INET6
361 static struct sockaddr_in6 blank_sin = {
362 .sin6_len = sizeof(blank_sin),
363 .sin6_family = AF_INET6
365 static struct sockaddr_in6 sin_m;
366 static struct sockaddr_dl blank_sdl = {
367 .sdl_len = sizeof(blank_sdl),
368 .sdl_family = AF_LINK
370 static struct sockaddr_dl sdl_m;
371 static time_t expire_time;
372 static int flags, found_entry;
374 struct rt_msghdr m_rtm;
379 * Set an individual neighbor cache entry
382 set(int argc, char **argv)
384 register struct sockaddr_in6 *sin = &sin_m;
385 register struct sockaddr_dl *sdl;
386 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
387 struct addrinfo hints, *res;
390 char *host = argv[0], *eaddr = argv[1];
398 bzero(&hints, sizeof(hints));
399 hints.ai_family = AF_INET6;
400 gai_error = getaddrinfo(host, NULL, &hints, &res);
402 fprintf(stderr, "ndp: %s: %s\n", host,
403 gai_strerror(gai_error));
406 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
408 ((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(&now, 0);
418 expire_time = now.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 *)(ALIGN(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_GATEWAY)) {
432 switch (sdl->sdl_type) {
433 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
434 case IFT_ISO88024: case IFT_ISO88025:
435 case IFT_L2VLAN: case IFT_BRIDGE:
439 fprintf(stderr, "set: cannot configure a new entry\n");
444 if (sdl->sdl_family != AF_LINK) {
445 printf("cannot intuit interface index and type for %s\n", host);
448 sdl_m.sdl_type = sdl->sdl_type;
449 sdl_m.sdl_index = sdl->sdl_index;
450 return (rtmsg(RTM_ADD));
454 * Display an individual neighbor cache entry
459 struct sockaddr_in6 *sin = &sin_m;
460 struct addrinfo hints, *res;
464 bzero(&hints, sizeof(hints));
465 hints.ai_family = AF_INET6;
466 gai_error = getaddrinfo(host, NULL, &hints, &res);
468 fprintf(stderr, "ndp: %s: %s\n", host,
469 gai_strerror(gai_error));
472 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
474 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
476 if (found_entry == 0) {
477 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
478 sizeof(host_buf), NULL ,0,
479 (nflag ? NI_NUMERICHOST : 0));
480 printf("%s (%s) -- no entry\n", host, host_buf);
486 * Delete a neighbor cache entry
491 struct sockaddr_in6 *sin = &sin_m;
492 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
493 register char *cp = m_rtmsg.m_space;
494 struct sockaddr_dl *sdl;
495 struct addrinfo hints, *res;
501 bzero(&hints, sizeof(hints));
502 hints.ai_family = AF_INET6;
503 gai_error = getaddrinfo(host, NULL, &hints, &res);
505 fprintf(stderr, "ndp: %s: %s\n", host,
506 gai_strerror(gai_error));
509 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
511 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
512 if (rtmsg(RTM_GET) < 0) {
513 errx(1, "RTM_GET(%s) failed", host);
516 sin = (struct sockaddr_in6 *)(rtm + 1);
517 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
518 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
519 if (sdl->sdl_family == AF_LINK &&
520 !(rtm->rtm_flags & RTF_GATEWAY)) {
523 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
528 if (sdl->sdl_family != AF_LINK) {
529 printf("cannot locate %s\n", host);
533 * need to reinit the field because it has rt_key
534 * but we want the actual address
536 NEXTADDR(RTA_DST, sin_m);
537 rtm->rtm_flags |= RTF_LLDATA;
538 if (rtmsg(RTM_DELETE) == 0) {
539 getnameinfo((struct sockaddr *)sin,
540 sin->sin6_len, host_buf,
541 sizeof(host_buf), NULL, 0,
542 (nflag ? NI_NUMERICHOST : 0));
543 printf("%s (%s) deleted\n", host, host_buf);
554 * Dump the entire neighbor cache
557 dump(struct sockaddr_in6 *addr, int cflag)
561 char *lim, *buf, *next;
562 struct rt_msghdr *rtm;
563 struct sockaddr_in6 *sin;
564 struct sockaddr_dl *sdl;
575 if (!tflag && !cflag)
576 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
577 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
578 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
585 mib[4] = NET_RT_FLAGS;
591 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
592 err(1, "sysctl(PF_ROUTE estimate)");
594 if ((buf = malloc(needed)) == NULL)
596 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
597 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
602 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
603 int isrouter = 0, prbs = 0;
605 rtm = (struct rt_msghdr *)next;
606 sin = (struct sockaddr_in6 *)(rtm + 1);
607 sdl = (struct sockaddr_dl *)((char *)sin + ALIGN(sin->sin6_len));
610 * Some OSes can produce a route that has the LINK flag but
611 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
612 * and BSD/OS, where xx is not the interface identifier on
613 * lo0). Such routes entry would annoy getnbrinfo() below,
615 * XXX: such routes should have the GATEWAY flag, not the
616 * LINK flag. However, there is rotten routing software
617 * that advertises all routes that have the GATEWAY flag.
618 * Thus, KAME kernel intentionally does not set the LINK flag.
619 * What is to be fixed is not ndp, but such routing software
620 * (and the kernel workaround)...
622 if (sdl->sdl_family != AF_LINK)
625 if (!(rtm->rtm_flags & RTF_HOST))
629 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
630 &sin->sin6_addr) == 0 ||
631 addr->sin6_scope_id != sin->sin6_scope_id)
634 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
636 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
637 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
638 /* XXX: should scope id be filled in the kernel? */
639 if (sin->sin6_scope_id == 0)
640 sin->sin6_scope_id = sdl->sdl_index;
642 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
643 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
646 if (rtm->rtm_flags & RTF_WASCLONED)
648 #elif defined(RTF_CLONED)
649 if (rtm->rtm_flags & RTF_CLONED)
652 if (rtm->rtm_flags & RTF_PINNED)
658 gettimeofday(&now, 0);
662 addrwidth = strlen(host_buf);
663 if (addrwidth < W_ADDR)
665 llwidth = strlen(ether_str(sdl));
666 if (W_ADDR + W_LL - addrwidth > llwidth)
667 llwidth = W_ADDR + W_LL - addrwidth;
668 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
671 ifwidth = strlen(ifname);
672 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
673 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
675 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
676 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
678 /* Print neighbor discovery specific information */
679 expire = rtm->rtm_rmx.rmx_expire;
680 if (expire > now.tv_sec)
681 printf(" %-9.9s", sec2str(expire - now.tv_sec));
682 else if (expire == 0)
683 printf(" %-9.9s", "permanent");
685 printf(" %-9.9s", "expired");
687 switch (rtm->rtm_rmx.rmx_state) {
688 case ND6_LLINFO_NOSTATE:
691 #ifdef ND6_LLINFO_WAITDELETE
692 case ND6_LLINFO_WAITDELETE:
696 case ND6_LLINFO_INCOMPLETE:
699 case ND6_LLINFO_REACHABLE:
702 case ND6_LLINFO_STALE:
705 case ND6_LLINFO_DELAY:
708 case ND6_LLINFO_PROBE:
716 isrouter = rtm->rtm_flags & RTF_GATEWAY;
717 prbs = rtm->rtm_rmx.rmx_pksent;
720 * other flags. R: router, P: proxy, W: ??
722 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
723 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
725 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
727 #if 0 /* W and P are mystery even for us */
728 sin = (struct sockaddr_in6 *)
729 (sdl->sdl_len + (char *)sdl);
730 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
732 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
733 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
734 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
736 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
738 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
741 printf(" %s", flgbuf);
759 static struct in6_nbrinfo *
760 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
762 static struct in6_nbrinfo nbi;
765 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
768 bzero(&nbi, sizeof(nbi));
769 if_indextoname(ifindex, nbi.ifname);
771 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
773 warn("ioctl(SIOCGNBRINFO_IN6)");
783 ether_str(struct sockaddr_dl *sdl)
785 static char hbuf[NI_MAXHOST];
787 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
788 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
790 } else if (sdl->sdl_alen) {
791 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
792 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
794 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
800 ndp_ether_aton(char *a, u_char *n)
804 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
805 &o[3], &o[4], &o[5]);
807 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
810 for (i = 0; i < 6; i++)
818 printf("usage: ndp [-nt] hostname\n");
819 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
820 printf(" ndp [-nt] -A wait\n");
821 printf(" ndp [-nt] -d hostname\n");
822 printf(" ndp [-nt] -f filename\n");
823 printf(" ndp [-nt] -i interface [flags...]\n");
824 #ifdef SIOCSDEFIFACE_IN6
825 printf(" ndp [-nt] -I [interface|delete]\n");
827 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
836 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
837 register char *cp = m_rtmsg.m_space;
841 if (cmd == RTM_DELETE)
843 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
844 rtm->rtm_flags = flags;
845 rtm->rtm_version = RTM_VERSION;
849 fprintf(stderr, "ndp: internal wrong cmd\n");
852 rtm->rtm_addrs |= RTA_GATEWAY;
854 rtm->rtm_rmx.rmx_expire = expire_time;
855 rtm->rtm_inits = RTV_EXPIRE;
857 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
858 #if 0 /* we don't support ipv6addr/128 type proxying */
859 if (rtm->rtm_flags & RTF_ANNOUNCE) {
860 rtm->rtm_flags &= ~RTF_HOST;
861 rtm->rtm_addrs |= RTA_NETMASK;
866 rtm->rtm_addrs |= RTA_DST;
869 NEXTADDR(RTA_DST, sin_m);
870 NEXTADDR(RTA_GATEWAY, sdl_m);
871 #if 0 /* we don't support ipv6addr/128 type proxying */
872 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
873 NEXTADDR(RTA_NETMASK, so_mask);
876 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
879 rtm->rtm_seq = ++seq;
881 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
882 if (errno != ESRCH || cmd != RTM_DELETE) {
883 err(1, "writing to routing socket");
888 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
889 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
891 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
897 ifinfo(char *ifname, int argc, char **argv)
899 struct in6_ndireq nd;
902 #ifdef IPV6CTL_USETEMPADDR
906 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
910 bzero(&nd, sizeof(nd));
911 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
912 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
913 err(1, "ioctl(SIOCGIFINFO_IN6)");
918 for (i = 0; i < argc; i++) {
927 #define SETFLAG(s, f) \
929 if (strcmp(cp, (s)) == 0) {\
937 * XXX: this macro is not 100% correct, in that it matches "nud" against
938 * "nudbogus". But we just let it go since this is minor.
940 #define SETVALUE(f, v) \
943 unsigned long newval; \
944 v = 0; /* unspecified */ \
945 if (strncmp(cp, f, strlen(f)) == 0) { \
946 valptr = strchr(cp, '='); \
947 if (valptr == NULL) \
948 err(1, "syntax error in %s field", (f)); \
950 newval = strtoul(++valptr, NULL, 0); \
952 err(1, "syntax error in %s's value", (f)); \
957 SETFLAG("disabled", ND6_IFF_IFDISABLED);
958 SETFLAG("nud", ND6_IFF_PERFORMNUD);
959 #ifdef ND6_IFF_ACCEPT_RTADV
960 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
962 #ifdef ND6_IFF_AUTO_LINKLOCAL
963 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
965 #ifdef ND6_IFF_NO_PREFER_IFACE
966 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
968 SETVALUE("basereachable", ND.basereachable);
969 SETVALUE("retrans", ND.retrans);
970 SETVALUE("curhlim", ND.chlim);
973 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
974 err(1, "ioctl(SIOCSIFINFO_IN6)");
981 if (!ND.initialized) {
982 errx(1, "%s: not initialized yet", ifname);
986 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
987 err(1, "ioctl(SIOCGIFINFO_IN6)");
990 printf("linkmtu=%d", ND.linkmtu);
991 printf(", maxmtu=%d", ND.maxmtu);
992 printf(", curhlim=%d", ND.chlim);
993 printf(", basereachable=%ds%dms",
994 ND.basereachable / 1000, ND.basereachable % 1000);
995 printf(", reachable=%ds", ND.reachable);
996 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
997 #ifdef IPV6CTL_USETEMPADDR
998 memset(nullbuf, 0, sizeof(nullbuf));
999 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1003 for (i = 0; i < 3; i++) {
1006 printf("\nRandom seed(0): ");
1007 rbuf = ND.randomseed0;
1010 printf("\nRandom seed(1): ");
1011 rbuf = ND.randomseed1;
1014 printf("\nRandom ID: ");
1018 errx(1, "impossible case for tempaddr display");
1020 for (j = 0; j < 8; j++)
1021 printf("%02x", rbuf[j]);
1026 printf("\nFlags: ");
1027 #ifdef ND6_IFF_IFDISABLED
1028 if ((ND.flags & ND6_IFF_IFDISABLED))
1029 printf("disabled ");
1031 if ((ND.flags & ND6_IFF_PERFORMNUD))
1033 #ifdef ND6_IFF_ACCEPT_RTADV
1034 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1035 printf("accept_rtadv ");
1037 #ifdef ND6_IFF_AUTO_LINKLOCAL
1038 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1039 printf("auto_linklocal ");
1041 #ifdef ND6_IFF_NO_PREFER_IFACE
1042 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1043 printf("no_prefer_iface ");
1052 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1053 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1059 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1061 struct in6_defrouter *p, *ep;
1065 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1066 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1076 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1077 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1081 ep = (struct in6_defrouter *)(buf + l);
1082 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1085 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1086 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1087 (nflag ? NI_NUMERICHOST : 0)) != 0)
1088 strlcpy(host_buf, "?", sizeof(host_buf));
1090 printf("%s if=%s", host_buf,
1091 if_indextoname(p->if_index, ifix_buf));
1092 printf(", flags=%s%s",
1093 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1094 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1095 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1096 printf(", pref=%s", rtpref_str[rtpref]);
1098 gettimeofday(&now, 0);
1100 printf(", expire=Never\n");
1102 printf(", expire=%s\n",
1103 sec2str(p->expire - now.tv_sec));
1111 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1113 struct in6_prefix *p, *ep, *n;
1114 struct sockaddr_in6 *advrtr;
1117 const int niflags = NI_NUMERICHOST;
1118 int ninflags = nflag ? NI_NUMERICHOST : 0;
1119 char namebuf[NI_MAXHOST];
1121 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1122 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1130 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1131 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1135 ep = (struct in6_prefix *)(buf + l);
1136 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1137 advrtr = (struct sockaddr_in6 *)(p + 1);
1138 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1140 if (getnameinfo((struct sockaddr *)&p->prefix,
1141 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1142 NULL, 0, niflags) != 0)
1143 strlcpy(namebuf, "?", sizeof(namebuf));
1144 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1145 if_indextoname(p->if_index, ifix_buf));
1147 gettimeofday(&now, 0);
1149 * meaning of fields, especially flags, is very different
1150 * by origin. notify the difference to the users.
1152 printf("flags=%s%s%s%s%s",
1153 p->raflags.onlink ? "L" : "",
1154 p->raflags.autonomous ? "A" : "",
1155 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1156 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1158 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1163 if (p->vltime == ND6_INFINITE_LIFETIME)
1164 printf(" vltime=infinity");
1166 printf(" vltime=%lu", (unsigned long)p->vltime);
1167 if (p->pltime == ND6_INFINITE_LIFETIME)
1168 printf(", pltime=infinity");
1170 printf(", pltime=%lu", (unsigned long)p->pltime);
1172 printf(", expire=Never");
1173 else if (p->expire >= now.tv_sec)
1174 printf(", expire=%s",
1175 sec2str(p->expire - now.tv_sec));
1177 printf(", expired");
1178 printf(", ref=%d", p->refcnt);
1181 * "advertising router" list is meaningful only if the prefix
1182 * information is from RA.
1186 struct sockaddr_in6 *sin6;
1189 printf(" advertised by\n");
1190 for (j = 0; j < p->advrtrs; j++) {
1191 struct in6_nbrinfo *nbi;
1193 if (getnameinfo((struct sockaddr *)sin6,
1194 sin6->sin6_len, namebuf, sizeof(namebuf),
1195 NULL, 0, ninflags) != 0)
1196 strlcpy(namebuf, "?", sizeof(namebuf));
1197 printf(" %s", namebuf);
1199 nbi = getnbrinfo(&sin6->sin6_addr,
1202 switch (nbi->state) {
1203 case ND6_LLINFO_REACHABLE:
1204 case ND6_LLINFO_STALE:
1205 case ND6_LLINFO_DELAY:
1206 case ND6_LLINFO_PROBE:
1207 printf(" (reachable)\n");
1210 printf(" (unreachable)\n");
1213 printf(" (no neighbor state)\n");
1217 printf(" No advertising router\n");
1225 char dummyif[IFNAMSIZ+8];
1228 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1230 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1231 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1232 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1238 char dummyif[IFNAMSIZ+8];
1241 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1243 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1244 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1245 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1253 char dummyif[IFNAMSIZ+8];
1256 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1258 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1259 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1260 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1265 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1267 setdefif(char *ifname)
1269 struct in6_ndifreq ndifreq;
1270 unsigned int ifindex;
1272 if (strcasecmp(ifname, "delete") == 0)
1275 if ((ifindex = if_nametoindex(ifname)) == 0)
1276 err(1, "failed to resolve i/f index for %s", ifname);
1279 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1282 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1283 ndifreq.ifindex = ifindex;
1285 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1286 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1294 struct in6_ndifreq ndifreq;
1295 char ifname[IFNAMSIZ+8];
1297 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1300 memset(&ndifreq, 0, sizeof(ndifreq));
1301 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1303 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1304 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1306 if (ndifreq.ifindex == 0)
1307 printf("No default interface.\n");
1309 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1310 err(1, "failed to resolve ifname for index %lu",
1312 printf("ND default interface = %s\n", ifname);
1320 sec2str(time_t total)
1322 static char result[256];
1323 int days, hours, mins, secs;
1326 char *ep = &result[sizeof(result)];
1329 days = total / 3600 / 24;
1330 hours = (total / 3600) % 24;
1331 mins = (total / 60) % 60;
1336 n = snprintf(p, ep - p, "%dd", days);
1337 if (n < 0 || n >= ep - p)
1341 if (!first || hours) {
1343 n = snprintf(p, ep - p, "%dh", hours);
1344 if (n < 0 || n >= ep - p)
1348 if (!first || mins) {
1350 n = snprintf(p, ep - p, "%dm", mins);
1351 if (n < 0 || n >= ep - p)
1355 snprintf(p, ep - p, "%ds", secs);
1361 * Print the timestamp
1362 * from tcpdump/util.c
1365 ts_print(const struct timeval *tvp)
1370 s = (tvp->tv_sec + thiszone) % 86400;
1371 (void)printf("%02d:%02d:%02d.%06u ",
1372 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);