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