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