1 /* $FreeBSD: src/usr.sbin/ndp/ndp.c,v 1.2.2.6 2003/08/12 16:27:57 ume Exp $ */
2 /* $DragonFly: src/usr.sbin/ndp/ndp.c,v 1.6 2004/12/30 02:35:24 hsu Exp $ */
3 /* $KAME: ndp.c,v 1.65 2001/05/08 04:36:34 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1984, 1993
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Sun Microsystems, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * "@(#) Copyright (c) 1984, 1993\n\
72 * The Regents of the University of California. All rights reserved.\n";
74 * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
78 * ndp - display, set, delete and flush neighbor cache
82 #include <sys/param.h>
84 #include <sys/ioctl.h>
85 #include <sys/socket.h>
86 #include <sys/sysctl.h>
88 #include <sys/queue.h>
91 #include <net/if_var.h>
92 #include <net/if_dl.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/if_ether.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/in6_var.h>
101 #include <netinet6/nd6.h>
103 #include <arpa/inet.h>
115 #include "gmt2local.h"
117 #ifndef NI_WITHSCOPEID
118 #define NI_WITHSCOPEID 0
121 /* packing rule for routing socket */
123 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
124 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
130 static int32_t thiszone
; /* time difference with gmt */
132 static int repeat
= 0;
134 char ntop_buf
[INET6_ADDRSTRLEN
]; /* inet_ntop() */
135 char host_buf
[NI_MAXHOST
]; /* getnameinfo() */
136 char ifix_buf
[IFNAMSIZ
]; /* if_indextoname() */
138 int main(int, char **);
140 void getsocket(void);
141 int set(int, char **);
144 void dump(struct in6_addr
*);
145 static struct in6_nbrinfo
*getnbrinfo(struct in6_addr
*addr
,
147 static char *ether_str(struct sockaddr_dl
*);
148 int ndp_ether_aton(char *, u_char
*);
151 void ifinfo(int, char **);
154 void pfx_flush(void);
156 void rtr_flush(void);
157 void harmonize_rtr(void);
158 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
159 static void getdefif(void);
160 static void setdefif(char *);
162 static char *sec2str(time_t t
);
163 static char *ether_str(struct sockaddr_dl
*sdl
);
164 static void ts_print(const struct timeval
*);
166 static char *rtpref_str
[] = {
174 main(int argc
, char **argv
)
177 int aflag
= 0, dflag
= 0, sflag
= 0, Hflag
= 0,
178 pflag
= 0, rflag
= 0, Pflag
= 0, Rflag
= 0;
181 thiszone
= gmt2local(0);
182 while ((ch
= getopt(argc
, argv
, "acndfIilprstA:HPR")) != -1)
194 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
197 getdefif(); /* always call it to print the result */
200 errx(1, "not supported yet");
222 /* obsolete, ignored */
235 repeat
= atoi(optarg
);
255 if (aflag
|| cflag
) {
274 if (argc
< 2 || argc
> 4)
276 exit(set(argc
, argv
) ? 1 : 0);
298 * Process a file to set standard ndp entries
305 char line
[100], arg
[5][50], *args
[5];
307 if ((fp
= fopen(name
, "r")) == NULL
) {
308 fprintf(stderr
, "ndp: cannot open %s\n", name
);
311 args
[0] = &arg
[0][0];
312 args
[1] = &arg
[1][0];
313 args
[2] = &arg
[2][0];
314 args
[3] = &arg
[3][0];
315 args
[4] = &arg
[4][0];
317 while(fgets(line
, 100, fp
) != NULL
) {
318 i
= sscanf(line
, "%s %s %s %s %s", arg
[0], arg
[1], arg
[2],
321 fprintf(stderr
, "ndp: bad line: %s\n", line
);
336 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
338 perror("ndp: socket");
344 struct sockaddr_in6 so_mask
= {sizeof(so_mask
), AF_INET6
};
345 struct sockaddr_in6 blank_sin
= {sizeof(blank_sin
), AF_INET6
}, sin_m
;
346 struct sockaddr_dl blank_sdl
= {sizeof(blank_sdl
), AF_LINK
}, sdl_m
;
347 int expire_time
, flags
, found_entry
;
349 struct rt_msghdr m_rtm
;
354 * Set an individual neighbor cache entry
357 set(int argc
, char **argv
)
359 struct sockaddr_in6
*sin
= &sin_m
;
360 struct sockaddr_dl
*sdl
;
361 struct rt_msghdr
*rtm
= &(m_rtmsg
.m_rtm
);
362 struct addrinfo hints
, *res
;
365 char *host
= argv
[0], *eaddr
= argv
[1];
373 bzero(&hints
, sizeof(hints
));
374 hints
.ai_family
= AF_INET6
;
375 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
377 fprintf(stderr
, "ndp: %s: %s\n", host
,
378 gai_strerror(gai_error
));
381 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
383 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
384 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
385 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
388 ea
= (u_char
*)LLADDR(&sdl_m
);
389 if (ndp_ether_aton(eaddr
, ea
) == 0)
391 flags
= expire_time
= 0;
393 if (strncmp(argv
[0], "temp", 4) == 0) {
395 gettimeofday(&time
, 0);
396 expire_time
= time
.tv_sec
+ 20 * 60;
397 } else if (strncmp(argv
[0], "proxy", 5) == 0)
398 flags
|= RTF_ANNOUNCE
;
401 if (rtmsg(RTM_GET
) < 0) {
405 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
406 sdl
= (struct sockaddr_dl
*)(ROUNDUP(sin
->sin6_len
) + (char *)sin
);
407 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
408 if (sdl
->sdl_family
== AF_LINK
&&
409 (rtm
->rtm_flags
& RTF_LLINFO
) &&
410 !(rtm
->rtm_flags
& RTF_GATEWAY
)) switch (sdl
->sdl_type
) {
411 case IFT_ETHER
: case IFT_FDDI
: case IFT_ISO88023
:
412 case IFT_ISO88024
: case IFT_ISO88025
:
416 * IPv4 arp command retries with sin_other = SIN_PROXY here.
418 fprintf(stderr
, "set: cannot configure a new entry\n");
423 if (sdl
->sdl_family
!= AF_LINK
) {
424 printf("cannot intuit interface index and type for %s\n", host
);
427 sdl_m
.sdl_type
= sdl
->sdl_type
;
428 sdl_m
.sdl_index
= sdl
->sdl_index
;
429 return (rtmsg(RTM_ADD
));
433 * Display an individual neighbor cache entry
438 struct sockaddr_in6
*sin
= &sin_m
;
439 struct addrinfo hints
, *res
;
443 bzero(&hints
, sizeof(hints
));
444 hints
.ai_family
= AF_INET6
;
445 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
447 fprintf(stderr
, "ndp: %s: %s\n", host
,
448 gai_strerror(gai_error
));
451 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
453 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
454 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
455 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
458 dump(&sin
->sin6_addr
);
459 if (found_entry
== 0) {
460 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
461 sizeof(host_buf
), NULL
,0,
462 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
463 printf("%s (%s) -- no entry\n", host
, host_buf
);
469 * Delete a neighbor cache entry
474 struct sockaddr_in6
*sin
= &sin_m
;
475 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
476 struct sockaddr_dl
*sdl
;
477 struct addrinfo hints
, *res
;
483 bzero(&hints
, sizeof(hints
));
484 hints
.ai_family
= AF_INET6
;
485 gai_error
= getaddrinfo(host
, NULL
, &hints
, &res
);
487 fprintf(stderr
, "ndp: %s: %s\n", host
,
488 gai_strerror(gai_error
));
491 sin
->sin6_addr
= ((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
493 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
)) {
494 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] =
495 htons(((struct sockaddr_in6
*)res
->ai_addr
)->sin6_scope_id
);
498 if (rtmsg(RTM_GET
) < 0) {
502 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
503 sdl
= (struct sockaddr_dl
*)(ROUNDUP(sin
->sin6_len
) + (char *)sin
);
504 if (IN6_ARE_ADDR_EQUAL(&sin
->sin6_addr
, &sin_m
.sin6_addr
)) {
505 if (sdl
->sdl_family
== AF_LINK
&&
506 (rtm
->rtm_flags
& RTF_LLINFO
) &&
507 !(rtm
->rtm_flags
& RTF_GATEWAY
)) {
511 * IPv4 arp command retries with sin_other = SIN_PROXY here.
513 fprintf(stderr
, "delete: cannot delete non-NDP entry\n");
518 if (sdl
->sdl_family
!= AF_LINK
) {
519 printf("cannot locate %s\n", host
);
522 if (rtmsg(RTM_DELETE
) == 0) {
523 struct sockaddr_in6 s6
= *sin
; /* XXX: for safety */
526 if (IN6_IS_ADDR_LINKLOCAL(&s6
.sin6_addr
)) {
527 s6
.sin6_scope_id
= ntohs(*(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2]);
528 *(u_int16_t
*)&s6
.sin6_addr
.s6_addr
[2] = 0;
531 getnameinfo((struct sockaddr
*)&s6
,
532 s6
.sin6_len
, host_buf
,
533 sizeof(host_buf
), NULL
, 0,
534 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
535 printf("%s (%s) deleted\n", host
, host_buf
);
546 * Dump the entire neighbor cache
549 dump(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
;
567 if (!tflag
&& !cflag
)
568 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
569 W_ADDR
, W_ADDR
, "Neighbor", W_LL
, W_LL
, "Linklayer Address",
570 W_IF
, W_IF
, "Netif", "Expire", "St", "Flgs", "Prbs");
577 mib
[4] = NET_RT_FLAGS
;
579 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
580 err(1, "sysctl(PF_ROUTE estimate)");
582 if ((buf
= malloc(needed
)) == NULL
)
584 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
585 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
590 for (next
= buf
; next
&& next
< lim
; next
+= rtm
->rtm_msglen
) {
591 int isrouter
= 0, prbs
= 0;
593 rtm
= (struct rt_msghdr
*)next
;
594 sin
= (struct sockaddr_in6
*)(rtm
+ 1);
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 are 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
)
614 if (!IN6_ARE_ADDR_EQUAL(addr
, &sin
->sin6_addr
))
617 } else if (IN6_IS_ADDR_MULTICAST(&sin
->sin6_addr
))
619 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) ||
620 IN6_IS_ADDR_MC_LINKLOCAL(&sin
->sin6_addr
)) {
621 /* XXX: should scope id be filled in the kernel? */
622 if (sin
->sin6_scope_id
== 0)
623 sin
->sin6_scope_id
= sdl
->sdl_index
;
625 /* KAME specific hack; removed the embedded id */
626 *(u_int16_t
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
629 getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, host_buf
,
630 sizeof(host_buf
), NULL
, 0,
631 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
634 if (rtm
->rtm_flags
& RTF_WASCLONED
)
641 gettimeofday(&time
, 0);
645 addrwidth
= strlen(host_buf
);
646 if (addrwidth
< W_ADDR
)
648 llwidth
= strlen(ether_str(sdl
));
649 if (W_ADDR
+ W_LL
- addrwidth
> llwidth
)
650 llwidth
= W_ADDR
+ W_LL
- addrwidth
;
651 ifname
= if_indextoname(sdl
->sdl_index
, ifix_buf
);
654 ifwidth
= strlen(ifname
);
655 if (W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
> ifwidth
)
656 ifwidth
= W_ADDR
+ W_LL
+ W_IF
- addrwidth
- llwidth
;
658 printf("%-*.*s %-*.*s %*.*s", addrwidth
, addrwidth
, host_buf
,
659 llwidth
, llwidth
, ether_str(sdl
), ifwidth
, ifwidth
, ifname
);
661 /* Print neighbor discovery specific informations */
662 nbi
= getnbrinfo(&sin
->sin6_addr
, sdl
->sdl_index
, 1);
664 if (nbi
->expire
> time
.tv_sec
) {
666 sec2str(nbi
->expire
- time
.tv_sec
));
667 } else if (nbi
->expire
== 0)
668 printf(" %-9.9s", "permanent");
670 printf(" %-9.9s", "expired");
673 case ND6_LLINFO_NOSTATE
:
676 #ifdef ND6_LLINFO_WAITDELETE
677 case ND6_LLINFO_WAITDELETE
:
681 case ND6_LLINFO_INCOMPLETE
:
684 case ND6_LLINFO_REACHABLE
:
687 case ND6_LLINFO_STALE
:
690 case ND6_LLINFO_DELAY
:
693 case ND6_LLINFO_PROBE
:
701 isrouter
= nbi
->isrouter
;
704 warnx("failed to get neighbor information");
710 * other flags. R: router, P: proxy, W: ??
712 if ((rtm
->rtm_addrs
& RTA_NETMASK
) == 0) {
713 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s",
715 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
717 sin
= (struct sockaddr_in6
*)
718 (sdl
->sdl_len
+ (char *)sdl
);
719 snprintf(flgbuf
, sizeof(flgbuf
), "%s%s%s%s",
721 !IN6_IS_ADDR_UNSPECIFIED(&sin
->sin6_addr
)
723 (sin
->sin6_len
!= sizeof(struct sockaddr_in6
))
725 (rtm
->rtm_flags
& RTF_ANNOUNCE
) ? "p" : "");
727 printf(" %-4.4s", flgbuf
);
730 printf(" %4d", prbs
);
744 static struct in6_nbrinfo
*
745 getnbrinfo(struct in6_addr
*addr
, int ifindex
, int warning
)
747 static struct in6_nbrinfo nbi
;
750 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
753 bzero(&nbi
, sizeof(nbi
));
754 if_indextoname(ifindex
, nbi
.ifname
);
756 if (ioctl(s
, SIOCGNBRINFO_IN6
, (caddr_t
)&nbi
) < 0) {
758 warn("ioctl(SIOCGNBRINFO_IN6)");
768 ether_str(struct sockaddr_dl
*sdl
)
770 static char ebuf
[32];
774 cp
= (u_char
*)LLADDR(sdl
);
775 sprintf(ebuf
, "%x:%x:%x:%x:%x:%x",
776 cp
[0], cp
[1], cp
[2], cp
[3], cp
[4], cp
[5]);
778 sprintf(ebuf
, "(incomplete)");
785 ndp_ether_aton(char *a
, u_char
*n
)
789 i
= sscanf(a
, "%x:%x:%x:%x:%x:%x", &o
[0], &o
[1], &o
[2],
790 &o
[3], &o
[4], &o
[5]);
792 fprintf(stderr
, "ndp: invalid Ethernet address '%s'\n", a
);
803 printf("usage: ndp hostname\n");
804 printf(" ndp -a[nt]\n");
805 printf(" ndp [-nt] -A wait\n");
806 printf(" ndp -c[nt]\n");
807 printf(" ndp -d[nt] hostname\n");
808 printf(" ndp -f[nt] filename\n");
809 printf(" ndp -i interface [flags...]\n");
810 #ifdef SIOCSDEFIFACE_IN6
811 printf(" ndp -I [interface|delete]\n");
815 printf(" ndp -s hostname ether_addr [temp] [proxy]\n");
827 struct rt_msghdr
*rtm
= &m_rtmsg
.m_rtm
;
828 char *cp
= m_rtmsg
.m_space
;
832 if (cmd
== RTM_DELETE
)
834 bzero((char *)&m_rtmsg
, sizeof(m_rtmsg
));
835 rtm
->rtm_flags
= flags
;
836 rtm
->rtm_version
= RTM_VERSION
;
840 fprintf(stderr
, "ndp: internal wrong cmd\n");
843 rtm
->rtm_addrs
|= RTA_GATEWAY
;
844 rtm
->rtm_rmx
.rmx_expire
= expire_time
;
845 rtm
->rtm_inits
= RTV_EXPIRE
;
846 rtm
->rtm_flags
|= (RTF_HOST
| RTF_STATIC
);
847 if (rtm
->rtm_flags
& RTF_ANNOUNCE
) {
848 rtm
->rtm_flags
&= ~RTF_HOST
;
849 rtm
->rtm_flags
|= RTA_NETMASK
;
853 rtm
->rtm_addrs
|= RTA_DST
;
855 #define NEXTADDR(w, s) \
856 if (rtm->rtm_addrs & (w)) { \
857 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
859 NEXTADDR(RTA_DST
, sin_m
);
860 NEXTADDR(RTA_GATEWAY
, sdl_m
);
861 memset(&so_mask
.sin6_addr
, 0xff, sizeof(so_mask
.sin6_addr
));
862 NEXTADDR(RTA_NETMASK
, so_mask
);
864 rtm
->rtm_msglen
= cp
- (char *)&m_rtmsg
;
867 rtm
->rtm_seq
= ++seq
;
869 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
870 if (errno
!= ESRCH
|| cmd
!= RTM_DELETE
) {
871 perror("writing to routing socket");
876 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
877 } while (l
> 0 && (rtm
->rtm_seq
!= seq
|| rtm
->rtm_pid
!= pid
));
879 fprintf(stderr
, "ndp: read from routing socket: %s\n",
885 ifinfo(int argc
, char **argv
)
887 struct in6_ndireq nd
;
889 char *ifname
= argv
[0];
891 #ifdef IPV6CTL_USETEMPADDR
895 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
896 perror("ndp: socket");
899 bzero(&nd
, sizeof(nd
));
900 strcpy(nd
.ifname
, ifname
);
901 if (ioctl(s
, SIOCGIFINFO_IN6
, (caddr_t
)&nd
) < 0) {
902 perror("ioctl (SIOCGIFINFO_IN6)");
907 for (i
= 1; i
< argc
; i
++) {
916 #define SETFLAG(s, f) \
918 if (strcmp(cp, (s)) == 0) {\
925 SETFLAG("nud", ND6_IFF_PERFORMNUD
);
926 #ifdef ND6_IFF_ACCEPT_RTADV
927 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV
);
930 if (ioctl(s
, SIOCSIFINFO_FLAGS
, (caddr_t
)&nd
) < 0) {
931 perror("ioctl(SIOCSIFINFO_FLAGS)");
937 printf("linkmtu=%d", ND
.linkmtu
);
938 printf(", curhlim=%d", ND
.chlim
);
939 printf(", basereachable=%ds%dms",
940 ND
.basereachable
/ 1000, ND
.basereachable
% 1000);
941 printf(", reachable=%ds", ND
.reachable
);
942 printf(", retrans=%ds%dms", ND
.retrans
/ 1000, ND
.retrans
% 1000);
943 #ifdef IPV6CTL_USETEMPADDR
944 memset(nullbuf
, 0, sizeof(nullbuf
));
945 if (memcmp(nullbuf
, ND
.randomid
, sizeof(nullbuf
)) != 0) {
949 for (i
= 0; i
< 3; i
++) {
952 printf("\nRandom seed(0): ");
953 rbuf
= ND
.randomseed0
;
956 printf("\nRandom seed(1): ");
957 rbuf
= ND
.randomseed1
;
960 printf("\nRandom ID: ");
964 for (j
= 0; j
< 8; j
++)
965 printf("%02x", rbuf
[j
]);
971 if ((ND
.flags
& ND6_IFF_PERFORMNUD
))
973 #ifdef ND6_IFF_ACCEPT_RTADV
974 if ((ND
.flags
& ND6_IFF_ACCEPT_RTADV
))
975 printf("accept_rtadv ");
984 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */
985 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
991 #ifdef ICMPV6CTL_ND6_DRLIST
992 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_DRLIST
};
994 struct in6_defrouter
*p
, *ep
;
998 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), NULL
, &l
, NULL
, 0) < 0) {
999 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1004 errx(1, "not enough core");
1007 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), buf
, &l
, NULL
, 0) < 0) {
1008 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1012 ep
= (struct in6_defrouter
*)(buf
+ l
);
1013 for (p
= (struct in6_defrouter
*)buf
; p
< ep
; p
++) {
1016 if (getnameinfo((struct sockaddr
*)&p
->rtaddr
,
1017 p
->rtaddr
.sin6_len
, host_buf
, sizeof(host_buf
), NULL
, 0,
1018 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0)) != 0)
1019 strlcpy(host_buf
, "?", sizeof(host_buf
));
1021 printf("%s if=%s", host_buf
,
1022 if_indextoname(p
->if_index
, ifix_buf
));
1023 printf(", flags=%s%s",
1024 p
->flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1025 p
->flags
& ND_RA_FLAG_OTHER
? "O" : "");
1026 rtpref
= ((p
->flags
& ND_RA_FLAG_RTPREF_MASK
) >> 3) & 0xff;
1027 printf(", pref=%s", rtpref_str
[rtpref
]);
1029 gettimeofday(&time
, 0);
1031 printf(", expire=Never\n");
1033 printf(", expire=%s\n",
1034 sec2str(p
->expire
- time
.tv_sec
));
1038 struct in6_drlist dr
;
1040 struct timeval time
;
1042 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1043 perror("ndp: socket");
1046 bzero(&dr
, sizeof(dr
));
1047 strcpy(dr
.ifname
, "lo0"); /* dummy */
1048 if (ioctl(s
, SIOCGDRLST_IN6
, (caddr_t
)&dr
) < 0) {
1049 perror("ioctl (SIOCGDRLST_IN6)");
1052 #define DR dr.defrouter[i]
1053 for (i
= 0 ; DR
.if_index
&& i
< DRLSTSIZ
; i
++) {
1054 struct sockaddr_in6 sin6
;
1056 bzero(&sin6
, sizeof(sin6
));
1057 sin6
.sin6_family
= AF_INET6
;
1058 sin6
.sin6_len
= sizeof(sin6
);
1059 sin6
.sin6_addr
= DR
.rtaddr
;
1060 getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
, host_buf
,
1061 sizeof(host_buf
), NULL
, 0,
1062 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
1064 printf("%s if=%s", host_buf
,
1065 if_indextoname(DR
.if_index
, ifix_buf
));
1066 printf(", flags=%s%s",
1067 DR
.flags
& ND_RA_FLAG_MANAGED
? "M" : "",
1068 DR
.flags
& ND_RA_FLAG_OTHER
? "O" : "");
1069 gettimeofday(&time
, 0);
1071 printf(", expire=Never\n");
1073 printf(", expire=%s\n",
1074 sec2str(DR
.expire
- time
.tv_sec
));
1084 #ifdef ICMPV6CTL_ND6_PRLIST
1085 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_ICMPV6
, ICMPV6CTL_ND6_PRLIST
};
1087 struct in6_prefix
*p
, *ep
, *n
;
1088 struct sockaddr_in6
*advrtr
;
1090 struct timeval time
;
1091 #ifdef NI_WITHSCOPEID
1092 const int niflags
= NI_NUMERICHOST
| NI_WITHSCOPEID
;
1093 int ninflags
= (nflag
? NI_NUMERICHOST
: 0) | NI_WITHSCOPEID
;
1095 const int niflags
= NI_NUMERICHOST
;
1096 int ninflags
= nflag
? NI_NUMERICHOST
: 0;
1098 char namebuf
[NI_MAXHOST
];
1100 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), NULL
, &l
, NULL
, 0) < 0) {
1101 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1106 errx(1, "not enough core");
1109 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), buf
, &l
, NULL
, 0) < 0) {
1110 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1114 ep
= (struct in6_prefix
*)(buf
+ l
);
1115 for (p
= (struct in6_prefix
*)buf
; p
< ep
; p
= n
) {
1116 advrtr
= (struct sockaddr_in6
*)(p
+ 1);
1117 n
= (struct in6_prefix
*)&advrtr
[p
->advrtrs
];
1119 if (getnameinfo((struct sockaddr
*)&p
->prefix
,
1120 p
->prefix
.sin6_len
, namebuf
, sizeof(namebuf
),
1121 NULL
, 0, niflags
) != 0)
1122 strlcpy(namebuf
, "?", sizeof(namebuf
));
1123 printf("%s/%d if=%s\n", namebuf
, p
->prefixlen
,
1124 if_indextoname(p
->if_index
, ifix_buf
));
1126 gettimeofday(&time
, 0);
1128 * meaning of fields, especially flags, is very different
1129 * by origin. notify the difference to the users.
1131 printf("flags=%s%s%s%s%s",
1132 p
->raflags
.onlink
? "L" : "",
1133 p
->raflags
.autonomous
? "A" : "",
1134 (p
->flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1135 (p
->flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1137 (p
->flags
& NDPRF_HOME
) != 0 ? "H" : ""
1142 if (p
->vltime
== ND6_INFINITE_LIFETIME
)
1143 printf(" vltime=infinity");
1145 printf(" vltime=%ld", (long)p
->vltime
);
1146 if (p
->pltime
== ND6_INFINITE_LIFETIME
)
1147 printf(", pltime=infinity");
1149 printf(", pltime=%ld", (long)p
->pltime
);
1151 printf(", expire=Never");
1152 else if (p
->expire
>= time
.tv_sec
)
1153 printf(", expire=%s",
1154 sec2str(p
->expire
- time
.tv_sec
));
1156 printf(", expired");
1157 printf(", ref=%d", p
->refcnt
);
1160 * "advertising router" list is meaningful only if the prefix
1161 * information is from RA.
1165 struct sockaddr_in6
*sin6
;
1167 sin6
= (struct sockaddr_in6
*)(p
+ 1);
1168 printf(" advertised by\n");
1169 for (j
= 0; j
< p
->advrtrs
; j
++) {
1170 struct in6_nbrinfo
*nbi
;
1172 if (getnameinfo((struct sockaddr
*)sin6
,
1173 sin6
->sin6_len
, namebuf
, sizeof(namebuf
),
1174 NULL
, 0, ninflags
) != 0)
1175 strlcpy(namebuf
, "?", sizeof(namebuf
));
1176 printf(" %s", namebuf
);
1178 nbi
= getnbrinfo(&sin6
->sin6_addr
, p
->if_index
,
1181 switch(nbi
->state
) {
1182 case ND6_LLINFO_REACHABLE
:
1183 case ND6_LLINFO_STALE
:
1184 case ND6_LLINFO_DELAY
:
1185 case ND6_LLINFO_PROBE
:
1186 printf(" (reachable)\n");
1189 printf(" (unreachable)\n");
1192 printf(" (no neighbor state)\n");
1196 printf(" No advertising router\n");
1200 struct in6_prlist pr
;
1202 struct timeval time
;
1204 gettimeofday(&time
, 0);
1206 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
1207 perror("ndp: socket");
1210 bzero(&pr
, sizeof(pr
));
1211 strcpy(pr
.ifname
, "lo0"); /* dummy */
1212 if (ioctl(s
, SIOCGPRLST_IN6
, (caddr_t
)&pr
) < 0) {
1213 perror("ioctl (SIOCGPRLST_IN6)");
1216 #define PR pr.prefix[i]
1217 for (i
= 0; PR
.if_index
&& i
< PRLSTSIZ
; i
++) {
1218 struct sockaddr_in6 p6
;
1219 char namebuf
[NI_MAXHOST
];
1225 memset(&p6
, 0, sizeof(p6
));
1226 p6
.sin6_family
= AF_INET6
;
1227 p6
.sin6_len
= sizeof(p6
);
1228 p6
.sin6_addr
= PR
.prefix
;
1232 * copy link index to sin6_scope_id field.
1233 * XXX: KAME specific.
1235 if (IN6_IS_ADDR_LINKLOCAL(&p6
.sin6_addr
)) {
1238 memcpy(&linkid
, &p6
.sin6_addr
.s6_addr
[2],
1240 linkid
= ntohs(linkid
);
1241 p6
.sin6_scope_id
= linkid
;
1242 p6
.sin6_addr
.s6_addr
[2] = 0;
1243 p6
.sin6_addr
.s6_addr
[3] = 0;
1246 niflags
= NI_NUMERICHOST
;
1248 niflags
|= NI_WITHSCOPEID
;
1250 if (getnameinfo((struct sockaddr
*)&p6
,
1251 sizeof(p6
), namebuf
, sizeof(namebuf
),
1252 NULL
, 0, niflags
)) {
1253 warnx("getnameinfo failed");
1256 printf("%s/%d if=%s\n", namebuf
, PR
.prefixlen
,
1257 if_indextoname(PR
.if_index
, ifix_buf
));
1259 gettimeofday(&time
, 0);
1261 * meaning of fields, especially flags, is very different
1262 * by origin. notify the difference to the users.
1266 PR
.origin
== PR_ORIG_RA
? "" : "advertise: ");
1269 printf("flags=%s%s%s%s%s",
1270 PR
.raflags
.onlink
? "L" : "",
1271 PR
.raflags
.autonomous
? "A" : "",
1272 (PR
.flags
& NDPRF_ONLINK
) != 0 ? "O" : "",
1273 (PR
.flags
& NDPRF_DETACHED
) != 0 ? "D" : "",
1275 (PR
.flags
& NDPRF_HOME
) != 0 ? "H" : ""
1281 printf("flags=%s%s",
1282 PR
.raflags
.onlink
? "L" : "",
1283 PR
.raflags
.autonomous
? "A" : "");
1285 if (PR
.vltime
== ND6_INFINITE_LIFETIME
)
1286 printf(" vltime=infinity");
1288 printf(" vltime=%ld", (long)PR
.vltime
);
1289 if (PR
.pltime
== ND6_INFINITE_LIFETIME
)
1290 printf(", pltime=infinity");
1292 printf(", pltime=%ld", (long)PR
.pltime
);
1294 printf(", expire=Never");
1295 else if (PR
.expire
>= time
.tv_sec
)
1296 printf(", expire=%s",
1297 sec2str(PR
.expire
- time
.tv_sec
));
1299 printf(", expired");
1301 printf(", ref=%d", PR
.refcnt
);
1304 switch (PR
.origin
) {
1306 printf(", origin=RA");
1309 printf(", origin=RR");
1311 case PR_ORIG_STATIC
:
1312 printf(", origin=static");
1314 case PR_ORIG_KERNEL
:
1315 printf(", origin=kernel");
1318 printf(", origin=?");
1324 * "advertising router" list is meaningful only if the prefix
1325 * information is from RA.
1327 if (0 && /* prefix origin is almost obsolted */
1328 PR
.origin
!= PR_ORIG_RA
)
1330 else if (PR
.advrtrs
) {
1332 printf(" advertised by\n");
1333 for (j
= 0; j
< PR
.advrtrs
; j
++) {
1334 struct sockaddr_in6 sin6
;
1335 struct in6_nbrinfo
*nbi
;
1337 bzero(&sin6
, sizeof(sin6
));
1338 sin6
.sin6_family
= AF_INET6
;
1339 sin6
.sin6_len
= sizeof(sin6
);
1340 sin6
.sin6_addr
= PR
.advrtr
[j
];
1341 sin6
.sin6_scope_id
= PR
.if_index
; /* XXX */
1342 getnameinfo((struct sockaddr
*)&sin6
,
1343 sin6
.sin6_len
, host_buf
,
1344 sizeof(host_buf
), NULL
, 0,
1345 NI_WITHSCOPEID
| (nflag
? NI_NUMERICHOST
: 0));
1346 printf(" %s", host_buf
);
1348 nbi
= getnbrinfo(&sin6
.sin6_addr
, PR
.if_index
,
1351 switch(nbi
->state
) {
1352 case ND6_LLINFO_REACHABLE
:
1353 case ND6_LLINFO_STALE
:
1354 case ND6_LLINFO_DELAY
:
1355 case ND6_LLINFO_PROBE
:
1356 printf(" (reachable)\n");
1359 printf(" (unreachable)\n");
1362 printf(" (no neighbor state)\n");
1364 if (PR
.advrtrs
> DRLSTSIZ
)
1365 printf(" and %d routers\n",
1366 PR
.advrtrs
- DRLSTSIZ
);
1368 printf(" No advertising router\n");
1378 char dummyif
[IFNAMSIZ
+8];
1381 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1383 strcpy(dummyif
, "lo0"); /* dummy */
1384 if (ioctl(s
, SIOCSPFXFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1385 err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1391 char dummyif
[IFNAMSIZ
+8];
1394 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1396 strcpy(dummyif
, "lo0"); /* dummy */
1397 if (ioctl(s
, SIOCSRTRFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1398 err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1406 char dummyif
[IFNAMSIZ
+8];
1409 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1411 strcpy(dummyif
, "lo0"); /* dummy */
1412 if (ioctl(s
, SIOCSNDFLUSH_IN6
, (caddr_t
)&dummyif
) < 0)
1413 err(1, "ioctl (SIOCSNDFLUSH_IN6)");
1418 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */
1420 setdefif(char *ifname
)
1422 struct in6_ndifreq ndifreq
;
1423 unsigned int ifindex
;
1425 if (strcasecmp(ifname
, "delete") == 0)
1428 if ((ifindex
= if_nametoindex(ifname
)) == 0)
1429 err(1, "failed to resolve i/f index for %s", ifname
);
1432 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1435 strcpy(ndifreq
.ifname
, "lo0"); /* dummy */
1436 ndifreq
.ifindex
= ifindex
;
1438 if (ioctl(s
, SIOCSDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1439 err(1, "ioctl (SIOCSDEFIFACE_IN6)");
1447 struct in6_ndifreq ndifreq
;
1448 char ifname
[IFNAMSIZ
+8];
1450 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
1453 memset(&ndifreq
, 0, sizeof(ndifreq
));
1454 strcpy(ndifreq
.ifname
, "lo0"); /* dummy */
1456 if (ioctl(s
, SIOCGDEFIFACE_IN6
, (caddr_t
)&ndifreq
) < 0)
1457 err(1, "ioctl (SIOCGDEFIFACE_IN6)");
1459 if (ndifreq
.ifindex
== 0)
1460 printf("No default interface.\n");
1462 if ((if_indextoname(ndifreq
.ifindex
, ifname
)) == NULL
)
1463 err(1, "failed to resolve ifname for index %lu",
1465 printf("ND default interface = %s\n", ifname
);
1473 sec2str(time_t total
)
1475 static char result
[256];
1476 int days
, hours
, mins
, secs
;
1480 days
= total
/ 3600 / 24;
1481 hours
= (total
/ 3600) % 24;
1482 mins
= (total
/ 60) % 60;
1487 p
+= sprintf(p
, "%dd", days
);
1489 if (!first
|| hours
) {
1491 p
+= sprintf(p
, "%dh", hours
);
1493 if (!first
|| mins
) {
1495 p
+= sprintf(p
, "%dm", mins
);
1497 sprintf(p
, "%ds", secs
);
1503 * Print the timestamp
1504 * from tcpdump/util.c
1507 ts_print(const struct timeval
*tvp
)
1512 s
= (tvp
->tv_sec
+ thiszone
) % 86400;
1513 printf("%02d:%02d:%02d.%06u ",
1514 s
/ 3600, (s
% 3600) / 60, s
% 60, (u_int32_t
)tvp
->tv_usec
);