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>
111 #ifndef NI_WITHSCOPEID
112 #define NI_WITHSCOPEID 0
119 static int32_t thiszone
; /* time difference with gmt */
121 static int repeat
= 0;
123 char ntop_buf
[INET6_ADDRSTRLEN
]; /* inet_ntop() */
124 char host_buf
[NI_MAXHOST
]; /* getnameinfo() */
125 char ifix_buf
[IFNAMSIZ
]; /* if_indextoname() */
128 void getsocket(void);
129 int set(int, char **);
132 void dump(struct in6_addr
*);
133 static struct in6_nbrinfo
*getnbrinfo(struct in6_addr
*addr
,
135 static char *ether_str(struct sockaddr_dl
*);
136 int ndp_ether_aton(char *, u_char
*);
137 void usage(void) __dead2
;
139 void ifinfo(int, char **);
142 void pfx_flush(void);
144 void rtr_flush(void);
145 void harmonize_rtr(void);
146 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
147 static void getdefif(void);
148 static void setdefif(char *);
150 static char *sec2str(time_t t
);
151 static char *ether_str(struct sockaddr_dl
*sdl
);
152 static void ts_print(const struct timespec
*);
154 static char *rtpref_str
[] = {
162 * Returns the difference between gmt and local time in seconds.
163 * Use gmtime() and localtime() to keep things simple.
168 register int dt
, dir
;
169 register struct tm
*gmt
, *loc
;
177 dt
= (loc
->tm_hour
- gmt
->tm_hour
) * 60 * 60 +
178 (loc
->tm_min
- gmt
->tm_min
) * 60;
181 * If the year or julian day is different, we span 00:00 GMT
182 * and must add or subtract a day. Check the year first to
183 * avoid problems when the julian day wraps.
185 dir
= loc
->tm_year
- gmt
->tm_year
;
187 dir
= loc
->tm_yday
- gmt
->tm_yday
;
188 dt
+= dir
* 24 * 60 * 60;
194 main(int argc
, char **argv
)
197 int aflag
= 0, dflag
= 0, sflag
= 0, Hflag
= 0,
198 pflag
= 0, rflag
= 0, Pflag
= 0, Rflag
= 0;
201 thiszone
= gmt2local(0);
202 while ((ch
= getopt(argc
, argv
, "acndfIilprstA:HPR")) != -1)
214 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
217 getdefif(); /* always call it to print the result */
220 errx(1, "not supported yet");
242 /* obsolete, ignored */
255 repeat
= atoi(optarg
);
275 if (aflag
|| cflag
) {
294 if (argc
< 2 || argc
> 4)
296 exit(set(argc
, argv
) ? 1 : 0);
318 * Process a file to set standard ndp entries
325 char line
[100], arg
[5][50], *args
[5];
327 if ((fp
= fopen(name
, "r")) == NULL
) {
328 fprintf(stderr
, "ndp: cannot open %s\n", name
);
331 args
[0] = &arg
[0][0];
332 args
[1] = &arg
[1][0];
333 args
[2] = &arg
[2][0];
334 args
[3] = &arg
[3][0];
335 args
[4] = &arg
[4][0];
337 while(fgets(line
, 100, fp
) != NULL
) {
338 i
= sscanf(line
, "%s %s %s %s %s", arg
[0], arg
[1], arg
[2],
341 fprintf(stderr
, "ndp: bad line: %s\n", line
);
356 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
358 perror("ndp: socket");
364 struct sockaddr_in6 so_mask
= {sizeof(so_mask
), AF_INET6
};
365 struct sockaddr_in6 blank_sin
= {sizeof(blank_sin
), AF_INET6
}, sin_m
;
366 struct sockaddr_dl blank_sdl
= {sizeof(blank_sdl
), AF_LINK
}, sdl_m
;
367 int flags
, found_entry
;
370 struct rt_msghdr m_rtm
;
375 * Set an individual neighbor cache entry
378 set(int argc
, char **argv
)
380 struct sockaddr_in6
*sin
= &sin_m
;
381 struct sockaddr_dl
*sdl
;
382 struct rt_msghdr
*rtm
= &(m_rtmsg
.m_rtm
);
383 struct addrinfo hints
, *res
;
386 char *host
= argv
[0], *eaddr
= argv
[1];
394 bzero(&hints
, sizeof(hints
));
395 hints
.ai_family
= AF_INET6
;
396 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
398 fprintf(stderr
, "ndp: %s: %s\n", host
,
399 gai_strerror(gai_error
));
402 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
404 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
405 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
406 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
409 ea
= (u_char
*)LLADDR(&sdl_m
);
410 if (ndp_ether_aton(eaddr
, ea
) == 0)
415 if (strncmp(argv
[0], "temp", 4) == 0) {
418 clock_gettime(CLOCK_MONOTONIC
, &sp
);
419 expire_time
= sp
.tv_sec
+ 20 * 60;
420 } else if (strncmp(argv
[0], "proxy", 5) == 0)
421 flags
|= RTF_ANNOUNCE
;
424 if (rtmsg(RTM_GET
) < 0) {
428 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
429 sdl
= (struct sockaddr_dl
*)(RT_ROUNDUP(sin
->sin6_len
) + (char *)sin
);
430 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
431 if (sdl
->sdl_family
== AF_LINK
&&
432 (rtm
->rtm_flags
& RTF_LLINFO
) &&
433 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
434 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
435 case IFT_ISO88024
: case IFT_ISO88025
:
439 * IPv4 arp command retries with sin_other = SIN_PROXY here.
441 fprintf(stderr
, "set: cannot configure a new entry\n");
446 if (sdl
->sdl_family
!= AF_LINK
) {
447 printf("cannot intuit interface index and type for %s\n", host
);
450 sdl_m
.sdl_type
= sdl
->sdl_type
;
451 sdl_m
.sdl_index
= sdl
->sdl_index
;
452 return (rtmsg(RTM_ADD
));
456 * Display an individual neighbor cache entry
461 struct sockaddr_in6
*sin
= &sin_m
;
462 struct addrinfo hints
, *res
;
466 bzero(&hints
, sizeof(hints
));
467 hints
.ai_family
= AF_INET6
;
468 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
470 fprintf(stderr
, "ndp: %s: %s\n", host
,
471 gai_strerror(gai_error
));
474 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
476 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
477 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
478 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
481 dump(&sin
->sin6_addr
);
482 if (found_entry
== 0) {
483 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
484 sizeof(host_buf
), NULL
,0,
485 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
486 printf("%s (%s) -- no entry\n", host
, host_buf
);
492 * Delete a neighbor cache entry
497 struct sockaddr_in6
*sin
= &sin_m
;
498 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
499 struct sockaddr_dl
*sdl
;
500 struct addrinfo hints
, *res
;
506 bzero(&hints
, sizeof(hints
));
507 hints
.ai_family
= AF_INET6
;
508 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
510 fprintf(stderr
, "ndp: %s: %s\n", host
,
511 gai_strerror(gai_error
));
514 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
516 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
517 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
518 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
521 if (rtmsg(RTM_GET
) < 0) {
525 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
526 sdl
= (struct sockaddr_dl
*)(RT_ROUNDUP(sin
->sin6_len
) + (char *)sin
);
527 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
528 if (sdl
->sdl_family
== AF_LINK
&&
529 (rtm
->rtm_flags
& RTF_LLINFO
) &&
530 !(rtm
->rtm_flags
& RTF_GATEWAY
)) {
534 * IPv4 arp command retries with sin_other = SIN_PROXY here.
536 fprintf(stderr
, "delete: cannot delete non-NDP entry\n");
541 if (sdl
->sdl_family
!= AF_LINK
) {
542 printf("cannot locate %s\n", host
);
545 if (rtmsg(RTM_DELETE
) == 0) {
546 struct sockaddr_in6 s6
= *sin
; /* XXX: for safety */
549 if (IN6_IS_ADDR_LINKLOCAL(&s6
.sin6_addr
)) {
550 s6
.sin6_scope_id
= ntohs(*(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2]);
551 *(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2] = 0;
554 getnameinfo((struct sockaddr
*)&s6
,
555 s6
.sin6_len
, host_buf
,
556 sizeof(host_buf
), NULL
, 0,
557 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
558 printf("%s (%s) deleted\n", host
, host_buf
);
569 * Dump the entire neighbor cache
572 dump(struct in6_addr
*addr
)
576 char *lim
, *buf
, *next
;
577 struct rt_msghdr
*rtm
;
578 struct sockaddr_in6
*sin
;
579 struct sockaddr_dl
*sdl
;
581 struct in6_nbrinfo
*nbi
;
590 if (!tflag
&& !cflag
)
591 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
592 W_ADDR
, W_ADDR
, "Neighbor", W_LL
, W_LL
, "Linklayer Address",
593 W_IF
, W_IF
, "Netif", "Expire", "St", "Flgs", "Prbs");
600 mib
[4] = NET_RT_FLAGS
;
602 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
603 err(1, "sysctl(PF_ROUTE estimate)");
605 if ((buf
= malloc(needed
)) == NULL
)
607 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
608 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
613 for (next
= buf
; next
&& next
< lim
; next
+= rtm
->rtm_msglen
) {
614 int isrouter
= 0, prbs
= 0;
616 rtm
= (struct rt_msghdr
*)next
;
617 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
618 sdl
= (struct sockaddr_dl
*)((char *)sin
+
619 RT_ROUNDUP(sin
->sin6_len
));
622 * Some OSes can produce a route that has the LINK flag but
623 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
624 * and BSD/OS, where xx is not the interface identifier on
625 * lo0). Such routes entry would annoy getnbrinfo() below,
627 * XXX: such routes should have the GATEWAY flag, not the
628 * LINK flag. However, there are rotten routing software
629 * that advertises all routes that have the GATEWAY flag.
630 * Thus, KAME kernel intentionally does not set the LINK flag.
631 * What is to be fixed is not ndp, but such routing software
632 * (and the kernel workaround)...
634 if (sdl
->sdl_family
!= AF_LINK
)
638 if (!IN6_ARE_ADDR_EQUAL(addr
, &sin
->sin6_addr
))
641 } else if (IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
))
643 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) ||
644 IN6_IS_ADDR_MC_LINKLOCAL(&sin
->sin6_addr
)) {
645 /* XXX: should scope id be filled in the kernel? */
646 if (sin
->sin6_scope_id
== 0)
647 sin
->sin6_scope_id
= sdl
->sdl_index
;
649 /* KAME specific hack; removed the embedded id */
650 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
653 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
654 sizeof(host_buf
), NULL
, 0,
655 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
658 if (rtm
->rtm_flags
& RTF_WASCLONED
)
665 clock_gettime(CLOCK_MONOTONIC
, &sp
);
669 addrwidth
= strlen(host_buf
);
670 if (addrwidth
< W_ADDR
)
672 llwidth
= strlen(ether_str(sdl
));
673 if (W_ADDR
+ W_LL
- addrwidth
> llwidth
)
674 llwidth
= W_ADDR
+ W_LL
- addrwidth
;
675 ifname
= if_indextoname(sdl
->sdl_index
, ifix_buf
);
678 ifwidth
= strlen(ifname
);
679 if (W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
> ifwidth
)
680 ifwidth
= W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
;
682 printf("%-*.*s %-*.*s %*.*s", addrwidth
, addrwidth
, host_buf
,
683 llwidth
, llwidth
, ether_str(sdl
), ifwidth
, ifwidth
, ifname
);
685 /* Print neighbor discovery specific informations */
686 nbi
= getnbrinfo(&sin
->sin6_addr
, sdl
->sdl_index
, 1);
688 if (nbi
->expire
> sp
.tv_sec
) {
690 sec2str(nbi
->expire
- sp
.tv_sec
));
691 } else if (nbi
->expire
== 0)
692 printf(" %-9.9s", "permanent");
694 printf(" %-9.9s", "expired");
697 case ND6_LLINFO_NOSTATE
:
700 #ifdef ND6_LLINFO_WAITDELETE
701 case ND6_LLINFO_WAITDELETE
:
705 case ND6_LLINFO_INCOMPLETE
:
708 case ND6_LLINFO_REACHABLE
:
711 case ND6_LLINFO_STALE
:
714 case ND6_LLINFO_DELAY
:
717 case ND6_LLINFO_PROBE
:
725 isrouter
= nbi
->isrouter
;
728 warnx("failed to get neighbor information");
734 * other flags. R: router, P: proxy, W: ??
736 if ((rtm
->rtm_addrs
& RTA_NETMASK
) == 0) {
737 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s",
739 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
741 sin
= (struct sockaddr_in6
*)
742 (sdl
->sdl_len
+ (char *)sdl
);
743 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s%s%s",
745 !IN6_IS_ADDR_UNSPECIFIED(&sin
->sin6_addr
)
747 (sin
->sin6_len
!= sizeof(struct sockaddr_in6
))
749 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
751 printf(" %-4.4s", flgbuf
);
754 printf(" %4d", prbs
);
768 static struct in6_nbrinfo
*
769 getnbrinfo(struct in6_addr
*addr
, int ifindex
, int warning
)
771 static struct in6_nbrinfo nbi
;
774 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
777 bzero(&nbi
, sizeof(nbi
));
778 if_indextoname(ifindex
, nbi
.ifname
);
780 if (ioctl(s
, SIOCGNBRINFO_IN6
, (caddr_t
)&nbi
) < 0) {
782 warn("ioctl(SIOCGNBRINFO_IN6)");
792 ether_str(struct sockaddr_dl
*sdl
)
794 static char ebuf
[32];
798 cp
= (u_char
*)LLADDR(sdl
);
799 sprintf(ebuf
, "%x:%x:%x:%x:%x:%x",
800 cp
[0], cp
[1], cp
[2], cp
[3], cp
[4], cp
[5]);
802 sprintf(ebuf
, "(incomplete)");
809 ndp_ether_aton(char *a
, u_char
*n
)
813 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x", &o
[0], &o
[1], &o
[2],
814 &o
[3], &o
[4], &o
[5]);
816 fprintf(stderr
, "ndp: invalid Ethernet address '%s'\n", a
);
827 printf("usage: ndp hostname\n");
828 printf(" ndp -a[nt]\n");
829 printf(" ndp [-nt] -A wait\n");
830 printf(" ndp -c[nt]\n");
831 printf(" ndp -d[nt] hostname\n");
832 printf(" ndp -f[nt] filename\n");
833 printf(" ndp -i interface [flags...]\n");
834 #ifdef SIOCSDEFIFACE_IN6
835 printf(" ndp -I [interface|delete]\n");
839 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
851 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
852 char *cp
= m_rtmsg
.m_space
;
856 if (cmd
== RTM_DELETE
)
858 bzero((char *)&m_rtmsg
, sizeof(m_rtmsg
));
859 rtm
->rtm_flags
= flags
;
860 rtm
->rtm_version
= RTM_VERSION
;
864 fprintf(stderr
, "ndp: internal wrong cmd\n");
867 rtm
->rtm_addrs
|= RTA_GATEWAY
;
868 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
869 rtm
->rtm_inits
= RTV_EXPIRE
;
870 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
871 if (rtm
->rtm_flags
& RTF_ANNOUNCE
) {
872 rtm
->rtm_flags
&= ~RTF_HOST
;
873 rtm
->rtm_flags
|= RTA_NETMASK
;
877 rtm
->rtm_addrs
|= RTA_DST
;
879 #define NEXTADDR(w, s) \
880 if (rtm->rtm_addrs & (w)) { \
881 bcopy((char *)&s, cp, sizeof(s)); cp += RT_ROUNDUP(sizeof(s));}
883 NEXTADDR(RTA_DST
, sin_m
);
884 NEXTADDR(RTA_GATEWAY
, sdl_m
);
885 memset(&so_mask
.sin6_addr
, 0xff, sizeof(so_mask
.sin6_addr
));
886 NEXTADDR(RTA_NETMASK
, so_mask
);
888 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
891 rtm
->rtm_seq
= ++seq
;
893 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
894 if (errno
!= ESRCH
|| cmd
!= RTM_DELETE
) {
895 perror("writing to routing socket");
900 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
901 } while (l
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
903 fprintf(stderr
, "ndp: read from routing socket: %s\n",
909 ifinfo(int argc
, char **argv
)
911 struct in6_ndireq nd
;
913 char *ifname
= argv
[0];
915 #ifdef IPV6CTL_USETEMPADDR
919 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
920 perror("ndp: socket");
923 bzero(&nd
, sizeof(nd
));
924 strcpy(nd
.ifname
, ifname
);
925 if (ioctl(s
, SIOCGIFINFO_IN6
, (caddr_t
)&nd
) < 0) {
926 perror("ioctl (SIOCGIFINFO_IN6)");
931 for (i
= 1; i
< argc
; i
++) {
940 #define SETFLAG(s, f) \
942 if (strcmp(cp, (s)) == 0) {\
949 SETFLAG("nud", ND6_IFF_PERFORMNUD
);
950 #ifdef ND6_IFF_ACCEPT_RTADV
951 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV
);
953 #ifdef ND6_IFF_AUTO_LINKLOCAL
954 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL
);
957 if (ioctl(s
, SIOCSIFINFO_FLAGS
, (caddr_t
)&nd
) < 0) {
958 perror("ioctl(SIOCSIFINFO_FLAGS)");
964 printf("linkmtu=%d", ND
.linkmtu
);
965 printf(", curhlim=%d", ND
.chlim
);
966 printf(", basereachable=%ds%dms",
967 ND
.basereachable
/ 1000, ND
.basereachable
% 1000);
968 printf(", reachable=%ds", ND
.reachable
);
969 printf(", retrans=%ds%dms", ND
.retrans
/ 1000, ND
.retrans
% 1000);
970 #ifdef IPV6CTL_USETEMPADDR
971 memset(nullbuf
, 0, sizeof(nullbuf
));
972 if (memcmp(nullbuf
, ND
.randomid
, sizeof(nullbuf
)) != 0) {
976 for (i
= 0; i
< 3; i
++) {
979 printf("\nRandom seed(0): ");
980 rbuf
= ND
.randomseed0
;
983 printf("\nRandom seed(1): ");
984 rbuf
= ND
.randomseed1
;
987 printf("\nRandom ID: ");
991 for (j
= 0; j
< 8; j
++)
992 printf("%02x", rbuf
[j
]);
998 if ((ND
.flags
& ND6_IFF_PERFORMNUD
))
1000 #ifdef ND6_IFF_ACCEPT_RTADV
1001 if ((ND
.flags
& ND6_IFF_ACCEPT_RTADV
))
1002 printf("accept_rtadv ");
1004 #ifdef ND6_IFF_AUTO_LINKLOCAL
1005 if ((ND
.flags
& ND6_IFF_AUTO_LINKLOCAL
))
1006 printf("auto_linklocal ");
1015 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
1016 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
1022 #ifdef ICMPV6CTL_ND6_DRLIST
1023 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_DRLIST
};
1025 struct in6_defrouter
*p
, *ep
;
1027 struct timeval time
;
1029 if (sysctl(mib
, NELEM(mib
), NULL
, &l
, NULL
, 0) < 0) {
1030 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1035 errx(1, "not enough core");
1038 if (sysctl(mib
, NELEM(mib
), buf
, &l
, NULL
, 0) < 0) {
1039 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1043 ep
= (struct in6_defrouter
*)(buf
+ l
);
1044 for (p
= (struct in6_defrouter
*)buf
; p
< ep
; p
++) {
1047 if (getnameinfo((struct sockaddr
*)&p
->rtaddr
,
1048 p
->rtaddr
.sin6_len
, host_buf
, sizeof(host_buf
), NULL
, 0,
1049 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0)) != 0)
1050 strlcpy(host_buf
, "?", sizeof(host_buf
));
1052 printf("%s if=%s", host_buf
,
1053 if_indextoname(p
->if_index
, ifix_buf
));
1054 printf(", flags=%s%s",
1055 p
->flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1056 p
->flags
& ND_RA_FLAG_OTHER
? "O" : "");
1057 rtpref
= ((p
->flags
& ND_RA_FLAG_RTPREF_MASK
) >> 3) & 0xff;
1058 printf(", pref=%s", rtpref_str
[rtpref
]);
1060 gettimeofday(&time
, 0);
1062 printf(", expire=Never\n");
1064 printf(", expire=%s\n",
1065 sec2str(p
->expire
- time
.tv_sec
));
1069 struct in6_drlist dr
;
1071 struct timeval time
;
1073 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1074 perror("ndp: socket");
1077 bzero(&dr
, sizeof(dr
));
1078 strcpy(dr
.ifname
, "lo0"); /* dummy */
1079 if (ioctl(s
, SIOCGDRLST_IN6
, (caddr_t
)&dr
) < 0) {
1080 perror("ioctl (SIOCGDRLST_IN6)");
1083 #define DR dr.defrouter[i]
1084 for (i
= 0 ; i
< DRLSTSIZ
&& DR
.if_index
; i
++) {
1085 struct sockaddr_in6 sin6
;
1087 bzero(&sin6
, sizeof(sin6
));
1088 sin6
.sin6_family
= AF_INET6
;
1089 sin6
.sin6_len
= sizeof(sin6
);
1090 sin6
.sin6_addr
= DR
.rtaddr
;
1091 getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
, host_buf
,
1092 sizeof(host_buf
), NULL
, 0,
1093 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
1095 printf("%s if=%s", host_buf
,
1096 if_indextoname(DR
.if_index
, ifix_buf
));
1097 printf(", flags=%s%s",
1098 DR
.flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1099 DR
.flags
& ND_RA_FLAG_OTHER
? "O" : "");
1100 gettimeofday(&time
, 0);
1102 printf(", expire=Never\n");
1104 printf(", expire=%s\n",
1105 sec2str(DR
.expire
- time
.tv_sec
));
1115 #ifdef ICMPV6CTL_ND6_PRLIST
1116 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_PRLIST
};
1118 struct in6_prefix
*p
, *ep
, *n
;
1119 struct sockaddr_in6
*advrtr
;
1121 struct timeval time
;
1122 #ifdef NI_WITHSCOPEID
1123 const int niflags
= NI_NUMERICHOST
| NI_WITHSCOPEID
;
1124 int ninflags
= (nflag
? NI_NUMERICHOST
: 0) | NI_WITHSCOPEID
;
1126 const int niflags
= NI_NUMERICHOST
;
1127 int ninflags
= nflag
? NI_NUMERICHOST
: 0;
1129 char namebuf
[NI_MAXHOST
];
1131 if (sysctl(mib
, NELEM(mib
), NULL
, &l
, NULL
, 0) < 0) {
1132 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1137 errx(1, "not enough core");
1140 if (sysctl(mib
, NELEM(mib
), buf
, &l
, NULL
, 0) < 0) {
1141 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1145 ep
= (struct in6_prefix
*)(buf
+ l
);
1146 for (p
= (struct in6_prefix
*)buf
; p
< ep
; p
= n
) {
1147 advrtr
= (struct sockaddr_in6
*)(p
+ 1);
1148 n
= (struct in6_prefix
*)&advrtr
[p
->advrtrs
];
1150 if (getnameinfo((struct sockaddr
*)&p
->prefix
,
1151 p
->prefix
.sin6_len
, namebuf
, sizeof(namebuf
),
1152 NULL
, 0, niflags
) != 0)
1153 strlcpy(namebuf
, "?", sizeof(namebuf
));
1154 printf("%s/%d if=%s\n", namebuf
, p
->prefixlen
,
1155 if_indextoname(p
->if_index
, ifix_buf
));
1157 gettimeofday(&time
, 0);
1159 * meaning of fields, especially flags, is very different
1160 * by origin. notify the difference to the users.
1162 printf("flags=%s%s%s%s%s",
1163 p
->raflags
.onlink
? "L" : "",
1164 p
->raflags
.autonomous
? "A" : "",
1165 (p
->flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1166 (p
->flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1168 (p
->flags
& NDPRF_HOME
) != 0 ? "H" : ""
1173 if (p
->vltime
== ND6_INFINITE_LIFETIME
)
1174 printf(" vltime=infinity");
1176 printf(" vltime=%ld", (long)p
->vltime
);
1177 if (p
->pltime
== ND6_INFINITE_LIFETIME
)
1178 printf(", pltime=infinity");
1180 printf(", pltime=%ld", (long)p
->pltime
);
1182 printf(", expire=Never");
1183 else if (p
->expire
>= time
.tv_sec
)
1184 printf(", expire=%s",
1185 sec2str(p
->expire
- time
.tv_sec
));
1187 printf(", expired");
1188 printf(", ref=%d", p
->refcnt
);
1191 * "advertising router" list is meaningful only if the prefix
1192 * information is from RA.
1196 struct sockaddr_in6
*sin6
;
1198 sin6
= (struct sockaddr_in6
*)(p
+ 1);
1199 printf(" advertised by\n");
1200 for (j
= 0; j
< p
->advrtrs
; j
++) {
1201 struct in6_nbrinfo
*nbi
;
1203 if (getnameinfo((struct sockaddr
*)sin6
,
1204 sin6
->sin6_len
, namebuf
, sizeof(namebuf
),
1205 NULL
, 0, ninflags
) != 0)
1206 strlcpy(namebuf
, "?", sizeof(namebuf
));
1207 printf(" %s", namebuf
);
1209 nbi
= getnbrinfo(&sin6
->sin6_addr
, p
->if_index
,
1212 switch(nbi
->state
) {
1213 case ND6_LLINFO_REACHABLE
:
1214 case ND6_LLINFO_STALE
:
1215 case ND6_LLINFO_DELAY
:
1216 case ND6_LLINFO_PROBE
:
1217 printf(" (reachable)\n");
1220 printf(" (unreachable)\n");
1223 printf(" (no neighbor state)\n");
1227 printf(" No advertising router\n");
1231 struct in6_prlist pr
;
1233 struct timeval time
;
1235 gettimeofday(&time
, 0);
1237 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1238 perror("ndp: socket");
1241 bzero(&pr
, sizeof(pr
));
1242 strcpy(pr
.ifname
, "lo0"); /* dummy */
1243 if (ioctl(s
, SIOCGPRLST_IN6
, (caddr_t
)&pr
) < 0) {
1244 perror("ioctl (SIOCGPRLST_IN6)");
1247 #define PR pr.prefix[i]
1248 for (i
= 0; i
< PRLSTSIZ
&& PR
.if_index
; i
++) {
1249 struct sockaddr_in6 p6
;
1250 char namebuf
[NI_MAXHOST
];
1256 memset(&p6
, 0, sizeof(p6
));
1257 p6
.sin6_family
= AF_INET6
;
1258 p6
.sin6_len
= sizeof(p6
);
1259 p6
.sin6_addr
= PR
.prefix
;
1263 * copy link index to sin6_scope_id field.
1264 * XXX: KAME specific.
1266 if (IN6_IS_ADDR_LINKLOCAL(&p6
.sin6_addr
)) {
1269 memcpy(&linkid
, &p6
.sin6_addr
.s6_addr
[2],
1271 linkid
= ntohs(linkid
);
1272 p6
.sin6_scope_id
= linkid
;
1273 p6
.sin6_addr
.s6_addr
[2] = 0;
1274 p6
.sin6_addr
.s6_addr
[3] = 0;
1277 niflags
= NI_NUMERICHOST
;
1279 niflags
|= NI_WITHSCOPEID
;
1281 if (getnameinfo((struct sockaddr
*)&p6
,
1282 sizeof(p6
), namebuf
, sizeof(namebuf
),
1283 NULL
, 0, niflags
)) {
1284 warnx("getnameinfo failed");
1287 printf("%s/%d if=%s\n", namebuf
, PR
.prefixlen
,
1288 if_indextoname(PR
.if_index
, ifix_buf
));
1290 gettimeofday(&time
, 0);
1292 * meaning of fields, especially flags, is very different
1293 * by origin. notify the difference to the users.
1297 PR
.origin
== PR_ORIG_RA
? "" : "advertise: ");
1300 printf("flags=%s%s%s%s%s",
1301 PR
.raflags
.onlink
? "L" : "",
1302 PR
.raflags
.autonomous
? "A" : "",
1303 (PR
.flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1304 (PR
.flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1306 (PR
.flags
& NDPRF_HOME
) != 0 ? "H" : ""
1312 printf("flags=%s%s",
1313 PR
.raflags
.onlink
? "L" : "",
1314 PR
.raflags
.autonomous
? "A" : "");
1316 if (PR
.vltime
== ND6_INFINITE_LIFETIME
)
1317 printf(" vltime=infinity");
1319 printf(" vltime=%ld", (long)PR
.vltime
);
1320 if (PR
.pltime
== ND6_INFINITE_LIFETIME
)
1321 printf(", pltime=infinity");
1323 printf(", pltime=%ld", (long)PR
.pltime
);
1325 printf(", expire=Never");
1326 else if (PR
.expire
>= time
.tv_sec
)
1327 printf(", expire=%s",
1328 sec2str(PR
.expire
- time
.tv_sec
));
1330 printf(", expired");
1332 printf(", ref=%d", PR
.refcnt
);
1335 switch (PR
.origin
) {
1337 printf(", origin=RA");
1340 printf(", origin=RR");
1342 case PR_ORIG_STATIC
:
1343 printf(", origin=static");
1345 case PR_ORIG_KERNEL
:
1346 printf(", origin=kernel");
1349 printf(", origin=?");
1355 * "advertising router" list is meaningful only if the prefix
1356 * information is from RA.
1358 if (0 && /* prefix origin is almost obsolted */
1359 PR
.origin
!= PR_ORIG_RA
)
1361 else if (PR
.advrtrs
) {
1363 printf(" advertised by\n");
1364 for (j
= 0; j
< PR
.advrtrs
; j
++) {
1365 struct sockaddr_in6 sin6
;
1366 struct in6_nbrinfo
*nbi
;
1368 bzero(&sin6
, sizeof(sin6
));
1369 sin6
.sin6_family
= AF_INET6
;
1370 sin6
.sin6_len
= sizeof(sin6
);
1371 sin6
.sin6_addr
= PR
.advrtr
[j
];
1372 sin6
.sin6_scope_id
= PR
.if_index
; /* XXX */
1373 getnameinfo((struct sockaddr
*)&sin6
,
1374 sin6
.sin6_len
, host_buf
,
1375 sizeof(host_buf
), NULL
, 0,
1376 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
1377 printf(" %s", host_buf
);
1379 nbi
= getnbrinfo(&sin6
.sin6_addr
, PR
.if_index
,
1382 switch(nbi
->state
) {
1383 case ND6_LLINFO_REACHABLE
:
1384 case ND6_LLINFO_STALE
:
1385 case ND6_LLINFO_DELAY
:
1386 case ND6_LLINFO_PROBE
:
1387 printf(" (reachable)\n");
1390 printf(" (unreachable)\n");
1393 printf(" (no neighbor state)\n");
1395 if (PR
.advrtrs
> DRLSTSIZ
)
1396 printf(" and %d routers\n",
1397 PR
.advrtrs
- DRLSTSIZ
);
1399 printf(" No advertising router\n");
1409 char dummyif
[IFNAMSIZ
+8];
1412 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1414 strcpy(dummyif
, "lo0"); /* dummy */
1415 if (ioctl(s
, SIOCSPFXFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1416 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1422 char dummyif
[IFNAMSIZ
+8];
1425 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1427 strcpy(dummyif
, "lo0"); /* dummy */
1428 if (ioctl(s
, SIOCSRTRFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1429 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1437 char dummyif
[IFNAMSIZ
+8];
1440 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1442 strcpy(dummyif
, "lo0"); /* dummy */
1443 if (ioctl(s
, SIOCSNDFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1444 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1449 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1451 setdefif(char *ifname
)
1453 struct in6_ndifreq ndifreq
;
1454 unsigned int ifindex
;
1456 if (strcasecmp(ifname
, "delete") == 0)
1459 if ((ifindex
= if_nametoindex(ifname
)) == 0)
1460 err(1, "failed to resolve i/f index for %s", ifname
);
1463 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1466 strcpy(ndifreq
.ifname
, "lo0"); /* dummy */
1467 ndifreq
.ifindex
= ifindex
;
1469 if (ioctl(s
, SIOCSDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1470 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1478 struct in6_ndifreq ndifreq
;
1479 char ifname
[IFNAMSIZ
+8];
1481 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1484 memset(&ndifreq
, 0, sizeof(ndifreq
));
1485 strcpy(ndifreq
.ifname
, "lo0"); /* dummy */
1487 if (ioctl(s
, SIOCGDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1488 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1490 if (ndifreq
.ifindex
== 0)
1491 printf("No default interface.\n");
1493 if ((if_indextoname(ndifreq
.ifindex
, ifname
)) == NULL
)
1494 err(1, "failed to resolve ifname for index %lu",
1496 printf("ND default interface = %s\n", ifname
);
1504 sec2str(time_t total
)
1506 static char result
[256];
1507 int days
, hours
, mins
, secs
;
1511 days
= total
/ 3600 / 24;
1512 hours
= (total
/ 3600) % 24;
1513 mins
= (total
/ 60) % 60;
1518 p
+= sprintf(p
, "%dd", days
);
1520 if (!first
|| hours
) {
1522 p
+= sprintf(p
, "%dh", hours
);
1524 if (!first
|| mins
) {
1526 p
+= sprintf(p
, "%dm", mins
);
1528 sprintf(p
, "%ds", secs
);
1534 * Print the timestamp
1535 * from tcpdump/util.c
1538 ts_print(const struct timespec
*sp
)
1543 s
= (sp
->tv_sec
+ thiszone
) % 86400;
1544 printf("%02d:%02d:%02d.%06u ",
1545 s
/ 3600, (s
% 3600) / 60, s
% 60, (u_int32_t
)(sp
->tv_nsec
/ 1000));