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