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