1 /* $FreeBSD: src/usr.sbin/ndp/ndp.c,v 1.2.2.6 2003/08/12 16:27:57 ume Exp $ */
2 /* $DragonFly: src/usr.sbin/ndp/ndp.c,v 1.6 2004/12/30 02:35:24 hsu 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. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * "@(#) Copyright (c) 1984, 1993\n\
68 * The Regents of the University of California. All rights reserved.\n";
70 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
74 * ndp - display, set, delete and flush neighbor cache
78 #include <sys/param.h>
80 #include <sys/ioctl.h>
81 #include <sys/socket.h>
82 #include <sys/sysctl.h>
84 #include <sys/queue.h>
87 #include <net/if_var.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>
111 #include "gmt2local.h"
113 #ifndef NI_WITHSCOPEID
114 #define NI_WITHSCOPEID 0
117 /* packing rule for routing socket */
119 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
120 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
126 static int32_t thiszone; /* time difference with gmt */
128 static int repeat = 0;
130 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
131 char host_buf[NI_MAXHOST]; /* getnameinfo() */
132 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
134 int main(int, char **);
136 void getsocket(void);
137 int set(int, char **);
140 void dump(struct in6_addr *);
141 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *addr,
143 static char *ether_str(struct sockaddr_dl *);
144 int ndp_ether_aton(char *, u_char *);
147 void ifinfo(int, char **);
150 void pfx_flush(void);
152 void rtr_flush(void);
153 void harmonize_rtr(void);
154 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
155 static void getdefif(void);
156 static void setdefif(char *);
158 static char *sec2str(time_t t);
159 static char *ether_str(struct sockaddr_dl *sdl);
160 static void ts_print(const struct timeval *);
162 static char *rtpref_str[] = {
170 main(int argc, char **argv)
173 int aflag = 0, dflag = 0, sflag = 0, Hflag = 0,
174 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
177 thiszone = gmt2local(0);
178 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1)
190 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
193 getdefif(); /* always call it to print the result */
196 errx(1, "not supported yet");
218 /* obsolete, ignored */
231 repeat = atoi(optarg);
251 if (aflag || cflag) {
270 if (argc < 2 || argc > 4)
272 exit(set(argc, argv) ? 1 : 0);
294 * Process a file to set standard ndp entries
301 char line[100], arg[5][50], *args[5];
303 if ((fp = fopen(name, "r")) == NULL) {
304 fprintf(stderr, "ndp: cannot open %s\n", name);
307 args[0] = &arg[0][0];
308 args[1] = &arg[1][0];
309 args[2] = &arg[2][0];
310 args[3] = &arg[3][0];
311 args[4] = &arg[4][0];
313 while(fgets(line, 100, fp) != NULL) {
314 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
317 fprintf(stderr, "ndp: bad line: %s\n", line);
332 s = socket(PF_ROUTE, SOCK_RAW, 0);
334 perror("ndp: socket");
340 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
341 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
342 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
343 int flags, found_entry;
346 struct rt_msghdr m_rtm;
351 * Set an individual neighbor cache entry
354 set(int argc, char **argv)
356 struct sockaddr_in6 *sin = &sin_m;
357 struct sockaddr_dl *sdl;
358 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
359 struct addrinfo hints, *res;
362 char *host = argv[0], *eaddr = argv[1];
370 bzero(&hints, sizeof(hints));
371 hints.ai_family = AF_INET6;
372 gai_error = getaddrinfo(host, NULL, &hints, &res);
374 fprintf(stderr, "ndp: %s: %s\n", host,
375 gai_strerror(gai_error));
378 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
380 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
381 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
382 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
385 ea = (u_char *)LLADDR(&sdl_m);
386 if (ndp_ether_aton(eaddr, ea) == 0)
391 if (strncmp(argv[0], "temp", 4) == 0) {
394 clock_gettime(CLOCK_MONOTONIC, &sp);
395 expire_time = sp.tv_sec + 20 * 60;
396 } else if (strncmp(argv[0], "proxy", 5) == 0)
397 flags |= RTF_ANNOUNCE;
400 if (rtmsg(RTM_GET) < 0) {
404 sin = (struct sockaddr_in6 *)(rtm + 1);
405 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
406 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
407 if (sdl->sdl_family == AF_LINK &&
408 (rtm->rtm_flags & RTF_LLINFO) &&
409 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
410 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
411 case IFT_ISO88024: case IFT_ISO88025:
415 * IPv4 arp command retries with sin_other = SIN_PROXY here.
417 fprintf(stderr, "set: cannot configure a new entry\n");
422 if (sdl->sdl_family != AF_LINK) {
423 printf("cannot intuit interface index and type for %s\n", host);
426 sdl_m.sdl_type = sdl->sdl_type;
427 sdl_m.sdl_index = sdl->sdl_index;
428 return (rtmsg(RTM_ADD));
432 * Display an individual neighbor cache entry
437 struct sockaddr_in6 *sin = &sin_m;
438 struct addrinfo hints, *res;
442 bzero(&hints, sizeof(hints));
443 hints.ai_family = AF_INET6;
444 gai_error = getaddrinfo(host, NULL, &hints, &res);
446 fprintf(stderr, "ndp: %s: %s\n", host,
447 gai_strerror(gai_error));
450 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
452 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
453 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
454 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
457 dump(&sin->sin6_addr);
458 if (found_entry == 0) {
459 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
460 sizeof(host_buf), NULL ,0,
461 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
462 printf("%s (%s) -- no entry\n", host, host_buf);
468 * Delete a neighbor cache entry
473 struct sockaddr_in6 *sin = &sin_m;
474 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
475 struct sockaddr_dl *sdl;
476 struct addrinfo hints, *res;
482 bzero(&hints, sizeof(hints));
483 hints.ai_family = AF_INET6;
484 gai_error = getaddrinfo(host, NULL, &hints, &res);
486 fprintf(stderr, "ndp: %s: %s\n", host,
487 gai_strerror(gai_error));
490 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
492 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
493 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
494 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
497 if (rtmsg(RTM_GET) < 0) {
501 sin = (struct sockaddr_in6 *)(rtm + 1);
502 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
503 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
504 if (sdl->sdl_family == AF_LINK &&
505 (rtm->rtm_flags & RTF_LLINFO) &&
506 !(rtm->rtm_flags & RTF_GATEWAY)) {
510 * IPv4 arp command retries with sin_other = SIN_PROXY here.
512 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
517 if (sdl->sdl_family != AF_LINK) {
518 printf("cannot locate %s\n", host);
521 if (rtmsg(RTM_DELETE) == 0) {
522 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
525 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
526 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
527 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
530 getnameinfo((struct sockaddr *)&s6,
531 s6.sin6_len, host_buf,
532 sizeof(host_buf), NULL, 0,
533 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
534 printf("%s (%s) deleted\n", host, host_buf);
545 * Dump the entire neighbor cache
548 dump(struct in6_addr *addr)
552 char *lim, *buf, *next;
553 struct rt_msghdr *rtm;
554 struct sockaddr_in6 *sin;
555 struct sockaddr_dl *sdl;
557 struct in6_nbrinfo *nbi;
566 if (!tflag && !cflag)
567 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
568 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
569 W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs");
576 mib[4] = NET_RT_FLAGS;
578 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
579 err(1, "sysctl(PF_ROUTE estimate)");
581 if ((buf = malloc(needed)) == NULL)
583 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
584 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
589 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
590 int isrouter = 0, prbs = 0;
592 rtm = (struct rt_msghdr *)next;
593 sin = (struct sockaddr_in6 *)(rtm + 1);
594 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
597 * Some OSes can produce a route that has the LINK flag but
598 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
599 * and BSD/OS, where xx is not the interface identifier on
600 * lo0). Such routes entry would annoy getnbrinfo() below,
602 * XXX: such routes should have the GATEWAY flag, not the
603 * LINK flag. However, there are rotten routing software
604 * that advertises all routes that have the GATEWAY flag.
605 * Thus, KAME kernel intentionally does not set the LINK flag.
606 * What is to be fixed is not ndp, but such routing software
607 * (and the kernel workaround)...
609 if (sdl->sdl_family != AF_LINK)
613 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
616 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
618 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
619 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
620 /* XXX: should scope id be filled in the kernel? */
621 if (sin->sin6_scope_id == 0)
622 sin->sin6_scope_id = sdl->sdl_index;
624 /* KAME specific hack; removed the embedded id */
625 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
628 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
629 sizeof(host_buf), NULL, 0,
630 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
633 if (rtm->rtm_flags & RTF_WASCLONED)
640 gettimeofday(&time, 0);
644 addrwidth = strlen(host_buf);
645 if (addrwidth < W_ADDR)
647 llwidth = strlen(ether_str(sdl));
648 if (W_ADDR + W_LL - addrwidth > llwidth)
649 llwidth = W_ADDR + W_LL - addrwidth;
650 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
653 ifwidth = strlen(ifname);
654 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
655 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
657 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
658 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
660 /* Print neighbor discovery specific informations */
661 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
663 if (nbi->expire > time.tv_sec) {
665 sec2str(nbi->expire - time.tv_sec));
666 } else if (nbi->expire == 0)
667 printf(" %-9.9s", "permanent");
669 printf(" %-9.9s", "expired");
672 case ND6_LLINFO_NOSTATE:
675 #ifdef ND6_LLINFO_WAITDELETE
676 case ND6_LLINFO_WAITDELETE:
680 case ND6_LLINFO_INCOMPLETE:
683 case ND6_LLINFO_REACHABLE:
686 case ND6_LLINFO_STALE:
689 case ND6_LLINFO_DELAY:
692 case ND6_LLINFO_PROBE:
700 isrouter = nbi->isrouter;
703 warnx("failed to get neighbor information");
709 * other flags. R: router, P: proxy, W: ??
711 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
712 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
714 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
716 sin = (struct sockaddr_in6 *)
717 (sdl->sdl_len + (char *)sdl);
718 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
720 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
722 (sin->sin6_len != sizeof(struct sockaddr_in6))
724 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
726 printf(" %-4.4s", flgbuf);
729 printf(" %4d", prbs);
743 static struct in6_nbrinfo *
744 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
746 static struct in6_nbrinfo nbi;
749 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
752 bzero(&nbi, sizeof(nbi));
753 if_indextoname(ifindex, nbi.ifname);
755 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
757 warn("ioctl(SIOCGNBRINFO_IN6)");
767 ether_str(struct sockaddr_dl *sdl)
769 static char ebuf[32];
773 cp = (u_char *)LLADDR(sdl);
774 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
775 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
777 sprintf(ebuf, "(incomplete)");
784 ndp_ether_aton(char *a, u_char *n)
788 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
789 &o[3], &o[4], &o[5]);
791 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
802 printf("usage: ndp hostname\n");
803 printf(" ndp -a[nt]\n");
804 printf(" ndp [-nt] -A wait\n");
805 printf(" ndp -c[nt]\n");
806 printf(" ndp -d[nt] hostname\n");
807 printf(" ndp -f[nt] filename\n");
808 printf(" ndp -i interface [flags...]\n");
809 #ifdef SIOCSDEFIFACE_IN6
810 printf(" ndp -I [interface|delete]\n");
814 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
826 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
827 char *cp = m_rtmsg.m_space;
831 if (cmd == RTM_DELETE)
833 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
834 rtm->rtm_flags = flags;
835 rtm->rtm_version = RTM_VERSION;
839 fprintf(stderr, "ndp: internal wrong cmd\n");
842 rtm->rtm_addrs |= RTA_GATEWAY;
843 rtm->rtm_rmx.rmx_expire = expire_time;
844 rtm->rtm_inits = RTV_EXPIRE;
845 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
846 if (rtm->rtm_flags & RTF_ANNOUNCE) {
847 rtm->rtm_flags &= ~RTF_HOST;
848 rtm->rtm_flags |= RTA_NETMASK;
852 rtm->rtm_addrs |= RTA_DST;
854 #define NEXTADDR(w, s) \
855 if (rtm->rtm_addrs & (w)) { \
856 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
858 NEXTADDR(RTA_DST, sin_m);
859 NEXTADDR(RTA_GATEWAY, sdl_m);
860 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
861 NEXTADDR(RTA_NETMASK, so_mask);
863 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
866 rtm->rtm_seq = ++seq;
868 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
869 if (errno != ESRCH || cmd != RTM_DELETE) {
870 perror("writing to routing socket");
875 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
876 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
878 fprintf(stderr, "ndp: read from routing socket: %s\n",
884 ifinfo(int argc, char **argv)
886 struct in6_ndireq nd;
888 char *ifname = argv[0];
890 #ifdef IPV6CTL_USETEMPADDR
894 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
895 perror("ndp: socket");
898 bzero(&nd, sizeof(nd));
899 strcpy(nd.ifname, ifname);
900 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
901 perror("ioctl (SIOCGIFINFO_IN6)");
906 for (i = 1; i < argc; i++) {
915 #define SETFLAG(s, f) \
917 if (strcmp(cp, (s)) == 0) {\
924 SETFLAG("nud", ND6_IFF_PERFORMNUD);
925 #ifdef ND6_IFF_ACCEPT_RTADV
926 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
929 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
930 perror("ioctl(SIOCSIFINFO_FLAGS)");
936 printf("linkmtu=%d", ND.linkmtu);
937 printf(", curhlim=%d", ND.chlim);
938 printf(", basereachable=%ds%dms",
939 ND.basereachable / 1000, ND.basereachable % 1000);
940 printf(", reachable=%ds", ND.reachable);
941 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
942 #ifdef IPV6CTL_USETEMPADDR
943 memset(nullbuf, 0, sizeof(nullbuf));
944 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
948 for (i = 0; i < 3; i++) {
951 printf("\nRandom seed(0): ");
952 rbuf = ND.randomseed0;
955 printf("\nRandom seed(1): ");
956 rbuf = ND.randomseed1;
959 printf("\nRandom ID: ");
963 for (j = 0; j < 8; j++)
964 printf("%02x", rbuf[j]);
970 if ((ND.flags & ND6_IFF_PERFORMNUD))
972 #ifdef ND6_IFF_ACCEPT_RTADV
973 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
974 printf("accept_rtadv ");
983 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
984 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
990 #ifdef ICMPV6CTL_ND6_DRLIST
991 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
993 struct in6_defrouter *p, *ep;
997 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
998 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1003 errx(1, "not enough core");
1006 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1007 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1011 ep = (struct in6_defrouter *)(buf + l);
1012 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1015 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1016 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1017 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)) != 0)
1018 strlcpy(host_buf, "?", sizeof(host_buf));
1020 printf("%s if=%s", host_buf,
1021 if_indextoname(p->if_index, ifix_buf));
1022 printf(", flags=%s%s",
1023 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1024 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1025 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1026 printf(", pref=%s", rtpref_str[rtpref]);
1028 gettimeofday(&time, 0);
1030 printf(", expire=Never\n");
1032 printf(", expire=%s\n",
1033 sec2str(p->expire - time.tv_sec));
1037 struct in6_drlist dr;
1039 struct timeval time;
1041 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1042 perror("ndp: socket");
1045 bzero(&dr, sizeof(dr));
1046 strcpy(dr.ifname, "lo0"); /* dummy */
1047 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1048 perror("ioctl (SIOCGDRLST_IN6)");
1051 #define DR dr.defrouter[i]
1052 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1053 struct sockaddr_in6 sin6;
1055 bzero(&sin6, sizeof(sin6));
1056 sin6.sin6_family = AF_INET6;
1057 sin6.sin6_len = sizeof(sin6);
1058 sin6.sin6_addr = DR.rtaddr;
1059 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1060 sizeof(host_buf), NULL, 0,
1061 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1063 printf("%s if=%s", host_buf,
1064 if_indextoname(DR.if_index, ifix_buf));
1065 printf(", flags=%s%s",
1066 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1067 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1068 gettimeofday(&time, 0);
1070 printf(", expire=Never\n");
1072 printf(", expire=%s\n",
1073 sec2str(DR.expire - time.tv_sec));
1083 #ifdef ICMPV6CTL_ND6_PRLIST
1084 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1086 struct in6_prefix *p, *ep, *n;
1087 struct sockaddr_in6 *advrtr;
1089 struct timeval time;
1090 #ifdef NI_WITHSCOPEID
1091 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
1092 int ninflags = (nflag ? NI_NUMERICHOST : 0) | NI_WITHSCOPEID;
1094 const int niflags = NI_NUMERICHOST;
1095 int ninflags = nflag ? NI_NUMERICHOST : 0;
1097 char namebuf[NI_MAXHOST];
1099 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1100 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1105 errx(1, "not enough core");
1108 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1109 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1113 ep = (struct in6_prefix *)(buf + l);
1114 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1115 advrtr = (struct sockaddr_in6 *)(p + 1);
1116 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1118 if (getnameinfo((struct sockaddr *)&p->prefix,
1119 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1120 NULL, 0, niflags) != 0)
1121 strlcpy(namebuf, "?", sizeof(namebuf));
1122 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1123 if_indextoname(p->if_index, ifix_buf));
1125 gettimeofday(&time, 0);
1127 * meaning of fields, especially flags, is very different
1128 * by origin. notify the difference to the users.
1130 printf("flags=%s%s%s%s%s",
1131 p->raflags.onlink ? "L" : "",
1132 p->raflags.autonomous ? "A" : "",
1133 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1134 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1136 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1141 if (p->vltime == ND6_INFINITE_LIFETIME)
1142 printf(" vltime=infinity");
1144 printf(" vltime=%ld", (long)p->vltime);
1145 if (p->pltime == ND6_INFINITE_LIFETIME)
1146 printf(", pltime=infinity");
1148 printf(", pltime=%ld", (long)p->pltime);
1150 printf(", expire=Never");
1151 else if (p->expire >= time.tv_sec)
1152 printf(", expire=%s",
1153 sec2str(p->expire - time.tv_sec));
1155 printf(", expired");
1156 printf(", ref=%d", p->refcnt);
1159 * "advertising router" list is meaningful only if the prefix
1160 * information is from RA.
1164 struct sockaddr_in6 *sin6;
1166 sin6 = (struct sockaddr_in6 *)(p + 1);
1167 printf(" advertised by\n");
1168 for (j = 0; j < p->advrtrs; j++) {
1169 struct in6_nbrinfo *nbi;
1171 if (getnameinfo((struct sockaddr *)sin6,
1172 sin6->sin6_len, namebuf, sizeof(namebuf),
1173 NULL, 0, ninflags) != 0)
1174 strlcpy(namebuf, "?", sizeof(namebuf));
1175 printf(" %s", namebuf);
1177 nbi = getnbrinfo(&sin6->sin6_addr, p->if_index,
1180 switch(nbi->state) {
1181 case ND6_LLINFO_REACHABLE:
1182 case ND6_LLINFO_STALE:
1183 case ND6_LLINFO_DELAY:
1184 case ND6_LLINFO_PROBE:
1185 printf(" (reachable)\n");
1188 printf(" (unreachable)\n");
1191 printf(" (no neighbor state)\n");
1195 printf(" No advertising router\n");
1199 struct in6_prlist pr;
1201 struct timeval time;
1203 gettimeofday(&time, 0);
1205 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1206 perror("ndp: socket");
1209 bzero(&pr, sizeof(pr));
1210 strcpy(pr.ifname, "lo0"); /* dummy */
1211 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1212 perror("ioctl (SIOCGPRLST_IN6)");
1215 #define PR pr.prefix[i]
1216 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1217 struct sockaddr_in6 p6;
1218 char namebuf[NI_MAXHOST];
1224 memset(&p6, 0, sizeof(p6));
1225 p6.sin6_family = AF_INET6;
1226 p6.sin6_len = sizeof(p6);
1227 p6.sin6_addr = PR.prefix;
1231 * copy link index to sin6_scope_id field.
1232 * XXX: KAME specific.
1234 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1237 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1239 linkid = ntohs(linkid);
1240 p6.sin6_scope_id = linkid;
1241 p6.sin6_addr.s6_addr[2] = 0;
1242 p6.sin6_addr.s6_addr[3] = 0;
1245 niflags = NI_NUMERICHOST;
1247 niflags |= NI_WITHSCOPEID;
1249 if (getnameinfo((struct sockaddr *)&p6,
1250 sizeof(p6), namebuf, sizeof(namebuf),
1251 NULL, 0, niflags)) {
1252 warnx("getnameinfo failed");
1255 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1256 if_indextoname(PR.if_index, ifix_buf));
1258 gettimeofday(&time, 0);
1260 * meaning of fields, especially flags, is very different
1261 * by origin. notify the difference to the users.
1265 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1268 printf("flags=%s%s%s%s%s",
1269 PR.raflags.onlink ? "L" : "",
1270 PR.raflags.autonomous ? "A" : "",
1271 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1272 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1274 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1280 printf("flags=%s%s",
1281 PR.raflags.onlink ? "L" : "",
1282 PR.raflags.autonomous ? "A" : "");
1284 if (PR.vltime == ND6_INFINITE_LIFETIME)
1285 printf(" vltime=infinity");
1287 printf(" vltime=%ld", (long)PR.vltime);
1288 if (PR.pltime == ND6_INFINITE_LIFETIME)
1289 printf(", pltime=infinity");
1291 printf(", pltime=%ld", (long)PR.pltime);
1293 printf(", expire=Never");
1294 else if (PR.expire >= time.tv_sec)
1295 printf(", expire=%s",
1296 sec2str(PR.expire - time.tv_sec));
1298 printf(", expired");
1300 printf(", ref=%d", PR.refcnt);
1303 switch (PR.origin) {
1305 printf(", origin=RA");
1308 printf(", origin=RR");
1310 case PR_ORIG_STATIC:
1311 printf(", origin=static");
1313 case PR_ORIG_KERNEL:
1314 printf(", origin=kernel");
1317 printf(", origin=?");
1323 * "advertising router" list is meaningful only if the prefix
1324 * information is from RA.
1326 if (0 && /* prefix origin is almost obsolted */
1327 PR.origin != PR_ORIG_RA)
1329 else if (PR.advrtrs) {
1331 printf(" advertised by\n");
1332 for (j = 0; j < PR.advrtrs; j++) {
1333 struct sockaddr_in6 sin6;
1334 struct in6_nbrinfo *nbi;
1336 bzero(&sin6, sizeof(sin6));
1337 sin6.sin6_family = AF_INET6;
1338 sin6.sin6_len = sizeof(sin6);
1339 sin6.sin6_addr = PR.advrtr[j];
1340 sin6.sin6_scope_id = PR.if_index; /* XXX */
1341 getnameinfo((struct sockaddr *)&sin6,
1342 sin6.sin6_len, host_buf,
1343 sizeof(host_buf), NULL, 0,
1344 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1345 printf(" %s", host_buf);
1347 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1350 switch(nbi->state) {
1351 case ND6_LLINFO_REACHABLE:
1352 case ND6_LLINFO_STALE:
1353 case ND6_LLINFO_DELAY:
1354 case ND6_LLINFO_PROBE:
1355 printf(" (reachable)\n");
1358 printf(" (unreachable)\n");
1361 printf(" (no neighbor state)\n");
1363 if (PR.advrtrs > DRLSTSIZ)
1364 printf(" and %d routers\n",
1365 PR.advrtrs - DRLSTSIZ);
1367 printf(" No advertising router\n");
1377 char dummyif[IFNAMSIZ+8];
1380 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1382 strcpy(dummyif, "lo0"); /* dummy */
1383 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1384 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1390 char dummyif[IFNAMSIZ+8];
1393 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1395 strcpy(dummyif, "lo0"); /* dummy */
1396 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1397 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1405 char dummyif[IFNAMSIZ+8];
1408 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1410 strcpy(dummyif, "lo0"); /* dummy */
1411 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1412 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1417 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1419 setdefif(char *ifname)
1421 struct in6_ndifreq ndifreq;
1422 unsigned int ifindex;
1424 if (strcasecmp(ifname, "delete") == 0)
1427 if ((ifindex = if_nametoindex(ifname)) == 0)
1428 err(1, "failed to resolve i/f index for %s", ifname);
1431 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1434 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1435 ndifreq.ifindex = ifindex;
1437 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1438 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1446 struct in6_ndifreq ndifreq;
1447 char ifname[IFNAMSIZ+8];
1449 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1452 memset(&ndifreq, 0, sizeof(ndifreq));
1453 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1455 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1456 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1458 if (ndifreq.ifindex == 0)
1459 printf("No default interface.\n");
1461 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1462 err(1, "failed to resolve ifname for index %lu",
1464 printf("ND default interface = %s\n", ifname);
1472 sec2str(time_t total)
1474 static char result[256];
1475 int days, hours, mins, secs;
1479 days = total / 3600 / 24;
1480 hours = (total / 3600) % 24;
1481 mins = (total / 60) % 60;
1486 p += sprintf(p, "%dd", days);
1488 if (!first || hours) {
1490 p += sprintf(p, "%dh", hours);
1492 if (!first || mins) {
1494 p += sprintf(p, "%dm", mins);
1496 sprintf(p, "%ds", secs);
1502 * Print the timestamp
1503 * from tcpdump/util.c
1506 ts_print(const struct timeval *tvp)
1511 s = (tvp->tv_sec + thiszone) % 86400;
1512 printf("%02d:%02d:%02d.%06u ",
1513 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);