ndp.c revision 287095
175295Sdes/*	$FreeBSD: head/usr.sbin/ndp/ndp.c 287095 2015-08-24 05:28:23Z hrs $	*/
275295Sdes/*	$KAME: ndp.c,v 1.104 2003/06/27 07:48:39 itojun Exp $	*/
375295Sdes
475295Sdes/*
575295Sdes * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
675295Sdes * All rights reserved.
775295Sdes *
875295Sdes * Redistribution and use in source and binary forms, with or without
975295Sdes * modification, are permitted provided that the following conditions
1075295Sdes * are met:
1175295Sdes * 1. Redistributions of source code must retain the above copyright
1275295Sdes *    notice, this list of conditions and the following disclaimer.
1375295Sdes * 2. Redistributions in binary form must reproduce the above copyright
1475295Sdes *    notice, this list of conditions and the following disclaimer in the
1575295Sdes *    documentation and/or other materials provided with the distribution.
1675295Sdes * 3. Neither the name of the project nor the names of its contributors
1775295Sdes *    may be used to endorse or promote products derived from this software
1875295Sdes *    without specific prior written permission.
1975295Sdes *
2075295Sdes * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2175295Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2275295Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2375295Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2475295Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2575295Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2675295Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2775295Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2875295Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2975295Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3075295Sdes * SUCH DAMAGE.
3175295Sdes */
3275295Sdes/*
3375295Sdes * Copyright (c) 1984, 1993
3478073Sdes *	The Regents of the University of California.  All rights reserved.
3575295Sdes *
3677965Sdes * This code is derived from software contributed to Berkeley by
3784246Sdes * Sun Microsystems, Inc.
3875295Sdes *
3975295Sdes * Redistribution and use in source and binary forms, with or without
4075295Sdes * modification, are permitted provided that the following conditions
4175295Sdes * are met:
4275295Sdes * 1. Redistributions of source code must retain the above copyright
4375295Sdes *    notice, this list of conditions and the following disclaimer.
4477998Sdes * 2. Redistributions in binary form must reproduce the above copyright
4575295Sdes *    notice, this list of conditions and the following disclaimer in the
4675295Sdes *    documentation and/or other materials provided with the distribution.
4789071Smsmith * 4. Neither the name of the University nor the names of its contributors
4884246Sdes *    may be used to endorse or promote products derived from this software
4975295Sdes *    without specific prior written permission.
5075295Sdes *
5175295Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5275295Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5384246Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5484246Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5584246Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5684246Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5784246Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5884246Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5984246Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6084246Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6184246Sdes * SUCH DAMAGE.
6284246Sdes */
6375295Sdes
6484246Sdes/*
6584246Sdes * Based on:
6675295Sdes * "@(#) Copyright (c) 1984, 1993\n\
6775295Sdes *	The Regents of the University of California.  All rights reserved.\n";
6875295Sdes *
6984246Sdes * "@(#)arp.c	8.2 (Berkeley) 1/2/94";
7084246Sdes */
7175295Sdes
7275295Sdes/*
7375295Sdes * ndp - display, set, delete and flush neighbor cache
7475295Sdes */
7575295Sdes
7675295Sdes
7775295Sdes#include <sys/param.h>
7875295Sdes#include <sys/file.h>
7975295Sdes#include <sys/ioctl.h>
8075295Sdes#include <sys/socket.h>
8184246Sdes#include <sys/sysctl.h>
8284246Sdes#include <sys/time.h>
8384246Sdes#include <sys/queue.h>
8475295Sdes
8575295Sdes#include <net/if.h>
8675295Sdes#include <net/if_dl.h>
8775295Sdes#include <net/if_types.h>
8875295Sdes#include <net/route.h>
8975295Sdes
9075295Sdes#include <netinet/in.h>
9175295Sdes#include <netinet/if_ether.h>
9284246Sdes
9384386Sdes#include <netinet/icmp6.h>
9484386Sdes#include <netinet6/in6_var.h>
9584386Sdes#include <netinet6/nd6.h>
9675295Sdes
9775295Sdes#include <arpa/inet.h>
9875295Sdes
9975295Sdes#include <ctype.h>
10075295Sdes#include <netdb.h>
10175295Sdes#include <errno.h>
10275295Sdes#include <nlist.h>
10377998Sdes#include <stdio.h>
10477998Sdes#include <string.h>
10575295Sdes#include <paths.h>
10677998Sdes#include <err.h>
10775295Sdes#include <stdlib.h>
10888234Sdillon#include <fcntl.h>
10988234Sdillon#include <unistd.h>
11088234Sdillon#include "gmt2local.h"
11188234Sdillon
11288234Sdillon#define NEXTADDR(w, s) \
11375295Sdes	if (rtm->rtm_addrs & (w)) { \
11484246Sdes		bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);}
11577998Sdes
11684246Sdes
11775295Sdesstatic pid_t pid;
11884246Sdesstatic int nflag;
11975295Sdesstatic int tflag;
12088234Sdillonstatic int32_t thiszone;	/* time difference with gmt */
12188234Sdillonstatic int s = -1;
12275295Sdesstatic int repeat = 0;
12375295Sdes
12477998Sdeschar ntop_buf[INET6_ADDRSTRLEN];	/* inet_ntop() */
12577998Sdeschar host_buf[NI_MAXHOST];		/* getnameinfo() */
12677998Sdeschar ifix_buf[IFNAMSIZ];		/* if_indextoname() */
12777998Sdes
12877998Sdesint main(int, char **);
12975295Sdesstatic int file(char *);
13075295Sdesvoid getsocket(void);
13175295Sdesint set(int, char **);
13277998Sdesvoid get(char *);
13384246Sdesint delete(char *);
13484246Sdesvoid dump(struct sockaddr_in6 *, int);
13575295Sdesstatic struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int);
13677998Sdesstatic char *ether_str(struct sockaddr_dl *);
13775295Sdesint ndp_ether_aton(char *, u_char *);
13877998Sdesvoid usage(void);
13977998Sdesint rtmsg(int);
14077998Sdesvoid ifinfo(char *, int, char **);
14175295Sdesvoid rtrlist(void);
14275295Sdesvoid plist(void);
14375295Sdesvoid pfx_flush(void);
14475295Sdesvoid rtr_flush(void);
14575295Sdesvoid harmonize_rtr(void);
14675295Sdes#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
14784246Sdesstatic void getdefif(void);
14875295Sdesstatic void setdefif(char *);
14975295Sdes#endif
15075295Sdesstatic char *sec2str(time_t);
15177998Sdesstatic void ts_print(const struct timeval *);
15275295Sdes
15375295Sdesstatic char *rtpref_str[] = {
15475295Sdes	"medium",		/* 00 */
15575295Sdes	"high",			/* 01 */
15675295Sdes	"rsv",			/* 10 */
15775295Sdes	"low"			/* 11 */
15875295Sdes};
15975295Sdes
16077998Sdesint mode = 0;
16177998Sdeschar *arg = NULL;
16275295Sdes
16375295Sdesint
16475295Sdesmain(int argc, char **argv)
16584246Sdes{
16677998Sdes	int ch;
16784246Sdes
16884246Sdes	pid = getpid();
16984246Sdes	thiszone = gmt2local(0);
17084246Sdes	while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1)
17184246Sdes		switch (ch) {
17275295Sdes		case 'a':
17375295Sdes		case 'c':
17475295Sdes		case 'p':
17575295Sdes		case 'r':
17675295Sdes		case 'H':
17775295Sdes		case 'P':
17875295Sdes		case 'R':
17975295Sdes		case 's':
18075295Sdes		case 'I':
18175295Sdes			if (mode) {
18277998Sdes				usage();
18388234Sdillon				/*NOTREACHED*/
18488234Sdillon			}
18575295Sdes			mode = ch;
18675295Sdes			arg = NULL;
18784246Sdes			break;
18884246Sdes		case 'f':
18984246Sdes			exit(file(optarg) ? 1 : 0);
19084246Sdes		case 'd':
19184246Sdes		case 'i':
19284246Sdes			if (mode) {
19377998Sdes				usage();
19484246Sdes				/*NOTREACHED*/
19577998Sdes			}
19684246Sdes			mode = ch;
19784246Sdes			arg = optarg;
19877998Sdes			break;
19975295Sdes		case 'n':
20075295Sdes			nflag = 1;
20175295Sdes			break;
20284246Sdes		case 't':
20384246Sdes			tflag = 1;
20484246Sdes			break;
20584246Sdes		case 'A':
20684246Sdes			if (mode) {
20784246Sdes				usage();
20884246Sdes				/*NOTREACHED*/
20984246Sdes			}
21084246Sdes			mode = 'a';
21184246Sdes			repeat = atoi(optarg);
21284246Sdes			if (repeat < 0) {
21384246Sdes				usage();
21484246Sdes				/*NOTREACHED*/
21584246Sdes			}
21684246Sdes			break;
21784246Sdes		default:
21884246Sdes			usage();
21984246Sdes		}
22084246Sdes
22184246Sdes	argc -= optind;
22284246Sdes	argv += optind;
22384246Sdes
22488868Stanimura	switch (mode) {
22588868Stanimura	case 'a':
22684246Sdes	case 'c':
22784246Sdes		if (argc != 0) {
22884246Sdes			usage();
22984386Sdes			/*NOTREACHED*/
23084386Sdes		}
23184386Sdes		dump(0, mode == 'c');
23284386Sdes		break;
23384386Sdes	case 'd':
23484386Sdes		if (argc != 0) {
23584386Sdes			usage();
23684386Sdes			/*NOTREACHED*/
23784386Sdes		}
23884386Sdes		delete(arg);
23984386Sdes		break;
24084386Sdes	case 'I':
24184386Sdes#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
24284386Sdes		if (argc > 1) {
24384386Sdes			usage();
24484386Sdes			/*NOTREACHED*/
24584386Sdes		} else if (argc == 1) {
24684386Sdes			if (strcmp(*argv, "delete") == 0 ||
24784386Sdes			    if_nametoindex(*argv))
24884386Sdes				setdefif(*argv);
24984386Sdes			else
25088868Stanimura				errx(1, "invalid interface %s", *argv);
25188868Stanimura		}
25284386Sdes		getdefif(); /* always call it to print the result */
25384386Sdes		break;
25484386Sdes#else
25584386Sdes		errx(1, "not supported yet");
25684386Sdes		/*NOTREACHED*/
25784386Sdes#endif
25884386Sdes	case 'p':
25984386Sdes		if (argc != 0) {
26084386Sdes			usage();
26184386Sdes			/*NOTREACHED*/
26284386Sdes		}
26384386Sdes		plist();
26484386Sdes		break;
26584386Sdes	case 'i':
266		ifinfo(arg, argc, argv);
267		break;
268	case 'r':
269		if (argc != 0) {
270			usage();
271			/*NOTREACHED*/
272		}
273		rtrlist();
274		break;
275	case 's':
276		if (argc < 2 || argc > 4)
277			usage();
278		exit(set(argc, argv) ? 1 : 0);
279	case 'H':
280		if (argc != 0) {
281			usage();
282			/*NOTREACHED*/
283		}
284		harmonize_rtr();
285		break;
286	case 'P':
287		if (argc != 0) {
288			usage();
289			/*NOTREACHED*/
290		}
291		pfx_flush();
292		break;
293	case 'R':
294		if (argc != 0) {
295			usage();
296			/*NOTREACHED*/
297		}
298		rtr_flush();
299		break;
300	case 0:
301		if (argc != 1) {
302			usage();
303			/*NOTREACHED*/
304		}
305		get(argv[0]);
306		break;
307	}
308	exit(0);
309}
310
311/*
312 * Process a file to set standard ndp entries
313 */
314static int
315file(char *name)
316{
317	FILE *fp;
318	int i, retval;
319	char line[100], arg[5][50], *args[5], *p;
320
321	if ((fp = fopen(name, "r")) == NULL)
322		err(1, "cannot open %s", name);
323	args[0] = &arg[0][0];
324	args[1] = &arg[1][0];
325	args[2] = &arg[2][0];
326	args[3] = &arg[3][0];
327	args[4] = &arg[4][0];
328	retval = 0;
329	while (fgets(line, sizeof(line), fp) != NULL) {
330		if ((p = strchr(line, '#')) != NULL)
331			*p = '\0';
332		for (p = line; isblank(*p); p++);
333		if (*p == '\n' || *p == '\0')
334			continue;
335		i = sscanf(line, "%49s %49s %49s %49s %49s",
336		    arg[0], arg[1], arg[2], arg[3], arg[4]);
337		if (i < 2) {
338			warnx("bad line: %s", line);
339			retval = 1;
340			continue;
341		}
342		if (set(i, args))
343			retval = 1;
344	}
345	fclose(fp);
346	return (retval);
347}
348
349void
350getsocket()
351{
352	if (s < 0) {
353		s = socket(PF_ROUTE, SOCK_RAW, 0);
354		if (s < 0) {
355			err(1, "socket");
356			/* NOTREACHED */
357		}
358	}
359}
360
361struct	sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 };
362struct	sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
363struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
364time_t	expire_time;
365int	flags, found_entry;
366struct	{
367	struct	rt_msghdr m_rtm;
368	char	m_space[512];
369}	m_rtmsg;
370
371/*
372 * Set an individual neighbor cache entry
373 */
374int
375set(int argc, char **argv)
376{
377	register struct sockaddr_in6 *sin = &sin_m;
378	register struct sockaddr_dl *sdl;
379	register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
380	struct addrinfo hints, *res;
381	int gai_error;
382	u_char *ea;
383	char *host = argv[0], *eaddr = argv[1];
384
385	getsocket();
386	argc -= 2;
387	argv += 2;
388	sdl_m = blank_sdl;
389	sin_m = blank_sin;
390
391	bzero(&hints, sizeof(hints));
392	hints.ai_family = AF_INET6;
393	gai_error = getaddrinfo(host, NULL, &hints, &res);
394	if (gai_error) {
395		fprintf(stderr, "ndp: %s: %s\n", host,
396			gai_strerror(gai_error));
397		return 1;
398	}
399	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
400	sin->sin6_scope_id =
401	    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
402	ea = (u_char *)LLADDR(&sdl_m);
403	if (ndp_ether_aton(eaddr, ea) == 0)
404		sdl_m.sdl_alen = 6;
405	flags = expire_time = 0;
406	while (argc-- > 0) {
407		if (strncmp(argv[0], "temp", 4) == 0) {
408			struct timeval now;
409
410			gettimeofday(&now, 0);
411			expire_time = now.tv_sec + 20 * 60;
412		} else if (strncmp(argv[0], "proxy", 5) == 0)
413			flags |= RTF_ANNOUNCE;
414		argv++;
415	}
416	if (rtmsg(RTM_GET) < 0) {
417		errx(1, "RTM_GET(%s) failed", host);
418		/* NOTREACHED */
419	}
420	sin = (struct sockaddr_in6 *)(rtm + 1);
421	sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
422	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
423		if (sdl->sdl_family == AF_LINK &&
424		    !(rtm->rtm_flags & RTF_GATEWAY)) {
425			switch (sdl->sdl_type) {
426			case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
427			case IFT_ISO88024: case IFT_ISO88025:
428			case IFT_L2VLAN: case IFT_BRIDGE:
429				goto overwrite;
430			}
431		}
432		fprintf(stderr, "set: cannot configure a new entry\n");
433		return 1;
434	}
435
436overwrite:
437	if (sdl->sdl_family != AF_LINK) {
438		printf("cannot intuit interface index and type for %s\n", host);
439		return (1);
440	}
441	sdl_m.sdl_type = sdl->sdl_type;
442	sdl_m.sdl_index = sdl->sdl_index;
443	return (rtmsg(RTM_ADD));
444}
445
446/*
447 * Display an individual neighbor cache entry
448 */
449void
450get(char *host)
451{
452	struct sockaddr_in6 *sin = &sin_m;
453	struct addrinfo hints, *res;
454	int gai_error;
455
456	sin_m = blank_sin;
457	bzero(&hints, sizeof(hints));
458	hints.ai_family = AF_INET6;
459	gai_error = getaddrinfo(host, NULL, &hints, &res);
460	if (gai_error) {
461		fprintf(stderr, "ndp: %s: %s\n", host,
462		    gai_strerror(gai_error));
463		return;
464	}
465	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
466	sin->sin6_scope_id =
467	    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
468	dump(sin, 0);
469	if (found_entry == 0) {
470		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
471		    sizeof(host_buf), NULL ,0,
472		    (nflag ? NI_NUMERICHOST : 0));
473		printf("%s (%s) -- no entry\n", host, host_buf);
474		exit(1);
475	}
476}
477
478/*
479 * Delete a neighbor cache entry
480 */
481int
482delete(char *host)
483{
484	struct sockaddr_in6 *sin = &sin_m;
485	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
486	register char *cp = m_rtmsg.m_space;
487	struct sockaddr_dl *sdl;
488	struct addrinfo hints, *res;
489	int gai_error;
490
491	getsocket();
492	sin_m = blank_sin;
493
494	bzero(&hints, sizeof(hints));
495	hints.ai_family = AF_INET6;
496	gai_error = getaddrinfo(host, NULL, &hints, &res);
497	if (gai_error) {
498		fprintf(stderr, "ndp: %s: %s\n", host,
499		    gai_strerror(gai_error));
500		return 1;
501	}
502	sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
503	sin->sin6_scope_id =
504	    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
505	if (rtmsg(RTM_GET) < 0) {
506		errx(1, "RTM_GET(%s) failed", host);
507		/* NOTREACHED */
508	}
509	sin = (struct sockaddr_in6 *)(rtm + 1);
510	sdl = (struct sockaddr_dl *)(ALIGN(sin->sin6_len) + (char *)sin);
511	if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
512		if (sdl->sdl_family == AF_LINK &&
513		    !(rtm->rtm_flags & RTF_GATEWAY)) {
514			goto delete;
515		}
516		fprintf(stderr, "delete: cannot delete non-NDP entry\n");
517		return 1;
518	}
519
520delete:
521	if (sdl->sdl_family != AF_LINK) {
522		printf("cannot locate %s\n", host);
523		return (1);
524	}
525        /*
526         * need to reinit the field because it has rt_key
527         * but we want the actual address
528         */
529	NEXTADDR(RTA_DST, sin_m);
530	rtm->rtm_flags |= RTF_LLDATA;
531	if (rtmsg(RTM_DELETE) == 0) {
532		getnameinfo((struct sockaddr *)sin,
533		    sin->sin6_len, host_buf,
534		    sizeof(host_buf), NULL, 0,
535		    (nflag ? NI_NUMERICHOST : 0));
536		printf("%s (%s) deleted\n", host, host_buf);
537	}
538
539	return 0;
540}
541
542#define W_ADDR	36
543#define W_LL	17
544#define W_IF	6
545
546/*
547 * Dump the entire neighbor cache
548 */
549void
550dump(struct sockaddr_in6 *addr, int cflag)
551{
552	int mib[6];
553	size_t needed;
554	char *lim, *buf, *next;
555	struct rt_msghdr *rtm;
556	struct sockaddr_in6 *sin;
557	struct sockaddr_dl *sdl;
558	extern int h_errno;
559	struct in6_nbrinfo *nbi;
560	struct timeval now;
561	int addrwidth;
562	int llwidth;
563	int ifwidth;
564	char flgbuf[8];
565	char *ifname;
566
567	/* Print header */
568	if (!tflag && !cflag)
569		printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n",
570		    W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address",
571		    W_IF, W_IF, "Netif", "Expire", "S", "Flags");
572
573again:;
574	mib[0] = CTL_NET;
575	mib[1] = PF_ROUTE;
576	mib[2] = 0;
577	mib[3] = AF_INET6;
578	mib[4] = NET_RT_FLAGS;
579#ifdef RTF_LLINFO
580	mib[5] = RTF_LLINFO;
581#else
582	mib[5] = 0;
583#endif
584	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
585		err(1, "sysctl(PF_ROUTE estimate)");
586	if (needed > 0) {
587		if ((buf = malloc(needed)) == NULL)
588			err(1, "malloc");
589		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
590			err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)");
591		lim = buf + needed;
592	} else
593		buf = lim = NULL;
594
595	for (next = buf; next && next < lim; next += rtm->rtm_msglen) {
596		int isrouter = 0, prbs = 0;
597
598		rtm = (struct rt_msghdr *)next;
599		sin = (struct sockaddr_in6 *)(rtm + 1);
600		sdl = (struct sockaddr_dl *)((char *)sin + ALIGN(sin->sin6_len));
601
602		/*
603		 * Some OSes can produce a route that has the LINK flag but
604		 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD
605		 * and BSD/OS, where xx is not the interface identifier on
606		 * lo0).  Such routes entry would annoy getnbrinfo() below,
607		 * so we skip them.
608		 * XXX: such routes should have the GATEWAY flag, not the
609		 * LINK flag.  However, there is rotten routing software
610		 * that advertises all routes that have the GATEWAY flag.
611		 * Thus, KAME kernel intentionally does not set the LINK flag.
612		 * What is to be fixed is not ndp, but such routing software
613		 * (and the kernel workaround)...
614		 */
615		if (sdl->sdl_family != AF_LINK)
616			continue;
617
618		if (!(rtm->rtm_flags & RTF_HOST))
619			continue;
620
621		if (addr) {
622			if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr,
623			    &sin->sin6_addr) == 0 ||
624			    addr->sin6_scope_id != sin->sin6_scope_id)
625				continue;
626			found_entry = 1;
627		} else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
628			continue;
629		if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) ||
630		    IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) {
631			/* XXX: should scope id be filled in the kernel? */
632			if (sin->sin6_scope_id == 0)
633				sin->sin6_scope_id = sdl->sdl_index;
634		}
635		getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf,
636		    sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0));
637		if (cflag) {
638#ifdef RTF_WASCLONED
639			if (rtm->rtm_flags & RTF_WASCLONED)
640				delete(host_buf);
641#elif defined(RTF_CLONED)
642			if (rtm->rtm_flags & RTF_CLONED)
643				delete(host_buf);
644#else
645			delete(host_buf);
646#endif
647			continue;
648		}
649		gettimeofday(&now, 0);
650		if (tflag)
651			ts_print(&now);
652
653		addrwidth = strlen(host_buf);
654		if (addrwidth < W_ADDR)
655			addrwidth = W_ADDR;
656		llwidth = strlen(ether_str(sdl));
657		if (W_ADDR + W_LL - addrwidth > llwidth)
658			llwidth = W_ADDR + W_LL - addrwidth;
659		ifname = if_indextoname(sdl->sdl_index, ifix_buf);
660		if (!ifname)
661			ifname = "?";
662		ifwidth = strlen(ifname);
663		if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth)
664			ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth;
665
666		printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf,
667		    llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname);
668
669		/* Print neighbor discovery specific informations */
670		nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1);
671		if (nbi) {
672			if (nbi->expire > now.tv_sec) {
673				printf(" %-9.9s",
674				    sec2str(nbi->expire - now.tv_sec));
675			} else if (nbi->expire == 0)
676				printf(" %-9.9s", "permanent");
677			else
678				printf(" %-9.9s", "expired");
679
680			switch (nbi->state) {
681			case ND6_LLINFO_NOSTATE:
682				 printf(" N");
683				 break;
684#ifdef ND6_LLINFO_WAITDELETE
685			case ND6_LLINFO_WAITDELETE:
686				 printf(" W");
687				 break;
688#endif
689			case ND6_LLINFO_INCOMPLETE:
690				 printf(" I");
691				 break;
692			case ND6_LLINFO_REACHABLE:
693				 printf(" R");
694				 break;
695			case ND6_LLINFO_STALE:
696				 printf(" S");
697				 break;
698			case ND6_LLINFO_DELAY:
699				 printf(" D");
700				 break;
701			case ND6_LLINFO_PROBE:
702				 printf(" P");
703				 break;
704			default:
705				 printf(" ?");
706				 break;
707			}
708
709			isrouter = nbi->isrouter;
710			prbs = nbi->asked;
711		} else {
712			warnx("failed to get neighbor information");
713			printf("  ");
714		}
715
716		/*
717		 * other flags. R: router, P: proxy, W: ??
718		 */
719		if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
720			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
721			    isrouter ? "R" : "",
722			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
723		} else {
724			sin = (struct sockaddr_in6 *)
725			    (sdl->sdl_len + (char *)sdl);
726#if 0	/* W and P are mystery even for us */
727			snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s",
728			    isrouter ? "R" : "",
729			    !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "",
730			    (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "",
731			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
732#else
733			snprintf(flgbuf, sizeof(flgbuf), "%s%s",
734			    isrouter ? "R" : "",
735			    (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : "");
736#endif
737		}
738		printf(" %s", flgbuf);
739
740		if (prbs)
741			printf(" %d", prbs);
742
743		printf("\n");
744	}
745	if (buf != NULL)
746		free(buf);
747
748	if (repeat) {
749		printf("\n");
750		fflush(stdout);
751		sleep(repeat);
752		goto again;
753	}
754}
755
756static struct in6_nbrinfo *
757getnbrinfo(struct in6_addr *addr, int ifindex, int warning)
758{
759	static struct in6_nbrinfo nbi;
760	int s;
761
762	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
763		err(1, "socket");
764
765	bzero(&nbi, sizeof(nbi));
766	if_indextoname(ifindex, nbi.ifname);
767	nbi.addr = *addr;
768	if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
769		if (warning)
770			warn("ioctl(SIOCGNBRINFO_IN6)");
771		close(s);
772		return(NULL);
773	}
774
775	close(s);
776	return(&nbi);
777}
778
779static char *
780ether_str(struct sockaddr_dl *sdl)
781{
782	static char hbuf[NI_MAXHOST];
783	char *cp;
784
785	if (sdl->sdl_alen == ETHER_ADDR_LEN) {
786		strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)),
787		    sizeof(hbuf));
788	} else if (sdl->sdl_alen) {
789		int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
790		snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n);
791	} else
792		snprintf(hbuf, sizeof(hbuf), "(incomplete)");
793
794	return(hbuf);
795}
796
797int
798ndp_ether_aton(char *a, u_char *n)
799{
800	int i, o[6];
801
802	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
803	    &o[3], &o[4], &o[5]);
804	if (i != 6) {
805		fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
806		return (1);
807	}
808	for (i = 0; i < 6; i++)
809		n[i] = o[i];
810	return (0);
811}
812
813void
814usage()
815{
816	printf("usage: ndp [-nt] hostname\n");
817	printf("       ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n");
818	printf("       ndp [-nt] -A wait\n");
819	printf("       ndp [-nt] -d hostname\n");
820	printf("       ndp [-nt] -f filename\n");
821	printf("       ndp [-nt] -i interface [flags...]\n");
822#ifdef SIOCSDEFIFACE_IN6
823	printf("       ndp [-nt] -I [interface|delete]\n");
824#endif
825	printf("       ndp [-nt] -s nodename etheraddr [temp] [proxy]\n");
826	exit(1);
827}
828
829int
830rtmsg(int cmd)
831{
832	static int seq;
833	int rlen;
834	register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
835	register char *cp = m_rtmsg.m_space;
836	register int l;
837
838	errno = 0;
839	if (cmd == RTM_DELETE)
840		goto doit;
841	bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
842	rtm->rtm_flags = flags;
843	rtm->rtm_version = RTM_VERSION;
844
845	switch (cmd) {
846	default:
847		fprintf(stderr, "ndp: internal wrong cmd\n");
848		exit(1);
849	case RTM_ADD:
850		rtm->rtm_addrs |= RTA_GATEWAY;
851		if (expire_time) {
852			rtm->rtm_rmx.rmx_expire = expire_time;
853			rtm->rtm_inits = RTV_EXPIRE;
854		}
855		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
856#if 0 /* we don't support ipv6addr/128 type proxying */
857		if (rtm->rtm_flags & RTF_ANNOUNCE) {
858			rtm->rtm_flags &= ~RTF_HOST;
859			rtm->rtm_addrs |= RTA_NETMASK;
860		}
861#endif
862		/* FALLTHROUGH */
863	case RTM_GET:
864		rtm->rtm_addrs |= RTA_DST;
865	}
866
867	NEXTADDR(RTA_DST, sin_m);
868	NEXTADDR(RTA_GATEWAY, sdl_m);
869#if 0 /* we don't support ipv6addr/128 type proxying */
870	memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr));
871	NEXTADDR(RTA_NETMASK, so_mask);
872#endif
873
874	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
875doit:
876	l = rtm->rtm_msglen;
877	rtm->rtm_seq = ++seq;
878	rtm->rtm_type = cmd;
879	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
880		if (errno != ESRCH || cmd != RTM_DELETE) {
881			err(1, "writing to routing socket");
882			/* NOTREACHED */
883		}
884	}
885	do {
886		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
887	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
888	if (l < 0)
889		(void) fprintf(stderr, "ndp: read from routing socket: %s\n",
890		    strerror(errno));
891	return (0);
892}
893
894void
895ifinfo(char *ifname, int argc, char **argv)
896{
897	struct in6_ndireq nd;
898	int i, s;
899	u_int32_t newflags;
900#ifdef IPV6CTL_USETEMPADDR
901	u_int8_t nullbuf[8];
902#endif
903
904	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
905		err(1, "socket");
906		/* NOTREACHED */
907	}
908	bzero(&nd, sizeof(nd));
909	strlcpy(nd.ifname, ifname, sizeof(nd.ifname));
910	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
911		err(1, "ioctl(SIOCGIFINFO_IN6)");
912		/* NOTREACHED */
913	}
914#define ND nd.ndi
915	newflags = ND.flags;
916	for (i = 0; i < argc; i++) {
917		int clear = 0;
918		char *cp = argv[i];
919
920		if (*cp == '-') {
921			clear = 1;
922			cp++;
923		}
924
925#define SETFLAG(s, f) \
926	do {\
927		if (strcmp(cp, (s)) == 0) {\
928			if (clear)\
929				newflags &= ~(f);\
930			else\
931				newflags |= (f);\
932		}\
933	} while (0)
934/*
935 * XXX: this macro is not 100% correct, in that it matches "nud" against
936 *      "nudbogus".  But we just let it go since this is minor.
937 */
938#define SETVALUE(f, v) \
939	do { \
940		char *valptr; \
941		unsigned long newval; \
942		v = 0; /* unspecified */ \
943		if (strncmp(cp, f, strlen(f)) == 0) { \
944			valptr = strchr(cp, '='); \
945			if (valptr == NULL) \
946				err(1, "syntax error in %s field", (f)); \
947			errno = 0; \
948			newval = strtoul(++valptr, NULL, 0); \
949			if (errno) \
950				err(1, "syntax error in %s's value", (f)); \
951			v = newval; \
952		} \
953	} while (0)
954
955		SETFLAG("disabled", ND6_IFF_IFDISABLED);
956		SETFLAG("nud", ND6_IFF_PERFORMNUD);
957#ifdef ND6_IFF_ACCEPT_RTADV
958		SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV);
959#endif
960#ifdef ND6_IFF_AUTO_LINKLOCAL
961		SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL);
962#endif
963#ifdef ND6_IFF_NO_PREFER_IFACE
964		SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE);
965#endif
966		SETVALUE("basereachable", ND.basereachable);
967		SETVALUE("retrans", ND.retrans);
968		SETVALUE("curhlim", ND.chlim);
969
970		ND.flags = newflags;
971		if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) {
972			err(1, "ioctl(SIOCSIFINFO_IN6)");
973			/* NOTREACHED */
974		}
975#undef SETFLAG
976#undef SETVALUE
977	}
978
979	if (!ND.initialized) {
980		errx(1, "%s: not initialized yet", ifname);
981		/* NOTREACHED */
982	}
983
984	if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
985		err(1, "ioctl(SIOCGIFINFO_IN6)");
986		/* NOTREACHED */
987	}
988	printf("linkmtu=%d", ND.linkmtu);
989	printf(", maxmtu=%d", ND.maxmtu);
990	printf(", curhlim=%d", ND.chlim);
991	printf(", basereachable=%ds%dms",
992	    ND.basereachable / 1000, ND.basereachable % 1000);
993	printf(", reachable=%ds", ND.reachable);
994	printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000);
995#ifdef IPV6CTL_USETEMPADDR
996	memset(nullbuf, 0, sizeof(nullbuf));
997	if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) {
998		int j;
999		u_int8_t *rbuf;
1000
1001		for (i = 0; i < 3; i++) {
1002			switch (i) {
1003			case 0:
1004				printf("\nRandom seed(0): ");
1005				rbuf = ND.randomseed0;
1006				break;
1007			case 1:
1008				printf("\nRandom seed(1): ");
1009				rbuf = ND.randomseed1;
1010				break;
1011			case 2:
1012				printf("\nRandom ID:      ");
1013				rbuf = ND.randomid;
1014				break;
1015			default:
1016				errx(1, "impossible case for tempaddr display");
1017			}
1018			for (j = 0; j < 8; j++)
1019				printf("%02x", rbuf[j]);
1020		}
1021	}
1022#endif
1023	if (ND.flags) {
1024		printf("\nFlags: ");
1025#ifdef ND6_IFF_IFDISABLED
1026		if ((ND.flags & ND6_IFF_IFDISABLED))
1027			printf("disabled ");
1028#endif
1029		if ((ND.flags & ND6_IFF_PERFORMNUD))
1030			printf("nud ");
1031#ifdef ND6_IFF_ACCEPT_RTADV
1032		if ((ND.flags & ND6_IFF_ACCEPT_RTADV))
1033			printf("accept_rtadv ");
1034#endif
1035#ifdef ND6_IFF_AUTO_LINKLOCAL
1036		if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL))
1037			printf("auto_linklocal ");
1038#endif
1039#ifdef ND6_IFF_NO_PREFER_IFACE
1040		if ((ND.flags & ND6_IFF_NO_PREFER_IFACE))
1041			printf("no_prefer_iface ");
1042#endif
1043	}
1044	putc('\n', stdout);
1045#undef ND
1046
1047	close(s);
1048}
1049
1050#ifndef ND_RA_FLAG_RTPREF_MASK	/* XXX: just for compilation on *BSD release */
1051#define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
1052#endif
1053
1054void
1055rtrlist()
1056{
1057	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST };
1058	char *buf;
1059	struct in6_defrouter *p, *ep;
1060	size_t l;
1061	struct timeval now;
1062
1063	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1064		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1065		/*NOTREACHED*/
1066	}
1067	if (l == 0)
1068		return;
1069	buf = malloc(l);
1070	if (!buf) {
1071		err(1, "malloc");
1072		/*NOTREACHED*/
1073	}
1074	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1075		err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)");
1076		/*NOTREACHED*/
1077	}
1078
1079	ep = (struct in6_defrouter *)(buf + l);
1080	for (p = (struct in6_defrouter *)buf; p < ep; p++) {
1081		int rtpref;
1082
1083		if (getnameinfo((struct sockaddr *)&p->rtaddr,
1084		    p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0,
1085		    (nflag ? NI_NUMERICHOST : 0)) != 0)
1086			strlcpy(host_buf, "?", sizeof(host_buf));
1087
1088		printf("%s if=%s", host_buf,
1089		    if_indextoname(p->if_index, ifix_buf));
1090		printf(", flags=%s%s",
1091		    p->flags & ND_RA_FLAG_MANAGED ? "M" : "",
1092		    p->flags & ND_RA_FLAG_OTHER   ? "O" : "");
1093		rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff;
1094		printf(", pref=%s", rtpref_str[rtpref]);
1095
1096		gettimeofday(&now, 0);
1097		if (p->expire == 0)
1098			printf(", expire=Never\n");
1099		else
1100			printf(", expire=%s\n",
1101			    sec2str(p->expire - now.tv_sec));
1102	}
1103	free(buf);
1104}
1105
1106void
1107plist()
1108{
1109	int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST };
1110	char *buf;
1111	struct in6_prefix *p, *ep, *n;
1112	struct sockaddr_in6 *advrtr;
1113	size_t l;
1114	struct timeval now;
1115	const int niflags = NI_NUMERICHOST;
1116	int ninflags = nflag ? NI_NUMERICHOST : 0;
1117	char namebuf[NI_MAXHOST];
1118
1119	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
1120		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1121		/*NOTREACHED*/
1122	}
1123	buf = malloc(l);
1124	if (!buf) {
1125		err(1, "malloc");
1126		/*NOTREACHED*/
1127	}
1128	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
1129		err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)");
1130		/*NOTREACHED*/
1131	}
1132
1133	ep = (struct in6_prefix *)(buf + l);
1134	for (p = (struct in6_prefix *)buf; p < ep; p = n) {
1135		advrtr = (struct sockaddr_in6 *)(p + 1);
1136		n = (struct in6_prefix *)&advrtr[p->advrtrs];
1137
1138		if (getnameinfo((struct sockaddr *)&p->prefix,
1139		    p->prefix.sin6_len, namebuf, sizeof(namebuf),
1140		    NULL, 0, niflags) != 0)
1141			strlcpy(namebuf, "?", sizeof(namebuf));
1142		printf("%s/%d if=%s\n", namebuf, p->prefixlen,
1143		    if_indextoname(p->if_index, ifix_buf));
1144
1145		gettimeofday(&now, 0);
1146		/*
1147		 * meaning of fields, especially flags, is very different
1148		 * by origin.  notify the difference to the users.
1149		 */
1150		printf("flags=%s%s%s%s%s",
1151		    p->raflags.onlink ? "L" : "",
1152		    p->raflags.autonomous ? "A" : "",
1153		    (p->flags & NDPRF_ONLINK) != 0 ? "O" : "",
1154		    (p->flags & NDPRF_DETACHED) != 0 ? "D" : "",
1155#ifdef NDPRF_HOME
1156		    (p->flags & NDPRF_HOME) != 0 ? "H" : ""
1157#else
1158		    ""
1159#endif
1160		    );
1161		if (p->vltime == ND6_INFINITE_LIFETIME)
1162			printf(" vltime=infinity");
1163		else
1164			printf(" vltime=%lu", (unsigned long)p->vltime);
1165		if (p->pltime == ND6_INFINITE_LIFETIME)
1166			printf(", pltime=infinity");
1167		else
1168			printf(", pltime=%lu", (unsigned long)p->pltime);
1169		if (p->expire == 0)
1170			printf(", expire=Never");
1171		else if (p->expire >= now.tv_sec)
1172			printf(", expire=%s",
1173			    sec2str(p->expire - now.tv_sec));
1174		else
1175			printf(", expired");
1176		printf(", ref=%d", p->refcnt);
1177		printf("\n");
1178		/*
1179		 * "advertising router" list is meaningful only if the prefix
1180		 * information is from RA.
1181		 */
1182		if (p->advrtrs) {
1183			int j;
1184			struct sockaddr_in6 *sin6;
1185
1186			sin6 = advrtr;
1187			printf("  advertised by\n");
1188			for (j = 0; j < p->advrtrs; j++) {
1189				struct in6_nbrinfo *nbi;
1190
1191				if (getnameinfo((struct sockaddr *)sin6,
1192				    sin6->sin6_len, namebuf, sizeof(namebuf),
1193				    NULL, 0, ninflags) != 0)
1194					strlcpy(namebuf, "?", sizeof(namebuf));
1195				printf("    %s", namebuf);
1196
1197				nbi = getnbrinfo(&sin6->sin6_addr,
1198				    p->if_index, 0);
1199				if (nbi) {
1200					switch (nbi->state) {
1201					case ND6_LLINFO_REACHABLE:
1202					case ND6_LLINFO_STALE:
1203					case ND6_LLINFO_DELAY:
1204					case ND6_LLINFO_PROBE:
1205						printf(" (reachable)\n");
1206						break;
1207					default:
1208						printf(" (unreachable)\n");
1209					}
1210				} else
1211					printf(" (no neighbor state)\n");
1212				sin6++;
1213			}
1214		} else
1215			printf("  No advertising router\n");
1216	}
1217	free(buf);
1218}
1219
1220void
1221pfx_flush()
1222{
1223	char dummyif[IFNAMSIZ+8];
1224	int s;
1225
1226	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1227		err(1, "socket");
1228	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1229	if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
1230		err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
1231}
1232
1233void
1234rtr_flush()
1235{
1236	char dummyif[IFNAMSIZ+8];
1237	int s;
1238
1239	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1240		err(1, "socket");
1241	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1242	if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
1243		err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
1244
1245	close(s);
1246}
1247
1248void
1249harmonize_rtr()
1250{
1251	char dummyif[IFNAMSIZ+8];
1252	int s;
1253
1254	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1255		err(1, "socket");
1256	strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */
1257	if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0)
1258		err(1, "ioctl(SIOCSNDFLUSH_IN6)");
1259
1260	close(s);
1261}
1262
1263#ifdef SIOCSDEFIFACE_IN6	/* XXX: check SIOCGDEFIFACE_IN6 as well? */
1264static void
1265setdefif(char *ifname)
1266{
1267	struct in6_ndifreq ndifreq;
1268	unsigned int ifindex;
1269
1270	if (strcasecmp(ifname, "delete") == 0)
1271		ifindex = 0;
1272	else {
1273		if ((ifindex = if_nametoindex(ifname)) == 0)
1274			err(1, "failed to resolve i/f index for %s", ifname);
1275	}
1276
1277	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1278		err(1, "socket");
1279
1280	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1281	ndifreq.ifindex = ifindex;
1282
1283	if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1284		err(1, "ioctl(SIOCSDEFIFACE_IN6)");
1285
1286	close(s);
1287}
1288
1289static void
1290getdefif()
1291{
1292	struct in6_ndifreq ndifreq;
1293	char ifname[IFNAMSIZ+8];
1294
1295	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1296		err(1, "socket");
1297
1298	memset(&ndifreq, 0, sizeof(ndifreq));
1299	strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */
1300
1301	if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0)
1302		err(1, "ioctl(SIOCGDEFIFACE_IN6)");
1303
1304	if (ndifreq.ifindex == 0)
1305		printf("No default interface.\n");
1306	else {
1307		if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL)
1308			err(1, "failed to resolve ifname for index %lu",
1309			    ndifreq.ifindex);
1310		printf("ND default interface = %s\n", ifname);
1311	}
1312
1313	close(s);
1314}
1315#endif
1316
1317static char *
1318sec2str(time_t total)
1319{
1320	static char result[256];
1321	int days, hours, mins, secs;
1322	int first = 1;
1323	char *p = result;
1324	char *ep = &result[sizeof(result)];
1325	int n;
1326
1327	days = total / 3600 / 24;
1328	hours = (total / 3600) % 24;
1329	mins = (total / 60) % 60;
1330	secs = total % 60;
1331
1332	if (days) {
1333		first = 0;
1334		n = snprintf(p, ep - p, "%dd", days);
1335		if (n < 0 || n >= ep - p)
1336			return "?";
1337		p += n;
1338	}
1339	if (!first || hours) {
1340		first = 0;
1341		n = snprintf(p, ep - p, "%dh", hours);
1342		if (n < 0 || n >= ep - p)
1343			return "?";
1344		p += n;
1345	}
1346	if (!first || mins) {
1347		first = 0;
1348		n = snprintf(p, ep - p, "%dm", mins);
1349		if (n < 0 || n >= ep - p)
1350			return "?";
1351		p += n;
1352	}
1353	snprintf(p, ep - p, "%ds", secs);
1354
1355	return(result);
1356}
1357
1358/*
1359 * Print the timestamp
1360 * from tcpdump/util.c
1361 */
1362static void
1363ts_print(const struct timeval *tvp)
1364{
1365	int s;
1366
1367	/* Default */
1368	s = (tvp->tv_sec + thiszone) % 86400;
1369	(void)printf("%02d:%02d:%02d.%06u ",
1370	    s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
1371}
1372
1373#undef NEXTADDR
1374