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