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