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
120 static int32_t thiszone
; /* time difference with gmt */
122 static int repeat
= 0;
124 char ntop_buf
[INET6_ADDRSTRLEN
]; /* inet_ntop() */
125 char host_buf
[NI_MAXHOST
]; /* getnameinfo() */
126 char ifix_buf
[IFNAMSIZ
]; /* if_indextoname() */
129 void getsocket(void);
130 int set(int, char **);
133 void dump(struct in6_addr
*);
134 static struct in6_nbrinfo
*getnbrinfo(struct in6_addr
*addr
,
136 static char *ether_str(struct sockaddr_dl
*);
137 int ndp_ether_aton(char *, u_char
*);
138 void usage(void) __dead2
;
140 void ifinfo(int, char **);
143 void pfx_flush(void);
145 void rtr_flush(void);
146 void harmonize_rtr(void);
147 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
148 static void getdefif(void);
149 static void setdefif(char *);
151 static char *sec2str(time_t t
);
152 static char *ether_str(struct sockaddr_dl
*sdl
);
153 static void ts_print(const struct timespec
*);
155 static char *rtpref_str
[] = {
163 main(int argc
, char **argv
)
166 int aflag
= 0, dflag
= 0, sflag
= 0, Hflag
= 0,
167 pflag
= 0, rflag
= 0, Pflag
= 0, Rflag
= 0;
170 thiszone
= gmt2local(0);
171 while ((ch
= getopt(argc
, argv
, "acndfIilprstA:HPR")) != -1)
183 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
186 getdefif(); /* always call it to print the result */
189 errx(1, "not supported yet");
211 /* obsolete, ignored */
224 repeat
= atoi(optarg
);
244 if (aflag
|| cflag
) {
263 if (argc
< 2 || argc
> 4)
265 exit(set(argc
, argv
) ? 1 : 0);
287 * Process a file to set standard ndp entries
294 char line
[100], arg
[5][50], *args
[5];
296 if ((fp
= fopen(name
, "r")) == NULL
) {
297 fprintf(stderr
, "ndp: cannot open %s\n", name
);
300 args
[0] = &arg
[0][0];
301 args
[1] = &arg
[1][0];
302 args
[2] = &arg
[2][0];
303 args
[3] = &arg
[3][0];
304 args
[4] = &arg
[4][0];
306 while(fgets(line
, 100, fp
) != NULL
) {
307 i
= sscanf(line
, "%s %s %s %s %s", arg
[0], arg
[1], arg
[2],
310 fprintf(stderr
, "ndp: bad line: %s\n", line
);
325 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
327 perror("ndp: socket");
333 struct sockaddr_in6 so_mask
= {sizeof(so_mask
), AF_INET6
};
334 struct sockaddr_in6 blank_sin
= {sizeof(blank_sin
), AF_INET6
}, sin_m
;
335 struct sockaddr_dl blank_sdl
= {sizeof(blank_sdl
), AF_LINK
}, sdl_m
;
336 int flags
, found_entry
;
339 struct rt_msghdr m_rtm
;
344 * Set an individual neighbor cache entry
347 set(int argc
, char **argv
)
349 struct sockaddr_in6
*sin
= &sin_m
;
350 struct sockaddr_dl
*sdl
;
351 struct rt_msghdr
*rtm
= &(m_rtmsg
.m_rtm
);
352 struct addrinfo hints
, *res
;
355 char *host
= argv
[0], *eaddr
= argv
[1];
363 bzero(&hints
, sizeof(hints
));
364 hints
.ai_family
= AF_INET6
;
365 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
367 fprintf(stderr
, "ndp: %s: %s\n", host
,
368 gai_strerror(gai_error
));
371 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
373 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
374 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
375 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
378 ea
= (u_char
*)LLADDR(&sdl_m
);
379 if (ndp_ether_aton(eaddr
, ea
) == 0)
384 if (strncmp(argv
[0], "temp", 4) == 0) {
387 clock_gettime(CLOCK_MONOTONIC
, &sp
);
388 expire_time
= sp
.tv_sec
+ 20 * 60;
389 } else if (strncmp(argv
[0], "proxy", 5) == 0)
390 flags
|= RTF_ANNOUNCE
;
393 if (rtmsg(RTM_GET
) < 0) {
397 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
398 sdl
= (struct sockaddr_dl
*)(RT_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
)) switch (sdl
->sdl_type
) {
403 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
404 case IFT_ISO88024
: case IFT_ISO88025
:
408 * IPv4 arp command retries with sin_other = SIN_PROXY here.
410 fprintf(stderr
, "set: cannot configure a new entry\n");
415 if (sdl
->sdl_family
!= AF_LINK
) {
416 printf("cannot intuit interface index and type for %s\n", host
);
419 sdl_m
.sdl_type
= sdl
->sdl_type
;
420 sdl_m
.sdl_index
= sdl
->sdl_index
;
421 return (rtmsg(RTM_ADD
));
425 * Display an individual neighbor cache entry
430 struct sockaddr_in6
*sin
= &sin_m
;
431 struct addrinfo hints
, *res
;
435 bzero(&hints
, sizeof(hints
));
436 hints
.ai_family
= AF_INET6
;
437 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
439 fprintf(stderr
, "ndp: %s: %s\n", host
,
440 gai_strerror(gai_error
));
443 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
445 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
446 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
447 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
450 dump(&sin
->sin6_addr
);
451 if (found_entry
== 0) {
452 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
453 sizeof(host_buf
), NULL
,0,
454 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
455 printf("%s (%s) -- no entry\n", host
, host_buf
);
461 * Delete a neighbor cache entry
466 struct sockaddr_in6
*sin
= &sin_m
;
467 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
468 struct sockaddr_dl
*sdl
;
469 struct addrinfo hints
, *res
;
475 bzero(&hints
, sizeof(hints
));
476 hints
.ai_family
= AF_INET6
;
477 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
479 fprintf(stderr
, "ndp: %s: %s\n", host
,
480 gai_strerror(gai_error
));
483 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
485 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
486 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
487 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
490 if (rtmsg(RTM_GET
) < 0) {
494 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
495 sdl
= (struct sockaddr_dl
*)(RT_ROUNDUP(sin
->sin6_len
) + (char *)sin
);
496 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
497 if (sdl
->sdl_family
== AF_LINK
&&
498 (rtm
->rtm_flags
& RTF_LLINFO
) &&
499 !(rtm
->rtm_flags
& RTF_GATEWAY
)) {
503 * IPv4 arp command retries with sin_other = SIN_PROXY here.
505 fprintf(stderr
, "delete: cannot delete non-NDP entry\n");
510 if (sdl
->sdl_family
!= AF_LINK
) {
511 printf("cannot locate %s\n", host
);
514 if (rtmsg(RTM_DELETE
) == 0) {
515 struct sockaddr_in6 s6
= *sin
; /* XXX: for safety */
518 if (IN6_IS_ADDR_LINKLOCAL(&s6
.sin6_addr
)) {
519 s6
.sin6_scope_id
= ntohs(*(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2]);
520 *(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2] = 0;
523 getnameinfo((struct sockaddr
*)&s6
,
524 s6
.sin6_len
, host_buf
,
525 sizeof(host_buf
), NULL
, 0,
526 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
527 printf("%s (%s) deleted\n", host
, host_buf
);
538 * Dump the entire neighbor cache
541 dump(struct in6_addr
*addr
)
545 char *lim
, *buf
, *next
;
546 struct rt_msghdr
*rtm
;
547 struct sockaddr_in6
*sin
;
548 struct sockaddr_dl
*sdl
;
550 struct in6_nbrinfo
*nbi
;
559 if (!tflag
&& !cflag
)
560 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
561 W_ADDR
, W_ADDR
, "Neighbor", W_LL
, W_LL
, "Linklayer Address",
562 W_IF
, W_IF
, "Netif", "Expire", "St", "Flgs", "Prbs");
569 mib
[4] = NET_RT_FLAGS
;
571 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
572 err(1, "sysctl(PF_ROUTE estimate)");
574 if ((buf
= malloc(needed
)) == NULL
)
576 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
577 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
582 for (next
= buf
; next
&& next
< lim
; next
+= rtm
->rtm_msglen
) {
583 int isrouter
= 0, prbs
= 0;
585 rtm
= (struct rt_msghdr
*)next
;
586 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
587 sdl
= (struct sockaddr_dl
*)((char *)sin
+
588 RT_ROUNDUP(sin
->sin6_len
));
591 * Some OSes can produce a route that has the LINK flag but
592 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
593 * and BSD/OS, where xx is not the interface identifier on
594 * lo0). Such routes entry would annoy getnbrinfo() below,
596 * XXX: such routes should have the GATEWAY flag, not the
597 * LINK flag. However, there are rotten routing software
598 * that advertises all routes that have the GATEWAY flag.
599 * Thus, KAME kernel intentionally does not set the LINK flag.
600 * What is to be fixed is not ndp, but such routing software
601 * (and the kernel workaround)...
603 if (sdl
->sdl_family
!= AF_LINK
)
607 if (!IN6_ARE_ADDR_EQUAL(addr
, &sin
->sin6_addr
))
610 } else if (IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
))
612 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) ||
613 IN6_IS_ADDR_MC_LINKLOCAL(&sin
->sin6_addr
)) {
614 /* XXX: should scope id be filled in the kernel? */
615 if (sin
->sin6_scope_id
== 0)
616 sin
->sin6_scope_id
= sdl
->sdl_index
;
618 /* KAME specific hack; removed the embedded id */
619 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
622 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
623 sizeof(host_buf
), NULL
, 0,
624 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
627 if (rtm
->rtm_flags
& RTF_WASCLONED
)
634 clock_gettime(CLOCK_MONOTONIC
, &sp
);
638 addrwidth
= strlen(host_buf
);
639 if (addrwidth
< W_ADDR
)
641 llwidth
= strlen(ether_str(sdl
));
642 if (W_ADDR
+ W_LL
- addrwidth
> llwidth
)
643 llwidth
= W_ADDR
+ W_LL
- addrwidth
;
644 ifname
= if_indextoname(sdl
->sdl_index
, ifix_buf
);
647 ifwidth
= strlen(ifname
);
648 if (W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
> ifwidth
)
649 ifwidth
= W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
;
651 printf("%-*.*s %-*.*s %*.*s", addrwidth
, addrwidth
, host_buf
,
652 llwidth
, llwidth
, ether_str(sdl
), ifwidth
, ifwidth
, ifname
);
654 /* Print neighbor discovery specific informations */
655 nbi
= getnbrinfo(&sin
->sin6_addr
, sdl
->sdl_index
, 1);
657 if (nbi
->expire
> sp
.tv_sec
) {
659 sec2str(nbi
->expire
- sp
.tv_sec
));
660 } else if (nbi
->expire
== 0)
661 printf(" %-9.9s", "permanent");
663 printf(" %-9.9s", "expired");
666 case ND6_LLINFO_NOSTATE
:
669 #ifdef ND6_LLINFO_WAITDELETE
670 case ND6_LLINFO_WAITDELETE
:
674 case ND6_LLINFO_INCOMPLETE
:
677 case ND6_LLINFO_REACHABLE
:
680 case ND6_LLINFO_STALE
:
683 case ND6_LLINFO_DELAY
:
686 case ND6_LLINFO_PROBE
:
694 isrouter
= nbi
->isrouter
;
697 warnx("failed to get neighbor information");
703 * other flags. R: router, P: proxy, W: ??
705 if ((rtm
->rtm_addrs
& RTA_NETMASK
) == 0) {
706 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s",
708 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
710 sin
= (struct sockaddr_in6
*)
711 (sdl
->sdl_len
+ (char *)sdl
);
712 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s%s%s",
714 !IN6_IS_ADDR_UNSPECIFIED(&sin
->sin6_addr
)
716 (sin
->sin6_len
!= sizeof(struct sockaddr_in6
))
718 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
720 printf(" %-4.4s", flgbuf
);
723 printf(" %4d", prbs
);
737 static struct in6_nbrinfo
*
738 getnbrinfo(struct in6_addr
*addr
, int ifindex
, int warning
)
740 static struct in6_nbrinfo nbi
;
743 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
746 bzero(&nbi
, sizeof(nbi
));
747 if_indextoname(ifindex
, nbi
.ifname
);
749 if (ioctl(s
, SIOCGNBRINFO_IN6
, (caddr_t
)&nbi
) < 0) {
751 warn("ioctl(SIOCGNBRINFO_IN6)");
761 ether_str(struct sockaddr_dl
*sdl
)
763 static char ebuf
[32];
767 cp
= (u_char
*)LLADDR(sdl
);
768 sprintf(ebuf
, "%x:%x:%x:%x:%x:%x",
769 cp
[0], cp
[1], cp
[2], cp
[3], cp
[4], cp
[5]);
771 sprintf(ebuf
, "(incomplete)");
778 ndp_ether_aton(char *a
, u_char
*n
)
782 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x", &o
[0], &o
[1], &o
[2],
783 &o
[3], &o
[4], &o
[5]);
785 fprintf(stderr
, "ndp: invalid Ethernet address '%s'\n", a
);
796 printf("usage: ndp hostname\n");
797 printf(" ndp -a[nt]\n");
798 printf(" ndp [-nt] -A wait\n");
799 printf(" ndp -c[nt]\n");
800 printf(" ndp -d[nt] hostname\n");
801 printf(" ndp -f[nt] filename\n");
802 printf(" ndp -i interface [flags...]\n");
803 #ifdef SIOCSDEFIFACE_IN6
804 printf(" ndp -I [interface|delete]\n");
808 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
820 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
821 char *cp
= m_rtmsg
.m_space
;
825 if (cmd
== RTM_DELETE
)
827 bzero((char *)&m_rtmsg
, sizeof(m_rtmsg
));
828 rtm
->rtm_flags
= flags
;
829 rtm
->rtm_version
= RTM_VERSION
;
833 fprintf(stderr
, "ndp: internal wrong cmd\n");
836 rtm
->rtm_addrs
|= RTA_GATEWAY
;
837 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
838 rtm
->rtm_inits
= RTV_EXPIRE
;
839 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
840 if (rtm
->rtm_flags
& RTF_ANNOUNCE
) {
841 rtm
->rtm_flags
&= ~RTF_HOST
;
842 rtm
->rtm_flags
|= RTA_NETMASK
;
846 rtm
->rtm_addrs
|= RTA_DST
;
848 #define NEXTADDR(w, s) \
849 if (rtm->rtm_addrs & (w)) { \
850 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
852 NEXTADDR(RTA_DST
, sin_m
);
853 NEXTADDR(RTA_GATEWAY
, sdl_m
);
854 memset(&so_mask
.sin6_addr
, 0xff, sizeof(so_mask
.sin6_addr
));
855 NEXTADDR(RTA_NETMASK
, so_mask
);
857 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
860 rtm
->rtm_seq
= ++seq
;
862 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
863 if (errno
!= ESRCH
|| cmd
!= RTM_DELETE
) {
864 perror("writing to routing socket");
869 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
870 } while (l
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
872 fprintf(stderr
, "ndp: read from routing socket: %s\n",
878 ifinfo(int argc
, char **argv
)
880 struct in6_ndireq nd
;
882 char *ifname
= argv
[0];
884 #ifdef IPV6CTL_USETEMPADDR
888 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
889 perror("ndp: socket");
892 bzero(&nd
, sizeof(nd
));
893 strcpy(nd
.ifname
, ifname
);
894 if (ioctl(s
, SIOCGIFINFO_IN6
, (caddr_t
)&nd
) < 0) {
895 perror("ioctl (SIOCGIFINFO_IN6)");
900 for (i
= 1; i
< argc
; i
++) {
909 #define SETFLAG(s, f) \
911 if (strcmp(cp, (s)) == 0) {\
918 SETFLAG("nud", ND6_IFF_PERFORMNUD
);
919 #ifdef ND6_IFF_ACCEPT_RTADV
920 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV
);
923 if (ioctl(s
, SIOCSIFINFO_FLAGS
, (caddr_t
)&nd
) < 0) {
924 perror("ioctl(SIOCSIFINFO_FLAGS)");
930 printf("linkmtu=%d", ND
.linkmtu
);
931 printf(", curhlim=%d", ND
.chlim
);
932 printf(", basereachable=%ds%dms",
933 ND
.basereachable
/ 1000, ND
.basereachable
% 1000);
934 printf(", reachable=%ds", ND
.reachable
);
935 printf(", retrans=%ds%dms", ND
.retrans
/ 1000, ND
.retrans
% 1000);
936 #ifdef IPV6CTL_USETEMPADDR
937 memset(nullbuf
, 0, sizeof(nullbuf
));
938 if (memcmp(nullbuf
, ND
.randomid
, sizeof(nullbuf
)) != 0) {
942 for (i
= 0; i
< 3; i
++) {
945 printf("\nRandom seed(0): ");
946 rbuf
= ND
.randomseed0
;
949 printf("\nRandom seed(1): ");
950 rbuf
= ND
.randomseed1
;
953 printf("\nRandom ID: ");
957 for (j
= 0; j
< 8; j
++)
958 printf("%02x", rbuf
[j
]);
964 if ((ND
.flags
& ND6_IFF_PERFORMNUD
))
966 #ifdef ND6_IFF_ACCEPT_RTADV
967 if ((ND
.flags
& ND6_IFF_ACCEPT_RTADV
))
968 printf("accept_rtadv ");
977 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
978 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
984 #ifdef ICMPV6CTL_ND6_DRLIST
985 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_DRLIST
};
987 struct in6_defrouter
*p
, *ep
;
991 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), NULL
, &l
, NULL
, 0) < 0) {
992 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
997 errx(1, "not enough core");
1000 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), buf
, &l
, NULL
, 0) < 0) {
1001 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1005 ep
= (struct in6_defrouter
*)(buf
+ l
);
1006 for (p
= (struct in6_defrouter
*)buf
; p
< ep
; p
++) {
1009 if (getnameinfo((struct sockaddr
*)&p
->rtaddr
,
1010 p
->rtaddr
.sin6_len
, host_buf
, sizeof(host_buf
), NULL
, 0,
1011 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0)) != 0)
1012 strlcpy(host_buf
, "?", sizeof(host_buf
));
1014 printf("%s if=%s", host_buf
,
1015 if_indextoname(p
->if_index
, ifix_buf
));
1016 printf(", flags=%s%s",
1017 p
->flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1018 p
->flags
& ND_RA_FLAG_OTHER
? "O" : "");
1019 rtpref
= ((p
->flags
& ND_RA_FLAG_RTPREF_MASK
) >> 3) & 0xff;
1020 printf(", pref=%s", rtpref_str
[rtpref
]);
1022 gettimeofday(&time
, 0);
1024 printf(", expire=Never\n");
1026 printf(", expire=%s\n",
1027 sec2str(p
->expire
- time
.tv_sec
));
1031 struct in6_drlist dr
;
1033 struct timeval time
;
1035 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1036 perror("ndp: socket");
1039 bzero(&dr
, sizeof(dr
));
1040 strcpy(dr
.ifname
, "lo0"); /* dummy */
1041 if (ioctl(s
, SIOCGDRLST_IN6
, (caddr_t
)&dr
) < 0) {
1042 perror("ioctl (SIOCGDRLST_IN6)");
1045 #define DR dr.defrouter[i]
1046 for (i
= 0 ; i
< DRLSTSIZ
&& DR
.if_index
; i
++) {
1047 struct sockaddr_in6 sin6
;
1049 bzero(&sin6
, sizeof(sin6
));
1050 sin6
.sin6_family
= AF_INET6
;
1051 sin6
.sin6_len
= sizeof(sin6
);
1052 sin6
.sin6_addr
= DR
.rtaddr
;
1053 getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
, host_buf
,
1054 sizeof(host_buf
), NULL
, 0,
1055 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
1057 printf("%s if=%s", host_buf
,
1058 if_indextoname(DR
.if_index
, ifix_buf
));
1059 printf(", flags=%s%s",
1060 DR
.flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1061 DR
.flags
& ND_RA_FLAG_OTHER
? "O" : "");
1062 gettimeofday(&time
, 0);
1064 printf(", expire=Never\n");
1066 printf(", expire=%s\n",
1067 sec2str(DR
.expire
- time
.tv_sec
));
1077 #ifdef ICMPV6CTL_ND6_PRLIST
1078 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_PRLIST
};
1080 struct in6_prefix
*p
, *ep
, *n
;
1081 struct sockaddr_in6
*advrtr
;
1083 struct timeval time
;
1084 #ifdef NI_WITHSCOPEID
1085 const int niflags
= NI_NUMERICHOST
| NI_WITHSCOPEID
;
1086 int ninflags
= (nflag
? NI_NUMERICHOST
: 0) | NI_WITHSCOPEID
;
1088 const int niflags
= NI_NUMERICHOST
;
1089 int ninflags
= nflag
? NI_NUMERICHOST
: 0;
1091 char namebuf
[NI_MAXHOST
];
1093 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), NULL
, &l
, NULL
, 0) < 0) {
1094 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1099 errx(1, "not enough core");
1102 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), buf
, &l
, NULL
, 0) < 0) {
1103 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1107 ep
= (struct in6_prefix
*)(buf
+ l
);
1108 for (p
= (struct in6_prefix
*)buf
; p
< ep
; p
= n
) {
1109 advrtr
= (struct sockaddr_in6
*)(p
+ 1);
1110 n
= (struct in6_prefix
*)&advrtr
[p
->advrtrs
];
1112 if (getnameinfo((struct sockaddr
*)&p
->prefix
,
1113 p
->prefix
.sin6_len
, namebuf
, sizeof(namebuf
),
1114 NULL
, 0, niflags
) != 0)
1115 strlcpy(namebuf
, "?", sizeof(namebuf
));
1116 printf("%s/%d if=%s\n", namebuf
, p
->prefixlen
,
1117 if_indextoname(p
->if_index
, ifix_buf
));
1119 gettimeofday(&time
, 0);
1121 * meaning of fields, especially flags, is very different
1122 * by origin. notify the difference to the users.
1124 printf("flags=%s%s%s%s%s",
1125 p
->raflags
.onlink
? "L" : "",
1126 p
->raflags
.autonomous
? "A" : "",
1127 (p
->flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1128 (p
->flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1130 (p
->flags
& NDPRF_HOME
) != 0 ? "H" : ""
1135 if (p
->vltime
== ND6_INFINITE_LIFETIME
)
1136 printf(" vltime=infinity");
1138 printf(" vltime=%ld", (long)p
->vltime
);
1139 if (p
->pltime
== ND6_INFINITE_LIFETIME
)
1140 printf(", pltime=infinity");
1142 printf(", pltime=%ld", (long)p
->pltime
);
1144 printf(", expire=Never");
1145 else if (p
->expire
>= time
.tv_sec
)
1146 printf(", expire=%s",
1147 sec2str(p
->expire
- time
.tv_sec
));
1149 printf(", expired");
1150 printf(", ref=%d", p
->refcnt
);
1153 * "advertising router" list is meaningful only if the prefix
1154 * information is from RA.
1158 struct sockaddr_in6
*sin6
;
1160 sin6
= (struct sockaddr_in6
*)(p
+ 1);
1161 printf(" advertised by\n");
1162 for (j
= 0; j
< p
->advrtrs
; j
++) {
1163 struct in6_nbrinfo
*nbi
;
1165 if (getnameinfo((struct sockaddr
*)sin6
,
1166 sin6
->sin6_len
, namebuf
, sizeof(namebuf
),
1167 NULL
, 0, ninflags
) != 0)
1168 strlcpy(namebuf
, "?", sizeof(namebuf
));
1169 printf(" %s", namebuf
);
1171 nbi
= getnbrinfo(&sin6
->sin6_addr
, p
->if_index
,
1174 switch(nbi
->state
) {
1175 case ND6_LLINFO_REACHABLE
:
1176 case ND6_LLINFO_STALE
:
1177 case ND6_LLINFO_DELAY
:
1178 case ND6_LLINFO_PROBE
:
1179 printf(" (reachable)\n");
1182 printf(" (unreachable)\n");
1185 printf(" (no neighbor state)\n");
1189 printf(" No advertising router\n");
1193 struct in6_prlist pr
;
1195 struct timeval time
;
1197 gettimeofday(&time
, 0);
1199 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1200 perror("ndp: socket");
1203 bzero(&pr
, sizeof(pr
));
1204 strcpy(pr
.ifname
, "lo0"); /* dummy */
1205 if (ioctl(s
, SIOCGPRLST_IN6
, (caddr_t
)&pr
) < 0) {
1206 perror("ioctl (SIOCGPRLST_IN6)");
1209 #define PR pr.prefix[i]
1210 for (i
= 0; i
< PRLSTSIZ
&& PR
.if_index
; i
++) {
1211 struct sockaddr_in6 p6
;
1212 char namebuf
[NI_MAXHOST
];
1218 memset(&p6
, 0, sizeof(p6
));
1219 p6
.sin6_family
= AF_INET6
;
1220 p6
.sin6_len
= sizeof(p6
);
1221 p6
.sin6_addr
= PR
.prefix
;
1225 * copy link index to sin6_scope_id field.
1226 * XXX: KAME specific.
1228 if (IN6_IS_ADDR_LINKLOCAL(&p6
.sin6_addr
)) {
1231 memcpy(&linkid
, &p6
.sin6_addr
.s6_addr
[2],
1233 linkid
= ntohs(linkid
);
1234 p6
.sin6_scope_id
= linkid
;
1235 p6
.sin6_addr
.s6_addr
[2] = 0;
1236 p6
.sin6_addr
.s6_addr
[3] = 0;
1239 niflags
= NI_NUMERICHOST
;
1241 niflags
|= NI_WITHSCOPEID
;
1243 if (getnameinfo((struct sockaddr
*)&p6
,
1244 sizeof(p6
), namebuf
, sizeof(namebuf
),
1245 NULL
, 0, niflags
)) {
1246 warnx("getnameinfo failed");
1249 printf("%s/%d if=%s\n", namebuf
, PR
.prefixlen
,
1250 if_indextoname(PR
.if_index
, ifix_buf
));
1252 gettimeofday(&time
, 0);
1254 * meaning of fields, especially flags, is very different
1255 * by origin. notify the difference to the users.
1259 PR
.origin
== PR_ORIG_RA
? "" : "advertise: ");
1262 printf("flags=%s%s%s%s%s",
1263 PR
.raflags
.onlink
? "L" : "",
1264 PR
.raflags
.autonomous
? "A" : "",
1265 (PR
.flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1266 (PR
.flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1268 (PR
.flags
& NDPRF_HOME
) != 0 ? "H" : ""
1274 printf("flags=%s%s",
1275 PR
.raflags
.onlink
? "L" : "",
1276 PR
.raflags
.autonomous
? "A" : "");
1278 if (PR
.vltime
== ND6_INFINITE_LIFETIME
)
1279 printf(" vltime=infinity");
1281 printf(" vltime=%ld", (long)PR
.vltime
);
1282 if (PR
.pltime
== ND6_INFINITE_LIFETIME
)
1283 printf(", pltime=infinity");
1285 printf(", pltime=%ld", (long)PR
.pltime
);
1287 printf(", expire=Never");
1288 else if (PR
.expire
>= time
.tv_sec
)
1289 printf(", expire=%s",
1290 sec2str(PR
.expire
- time
.tv_sec
));
1292 printf(", expired");
1294 printf(", ref=%d", PR
.refcnt
);
1297 switch (PR
.origin
) {
1299 printf(", origin=RA");
1302 printf(", origin=RR");
1304 case PR_ORIG_STATIC
:
1305 printf(", origin=static");
1307 case PR_ORIG_KERNEL
:
1308 printf(", origin=kernel");
1311 printf(", origin=?");
1317 * "advertising router" list is meaningful only if the prefix
1318 * information is from RA.
1320 if (0 && /* prefix origin is almost obsolted */
1321 PR
.origin
!= PR_ORIG_RA
)
1323 else if (PR
.advrtrs
) {
1325 printf(" advertised by\n");
1326 for (j
= 0; j
< PR
.advrtrs
; j
++) {
1327 struct sockaddr_in6 sin6
;
1328 struct in6_nbrinfo
*nbi
;
1330 bzero(&sin6
, sizeof(sin6
));
1331 sin6
.sin6_family
= AF_INET6
;
1332 sin6
.sin6_len
= sizeof(sin6
);
1333 sin6
.sin6_addr
= PR
.advrtr
[j
];
1334 sin6
.sin6_scope_id
= PR
.if_index
; /* XXX */
1335 getnameinfo((struct sockaddr
*)&sin6
,
1336 sin6
.sin6_len
, host_buf
,
1337 sizeof(host_buf
), NULL
, 0,
1338 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
1339 printf(" %s", host_buf
);
1341 nbi
= getnbrinfo(&sin6
.sin6_addr
, PR
.if_index
,
1344 switch(nbi
->state
) {
1345 case ND6_LLINFO_REACHABLE
:
1346 case ND6_LLINFO_STALE
:
1347 case ND6_LLINFO_DELAY
:
1348 case ND6_LLINFO_PROBE
:
1349 printf(" (reachable)\n");
1352 printf(" (unreachable)\n");
1355 printf(" (no neighbor state)\n");
1357 if (PR
.advrtrs
> DRLSTSIZ
)
1358 printf(" and %d routers\n",
1359 PR
.advrtrs
- DRLSTSIZ
);
1361 printf(" No advertising router\n");
1371 char dummyif
[IFNAMSIZ
+8];
1374 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1376 strcpy(dummyif
, "lo0"); /* dummy */
1377 if (ioctl(s
, SIOCSPFXFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1378 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1384 char dummyif
[IFNAMSIZ
+8];
1387 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1389 strcpy(dummyif
, "lo0"); /* dummy */
1390 if (ioctl(s
, SIOCSRTRFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1391 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1399 char dummyif
[IFNAMSIZ
+8];
1402 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1404 strcpy(dummyif
, "lo0"); /* dummy */
1405 if (ioctl(s
, SIOCSNDFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1406 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1411 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1413 setdefif(char *ifname
)
1415 struct in6_ndifreq ndifreq
;
1416 unsigned int ifindex
;
1418 if (strcasecmp(ifname
, "delete") == 0)
1421 if ((ifindex
= if_nametoindex(ifname
)) == 0)
1422 err(1, "failed to resolve i/f index for %s", ifname
);
1425 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1428 strcpy(ndifreq
.ifname
, "lo0"); /* dummy */
1429 ndifreq
.ifindex
= ifindex
;
1431 if (ioctl(s
, SIOCSDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1432 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1440 struct in6_ndifreq ndifreq
;
1441 char ifname
[IFNAMSIZ
+8];
1443 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1446 memset(&ndifreq
, 0, sizeof(ndifreq
));
1447 strcpy(ndifreq
.ifname
, "lo0"); /* dummy */
1449 if (ioctl(s
, SIOCGDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1450 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1452 if (ndifreq
.ifindex
== 0)
1453 printf("No default interface.\n");
1455 if ((if_indextoname(ndifreq
.ifindex
, ifname
)) == NULL
)
1456 err(1, "failed to resolve ifname for index %lu",
1458 printf("ND default interface = %s\n", ifname
);
1466 sec2str(time_t total
)
1468 static char result
[256];
1469 int days
, hours
, mins
, secs
;
1473 days
= total
/ 3600 / 24;
1474 hours
= (total
/ 3600) % 24;
1475 mins
= (total
/ 60) % 60;
1480 p
+= sprintf(p
, "%dd", days
);
1482 if (!first
|| hours
) {
1484 p
+= sprintf(p
, "%dh", hours
);
1486 if (!first
|| mins
) {
1488 p
+= sprintf(p
, "%dm", mins
);
1490 sprintf(p
, "%ds", secs
);
1496 * Print the timestamp
1497 * from tcpdump/util.c
1500 ts_print(const struct timespec
*sp
)
1505 s
= (sp
->tv_sec
+ thiszone
) % 86400;
1506 printf("%02d:%02d:%02d.%06u ",
1507 s
/ 3600, (s
% 3600) / 60, s
% 60, (u_int32_t
)(sp
->tv_nsec
/ 1000));