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 <libxo/xo.h>
113 #include "gmt2local.h"
115 #define NEXTADDR(w, s) \
116 if (rtm->rtm_addrs & (w)) { \
117 bcopy((char *)&s, cp, sizeof(s)); \
124 static int32_t thiszone; /* time difference with gmt */
126 static int repeat = 0;
128 static char host_buf[NI_MAXHOST]; /* getnameinfo() */
129 static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
131 static int file(char *);
132 static void getsocket(void);
133 static int set(int, char **);
134 static void get(char *);
135 static int delete(char *);
136 static void dump(struct sockaddr_in6 *, int);
137 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
138 static char *ether_str(struct sockaddr_dl *);
139 static int ndp_ether_aton(char *, u_char *);
140 static void usage(void);
141 static int rtmsg(int);
142 static void ifinfo(char *, int, char **);
143 static void rtrlist(void);
144 static void plist(void);
145 static void pfx_flush(void);
146 static void rtr_flush(void);
147 static void harmonize_rtr(void);
148 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
149 static void getdefif(void);
150 static void setdefif(char *);
152 static char *sec2str(time_t);
153 static void ts_print(const struct timeval *);
155 static const char *rtpref_str[] = {
162 #define NDP_XO_VERSION "1"
165 main(int argc, char **argv)
171 thiszone = gmt2local(0);
173 argc = xo_parse_args(argc, argv);
176 xo_set_version(NDP_XO_VERSION);
177 xo_open_container("ndp");
179 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
198 exit(file(optarg) ? 1 : 0);
220 repeat = atoi(optarg);
240 dump(0, mode == 'c');
247 xo_open_list("neighbor-cache");
249 xo_close_list("neighbor-cache");
252 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
256 } else if (argc == 1) {
257 if (strcmp(*argv, "delete") == 0 ||
258 if_nametoindex(*argv))
261 xo_errx(1, "invalid interface %s", *argv);
263 getdefif(); /* always call it to print the result */
266 xo_errx(1, "not supported yet");
277 ifinfo(arg, argc, argv);
287 if (argc < 2 || argc > 4)
289 exit(set(argc, argv) ? 1 : 0);
319 xo_close_container("ndp");
325 * Process a file to set standard ndp entries
332 char line[100], arg[5][50], *args[5], *p;
334 if ((fp = fopen(name, "r")) == NULL)
335 xo_err(1, "cannot open %s", name);
336 args[0] = &arg[0][0];
337 args[1] = &arg[1][0];
338 args[2] = &arg[2][0];
339 args[3] = &arg[3][0];
340 args[4] = &arg[4][0];
342 while (fgets(line, sizeof(line), fp) != NULL) {
343 if ((p = strchr(line, '#')) != NULL)
345 for (p = line; isblank(*p); p++);
346 if (*p == '\n' || *p == '\0')
348 i = sscanf(line, "%49s %49s %49s %49s %49s",
349 arg[0], arg[1], arg[2], arg[3], arg[4]);
351 xo_warnx("bad line: %s", line);
366 s = socket(PF_ROUTE, SOCK_RAW, 0);
374 static struct sockaddr_in6 so_mask = {
375 .sin6_len = sizeof(so_mask),
376 .sin6_family = AF_INET6
378 static struct sockaddr_in6 blank_sin = {
379 .sin6_len = sizeof(blank_sin),
380 .sin6_family = AF_INET6
382 static struct sockaddr_in6 sin_m;
383 static struct sockaddr_dl blank_sdl = {
384 .sdl_len = sizeof(blank_sdl),
385 .sdl_family = AF_LINK
387 static struct sockaddr_dl sdl_m;
388 static time_t expire_time;
389 static int flags, found_entry;
391 struct rt_msghdr m_rtm;
396 * Set an individual neighbor cache entry
399 set(int argc, char **argv)
401 register struct sockaddr_in6 *sin = &sin_m;
402 register struct sockaddr_dl *sdl;
403 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
404 struct addrinfo hints, *res;
407 char *host = argv[0], *eaddr = argv[1];
415 bzero(&hints, sizeof(hints));
416 hints.ai_family = AF_INET6;
417 gai_error = getaddrinfo(host, NULL, &hints, &res);
419 xo_warnx("%s: %s", host, gai_strerror(gai_error));
422 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
424 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
425 ea = (u_char *)LLADDR(&sdl_m);
426 if (ndp_ether_aton(eaddr, ea) == 0)
428 flags = expire_time = 0;
430 if (strncmp(argv[0], "temp", 4) == 0) {
433 gettimeofday(&now, 0);
434 expire_time = now.tv_sec + 20 * 60;
435 } else if (strncmp(argv[0], "proxy", 5) == 0)
436 flags |= RTF_ANNOUNCE;
439 if (rtmsg(RTM_GET) < 0) {
440 xo_errx(1, "RTM_GET(%s) failed", host);
443 sin = (struct sockaddr_in6 *)(rtm + 1);
444 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
445 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
446 if (sdl->sdl_family == AF_LINK &&
447 !(rtm->rtm_flags & RTF_GATEWAY)) {
448 switch (sdl->sdl_type) {
449 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
450 case IFT_ISO88024: case IFT_ISO88025:
451 case IFT_L2VLAN: case IFT_BRIDGE:
455 xo_warnx("cannot configure a new entry");
460 if (sdl->sdl_family != AF_LINK) {
461 xo_warnx("cannot intuit interface index and type for %s", host);
464 sdl_m.sdl_type = sdl->sdl_type;
465 sdl_m.sdl_index = sdl->sdl_index;
466 return (rtmsg(RTM_ADD));
470 * Display an individual neighbor cache entry
475 struct sockaddr_in6 *sin = &sin_m;
476 struct addrinfo hints, *res;
480 bzero(&hints, sizeof(hints));
481 hints.ai_family = AF_INET6;
482 gai_error = getaddrinfo(host, NULL, &hints, &res);
484 xo_warnx("%s: %s", host, gai_strerror(gai_error));
487 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
489 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
491 if (found_entry == 0) {
492 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
493 sizeof(host_buf), NULL ,0,
494 (nflag ? NI_NUMERICHOST : 0));
495 xo_errx(1, "%s (%s) -- no entry", host, host_buf);
500 * Delete a neighbor cache entry
505 struct sockaddr_in6 *sin = &sin_m;
506 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
507 register char *cp = m_rtmsg.m_space;
508 struct sockaddr_dl *sdl;
509 struct addrinfo hints, *res;
515 bzero(&hints, sizeof(hints));
516 hints.ai_family = AF_INET6;
517 gai_error = getaddrinfo(host, NULL, &hints, &res);
519 xo_warnx("%s: %s", host, gai_strerror(gai_error));
522 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
524 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
525 if (rtmsg(RTM_GET) < 0) {
526 xo_errx(1, "RTM_GET(%s) failed", host);
529 sin = (struct sockaddr_in6 *)(rtm + 1);
530 sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
531 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
532 if (sdl->sdl_family == AF_LINK &&
533 !(rtm->rtm_flags & RTF_GATEWAY)) {
536 xo_warnx("delete: cannot delete non-NDP entry");
541 if (sdl->sdl_family != AF_LINK) {
542 xo_warnx("cannot locate %s", host);
546 * need to reinit the field because it has rt_key
547 * but we want the actual address
549 NEXTADDR(RTA_DST, sin_m);
550 rtm->rtm_flags |= RTF_LLDATA;
551 if (rtmsg(RTM_DELETE) == 0) {
552 getnameinfo((struct sockaddr *)sin,
553 sin->sin6_len, host_buf,
554 sizeof(host_buf), NULL, 0,
555 (nflag ? NI_NUMERICHOST : 0));
556 xo_open_instance("neighbor-cache");
558 char *ifname = if_indextoname(sdl->sdl_index, ifix_buf);
559 if (ifname == NULL) {
560 strlcpy(ifix_buf, "?", sizeof(ifix_buf));
563 char abuf[INET6_ADDRSTRLEN];
564 inet_ntop(AF_INET6, &sin->sin6_addr, abuf, sizeof(abuf));
566 xo_emit("{:hostname/%s}{d:/ (%s) deleted\n}", host, host_buf);
567 xo_emit("{e:address/%s}{e:interface/%s}", abuf, ifname);
568 xo_close_instance("neighbor-cache");
579 * Dump the entire neighbor cache
582 dump(struct sockaddr_in6 *addr, int cflag)
586 char *lim, *buf, *next;
587 struct rt_msghdr *rtm;
588 struct sockaddr_in6 *sin;
589 struct sockaddr_dl *sdl;
599 if (!tflag && !cflag) {
601 snprintf(xobuf, sizeof(xobuf),
602 "{T:/%%-%d.%ds} {T:/%%-%d.%ds} {T:/%%%d.%ds} {T:/%%-9.9s} {T:%%1s} {T:%%5s}\n",
603 W_ADDR, W_ADDR, W_LL, W_LL, W_IF, W_IF);
604 xo_emit(xobuf, "Neighbor", "Linklayer Address", "Netif", "Expire", "S", "Flags");
606 xo_open_list("neighbor-cache");
612 mib[4] = NET_RT_FLAGS;
618 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
619 xo_err(1, "sysctl(PF_ROUTE estimate)");
621 if ((buf = malloc(needed)) == NULL)
623 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
624 xo_err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
629 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
630 int isrouter = 0, prbs = 0;
632 rtm = (struct rt_msghdr *)next;
633 sin = (struct sockaddr_in6 *)(rtm + 1);
634 sdl = (struct sockaddr_dl *)((char *)sin +
635 ALIGN(sin->sin6_len));
638 * Some OSes can produce a route that has the LINK flag but
639 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
640 * and BSD/OS, where xx is not the interface identifier on
641 * lo0). Such routes entry would annoy getnbrinfo() below,
643 * XXX: such routes should have the GATEWAY flag, not the
644 * LINK flag. However, there is rotten routing software
645 * that advertises all routes that have the GATEWAY flag.
646 * Thus, KAME kernel intentionally does not set the LINK flag.
647 * What is to be fixed is not ndp, but such routing software
648 * (and the kernel workaround)...
650 if (sdl->sdl_family != AF_LINK)
653 if (!(rtm->rtm_flags & RTF_HOST))
657 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
658 &sin->sin6_addr) == 0 ||
659 addr->sin6_scope_id != sin->sin6_scope_id)
662 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
664 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
665 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
666 /* XXX: should scope id be filled in the kernel? */
667 if (sin->sin6_scope_id == 0)
668 sin->sin6_scope_id = sdl->sdl_index;
670 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
671 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
674 if (rtm->rtm_flags & RTF_WASCLONED)
676 #elif defined(RTF_CLONED)
677 if (rtm->rtm_flags & RTF_CLONED)
680 if (rtm->rtm_flags & RTF_PINNED)
686 gettimeofday(&now, 0);
690 addrwidth = strlen(host_buf);
691 if (addrwidth < W_ADDR)
693 llwidth = strlen(ether_str(sdl));
694 if (W_ADDR + W_LL - addrwidth > llwidth)
695 llwidth = W_ADDR + W_LL - addrwidth;
696 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
697 if (ifname == NULL) {
698 strlcpy(ifix_buf, "?", sizeof(ifix_buf));
701 ifwidth = strlen(ifname);
702 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
703 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
705 xo_open_instance("neighbor-cache");
706 /* Compose format string for libxo, as it doesn't support *.* */
708 snprintf(xobuf, sizeof(xobuf),
709 "{:address/%%-%d.%ds/%%s} {:mac-address/%%-%d.%ds/%%s} {:interface/%%%d.%ds/%%s}",
710 addrwidth, addrwidth, llwidth, llwidth, ifwidth, ifwidth);
711 xo_emit(xobuf, host_buf, ether_str(sdl), ifname);
713 /* Print neighbor discovery specific information */
714 expire = rtm->rtm_rmx.rmx_expire;
715 int expire_in = expire - now.tv_sec;
716 if (expire > now.tv_sec)
717 xo_emit("{d:/ %-9.9s}{e:expires_sec/%d}", sec2str(expire_in), expire_in);
718 else if (expire == 0)
719 xo_emit("{d:/ %-9.9s}{en:permanent/true}", "permanent");
721 xo_emit("{d:/ %-9.9s}{e:expires_sec/%d}", "expired", expire_in);
723 char *lle_state = "";
724 switch (rtm->rtm_rmx.rmx_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:
752 xo_emit(" {:neighbor-state/%s}", lle_state);
754 isrouter = rtm->rtm_flags & RTF_GATEWAY;
755 prbs = rtm->rtm_rmx.rmx_pksent;
758 * other flags. R: router, P: proxy, W: ??
760 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
761 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
763 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
765 #if 0 /* W and P are mystery even for us */
766 sin = (struct sockaddr_in6 *)
767 (sdl->sdl_len + (char *)sdl);
768 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
770 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
771 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
772 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
774 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
776 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
779 xo_emit(" {:nd-flags/%s}", flgbuf);
782 xo_emit("{d:/ %d}", prbs);
785 xo_close_instance("neighbor-cache");
797 xo_close_list("neighbor-cache");
800 static struct in6_nbrinfo *
801 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
803 static struct in6_nbrinfo nbi;
806 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
809 bzero(&nbi, sizeof(nbi));
810 if_indextoname(ifindex, nbi.ifname);
812 if (ioctl(sock, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
814 xo_warn("ioctl(SIOCGNBRINFO_IN6)");
824 ether_str(struct sockaddr_dl *sdl)
826 static char hbuf[NI_MAXHOST];
828 if (sdl->sdl_alen == ETHER_ADDR_LEN) {
829 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
831 } else if (sdl->sdl_alen) {
832 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
833 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
835 snprintf(hbuf, sizeof(hbuf), "(incomplete)");
841 ndp_ether_aton(char *a, u_char *n)
845 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
846 &o[3], &o[4], &o[5]);
848 xo_warnx("invalid Ethernet address '%s'", a);
851 for (i = 0; i < 6; i++)
859 printf("usage: ndp [-nt] hostname\n");
860 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
861 printf(" ndp [-nt] -A wait\n");
862 printf(" ndp [-nt] -d hostname\n");
863 printf(" ndp [-nt] -f filename\n");
864 printf(" ndp [-nt] -i interface [flags...]\n");
865 #ifdef SIOCSDEFIFACE_IN6
866 printf(" ndp [-nt] -I [interface|delete]\n");
868 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
877 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
878 register char *cp = m_rtmsg.m_space;
882 if (cmd == RTM_DELETE)
884 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
885 rtm->rtm_flags = flags;
886 rtm->rtm_version = RTM_VERSION;
890 xo_errx(1, "internal wrong cmd");
892 rtm->rtm_addrs |= RTA_GATEWAY;
894 rtm->rtm_rmx.rmx_expire = expire_time;
895 rtm->rtm_inits = RTV_EXPIRE;
897 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
900 rtm->rtm_addrs |= RTA_DST;
903 NEXTADDR(RTA_DST, sin_m);
904 NEXTADDR(RTA_GATEWAY, sdl_m);
906 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
909 rtm->rtm_seq = ++seq;
911 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
912 if (errno != ESRCH || cmd != RTM_DELETE) {
913 xo_err(1, "writing to routing socket");
918 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
919 } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq ||
920 rtm->rtm_pid != pid));
922 xo_warn("read from routing socket");
927 ifinfo(char *ifname, int argc, char **argv)
929 struct in6_ndireq nd;
932 #ifdef IPV6CTL_USETEMPADDR
936 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
940 bzero(&nd, sizeof(nd));
941 strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
942 if (ioctl(sock, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
943 xo_err(1, "ioctl(SIOCGIFINFO_IN6)");
948 for (i = 0; i < argc; i++) {
957 #define SETFLAG(s, f) do { \
958 if (strcmp(cp, (s)) == 0) { \
966 * XXX: this macro is not 100% correct, in that it matches "nud" against
967 * "nudbogus". But we just let it go since this is minor.
969 #define SETVALUE(f, v) do { \
971 unsigned long newval; \
972 v = 0; /* unspecified */ \
973 if (strncmp(cp, f, strlen(f)) == 0) { \
974 valptr = strchr(cp, '='); \
975 if (valptr == NULL) \
976 xo_err(1, "syntax error in %s field", (f)); \
978 newval = strtoul(++valptr, NULL, 0); \
980 xo_err(1, "syntax error in %s's value", (f)); \
985 SETFLAG("disabled", ND6_IFF_IFDISABLED);
986 SETFLAG("nud", ND6_IFF_PERFORMNUD);
987 #ifdef ND6_IFF_ACCEPT_RTADV
988 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
990 #ifdef ND6_IFF_AUTO_LINKLOCAL
991 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
993 #ifdef ND6_IFF_NO_PREFER_IFACE
994 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
996 SETVALUE("basereachable", ND.basereachable);
997 SETVALUE("retrans", ND.retrans);
998 SETVALUE("curhlim", ND.chlim);
1000 ND.flags = newflags;
1001 if (ioctl(sock, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
1002 xo_err(1, "ioctl(SIOCSIFINFO_IN6)");
1009 if (!ND.initialized) {
1010 xo_errx(1, "%s: not initialized yet", ifname);
1014 if (ioctl(sock, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
1015 xo_err(1, "ioctl(SIOCGIFINFO_IN6)");
1018 xo_open_container("ifinfo");
1020 xo_emit("{e:interface/%s}", ifname);
1021 xo_emit("linkmtu={:linkmtu/%d}", ND.linkmtu);
1022 xo_emit(", maxmtu={:maxmtu/%d}", ND.maxmtu);
1023 xo_emit(", curhlim={:curhlim/%d}", ND.chlim);
1024 xo_emit("{d:/, basereachable=%ds%dms}{e:basereachable_ms/%u}",
1025 ND.basereachable / 1000, ND.basereachable % 1000, ND.basereachable);
1026 xo_emit("{d:/, reachable=%ds}{e:reachable_ms/%u}", ND.reachable, ND.reachable * 1000);
1027 xo_emit("{d:/, retrans=%ds%dms}{e:retrans_ms/%u}", ND.retrans / 1000, ND.retrans % 1000,
1029 #ifdef IPV6CTL_USETEMPADDR
1030 memset(nullbuf, 0, sizeof(nullbuf));
1031 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1035 for (i = 0; i < 3; i++) {
1036 const char *txt, *field;
1039 txt = "\nRandom seed(0): ";
1041 rbuf = ND.randomseed0;
1044 txt = "\nRandom seed(1): ";
1046 rbuf = ND.randomseed1;
1049 txt = "\nRandom ID: ";
1050 field = "random_id";
1054 xo_errx(1, "impossible case for tempaddr display");
1056 char abuf[20], xobuf[200];
1057 for (j = 0; j < 8; j++)
1058 snprintf(&abuf[j * 2], sizeof(abuf), "%02X", rbuf[j]);
1059 snprintf(xobuf, sizeof(xobuf), "%s{:%s/%%s}", txt, field);
1060 xo_emit(xobuf, abuf);
1063 #endif /* IPV6CTL_USETEMPADDR */
1065 xo_emit("\nFlags: {e:flags/%u}", ND.flags);
1066 xo_open_list("flags_pretty");
1067 #ifdef ND6_IFF_IFDISABLED
1068 if ((ND.flags & ND6_IFF_IFDISABLED))
1069 xo_emit("{l:%s} ", "disabled");
1071 if ((ND.flags & ND6_IFF_PERFORMNUD))
1072 xo_emit("{l:%s} ", "nud");
1073 #ifdef ND6_IFF_ACCEPT_RTADV
1074 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1075 xo_emit("{l:%s} ", "accept_rtadv");
1077 #ifdef ND6_IFF_AUTO_LINKLOCAL
1078 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1079 xo_emit("{l:%s} ", "auto_linklocal");
1081 #ifdef ND6_IFF_NO_PREFER_IFACE
1082 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1083 xo_emit("{l:%s} ", "no_prefer_iface");
1085 xo_close_list("flags");
1089 xo_close_container("ifinfo");
1094 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1095 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1101 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1103 struct in6_defrouter *p, *ep;
1107 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1108 xo_err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1115 xo_err(1, "malloc");
1118 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1119 xo_err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1123 xo_open_list("router-list");
1125 ep = (struct in6_defrouter *)(buf + l);
1126 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1128 char abuf[INET6_ADDRSTRLEN], *paddr;
1130 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1131 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1132 (nflag ? NI_NUMERICHOST : 0)) != 0)
1133 strlcpy(host_buf, "?", sizeof(host_buf));
1137 inet_ntop(AF_INET6, &p->rtaddr.sin6_addr, abuf, sizeof(abuf));
1141 xo_open_instance("router-list");
1142 xo_emit("{:hostname/%s}{e:address/%s} if={:interface/%s}",
1144 if_indextoname(p->if_index, ifix_buf));
1145 xo_open_list("flags_pretty");
1146 char rflags[6] = {}, *pflags = rflags;
1147 if (p->flags & ND_RA_FLAG_MANAGED) {
1149 xo_emit("{el:%s}", "managed");
1151 if (p->flags & ND_RA_FLAG_OTHER) {
1153 xo_emit("{el:%s}", "other");
1155 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
1156 if (p->flags & ND_RA_FLAG_IPV6_ONLY) {
1158 xo_emit("{el:%s}", "ipv6only");
1161 xo_close_list("flags_pretty");
1162 xo_emit(", flags={:flags/%s}", rflags);
1164 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1165 xo_emit(", pref={:preference/%s}", rtpref_str[rtpref]);
1167 gettimeofday(&now, 0);
1169 xo_emit(", expire=Never\n{en:permanent/true}");
1171 xo_emit("{d:/, expire=%s\n}{e:expires_sec/%ld}",
1172 sec2str(p->expire - now.tv_sec),
1173 (long)p->expire - now.tv_sec);
1174 xo_close_instance("router-list");
1177 xo_close_list("router-list");
1183 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1185 struct in6_prefix *p, *ep, *n;
1186 struct sockaddr_in6 *advrtr;
1189 const int niflags = NI_NUMERICHOST;
1190 int ninflags = nflag ? NI_NUMERICHOST : 0;
1191 char namebuf[NI_MAXHOST];
1193 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1194 xo_err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1199 xo_err(1, "malloc");
1202 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1203 xo_err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1207 xo_open_list("prefix-list");
1209 ep = (struct in6_prefix *)(buf + l);
1210 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1211 advrtr = (struct sockaddr_in6 *)(p + 1);
1212 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1214 xo_open_instance("prefix-list");
1215 if (getnameinfo((struct sockaddr *)&p->prefix,
1216 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1217 NULL, 0, niflags) != 0)
1218 strlcpy(namebuf, "?", sizeof(namebuf));
1219 xo_emit("{:prefix/%s%s%d} if={:interface/%s}\n", namebuf, "/",
1220 p->prefixlen, if_indextoname(p->if_index, ifix_buf));
1222 gettimeofday(&now, 0);
1224 * meaning of fields, especially flags, is very different
1225 * by origin. notify the difference to the users.
1227 char flags[10] = {}, *pflags = flags;
1228 xo_open_list("flags_pretty");
1229 if (p->raflags.onlink) {
1231 xo_emit("{el:%s}", "ra_onlink");
1233 if (p->raflags.autonomous) {
1235 xo_emit("{el:%s}", "ra_autonomous");
1237 if (p->flags & NDPRF_ONLINK) {
1239 xo_emit("{el:%s}", "is_onlink");
1241 if (p->flags & NDPRF_DETACHED) {
1243 xo_emit("{el:%s}", "is_detached");
1246 if (p->flags & NDPRF_HOME) {
1248 xo_emit("{el:%s}", "is_home");
1251 xo_close_list("flags_pretty");
1252 xo_emit("flags={:flags/%s}", flags);
1253 int expire_in = p->expire - now.tv_sec;
1255 if (p->vltime == ND6_INFINITE_LIFETIME)
1256 xo_emit(" vltime=infinity{e:valid-lifetime/%lu}",
1257 (unsigned long)p->vltime);
1259 xo_emit(" vltime={:valid-lifetime/%lu}",
1260 (unsigned long)p->vltime);
1261 if (p->pltime == ND6_INFINITE_LIFETIME)
1262 xo_emit(", pltime=infinity{e:preferred-lifetime/%lu}",
1263 (unsigned long)p->pltime);
1265 xo_emit(", pltime={:preferred-lifetime/%lu}",
1266 (unsigned long)p->pltime);
1268 xo_emit(", expire=Never{en:permanent/true}");
1269 else if (p->expire >= now.tv_sec)
1270 xo_emit(", expire=%s{e:expires_sec/%d}",
1271 sec2str(expire_in), expire_in);
1273 xo_emit(", expired{e:expires_sec/%d}", expire_in);
1274 xo_emit(", ref={:refcount/%d}", p->refcnt);
1277 * "advertising router" list is meaningful only if the prefix
1278 * information is from RA.
1282 struct sockaddr_in6 *sin6;
1285 xo_emit(" advertised by\n");
1286 xo_open_list("advertising-routers");
1287 for (j = 0; j < p->advrtrs; j++) {
1288 struct in6_nbrinfo *nbi;
1290 xo_open_instance("advertising-routers");
1291 if (getnameinfo((struct sockaddr *)sin6,
1292 sin6->sin6_len, namebuf, sizeof(namebuf),
1293 NULL, 0, ninflags) != 0)
1294 strlcpy(namebuf, "?", sizeof(namebuf));
1295 char abuf[INET6_ADDRSTRLEN];
1296 inet_ntop(AF_INET6, &sin6->sin6_addr, abuf,
1299 xo_emit(" {:hostname/%s}{e:address/%s}",
1302 nbi = getnbrinfo(&sin6->sin6_addr,
1304 const char *state = "";
1306 switch (nbi->state) {
1307 case ND6_LLINFO_REACHABLE:
1308 case ND6_LLINFO_STALE:
1309 case ND6_LLINFO_DELAY:
1310 case ND6_LLINFO_PROBE:
1311 state = "reachable";
1314 state = "unreachable";
1317 state = "no neighbor state";
1318 xo_emit(" ({:state/%s})\n", state);
1320 xo_close_instance("advertising-routers");
1322 xo_close_list("advertising-routers");
1324 xo_emit(" No advertising router\n");
1325 xo_close_instance("prefix-list");
1329 xo_close_list("prefix-list");
1335 char dummyif[IFNAMSIZ+8];
1338 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1339 xo_err(1, "socket");
1340 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1341 if (ioctl(sock, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1342 xo_err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1350 char dummyif[IFNAMSIZ+8];
1353 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1354 xo_err(1, "socket");
1355 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1356 if (ioctl(sock, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1357 xo_err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1365 char dummyif[IFNAMSIZ+8];
1368 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1369 xo_err(1, "socket");
1370 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1371 if (ioctl(sock, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1372 xo_err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1377 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1379 setdefif(char *ifname)
1381 struct in6_ndifreq ndifreq;
1382 unsigned int ifindex;
1385 if (strcasecmp(ifname, "delete") == 0)
1388 if ((ifindex = if_nametoindex(ifname)) == 0)
1389 xo_err(1, "failed to resolve i/f index for %s", ifname);
1392 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1393 xo_err(1, "socket");
1395 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1396 ndifreq.ifindex = ifindex;
1398 if (ioctl(sock, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1399 xo_err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1407 struct in6_ndifreq ndifreq;
1408 char ifname[IFNAMSIZ+8];
1411 if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1412 xo_err(1, "socket");
1414 memset(&ndifreq, 0, sizeof(ndifreq));
1415 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1417 if (ioctl(sock, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1418 xo_err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1420 if (ndifreq.ifindex == 0)
1421 xo_emit("No default interface.\n");
1423 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1424 xo_err(1, "failed to resolve ifname for index %lu",
1426 xo_emit("ND default interface = {:default-interface/%s}\n", ifname);
1431 #endif /* SIOCSDEFIFACE_IN6 */
1434 sec2str(time_t total)
1436 static char result[256];
1437 int days, hours, mins, secs;
1440 char *ep = &result[sizeof(result)];
1443 days = total / 3600 / 24;
1444 hours = (total / 3600) % 24;
1445 mins = (total / 60) % 60;
1450 n = snprintf(p, ep - p, "%dd", days);
1451 if (n < 0 || n >= ep - p)
1455 if (!first || hours) {
1457 n = snprintf(p, ep - p, "%dh", hours);
1458 if (n < 0 || n >= ep - p)
1462 if (!first || mins) {
1464 n = snprintf(p, ep - p, "%dm", mins);
1465 if (n < 0 || n >= ep - p)
1469 snprintf(p, ep - p, "%ds", secs);
1475 * Print the timestamp
1476 * from tcpdump/util.c
1479 ts_print(const struct timeval *tvp)
1484 sec = (tvp->tv_sec + thiszone) % 86400;
1485 xo_emit("{:tv_sec/%lld}{:tv_usec/%lld}%02d:%02d:%02d.%06u ",
1486 tvp->tv_sec, tvp->tv_usec,
1487 sec / 3600, (sec % 3600) / 60, sec % 60, (u_int32_t)tvp->tv_usec);