1 /* $FreeBSD: src/usr.sbin/ndp/ndp.c,v 1.2.2.5 2001/08/13 02:58:26 sumikawa Exp $ */
2 /* $KAME: ndp.c,v 1.65 2001/05/08 04:36:34 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 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * "@(#) Copyright (c) 1984, 1993\n\
71 * The Regents of the University of California. All rights reserved.\n";
73 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
77 * ndp - display, set, delete and flush neighbor cache
81 #include <sys/param.h>
83 #include <sys/ioctl.h>
84 #include <sys/socket.h>
85 #include <sys/sysctl.h>
87 #include <sys/queue.h>
90 #include <net/if_var.h>
91 #include <net/if_dl.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
95 #include <netinet/in.h>
96 #include <netinet/if_ether.h>
98 #include <netinet/icmp6.h>
99 #include <netinet6/in6_var.h>
100 #include <netinet6/nd6.h>
102 #include <arpa/inet.h>
114 #include "gmt2local.h"
116 #ifndef NI_WITHSCOPEID
117 #define NI_WITHSCOPEID 0
120 /* packing rule for routing socket */
122 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
123 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
129 static int32_t thiszone; /* time difference with gmt */
131 static int repeat = 0;
133 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
134 char host_buf[NI_MAXHOST]; /* getnameinfo() */
135 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
137 int main __P((int, char **));
138 int file __P((char *));
139 void getsocket __P((void));
140 int set __P((int, char **));
141 void get __P((char *));
142 int delete __P((char *));
143 void dump __P((struct in6_addr *));
144 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
146 static char *ether_str __P((struct sockaddr_dl *));
147 int ndp_ether_aton __P((char *, u_char *));
148 void usage __P((void));
149 int rtmsg __P((int));
150 void ifinfo __P((int, char **));
151 void rtrlist __P((void));
152 void plist __P((void));
153 void pfx_flush __P((void));
154 void rtrlist __P((void));
155 void rtr_flush __P((void));
156 void harmonize_rtr __P((void));
157 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
158 static void getdefif __P((void));
159 static void setdefif __P((char *));
161 static char *sec2str __P((time_t t));
162 static char *ether_str __P((struct sockaddr_dl *sdl));
163 static void ts_print __P((const struct timeval *));
165 static char *rtpref_str[] = {
178 int aflag = 0, dflag = 0, sflag = 0, Hflag = 0,
179 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
182 thiszone = gmt2local(0);
183 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1)
195 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
198 getdefif(); /* always call it to print the result */
201 errx(1, "not supported yet");
223 /* obsolete, ignored */
236 repeat = atoi(optarg);
256 if (aflag || cflag) {
275 if (argc < 2 || argc > 4)
277 exit(set(argc, argv) ? 1 : 0);
299 * Process a file to set standard ndp entries
307 char line[100], arg[5][50], *args[5];
309 if ((fp = fopen(name, "r")) == NULL) {
310 fprintf(stderr, "ndp: cannot open %s\n", name);
313 args[0] = &arg[0][0];
314 args[1] = &arg[1][0];
315 args[2] = &arg[2][0];
316 args[3] = &arg[3][0];
317 args[4] = &arg[4][0];
319 while(fgets(line, 100, fp) != NULL) {
320 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
323 fprintf(stderr, "ndp: bad line: %s\n", line);
338 s = socket(PF_ROUTE, SOCK_RAW, 0);
340 perror("ndp: socket");
346 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
347 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
348 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
349 int expire_time, flags, found_entry;
351 struct rt_msghdr m_rtm;
356 * Set an individual neighbor cache entry
363 register struct sockaddr_in6 *sin = &sin_m;
364 register struct sockaddr_dl *sdl;
365 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
366 struct addrinfo hints, *res;
369 char *host = argv[0], *eaddr = argv[1];
377 bzero(&hints, sizeof(hints));
378 hints.ai_family = AF_INET6;
379 gai_error = getaddrinfo(host, NULL, &hints, &res);
381 fprintf(stderr, "ndp: %s: %s\n", host,
382 gai_strerror(gai_error));
385 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
387 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
388 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
389 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
392 ea = (u_char *)LLADDR(&sdl_m);
393 if (ndp_ether_aton(eaddr, ea) == 0)
395 flags = expire_time = 0;
397 if (strncmp(argv[0], "temp", 4) == 0) {
399 gettimeofday(&time, 0);
400 expire_time = time.tv_sec + 20 * 60;
401 } else if (strncmp(argv[0], "proxy", 5) == 0)
402 flags |= RTF_ANNOUNCE;
405 if (rtmsg(RTM_GET) < 0) {
409 sin = (struct sockaddr_in6 *)(rtm + 1);
410 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
411 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
412 if (sdl->sdl_family == AF_LINK &&
413 (rtm->rtm_flags & RTF_LLINFO) &&
414 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
415 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
416 case IFT_ISO88024: case IFT_ISO88025:
420 * IPv4 arp command retries with sin_other = SIN_PROXY here.
422 fprintf(stderr, "set: cannot configure a new entry\n");
427 if (sdl->sdl_family != AF_LINK) {
428 printf("cannot intuit interface index and type for %s\n", host);
431 sdl_m.sdl_type = sdl->sdl_type;
432 sdl_m.sdl_index = sdl->sdl_index;
433 return (rtmsg(RTM_ADD));
437 * Display an individual neighbor cache entry
443 struct sockaddr_in6 *sin = &sin_m;
444 struct addrinfo hints, *res;
448 bzero(&hints, sizeof(hints));
449 hints.ai_family = AF_INET6;
450 gai_error = getaddrinfo(host, NULL, &hints, &res);
452 fprintf(stderr, "ndp: %s: %s\n", host,
453 gai_strerror(gai_error));
456 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
458 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
459 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
460 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
463 dump(&sin->sin6_addr);
464 if (found_entry == 0) {
465 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
466 sizeof(host_buf), NULL ,0,
467 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
468 printf("%s (%s) -- no entry\n", host, host_buf);
474 * Delete a neighbor cache entry
480 struct sockaddr_in6 *sin = &sin_m;
481 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
482 struct sockaddr_dl *sdl;
483 struct addrinfo hints, *res;
489 bzero(&hints, sizeof(hints));
490 hints.ai_family = AF_INET6;
491 gai_error = getaddrinfo(host, NULL, &hints, &res);
493 fprintf(stderr, "ndp: %s: %s\n", host,
494 gai_strerror(gai_error));
497 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
499 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
500 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
501 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
504 if (rtmsg(RTM_GET) < 0) {
508 sin = (struct sockaddr_in6 *)(rtm + 1);
509 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
510 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
511 if (sdl->sdl_family == AF_LINK &&
512 (rtm->rtm_flags & RTF_LLINFO) &&
513 !(rtm->rtm_flags & RTF_GATEWAY)) {
517 * IPv4 arp command retries with sin_other = SIN_PROXY here.
519 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
524 if (sdl->sdl_family != AF_LINK) {
525 printf("cannot locate %s\n", host);
528 if (rtmsg(RTM_DELETE) == 0) {
529 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
532 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
533 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
534 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
537 getnameinfo((struct sockaddr *)&s6,
538 s6.sin6_len, host_buf,
539 sizeof(host_buf), NULL, 0,
540 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
541 printf("%s (%s) deleted\n", host, host_buf);
552 * Dump the entire neighbor cache
556 struct in6_addr *addr;
560 char *lim, *buf, *next;
561 struct rt_msghdr *rtm;
562 struct sockaddr_in6 *sin;
563 struct sockaddr_dl *sdl;
565 struct in6_nbrinfo *nbi;
574 if (!tflag && !cflag)
575 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
576 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
577 W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs");
584 mib[4] = NET_RT_FLAGS;
586 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
587 err(1, "sysctl(PF_ROUTE estimate)");
589 if ((buf = malloc(needed)) == NULL)
591 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
592 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
597 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
598 int isrouter = 0, prbs = 0;
600 rtm = (struct rt_msghdr *)next;
601 sin = (struct sockaddr_in6 *)(rtm + 1);
602 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
605 * Some OSes can produce a route that has the LINK flag but
606 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
607 * and BSD/OS, where xx is not the interface identifier on
608 * lo0). Such routes entry would annoy getnbrinfo() below,
610 * XXX: such routes should have the GATEWAY flag, not the
611 * LINK flag. However, there are rotten routing software
612 * that advertises all routes that have the GATEWAY flag.
613 * Thus, KAME kernel intentionally does not set the LINK flag.
614 * What is to be fixed is not ndp, but such routing software
615 * (and the kernel workaround)...
617 if (sdl->sdl_family != AF_LINK)
621 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
624 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
626 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
627 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
628 /* XXX: should scope id be filled in the kernel? */
629 if (sin->sin6_scope_id == 0)
630 sin->sin6_scope_id = sdl->sdl_index;
632 /* KAME specific hack; removed the embedded id */
633 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
636 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
637 sizeof(host_buf), NULL, 0,
638 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
641 if (rtm->rtm_flags & RTF_WASCLONED)
648 gettimeofday(&time, 0);
652 addrwidth = strlen(host_buf);
653 if (addrwidth < W_ADDR)
655 llwidth = strlen(ether_str(sdl));
656 if (W_ADDR + W_LL - addrwidth > llwidth)
657 llwidth = W_ADDR + W_LL - addrwidth;
658 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
661 ifwidth = strlen(ifname);
662 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
663 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
665 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
666 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
668 /* Print neighbor discovery specific informations */
669 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
671 if (nbi->expire > time.tv_sec) {
673 sec2str(nbi->expire - time.tv_sec));
674 } else if (nbi->expire == 0)
675 printf(" %-9.9s", "permanent");
677 printf(" %-9.9s", "expired");
680 case ND6_LLINFO_NOSTATE:
683 #ifdef ND6_LLINFO_WAITDELETE
684 case ND6_LLINFO_WAITDELETE:
688 case ND6_LLINFO_INCOMPLETE:
691 case ND6_LLINFO_REACHABLE:
694 case ND6_LLINFO_STALE:
697 case ND6_LLINFO_DELAY:
700 case ND6_LLINFO_PROBE:
708 isrouter = nbi->isrouter;
711 warnx("failed to get neighbor information");
717 * other flags. R: router, P: proxy, W: ??
719 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
720 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
722 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
724 sin = (struct sockaddr_in6 *)
725 (sdl->sdl_len + (char *)sdl);
726 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
728 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
730 (sin->sin6_len != sizeof(struct sockaddr_in6))
732 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
734 printf(" %-4.4s", flgbuf);
737 printf(" %4d", prbs);
751 static struct in6_nbrinfo *
752 getnbrinfo(addr, ifindex, warning)
753 struct in6_addr *addr;
757 static struct in6_nbrinfo nbi;
760 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
763 bzero(&nbi, sizeof(nbi));
764 if_indextoname(ifindex, nbi.ifname);
766 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
768 warn("ioctl(SIOCGNBRINFO_IN6)");
779 struct sockaddr_dl *sdl;
781 static char ebuf[32];
785 cp = (u_char *)LLADDR(sdl);
786 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
787 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
789 sprintf(ebuf, "(incomplete)");
802 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
803 &o[3], &o[4], &o[5]);
805 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
816 printf("usage: ndp hostname\n");
817 printf(" ndp -a[nt]\n");
818 printf(" ndp [-nt] -A wait\n");
819 printf(" ndp -c[nt]\n");
820 printf(" ndp -d[nt] hostname\n");
821 printf(" ndp -f[nt] filename\n");
822 printf(" ndp -i interface [flags...]\n");
823 #ifdef SIOCSDEFIFACE_IN6
824 printf(" ndp -I [interface|delete]\n");
828 printf(" ndp -s hostname ether_addr [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;
858 rtm->rtm_rmx.rmx_expire = expire_time;
859 rtm->rtm_inits = RTV_EXPIRE;
860 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
861 if (rtm->rtm_flags & RTF_ANNOUNCE) {
862 rtm->rtm_flags &= ~RTF_HOST;
863 rtm->rtm_flags |= RTA_NETMASK;
867 rtm->rtm_addrs |= RTA_DST;
869 #define NEXTADDR(w, s) \
870 if (rtm->rtm_addrs & (w)) { \
871 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
873 NEXTADDR(RTA_DST, sin_m);
874 NEXTADDR(RTA_GATEWAY, sdl_m);
875 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
876 NEXTADDR(RTA_NETMASK, so_mask);
878 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
881 rtm->rtm_seq = ++seq;
883 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
884 if (errno != ESRCH || cmd != RTM_DELETE) {
885 perror("writing to routing socket");
890 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
891 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
893 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
903 struct in6_ndireq nd;
905 char *ifname = argv[0];
907 #ifdef IPV6CTL_USETEMPADDR
911 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
912 perror("ndp: socket");
915 bzero(&nd, sizeof(nd));
916 strcpy(nd.ifname, ifname);
917 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
918 perror("ioctl (SIOCGIFINFO_IN6)");
923 for (i = 1; i < argc; i++) {
932 #define SETFLAG(s, f) \
934 if (strcmp(cp, (s)) == 0) {\
941 SETFLAG("nud", ND6_IFF_PERFORMNUD);
944 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
945 perror("ioctl(SIOCSIFINFO_FLAGS)");
951 printf("linkmtu=%d", ND.linkmtu);
952 printf(", curhlim=%d", ND.chlim);
953 printf(", basereachable=%ds%dms",
954 ND.basereachable / 1000, ND.basereachable % 1000);
955 printf(", reachable=%ds", ND.reachable);
956 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
957 #ifdef IPV6CTL_USETEMPADDR
958 memset(nullbuf, 0, sizeof(nullbuf));
959 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
963 for (i = 0; i < 3; i++) {
966 printf("\nRandom seed(0): ");
967 rbuf = ND.randomseed0;
970 printf("\nRandom seed(1): ");
971 rbuf = ND.randomseed1;
974 printf("\nRandom ID: ");
978 for (j = 0; j < 8; j++)
979 printf("%02x", rbuf[j]);
985 if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
986 printf("PERFORMNUD ");
994 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
995 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1001 #ifdef ICMPV6CTL_ND6_DRLIST
1002 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1004 struct in6_defrouter *p, *ep;
1006 struct timeval time;
1008 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1009 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1014 errx(1, "not enough core");
1017 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1018 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1022 ep = (struct in6_defrouter *)(buf + l);
1023 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1026 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1027 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1028 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)) != 0)
1029 strlcpy(host_buf, "?", sizeof(host_buf));
1031 printf("%s if=%s", host_buf,
1032 if_indextoname(p->if_index, ifix_buf));
1033 printf(", flags=%s%s",
1034 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1035 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1036 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1037 printf(", pref=%s", rtpref_str[rtpref]);
1039 gettimeofday(&time, 0);
1041 printf(", expire=Never\n");
1043 printf(", expire=%s\n",
1044 sec2str(p->expire - time.tv_sec));
1048 struct in6_drlist dr;
1050 struct timeval time;
1052 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1053 perror("ndp: socket");
1056 bzero(&dr, sizeof(dr));
1057 strcpy(dr.ifname, "lo0"); /* dummy */
1058 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1059 perror("ioctl (SIOCGDRLST_IN6)");
1062 #define DR dr.defrouter[i]
1063 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1064 struct sockaddr_in6 sin6;
1066 bzero(&sin6, sizeof(sin6));
1067 sin6.sin6_family = AF_INET6;
1068 sin6.sin6_len = sizeof(sin6);
1069 sin6.sin6_addr = DR.rtaddr;
1070 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1071 sizeof(host_buf), NULL, 0,
1072 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1074 printf("%s if=%s", host_buf,
1075 if_indextoname(DR.if_index, ifix_buf));
1076 printf(", flags=%s%s",
1077 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1078 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1079 gettimeofday(&time, 0);
1081 printf(", expire=Never\n");
1083 printf(", expire=%s\n",
1084 sec2str(DR.expire - time.tv_sec));
1094 #ifdef ICMPV6CTL_ND6_PRLIST
1095 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1097 struct in6_prefix *p, *ep, *n;
1098 struct sockaddr_in6 *advrtr;
1100 struct timeval time;
1101 #ifdef NI_WITHSCOPEID
1102 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
1103 int ninflags = (nflag ? NI_NUMERICHOST : 0) | NI_WITHSCOPEID;
1105 const int niflags = NI_NUMERICHOST;
1106 int ninflags = nflag ? NI_NUMERICHOST : 0;
1108 char namebuf[NI_MAXHOST];
1110 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1111 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1116 errx(1, "not enough core");
1119 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1120 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1124 ep = (struct in6_prefix *)(buf + l);
1125 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1126 advrtr = (struct sockaddr_in6 *)(p + 1);
1127 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1129 if (getnameinfo((struct sockaddr *)&p->prefix,
1130 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1131 NULL, 0, niflags) != 0)
1132 strlcpy(namebuf, "?", sizeof(namebuf));
1133 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1134 if_indextoname(p->if_index, ifix_buf));
1136 gettimeofday(&time, 0);
1138 * meaning of fields, especially flags, is very different
1139 * by origin. notify the difference to the users.
1141 printf("flags=%s%s%s%s%s",
1142 p->raflags.onlink ? "L" : "",
1143 p->raflags.autonomous ? "A" : "",
1144 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1145 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1147 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1152 if (p->vltime == ND6_INFINITE_LIFETIME)
1153 printf(" vltime=infinity");
1155 printf(" vltime=%ld", (long)p->vltime);
1156 if (p->pltime == ND6_INFINITE_LIFETIME)
1157 printf(", pltime=infinity");
1159 printf(", pltime=%ld", (long)p->pltime);
1161 printf(", expire=Never");
1162 else if (p->expire >= time.tv_sec)
1163 printf(", expire=%s",
1164 sec2str(p->expire - time.tv_sec));
1166 printf(", expired");
1167 printf(", ref=%d", p->refcnt);
1170 * "advertising router" list is meaningful only if the prefix
1171 * information is from RA.
1175 struct sockaddr_in6 *sin6;
1177 sin6 = (struct sockaddr_in6 *)(p + 1);
1178 printf(" advertised by\n");
1179 for (j = 0; j < p->advrtrs; j++) {
1180 struct in6_nbrinfo *nbi;
1182 if (getnameinfo((struct sockaddr *)sin6,
1183 sin6->sin6_len, namebuf, sizeof(namebuf),
1184 NULL, 0, ninflags) != 0)
1185 strlcpy(namebuf, "?", sizeof(namebuf));
1186 printf(" %s", namebuf);
1188 nbi = getnbrinfo(&sin6->sin6_addr, p->if_index,
1191 switch(nbi->state) {
1192 case ND6_LLINFO_REACHABLE:
1193 case ND6_LLINFO_STALE:
1194 case ND6_LLINFO_DELAY:
1195 case ND6_LLINFO_PROBE:
1196 printf(" (reachable)\n");
1199 printf(" (unreachable)\n");
1202 printf(" (no neighbor state)\n");
1206 printf(" No advertising router\n");
1210 struct in6_prlist pr;
1212 struct timeval time;
1214 gettimeofday(&time, 0);
1216 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1217 perror("ndp: socket");
1220 bzero(&pr, sizeof(pr));
1221 strcpy(pr.ifname, "lo0"); /* dummy */
1222 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1223 perror("ioctl (SIOCGPRLST_IN6)");
1226 #define PR pr.prefix[i]
1227 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1228 struct sockaddr_in6 p6;
1229 char namebuf[NI_MAXHOST];
1235 memset(&p6, 0, sizeof(p6));
1236 p6.sin6_family = AF_INET6;
1237 p6.sin6_len = sizeof(p6);
1238 p6.sin6_addr = PR.prefix;
1242 * copy link index to sin6_scope_id field.
1243 * XXX: KAME specific.
1245 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1248 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1250 linkid = ntohs(linkid);
1251 p6.sin6_scope_id = linkid;
1252 p6.sin6_addr.s6_addr[2] = 0;
1253 p6.sin6_addr.s6_addr[3] = 0;
1256 niflags = NI_NUMERICHOST;
1258 niflags |= NI_WITHSCOPEID;
1260 if (getnameinfo((struct sockaddr *)&p6,
1261 sizeof(p6), namebuf, sizeof(namebuf),
1262 NULL, 0, niflags)) {
1263 warnx("getnameinfo failed");
1266 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1267 if_indextoname(PR.if_index, ifix_buf));
1269 gettimeofday(&time, 0);
1271 * meaning of fields, especially flags, is very different
1272 * by origin. notify the difference to the users.
1276 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1279 printf("flags=%s%s%s%s%s",
1280 PR.raflags.onlink ? "L" : "",
1281 PR.raflags.autonomous ? "A" : "",
1282 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1283 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1285 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1291 printf("flags=%s%s",
1292 PR.raflags.onlink ? "L" : "",
1293 PR.raflags.autonomous ? "A" : "");
1295 if (PR.vltime == ND6_INFINITE_LIFETIME)
1296 printf(" vltime=infinity");
1298 printf(" vltime=%ld", (long)PR.vltime);
1299 if (PR.pltime == ND6_INFINITE_LIFETIME)
1300 printf(", pltime=infinity");
1302 printf(", pltime=%ld", (long)PR.pltime);
1304 printf(", expire=Never");
1305 else if (PR.expire >= time.tv_sec)
1306 printf(", expire=%s",
1307 sec2str(PR.expire - time.tv_sec));
1309 printf(", expired");
1311 printf(", ref=%d", PR.refcnt);
1314 switch (PR.origin) {
1316 printf(", origin=RA");
1319 printf(", origin=RR");
1321 case PR_ORIG_STATIC:
1322 printf(", origin=static");
1324 case PR_ORIG_KERNEL:
1325 printf(", origin=kernel");
1328 printf(", origin=?");
1334 * "advertising router" list is meaningful only if the prefix
1335 * information is from RA.
1337 if (0 && /* prefix origin is almost obsolted */
1338 PR.origin != PR_ORIG_RA)
1340 else if (PR.advrtrs) {
1342 printf(" advertised by\n");
1343 for (j = 0; j < PR.advrtrs; j++) {
1344 struct sockaddr_in6 sin6;
1345 struct in6_nbrinfo *nbi;
1347 bzero(&sin6, sizeof(sin6));
1348 sin6.sin6_family = AF_INET6;
1349 sin6.sin6_len = sizeof(sin6);
1350 sin6.sin6_addr = PR.advrtr[j];
1351 sin6.sin6_scope_id = PR.if_index; /* XXX */
1352 getnameinfo((struct sockaddr *)&sin6,
1353 sin6.sin6_len, host_buf,
1354 sizeof(host_buf), NULL, 0,
1355 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1356 printf(" %s", host_buf);
1358 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1361 switch(nbi->state) {
1362 case ND6_LLINFO_REACHABLE:
1363 case ND6_LLINFO_STALE:
1364 case ND6_LLINFO_DELAY:
1365 case ND6_LLINFO_PROBE:
1366 printf(" (reachable)\n");
1369 printf(" (unreachable)\n");
1372 printf(" (no neighbor state)\n");
1374 if (PR.advrtrs > DRLSTSIZ)
1375 printf(" and %d routers\n",
1376 PR.advrtrs - DRLSTSIZ);
1378 printf(" No advertising router\n");
1388 char dummyif[IFNAMSIZ+8];
1391 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1393 strcpy(dummyif, "lo0"); /* dummy */
1394 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1395 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1401 char dummyif[IFNAMSIZ+8];
1404 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1406 strcpy(dummyif, "lo0"); /* dummy */
1407 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1408 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1416 char dummyif[IFNAMSIZ+8];
1419 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1421 strcpy(dummyif, "lo0"); /* dummy */
1422 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1423 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1428 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1433 struct in6_ndifreq ndifreq;
1434 unsigned int ifindex;
1436 if (strcasecmp(ifname, "delete") == 0)
1439 if ((ifindex = if_nametoindex(ifname)) == 0)
1440 err(1, "failed to resolve i/f index for %s", ifname);
1443 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1446 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1447 ndifreq.ifindex = ifindex;
1449 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1450 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1458 struct in6_ndifreq ndifreq;
1459 char ifname[IFNAMSIZ+8];
1461 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1464 memset(&ndifreq, 0, sizeof(ndifreq));
1465 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1467 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1468 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1470 if (ndifreq.ifindex == 0)
1471 printf("No default interface.\n");
1473 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1474 err(1, "failed to resolve ifname for index %lu",
1476 printf("ND default interface = %s\n", ifname);
1487 static char result[256];
1488 int days, hours, mins, secs;
1492 days = total / 3600 / 24;
1493 hours = (total / 3600) % 24;
1494 mins = (total / 60) % 60;
1499 p += sprintf(p, "%dd", days);
1501 if (!first || hours) {
1503 p += sprintf(p, "%dh", hours);
1505 if (!first || mins) {
1507 p += sprintf(p, "%dm", mins);
1509 sprintf(p, "%ds", secs);
1515 * Print the timestamp
1516 * from tcpdump/util.c
1520 const struct timeval *tvp;
1525 s = (tvp->tv_sec + thiszone) % 86400;
1526 (void)printf("%02d:%02d:%02d.%06u ",
1527 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);