xref: /freebsd/usr.sbin/ndp/ndp.c (revision 8a0a413e)
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 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 	extern int h_errno;
569 	struct timeval now;
570 	u_long expire;
571 	int addrwidth;
572 	int llwidth;
573 	int ifwidth;
574 	char flgbuf[8];
575 	char *ifname;
576 
577 	/* Print header */
578 	if (!tflag && !cflag)
579 		printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
580 		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
581 		    W_IF, W_IF, "Netif", "Expire", "S", "Flags");
582 
583 again:;
584 	mib[0] = CTL_NET;
585 	mib[1] = PF_ROUTE;
586 	mib[2] = 0;
587 	mib[3] = AF_INET6;
588 	mib[4] = NET_RT_FLAGS;
589 #ifdef RTF_LLINFO
590 	mib[5] = RTF_LLINFO;
591 #else
592 	mib[5] = 0;
593 #endif
594 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
595 		err(1, "sysctl(PF_ROUTE estimate)");
596 	if (needed > 0) {
597 		if ((buf = malloc(needed)) == NULL)
598 			err(1, "malloc");
599 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
600 			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
601 		lim = buf + needed;
602 	} else
603 		buf = lim = NULL;
604 
605 	for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
606 		int isrouter = 0, prbs = 0;
607 
608 		rtm = (struct rt_msghdr *)next;
609 		sin = (struct sockaddr_in6 *)(rtm + 1);
610 		sdl = (struct sockaddr_dl *)((char *)sin +
611 		    ALIGN(sin->sin6_len));
612 
613 		/*
614 		 * Some OSes can produce a route that has the LINK flag but
615 		 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
616 		 * and BSD/OS, where xx is not the interface identifier on
617 		 * lo0).  Such routes entry would annoy getnbrinfo() below,
618 		 * so we skip them.
619 		 * XXX: such routes should have the GATEWAY flag, not the
620 		 * LINK flag.  However, there is rotten routing software
621 		 * that advertises all routes that have the GATEWAY flag.
622 		 * Thus, KAME kernel intentionally does not set the LINK flag.
623 		 * What is to be fixed is not ndp, but such routing software
624 		 * (and the kernel workaround)...
625 		 */
626 		if (sdl->sdl_family != AF_LINK)
627 			continue;
628 
629 		if (!(rtm->rtm_flags & RTF_HOST))
630 			continue;
631 
632 		if (addr) {
633 			if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
634 			    &sin->sin6_addr) == 0 ||
635 			    addr->sin6_scope_id != sin->sin6_scope_id)
636 				continue;
637 			found_entry = 1;
638 		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
639 			continue;
640 		if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
641 		    IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
642 			/* XXX: should scope id be filled in the kernel? */
643 			if (sin->sin6_scope_id == 0)
644 				sin->sin6_scope_id = sdl->sdl_index;
645 		}
646 		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
647 		    sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
648 		if (cflag) {
649 #ifdef RTF_WASCLONED
650 			if (rtm->rtm_flags & RTF_WASCLONED)
651 				delete(host_buf);
652 #elif defined(RTF_CLONED)
653 			if (rtm->rtm_flags & RTF_CLONED)
654 				delete(host_buf);
655 #else
656 			if (rtm->rtm_flags & RTF_PINNED)
657 				continue;
658 			delete(host_buf);
659 #endif
660 			continue;
661 		}
662 		gettimeofday(&now, 0);
663 		if (tflag)
664 			ts_print(&now);
665 
666 		addrwidth = strlen(host_buf);
667 		if (addrwidth < W_ADDR)
668 			addrwidth = W_ADDR;
669 		llwidth = strlen(ether_str(sdl));
670 		if (W_ADDR + W_LL - addrwidth > llwidth)
671 			llwidth = W_ADDR + W_LL - addrwidth;
672 		ifname = if_indextoname(sdl->sdl_index, ifix_buf);
673 		if (!ifname)
674 			ifname = "?";
675 		ifwidth = strlen(ifname);
676 		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
677 			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
678 
679 		printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
680 		    llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
681 
682 		/* Print neighbor discovery specific information */
683 		expire = rtm->rtm_rmx.rmx_expire;
684 		if (expire > now.tv_sec)
685 			printf(" %-9.9s", sec2str(expire - now.tv_sec));
686 		else if (expire == 0)
687 			printf(" %-9.9s", "permanent");
688 		else
689 			printf(" %-9.9s", "expired");
690 
691 		switch (rtm->rtm_rmx.rmx_state) {
692 		case ND6_LLINFO_NOSTATE:
693 			printf(" N");
694 			break;
695 #ifdef ND6_LLINFO_WAITDELETE
696 		case ND6_LLINFO_WAITDELETE:
697 			printf(" W");
698 			break;
699 #endif
700 		case ND6_LLINFO_INCOMPLETE:
701 			printf(" I");
702 			break;
703 		case ND6_LLINFO_REACHABLE:
704 			printf(" R");
705 			break;
706 		case ND6_LLINFO_STALE:
707 			printf(" S");
708 			break;
709 		case ND6_LLINFO_DELAY:
710 			printf(" D");
711 			break;
712 		case ND6_LLINFO_PROBE:
713 			printf(" P");
714 			break;
715 		default:
716 			printf(" ?");
717 			break;
718 		}
719 
720 		isrouter = rtm->rtm_flags & RTF_GATEWAY;
721 		prbs = rtm->rtm_rmx.rmx_pksent;
722 
723 		/*
724 		 * other flags. R: router, P: proxy, W: ??
725 		 */
726 		if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
727 			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
728 			    isrouter ? "R" : "",
729 			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
730 		} else {
731 #if 0			/* W and P are mystery even for us */
732 			sin = (struct sockaddr_in6 *)
733 			    (sdl->sdl_len + (char *)sdl);
734 			snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
735 			    isrouter ? "R" : "",
736 			    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
737 			    (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
738 			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
739 #else
740 			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
741 			    isrouter ? "R" : "",
742 			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
743 #endif
744 		}
745 		printf(" %s", flgbuf);
746 
747 		if (prbs)
748 			printf(" %d", prbs);
749 
750 		printf("\n");
751 	}
752 	if (buf != NULL)
753 		free(buf);
754 
755 	if (repeat) {
756 		printf("\n");
757 		fflush(stdout);
758 		sleep(repeat);
759 		goto again;
760 	}
761 }
762 
763 static struct in6_nbrinfo *
764 getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
765 {
766 	static struct in6_nbrinfo nbi;
767 	int s;
768 
769 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
770 		err(1, "socket");
771 
772 	bzero(&nbi, sizeof(nbi));
773 	if_indextoname(ifindex, nbi.ifname);
774 	nbi.addr = *addr;
775 	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
776 		if (warning)
777 			warn("ioctl(SIOCGNBRINFO_IN6)");
778 		close(s);
779 		return(NULL);
780 	}
781 
782 	close(s);
783 	return(&nbi);
784 }
785 
786 static char *
787 ether_str(struct sockaddr_dl *sdl)
788 {
789 	static char hbuf[NI_MAXHOST];
790 
791 	if (sdl->sdl_alen == ETHER_ADDR_LEN) {
792 		strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
793 		    sizeof(hbuf));
794 	} else if (sdl->sdl_alen) {
795 		int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
796 		snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
797 	} else
798 		snprintf(hbuf, sizeof(hbuf), "(incomplete)");
799 
800 	return(hbuf);
801 }
802 
803 static int
804 ndp_ether_aton(char *a, u_char *n)
805 {
806 	int i, o[6];
807 
808 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
809 	    &o[3], &o[4], &o[5]);
810 	if (i != 6) {
811 		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
812 		return (1);
813 	}
814 	for (i = 0; i < 6; i++)
815 		n[i] = o[i];
816 	return (0);
817 }
818 
819 static void
820 usage()
821 {
822 	printf("usage: ndp [-nt] hostname\n");
823 	printf("       ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
824 	printf("       ndp [-nt] -A wait\n");
825 	printf("       ndp [-nt] -d hostname\n");
826 	printf("       ndp [-nt] -f filename\n");
827 	printf("       ndp [-nt] -i interface [flags...]\n");
828 #ifdef SIOCSDEFIFACE_IN6
829 	printf("       ndp [-nt] -I [interface|delete]\n");
830 #endif
831 	printf("       ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
832 	exit(1);
833 }
834 
835 static int
836 rtmsg(int cmd)
837 {
838 	static int seq;
839 	int rlen;
840 	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
841 	register char *cp = m_rtmsg.m_space;
842 	register int l;
843 
844 	errno = 0;
845 	if (cmd == RTM_DELETE)
846 		goto doit;
847 	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
848 	rtm->rtm_flags = flags;
849 	rtm->rtm_version = RTM_VERSION;
850 
851 	switch (cmd) {
852 	default:
853 		fprintf(stderr, "ndp: internal wrong cmd\n");
854 		exit(1);
855 	case RTM_ADD:
856 		rtm->rtm_addrs |= RTA_GATEWAY;
857 		if (expire_time) {
858 			rtm->rtm_rmx.rmx_expire = expire_time;
859 			rtm->rtm_inits = RTV_EXPIRE;
860 		}
861 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
862 #if 0		/* we don't support ipv6addr/128 type proxying */
863 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
864 			rtm->rtm_flags &= ~RTF_HOST;
865 			rtm->rtm_addrs |= RTA_NETMASK;
866 		}
867 #endif
868 		/* FALLTHROUGH */
869 	case RTM_GET:
870 		rtm->rtm_addrs |= RTA_DST;
871 	}
872 
873 	NEXTADDR(RTA_DST, sin_m);
874 	NEXTADDR(RTA_GATEWAY, sdl_m);
875 #if 0	/* we don't support ipv6addr/128 type proxying */
876 	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
877 	NEXTADDR(RTA_NETMASK, so_mask);
878 #endif
879 
880 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
881 doit:
882 	l = rtm->rtm_msglen;
883 	rtm->rtm_seq = ++seq;
884 	rtm->rtm_type = cmd;
885 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
886 		if (errno != ESRCH || cmd != RTM_DELETE) {
887 			err(1, "writing to routing socket");
888 			/* NOTREACHED */
889 		}
890 	}
891 	do {
892 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
893 	} while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq ||
894 	    rtm->rtm_pid != pid));
895 	if (l < 0)
896 		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
897 		    strerror(errno));
898 	return (0);
899 }
900 
901 static void
902 ifinfo(char *ifname, int argc, char **argv)
903 {
904 	struct in6_ndireq nd;
905 	int i, s;
906 	u_int32_t newflags;
907 #ifdef IPV6CTL_USETEMPADDR
908 	u_int8_t nullbuf[8];
909 #endif
910 
911 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
912 		err(1, "socket");
913 		/* NOTREACHED */
914 	}
915 	bzero(&nd, sizeof(nd));
916 	strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
917 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
918 		err(1, "ioctl(SIOCGIFINFO_IN6)");
919 		/* NOTREACHED */
920 	}
921 #define	ND nd.ndi
922 	newflags = ND.flags;
923 	for (i = 0; i < argc; i++) {
924 		int clear = 0;
925 		char *cp = argv[i];
926 
927 		if (*cp == '-') {
928 			clear = 1;
929 			cp++;
930 		}
931 
932 #define	SETFLAG(s, f) do {			\
933 	if (strcmp(cp, (s)) == 0) {		\
934 		if (clear)			\
935 			newflags &= ~(f);	\
936 		else				\
937 			newflags |= (f);	\
938 	}					\
939 } while (0)
940 /*
941  * XXX: this macro is not 100% correct, in that it matches "nud" against
942  *      "nudbogus".  But we just let it go since this is minor.
943  */
944 #define	SETVALUE(f, v) do {						\
945 	char *valptr;							\
946 	unsigned long newval;						\
947 	v = 0; /* unspecified */					\
948 	if (strncmp(cp, f, strlen(f)) == 0) {				\
949 		valptr = strchr(cp, '=');				\
950 		if (valptr == NULL)					\
951 			err(1, "syntax error in %s field", (f));	\
952 		errno = 0;						\
953 		newval = strtoul(++valptr, NULL, 0);			\
954 		if (errno)						\
955 			err(1, "syntax error in %s's value", (f));	\
956 		v = newval;						\
957 	}								\
958 } while (0)
959 
960 		SETFLAG("disabled", ND6_IFF_IFDISABLED);
961 		SETFLAG("nud", ND6_IFF_PERFORMNUD);
962 #ifdef ND6_IFF_ACCEPT_RTADV
963 		SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
964 #endif
965 #ifdef ND6_IFF_AUTO_LINKLOCAL
966 		SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
967 #endif
968 #ifdef ND6_IFF_NO_PREFER_IFACE
969 		SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
970 #endif
971 		SETVALUE("basereachable", ND.basereachable);
972 		SETVALUE("retrans", ND.retrans);
973 		SETVALUE("curhlim", ND.chlim);
974 
975 		ND.flags = newflags;
976 		if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
977 			err(1, "ioctl(SIOCSIFINFO_IN6)");
978 			/* NOTREACHED */
979 		}
980 #undef SETFLAG
981 #undef SETVALUE
982 	}
983 
984 	if (!ND.initialized) {
985 		errx(1, "%s: not initialized yet", ifname);
986 		/* NOTREACHED */
987 	}
988 
989 	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
990 		err(1, "ioctl(SIOCGIFINFO_IN6)");
991 		/* NOTREACHED */
992 	}
993 	printf("linkmtu=%d", ND.linkmtu);
994 	printf(", maxmtu=%d", ND.maxmtu);
995 	printf(", curhlim=%d", ND.chlim);
996 	printf(", basereachable=%ds%dms",
997 	    ND.basereachable / 1000, ND.basereachable % 1000);
998 	printf(", reachable=%ds", ND.reachable);
999 	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
1000 #ifdef IPV6CTL_USETEMPADDR
1001 	memset(nullbuf, 0, sizeof(nullbuf));
1002 	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
1003 		int j;
1004 		u_int8_t *rbuf;
1005 
1006 		for (i = 0; i < 3; i++) {
1007 			switch (i) {
1008 			case 0:
1009 				printf("\nRandom seed(0): ");
1010 				rbuf = ND.randomseed0;
1011 				break;
1012 			case 1:
1013 				printf("\nRandom seed(1): ");
1014 				rbuf = ND.randomseed1;
1015 				break;
1016 			case 2:
1017 				printf("\nRandom ID:      ");
1018 				rbuf = ND.randomid;
1019 				break;
1020 			default:
1021 				errx(1, "impossible case for tempaddr display");
1022 			}
1023 			for (j = 0; j < 8; j++)
1024 				printf("%02x", rbuf[j]);
1025 		}
1026 	}
1027 #endif /* IPV6CTL_USETEMPADDR */
1028 	if (ND.flags) {
1029 		printf("\nFlags: ");
1030 #ifdef ND6_IFF_IFDISABLED
1031 		if ((ND.flags & ND6_IFF_IFDISABLED))
1032 			printf("disabled ");
1033 #endif
1034 		if ((ND.flags & ND6_IFF_PERFORMNUD))
1035 			printf("nud ");
1036 #ifdef ND6_IFF_ACCEPT_RTADV
1037 		if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1038 			printf("accept_rtadv ");
1039 #endif
1040 #ifdef ND6_IFF_AUTO_LINKLOCAL
1041 		if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1042 			printf("auto_linklocal ");
1043 #endif
1044 #ifdef ND6_IFF_NO_PREFER_IFACE
1045 		if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1046 			printf("no_prefer_iface ");
1047 #endif
1048 	}
1049 	putc('\n', stdout);
1050 #undef ND
1051 
1052 	close(s);
1053 }
1054 
1055 #ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
1056 #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
1057 #endif
1058 
1059 static void
1060 rtrlist()
1061 {
1062 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1063 	char *buf;
1064 	struct in6_defrouter *p, *ep;
1065 	size_t l;
1066 	struct timeval now;
1067 
1068 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1069 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1070 		/*NOTREACHED*/
1071 	}
1072 	if (l == 0)
1073 		return;
1074 	buf = malloc(l);
1075 	if (!buf) {
1076 		err(1, "malloc");
1077 		/*NOTREACHED*/
1078 	}
1079 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1080 		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1081 		/*NOTREACHED*/
1082 	}
1083 
1084 	ep = (struct in6_defrouter *)(buf + l);
1085 	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1086 		int rtpref;
1087 
1088 		if (getnameinfo((struct sockaddr *)&p->rtaddr,
1089 		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1090 		    (nflag ? NI_NUMERICHOST : 0)) != 0)
1091 			strlcpy(host_buf, "?", sizeof(host_buf));
1092 
1093 		printf("%s if=%s", host_buf,
1094 		    if_indextoname(p->if_index, ifix_buf));
1095 		printf(", flags=%s%s",
1096 		    p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1097 		    p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
1098 		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1099 		printf(", pref=%s", rtpref_str[rtpref]);
1100 
1101 		gettimeofday(&now, 0);
1102 		if (p->expire == 0)
1103 			printf(", expire=Never\n");
1104 		else
1105 			printf(", expire=%s\n",
1106 			    sec2str(p->expire - now.tv_sec));
1107 	}
1108 	free(buf);
1109 }
1110 
1111 static void
1112 plist()
1113 {
1114 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1115 	char *buf;
1116 	struct in6_prefix *p, *ep, *n;
1117 	struct sockaddr_in6 *advrtr;
1118 	size_t l;
1119 	struct timeval now;
1120 	const int niflags = NI_NUMERICHOST;
1121 	int ninflags = nflag ? NI_NUMERICHOST : 0;
1122 	char namebuf[NI_MAXHOST];
1123 
1124 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1125 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1126 		/*NOTREACHED*/
1127 	}
1128 	buf = malloc(l);
1129 	if (!buf) {
1130 		err(1, "malloc");
1131 		/*NOTREACHED*/
1132 	}
1133 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1134 		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1135 		/*NOTREACHED*/
1136 	}
1137 
1138 	ep = (struct in6_prefix *)(buf + l);
1139 	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1140 		advrtr = (struct sockaddr_in6 *)(p + 1);
1141 		n = (struct in6_prefix *)&advrtr[p->advrtrs];
1142 
1143 		if (getnameinfo((struct sockaddr *)&p->prefix,
1144 		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
1145 		    NULL, 0, niflags) != 0)
1146 			strlcpy(namebuf, "?", sizeof(namebuf));
1147 		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1148 		    if_indextoname(p->if_index, ifix_buf));
1149 
1150 		gettimeofday(&now, 0);
1151 		/*
1152 		 * meaning of fields, especially flags, is very different
1153 		 * by origin.  notify the difference to the users.
1154 		 */
1155 		printf("flags=%s%s%s%s%s",
1156 		    p->raflags.onlink ? "L" : "",
1157 		    p->raflags.autonomous ? "A" : "",
1158 		    (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1159 		    (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1160 #ifdef NDPRF_HOME
1161 		    (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1162 #else
1163 		    ""
1164 #endif
1165 		    );
1166 		if (p->vltime == ND6_INFINITE_LIFETIME)
1167 			printf(" vltime=infinity");
1168 		else
1169 			printf(" vltime=%lu", (unsigned long)p->vltime);
1170 		if (p->pltime == ND6_INFINITE_LIFETIME)
1171 			printf(", pltime=infinity");
1172 		else
1173 			printf(", pltime=%lu", (unsigned long)p->pltime);
1174 		if (p->expire == 0)
1175 			printf(", expire=Never");
1176 		else if (p->expire >= now.tv_sec)
1177 			printf(", expire=%s",
1178 			    sec2str(p->expire - now.tv_sec));
1179 		else
1180 			printf(", expired");
1181 		printf(", ref=%d", p->refcnt);
1182 		printf("\n");
1183 		/*
1184 		 * "advertising router" list is meaningful only if the prefix
1185 		 * information is from RA.
1186 		 */
1187 		if (p->advrtrs) {
1188 			int j;
1189 			struct sockaddr_in6 *sin6;
1190 
1191 			sin6 = advrtr;
1192 			printf("  advertised by\n");
1193 			for (j = 0; j < p->advrtrs; j++) {
1194 				struct in6_nbrinfo *nbi;
1195 
1196 				if (getnameinfo((struct sockaddr *)sin6,
1197 				    sin6->sin6_len, namebuf, sizeof(namebuf),
1198 				    NULL, 0, ninflags) != 0)
1199 					strlcpy(namebuf, "?", sizeof(namebuf));
1200 				printf("    %s", namebuf);
1201 
1202 				nbi = getnbrinfo(&sin6->sin6_addr,
1203 				    p->if_index, 0);
1204 				if (nbi) {
1205 					switch (nbi->state) {
1206 					case ND6_LLINFO_REACHABLE:
1207 					case ND6_LLINFO_STALE:
1208 					case ND6_LLINFO_DELAY:
1209 					case ND6_LLINFO_PROBE:
1210 						printf(" (reachable)\n");
1211 						break;
1212 					default:
1213 						printf(" (unreachable)\n");
1214 					}
1215 				} else
1216 					printf(" (no neighbor state)\n");
1217 				sin6++;
1218 			}
1219 		} else
1220 			printf("  No advertising router\n");
1221 	}
1222 	free(buf);
1223 }
1224 
1225 static void
1226 pfx_flush()
1227 {
1228 	char dummyif[IFNAMSIZ+8];
1229 	int s;
1230 
1231 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1232 		err(1, "socket");
1233 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1234 	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1235 		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1236 }
1237 
1238 static void
1239 rtr_flush()
1240 {
1241 	char dummyif[IFNAMSIZ+8];
1242 	int s;
1243 
1244 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1245 		err(1, "socket");
1246 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1247 	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1248 		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1249 
1250 	close(s);
1251 }
1252 
1253 static void
1254 harmonize_rtr()
1255 {
1256 	char dummyif[IFNAMSIZ+8];
1257 	int s;
1258 
1259 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1260 		err(1, "socket");
1261 	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1262 	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1263 		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1264 
1265 	close(s);
1266 }
1267 
1268 #ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
1269 static void
1270 setdefif(char *ifname)
1271 {
1272 	struct in6_ndifreq ndifreq;
1273 	unsigned int ifindex;
1274 
1275 	if (strcasecmp(ifname, "delete") == 0)
1276 		ifindex = 0;
1277 	else {
1278 		if ((ifindex = if_nametoindex(ifname)) == 0)
1279 			err(1, "failed to resolve i/f index for %s", ifname);
1280 	}
1281 
1282 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1283 		err(1, "socket");
1284 
1285 	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1286 	ndifreq.ifindex = ifindex;
1287 
1288 	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1289 		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1290 
1291 	close(s);
1292 }
1293 
1294 static void
1295 getdefif()
1296 {
1297 	struct in6_ndifreq ndifreq;
1298 	char ifname[IFNAMSIZ+8];
1299 
1300 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1301 		err(1, "socket");
1302 
1303 	memset(&ndifreq, 0, sizeof(ndifreq));
1304 	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1305 
1306 	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1307 		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1308 
1309 	if (ndifreq.ifindex == 0)
1310 		printf("No default interface.\n");
1311 	else {
1312 		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1313 			err(1, "failed to resolve ifname for index %lu",
1314 			    ndifreq.ifindex);
1315 		printf("ND default interface = %s\n", ifname);
1316 	}
1317 
1318 	close(s);
1319 }
1320 #endif /* SIOCSDEFIFACE_IN6 */
1321 
1322 static char *
1323 sec2str(time_t total)
1324 {
1325 	static char result[256];
1326 	int days, hours, mins, secs;
1327 	int first = 1;
1328 	char *p = result;
1329 	char *ep = &result[sizeof(result)];
1330 	int n;
1331 
1332 	days = total / 3600 / 24;
1333 	hours = (total / 3600) % 24;
1334 	mins = (total / 60) % 60;
1335 	secs = total % 60;
1336 
1337 	if (days) {
1338 		first = 0;
1339 		n = snprintf(p, ep - p, "%dd", days);
1340 		if (n < 0 || n >= ep - p)
1341 			return "?";
1342 		p += n;
1343 	}
1344 	if (!first || hours) {
1345 		first = 0;
1346 		n = snprintf(p, ep - p, "%dh", hours);
1347 		if (n < 0 || n >= ep - p)
1348 			return "?";
1349 		p += n;
1350 	}
1351 	if (!first || mins) {
1352 		first = 0;
1353 		n = snprintf(p, ep - p, "%dm", mins);
1354 		if (n < 0 || n >= ep - p)
1355 			return "?";
1356 		p += n;
1357 	}
1358 	snprintf(p, ep - p, "%ds", secs);
1359 
1360 	return(result);
1361 }
1362 
1363 /*
1364  * Print the timestamp
1365  * from tcpdump/util.c
1366  */
1367 static void
1368 ts_print(const struct timeval *tvp)
1369 {
1370 	int s;
1371 
1372 	/* Default */
1373 	s = (tvp->tv_sec + thiszone) % 86400;
1374 	(void)printf("%02d:%02d:%02d.%06u ",
1375 	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1376 }
1377 
1378 #undef NEXTADDR
1379