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