2 /* $KAME: ndp.c,v 1.104 2003/06/27 07:48:39 itojun Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1984, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
39 * Sun Microsystems, Inc.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * "@(#) Copyright (c) 1984, 1993\n\
69 * The Regents of the University of California. All rights reserved.\n";
71 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
75 * ndp - display, set, delete and flush neighbor cache
79 #include <sys/param.h>
81 #include <sys/ioctl.h>
82 #include <sys/socket.h>
83 #include <sys/sysctl.h>
85 #include <sys/queue.h>
88 #include <net/if_dl.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/if_ether.h>
95 #include <netinet/icmp6.h>
96 #include <netinet6/in6_var.h>
97 #include <netinet6/nd6.h>
99 #include <arpa/inet.h>
112 #include "gmt2local.h"
114 #define NEXTADDR(w, s) \
115 if (rtm->rtm_addrs & (w)) { \
116 bcopy((char *)&s, cp, sizeof(s)); \
123 static int32_t thiszone; /* time difference with gmt */
125 static int repeat = 0;
127 static char host_buf[NI_MAXHOST]; /* getnameinfo() */
128 static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
130 static int file(char *);
131 static void getsocket(void);
132 static int set(int, char **);
133 static void get(char *);
134 static int delete(char *);
135 static 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 static int ndp_ether_aton(char *, u_char *);
139 static void usage(void);
140 static int rtmsg(int);
141 static void ifinfo(char *, int, char **);
142 static void rtrlist(void);
143 static void plist(void);
144 static void pfx_flush(void);
145 static void rtr_flush(void);
146 static 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 static const char *rtpref_str[] = {
162 main(int argc, char **argv)
168 thiszone = gmt2local(0);
169 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
188 exit(file(optarg) ? 1 : 0);
210 repeat = atoi(optarg);
230 dump(0, mode == 'c');
240 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
244 } else if (argc == 1) {
245 if (strcmp(*argv, "delete") == 0 ||
246 if_nametoindex(*argv))
249 errx(1, "invalid interface %s", *argv);
251 getdefif(); /* always call it to print the result */
254 errx(1, "not supported yet");
265 ifinfo(arg, argc, argv);
275 if (argc < 2 || argc > 4)
277 exit(set(argc, argv) ? 1 : 0);
311 * Process a file to set standard ndp entries
318 char line[100], arg[5][50], *args[5], *p;
320 if ((fp = fopen(name, "r")) == NULL)
321 err(1, "cannot open %s", name);
322 args[0] = &arg[0][0];
323 args[1] = &arg[1][0];
324 args[2] = &arg[2][0];
325 args[3] = &arg[3][0];
326 args[4] = &arg[4][0];
328 while (fgets(line, sizeof(line), fp) != NULL) {
329 if ((p = strchr(line, '#')) != NULL)
331 for (p = line; isblank(*p); p++);
332 if (*p == '\n' || *p == '\0')
334 i = sscanf(line, "%49s %49s %49s %49s %49s",
335 arg[0], arg[1], arg[2], arg[3], arg[4]);
337 warnx("bad line: %s", line);
352 s = socket(PF_ROUTE, SOCK_RAW, 0);
360 static struct sockaddr_in6 so_mask = {
361 .sin6_len = sizeof(so_mask),
362 .sin6_family = AF_INET6
364 static struct sockaddr_in6 blank_sin = {
365 .sin6_len = sizeof(blank_sin),
366 .sin6_family = AF_INET6
368 static struct sockaddr_in6 sin_m;
369 static struct sockaddr_dl blank_sdl = {
370 .sdl_len = sizeof(blank_sdl),
371 .sdl_family = AF_LINK
373 static struct sockaddr_dl sdl_m;
374 static time_t expire_time;
375 static int flags, found_entry;
377 struct rt_msghdr m_rtm;
382 * Set an individual neighbor cache entry
385 set(int argc, char **argv)
387 register struct sockaddr_in6 *sin = &sin_m;
388 register struct sockaddr_dl *sdl;
389 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
390 struct addrinfo hints, *res;
393 char *host = argv[0], *eaddr = argv[1];
401 bzero(&hints, sizeof(hints));
402 hints.ai_family = AF_INET6;
403 gai_error = getaddrinfo(host, NULL, &hints, &res);
405 fprintf(stderr, "ndp: %s: %s\n", host,
406 gai_strerror(gai_error));
409 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
411 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
412 ea = (u_char *)LLADDR(&sdl_m);
413 if (ndp_ether_aton(eaddr, ea) == 0)
415 flags = expire_time = 0;
417 if (strncmp(argv[0], "temp", 4) == 0) {
420 gettimeofday(&now, 0);
421 expire_time = now.tv_sec + 20 * 60;
422 } else if (strncmp(argv[0], "proxy", 5) == 0)
423 flags |= RTF_ANNOUNCE;
426 if (rtmsg(RTM_GET) < 0) {
427 errx(1, "RTM_GET(%s) failed", host);
430 sin = (struct sockaddr_in6 *)(rtm + 1);
431 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
432 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
433 if (sdl->sdl_family == AF_LINK &&
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:
438 case IFT_L2VLAN: case IFT_BRIDGE:
442 fprintf(stderr, "set: cannot configure a new entry\n");
447 if (sdl->sdl_family != AF_LINK) {
448 printf("cannot intuit interface index and type for %s\n", host);
451 sdl_m.sdl_type = sdl->sdl_type;
452 sdl_m.sdl_index = sdl->sdl_index;
453 return (rtmsg(RTM_ADD));
457 * Display an individual neighbor cache entry
462 struct sockaddr_in6 *sin = &sin_m;
463 struct addrinfo hints, *res;
467 bzero(&hints, sizeof(hints));
468 hints.ai_family = AF_INET6;
469 gai_error = getaddrinfo(host, NULL, &hints, &res);
471 fprintf(stderr, "ndp: %s: %s\n", host,
472 gai_strerror(gai_error));
475 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
477 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
479 if (found_entry == 0) {
480 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
481 sizeof(host_buf), NULL ,0,
482 (nflag ? NI_NUMERICHOST : 0));
483 printf("%s (%s) -- no entry\n", host, host_buf);
489 * Delete a neighbor cache entry
494 struct sockaddr_in6 *sin = &sin_m;
495 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
496 register char *cp = m_rtmsg.m_space;
497 struct sockaddr_dl *sdl;
498 struct addrinfo hints, *res;
504 bzero(&hints, sizeof(hints));
505 hints.ai_family = AF_INET6;
506 gai_error = getaddrinfo(host, NULL, &hints, &res);
508 fprintf(stderr, "ndp: %s: %s\n", host,
509 gai_strerror(gai_error));
512 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
514 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
515 if (rtmsg(RTM_GET) < 0) {
516 errx(1, "RTM_GET(%s) failed", host);
519 sin = (struct sockaddr_in6 *)(rtm + 1);
520 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
521 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
522 if (sdl->sdl_family == AF_LINK &&
523 !(rtm->rtm_flags & RTF_GATEWAY)) {
526 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
531 if (sdl->sdl_family != AF_LINK) {
532 printf("cannot locate %s\n", host);
536 * need to reinit the field because it has rt_key
537 * but we want the actual address
539 NEXTADDR(RTA_DST, sin_m);
540 rtm->rtm_flags |= RTF_LLDATA;
541 if (rtmsg(RTM_DELETE) == 0) {
542 getnameinfo((struct sockaddr *)sin,
543 sin->sin6_len, host_buf,
544 sizeof(host_buf), NULL, 0,
545 (nflag ? NI_NUMERICHOST : 0));
546 printf("%s (%s) deleted\n", host, host_buf);
557 * Dump the entire neighbor cache
560 dump(struct sockaddr_in6 *addr, int cflag)
564 char *lim, *buf, *next;
565 struct rt_msghdr *rtm;
566 struct sockaddr_in6 *sin;
567 struct sockaddr_dl *sdl;
577 if (!tflag && !cflag)
578 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
579 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
580 W_IF, W_IF, "Netif", "Expire", "S", "Flags");
587 mib[4] = NET_RT_FLAGS;
593 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
594 err(1, "sysctl(PF_ROUTE estimate)");
596 if ((buf = malloc(needed)) == NULL)
598 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
599 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
604 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
605 int isrouter = 0, prbs = 0;
607 rtm = (struct rt_msghdr *)next;
608 sin = (struct sockaddr_in6 *)(rtm + 1);
609 sdl = (struct sockaddr_dl *)((char *)sin +
610 ALIGN(sin->sin6_len));
613 * Some OSes can produce a route that has the LINK flag but
614 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
615 * and BSD/OS, where xx is not the interface identifier on
616 * lo0). Such routes entry would annoy getnbrinfo() below,
618 * XXX: such routes should have the GATEWAY flag, not the
619 * LINK flag. However, there is rotten routing software
620 * that advertises all routes that have the GATEWAY flag.
621 * Thus, KAME kernel intentionally does not set the LINK flag.
622 * What is to be fixed is not ndp, but such routing software
623 * (and the kernel workaround)...
625 if (sdl->sdl_family != AF_LINK)
628 if (!(rtm->rtm_flags & RTF_HOST))
632 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
633 &sin->sin6_addr) == 0 ||
634 addr->sin6_scope_id != sin->sin6_scope_id)
637 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
639 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
640 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
641 /* XXX: should scope id be filled in the kernel? */
642 if (sin->sin6_scope_id == 0)
643 sin->sin6_scope_id = sdl->sdl_index;
645 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
646 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
649 if (rtm->rtm_flags & RTF_WASCLONED)
651 #elif defined(RTF_CLONED)
652 if (rtm->rtm_flags & RTF_CLONED)
655 if (rtm->rtm_flags & RTF_PINNED)
661 gettimeofday(&now, 0);
665 addrwidth = strlen(host_buf);
666 if (addrwidth < W_ADDR)
668 llwidth = strlen(ether_str(sdl));
669 if (W_ADDR + W_LL - addrwidth > llwidth)
670 llwidth = W_ADDR + W_LL - addrwidth;
671 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
672 if (ifname == NULL) {
673 strlcpy(ifix_buf, "?", sizeof(ifix_buf));
676 ifwidth = strlen(ifname);
677 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
678 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
680 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
681 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
683 /* Print neighbor discovery specific information */
684 expire = rtm->rtm_rmx.rmx_expire;
685 if (expire > now.tv_sec)
686 printf(" %-9.9s", sec2str(expire - now.tv_sec));
687 else if (expire == 0)
688 printf(" %-9.9s", "permanent");
690 printf(" %-9.9s", "expired");
692 switch (rtm->rtm_rmx.rmx_state) {
693 case ND6_LLINFO_NOSTATE:
696 #ifdef ND6_LLINFO_WAITDELETE
697 case ND6_LLINFO_WAITDELETE:
701 case ND6_LLINFO_INCOMPLETE:
704 case ND6_LLINFO_REACHABLE:
707 case ND6_LLINFO_STALE:
710 case ND6_LLINFO_DELAY:
713 case ND6_LLINFO_PROBE:
721 isrouter = rtm->rtm_flags & RTF_GATEWAY;
722 prbs = rtm->rtm_rmx.rmx_pksent;
725 * other flags. R: router, P: proxy, W: ??
727 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
728 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
730 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
732 #if 0 /* W and P are mystery even for us */
733 sin = (struct sockaddr_in6 *)
734 (sdl->sdl_len + (char *)sdl);
735 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
737 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
738 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
739 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
741 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
743 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
746 printf(" %s", flgbuf);
764 static struct in6_nbrinfo *
765 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
767 static struct in6_nbrinfo nbi;
770 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
773 bzero(&nbi, sizeof(nbi));
774 if_indextoname(ifindex, nbi.ifname);
776 if (ioctl(sock, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
778 warn("ioctl(SIOCGNBRINFO_IN6)");
788 ether_str(struct sockaddr_dl *sdl)
790 static char hbuf[NI_MAXHOST];
792 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
793 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
795 } else if (sdl->sdl_alen) {
796 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
797 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
799 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
805 ndp_ether_aton(char *a, u_char *n)
809 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
810 &o[3], &o[4], &o[5]);
812 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
815 for (i = 0; i < 6; i++)
823 printf("usage: ndp [-nt] hostname\n");
824 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
825 printf(" ndp [-nt] -A wait\n");
826 printf(" ndp [-nt] -d hostname\n");
827 printf(" ndp [-nt] -f filename\n");
828 printf(" ndp [-nt] -i interface [flags...]\n");
829 #ifdef SIOCSDEFIFACE_IN6
830 printf(" ndp [-nt] -I [interface|delete]\n");
832 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
841 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
842 register char *cp = m_rtmsg.m_space;
846 if (cmd == RTM_DELETE)
848 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
849 rtm->rtm_flags = flags;
850 rtm->rtm_version = RTM_VERSION;
854 fprintf(stderr, "ndp: internal wrong cmd\n");
857 rtm->rtm_addrs |= RTA_GATEWAY;
859 rtm->rtm_rmx.rmx_expire = expire_time;
860 rtm->rtm_inits = RTV_EXPIRE;
862 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
863 #if 0 /* we don't support ipv6addr/128 type proxying */
864 if (rtm->rtm_flags & RTF_ANNOUNCE) {
865 rtm->rtm_flags &= ~RTF_HOST;
866 rtm->rtm_addrs |= RTA_NETMASK;
871 rtm->rtm_addrs |= RTA_DST;
874 NEXTADDR(RTA_DST, sin_m);
875 NEXTADDR(RTA_GATEWAY, sdl_m);
876 #if 0 /* we don't support ipv6addr/128 type proxying */
877 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
878 NEXTADDR(RTA_NETMASK, so_mask);
881 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
884 rtm->rtm_seq = ++seq;
886 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
887 if (errno != ESRCH || cmd != RTM_DELETE) {
888 err(1, "writing to routing socket");
893 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
894 } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq ||
895 rtm->rtm_pid != pid));
897 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
903 ifinfo(char *ifname, int argc, char **argv)
905 struct in6_ndireq nd;
908 #ifdef IPV6CTL_USETEMPADDR
912 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
916 bzero(&nd, sizeof(nd));
917 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
918 if (ioctl(sock, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
919 err(1, "ioctl(SIOCGIFINFO_IN6)");
924 for (i = 0; i < argc; i++) {
933 #define SETFLAG(s, f) do { \
934 if (strcmp(cp, (s)) == 0) { \
942 * XXX: this macro is not 100% correct, in that it matches "nud" against
943 * "nudbogus". But we just let it go since this is minor.
945 #define SETVALUE(f, v) do { \
947 unsigned long newval; \
948 v = 0; /* unspecified */ \
949 if (strncmp(cp, f, strlen(f)) == 0) { \
950 valptr = strchr(cp, '='); \
951 if (valptr == NULL) \
952 err(1, "syntax error in %s field", (f)); \
954 newval = strtoul(++valptr, NULL, 0); \
956 err(1, "syntax error in %s's value", (f)); \
961 SETFLAG("disabled", ND6_IFF_IFDISABLED);
962 SETFLAG("nud", ND6_IFF_PERFORMNUD);
963 #ifdef ND6_IFF_ACCEPT_RTADV
964 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
966 #ifdef ND6_IFF_AUTO_LINKLOCAL
967 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
969 #ifdef ND6_IFF_NO_PREFER_IFACE
970 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
972 SETVALUE("basereachable", ND.basereachable);
973 SETVALUE("retrans", ND.retrans);
974 SETVALUE("curhlim", ND.chlim);
977 if (ioctl(sock, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
978 err(1, "ioctl(SIOCSIFINFO_IN6)");
985 if (!ND.initialized) {
986 errx(1, "%s: not initialized yet", ifname);
990 if (ioctl(sock, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
991 err(1, "ioctl(SIOCGIFINFO_IN6)");
994 printf("linkmtu=%d", ND.linkmtu);
995 printf(", maxmtu=%d", ND.maxmtu);
996 printf(", curhlim=%d", ND.chlim);
997 printf(", basereachable=%ds%dms",
998 ND.basereachable / 1000, ND.basereachable % 1000);
999 printf(", reachable=%ds", ND.reachable);
1000 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1001 #ifdef IPV6CTL_USETEMPADDR
1002 memset(nullbuf, 0, sizeof(nullbuf));
1003 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1007 for (i = 0; i < 3; i++) {
1010 printf("\nRandom seed(0): ");
1011 rbuf = ND.randomseed0;
1014 printf("\nRandom seed(1): ");
1015 rbuf = ND.randomseed1;
1018 printf("\nRandom ID: ");
1022 errx(1, "impossible case for tempaddr display");
1024 for (j = 0; j < 8; j++)
1025 printf("%02x", rbuf[j]);
1028 #endif /* IPV6CTL_USETEMPADDR */
1030 printf("\nFlags: ");
1031 #ifdef ND6_IFF_IFDISABLED
1032 if ((ND.flags & ND6_IFF_IFDISABLED))
1033 printf("disabled ");
1035 if ((ND.flags & ND6_IFF_PERFORMNUD))
1037 #ifdef ND6_IFF_ACCEPT_RTADV
1038 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1039 printf("accept_rtadv ");
1041 #ifdef ND6_IFF_AUTO_LINKLOCAL
1042 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1043 printf("auto_linklocal ");
1045 #ifdef ND6_IFF_NO_PREFER_IFACE
1046 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1047 printf("no_prefer_iface ");
1056 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1057 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1063 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1065 struct in6_defrouter *p, *ep;
1069 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1070 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1080 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1081 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1085 ep = (struct in6_defrouter *)(buf + l);
1086 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1089 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1090 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1091 (nflag ? NI_NUMERICHOST : 0)) != 0)
1092 strlcpy(host_buf, "?", sizeof(host_buf));
1094 printf("%s if=%s", host_buf,
1095 if_indextoname(p->if_index, ifix_buf));
1096 printf(", flags=%s%s",
1097 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1098 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1099 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
1100 printf("%s", p->flags & ND_RA_FLAG_IPV6_ONLY ? "S" : "");
1102 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1103 printf(", pref=%s", rtpref_str[rtpref]);
1105 gettimeofday(&now, 0);
1107 printf(", expire=Never\n");
1109 printf(", expire=%s\n",
1110 sec2str(p->expire - now.tv_sec));
1118 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1120 struct in6_prefix *p, *ep, *n;
1121 struct sockaddr_in6 *advrtr;
1124 const int niflags = NI_NUMERICHOST;
1125 int ninflags = nflag ? NI_NUMERICHOST : 0;
1126 char namebuf[NI_MAXHOST];
1128 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1129 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1137 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1138 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1142 ep = (struct in6_prefix *)(buf + l);
1143 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1144 advrtr = (struct sockaddr_in6 *)(p + 1);
1145 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1147 if (getnameinfo((struct sockaddr *)&p->prefix,
1148 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1149 NULL, 0, niflags) != 0)
1150 strlcpy(namebuf, "?", sizeof(namebuf));
1151 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1152 if_indextoname(p->if_index, ifix_buf));
1154 gettimeofday(&now, 0);
1156 * meaning of fields, especially flags, is very different
1157 * by origin. notify the difference to the users.
1159 printf("flags=%s%s%s%s%s",
1160 p->raflags.onlink ? "L" : "",
1161 p->raflags.autonomous ? "A" : "",
1162 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1163 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1165 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1170 if (p->vltime == ND6_INFINITE_LIFETIME)
1171 printf(" vltime=infinity");
1173 printf(" vltime=%lu", (unsigned long)p->vltime);
1174 if (p->pltime == ND6_INFINITE_LIFETIME)
1175 printf(", pltime=infinity");
1177 printf(", pltime=%lu", (unsigned long)p->pltime);
1179 printf(", expire=Never");
1180 else if (p->expire >= now.tv_sec)
1181 printf(", expire=%s",
1182 sec2str(p->expire - now.tv_sec));
1184 printf(", expired");
1185 printf(", ref=%d", p->refcnt);
1188 * "advertising router" list is meaningful only if the prefix
1189 * information is from RA.
1193 struct sockaddr_in6 *sin6;
1196 printf(" advertised by\n");
1197 for (j = 0; j < p->advrtrs; j++) {
1198 struct in6_nbrinfo *nbi;
1200 if (getnameinfo((struct sockaddr *)sin6,
1201 sin6->sin6_len, namebuf, sizeof(namebuf),
1202 NULL, 0, ninflags) != 0)
1203 strlcpy(namebuf, "?", sizeof(namebuf));
1204 printf(" %s", namebuf);
1206 nbi = getnbrinfo(&sin6->sin6_addr,
1209 switch (nbi->state) {
1210 case ND6_LLINFO_REACHABLE:
1211 case ND6_LLINFO_STALE:
1212 case ND6_LLINFO_DELAY:
1213 case ND6_LLINFO_PROBE:
1214 printf(" (reachable)\n");
1217 printf(" (unreachable)\n");
1220 printf(" (no neighbor state)\n");
1224 printf(" No advertising router\n");
1232 char dummyif[IFNAMSIZ+8];
1235 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1237 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1238 if (ioctl(sock, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1239 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1247 char dummyif[IFNAMSIZ+8];
1250 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1252 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1253 if (ioctl(sock, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1254 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1262 char dummyif[IFNAMSIZ+8];
1265 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1267 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1268 if (ioctl(sock, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1269 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1274 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1276 setdefif(char *ifname)
1278 struct in6_ndifreq ndifreq;
1279 unsigned int ifindex;
1282 if (strcasecmp(ifname, "delete") == 0)
1285 if ((ifindex = if_nametoindex(ifname)) == 0)
1286 err(1, "failed to resolve i/f index for %s", ifname);
1289 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1292 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1293 ndifreq.ifindex = ifindex;
1295 if (ioctl(sock, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1296 err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1304 struct in6_ndifreq ndifreq;
1305 char ifname[IFNAMSIZ+8];
1308 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1311 memset(&ndifreq, 0, sizeof(ndifreq));
1312 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1314 if (ioctl(sock, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1315 err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1317 if (ndifreq.ifindex == 0)
1318 printf("No default interface.\n");
1320 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1321 err(1, "failed to resolve ifname for index %lu",
1323 printf("ND default interface = %s\n", ifname);
1328 #endif /* SIOCSDEFIFACE_IN6 */
1331 sec2str(time_t total)
1333 static char result[256];
1334 int days, hours, mins, secs;
1337 char *ep = &result[sizeof(result)];
1340 days = total / 3600 / 24;
1341 hours = (total / 3600) % 24;
1342 mins = (total / 60) % 60;
1347 n = snprintf(p, ep - p, "%dd", days);
1348 if (n < 0 || n >= ep - p)
1352 if (!first || hours) {
1354 n = snprintf(p, ep - p, "%dh", hours);
1355 if (n < 0 || n >= ep - p)
1359 if (!first || mins) {
1361 n = snprintf(p, ep - p, "%dm", mins);
1362 if (n < 0 || n >= ep - p)
1366 snprintf(p, ep - p, "%ds", secs);
1372 * Print the timestamp
1373 * from tcpdump/util.c
1376 ts_print(const struct timeval *tvp)
1381 sec = (tvp->tv_sec + thiszone) % 86400;
1382 (void)printf("%02d:%02d:%02d.%06u ",
1383 sec / 3600, (sec % 3600) / 60, sec % 60, (u_int32_t)tvp->tv_usec);