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.4 2004/03/30 01:14:22 cpressey 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(int, char **);
140 void getsocket(void);
141 int set(int, char **);
144 void dump(struct in6_addr *);
145 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *addr,
147 static char *ether_str(struct sockaddr_dl *);
148 int ndp_ether_aton(char *, u_char *);
151 void ifinfo(int, char **);
154 void pfx_flush(void);
156 void rtr_flush(void);
157 void harmonize_rtr(void);
158 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
159 static void getdefif(void);
160 static void setdefif(char *);
162 static char *sec2str(time_t t);
163 static char *ether_str(struct sockaddr_dl *sdl);
164 static void ts_print(const struct timeval *);
166 static char *rtpref_str[] = {
174 main(int argc, char **argv)
177 int aflag = 0, dflag = 0, sflag = 0, Hflag = 0,
178 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
181 thiszone = gmt2local(0);
182 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1)
194 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
197 getdefif(); /* always call it to print the result */
200 errx(1, "not supported yet");
222 /* obsolete, ignored */
235 repeat = atoi(optarg);
255 if (aflag || cflag) {
274 if (argc < 2 || argc > 4)
276 exit(set(argc, argv) ? 1 : 0);
298 * Process a file to set standard ndp entries
305 char line[100], arg[5][50], *args[5];
307 if ((fp = fopen(name, "r")) == NULL) {
308 fprintf(stderr, "ndp: cannot open %s\n", name);
311 args[0] = &arg[0][0];
312 args[1] = &arg[1][0];
313 args[2] = &arg[2][0];
314 args[3] = &arg[3][0];
315 args[4] = &arg[4][0];
317 while(fgets(line, 100, fp) != NULL) {
318 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
321 fprintf(stderr, "ndp: bad line: %s\n", line);
336 s = socket(PF_ROUTE, SOCK_RAW, 0);
338 perror("ndp: socket");
344 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
345 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
346 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
347 int expire_time, flags, found_entry;
349 struct rt_msghdr m_rtm;
354 * Set an individual neighbor cache entry
357 set(int argc, char **argv)
359 struct sockaddr_in6 *sin = &sin_m;
360 struct sockaddr_dl *sdl;
361 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
362 struct addrinfo hints, *res;
365 char *host = argv[0], *eaddr = argv[1];
373 bzero(&hints, sizeof(hints));
374 hints.ai_family = AF_INET6;
375 gai_error = getaddrinfo(host, NULL, &hints, &res);
377 fprintf(stderr, "ndp: %s: %s\n", host,
378 gai_strerror(gai_error));
381 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
383 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
384 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
385 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
388 ea = (u_char *)LLADDR(&sdl_m);
389 if (ndp_ether_aton(eaddr, ea) == 0)
391 flags = expire_time = 0;
393 if (strncmp(argv[0], "temp", 4) == 0) {
395 gettimeofday(&time, 0);
396 expire_time = time.tv_sec + 20 * 60;
397 } else if (strncmp(argv[0], "proxy", 5) == 0)
398 flags |= RTF_ANNOUNCE;
401 if (rtmsg(RTM_GET) < 0) {
405 sin = (struct sockaddr_in6 *)(rtm + 1);
406 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
407 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
408 if (sdl->sdl_family == AF_LINK &&
409 (rtm->rtm_flags & RTF_LLINFO) &&
410 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
411 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
412 case IFT_ISO88024: case IFT_ISO88025:
416 * IPv4 arp command retries with sin_other = SIN_PROXY here.
418 fprintf(stderr, "set: cannot configure a new entry\n");
423 if (sdl->sdl_family != AF_LINK) {
424 printf("cannot intuit interface index and type for %s\n", host);
427 sdl_m.sdl_type = sdl->sdl_type;
428 sdl_m.sdl_index = sdl->sdl_index;
429 return (rtmsg(RTM_ADD));
433 * Display an individual neighbor cache entry
438 struct sockaddr_in6 *sin = &sin_m;
439 struct addrinfo hints, *res;
443 bzero(&hints, sizeof(hints));
444 hints.ai_family = AF_INET6;
445 gai_error = getaddrinfo(host, NULL, &hints, &res);
447 fprintf(stderr, "ndp: %s: %s\n", host,
448 gai_strerror(gai_error));
451 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
453 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
454 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
455 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
458 dump(&sin->sin6_addr);
459 if (found_entry == 0) {
460 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
461 sizeof(host_buf), NULL ,0,
462 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
463 printf("%s (%s) -- no entry\n", host, host_buf);
469 * Delete a neighbor cache entry
474 struct sockaddr_in6 *sin = &sin_m;
475 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
476 struct sockaddr_dl *sdl;
477 struct addrinfo hints, *res;
483 bzero(&hints, sizeof(hints));
484 hints.ai_family = AF_INET6;
485 gai_error = getaddrinfo(host, NULL, &hints, &res);
487 fprintf(stderr, "ndp: %s: %s\n", host,
488 gai_strerror(gai_error));
491 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
493 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
494 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
495 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
498 if (rtmsg(RTM_GET) < 0) {
502 sin = (struct sockaddr_in6 *)(rtm + 1);
503 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
504 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
505 if (sdl->sdl_family == AF_LINK &&
506 (rtm->rtm_flags & RTF_LLINFO) &&
507 !(rtm->rtm_flags & RTF_GATEWAY)) {
511 * IPv4 arp command retries with sin_other = SIN_PROXY here.
513 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
518 if (sdl->sdl_family != AF_LINK) {
519 printf("cannot locate %s\n", host);
522 if (rtmsg(RTM_DELETE) == 0) {
523 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
526 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
527 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
528 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
531 getnameinfo((struct sockaddr *)&s6,
532 s6.sin6_len, host_buf,
533 sizeof(host_buf), NULL, 0,
534 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
535 printf("%s (%s) deleted\n", host, host_buf);
546 * Dump the entire neighbor cache
549 dump(struct in6_addr *addr)
553 char *lim, *buf, *next;
554 struct rt_msghdr *rtm;
555 struct sockaddr_in6 *sin;
556 struct sockaddr_dl *sdl;
558 struct in6_nbrinfo *nbi;
567 if (!tflag && !cflag)
568 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
569 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
570 W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs");
577 mib[4] = NET_RT_FLAGS;
579 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
580 err(1, "sysctl(PF_ROUTE estimate)");
582 if ((buf = malloc(needed)) == NULL)
584 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
585 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
590 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
591 int isrouter = 0, prbs = 0;
593 rtm = (struct rt_msghdr *)next;
594 sin = (struct sockaddr_in6 *)(rtm + 1);
595 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
598 * Some OSes can produce a route that has the LINK flag but
599 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
600 * and BSD/OS, where xx is not the interface identifier on
601 * lo0). Such routes entry would annoy getnbrinfo() below,
603 * XXX: such routes should have the GATEWAY flag, not the
604 * LINK flag. However, there are rotten routing software
605 * that advertises all routes that have the GATEWAY flag.
606 * Thus, KAME kernel intentionally does not set the LINK flag.
607 * What is to be fixed is not ndp, but such routing software
608 * (and the kernel workaround)...
610 if (sdl->sdl_family != AF_LINK)
614 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
617 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
619 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
620 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
621 /* XXX: should scope id be filled in the kernel? */
622 if (sin->sin6_scope_id == 0)
623 sin->sin6_scope_id = sdl->sdl_index;
625 /* KAME specific hack; removed the embedded id */
626 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
629 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
630 sizeof(host_buf), NULL, 0,
631 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
634 if (rtm->rtm_flags & RTF_WASCLONED)
641 gettimeofday(&time, 0);
645 addrwidth = strlen(host_buf);
646 if (addrwidth < W_ADDR)
648 llwidth = strlen(ether_str(sdl));
649 if (W_ADDR + W_LL - addrwidth > llwidth)
650 llwidth = W_ADDR + W_LL - addrwidth;
651 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
654 ifwidth = strlen(ifname);
655 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
656 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
658 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
659 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
661 /* Print neighbor discovery specific informations */
662 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
664 if (nbi->expire > time.tv_sec) {
666 sec2str(nbi->expire - time.tv_sec));
667 } else if (nbi->expire == 0)
668 printf(" %-9.9s", "permanent");
670 printf(" %-9.9s", "expired");
673 case ND6_LLINFO_NOSTATE:
676 #ifdef ND6_LLINFO_WAITDELETE
677 case ND6_LLINFO_WAITDELETE:
681 case ND6_LLINFO_INCOMPLETE:
684 case ND6_LLINFO_REACHABLE:
687 case ND6_LLINFO_STALE:
690 case ND6_LLINFO_DELAY:
693 case ND6_LLINFO_PROBE:
701 isrouter = nbi->isrouter;
704 warnx("failed to get neighbor information");
710 * other flags. R: router, P: proxy, W: ??
712 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
713 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
715 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
717 sin = (struct sockaddr_in6 *)
718 (sdl->sdl_len + (char *)sdl);
719 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
721 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
723 (sin->sin6_len != sizeof(struct sockaddr_in6))
725 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
727 printf(" %-4.4s", flgbuf);
730 printf(" %4d", prbs);
744 static struct in6_nbrinfo *
745 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
747 static struct in6_nbrinfo nbi;
750 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
753 bzero(&nbi, sizeof(nbi));
754 if_indextoname(ifindex, nbi.ifname);
756 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
758 warn("ioctl(SIOCGNBRINFO_IN6)");
768 ether_str(struct sockaddr_dl *sdl)
770 static char ebuf[32];
774 cp = (u_char *)LLADDR(sdl);
775 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
776 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
778 sprintf(ebuf, "(incomplete)");
785 ndp_ether_aton(char *a, u_char *n)
789 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
790 &o[3], &o[4], &o[5]);
792 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
803 printf("usage: ndp hostname\n");
804 printf(" ndp -a[nt]\n");
805 printf(" ndp [-nt] -A wait\n");
806 printf(" ndp -c[nt]\n");
807 printf(" ndp -d[nt] hostname\n");
808 printf(" ndp -f[nt] filename\n");
809 printf(" ndp -i interface [flags...]\n");
810 #ifdef SIOCSDEFIFACE_IN6
811 printf(" ndp -I [interface|delete]\n");
815 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
827 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
828 char *cp = m_rtmsg.m_space;
832 if (cmd == RTM_DELETE)
834 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
835 rtm->rtm_flags = flags;
836 rtm->rtm_version = RTM_VERSION;
840 fprintf(stderr, "ndp: internal wrong cmd\n");
843 rtm->rtm_addrs |= RTA_GATEWAY;
844 rtm->rtm_rmx.rmx_expire = expire_time;
845 rtm->rtm_inits = RTV_EXPIRE;
846 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
847 if (rtm->rtm_flags & RTF_ANNOUNCE) {
848 rtm->rtm_flags &= ~RTF_HOST;
849 rtm->rtm_flags |= RTA_NETMASK;
853 rtm->rtm_addrs |= RTA_DST;
855 #define NEXTADDR(w, s) \
856 if (rtm->rtm_addrs & (w)) { \
857 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
859 NEXTADDR(RTA_DST, sin_m);
860 NEXTADDR(RTA_GATEWAY, sdl_m);
861 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
862 NEXTADDR(RTA_NETMASK, so_mask);
864 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
867 rtm->rtm_seq = ++seq;
869 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
870 if (errno != ESRCH || cmd != RTM_DELETE) {
871 perror("writing to routing socket");
876 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
877 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
879 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
885 ifinfo(int argc, char **argv)
887 struct in6_ndireq nd;
889 char *ifname = argv[0];
891 #ifdef IPV6CTL_USETEMPADDR
895 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
896 perror("ndp: socket");
899 bzero(&nd, sizeof(nd));
900 strcpy(nd.ifname, ifname);
901 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
902 perror("ioctl (SIOCGIFINFO_IN6)");
907 for (i = 1; i < argc; i++) {
916 #define SETFLAG(s, f) \
918 if (strcmp(cp, (s)) == 0) {\
925 SETFLAG("nud", ND6_IFF_PERFORMNUD);
928 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
929 perror("ioctl(SIOCSIFINFO_FLAGS)");
935 printf("linkmtu=%d", ND.linkmtu);
936 printf(", curhlim=%d", ND.chlim);
937 printf(", basereachable=%ds%dms",
938 ND.basereachable / 1000, ND.basereachable % 1000);
939 printf(", reachable=%ds", ND.reachable);
940 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
941 #ifdef IPV6CTL_USETEMPADDR
942 memset(nullbuf, 0, sizeof(nullbuf));
943 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
947 for (i = 0; i < 3; i++) {
950 printf("\nRandom seed(0): ");
951 rbuf = ND.randomseed0;
954 printf("\nRandom seed(1): ");
955 rbuf = ND.randomseed1;
958 printf("\nRandom ID: ");
962 for (j = 0; j < 8; j++)
963 printf("%02x", rbuf[j]);
969 if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
970 printf("PERFORMNUD ");
978 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
979 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
985 #ifdef ICMPV6CTL_ND6_DRLIST
986 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
988 struct in6_defrouter *p, *ep;
992 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
993 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
998 errx(1, "not enough core");
1001 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1002 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1006 ep = (struct in6_defrouter *)(buf + l);
1007 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1010 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1011 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1012 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)) != 0)
1013 strlcpy(host_buf, "?", sizeof(host_buf));
1015 printf("%s if=%s", host_buf,
1016 if_indextoname(p->if_index, ifix_buf));
1017 printf(", flags=%s%s",
1018 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1019 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1020 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1021 printf(", pref=%s", rtpref_str[rtpref]);
1023 gettimeofday(&time, 0);
1025 printf(", expire=Never\n");
1027 printf(", expire=%s\n",
1028 sec2str(p->expire - time.tv_sec));
1032 struct in6_drlist dr;
1034 struct timeval time;
1036 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1037 perror("ndp: socket");
1040 bzero(&dr, sizeof(dr));
1041 strcpy(dr.ifname, "lo0"); /* dummy */
1042 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1043 perror("ioctl (SIOCGDRLST_IN6)");
1046 #define DR dr.defrouter[i]
1047 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1048 struct sockaddr_in6 sin6;
1050 bzero(&sin6, sizeof(sin6));
1051 sin6.sin6_family = AF_INET6;
1052 sin6.sin6_len = sizeof(sin6);
1053 sin6.sin6_addr = DR.rtaddr;
1054 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1055 sizeof(host_buf), NULL, 0,
1056 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1058 printf("%s if=%s", host_buf,
1059 if_indextoname(DR.if_index, ifix_buf));
1060 printf(", flags=%s%s",
1061 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1062 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1063 gettimeofday(&time, 0);
1065 printf(", expire=Never\n");
1067 printf(", expire=%s\n",
1068 sec2str(DR.expire - time.tv_sec));
1078 #ifdef ICMPV6CTL_ND6_PRLIST
1079 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1081 struct in6_prefix *p, *ep, *n;
1082 struct sockaddr_in6 *advrtr;
1084 struct timeval time;
1085 #ifdef NI_WITHSCOPEID
1086 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
1087 int ninflags = (nflag ? NI_NUMERICHOST : 0) | NI_WITHSCOPEID;
1089 const int niflags = NI_NUMERICHOST;
1090 int ninflags = nflag ? NI_NUMERICHOST : 0;
1092 char namebuf[NI_MAXHOST];
1094 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1095 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1100 errx(1, "not enough core");
1103 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1104 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1108 ep = (struct in6_prefix *)(buf + l);
1109 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1110 advrtr = (struct sockaddr_in6 *)(p + 1);
1111 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1113 if (getnameinfo((struct sockaddr *)&p->prefix,
1114 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1115 NULL, 0, niflags) != 0)
1116 strlcpy(namebuf, "?", sizeof(namebuf));
1117 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1118 if_indextoname(p->if_index, ifix_buf));
1120 gettimeofday(&time, 0);
1122 * meaning of fields, especially flags, is very different
1123 * by origin. notify the difference to the users.
1125 printf("flags=%s%s%s%s%s",
1126 p->raflags.onlink ? "L" : "",
1127 p->raflags.autonomous ? "A" : "",
1128 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1129 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1131 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1136 if (p->vltime == ND6_INFINITE_LIFETIME)
1137 printf(" vltime=infinity");
1139 printf(" vltime=%ld", (long)p->vltime);
1140 if (p->pltime == ND6_INFINITE_LIFETIME)
1141 printf(", pltime=infinity");
1143 printf(", pltime=%ld", (long)p->pltime);
1145 printf(", expire=Never");
1146 else if (p->expire >= time.tv_sec)
1147 printf(", expire=%s",
1148 sec2str(p->expire - time.tv_sec));
1150 printf(", expired");
1151 printf(", ref=%d", p->refcnt);
1154 * "advertising router" list is meaningful only if the prefix
1155 * information is from RA.
1159 struct sockaddr_in6 *sin6;
1161 sin6 = (struct sockaddr_in6 *)(p + 1);
1162 printf(" advertised by\n");
1163 for (j = 0; j < p->advrtrs; j++) {
1164 struct in6_nbrinfo *nbi;
1166 if (getnameinfo((struct sockaddr *)sin6,
1167 sin6->sin6_len, namebuf, sizeof(namebuf),
1168 NULL, 0, ninflags) != 0)
1169 strlcpy(namebuf, "?", sizeof(namebuf));
1170 printf(" %s", namebuf);
1172 nbi = getnbrinfo(&sin6->sin6_addr, p->if_index,
1175 switch(nbi->state) {
1176 case ND6_LLINFO_REACHABLE:
1177 case ND6_LLINFO_STALE:
1178 case ND6_LLINFO_DELAY:
1179 case ND6_LLINFO_PROBE:
1180 printf(" (reachable)\n");
1183 printf(" (unreachable)\n");
1186 printf(" (no neighbor state)\n");
1190 printf(" No advertising router\n");
1194 struct in6_prlist pr;
1196 struct timeval time;
1198 gettimeofday(&time, 0);
1200 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1201 perror("ndp: socket");
1204 bzero(&pr, sizeof(pr));
1205 strcpy(pr.ifname, "lo0"); /* dummy */
1206 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1207 perror("ioctl (SIOCGPRLST_IN6)");
1210 #define PR pr.prefix[i]
1211 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1212 struct sockaddr_in6 p6;
1213 char namebuf[NI_MAXHOST];
1219 memset(&p6, 0, sizeof(p6));
1220 p6.sin6_family = AF_INET6;
1221 p6.sin6_len = sizeof(p6);
1222 p6.sin6_addr = PR.prefix;
1226 * copy link index to sin6_scope_id field.
1227 * XXX: KAME specific.
1229 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1232 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1234 linkid = ntohs(linkid);
1235 p6.sin6_scope_id = linkid;
1236 p6.sin6_addr.s6_addr[2] = 0;
1237 p6.sin6_addr.s6_addr[3] = 0;
1240 niflags = NI_NUMERICHOST;
1242 niflags |= NI_WITHSCOPEID;
1244 if (getnameinfo((struct sockaddr *)&p6,
1245 sizeof(p6), namebuf, sizeof(namebuf),
1246 NULL, 0, niflags)) {
1247 warnx("getnameinfo failed");
1250 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1251 if_indextoname(PR.if_index, ifix_buf));
1253 gettimeofday(&time, 0);
1255 * meaning of fields, especially flags, is very different
1256 * by origin. notify the difference to the users.
1260 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1263 printf("flags=%s%s%s%s%s",
1264 PR.raflags.onlink ? "L" : "",
1265 PR.raflags.autonomous ? "A" : "",
1266 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1267 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1269 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1275 printf("flags=%s%s",
1276 PR.raflags.onlink ? "L" : "",
1277 PR.raflags.autonomous ? "A" : "");
1279 if (PR.vltime == ND6_INFINITE_LIFETIME)
1280 printf(" vltime=infinity");
1282 printf(" vltime=%ld", (long)PR.vltime);
1283 if (PR.pltime == ND6_INFINITE_LIFETIME)
1284 printf(", pltime=infinity");
1286 printf(", pltime=%ld", (long)PR.pltime);
1288 printf(", expire=Never");
1289 else if (PR.expire >= time.tv_sec)
1290 printf(", expire=%s",
1291 sec2str(PR.expire - time.tv_sec));
1293 printf(", expired");
1295 printf(", ref=%d", PR.refcnt);
1298 switch (PR.origin) {
1300 printf(", origin=RA");
1303 printf(", origin=RR");
1305 case PR_ORIG_STATIC:
1306 printf(", origin=static");
1308 case PR_ORIG_KERNEL:
1309 printf(", origin=kernel");
1312 printf(", origin=?");
1318 * "advertising router" list is meaningful only if the prefix
1319 * information is from RA.
1321 if (0 && /* prefix origin is almost obsolted */
1322 PR.origin != PR_ORIG_RA)
1324 else if (PR.advrtrs) {
1326 printf(" advertised by\n");
1327 for (j = 0; j < PR.advrtrs; j++) {
1328 struct sockaddr_in6 sin6;
1329 struct in6_nbrinfo *nbi;
1331 bzero(&sin6, sizeof(sin6));
1332 sin6.sin6_family = AF_INET6;
1333 sin6.sin6_len = sizeof(sin6);
1334 sin6.sin6_addr = PR.advrtr[j];
1335 sin6.sin6_scope_id = PR.if_index; /* XXX */
1336 getnameinfo((struct sockaddr *)&sin6,
1337 sin6.sin6_len, host_buf,
1338 sizeof(host_buf), NULL, 0,
1339 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1340 printf(" %s", host_buf);
1342 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1345 switch(nbi->state) {
1346 case ND6_LLINFO_REACHABLE:
1347 case ND6_LLINFO_STALE:
1348 case ND6_LLINFO_DELAY:
1349 case ND6_LLINFO_PROBE:
1350 printf(" (reachable)\n");
1353 printf(" (unreachable)\n");
1356 printf(" (no neighbor state)\n");
1358 if (PR.advrtrs > DRLSTSIZ)
1359 printf(" and %d routers\n",
1360 PR.advrtrs - DRLSTSIZ);
1362 printf(" No advertising router\n");
1372 char dummyif[IFNAMSIZ+8];
1375 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1377 strcpy(dummyif, "lo0"); /* dummy */
1378 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1379 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1385 char dummyif[IFNAMSIZ+8];
1388 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1390 strcpy(dummyif, "lo0"); /* dummy */
1391 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1392 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1400 char dummyif[IFNAMSIZ+8];
1403 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1405 strcpy(dummyif, "lo0"); /* dummy */
1406 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1407 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1412 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1414 setdefif(char *ifname)
1416 struct in6_ndifreq ndifreq;
1417 unsigned int ifindex;
1419 if (strcasecmp(ifname, "delete") == 0)
1422 if ((ifindex = if_nametoindex(ifname)) == 0)
1423 err(1, "failed to resolve i/f index for %s", ifname);
1426 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1429 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1430 ndifreq.ifindex = ifindex;
1432 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1433 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1441 struct in6_ndifreq ndifreq;
1442 char ifname[IFNAMSIZ+8];
1444 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1447 memset(&ndifreq, 0, sizeof(ndifreq));
1448 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1450 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1451 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1453 if (ndifreq.ifindex == 0)
1454 printf("No default interface.\n");
1456 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1457 err(1, "failed to resolve ifname for index %lu",
1459 printf("ND default interface = %s\n", ifname);
1467 sec2str(time_t total)
1469 static char result[256];
1470 int days, hours, mins, secs;
1474 days = total / 3600 / 24;
1475 hours = (total / 3600) % 24;
1476 mins = (total / 60) % 60;
1481 p += sprintf(p, "%dd", days);
1483 if (!first || hours) {
1485 p += sprintf(p, "%dh", hours);
1487 if (!first || mins) {
1489 p += sprintf(p, "%dm", mins);
1491 sprintf(p, "%ds", secs);
1497 * Print the timestamp
1498 * from tcpdump/util.c
1501 ts_print(const struct timeval *tvp)
1506 s = (tvp->tv_sec + thiszone) % 86400;
1507 (void)printf("%02d:%02d:%02d.%06u ",
1508 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);