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