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