1 /* $OpenBSD: ndp.c,v 1.47 2013/03/21 04:43:17 deraadt Exp $ */
2 /* $KAME: ndp.c,v 1.101 2002/07/17 08:46:33 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_dl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet6/nd6.h>
96 #include <arpa/inet.h>
108 #include "gmt2local.h"
110 /* packing rule for routing socket */
112 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
113 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
118 static int32_t thiszone
; /* time difference with gmt */
120 static int repeat
= 0;
122 char ntop_buf
[INET6_ADDRSTRLEN
]; /* inet_ntop() */
123 char host_buf
[NI_MAXHOST
]; /* getnameinfo() */
124 char ifix_buf
[IFNAMSIZ
]; /* if_indextoname() */
127 void getsocket(void);
128 int set(int, char **);
131 void dump(struct in6_addr
*, int);
132 static struct in6_nbrinfo
*getnbrinfo(struct in6_addr
*, int, int);
133 static char *ether_str(struct sockaddr_dl
*);
134 int ndp_ether_aton(char *, u_char
*);
137 void ifinfo(char *, int, char **);
140 void pfx_flush(void);
142 void rtr_flush(void);
143 void harmonize_rtr(void);
144 static char *sec2str(time_t);
145 static char *ether_str(struct sockaddr_dl
*);
146 static void ts_print(const struct timeval
*);
148 #ifdef ICMPV6CTL_ND6_DRLIST
149 static char *rtpref_str
[] = {
161 main(int argc
, char *argv
[])
166 thiszone
= gmt2local(0);
167 while ((ch
= getopt(argc
, argv
, "acd:f:i:nprstA:HPR")) != -1)
206 repeat
= atoi(optarg
);
226 dump(0, mode
== 'c');
243 ifinfo(arg
, argc
, argv
);
253 if (argc
< 2 || argc
> 4)
255 exit(set(argc
, argv
) ? 1 : 0);
289 * Process a file to set standard ndp entries
296 char line
[100], arg
[5][50], *args
[5];
298 if ((fp
= fopen(name
, "r")) == NULL
) {
299 fprintf(stderr
, "ndp: cannot open %s\n", name
);
302 args
[0] = &arg
[0][0];
303 args
[1] = &arg
[1][0];
304 args
[2] = &arg
[2][0];
305 args
[3] = &arg
[3][0];
306 args
[4] = &arg
[4][0];
308 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
309 i
= sscanf(line
, "%49s %49s %49s %49s %49s",
310 arg
[0], arg
[1], arg
[2], arg
[3], arg
[4]);
312 fprintf(stderr
, "ndp: bad line: %s\n", line
);
327 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
335 struct sockaddr_in6 so_mask
= {sizeof(so_mask
), AF_INET6
};
336 struct sockaddr_in6 blank_sin
= {sizeof(blank_sin
), AF_INET6
}, sin_m
;
337 struct sockaddr_dl blank_sdl
= {sizeof(blank_sdl
), AF_LINK
}, sdl_m
;
338 int expire_time
, flags
, found_entry
;
340 struct rt_msghdr m_rtm
;
345 * Set an individual neighbor cache entry
348 set(int argc
, char **argv
)
350 struct sockaddr_in6
*sin
= &sin_m
;
351 struct sockaddr_dl
*sdl
;
352 struct rt_msghdr
*rtm
= &(m_rtmsg
.m_rtm
);
353 struct addrinfo hints
, *res
;
356 char *host
= argv
[0], *eaddr
= argv
[1];
364 bzero(&hints
, sizeof(hints
));
365 hints
.ai_family
= AF_INET6
;
366 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
368 fprintf(stderr
, "ndp: %s: %s\n", host
,
369 gai_strerror(gai_error
));
372 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
374 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
375 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
376 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
379 ea
= (u_char
*)LLADDR(&sdl_m
);
380 if (ndp_ether_aton(eaddr
, ea
) == 0)
382 flags
= expire_time
= 0;
384 if (strncmp(argv
[0], "temp", 4) == 0) {
387 gettimeofday(&time
, 0);
388 expire_time
= time
.tv_sec
+ 20 * 60;
389 } else if (strncmp(argv
[0], "proxy", 5) == 0)
390 flags
|= RTF_ANNOUNCE
;
393 if (rtmsg(RTM_GET
) < 0) {
394 errx(1, "RTM_GET(%s) failed", host
);
397 sin
= (struct sockaddr_in6
*)((char *)rtm
+ rtm
->rtm_hdrlen
);
398 sdl
= (struct sockaddr_dl
*)(ROUNDUP(sin
->sin6_len
) + (char *)sin
);
399 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
400 if (sdl
->sdl_family
== AF_LINK
&&
401 (rtm
->rtm_flags
& RTF_LLINFO
) &&
402 !(rtm
->rtm_flags
& RTF_GATEWAY
)) {
403 switch (sdl
->sdl_type
) {
404 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
405 case IFT_ISO88024
: case IFT_ISO88025
:
410 * IPv4 arp command retries with sin_other = SIN_PROXY here.
412 fprintf(stderr
, "set: cannot configure a new entry\n");
417 if (sdl
->sdl_family
!= AF_LINK
) {
418 printf("cannot intuit interface index and type for %s\n", host
);
421 sdl_m
.sdl_type
= sdl
->sdl_type
;
422 sdl_m
.sdl_index
= sdl
->sdl_index
;
423 return (rtmsg(RTM_ADD
));
427 * Display an individual neighbor cache entry
432 struct sockaddr_in6
*sin
= &sin_m
;
433 struct addrinfo hints
, *res
;
437 bzero(&hints
, sizeof(hints
));
438 hints
.ai_family
= AF_INET6
;
439 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
441 fprintf(stderr
, "ndp: %s: %s\n", host
,
442 gai_strerror(gai_error
));
445 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
447 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
448 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
449 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
452 dump(&sin
->sin6_addr
, 0);
453 if (found_entry
== 0) {
454 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
455 sizeof(host_buf
), NULL
,0,
456 (nflag
? NI_NUMERICHOST
: 0));
457 printf("%s (%s) -- no entry\n", host
, host_buf
);
463 * Delete a neighbor cache entry
468 struct sockaddr_in6
*sin
= &sin_m
;
469 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
470 struct sockaddr_dl
*sdl
;
471 struct addrinfo hints
, *res
;
477 bzero(&hints
, sizeof(hints
));
478 hints
.ai_family
= AF_INET6
;
479 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
481 fprintf(stderr
, "ndp: %s: %s\n", host
,
482 gai_strerror(gai_error
));
485 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
487 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
488 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
489 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
492 if (rtmsg(RTM_GET
) < 0) {
493 errx(1, "RTM_GET(%s) failed", host
);
496 sin
= (struct sockaddr_in6
*)((char *)rtm
+ rtm
->rtm_hdrlen
);
497 sdl
= (struct sockaddr_dl
*)(ROUNDUP(sin
->sin6_len
) + (char *)sin
);
498 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
499 if (sdl
->sdl_family
== AF_LINK
&&
500 (rtm
->rtm_flags
& RTF_LLINFO
) &&
501 !(rtm
->rtm_flags
& RTF_GATEWAY
)) {
505 * IPv4 arp command retries with sin_other = SIN_PROXY here.
507 fprintf(stderr
, "delete: cannot delete non-NDP entry\n");
512 if (sdl
->sdl_family
!= AF_LINK
) {
513 printf("cannot locate %s\n", host
);
516 if (rtmsg(RTM_DELETE
) == 0) {
517 struct sockaddr_in6 s6
= *sin
; /* XXX: for safety */
520 if (IN6_IS_ADDR_LINKLOCAL(&s6
.sin6_addr
)) {
521 s6
.sin6_scope_id
= ntohs(*(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2]);
522 *(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2] = 0;
525 getnameinfo((struct sockaddr
*)&s6
,
526 s6
.sin6_len
, host_buf
,
527 sizeof(host_buf
), NULL
, 0,
528 (nflag
? NI_NUMERICHOST
: 0));
529 printf("%s (%s) deleted\n", host
, host_buf
);
540 * Dump the entire neighbor cache
543 dump(struct in6_addr
*addr
, int cflag
)
547 char *lim
, *buf
= NULL
, *next
;
548 struct rt_msghdr
*rtm
;
549 struct sockaddr_in6
*sin
;
550 struct sockaddr_dl
*sdl
;
551 struct in6_nbrinfo
*nbi
;
560 if (!tflag
&& !cflag
)
561 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
562 W_ADDR
, W_ADDR
, "Neighbor", W_LL
, W_LL
, "Linklayer Address",
563 W_IF
, W_IF
, "Netif", "Expire", "S", "Flags");
570 mib
[4] = NET_RT_FLAGS
;
573 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) == -1)
574 err(1, "sysctl(PF_ROUTE estimate)");
577 if ((buf
= realloc(buf
, needed
)) == NULL
)
579 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) == -1) {
582 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
588 for (next
= buf
; next
&& next
< lim
; next
+= rtm
->rtm_msglen
) {
589 int isrouter
= 0, prbs
= 0;
591 rtm
= (struct rt_msghdr
*)next
;
592 if (rtm
->rtm_version
!= RTM_VERSION
)
594 sin
= (struct sockaddr_in6
*)(next
+ rtm
->rtm_hdrlen
);
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 is 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
)
613 if (!(rtm
->rtm_flags
& RTF_HOST
))
617 if (!IN6_ARE_ADDR_EQUAL(addr
, &sin
->sin6_addr
))
620 } else if (IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
))
622 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) ||
623 IN6_IS_ADDR_MC_LINKLOCAL(&sin
->sin6_addr
)) {
624 /* XXX: should scope id be filled in the kernel? */
625 if (sin
->sin6_scope_id
== 0)
626 sin
->sin6_scope_id
= sdl
->sdl_index
;
628 /* KAME specific hack; removed the embedded id */
629 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
632 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
633 sizeof(host_buf
), NULL
, 0, (nflag
? NI_NUMERICHOST
: 0));
636 if (rtm
->rtm_flags
& RTF_WASCLONED
)
638 #elif defined(RTF_CLONED)
639 if (rtm
->rtm_flags
& RTF_CLONED
)
646 gettimeofday(&time
, 0);
650 addrwidth
= strlen(host_buf
);
651 if (addrwidth
< W_ADDR
)
653 llwidth
= strlen(ether_str(sdl
));
654 if (W_ADDR
+ W_LL
- addrwidth
> llwidth
)
655 llwidth
= W_ADDR
+ W_LL
- addrwidth
;
656 ifname
= if_indextoname(sdl
->sdl_index
, ifix_buf
);
659 ifwidth
= strlen(ifname
);
660 if (W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
> ifwidth
)
661 ifwidth
= W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
;
663 printf("%-*.*s %-*.*s %*.*s", addrwidth
, addrwidth
, host_buf
,
664 llwidth
, llwidth
, ether_str(sdl
), ifwidth
, ifwidth
, ifname
);
666 /* Print neighbor discovery specific informations */
667 nbi
= getnbrinfo(&sin
->sin6_addr
, sdl
->sdl_index
, 1);
669 if (nbi
->expire
> time
.tv_sec
) {
671 sec2str(nbi
->expire
- time
.tv_sec
));
672 } else if (nbi
->expire
== 0)
673 printf(" %-9.9s", "permanent");
675 printf(" %-9.9s", "expired");
677 switch (nbi
->state
) {
678 case ND6_LLINFO_NOSTATE
:
681 #ifdef ND6_LLINFO_WAITDELETE
682 case ND6_LLINFO_WAITDELETE
:
686 case ND6_LLINFO_INCOMPLETE
:
689 case ND6_LLINFO_REACHABLE
:
692 case ND6_LLINFO_STALE
:
695 case ND6_LLINFO_DELAY
:
698 case ND6_LLINFO_PROBE
:
706 isrouter
= nbi
->isrouter
;
709 warnx("failed to get neighbor information");
714 * other flags. R: router, P: proxy, W: ??
716 if ((rtm
->rtm_addrs
& RTA_NETMASK
) == 0) {
717 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s",
719 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
721 sin
= (struct sockaddr_in6
*)
722 (sdl
->sdl_len
+ (char *)sdl
);
723 #if 0 /* W and P are mystery even for us */
724 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s%s%s",
726 !IN6_IS_ADDR_UNSPECIFIED(&sin
->sin6_addr
) ? "P" : "",
727 (sin
->sin6_len
!= sizeof(struct sockaddr_in6
)) ? "W" : "",
728 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
730 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s",
732 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
735 printf(" %s", flgbuf
);
753 static struct in6_nbrinfo
*
754 getnbrinfo(struct in6_addr
*addr
, int ifindex
, int warning
)
756 static struct in6_nbrinfo nbi
;
759 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
762 bzero(&nbi
, sizeof(nbi
));
763 if_indextoname(ifindex
, nbi
.ifname
);
765 if (ioctl(s
, SIOCGNBRINFO_IN6
, (caddr_t
)&nbi
) < 0) {
767 warn("ioctl(SIOCGNBRINFO_IN6)");
777 ether_str(struct sockaddr_dl
*sdl
)
779 static char hbuf
[NI_MAXHOST
];
783 cp
= (u_char
*)LLADDR(sdl
);
784 snprintf(hbuf
, sizeof(hbuf
), "%x:%x:%x:%x:%x:%x",
785 cp
[0], cp
[1], cp
[2], cp
[3], cp
[4], cp
[5]);
787 snprintf(hbuf
, sizeof(hbuf
), "(incomplete)");
793 ndp_ether_aton(char *a
, u_char
*n
)
797 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x", &o
[0], &o
[1], &o
[2],
798 &o
[3], &o
[4], &o
[5]);
800 fprintf(stderr
, "ndp: invalid Ethernet address '%s'\n", a
);
803 for (i
= 0; i
< 6; i
++)
811 printf("usage: ndp [-nrt] [-a | -c | -p] [-H | -P | -R] ");
812 printf("[-A wait] [-d hostname]\n");
813 printf("\t[-f filename] ");
814 printf("[-i interface [flag ...]]\n");
815 printf("\t[-s nodename etheraddr [temp] [proxy]] [hostname]\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
;
842 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
843 rtm
->rtm_inits
= RTV_EXPIRE
;
845 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
846 #if 0 /* we don't support ipv6addr/128 type proxying. */
847 if (rtm
->rtm_flags
& RTF_ANNOUNCE
) {
848 rtm
->rtm_flags
&= ~RTF_HOST
;
849 rtm
->rtm_addrs
|= RTA_NETMASK
;
854 rtm
->rtm_addrs
|= RTA_DST
;
856 #define NEXTADDR(w, s) \
857 if (rtm->rtm_addrs & (w)) { \
858 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
860 NEXTADDR(RTA_DST
, sin_m
);
861 NEXTADDR(RTA_GATEWAY
, sdl_m
);
862 #if 0 /* we don't support ipv6addr/128 type proxying. */
863 memset(&so_mask
.sin6_addr
, 0xff, sizeof(so_mask
.sin6_addr
));
864 NEXTADDR(RTA_NETMASK
, so_mask
);
867 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
870 rtm
->rtm_seq
= ++seq
;
872 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
873 if (errno
!= ESRCH
|| cmd
!= RTM_DELETE
) {
874 err(1, "writing to routing socket");
879 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
880 } while (l
> 0 && (rtm
->rtm_version
!= RTM_VERSION
||
881 rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
883 (void) fprintf(stderr
, "ndp: read from routing socket: %s\n",
889 ifinfo(char *ifname
, int argc
, char **argv
)
891 struct in6_ndireq nd
;
894 #ifdef IPV6CTL_USETEMPADDR
898 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
902 bzero(&nd
, sizeof(nd
));
903 strlcpy(nd
.ifname
, ifname
, sizeof(nd
.ifname
));
904 if (ioctl(s
, SIOCGIFINFO_IN6
, (caddr_t
)&nd
) < 0) {
905 err(1, "ioctl(SIOCGIFINFO_IN6)");
910 for (i
= 0; i
< argc
; i
++) {
919 #define SETFLAG(s, f) \
921 if (strcmp(cp, (s)) == 0) {\
928 SETFLAG("nud", ND6_IFF_PERFORMNUD
);
929 #ifdef ND6_IFF_ACCEPT_RTADV
930 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV
);
932 #ifdef ND6_IFF_PREFER_SOURCE
933 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE
);
937 if (ioctl(s
, SIOCSIFINFO_FLAGS
, (caddr_t
)&nd
) < 0) {
938 err(1, "ioctl(SIOCSIFINFO_FLAGS)");
944 if (!ND
.initialized
) {
945 errx(1, "%s: not initialized yet", ifname
);
949 printf("linkmtu=%d", ND
.linkmtu
);
950 printf(", curhlim=%d", ND
.chlim
);
951 printf(", basereachable=%ds%dms",
952 ND
.basereachable
/ 1000, ND
.basereachable
% 1000);
953 printf(", reachable=%ds", ND
.reachable
);
954 printf(", retrans=%ds%dms", ND
.retrans
/ 1000, ND
.retrans
% 1000);
955 #ifdef IPV6CTL_USETEMPADDR
956 memset(nullbuf
, 0, sizeof(nullbuf
));
957 if (memcmp(nullbuf
, ND
.randomid
, sizeof(nullbuf
)) != 0) {
961 for (i
= 0; i
< 3; i
++) {
964 printf("\nRandom seed(0): ");
965 rbuf
= ND
.randomseed0
;
968 printf("\nRandom seed(1): ");
969 rbuf
= ND
.randomseed1
;
972 printf("\nRandom ID: ");
976 for (j
= 0; j
< 8; j
++)
977 printf("%02x", rbuf
[j
]);
983 if ((ND
.flags
& ND6_IFF_PERFORMNUD
))
985 #ifdef ND6_IFF_ACCEPT_RTADV
986 if ((ND
.flags
& ND6_IFF_ACCEPT_RTADV
))
987 printf("accept_rtadv ");
989 #ifdef ND6_IFF_PREFER_SOURCE
990 if ((ND
.flags
& ND6_IFF_PREFER_SOURCE
))
991 printf("prefer_source ");
1000 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1001 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1007 #ifdef ICMPV6CTL_ND6_DRLIST
1008 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_DRLIST
};
1010 struct in6_defrouter
*p
, *ep
;
1012 struct timeval time
;
1014 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), NULL
, &l
, NULL
, 0) < 0) {
1015 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1025 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), buf
, &l
, NULL
, 0) < 0) {
1026 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1030 ep
= (struct in6_defrouter
*)(buf
+ l
);
1031 for (p
= (struct in6_defrouter
*)buf
; p
< ep
; p
++) {
1034 if (getnameinfo((struct sockaddr
*)&p
->rtaddr
,
1035 p
->rtaddr
.sin6_len
, host_buf
, sizeof(host_buf
), NULL
, 0,
1036 (nflag
? NI_NUMERICHOST
: 0)) != 0)
1037 strlcpy(host_buf
, "?", sizeof(host_buf
));
1039 printf("%s if=%s", host_buf
,
1040 if_indextoname(p
->if_index
, ifix_buf
));
1041 printf(", flags=%s%s",
1042 p
->flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1043 p
->flags
& ND_RA_FLAG_OTHER
? "O" : "");
1044 rtpref
= ((p
->flags
& ND_RA_FLAG_RTPREF_MASK
) >> 3) & 0xff;
1045 printf(", pref=%s", rtpref_str
[rtpref
]);
1047 gettimeofday(&time
, 0);
1049 printf(", expire=Never\n");
1051 printf(", expire=%s\n",
1052 sec2str(p
->expire
- time
.tv_sec
));
1056 struct in6_drlist dr
;
1058 struct timeval time
;
1060 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1064 bzero(&dr
, sizeof(dr
));
1065 strlcpy(dr
.ifname
, "lo0", sizeof(dr
.ifname
)); /* dummy */
1066 if (ioctl(s
, SIOCGDRLST_IN6
, (caddr_t
)&dr
) < 0) {
1067 err(1, "ioctl(SIOCGDRLST_IN6)");
1070 #define DR dr.defrouter[i]
1071 for (i
= 0 ; DR
.if_index
&& i
< DRLSTSIZ
; i
++) {
1072 struct sockaddr_in6 sin6
;
1074 bzero(&sin6
, sizeof(sin6
));
1075 sin6
.sin6_family
= AF_INET6
;
1076 sin6
.sin6_len
= sizeof(sin6
);
1077 sin6
.sin6_addr
= DR
.rtaddr
;
1078 getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
, host_buf
,
1079 sizeof(host_buf
), NULL
, 0,
1080 (nflag
? NI_NUMERICHOST
: 0));
1082 printf("%s if=%s", host_buf
,
1083 if_indextoname(DR
.if_index
, ifix_buf
));
1084 printf(", flags=%s%s",
1085 DR
.flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1086 DR
.flags
& ND_RA_FLAG_OTHER
? "O" : "");
1087 gettimeofday(&time
, 0);
1089 printf(", expire=Never\n");
1091 printf(", expire=%s\n",
1092 sec2str(DR
.expire
- time
.tv_sec
));
1102 #ifdef ICMPV6CTL_ND6_PRLIST
1103 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_PRLIST
};
1105 struct in6_prefix
*p
, *ep
, *n
;
1106 struct sockaddr_in6
*advrtr
;
1108 struct timeval time
;
1109 const int niflags
= NI_NUMERICHOST
;
1110 int ninflags
= nflag
? NI_NUMERICHOST
: 0;
1111 char namebuf
[NI_MAXHOST
];
1113 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), NULL
, &l
, NULL
, 0) < 0) {
1114 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1122 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), buf
, &l
, NULL
, 0) < 0) {
1123 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1127 ep
= (struct in6_prefix
*)(buf
+ l
);
1128 for (p
= (struct in6_prefix
*)buf
; p
< ep
; p
= n
) {
1129 advrtr
= (struct sockaddr_in6
*)(p
+ 1);
1130 n
= (struct in6_prefix
*)&advrtr
[p
->advrtrs
];
1132 if (getnameinfo((struct sockaddr
*)&p
->prefix
,
1133 p
->prefix
.sin6_len
, namebuf
, sizeof(namebuf
),
1134 NULL
, 0, niflags
) != 0)
1135 strlcpy(namebuf
, "?", sizeof(namebuf
));
1136 printf("%s/%d if=%s\n", namebuf
, p
->prefixlen
,
1137 if_indextoname(p
->if_index
, ifix_buf
));
1139 gettimeofday(&time
, 0);
1141 * meaning of fields, especially flags, is very different
1142 * by origin. notify the difference to the users.
1144 printf("flags=%s%s%s%s%s",
1145 p
->raflags
.onlink
? "L" : "",
1146 p
->raflags
.autonomous
? "A" : "",
1147 (p
->flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1148 (p
->flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1150 (p
->flags
& NDPRF_HOME
) != 0 ? "H" : ""
1155 if (p
->vltime
== ND6_INFINITE_LIFETIME
)
1156 printf(" vltime=infinity");
1158 printf(" vltime=%lu", (unsigned long)p
->vltime
);
1159 if (p
->pltime
== ND6_INFINITE_LIFETIME
)
1160 printf(", pltime=infinity");
1162 printf(", pltime=%lu", (unsigned long)p
->pltime
);
1164 printf(", expire=Never");
1165 else if (p
->expire
>= time
.tv_sec
)
1166 printf(", expire=%s",
1167 sec2str(p
->expire
- time
.tv_sec
));
1169 printf(", expired");
1170 printf(", ref=%d", p
->refcnt
);
1173 * "advertising router" list is meaningful only if the prefix
1174 * information is from RA.
1178 struct sockaddr_in6
*sin6
;
1181 printf(" advertised by\n");
1182 for (j
= 0; j
< p
->advrtrs
; j
++) {
1183 struct in6_nbrinfo
*nbi
;
1185 if (getnameinfo((struct sockaddr
*)sin6
,
1186 sin6
->sin6_len
, namebuf
, sizeof(namebuf
),
1187 NULL
, 0, ninflags
) != 0)
1188 strlcpy(namebuf
, "?", sizeof(namebuf
));
1189 printf(" %s", namebuf
);
1191 nbi
= getnbrinfo(&sin6
->sin6_addr
,
1194 switch (nbi
->state
) {
1195 case ND6_LLINFO_REACHABLE
:
1196 case ND6_LLINFO_STALE
:
1197 case ND6_LLINFO_DELAY
:
1198 case ND6_LLINFO_PROBE
:
1199 printf(" (reachable)\n");
1202 printf(" (unreachable)\n");
1205 printf(" (no neighbor state)\n");
1209 printf(" No advertising router\n");
1213 struct in6_prlist pr
;
1215 struct timeval time
;
1217 gettimeofday(&time
, 0);
1219 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1223 bzero(&pr
, sizeof(pr
));
1224 strlcpy(pr
.ifname
, "lo0", sizeof(pr
.ifname
)); /* dummy */
1225 if (ioctl(s
, SIOCGPRLST_IN6
, (caddr_t
)&pr
) < 0) {
1226 err(1, "ioctl(SIOCGPRLST_IN6)");
1229 #define PR pr.prefix[i]
1230 for (i
= 0; PR
.if_index
&& i
< PRLSTSIZ
; i
++) {
1231 struct sockaddr_in6 p6
;
1232 char namebuf
[NI_MAXHOST
];
1238 memset(&p6
, 0, sizeof(p6
));
1239 p6
.sin6_family
= AF_INET6
;
1240 p6
.sin6_len
= sizeof(p6
);
1241 p6
.sin6_addr
= PR
.prefix
;
1245 * copy link index to sin6_scope_id field.
1246 * XXX: KAME specific.
1248 if (IN6_IS_ADDR_LINKLOCAL(&p6
.sin6_addr
)) {
1251 memcpy(&linkid
, &p6
.sin6_addr
.s6_addr
[2],
1253 linkid
= ntohs(linkid
);
1254 p6
.sin6_scope_id
= linkid
;
1255 p6
.sin6_addr
.s6_addr
[2] = 0;
1256 p6
.sin6_addr
.s6_addr
[3] = 0;
1259 niflags
= NI_NUMERICHOST
;
1260 if (getnameinfo((struct sockaddr
*)&p6
,
1261 sizeof(p6
), namebuf
, sizeof(namebuf
),
1262 NULL
, 0, niflags
)) {
1263 warnx("getnameinfo failed");
1266 printf("%s/%d if=%s\n", namebuf
, PR
.prefixlen
,
1267 if_indextoname(PR
.if_index
, ifix_buf
));
1269 gettimeofday(&time
, 0);
1271 * meaning of fields, especially flags, is very different
1272 * by origin. notify the difference to the users.
1276 PR
.origin
== PR_ORIG_RA
? "" : "advertise: ");
1279 printf("flags=%s%s%s%s%s",
1280 PR
.raflags
.onlink
? "L" : "",
1281 PR
.raflags
.autonomous
? "A" : "",
1282 (PR
.flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1283 (PR
.flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1285 (PR
.flags
& NDPRF_HOME
) != 0 ? "H" : ""
1291 printf("flags=%s%s",
1292 PR
.raflags
.onlink
? "L" : "",
1293 PR
.raflags
.autonomous
? "A" : "");
1295 if (PR
.vltime
== ND6_INFINITE_LIFETIME
)
1296 printf(" vltime=infinity");
1298 printf(" vltime=%lu", PR
.vltime
);
1299 if (PR
.pltime
== ND6_INFINITE_LIFETIME
)
1300 printf(", pltime=infinity");
1302 printf(", pltime=%lu", PR
.pltime
);
1304 printf(", expire=Never");
1305 else if (PR
.expire
>= time
.tv_sec
)
1306 printf(", expire=%s",
1307 sec2str(PR
.expire
- time
.tv_sec
));
1309 printf(", expired");
1311 printf(", ref=%d", PR
.refcnt
);
1314 switch (PR
.origin
) {
1316 printf(", origin=RA");
1319 printf(", origin=RR");
1321 case PR_ORIG_STATIC
:
1322 printf(", origin=static");
1324 case PR_ORIG_KERNEL
:
1325 printf(", origin=kernel");
1328 printf(", origin=?");
1334 * "advertising router" list is meaningful only if the prefix
1335 * information is from RA.
1337 if (0 && /* prefix origin is almost obsolted */
1338 PR
.origin
!= PR_ORIG_RA
)
1340 else if (PR
.advrtrs
) {
1342 printf(" advertised by\n");
1343 for (j
= 0; j
< PR
.advrtrs
; j
++) {
1344 struct sockaddr_in6 sin6
;
1345 struct in6_nbrinfo
*nbi
;
1347 bzero(&sin6
, sizeof(sin6
));
1348 sin6
.sin6_family
= AF_INET6
;
1349 sin6
.sin6_len
= sizeof(sin6
);
1350 sin6
.sin6_addr
= PR
.advrtr
[j
];
1351 sin6
.sin6_scope_id
= PR
.if_index
; /* XXX */
1352 getnameinfo((struct sockaddr
*)&sin6
,
1353 sin6
.sin6_len
, host_buf
,
1354 sizeof(host_buf
), NULL
, 0,
1355 (nflag
? NI_NUMERICHOST
: 0));
1356 printf(" %s", host_buf
);
1358 nbi
= getnbrinfo(&sin6
.sin6_addr
,
1361 switch (nbi
->state
) {
1362 case ND6_LLINFO_REACHABLE
:
1363 case ND6_LLINFO_STALE
:
1364 case ND6_LLINFO_DELAY
:
1365 case ND6_LLINFO_PROBE
:
1366 printf(" (reachable)\n");
1369 printf(" (unreachable)\n");
1372 printf(" (no neighbor state)\n");
1374 if (PR
.advrtrs
> DRLSTSIZ
)
1375 printf(" and %d routers\n",
1376 PR
.advrtrs
- DRLSTSIZ
);
1378 printf(" No advertising router\n");
1388 char dummyif
[IFNAMSIZ
+8];
1391 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1393 strlcpy(dummyif
, "lo0", sizeof(dummyif
)); /* dummy */
1394 if (ioctl(s
, SIOCSPFXFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1395 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1402 char dummyif
[IFNAMSIZ
+8];
1405 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1407 strlcpy(dummyif
, "lo0", sizeof(dummyif
)); /* dummy */
1408 if (ioctl(s
, SIOCSRTRFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1409 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1417 char dummyif
[IFNAMSIZ
+8];
1420 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1422 strlcpy(dummyif
, "lo0", sizeof(dummyif
)); /* dummy */
1423 if (ioctl(s
, SIOCSNDFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1424 err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1430 sec2str(time_t total
)
1432 static char result
[256];
1433 int days
, hours
, mins
, secs
;
1436 char *ep
= &result
[sizeof(result
)];
1439 days
= total
/ 3600 / 24;
1440 hours
= (total
/ 3600) % 24;
1441 mins
= (total
/ 60) % 60;
1446 n
= snprintf(p
, ep
- p
, "%dd", days
);
1447 if (n
< 0 || n
>= ep
- p
)
1451 if (!first
|| hours
) {
1453 n
= snprintf(p
, ep
- p
, "%dh", hours
);
1454 if (n
< 0 || n
>= ep
- p
)
1458 if (!first
|| mins
) {
1460 n
= snprintf(p
, ep
- p
, "%dm", mins
);
1461 if (n
< 0 || n
>= ep
- p
)
1465 snprintf(p
, ep
- p
, "%ds", secs
);
1471 * Print the timestamp
1472 * from tcpdump/util.c
1475 ts_print(const struct timeval
*tvp
)
1480 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
1481 (void)printf("%02d:%02d:%02d.%06u ",
1482 s
/ 3600, (s
% 3600) / 60, s
% 60, (u_int32_t
)tvp
->tv_usec
);