2 /* $KAME: ndp.c,v 1.46 2000/10/09 09:17:10 sumikawa Exp $ */
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1984, 1993
34 * The Regents of the University of California. All rights reserved.
36 * This code is derived from software contributed to Berkeley by
37 * Sun Microsystems, Inc.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * "@(#) Copyright (c) 1984, 1993\n\
71 * The Regents of the University of California. All rights reserved.\n";
73 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
77 * ndp - display, set, delete and flush neighbor cache
81 #include <sys/param.h>
83 #include <sys/ioctl.h>
84 #include <sys/socket.h>
85 #include <sys/sysctl.h>
89 #include <net/if_var.h>
90 #include <net/if_dl.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
94 #include <netinet/in.h>
95 #include <netinet/if_ether.h>
97 #include <netinet/icmp6.h>
98 #include <netinet6/in6_var.h>
99 #include <netinet6/nd6.h>
101 #include <arpa/inet.h>
113 #include "gmt2local.h"
115 #ifndef NI_WITHSCOPEID
116 #define NI_WITHSCOPEID 0
119 /* packing rule for routing socket */
121 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
122 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
128 static int32_t thiszone; /* time difference with gmt */
130 static int repeat = 0;
131 static int lflag = 0;
133 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
134 char host_buf[NI_MAXHOST]; /* getnameinfo() */
135 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
137 int main __P((int, char **));
138 int file __P((char *));
139 void getsocket __P((void));
140 int set __P((int, char **));
141 void get __P((char *));
142 int delete __P((char *));
143 void dump __P((struct in6_addr *));
144 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
146 static char *ether_str __P((struct sockaddr_dl *));
147 int ndp_ether_aton __P((char *, u_char *));
148 void usage __P((void));
149 int rtmsg __P((int));
150 void ifinfo __P((int, char **));
151 void rtrlist __P((void));
152 void plist __P((void));
153 void pfx_flush __P((void));
154 void rtrlist __P((void));
155 void rtr_flush __P((void));
156 void harmonize_rtr __P((void));
157 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
158 static void getdefif __P((void));
159 static void setdefif __P((char *));
161 static char *sec2str __P((time_t t));
162 static char *ether_str __P((struct sockaddr_dl *sdl));
163 static void ts_print __P((const struct timeval *));
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")) != EOF)
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");
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
300 char line[100], arg[5][50], *args[5];
302 if ((fp = fopen(name, "r")) == NULL) {
303 fprintf(stderr, "ndp: cannot open %s\n", name);
306 args[0] = &arg[0][0];
307 args[1] = &arg[1][0];
308 args[2] = &arg[2][0];
309 args[3] = &arg[3][0];
310 args[4] = &arg[4][0];
312 while(fgets(line, 100, fp) != NULL) {
313 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
316 fprintf(stderr, "ndp: bad line: %s\n", line);
331 s = socket(PF_ROUTE, SOCK_RAW, 0);
333 perror("ndp: socket");
339 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
340 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
341 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
342 int expire_time, flags, found_entry;
344 struct rt_msghdr m_rtm;
349 * Set an individual neighbor cache entry
356 register struct sockaddr_in6 *sin = &sin_m;
357 register struct sockaddr_dl *sdl;
358 register 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)
388 flags = expire_time = 0;
390 if (strncmp(argv[0], "temp", 4) == 0) {
392 gettimeofday(&time, 0);
393 expire_time = time.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
436 struct sockaddr_in6 *sin = &sin_m;
437 struct addrinfo hints, *res;
441 bzero(&hints, sizeof(hints));
442 hints.ai_family = AF_INET6;
443 gai_error = getaddrinfo(host, NULL, &hints, &res);
445 fprintf(stderr, "ndp: %s: %s\n", host,
446 gai_strerror(gai_error));
449 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
451 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
452 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
453 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
456 dump(&sin->sin6_addr);
457 if (found_entry == 0) {
458 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
459 sizeof(host_buf), NULL ,0,
460 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
461 printf("%s (%s) -- no entry\n", host, host_buf);
467 * Delete a neighbor cache entry
473 struct sockaddr_in6 *sin = &sin_m;
474 register 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)) {
507 switch (sdl->sdl_type) {
508 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
509 case IFT_ISO88024: case IFT_ISO88025:
514 * IPv4 arp command retries with sin_other = SIN_PROXY here.
516 fprintf(stderr, "delete: cannot delete non-NDP entry\n");
521 if (sdl->sdl_family != AF_LINK) {
522 printf("cannot locate %s\n", host);
525 if (rtmsg(RTM_DELETE) == 0) {
526 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
529 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
530 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
531 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
534 getnameinfo((struct sockaddr *)&s6,
535 s6.sin6_len, host_buf,
536 sizeof(host_buf), NULL, 0,
537 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
538 printf("%s (%s) deleted\n", host, host_buf);
545 * Dump the entire neighbor cache
549 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;
564 if (!tflag && !cflag)
565 printf("%-31.31s %-17.17s %6.6s %-9.9s %2s %4s %4s\n",
566 "Neighbor", "Linklayer Address", "Netif", "Expire",
567 "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));
594 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
597 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
599 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
600 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
601 /* XXX: should scope id be filled in the kernel? */
602 if (sin->sin6_scope_id == 0)
603 sin->sin6_scope_id = sdl->sdl_index;
605 /* KAME specific hack; removed the embedded id */
606 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
609 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
610 sizeof(host_buf), NULL, 0,
611 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
616 gettimeofday(&time, 0);
621 addrwidth = strlen(host_buf);
627 printf("%-*.*s %-17.17s %6.6s", addrwidth, addrwidth, host_buf,
629 if_indextoname(sdl->sdl_index, ifix_buf));
631 /* Print neighbor discovery specific informations */
632 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
634 if (nbi->expire > time.tv_sec) {
636 sec2str(nbi->expire - time.tv_sec));
638 else if (nbi->expire == 0)
639 printf(" %-9.9s", "permanent");
641 printf(" %-9.9s", "expired");
644 case ND6_LLINFO_NOSTATE:
647 case ND6_LLINFO_WAITDELETE:
650 case ND6_LLINFO_INCOMPLETE:
653 case ND6_LLINFO_REACHABLE:
656 case ND6_LLINFO_STALE:
659 case ND6_LLINFO_DELAY:
662 case ND6_LLINFO_PROBE:
670 isrouter = nbi->isrouter;
674 warnx("failed to get neighbor information");
680 * other flags. R: router, P: proxy, W: ??
682 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
683 snprintf(flgbuf, sizeof(flgbuf), "%s%s",
685 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
687 sin = (struct sockaddr_in6 *)
688 (sdl->sdl_len + (char *)sdl);
689 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
691 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
693 (sin->sin6_len != sizeof(struct sockaddr_in6))
695 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
697 printf(" %-4.4s", flgbuf);
700 printf(" %4d", prbs);
712 static struct in6_nbrinfo *
713 getnbrinfo(addr, ifindex, warning)
714 struct in6_addr *addr;
718 static struct in6_nbrinfo nbi;
721 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
724 bzero(&nbi, sizeof(nbi));
725 if_indextoname(ifindex, nbi.ifname);
727 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
729 warn("ioctl(SIOCGNBRINFO_IN6)");
740 struct sockaddr_dl *sdl;
742 static char ebuf[32];
746 cp = (u_char *)LLADDR(sdl);
747 sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
748 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
751 sprintf(ebuf, "(incomplete)");
764 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
765 &o[3], &o[4], &o[5]);
767 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
778 printf("usage: ndp hostname\n");
779 printf(" ndp -a[ntl]\n");
780 printf(" ndp [-ntl] -A wait\n");
781 printf(" ndp -c[nt]\n");
782 printf(" ndp -d[nt] hostname\n");
783 printf(" ndp -f[nt] filename\n");
784 printf(" ndp -i interface [flags...]\n");
785 #ifdef SIOCSDEFIFACE_IN6
786 printf(" ndp -I [interface|delete]\n");
790 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
803 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
804 register char *cp = m_rtmsg.m_space;
808 if (cmd == RTM_DELETE)
810 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
811 rtm->rtm_flags = flags;
812 rtm->rtm_version = RTM_VERSION;
816 fprintf(stderr, "ndp: internal wrong cmd\n");
819 rtm->rtm_addrs |= RTA_GATEWAY;
820 rtm->rtm_rmx.rmx_expire = expire_time;
821 rtm->rtm_inits = RTV_EXPIRE;
822 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
823 if (rtm->rtm_flags & RTF_ANNOUNCE) {
824 rtm->rtm_flags &= ~RTF_HOST;
825 rtm->rtm_flags |= RTA_NETMASK;
829 rtm->rtm_addrs |= RTA_DST;
831 #define NEXTADDR(w, s) \
832 if (rtm->rtm_addrs & (w)) { \
833 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
835 NEXTADDR(RTA_DST, sin_m);
836 NEXTADDR(RTA_GATEWAY, sdl_m);
837 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
838 NEXTADDR(RTA_NETMASK, so_mask);
840 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
843 rtm->rtm_seq = ++seq;
845 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
846 if (errno != ESRCH || cmd != RTM_DELETE) {
847 perror("writing to routing socket");
852 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
853 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
855 (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
865 struct in6_ndireq nd;
867 char *ifname = argv[0];
870 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
871 perror("ndp: socket");
874 bzero(&nd, sizeof(nd));
875 strcpy(nd.ifname, ifname);
876 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
877 perror("ioctl (SIOCGIFINFO_IN6)");
882 for (i = 1; i < argc; i++) {
891 #define SETFLAG(s, f) \
893 if (strcmp(cp, (s)) == 0) {\
900 SETFLAG("nud", ND6_IFF_PERFORMNUD);
903 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
904 perror("ioctl(SIOCSIFINFO_FLAGS)");
910 printf("linkmtu=%d", ND.linkmtu);
911 printf(", curhlim=%d", ND.chlim);
912 printf(", basereachable=%ds%dms",
913 ND.basereachable / 1000, ND.basereachable % 1000);
914 printf(", reachable=%ds", ND.reachable);
915 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
918 if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
919 printf("PERFORMNUD ");
930 struct in6_drlist dr;
934 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
935 perror("ndp: socket");
938 bzero(&dr, sizeof(dr));
939 strcpy(dr.ifname, "lo0"); /* dummy */
940 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
941 perror("ioctl (SIOCGDRLST_IN6)");
944 #define DR dr.defrouter[i]
945 for (i = 0 ; DR.if_index && i < PRLSTSIZ ; i++) {
946 struct sockaddr_in6 sin6;
948 bzero(&sin6, sizeof(sin6));
949 sin6.sin6_family = AF_INET6;
950 sin6.sin6_len = sizeof(sin6);
951 sin6.sin6_addr = DR.rtaddr;
952 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
953 sizeof(host_buf), NULL, 0,
954 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
956 printf("%s if=%s", host_buf,
957 if_indextoname(DR.if_index, ifix_buf));
958 printf(", flags=%s%s",
959 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
960 DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
961 gettimeofday(&time, 0);
963 printf(", expire=Never\n");
965 printf(", expire=%s\n",
966 sec2str(DR.expire - time.tv_sec));
975 struct in6_prlist pr;
979 gettimeofday(&time, 0);
981 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
982 perror("ndp: socket");
985 bzero(&pr, sizeof(pr));
986 strcpy(pr.ifname, "lo0"); /* dummy */
987 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
988 perror("ioctl (SIOCGPRLST_IN6)");
991 #define PR pr.prefix[i]
992 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
993 printf("%s/%d if=%s\n",
994 inet_ntop(AF_INET6, &PR.prefix, ntop_buf,
995 sizeof(ntop_buf)), PR.prefixlen,
996 if_indextoname(PR.if_index, ifix_buf));
997 gettimeofday(&time, 0);
999 * meaning of fields, especially flags, is very different
1000 * by origin. notify the difference to the users.
1002 printf(" %s", PR.origin == PR_ORIG_RA ? "" : "advertise: ");
1003 printf("flags=%s%s",
1004 PR.raflags.onlink ? "L" : "",
1005 PR.raflags.autonomous ? "A" : "");
1006 if (PR.vltime == ND6_INFINITE_LIFETIME)
1007 printf(" vltime=infinity");
1009 printf(" vltime=%ld", (long)PR.vltime);
1010 if (PR.pltime == ND6_INFINITE_LIFETIME)
1011 printf(", pltime=infinity");
1013 printf(", pltime=%ld", (long)PR.pltime);
1015 printf(", expire=Never");
1016 else if (PR.expire >= time.tv_sec)
1017 printf(", expire=%s",
1018 sec2str(PR.expire - time.tv_sec));
1020 printf(", expired");
1021 switch (PR.origin) {
1023 printf(", origin=RA");
1026 printf(", origin=RR");
1028 case PR_ORIG_STATIC:
1029 printf(", origin=static");
1031 case PR_ORIG_KERNEL:
1032 printf(", origin=kernel");
1035 printf(", origin=?");
1040 * "advertising router" list is meaningful only if the prefix
1041 * information is from RA.
1043 if (PR.origin != PR_ORIG_RA)
1045 else if (PR.advrtrs) {
1047 printf(" advertised by\n");
1048 for (j = 0; j < PR.advrtrs; j++) {
1049 struct sockaddr_in6 sin6;
1050 struct in6_nbrinfo *nbi;
1052 bzero(&sin6, sizeof(sin6));
1053 sin6.sin6_family = AF_INET6;
1054 sin6.sin6_len = sizeof(sin6);
1055 sin6.sin6_addr = PR.advrtr[j];
1056 sin6.sin6_scope_id = PR.if_index; /* XXX */
1057 getnameinfo((struct sockaddr *)&sin6,
1058 sin6.sin6_len, host_buf,
1059 sizeof(host_buf), NULL, 0,
1060 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
1061 printf(" %s", host_buf);
1063 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
1066 switch(nbi->state) {
1067 case ND6_LLINFO_REACHABLE:
1068 case ND6_LLINFO_STALE:
1069 case ND6_LLINFO_DELAY:
1070 case ND6_LLINFO_PROBE:
1071 printf(" (reachable)\n");
1074 printf(" (unreachable)\n");
1078 printf(" (no neighbor state)\n");
1080 if (PR.advrtrs > DRLSTSIZ)
1081 printf(" and %d routers\n",
1082 PR.advrtrs - DRLSTSIZ);
1084 printf(" No advertising router\n");
1093 char dummyif[IFNAMSIZ+8];
1096 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1098 strcpy(dummyif, "lo0"); /* dummy */
1099 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1100 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1106 char dummyif[IFNAMSIZ+8];
1109 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1111 strcpy(dummyif, "lo0"); /* dummy */
1112 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1113 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1121 char dummyif[IFNAMSIZ+8];
1124 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1126 strcpy(dummyif, "lo0"); /* dummy */
1127 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1128 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1133 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1138 struct in6_ndifreq ndifreq;
1139 unsigned int ifindex;
1141 if (strcasecmp(ifname, "delete") == 0)
1144 if ((ifindex = if_nametoindex(ifname)) == 0)
1145 err(1, "failed to resolve i/f index for %s", ifname);
1148 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1151 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1152 ndifreq.ifindex = ifindex;
1154 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1155 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1163 struct in6_ndifreq ndifreq;
1164 char ifname[IFNAMSIZ+8];
1166 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1169 memset(&ndifreq, 0, sizeof(ndifreq));
1170 strcpy(ndifreq.ifname, "lo0"); /* dummy */
1172 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1173 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1175 if (ndifreq.ifindex == 0)
1176 printf("No default interface.\n");
1178 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1179 err(1, "failed to resolve ifname for index %lu",
1181 printf("ND default interface = %s\n", ifname);
1192 static char result[256];
1193 int days, hours, mins, secs;
1197 days = total / 3600 / 24;
1198 hours = (total / 3600) % 24;
1199 mins = (total / 60) % 60;
1204 p += sprintf(p, "%dd", days);
1206 if (!first || hours) {
1208 p += sprintf(p, "%dh", hours);
1210 if (!first || mins) {
1212 p += sprintf(p, "%dm", mins);
1214 sprintf(p, "%ds", secs);
1220 * Print the timestamp
1221 * from tcpdump/util.c
1225 const struct timeval *tvp;
1230 s = (tvp->tv_sec + thiszone) % 86400;
1231 (void)printf("%02d:%02d:%02d.%06u ",
1232 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);