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