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))
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 __P((int, char **));
130 int file __P((char *));
131 void getsocket __P((void));
132 int set __P((int, char **));
133 void get __P((char *));
134 int delete __P((char *));
135 void dump __P((struct in6_addr *, int));
136 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *, int, int));
137 static char *ether_str __P((struct sockaddr_dl *));
138 int ndp_ether_aton __P((char *, u_char *));
139 void usage __P((void));
140 int rtmsg __P((int));
141 void ifinfo __P((char *, int, char **));
142 void rtrlist __P((void));
143 void plist __P((void));
144 void pfx_flush __P((void));
145 void rtrlist __P((void));
146 void rtr_flush __P((void));
147 void harmonize_rtr __P((void));
148 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
149 static void getdefif __P((void));
150 static void setdefif __P((char *));
152 static char *sec2str __P((time_t));
153 static char *ether_str __P((struct sockaddr_dl *));
154 static void ts_print __P((const struct timeval *));
156 #ifdef ICMPV6CTL_ND6_DRLIST
157 static char *rtpref_str[] = {
176 thiszone = gmt2local(0);
177 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
217 repeat = atoi(optarg);
237 dump(0, mode == 'c');
247 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
251 } else if (argc == 1) {
252 if (strcmp(*argv, "delete") == 0 ||
253 if_nametoindex(*argv))
256 errx(1, "invalid interface %s", *argv);
258 getdefif(); /* always call it to print the result */
261 errx(1, "not supported yet");
272 ifinfo(arg, argc, argv);
282 if (argc < 2 || argc > 4)
284 exit(set(argc, argv) ? 1 : 0);
318 * Process a file to set standard ndp entries
326 char line[100], arg[5][50], *args[5];
328 if ((fp = fopen(name, "r")) == NULL) {
329 fprintf(stderr, "ndp: cannot open %s\n", name);
332 args[0] = &arg[0][0];
333 args[1] = &arg[1][0];
334 args[2] = &arg[2][0];
335 args[3] = &arg[3][0];
336 args[4] = &arg[4][0];
338 while (fgets(line, 100, fp) != NULL) {
339 i = sscanf(line, "%49s %49s %49s %49s %49s",
340 arg[0], arg[1], arg[2], arg[3], arg[4]);
342 fprintf(stderr, "ndp: bad line: %s\n", line);
357 s = socket(PF_ROUTE, SOCK_RAW, 0);
365 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
366 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
367 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
368 int expire_time, flags, found_entry;
370 struct rt_msghdr m_rtm;
375 * Set an individual neighbor cache entry
382 register struct sockaddr_in6 *sin = &sin_m;
383 register struct sockaddr_dl *sdl;
384 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
385 struct addrinfo hints, *res;
388 char *host = argv[0], *eaddr = argv[1];
396 bzero(&hints, sizeof(hints));
397 hints.ai_family = AF_INET6;
398 gai_error = getaddrinfo(host, NULL, &hints, &res);
400 fprintf(stderr, "ndp: %s: %s\n", host,
401 gai_strerror(gai_error));
404 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
406 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
407 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
408 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
411 ea = (u_char *)LLADDR(&sdl_m);
412 if (ndp_ether_aton(eaddr, ea) == 0)
414 flags = expire_time = 0;
416 if (strncmp(argv[0], "temp", 4) == 0) {
419 gettimeofday(&time, 0);
420 expire_time = time.tv_sec + 20 * 60;
421 } else if (strncmp(argv[0], "proxy", 5) == 0)
422 flags |= RTF_ANNOUNCE;
425 if (rtmsg(RTM_GET) < 0) {
426 errx(1, "RTM_GET(%s) failed", host);
429 sin = (struct sockaddr_in6 *)(rtm + 1);
430 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
431 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
432 if (sdl->sdl_family == AF_LINK &&
433 (rtm->rtm_flags & RTF_LLINFO) &&
434 !(rtm->rtm_flags & RTF_GATEWAY)) {
435 switch (sdl->sdl_type) {
436 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
437 case IFT_ISO88024: case IFT_ISO88025:
442 * IPv4 arp command retries with sin_other = SIN_PROXY here.
444 fprintf(stderr, "set: cannot configure a new entry\n");
449 if (sdl->sdl_family != AF_LINK) {
450 printf("cannot intuit interface index and type for %s\n", host);
453 sdl_m.sdl_type = sdl->sdl_type;
454 sdl_m.sdl_index = sdl->sdl_index;
455 return (rtmsg(RTM_ADD));
459 * 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 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
481 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
482 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
485 dump(&sin->sin6_addr, 0);
486 if (found_entry == 0) {
487 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
488 sizeof(host_buf), NULL ,0,
489 (nflag ? NI_NUMERICHOST : 0));
490 printf("%s (%s) -- no entry\n", host, host_buf);
496 * Delete a neighbor cache entry
502 struct sockaddr_in6 *sin = &sin_m;
503 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
504 struct sockaddr_dl *sdl;
505 struct addrinfo hints, *res;
511 bzero(&hints, sizeof(hints));
512 hints.ai_family = AF_INET6;
513 gai_error = getaddrinfo(host, NULL, &hints, &res);
515 fprintf(stderr, "ndp: %s: %s\n", host,
516 gai_strerror(gai_error));
519 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
521 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
522 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
523 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
526 if (rtmsg(RTM_GET) < 0) {
527 errx(1, "RTM_GET(%s) failed", host);
530 sin = (struct sockaddr_in6 *)(rtm + 1);
531 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
532 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
533 if (sdl->sdl_family == AF_LINK &&
534 (rtm->rtm_flags & RTF_LLINFO) &&
535 !(rtm->rtm_flags & RTF_GATEWAY)) {
539 * IPv4 arp command retries with sin_other = SIN_PROXY here.
541 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
546 if (sdl->sdl_family != AF_LINK) {
547 printf("cannot locate %s\n", host);
550 if (rtmsg(RTM_DELETE) == 0) {
551 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
554 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
555 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
556 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
559 getnameinfo((struct sockaddr *)&s6,
560 s6.sin6_len, host_buf,
561 sizeof(host_buf), NULL, 0,
562 (nflag ? NI_NUMERICHOST : 0));
563 printf("%s (%s) deleted\n", host, host_buf);
574 * Dump the entire neighbor cache
578 struct in6_addr *addr;
583 char *lim, *buf, *next;
584 struct rt_msghdr *rtm;
585 struct sockaddr_in6 *sin;
586 struct sockaddr_dl *sdl;
588 struct in6_nbrinfo *nbi;
597 if (!tflag && !cflag)
598 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
599 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
600 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
607 mib[4] = NET_RT_FLAGS;
609 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
610 err(1, "sysctl(PF_ROUTE estimate)");
612 if ((buf = malloc(needed)) == NULL)
614 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
615 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
620 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
621 int isrouter = 0, prbs = 0;
623 rtm = (struct rt_msghdr *)next;
624 sin = (struct sockaddr_in6 *)(rtm + 1);
625 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
628 * Some OSes can produce a route that has the LINK flag but
629 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
630 * and BSD/OS, where xx is not the interface identifier on
631 * lo0). Such routes entry would annoy getnbrinfo() below,
633 * XXX: such routes should have the GATEWAY flag, not the
634 * LINK flag. However, there is rotten routing software
635 * that advertises all routes that have the GATEWAY flag.
636 * Thus, KAME kernel intentionally does not set the LINK flag.
637 * What is to be fixed is not ndp, but such routing software
638 * (and the kernel workaround)...
640 if (sdl->sdl_family != AF_LINK)
643 if (!(rtm->rtm_flags & RTF_HOST))
647 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
650 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
652 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
653 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
654 /* XXX: should scope id be filled in the kernel? */
655 if (sin->sin6_scope_id == 0)
656 sin->sin6_scope_id = sdl->sdl_index;
658 /* KAME specific hack; removed the embedded id */
659 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
662 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
663 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
666 if (rtm->rtm_flags & RTF_WASCLONED)
668 #elif defined(RTF_CLONED)
669 if (rtm->rtm_flags & RTF_CLONED)
676 gettimeofday(&time, 0);
680 addrwidth = strlen(host_buf);
681 if (addrwidth < W_ADDR)
683 llwidth = strlen(ether_str(sdl));
684 if (W_ADDR + W_LL - addrwidth > llwidth)
685 llwidth = W_ADDR + W_LL - addrwidth;
686 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
689 ifwidth = strlen(ifname);
690 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
691 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
693 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
694 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
696 /* Print neighbor discovery specific informations */
697 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
699 if (nbi->expire > time.tv_sec) {
701 sec2str(nbi->expire - time.tv_sec));
702 } else if (nbi->expire == 0)
703 printf(" %-9.9s", "permanent");
705 printf(" %-9.9s", "expired");
707 switch (nbi->state) {
708 case ND6_LLINFO_NOSTATE:
711 #ifdef ND6_LLINFO_WAITDELETE
712 case ND6_LLINFO_WAITDELETE:
716 case ND6_LLINFO_INCOMPLETE:
719 case ND6_LLINFO_REACHABLE:
722 case ND6_LLINFO_STALE:
725 case ND6_LLINFO_DELAY:
728 case ND6_LLINFO_PROBE:
736 isrouter = nbi->isrouter;
739 warnx("failed to get neighbor information");
744 * other flags. R: router, P: proxy, W: ??
746 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
747 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
749 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
751 sin = (struct sockaddr_in6 *)
752 (sdl->sdl_len + (char *)sdl);
753 #if 0 /* W and P are mystery even for us */
754 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
756 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
757 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
758 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
760 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
762 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
765 printf(" %s", flgbuf);
783 static struct in6_nbrinfo *
784 getnbrinfo(addr, ifindex, warning)
785 struct in6_addr *addr;
789 static struct in6_nbrinfo nbi;
792 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
795 bzero(&nbi, sizeof(nbi));
796 if_indextoname(ifindex, nbi.ifname);
798 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
800 warn("ioctl(SIOCGNBRINFO_IN6)");
811 struct sockaddr_dl *sdl;
813 static char hbuf[NI_MAXHOST];
817 cp = (u_char *)LLADDR(sdl);
818 snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x",
819 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
821 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
833 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
834 &o[3], &o[4], &o[5]);
836 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
839 for (i = 0; i < 6; i++)
847 printf("usage: ndp [-nt] hostname\n");
848 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
849 printf(" ndp [-nt] -A wait\n");
850 printf(" ndp [-nt] -d hostname\n");
851 printf(" ndp [-nt] -f filename\n");
852 printf(" ndp [-nt] -i interface [flags...]\n");
853 #ifdef SIOCSDEFIFACE_IN6
854 printf(" ndp [-nt] -I [interface|delete]\n");
856 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
866 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
867 register char *cp = m_rtmsg.m_space;
871 if (cmd == RTM_DELETE)
873 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
874 rtm->rtm_flags = flags;
875 rtm->rtm_version = RTM_VERSION;
879 fprintf(stderr, "ndp: internal wrong cmd\n");
882 rtm->rtm_addrs |= RTA_GATEWAY;
884 rtm->rtm_rmx.rmx_expire = expire_time;
885 rtm->rtm_inits = RTV_EXPIRE;
887 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
888 if (rtm->rtm_flags & RTF_ANNOUNCE) {
889 rtm->rtm_flags &= ~RTF_HOST;
890 rtm->rtm_addrs |= RTA_NETMASK;
894 rtm->rtm_addrs |= RTA_DST;
896 #define NEXTADDR(w, s) \
897 if (rtm->rtm_addrs & (w)) { \
898 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
900 NEXTADDR(RTA_DST, sin_m);
901 NEXTADDR(RTA_GATEWAY, sdl_m);
902 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
903 NEXTADDR(RTA_NETMASK, so_mask);
905 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
908 rtm->rtm_seq = ++seq;
910 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
911 if (errno != ESRCH || cmd != RTM_DELETE) {
912 err(1, "writing to routing socket");
917 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
918 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
920 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
926 ifinfo(ifname, argc, argv)
931 struct in6_ndireq nd;
934 #ifdef IPV6CTL_USETEMPADDR
938 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
942 bzero(&nd, sizeof(nd));
943 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
944 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
945 err(1, "ioctl(SIOCGIFINFO_IN6)");
950 for (i = 0; i < argc; i++) {
959 #define SETFLAG(s, f) \
961 if (strcmp(cp, (s)) == 0) {\
968 SETFLAG("nud", ND6_IFF_PERFORMNUD);
969 #ifdef ND6_IFF_ACCEPT_RTADV
970 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
972 #ifdef ND6_IFF_PREFER_SOURCE
973 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE);
977 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
978 err(1, "ioctl(SIOCSIFINFO_FLAGS)");
984 if (!ND.initialized) {
985 errx(1, "%s: not initialized yet", ifname);
989 printf("linkmtu=%d", ND.linkmtu);
990 printf(", maxmtu=%d", ND.maxmtu);
991 printf(", curhlim=%d", ND.chlim);
992 printf(", basereachable=%ds%dms",
993 ND.basereachable / 1000, ND.basereachable % 1000);
994 printf(", reachable=%ds", ND.reachable);
995 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
996 #ifdef IPV6CTL_USETEMPADDR
997 memset(nullbuf, 0, sizeof(nullbuf));
998 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1002 for (i = 0; i < 3; i++) {
1005 printf("\nRandom seed(0): ");
1006 rbuf = ND.randomseed0;
1009 printf("\nRandom seed(1): ");
1010 rbuf = ND.randomseed1;
1013 printf("\nRandom ID: ");
1017 for (j = 0; j < 8; j++)
1018 printf("%02x", rbuf[j]);
1023 printf("\nFlags: ");
1024 if ((ND.flags & ND6_IFF_PERFORMNUD))
1026 #ifdef ND6_IFF_ACCEPT_RTADV
1027 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1028 printf("accept_rtadv ");
1030 #ifdef ND6_IFF_PREFER_SOURCE
1031 if ((ND.flags & ND6_IFF_PREFER_SOURCE))
1032 printf("prefer_source ");
1041 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1042 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1048 #ifdef ICMPV6CTL_ND6_DRLIST
1049 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1051 struct in6_defrouter *p, *ep;
1053 struct timeval time;
1055 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1056 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1064 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1065 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1069 ep = (struct in6_defrouter *)(buf + l);
1070 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1073 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1074 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1075 (nflag ? NI_NUMERICHOST : 0)) != 0)
1076 strlcpy(host_buf, "?", sizeof(host_buf));
1078 printf("%s if=%s", host_buf,
1079 if_indextoname(p->if_index, ifix_buf));
1080 printf(", flags=%s%s",
1081 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1082 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1083 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1084 printf(", pref=%s", rtpref_str[rtpref]);
1086 gettimeofday(&time, 0);
1088 printf(", expire=Never\n");
1090 printf(", expire=%s\n",
1091 sec2str(p->expire - time.tv_sec));
1095 struct in6_drlist dr;
1097 struct timeval time;
1099 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1103 bzero(&dr, sizeof(dr));
1104 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1105 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1106 err(1, "ioctl(SIOCGDRLST_IN6)");
1109 #define DR dr.defrouter[i]
1110 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1111 struct sockaddr_in6 sin6;
1113 bzero(&sin6, sizeof(sin6));
1114 sin6.sin6_family = AF_INET6;
1115 sin6.sin6_len = sizeof(sin6);
1116 sin6.sin6_addr = DR.rtaddr;
1117 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1118 sizeof(host_buf), NULL, 0,
1119 (nflag ? NI_NUMERICHOST : 0));
1121 printf("%s if=%s", host_buf,
1122 if_indextoname(DR.if_index, ifix_buf));
1123 printf(", flags=%s%s",
1124 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1125 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1126 gettimeofday(&time, 0);
1128 printf(", expire=Never\n");
1130 printf(", expire=%s\n",
1131 sec2str(DR.expire - time.tv_sec));
1141 #ifdef ICMPV6CTL_ND6_PRLIST
1142 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1144 struct in6_prefix *p, *ep, *n;
1145 struct sockaddr_in6 *advrtr;
1147 struct timeval time;
1148 const int niflags = NI_NUMERICHOST;
1149 int ninflags = nflag ? NI_NUMERICHOST : 0;
1150 char namebuf[NI_MAXHOST];
1152 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1153 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1161 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1162 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1166 ep = (struct in6_prefix *)(buf + l);
1167 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1168 advrtr = (struct sockaddr_in6 *)(p + 1);
1169 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1171 if (getnameinfo((struct sockaddr *)&p->prefix,
1172 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1173 NULL, 0, niflags) != 0)
1174 strlcpy(namebuf, "?", sizeof(namebuf));
1175 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1176 if_indextoname(p->if_index, ifix_buf));
1178 gettimeofday(&time, 0);
1180 * meaning of fields, especially flags, is very different
1181 * by origin. notify the difference to the users.
1183 printf("flags=%s%s%s%s%s",
1184 p->raflags.onlink ? "L" : "",
1185 p->raflags.autonomous ? "A" : "",
1186 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1187 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1189 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1194 if (p->vltime == ND6_INFINITE_LIFETIME)
1195 printf(" vltime=infinity");
1197 printf(" vltime=%lu", (unsigned long)p->vltime);
1198 if (p->pltime == ND6_INFINITE_LIFETIME)
1199 printf(", pltime=infinity");
1201 printf(", pltime=%lu", (unsigned long)p->pltime);
1203 printf(", expire=Never");
1204 else if (p->expire >= time.tv_sec)
1205 printf(", expire=%s",
1206 sec2str(p->expire - time.tv_sec));
1208 printf(", expired");
1209 printf(", ref=%d", p->refcnt);
1212 * "advertising router" list is meaningful only if the prefix
1213 * information is from RA.
1217 struct sockaddr_in6 *sin6;
1220 printf(" advertised by\n");
1221 for (j = 0; j < p->advrtrs; j++) {
1222 struct in6_nbrinfo *nbi;
1224 if (getnameinfo((struct sockaddr *)sin6,
1225 sin6->sin6_len, namebuf, sizeof(namebuf),
1226 NULL, 0, ninflags) != 0)
1227 strlcpy(namebuf, "?", sizeof(namebuf));
1228 printf(" %s", namebuf);
1230 nbi = getnbrinfo(&sin6->sin6_addr,
1233 switch (nbi->state) {
1234 case ND6_LLINFO_REACHABLE:
1235 case ND6_LLINFO_STALE:
1236 case ND6_LLINFO_DELAY:
1237 case ND6_LLINFO_PROBE:
1238 printf(" (reachable)\n");
1241 printf(" (unreachable)\n");
1244 printf(" (no neighbor state)\n");
1248 printf(" No advertising router\n");
1252 struct in6_prlist pr;
1254 struct timeval time;
1256 gettimeofday(&time, 0);
1258 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1262 bzero(&pr, sizeof(pr));
1263 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1264 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1265 err(1, "ioctl(SIOCGPRLST_IN6)");
1268 #define PR pr.prefix[i]
1269 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1270 struct sockaddr_in6 p6;
1271 char namebuf[NI_MAXHOST];
1277 memset(&p6, 0, sizeof(p6));
1278 p6.sin6_family = AF_INET6;
1279 p6.sin6_len = sizeof(p6);
1280 p6.sin6_addr = PR.prefix;
1284 * copy link index to sin6_scope_id field.
1285 * XXX: KAME specific.
1287 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1290 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1292 linkid = ntohs(linkid);
1293 p6.sin6_scope_id = linkid;
1294 p6.sin6_addr.s6_addr[2] = 0;
1295 p6.sin6_addr.s6_addr[3] = 0;
1298 niflags = NI_NUMERICHOST;
1299 if (getnameinfo((struct sockaddr *)&p6,
1300 sizeof(p6), namebuf, sizeof(namebuf),
1301 NULL, 0, niflags)) {
1302 warnx("getnameinfo failed");
1305 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1306 if_indextoname(PR.if_index, ifix_buf));
1308 gettimeofday(&time, 0);
1310 * meaning of fields, especially flags, is very different
1311 * by origin. notify the difference to the users.
1315 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1318 printf("flags=%s%s%s%s%s",
1319 PR.raflags.onlink ? "L" : "",
1320 PR.raflags.autonomous ? "A" : "",
1321 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1322 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1324 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1330 printf("flags=%s%s",
1331 PR.raflags.onlink ? "L" : "",
1332 PR.raflags.autonomous ? "A" : "");
1334 if (PR.vltime == ND6_INFINITE_LIFETIME)
1335 printf(" vltime=infinity");
1337 printf(" vltime=%lu", PR.vltime);
1338 if (PR.pltime == ND6_INFINITE_LIFETIME)
1339 printf(", pltime=infinity");
1341 printf(", pltime=%lu", PR.pltime);
1343 printf(", expire=Never");
1344 else if (PR.expire >= time.tv_sec)
1345 printf(", expire=%s",
1346 sec2str(PR.expire - time.tv_sec));
1348 printf(", expired");
1350 printf(", ref=%d", PR.refcnt);
1353 switch (PR.origin) {
1355 printf(", origin=RA");
1358 printf(", origin=RR");
1360 case PR_ORIG_STATIC:
1361 printf(", origin=static");
1363 case PR_ORIG_KERNEL:
1364 printf(", origin=kernel");
1367 printf(", origin=?");
1373 * "advertising router" list is meaningful only if the prefix
1374 * information is from RA.
1376 if (0 && /* prefix origin is almost obsolted */
1377 PR.origin != PR_ORIG_RA)
1379 else if (PR.advrtrs) {
1381 printf(" advertised by\n");
1382 for (j = 0; j < PR.advrtrs; j++) {
1383 struct sockaddr_in6 sin6;
1384 struct in6_nbrinfo *nbi;
1386 bzero(&sin6, sizeof(sin6));
1387 sin6.sin6_family = AF_INET6;
1388 sin6.sin6_len = sizeof(sin6);
1389 sin6.sin6_addr = PR.advrtr[j];
1390 sin6.sin6_scope_id = PR.if_index; /* XXX */
1391 getnameinfo((struct sockaddr *)&sin6,
1392 sin6.sin6_len, host_buf,
1393 sizeof(host_buf), NULL, 0,
1394 (nflag ? NI_NUMERICHOST : 0));
1395 printf(" %s", host_buf);
1397 nbi = getnbrinfo(&sin6.sin6_addr,
1400 switch (nbi->state) {
1401 case ND6_LLINFO_REACHABLE:
1402 case ND6_LLINFO_STALE:
1403 case ND6_LLINFO_DELAY:
1404 case ND6_LLINFO_PROBE:
1405 printf(" (reachable)\n");
1408 printf(" (unreachable)\n");
1411 printf(" (no neighbor state)\n");
1413 if (PR.advrtrs > DRLSTSIZ)
1414 printf(" and %d routers\n",
1415 PR.advrtrs - DRLSTSIZ);
1417 printf(" No advertising router\n");
1427 char dummyif[IFNAMSIZ+8];
1430 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1432 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1433 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1434 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1440 char dummyif[IFNAMSIZ+8];
1443 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1445 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1446 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1447 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1455 char dummyif[IFNAMSIZ+8];
1458 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1460 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1461 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1462 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1467 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1472 struct in6_ndifreq ndifreq;
1473 unsigned int ifindex;
1475 if (strcasecmp(ifname, "delete") == 0)
1478 if ((ifindex = if_nametoindex(ifname)) == 0)
1479 err(1, "failed to resolve i/f index for %s", ifname);
1482 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1485 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1486 ndifreq.ifindex = ifindex;
1488 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1489 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1497 struct in6_ndifreq ndifreq;
1498 char ifname[IFNAMSIZ+8];
1500 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1503 memset(&ndifreq, 0, sizeof(ndifreq));
1504 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1506 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1507 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1509 if (ndifreq.ifindex == 0)
1510 printf("No default interface.\n");
1512 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1513 err(1, "failed to resolve ifname for index %lu",
1515 printf("ND default interface = %s\n", ifname);
1526 static char result[256];
1527 int days, hours, mins, secs;
1530 char *ep = &result[sizeof(result)];
1533 days = total / 3600 / 24;
1534 hours = (total / 3600) % 24;
1535 mins = (total / 60) % 60;
1540 n = snprintf(p, ep - p, "%dd", days);
1541 if (n < 0 || n >= ep - p)
1545 if (!first || hours) {
1547 n = snprintf(p, ep - p, "%dh", hours);
1548 if (n < 0 || n >= ep - p)
1552 if (!first || mins) {
1554 n = snprintf(p, ep - p, "%dm", mins);
1555 if (n < 0 || n >= ep - p)
1559 snprintf(p, ep - p, "%ds", secs);
1565 * Print the timestamp
1566 * from tcpdump/util.c
1570 const struct timeval *tvp;
1575 s = (tvp->tv_sec + thiszone) % 86400;
1576 (void)printf("%02d:%02d:%02d.%06u ",
1577 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);