Home | History | Annotate | Line # | Download | only in ndp
ndp.c revision 1.16
      1  1.16  itojun /*	$NetBSD: ndp.c,v 1.16 2001/07/23 14:47:43 itojun Exp $	*/
      2  1.16  itojun /*	$KAME: ndp.c,v 1.69 2001/07/23 14:46:31 itojun Exp $	*/
      3   1.9  itojun 
      4   1.1  itojun /*
      5   1.1  itojun  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
      6   1.1  itojun  * All rights reserved.
      7   1.9  itojun  *
      8   1.1  itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1  itojun  * modification, are permitted provided that the following conditions
     10   1.1  itojun  * are met:
     11   1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  itojun  *    documentation and/or other materials provided with the distribution.
     16   1.1  itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.1  itojun  *    may be used to endorse or promote products derived from this software
     18   1.1  itojun  *    without specific prior written permission.
     19   1.9  itojun  *
     20   1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1  itojun  * SUCH DAMAGE.
     31   1.1  itojun  */
     32   1.1  itojun /*
     33   1.1  itojun  * Copyright (c) 1984, 1993
     34   1.1  itojun  *	The Regents of the University of California.  All rights reserved.
     35   1.1  itojun  *
     36   1.1  itojun  * This code is derived from software contributed to Berkeley by
     37   1.1  itojun  * Sun Microsystems, Inc.
     38   1.1  itojun  *
     39   1.1  itojun  * Redistribution and use in source and binary forms, with or without
     40   1.1  itojun  * modification, are permitted provided that the following conditions
     41   1.1  itojun  * are met:
     42   1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     43   1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     44   1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     45   1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     46   1.1  itojun  *    documentation and/or other materials provided with the distribution.
     47   1.1  itojun  * 3. All advertising materials mentioning features or use of this software
     48   1.1  itojun  *    must display the following acknowledgement:
     49   1.1  itojun  *	This product includes software developed by the University of
     50   1.1  itojun  *	California, Berkeley and its contributors.
     51   1.1  itojun  * 4. Neither the name of the University nor the names of its contributors
     52   1.1  itojun  *    may be used to endorse or promote products derived from this software
     53   1.1  itojun  *    without specific prior written permission.
     54   1.1  itojun  *
     55   1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56   1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57   1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58   1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59   1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60   1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61   1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62   1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63   1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64   1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65   1.1  itojun  * SUCH DAMAGE.
     66   1.1  itojun  */
     67   1.1  itojun 
     68   1.1  itojun /*
     69   1.1  itojun  * Based on:
     70   1.1  itojun  * "@(#) Copyright (c) 1984, 1993\n\
     71   1.1  itojun  *	The Regents of the University of California.  All rights reserved.\n";
     72   1.1  itojun  *
     73   1.1  itojun  * "@(#)arp.c	8.2 (Berkeley) 1/2/94";
     74   1.1  itojun  */
     75   1.1  itojun 
     76   1.1  itojun /*
     77   1.1  itojun  * ndp - display, set, delete and flush neighbor cache
     78   1.1  itojun  */
     79   1.1  itojun 
     80   1.1  itojun 
     81   1.1  itojun #include <sys/param.h>
     82   1.1  itojun #include <sys/file.h>
     83   1.1  itojun #include <sys/ioctl.h>
     84   1.1  itojun #include <sys/socket.h>
     85   1.1  itojun #include <sys/sysctl.h>
     86   1.1  itojun #include <sys/time.h>
     87  1.11  itojun #include <sys/queue.h>
     88   1.1  itojun 
     89   1.1  itojun #include <net/if.h>
     90   1.1  itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     91   1.1  itojun #include <net/if_var.h>
     92   1.1  itojun #endif /* __FreeBSD__ >= 3 */
     93   1.1  itojun #include <net/if_dl.h>
     94   1.1  itojun #include <net/if_types.h>
     95   1.1  itojun #include <net/route.h>
     96   1.1  itojun 
     97   1.1  itojun #include <netinet/in.h>
     98   1.1  itojun #ifndef __NetBSD__
     99   1.1  itojun #include <netinet/if_ether.h>
    100   1.1  itojun #endif
    101   1.1  itojun 
    102   1.1  itojun #include <netinet/icmp6.h>
    103   1.1  itojun #include <netinet6/in6_var.h>
    104   1.1  itojun #include <netinet6/nd6.h>
    105   1.1  itojun 
    106   1.1  itojun #include <arpa/inet.h>
    107   1.1  itojun 
    108   1.1  itojun #include <netdb.h>
    109   1.1  itojun #include <errno.h>
    110   1.1  itojun #include <nlist.h>
    111   1.1  itojun #include <stdio.h>
    112   1.1  itojun #include <string.h>
    113   1.1  itojun #include <paths.h>
    114   1.1  itojun #include <err.h>
    115   1.1  itojun #include <stdlib.h>
    116   1.1  itojun #include <fcntl.h>
    117   1.1  itojun #include <unistd.h>
    118  1.16  itojun #include <err.h>
    119   1.1  itojun #include "gmt2local.h"
    120   1.1  itojun 
    121   1.4  itojun #ifndef NI_WITHSCOPEID
    122   1.4  itojun #define NI_WITHSCOPEID	0
    123   1.4  itojun #endif
    124   1.4  itojun 
    125   1.3  itojun /* packing rule for routing socket */
    126   1.3  itojun #define ROUNDUP(a) \
    127   1.3  itojun 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    128   1.3  itojun #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    129   1.3  itojun 
    130   1.1  itojun static int pid;
    131  1.11  itojun static int cflag;
    132   1.1  itojun static int nflag;
    133   1.1  itojun static int tflag;
    134   1.1  itojun static int32_t thiszone;	/* time difference with gmt */
    135   1.1  itojun static int s = -1;
    136   1.1  itojun static int repeat = 0;
    137   1.1  itojun 
    138   1.1  itojun char ntop_buf[INET6_ADDRSTRLEN];	/* inet_ntop() */
    139   1.4  itojun char host_buf[NI_MAXHOST];		/* getnameinfo() */
    140   1.1  itojun char ifix_buf[IFNAMSIZ];		/* if_indextoname() */
    141   1.1  itojun 
    142   1.1  itojun int main __P((int, char **));
    143   1.1  itojun int file __P((char *));
    144   1.1  itojun void getsocket __P((void));
    145   1.1  itojun int set __P((int, char **));
    146   1.1  itojun void get __P((char *));
    147   1.1  itojun int delete __P((char *));
    148   1.1  itojun void dump __P((struct in6_addr *));
    149   1.4  itojun static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr,
    150   1.4  itojun 					   int ifindex, int));
    151   1.1  itojun static char *ether_str __P((struct sockaddr_dl *));
    152   1.1  itojun int ndp_ether_aton __P((char *, u_char *));
    153   1.1  itojun void usage __P((void));
    154   1.1  itojun int rtmsg __P((int));
    155   1.8  itojun void ifinfo __P((int, char **));
    156   1.1  itojun void rtrlist __P((void));
    157   1.1  itojun void plist __P((void));
    158   1.1  itojun void pfx_flush __P((void));
    159   1.9  itojun void rtrlist __P((void));
    160   1.1  itojun void rtr_flush __P((void));
    161   1.1  itojun void harmonize_rtr __P((void));
    162   1.4  itojun #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
    163   1.4  itojun static void getdefif __P((void));
    164   1.4  itojun static void setdefif __P((char *));
    165   1.4  itojun #endif
    166   1.1  itojun static char *sec2str __P((time_t t));
    167   1.1  itojun static char *ether_str __P((struct sockaddr_dl *sdl));
    168   1.1  itojun static void ts_print __P((const struct timeval *));
    169   1.1  itojun 
    170  1.16  itojun #ifdef ICMPV6CTL_ND6_DRLIST
    171  1.16  itojun static char *rtpref_str[] = {
    172  1.16  itojun 	"medium",		/* 00 */
    173  1.16  itojun 	"high",			/* 01 */
    174  1.16  itojun 	"rsv",			/* 10 */
    175  1.16  itojun 	"low"			/* 11 */
    176  1.16  itojun };
    177  1.16  itojun #endif
    178  1.16  itojun 
    179   1.1  itojun int
    180   1.1  itojun main(argc, argv)
    181   1.1  itojun 	int argc;
    182   1.1  itojun 	char **argv;
    183   1.1  itojun {
    184   1.1  itojun 	int ch;
    185  1.11  itojun 	int aflag = 0, dflag = 0, sflag = 0, Hflag = 0,
    186   1.1  itojun 		pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
    187   1.1  itojun 
    188   1.1  itojun 	pid = getpid();
    189   1.1  itojun 	thiszone = gmt2local(0);
    190  1.15  kleink 	while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1)
    191   1.1  itojun 		switch ((char)ch) {
    192   1.1  itojun 		case 'a':
    193   1.1  itojun 			aflag = 1;
    194   1.1  itojun 			break;
    195   1.1  itojun 		case 'c':
    196   1.1  itojun 			cflag = 1;
    197   1.1  itojun 			break;
    198   1.1  itojun 		case 'd':
    199   1.1  itojun 			dflag = 1;
    200   1.1  itojun 			break;
    201   1.4  itojun 		case 'I':
    202   1.4  itojun #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
    203   1.4  itojun 			if (argc > 2)
    204   1.4  itojun 				setdefif(argv[2]);
    205   1.4  itojun 			getdefif(); /* always call it to print the result */
    206   1.4  itojun 			exit(0);
    207   1.4  itojun #else
    208   1.4  itojun 			errx(1, "not supported yet");
    209   1.4  itojun 			/*NOTREACHED*/
    210   1.4  itojun #endif
    211   1.1  itojun 		case 'i' :
    212   1.8  itojun 			argc -= optind;
    213   1.8  itojun 			argv += optind;
    214   1.8  itojun 			if (argc < 1)
    215   1.1  itojun 				usage();
    216   1.8  itojun 			ifinfo(argc, argv);
    217   1.1  itojun 			exit(0);
    218   1.1  itojun 		case 'n':
    219   1.1  itojun 			nflag = 1;
    220   1.1  itojun 			continue;
    221   1.1  itojun 		case 'p':
    222   1.1  itojun 			pflag = 1;
    223   1.1  itojun 			break;
    224   1.1  itojun 		case 'f' :
    225   1.1  itojun 			if (argc != 3)
    226   1.1  itojun 				usage();
    227   1.1  itojun 			file(argv[2]);
    228   1.1  itojun 			exit(0);
    229   1.4  itojun 		case 'l' :
    230  1.13  itojun 			/* obsolete, ignored */
    231   1.4  itojun 			break;
    232   1.1  itojun 		case 'r' :
    233   1.1  itojun 			rflag = 1;
    234   1.1  itojun 			break;
    235   1.1  itojun 		case 's':
    236   1.1  itojun 			sflag = 1;
    237   1.1  itojun 			break;
    238   1.1  itojun 		case 't':
    239   1.1  itojun 			tflag = 1;
    240   1.1  itojun 			break;
    241   1.1  itojun 		case 'A':
    242   1.1  itojun 			aflag = 1;
    243   1.1  itojun 			repeat = atoi(optarg);
    244   1.1  itojun 			if (repeat < 0)
    245   1.1  itojun 				usage();
    246   1.1  itojun 			break;
    247   1.1  itojun 		case 'H' :
    248   1.1  itojun 			Hflag = 1;
    249   1.1  itojun 			break;
    250   1.1  itojun 		case 'P':
    251   1.1  itojun 			Pflag = 1;
    252   1.1  itojun 			break;
    253   1.1  itojun 		case 'R':
    254   1.1  itojun 			Rflag = 1;
    255   1.1  itojun 			break;
    256   1.1  itojun 		default:
    257   1.1  itojun 			usage();
    258   1.1  itojun 		}
    259   1.1  itojun 
    260   1.1  itojun 	argc -= optind;
    261   1.1  itojun 	argv += optind;
    262   1.1  itojun 
    263   1.1  itojun 	if (aflag || cflag) {
    264   1.1  itojun 		dump(0);
    265   1.1  itojun 		exit(0);
    266   1.1  itojun 	}
    267   1.1  itojun 	if (dflag) {
    268   1.1  itojun 		if (argc != 1)
    269   1.1  itojun 			usage();
    270   1.1  itojun 		delete(argv[0]);
    271   1.9  itojun 		exit(0);
    272   1.1  itojun 	}
    273   1.1  itojun 	if (pflag) {
    274   1.1  itojun 		plist();
    275   1.1  itojun 		exit(0);
    276   1.1  itojun 	}
    277   1.1  itojun 	if (rflag) {
    278   1.1  itojun 		rtrlist();
    279   1.1  itojun 		exit(0);
    280   1.1  itojun 	}
    281   1.1  itojun 	if (sflag) {
    282   1.1  itojun 		if (argc < 2 || argc > 4)
    283   1.1  itojun 			usage();
    284   1.1  itojun 		exit(set(argc, argv) ? 1 : 0);
    285   1.1  itojun 	}
    286   1.1  itojun 	if (Hflag) {
    287   1.1  itojun 		harmonize_rtr();
    288   1.1  itojun 		exit(0);
    289   1.1  itojun 	}
    290   1.1  itojun 	if (Pflag) {
    291   1.1  itojun 		pfx_flush();
    292   1.1  itojun 		exit(0);
    293   1.1  itojun 	}
    294   1.1  itojun 	if (Rflag) {
    295   1.1  itojun 		rtr_flush();
    296   1.1  itojun 		exit(0);
    297   1.1  itojun 	}
    298   1.1  itojun 
    299   1.1  itojun 	if (argc != 1)
    300   1.1  itojun 		usage();
    301   1.1  itojun 	get(argv[0]);
    302   1.1  itojun 	exit(0);
    303   1.1  itojun }
    304   1.1  itojun 
    305   1.1  itojun /*
    306   1.1  itojun  * Process a file to set standard ndp entries
    307   1.1  itojun  */
    308   1.1  itojun int
    309   1.1  itojun file(name)
    310   1.1  itojun 	char *name;
    311   1.1  itojun {
    312   1.1  itojun 	FILE *fp;
    313   1.1  itojun 	int i, retval;
    314   1.1  itojun 	char line[100], arg[5][50], *args[5];
    315   1.1  itojun 
    316   1.1  itojun 	if ((fp = fopen(name, "r")) == NULL) {
    317   1.1  itojun 		fprintf(stderr, "ndp: cannot open %s\n", name);
    318   1.1  itojun 		exit(1);
    319   1.1  itojun 	}
    320   1.1  itojun 	args[0] = &arg[0][0];
    321   1.1  itojun 	args[1] = &arg[1][0];
    322   1.1  itojun 	args[2] = &arg[2][0];
    323   1.1  itojun 	args[3] = &arg[3][0];
    324   1.1  itojun 	args[4] = &arg[4][0];
    325   1.1  itojun 	retval = 0;
    326   1.1  itojun 	while(fgets(line, 100, fp) != NULL) {
    327   1.1  itojun 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
    328   1.1  itojun 		    arg[3], arg[4]);
    329   1.1  itojun 		if (i < 2) {
    330   1.1  itojun 			fprintf(stderr, "ndp: bad line: %s\n", line);
    331   1.1  itojun 			retval = 1;
    332   1.1  itojun 			continue;
    333   1.1  itojun 		}
    334   1.1  itojun 		if (set(i, args))
    335   1.1  itojun 			retval = 1;
    336   1.1  itojun 	}
    337   1.1  itojun 	fclose(fp);
    338   1.1  itojun 	return (retval);
    339   1.1  itojun }
    340   1.1  itojun 
    341   1.1  itojun void
    342   1.1  itojun getsocket()
    343   1.1  itojun {
    344   1.1  itojun 	if (s < 0) {
    345   1.1  itojun 		s = socket(PF_ROUTE, SOCK_RAW, 0);
    346   1.1  itojun 		if (s < 0) {
    347  1.16  itojun 			err(1, "ndp: socket");
    348  1.16  itojun 			/* NOTREACHED */
    349   1.1  itojun 		}
    350   1.1  itojun 	}
    351   1.1  itojun }
    352   1.1  itojun 
    353   1.6  itojun struct	sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
    354   1.1  itojun struct	sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
    355   1.1  itojun struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
    356   1.1  itojun int	expire_time, flags, found_entry;
    357   1.1  itojun struct	{
    358   1.1  itojun 	struct	rt_msghdr m_rtm;
    359   1.1  itojun 	char	m_space[512];
    360   1.1  itojun }	m_rtmsg;
    361   1.1  itojun 
    362   1.1  itojun /*
    363   1.1  itojun  * Set an individual neighbor cache entry
    364   1.1  itojun  */
    365   1.1  itojun int
    366   1.1  itojun set(argc, argv)
    367   1.1  itojun 	int argc;
    368   1.1  itojun 	char **argv;
    369   1.1  itojun {
    370   1.1  itojun 	register struct sockaddr_in6 *sin = &sin_m;
    371   1.1  itojun 	register struct sockaddr_dl *sdl;
    372   1.1  itojun 	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
    373   1.1  itojun 	struct addrinfo hints, *res;
    374   1.1  itojun 	int gai_error;
    375   1.1  itojun 	u_char *ea;
    376   1.1  itojun 	char *host = argv[0], *eaddr = argv[1];
    377   1.1  itojun 
    378   1.1  itojun 	getsocket();
    379   1.1  itojun 	argc -= 2;
    380   1.1  itojun 	argv += 2;
    381   1.1  itojun 	sdl_m = blank_sdl;
    382   1.1  itojun 	sin_m = blank_sin;
    383   1.1  itojun 
    384   1.1  itojun 	bzero(&hints, sizeof(hints));
    385   1.1  itojun 	hints.ai_family = AF_INET6;
    386   1.1  itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    387   1.1  itojun 	if (gai_error) {
    388   1.1  itojun 		fprintf(stderr, "ndp: %s: %s\n", host,
    389   1.1  itojun 			gai_strerror(gai_error));
    390   1.1  itojun 		return 1;
    391   1.1  itojun 	}
    392   1.1  itojun 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
    393   1.9  itojun #ifdef __KAME__
    394   1.9  itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
    395   1.9  itojun 		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
    396   1.9  itojun 			htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
    397   1.9  itojun 	}
    398   1.9  itojun #endif
    399   1.1  itojun 	ea = (u_char *)LLADDR(&sdl_m);
    400   1.1  itojun 	if (ndp_ether_aton(eaddr, ea) == 0)
    401   1.1  itojun 		sdl_m.sdl_alen = 6;
    402   1.1  itojun 	flags = expire_time = 0;
    403   1.1  itojun 	while (argc-- > 0) {
    404   1.1  itojun 		if (strncmp(argv[0], "temp", 4) == 0) {
    405   1.1  itojun 			struct timeval time;
    406   1.1  itojun 			gettimeofday(&time, 0);
    407   1.1  itojun 			expire_time = time.tv_sec + 20 * 60;
    408   1.6  itojun 		} else if (strncmp(argv[0], "proxy", 5) == 0)
    409   1.6  itojun 			flags |= RTF_ANNOUNCE;
    410   1.1  itojun 		argv++;
    411   1.1  itojun 	}
    412   1.1  itojun 	if (rtmsg(RTM_GET) < 0) {
    413  1.16  itojun 		errx(1, "RTM_GET(%s) failed", host);
    414  1.16  itojun 		/* NOTREACHED */
    415   1.1  itojun 	}
    416   1.1  itojun 	sin = (struct sockaddr_in6 *)(rtm + 1);
    417   1.3  itojun 	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
    418   1.1  itojun 	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
    419   1.1  itojun 		if (sdl->sdl_family == AF_LINK &&
    420   1.1  itojun 		    (rtm->rtm_flags & RTF_LLINFO) &&
    421   1.1  itojun 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    422   1.1  itojun 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    423   1.1  itojun 		case IFT_ISO88024: case IFT_ISO88025:
    424   1.1  itojun 			goto overwrite;
    425   1.1  itojun 		}
    426   1.9  itojun 		/*
    427   1.9  itojun 		 * IPv4 arp command retries with sin_other = SIN_PROXY here.
    428   1.9  itojun 		 */
    429   1.9  itojun 		fprintf(stderr, "set: cannot configure a new entry\n");
    430   1.9  itojun 		return 1;
    431   1.1  itojun 	}
    432   1.9  itojun 
    433   1.1  itojun overwrite:
    434   1.1  itojun 	if (sdl->sdl_family != AF_LINK) {
    435   1.1  itojun 		printf("cannot intuit interface index and type for %s\n", host);
    436   1.1  itojun 		return (1);
    437   1.1  itojun 	}
    438   1.1  itojun 	sdl_m.sdl_type = sdl->sdl_type;
    439   1.1  itojun 	sdl_m.sdl_index = sdl->sdl_index;
    440   1.1  itojun 	return (rtmsg(RTM_ADD));
    441   1.1  itojun }
    442   1.1  itojun 
    443   1.1  itojun /*
    444   1.1  itojun  * Display an individual neighbor cache entry
    445   1.1  itojun  */
    446   1.1  itojun void
    447   1.1  itojun get(host)
    448   1.1  itojun 	char *host;
    449   1.1  itojun {
    450   1.1  itojun 	struct sockaddr_in6 *sin = &sin_m;
    451   1.1  itojun 	struct addrinfo hints, *res;
    452   1.1  itojun 	int gai_error;
    453   1.1  itojun 
    454   1.1  itojun 	sin_m = blank_sin;
    455   1.1  itojun 	bzero(&hints, sizeof(hints));
    456   1.1  itojun 	hints.ai_family = AF_INET6;
    457   1.1  itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    458   1.1  itojun 	if (gai_error) {
    459   1.1  itojun 		fprintf(stderr, "ndp: %s: %s\n", host,
    460   1.1  itojun 			gai_strerror(gai_error));
    461   1.1  itojun 		return;
    462   1.1  itojun 	}
    463   1.1  itojun 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
    464   1.9  itojun #ifdef __KAME__
    465   1.9  itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
    466   1.9  itojun 		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
    467   1.9  itojun 			htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
    468   1.9  itojun 	}
    469   1.9  itojun #endif
    470   1.1  itojun 	dump(&sin->sin6_addr);
    471   1.1  itojun 	if (found_entry == 0) {
    472   1.4  itojun 		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
    473   1.4  itojun 			    sizeof(host_buf), NULL ,0,
    474   1.4  itojun 			    NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
    475   1.4  itojun 		printf("%s (%s) -- no entry\n", host, host_buf);
    476   1.1  itojun 		exit(1);
    477   1.1  itojun 	}
    478   1.1  itojun }
    479   1.1  itojun 
    480   1.1  itojun /*
    481   1.1  itojun  * Delete a neighbor cache entry
    482   1.1  itojun  */
    483   1.1  itojun int
    484   1.1  itojun delete(host)
    485   1.1  itojun 	char *host;
    486   1.1  itojun {
    487   1.4  itojun 	struct sockaddr_in6 *sin = &sin_m;
    488   1.1  itojun 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
    489   1.1  itojun 	struct sockaddr_dl *sdl;
    490   1.1  itojun 	struct addrinfo hints, *res;
    491   1.1  itojun 	int gai_error;
    492   1.1  itojun 
    493   1.1  itojun 	getsocket();
    494   1.1  itojun 	sin_m = blank_sin;
    495   1.1  itojun 
    496   1.1  itojun 	bzero(&hints, sizeof(hints));
    497   1.1  itojun 	hints.ai_family = AF_INET6;
    498   1.1  itojun 	gai_error = getaddrinfo(host, NULL, &hints, &res);
    499   1.1  itojun 	if (gai_error) {
    500   1.1  itojun 		fprintf(stderr, "ndp: %s: %s\n", host,
    501   1.1  itojun 			gai_strerror(gai_error));
    502   1.1  itojun 		return 1;
    503   1.1  itojun 	}
    504   1.1  itojun 	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
    505   1.9  itojun #ifdef __KAME__
    506   1.9  itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
    507   1.9  itojun 		*(u_int16_t *)&sin->sin6_addr.s6_addr[2] =
    508   1.9  itojun 			htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id);
    509   1.9  itojun 	}
    510   1.9  itojun #endif
    511   1.1  itojun 	if (rtmsg(RTM_GET) < 0) {
    512  1.16  itojun 		errx(1, "RTM_GET(%s) failed", host);
    513  1.16  itojun 		/* NOTREACHED */
    514   1.1  itojun 	}
    515   1.1  itojun 	sin = (struct sockaddr_in6 *)(rtm + 1);
    516   1.3  itojun 	sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin);
    517   1.1  itojun 	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
    518   1.1  itojun 		if (sdl->sdl_family == AF_LINK &&
    519   1.1  itojun 		    (rtm->rtm_flags & RTF_LLINFO) &&
    520   1.8  itojun 		    !(rtm->rtm_flags & RTF_GATEWAY)) {
    521  1.13  itojun 			goto delete;
    522   1.1  itojun 		}
    523   1.9  itojun 		/*
    524   1.9  itojun 		 * IPv4 arp command retries with sin_other = SIN_PROXY here.
    525   1.9  itojun 		 */
    526   1.9  itojun 		fprintf(stderr, "delete: cannot delete non-NDP entry\n");
    527   1.9  itojun 		return 1;
    528   1.1  itojun 	}
    529   1.8  itojun 
    530   1.1  itojun delete:
    531   1.1  itojun 	if (sdl->sdl_family != AF_LINK) {
    532   1.1  itojun 		printf("cannot locate %s\n", host);
    533   1.1  itojun 		return (1);
    534   1.1  itojun 	}
    535   1.4  itojun 	if (rtmsg(RTM_DELETE) == 0) {
    536  1.11  itojun 		struct sockaddr_in6 s6 = *sin; /* XXX: for safety */
    537  1.11  itojun 
    538  1.11  itojun #ifdef __KAME__
    539  1.11  itojun 		if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
    540  1.11  itojun 			s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]);
    541  1.11  itojun 			*(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0;
    542  1.11  itojun 		}
    543  1.11  itojun #endif
    544  1.11  itojun 		getnameinfo((struct sockaddr *)&s6,
    545  1.11  itojun 			    s6.sin6_len, host_buf,
    546  1.11  itojun 			    sizeof(host_buf), NULL, 0,
    547  1.11  itojun 			    NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
    548   1.4  itojun 		printf("%s (%s) deleted\n", host, host_buf);
    549   1.4  itojun 	}
    550   1.4  itojun 
    551   1.1  itojun 	return 0;
    552   1.1  itojun }
    553   1.1  itojun 
    554  1.13  itojun #define W_ADDR	31
    555  1.13  itojun #define W_LL	17
    556  1.13  itojun #define W_IF	6
    557  1.13  itojun 
    558   1.1  itojun /*
    559   1.1  itojun  * Dump the entire neighbor cache
    560   1.1  itojun  */
    561   1.1  itojun void
    562   1.1  itojun dump(addr)
    563   1.1  itojun 	struct in6_addr *addr;
    564   1.1  itojun {
    565   1.1  itojun 	int mib[6];
    566   1.1  itojun 	size_t needed;
    567   1.4  itojun 	char *lim, *buf, *next;
    568   1.1  itojun 	struct rt_msghdr *rtm;
    569   1.1  itojun 	struct sockaddr_in6 *sin;
    570   1.1  itojun 	struct sockaddr_dl *sdl;
    571  1.11  itojun 	extern int h_errno;
    572   1.1  itojun 	struct in6_nbrinfo *nbi;
    573   1.1  itojun 	struct timeval time;
    574   1.4  itojun 	int addrwidth;
    575  1.13  itojun 	int llwidth;
    576  1.13  itojun 	int ifwidth;
    577   1.4  itojun 	char flgbuf[8];
    578  1.14  itojun 	char *ifname;
    579   1.1  itojun 
    580   1.1  itojun 	/* Print header */
    581  1.11  itojun 	if (!tflag && !cflag)
    582  1.13  itojun 		printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n",
    583  1.13  itojun 		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
    584  1.13  itojun 		    W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs");
    585   1.1  itojun 
    586   1.1  itojun again:;
    587   1.1  itojun 	mib[0] = CTL_NET;
    588   1.1  itojun 	mib[1] = PF_ROUTE;
    589   1.1  itojun 	mib[2] = 0;
    590   1.1  itojun 	mib[3] = AF_INET6;
    591   1.1  itojun 	mib[4] = NET_RT_FLAGS;
    592   1.1  itojun 	mib[5] = RTF_LLINFO;
    593   1.1  itojun 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    594   1.3  itojun 		err(1, "sysctl(PF_ROUTE estimate)");
    595   1.3  itojun 	if (needed > 0) {
    596   1.3  itojun 		if ((buf = malloc(needed)) == NULL)
    597   1.3  itojun 			errx(1, "malloc");
    598   1.3  itojun 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    599   1.3  itojun 			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
    600   1.3  itojun 		lim = buf + needed;
    601   1.3  itojun 	} else
    602   1.3  itojun 		buf = lim = NULL;
    603   1.1  itojun 
    604   1.3  itojun 	for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
    605   1.1  itojun 		int isrouter = 0, prbs = 0;
    606   1.1  itojun 
    607   1.1  itojun 		rtm = (struct rt_msghdr *)next;
    608   1.1  itojun 		sin = (struct sockaddr_in6 *)(rtm + 1);
    609   1.3  itojun 		sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len));
    610  1.16  itojun 
    611  1.16  itojun 		/*
    612  1.16  itojun 		 * Some OSes can produce a route that has the LINK flag but
    613  1.16  itojun 		 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
    614  1.16  itojun 		 * and BSD/OS, where xx is not the interface identifier on
    615  1.16  itojun 		 * lo0).  Such routes entry would annoy getnbrinfo() below,
    616  1.16  itojun 		 * so we skip them.
    617  1.16  itojun 		 * XXX: such routes should have the GATEWAY flag, not the
    618  1.16  itojun 		 * LINK flag.  However, there is rotten routing software
    619  1.16  itojun 		 * that advertises all routes that have the GATEWAY flag.
    620  1.16  itojun 		 * Thus, KAME kernel intentionally does not set the LINK flag.
    621  1.16  itojun 		 * What is to be fixed is not ndp, but such routing software
    622  1.16  itojun 		 * (and the kernel workaround)...
    623  1.16  itojun 		 */
    624  1.16  itojun 		if (sdl->sdl_family != AF_LINK)
    625  1.16  itojun 			continue;
    626  1.16  itojun 
    627   1.1  itojun 		if (addr) {
    628   1.1  itojun 			if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
    629   1.1  itojun 				continue;
    630   1.1  itojun 			found_entry = 1;
    631   1.1  itojun 		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
    632   1.1  itojun 			continue;
    633   1.4  itojun 		if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
    634   1.4  itojun 		    IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
    635   1.4  itojun 			/* XXX: should scope id be filled in the kernel? */
    636   1.4  itojun 			if (sin->sin6_scope_id == 0)
    637   1.4  itojun 				sin->sin6_scope_id = sdl->sdl_index;
    638  1.11  itojun #ifdef __KAME__
    639  1.11  itojun 			/* KAME specific hack; removed the embedded id */
    640   1.4  itojun 			*(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0;
    641  1.11  itojun #endif
    642   1.1  itojun 		}
    643   1.4  itojun 		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
    644   1.4  itojun 			    sizeof(host_buf), NULL, 0,
    645   1.4  itojun 			    NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
    646  1.11  itojun 		if (cflag == 1) {
    647  1.11  itojun 			delete(host_buf);
    648  1.11  itojun 			continue;
    649  1.11  itojun 		}
    650   1.1  itojun 		gettimeofday(&time, 0);
    651   1.1  itojun 		if (tflag)
    652   1.1  itojun 			ts_print(&time);
    653   1.1  itojun 
    654  1.13  itojun 		addrwidth = strlen(host_buf);
    655  1.13  itojun 		if (addrwidth < W_ADDR)
    656  1.13  itojun 			addrwidth = W_ADDR;
    657  1.13  itojun 		llwidth = strlen(ether_str(sdl));
    658  1.13  itojun 		if (W_ADDR + W_LL - addrwidth > llwidth)
    659  1.13  itojun 			llwidth = W_ADDR + W_LL - addrwidth;
    660  1.14  itojun 		ifname = if_indextoname(sdl->sdl_index, ifix_buf);
    661  1.14  itojun 		if (!ifname)
    662  1.14  itojun 			ifname = "?";
    663  1.14  itojun 		ifwidth = strlen(ifname);
    664  1.13  itojun 		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
    665  1.13  itojun 			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
    666  1.13  itojun 
    667  1.13  itojun 		printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
    668  1.14  itojun 		    llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
    669   1.1  itojun 
    670   1.1  itojun 		/* Print neighbor discovery specific informations */
    671   1.4  itojun 		nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
    672   1.1  itojun 		if (nbi) {
    673   1.1  itojun 			if (nbi->expire > time.tv_sec) {
    674   1.1  itojun 				printf(" %-9.9s",
    675   1.1  itojun 				       sec2str(nbi->expire - time.tv_sec));
    676  1.11  itojun 			} else if (nbi->expire == 0)
    677   1.1  itojun 				printf(" %-9.9s", "permanent");
    678   1.1  itojun 			else
    679   1.1  itojun 				printf(" %-9.9s", "expired");
    680   1.1  itojun 
    681   1.1  itojun 			switch(nbi->state) {
    682   1.1  itojun 			 case ND6_LLINFO_NOSTATE:
    683   1.1  itojun 				 printf(" N");
    684   1.1  itojun 				 break;
    685  1.11  itojun #ifdef ND6_LLINFO_WAITDELETE
    686   1.1  itojun 			 case ND6_LLINFO_WAITDELETE:
    687   1.1  itojun 				 printf(" W");
    688   1.1  itojun 				 break;
    689  1.11  itojun #endif
    690   1.1  itojun 			 case ND6_LLINFO_INCOMPLETE:
    691   1.1  itojun 				 printf(" I");
    692   1.1  itojun 				 break;
    693   1.1  itojun 			 case ND6_LLINFO_REACHABLE:
    694   1.1  itojun 				 printf(" R");
    695   1.1  itojun 				 break;
    696   1.1  itojun 			 case ND6_LLINFO_STALE:
    697   1.1  itojun 				 printf(" S");
    698   1.1  itojun 				 break;
    699   1.1  itojun 			 case ND6_LLINFO_DELAY:
    700   1.1  itojun 				 printf(" D");
    701   1.1  itojun 				 break;
    702   1.1  itojun 			 case ND6_LLINFO_PROBE:
    703   1.1  itojun 				 printf(" P");
    704   1.1  itojun 				 break;
    705   1.1  itojun 			 default:
    706   1.1  itojun 				 printf(" ?");
    707   1.1  itojun 				 break;
    708   1.1  itojun 			}
    709   1.1  itojun 
    710   1.1  itojun 			isrouter = nbi->isrouter;
    711   1.1  itojun 			prbs = nbi->asked;
    712  1.11  itojun 		} else {
    713   1.1  itojun 			warnx("failed to get neighbor information");
    714   1.1  itojun 			printf("  ");
    715   1.1  itojun 		}
    716   1.1  itojun 		putchar(' ');
    717   1.1  itojun 
    718   1.4  itojun 		/*
    719   1.4  itojun 		 * other flags. R: router, P: proxy, W: ??
    720   1.4  itojun 		 */
    721   1.4  itojun 		if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
    722   1.6  itojun 			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
    723   1.6  itojun 				isrouter ? "R" : "",
    724   1.6  itojun 				(rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
    725   1.4  itojun 		} else {
    726   1.4  itojun 			sin = (struct sockaddr_in6 *)
    727   1.4  itojun 				(sdl->sdl_len + (char *)sdl);
    728   1.6  itojun 			snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
    729   1.4  itojun 				isrouter ? "R" : "",
    730   1.4  itojun 				!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr)
    731   1.4  itojun 					? "P" : "",
    732   1.4  itojun 				(sin->sin6_len != sizeof(struct sockaddr_in6))
    733   1.6  itojun 					? "W" : "",
    734   1.6  itojun 				(rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
    735   1.1  itojun 		}
    736   1.4  itojun 		printf(" %-4.4s", flgbuf);
    737   1.1  itojun 
    738   1.1  itojun 		if (prbs)
    739   1.4  itojun 			printf(" %4d", prbs);
    740   1.1  itojun 
    741   1.1  itojun 		printf("\n");
    742   1.1  itojun 	}
    743  1.12  itojun 	if (buf != NULL)
    744  1.12  itojun 		free(buf);
    745   1.1  itojun 
    746   1.1  itojun 	if (repeat) {
    747   1.1  itojun 		printf("\n");
    748   1.1  itojun 		sleep(repeat);
    749   1.1  itojun 		goto again;
    750   1.1  itojun 	}
    751   1.1  itojun }
    752   1.1  itojun 
    753   1.1  itojun static struct in6_nbrinfo *
    754   1.4  itojun getnbrinfo(addr, ifindex, warning)
    755   1.1  itojun 	struct in6_addr *addr;
    756   1.1  itojun 	int ifindex;
    757   1.4  itojun 	int warning;
    758   1.1  itojun {
    759   1.1  itojun 	static struct in6_nbrinfo nbi;
    760   1.1  itojun 	int s;
    761   1.1  itojun 
    762   1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    763   1.1  itojun 		err(1, "socket");
    764   1.1  itojun 
    765   1.1  itojun 	bzero(&nbi, sizeof(nbi));
    766   1.1  itojun 	if_indextoname(ifindex, nbi.ifname);
    767   1.1  itojun 	nbi.addr = *addr;
    768   1.1  itojun 	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
    769   1.4  itojun 		if (warning)
    770   1.8  itojun 			warn("ioctl(SIOCGNBRINFO_IN6)");
    771   1.1  itojun 		close(s);
    772   1.1  itojun 		return(NULL);
    773   1.1  itojun 	}
    774   1.1  itojun 
    775   1.1  itojun 	close(s);
    776   1.1  itojun 	return(&nbi);
    777   1.1  itojun }
    778   1.1  itojun 
    779   1.1  itojun static char *
    780   1.1  itojun ether_str(sdl)
    781   1.1  itojun 	struct sockaddr_dl *sdl;
    782   1.1  itojun {
    783   1.1  itojun 	static char ebuf[32];
    784   1.1  itojun 	u_char *cp;
    785   1.1  itojun 
    786   1.1  itojun 	if (sdl->sdl_alen) {
    787   1.1  itojun 		cp = (u_char *)LLADDR(sdl);
    788   1.1  itojun 		sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
    789   1.1  itojun 			cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
    790  1.11  itojun 	} else {
    791   1.1  itojun 		sprintf(ebuf, "(incomplete)");
    792   1.1  itojun 	}
    793   1.1  itojun 
    794   1.1  itojun 	return(ebuf);
    795   1.1  itojun }
    796   1.1  itojun 
    797   1.1  itojun int
    798   1.1  itojun ndp_ether_aton(a, n)
    799   1.1  itojun 	char *a;
    800   1.1  itojun 	u_char *n;
    801   1.1  itojun {
    802   1.1  itojun 	int i, o[6];
    803   1.1  itojun 
    804   1.1  itojun 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
    805   1.1  itojun 					   &o[3], &o[4], &o[5]);
    806   1.1  itojun 	if (i != 6) {
    807   1.1  itojun 		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
    808   1.1  itojun 		return (1);
    809   1.1  itojun 	}
    810   1.1  itojun 	for (i=0; i<6; i++)
    811   1.1  itojun 		n[i] = o[i];
    812   1.1  itojun 	return (0);
    813   1.1  itojun }
    814   1.1  itojun 
    815   1.1  itojun void
    816   1.1  itojun usage()
    817   1.1  itojun {
    818   1.1  itojun 	printf("usage: ndp hostname\n");
    819  1.13  itojun 	printf("       ndp -a[nt]\n");
    820  1.13  itojun 	printf("       ndp [-nt] -A wait\n");
    821   1.1  itojun 	printf("       ndp -c[nt]\n");
    822   1.1  itojun 	printf("       ndp -d[nt] hostname\n");
    823   1.1  itojun 	printf("       ndp -f[nt] filename\n");
    824   1.8  itojun 	printf("       ndp -i interface [flags...]\n");
    825   1.4  itojun #ifdef SIOCSDEFIFACE_IN6
    826   1.4  itojun 	printf("       ndp -I [interface|delete]\n");
    827   1.4  itojun #endif
    828   1.1  itojun 	printf("       ndp -p\n");
    829   1.1  itojun 	printf("       ndp -r\n");
    830   1.6  itojun 	printf("       ndp -s hostname ether_addr [temp] [proxy]\n");
    831   1.1  itojun 	printf("       ndp -H\n");
    832   1.1  itojun 	printf("       ndp -P\n");
    833   1.1  itojun 	printf("       ndp -R\n");
    834   1.1  itojun 	exit(1);
    835   1.1  itojun }
    836   1.1  itojun 
    837   1.1  itojun int
    838   1.1  itojun rtmsg(cmd)
    839   1.1  itojun 	int cmd;
    840   1.1  itojun {
    841   1.1  itojun 	static int seq;
    842   1.1  itojun 	int rlen;
    843   1.1  itojun 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
    844   1.1  itojun 	register char *cp = m_rtmsg.m_space;
    845   1.1  itojun 	register int l;
    846   1.1  itojun 
    847   1.1  itojun 	errno = 0;
    848   1.1  itojun 	if (cmd == RTM_DELETE)
    849   1.1  itojun 		goto doit;
    850   1.1  itojun 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
    851   1.1  itojun 	rtm->rtm_flags = flags;
    852   1.1  itojun 	rtm->rtm_version = RTM_VERSION;
    853   1.1  itojun 
    854   1.1  itojun 	switch (cmd) {
    855   1.1  itojun 	default:
    856   1.1  itojun 		fprintf(stderr, "ndp: internal wrong cmd\n");
    857   1.1  itojun 		exit(1);
    858   1.1  itojun 	case RTM_ADD:
    859   1.1  itojun 		rtm->rtm_addrs |= RTA_GATEWAY;
    860   1.1  itojun 		rtm->rtm_rmx.rmx_expire = expire_time;
    861   1.1  itojun 		rtm->rtm_inits = RTV_EXPIRE;
    862   1.1  itojun 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    863   1.6  itojun 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
    864   1.6  itojun 			rtm->rtm_flags &= ~RTF_HOST;
    865   1.6  itojun 			rtm->rtm_flags |= RTA_NETMASK;
    866   1.6  itojun 		}
    867   1.1  itojun 		/* FALLTHROUGH */
    868   1.1  itojun 	case RTM_GET:
    869   1.1  itojun 		rtm->rtm_addrs |= RTA_DST;
    870   1.1  itojun 	}
    871   1.1  itojun #define NEXTADDR(w, s) \
    872   1.1  itojun 	if (rtm->rtm_addrs & (w)) { \
    873   1.1  itojun 		bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
    874   1.1  itojun 
    875   1.1  itojun 	NEXTADDR(RTA_DST, sin_m);
    876   1.1  itojun 	NEXTADDR(RTA_GATEWAY, sdl_m);
    877   1.6  itojun 	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
    878   1.1  itojun 	NEXTADDR(RTA_NETMASK, so_mask);
    879   1.1  itojun 
    880   1.1  itojun 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    881   1.1  itojun doit:
    882   1.1  itojun 	l = rtm->rtm_msglen;
    883   1.1  itojun 	rtm->rtm_seq = ++seq;
    884   1.1  itojun 	rtm->rtm_type = cmd;
    885   1.1  itojun 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    886   1.1  itojun 		if (errno != ESRCH || cmd != RTM_DELETE) {
    887  1.16  itojun 			err(1, "writing to routing socket");
    888  1.16  itojun 			/* NOTREACHED */
    889   1.1  itojun 		}
    890   1.1  itojun 	}
    891   1.1  itojun 	do {
    892   1.1  itojun 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    893   1.1  itojun 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    894   1.1  itojun 	if (l < 0)
    895   1.1  itojun 		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
    896   1.1  itojun 		    strerror(errno));
    897   1.1  itojun 	return (0);
    898   1.1  itojun }
    899   1.1  itojun 
    900   1.1  itojun void
    901   1.8  itojun ifinfo(argc, argv)
    902   1.8  itojun 	int argc;
    903   1.8  itojun 	char **argv;
    904   1.1  itojun {
    905   1.1  itojun 	struct in6_ndireq nd;
    906   1.8  itojun 	int i, s;
    907   1.8  itojun 	char *ifname = argv[0];
    908   1.8  itojun 	u_int32_t newflags;
    909  1.13  itojun #ifdef IPV6CTL_USETEMPADDR
    910  1.13  itojun 	u_int8_t nullbuf[8];
    911  1.13  itojun #endif
    912   1.1  itojun 
    913   1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    914  1.16  itojun 		err(1, "socket");
    915  1.16  itojun 		/* NOTREACHED */
    916   1.1  itojun 	}
    917   1.1  itojun 	bzero(&nd, sizeof(nd));
    918   1.1  itojun 	strcpy(nd.ifname, ifname);
    919   1.1  itojun 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
    920  1.16  itojun  		err(1, "ioctl(SIOCGIFINFO_IN6)");
    921  1.16  itojun 		/* NOTREACHED */
    922   1.1  itojun  	}
    923   1.1  itojun #define ND nd.ndi
    924   1.8  itojun 	newflags = ND.flags;
    925   1.8  itojun 	for (i = 1; i < argc; i++) {
    926   1.8  itojun 		int clear = 0;
    927   1.8  itojun 		char *cp = argv[i];
    928   1.8  itojun 
    929   1.8  itojun 		if (*cp == '-') {
    930   1.8  itojun 			clear = 1;
    931   1.8  itojun 			cp++;
    932   1.8  itojun 		}
    933   1.8  itojun 
    934   1.8  itojun #define SETFLAG(s, f) \
    935   1.8  itojun 	do {\
    936   1.8  itojun 		if (strcmp(cp, (s)) == 0) {\
    937   1.8  itojun 			if (clear)\
    938   1.8  itojun 				newflags &= ~(f);\
    939   1.8  itojun 			else\
    940   1.8  itojun 				newflags |= (f);\
    941   1.8  itojun 		}\
    942   1.8  itojun 	} while (0)
    943   1.8  itojun 		SETFLAG("nud", ND6_IFF_PERFORMNUD);
    944   1.8  itojun 
    945   1.8  itojun 		ND.flags = newflags;
    946   1.8  itojun 		if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) {
    947  1.16  itojun 			err(1, "ioctl(SIOCSIFINFO_FLAGS)");
    948  1.16  itojun 			/* NOTREACHED */
    949   1.8  itojun 		}
    950   1.8  itojun #undef SETFLAG
    951   1.8  itojun 	}
    952   1.8  itojun 
    953  1.16  itojun 	if (!ND.linkmtu) {
    954  1.16  itojun 		errx(1, "%s: not initialized yet", ifname);
    955  1.16  itojun 		/* NOTREACHED */
    956  1.16  itojun 	}
    957  1.16  itojun 
    958   1.1  itojun 	printf("linkmtu=%d", ND.linkmtu);
    959   1.1  itojun 	printf(", curhlim=%d", ND.chlim);
    960   1.1  itojun 	printf(", basereachable=%ds%dms",
    961   1.1  itojun 	       ND.basereachable / 1000, ND.basereachable % 1000);
    962   1.1  itojun 	printf(", reachable=%ds", ND.reachable);
    963   1.8  itojun 	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
    964  1.13  itojun #ifdef IPV6CTL_USETEMPADDR
    965  1.13  itojun 	memset(nullbuf, 0, sizeof(nullbuf));
    966  1.13  itojun 	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
    967  1.13  itojun 		int j;
    968  1.13  itojun 		u_int8_t *rbuf;
    969  1.13  itojun 
    970  1.13  itojun 		for (i = 0; i < 3; i++) {
    971  1.13  itojun 			switch(i) {
    972  1.13  itojun 			case 0:
    973  1.13  itojun 				printf("\nRandom seed(0): ");
    974  1.13  itojun 				rbuf = ND.randomseed0;
    975  1.13  itojun 				break;
    976  1.13  itojun 			case 1:
    977  1.13  itojun 				printf("\nRandom seed(1): ");
    978  1.13  itojun 				rbuf = ND.randomseed1;
    979  1.13  itojun 				break;
    980  1.13  itojun 			case 2:
    981  1.13  itojun 				printf("\nRandom ID:      ");
    982  1.13  itojun 				rbuf = ND.randomid;
    983  1.13  itojun 				break;
    984  1.13  itojun 			}
    985  1.13  itojun 			for (j = 0; j < 8; j++)
    986  1.13  itojun 				printf("%02x", rbuf[j]);
    987  1.13  itojun 		}
    988  1.13  itojun 	}
    989  1.13  itojun #endif
    990   1.8  itojun 	if (ND.flags) {
    991   1.8  itojun 		printf("\nFlags: ");
    992   1.8  itojun 		if ((ND.flags & ND6_IFF_PERFORMNUD) != 0)
    993   1.8  itojun 			printf("PERFORMNUD ");
    994   1.8  itojun 	}
    995   1.8  itojun 	putc('\n', stdout);
    996   1.1  itojun #undef ND
    997   1.8  itojun 
    998   1.1  itojun 	close(s);
    999   1.1  itojun }
   1000   1.1  itojun 
   1001  1.16  itojun #ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
   1002  1.16  itojun #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
   1003  1.16  itojun #endif
   1004  1.16  itojun 
   1005   1.1  itojun void
   1006   1.1  itojun rtrlist()
   1007   1.1  itojun {
   1008  1.16  itojun #ifdef ICMPV6CTL_ND6_DRLIST
   1009  1.16  itojun 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
   1010  1.16  itojun 	char *buf;
   1011  1.16  itojun 	struct in6_defrouter *p, *ep;
   1012  1.16  itojun 	size_t l;
   1013  1.16  itojun 	struct timeval time;
   1014  1.16  itojun 
   1015  1.16  itojun 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
   1016  1.16  itojun 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
   1017  1.16  itojun 		/*NOTREACHED*/
   1018  1.16  itojun 	}
   1019  1.16  itojun 	buf = malloc(l);
   1020  1.16  itojun 	if (!buf) {
   1021  1.16  itojun 		errx(1, "not enough core");
   1022  1.16  itojun 		/*NOTREACHED*/
   1023  1.16  itojun 	}
   1024  1.16  itojun 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
   1025  1.16  itojun 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
   1026  1.16  itojun 		/*NOTREACHED*/
   1027  1.16  itojun 	}
   1028  1.16  itojun 
   1029  1.16  itojun 	ep = (struct in6_defrouter *)(buf + l);
   1030  1.16  itojun 	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
   1031  1.16  itojun 		int rtpref;
   1032  1.16  itojun 
   1033  1.16  itojun 		if (getnameinfo((struct sockaddr *)&p->rtaddr,
   1034  1.16  itojun 		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
   1035  1.16  itojun 		    NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)) != 0)
   1036  1.16  itojun 			strlcpy(host_buf, "?", sizeof(host_buf));
   1037  1.16  itojun 
   1038  1.16  itojun 		printf("%s if=%s", host_buf,
   1039  1.16  itojun 		       if_indextoname(p->if_index, ifix_buf));
   1040  1.16  itojun 		printf(", flags=%s%s",
   1041  1.16  itojun 		       p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
   1042  1.16  itojun 		       p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
   1043  1.16  itojun 		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
   1044  1.16  itojun 		printf(", pref=%s", rtpref_str[rtpref]);
   1045  1.16  itojun 
   1046  1.16  itojun 		gettimeofday(&time, 0);
   1047  1.16  itojun 		if (p->expire == 0)
   1048  1.16  itojun 			printf(", expire=Never\n");
   1049  1.16  itojun 		else
   1050  1.16  itojun 			printf(", expire=%s\n",
   1051  1.16  itojun 				sec2str(p->expire - time.tv_sec));
   1052  1.16  itojun 	}
   1053  1.16  itojun 	free(buf);
   1054  1.16  itojun #else
   1055   1.1  itojun 	struct in6_drlist dr;
   1056   1.1  itojun 	int s, i;
   1057   1.1  itojun 	struct timeval time;
   1058   1.1  itojun 
   1059   1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
   1060  1.16  itojun 		err(1, "socket");
   1061  1.16  itojun 		/* NOTREACHED */
   1062   1.1  itojun 	}
   1063   1.1  itojun 	bzero(&dr, sizeof(dr));
   1064   1.1  itojun 	strcpy(dr.ifname, "lo0"); /* dummy */
   1065   1.1  itojun 	if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
   1066  1.16  itojun  		err(1, "ioctl(SIOCGDRLST_IN6)");
   1067  1.16  itojun 		/* NOTREACHED */
   1068   1.1  itojun  	}
   1069   1.1  itojun #define DR dr.defrouter[i]
   1070  1.16  itojun 	for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) {
   1071   1.4  itojun 		struct sockaddr_in6 sin6;
   1072   1.4  itojun 
   1073   1.4  itojun 		bzero(&sin6, sizeof(sin6));
   1074   1.4  itojun 		sin6.sin6_family = AF_INET6;
   1075   1.4  itojun 		sin6.sin6_len = sizeof(sin6);
   1076   1.4  itojun 		sin6.sin6_addr = DR.rtaddr;
   1077   1.4  itojun 		getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf,
   1078   1.4  itojun 			    sizeof(host_buf), NULL, 0,
   1079   1.4  itojun 			    NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
   1080   1.4  itojun 
   1081   1.4  itojun 		printf("%s if=%s", host_buf,
   1082   1.1  itojun 		       if_indextoname(DR.if_index, ifix_buf));
   1083   1.1  itojun 		printf(", flags=%s%s",
   1084   1.1  itojun 		       DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
   1085   1.1  itojun 		       DR.flags & ND_RA_FLAG_OTHER   ? "O" : "");
   1086   1.1  itojun 		gettimeofday(&time, 0);
   1087   1.1  itojun 		if (DR.expire == 0)
   1088   1.1  itojun 			printf(", expire=Never\n");
   1089   1.1  itojun 		else
   1090   1.1  itojun 			printf(", expire=%s\n",
   1091   1.1  itojun 				sec2str(DR.expire - time.tv_sec));
   1092   1.1  itojun 	}
   1093   1.1  itojun #undef DR
   1094   1.1  itojun 	close(s);
   1095  1.16  itojun #endif
   1096   1.1  itojun }
   1097   1.1  itojun 
   1098   1.1  itojun void
   1099   1.1  itojun plist()
   1100   1.1  itojun {
   1101  1.16  itojun #ifdef ICMPV6CTL_ND6_PRLIST
   1102  1.16  itojun 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
   1103  1.16  itojun 	char *buf;
   1104  1.16  itojun 	struct in6_prefix *p, *ep, *n;
   1105  1.16  itojun 	struct sockaddr_in6 *advrtr;
   1106  1.16  itojun 	size_t l;
   1107  1.16  itojun 	struct timeval time;
   1108  1.16  itojun #ifdef NI_WITHSCOPEID
   1109  1.16  itojun 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
   1110  1.16  itojun 	int ninflags = (nflag ? NI_NUMERICHOST : 0) | NI_WITHSCOPEID;
   1111  1.16  itojun #else
   1112  1.16  itojun 	const int niflags = NI_NUMERICHOST;
   1113  1.16  itojun 	int ninflags = nflag ? NI_NUMERICHOST : 0;
   1114  1.16  itojun #endif
   1115  1.16  itojun 	char namebuf[NI_MAXHOST];
   1116  1.16  itojun 
   1117  1.16  itojun 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
   1118  1.16  itojun 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
   1119  1.16  itojun 		/*NOTREACHED*/
   1120  1.16  itojun 	}
   1121  1.16  itojun 	buf = malloc(l);
   1122  1.16  itojun 	if (!buf) {
   1123  1.16  itojun 		errx(1, "not enough core");
   1124  1.16  itojun 		/*NOTREACHED*/
   1125  1.16  itojun 	}
   1126  1.16  itojun 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
   1127  1.16  itojun 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
   1128  1.16  itojun 		/*NOTREACHED*/
   1129  1.16  itojun 	}
   1130  1.16  itojun 
   1131  1.16  itojun 	ep = (struct in6_prefix *)(buf + l);
   1132  1.16  itojun 	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
   1133  1.16  itojun 		advrtr = (struct sockaddr_in6 *)(p + 1);
   1134  1.16  itojun 		n = (struct in6_prefix *)&advrtr[p->advrtrs];
   1135  1.16  itojun 
   1136  1.16  itojun 		if (getnameinfo((struct sockaddr *)&p->prefix,
   1137  1.16  itojun 		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
   1138  1.16  itojun 		    NULL, 0, niflags) != 0)
   1139  1.16  itojun 			strlcpy(namebuf, "?", sizeof(namebuf));
   1140  1.16  itojun 		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
   1141  1.16  itojun 		       if_indextoname(p->if_index, ifix_buf));
   1142  1.16  itojun 
   1143  1.16  itojun 		gettimeofday(&time, 0);
   1144  1.16  itojun 		/*
   1145  1.16  itojun 		 * meaning of fields, especially flags, is very different
   1146  1.16  itojun 		 * by origin.  notify the difference to the users.
   1147  1.16  itojun 		 */
   1148  1.16  itojun 		printf("flags=%s%s%s%s%s",
   1149  1.16  itojun 		       p->raflags.onlink ? "L" : "",
   1150  1.16  itojun 		       p->raflags.autonomous ? "A" : "",
   1151  1.16  itojun 		       (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
   1152  1.16  itojun 		       (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
   1153  1.16  itojun #ifdef NDPRF_HOME
   1154  1.16  itojun 		       (p->flags & NDPRF_HOME) != 0 ? "H" : ""
   1155  1.16  itojun #else
   1156  1.16  itojun 		       ""
   1157  1.16  itojun #endif
   1158  1.16  itojun 		       );
   1159  1.16  itojun 		if (p->vltime == ND6_INFINITE_LIFETIME)
   1160  1.16  itojun 			printf(" vltime=infinity");
   1161  1.16  itojun 		else
   1162  1.16  itojun 			printf(" vltime=%ld", (long)p->vltime);
   1163  1.16  itojun 		if (p->pltime == ND6_INFINITE_LIFETIME)
   1164  1.16  itojun 			printf(", pltime=infinity");
   1165  1.16  itojun 		else
   1166  1.16  itojun 			printf(", pltime=%ld", (long)p->pltime);
   1167  1.16  itojun 		if (p->expire == 0)
   1168  1.16  itojun 			printf(", expire=Never");
   1169  1.16  itojun 		else if (p->expire >= time.tv_sec)
   1170  1.16  itojun 			printf(", expire=%s",
   1171  1.16  itojun 				sec2str(p->expire - time.tv_sec));
   1172  1.16  itojun 		else
   1173  1.16  itojun 			printf(", expired");
   1174  1.16  itojun 		printf(", ref=%d", p->refcnt);
   1175  1.16  itojun 		printf("\n");
   1176  1.16  itojun 		/*
   1177  1.16  itojun 		 * "advertising router" list is meaningful only if the prefix
   1178  1.16  itojun 		 * information is from RA.
   1179  1.16  itojun 		 */
   1180  1.16  itojun 		if (p->advrtrs) {
   1181  1.16  itojun 			int j;
   1182  1.16  itojun 			struct sockaddr_in6 *sin6;
   1183  1.16  itojun 
   1184  1.16  itojun 			sin6 = (struct sockaddr_in6 *)(p + 1);
   1185  1.16  itojun 			printf("  advertised by\n");
   1186  1.16  itojun 			for (j = 0; j < p->advrtrs; j++) {
   1187  1.16  itojun 				struct in6_nbrinfo *nbi;
   1188  1.16  itojun 
   1189  1.16  itojun 				if (getnameinfo((struct sockaddr *)sin6,
   1190  1.16  itojun 				    sin6->sin6_len, namebuf, sizeof(namebuf),
   1191  1.16  itojun 				    NULL, 0, ninflags) != 0)
   1192  1.16  itojun 					strlcpy(namebuf, "?", sizeof(namebuf));
   1193  1.16  itojun 				printf("    %s", namebuf);
   1194  1.16  itojun 
   1195  1.16  itojun 				nbi = getnbrinfo(&sin6->sin6_addr, p->if_index,
   1196  1.16  itojun 						 0);
   1197  1.16  itojun 				if (nbi) {
   1198  1.16  itojun 					switch(nbi->state) {
   1199  1.16  itojun 					case ND6_LLINFO_REACHABLE:
   1200  1.16  itojun 					case ND6_LLINFO_STALE:
   1201  1.16  itojun 					case ND6_LLINFO_DELAY:
   1202  1.16  itojun 					case ND6_LLINFO_PROBE:
   1203  1.16  itojun 						printf(" (reachable)\n");
   1204  1.16  itojun 						break;
   1205  1.16  itojun 					default:
   1206  1.16  itojun 						printf(" (unreachable)\n");
   1207  1.16  itojun 					}
   1208  1.16  itojun 				} else
   1209  1.16  itojun 					printf(" (no neighbor state)\n");
   1210  1.16  itojun 				sin6++;
   1211  1.16  itojun 			}
   1212  1.16  itojun 		} else
   1213  1.16  itojun 			printf("  No advertising router\n");
   1214  1.16  itojun 	}
   1215  1.16  itojun 	free(buf);
   1216  1.16  itojun #else
   1217   1.1  itojun 	struct in6_prlist pr;
   1218   1.1  itojun 	int s, i;
   1219   1.1  itojun 	struct timeval time;
   1220   1.1  itojun 
   1221   1.1  itojun 	gettimeofday(&time, 0);
   1222   1.1  itojun 
   1223   1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
   1224  1.16  itojun 		err(1, "socket");
   1225  1.16  itojun 		/* NOTREACHED */
   1226   1.1  itojun 	}
   1227   1.1  itojun 	bzero(&pr, sizeof(pr));
   1228   1.1  itojun 	strcpy(pr.ifname, "lo0"); /* dummy */
   1229   1.1  itojun 	if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
   1230  1.16  itojun  		err(1, "ioctl(SIOCGPRLST_IN6)");
   1231  1.16  itojun 		/* NOTREACHED */
   1232   1.1  itojun  	}
   1233   1.1  itojun #define PR pr.prefix[i]
   1234   1.1  itojun 	for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
   1235  1.13  itojun 		struct sockaddr_in6 p6;
   1236  1.13  itojun 		char namebuf[NI_MAXHOST];
   1237  1.13  itojun 		int niflags;
   1238  1.13  itojun 
   1239  1.13  itojun #ifdef NDPRF_ONLINK
   1240  1.13  itojun 		p6 = PR.prefix;
   1241  1.13  itojun #else
   1242  1.13  itojun 		memset(&p6, 0, sizeof(p6));
   1243  1.13  itojun 		p6.sin6_family = AF_INET6;
   1244  1.13  itojun 		p6.sin6_len = sizeof(p6);
   1245  1.13  itojun 		p6.sin6_addr = PR.prefix;
   1246  1.13  itojun #endif
   1247  1.13  itojun 
   1248  1.13  itojun 		/*
   1249  1.13  itojun 		 * copy link index to sin6_scope_id field.
   1250  1.13  itojun 		 * XXX: KAME specific.
   1251  1.13  itojun 		 */
   1252  1.13  itojun 		if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) {
   1253  1.13  itojun 			u_int16_t linkid;
   1254  1.13  itojun 
   1255  1.13  itojun 			memcpy(&linkid, &p6.sin6_addr.s6_addr[2],
   1256  1.13  itojun 			       sizeof(linkid));
   1257  1.13  itojun 			linkid = ntohs(linkid);
   1258  1.13  itojun 			p6.sin6_scope_id = linkid;
   1259  1.13  itojun 			p6.sin6_addr.s6_addr[2] = 0;
   1260  1.13  itojun 			p6.sin6_addr.s6_addr[3] = 0;
   1261  1.13  itojun 		}
   1262  1.13  itojun 
   1263  1.13  itojun 		niflags = NI_NUMERICHOST;
   1264  1.13  itojun #ifdef __KAME__
   1265  1.13  itojun 		niflags |= NI_WITHSCOPEID;
   1266  1.13  itojun #endif
   1267  1.13  itojun 		if (getnameinfo((struct sockaddr *)&p6,
   1268  1.13  itojun 				sizeof(p6), namebuf, sizeof(namebuf),
   1269  1.13  itojun 				NULL, 0, niflags)) {
   1270  1.13  itojun 			warnx("getnameinfo failed");
   1271  1.13  itojun 			continue;
   1272  1.13  itojun 		}
   1273  1.13  itojun 		printf("%s/%d if=%s\n", namebuf, PR.prefixlen,
   1274   1.1  itojun 		       if_indextoname(PR.if_index, ifix_buf));
   1275  1.13  itojun 
   1276   1.1  itojun 		gettimeofday(&time, 0);
   1277   1.6  itojun 		/*
   1278   1.6  itojun 		 * meaning of fields, especially flags, is very different
   1279   1.6  itojun 		 * by origin.  notify the difference to the users.
   1280   1.6  itojun 		 */
   1281  1.16  itojun #if 0
   1282  1.16  itojun 		printf("  %s",
   1283  1.16  itojun 		       PR.origin == PR_ORIG_RA ? "" : "advertise: ");
   1284  1.16  itojun #endif
   1285  1.13  itojun #ifdef NDPRF_ONLINK
   1286  1.16  itojun 		printf("flags=%s%s%s%s%s",
   1287  1.13  itojun 		       PR.raflags.onlink ? "L" : "",
   1288  1.13  itojun 		       PR.raflags.autonomous ? "A" : "",
   1289  1.13  itojun 		       (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "",
   1290  1.16  itojun 		       (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "",
   1291  1.16  itojun #ifdef NDPRF_HOME
   1292  1.16  itojun 		       (PR.flags & NDPRF_HOME) != 0 ? "H" : ""
   1293  1.16  itojun #else
   1294  1.16  itojun 		       ""
   1295  1.16  itojun #endif
   1296  1.16  itojun 		       );
   1297  1.13  itojun #else
   1298   1.6  itojun 		printf("flags=%s%s",
   1299   1.1  itojun 		       PR.raflags.onlink ? "L" : "",
   1300   1.1  itojun 		       PR.raflags.autonomous ? "A" : "");
   1301  1.13  itojun #endif
   1302   1.1  itojun 		if (PR.vltime == ND6_INFINITE_LIFETIME)
   1303   1.1  itojun 			printf(" vltime=infinity");
   1304   1.1  itojun 		else
   1305   1.1  itojun 			printf(" vltime=%ld", (long)PR.vltime);
   1306   1.1  itojun 		if (PR.pltime == ND6_INFINITE_LIFETIME)
   1307   1.1  itojun 			printf(", pltime=infinity");
   1308   1.1  itojun 		else
   1309   1.1  itojun 			printf(", pltime=%ld", (long)PR.pltime);
   1310   1.1  itojun 		if (PR.expire == 0)
   1311   1.6  itojun 			printf(", expire=Never");
   1312   1.1  itojun 		else if (PR.expire >= time.tv_sec)
   1313   1.6  itojun 			printf(", expire=%s",
   1314   1.1  itojun 				sec2str(PR.expire - time.tv_sec));
   1315   1.1  itojun 		else
   1316   1.6  itojun 			printf(", expired");
   1317  1.13  itojun #ifdef NDPRF_ONLINK
   1318  1.13  itojun 		printf(", ref=%d", PR.refcnt);
   1319  1.13  itojun #endif
   1320  1.16  itojun #if 0
   1321   1.6  itojun 		switch (PR.origin) {
   1322   1.6  itojun 		case PR_ORIG_RA:
   1323   1.6  itojun 			printf(", origin=RA");
   1324   1.6  itojun 			break;
   1325   1.6  itojun 		case PR_ORIG_RR:
   1326   1.6  itojun 			printf(", origin=RR");
   1327   1.6  itojun 			break;
   1328   1.6  itojun 		case PR_ORIG_STATIC:
   1329   1.6  itojun 			printf(", origin=static");
   1330   1.6  itojun 			break;
   1331   1.6  itojun 		case PR_ORIG_KERNEL:
   1332   1.6  itojun 			printf(", origin=kernel");
   1333   1.6  itojun 			break;
   1334   1.6  itojun 		default:
   1335   1.6  itojun 			printf(", origin=?");
   1336   1.6  itojun 			break;
   1337   1.6  itojun 		}
   1338  1.16  itojun #endif
   1339   1.6  itojun 		printf("\n");
   1340   1.6  itojun 		/*
   1341   1.6  itojun 		 * "advertising router" list is meaningful only if the prefix
   1342   1.6  itojun 		 * information is from RA.
   1343   1.6  itojun 		 */
   1344  1.16  itojun 		if (0 &&	/* prefix origin is almost obsolted */
   1345  1.16  itojun 		    PR.origin != PR_ORIG_RA)
   1346   1.6  itojun 			;
   1347   1.6  itojun 		else if (PR.advrtrs) {
   1348   1.1  itojun 			int j;
   1349   1.1  itojun 			printf("  advertised by\n");
   1350   1.1  itojun 			for (j = 0; j < PR.advrtrs; j++) {
   1351   1.4  itojun 				struct sockaddr_in6 sin6;
   1352   1.4  itojun 				struct in6_nbrinfo *nbi;
   1353   1.4  itojun 
   1354   1.4  itojun 				bzero(&sin6, sizeof(sin6));
   1355   1.4  itojun 				sin6.sin6_family = AF_INET6;
   1356   1.4  itojun 				sin6.sin6_len = sizeof(sin6);
   1357   1.4  itojun 				sin6.sin6_addr = PR.advrtr[j];
   1358   1.4  itojun 				sin6.sin6_scope_id = PR.if_index; /* XXX */
   1359   1.4  itojun 				getnameinfo((struct sockaddr *)&sin6,
   1360   1.4  itojun 					    sin6.sin6_len, host_buf,
   1361   1.4  itojun 					    sizeof(host_buf), NULL, 0,
   1362   1.4  itojun 					    NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0));
   1363   1.4  itojun 				printf("    %s", host_buf);
   1364   1.4  itojun 
   1365   1.4  itojun 				nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index,
   1366   1.4  itojun 						 0);
   1367   1.4  itojun 				if (nbi) {
   1368   1.4  itojun 					switch(nbi->state) {
   1369   1.4  itojun 					 case ND6_LLINFO_REACHABLE:
   1370   1.4  itojun 					 case ND6_LLINFO_STALE:
   1371   1.4  itojun 					 case ND6_LLINFO_DELAY:
   1372   1.4  itojun 					 case ND6_LLINFO_PROBE:
   1373   1.4  itojun 						 printf(" (reachable)\n");
   1374   1.4  itojun 						 break;
   1375   1.4  itojun 					 default:
   1376   1.9  itojun 						 printf(" (unreachable)\n");
   1377   1.4  itojun 					}
   1378  1.11  itojun 				} else
   1379   1.4  itojun 					printf(" (no neighbor state)\n");
   1380   1.1  itojun 			}
   1381   1.1  itojun 			if (PR.advrtrs > DRLSTSIZ)
   1382   1.1  itojun 				printf("    and %d routers\n",
   1383   1.1  itojun 				       PR.advrtrs - DRLSTSIZ);
   1384   1.6  itojun 		} else
   1385   1.1  itojun 			printf("  No advertising router\n");
   1386   1.1  itojun 	}
   1387   1.1  itojun #undef PR
   1388   1.1  itojun 	close(s);
   1389  1.16  itojun #endif
   1390   1.1  itojun }
   1391   1.1  itojun 
   1392   1.1  itojun void
   1393   1.1  itojun pfx_flush()
   1394   1.1  itojun {
   1395   1.1  itojun 	char dummyif[IFNAMSIZ+8];
   1396   1.1  itojun 	int s;
   1397   1.1  itojun 
   1398   1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
   1399   1.1  itojun 		err(1, "socket");
   1400   1.1  itojun 	strcpy(dummyif, "lo0"); /* dummy */
   1401   1.1  itojun 	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
   1402   1.1  itojun  		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
   1403   1.1  itojun }
   1404   1.1  itojun 
   1405   1.1  itojun void
   1406   1.1  itojun rtr_flush()
   1407   1.1  itojun {
   1408   1.1  itojun 	char dummyif[IFNAMSIZ+8];
   1409   1.1  itojun 	int s;
   1410   1.1  itojun 
   1411   1.1  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
   1412   1.1  itojun 		err(1, "socket");
   1413   1.1  itojun 	strcpy(dummyif, "lo0"); /* dummy */
   1414   1.1  itojun 	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
   1415   1.1  itojun  		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
   1416   1.4  itojun 
   1417   1.4  itojun 	close(s);
   1418   1.1  itojun }
   1419   1.1  itojun 
   1420   1.1  itojun void
   1421   1.1  itojun harmonize_rtr()
   1422   1.1  itojun {
   1423   1.1  itojun 	char dummyif[IFNAMSIZ+8];
   1424   1.1  itojun 	int s;
   1425   1.1  itojun 
   1426   1.4  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
   1427   1.4  itojun 		err(1, "socket");
   1428   1.4  itojun 	strcpy(dummyif, "lo0"); /* dummy */
   1429   1.4  itojun 	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
   1430  1.16  itojun  		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
   1431   1.4  itojun 
   1432   1.4  itojun 	close(s);
   1433   1.4  itojun }
   1434   1.4  itojun 
   1435   1.4  itojun #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
   1436   1.4  itojun static void
   1437   1.4  itojun setdefif(ifname)
   1438   1.4  itojun 	char *ifname;
   1439   1.4  itojun {
   1440   1.4  itojun 	struct in6_ndifreq ndifreq;
   1441   1.4  itojun 	unsigned int ifindex;
   1442   1.4  itojun 
   1443   1.4  itojun 	if (strcasecmp(ifname, "delete") == 0)
   1444   1.4  itojun 		ifindex = 0;
   1445   1.4  itojun 	else {
   1446   1.4  itojun 		if ((ifindex = if_nametoindex(ifname)) == 0)
   1447   1.4  itojun 			err(1, "failed to resolve i/f index for %s", ifname);
   1448   1.4  itojun 	}
   1449   1.4  itojun 
   1450   1.9  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
   1451   1.4  itojun 		err(1, "socket");
   1452   1.4  itojun 
   1453   1.4  itojun 	strcpy(ndifreq.ifname, "lo0"); /* dummy */
   1454   1.4  itojun 	ndifreq.ifindex = ifindex;
   1455   1.4  itojun 
   1456   1.4  itojun 	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
   1457  1.16  itojun  		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
   1458   1.4  itojun 
   1459   1.4  itojun 	close(s);
   1460   1.4  itojun }
   1461   1.4  itojun 
   1462   1.4  itojun static void
   1463   1.4  itojun getdefif()
   1464   1.4  itojun {
   1465   1.4  itojun 	struct in6_ndifreq ndifreq;
   1466   1.4  itojun 	char ifname[IFNAMSIZ+8];
   1467   1.4  itojun 
   1468   1.9  itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
   1469   1.4  itojun 		err(1, "socket");
   1470   1.4  itojun 
   1471   1.4  itojun 	memset(&ndifreq, 0, sizeof(ndifreq));
   1472   1.4  itojun 	strcpy(ndifreq.ifname, "lo0"); /* dummy */
   1473   1.4  itojun 
   1474   1.4  itojun 	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
   1475  1.16  itojun  		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
   1476   1.4  itojun 
   1477   1.4  itojun 	if (ndifreq.ifindex == 0)
   1478   1.4  itojun 		printf("No default interface.\n");
   1479   1.4  itojun 	else {
   1480   1.4  itojun 		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
   1481   1.4  itojun 			err(1, "failed to resolve ifname for index %lu",
   1482   1.4  itojun 			    ndifreq.ifindex);
   1483   1.4  itojun 		printf("ND default interface = %s\n", ifname);
   1484   1.1  itojun 	}
   1485   1.4  itojun 
   1486   1.4  itojun 	close(s);
   1487   1.1  itojun }
   1488   1.4  itojun #endif
   1489   1.1  itojun 
   1490   1.1  itojun static char *
   1491   1.1  itojun sec2str(total)
   1492   1.1  itojun 	time_t total;
   1493   1.1  itojun {
   1494   1.1  itojun 	static char result[256];
   1495   1.1  itojun 	int days, hours, mins, secs;
   1496   1.1  itojun 	int first = 1;
   1497   1.1  itojun 	char *p = result;
   1498   1.1  itojun 
   1499   1.1  itojun 	days = total / 3600 / 24;
   1500   1.1  itojun 	hours = (total / 3600) % 24;
   1501   1.1  itojun 	mins = (total / 60) % 60;
   1502   1.1  itojun 	secs = total % 60;
   1503   1.1  itojun 
   1504   1.1  itojun 	if (days) {
   1505   1.1  itojun 		first = 0;
   1506   1.1  itojun 		p += sprintf(p, "%dd", days);
   1507   1.1  itojun 	}
   1508   1.1  itojun 	if (!first || hours) {
   1509   1.1  itojun 		first = 0;
   1510   1.1  itojun 		p += sprintf(p, "%dh", hours);
   1511   1.1  itojun 	}
   1512   1.1  itojun 	if (!first || mins) {
   1513   1.1  itojun 		first = 0;
   1514   1.1  itojun 		p += sprintf(p, "%dm", mins);
   1515   1.1  itojun 	}
   1516   1.1  itojun 	sprintf(p, "%ds", secs);
   1517   1.1  itojun 
   1518   1.1  itojun 	return(result);
   1519   1.1  itojun }
   1520   1.1  itojun 
   1521   1.1  itojun /*
   1522   1.1  itojun  * Print the timestamp
   1523   1.1  itojun  * from tcpdump/util.c
   1524   1.1  itojun  */
   1525   1.1  itojun static void
   1526   1.1  itojun ts_print(tvp)
   1527   1.1  itojun 	const struct timeval *tvp;
   1528   1.1  itojun {
   1529   1.1  itojun 	int s;
   1530   1.1  itojun 
   1531   1.1  itojun 	/* Default */
   1532   1.1  itojun 	s = (tvp->tv_sec + thiszone) % 86400;
   1533   1.1  itojun 	(void)printf("%02d:%02d:%02d.%06u ",
   1534   1.1  itojun 	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
   1535   1.1  itojun }
   1536