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