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