1 /* $FreeBSD: src/usr.sbin/ndp/ndp.c,v 1.2.2.5 2001/08/13 02:58:26 sumikawa Exp $ */
2 /* $DragonFly: src/usr.sbin/ndp/ndp.c,v 1.2 2003/06/17 04:29:57 dillon Exp $ */
3 /* $KAME: ndp.c,v 1.65 2001/05/08 04:36:34 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1984, 1993
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Sun Microsystems, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * "@(#) Copyright (c) 1984, 1993\n\
72 * The Regents of the University of California. All rights reserved.\n";
74 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
78 * ndp - display, set, delete and flush neighbor cache
82 #include <sys/param.h>
84 #include <sys/ioctl.h>
85 #include <sys/socket.h>
86 #include <sys/sysctl.h>
88 #include <sys/queue.h>
91 #include <net/if_var.h>
92 #include <net/if_dl.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/if_ether.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/in6_var.h>
101 #include <netinet6/nd6.h>
103 #include <arpa/inet.h>
115 #include "gmt2local.h"
117 #ifndef NI_WITHSCOPEID
118 #define NI_WITHSCOPEID 0
121 /* packing rule for routing socket */
123 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
124 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
130 static int32_t thiszone; /* time difference with gmt */
132 static int repeat = 0;
134 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
135 char host_buf[NI_MAXHOST]; /* getnameinfo() */
136 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
138 int main __P((int, char **));
139 int file __P((char *));
140 void getsocket __P((void));
141 int set __P((int, char **));
142 void get __P((char *));
143 int delete __P((char *));
144 void dump __P((struct in6_addr *));
145 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
147 static char *ether_str __P((struct sockaddr_dl *));
148 int ndp_ether_aton __P((char *, u_char *));
149 void usage __P((void));
150 int rtmsg __P((int));
151 void ifinfo __P((int, char **));
152 void rtrlist __P((void));
153 void plist __P((void));
154 void pfx_flush __P((void));
155 void rtrlist __P((void));
156 void rtr_flush __P((void));
157 void harmonize_rtr __P((void));
158 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
159 static void getdefif __P((void));
160 static void setdefif __P((char *));
162 static char *sec2str __P((time_t t));
163 static char *ether_str __P((struct sockaddr_dl *sdl));
164 static void ts_print __P((const struct timeval *));
166 static char *rtpref_str[] = {
179 int aflag = 0, dflag = 0, sflag = 0, Hflag = 0,
180 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
183 thiszone = gmt2local(0);
184 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1)
196 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
199 getdefif(); /* always call it to print the result */
202 errx(1, "not supported yet");
224 /* obsolete, ignored */
237 repeat = atoi(optarg);
257 if (aflag || cflag) {
276 if (argc < 2 || argc > 4)
278 exit(set(argc, argv) ? 1 : 0);
300 * Process a file to set standard ndp entries
308 char line[100], arg[5][50], *args[5];
310 if ((fp = fopen(name, "r")) == NULL) {
311 fprintf(stderr, "ndp: cannot open %s\n", name);
314 args[0] = &arg[0][0];
315 args[1] = &arg[1][0];
316 args[2] = &arg[2][0];
317 args[3] = &arg[3][0];
318 args[4] = &arg[4][0];
320 while(fgets(line, 100, fp) != NULL) {
321 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
324 fprintf(stderr, "ndp: bad line: %s\n", line);
339 s = socket(PF_ROUTE, SOCK_RAW, 0);
341 perror("ndp: socket");
347 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
348 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
349 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
350 int expire_time, flags, found_entry;
352 struct rt_msghdr m_rtm;
357 * Set an individual neighbor cache entry
364 register struct sockaddr_in6 *sin = &sin_m;
365 register struct sockaddr_dl *sdl;
366 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
367 struct addrinfo hints, *res;
370 char *host = argv[0], *eaddr = argv[1];
378 bzero(&hints, sizeof(hints));
379 hints.ai_family = AF_INET6;
380 gai_error = getaddrinfo(host, NULL, &hints, &res);
382 fprintf(stderr, "ndp: %s: %s\n", host,
383 gai_strerror(gai_error));
386 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
388 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
389 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
390 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
393 ea = (u_char *)LLADDR(&sdl_m);
394 if (ndp_ether_aton(eaddr, ea) == 0)
396 flags = expire_time = 0;
398 if (strncmp(argv[0], "temp", 4) == 0) {
400 gettimeofday(&time, 0);
401 expire_time = time.tv_sec + 20 * 60;
402 } else if (strncmp(argv[0], "proxy", 5) == 0)
403 flags |= RTF_ANNOUNCE;
406 if (rtmsg(RTM_GET) < 0) {
410 sin = (struct sockaddr_in6 *)(rtm + 1);
411 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
412 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
413 if (sdl->sdl_family == AF_LINK &&
414 (rtm->rtm_flags & RTF_LLINFO) &&
415 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
416 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
417 case IFT_ISO88024: case IFT_ISO88025:
421 * IPv4 arp command retries with sin_other = SIN_PROXY here.
423 fprintf(stderr, "set: cannot configure a new entry\n");
428 if (sdl->sdl_family != AF_LINK) {
429 printf("cannot intuit interface index and type for %s\n", host);
432 sdl_m.sdl_type = sdl->sdl_type;
433 sdl_m.sdl_index = sdl->sdl_index;
434 return (rtmsg(RTM_ADD));
438 * Display an individual neighbor cache entry
444 struct sockaddr_in6 *sin = &sin_m;
445 struct addrinfo hints, *res;
449 bzero(&hints, sizeof(hints));
450 hints.ai_family = AF_INET6;
451 gai_error = getaddrinfo(host, NULL, &hints, &res);
453 fprintf(stderr, "ndp: %s: %s\n", host,
454 gai_strerror(gai_error));
457 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
459 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
460 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
461 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
464 dump(&sin->sin6_addr);
465 if (found_entry == 0) {
466 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
467 sizeof(host_buf), NULL ,0,
468 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
469 printf("%s (%s) -- no entry\n", host, host_buf);
475 * Delete a neighbor cache entry
481 struct sockaddr_in6 *sin = &sin_m;
482 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
483 struct sockaddr_dl *sdl;
484 struct addrinfo hints, *res;
490 bzero(&hints, sizeof(hints));
491 hints.ai_family = AF_INET6;
492 gai_error = getaddrinfo(host, NULL, &hints, &res);
494 fprintf(stderr, "ndp: %s: %s\n", host,
495 gai_strerror(gai_error));
498 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
500 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
501 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
502 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
505 if (rtmsg(RTM_GET) < 0) {
509 sin = (struct sockaddr_in6 *)(rtm + 1);
510 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
511 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
512 if (sdl->sdl_family == AF_LINK &&
513 (rtm->rtm_flags & RTF_LLINFO) &&
514 !(rtm->rtm_flags & RTF_GATEWAY)) {
518 * IPv4 arp command retries with sin_other = SIN_PROXY here.
520 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
525 if (sdl->sdl_family != AF_LINK) {
526 printf("cannot locate %s\n", host);
529 if (rtmsg(RTM_DELETE) == 0) {
530 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
533 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
534 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
535 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
538 getnameinfo((struct sockaddr *)&s6,
539 s6.sin6_len, host_buf,
540 sizeof(host_buf), NULL, 0,
541 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
542 printf("%s (%s) deleted\n", host, host_buf);
553 * Dump the entire neighbor cache
557 struct in6_addr *addr;
561 char *lim, *buf, *next;
562 struct rt_msghdr *rtm;
563 struct sockaddr_in6 *sin;
564 struct sockaddr_dl *sdl;
566 struct in6_nbrinfo *nbi;
575 if (!tflag && !cflag)
576 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
577 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
578 W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs");
585 mib[4] = NET_RT_FLAGS;
587 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
588 err(1, "sysctl(PF_ROUTE estimate)");
590 if ((buf = malloc(needed)) == NULL)
592 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
593 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
598 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
599 int isrouter = 0, prbs = 0;
601 rtm = (struct rt_msghdr *)next;
602 sin = (struct sockaddr_in6 *)(rtm + 1);
603 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
606 * Some OSes can produce a route that has the LINK flag but
607 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
608 * and BSD/OS, where xx is not the interface identifier on
609 * lo0). Such routes entry would annoy getnbrinfo() below,
611 * XXX: such routes should have the GATEWAY flag, not the
612 * LINK flag. However, there are rotten routing software
613 * that advertises all routes that have the GATEWAY flag.
614 * Thus, KAME kernel intentionally does not set the LINK flag.
615 * What is to be fixed is not ndp, but such routing software
616 * (and the kernel workaround)...
618 if (sdl->sdl_family != AF_LINK)
622 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
625 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
627 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
628 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
629 /* XXX: should scope id be filled in the kernel? */
630 if (sin->sin6_scope_id == 0)
631 sin->sin6_scope_id = sdl->sdl_index;
633 /* KAME specific hack; removed the embedded id */
634 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
637 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
638 sizeof(host_buf), NULL, 0,
639 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
642 if (rtm->rtm_flags & RTF_WASCLONED)
649 gettimeofday(&time, 0);
653 addrwidth = strlen(host_buf);
654 if (addrwidth < W_ADDR)
656 llwidth = strlen(ether_str(sdl));
657 if (W_ADDR + W_LL - addrwidth > llwidth)
658 llwidth = W_ADDR + W_LL - addrwidth;
659 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
662 ifwidth = strlen(ifname);
663 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
664 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
666 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
667 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
669 /* Print neighbor discovery specific informations */
670 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
672 if (nbi->expire > time.tv_sec) {
674 sec2str(nbi->expire - time.tv_sec));
675 } else if (nbi->expire == 0)
676 printf(" %-9.9s", "permanent");
678 printf(" %-9.9s", "expired");
681 case ND6_LLINFO_NOSTATE:
684 #ifdef ND6_LLINFO_WAITDELETE
685 case ND6_LLINFO_WAITDELETE:
689 case ND6_LLINFO_INCOMPLETE:
692 case ND6_LLINFO_REACHABLE:
695 case ND6_LLINFO_STALE:
698 case ND6_LLINFO_DELAY:
701 case ND6_LLINFO_PROBE:
709 isrouter = nbi->isrouter;
712 warnx("failed to get neighbor information");
718 * other flags. R: router, P: proxy, W: ??
720 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
721 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
723 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
725 sin = (struct sockaddr_in6 *)
726 (sdl->sdl_len + (char *)sdl);
727 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
729 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
731 (sin->sin6_len != sizeof(struct sockaddr_in6))
733 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
735 printf(" %-4.4s", flgbuf);
738 printf(" %4d", prbs);
752 static struct in6_nbrinfo *
753 getnbrinfo(addr, ifindex, warning)
754 struct in6_addr *addr;
758 static struct in6_nbrinfo nbi;
761 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
764 bzero(&nbi, sizeof(nbi));
765 if_indextoname(ifindex, nbi.ifname);
767 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
769 warn("ioctl(SIOCGNBRINFO_IN6)");
780 struct sockaddr_dl *sdl;
782 static char ebuf[32];
786 cp = (u_char *)LLADDR(sdl);
787 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
788 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
790 sprintf(ebuf, "(incomplete)");
803 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
804 &o[3], &o[4], &o[5]);
806 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
817 printf("usage: ndp hostname\n");
818 printf(" ndp -a[nt]\n");
819 printf(" ndp [-nt] -A wait\n");
820 printf(" ndp -c[nt]\n");
821 printf(" ndp -d[nt] hostname\n");
822 printf(" ndp -f[nt] filename\n");
823 printf(" ndp -i interface [flags...]\n");
824 #ifdef SIOCSDEFIFACE_IN6
825 printf(" ndp -I [interface|delete]\n");
829 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
842 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
843 register char *cp = m_rtmsg.m_space;
847 if (cmd == RTM_DELETE)
849 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
850 rtm->rtm_flags = flags;
851 rtm->rtm_version = RTM_VERSION;
855 fprintf(stderr, "ndp: internal wrong cmd\n");
858 rtm->rtm_addrs |= RTA_GATEWAY;
859 rtm->rtm_rmx.rmx_expire = expire_time;
860 rtm->rtm_inits = RTV_EXPIRE;
861 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
862 if (rtm->rtm_flags & RTF_ANNOUNCE) {
863 rtm->rtm_flags &= ~RTF_HOST;
864 rtm->rtm_flags |= RTA_NETMASK;
868 rtm->rtm_addrs |= RTA_DST;
870 #define NEXTADDR(w, s) \
871 if (rtm->rtm_addrs & (w)) { \
872 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
874 NEXTADDR(RTA_DST, sin_m);
875 NEXTADDR(RTA_GATEWAY, sdl_m);
876 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
877 NEXTADDR(RTA_NETMASK, so_mask);
879 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
882 rtm->rtm_seq = ++seq;
884 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
885 if (errno != ESRCH || cmd != RTM_DELETE) {
886 perror("writing to routing socket");
891 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
892 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
894 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
904 struct in6_ndireq nd;
906 char *ifname = argv[0];
908 #ifdef IPV6CTL_USETEMPADDR
912 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
913 perror("ndp: socket");
916 bzero(&nd, sizeof(nd));
917 strcpy(nd.ifname, ifname);
918 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
919 perror("ioctl (SIOCGIFINFO_IN6)");
924 for (i = 1; i < argc; i++) {
933 #define SETFLAG(s, f) \
935 if (strcmp(cp, (s)) == 0) {\
942 SETFLAG("nud", ND6_IFF_PERFORMNUD);
945 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
946 perror("ioctl(SIOCSIFINFO_FLAGS)");
952 printf("linkmtu=%d", ND.linkmtu);
953 printf(", curhlim=%d", ND.chlim);
954 printf(", basereachable=%ds%dms",
955 ND.basereachable / 1000, ND.basereachable % 1000);
956 printf(", reachable=%ds", ND.reachable);
957 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
958 #ifdef IPV6CTL_USETEMPADDR
959 memset(nullbuf, 0, sizeof(nullbuf));
960 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
964 for (i = 0; i < 3; i++) {
967 printf("\nRandom seed(0): ");
968 rbuf = ND.randomseed0;
971 printf("\nRandom seed(1): ");
972 rbuf = ND.randomseed1;
975 printf("\nRandom ID: ");
979 for (j = 0; j < 8; j++)
980 printf("%02x", rbuf[j]);
986 if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
987 printf("PERFORMNUD ");
995 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
996 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1002 #ifdef ICMPV6CTL_ND6_DRLIST
1003 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1005 struct in6_defrouter *p, *ep;
1007 struct timeval time;
1009 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1010 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1015 errx(1, "not enough core");
1018 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1019 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1023 ep = (struct in6_defrouter *)(buf + l);
1024 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1027 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1028 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1029 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)) != 0)
1030 strlcpy(host_buf, "?", sizeof(host_buf));
1032 printf("%s if=%s", host_buf,
1033 if_indextoname(p->if_index, ifix_buf));
1034 printf(", flags=%s%s",
1035 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1036 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1037 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1038 printf(", pref=%s", rtpref_str[rtpref]);
1040 gettimeofday(&time, 0);
1042 printf(", expire=Never\n");
1044 printf(", expire=%s\n",
1045 sec2str(p->expire - time.tv_sec));
1049 struct in6_drlist dr;
1051 struct timeval time;
1053 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1054 perror("ndp: socket");
1057 bzero(&dr, sizeof(dr));
1058 strcpy(dr.ifname, "lo0"); /* dummy */
1059 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1060 perror("ioctl (SIOCGDRLST_IN6)");
1063 #define DR dr.defrouter[i]
1064 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1065 struct sockaddr_in6 sin6;
1067 bzero(&sin6, sizeof(sin6));
1068 sin6.sin6_family = AF_INET6;
1069 sin6.sin6_len = sizeof(sin6);
1070 sin6.sin6_addr = DR.rtaddr;
1071 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1072 sizeof(host_buf), NULL, 0,
1073 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1075 printf("%s if=%s", host_buf,
1076 if_indextoname(DR.if_index, ifix_buf));
1077 printf(", flags=%s%s",
1078 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1079 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1080 gettimeofday(&time, 0);
1082 printf(", expire=Never\n");
1084 printf(", expire=%s\n",
1085 sec2str(DR.expire - time.tv_sec));
1095 #ifdef ICMPV6CTL_ND6_PRLIST
1096 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1098 struct in6_prefix *p, *ep, *n;
1099 struct sockaddr_in6 *advrtr;
1101 struct timeval time;
1102 #ifdef NI_WITHSCOPEID
1103 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
1104 int ninflags = (nflag ? NI_NUMERICHOST : 0) | NI_WITHSCOPEID;
1106 const int niflags = NI_NUMERICHOST;
1107 int ninflags = nflag ? NI_NUMERICHOST : 0;
1109 char namebuf[NI_MAXHOST];
1111 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1112 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1117 errx(1, "not enough core");
1120 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1121 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1125 ep = (struct in6_prefix *)(buf + l);
1126 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1127 advrtr = (struct sockaddr_in6 *)(p + 1);
1128 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1130 if (getnameinfo((struct sockaddr *)&p->prefix,
1131 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1132 NULL, 0, niflags) != 0)
1133 strlcpy(namebuf, "?", sizeof(namebuf));
1134 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1135 if_indextoname(p->if_index, ifix_buf));
1137 gettimeofday(&time, 0);
1139 * meaning of fields, especially flags, is very different
1140 * by origin. notify the difference to the users.
1142 printf("flags=%s%s%s%s%s",
1143 p->raflags.onlink ? "L" : "",
1144 p->raflags.autonomous ? "A" : "",
1145 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1146 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1148 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1153 if (p->vltime == ND6_INFINITE_LIFETIME)
1154 printf(" vltime=infinity");
1156 printf(" vltime=%ld", (long)p->vltime);
1157 if (p->pltime == ND6_INFINITE_LIFETIME)
1158 printf(", pltime=infinity");
1160 printf(", pltime=%ld", (long)p->pltime);
1162 printf(", expire=Never");
1163 else if (p->expire >= time.tv_sec)
1164 printf(", expire=%s",
1165 sec2str(p->expire - time.tv_sec));
1167 printf(", expired");
1168 printf(", ref=%d", p->refcnt);
1171 * "advertising router" list is meaningful only if the prefix
1172 * information is from RA.
1176 struct sockaddr_in6 *sin6;
1178 sin6 = (struct sockaddr_in6 *)(p + 1);
1179 printf(" advertised by\n");
1180 for (j = 0; j < p->advrtrs; j++) {
1181 struct in6_nbrinfo *nbi;
1183 if (getnameinfo((struct sockaddr *)sin6,
1184 sin6->sin6_len, namebuf, sizeof(namebuf),
1185 NULL, 0, ninflags) != 0)
1186 strlcpy(namebuf, "?", sizeof(namebuf));
1187 printf(" %s", namebuf);
1189 nbi = getnbrinfo(&sin6->sin6_addr, p->if_index,
1192 switch(nbi->state) {
1193 case ND6_LLINFO_REACHABLE:
1194 case ND6_LLINFO_STALE:
1195 case ND6_LLINFO_DELAY:
1196 case ND6_LLINFO_PROBE:
1197 printf(" (reachable)\n");
1200 printf(" (unreachable)\n");
1203 printf(" (no neighbor state)\n");
1207 printf(" No advertising router\n");
1211 struct in6_prlist pr;
1213 struct timeval time;
1215 gettimeofday(&time, 0);
1217 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1218 perror("ndp: socket");
1221 bzero(&pr, sizeof(pr));
1222 strcpy(pr.ifname, "lo0"); /* dummy */
1223 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1224 perror("ioctl (SIOCGPRLST_IN6)");
1227 #define PR pr.prefix[i]
1228 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1229 struct sockaddr_in6 p6;
1230 char namebuf[NI_MAXHOST];
1236 memset(&p6, 0, sizeof(p6));
1237 p6.sin6_family = AF_INET6;
1238 p6.sin6_len = sizeof(p6);
1239 p6.sin6_addr = PR.prefix;
1243 * copy link index to sin6_scope_id field.
1244 * XXX: KAME specific.
1246 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1249 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1251 linkid = ntohs(linkid);
1252 p6.sin6_scope_id = linkid;
1253 p6.sin6_addr.s6_addr[2] = 0;
1254 p6.sin6_addr.s6_addr[3] = 0;
1257 niflags = NI_NUMERICHOST;
1259 niflags |= NI_WITHSCOPEID;
1261 if (getnameinfo((struct sockaddr *)&p6,
1262 sizeof(p6), namebuf, sizeof(namebuf),
1263 NULL, 0, niflags)) {
1264 warnx("getnameinfo failed");
1267 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1268 if_indextoname(PR.if_index, ifix_buf));
1270 gettimeofday(&time, 0);
1272 * meaning of fields, especially flags, is very different
1273 * by origin. notify the difference to the users.
1277 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1280 printf("flags=%s%s%s%s%s",
1281 PR.raflags.onlink ? "L" : "",
1282 PR.raflags.autonomous ? "A" : "",
1283 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1284 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1286 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1292 printf("flags=%s%s",
1293 PR.raflags.onlink ? "L" : "",
1294 PR.raflags.autonomous ? "A" : "");
1296 if (PR.vltime == ND6_INFINITE_LIFETIME)
1297 printf(" vltime=infinity");
1299 printf(" vltime=%ld", (long)PR.vltime);
1300 if (PR.pltime == ND6_INFINITE_LIFETIME)
1301 printf(", pltime=infinity");
1303 printf(", pltime=%ld", (long)PR.pltime);
1305 printf(", expire=Never");
1306 else if (PR.expire >= time.tv_sec)
1307 printf(", expire=%s",
1308 sec2str(PR.expire - time.tv_sec));
1310 printf(", expired");
1312 printf(", ref=%d", PR.refcnt);
1315 switch (PR.origin) {
1317 printf(", origin=RA");
1320 printf(", origin=RR");
1322 case PR_ORIG_STATIC:
1323 printf(", origin=static");
1325 case PR_ORIG_KERNEL:
1326 printf(", origin=kernel");
1329 printf(", origin=?");
1335 * "advertising router" list is meaningful only if the prefix
1336 * information is from RA.
1338 if (0 && /* prefix origin is almost obsolted */
1339 PR.origin != PR_ORIG_RA)
1341 else if (PR.advrtrs) {
1343 printf(" advertised by\n");
1344 for (j = 0; j < PR.advrtrs; j++) {
1345 struct sockaddr_in6 sin6;
1346 struct in6_nbrinfo *nbi;
1348 bzero(&sin6, sizeof(sin6));
1349 sin6.sin6_family = AF_INET6;
1350 sin6.sin6_len = sizeof(sin6);
1351 sin6.sin6_addr = PR.advrtr[j];
1352 sin6.sin6_scope_id = PR.if_index; /* XXX */
1353 getnameinfo((struct sockaddr *)&sin6,
1354 sin6.sin6_len, host_buf,
1355 sizeof(host_buf), NULL, 0,
1356 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1357 printf(" %s", host_buf);
1359 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1362 switch(nbi->state) {
1363 case ND6_LLINFO_REACHABLE:
1364 case ND6_LLINFO_STALE:
1365 case ND6_LLINFO_DELAY:
1366 case ND6_LLINFO_PROBE:
1367 printf(" (reachable)\n");
1370 printf(" (unreachable)\n");
1373 printf(" (no neighbor state)\n");
1375 if (PR.advrtrs > DRLSTSIZ)
1376 printf(" and %d routers\n",
1377 PR.advrtrs - DRLSTSIZ);
1379 printf(" No advertising router\n");
1389 char dummyif[IFNAMSIZ+8];
1392 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1394 strcpy(dummyif, "lo0"); /* dummy */
1395 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1396 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1402 char dummyif[IFNAMSIZ+8];
1405 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1407 strcpy(dummyif, "lo0"); /* dummy */
1408 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1409 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1417 char dummyif[IFNAMSIZ+8];
1420 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1422 strcpy(dummyif, "lo0"); /* dummy */
1423 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1424 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1429 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1434 struct in6_ndifreq ndifreq;
1435 unsigned int ifindex;
1437 if (strcasecmp(ifname, "delete") == 0)
1440 if ((ifindex = if_nametoindex(ifname)) == 0)
1441 err(1, "failed to resolve i/f index for %s", ifname);
1444 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1447 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1448 ndifreq.ifindex = ifindex;
1450 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1451 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1459 struct in6_ndifreq ndifreq;
1460 char ifname[IFNAMSIZ+8];
1462 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1465 memset(&ndifreq, 0, sizeof(ndifreq));
1466 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1468 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1469 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1471 if (ndifreq.ifindex == 0)
1472 printf("No default interface.\n");
1474 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1475 err(1, "failed to resolve ifname for index %lu",
1477 printf("ND default interface = %s\n", ifname);
1488 static char result[256];
1489 int days, hours, mins, secs;
1493 days = total / 3600 / 24;
1494 hours = (total / 3600) % 24;
1495 mins = (total / 60) % 60;
1500 p += sprintf(p, "%dd", days);
1502 if (!first || hours) {
1504 p += sprintf(p, "%dh", hours);
1506 if (!first || mins) {
1508 p += sprintf(p, "%dm", mins);
1510 sprintf(p, "%ds", secs);
1516 * Print the timestamp
1517 * from tcpdump/util.c
1521 const struct timeval *tvp;
1526 s = (tvp->tv_sec + thiszone) % 86400;
1527 (void)printf("%02d:%02d:%02d.%06u ",
1528 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);