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 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
131 char host_buf[NI_MAXHOST]; /* getnameinfo() */
132 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
134 int main(int, char **);
135 static int file(char *);
136 void getsocket(void);
137 int set(int, char **);
140 void dump(struct in6_addr *, int);
141 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
142 static char *ether_str(struct sockaddr_dl *);
143 int ndp_ether_aton(char *, u_char *);
146 void ifinfo(char *, int, char **);
149 void pfx_flush(void);
150 void rtr_flush(void);
151 void harmonize_rtr(void);
152 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
153 static void getdefif(void);
154 static void setdefif(char *);
156 static char *sec2str(time_t);
157 static void ts_print(const struct timeval *);
159 #ifdef ICMPV6CTL_ND6_DRLIST
160 static char *rtpref_str[] = {
179 thiszone = gmt2local(0);
180 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
199 exit(file(optarg) ? 1 : 0);
221 repeat = atoi(optarg);
241 dump(0, mode == 'c');
251 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
255 } else if (argc == 1) {
256 if (strcmp(*argv, "delete") == 0 ||
257 if_nametoindex(*argv))
260 errx(1, "invalid interface %s", *argv);
262 getdefif(); /* always call it to print the result */
265 errx(1, "not supported yet");
276 ifinfo(arg, argc, argv);
286 if (argc < 2 || argc > 4)
288 exit(set(argc, argv) ? 1 : 0);
322 * Process a file to set standard ndp entries
330 char line[100], arg[5][50], *args[5], *p;
332 if ((fp = fopen(name, "r")) == NULL)
333 err(1, "cannot open %s", 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 if ((p = strchr(line, '#')) != NULL)
343 for (p = line; isblank(*p); p++);
344 if (*p == '\n' || *p == '\0')
346 i = sscanf(line, "%49s %49s %49s %49s %49s",
347 arg[0], arg[1], arg[2], arg[3], arg[4]);
349 warnx("bad line: %s", line);
364 s = socket(PF_ROUTE, SOCK_RAW, 0);
372 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
373 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
374 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
375 int expire_time, flags, found_entry;
377 struct rt_msghdr m_rtm;
382 * Set an individual neighbor cache entry
389 register struct sockaddr_in6 *sin = &sin_m;
390 register struct sockaddr_dl *sdl;
391 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
392 struct addrinfo hints, *res;
395 char *host = argv[0], *eaddr = argv[1];
403 bzero(&hints, sizeof(hints));
404 hints.ai_family = AF_INET6;
405 gai_error = getaddrinfo(host, NULL, &hints, &res);
407 fprintf(stderr, "ndp: %s: %s\n", host,
408 gai_strerror(gai_error));
411 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
413 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
414 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
415 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
418 ea = (u_char *)LLADDR(&sdl_m);
419 if (ndp_ether_aton(eaddr, ea) == 0)
421 flags = expire_time = 0;
423 if (strncmp(argv[0], "temp", 4) == 0) {
426 gettimeofday(&time, 0);
427 expire_time = time.tv_sec + 20 * 60;
428 } else if (strncmp(argv[0], "proxy", 5) == 0)
429 flags |= RTF_ANNOUNCE;
432 if (rtmsg(RTM_GET) < 0) {
433 errx(1, "RTM_GET(%s) failed", host);
436 sin = (struct sockaddr_in6 *)(rtm + 1);
437 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
438 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
439 if (sdl->sdl_family == AF_LINK &&
440 !(rtm->rtm_flags & RTF_GATEWAY)) {
441 switch (sdl->sdl_type) {
442 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
443 case IFT_ISO88024: case IFT_ISO88025:
444 case IFT_L2VLAN: case IFT_BRIDGE:
449 * IPv4 arp command retries with sin_other = SIN_PROXY here.
451 fprintf(stderr, "set: cannot configure a new entry\n");
456 if (sdl->sdl_family != AF_LINK) {
457 printf("cannot intuit interface index and type for %s\n", host);
460 sdl_m.sdl_type = sdl->sdl_type;
461 sdl_m.sdl_index = sdl->sdl_index;
462 return (rtmsg(RTM_ADD));
466 * Display an individual neighbor cache entry
472 struct sockaddr_in6 *sin = &sin_m;
473 struct addrinfo hints, *res;
477 bzero(&hints, sizeof(hints));
478 hints.ai_family = AF_INET6;
479 gai_error = getaddrinfo(host, NULL, &hints, &res);
481 fprintf(stderr, "ndp: %s: %s\n", host,
482 gai_strerror(gai_error));
485 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
487 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
488 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
489 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
492 dump(&sin->sin6_addr, 0);
493 if (found_entry == 0) {
494 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
495 sizeof(host_buf), NULL ,0,
496 (nflag ? NI_NUMERICHOST : 0));
497 printf("%s (%s) -- no entry\n", host, host_buf);
503 * Delete a neighbor cache entry
509 struct sockaddr_in6 *sin = &sin_m;
510 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
511 register char *cp = m_rtmsg.m_space;
512 struct sockaddr_dl *sdl;
513 struct addrinfo hints, *res;
519 bzero(&hints, sizeof(hints));
520 hints.ai_family = AF_INET6;
521 gai_error = getaddrinfo(host, NULL, &hints, &res);
523 fprintf(stderr, "ndp: %s: %s\n", host,
524 gai_strerror(gai_error));
527 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
529 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
530 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
531 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
534 if (rtmsg(RTM_GET) < 0) {
535 errx(1, "RTM_GET(%s) failed", host);
538 sin = (struct sockaddr_in6 *)(rtm + 1);
539 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
540 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
541 if (sdl->sdl_family == AF_LINK &&
542 !(rtm->rtm_flags & RTF_GATEWAY)) {
546 * IPv4 arp command retries with sin_other = SIN_PROXY here.
548 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
553 if (sdl->sdl_family != AF_LINK) {
554 printf("cannot locate %s\n", host);
558 * need to reinit the field because it has rt_key
559 * but we want the actual address
561 NEXTADDR(RTA_DST, sin_m);
562 rtm->rtm_flags |= RTF_LLDATA;
563 if (rtmsg(RTM_DELETE) == 0) {
564 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
567 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
568 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
569 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
572 getnameinfo((struct sockaddr *)&s6,
573 s6.sin6_len, host_buf,
574 sizeof(host_buf), NULL, 0,
575 (nflag ? NI_NUMERICHOST : 0));
576 printf("%s (%s) deleted\n", host, host_buf);
587 * Dump the entire neighbor cache
591 struct in6_addr *addr;
596 char *lim, *buf, *next;
597 struct rt_msghdr *rtm;
598 struct sockaddr_in6 *sin;
599 struct sockaddr_dl *sdl;
601 struct in6_nbrinfo *nbi;
610 if (!tflag && !cflag)
611 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
612 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
613 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
620 mib[4] = NET_RT_FLAGS;
626 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
627 err(1, "sysctl(PF_ROUTE estimate)");
629 if ((buf = malloc(needed)) == NULL)
631 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
632 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
637 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
638 int isrouter = 0, prbs = 0;
640 rtm = (struct rt_msghdr *)next;
641 sin = (struct sockaddr_in6 *)(rtm + 1);
642 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
645 * Some OSes can produce a route that has the LINK flag but
646 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
647 * and BSD/OS, where xx is not the interface identifier on
648 * lo0). Such routes entry would annoy getnbrinfo() below,
650 * XXX: such routes should have the GATEWAY flag, not the
651 * LINK flag. However, there is rotten routing software
652 * that advertises all routes that have the GATEWAY flag.
653 * Thus, KAME kernel intentionally does not set the LINK flag.
654 * What is to be fixed is not ndp, but such routing software
655 * (and the kernel workaround)...
657 if (sdl->sdl_family != AF_LINK)
660 if (!(rtm->rtm_flags & RTF_HOST))
664 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
667 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
669 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
670 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
671 /* XXX: should scope id be filled in the kernel? */
672 if (sin->sin6_scope_id == 0)
673 sin->sin6_scope_id = sdl->sdl_index;
675 /* KAME specific hack; removed the embedded id */
676 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
679 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
680 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
683 if (rtm->rtm_flags & RTF_WASCLONED)
685 #elif defined(RTF_CLONED)
686 if (rtm->rtm_flags & RTF_CLONED)
693 gettimeofday(&time, 0);
697 addrwidth = strlen(host_buf);
698 if (addrwidth < W_ADDR)
700 llwidth = strlen(ether_str(sdl));
701 if (W_ADDR + W_LL - addrwidth > llwidth)
702 llwidth = W_ADDR + W_LL - addrwidth;
703 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
706 ifwidth = strlen(ifname);
707 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
708 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
710 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
711 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
713 /* Print neighbor discovery specific informations */
714 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
716 if (nbi->expire > time.tv_sec) {
718 sec2str(nbi->expire - time.tv_sec));
719 } else if (nbi->expire == 0)
720 printf(" %-9.9s", "permanent");
722 printf(" %-9.9s", "expired");
724 switch (nbi->state) {
725 case ND6_LLINFO_NOSTATE:
728 #ifdef ND6_LLINFO_WAITDELETE
729 case ND6_LLINFO_WAITDELETE:
733 case ND6_LLINFO_INCOMPLETE:
736 case ND6_LLINFO_REACHABLE:
739 case ND6_LLINFO_STALE:
742 case ND6_LLINFO_DELAY:
745 case ND6_LLINFO_PROBE:
753 isrouter = nbi->isrouter;
756 warnx("failed to get neighbor information");
761 * other flags. R: router, P: proxy, W: ??
763 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
764 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
766 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
768 sin = (struct sockaddr_in6 *)
769 (sdl->sdl_len + (char *)sdl);
770 #if 0 /* W and P are mystery even for us */
771 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
773 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
774 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
775 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
777 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
779 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
782 printf(" %s", flgbuf);
800 static struct in6_nbrinfo *
801 getnbrinfo(addr, ifindex, warning)
802 struct in6_addr *addr;
806 static struct in6_nbrinfo nbi;
809 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
812 bzero(&nbi, sizeof(nbi));
813 if_indextoname(ifindex, nbi.ifname);
815 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
817 warn("ioctl(SIOCGNBRINFO_IN6)");
827 ether_str(struct sockaddr_dl *sdl)
829 static char hbuf[NI_MAXHOST];
832 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
833 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
835 } else if (sdl->sdl_alen) {
836 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
837 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
839 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
851 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
852 &o[3], &o[4], &o[5]);
854 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
857 for (i = 0; i < 6; i++)
865 printf("usage: ndp [-nt] hostname\n");
866 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
867 printf(" ndp [-nt] -A wait\n");
868 printf(" ndp [-nt] -d hostname\n");
869 printf(" ndp [-nt] -f filename\n");
870 printf(" ndp [-nt] -i interface [flags...]\n");
871 #ifdef SIOCSDEFIFACE_IN6
872 printf(" ndp [-nt] -I [interface|delete]\n");
874 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
884 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
885 register char *cp = m_rtmsg.m_space;
889 if (cmd == RTM_DELETE)
891 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
892 rtm->rtm_flags = flags;
893 rtm->rtm_version = RTM_VERSION;
897 fprintf(stderr, "ndp: internal wrong cmd\n");
900 rtm->rtm_addrs |= RTA_GATEWAY;
902 rtm->rtm_rmx.rmx_expire = expire_time;
903 rtm->rtm_inits = RTV_EXPIRE;
905 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
906 #if 0 /* we don't support ipv6addr/128 type proxying */
907 if (rtm->rtm_flags & RTF_ANNOUNCE) {
908 rtm->rtm_flags &= ~RTF_HOST;
909 rtm->rtm_addrs |= RTA_NETMASK;
914 rtm->rtm_addrs |= RTA_DST;
917 NEXTADDR(RTA_DST, sin_m);
918 NEXTADDR(RTA_GATEWAY, sdl_m);
919 #if 0 /* we don't support ipv6addr/128 type proxying */
920 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
921 NEXTADDR(RTA_NETMASK, so_mask);
924 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
927 rtm->rtm_seq = ++seq;
929 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
930 if (errno != ESRCH || cmd != RTM_DELETE) {
931 err(1, "writing to routing socket");
936 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
937 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
939 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
945 ifinfo(ifname, argc, argv)
950 struct in6_ndireq nd;
953 #ifdef IPV6CTL_USETEMPADDR
957 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
961 bzero(&nd, sizeof(nd));
962 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
963 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
964 err(1, "ioctl(SIOCGIFINFO_IN6)");
969 for (i = 0; i < argc; i++) {
978 #define SETFLAG(s, f) \
980 if (strcmp(cp, (s)) == 0) {\
988 * XXX: this macro is not 100% correct, in that it matches "nud" against
989 * "nudbogus". But we just let it go since this is minor.
991 #define SETVALUE(f, v) \
994 unsigned long newval; \
995 v = 0; /* unspecified */ \
996 if (strncmp(cp, f, strlen(f)) == 0) { \
997 valptr = strchr(cp, '='); \
998 if (valptr == NULL) \
999 err(1, "syntax error in %s field", (f)); \
1001 newval = strtoul(++valptr, NULL, 0); \
1003 err(1, "syntax error in %s's value", (f)); \
1008 SETFLAG("disabled", ND6_IFF_IFDISABLED);
1009 SETFLAG("nud", ND6_IFF_PERFORMNUD);
1010 #ifdef ND6_IFF_ACCEPT_RTADV
1011 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
1013 #ifdef ND6_IFF_AUTO_LINKLOCAL
1014 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
1016 #ifdef ND6_IFF_NO_PREFER_IFACE
1017 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
1019 SETVALUE("basereachable", ND.basereachable);
1020 SETVALUE("retrans", ND.retrans);
1021 SETVALUE("curhlim", ND.chlim);
1023 ND.flags = newflags;
1024 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
1025 err(1, "ioctl(SIOCSIFINFO_IN6)");
1032 if (!ND.initialized) {
1033 errx(1, "%s: not initialized yet", ifname);
1037 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
1038 err(1, "ioctl(SIOCGIFINFO_IN6)");
1041 printf("linkmtu=%d", ND.linkmtu);
1042 printf(", maxmtu=%d", ND.maxmtu);
1043 printf(", curhlim=%d", ND.chlim);
1044 printf(", basereachable=%ds%dms",
1045 ND.basereachable / 1000, ND.basereachable % 1000);
1046 printf(", reachable=%ds", ND.reachable);
1047 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1048 #ifdef IPV6CTL_USETEMPADDR
1049 memset(nullbuf, 0, sizeof(nullbuf));
1050 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1054 for (i = 0; i < 3; i++) {
1057 printf("\nRandom seed(0): ");
1058 rbuf = ND.randomseed0;
1061 printf("\nRandom seed(1): ");
1062 rbuf = ND.randomseed1;
1065 printf("\nRandom ID: ");
1069 errx(1, "impossible case for tempaddr display");
1071 for (j = 0; j < 8; j++)
1072 printf("%02x", rbuf[j]);
1077 printf("\nFlags: ");
1078 #ifdef ND6_IFF_IFDISABLED
1079 if ((ND.flags & ND6_IFF_IFDISABLED))
1080 printf("disabled ");
1082 if ((ND.flags & ND6_IFF_PERFORMNUD))
1084 #ifdef ND6_IFF_ACCEPT_RTADV
1085 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1086 printf("accept_rtadv ");
1088 #ifdef ND6_IFF_AUTO_LINKLOCAL
1089 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1090 printf("auto_linklocal ");
1092 #ifdef ND6_IFF_NO_PREFER_IFACE
1093 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1094 printf("no_prefer_iface ");
1103 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1104 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1110 #ifdef ICMPV6CTL_ND6_DRLIST
1111 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1113 struct in6_defrouter *p, *ep;
1115 struct timeval time;
1117 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1118 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1128 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1129 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1133 ep = (struct in6_defrouter *)(buf + l);
1134 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1137 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1138 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1139 (nflag ? NI_NUMERICHOST : 0)) != 0)
1140 strlcpy(host_buf, "?", sizeof(host_buf));
1142 printf("%s if=%s", host_buf,
1143 if_indextoname(p->if_index, ifix_buf));
1144 printf(", flags=%s%s",
1145 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1146 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1147 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1148 printf(", pref=%s", rtpref_str[rtpref]);
1150 gettimeofday(&time, 0);
1152 printf(", expire=Never\n");
1154 printf(", expire=%s\n",
1155 sec2str(p->expire - time.tv_sec));
1159 struct in6_drlist dr;
1161 struct timeval time;
1163 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1167 bzero(&dr, sizeof(dr));
1168 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */
1169 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1170 err(1, "ioctl(SIOCGDRLST_IN6)");
1173 #define DR dr.defrouter[i]
1174 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1175 struct sockaddr_in6 sin6;
1177 bzero(&sin6, sizeof(sin6));
1178 sin6.sin6_family = AF_INET6;
1179 sin6.sin6_len = sizeof(sin6);
1180 sin6.sin6_addr = DR.rtaddr;
1181 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1182 sizeof(host_buf), NULL, 0,
1183 (nflag ? NI_NUMERICHOST : 0));
1185 printf("%s if=%s", host_buf,
1186 if_indextoname(DR.if_index, ifix_buf));
1187 printf(", flags=%s%s",
1188 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1189 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1190 gettimeofday(&time, 0);
1192 printf(", expire=Never\n");
1194 printf(", expire=%s\n",
1195 sec2str(DR.expire - time.tv_sec));
1205 #ifdef ICMPV6CTL_ND6_PRLIST
1206 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1208 struct in6_prefix *p, *ep, *n;
1209 struct sockaddr_in6 *advrtr;
1211 struct timeval time;
1212 const int niflags = NI_NUMERICHOST;
1213 int ninflags = nflag ? NI_NUMERICHOST : 0;
1214 char namebuf[NI_MAXHOST];
1216 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1217 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1225 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1226 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1230 ep = (struct in6_prefix *)(buf + l);
1231 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1232 advrtr = (struct sockaddr_in6 *)(p + 1);
1233 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1235 if (getnameinfo((struct sockaddr *)&p->prefix,
1236 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1237 NULL, 0, niflags) != 0)
1238 strlcpy(namebuf, "?", sizeof(namebuf));
1239 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1240 if_indextoname(p->if_index, ifix_buf));
1242 gettimeofday(&time, 0);
1244 * meaning of fields, especially flags, is very different
1245 * by origin. notify the difference to the users.
1247 printf("flags=%s%s%s%s%s",
1248 p->raflags.onlink ? "L" : "",
1249 p->raflags.autonomous ? "A" : "",
1250 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1251 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1253 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1258 if (p->vltime == ND6_INFINITE_LIFETIME)
1259 printf(" vltime=infinity");
1261 printf(" vltime=%lu", (unsigned long)p->vltime);
1262 if (p->pltime == ND6_INFINITE_LIFETIME)
1263 printf(", pltime=infinity");
1265 printf(", pltime=%lu", (unsigned long)p->pltime);
1267 printf(", expire=Never");
1268 else if (p->expire >= time.tv_sec)
1269 printf(", expire=%s",
1270 sec2str(p->expire - time.tv_sec));
1272 printf(", expired");
1273 printf(", ref=%d", p->refcnt);
1276 * "advertising router" list is meaningful only if the prefix
1277 * information is from RA.
1281 struct sockaddr_in6 *sin6;
1284 printf(" advertised by\n");
1285 for (j = 0; j < p->advrtrs; j++) {
1286 struct in6_nbrinfo *nbi;
1288 if (getnameinfo((struct sockaddr *)sin6,
1289 sin6->sin6_len, namebuf, sizeof(namebuf),
1290 NULL, 0, ninflags) != 0)
1291 strlcpy(namebuf, "?", sizeof(namebuf));
1292 printf(" %s", namebuf);
1294 nbi = getnbrinfo(&sin6->sin6_addr,
1297 switch (nbi->state) {
1298 case ND6_LLINFO_REACHABLE:
1299 case ND6_LLINFO_STALE:
1300 case ND6_LLINFO_DELAY:
1301 case ND6_LLINFO_PROBE:
1302 printf(" (reachable)\n");
1305 printf(" (unreachable)\n");
1308 printf(" (no neighbor state)\n");
1312 printf(" No advertising router\n");
1316 struct in6_prlist pr;
1318 struct timeval time;
1320 gettimeofday(&time, 0);
1322 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1326 bzero(&pr, sizeof(pr));
1327 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */
1328 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1329 err(1, "ioctl(SIOCGPRLST_IN6)");
1332 #define PR pr.prefix[i]
1333 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1334 struct sockaddr_in6 p6;
1335 char namebuf[NI_MAXHOST];
1341 memset(&p6, 0, sizeof(p6));
1342 p6.sin6_family = AF_INET6;
1343 p6.sin6_len = sizeof(p6);
1344 p6.sin6_addr = PR.prefix;
1348 * copy link index to sin6_scope_id field.
1349 * XXX: KAME specific.
1351 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1354 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1356 linkid = ntohs(linkid);
1357 p6.sin6_scope_id = linkid;
1358 p6.sin6_addr.s6_addr[2] = 0;
1359 p6.sin6_addr.s6_addr[3] = 0;
1362 niflags = NI_NUMERICHOST;
1363 if (getnameinfo((struct sockaddr *)&p6,
1364 sizeof(p6), namebuf, sizeof(namebuf),
1365 NULL, 0, niflags)) {
1366 warnx("getnameinfo failed");
1369 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1370 if_indextoname(PR.if_index, ifix_buf));
1372 gettimeofday(&time, 0);
1374 * meaning of fields, especially flags, is very different
1375 * by origin. notify the difference to the users.
1379 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1382 printf("flags=%s%s%s%s%s",
1383 PR.raflags.onlink ? "L" : "",
1384 PR.raflags.autonomous ? "A" : "",
1385 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1386 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1388 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1394 printf("flags=%s%s",
1395 PR.raflags.onlink ? "L" : "",
1396 PR.raflags.autonomous ? "A" : "");
1398 if (PR.vltime == ND6_INFINITE_LIFETIME)
1399 printf(" vltime=infinity");
1401 printf(" vltime=%lu", PR.vltime);
1402 if (PR.pltime == ND6_INFINITE_LIFETIME)
1403 printf(", pltime=infinity");
1405 printf(", pltime=%lu", PR.pltime);
1407 printf(", expire=Never");
1408 else if (PR.expire >= time.tv_sec)
1409 printf(", expire=%s",
1410 sec2str(PR.expire - time.tv_sec));
1412 printf(", expired");
1414 printf(", ref=%d", PR.refcnt);
1417 switch (PR.origin) {
1419 printf(", origin=RA");
1422 printf(", origin=RR");
1424 case PR_ORIG_STATIC:
1425 printf(", origin=static");
1427 case PR_ORIG_KERNEL:
1428 printf(", origin=kernel");
1431 printf(", origin=?");
1437 * "advertising router" list is meaningful only if the prefix
1438 * information is from RA.
1440 if (0 && /* prefix origin is almost obsolted */
1441 PR.origin != PR_ORIG_RA)
1443 else if (PR.advrtrs) {
1445 printf(" advertised by\n");
1446 for (j = 0; j < PR.advrtrs; j++) {
1447 struct sockaddr_in6 sin6;
1448 struct in6_nbrinfo *nbi;
1450 bzero(&sin6, sizeof(sin6));
1451 sin6.sin6_family = AF_INET6;
1452 sin6.sin6_len = sizeof(sin6);
1453 sin6.sin6_addr = PR.advrtr[j];
1454 sin6.sin6_scope_id = PR.if_index; /* XXX */
1455 getnameinfo((struct sockaddr *)&sin6,
1456 sin6.sin6_len, host_buf,
1457 sizeof(host_buf), NULL, 0,
1458 (nflag ? NI_NUMERICHOST : 0));
1459 printf(" %s", host_buf);
1461 nbi = getnbrinfo(&sin6.sin6_addr,
1464 switch (nbi->state) {
1465 case ND6_LLINFO_REACHABLE:
1466 case ND6_LLINFO_STALE:
1467 case ND6_LLINFO_DELAY:
1468 case ND6_LLINFO_PROBE:
1469 printf(" (reachable)\n");
1472 printf(" (unreachable)\n");
1475 printf(" (no neighbor state)\n");
1477 if (PR.advrtrs > DRLSTSIZ)
1478 printf(" and %d routers\n",
1479 PR.advrtrs - DRLSTSIZ);
1481 printf(" No advertising router\n");
1491 char dummyif[IFNAMSIZ+8];
1494 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1496 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1497 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1498 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1504 char dummyif[IFNAMSIZ+8];
1507 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1509 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1510 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1511 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1519 char dummyif[IFNAMSIZ+8];
1522 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1524 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1525 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1526 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1531 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1536 struct in6_ndifreq ndifreq;
1537 unsigned int ifindex;
1539 if (strcasecmp(ifname, "delete") == 0)
1542 if ((ifindex = if_nametoindex(ifname)) == 0)
1543 err(1, "failed to resolve i/f index for %s", ifname);
1546 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1549 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1550 ndifreq.ifindex = ifindex;
1552 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1553 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1561 struct in6_ndifreq ndifreq;
1562 char ifname[IFNAMSIZ+8];
1564 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1567 memset(&ndifreq, 0, sizeof(ndifreq));
1568 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1570 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1571 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1573 if (ndifreq.ifindex == 0)
1574 printf("No default interface.\n");
1576 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1577 err(1, "failed to resolve ifname for index %lu",
1579 printf("ND default interface = %s\n", ifname);
1590 static char result[256];
1591 int days, hours, mins, secs;
1594 char *ep = &result[sizeof(result)];
1597 days = total / 3600 / 24;
1598 hours = (total / 3600) % 24;
1599 mins = (total / 60) % 60;
1604 n = snprintf(p, ep - p, "%dd", days);
1605 if (n < 0 || n >= ep - p)
1609 if (!first || hours) {
1611 n = snprintf(p, ep - p, "%dh", hours);
1612 if (n < 0 || n >= ep - p)
1616 if (!first || mins) {
1618 n = snprintf(p, ep - p, "%dm", mins);
1619 if (n < 0 || n >= ep - p)
1623 snprintf(p, ep - p, "%ds", secs);
1629 * Print the timestamp
1630 * from tcpdump/util.c
1634 const struct timeval *tvp;
1639 s = (tvp->tv_sec + thiszone) % 86400;
1640 (void)printf("%02d:%02d:%02d.%06u ",
1641 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);