1 /* $FreeBSD: src/usr.sbin/ndp/ndp.c,v 1.2.2.6 2003/08/12 16:27:57 ume 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. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * "@(#) Copyright (c) 1984, 1993\n\
67 * The Regents of the University of California. All rights reserved.\n";
69 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
73 * ndp - display, set, delete and flush neighbor cache
77 #include <sys/param.h>
79 #include <sys/ioctl.h>
80 #include <sys/socket.h>
81 #include <sys/sysctl.h>
83 #include <sys/queue.h>
86 #include <net/if_var.h>
87 #include <net/if_dl.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/if_ether.h>
94 #include <netinet/icmp6.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet6/nd6.h>
98 #include <arpa/inet.h>
110 #include "gmt2local.h"
112 #ifndef NI_WITHSCOPEID
113 #define NI_WITHSCOPEID 0
116 /* packing rule for routing socket */
118 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
119 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
125 static int32_t thiszone; /* time difference with gmt */
127 static int repeat = 0;
129 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
130 char host_buf[NI_MAXHOST]; /* getnameinfo() */
131 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
134 void getsocket(void);
135 int set(int, char **);
138 void dump(struct in6_addr *);
139 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *addr,
141 static char *ether_str(struct sockaddr_dl *);
142 int ndp_ether_aton(char *, u_char *);
145 void ifinfo(int, char **);
148 void pfx_flush(void);
150 void rtr_flush(void);
151 void harmonize_rtr(void);
152 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
153 static void getdefif(void);
154 static void setdefif(char *);
156 static char *sec2str(time_t t);
157 static char *ether_str(struct sockaddr_dl *sdl);
158 static void ts_print(const struct timeval *);
160 static char *rtpref_str[] = {
168 main(int argc, char **argv)
171 int aflag = 0, dflag = 0, sflag = 0, Hflag = 0,
172 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
175 thiszone = gmt2local(0);
176 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1)
188 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
191 getdefif(); /* always call it to print the result */
194 errx(1, "not supported yet");
216 /* obsolete, ignored */
229 repeat = atoi(optarg);
249 if (aflag || cflag) {
268 if (argc < 2 || argc > 4)
270 exit(set(argc, argv) ? 1 : 0);
292 * Process a file to set standard ndp entries
299 char line[100], arg[5][50], *args[5];
301 if ((fp = fopen(name, "r")) == NULL) {
302 fprintf(stderr, "ndp: cannot open %s\n", name);
305 args[0] = &arg[0][0];
306 args[1] = &arg[1][0];
307 args[2] = &arg[2][0];
308 args[3] = &arg[3][0];
309 args[4] = &arg[4][0];
311 while(fgets(line, 100, fp) != NULL) {
312 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
315 fprintf(stderr, "ndp: bad line: %s\n", line);
330 s = socket(PF_ROUTE, SOCK_RAW, 0);
332 perror("ndp: socket");
338 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
339 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
340 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
341 int flags, found_entry;
344 struct rt_msghdr m_rtm;
349 * Set an individual neighbor cache entry
352 set(int argc, char **argv)
354 struct sockaddr_in6 *sin = &sin_m;
355 struct sockaddr_dl *sdl;
356 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
357 struct addrinfo hints, *res;
360 char *host = argv[0], *eaddr = argv[1];
368 bzero(&hints, sizeof(hints));
369 hints.ai_family = AF_INET6;
370 gai_error = getaddrinfo(host, NULL, &hints, &res);
372 fprintf(stderr, "ndp: %s: %s\n", host,
373 gai_strerror(gai_error));
376 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
378 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
379 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
380 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
383 ea = (u_char *)LLADDR(&sdl_m);
384 if (ndp_ether_aton(eaddr, ea) == 0)
389 if (strncmp(argv[0], "temp", 4) == 0) {
392 clock_gettime(CLOCK_MONOTONIC, &sp);
393 expire_time = sp.tv_sec + 20 * 60;
394 } else if (strncmp(argv[0], "proxy", 5) == 0)
395 flags |= RTF_ANNOUNCE;
398 if (rtmsg(RTM_GET) < 0) {
402 sin = (struct sockaddr_in6 *)(rtm + 1);
403 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
404 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
405 if (sdl->sdl_family == AF_LINK &&
406 (rtm->rtm_flags & RTF_LLINFO) &&
407 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
408 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
409 case IFT_ISO88024: case IFT_ISO88025:
413 * IPv4 arp command retries with sin_other = SIN_PROXY here.
415 fprintf(stderr, "set: cannot configure a new entry\n");
420 if (sdl->sdl_family != AF_LINK) {
421 printf("cannot intuit interface index and type for %s\n", host);
424 sdl_m.sdl_type = sdl->sdl_type;
425 sdl_m.sdl_index = sdl->sdl_index;
426 return (rtmsg(RTM_ADD));
430 * Display an individual neighbor cache entry
435 struct sockaddr_in6 *sin = &sin_m;
436 struct addrinfo hints, *res;
440 bzero(&hints, sizeof(hints));
441 hints.ai_family = AF_INET6;
442 gai_error = getaddrinfo(host, NULL, &hints, &res);
444 fprintf(stderr, "ndp: %s: %s\n", host,
445 gai_strerror(gai_error));
448 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
450 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
451 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
452 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
455 dump(&sin->sin6_addr);
456 if (found_entry == 0) {
457 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
458 sizeof(host_buf), NULL ,0,
459 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
460 printf("%s (%s) -- no entry\n", host, host_buf);
466 * Delete a neighbor cache entry
471 struct sockaddr_in6 *sin = &sin_m;
472 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
473 struct sockaddr_dl *sdl;
474 struct addrinfo hints, *res;
480 bzero(&hints, sizeof(hints));
481 hints.ai_family = AF_INET6;
482 gai_error = getaddrinfo(host, NULL, &hints, &res);
484 fprintf(stderr, "ndp: %s: %s\n", host,
485 gai_strerror(gai_error));
488 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
490 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
491 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
492 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
495 if (rtmsg(RTM_GET) < 0) {
499 sin = (struct sockaddr_in6 *)(rtm + 1);
500 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
501 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
502 if (sdl->sdl_family == AF_LINK &&
503 (rtm->rtm_flags & RTF_LLINFO) &&
504 !(rtm->rtm_flags & RTF_GATEWAY)) {
508 * IPv4 arp command retries with sin_other = SIN_PROXY here.
510 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
515 if (sdl->sdl_family != AF_LINK) {
516 printf("cannot locate %s\n", host);
519 if (rtmsg(RTM_DELETE) == 0) {
520 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
523 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
524 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
525 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
528 getnameinfo((struct sockaddr *)&s6,
529 s6.sin6_len, host_buf,
530 sizeof(host_buf), NULL, 0,
531 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
532 printf("%s (%s) deleted\n", host, host_buf);
543 * Dump the entire neighbor cache
546 dump(struct in6_addr *addr)
550 char *lim, *buf, *next;
551 struct rt_msghdr *rtm;
552 struct sockaddr_in6 *sin;
553 struct sockaddr_dl *sdl;
555 struct in6_nbrinfo *nbi;
564 if (!tflag && !cflag)
565 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
566 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
567 W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs");
574 mib[4] = NET_RT_FLAGS;
576 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
577 err(1, "sysctl(PF_ROUTE estimate)");
579 if ((buf = malloc(needed)) == NULL)
581 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
582 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
587 for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
588 int isrouter = 0, prbs = 0;
590 rtm = (struct rt_msghdr *)next;
591 sin = (struct sockaddr_in6 *)(rtm + 1);
592 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
595 * Some OSes can produce a route that has the LINK flag but
596 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
597 * and BSD/OS, where xx is not the interface identifier on
598 * lo0). Such routes entry would annoy getnbrinfo() below,
600 * XXX: such routes should have the GATEWAY flag, not the
601 * LINK flag. However, there are rotten routing software
602 * that advertises all routes that have the GATEWAY flag.
603 * Thus, KAME kernel intentionally does not set the LINK flag.
604 * What is to be fixed is not ndp, but such routing software
605 * (and the kernel workaround)...
607 if (sdl->sdl_family != AF_LINK)
611 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
614 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
616 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
617 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
618 /* XXX: should scope id be filled in the kernel? */
619 if (sin->sin6_scope_id == 0)
620 sin->sin6_scope_id = sdl->sdl_index;
622 /* KAME specific hack; removed the embedded id */
623 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
626 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
627 sizeof(host_buf), NULL, 0,
628 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
631 if (rtm->rtm_flags & RTF_WASCLONED)
638 gettimeofday(&time, 0);
642 addrwidth = strlen(host_buf);
643 if (addrwidth < W_ADDR)
645 llwidth = strlen(ether_str(sdl));
646 if (W_ADDR + W_LL - addrwidth > llwidth)
647 llwidth = W_ADDR + W_LL - addrwidth;
648 ifname = if_indextoname(sdl->sdl_index, ifix_buf);
651 ifwidth = strlen(ifname);
652 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
653 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
655 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
656 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
658 /* Print neighbor discovery specific informations */
659 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
661 if (nbi->expire > time.tv_sec) {
663 sec2str(nbi->expire - time.tv_sec));
664 } else if (nbi->expire == 0)
665 printf(" %-9.9s", "permanent");
667 printf(" %-9.9s", "expired");
670 case ND6_LLINFO_NOSTATE:
673 #ifdef ND6_LLINFO_WAITDELETE
674 case ND6_LLINFO_WAITDELETE:
678 case ND6_LLINFO_INCOMPLETE:
681 case ND6_LLINFO_REACHABLE:
684 case ND6_LLINFO_STALE:
687 case ND6_LLINFO_DELAY:
690 case ND6_LLINFO_PROBE:
698 isrouter = nbi->isrouter;
701 warnx("failed to get neighbor information");
707 * other flags. R: router, P: proxy, W: ??
709 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
710 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
712 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
714 sin = (struct sockaddr_in6 *)
715 (sdl->sdl_len + (char *)sdl);
716 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
718 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
720 (sin->sin6_len != sizeof(struct sockaddr_in6))
722 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
724 printf(" %-4.4s", flgbuf);
727 printf(" %4d", prbs);
741 static struct in6_nbrinfo *
742 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
744 static struct in6_nbrinfo nbi;
747 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
750 bzero(&nbi, sizeof(nbi));
751 if_indextoname(ifindex, nbi.ifname);
753 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
755 warn("ioctl(SIOCGNBRINFO_IN6)");
765 ether_str(struct sockaddr_dl *sdl)
767 static char ebuf[32];
771 cp = (u_char *)LLADDR(sdl);
772 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
773 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
775 sprintf(ebuf, "(incomplete)");
782 ndp_ether_aton(char *a, u_char *n)
786 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
787 &o[3], &o[4], &o[5]);
789 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
800 printf("usage: ndp hostname\n");
801 printf(" ndp -a[nt]\n");
802 printf(" ndp [-nt] -A wait\n");
803 printf(" ndp -c[nt]\n");
804 printf(" ndp -d[nt] hostname\n");
805 printf(" ndp -f[nt] filename\n");
806 printf(" ndp -i interface [flags...]\n");
807 #ifdef SIOCSDEFIFACE_IN6
808 printf(" ndp -I [interface|delete]\n");
812 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
824 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
825 char *cp = m_rtmsg.m_space;
829 if (cmd == RTM_DELETE)
831 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
832 rtm->rtm_flags = flags;
833 rtm->rtm_version = RTM_VERSION;
837 fprintf(stderr, "ndp: internal wrong cmd\n");
840 rtm->rtm_addrs |= RTA_GATEWAY;
841 rtm->rtm_rmx.rmx_expire = expire_time;
842 rtm->rtm_inits = RTV_EXPIRE;
843 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
844 if (rtm->rtm_flags & RTF_ANNOUNCE) {
845 rtm->rtm_flags &= ~RTF_HOST;
846 rtm->rtm_flags |= RTA_NETMASK;
850 rtm->rtm_addrs |= RTA_DST;
852 #define NEXTADDR(w, s) \
853 if (rtm->rtm_addrs & (w)) { \
854 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
856 NEXTADDR(RTA_DST, sin_m);
857 NEXTADDR(RTA_GATEWAY, sdl_m);
858 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
859 NEXTADDR(RTA_NETMASK, so_mask);
861 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
864 rtm->rtm_seq = ++seq;
866 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
867 if (errno != ESRCH || cmd != RTM_DELETE) {
868 perror("writing to routing socket");
873 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
874 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
876 fprintf(stderr, "ndp: read from routing socket: %s\n",
882 ifinfo(int argc, char **argv)
884 struct in6_ndireq nd;
886 char *ifname = argv[0];
888 #ifdef IPV6CTL_USETEMPADDR
892 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
893 perror("ndp: socket");
896 bzero(&nd, sizeof(nd));
897 strcpy(nd.ifname, ifname);
898 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
899 perror("ioctl (SIOCGIFINFO_IN6)");
904 for (i = 1; i < argc; i++) {
913 #define SETFLAG(s, f) \
915 if (strcmp(cp, (s)) == 0) {\
922 SETFLAG("nud", ND6_IFF_PERFORMNUD);
923 #ifdef ND6_IFF_ACCEPT_RTADV
924 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
927 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
928 perror("ioctl(SIOCSIFINFO_FLAGS)");
934 printf("linkmtu=%d", ND.linkmtu);
935 printf(", curhlim=%d", ND.chlim);
936 printf(", basereachable=%ds%dms",
937 ND.basereachable / 1000, ND.basereachable % 1000);
938 printf(", reachable=%ds", ND.reachable);
939 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
940 #ifdef IPV6CTL_USETEMPADDR
941 memset(nullbuf, 0, sizeof(nullbuf));
942 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
946 for (i = 0; i < 3; i++) {
949 printf("\nRandom seed(0): ");
950 rbuf = ND.randomseed0;
953 printf("\nRandom seed(1): ");
954 rbuf = ND.randomseed1;
957 printf("\nRandom ID: ");
961 for (j = 0; j < 8; j++)
962 printf("%02x", rbuf[j]);
968 if ((ND.flags & ND6_IFF_PERFORMNUD))
970 #ifdef ND6_IFF_ACCEPT_RTADV
971 if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
972 printf("accept_rtadv ");
981 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
982 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
988 #ifdef ICMPV6CTL_ND6_DRLIST
989 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
991 struct in6_defrouter *p, *ep;
995 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
996 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1001 errx(1, "not enough core");
1004 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1005 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1009 ep = (struct in6_defrouter *)(buf + l);
1010 for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1013 if (getnameinfo((struct sockaddr *)&p->rtaddr,
1014 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1015 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)) != 0)
1016 strlcpy(host_buf, "?", sizeof(host_buf));
1018 printf("%s if=%s", host_buf,
1019 if_indextoname(p->if_index, ifix_buf));
1020 printf(", flags=%s%s",
1021 p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1022 p->flags & ND_RA_FLAG_OTHER ? "O" : "");
1023 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1024 printf(", pref=%s", rtpref_str[rtpref]);
1026 gettimeofday(&time, 0);
1028 printf(", expire=Never\n");
1030 printf(", expire=%s\n",
1031 sec2str(p->expire - time.tv_sec));
1035 struct in6_drlist dr;
1037 struct timeval time;
1039 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1040 perror("ndp: socket");
1043 bzero(&dr, sizeof(dr));
1044 strcpy(dr.ifname, "lo0"); /* dummy */
1045 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
1046 perror("ioctl (SIOCGDRLST_IN6)");
1049 #define DR dr.defrouter[i]
1050 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
1051 struct sockaddr_in6 sin6;
1053 bzero(&sin6, sizeof(sin6));
1054 sin6.sin6_family = AF_INET6;
1055 sin6.sin6_len = sizeof(sin6);
1056 sin6.sin6_addr = DR.rtaddr;
1057 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
1058 sizeof(host_buf), NULL, 0,
1059 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1061 printf("%s if=%s", host_buf,
1062 if_indextoname(DR.if_index, ifix_buf));
1063 printf(", flags=%s%s",
1064 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
1065 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
1066 gettimeofday(&time, 0);
1068 printf(", expire=Never\n");
1070 printf(", expire=%s\n",
1071 sec2str(DR.expire - time.tv_sec));
1081 #ifdef ICMPV6CTL_ND6_PRLIST
1082 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1084 struct in6_prefix *p, *ep, *n;
1085 struct sockaddr_in6 *advrtr;
1087 struct timeval time;
1088 #ifdef NI_WITHSCOPEID
1089 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
1090 int ninflags = (nflag ? NI_NUMERICHOST : 0) | NI_WITHSCOPEID;
1092 const int niflags = NI_NUMERICHOST;
1093 int ninflags = nflag ? NI_NUMERICHOST : 0;
1095 char namebuf[NI_MAXHOST];
1097 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1098 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1103 errx(1, "not enough core");
1106 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1107 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1111 ep = (struct in6_prefix *)(buf + l);
1112 for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1113 advrtr = (struct sockaddr_in6 *)(p + 1);
1114 n = (struct in6_prefix *)&advrtr[p->advrtrs];
1116 if (getnameinfo((struct sockaddr *)&p->prefix,
1117 p->prefix.sin6_len, namebuf, sizeof(namebuf),
1118 NULL, 0, niflags) != 0)
1119 strlcpy(namebuf, "?", sizeof(namebuf));
1120 printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1121 if_indextoname(p->if_index, ifix_buf));
1123 gettimeofday(&time, 0);
1125 * meaning of fields, especially flags, is very different
1126 * by origin. notify the difference to the users.
1128 printf("flags=%s%s%s%s%s",
1129 p->raflags.onlink ? "L" : "",
1130 p->raflags.autonomous ? "A" : "",
1131 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1132 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1134 (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1139 if (p->vltime == ND6_INFINITE_LIFETIME)
1140 printf(" vltime=infinity");
1142 printf(" vltime=%ld", (long)p->vltime);
1143 if (p->pltime == ND6_INFINITE_LIFETIME)
1144 printf(", pltime=infinity");
1146 printf(", pltime=%ld", (long)p->pltime);
1148 printf(", expire=Never");
1149 else if (p->expire >= time.tv_sec)
1150 printf(", expire=%s",
1151 sec2str(p->expire - time.tv_sec));
1153 printf(", expired");
1154 printf(", ref=%d", p->refcnt);
1157 * "advertising router" list is meaningful only if the prefix
1158 * information is from RA.
1162 struct sockaddr_in6 *sin6;
1164 sin6 = (struct sockaddr_in6 *)(p + 1);
1165 printf(" advertised by\n");
1166 for (j = 0; j < p->advrtrs; j++) {
1167 struct in6_nbrinfo *nbi;
1169 if (getnameinfo((struct sockaddr *)sin6,
1170 sin6->sin6_len, namebuf, sizeof(namebuf),
1171 NULL, 0, ninflags) != 0)
1172 strlcpy(namebuf, "?", sizeof(namebuf));
1173 printf(" %s", namebuf);
1175 nbi = getnbrinfo(&sin6->sin6_addr, p->if_index,
1178 switch(nbi->state) {
1179 case ND6_LLINFO_REACHABLE:
1180 case ND6_LLINFO_STALE:
1181 case ND6_LLINFO_DELAY:
1182 case ND6_LLINFO_PROBE:
1183 printf(" (reachable)\n");
1186 printf(" (unreachable)\n");
1189 printf(" (no neighbor state)\n");
1193 printf(" No advertising router\n");
1197 struct in6_prlist pr;
1199 struct timeval time;
1201 gettimeofday(&time, 0);
1203 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1204 perror("ndp: socket");
1207 bzero(&pr, sizeof(pr));
1208 strcpy(pr.ifname, "lo0"); /* dummy */
1209 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
1210 perror("ioctl (SIOCGPRLST_IN6)");
1213 #define PR pr.prefix[i]
1214 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
1215 struct sockaddr_in6 p6;
1216 char namebuf[NI_MAXHOST];
1222 memset(&p6, 0, sizeof(p6));
1223 p6.sin6_family = AF_INET6;
1224 p6.sin6_len = sizeof(p6);
1225 p6.sin6_addr = PR.prefix;
1229 * copy link index to sin6_scope_id field.
1230 * XXX: KAME specific.
1232 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
1235 memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
1237 linkid = ntohs(linkid);
1238 p6.sin6_scope_id = linkid;
1239 p6.sin6_addr.s6_addr[2] = 0;
1240 p6.sin6_addr.s6_addr[3] = 0;
1243 niflags = NI_NUMERICHOST;
1245 niflags |= NI_WITHSCOPEID;
1247 if (getnameinfo((struct sockaddr *)&p6,
1248 sizeof(p6), namebuf, sizeof(namebuf),
1249 NULL, 0, niflags)) {
1250 warnx("getnameinfo failed");
1253 printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
1254 if_indextoname(PR.if_index, ifix_buf));
1256 gettimeofday(&time, 0);
1258 * meaning of fields, especially flags, is very different
1259 * by origin. notify the difference to the users.
1263 PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1266 printf("flags=%s%s%s%s%s",
1267 PR.raflags.onlink ? "L" : "",
1268 PR.raflags.autonomous ? "A" : "",
1269 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
1270 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
1272 (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
1278 printf("flags=%s%s",
1279 PR.raflags.onlink ? "L" : "",
1280 PR.raflags.autonomous ? "A" : "");
1282 if (PR.vltime == ND6_INFINITE_LIFETIME)
1283 printf(" vltime=infinity");
1285 printf(" vltime=%ld", (long)PR.vltime);
1286 if (PR.pltime == ND6_INFINITE_LIFETIME)
1287 printf(", pltime=infinity");
1289 printf(", pltime=%ld", (long)PR.pltime);
1291 printf(", expire=Never");
1292 else if (PR.expire >= time.tv_sec)
1293 printf(", expire=%s",
1294 sec2str(PR.expire - time.tv_sec));
1296 printf(", expired");
1298 printf(", ref=%d", PR.refcnt);
1301 switch (PR.origin) {
1303 printf(", origin=RA");
1306 printf(", origin=RR");
1308 case PR_ORIG_STATIC:
1309 printf(", origin=static");
1311 case PR_ORIG_KERNEL:
1312 printf(", origin=kernel");
1315 printf(", origin=?");
1321 * "advertising router" list is meaningful only if the prefix
1322 * information is from RA.
1324 if (0 && /* prefix origin is almost obsolted */
1325 PR.origin != PR_ORIG_RA)
1327 else if (PR.advrtrs) {
1329 printf(" advertised by\n");
1330 for (j = 0; j < PR.advrtrs; j++) {
1331 struct sockaddr_in6 sin6;
1332 struct in6_nbrinfo *nbi;
1334 bzero(&sin6, sizeof(sin6));
1335 sin6.sin6_family = AF_INET6;
1336 sin6.sin6_len = sizeof(sin6);
1337 sin6.sin6_addr = PR.advrtr[j];
1338 sin6.sin6_scope_id = PR.if_index; /* XXX */
1339 getnameinfo((struct sockaddr *)&sin6,
1340 sin6.sin6_len, host_buf,
1341 sizeof(host_buf), NULL, 0,
1342 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1343 printf(" %s", host_buf);
1345 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1348 switch(nbi->state) {
1349 case ND6_LLINFO_REACHABLE:
1350 case ND6_LLINFO_STALE:
1351 case ND6_LLINFO_DELAY:
1352 case ND6_LLINFO_PROBE:
1353 printf(" (reachable)\n");
1356 printf(" (unreachable)\n");
1359 printf(" (no neighbor state)\n");
1361 if (PR.advrtrs > DRLSTSIZ)
1362 printf(" and %d routers\n",
1363 PR.advrtrs - DRLSTSIZ);
1365 printf(" No advertising router\n");
1375 char dummyif[IFNAMSIZ+8];
1378 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1380 strcpy(dummyif, "lo0"); /* dummy */
1381 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1382 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1388 char dummyif[IFNAMSIZ+8];
1391 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1393 strcpy(dummyif, "lo0"); /* dummy */
1394 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1395 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1403 char dummyif[IFNAMSIZ+8];
1406 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1408 strcpy(dummyif, "lo0"); /* dummy */
1409 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1410 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1415 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1417 setdefif(char *ifname)
1419 struct in6_ndifreq ndifreq;
1420 unsigned int ifindex;
1422 if (strcasecmp(ifname, "delete") == 0)
1425 if ((ifindex = if_nametoindex(ifname)) == 0)
1426 err(1, "failed to resolve i/f index for %s", ifname);
1429 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1432 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1433 ndifreq.ifindex = ifindex;
1435 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1436 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1444 struct in6_ndifreq ndifreq;
1445 char ifname[IFNAMSIZ+8];
1447 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1450 memset(&ndifreq, 0, sizeof(ndifreq));
1451 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1453 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1454 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1456 if (ndifreq.ifindex == 0)
1457 printf("No default interface.\n");
1459 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1460 err(1, "failed to resolve ifname for index %lu",
1462 printf("ND default interface = %s\n", ifname);
1470 sec2str(time_t total)
1472 static char result[256];
1473 int days, hours, mins, secs;
1477 days = total / 3600 / 24;
1478 hours = (total / 3600) % 24;
1479 mins = (total / 60) % 60;
1484 p += sprintf(p, "%dd", days);
1486 if (!first || hours) {
1488 p += sprintf(p, "%dh", hours);
1490 if (!first || mins) {
1492 p += sprintf(p, "%dm", mins);
1494 sprintf(p, "%ds", secs);
1500 * Print the timestamp
1501 * from tcpdump/util.c
1504 ts_print(const struct timeval *tvp)
1509 s = (tvp->tv_sec + thiszone) % 86400;
1510 printf("%02d:%02d:%02d.%06u ",
1511 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);