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