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