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 #define NEXTADDR(w, s) \
114 if (rtm->rtm_addrs & (w)) { \
115 bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
121 static int32_t thiszone; /* time difference with gmt */
123 static int repeat = 0;
125 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
126 char host_buf[NI_MAXHOST]; /* getnameinfo() */
127 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
129 int main(int, char **);
130 static int file(char *);
131 void getsocket(void);
132 int set(int, char **);
135 void dump(struct sockaddr_in6 *, int);
136 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
137 static char *ether_str(struct sockaddr_dl *);
138 int ndp_ether_aton(char *, u_char *);
141 void ifinfo(char *, int, char **);
144 void pfx_flush(void);
145 void rtr_flush(void);
146 void harmonize_rtr(void);
147 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
148 static void getdefif(void);
149 static void setdefif(char *);
151 static char *sec2str(time_t);
152 static void ts_print(const struct timeval *);
154 #ifdef ICMPV6CTL_ND6_DRLIST
155 static char *rtpref_str[] = {
167 main(int argc, char **argv)
172 thiszone = gmt2local(0);
173 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
192 exit(file(optarg) ? 1 : 0);
214 repeat = atoi(optarg);
234 dump(0, mode == 'c');
244 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
248 } else if (argc == 1) {
249 if (strcmp(*argv, "delete") == 0 ||
250 if_nametoindex(*argv))
253 errx(1, "invalid interface %s", *argv);
255 getdefif(); /* always call it to print the result */
258 errx(1, "not supported yet");
269 ifinfo(arg, argc, argv);
279 if (argc < 2 || argc > 4)
281 exit(set(argc, argv) ? 1 : 0);
315 * Process a file to set standard ndp entries
322 char line[100], arg[5][50], *args[5], *p;
324 if ((fp = fopen(name, "r")) == NULL)
325 err(1, "cannot open %s", name);
326 args[0] = &arg[0][0];
327 args[1] = &arg[1][0];
328 args[2] = &arg[2][0];
329 args[3] = &arg[3][0];
330 args[4] = &arg[4][0];
332 while (fgets(line, sizeof(line), fp) != NULL) {
333 if ((p = strchr(line, '#')) != NULL)
335 for (p = line; isblank(*p); p++);
336 if (*p == '\n' || *p == '\0')
338 i = sscanf(line, "%49s %49s %49s %49s %49s",
339 arg[0], arg[1], arg[2], arg[3], arg[4]);
341 warnx("bad line: %s", line);
356 s = socket(PF_ROUTE, SOCK_RAW, 0);
364 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
365 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
366 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
368 int flags, found_entry;
370 struct rt_msghdr m_rtm;
375 * Set an individual neighbor cache entry
378 set(int argc, char **argv)
380 register struct sockaddr_in6 *sin = &sin_m;
381 register struct sockaddr_dl *sdl;
382 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
383 struct addrinfo hints, *res;
386 char *host = argv[0], *eaddr = argv[1];
394 bzero(&hints, sizeof(hints));
395 hints.ai_family = AF_INET6;
396 gai_error = getaddrinfo(host, NULL, &hints, &res);
398 fprintf(stderr, "ndp: %s: %s\n", host,
399 gai_strerror(gai_error));
402 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
404 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
405 ea = (u_char *)LLADDR(&sdl_m);
406 if (ndp_ether_aton(eaddr, ea) == 0)
408 flags = expire_time = 0;
410 if (strncmp(argv[0], "temp", 4) == 0) {
413 gettimeofday(&now, 0);
414 expire_time = now.tv_sec + 20 * 60;
415 } else if (strncmp(argv[0], "proxy", 5) == 0)
416 flags |= RTF_ANNOUNCE;
419 if (rtmsg(RTM_GET) < 0) {
420 errx(1, "RTM_GET(%s) failed", host);
423 sin = (struct sockaddr_in6 *)(rtm + 1);
424 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
425 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
426 if (sdl->sdl_family == AF_LINK &&
427 !(rtm->rtm_flags & RTF_GATEWAY)) {
428 switch (sdl->sdl_type) {
429 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
430 case IFT_ISO88024: case IFT_ISO88025:
431 case IFT_L2VLAN: case IFT_BRIDGE:
435 fprintf(stderr, "set: cannot configure a new entry\n");
440 if (sdl->sdl_family != AF_LINK) {
441 printf("cannot intuit interface index and type for %s\n", host);
444 sdl_m.sdl_type = sdl->sdl_type;
445 sdl_m.sdl_index = sdl->sdl_index;
446 return (rtmsg(RTM_ADD));
450 * Display an individual neighbor cache entry
455 struct sockaddr_in6 *sin = &sin_m;
456 struct addrinfo hints, *res;
460 bzero(&hints, sizeof(hints));
461 hints.ai_family = AF_INET6;
462 gai_error = getaddrinfo(host, NULL, &hints, &res);
464 fprintf(stderr, "ndp: %s: %s\n", host,
465 gai_strerror(gai_error));
468 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
470 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
472 if (found_entry == 0) {
473 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
474 sizeof(host_buf), NULL ,0,
475 (nflag ? NI_NUMERICHOST : 0));
476 printf("%s (%s) -- no entry\n", host, host_buf);
482 * Delete a neighbor cache entry
487 struct sockaddr_in6 *sin = &sin_m;
488 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
489 register char *cp = m_rtmsg.m_space;
490 struct sockaddr_dl *sdl;
491 struct addrinfo hints, *res;
497 bzero(&hints, sizeof(hints));
498 hints.ai_family = AF_INET6;
499 gai_error = getaddrinfo(host, NULL, &hints, &res);
501 fprintf(stderr, "ndp: %s: %s\n", host,
502 gai_strerror(gai_error));
505 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
507 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
508 if (rtmsg(RTM_GET) < 0) {
509 errx(1, "RTM_GET(%s) failed", host);
512 sin = (struct sockaddr_in6 *)(rtm + 1);
513 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
514 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
515 if (sdl->sdl_family == AF_LINK &&
516 !(rtm->rtm_flags & RTF_GATEWAY)) {
519 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
524 if (sdl->sdl_family != AF_LINK) {
525 printf("cannot locate %s\n", host);
529 * need to reinit the field because it has rt_key
530 * but we want the actual address
532 NEXTADDR(RTA_DST, sin_m);
533 rtm->rtm_flags |= RTF_LLDATA;
534 if (rtmsg(RTM_DELETE) == 0) {
535 getnameinfo((struct sockaddr *)sin,
536 sin->sin6_len, host_buf,
537 sizeof(host_buf), NULL, 0,
538 (nflag ? NI_NUMERICHOST : 0));
539 printf("%s (%s) deleted\n", host, host_buf);
550 * Dump the entire neighbor cache
553 dump(struct sockaddr_in6 *addr, int cflag)
557 char *lim, *buf, *next;
558 struct rt_msghdr *rtm;
559 struct sockaddr_in6 *sin;
560 struct sockaddr_dl *sdl;
562 struct in6_nbrinfo *nbi;
571 if (!tflag && !cflag)
572 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
573 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
574 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
581 mib[4] = NET_RT_FLAGS;
587 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
588 err(1, "sysctl(PF_ROUTE estimate)");
590 if ((buf = malloc(needed)) == NULL)
592 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
593 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
598 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
599 int isrouter = 0, prbs = 0;
601 rtm = (struct rt_msghdr *)next;
602 sin = (struct sockaddr_in6 *)(rtm + 1);
603 sdl = (struct sockaddr_dl *)((char *)sin + ALIGN(sin->sin6_len));
606 * Some OSes can produce a route that has the LINK flag but
607 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
608 * and BSD/OS, where xx is not the interface identifier on
609 * lo0). Such routes entry would annoy getnbrinfo() below,
611 * XXX: such routes should have the GATEWAY flag, not the
612 * LINK flag. However, there is rotten routing software
613 * that advertises all routes that have the GATEWAY flag.
614 * Thus, KAME kernel intentionally does not set the LINK flag.
615 * What is to be fixed is not ndp, but such routing software
616 * (and the kernel workaround)...
618 if (sdl->sdl_family != AF_LINK)
621 if (!(rtm->rtm_flags & RTF_HOST))
625 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
626 &sin->sin6_addr) == 0 ||
627 addr->sin6_scope_id != sin->sin6_scope_id)
630 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
632 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
633 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
634 /* XXX: should scope id be filled in the kernel? */
635 if (sin->sin6_scope_id == 0)
636 sin->sin6_scope_id = sdl->sdl_index;
638 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
639 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
642 if (rtm->rtm_flags & RTF_WASCLONED)
644 #elif defined(RTF_CLONED)
645 if (rtm->rtm_flags & RTF_CLONED)
652 gettimeofday(&now, 0);
656 addrwidth = strlen(host_buf);
657 if (addrwidth < W_ADDR)
659 llwidth = strlen(ether_str(sdl));
660 if (W_ADDR + W_LL - addrwidth > llwidth)
661 llwidth = W_ADDR + W_LL - addrwidth;
662 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
665 ifwidth = strlen(ifname);
666 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
667 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
669 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
670 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
672 /* Print neighbor discovery specific informations */
673 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
675 if (nbi->expire > now.tv_sec) {
677 sec2str(nbi->expire - now.tv_sec));
678 } else if (nbi->expire == 0)
679 printf(" %-9.9s", "permanent");
681 printf(" %-9.9s", "expired");
683 switch (nbi->state) {
684 case ND6_LLINFO_NOSTATE:
687 #ifdef ND6_LLINFO_WAITDELETE
688 case ND6_LLINFO_WAITDELETE:
692 case ND6_LLINFO_INCOMPLETE:
695 case ND6_LLINFO_REACHABLE:
698 case ND6_LLINFO_STALE:
701 case ND6_LLINFO_DELAY:
704 case ND6_LLINFO_PROBE:
712 isrouter = nbi->isrouter;
715 warnx("failed to get neighbor information");
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 sin = (struct sockaddr_in6 *)
728 (sdl->sdl_len + (char *)sdl);
729 #if 0 /* W and P are mystery even for us */
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];
788 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
789 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
791 } else if (sdl->sdl_alen) {
792 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
793 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
795 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
801 ndp_ether_aton(char *a, u_char *n)
805 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
806 &o[3], &o[4], &o[5]);
808 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
811 for (i = 0; i < 6; i++)
819 printf("usage: ndp [-nt] hostname\n");
820 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
821 printf(" ndp [-nt] -A wait\n");
822 printf(" ndp [-nt] -d hostname\n");
823 printf(" ndp [-nt] -f filename\n");
824 printf(" ndp [-nt] -i interface [flags...]\n");
825 #ifdef SIOCSDEFIFACE_IN6
826 printf(" ndp [-nt] -I [interface|delete]\n");
828 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
837 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
838 register char *cp = m_rtmsg.m_space;
842 if (cmd == RTM_DELETE)
844 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
845 rtm->rtm_flags = flags;
846 rtm->rtm_version = RTM_VERSION;
850 fprintf(stderr, "ndp: internal wrong cmd\n");
853 rtm->rtm_addrs |= RTA_GATEWAY;
855 rtm->rtm_rmx.rmx_expire = expire_time;
856 rtm->rtm_inits = RTV_EXPIRE;
858 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
859 #if 0 /* we don't support ipv6addr/128 type proxying */
860 if (rtm->rtm_flags & RTF_ANNOUNCE) {
861 rtm->rtm_flags &= ~RTF_HOST;
862 rtm->rtm_addrs |= RTA_NETMASK;
867 rtm->rtm_addrs |= RTA_DST;
870 NEXTADDR(RTA_DST, sin_m);
871 NEXTADDR(RTA_GATEWAY, sdl_m);
872 #if 0 /* we don't support ipv6addr/128 type proxying */
873 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
874 NEXTADDR(RTA_NETMASK, so_mask);
877 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
880 rtm->rtm_seq = ++seq;
882 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
883 if (errno != ESRCH || cmd != RTM_DELETE) {
884 err(1, "writing to routing socket");
889 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
890 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
892 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
898 ifinfo(char *ifname, int argc, char **argv)
900 struct in6_ndireq nd;
903 #ifdef IPV6CTL_USETEMPADDR
907 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
911 bzero(&nd, sizeof(nd));
912 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
913 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
914 err(1, "ioctl(SIOCGIFINFO_IN6)");
919 for (i = 0; i < argc; i++) {
928 #define SETFLAG(s, f) \
930 if (strcmp(cp, (s)) == 0) {\
938 * XXX: this macro is not 100% correct, in that it matches "nud" against
939 * "nudbogus". But we just let it go since this is minor.
941 #define SETVALUE(f, v) \
944 unsigned long newval; \
945 v = 0; /* unspecified */ \
946 if (strncmp(cp, f, strlen(f)) == 0) { \
947 valptr = strchr(cp, '='); \
948 if (valptr == NULL) \
949 err(1, "syntax error in %s field", (f)); \
951 newval = strtoul(++valptr, NULL, 0); \
953 err(1, "syntax error in %s's value", (f)); \
958 SETFLAG("disabled", ND6_IFF_IFDISABLED);
959 SETFLAG("nud", ND6_IFF_PERFORMNUD);
960 #ifdef ND6_IFF_ACCEPT_RTADV
961 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
963 #ifdef ND6_IFF_AUTO_LINKLOCAL
964 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
966 #ifdef ND6_IFF_NO_PREFER_IFACE
967 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
969 SETVALUE("basereachable", ND.basereachable);
970 SETVALUE("retrans", ND.retrans);
971 SETVALUE("curhlim", ND.chlim);
974 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
975 err(1, "ioctl(SIOCSIFINFO_IN6)");
982 if (!ND.initialized) {
983 errx(1, "%s: not initialized yet", ifname);
987 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
988 err(1, "ioctl(SIOCGIFINFO_IN6)");
991 printf("linkmtu=%d", ND.linkmtu);
992 printf(", maxmtu=%d", ND.maxmtu);
993 printf(", curhlim=%d", ND.chlim);
994 printf(", basereachable=%ds%dms",
995 ND.basereachable / 1000, ND.basereachable % 1000);
996 printf(", reachable=%ds", ND.reachable);
997 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
998 #ifdef IPV6CTL_USETEMPADDR
999 memset(nullbuf, 0, sizeof(nullbuf));
1000 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1004 for (i = 0; i < 3; i++) {
1007 printf("\nRandom seed(0): ");
1008 rbuf = ND.randomseed0;
1011 printf("\nRandom seed(1): ");
1012 rbuf = ND.randomseed1;
1015 printf("\nRandom ID: ");
1019 errx(1, "impossible case for tempaddr display");
1021 for (j = 0; j < 8; j++)
1022 printf("%02x", rbuf[j]);
1027 printf("\nFlags: ");
1028 #ifdef ND6_IFF_IFDISABLED
1029 if ((ND.flags & ND6_IFF_IFDISABLED))
1030 printf("disabled ");
1032 if ((ND.flags & ND6_IFF_PERFORMNUD))
1034 #ifdef ND6_IFF_ACCEPT_RTADV
1035 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1036 printf("accept_rtadv ");
1038 #ifdef ND6_IFF_AUTO_LINKLOCAL
1039 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1040 printf("auto_linklocal ");
1042 #ifdef ND6_IFF_NO_PREFER_IFACE
1043 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1044 printf("no_prefer_iface ");
1053 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1054 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1060 #ifdef ICMPV6CTL_ND6_DRLIST
1061 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1063 struct in6_defrouter *p, *ep;
1067 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1068 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1078 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1079 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1083 ep = (struct in6_defrouter *)(buf + l);
1084 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1087 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1088 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1089 (nflag ? NI_NUMERICHOST : 0)) != 0)
1090 strlcpy(host_buf, "?", sizeof(host_buf));
1092 printf("%s if=%s", host_buf,
1093 if_indextoname(p->if_index, ifix_buf));
1094 printf(", flags=%s%s",
1095 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1096 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1097 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1098 printf(", pref=%s", rtpref_str[rtpref]);
1100 gettimeofday(&now, 0);
1102 printf(", expire=Never\n");
1104 printf(", expire=%s\n",
1105 sec2str(p->expire - now.tv_sec));
1109 struct in6_drlist dr;
1113 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1117 bzero(&dr, sizeof(dr));
1118 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1119 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1120 err(1, "ioctl(SIOCGDRLST_IN6)");
1123 #define DR dr.defrouter[i]
1124 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1125 struct sockaddr_in6 sin6;
1127 bzero(&sin6, sizeof(sin6));
1128 sin6.sin6_family = AF_INET6;
1129 sin6.sin6_len = sizeof(sin6);
1130 sin6.sin6_addr = DR.rtaddr;
1131 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1132 sizeof(host_buf), NULL, 0,
1133 (nflag ? NI_NUMERICHOST : 0));
1135 printf("%s if=%s", host_buf,
1136 if_indextoname(DR.if_index, ifix_buf));
1137 printf(", flags=%s%s",
1138 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1139 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1140 gettimeofday(&now, 0);
1142 printf(", expire=Never\n");
1144 printf(", expire=%s\n",
1145 sec2str(DR.expire - now.tv_sec));
1155 #ifdef ICMPV6CTL_ND6_PRLIST
1156 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1158 struct in6_prefix *p, *ep, *n;
1159 struct sockaddr_in6 *advrtr;
1162 const int niflags = NI_NUMERICHOST;
1163 int ninflags = nflag ? NI_NUMERICHOST : 0;
1164 char namebuf[NI_MAXHOST];
1166 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1167 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1175 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1176 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1180 ep = (struct in6_prefix *)(buf + l);
1181 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1182 advrtr = (struct sockaddr_in6 *)(p + 1);
1183 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1185 if (getnameinfo((struct sockaddr *)&p->prefix,
1186 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1187 NULL, 0, niflags) != 0)
1188 strlcpy(namebuf, "?", sizeof(namebuf));
1189 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1190 if_indextoname(p->if_index, ifix_buf));
1192 gettimeofday(&now, 0);
1194 * meaning of fields, especially flags, is very different
1195 * by origin. notify the difference to the users.
1197 printf("flags=%s%s%s%s%s",
1198 p->raflags.onlink ? "L" : "",
1199 p->raflags.autonomous ? "A" : "",
1200 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1201 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1203 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1208 if (p->vltime == ND6_INFINITE_LIFETIME)
1209 printf(" vltime=infinity");
1211 printf(" vltime=%lu", (unsigned long)p->vltime);
1212 if (p->pltime == ND6_INFINITE_LIFETIME)
1213 printf(", pltime=infinity");
1215 printf(", pltime=%lu", (unsigned long)p->pltime);
1217 printf(", expire=Never");
1218 else if (p->expire >= now.tv_sec)
1219 printf(", expire=%s",
1220 sec2str(p->expire - now.tv_sec));
1222 printf(", expired");
1223 printf(", ref=%d", p->refcnt);
1226 * "advertising router" list is meaningful only if the prefix
1227 * information is from RA.
1231 struct sockaddr_in6 *sin6;
1234 printf(" advertised by\n");
1235 for (j = 0; j < p->advrtrs; j++) {
1236 struct in6_nbrinfo *nbi;
1238 if (getnameinfo((struct sockaddr *)sin6,
1239 sin6->sin6_len, namebuf, sizeof(namebuf),
1240 NULL, 0, ninflags) != 0)
1241 strlcpy(namebuf, "?", sizeof(namebuf));
1242 printf(" %s", namebuf);
1244 nbi = getnbrinfo(&sin6->sin6_addr,
1247 switch (nbi->state) {
1248 case ND6_LLINFO_REACHABLE:
1249 case ND6_LLINFO_STALE:
1250 case ND6_LLINFO_DELAY:
1251 case ND6_LLINFO_PROBE:
1252 printf(" (reachable)\n");
1255 printf(" (unreachable)\n");
1258 printf(" (no neighbor state)\n");
1262 printf(" No advertising router\n");
1266 struct in6_prlist pr;
1270 gettimeofday(&now, 0);
1272 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1276 bzero(&pr, sizeof(pr));
1277 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1278 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1279 err(1, "ioctl(SIOCGPRLST_IN6)");
1282 #define PR pr.prefix[i]
1283 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1284 struct sockaddr_in6 p6;
1285 char namebuf[NI_MAXHOST];
1291 memset(&p6, 0, sizeof(p6));
1292 p6.sin6_family = AF_INET6;
1293 p6.sin6_len = sizeof(p6);
1294 p6.sin6_addr = PR.prefix;
1296 niflags = NI_NUMERICHOST;
1297 if (getnameinfo((struct sockaddr *)&p6,
1298 sizeof(p6), namebuf, sizeof(namebuf),
1299 NULL, 0, niflags)) {
1300 warnx("getnameinfo failed");
1303 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1304 if_indextoname(PR.if_index, ifix_buf));
1306 gettimeofday(&now, 0);
1308 * meaning of fields, especially flags, is very different
1309 * by origin. notify the difference to the users.
1313 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1316 printf("flags=%s%s%s%s%s",
1317 PR.raflags.onlink ? "L" : "",
1318 PR.raflags.autonomous ? "A" : "",
1319 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1320 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1322 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1328 printf("flags=%s%s",
1329 PR.raflags.onlink ? "L" : "",
1330 PR.raflags.autonomous ? "A" : "");
1332 if (PR.vltime == ND6_INFINITE_LIFETIME)
1333 printf(" vltime=infinity");
1335 printf(" vltime=%lu", PR.vltime);
1336 if (PR.pltime == ND6_INFINITE_LIFETIME)
1337 printf(", pltime=infinity");
1339 printf(", pltime=%lu", PR.pltime);
1341 printf(", expire=Never");
1342 else if (PR.expire >= now.tv_sec)
1343 printf(", expire=%s",
1344 sec2str(PR.expire - now.tv_sec));
1346 printf(", expired");
1348 printf(", ref=%d", PR.refcnt);
1351 switch (PR.origin) {
1353 printf(", origin=RA");
1356 printf(", origin=RR");
1358 case PR_ORIG_STATIC:
1359 printf(", origin=static");
1361 case PR_ORIG_KERNEL:
1362 printf(", origin=kernel");
1365 printf(", origin=?");
1371 * "advertising router" list is meaningful only if the prefix
1372 * information is from RA.
1374 if (0 && /* prefix origin is almost obsolted */
1375 PR.origin != PR_ORIG_RA)
1377 else if (PR.advrtrs) {
1379 printf(" advertised by\n");
1380 for (j = 0; j < PR.advrtrs; j++) {
1381 struct sockaddr_in6 sin6;
1382 struct in6_nbrinfo *nbi;
1384 bzero(&sin6, sizeof(sin6));
1385 sin6.sin6_family = AF_INET6;
1386 sin6.sin6_len = sizeof(sin6);
1387 sin6.sin6_addr = PR.advrtr[j];
1388 sin6.sin6_scope_id = PR.if_index; /* XXX */
1389 getnameinfo((struct sockaddr *)&sin6,
1390 sin6.sin6_len, host_buf,
1391 sizeof(host_buf), NULL, 0,
1392 (nflag ? NI_NUMERICHOST : 0));
1393 printf(" %s", host_buf);
1395 nbi = getnbrinfo(&sin6.sin6_addr,
1398 switch (nbi->state) {
1399 case ND6_LLINFO_REACHABLE:
1400 case ND6_LLINFO_STALE:
1401 case ND6_LLINFO_DELAY:
1402 case ND6_LLINFO_PROBE:
1403 printf(" (reachable)\n");
1406 printf(" (unreachable)\n");
1409 printf(" (no neighbor state)\n");
1411 if (PR.advrtrs > DRLSTSIZ)
1412 printf(" and %d routers\n",
1413 PR.advrtrs - DRLSTSIZ);
1415 printf(" No advertising router\n");
1425 char dummyif[IFNAMSIZ+8];
1428 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1430 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1431 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1432 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1438 char dummyif[IFNAMSIZ+8];
1441 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1443 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1444 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1445 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1453 char dummyif[IFNAMSIZ+8];
1456 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1458 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1459 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1460 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1465 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1467 setdefif(char *ifname)
1469 struct in6_ndifreq ndifreq;
1470 unsigned int ifindex;
1472 if (strcasecmp(ifname, "delete") == 0)
1475 if ((ifindex = if_nametoindex(ifname)) == 0)
1476 err(1, "failed to resolve i/f index for %s", ifname);
1479 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1482 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1483 ndifreq.ifindex = ifindex;
1485 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1486 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1494 struct in6_ndifreq ndifreq;
1495 char ifname[IFNAMSIZ+8];
1497 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1500 memset(&ndifreq, 0, sizeof(ndifreq));
1501 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1503 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1504 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1506 if (ndifreq.ifindex == 0)
1507 printf("No default interface.\n");
1509 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1510 err(1, "failed to resolve ifname for index %lu",
1512 printf("ND default interface = %s\n", ifname);
1520 sec2str(time_t total)
1522 static char result[256];
1523 int days, hours, mins, secs;
1526 char *ep = &result[sizeof(result)];
1529 days = total / 3600 / 24;
1530 hours = (total / 3600) % 24;
1531 mins = (total / 60) % 60;
1536 n = snprintf(p, ep - p, "%dd", days);
1537 if (n < 0 || n >= ep - p)
1541 if (!first || hours) {
1543 n = snprintf(p, ep - p, "%dh", hours);
1544 if (n < 0 || n >= ep - p)
1548 if (!first || mins) {
1550 n = snprintf(p, ep - p, "%dm", mins);
1551 if (n < 0 || n >= ep - p)
1555 snprintf(p, ep - p, "%ds", secs);
1561 * Print the timestamp
1562 * from tcpdump/util.c
1565 ts_print(const struct timeval *tvp)
1570 s = (tvp->tv_sec + thiszone) % 86400;
1571 (void)printf("%02d:%02d:%02d.%06u ",
1572 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);