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 static char host_buf[NI_MAXHOST]; /* getnameinfo() */
131 static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
133 static int file(char *);
134 static void getsocket(void);
135 static int set(int, char **);
136 static void get(char *);
137 static int delete(char *);
138 static void dump(struct sockaddr_in6 *, int);
139 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
140 static char *ether_str(struct sockaddr_dl *);
141 static int ndp_ether_aton(char *, u_char *);
142 static void usage(void);
143 static int rtmsg(int);
144 static void ifinfo(char *, int, char **);
145 static void rtrlist(void);
146 static void plist(void);
147 static void pfx_flush(void);
148 static void rtr_flush(void);
149 static void harmonize_rtr(void);
150 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
151 static void getdefif(void);
152 static void setdefif(char *);
154 static char *sec2str(time_t);
155 static void ts_print(const struct timeval *);
157 static char *rtpref_str[] = {
165 main(int argc, char **argv)
171 thiszone = gmt2local(0);
172 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
191 exit(file(optarg) ? 1 : 0);
213 repeat = atoi(optarg);
233 dump(0, mode == 'c');
243 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
247 } else if (argc == 1) {
248 if (strcmp(*argv, "delete") == 0 ||
249 if_nametoindex(*argv))
252 errx(1, "invalid interface %s", *argv);
254 getdefif(); /* always call it to print the result */
257 errx(1, "not supported yet");
268 ifinfo(arg, argc, argv);
278 if (argc < 2 || argc > 4)
280 exit(set(argc, argv) ? 1 : 0);
314 * Process a file to set standard ndp entries
321 char line[100], arg[5][50], *args[5], *p;
323 if ((fp = fopen(name, "r")) == NULL)
324 err(1, "cannot open %s", name);
325 args[0] = &arg[0][0];
326 args[1] = &arg[1][0];
327 args[2] = &arg[2][0];
328 args[3] = &arg[3][0];
329 args[4] = &arg[4][0];
331 while (fgets(line, sizeof(line), fp) != NULL) {
332 if ((p = strchr(line, '#')) != NULL)
334 for (p = line; isblank(*p); p++);
335 if (*p == '\n' || *p == '\0')
337 i = sscanf(line, "%49s %49s %49s %49s %49s",
338 arg[0], arg[1], arg[2], arg[3], arg[4]);
340 warnx("bad line: %s", line);
355 s = socket(PF_ROUTE, SOCK_RAW, 0);
363 static struct sockaddr_in6 so_mask = {
364 .sin6_len = sizeof(so_mask),
365 .sin6_family = AF_INET6
367 static struct sockaddr_in6 blank_sin = {
368 .sin6_len = sizeof(blank_sin),
369 .sin6_family = AF_INET6
371 static struct sockaddr_in6 sin_m;
372 static struct sockaddr_dl blank_sdl = {
373 .sdl_len = sizeof(blank_sdl),
374 .sdl_family = AF_LINK
376 static struct sockaddr_dl sdl_m;
377 static time_t expire_time;
378 static int flags, found_entry;
380 struct rt_msghdr m_rtm;
385 * Set an individual neighbor cache entry
388 set(int argc, char **argv)
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
465 struct sockaddr_in6 *sin = &sin_m;
466 struct addrinfo hints, *res;
470 bzero(&hints, sizeof(hints));
471 hints.ai_family = AF_INET6;
472 gai_error = getaddrinfo(host, NULL, &hints, &res);
474 fprintf(stderr, "ndp: %s: %s\n", host,
475 gai_strerror(gai_error));
478 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
480 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
482 if (found_entry == 0) {
483 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
484 sizeof(host_buf), NULL ,0,
485 (nflag ? NI_NUMERICHOST : 0));
486 printf("%s (%s) -- no entry\n", host, host_buf);
492 * 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
563 dump(struct sockaddr_in6 *addr, int cflag)
567 char *lim, *buf, *next;
568 struct rt_msghdr *rtm;
569 struct sockaddr_in6 *sin;
570 struct sockaddr_dl *sdl;
572 struct in6_nbrinfo *nbi;
581 if (!tflag && !cflag)
582 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
583 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
584 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
591 mib[4] = NET_RT_FLAGS;
597 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
598 err(1, "sysctl(PF_ROUTE estimate)");
600 if ((buf = malloc(needed)) == NULL)
602 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
603 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
608 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
609 int isrouter = 0, prbs = 0;
611 rtm = (struct rt_msghdr *)next;
612 sin = (struct sockaddr_in6 *)(rtm + 1);
613 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
616 * Some OSes can produce a route that has the LINK flag but
617 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
618 * and BSD/OS, where xx is not the interface identifier on
619 * lo0). Such routes entry would annoy getnbrinfo() below,
621 * XXX: such routes should have the GATEWAY flag, not the
622 * LINK flag. However, there is rotten routing software
623 * that advertises all routes that have the GATEWAY flag.
624 * Thus, KAME kernel intentionally does not set the LINK flag.
625 * What is to be fixed is not ndp, but such routing software
626 * (and the kernel workaround)...
628 if (sdl->sdl_family != AF_LINK)
631 if (!(rtm->rtm_flags & RTF_HOST))
635 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
636 &sin->sin6_addr) == 0 ||
637 addr->sin6_scope_id != sin->sin6_scope_id)
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)
658 if (rtm->rtm_flags & RTF_PINNED)
664 gettimeofday(&now, 0);
668 addrwidth = strlen(host_buf);
669 if (addrwidth < W_ADDR)
671 llwidth = strlen(ether_str(sdl));
672 if (W_ADDR + W_LL - addrwidth > llwidth)
673 llwidth = W_ADDR + W_LL - addrwidth;
674 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
677 ifwidth = strlen(ifname);
678 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
679 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
681 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
682 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
684 /* Print neighbor discovery specific information */
685 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
687 if (nbi->expire > now.tv_sec) {
689 sec2str(nbi->expire - now.tv_sec));
690 } else if (nbi->expire == 0)
691 printf(" %-9.9s", "permanent");
693 printf(" %-9.9s", "expired");
695 switch (nbi->state) {
696 case ND6_LLINFO_NOSTATE:
699 #ifdef ND6_LLINFO_WAITDELETE
700 case ND6_LLINFO_WAITDELETE:
704 case ND6_LLINFO_INCOMPLETE:
707 case ND6_LLINFO_REACHABLE:
710 case ND6_LLINFO_STALE:
713 case ND6_LLINFO_DELAY:
716 case ND6_LLINFO_PROBE:
724 isrouter = nbi->isrouter;
727 warnx("failed to get neighbor information");
732 * other flags. R: router, P: proxy, W: ??
734 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
735 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
737 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
739 sin = (struct sockaddr_in6 *)
740 (sdl->sdl_len + (char *)sdl);
741 #if 0 /* W and P are mystery even for us */
742 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
744 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
745 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
746 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
748 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
750 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
753 printf(" %s", flgbuf);
771 static struct in6_nbrinfo *
772 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
774 static struct in6_nbrinfo nbi;
777 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
780 bzero(&nbi, sizeof(nbi));
781 if_indextoname(ifindex, nbi.ifname);
783 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
785 warn("ioctl(SIOCGNBRINFO_IN6)");
795 ether_str(struct sockaddr_dl *sdl)
797 static char hbuf[NI_MAXHOST];
799 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
800 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
802 } else if (sdl->sdl_alen) {
803 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
804 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
806 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
812 ndp_ether_aton(char *a, u_char *n)
816 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
817 &o[3], &o[4], &o[5]);
819 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
822 for (i = 0; i < 6; i++)
830 printf("usage: ndp [-nt] hostname\n");
831 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
832 printf(" ndp [-nt] -A wait\n");
833 printf(" ndp [-nt] -d hostname\n");
834 printf(" ndp [-nt] -f filename\n");
835 printf(" ndp [-nt] -i interface [flags...]\n");
836 #ifdef SIOCSDEFIFACE_IN6
837 printf(" ndp [-nt] -I [interface|delete]\n");
839 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
848 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
849 register char *cp = m_rtmsg.m_space;
853 if (cmd == RTM_DELETE)
855 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
856 rtm->rtm_flags = flags;
857 rtm->rtm_version = RTM_VERSION;
861 fprintf(stderr, "ndp: internal wrong cmd\n");
864 rtm->rtm_addrs |= RTA_GATEWAY;
866 rtm->rtm_rmx.rmx_expire = expire_time;
867 rtm->rtm_inits = RTV_EXPIRE;
869 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
870 #if 0 /* we don't support ipv6addr/128 type proxying */
871 if (rtm->rtm_flags & RTF_ANNOUNCE) {
872 rtm->rtm_flags &= ~RTF_HOST;
873 rtm->rtm_addrs |= RTA_NETMASK;
878 rtm->rtm_addrs |= RTA_DST;
881 NEXTADDR(RTA_DST, sin_m);
882 NEXTADDR(RTA_GATEWAY, sdl_m);
883 #if 0 /* we don't support ipv6addr/128 type proxying */
884 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
885 NEXTADDR(RTA_NETMASK, so_mask);
888 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
891 rtm->rtm_seq = ++seq;
893 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
894 if (errno != ESRCH || cmd != RTM_DELETE) {
895 err(1, "writing to routing socket");
900 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
901 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
903 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
909 ifinfo(char *ifname, int argc, char **argv)
911 struct in6_ndireq nd;
914 #ifdef IPV6CTL_USETEMPADDR
918 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
922 bzero(&nd, sizeof(nd));
923 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
924 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
925 err(1, "ioctl(SIOCGIFINFO_IN6)");
930 for (i = 0; i < argc; i++) {
939 #define SETFLAG(s, f) \
941 if (strcmp(cp, (s)) == 0) {\
949 * XXX: this macro is not 100% correct, in that it matches "nud" against
950 * "nudbogus". But we just let it go since this is minor.
952 #define SETVALUE(f, v) \
955 unsigned long newval; \
956 v = 0; /* unspecified */ \
957 if (strncmp(cp, f, strlen(f)) == 0) { \
958 valptr = strchr(cp, '='); \
959 if (valptr == NULL) \
960 err(1, "syntax error in %s field", (f)); \
962 newval = strtoul(++valptr, NULL, 0); \
964 err(1, "syntax error in %s's value", (f)); \
969 SETFLAG("disabled", ND6_IFF_IFDISABLED);
970 SETFLAG("nud", ND6_IFF_PERFORMNUD);
971 #ifdef ND6_IFF_ACCEPT_RTADV
972 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
974 #ifdef ND6_IFF_AUTO_LINKLOCAL
975 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
977 #ifdef ND6_IFF_NO_PREFER_IFACE
978 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
980 SETVALUE("basereachable", ND.basereachable);
981 SETVALUE("retrans", ND.retrans);
982 SETVALUE("curhlim", ND.chlim);
985 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
986 err(1, "ioctl(SIOCSIFINFO_IN6)");
993 if (!ND.initialized) {
994 errx(1, "%s: not initialized yet", ifname);
998 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
999 err(1, "ioctl(SIOCGIFINFO_IN6)");
1002 printf("linkmtu=%d", ND.linkmtu);
1003 printf(", maxmtu=%d", ND.maxmtu);
1004 printf(", curhlim=%d", ND.chlim);
1005 printf(", basereachable=%ds%dms",
1006 ND.basereachable / 1000, ND.basereachable % 1000);
1007 printf(", reachable=%ds", ND.reachable);
1008 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1009 #ifdef IPV6CTL_USETEMPADDR
1010 memset(nullbuf, 0, sizeof(nullbuf));
1011 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1015 for (i = 0; i < 3; i++) {
1018 printf("\nRandom seed(0): ");
1019 rbuf = ND.randomseed0;
1022 printf("\nRandom seed(1): ");
1023 rbuf = ND.randomseed1;
1026 printf("\nRandom ID: ");
1030 errx(1, "impossible case for tempaddr display");
1032 for (j = 0; j < 8; j++)
1033 printf("%02x", rbuf[j]);
1038 printf("\nFlags: ");
1039 #ifdef ND6_IFF_IFDISABLED
1040 if ((ND.flags & ND6_IFF_IFDISABLED))
1041 printf("disabled ");
1043 if ((ND.flags & ND6_IFF_PERFORMNUD))
1045 #ifdef ND6_IFF_ACCEPT_RTADV
1046 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1047 printf("accept_rtadv ");
1049 #ifdef ND6_IFF_AUTO_LINKLOCAL
1050 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1051 printf("auto_linklocal ");
1053 #ifdef ND6_IFF_NO_PREFER_IFACE
1054 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1055 printf("no_prefer_iface ");
1064 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1065 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1071 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1073 struct in6_defrouter *p, *ep;
1077 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1078 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1088 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1089 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1093 ep = (struct in6_defrouter *)(buf + l);
1094 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1097 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1098 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1099 (nflag ? NI_NUMERICHOST : 0)) != 0)
1100 strlcpy(host_buf, "?", sizeof(host_buf));
1102 printf("%s if=%s", host_buf,
1103 if_indextoname(p->if_index, ifix_buf));
1104 printf(", flags=%s%s",
1105 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1106 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1107 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1108 printf(", pref=%s", rtpref_str[rtpref]);
1110 gettimeofday(&now, 0);
1112 printf(", expire=Never\n");
1114 printf(", expire=%s\n",
1115 sec2str(p->expire - now.tv_sec));
1123 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1125 struct in6_prefix *p, *ep, *n;
1126 struct sockaddr_in6 *advrtr;
1129 const int niflags = NI_NUMERICHOST;
1130 int ninflags = nflag ? NI_NUMERICHOST : 0;
1131 char namebuf[NI_MAXHOST];
1133 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1134 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1142 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1143 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1147 ep = (struct in6_prefix *)(buf + l);
1148 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1149 advrtr = (struct sockaddr_in6 *)(p + 1);
1150 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1152 if (getnameinfo((struct sockaddr *)&p->prefix,
1153 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1154 NULL, 0, niflags) != 0)
1155 strlcpy(namebuf, "?", sizeof(namebuf));
1156 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1157 if_indextoname(p->if_index, ifix_buf));
1159 gettimeofday(&now, 0);
1161 * meaning of fields, especially flags, is very different
1162 * by origin. notify the difference to the users.
1164 printf("flags=%s%s%s%s%s",
1165 p->raflags.onlink ? "L" : "",
1166 p->raflags.autonomous ? "A" : "",
1167 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1168 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1170 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1175 if (p->vltime == ND6_INFINITE_LIFETIME)
1176 printf(" vltime=infinity");
1178 printf(" vltime=%lu", (unsigned long)p->vltime);
1179 if (p->pltime == ND6_INFINITE_LIFETIME)
1180 printf(", pltime=infinity");
1182 printf(", pltime=%lu", (unsigned long)p->pltime);
1184 printf(", expire=Never");
1185 else if (p->expire >= now.tv_sec)
1186 printf(", expire=%s",
1187 sec2str(p->expire - now.tv_sec));
1189 printf(", expired");
1190 printf(", ref=%d", p->refcnt);
1193 * "advertising router" list is meaningful only if the prefix
1194 * information is from RA.
1198 struct sockaddr_in6 *sin6;
1201 printf(" advertised by\n");
1202 for (j = 0; j < p->advrtrs; j++) {
1203 struct in6_nbrinfo *nbi;
1205 if (getnameinfo((struct sockaddr *)sin6,
1206 sin6->sin6_len, namebuf, sizeof(namebuf),
1207 NULL, 0, ninflags) != 0)
1208 strlcpy(namebuf, "?", sizeof(namebuf));
1209 printf(" %s", namebuf);
1211 nbi = getnbrinfo(&sin6->sin6_addr,
1214 switch (nbi->state) {
1215 case ND6_LLINFO_REACHABLE:
1216 case ND6_LLINFO_STALE:
1217 case ND6_LLINFO_DELAY:
1218 case ND6_LLINFO_PROBE:
1219 printf(" (reachable)\n");
1222 printf(" (unreachable)\n");
1225 printf(" (no neighbor state)\n");
1229 printf(" No advertising router\n");
1237 char dummyif[IFNAMSIZ+8];
1240 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1242 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1243 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1244 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1250 char dummyif[IFNAMSIZ+8];
1253 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1255 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1256 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1257 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1265 char dummyif[IFNAMSIZ+8];
1268 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1270 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1271 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1272 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1277 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1279 setdefif(char *ifname)
1281 struct in6_ndifreq ndifreq;
1282 unsigned int ifindex;
1284 if (strcasecmp(ifname, "delete") == 0)
1287 if ((ifindex = if_nametoindex(ifname)) == 0)
1288 err(1, "failed to resolve i/f index for %s", ifname);
1291 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1294 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1295 ndifreq.ifindex = ifindex;
1297 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1298 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1306 struct in6_ndifreq ndifreq;
1307 char ifname[IFNAMSIZ+8];
1309 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1312 memset(&ndifreq, 0, sizeof(ndifreq));
1313 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1315 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1316 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1318 if (ndifreq.ifindex == 0)
1319 printf("No default interface.\n");
1321 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1322 err(1, "failed to resolve ifname for index %lu",
1324 printf("ND default interface = %s\n", ifname);
1332 sec2str(time_t total)
1334 static char result[256];
1335 int days, hours, mins, secs;
1338 char *ep = &result[sizeof(result)];
1341 days = total / 3600 / 24;
1342 hours = (total / 3600) % 24;
1343 mins = (total / 60) % 60;
1348 n = snprintf(p, ep - p, "%dd", days);
1349 if (n < 0 || n >= ep - p)
1353 if (!first || hours) {
1355 n = snprintf(p, ep - p, "%dh", hours);
1356 if (n < 0 || n >= ep - p)
1360 if (!first || mins) {
1362 n = snprintf(p, ep - p, "%dm", mins);
1363 if (n < 0 || n >= ep - p)
1367 snprintf(p, ep - p, "%ds", secs);
1373 * Print the timestamp
1374 * from tcpdump/util.c
1377 ts_print(const struct timeval *tvp)
1382 s = (tvp->tv_sec + thiszone) % 86400;
1383 (void)printf("%02d:%02d:%02d.%06u ",
1384 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);