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