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)
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 information */
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(struct in6_addr *addr, int ifindex, int warning)
772 static struct in6_nbrinfo nbi;
775 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
778 bzero(&nbi, sizeof(nbi));
779 if_indextoname(ifindex, nbi.ifname);
781 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
783 warn("ioctl(SIOCGNBRINFO_IN6)");
793 ether_str(struct sockaddr_dl *sdl)
795 static char hbuf[NI_MAXHOST];
797 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
798 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
800 } else if (sdl->sdl_alen) {
801 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
802 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
804 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
810 ndp_ether_aton(char *a, u_char *n)
814 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
815 &o[3], &o[4], &o[5]);
817 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
820 for (i = 0; i < 6; i++)
828 printf("usage: ndp [-nt] hostname\n");
829 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
830 printf(" ndp [-nt] -A wait\n");
831 printf(" ndp [-nt] -d hostname\n");
832 printf(" ndp [-nt] -f filename\n");
833 printf(" ndp [-nt] -i interface [flags...]\n");
834 #ifdef SIOCSDEFIFACE_IN6
835 printf(" ndp [-nt] -I [interface|delete]\n");
837 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
846 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
847 register char *cp = m_rtmsg.m_space;
851 if (cmd == RTM_DELETE)
853 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
854 rtm->rtm_flags = flags;
855 rtm->rtm_version = RTM_VERSION;
859 fprintf(stderr, "ndp: internal wrong cmd\n");
862 rtm->rtm_addrs |= RTA_GATEWAY;
864 rtm->rtm_rmx.rmx_expire = expire_time;
865 rtm->rtm_inits = RTV_EXPIRE;
867 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
868 #if 0 /* we don't support ipv6addr/128 type proxying */
869 if (rtm->rtm_flags & RTF_ANNOUNCE) {
870 rtm->rtm_flags &= ~RTF_HOST;
871 rtm->rtm_addrs |= RTA_NETMASK;
876 rtm->rtm_addrs |= RTA_DST;
879 NEXTADDR(RTA_DST, sin_m);
880 NEXTADDR(RTA_GATEWAY, sdl_m);
881 #if 0 /* we don't support ipv6addr/128 type proxying */
882 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
883 NEXTADDR(RTA_NETMASK, so_mask);
886 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
889 rtm->rtm_seq = ++seq;
891 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
892 if (errno != ESRCH || cmd != RTM_DELETE) {
893 err(1, "writing to routing socket");
898 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
899 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
901 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
907 ifinfo(char *ifname, int argc, char **argv)
909 struct in6_ndireq nd;
912 #ifdef IPV6CTL_USETEMPADDR
916 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
920 bzero(&nd, sizeof(nd));
921 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
922 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
923 err(1, "ioctl(SIOCGIFINFO_IN6)");
928 for (i = 0; i < argc; i++) {
937 #define SETFLAG(s, f) \
939 if (strcmp(cp, (s)) == 0) {\
947 * XXX: this macro is not 100% correct, in that it matches "nud" against
948 * "nudbogus". But we just let it go since this is minor.
950 #define SETVALUE(f, v) \
953 unsigned long newval; \
954 v = 0; /* unspecified */ \
955 if (strncmp(cp, f, strlen(f)) == 0) { \
956 valptr = strchr(cp, '='); \
957 if (valptr == NULL) \
958 err(1, "syntax error in %s field", (f)); \
960 newval = strtoul(++valptr, NULL, 0); \
962 err(1, "syntax error in %s's value", (f)); \
967 SETFLAG("disabled", ND6_IFF_IFDISABLED);
968 SETFLAG("nud", ND6_IFF_PERFORMNUD);
969 #ifdef ND6_IFF_ACCEPT_RTADV
970 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
972 #ifdef ND6_IFF_AUTO_LINKLOCAL
973 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
975 #ifdef ND6_IFF_NO_PREFER_IFACE
976 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
978 SETVALUE("basereachable", ND.basereachable);
979 SETVALUE("retrans", ND.retrans);
980 SETVALUE("curhlim", ND.chlim);
983 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
984 err(1, "ioctl(SIOCSIFINFO_IN6)");
991 if (!ND.initialized) {
992 errx(1, "%s: not initialized yet", ifname);
996 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
997 err(1, "ioctl(SIOCGIFINFO_IN6)");
1000 printf("linkmtu=%d", ND.linkmtu);
1001 printf(", maxmtu=%d", ND.maxmtu);
1002 printf(", curhlim=%d", ND.chlim);
1003 printf(", basereachable=%ds%dms",
1004 ND.basereachable / 1000, ND.basereachable % 1000);
1005 printf(", reachable=%ds", ND.reachable);
1006 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1007 #ifdef IPV6CTL_USETEMPADDR
1008 memset(nullbuf, 0, sizeof(nullbuf));
1009 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1013 for (i = 0; i < 3; i++) {
1016 printf("\nRandom seed(0): ");
1017 rbuf = ND.randomseed0;
1020 printf("\nRandom seed(1): ");
1021 rbuf = ND.randomseed1;
1024 printf("\nRandom ID: ");
1028 errx(1, "impossible case for tempaddr display");
1030 for (j = 0; j < 8; j++)
1031 printf("%02x", rbuf[j]);
1036 printf("\nFlags: ");
1037 #ifdef ND6_IFF_IFDISABLED
1038 if ((ND.flags & ND6_IFF_IFDISABLED))
1039 printf("disabled ");
1041 if ((ND.flags & ND6_IFF_PERFORMNUD))
1043 #ifdef ND6_IFF_ACCEPT_RTADV
1044 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1045 printf("accept_rtadv ");
1047 #ifdef ND6_IFF_AUTO_LINKLOCAL
1048 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1049 printf("auto_linklocal ");
1051 #ifdef ND6_IFF_NO_PREFER_IFACE
1052 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1053 printf("no_prefer_iface ");
1062 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1063 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1069 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1071 struct in6_defrouter *p, *ep;
1075 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1076 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1086 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1087 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1091 ep = (struct in6_defrouter *)(buf + l);
1092 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1095 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1096 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1097 (nflag ? NI_NUMERICHOST : 0)) != 0)
1098 strlcpy(host_buf, "?", sizeof(host_buf));
1100 printf("%s if=%s", host_buf,
1101 if_indextoname(p->if_index, ifix_buf));
1102 printf(", flags=%s%s",
1103 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1104 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1105 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1106 printf(", pref=%s", rtpref_str[rtpref]);
1108 gettimeofday(&now, 0);
1110 printf(", expire=Never\n");
1112 printf(", expire=%s\n",
1113 sec2str(p->expire - now.tv_sec));
1121 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1123 struct in6_prefix *p, *ep, *n;
1124 struct sockaddr_in6 *advrtr;
1127 const int niflags = NI_NUMERICHOST;
1128 int ninflags = nflag ? NI_NUMERICHOST : 0;
1129 char namebuf[NI_MAXHOST];
1131 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1132 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1140 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1141 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1145 ep = (struct in6_prefix *)(buf + l);
1146 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1147 advrtr = (struct sockaddr_in6 *)(p + 1);
1148 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1150 if (getnameinfo((struct sockaddr *)&p->prefix,
1151 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1152 NULL, 0, niflags) != 0)
1153 strlcpy(namebuf, "?", sizeof(namebuf));
1154 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1155 if_indextoname(p->if_index, ifix_buf));
1157 gettimeofday(&now, 0);
1159 * meaning of fields, especially flags, is very different
1160 * by origin. notify the difference to the users.
1162 printf("flags=%s%s%s%s%s",
1163 p->raflags.onlink ? "L" : "",
1164 p->raflags.autonomous ? "A" : "",
1165 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1166 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1168 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1173 if (p->vltime == ND6_INFINITE_LIFETIME)
1174 printf(" vltime=infinity");
1176 printf(" vltime=%lu", (unsigned long)p->vltime);
1177 if (p->pltime == ND6_INFINITE_LIFETIME)
1178 printf(", pltime=infinity");
1180 printf(", pltime=%lu", (unsigned long)p->pltime);
1182 printf(", expire=Never");
1183 else if (p->expire >= now.tv_sec)
1184 printf(", expire=%s",
1185 sec2str(p->expire - now.tv_sec));
1187 printf(", expired");
1188 printf(", ref=%d", p->refcnt);
1191 * "advertising router" list is meaningful only if the prefix
1192 * information is from RA.
1196 struct sockaddr_in6 *sin6;
1199 printf(" advertised by\n");
1200 for (j = 0; j < p->advrtrs; j++) {
1201 struct in6_nbrinfo *nbi;
1203 if (getnameinfo((struct sockaddr *)sin6,
1204 sin6->sin6_len, namebuf, sizeof(namebuf),
1205 NULL, 0, ninflags) != 0)
1206 strlcpy(namebuf, "?", sizeof(namebuf));
1207 printf(" %s", namebuf);
1209 nbi = getnbrinfo(&sin6->sin6_addr,
1212 switch (nbi->state) {
1213 case ND6_LLINFO_REACHABLE:
1214 case ND6_LLINFO_STALE:
1215 case ND6_LLINFO_DELAY:
1216 case ND6_LLINFO_PROBE:
1217 printf(" (reachable)\n");
1220 printf(" (unreachable)\n");
1223 printf(" (no neighbor state)\n");
1227 printf(" No advertising router\n");
1235 char dummyif[IFNAMSIZ+8];
1238 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1240 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1241 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1242 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1248 char dummyif[IFNAMSIZ+8];
1251 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1253 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1254 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1255 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1263 char dummyif[IFNAMSIZ+8];
1266 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1268 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1269 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1270 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1275 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1277 setdefif(char *ifname)
1279 struct in6_ndifreq ndifreq;
1280 unsigned int ifindex;
1282 if (strcasecmp(ifname, "delete") == 0)
1285 if ((ifindex = if_nametoindex(ifname)) == 0)
1286 err(1, "failed to resolve i/f index for %s", ifname);
1289 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1292 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1293 ndifreq.ifindex = ifindex;
1295 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1296 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1304 struct in6_ndifreq ndifreq;
1305 char ifname[IFNAMSIZ+8];
1307 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1310 memset(&ndifreq, 0, sizeof(ndifreq));
1311 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1313 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1314 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1316 if (ndifreq.ifindex == 0)
1317 printf("No default interface.\n");
1319 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1320 err(1, "failed to resolve ifname for index %lu",
1322 printf("ND default interface = %s\n", ifname);
1330 sec2str(time_t total)
1332 static char result[256];
1333 int days, hours, mins, secs;
1336 char *ep = &result[sizeof(result)];
1339 days = total / 3600 / 24;
1340 hours = (total / 3600) % 24;
1341 mins = (total / 60) % 60;
1346 n = snprintf(p, ep - p, "%dd", days);
1347 if (n < 0 || n >= ep - p)
1351 if (!first || hours) {
1353 n = snprintf(p, ep - p, "%dh", hours);
1354 if (n < 0 || n >= ep - p)
1358 if (!first || mins) {
1360 n = snprintf(p, ep - p, "%dm", mins);
1361 if (n < 0 || n >= ep - p)
1365 snprintf(p, ep - p, "%ds", secs);
1371 * Print the timestamp
1372 * from tcpdump/util.c
1375 ts_print(const struct timeval *tvp)
1380 s = (tvp->tv_sec + thiszone) % 86400;
1381 (void)printf("%02d:%02d:%02d.%06u ",
1382 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);