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 /* packing rule for routing socket */
114 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
115 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
117 #define NEXTADDR(w, s) \
118 if (rtm->rtm_addrs & (w)) { \
119 bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
125 static int32_t thiszone; /* time difference with gmt */
127 static int repeat = 0;
129 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
130 char host_buf[NI_MAXHOST]; /* getnameinfo() */
131 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
133 int main(int, char **);
135 void getsocket(void);
136 int set(int, char **);
139 void dump(struct in6_addr *, int);
140 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
141 static char *ether_str(struct sockaddr_dl *);
142 int ndp_ether_aton(char *, u_char *);
145 void ifinfo(char *, int, char **);
148 void pfx_flush(void);
149 void rtr_flush(void);
150 void harmonize_rtr(void);
151 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
152 static void getdefif(void);
153 static void setdefif(char *);
155 static char *sec2str(time_t);
156 static void ts_print(const struct timeval *);
158 #ifdef ICMPV6CTL_ND6_DRLIST
159 static char *rtpref_str[] = {
178 thiszone = gmt2local(0);
179 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
219 repeat = atoi(optarg);
239 dump(0, mode == 'c');
249 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
253 } else if (argc == 1) {
254 if (strcmp(*argv, "delete") == 0 ||
255 if_nametoindex(*argv))
258 errx(1, "invalid interface %s", *argv);
260 getdefif(); /* always call it to print the result */
263 errx(1, "not supported yet");
274 ifinfo(arg, argc, argv);
284 if (argc < 2 || argc > 4)
286 exit(set(argc, argv) ? 1 : 0);
320 * Process a file to set standard ndp entries
328 char line[100], arg[5][50], *args[5];
330 if ((fp = fopen(name, "r")) == NULL) {
331 fprintf(stderr, "ndp: cannot open %s\n", name);
334 args[0] = &arg[0][0];
335 args[1] = &arg[1][0];
336 args[2] = &arg[2][0];
337 args[3] = &arg[3][0];
338 args[4] = &arg[4][0];
340 while (fgets(line, sizeof(line), fp) != NULL) {
341 i = sscanf(line, "%49s %49s %49s %49s %49s",
342 arg[0], arg[1], arg[2], arg[3], arg[4]);
344 fprintf(stderr, "ndp: bad line: %s\n", line);
359 s = socket(PF_ROUTE, SOCK_RAW, 0);
367 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
368 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
369 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
370 int expire_time, flags, found_entry;
372 struct rt_msghdr m_rtm;
377 * Set an individual neighbor cache entry
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 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
409 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
410 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
413 ea = (u_char *)LLADDR(&sdl_m);
414 if (ndp_ether_aton(eaddr, ea) == 0)
416 flags = expire_time = 0;
418 if (strncmp(argv[0], "temp", 4) == 0) {
421 gettimeofday(&time, 0);
422 expire_time = time.tv_sec + 20 * 60;
423 } else if (strncmp(argv[0], "proxy", 5) == 0)
424 flags |= RTF_ANNOUNCE;
427 if (rtmsg(RTM_GET) < 0) {
428 errx(1, "RTM_GET(%s) failed", host);
431 sin = (struct sockaddr_in6 *)(rtm + 1);
432 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
433 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
434 if (sdl->sdl_family == AF_LINK &&
435 !(rtm->rtm_flags & RTF_GATEWAY)) {
436 switch (sdl->sdl_type) {
437 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
438 case IFT_ISO88024: case IFT_ISO88025:
439 case IFT_L2VLAN: case IFT_BRIDGE:
444 * IPv4 arp command retries with sin_other = SIN_PROXY here.
446 fprintf(stderr, "set: cannot configure a new entry\n");
451 if (sdl->sdl_family != AF_LINK) {
452 printf("cannot intuit interface index and type for %s\n", host);
455 sdl_m.sdl_type = sdl->sdl_type;
456 sdl_m.sdl_index = sdl->sdl_index;
457 return (rtmsg(RTM_ADD));
461 * Display an individual neighbor cache entry
467 struct sockaddr_in6 *sin = &sin_m;
468 struct addrinfo hints, *res;
472 bzero(&hints, sizeof(hints));
473 hints.ai_family = AF_INET6;
474 gai_error = getaddrinfo(host, NULL, &hints, &res);
476 fprintf(stderr, "ndp: %s: %s\n", host,
477 gai_strerror(gai_error));
480 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
482 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
483 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
484 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
487 dump(&sin->sin6_addr, 0);
488 if (found_entry == 0) {
489 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
490 sizeof(host_buf), NULL ,0,
491 (nflag ? NI_NUMERICHOST : 0));
492 printf("%s (%s) -- no entry\n", host, host_buf);
498 * Delete a neighbor cache entry
504 struct sockaddr_in6 *sin = &sin_m;
505 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
506 register char *cp = m_rtmsg.m_space;
507 struct sockaddr_dl *sdl;
508 struct addrinfo hints, *res;
514 bzero(&hints, sizeof(hints));
515 hints.ai_family = AF_INET6;
516 gai_error = getaddrinfo(host, NULL, &hints, &res);
518 fprintf(stderr, "ndp: %s: %s\n", host,
519 gai_strerror(gai_error));
522 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
524 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
525 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
526 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
529 if (rtmsg(RTM_GET) < 0) {
530 errx(1, "RTM_GET(%s) failed", host);
533 sin = (struct sockaddr_in6 *)(rtm + 1);
534 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
535 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
536 if (sdl->sdl_family == AF_LINK &&
537 !(rtm->rtm_flags & RTF_GATEWAY)) {
541 * IPv4 arp command retries with sin_other = SIN_PROXY here.
543 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
548 if (sdl->sdl_family != AF_LINK) {
549 printf("cannot locate %s\n", host);
553 * need to reinit the field because it has rt_key
554 * but we want the actual address
556 NEXTADDR(RTA_DST, sin_m);
557 rtm->rtm_flags |= RTF_LLDATA;
558 if (rtmsg(RTM_DELETE) == 0) {
559 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
562 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
563 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
564 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
567 getnameinfo((struct sockaddr *)&s6,
568 s6.sin6_len, host_buf,
569 sizeof(host_buf), NULL, 0,
570 (nflag ? NI_NUMERICHOST : 0));
571 printf("%s (%s) deleted\n", host, host_buf);
582 * Dump the entire neighbor cache
586 struct in6_addr *addr;
591 char *lim, *buf, *next;
592 struct rt_msghdr *rtm;
593 struct sockaddr_in6 *sin;
594 struct sockaddr_dl *sdl;
596 struct in6_nbrinfo *nbi;
605 if (!tflag && !cflag)
606 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
607 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
608 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
615 mib[4] = NET_RT_FLAGS;
621 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
622 err(1, "sysctl(PF_ROUTE estimate)");
624 if ((buf = malloc(needed)) == NULL)
626 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
627 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
632 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
633 int isrouter = 0, prbs = 0;
635 rtm = (struct rt_msghdr *)next;
636 sin = (struct sockaddr_in6 *)(rtm + 1);
637 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
640 * Some OSes can produce a route that has the LINK flag but
641 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
642 * and BSD/OS, where xx is not the interface identifier on
643 * lo0). Such routes entry would annoy getnbrinfo() below,
645 * XXX: such routes should have the GATEWAY flag, not the
646 * LINK flag. However, there is rotten routing software
647 * that advertises all routes that have the GATEWAY flag.
648 * Thus, KAME kernel intentionally does not set the LINK flag.
649 * What is to be fixed is not ndp, but such routing software
650 * (and the kernel workaround)...
652 if (sdl->sdl_family != AF_LINK)
655 if (!(rtm->rtm_flags & RTF_HOST))
659 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
662 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
664 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
665 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
666 /* XXX: should scope id be filled in the kernel? */
667 if (sin->sin6_scope_id == 0)
668 sin->sin6_scope_id = sdl->sdl_index;
670 /* KAME specific hack; removed the embedded id */
671 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
674 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
675 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
678 if (rtm->rtm_flags & RTF_WASCLONED)
680 #elif defined(RTF_CLONED)
681 if (rtm->rtm_flags & RTF_CLONED)
688 gettimeofday(&time, 0);
692 addrwidth = strlen(host_buf);
693 if (addrwidth < W_ADDR)
695 llwidth = strlen(ether_str(sdl));
696 if (W_ADDR + W_LL - addrwidth > llwidth)
697 llwidth = W_ADDR + W_LL - addrwidth;
698 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
701 ifwidth = strlen(ifname);
702 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
703 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
705 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
706 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
708 /* Print neighbor discovery specific informations */
709 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
711 if (nbi->expire > time.tv_sec) {
713 sec2str(nbi->expire - time.tv_sec));
714 } else if (nbi->expire == 0)
715 printf(" %-9.9s", "permanent");
717 printf(" %-9.9s", "expired");
719 switch (nbi->state) {
720 case ND6_LLINFO_NOSTATE:
723 #ifdef ND6_LLINFO_WAITDELETE
724 case ND6_LLINFO_WAITDELETE:
728 case ND6_LLINFO_INCOMPLETE:
731 case ND6_LLINFO_REACHABLE:
734 case ND6_LLINFO_STALE:
737 case ND6_LLINFO_DELAY:
740 case ND6_LLINFO_PROBE:
748 isrouter = nbi->isrouter;
751 warnx("failed to get neighbor information");
756 * other flags. R: router, P: proxy, W: ??
758 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
759 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
761 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
763 sin = (struct sockaddr_in6 *)
764 (sdl->sdl_len + (char *)sdl);
765 #if 0 /* W and P are mystery even for us */
766 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
768 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
769 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
770 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
772 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
774 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
777 printf(" %s", flgbuf);
795 static struct in6_nbrinfo *
796 getnbrinfo(addr, ifindex, warning)
797 struct in6_addr *addr;
801 static struct in6_nbrinfo nbi;
804 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
807 bzero(&nbi, sizeof(nbi));
808 if_indextoname(ifindex, nbi.ifname);
810 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
812 warn("ioctl(SIOCGNBRINFO_IN6)");
822 ether_str(struct sockaddr_dl *sdl)
824 static char hbuf[NI_MAXHOST];
826 if (sdl->sdl_alen > 0)
827 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
830 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
842 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
843 &o[3], &o[4], &o[5]);
845 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
848 for (i = 0; i < 6; i++)
856 printf("usage: ndp [-nt] hostname\n");
857 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
858 printf(" ndp [-nt] -A wait\n");
859 printf(" ndp [-nt] -d hostname\n");
860 printf(" ndp [-nt] -f filename\n");
861 printf(" ndp [-nt] -i interface [flags...]\n");
862 #ifdef SIOCSDEFIFACE_IN6
863 printf(" ndp [-nt] -I [interface|delete]\n");
865 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
875 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
876 register char *cp = m_rtmsg.m_space;
880 if (cmd == RTM_DELETE)
882 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
883 rtm->rtm_flags = flags;
884 rtm->rtm_version = RTM_VERSION;
888 fprintf(stderr, "ndp: internal wrong cmd\n");
891 rtm->rtm_addrs |= RTA_GATEWAY;
893 rtm->rtm_rmx.rmx_expire = expire_time;
894 rtm->rtm_inits = RTV_EXPIRE;
896 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
897 #if 0 /* we don't support ipv6addr/128 type proxying */
898 if (rtm->rtm_flags & RTF_ANNOUNCE) {
899 rtm->rtm_flags &= ~RTF_HOST;
900 rtm->rtm_addrs |= RTA_NETMASK;
905 rtm->rtm_addrs |= RTA_DST;
908 NEXTADDR(RTA_DST, sin_m);
909 NEXTADDR(RTA_GATEWAY, sdl_m);
910 #if 0 /* we don't support ipv6addr/128 type proxying */
911 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
912 NEXTADDR(RTA_NETMASK, so_mask);
915 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
918 rtm->rtm_seq = ++seq;
920 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
921 if (errno != ESRCH || cmd != RTM_DELETE) {
922 err(1, "writing to routing socket");
927 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
928 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
930 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
936 ifinfo(ifname, argc, argv)
941 struct in6_ndireq nd;
944 #ifdef IPV6CTL_USETEMPADDR
948 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
952 bzero(&nd, sizeof(nd));
953 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
954 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
955 err(1, "ioctl(SIOCGIFINFO_IN6)");
960 for (i = 0; i < argc; i++) {
969 #define SETFLAG(s, f) \
971 if (strcmp(cp, (s)) == 0) {\
979 * XXX: this macro is not 100% correct, in that it matches "nud" against
980 * "nudbogus". But we just let it go since this is minor.
982 #define SETVALUE(f, v) \
985 unsigned long newval; \
986 v = 0; /* unspecified */ \
987 if (strncmp(cp, f, strlen(f)) == 0) { \
988 valptr = strchr(cp, '='); \
989 if (valptr == NULL) \
990 err(1, "syntax error in %s field", (f)); \
992 newval = strtoul(++valptr, NULL, 0); \
994 err(1, "syntax error in %s's value", (f)); \
999 SETFLAG("disabled", ND6_IFF_IFDISABLED);
1000 SETFLAG("nud", ND6_IFF_PERFORMNUD);
1001 #ifdef ND6_IFF_ACCEPT_RTADV
1002 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
1004 #ifdef ND6_IFF_PREFER_SOURCE
1005 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
1007 SETVALUE("basereachable", ND.basereachable);
1008 SETVALUE("retrans", ND.retrans);
1009 SETVALUE("curhlim", ND.chlim);
1011 ND.flags = newflags;
1012 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
1013 err(1, "ioctl(SIOCSIFINFO_IN6)");
1020 if (!ND.initialized) {
1021 errx(1, "%s: not initialized yet", ifname);
1025 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
1026 err(1, "ioctl(SIOCGIFINFO_IN6)");
1029 printf("linkmtu=%d", ND.linkmtu);
1030 printf(", maxmtu=%d", ND.maxmtu);
1031 printf(", curhlim=%d", ND.chlim);
1032 printf(", basereachable=%ds%dms",
1033 ND.basereachable / 1000, ND.basereachable % 1000);
1034 printf(", reachable=%ds", ND.reachable);
1035 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1036 #ifdef IPV6CTL_USETEMPADDR
1037 memset(nullbuf, 0, sizeof(nullbuf));
1038 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1042 for (i = 0; i < 3; i++) {
1045 printf("\nRandom seed(0): ");
1046 rbuf = ND.randomseed0;
1049 printf("\nRandom seed(1): ");
1050 rbuf = ND.randomseed1;
1053 printf("\nRandom ID: ");
1057 errx(1, "impossible case for tempaddr display");
1059 for (j = 0; j < 8; j++)
1060 printf("%02x", rbuf[j]);
1065 printf("\nFlags: ");
1066 #ifdef ND6_IFF_IFDISABLED
1067 if ((ND.flags & ND6_IFF_IFDISABLED))
1068 printf("disabled ");
1070 if ((ND.flags & ND6_IFF_PERFORMNUD))
1072 #ifdef ND6_IFF_ACCEPT_RTADV
1073 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1074 printf("accept_rtadv ");
1076 #ifdef ND6_IFF_PREFER_SOURCE
1077 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1078 printf("prefer_source ");
1087 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1088 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1094 #ifdef ICMPV6CTL_ND6_DRLIST
1095 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1097 struct in6_defrouter *p, *ep;
1099 struct timeval time;
1101 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1102 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1112 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1113 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1117 ep = (struct in6_defrouter *)(buf + l);
1118 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1121 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1122 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1123 (nflag ? NI_NUMERICHOST : 0)) != 0)
1124 strlcpy(host_buf, "?", sizeof(host_buf));
1126 printf("%s if=%s", host_buf,
1127 if_indextoname(p->if_index, ifix_buf));
1128 printf(", flags=%s%s",
1129 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1130 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1131 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1132 printf(", pref=%s", rtpref_str[rtpref]);
1134 gettimeofday(&time, 0);
1136 printf(", expire=Never\n");
1138 printf(", expire=%s\n",
1139 sec2str(p->expire - time.tv_sec));
1143 struct in6_drlist dr;
1145 struct timeval time;
1147 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1151 bzero(&dr, sizeof(dr));
1152 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1153 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1154 err(1, "ioctl(SIOCGDRLST_IN6)");
1157 #define DR dr.defrouter[i]
1158 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1159 struct sockaddr_in6 sin6;
1161 bzero(&sin6, sizeof(sin6));
1162 sin6.sin6_family = AF_INET6;
1163 sin6.sin6_len = sizeof(sin6);
1164 sin6.sin6_addr = DR.rtaddr;
1165 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1166 sizeof(host_buf), NULL, 0,
1167 (nflag ? NI_NUMERICHOST : 0));
1169 printf("%s if=%s", host_buf,
1170 if_indextoname(DR.if_index, ifix_buf));
1171 printf(", flags=%s%s",
1172 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1173 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1174 gettimeofday(&time, 0);
1176 printf(", expire=Never\n");
1178 printf(", expire=%s\n",
1179 sec2str(DR.expire - time.tv_sec));
1189 #ifdef ICMPV6CTL_ND6_PRLIST
1190 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1192 struct in6_prefix *p, *ep, *n;
1193 struct sockaddr_in6 *advrtr;
1195 struct timeval time;
1196 const int niflags = NI_NUMERICHOST;
1197 int ninflags = nflag ? NI_NUMERICHOST : 0;
1198 char namebuf[NI_MAXHOST];
1200 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1201 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1209 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1210 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1214 ep = (struct in6_prefix *)(buf + l);
1215 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1216 advrtr = (struct sockaddr_in6 *)(p + 1);
1217 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1219 if (getnameinfo((struct sockaddr *)&p->prefix,
1220 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1221 NULL, 0, niflags) != 0)
1222 strlcpy(namebuf, "?", sizeof(namebuf));
1223 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1224 if_indextoname(p->if_index, ifix_buf));
1226 gettimeofday(&time, 0);
1228 * meaning of fields, especially flags, is very different
1229 * by origin. notify the difference to the users.
1231 printf("flags=%s%s%s%s%s",
1232 p->raflags.onlink ? "L" : "",
1233 p->raflags.autonomous ? "A" : "",
1234 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1235 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1237 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1242 if (p->vltime == ND6_INFINITE_LIFETIME)
1243 printf(" vltime=infinity");
1245 printf(" vltime=%lu", (unsigned long)p->vltime);
1246 if (p->pltime == ND6_INFINITE_LIFETIME)
1247 printf(", pltime=infinity");
1249 printf(", pltime=%lu", (unsigned long)p->pltime);
1251 printf(", expire=Never");
1252 else if (p->expire >= time.tv_sec)
1253 printf(", expire=%s",
1254 sec2str(p->expire - time.tv_sec));
1256 printf(", expired");
1257 printf(", ref=%d", p->refcnt);
1260 * "advertising router" list is meaningful only if the prefix
1261 * information is from RA.
1265 struct sockaddr_in6 *sin6;
1268 printf(" advertised by\n");
1269 for (j = 0; j < p->advrtrs; j++) {
1270 struct in6_nbrinfo *nbi;
1272 if (getnameinfo((struct sockaddr *)sin6,
1273 sin6->sin6_len, namebuf, sizeof(namebuf),
1274 NULL, 0, ninflags) != 0)
1275 strlcpy(namebuf, "?", sizeof(namebuf));
1276 printf(" %s", namebuf);
1278 nbi = getnbrinfo(&sin6->sin6_addr,
1281 switch (nbi->state) {
1282 case ND6_LLINFO_REACHABLE:
1283 case ND6_LLINFO_STALE:
1284 case ND6_LLINFO_DELAY:
1285 case ND6_LLINFO_PROBE:
1286 printf(" (reachable)\n");
1289 printf(" (unreachable)\n");
1292 printf(" (no neighbor state)\n");
1296 printf(" No advertising router\n");
1300 struct in6_prlist pr;
1302 struct timeval time;
1304 gettimeofday(&time, 0);
1306 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1310 bzero(&pr, sizeof(pr));
1311 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1312 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1313 err(1, "ioctl(SIOCGPRLST_IN6)");
1316 #define PR pr.prefix[i]
1317 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1318 struct sockaddr_in6 p6;
1319 char namebuf[NI_MAXHOST];
1325 memset(&p6, 0, sizeof(p6));
1326 p6.sin6_family = AF_INET6;
1327 p6.sin6_len = sizeof(p6);
1328 p6.sin6_addr = PR.prefix;
1332 * copy link index to sin6_scope_id field.
1333 * XXX: KAME specific.
1335 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1338 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1340 linkid = ntohs(linkid);
1341 p6.sin6_scope_id = linkid;
1342 p6.sin6_addr.s6_addr[2] = 0;
1343 p6.sin6_addr.s6_addr[3] = 0;
1346 niflags = NI_NUMERICHOST;
1347 if (getnameinfo((struct sockaddr *)&p6,
1348 sizeof(p6), namebuf, sizeof(namebuf),
1349 NULL, 0, niflags)) {
1350 warnx("getnameinfo failed");
1353 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1354 if_indextoname(PR.if_index, ifix_buf));
1356 gettimeofday(&time, 0);
1358 * meaning of fields, especially flags, is very different
1359 * by origin. notify the difference to the users.
1363 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1366 printf("flags=%s%s%s%s%s",
1367 PR.raflags.onlink ? "L" : "",
1368 PR.raflags.autonomous ? "A" : "",
1369 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1370 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1372 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1378 printf("flags=%s%s",
1379 PR.raflags.onlink ? "L" : "",
1380 PR.raflags.autonomous ? "A" : "");
1382 if (PR.vltime == ND6_INFINITE_LIFETIME)
1383 printf(" vltime=infinity");
1385 printf(" vltime=%lu", PR.vltime);
1386 if (PR.pltime == ND6_INFINITE_LIFETIME)
1387 printf(", pltime=infinity");
1389 printf(", pltime=%lu", PR.pltime);
1391 printf(", expire=Never");
1392 else if (PR.expire >= time.tv_sec)
1393 printf(", expire=%s",
1394 sec2str(PR.expire - time.tv_sec));
1396 printf(", expired");
1398 printf(", ref=%d", PR.refcnt);
1401 switch (PR.origin) {
1403 printf(", origin=RA");
1406 printf(", origin=RR");
1408 case PR_ORIG_STATIC:
1409 printf(", origin=static");
1411 case PR_ORIG_KERNEL:
1412 printf(", origin=kernel");
1415 printf(", origin=?");
1421 * "advertising router" list is meaningful only if the prefix
1422 * information is from RA.
1424 if (0 && /* prefix origin is almost obsolted */
1425 PR.origin != PR_ORIG_RA)
1427 else if (PR.advrtrs) {
1429 printf(" advertised by\n");
1430 for (j = 0; j < PR.advrtrs; j++) {
1431 struct sockaddr_in6 sin6;
1432 struct in6_nbrinfo *nbi;
1434 bzero(&sin6, sizeof(sin6));
1435 sin6.sin6_family = AF_INET6;
1436 sin6.sin6_len = sizeof(sin6);
1437 sin6.sin6_addr = PR.advrtr[j];
1438 sin6.sin6_scope_id = PR.if_index; /* XXX */
1439 getnameinfo((struct sockaddr *)&sin6,
1440 sin6.sin6_len, host_buf,
1441 sizeof(host_buf), NULL, 0,
1442 (nflag ? NI_NUMERICHOST : 0));
1443 printf(" %s", host_buf);
1445 nbi = getnbrinfo(&sin6.sin6_addr,
1448 switch (nbi->state) {
1449 case ND6_LLINFO_REACHABLE:
1450 case ND6_LLINFO_STALE:
1451 case ND6_LLINFO_DELAY:
1452 case ND6_LLINFO_PROBE:
1453 printf(" (reachable)\n");
1456 printf(" (unreachable)\n");
1459 printf(" (no neighbor state)\n");
1461 if (PR.advrtrs > DRLSTSIZ)
1462 printf(" and %d routers\n",
1463 PR.advrtrs - DRLSTSIZ);
1465 printf(" No advertising router\n");
1475 char dummyif[IFNAMSIZ+8];
1478 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1480 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1481 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1482 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1488 char dummyif[IFNAMSIZ+8];
1491 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1493 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1494 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1495 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1503 char dummyif[IFNAMSIZ+8];
1506 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1508 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1509 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1510 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1515 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1520 struct in6_ndifreq ndifreq;
1521 unsigned int ifindex;
1523 if (strcasecmp(ifname, "delete") == 0)
1526 if ((ifindex = if_nametoindex(ifname)) == 0)
1527 err(1, "failed to resolve i/f index for %s", ifname);
1530 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1533 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1534 ndifreq.ifindex = ifindex;
1536 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1537 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1545 struct in6_ndifreq ndifreq;
1546 char ifname[IFNAMSIZ+8];
1548 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1551 memset(&ndifreq, 0, sizeof(ndifreq));
1552 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1554 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1555 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1557 if (ndifreq.ifindex == 0)
1558 printf("No default interface.\n");
1560 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1561 err(1, "failed to resolve ifname for index %lu",
1563 printf("ND default interface = %s\n", ifname);
1574 static char result[256];
1575 int days, hours, mins, secs;
1578 char *ep = &result[sizeof(result)];
1581 days = total / 3600 / 24;
1582 hours = (total / 3600) % 24;
1583 mins = (total / 60) % 60;
1588 n = snprintf(p, ep - p, "%dd", days);
1589 if (n < 0 || n >= ep - p)
1593 if (!first || hours) {
1595 n = snprintf(p, ep - p, "%dh", hours);
1596 if (n < 0 || n >= ep - p)
1600 if (!first || mins) {
1602 n = snprintf(p, ep - p, "%dm", mins);
1603 if (n < 0 || n >= ep - p)
1607 snprintf(p, ep - p, "%ds", secs);
1613 * Print the timestamp
1614 * from tcpdump/util.c
1618 const struct timeval *tvp;
1623 s = (tvp->tv_sec + thiszone) % 86400;
1624 (void)printf("%02d:%02d:%02d.%06u ",
1625 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);