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