Home | History | Annotate | Line # | Download | only in netinet6
nd6.c revision 1.205
      1  1.205     ozaki /*	$NetBSD: nd6.c,v 1.205 2016/08/01 03:15:31 ozaki-r Exp $	*/
      2   1.65    itojun /*	$KAME: nd6.c,v 1.279 2002/06/08 11:16:51 itojun Exp $	*/
      3    1.4   thorpej 
      4    1.2    itojun /*
      5    1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6    1.2    itojun  * All rights reserved.
      7   1.25    itojun  *
      8    1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9    1.2    itojun  * modification, are permitted provided that the following conditions
     10    1.2    itojun  * are met:
     11    1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12    1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13    1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15    1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16    1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17    1.2    itojun  *    may be used to endorse or promote products derived from this software
     18    1.2    itojun  *    without specific prior written permission.
     19   1.25    itojun  *
     20    1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21    1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22    1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23    1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24    1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25    1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26    1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27    1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28    1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29    1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30    1.2    itojun  * SUCH DAMAGE.
     31    1.2    itojun  */
     32    1.2    itojun 
     33   1.55     lukem #include <sys/cdefs.h>
     34  1.205     ozaki __KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.205 2016/08/01 03:15:31 ozaki-r Exp $");
     35  1.162     ozaki 
     36  1.169     pooka #ifdef _KERNEL_OPT
     37  1.162     ozaki #include "opt_net_mpsafe.h"
     38  1.169     pooka #endif
     39   1.87    itojun 
     40  1.153       roy #include "bridge.h"
     41  1.153       roy #include "carp.h"
     42    1.2    itojun 
     43    1.2    itojun #include <sys/param.h>
     44    1.2    itojun #include <sys/systm.h>
     45   1.20   thorpej #include <sys/callout.h>
     46    1.2    itojun #include <sys/malloc.h>
     47    1.2    itojun #include <sys/mbuf.h>
     48    1.2    itojun #include <sys/socket.h>
     49  1.126        ad #include <sys/socketvar.h>
     50    1.2    itojun #include <sys/sockio.h>
     51    1.2    itojun #include <sys/time.h>
     52    1.2    itojun #include <sys/kernel.h>
     53   1.18    itojun #include <sys/protosw.h>
     54    1.2    itojun #include <sys/errno.h>
     55    1.2    itojun #include <sys/ioctl.h>
     56    1.2    itojun #include <sys/syslog.h>
     57    1.2    itojun #include <sys/queue.h>
     58  1.138       tls #include <sys/cprng.h>
     59  1.203     ozaki #include <sys/workqueue.h>
     60    1.2    itojun 
     61    1.2    itojun #include <net/if.h>
     62    1.2    itojun #include <net/if_dl.h>
     63  1.181     ozaki #include <net/if_llatbl.h>
     64    1.2    itojun #include <net/if_types.h>
     65    1.2    itojun #include <net/route.h>
     66   1.62    itojun #include <net/if_ether.h>
     67   1.62    itojun #include <net/if_fddi.h>
     68   1.62    itojun #include <net/if_arc.h>
     69    1.2    itojun 
     70    1.2    itojun #include <netinet/in.h>
     71    1.2    itojun #include <netinet6/in6_var.h>
     72   1.17    itojun #include <netinet/ip6.h>
     73    1.2    itojun #include <netinet6/ip6_var.h>
     74   1.96    rpaulo #include <netinet6/scope6_var.h>
     75    1.2    itojun #include <netinet6/nd6.h>
     76  1.151       roy #include <netinet6/in6_ifattach.h>
     77   1.17    itojun #include <netinet/icmp6.h>
     78  1.125   thorpej #include <netinet6/icmp6_private.h>
     79   1.87    itojun 
     80   1.12    itojun #include <net/net_osdep.h>
     81   1.12    itojun 
     82    1.2    itojun #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
     83    1.2    itojun #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
     84    1.2    itojun 
     85    1.2    itojun /* timer values */
     86    1.2    itojun int	nd6_prune	= 1;	/* walk list every 1 seconds */
     87    1.2    itojun int	nd6_delay	= 5;	/* delay first probe time 5 second */
     88    1.2    itojun int	nd6_umaxtries	= 3;	/* maximum unicast query */
     89    1.2    itojun int	nd6_mmaxtries	= 3;	/* maximum multicast query */
     90    1.2    itojun int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
     91   1.42    itojun int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
     92    1.2    itojun 
     93   1.12    itojun /* preventing too many loops in ND option parsing */
     94   1.12    itojun int nd6_maxndopt = 10;	/* max # of ND options allowed */
     95   1.12    itojun 
     96   1.31    itojun int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
     97   1.31    itojun 
     98   1.99    rpaulo int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
     99   1.99    rpaulo 
    100   1.36    itojun #ifdef ND6_DEBUG
    101   1.36    itojun int nd6_debug = 1;
    102   1.36    itojun #else
    103   1.36    itojun int nd6_debug = 0;
    104   1.36    itojun #endif
    105   1.36    itojun 
    106   1.12    itojun struct nd_drhead nd_defrouter;
    107    1.2    itojun struct nd_prhead nd_prefix = { 0 };
    108    1.2    itojun 
    109    1.2    itojun int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
    110    1.2    itojun 
    111  1.121    dyoung static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
    112  1.121    dyoung static void nd6_slowtimo(void *);
    113  1.201     ozaki static int regen_tmpaddr(const struct in6_ifaddr *);
    114  1.188     ozaki static void nd6_free(struct llentry *, int);
    115  1.121    dyoung static void nd6_llinfo_timer(void *);
    116  1.186     ozaki static void nd6_timer(void *);
    117  1.203     ozaki static void nd6_timer_work(struct work *, void *);
    118  1.181     ozaki static void clear_llinfo_pqueue(struct llentry *);
    119    1.2    itojun 
    120  1.186     ozaki static callout_t nd6_slowtimo_ch;
    121  1.186     ozaki static callout_t nd6_timer_ch;
    122  1.203     ozaki static struct workqueue	*nd6_timer_wq;
    123  1.203     ozaki static struct work	nd6_timer_wk;
    124   1.20   thorpej 
    125  1.121    dyoung static int fill_drlist(void *, size_t *, size_t);
    126  1.121    dyoung static int fill_prlist(void *, size_t *, size_t);
    127   1.79   thorpej 
    128   1.79   thorpej MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
    129   1.65    itojun 
    130    1.2    itojun void
    131  1.112    dyoung nd6_init(void)
    132    1.2    itojun {
    133  1.203     ozaki 	int error;
    134   1.12    itojun 
    135   1.12    itojun 	/* initialization of the default router list */
    136   1.12    itojun 	TAILQ_INIT(&nd_defrouter);
    137   1.12    itojun 
    138  1.126        ad 	callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE);
    139  1.126        ad 	callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
    140  1.116        ad 
    141  1.203     ozaki 	error = workqueue_create(&nd6_timer_wq, "nd6_timer",
    142  1.203     ozaki 	    nd6_timer_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
    143  1.203     ozaki 	if (error)
    144  1.203     ozaki 		panic("%s: workqueue_create failed (%d)\n", __func__, error);
    145  1.203     ozaki 
    146    1.2    itojun 	/* start timer */
    147   1.20   thorpej 	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
    148   1.20   thorpej 	    nd6_slowtimo, NULL);
    149  1.186     ozaki 	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
    150    1.2    itojun }
    151    1.2    itojun 
    152   1.58    itojun struct nd_ifinfo *
    153  1.112    dyoung nd6_ifattach(struct ifnet *ifp)
    154    1.2    itojun {
    155   1.58    itojun 	struct nd_ifinfo *nd;
    156    1.2    itojun 
    157  1.141  christos 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
    158    1.2    itojun 
    159   1.58    itojun 	nd->initialized = 1;
    160    1.2    itojun 
    161   1.58    itojun 	nd->chlim = IPV6_DEFHLIM;
    162   1.58    itojun 	nd->basereachable = REACHABLE_TIME;
    163   1.58    itojun 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
    164   1.58    itojun 	nd->retrans = RETRANS_TIMER;
    165  1.151       roy 
    166  1.135    dyoung 	nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV;
    167   1.61    itojun 
    168  1.151       roy 	/* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
    169  1.151       roy 	 * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL
    170  1.154       snj 	 * because one of its members should. */
    171  1.151       roy 	if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
    172  1.151       roy 	    (ifp->if_flags & IFF_LOOPBACK))
    173  1.151       roy 		nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
    174  1.151       roy 
    175  1.151       roy 	/* A loopback interface does not need to accept RTADV.
    176  1.151       roy 	 * A bridge interface should not accept RTADV
    177  1.154       snj 	 * because one of its members should. */
    178  1.151       roy 	if (ip6_accept_rtadv &&
    179  1.151       roy 	    !(ifp->if_flags & IFF_LOOPBACK) &&
    180  1.151       roy 	    !(ifp->if_type != IFT_BRIDGE))
    181  1.151       roy 		nd->flags |= ND6_IFF_ACCEPT_RTADV;
    182  1.151       roy 
    183   1.61    itojun 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
    184   1.61    itojun 	nd6_setmtu0(ifp, nd);
    185    1.2    itojun 
    186   1.58    itojun 	return nd;
    187   1.58    itojun }
    188   1.21    itojun 
    189   1.58    itojun void
    190  1.158    martin nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext)
    191   1.58    itojun {
    192   1.21    itojun 
    193  1.158    martin 	nd6_purge(ifp, ext);
    194  1.158    martin 	free(ext->nd_ifinfo, M_IP6NDP);
    195    1.2    itojun }
    196    1.2    itojun 
    197    1.2    itojun void
    198  1.112    dyoung nd6_setmtu(struct ifnet *ifp)
    199   1.61    itojun {
    200   1.61    itojun 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
    201   1.61    itojun }
    202   1.61    itojun 
    203   1.61    itojun void
    204  1.112    dyoung nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
    205    1.2    itojun {
    206   1.61    itojun 	u_int32_t omaxmtu;
    207   1.61    itojun 
    208   1.61    itojun 	omaxmtu = ndi->maxmtu;
    209    1.2    itojun 
    210   1.40    itojun 	switch (ifp->if_type) {
    211   1.62    itojun 	case IFT_ARCNET:
    212   1.62    itojun 		ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
    213   1.39    itojun 		break;
    214   1.58    itojun 	case IFT_FDDI:
    215   1.58    itojun 		ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
    216   1.58    itojun 		break;
    217   1.39    itojun 	default:
    218   1.39    itojun 		ndi->maxmtu = ifp->if_mtu;
    219   1.39    itojun 		break;
    220    1.2    itojun 	}
    221    1.2    itojun 
    222   1.61    itojun 	/*
    223   1.61    itojun 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
    224   1.61    itojun 	 * undesirable situation.  We thus notify the operator of the change
    225   1.61    itojun 	 * explicitly.  The check for omaxmtu is necessary to restrict the
    226   1.63    itojun 	 * log to the case of changing the MTU, not initializing it.
    227   1.61    itojun 	 */
    228   1.61    itojun 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
    229   1.81  christos 		log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too"
    230   1.81  christos 		    " small for IPv6 which needs %lu\n",
    231   1.81  christos 		    if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long)
    232   1.81  christos 		    IPV6_MMTU);
    233    1.2    itojun 	}
    234   1.58    itojun 
    235   1.58    itojun 	if (ndi->maxmtu > in6_maxmtu)
    236   1.58    itojun 		in6_setmaxmtu(); /* check all interfaces just in case */
    237    1.2    itojun }
    238    1.2    itojun 
    239    1.2    itojun void
    240  1.112    dyoung nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
    241    1.2    itojun {
    242   1.58    itojun 
    243  1.129    dyoung 	memset(ndopts, 0, sizeof(*ndopts));
    244    1.2    itojun 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
    245    1.2    itojun 	ndopts->nd_opts_last
    246    1.2    itojun 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
    247    1.2    itojun 
    248    1.2    itojun 	if (icmp6len == 0) {
    249    1.2    itojun 		ndopts->nd_opts_done = 1;
    250    1.2    itojun 		ndopts->nd_opts_search = NULL;
    251    1.2    itojun 	}
    252    1.2    itojun }
    253    1.2    itojun 
    254    1.2    itojun /*
    255    1.2    itojun  * Take one ND option.
    256    1.2    itojun  */
    257    1.2    itojun struct nd_opt_hdr *
    258  1.112    dyoung nd6_option(union nd_opts *ndopts)
    259    1.2    itojun {
    260    1.2    itojun 	struct nd_opt_hdr *nd_opt;
    261    1.2    itojun 	int olen;
    262    1.2    itojun 
    263  1.163     ozaki 	KASSERT(ndopts != NULL);
    264  1.163     ozaki 	KASSERT(ndopts->nd_opts_last != NULL);
    265  1.163     ozaki 
    266   1.99    rpaulo 	if (ndopts->nd_opts_search == NULL)
    267    1.2    itojun 		return NULL;
    268    1.2    itojun 	if (ndopts->nd_opts_done)
    269    1.2    itojun 		return NULL;
    270    1.2    itojun 
    271    1.2    itojun 	nd_opt = ndopts->nd_opts_search;
    272    1.2    itojun 
    273   1.40    itojun 	/* make sure nd_opt_len is inside the buffer */
    274  1.111  christos 	if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) {
    275  1.129    dyoung 		memset(ndopts, 0, sizeof(*ndopts));
    276   1.40    itojun 		return NULL;
    277   1.40    itojun 	}
    278   1.40    itojun 
    279    1.2    itojun 	olen = nd_opt->nd_opt_len << 3;
    280    1.2    itojun 	if (olen == 0) {
    281    1.2    itojun 		/*
    282    1.2    itojun 		 * Message validation requires that all included
    283    1.2    itojun 		 * options have a length that is greater than zero.
    284    1.2    itojun 		 */
    285  1.129    dyoung 		memset(ndopts, 0, sizeof(*ndopts));
    286    1.2    itojun 		return NULL;
    287    1.2    itojun 	}
    288    1.2    itojun 
    289  1.111  christos 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen);
    290   1.40    itojun 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
    291   1.40    itojun 		/* option overruns the end of buffer, invalid */
    292  1.129    dyoung 		memset(ndopts, 0, sizeof(*ndopts));
    293   1.40    itojun 		return NULL;
    294   1.40    itojun 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
    295   1.40    itojun 		/* reached the end of options chain */
    296    1.2    itojun 		ndopts->nd_opts_done = 1;
    297    1.2    itojun 		ndopts->nd_opts_search = NULL;
    298    1.2    itojun 	}
    299    1.2    itojun 	return nd_opt;
    300    1.2    itojun }
    301    1.2    itojun 
    302    1.2    itojun /*
    303    1.2    itojun  * Parse multiple ND options.
    304    1.2    itojun  * This function is much easier to use, for ND routines that do not need
    305    1.2    itojun  * multiple options of the same type.
    306    1.2    itojun  */
    307    1.2    itojun int
    308  1.112    dyoung nd6_options(union nd_opts *ndopts)
    309    1.2    itojun {
    310    1.2    itojun 	struct nd_opt_hdr *nd_opt;
    311    1.2    itojun 	int i = 0;
    312    1.2    itojun 
    313  1.163     ozaki 	KASSERT(ndopts != NULL);
    314  1.163     ozaki 	KASSERT(ndopts->nd_opts_last != NULL);
    315  1.163     ozaki 
    316   1.99    rpaulo 	if (ndopts->nd_opts_search == NULL)
    317    1.2    itojun 		return 0;
    318   1.99    rpaulo 
    319    1.2    itojun 	while (1) {
    320    1.2    itojun 		nd_opt = nd6_option(ndopts);
    321   1.99    rpaulo 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
    322    1.2    itojun 			/*
    323    1.2    itojun 			 * Message validation requires that all included
    324    1.2    itojun 			 * options have a length that is greater than zero.
    325    1.2    itojun 			 */
    326  1.125   thorpej 			ICMP6_STATINC(ICMP6_STAT_ND_BADOPT);
    327  1.129    dyoung 			memset(ndopts, 0, sizeof(*ndopts));
    328    1.2    itojun 			return -1;
    329    1.2    itojun 		}
    330    1.2    itojun 
    331   1.99    rpaulo 		if (nd_opt == NULL)
    332    1.2    itojun 			goto skip1;
    333    1.2    itojun 
    334    1.2    itojun 		switch (nd_opt->nd_opt_type) {
    335    1.2    itojun 		case ND_OPT_SOURCE_LINKADDR:
    336    1.2    itojun 		case ND_OPT_TARGET_LINKADDR:
    337    1.2    itojun 		case ND_OPT_MTU:
    338    1.2    itojun 		case ND_OPT_REDIRECTED_HEADER:
    339    1.2    itojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
    340  1.187     ozaki 				nd6log(LOG_INFO,
    341   1.36    itojun 				    "duplicated ND6 option found (type=%d)\n",
    342  1.187     ozaki 				    nd_opt->nd_opt_type);
    343    1.2    itojun 				/* XXX bark? */
    344    1.2    itojun 			} else {
    345    1.2    itojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
    346    1.2    itojun 					= nd_opt;
    347    1.2    itojun 			}
    348    1.2    itojun 			break;
    349    1.2    itojun 		case ND_OPT_PREFIX_INFORMATION:
    350    1.2    itojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
    351    1.2    itojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
    352    1.2    itojun 					= nd_opt;
    353    1.2    itojun 			}
    354    1.2    itojun 			ndopts->nd_opts_pi_end =
    355    1.2    itojun 				(struct nd_opt_prefix_info *)nd_opt;
    356    1.2    itojun 			break;
    357    1.2    itojun 		default:
    358    1.2    itojun 			/*
    359    1.2    itojun 			 * Unknown options must be silently ignored,
    360  1.109  christos 			 * to accommodate future extension to the protocol.
    361    1.2    itojun 			 */
    362  1.187     ozaki 			nd6log(LOG_DEBUG,
    363    1.2    itojun 			    "nd6_options: unsupported option %d - "
    364  1.187     ozaki 			    "option ignored\n", nd_opt->nd_opt_type);
    365    1.2    itojun 		}
    366    1.2    itojun 
    367    1.2    itojun skip1:
    368    1.2    itojun 		i++;
    369   1.12    itojun 		if (i > nd6_maxndopt) {
    370  1.125   thorpej 			ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT);
    371  1.187     ozaki 			nd6log(LOG_INFO, "too many loop in nd opt\n");
    372    1.2    itojun 			break;
    373    1.2    itojun 		}
    374    1.2    itojun 
    375    1.2    itojun 		if (ndopts->nd_opts_done)
    376    1.2    itojun 			break;
    377    1.2    itojun 	}
    378    1.2    itojun 
    379    1.2    itojun 	return 0;
    380    1.2    itojun }
    381    1.2    itojun 
    382    1.2    itojun /*
    383   1.86    itojun  * ND6 timer routine to handle ND6 entries
    384    1.2    itojun  */
    385    1.2    itojun void
    386  1.188     ozaki nd6_llinfo_settimer(struct llentry *ln, time_t xtick)
    387   1.86    itojun {
    388   1.86    itojun 
    389  1.182     ozaki 	CTASSERT(sizeof(time_t) > sizeof(int));
    390  1.181     ozaki 	LLE_WLOCK_ASSERT(ln);
    391   1.86    itojun 
    392   1.94  christos 	if (xtick < 0) {
    393   1.86    itojun 		ln->ln_expire = 0;
    394   1.86    itojun 		ln->ln_ntick = 0;
    395  1.181     ozaki 		callout_halt(&ln->ln_timer_ch, &ln->lle_lock);
    396   1.86    itojun 	} else {
    397  1.166     ozaki 		ln->ln_expire = time_uptime + xtick / hz;
    398  1.181     ozaki 		LLE_ADDREF(ln);
    399   1.94  christos 		if (xtick > INT_MAX) {
    400   1.94  christos 			ln->ln_ntick = xtick - INT_MAX;
    401   1.86    itojun 			callout_reset(&ln->ln_timer_ch, INT_MAX,
    402   1.86    itojun 			    nd6_llinfo_timer, ln);
    403   1.86    itojun 		} else {
    404   1.86    itojun 			ln->ln_ntick = 0;
    405   1.94  christos 			callout_reset(&ln->ln_timer_ch, xtick,
    406   1.86    itojun 			    nd6_llinfo_timer, ln);
    407   1.86    itojun 		}
    408   1.86    itojun 	}
    409  1.181     ozaki }
    410  1.181     ozaki 
    411  1.179     ozaki /*
    412  1.179     ozaki  * Gets source address of the first packet in hold queue
    413  1.179     ozaki  * and stores it in @src.
    414  1.179     ozaki  * Returns pointer to @src (if hold queue is not empty) or NULL.
    415  1.179     ozaki  */
    416  1.179     ozaki static struct in6_addr *
    417  1.181     ozaki nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
    418  1.179     ozaki {
    419  1.179     ozaki 	struct ip6_hdr *hip6;
    420  1.179     ozaki 
    421  1.179     ozaki 	if (ln == NULL || ln->ln_hold == NULL)
    422  1.179     ozaki 		return NULL;
    423  1.179     ozaki 
    424  1.179     ozaki 	/*
    425  1.179     ozaki 	 * assuming every packet in ln_hold has the same IP header
    426  1.179     ozaki 	 */
    427  1.179     ozaki 	hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
    428  1.179     ozaki 	/* XXX pullup? */
    429  1.179     ozaki 	if (sizeof(*hip6) < ln->ln_hold->m_len)
    430  1.179     ozaki 		*src = hip6->ip6_src;
    431  1.179     ozaki 	else
    432  1.179     ozaki 		src = NULL;
    433  1.179     ozaki 
    434  1.179     ozaki 	return src;
    435  1.179     ozaki }
    436  1.179     ozaki 
    437   1.86    itojun static void
    438  1.112    dyoung nd6_llinfo_timer(void *arg)
    439    1.2    itojun {
    440  1.181     ozaki 	struct llentry *ln = arg;
    441   1.65    itojun 	struct ifnet *ifp;
    442   1.86    itojun 	struct nd_ifinfo *ndi = NULL;
    443  1.178     ozaki 	bool send_ns = false;
    444  1.178     ozaki 	const struct in6_addr *daddr6 = NULL;
    445   1.63    itojun 
    446  1.126        ad 	mutex_enter(softnet_lock);
    447  1.126        ad 	KERNEL_LOCK(1, NULL);
    448    1.2    itojun 
    449  1.181     ozaki 	LLE_WLOCK(ln);
    450  1.181     ozaki 	if (ln->ln_ntick > 0) {
    451  1.188     ozaki 		nd6_llinfo_settimer(ln, ln->ln_ntick);
    452  1.181     ozaki 		goto out;
    453  1.181     ozaki 	}
    454   1.86    itojun 
    455  1.181     ozaki 	if (callout_pending(&ln->la_timer)) {
    456  1.181     ozaki 		/*
    457  1.181     ozaki 		 * Here we are a bit odd here in the treatment of
    458  1.181     ozaki 		 * active/pending. If the pending bit is set, it got
    459  1.181     ozaki 		 * rescheduled before I ran. The active
    460  1.181     ozaki 		 * bit we ignore, since if it was stopped
    461  1.181     ozaki 		 * in ll_tablefree() and was currently running
    462  1.181     ozaki 		 * it would have return 0 so the code would
    463  1.181     ozaki 		 * not have deleted it since the callout could
    464  1.181     ozaki 		 * not be stopped so we want to go through
    465  1.181     ozaki 		 * with the delete here now. If the callout
    466  1.181     ozaki 		 * was restarted, the pending bit will be back on and
    467  1.181     ozaki 		 * we just want to bail since the callout_reset would
    468  1.181     ozaki 		 * return 1 and our reference would have been removed
    469  1.188     ozaki 		 * by nd6_llinfo_settimer above since canceled
    470  1.181     ozaki 		 * would have been 1.
    471  1.181     ozaki 		 */
    472  1.181     ozaki 		goto out;
    473   1.86    itojun 	}
    474   1.86    itojun 
    475  1.181     ozaki 	ifp = ln->lle_tbl->llt_ifp;
    476  1.181     ozaki 
    477  1.173     ozaki 	KASSERT(ifp != NULL);
    478  1.173     ozaki 
    479   1.86    itojun 	ndi = ND_IFINFO(ifp);
    480   1.86    itojun 
    481   1.86    itojun 	switch (ln->ln_state) {
    482   1.86    itojun 	case ND6_LLINFO_INCOMPLETE:
    483   1.86    itojun 		if (ln->ln_asked < nd6_mmaxtries) {
    484   1.86    itojun 			ln->ln_asked++;
    485  1.178     ozaki 			send_ns = true;
    486   1.86    itojun 		} else {
    487   1.86    itojun 			struct mbuf *m = ln->ln_hold;
    488   1.86    itojun 			if (m) {
    489  1.100    rpaulo 				struct mbuf *m0;
    490  1.100    rpaulo 
    491   1.86    itojun 				/*
    492   1.96    rpaulo 				 * assuming every packet in ln_hold has
    493   1.96    rpaulo 				 * the same IP header
    494   1.86    itojun 				 */
    495  1.100    rpaulo 				m0 = m->m_nextpkt;
    496  1.100    rpaulo 				m->m_nextpkt = NULL;
    497  1.100    rpaulo 				ln->ln_hold = m0;
    498  1.100    rpaulo 				clear_llinfo_pqueue(ln);
    499  1.100    rpaulo  			}
    500  1.188     ozaki 			nd6_free(ln, 0);
    501   1.86    itojun 			ln = NULL;
    502  1.180     ozaki 			if (m != NULL)
    503  1.180     ozaki 				icmp6_error2(m, ICMP6_DST_UNREACH,
    504  1.180     ozaki 				    ICMP6_DST_UNREACH_ADDR, 0, ifp);
    505    1.2    itojun 		}
    506   1.86    itojun 		break;
    507   1.86    itojun 	case ND6_LLINFO_REACHABLE:
    508   1.86    itojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
    509   1.86    itojun 			ln->ln_state = ND6_LLINFO_STALE;
    510  1.188     ozaki 			nd6_llinfo_settimer(ln, nd6_gctimer * hz);
    511    1.2    itojun 		}
    512   1.86    itojun 		break;
    513    1.2    itojun 
    514  1.143  christos 	case ND6_LLINFO_PURGE:
    515   1.86    itojun 	case ND6_LLINFO_STALE:
    516   1.86    itojun 		/* Garbage Collection(RFC 2461 5.3) */
    517   1.86    itojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
    518  1.188     ozaki 			nd6_free(ln, 1);
    519   1.86    itojun 			ln = NULL;
    520    1.2    itojun 		}
    521   1.86    itojun 		break;
    522   1.54    itojun 
    523   1.86    itojun 	case ND6_LLINFO_DELAY:
    524   1.86    itojun 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
    525   1.86    itojun 			/* We need NUD */
    526   1.86    itojun 			ln->ln_asked = 1;
    527   1.86    itojun 			ln->ln_state = ND6_LLINFO_PROBE;
    528  1.188     ozaki 			daddr6 = &ln->r_l3addr.addr6;
    529  1.178     ozaki 			send_ns = true;
    530   1.86    itojun 		} else {
    531   1.86    itojun 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
    532  1.188     ozaki 			nd6_llinfo_settimer(ln, nd6_gctimer * hz);
    533   1.86    itojun 		}
    534   1.86    itojun 		break;
    535   1.86    itojun 	case ND6_LLINFO_PROBE:
    536   1.86    itojun 		if (ln->ln_asked < nd6_umaxtries) {
    537   1.86    itojun 			ln->ln_asked++;
    538  1.188     ozaki 			daddr6 = &ln->r_l3addr.addr6;
    539  1.178     ozaki 			send_ns = true;
    540   1.86    itojun 		} else {
    541  1.188     ozaki 			nd6_free(ln, 0);
    542   1.86    itojun 			ln = NULL;
    543   1.86    itojun 		}
    544   1.86    itojun 		break;
    545   1.86    itojun 	}
    546   1.65    itojun 
    547  1.178     ozaki 	if (send_ns) {
    548  1.179     ozaki 		struct in6_addr src, *psrc;
    549  1.188     ozaki 		const struct in6_addr *taddr6 = &ln->r_l3addr.addr6;
    550  1.179     ozaki 
    551  1.188     ozaki 		nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000);
    552  1.179     ozaki 		psrc = nd6_llinfo_get_holdsrc(ln, &src);
    553  1.181     ozaki 		LLE_FREE_LOCKED(ln);
    554  1.181     ozaki 		ln = NULL;
    555  1.188     ozaki 		nd6_ns_output(ifp, daddr6, taddr6, psrc, 0);
    556  1.178     ozaki 	}
    557  1.178     ozaki 
    558  1.181     ozaki out:
    559  1.181     ozaki 	if (ln != NULL)
    560  1.181     ozaki 		LLE_FREE_LOCKED(ln);
    561  1.126        ad 	KERNEL_UNLOCK_ONE(NULL);
    562  1.126        ad 	mutex_exit(softnet_lock);
    563   1.86    itojun }
    564   1.42    itojun 
    565   1.86    itojun /*
    566   1.86    itojun  * ND6 timer routine to expire default route list and prefix list
    567   1.86    itojun  */
    568  1.186     ozaki static void
    569  1.203     ozaki nd6_timer_work(struct work *wk, void *arg)
    570   1.86    itojun {
    571  1.108    dyoung 	struct nd_defrouter *next_dr, *dr;
    572  1.108    dyoung 	struct nd_prefix *next_pr, *pr;
    573   1.86    itojun 	struct in6_ifaddr *ia6, *nia6;
    574  1.205     ozaki 	int s, bound;
    575  1.205     ozaki 	struct psref psref;
    576   1.42    itojun 
    577   1.86    itojun 	callout_reset(&nd6_timer_ch, nd6_prune * hz,
    578   1.86    itojun 	    nd6_timer, NULL);
    579   1.63    itojun 
    580  1.126        ad 	mutex_enter(softnet_lock);
    581  1.126        ad 	KERNEL_LOCK(1, NULL);
    582  1.126        ad 
    583   1.43    itojun 	/* expire default router list */
    584  1.108    dyoung 
    585  1.140  christos 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, next_dr) {
    586  1.166     ozaki 		if (dr->expire && dr->expire < time_uptime) {
    587  1.158    martin 			defrtrlist_del(dr, NULL);
    588   1.31    itojun 		}
    589    1.2    itojun 	}
    590    1.2    itojun 
    591   1.65    itojun 	/*
    592   1.65    itojun 	 * expire interface addresses.
    593   1.65    itojun 	 * in the past the loop was inside prefix expiry processing.
    594   1.65    itojun 	 * However, from a stricter speci-confrmance standpoint, we should
    595   1.65    itojun 	 * rather separate address lifetimes and prefix lifetimes.
    596   1.65    itojun 	 */
    597  1.205     ozaki 	bound = curlwp_bind();
    598   1.99    rpaulo   addrloop:
    599  1.205     ozaki 	s = pserialize_read_enter();
    600  1.205     ozaki 	for (ia6 = IN6_ADDRLIST_READER_FIRST(); ia6; ia6 = nia6) {
    601  1.205     ozaki 		nia6 = IN6_ADDRLIST_READER_NEXT(ia6);
    602  1.205     ozaki 
    603  1.205     ozaki 		ia6_acquire(ia6, &psref);
    604  1.205     ozaki 		pserialize_read_exit(s);
    605  1.205     ozaki 
    606   1.65    itojun 		/* check address lifetime */
    607   1.65    itojun 		if (IFA6_IS_INVALID(ia6)) {
    608   1.99    rpaulo 			int regen = 0;
    609   1.99    rpaulo 
    610   1.99    rpaulo 			/*
    611   1.99    rpaulo 			 * If the expiring address is temporary, try
    612   1.99    rpaulo 			 * regenerating a new one.  This would be useful when
    613   1.99    rpaulo 			 * we suspended a laptop PC, then turned it on after a
    614   1.99    rpaulo 			 * period that could invalidate all temporary
    615   1.99    rpaulo 			 * addresses.  Although we may have to restart the
    616   1.99    rpaulo 			 * loop (see below), it must be after purging the
    617   1.99    rpaulo 			 * address.  Otherwise, we'd see an infinite loop of
    618   1.99    rpaulo 			 * regeneration.
    619   1.99    rpaulo 			 */
    620   1.99    rpaulo 			if (ip6_use_tempaddr &&
    621   1.99    rpaulo 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
    622   1.99    rpaulo 				if (regen_tmpaddr(ia6) == 0)
    623   1.99    rpaulo 					regen = 1;
    624   1.99    rpaulo 			}
    625   1.99    rpaulo 
    626  1.205     ozaki 			ia6_release(ia6, &psref);
    627   1.99    rpaulo  			in6_purgeaddr(&ia6->ia_ifa);
    628  1.205     ozaki 			ia6 = NULL;
    629   1.99    rpaulo 
    630   1.99    rpaulo 			if (regen)
    631   1.99    rpaulo 				goto addrloop; /* XXX: see below */
    632   1.99    rpaulo 		} else if (IFA6_IS_DEPRECATED(ia6)) {
    633   1.99    rpaulo 			int oldflags = ia6->ia6_flags;
    634   1.99    rpaulo 
    635  1.145       roy 			if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
    636  1.145       roy 				ia6->ia6_flags |= IN6_IFF_DEPRECATED;
    637  1.159       roy 				rt_newaddrmsg(RTM_NEWADDR,
    638  1.159       roy 				    (struct ifaddr *)ia6, 0, NULL);
    639  1.145       roy 			}
    640   1.99    rpaulo 
    641   1.99    rpaulo 			/*
    642   1.99    rpaulo 			 * If a temporary address has just become deprecated,
    643   1.99    rpaulo 			 * regenerate a new one if possible.
    644   1.99    rpaulo 			 */
    645   1.99    rpaulo 			if (ip6_use_tempaddr &&
    646   1.99    rpaulo 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
    647   1.99    rpaulo 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
    648   1.99    rpaulo 
    649   1.99    rpaulo 				if (regen_tmpaddr(ia6) == 0) {
    650   1.99    rpaulo 					/*
    651   1.99    rpaulo 					 * A new temporary address is
    652   1.99    rpaulo 					 * generated.
    653   1.99    rpaulo 					 * XXX: this means the address chain
    654   1.99    rpaulo 					 * has changed while we are still in
    655   1.99    rpaulo 					 * the loop.  Although the change
    656   1.99    rpaulo 					 * would not cause disaster (because
    657   1.99    rpaulo 					 * it's not a deletion, but an
    658   1.99    rpaulo 					 * addition,) we'd rather restart the
    659   1.99    rpaulo 					 * loop just for safety.  Or does this
    660   1.99    rpaulo 					 * significantly reduce performance??
    661   1.99    rpaulo 					 */
    662  1.205     ozaki 					ia6_release(ia6, &psref);
    663   1.99    rpaulo 					goto addrloop;
    664   1.99    rpaulo 				}
    665   1.99    rpaulo 			}
    666   1.65    itojun 		} else {
    667   1.65    itojun 			/*
    668   1.65    itojun 			 * A new RA might have made a deprecated address
    669   1.65    itojun 			 * preferred.
    670   1.65    itojun 			 */
    671  1.145       roy 			if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
    672  1.145       roy 				ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
    673  1.159       roy 				rt_newaddrmsg(RTM_NEWADDR,
    674  1.159       roy 				    (struct ifaddr *)ia6, 0, NULL);
    675  1.145       roy 			}
    676    1.2    itojun 		}
    677  1.205     ozaki 		s = pserialize_read_enter();
    678  1.205     ozaki 		ia6_release(ia6, &psref);
    679   1.65    itojun 	}
    680  1.205     ozaki 	pserialize_read_exit(s);
    681  1.205     ozaki 	curlwp_bindx(bound);
    682    1.2    itojun 
    683   1.65    itojun 	/* expire prefix list */
    684  1.140  christos 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, next_pr) {
    685    1.2    itojun 		/*
    686    1.2    itojun 		 * check prefix lifetime.
    687    1.2    itojun 		 * since pltime is just for autoconf, pltime processing for
    688    1.2    itojun 		 * prefix is not necessary.
    689    1.2    itojun 		 */
    690   1.65    itojun 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
    691  1.166     ozaki 		    time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
    692    1.2    itojun 
    693    1.2    itojun 			/*
    694    1.2    itojun 			 * address expiration and prefix expiration are
    695   1.65    itojun 			 * separate.  NEVER perform in6_purgeaddr here.
    696    1.2    itojun 			 */
    697    1.2    itojun 
    698    1.2    itojun 			prelist_remove(pr);
    699  1.108    dyoung 		}
    700    1.2    itojun 	}
    701  1.126        ad 
    702  1.126        ad 	KERNEL_UNLOCK_ONE(NULL);
    703  1.126        ad 	mutex_exit(softnet_lock);
    704   1.16    itojun }
    705   1.16    itojun 
    706  1.203     ozaki static void
    707  1.203     ozaki nd6_timer(void *ignored_arg)
    708  1.203     ozaki {
    709  1.203     ozaki 
    710  1.203     ozaki 	workqueue_enqueue(nd6_timer_wq, &nd6_timer_wk, NULL);
    711  1.203     ozaki }
    712  1.203     ozaki 
    713  1.112    dyoung /* ia6: deprecated/invalidated temporary address */
    714   1.99    rpaulo static int
    715  1.201     ozaki regen_tmpaddr(const struct in6_ifaddr *ia6)
    716   1.99    rpaulo {
    717   1.99    rpaulo 	struct ifaddr *ifa;
    718   1.99    rpaulo 	struct ifnet *ifp;
    719   1.99    rpaulo 	struct in6_ifaddr *public_ifa6 = NULL;
    720  1.205     ozaki 	int s;
    721   1.99    rpaulo 
    722   1.99    rpaulo 	ifp = ia6->ia_ifa.ifa_ifp;
    723  1.205     ozaki 	s = pserialize_read_enter();
    724  1.202     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
    725   1.99    rpaulo 		struct in6_ifaddr *it6;
    726   1.99    rpaulo 
    727   1.99    rpaulo 		if (ifa->ifa_addr->sa_family != AF_INET6)
    728   1.99    rpaulo 			continue;
    729   1.99    rpaulo 
    730   1.99    rpaulo 		it6 = (struct in6_ifaddr *)ifa;
    731   1.99    rpaulo 
    732   1.99    rpaulo 		/* ignore no autoconf addresses. */
    733   1.99    rpaulo 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
    734   1.99    rpaulo 			continue;
    735   1.99    rpaulo 
    736   1.99    rpaulo 		/* ignore autoconf addresses with different prefixes. */
    737   1.99    rpaulo 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
    738   1.99    rpaulo 			continue;
    739   1.99    rpaulo 
    740   1.99    rpaulo 		/*
    741   1.99    rpaulo 		 * Now we are looking at an autoconf address with the same
    742   1.99    rpaulo 		 * prefix as ours.  If the address is temporary and is still
    743   1.99    rpaulo 		 * preferred, do not create another one.  It would be rare, but
    744   1.99    rpaulo 		 * could happen, for example, when we resume a laptop PC after
    745   1.99    rpaulo 		 * a long period.
    746   1.99    rpaulo 		 */
    747   1.99    rpaulo 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
    748   1.99    rpaulo 		    !IFA6_IS_DEPRECATED(it6)) {
    749   1.99    rpaulo 			public_ifa6 = NULL;
    750   1.99    rpaulo 			break;
    751   1.99    rpaulo 		}
    752   1.99    rpaulo 
    753   1.99    rpaulo 		/*
    754   1.99    rpaulo 		 * This is a public autoconf address that has the same prefix
    755   1.99    rpaulo 		 * as ours.  If it is preferred, keep it.  We can't break the
    756   1.99    rpaulo 		 * loop here, because there may be a still-preferred temporary
    757   1.99    rpaulo 		 * address with the prefix.
    758   1.99    rpaulo 		 */
    759   1.99    rpaulo 		if (!IFA6_IS_DEPRECATED(it6))
    760  1.200     ozaki 			public_ifa6 = it6;
    761   1.99    rpaulo 	}
    762   1.99    rpaulo 
    763   1.99    rpaulo 	if (public_ifa6 != NULL) {
    764   1.99    rpaulo 		int e;
    765  1.205     ozaki 		struct psref psref;
    766   1.99    rpaulo 
    767  1.205     ozaki 		ia6_acquire(public_ifa6, &psref);
    768  1.205     ozaki 		pserialize_read_exit(s);
    769   1.99    rpaulo 		/*
    770   1.99    rpaulo 		 * Random factor is introduced in the preferred lifetime, so
    771   1.99    rpaulo 		 * we do not need additional delay (3rd arg to in6_tmpifadd).
    772   1.99    rpaulo 		 */
    773   1.99    rpaulo 		if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
    774  1.205     ozaki 			ia6_release(public_ifa6, &psref);
    775   1.99    rpaulo 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
    776   1.99    rpaulo 			    " tmp addr, errno=%d\n", e);
    777  1.112    dyoung 			return -1;
    778   1.99    rpaulo 		}
    779  1.205     ozaki 		ia6_release(public_ifa6, &psref);
    780  1.112    dyoung 		return 0;
    781   1.99    rpaulo 	}
    782  1.205     ozaki 		pserialize_read_exit(s);
    783   1.99    rpaulo 
    784  1.112    dyoung 	return -1;
    785   1.99    rpaulo }
    786   1.99    rpaulo 
    787  1.135    dyoung bool
    788  1.135    dyoung nd6_accepts_rtadv(const struct nd_ifinfo *ndi)
    789  1.135    dyoung {
    790  1.135    dyoung 	switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) {
    791  1.135    dyoung 	case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV:
    792  1.135    dyoung 		return true;
    793  1.135    dyoung 	case ND6_IFF_ACCEPT_RTADV:
    794  1.135    dyoung 		return ip6_accept_rtadv != 0;
    795  1.135    dyoung 	case ND6_IFF_OVERRIDE_RTADV:
    796  1.135    dyoung 	case 0:
    797  1.135    dyoung 	default:
    798  1.135    dyoung 		return false;
    799  1.135    dyoung 	}
    800  1.135    dyoung }
    801  1.135    dyoung 
    802   1.16    itojun /*
    803   1.16    itojun  * Nuke neighbor cache/prefix/default router management table, right before
    804   1.16    itojun  * ifp goes away.
    805   1.16    itojun  */
    806   1.16    itojun void
    807  1.158    martin nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext)
    808   1.16    itojun {
    809   1.65    itojun 	struct nd_defrouter *dr, *ndr;
    810   1.16    itojun 	struct nd_prefix *pr, *npr;
    811   1.16    itojun 
    812   1.65    itojun 	/*
    813  1.158    martin 	 * During detach, the ND info might be already removed, but
    814  1.158    martin 	 * then is explitly passed as argument.
    815  1.158    martin 	 * Otherwise get it from ifp->if_afdata.
    816  1.158    martin 	 */
    817  1.158    martin 	if (ext == NULL)
    818  1.158    martin 		ext = ifp->if_afdata[AF_INET6];
    819  1.158    martin 	if (ext == NULL)
    820  1.158    martin 		return;
    821  1.158    martin 
    822  1.158    martin 	/*
    823   1.65    itojun 	 * Nuke default router list entries toward ifp.
    824   1.65    itojun 	 * We defer removal of default router list entries that is installed
    825   1.65    itojun 	 * in the routing table, in order to keep additional side effects as
    826   1.65    itojun 	 * small as possible.
    827   1.65    itojun 	 */
    828  1.140  christos 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
    829   1.65    itojun 		if (dr->installed)
    830   1.65    itojun 			continue;
    831   1.65    itojun 
    832  1.158    martin 		if (dr->ifp == ifp) {
    833  1.158    martin 			KASSERT(ext != NULL);
    834  1.158    martin 			defrtrlist_del(dr, ext);
    835  1.158    martin 		}
    836   1.65    itojun 	}
    837  1.140  christos 
    838  1.140  christos 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
    839   1.65    itojun 		if (!dr->installed)
    840   1.65    itojun 			continue;
    841   1.65    itojun 
    842  1.158    martin 		if (dr->ifp == ifp) {
    843  1.158    martin 			KASSERT(ext != NULL);
    844  1.158    martin 			defrtrlist_del(dr, ext);
    845  1.158    martin 		}
    846   1.16    itojun 	}
    847   1.16    itojun 
    848   1.16    itojun 	/* Nuke prefix list entries toward ifp */
    849  1.140  christos 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
    850   1.16    itojun 		if (pr->ndpr_ifp == ifp) {
    851   1.65    itojun 			/*
    852   1.92      tron 			 * Because if_detach() does *not* release prefixes
    853   1.92      tron 			 * while purging addresses the reference count will
    854   1.92      tron 			 * still be above zero. We therefore reset it to
    855   1.92      tron 			 * make sure that the prefix really gets purged.
    856   1.92      tron 			 */
    857   1.92      tron 			pr->ndpr_refcnt = 0;
    858   1.92      tron 			/*
    859   1.65    itojun 			 * Previously, pr->ndpr_addr is removed as well,
    860   1.65    itojun 			 * but I strongly believe we don't have to do it.
    861   1.65    itojun 			 * nd6_purge() is only called from in6_ifdetach(),
    862   1.65    itojun 			 * which removes all the associated interface addresses
    863   1.65    itojun 			 * by itself.
    864   1.65    itojun 			 * (jinmei (at) kame.net 20010129)
    865   1.65    itojun 			 */
    866   1.16    itojun 			prelist_remove(pr);
    867   1.16    itojun 		}
    868   1.16    itojun 	}
    869   1.16    itojun 
    870   1.16    itojun 	/* cancel default outgoing interface setting */
    871   1.16    itojun 	if (nd6_defifindex == ifp->if_index)
    872   1.16    itojun 		nd6_setdefaultiface(0);
    873   1.16    itojun 
    874  1.132  tonnerre 	/* XXX: too restrictive? */
    875  1.141  christos 	if (!ip6_forwarding && ifp->if_afdata[AF_INET6]) {
    876  1.141  christos 		struct nd_ifinfo *ndi = ND_IFINFO(ifp);
    877  1.141  christos 		if (ndi && nd6_accepts_rtadv(ndi)) {
    878  1.141  christos 			/* refresh default router list */
    879  1.141  christos 			defrouter_select();
    880  1.141  christos 		}
    881   1.48    itojun 	}
    882   1.16    itojun 
    883   1.16    itojun 	/*
    884  1.181     ozaki 	 * We may not need to nuke the neighbor cache entries here
    885  1.181     ozaki 	 * because the neighbor cache is kept in if_afdata[AF_INET6].
    886  1.181     ozaki 	 * nd6_purge() is invoked by in6_ifdetach() which is called
    887  1.181     ozaki 	 * from if_detach() where everything gets purged. However
    888  1.181     ozaki 	 * in6_ifdetach is directly called from vlan(4), so we still
    889  1.181     ozaki 	 * need to purge entries here.
    890   1.16    itojun 	 */
    891  1.181     ozaki 	if (ext->lltable != NULL)
    892  1.181     ozaki 		lltable_purge_entries(ext->lltable);
    893    1.2    itojun }
    894    1.2    itojun 
    895  1.188     ozaki struct llentry *
    896  1.188     ozaki nd6_lookup(const struct in6_addr *addr6, const struct ifnet *ifp, bool wlock)
    897  1.188     ozaki {
    898  1.188     ozaki 	struct sockaddr_in6 sin6;
    899  1.188     ozaki 	struct llentry *ln;
    900  1.188     ozaki 
    901  1.188     ozaki 	sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
    902  1.188     ozaki 
    903  1.188     ozaki 	IF_AFDATA_RLOCK(ifp);
    904  1.188     ozaki 	ln = lla_lookup(LLTABLE6(ifp), wlock ? LLE_EXCLUSIVE : 0,
    905  1.188     ozaki 	    sin6tosa(&sin6));
    906  1.188     ozaki 	IF_AFDATA_RUNLOCK(ifp);
    907  1.188     ozaki 
    908  1.188     ozaki 	return ln;
    909  1.188     ozaki }
    910  1.188     ozaki 
    911  1.188     ozaki struct llentry *
    912  1.188     ozaki nd6_create(const struct in6_addr *addr6, const struct ifnet *ifp)
    913    1.2    itojun {
    914    1.2    itojun 	struct sockaddr_in6 sin6;
    915  1.188     ozaki 	struct llentry *ln;
    916    1.2    itojun 
    917  1.122    dyoung 	sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
    918  1.188     ozaki 
    919  1.188     ozaki 	IF_AFDATA_WLOCK(ifp);
    920  1.188     ozaki 	ln = lla_create(LLTABLE6(ifp), LLE_EXCLUSIVE,
    921  1.188     ozaki 	    sin6tosa(&sin6));
    922  1.188     ozaki 	IF_AFDATA_WUNLOCK(ifp);
    923  1.188     ozaki 
    924  1.188     ozaki 	if (ln != NULL)
    925  1.188     ozaki 		ln->ln_state = ND6_LLINFO_NOSTATE;
    926  1.188     ozaki 
    927  1.188     ozaki 	return ln;
    928  1.188     ozaki }
    929  1.188     ozaki 
    930  1.188     ozaki /*
    931  1.188     ozaki  * Test whether a given IPv6 address is a neighbor or not, ignoring
    932  1.188     ozaki  * the actual neighbor cache.  The neighbor cache is ignored in order
    933  1.188     ozaki  * to not reenter the routing code from within itself.
    934  1.188     ozaki  */
    935  1.188     ozaki static int
    936  1.188     ozaki nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
    937  1.188     ozaki {
    938  1.188     ozaki 	struct nd_prefix *pr;
    939  1.188     ozaki 	struct ifaddr *dstaddr;
    940  1.205     ozaki 	int s;
    941  1.188     ozaki 
    942  1.188     ozaki 	/*
    943  1.188     ozaki 	 * A link-local address is always a neighbor.
    944  1.188     ozaki 	 * XXX: a link does not necessarily specify a single interface.
    945  1.188     ozaki 	 */
    946  1.188     ozaki 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
    947  1.188     ozaki 		struct sockaddr_in6 sin6_copy;
    948  1.188     ozaki 		u_int32_t zone;
    949  1.188     ozaki 
    950    1.2    itojun 		/*
    951  1.188     ozaki 		 * We need sin6_copy since sa6_recoverscope() may modify the
    952  1.188     ozaki 		 * content (XXX).
    953  1.188     ozaki 		 */
    954  1.188     ozaki 		sin6_copy = *addr;
    955  1.188     ozaki 		if (sa6_recoverscope(&sin6_copy))
    956  1.188     ozaki 			return 0; /* XXX: should be impossible */
    957  1.188     ozaki 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
    958  1.188     ozaki 			return 0;
    959  1.188     ozaki 		if (sin6_copy.sin6_scope_id == zone)
    960  1.188     ozaki 			return 1;
    961  1.188     ozaki 		else
    962  1.188     ozaki 			return 0;
    963  1.188     ozaki 	}
    964  1.188     ozaki 
    965  1.188     ozaki 	/*
    966  1.188     ozaki 	 * If the address matches one of our addresses,
    967  1.188     ozaki 	 * it should be a neighbor.
    968  1.188     ozaki 	 * If the address matches one of our on-link prefixes, it should be a
    969  1.188     ozaki 	 * neighbor.
    970  1.188     ozaki 	 */
    971  1.188     ozaki 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
    972  1.188     ozaki 		if (pr->ndpr_ifp != ifp)
    973  1.188     ozaki 			continue;
    974   1.25    itojun 
    975  1.188     ozaki 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
    976  1.188     ozaki 			struct rtentry *rt;
    977    1.2    itojun 
    978  1.204     ozaki 			rt = rtalloc1(sin6tosa(&pr->ndpr_prefix), 0);
    979  1.188     ozaki 			if (rt == NULL)
    980  1.188     ozaki 				continue;
    981  1.188     ozaki 			/*
    982  1.188     ozaki 			 * This is the case where multiple interfaces
    983  1.188     ozaki 			 * have the same prefix, but only one is installed
    984  1.188     ozaki 			 * into the routing table and that prefix entry
    985  1.188     ozaki 			 * is not the one being examined here. In the case
    986  1.188     ozaki 			 * where RADIX_MPATH is enabled, multiple route
    987  1.188     ozaki 			 * entries (of the same rt_key value) will be
    988  1.188     ozaki 			 * installed because the interface addresses all
    989  1.188     ozaki 			 * differ.
    990  1.188     ozaki 			 */
    991  1.188     ozaki 			if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
    992  1.188     ozaki 			    &satocsin6(rt_getkey(rt))->sin6_addr)) {
    993  1.188     ozaki 				continue;
    994  1.188     ozaki 			}
    995  1.112    dyoung 		}
    996  1.188     ozaki 
    997  1.188     ozaki 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
    998  1.188     ozaki 		    &addr->sin6_addr, &pr->ndpr_mask))
    999  1.188     ozaki 			return 1;
   1000  1.188     ozaki 	}
   1001  1.147       roy 
   1002  1.147       roy 	/*
   1003  1.188     ozaki 	 * If the address is assigned on the node of the other side of
   1004  1.188     ozaki 	 * a p2p interface, the address should be a neighbor.
   1005  1.188     ozaki 	 */
   1006  1.205     ozaki 	s = pserialize_read_enter();
   1007  1.204     ozaki 	dstaddr = ifa_ifwithdstaddr(sin6tocsa(addr));
   1008  1.188     ozaki 	if (dstaddr != NULL) {
   1009  1.188     ozaki 		if (dstaddr->ifa_ifp == ifp) {
   1010  1.205     ozaki 			pserialize_read_exit(s);
   1011  1.188     ozaki 			return 1;
   1012  1.188     ozaki 		}
   1013  1.188     ozaki 	}
   1014  1.205     ozaki 	pserialize_read_exit(s);
   1015  1.147       roy 
   1016   1.12    itojun 	/*
   1017  1.188     ozaki 	 * If the default router list is empty, all addresses are regarded
   1018  1.188     ozaki 	 * as on-link, and thus, as a neighbor.
   1019  1.188     ozaki 	 */
   1020  1.188     ozaki 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
   1021  1.188     ozaki 	    TAILQ_EMPTY(&nd_defrouter) &&
   1022  1.188     ozaki 	    nd6_defifindex == ifp->if_index) {
   1023  1.188     ozaki 		return 1;
   1024    1.2    itojun 	}
   1025    1.6    itojun 
   1026  1.188     ozaki 	return 0;
   1027  1.147       roy }
   1028  1.147       roy 
   1029    1.6    itojun /*
   1030    1.6    itojun  * Detect if a given IPv6 address identifies a neighbor on a given link.
   1031   1.25    itojun  * XXX: should take care of the destination of a p2p link?
   1032    1.6    itojun  */
   1033    1.6    itojun int
   1034  1.110    dyoung nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
   1035    1.6    itojun {
   1036   1.65    itojun 	struct nd_prefix *pr;
   1037  1.188     ozaki 	struct llentry *ln;
   1038  1.165     ozaki 	struct rtentry *rt;
   1039    1.6    itojun 
   1040   1.29    itojun 	/*
   1041   1.29    itojun 	 * A link-local address is always a neighbor.
   1042   1.52    itojun 	 * XXX: a link does not necessarily specify a single interface.
   1043   1.29    itojun 	 */
   1044   1.96    rpaulo 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
   1045   1.96    rpaulo 		struct sockaddr_in6 sin6_copy;
   1046   1.96    rpaulo 		u_int32_t zone;
   1047   1.96    rpaulo 
   1048   1.96    rpaulo 		/*
   1049   1.96    rpaulo 		 * We need sin6_copy since sa6_recoverscope() may modify the
   1050   1.96    rpaulo 		 * content (XXX).
   1051   1.96    rpaulo 		 */
   1052   1.96    rpaulo 		sin6_copy = *addr;
   1053   1.96    rpaulo 		if (sa6_recoverscope(&sin6_copy))
   1054  1.112    dyoung 			return 0; /* XXX: should be impossible */
   1055   1.96    rpaulo 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
   1056  1.112    dyoung 			return 0;
   1057   1.96    rpaulo 		if (sin6_copy.sin6_scope_id == zone)
   1058  1.112    dyoung 			return 1;
   1059   1.96    rpaulo 		else
   1060  1.112    dyoung 			return 0;
   1061   1.96    rpaulo 	}
   1062    1.6    itojun 
   1063    1.6    itojun 	/*
   1064   1.65    itojun 	 * If the address matches one of our on-link prefixes, it should be a
   1065   1.65    itojun 	 * neighbor.
   1066    1.6    itojun 	 */
   1067  1.108    dyoung 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1068   1.65    itojun 		if (pr->ndpr_ifp != ifp)
   1069   1.65    itojun 			continue;
   1070   1.65    itojun 
   1071   1.65    itojun 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
   1072   1.65    itojun 			continue;
   1073   1.65    itojun 
   1074   1.65    itojun 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
   1075   1.65    itojun 		    &addr->sin6_addr, &pr->ndpr_mask))
   1076  1.112    dyoung 			return 1;
   1077   1.65    itojun 	}
   1078    1.6    itojun 
   1079   1.65    itojun 	/*
   1080   1.65    itojun 	 * If the default router list is empty, all addresses are regarded
   1081   1.65    itojun 	 * as on-link, and thus, as a neighbor.
   1082   1.65    itojun 	 * XXX: we restrict the condition to hosts, because routers usually do
   1083   1.65    itojun 	 * not have the "default router list".
   1084   1.65    itojun 	 */
   1085   1.65    itojun 	if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
   1086   1.65    itojun 	    nd6_defifindex == ifp->if_index) {
   1087  1.112    dyoung 		return 1;
   1088    1.6    itojun 	}
   1089    1.6    itojun 
   1090  1.188     ozaki 	IF_AFDATA_UNLOCK_ASSERT(ifp);
   1091  1.188     ozaki 	if (nd6_is_new_addr_neighbor(addr, ifp))
   1092  1.188     ozaki 		return 1;
   1093  1.188     ozaki 
   1094    1.6    itojun 	/*
   1095  1.188     ozaki 	 * Even if the address matches none of our addresses, it might be
   1096  1.188     ozaki 	 * in the neighbor cache or a connected route.
   1097    1.6    itojun 	 */
   1098  1.188     ozaki 	ln = nd6_lookup(&addr->sin6_addr, ifp, false);
   1099  1.188     ozaki 	if (ln != NULL) {
   1100  1.188     ozaki 		LLE_RUNLOCK(ln);
   1101  1.188     ozaki 		return 1;
   1102  1.188     ozaki 	}
   1103  1.188     ozaki 
   1104  1.188     ozaki 	rt = rtalloc1(sin6tocsa(addr), 0);
   1105  1.188     ozaki 	if (rt == NULL)
   1106  1.188     ozaki 		return 0;
   1107  1.188     ozaki 
   1108  1.188     ozaki 	if ((rt->rt_flags & RTF_CONNECTED) && (rt->rt_ifp == ifp
   1109  1.188     ozaki #if NBRIDGE > 0
   1110  1.188     ozaki 	    || rt->rt_ifp->if_bridge == ifp->if_bridge
   1111  1.188     ozaki #endif
   1112  1.188     ozaki #if NCARP > 0
   1113  1.188     ozaki 	    || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) ||
   1114  1.188     ozaki 	    (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)||
   1115  1.188     ozaki 	    (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP &&
   1116  1.188     ozaki 	    rt->rt_ifp->if_carpdev == ifp->if_carpdev)
   1117  1.188     ozaki #endif
   1118  1.188     ozaki 	    )) {
   1119  1.165     ozaki 		rtfree(rt);
   1120  1.112    dyoung 		return 1;
   1121  1.165     ozaki 	}
   1122  1.188     ozaki 	rtfree(rt);
   1123    1.6    itojun 
   1124  1.112    dyoung 	return 0;
   1125    1.2    itojun }
   1126    1.2    itojun 
   1127    1.2    itojun /*
   1128    1.2    itojun  * Free an nd6 llinfo entry.
   1129   1.54    itojun  * Since the function would cause significant changes in the kernel, DO NOT
   1130   1.54    itojun  * make it global, unless you have a strong reason for the change, and are sure
   1131   1.54    itojun  * that the change is safe.
   1132    1.2    itojun  */
   1133  1.181     ozaki static void
   1134  1.188     ozaki nd6_free(struct llentry *ln, int gc)
   1135    1.2    itojun {
   1136   1.12    itojun 	struct nd_defrouter *dr;
   1137  1.188     ozaki 	struct ifnet *ifp;
   1138  1.188     ozaki 	struct in6_addr *in6;
   1139    1.2    itojun 
   1140  1.181     ozaki 	KASSERT(ln != NULL);
   1141  1.181     ozaki 	LLE_WLOCK_ASSERT(ln);
   1142  1.181     ozaki 
   1143  1.188     ozaki 	ifp = ln->lle_tbl->llt_ifp;
   1144  1.188     ozaki 	in6 = &ln->r_l3addr.addr6;
   1145   1.18    itojun 	/*
   1146   1.54    itojun 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
   1147   1.54    itojun 	 * even though it is not harmful, it was not really necessary.
   1148   1.18    itojun 	 */
   1149   1.18    itojun 
   1150   1.86    itojun 	/* cancel timer */
   1151  1.188     ozaki 	nd6_llinfo_settimer(ln, -1);
   1152   1.86    itojun 
   1153   1.85    itojun 	if (!ip6_forwarding) {
   1154    1.2    itojun 		int s;
   1155    1.5    itojun 		s = splsoftnet();
   1156  1.188     ozaki 		dr = defrouter_lookup(in6, ifp);
   1157   1.54    itojun 
   1158   1.54    itojun 		if (dr != NULL && dr->expire &&
   1159   1.54    itojun 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
   1160   1.54    itojun 			/*
   1161   1.54    itojun 			 * If the reason for the deletion is just garbage
   1162   1.54    itojun 			 * collection, and the neighbor is an active default
   1163   1.54    itojun 			 * router, do not delete it.  Instead, reset the GC
   1164   1.54    itojun 			 * timer using the router's lifetime.
   1165   1.54    itojun 			 * Simply deleting the entry would affect default
   1166   1.54    itojun 			 * router selection, which is not necessarily a good
   1167   1.54    itojun 			 * thing, especially when we're using router preference
   1168   1.54    itojun 			 * values.
   1169   1.54    itojun 			 * XXX: the check for ln_state would be redundant,
   1170   1.54    itojun 			 *      but we intentionally keep it just in case.
   1171   1.54    itojun 			 */
   1172  1.166     ozaki 			if (dr->expire > time_uptime)
   1173  1.188     ozaki 				nd6_llinfo_settimer(ln,
   1174  1.166     ozaki 				    (dr->expire - time_uptime) * hz);
   1175   1.86    itojun 			else
   1176  1.188     ozaki 				nd6_llinfo_settimer(ln, nd6_gctimer * hz);
   1177   1.54    itojun 			splx(s);
   1178  1.183     ozaki 			LLE_WUNLOCK(ln);
   1179  1.181     ozaki 			return;
   1180   1.54    itojun 		}
   1181   1.54    itojun 
   1182   1.12    itojun 		if (ln->ln_router || dr) {
   1183   1.12    itojun 			/*
   1184  1.190     ozaki 			 * We need to unlock to avoid a LOR with rt6_flush()
   1185  1.190     ozaki 			 * with the rnh and for the calls to
   1186  1.190     ozaki 			 * pfxlist_onlink_check() and defrouter_select() in the
   1187  1.190     ozaki 			 * block further down for calls into nd6_lookup().
   1188  1.190     ozaki 			 * We still hold a ref.
   1189  1.190     ozaki 			 */
   1190  1.190     ozaki 			LLE_WUNLOCK(ln);
   1191  1.190     ozaki 
   1192  1.190     ozaki 			/*
   1193   1.12    itojun 			 * rt6_flush must be called whether or not the neighbor
   1194   1.12    itojun 			 * is in the Default Router List.
   1195   1.12    itojun 			 * See a corresponding comment in nd6_na_input().
   1196   1.12    itojun 			 */
   1197  1.188     ozaki 			rt6_flush(in6, ifp);
   1198   1.12    itojun 		}
   1199   1.12    itojun 
   1200   1.12    itojun 		if (dr) {
   1201   1.12    itojun 			/*
   1202   1.12    itojun 			 * Unreachablity of a router might affect the default
   1203   1.12    itojun 			 * router selection and on-link detection of advertised
   1204   1.12    itojun 			 * prefixes.
   1205   1.12    itojun 			 */
   1206   1.12    itojun 
   1207    1.2    itojun 			/*
   1208   1.12    itojun 			 * Temporarily fake the state to choose a new default
   1209   1.12    itojun 			 * router and to perform on-link determination of
   1210   1.54    itojun 			 * prefixes correctly.
   1211   1.12    itojun 			 * Below the state will be set correctly,
   1212   1.12    itojun 			 * or the entry itself will be deleted.
   1213    1.2    itojun 			 */
   1214   1.12    itojun 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
   1215   1.12    itojun 
   1216   1.54    itojun 			/*
   1217   1.54    itojun 			 * Since defrouter_select() does not affect the
   1218   1.54    itojun 			 * on-link determination and MIP6 needs the check
   1219   1.54    itojun 			 * before the default router selection, we perform
   1220   1.54    itojun 			 * the check now.
   1221   1.54    itojun 			 */
   1222   1.54    itojun 			pfxlist_onlink_check();
   1223   1.54    itojun 
   1224   1.65    itojun 			/*
   1225   1.65    itojun 			 * refresh default router list
   1226   1.65    itojun 			 */
   1227   1.65    itojun 			defrouter_select();
   1228    1.2    itojun 		}
   1229  1.190     ozaki 
   1230  1.190     ozaki #ifdef __FreeBSD__
   1231  1.190     ozaki 		/*
   1232  1.190     ozaki 		 * If this entry was added by an on-link redirect, remove the
   1233  1.190     ozaki 		 * corresponding host route.
   1234  1.190     ozaki 		 */
   1235  1.190     ozaki 		if (ln->la_flags & LLE_REDIRECT)
   1236  1.190     ozaki 			nd6_free_redirect(ln);
   1237  1.190     ozaki #endif
   1238  1.190     ozaki 
   1239  1.190     ozaki 		if (ln->ln_router || dr)
   1240  1.190     ozaki 			LLE_WLOCK(ln);
   1241  1.190     ozaki 
   1242    1.2    itojun 		splx(s);
   1243    1.2    itojun 	}
   1244   1.12    itojun 
   1245  1.188     ozaki 	/*
   1246  1.188     ozaki 	 * Save to unlock. We still hold an extra reference and will not
   1247  1.188     ozaki 	 * free(9) in llentry_free() if someone else holds one as well.
   1248  1.188     ozaki 	 */
   1249  1.181     ozaki 	LLE_WUNLOCK(ln);
   1250  1.188     ozaki 	IF_AFDATA_LOCK(ifp);
   1251  1.188     ozaki 	LLE_WLOCK(ln);
   1252  1.188     ozaki 
   1253  1.188     ozaki 	/* Guard against race with other llentry_free(). */
   1254  1.188     ozaki 	if (ln->la_flags & LLE_LINKED) {
   1255  1.188     ozaki 		LLE_REMREF(ln);
   1256  1.188     ozaki 		llentry_free(ln);
   1257  1.188     ozaki 	} else
   1258  1.188     ozaki 		LLE_FREE_LOCKED(ln);
   1259  1.188     ozaki 
   1260  1.188     ozaki 	IF_AFDATA_UNLOCK(ifp);
   1261    1.2    itojun }
   1262    1.2    itojun 
   1263    1.2    itojun /*
   1264    1.2    itojun  * Upper-layer reachability hint for Neighbor Unreachability Detection.
   1265    1.2    itojun  *
   1266   1.98    rpaulo  * XXX cost-effective methods?
   1267    1.2    itojun  */
   1268    1.2    itojun void
   1269  1.171     ozaki nd6_nud_hint(struct rtentry *rt)
   1270    1.2    itojun {
   1271  1.181     ozaki 	struct llentry *ln;
   1272  1.188     ozaki 	struct ifnet *ifp;
   1273    1.2    itojun 
   1274  1.164     ozaki 	if (rt == NULL)
   1275  1.164     ozaki 		return;
   1276    1.2    itojun 
   1277  1.188     ozaki 	ifp = rt->rt_ifp;
   1278  1.188     ozaki 	ln = nd6_lookup(&(satocsin6(rt_getkey(rt)))->sin6_addr, ifp, true);
   1279  1.188     ozaki 	if (ln == NULL)
   1280  1.171     ozaki 		return;
   1281    1.2    itojun 
   1282   1.22    itojun 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
   1283  1.188     ozaki 		goto done;
   1284    1.2    itojun 
   1285   1.31    itojun 	/*
   1286   1.31    itojun 	 * if we get upper-layer reachability confirmation many times,
   1287   1.31    itojun 	 * it is possible we have false information.
   1288   1.31    itojun 	 */
   1289  1.164     ozaki 	ln->ln_byhint++;
   1290  1.164     ozaki 	if (ln->ln_byhint > nd6_maxnudhint)
   1291  1.188     ozaki 		goto done;
   1292   1.31    itojun 
   1293    1.2    itojun 	ln->ln_state = ND6_LLINFO_REACHABLE;
   1294  1.188     ozaki 	if (!ND6_LLINFO_PERMANENT(ln))
   1295  1.188     ozaki 		nd6_llinfo_settimer(ln, ND_IFINFO(rt->rt_ifp)->reachable * hz);
   1296  1.188     ozaki 
   1297  1.188     ozaki done:
   1298  1.188     ozaki 	LLE_WUNLOCK(ln);
   1299  1.171     ozaki 
   1300  1.165     ozaki 	return;
   1301    1.2    itojun }
   1302    1.2    itojun 
   1303  1.181     ozaki static int
   1304  1.181     ozaki nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg)
   1305  1.181     ozaki {
   1306  1.181     ozaki 	int *n = farg;
   1307  1.181     ozaki 
   1308  1.181     ozaki 	if (*n <= 0)
   1309  1.181     ozaki 		return 0;
   1310  1.181     ozaki 
   1311  1.181     ozaki 	if (ND6_LLINFO_PERMANENT(ln))
   1312  1.181     ozaki 		return 0;
   1313  1.181     ozaki 
   1314  1.181     ozaki 	LLE_WLOCK(ln);
   1315  1.181     ozaki 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
   1316  1.181     ozaki 		ln->ln_state = ND6_LLINFO_STALE;
   1317  1.181     ozaki 	else
   1318  1.181     ozaki 		ln->ln_state = ND6_LLINFO_PURGE;
   1319  1.188     ozaki 	nd6_llinfo_settimer(ln, 0);
   1320  1.181     ozaki 	LLE_WUNLOCK(ln);
   1321  1.181     ozaki 
   1322  1.181     ozaki 	(*n)--;
   1323  1.181     ozaki 	return 0;
   1324  1.181     ozaki }
   1325  1.181     ozaki 
   1326  1.181     ozaki static void
   1327  1.181     ozaki nd6_gc_neighbors(struct lltable *llt)
   1328  1.181     ozaki {
   1329  1.181     ozaki 	int max_gc_entries = 10;
   1330  1.181     ozaki 
   1331  1.181     ozaki 	if (ip6_neighborgcthresh >= 0 &&
   1332  1.181     ozaki 	    lltable_get_entry_count(llt) >= ip6_neighborgcthresh) {
   1333  1.181     ozaki 		/*
   1334  1.181     ozaki 		 * XXX entries that are "less recently used" should be
   1335  1.181     ozaki 		 * freed first.
   1336  1.181     ozaki 		 */
   1337  1.181     ozaki 		lltable_foreach_lle(llt, nd6_purge_entry, &max_gc_entries);
   1338  1.181     ozaki 	}
   1339  1.181     ozaki }
   1340  1.181     ozaki 
   1341    1.2    itojun void
   1342  1.130    dyoung nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
   1343    1.2    itojun {
   1344    1.2    itojun 	struct sockaddr *gate = rt->rt_gateway;
   1345    1.2    itojun 	struct ifnet *ifp = rt->rt_ifp;
   1346  1.119    dyoung 	uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
   1347    1.2    itojun 	struct ifaddr *ifa;
   1348    1.2    itojun 
   1349  1.144     joerg 	RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1350  1.117    dyoung 
   1351  1.131    dyoung 	if (req == RTM_LLINFO_UPD) {
   1352  1.131    dyoung 		int rc;
   1353  1.131    dyoung 		struct in6_addr *in6;
   1354  1.131    dyoung 		struct in6_addr in6_all;
   1355  1.131    dyoung 		int anycast;
   1356  1.131    dyoung 
   1357  1.131    dyoung 		if ((ifa = info->rti_ifa) == NULL)
   1358  1.131    dyoung 			return;
   1359  1.131    dyoung 
   1360  1.131    dyoung 		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
   1361  1.131    dyoung 		anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
   1362  1.131    dyoung 
   1363  1.131    dyoung 		in6_all = in6addr_linklocal_allnodes;
   1364  1.131    dyoung 		if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
   1365  1.131    dyoung 			log(LOG_ERR, "%s: failed to set scope %s "
   1366  1.131    dyoung 			    "(errno=%d)\n", __func__, if_name(ifp), rc);
   1367  1.131    dyoung 			return;
   1368  1.131    dyoung 		}
   1369  1.131    dyoung 
   1370  1.131    dyoung 		/* XXX don't set Override for proxy addresses */
   1371  1.131    dyoung 		nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
   1372  1.131    dyoung 		    (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
   1373  1.131    dyoung #if 0
   1374  1.131    dyoung 		    | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
   1375  1.131    dyoung #endif
   1376  1.131    dyoung 		    , 1, NULL);
   1377  1.131    dyoung 		return;
   1378  1.131    dyoung 	}
   1379  1.131    dyoung 
   1380   1.65    itojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
   1381    1.2    itojun 		return;
   1382    1.2    itojun 
   1383   1.54    itojun 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
   1384  1.144     joerg 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1385   1.54    itojun 		/*
   1386   1.54    itojun 		 * This is probably an interface direct route for a link
   1387   1.54    itojun 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
   1388   1.54    itojun 		 * We do not need special treatment below for such a route.
   1389   1.54    itojun 		 * Moreover, the RTF_LLINFO flag which would be set below
   1390   1.54    itojun 		 * would annoy the ndp(8) command.
   1391   1.54    itojun 		 */
   1392   1.54    itojun 		return;
   1393   1.54    itojun 	}
   1394   1.54    itojun 
   1395    1.2    itojun 	switch (req) {
   1396  1.205     ozaki 	case RTM_ADD: {
   1397  1.205     ozaki 		int s;
   1398  1.205     ozaki 
   1399  1.144     joerg 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1400    1.2    itojun 		/*
   1401    1.2    itojun 		 * There is no backward compatibility :)
   1402    1.2    itojun 		 *
   1403    1.2    itojun 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
   1404    1.2    itojun 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
   1405    1.2    itojun 		 *	   rt->rt_flags |= RTF_CLONING;
   1406    1.2    itojun 		 */
   1407  1.188     ozaki 		/* XXX should move to route.c? */
   1408  1.188     ozaki 		if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) {
   1409  1.120    dyoung 			union {
   1410  1.120    dyoung 				struct sockaddr sa;
   1411  1.120    dyoung 				struct sockaddr_dl sdl;
   1412  1.120    dyoung 				struct sockaddr_storage ss;
   1413  1.120    dyoung 			} u;
   1414    1.2    itojun 			/*
   1415   1.84    itojun 			 * Case 1: This route should come from a route to
   1416   1.84    itojun 			 * interface (RTF_CLONING case) or the route should be
   1417   1.84    itojun 			 * treated as on-link but is currently not
   1418  1.129    dyoung 			 * (RTF_LLINFO && ln == NULL case).
   1419    1.2    itojun 			 */
   1420  1.136    dyoung 			if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
   1421  1.120    dyoung 			    ifp->if_index, ifp->if_type,
   1422  1.136    dyoung 			    NULL, namelen, NULL, addrlen) == NULL) {
   1423  1.136    dyoung 				printf("%s.%d: sockaddr_dl_init(, %zu, ) "
   1424  1.136    dyoung 				    "failed on %s\n", __func__, __LINE__,
   1425  1.136    dyoung 				    sizeof(u.ss), if_name(ifp));
   1426  1.136    dyoung 			}
   1427  1.120    dyoung 			rt_setgate(rt, &u.sa);
   1428  1.119    dyoung 			gate = rt->rt_gateway;
   1429  1.144     joerg 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1430  1.192     ozaki 			if (gate == NULL) {
   1431  1.192     ozaki 				log(LOG_ERR,
   1432  1.192     ozaki 				    "%s: rt_setgate failed on %s\n", __func__,
   1433  1.192     ozaki 				    if_name(ifp));
   1434  1.192     ozaki 				break;
   1435  1.192     ozaki 			}
   1436  1.188     ozaki 
   1437  1.144     joerg 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1438  1.188     ozaki 			if ((rt->rt_flags & RTF_CONNECTED) != 0)
   1439    1.2    itojun 				break;
   1440    1.2    itojun 		}
   1441  1.144     joerg 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1442   1.18    itojun 		/*
   1443   1.18    itojun 		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
   1444   1.18    itojun 		 * We don't do that here since llinfo is not ready yet.
   1445   1.18    itojun 		 *
   1446   1.18    itojun 		 * There are also couple of other things to be discussed:
   1447   1.18    itojun 		 * - unsolicited NA code needs improvement beforehand
   1448   1.18    itojun 		 * - RFC2461 says we MAY send multicast unsolicited NA
   1449   1.18    itojun 		 *   (7.2.6 paragraph 4), however, it also says that we
   1450   1.18    itojun 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
   1451   1.18    itojun 		 *   we don't have anything like it right now.
   1452   1.38    itojun 		 *   note that the mechanism needs a mutual agreement
   1453   1.18    itojun 		 *   between proxies, which means that we need to implement
   1454   1.38    itojun 		 *   a new protocol, or a new kludge.
   1455   1.38    itojun 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
   1456   1.18    itojun 		 *   we need to check ip6forwarding before sending it.
   1457   1.18    itojun 		 *   (or should we allow proxy ND configuration only for
   1458   1.18    itojun 		 *   routers?  there's no mention about proxy ND from hosts)
   1459   1.18    itojun 		 */
   1460   1.18    itojun #if 0
   1461   1.18    itojun 		/* XXX it does not work */
   1462    1.2    itojun 		if (rt->rt_flags & RTF_ANNOUNCE)
   1463    1.2    itojun 			nd6_na_output(ifp,
   1464  1.118    dyoung 			      &satocsin6(rt_getkey(rt))->sin6_addr,
   1465  1.118    dyoung 			      &satocsin6(rt_getkey(rt))->sin6_addr,
   1466   1.18    itojun 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
   1467   1.18    itojun 			      1, NULL);
   1468   1.18    itojun #endif
   1469  1.188     ozaki 
   1470   1.65    itojun 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
   1471  1.144     joerg 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1472   1.26    itojun 			/*
   1473   1.26    itojun 			 * Address resolution isn't necessary for a point to
   1474   1.26    itojun 			 * point link, so we can skip this test for a p2p link.
   1475   1.26    itojun 			 */
   1476   1.26    itojun 			if (gate->sa_family != AF_LINK ||
   1477  1.119    dyoung 			    gate->sa_len <
   1478  1.119    dyoung 			    sockaddr_dl_measure(namelen, addrlen)) {
   1479   1.26    itojun 				log(LOG_DEBUG,
   1480   1.36    itojun 				    "nd6_rtrequest: bad gateway value: %s\n",
   1481   1.36    itojun 				    if_name(ifp));
   1482   1.26    itojun 				break;
   1483   1.26    itojun 			}
   1484  1.118    dyoung 			satosdl(gate)->sdl_type = ifp->if_type;
   1485  1.118    dyoung 			satosdl(gate)->sdl_index = ifp->if_index;
   1486  1.144     joerg 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1487    1.2    itojun 		}
   1488  1.144     joerg 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1489    1.2    itojun 
   1490  1.143  christos 		/*
   1491  1.198     ozaki 		 * When called from rt_ifa_addlocal, we cannot depend on that
   1492  1.198     ozaki 		 * the address (rt_getkey(rt)) exits in the address list of the
   1493  1.198     ozaki 		 * interface. So check RTF_LOCAL instead.
   1494  1.198     ozaki 		 */
   1495  1.198     ozaki 		if (rt->rt_flags & RTF_LOCAL) {
   1496  1.198     ozaki 			if (nd6_useloopback)
   1497  1.198     ozaki 				rt->rt_ifp = lo0ifp;	/* XXX */
   1498  1.198     ozaki 			break;
   1499  1.198     ozaki 		}
   1500  1.198     ozaki 
   1501  1.198     ozaki 		/*
   1502  1.117    dyoung 		 * check if rt_getkey(rt) is an address assigned
   1503    1.2    itojun 		 * to the interface.
   1504    1.2    itojun 		 */
   1505  1.205     ozaki 		s = pserialize_read_enter();
   1506  1.188     ozaki 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
   1507  1.188     ozaki 		    &satocsin6(rt_getkey(rt))->sin6_addr);
   1508  1.129    dyoung 		if (ifa != NULL) {
   1509    1.2    itojun 			if (nd6_useloopback) {
   1510  1.195     ozaki 				rt->rt_ifp = lo0ifp;	/* XXX */
   1511    1.2    itojun 				/*
   1512    1.2    itojun 				 * Make sure rt_ifa be equal to the ifaddr
   1513    1.2    itojun 				 * corresponding to the address.
   1514    1.2    itojun 				 * We need this because when we refer
   1515    1.2    itojun 				 * rt_ifa->ia6_flags in ip6_input, we assume
   1516    1.2    itojun 				 * that the rt_ifa points to the address instead
   1517    1.2    itojun 				 * of the loopback address.
   1518    1.2    itojun 				 */
   1519  1.106    dyoung 				if (ifa != rt->rt_ifa)
   1520  1.106    dyoung 					rt_replace_ifa(rt, ifa);
   1521    1.2    itojun 			}
   1522   1.18    itojun 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
   1523   1.18    itojun 			/* join solicited node multicast for proxy ND */
   1524   1.18    itojun 			if (ifp->if_flags & IFF_MULTICAST) {
   1525   1.18    itojun 				struct in6_addr llsol;
   1526   1.18    itojun 				int error;
   1527   1.18    itojun 
   1528  1.118    dyoung 				llsol = satocsin6(rt_getkey(rt))->sin6_addr;
   1529   1.96    rpaulo 				llsol.s6_addr32[0] = htonl(0xff020000);
   1530   1.18    itojun 				llsol.s6_addr32[1] = 0;
   1531   1.18    itojun 				llsol.s6_addr32[2] = htonl(1);
   1532   1.18    itojun 				llsol.s6_addr8[12] = 0xff;
   1533   1.96    rpaulo 				if (in6_setscope(&llsol, ifp, NULL))
   1534  1.188     ozaki 					goto out;
   1535   1.99    rpaulo 				if (!in6_addmulti(&llsol, ifp, &error, 0)) {
   1536  1.187     ozaki 					nd6log(LOG_ERR, "%s: failed to join "
   1537   1.54    itojun 					    "%s (errno=%d)\n", if_name(ifp),
   1538  1.187     ozaki 					    ip6_sprintf(&llsol), error);
   1539   1.46    itojun 				}
   1540   1.18    itojun 			}
   1541    1.2    itojun 		}
   1542  1.188     ozaki 	out:
   1543  1.205     ozaki 		pserialize_read_exit(s);
   1544  1.181     ozaki 		/*
   1545  1.181     ozaki 		 * If we have too many cache entries, initiate immediate
   1546  1.181     ozaki 		 * purging for some entries.
   1547  1.181     ozaki 		 */
   1548  1.188     ozaki 		if (rt->rt_ifp != NULL)
   1549  1.188     ozaki 			nd6_gc_neighbors(LLTABLE6(rt->rt_ifp));
   1550    1.2    itojun 		break;
   1551  1.205     ozaki 	    }
   1552    1.2    itojun 
   1553    1.2    itojun 	case RTM_DELETE:
   1554   1.18    itojun 		/* leave from solicited node multicast for proxy ND */
   1555   1.18    itojun 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
   1556   1.18    itojun 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
   1557   1.18    itojun 			struct in6_addr llsol;
   1558   1.18    itojun 			struct in6_multi *in6m;
   1559   1.18    itojun 
   1560  1.118    dyoung 			llsol = satocsin6(rt_getkey(rt))->sin6_addr;
   1561   1.96    rpaulo 			llsol.s6_addr32[0] = htonl(0xff020000);
   1562   1.18    itojun 			llsol.s6_addr32[1] = 0;
   1563   1.18    itojun 			llsol.s6_addr32[2] = htonl(1);
   1564   1.18    itojun 			llsol.s6_addr8[12] = 0xff;
   1565   1.96    rpaulo 			if (in6_setscope(&llsol, ifp, NULL) == 0) {
   1566   1.96    rpaulo 				IN6_LOOKUP_MULTI(llsol, ifp, in6m);
   1567   1.96    rpaulo 				if (in6m)
   1568   1.96    rpaulo 					in6_delmulti(in6m);
   1569  1.104  christos 			}
   1570   1.18    itojun 		}
   1571  1.188     ozaki 		break;
   1572    1.2    itojun 	}
   1573    1.2    itojun }
   1574    1.2    itojun 
   1575    1.2    itojun int
   1576  1.112    dyoung nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
   1577    1.2    itojun {
   1578    1.2    itojun 	struct in6_drlist *drl = (struct in6_drlist *)data;
   1579   1.65    itojun 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
   1580    1.2    itojun 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
   1581    1.2    itojun 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
   1582   1.12    itojun 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
   1583   1.65    itojun 	struct nd_defrouter *dr;
   1584    1.2    itojun 	struct nd_prefix *pr;
   1585    1.2    itojun 	int i = 0, error = 0;
   1586    1.2    itojun 	int s;
   1587    1.2    itojun 
   1588    1.2    itojun 	switch (cmd) {
   1589    1.2    itojun 	case SIOCGDRLST_IN6:
   1590   1.65    itojun 		/*
   1591   1.65    itojun 		 * obsolete API, use sysctl under net.inet6.icmp6
   1592   1.65    itojun 		 */
   1593  1.129    dyoung 		memset(drl, 0, sizeof(*drl));
   1594    1.5    itojun 		s = splsoftnet();
   1595  1.108    dyoung 		TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
   1596  1.108    dyoung 			if (i >= DRLSTSIZ)
   1597  1.108    dyoung 				break;
   1598    1.2    itojun 			drl->defrouter[i].rtaddr = dr->rtaddr;
   1599   1.96    rpaulo 			in6_clearscope(&drl->defrouter[i].rtaddr);
   1600    1.2    itojun 
   1601    1.2    itojun 			drl->defrouter[i].flags = dr->flags;
   1602    1.2    itojun 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
   1603  1.166     ozaki 			drl->defrouter[i].expire = dr->expire ?
   1604  1.166     ozaki 			    time_mono_to_wall(dr->expire) : 0;
   1605    1.2    itojun 			drl->defrouter[i].if_index = dr->ifp->if_index;
   1606    1.2    itojun 			i++;
   1607    1.2    itojun 		}
   1608    1.2    itojun 		splx(s);
   1609    1.2    itojun 		break;
   1610    1.2    itojun 	case SIOCGPRLST_IN6:
   1611   1.18    itojun 		/*
   1612   1.65    itojun 		 * obsolete API, use sysctl under net.inet6.icmp6
   1613   1.65    itojun 		 *
   1614   1.65    itojun 		 * XXX the structure in6_prlist was changed in backward-
   1615   1.65    itojun 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
   1616   1.65    itojun 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
   1617   1.65    itojun 		 */
   1618   1.65    itojun 		/*
   1619   1.18    itojun 		 * XXX meaning of fields, especialy "raflags", is very
   1620   1.18    itojun 		 * differnet between RA prefix list and RR/static prefix list.
   1621   1.18    itojun 		 * how about separating ioctls into two?
   1622   1.18    itojun 		 */
   1623  1.129    dyoung 		memset(oprl, 0, sizeof(*oprl));
   1624    1.5    itojun 		s = splsoftnet();
   1625  1.108    dyoung 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1626    1.2    itojun 			struct nd_pfxrouter *pfr;
   1627    1.2    itojun 			int j;
   1628    1.2    itojun 
   1629  1.108    dyoung 			if (i >= PRLSTSIZ)
   1630  1.108    dyoung 				break;
   1631   1.65    itojun 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
   1632   1.65    itojun 			oprl->prefix[i].raflags = pr->ndpr_raf;
   1633   1.65    itojun 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
   1634   1.65    itojun 			oprl->prefix[i].vltime = pr->ndpr_vltime;
   1635   1.65    itojun 			oprl->prefix[i].pltime = pr->ndpr_pltime;
   1636   1.65    itojun 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
   1637   1.99    rpaulo 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
   1638   1.99    rpaulo 				oprl->prefix[i].expire = 0;
   1639   1.99    rpaulo 			else {
   1640   1.99    rpaulo 				time_t maxexpire;
   1641   1.99    rpaulo 
   1642   1.99    rpaulo 				/* XXX: we assume time_t is signed. */
   1643   1.99    rpaulo 				maxexpire = (-1) &
   1644   1.99    rpaulo 				    ~((time_t)1 <<
   1645   1.99    rpaulo 				    ((sizeof(maxexpire) * 8) - 1));
   1646   1.99    rpaulo 				if (pr->ndpr_vltime <
   1647   1.99    rpaulo 				    maxexpire - pr->ndpr_lastupdate) {
   1648  1.166     ozaki 					time_t expire;
   1649  1.166     ozaki 					expire = pr->ndpr_lastupdate +
   1650  1.166     ozaki 					    pr->ndpr_vltime;
   1651  1.166     ozaki 					oprl->prefix[i].expire = expire ?
   1652  1.166     ozaki 					    time_mono_to_wall(expire) : 0;
   1653   1.99    rpaulo 				} else
   1654   1.99    rpaulo 					oprl->prefix[i].expire = maxexpire;
   1655   1.99    rpaulo 			}
   1656    1.2    itojun 
   1657    1.2    itojun 			j = 0;
   1658  1.108    dyoung 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
   1659    1.2    itojun 				if (j < DRLSTSIZ) {
   1660   1.65    itojun #define RTRADDR oprl->prefix[i].advrtr[j]
   1661    1.2    itojun 					RTRADDR = pfr->router->rtaddr;
   1662   1.96    rpaulo 					in6_clearscope(&RTRADDR);
   1663    1.2    itojun #undef RTRADDR
   1664    1.2    itojun 				}
   1665    1.2    itojun 				j++;
   1666    1.2    itojun 			}
   1667   1.65    itojun 			oprl->prefix[i].advrtrs = j;
   1668   1.65    itojun 			oprl->prefix[i].origin = PR_ORIG_RA;
   1669    1.2    itojun 
   1670    1.2    itojun 			i++;
   1671    1.2    itojun 		}
   1672   1.18    itojun 		splx(s);
   1673   1.12    itojun 
   1674    1.2    itojun 		break;
   1675   1.58    itojun 	case OSIOCGIFINFO_IN6:
   1676   1.99    rpaulo #define ND	ndi->ndi
   1677   1.58    itojun 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
   1678   1.99    rpaulo 		memset(&ND, 0, sizeof(ND));
   1679   1.99    rpaulo 		ND.linkmtu = IN6_LINKMTU(ifp);
   1680   1.99    rpaulo 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
   1681   1.99    rpaulo 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
   1682   1.99    rpaulo 		ND.reachable = ND_IFINFO(ifp)->reachable;
   1683   1.99    rpaulo 		ND.retrans = ND_IFINFO(ifp)->retrans;
   1684   1.99    rpaulo 		ND.flags = ND_IFINFO(ifp)->flags;
   1685   1.99    rpaulo 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
   1686   1.99    rpaulo 		ND.chlim = ND_IFINFO(ifp)->chlim;
   1687   1.58    itojun 		break;
   1688    1.2    itojun 	case SIOCGIFINFO_IN6:
   1689   1.99    rpaulo 		ND = *ND_IFINFO(ifp);
   1690    1.2    itojun 		break;
   1691   1.99    rpaulo 	case SIOCSIFINFO_IN6:
   1692   1.99    rpaulo 		/*
   1693   1.99    rpaulo 		 * used to change host variables from userland.
   1694   1.99    rpaulo 		 * intented for a use on router to reflect RA configurations.
   1695   1.99    rpaulo 		 */
   1696   1.99    rpaulo 		/* 0 means 'unspecified' */
   1697   1.99    rpaulo 		if (ND.linkmtu != 0) {
   1698   1.99    rpaulo 			if (ND.linkmtu < IPV6_MMTU ||
   1699   1.99    rpaulo 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
   1700   1.99    rpaulo 				error = EINVAL;
   1701   1.99    rpaulo 				break;
   1702   1.99    rpaulo 			}
   1703   1.99    rpaulo 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
   1704   1.99    rpaulo 		}
   1705   1.99    rpaulo 
   1706   1.99    rpaulo 		if (ND.basereachable != 0) {
   1707   1.99    rpaulo 			int obasereachable = ND_IFINFO(ifp)->basereachable;
   1708   1.99    rpaulo 
   1709   1.99    rpaulo 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
   1710   1.99    rpaulo 			if (ND.basereachable != obasereachable)
   1711   1.99    rpaulo 				ND_IFINFO(ifp)->reachable =
   1712   1.99    rpaulo 				    ND_COMPUTE_RTIME(ND.basereachable);
   1713   1.99    rpaulo 		}
   1714   1.99    rpaulo 		if (ND.retrans != 0)
   1715   1.99    rpaulo 			ND_IFINFO(ifp)->retrans = ND.retrans;
   1716   1.99    rpaulo 		if (ND.chlim != 0)
   1717   1.99    rpaulo 			ND_IFINFO(ifp)->chlim = ND.chlim;
   1718   1.99    rpaulo 		/* FALLTHROUGH */
   1719   1.26    itojun 	case SIOCSIFINFO_FLAGS:
   1720  1.148       roy 	{
   1721  1.148       roy 		struct ifaddr *ifa;
   1722  1.148       roy 		struct in6_ifaddr *ia;
   1723  1.148       roy 
   1724  1.148       roy 		if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
   1725  1.148       roy 		    !(ND.flags & ND6_IFF_IFDISABLED))
   1726  1.148       roy 		{
   1727  1.148       roy 			/*
   1728  1.148       roy 			 * If the interface is marked as ND6_IFF_IFDISABLED and
   1729  1.148       roy 			 * has a link-local address with IN6_IFF_DUPLICATED,
   1730  1.148       roy 			 * do not clear ND6_IFF_IFDISABLED.
   1731  1.148       roy 			 * See RFC 4862, section 5.4.5.
   1732  1.148       roy 			 */
   1733  1.148       roy 			int duplicated_linklocal = 0;
   1734  1.148       roy 
   1735  1.205     ozaki 			s = pserialize_read_enter();
   1736  1.202     ozaki 			IFADDR_READER_FOREACH(ifa, ifp) {
   1737  1.148       roy 				if (ifa->ifa_addr->sa_family != AF_INET6)
   1738  1.148       roy 					continue;
   1739  1.148       roy 				ia = (struct in6_ifaddr *)ifa;
   1740  1.148       roy 				if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
   1741  1.148       roy 				    IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
   1742  1.148       roy 				{
   1743  1.148       roy 					duplicated_linklocal = 1;
   1744  1.148       roy 					break;
   1745  1.148       roy 				}
   1746  1.148       roy 			}
   1747  1.205     ozaki 			pserialize_read_exit(s);
   1748  1.148       roy 
   1749  1.148       roy 			if (duplicated_linklocal) {
   1750  1.148       roy 				ND.flags |= ND6_IFF_IFDISABLED;
   1751  1.148       roy 				log(LOG_ERR, "Cannot enable an interface"
   1752  1.148       roy 				    " with a link-local address marked"
   1753  1.148       roy 				    " duplicate.\n");
   1754  1.148       roy 			} else {
   1755  1.148       roy 				ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
   1756  1.148       roy 				if (ifp->if_flags & IFF_UP)
   1757  1.148       roy 					in6_if_up(ifp);
   1758  1.148       roy 			}
   1759  1.148       roy 		} else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
   1760  1.205     ozaki 		    (ND.flags & ND6_IFF_IFDISABLED)) {
   1761  1.205     ozaki 			int bound = curlwp_bind();
   1762  1.148       roy 			/* Mark all IPv6 addresses as tentative. */
   1763  1.148       roy 
   1764  1.148       roy 			ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
   1765  1.205     ozaki 			s = pserialize_read_enter();
   1766  1.202     ozaki 			IFADDR_READER_FOREACH(ifa, ifp) {
   1767  1.205     ozaki 				struct psref psref;
   1768  1.148       roy 				if (ifa->ifa_addr->sa_family != AF_INET6)
   1769  1.148       roy 					continue;
   1770  1.205     ozaki 				ifa_acquire(ifa, &psref);
   1771  1.205     ozaki 				pserialize_read_exit(s);
   1772  1.205     ozaki 
   1773  1.148       roy 				nd6_dad_stop(ifa);
   1774  1.205     ozaki 
   1775  1.148       roy 				ia = (struct in6_ifaddr *)ifa;
   1776  1.148       roy 				ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1777  1.205     ozaki 
   1778  1.205     ozaki 				s = pserialize_read_enter();
   1779  1.205     ozaki 				ifa_release(ifa, &psref);
   1780  1.148       roy 			}
   1781  1.205     ozaki 			pserialize_read_exit(s);
   1782  1.205     ozaki 			curlwp_bindx(bound);
   1783  1.148       roy 		}
   1784  1.151       roy 
   1785  1.151       roy 		if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
   1786  1.151       roy 			if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
   1787  1.151       roy 				/* auto_linklocal 0->1 transition */
   1788  1.151       roy 
   1789  1.151       roy 				ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
   1790  1.151       roy 				in6_ifattach(ifp, NULL);
   1791  1.151       roy 			} else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
   1792  1.151       roy 			    ifp->if_flags & IFF_UP)
   1793  1.151       roy 			{
   1794  1.151       roy 				/*
   1795  1.151       roy 				 * When the IF already has
   1796  1.151       roy 				 * ND6_IFF_AUTO_LINKLOCAL, no link-local
   1797  1.151       roy 				 * address is assigned, and IFF_UP, try to
   1798  1.151       roy 				 * assign one.
   1799  1.151       roy 				 */
   1800  1.205     ozaki 				int haslinklocal = 0;
   1801  1.151       roy 
   1802  1.205     ozaki 				s = pserialize_read_enter();
   1803  1.205     ozaki 				IFADDR_READER_FOREACH(ifa, ifp) {
   1804  1.151       roy 					if (ifa->ifa_addr->sa_family !=AF_INET6)
   1805  1.151       roy 						continue;
   1806  1.151       roy 					ia = (struct in6_ifaddr *)ifa;
   1807  1.151       roy 					if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){
   1808  1.151       roy 						haslinklocal = 1;
   1809  1.151       roy 						break;
   1810  1.151       roy 					}
   1811  1.205     ozaki 				}
   1812  1.205     ozaki 				pserialize_read_exit(s);
   1813  1.205     ozaki 				if (!haslinklocal)
   1814  1.151       roy 					in6_ifattach(ifp, NULL);
   1815  1.151       roy 			}
   1816  1.151       roy 		}
   1817  1.148       roy 	}
   1818   1.99    rpaulo 		ND_IFINFO(ifp)->flags = ND.flags;
   1819   1.26    itojun 		break;
   1820   1.99    rpaulo #undef ND
   1821   1.12    itojun 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
   1822   1.65    itojun 		/* sync kernel routing table with the default router list */
   1823   1.65    itojun 		defrouter_reset();
   1824   1.12    itojun 		defrouter_select();
   1825    1.2    itojun 		break;
   1826    1.2    itojun 	case SIOCSPFXFLUSH_IN6:
   1827   1.65    itojun 	{
   1828    1.2    itojun 		/* flush all the prefix advertised by routers */
   1829   1.94  christos 		struct nd_prefix *pfx, *next;
   1830    1.2    itojun 
   1831    1.5    itojun 		s = splsoftnet();
   1832  1.140  christos 		LIST_FOREACH_SAFE(pfx, &nd_prefix, ndpr_entry, next) {
   1833   1.65    itojun 			struct in6_ifaddr *ia, *ia_next;
   1834  1.205     ozaki 			int _s;
   1835   1.65    itojun 
   1836   1.94  christos 			if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr))
   1837   1.65    itojun 				continue; /* XXX */
   1838   1.65    itojun 
   1839   1.65    itojun 			/* do we really have to remove addresses as well? */
   1840  1.205     ozaki 		restart:
   1841  1.205     ozaki 			_s = pserialize_read_enter();
   1842  1.205     ozaki 			for (ia = IN6_ADDRLIST_READER_FIRST(); ia;
   1843  1.199     ozaki 			     ia = ia_next) {
   1844   1.65    itojun 				/* ia might be removed.  keep the next ptr. */
   1845  1.205     ozaki 				ia_next = IN6_ADDRLIST_READER_NEXT(ia);
   1846   1.65    itojun 
   1847   1.65    itojun 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
   1848   1.65    itojun 					continue;
   1849   1.65    itojun 
   1850  1.205     ozaki 				if (ia->ia6_ndpr == pfx) {
   1851  1.205     ozaki 					pserialize_read_exit(_s);
   1852  1.205     ozaki 					/* XXX NOMPSAFE? */
   1853   1.65    itojun 					in6_purgeaddr(&ia->ia_ifa);
   1854  1.205     ozaki 					goto restart;
   1855  1.205     ozaki 				}
   1856   1.65    itojun 			}
   1857  1.205     ozaki 			pserialize_read_exit(_s);
   1858   1.94  christos 			prelist_remove(pfx);
   1859    1.2    itojun 		}
   1860    1.2    itojun 		splx(s);
   1861    1.2    itojun 		break;
   1862   1.65    itojun 	}
   1863    1.2    itojun 	case SIOCSRTRFLUSH_IN6:
   1864   1.65    itojun 	{
   1865    1.2    itojun 		/* flush all the default routers */
   1866   1.94  christos 		struct nd_defrouter *drtr, *next;
   1867    1.2    itojun 
   1868    1.5    itojun 		s = splsoftnet();
   1869   1.65    itojun 		defrouter_reset();
   1870  1.140  christos 		TAILQ_FOREACH_SAFE(drtr, &nd_defrouter, dr_entry, next) {
   1871  1.158    martin 			defrtrlist_del(drtr, NULL);
   1872    1.2    itojun 		}
   1873   1.65    itojun 		defrouter_select();
   1874    1.2    itojun 		splx(s);
   1875    1.2    itojun 		break;
   1876   1.65    itojun 	}
   1877    1.2    itojun 	case SIOCGNBRINFO_IN6:
   1878   1.99    rpaulo 	{
   1879  1.181     ozaki 		struct llentry *ln;
   1880   1.12    itojun 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
   1881   1.12    itojun 
   1882   1.96    rpaulo 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
   1883  1.112    dyoung 			return error;
   1884    1.2    itojun 
   1885  1.188     ozaki 		ln = nd6_lookup(&nb_addr, ifp, false);
   1886  1.165     ozaki 		if (ln == NULL) {
   1887   1.12    itojun 			error = EINVAL;
   1888   1.12    itojun 			break;
   1889   1.12    itojun 		}
   1890   1.12    itojun 		nbi->state = ln->ln_state;
   1891   1.12    itojun 		nbi->asked = ln->ln_asked;
   1892   1.12    itojun 		nbi->isrouter = ln->ln_router;
   1893  1.166     ozaki 		nbi->expire = ln->ln_expire ?
   1894  1.166     ozaki 		    time_mono_to_wall(ln->ln_expire) : 0;
   1895  1.188     ozaki 		LLE_RUNLOCK(ln);
   1896   1.63    itojun 
   1897   1.12    itojun 		break;
   1898   1.65    itojun 	}
   1899   1.12    itojun 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
   1900   1.12    itojun 		ndif->ifindex = nd6_defifindex;
   1901   1.12    itojun 		break;
   1902   1.12    itojun 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
   1903  1.112    dyoung 		return nd6_setdefaultiface(ndif->ifindex);
   1904    1.2    itojun 	}
   1905  1.112    dyoung 	return error;
   1906    1.2    itojun }
   1907    1.2    itojun 
   1908  1.115    dyoung void
   1909  1.188     ozaki nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp)
   1910  1.115    dyoung {
   1911  1.115    dyoung 	struct mbuf *m_hold, *m_hold_next;
   1912  1.188     ozaki 	struct sockaddr_in6 sin6;
   1913  1.188     ozaki 
   1914  1.188     ozaki 	LLE_WLOCK_ASSERT(ln);
   1915  1.188     ozaki 
   1916  1.188     ozaki 	sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
   1917  1.115    dyoung 
   1918  1.188     ozaki 	m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0;
   1919  1.188     ozaki 
   1920  1.188     ozaki 	LLE_WUNLOCK(ln);
   1921  1.188     ozaki 	for (; m_hold != NULL; m_hold = m_hold_next) {
   1922  1.115    dyoung 		m_hold_next = m_hold->m_nextpkt;
   1923  1.115    dyoung 		m_hold->m_nextpkt = NULL;
   1924  1.115    dyoung 
   1925  1.115    dyoung 		/*
   1926  1.115    dyoung 		 * we assume ifp is not a p2p here, so
   1927  1.115    dyoung 		 * just set the 2nd argument as the
   1928  1.115    dyoung 		 * 1st one.
   1929  1.115    dyoung 		 */
   1930  1.188     ozaki 		nd6_output(ifp, ifp, m_hold, &sin6, NULL);
   1931  1.115    dyoung 	}
   1932  1.188     ozaki 	LLE_WLOCK(ln);
   1933  1.115    dyoung }
   1934  1.115    dyoung 
   1935    1.2    itojun /*
   1936    1.2    itojun  * Create neighbor cache entry and cache link-layer address,
   1937   1.52    itojun  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
   1938    1.2    itojun  */
   1939  1.165     ozaki void
   1940  1.105  christos nd6_cache_lladdr(
   1941  1.105  christos     struct ifnet *ifp,
   1942  1.105  christos     struct in6_addr *from,
   1943  1.105  christos     char *lladdr,
   1944  1.107  christos     int lladdrlen,
   1945  1.105  christos     int type,	/* ICMP6 type */
   1946  1.105  christos     int code	/* type dependent information */
   1947  1.105  christos )
   1948    1.2    itojun {
   1949  1.132  tonnerre 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
   1950  1.181     ozaki 	struct llentry *ln = NULL;
   1951    1.2    itojun 	int is_newentry;
   1952    1.2    itojun 	int do_update;
   1953    1.2    itojun 	int olladdr;
   1954    1.2    itojun 	int llchange;
   1955    1.2    itojun 	int newstate = 0;
   1956  1.188     ozaki 	uint16_t router = 0;
   1957    1.2    itojun 
   1958  1.163     ozaki 	KASSERT(ifp != NULL);
   1959  1.163     ozaki 	KASSERT(from != NULL);
   1960    1.2    itojun 
   1961    1.2    itojun 	/* nothing must be updated for unspecified address */
   1962    1.2    itojun 	if (IN6_IS_ADDR_UNSPECIFIED(from))
   1963  1.165     ozaki 		return;
   1964    1.2    itojun 
   1965    1.2    itojun 	/*
   1966    1.2    itojun 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
   1967    1.2    itojun 	 * the caller.
   1968    1.2    itojun 	 *
   1969    1.2    itojun 	 * XXX If the link does not have link-layer adderss, what should
   1970    1.2    itojun 	 * we do? (ifp->if_addrlen == 0)
   1971    1.2    itojun 	 * Spec says nothing in sections for RA, RS and NA.  There's small
   1972    1.2    itojun 	 * description on it in NS section (RFC 2461 7.2.3).
   1973    1.2    itojun 	 */
   1974    1.2    itojun 
   1975  1.188     ozaki 	ln = nd6_lookup(from, ifp, true);
   1976  1.188     ozaki 	if (ln == NULL) {
   1977    1.2    itojun #if 0
   1978    1.2    itojun 		/* nothing must be done if there's no lladdr */
   1979    1.2    itojun 		if (!lladdr || !lladdrlen)
   1980    1.2    itojun 			return NULL;
   1981    1.2    itojun #endif
   1982    1.2    itojun 
   1983  1.188     ozaki 		ln = nd6_create(from, ifp);
   1984    1.2    itojun 		is_newentry = 1;
   1985   1.43    itojun 	} else {
   1986   1.43    itojun 		/* do nothing if static ndp is set */
   1987  1.188     ozaki 		if (ln->la_flags & LLE_STATIC) {
   1988  1.188     ozaki 			LLE_WUNLOCK(ln);
   1989  1.165     ozaki 			return;
   1990  1.165     ozaki 		}
   1991    1.2    itojun 		is_newentry = 0;
   1992   1.43    itojun 	}
   1993    1.2    itojun 
   1994  1.188     ozaki 	if (ln == NULL)
   1995  1.165     ozaki 		return;
   1996    1.2    itojun 
   1997  1.188     ozaki 	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
   1998    1.2    itojun 	if (olladdr && lladdr) {
   1999  1.188     ozaki 		llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen);
   2000    1.2    itojun 	} else
   2001    1.2    itojun 		llchange = 0;
   2002    1.2    itojun 
   2003    1.2    itojun 	/*
   2004    1.2    itojun 	 * newentry olladdr  lladdr  llchange	(*=record)
   2005    1.2    itojun 	 *	0	n	n	--	(1)
   2006    1.2    itojun 	 *	0	y	n	--	(2)
   2007    1.2    itojun 	 *	0	n	y	--	(3) * STALE
   2008    1.2    itojun 	 *	0	y	y	n	(4) *
   2009    1.2    itojun 	 *	0	y	y	y	(5) * STALE
   2010    1.2    itojun 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
   2011    1.2    itojun 	 *	1	--	y	--	(7) * STALE
   2012    1.2    itojun 	 */
   2013    1.2    itojun 
   2014   1.52    itojun 	if (lladdr) {		/* (3-5) and (7) */
   2015    1.2    itojun 		/*
   2016    1.2    itojun 		 * Record source link-layer address
   2017    1.2    itojun 		 * XXX is it dependent to ifp->if_type?
   2018    1.2    itojun 		 */
   2019  1.188     ozaki 		memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
   2020  1.188     ozaki 		ln->la_flags |= LLE_VALID;
   2021    1.2    itojun 	}
   2022    1.2    itojun 
   2023    1.2    itojun 	if (!is_newentry) {
   2024   1.65    itojun 		if ((!olladdr && lladdr) ||		/* (3) */
   2025   1.65    itojun 		    (olladdr && lladdr && llchange)) {	/* (5) */
   2026    1.2    itojun 			do_update = 1;
   2027    1.2    itojun 			newstate = ND6_LLINFO_STALE;
   2028   1.52    itojun 		} else					/* (1-2,4) */
   2029    1.2    itojun 			do_update = 0;
   2030    1.2    itojun 	} else {
   2031    1.2    itojun 		do_update = 1;
   2032   1.99    rpaulo 		if (lladdr == NULL)			/* (6) */
   2033    1.2    itojun 			newstate = ND6_LLINFO_NOSTATE;
   2034   1.52    itojun 		else					/* (7) */
   2035    1.2    itojun 			newstate = ND6_LLINFO_STALE;
   2036    1.2    itojun 	}
   2037    1.2    itojun 
   2038    1.2    itojun 	if (do_update) {
   2039    1.2    itojun 		/*
   2040    1.2    itojun 		 * Update the state of the neighbor cache.
   2041    1.2    itojun 		 */
   2042    1.2    itojun 		ln->ln_state = newstate;
   2043    1.2    itojun 
   2044    1.2    itojun 		if (ln->ln_state == ND6_LLINFO_STALE) {
   2045   1.44    itojun 			/*
   2046   1.44    itojun 			 * XXX: since nd6_output() below will cause
   2047   1.44    itojun 			 * state tansition to DELAY and reset the timer,
   2048   1.44    itojun 			 * we must set the timer now, although it is actually
   2049   1.44    itojun 			 * meaningless.
   2050   1.44    itojun 			 */
   2051  1.182     ozaki 			nd6_llinfo_settimer(ln, nd6_gctimer * hz);
   2052   1.44    itojun 
   2053  1.188     ozaki 			nd6_llinfo_release_pkts(ln, ifp);
   2054    1.2    itojun 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
   2055    1.2    itojun 			/* probe right away */
   2056   1.86    itojun 			nd6_llinfo_settimer((void *)ln, 0);
   2057    1.2    itojun 		}
   2058    1.2    itojun 	}
   2059    1.2    itojun 
   2060    1.2    itojun 	/*
   2061    1.2    itojun 	 * ICMP6 type dependent behavior.
   2062    1.2    itojun 	 *
   2063    1.2    itojun 	 * NS: clear IsRouter if new entry
   2064    1.2    itojun 	 * RS: clear IsRouter
   2065    1.2    itojun 	 * RA: set IsRouter if there's lladdr
   2066    1.2    itojun 	 * redir: clear IsRouter if new entry
   2067    1.2    itojun 	 *
   2068    1.2    itojun 	 * RA case, (1):
   2069    1.2    itojun 	 * The spec says that we must set IsRouter in the following cases:
   2070    1.2    itojun 	 * - If lladdr exist, set IsRouter.  This means (1-5).
   2071    1.2    itojun 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
   2072    1.2    itojun 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
   2073    1.2    itojun 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
   2074    1.2    itojun 	 * neighbor cache, this is similar to (6).
   2075    1.2    itojun 	 * This case is rare but we figured that we MUST NOT set IsRouter.
   2076    1.2    itojun 	 *
   2077    1.2    itojun 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
   2078    1.8    itojun 	 *							D R
   2079    1.8    itojun 	 *	0	n	n	--	(1)	c   ?     s
   2080    1.8    itojun 	 *	0	y	n	--	(2)	c   s     s
   2081    1.8    itojun 	 *	0	n	y	--	(3)	c   s     s
   2082    1.8    itojun 	 *	0	y	y	n	(4)	c   s     s
   2083    1.8    itojun 	 *	0	y	y	y	(5)	c   s     s
   2084    1.8    itojun 	 *	1	--	n	--	(6) c	c 	c s
   2085    1.8    itojun 	 *	1	--	y	--	(7) c	c   s	c s
   2086    1.2    itojun 	 *
   2087    1.2    itojun 	 *					(c=clear s=set)
   2088    1.2    itojun 	 */
   2089    1.2    itojun 	switch (type & 0xff) {
   2090    1.2    itojun 	case ND_NEIGHBOR_SOLICIT:
   2091    1.2    itojun 		/*
   2092    1.2    itojun 		 * New entry must have is_router flag cleared.
   2093    1.2    itojun 		 */
   2094   1.52    itojun 		if (is_newentry)	/* (6-7) */
   2095    1.8    itojun 			ln->ln_router = 0;
   2096    1.8    itojun 		break;
   2097    1.8    itojun 	case ND_REDIRECT:
   2098    1.8    itojun 		/*
   2099    1.8    itojun 		 * If the icmp is a redirect to a better router, always set the
   2100   1.52    itojun 		 * is_router flag.  Otherwise, if the entry is newly created,
   2101   1.52    itojun 		 * clear the flag.  [RFC 2461, sec 8.3]
   2102    1.8    itojun 		 */
   2103    1.8    itojun 		if (code == ND_REDIRECT_ROUTER)
   2104    1.8    itojun 			ln->ln_router = 1;
   2105   1.52    itojun 		else if (is_newentry) /* (6-7) */
   2106    1.2    itojun 			ln->ln_router = 0;
   2107    1.2    itojun 		break;
   2108    1.2    itojun 	case ND_ROUTER_SOLICIT:
   2109    1.2    itojun 		/*
   2110    1.2    itojun 		 * is_router flag must always be cleared.
   2111    1.2    itojun 		 */
   2112    1.2    itojun 		ln->ln_router = 0;
   2113    1.2    itojun 		break;
   2114    1.2    itojun 	case ND_ROUTER_ADVERT:
   2115    1.2    itojun 		/*
   2116    1.2    itojun 		 * Mark an entry with lladdr as a router.
   2117    1.2    itojun 		 */
   2118   1.65    itojun 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
   2119   1.65    itojun 		    (is_newentry && lladdr)) {			/* (7) */
   2120    1.2    itojun 			ln->ln_router = 1;
   2121    1.2    itojun 		}
   2122    1.2    itojun 		break;
   2123    1.2    itojun 	}
   2124   1.47    itojun 
   2125  1.188     ozaki #if 0
   2126  1.188     ozaki 	/* XXX should we send rtmsg as it used to be? */
   2127  1.156       roy 	if (do_update)
   2128  1.160       roy 		rt_newmsg(RTM_CHANGE, rt);  /* tell user process */
   2129  1.188     ozaki #endif
   2130  1.188     ozaki 
   2131  1.188     ozaki 	if (ln != NULL) {
   2132  1.188     ozaki 		router = ln->ln_router;
   2133  1.188     ozaki 		LLE_WUNLOCK(ln);
   2134  1.188     ozaki 	}
   2135  1.188     ozaki 
   2136  1.188     ozaki 	/*
   2137  1.188     ozaki 	 * If we have too many cache entries, initiate immediate
   2138  1.188     ozaki 	 * purging for some entries.
   2139  1.188     ozaki 	 */
   2140  1.188     ozaki 	if (is_newentry)
   2141  1.188     ozaki 		nd6_gc_neighbors(LLTABLE6(ifp));
   2142  1.156       roy 
   2143   1.47    itojun 	/*
   2144   1.47    itojun 	 * When the link-layer address of a router changes, select the
   2145   1.47    itojun 	 * best router again.  In particular, when the neighbor entry is newly
   2146   1.47    itojun 	 * created, it might affect the selection policy.
   2147   1.47    itojun 	 * Question: can we restrict the first condition to the "is_newentry"
   2148   1.47    itojun 	 * case?
   2149   1.47    itojun 	 * XXX: when we hear an RA from a new router with the link-layer
   2150   1.47    itojun 	 * address option, defrouter_select() is called twice, since
   2151   1.47    itojun 	 * defrtrlist_update called the function as well.  However, I believe
   2152   1.47    itojun 	 * we can compromise the overhead, since it only happens the first
   2153   1.47    itojun 	 * time.
   2154   1.52    itojun 	 * XXX: although defrouter_select() should not have a bad effect
   2155   1.52    itojun 	 * for those are not autoconfigured hosts, we explicitly avoid such
   2156   1.52    itojun 	 * cases for safety.
   2157   1.47    itojun 	 */
   2158  1.188     ozaki 	if (do_update && router && !ip6_forwarding &&
   2159  1.135    dyoung 	    nd6_accepts_rtadv(ndi))
   2160   1.47    itojun 		defrouter_select();
   2161    1.2    itojun }
   2162    1.2    itojun 
   2163    1.2    itojun static void
   2164  1.107  christos nd6_slowtimo(void *ignored_arg)
   2165    1.2    itojun {
   2166   1.38    itojun 	struct nd_ifinfo *nd6if;
   2167   1.58    itojun 	struct ifnet *ifp;
   2168  1.194     ozaki 	int s;
   2169    1.2    itojun 
   2170  1.126        ad 	mutex_enter(softnet_lock);
   2171  1.126        ad 	KERNEL_LOCK(1, NULL);
   2172  1.170     ozaki 	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
   2173   1.20   thorpej 	    nd6_slowtimo, NULL);
   2174  1.194     ozaki 
   2175  1.194     ozaki 	s = pserialize_read_enter();
   2176  1.194     ozaki 	IFNET_READER_FOREACH(ifp) {
   2177   1.58    itojun 		nd6if = ND_IFINFO(ifp);
   2178    1.2    itojun 		if (nd6if->basereachable && /* already initialized */
   2179    1.2    itojun 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
   2180    1.2    itojun 			/*
   2181    1.2    itojun 			 * Since reachable time rarely changes by router
   2182    1.2    itojun 			 * advertisements, we SHOULD insure that a new random
   2183    1.2    itojun 			 * value gets recomputed at least once every few hours.
   2184    1.2    itojun 			 * (RFC 2461, 6.3.4)
   2185    1.2    itojun 			 */
   2186    1.2    itojun 			nd6if->recalctm = nd6_recalc_reachtm_interval;
   2187    1.2    itojun 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
   2188    1.2    itojun 		}
   2189    1.2    itojun 	}
   2190  1.194     ozaki 	pserialize_read_exit(s);
   2191  1.194     ozaki 
   2192  1.126        ad 	KERNEL_UNLOCK_ONE(NULL);
   2193  1.126        ad 	mutex_exit(softnet_lock);
   2194    1.2    itojun }
   2195    1.2    itojun 
   2196  1.188     ozaki int
   2197  1.188     ozaki nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
   2198  1.188     ozaki     const struct sockaddr_in6 *dst, struct rtentry *rt)
   2199  1.176     ozaki {
   2200    1.2    itojun #define senderr(e) { error = (e); goto bad;}
   2201  1.181     ozaki 	struct llentry *ln = NULL;
   2202    1.2    itojun 	int error = 0;
   2203  1.188     ozaki 	bool created = false;
   2204  1.193     ozaki 
   2205  1.193     ozaki 	if (rt != NULL) {
   2206  1.193     ozaki 		error = rt_check_reject_route(rt, ifp);
   2207  1.193     ozaki 		if (error != 0) {
   2208  1.193     ozaki 			m_freem(m);
   2209  1.193     ozaki 			return error;
   2210  1.193     ozaki 		}
   2211  1.193     ozaki 	}
   2212  1.165     ozaki 
   2213    1.2    itojun 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
   2214    1.2    itojun 		goto sendpkt;
   2215    1.2    itojun 
   2216   1.54    itojun 	if (nd6_need_cache(ifp) == 0)
   2217    1.2    itojun 		goto sendpkt;
   2218    1.2    itojun 
   2219  1.193     ozaki 	if (rt != NULL && (rt->rt_flags & RTF_GATEWAY) != 0) {
   2220  1.193     ozaki 		struct sockaddr_in6 *gw6 = satosin6(rt->rt_gateway);
   2221  1.205     ozaki 		int s;
   2222  1.193     ozaki 
   2223  1.193     ozaki 		/* XXX remain the check to keep the original behavior. */
   2224  1.193     ozaki 		/*
   2225  1.193     ozaki 		 * We skip link-layer address resolution and NUD
   2226  1.193     ozaki 		 * if the gateway is not a neighbor from ND point
   2227  1.193     ozaki 		 * of view, regardless of the value of nd_ifinfo.flags.
   2228  1.193     ozaki 		 * The second condition is a bit tricky; we skip
   2229  1.193     ozaki 		 * if the gateway is our own address, which is
   2230  1.193     ozaki 		 * sometimes used to install a route to a p2p link.
   2231  1.193     ozaki 		 */
   2232  1.205     ozaki 		s = pserialize_read_enter();
   2233  1.193     ozaki 		if (!nd6_is_addr_neighbor(gw6, ifp) ||
   2234  1.193     ozaki 		    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
   2235  1.193     ozaki 			/*
   2236  1.193     ozaki 			 * We allow this kind of tricky route only
   2237  1.193     ozaki 			 * when the outgoing interface is p2p.
   2238  1.193     ozaki 			 * XXX: we may need a more generic rule here.
   2239  1.193     ozaki 			 */
   2240  1.193     ozaki 			if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
   2241  1.193     ozaki 				senderr(EHOSTUNREACH);
   2242  1.193     ozaki 
   2243  1.205     ozaki 			pserialize_read_exit(s);
   2244  1.193     ozaki 			goto sendpkt;
   2245  1.193     ozaki 		}
   2246  1.205     ozaki 		pserialize_read_exit(s);
   2247  1.193     ozaki 	}
   2248  1.193     ozaki 
   2249    1.2    itojun 	/*
   2250    1.2    itojun 	 * Address resolution or Neighbor Unreachability Detection
   2251    1.2    itojun 	 * for the next hop.
   2252    1.2    itojun 	 * At this point, the destination of the packet must be a unicast
   2253    1.2    itojun 	 * or an anycast address(i.e. not a multicast).
   2254    1.2    itojun 	 */
   2255    1.2    itojun 
   2256    1.2    itojun 	/* Look up the neighbor cache for the nexthop */
   2257  1.188     ozaki 	ln = nd6_lookup(&dst->sin6_addr, ifp, true);
   2258  1.188     ozaki 	if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp))  {
   2259   1.29    itojun 		/*
   2260   1.29    itojun 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
   2261   1.52    itojun 		 * the condition below is not very efficient.  But we believe
   2262   1.29    itojun 		 * it is tolerable, because this should be a rare case.
   2263   1.29    itojun 		 */
   2264  1.188     ozaki 		ln = nd6_create(&dst->sin6_addr, ifp);
   2265  1.188     ozaki 		if (ln != NULL)
   2266  1.188     ozaki 			created = true;
   2267    1.2    itojun 	}
   2268  1.188     ozaki 
   2269  1.188     ozaki 	if (ln == NULL) {
   2270   1.29    itojun 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
   2271   1.58    itojun 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
   2272   1.29    itojun 			log(LOG_DEBUG,
   2273   1.29    itojun 			    "nd6_output: can't allocate llinfo for %s "
   2274   1.29    itojun 			    "(ln=%p, rt=%p)\n",
   2275   1.29    itojun 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
   2276   1.29    itojun 			senderr(EIO);	/* XXX: good error? */
   2277   1.29    itojun 		}
   2278   1.29    itojun 		goto sendpkt;	/* send anyway */
   2279    1.2    itojun 	}
   2280    1.2    itojun 
   2281  1.188     ozaki 	LLE_WLOCK_ASSERT(ln);
   2282  1.188     ozaki 
   2283   1.26    itojun 	/* We don't have to do link-layer address resolution on a p2p link. */
   2284   1.26    itojun 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
   2285   1.42    itojun 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
   2286   1.26    itojun 		ln->ln_state = ND6_LLINFO_STALE;
   2287  1.182     ozaki 		nd6_llinfo_settimer(ln, nd6_gctimer * hz);
   2288   1.42    itojun 	}
   2289    1.2    itojun 
   2290    1.2    itojun 	/*
   2291    1.2    itojun 	 * The first time we send a packet to a neighbor whose entry is
   2292    1.2    itojun 	 * STALE, we have to change the state to DELAY and a sets a timer to
   2293    1.2    itojun 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
   2294    1.2    itojun 	 * neighbor unreachability detection on expiration.
   2295    1.2    itojun 	 * (RFC 2461 7.3.3)
   2296    1.2    itojun 	 */
   2297    1.2    itojun 	if (ln->ln_state == ND6_LLINFO_STALE) {
   2298    1.2    itojun 		ln->ln_asked = 0;
   2299    1.2    itojun 		ln->ln_state = ND6_LLINFO_DELAY;
   2300  1.182     ozaki 		nd6_llinfo_settimer(ln, nd6_delay * hz);
   2301    1.2    itojun 	}
   2302    1.2    itojun 
   2303    1.2    itojun 	/*
   2304    1.2    itojun 	 * If the neighbor cache entry has a state other than INCOMPLETE
   2305   1.52    itojun 	 * (i.e. its link-layer address is already resolved), just
   2306    1.2    itojun 	 * send the packet.
   2307    1.2    itojun 	 */
   2308    1.2    itojun 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
   2309    1.2    itojun 		goto sendpkt;
   2310    1.2    itojun 
   2311    1.2    itojun 	/*
   2312    1.2    itojun 	 * There is a neighbor cache entry, but no ethernet address
   2313   1.99    rpaulo 	 * response yet.  Append this latest packet to the end of the
   2314   1.99    rpaulo 	 * packet queue in the mbuf, unless the number of the packet
   2315   1.99    rpaulo 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
   2316   1.99    rpaulo 	 * the oldest packet in the queue will be removed.
   2317    1.2    itojun 	 */
   2318   1.41    itojun 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
   2319    1.2    itojun 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
   2320   1.99    rpaulo 	if (ln->ln_hold) {
   2321   1.99    rpaulo 		struct mbuf *m_hold;
   2322   1.99    rpaulo 		int i;
   2323   1.99    rpaulo 
   2324   1.99    rpaulo 		i = 0;
   2325   1.99    rpaulo 		for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
   2326   1.99    rpaulo 			i++;
   2327   1.99    rpaulo 			if (m_hold->m_nextpkt == NULL) {
   2328   1.99    rpaulo 				m_hold->m_nextpkt = m;
   2329   1.99    rpaulo 				break;
   2330   1.99    rpaulo 			}
   2331   1.99    rpaulo 		}
   2332   1.99    rpaulo 		while (i >= nd6_maxqueuelen) {
   2333   1.99    rpaulo 			m_hold = ln->ln_hold;
   2334   1.99    rpaulo 			ln->ln_hold = ln->ln_hold->m_nextpkt;
   2335  1.100    rpaulo 			m_freem(m_hold);
   2336   1.99    rpaulo 			i--;
   2337   1.99    rpaulo 		}
   2338   1.99    rpaulo 	} else {
   2339   1.99    rpaulo 		ln->ln_hold = m;
   2340   1.99    rpaulo 	}
   2341   1.99    rpaulo 
   2342   1.64    itojun 	/*
   2343   1.64    itojun 	 * If there has been no NS for the neighbor after entering the
   2344   1.64    itojun 	 * INCOMPLETE state, send the first solicitation.
   2345   1.64    itojun 	 */
   2346   1.86    itojun 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
   2347  1.179     ozaki 		struct in6_addr src, *psrc;
   2348  1.179     ozaki 
   2349   1.64    itojun 		ln->ln_asked++;
   2350  1.188     ozaki 		nd6_llinfo_settimer(ln, ND_IFINFO(ifp)->retrans * hz / 1000);
   2351  1.179     ozaki 		psrc = nd6_llinfo_get_holdsrc(ln, &src);
   2352  1.188     ozaki 		LLE_WUNLOCK(ln);
   2353  1.188     ozaki 		ln = NULL;
   2354  1.179     ozaki 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, psrc, 0);
   2355  1.188     ozaki 	} else {
   2356  1.188     ozaki 		/* We did the lookup so we need to do the unlock here. */
   2357  1.188     ozaki 		LLE_WUNLOCK(ln);
   2358    1.2    itojun 	}
   2359  1.188     ozaki 
   2360  1.165     ozaki 	error = 0;
   2361  1.165     ozaki 	goto exit;
   2362   1.63    itojun 
   2363    1.2    itojun   sendpkt:
   2364   1.99    rpaulo 	/* discard the packet if IPv6 operation is disabled on the interface */
   2365   1.99    rpaulo 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
   2366   1.99    rpaulo 		error = ENETDOWN; /* better error? */
   2367   1.99    rpaulo 		goto bad;
   2368   1.99    rpaulo 	}
   2369   1.30    itojun 
   2370  1.188     ozaki 	if (ln != NULL)
   2371  1.188     ozaki 		LLE_WUNLOCK(ln);
   2372  1.188     ozaki 
   2373  1.127    dyoung 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
   2374  1.197     ozaki 		error = if_output_lock(ifp, origifp, m, sin6tocsa(dst), rt);
   2375  1.150    bouyer 	else
   2376  1.196  knakahar 		error = if_output_lock(ifp, ifp, m, sin6tocsa(dst), rt);
   2377  1.165     ozaki 	goto exit;
   2378   1.25    itojun 
   2379    1.2    itojun   bad:
   2380  1.114    dyoung 	if (m != NULL)
   2381    1.2    itojun 		m_freem(m);
   2382  1.165     ozaki   exit:
   2383  1.188     ozaki 	if (created)
   2384  1.188     ozaki 		nd6_gc_neighbors(LLTABLE6(ifp));
   2385  1.165     ozaki 
   2386  1.112    dyoung 	return error;
   2387  1.188     ozaki #undef senderr
   2388   1.63    itojun }
   2389   1.54    itojun 
   2390   1.54    itojun int
   2391  1.112    dyoung nd6_need_cache(struct ifnet *ifp)
   2392   1.54    itojun {
   2393   1.54    itojun 	/*
   2394   1.54    itojun 	 * XXX: we currently do not make neighbor cache on any interface
   2395   1.54    itojun 	 * other than ARCnet, Ethernet, FDDI and GIF.
   2396   1.54    itojun 	 *
   2397   1.54    itojun 	 * RFC2893 says:
   2398   1.54    itojun 	 * - unidirectional tunnels needs no ND
   2399   1.54    itojun 	 */
   2400   1.54    itojun 	switch (ifp->if_type) {
   2401   1.54    itojun 	case IFT_ARCNET:
   2402   1.54    itojun 	case IFT_ETHER:
   2403   1.54    itojun 	case IFT_FDDI:
   2404   1.54    itojun 	case IFT_IEEE1394:
   2405  1.102  liamjfoy 	case IFT_CARP:
   2406   1.54    itojun 	case IFT_GIF:		/* XXX need more cases? */
   2407   1.99    rpaulo 	case IFT_PPP:
   2408   1.99    rpaulo 	case IFT_TUNNEL:
   2409  1.112    dyoung 		return 1;
   2410   1.54    itojun 	default:
   2411  1.112    dyoung 		return 0;
   2412   1.54    itojun 	}
   2413   1.54    itojun }
   2414    1.2    itojun 
   2415  1.188     ozaki /*
   2416  1.188     ozaki  * Add pernament ND6 link-layer record for given
   2417  1.188     ozaki  * interface address.
   2418  1.188     ozaki  *
   2419  1.188     ozaki  * Very similar to IPv4 arp_ifinit(), but:
   2420  1.188     ozaki  * 1) IPv6 DAD is performed in different place
   2421  1.188     ozaki  * 2) It is called by IPv6 protocol stack in contrast to
   2422  1.188     ozaki  * arp_ifinit() which is typically called in SIOCSIFADDR
   2423  1.188     ozaki  * driver ioctl handler.
   2424  1.188     ozaki  *
   2425  1.188     ozaki  */
   2426  1.188     ozaki int
   2427  1.188     ozaki nd6_add_ifa_lle(struct in6_ifaddr *ia)
   2428  1.188     ozaki {
   2429  1.188     ozaki 	struct ifnet *ifp;
   2430  1.188     ozaki 	struct llentry *ln;
   2431  1.188     ozaki 
   2432  1.188     ozaki 	ifp = ia->ia_ifa.ifa_ifp;
   2433  1.188     ozaki 	if (nd6_need_cache(ifp) == 0)
   2434  1.188     ozaki 		return 0;
   2435  1.188     ozaki 	ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
   2436  1.188     ozaki 	ia->ia_ifa.ifa_flags = RTF_CONNECTED;
   2437  1.188     ozaki 
   2438  1.188     ozaki 	IF_AFDATA_WLOCK(ifp);
   2439  1.188     ozaki 	ln = lla_create(LLTABLE6(ifp), LLE_IFADDR | LLE_EXCLUSIVE,
   2440  1.204     ozaki 	    sin6tosa(&ia->ia_addr));
   2441  1.188     ozaki 	IF_AFDATA_WUNLOCK(ifp);
   2442  1.188     ozaki 	if (ln == NULL)
   2443  1.188     ozaki 		return ENOBUFS;
   2444  1.188     ozaki 
   2445  1.188     ozaki 	ln->la_expire = 0;  /* for IPv6 this means permanent */
   2446  1.188     ozaki 	ln->ln_state = ND6_LLINFO_REACHABLE;
   2447  1.188     ozaki 
   2448  1.188     ozaki 	LLE_WUNLOCK(ln);
   2449  1.188     ozaki 	return 0;
   2450  1.188     ozaki }
   2451  1.188     ozaki 
   2452  1.188     ozaki /*
   2453  1.188     ozaki  * Removes ALL lle records for interface address prefix.
   2454  1.188     ozaki  * XXXME: That's probably not we really want to do, we need
   2455  1.188     ozaki  * to remove address record only and keep other records
   2456  1.188     ozaki  * until we determine if given prefix is really going
   2457  1.188     ozaki  * to be removed.
   2458  1.188     ozaki  */
   2459  1.188     ozaki void
   2460  1.188     ozaki nd6_rem_ifa_lle(struct in6_ifaddr *ia)
   2461  1.188     ozaki {
   2462  1.188     ozaki 	struct sockaddr_in6 mask, addr;
   2463  1.188     ozaki 
   2464  1.188     ozaki 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
   2465  1.188     ozaki 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
   2466  1.204     ozaki 	lltable_prefix_free(AF_INET6, sin6tosa(&addr), sin6tosa(&mask),
   2467  1.204     ozaki 	    LLE_STATIC);
   2468  1.188     ozaki }
   2469  1.188     ozaki 
   2470    1.2    itojun int
   2471  1.110    dyoung nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt,
   2472  1.127    dyoung     struct mbuf *m, const struct sockaddr *dst, uint8_t *lldst,
   2473  1.110    dyoung     size_t dstsize)
   2474    1.2    itojun {
   2475  1.188     ozaki 	struct llentry *ln;
   2476    1.2    itojun 
   2477    1.2    itojun 	if (m->m_flags & M_MCAST) {
   2478    1.2    itojun 		switch (ifp->if_type) {
   2479    1.2    itojun 		case IFT_ETHER:
   2480   1.63    itojun 		case IFT_FDDI:
   2481  1.118    dyoung 			ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
   2482  1.118    dyoung 			    lldst);
   2483  1.110    dyoung 			return 1;
   2484   1.33      onoe 		case IFT_IEEE1394:
   2485  1.118    dyoung 			memcpy(lldst, ifp->if_broadcastaddr,
   2486  1.118    dyoung 			    MIN(dstsize, ifp->if_addrlen));
   2487  1.110    dyoung 			return 1;
   2488    1.9        is 		case IFT_ARCNET:
   2489  1.110    dyoung 			*lldst = 0;
   2490  1.110    dyoung 			return 1;
   2491    1.2    itojun 		default:
   2492   1.43    itojun 			m_freem(m);
   2493  1.110    dyoung 			return 0;
   2494    1.2    itojun 		}
   2495    1.2    itojun 	}
   2496    1.2    itojun 
   2497  1.188     ozaki 	/*
   2498  1.188     ozaki 	 * the entry should have been created in nd6_store_lladdr
   2499  1.188     ozaki 	 */
   2500  1.188     ozaki 	ln = nd6_lookup(&satocsin6(dst)->sin6_addr, ifp, false);
   2501  1.188     ozaki 	if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
   2502  1.188     ozaki 		if (ln != NULL)
   2503  1.188     ozaki 			LLE_RUNLOCK(ln);
   2504   1.32    itojun 		/* this could happen, if we could not allocate memory */
   2505   1.43    itojun 		m_freem(m);
   2506  1.110    dyoung 		return 0;
   2507   1.32    itojun 	}
   2508  1.188     ozaki 
   2509  1.188     ozaki 	/* XXX llentry should have addrlen? */
   2510  1.188     ozaki #if 0
   2511  1.118    dyoung 	sdl = satocsdl(rt->rt_gateway);
   2512  1.110    dyoung 	if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) {
   2513  1.155  christos 		char sbuf[INET6_ADDRSTRLEN];
   2514  1.155  christos 		char dbuf[LINK_ADDRSTRLEN];
   2515   1.24    itojun 		/* this should be impossible, but we bark here for debugging */
   2516  1.155  christos 		printf("%s: sdl_alen == %" PRIu8 ", if=%s, dst=%s, sdl=%s\n",
   2517  1.155  christos 		    __func__, sdl->sdl_alen, if_name(ifp),
   2518  1.155  christos 		    IN6_PRINT(sbuf, &satocsin6(dst)->sin6_addr),
   2519  1.155  christos 		    DL_PRINT(dbuf, &sdl->sdl_addr));
   2520   1.43    itojun 		m_freem(m);
   2521  1.110    dyoung 		return 0;
   2522   1.23    itojun 	}
   2523  1.188     ozaki #endif
   2524  1.188     ozaki 
   2525  1.188     ozaki 	memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
   2526  1.188     ozaki 
   2527  1.188     ozaki 	LLE_RUNLOCK(ln);
   2528    1.2    itojun 
   2529  1.110    dyoung 	return 1;
   2530   1.65    itojun }
   2531   1.65    itojun 
   2532  1.100    rpaulo static void
   2533  1.181     ozaki clear_llinfo_pqueue(struct llentry *ln)
   2534  1.100    rpaulo {
   2535  1.100    rpaulo 	struct mbuf *m_hold, *m_hold_next;
   2536  1.100    rpaulo 
   2537  1.100    rpaulo 	for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
   2538  1.100    rpaulo 		m_hold_next = m_hold->m_nextpkt;
   2539  1.100    rpaulo 		m_hold->m_nextpkt = NULL;
   2540  1.100    rpaulo 		m_freem(m_hold);
   2541  1.100    rpaulo 	}
   2542  1.100    rpaulo 
   2543  1.100    rpaulo 	ln->ln_hold = NULL;
   2544  1.100    rpaulo 	return;
   2545  1.100    rpaulo }
   2546  1.100    rpaulo 
   2547   1.65    itojun int
   2548  1.105  christos nd6_sysctl(
   2549  1.105  christos     int name,
   2550  1.105  christos     void *oldp,	/* syscall arg, need copyout */
   2551  1.105  christos     size_t *oldlenp,
   2552  1.105  christos     void *newp,	/* syscall arg, need copyin */
   2553  1.107  christos     size_t newlen
   2554  1.105  christos )
   2555   1.65    itojun {
   2556   1.69    itojun 	void *p;
   2557   1.88    simonb 	size_t ol;
   2558   1.65    itojun 	int error;
   2559   1.65    itojun 
   2560   1.65    itojun 	error = 0;
   2561   1.65    itojun 
   2562   1.65    itojun 	if (newp)
   2563   1.65    itojun 		return EPERM;
   2564   1.65    itojun 	if (oldp && !oldlenp)
   2565   1.65    itojun 		return EINVAL;
   2566   1.65    itojun 	ol = oldlenp ? *oldlenp : 0;
   2567   1.65    itojun 
   2568   1.69    itojun 	if (oldp) {
   2569   1.69    itojun 		p = malloc(*oldlenp, M_TEMP, M_WAITOK);
   2570   1.99    rpaulo 		if (p == NULL)
   2571   1.69    itojun 			return ENOMEM;
   2572   1.69    itojun 	} else
   2573   1.69    itojun 		p = NULL;
   2574   1.65    itojun 	switch (name) {
   2575   1.65    itojun 	case ICMPV6CTL_ND6_DRLIST:
   2576   1.69    itojun 		error = fill_drlist(p, oldlenp, ol);
   2577   1.99    rpaulo 		if (!error && p != NULL && oldp != NULL)
   2578   1.71    itojun 			error = copyout(p, oldp, *oldlenp);
   2579   1.65    itojun 		break;
   2580   1.65    itojun 
   2581   1.65    itojun 	case ICMPV6CTL_ND6_PRLIST:
   2582   1.69    itojun 		error = fill_prlist(p, oldlenp, ol);
   2583   1.99    rpaulo 		if (!error && p != NULL && oldp != NULL)
   2584   1.71    itojun 			error = copyout(p, oldp, *oldlenp);
   2585   1.65    itojun 		break;
   2586   1.65    itojun 
   2587   1.99    rpaulo 	case ICMPV6CTL_ND6_MAXQLEN:
   2588   1.99    rpaulo 		break;
   2589   1.99    rpaulo 
   2590   1.65    itojun 	default:
   2591   1.65    itojun 		error = ENOPROTOOPT;
   2592   1.65    itojun 		break;
   2593   1.65    itojun 	}
   2594   1.69    itojun 	if (p)
   2595   1.69    itojun 		free(p, M_TEMP);
   2596   1.65    itojun 
   2597  1.112    dyoung 	return error;
   2598   1.65    itojun }
   2599   1.65    itojun 
   2600   1.65    itojun static int
   2601  1.112    dyoung fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
   2602   1.65    itojun {
   2603   1.65    itojun 	int error = 0, s;
   2604   1.65    itojun 	struct in6_defrouter *d = NULL, *de = NULL;
   2605   1.65    itojun 	struct nd_defrouter *dr;
   2606   1.65    itojun 	size_t l;
   2607   1.65    itojun 
   2608   1.65    itojun 	s = splsoftnet();
   2609   1.66    itojun 
   2610   1.65    itojun 	if (oldp) {
   2611   1.65    itojun 		d = (struct in6_defrouter *)oldp;
   2612  1.111  christos 		de = (struct in6_defrouter *)((char *)oldp + *oldlenp);
   2613   1.65    itojun 	}
   2614   1.65    itojun 	l = 0;
   2615   1.65    itojun 
   2616  1.108    dyoung 	TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
   2617   1.65    itojun 
   2618   1.65    itojun 		if (oldp && d + 1 <= de) {
   2619  1.122    dyoung 			memset(d, 0, sizeof(*d));
   2620  1.122    dyoung 			sockaddr_in6_init(&d->rtaddr, &dr->rtaddr, 0, 0, 0);
   2621   1.96    rpaulo 			if (sa6_recoverscope(&d->rtaddr)) {
   2622   1.96    rpaulo 				log(LOG_ERR,
   2623   1.96    rpaulo 				    "scope error in router list (%s)\n",
   2624   1.96    rpaulo 				    ip6_sprintf(&d->rtaddr.sin6_addr));
   2625   1.96    rpaulo 				/* XXX: press on... */
   2626   1.96    rpaulo 			}
   2627   1.65    itojun 			d->flags = dr->flags;
   2628   1.65    itojun 			d->rtlifetime = dr->rtlifetime;
   2629  1.166     ozaki 			d->expire = dr->expire ?
   2630  1.166     ozaki 			    time_mono_to_wall(dr->expire) : 0;
   2631   1.65    itojun 			d->if_index = dr->ifp->if_index;
   2632   1.65    itojun 		}
   2633   1.65    itojun 
   2634   1.65    itojun 		l += sizeof(*d);
   2635   1.65    itojun 		if (d)
   2636   1.65    itojun 			d++;
   2637   1.65    itojun 	}
   2638   1.65    itojun 
   2639   1.65    itojun 	if (oldp) {
   2640   1.65    itojun 		if (l > ol)
   2641   1.65    itojun 			error = ENOMEM;
   2642  1.101  christos 	}
   2643  1.101  christos 	if (oldlenp)
   2644  1.111  christos 		*oldlenp = l;	/* (void *)d - (void *)oldp */
   2645   1.65    itojun 
   2646   1.65    itojun 	splx(s);
   2647   1.65    itojun 
   2648  1.112    dyoung 	return error;
   2649   1.65    itojun }
   2650   1.65    itojun 
   2651   1.65    itojun static int
   2652  1.112    dyoung fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
   2653   1.65    itojun {
   2654   1.65    itojun 	int error = 0, s;
   2655   1.65    itojun 	struct nd_prefix *pr;
   2656  1.146    martin 	uint8_t *p = NULL, *ps = NULL;
   2657  1.146    martin 	uint8_t *pe = NULL;
   2658   1.65    itojun 	size_t l;
   2659   1.65    itojun 
   2660   1.65    itojun 	s = splsoftnet();
   2661   1.65    itojun 
   2662   1.65    itojun 	if (oldp) {
   2663  1.146    martin 		ps = p = (uint8_t*)oldp;
   2664  1.146    martin 		pe = (uint8_t*)oldp + *oldlenp;
   2665   1.65    itojun 	}
   2666   1.65    itojun 	l = 0;
   2667   1.65    itojun 
   2668  1.108    dyoung 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   2669   1.65    itojun 		u_short advrtrs;
   2670  1.146    martin 		struct sockaddr_in6 sin6;
   2671   1.65    itojun 		struct nd_pfxrouter *pfr;
   2672  1.146    martin 		struct in6_prefix pfx;
   2673   1.65    itojun 
   2674  1.146    martin 		if (oldp && p + sizeof(struct in6_prefix) <= pe)
   2675   1.65    itojun 		{
   2676  1.146    martin 			memset(&pfx, 0, sizeof(pfx));
   2677  1.146    martin 			ps = p;
   2678  1.146    martin 			pfx.prefix = pr->ndpr_prefix;
   2679   1.65    itojun 
   2680  1.146    martin 			if (sa6_recoverscope(&pfx.prefix)) {
   2681   1.65    itojun 				log(LOG_ERR,
   2682   1.65    itojun 				    "scope error in prefix list (%s)\n",
   2683  1.146    martin 				    ip6_sprintf(&pfx.prefix.sin6_addr));
   2684   1.96    rpaulo 				/* XXX: press on... */
   2685   1.96    rpaulo 			}
   2686  1.146    martin 			pfx.raflags = pr->ndpr_raf;
   2687  1.146    martin 			pfx.prefixlen = pr->ndpr_plen;
   2688  1.146    martin 			pfx.vltime = pr->ndpr_vltime;
   2689  1.146    martin 			pfx.pltime = pr->ndpr_pltime;
   2690  1.146    martin 			pfx.if_index = pr->ndpr_ifp->if_index;
   2691   1.65    itojun 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
   2692  1.146    martin 				pfx.expire = 0;
   2693   1.65    itojun 			else {
   2694   1.65    itojun 				time_t maxexpire;
   2695   1.65    itojun 
   2696   1.65    itojun 				/* XXX: we assume time_t is signed. */
   2697   1.65    itojun 				maxexpire = (-1) &
   2698   1.99    rpaulo 				    ~((time_t)1 <<
   2699   1.99    rpaulo 				    ((sizeof(maxexpire) * 8) - 1));
   2700   1.65    itojun 				if (pr->ndpr_vltime <
   2701   1.65    itojun 				    maxexpire - pr->ndpr_lastupdate) {
   2702  1.146    martin 					pfx.expire = pr->ndpr_lastupdate +
   2703   1.65    itojun 						pr->ndpr_vltime;
   2704   1.65    itojun 				} else
   2705  1.146    martin 					pfx.expire = maxexpire;
   2706   1.65    itojun 			}
   2707  1.146    martin 			pfx.refcnt = pr->ndpr_refcnt;
   2708  1.146    martin 			pfx.flags = pr->ndpr_stateflags;
   2709  1.146    martin 			pfx.origin = PR_ORIG_RA;
   2710  1.146    martin 
   2711  1.146    martin 			p += sizeof(pfx); l += sizeof(pfx);
   2712  1.146    martin 
   2713   1.65    itojun 			advrtrs = 0;
   2714  1.108    dyoung 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
   2715  1.146    martin 				if (p + sizeof(sin6) > pe) {
   2716   1.65    itojun 					advrtrs++;
   2717   1.65    itojun 					continue;
   2718   1.65    itojun 				}
   2719  1.146    martin 
   2720  1.146    martin 				sockaddr_in6_init(&sin6, &pfr->router->rtaddr,
   2721  1.122    dyoung 				    0, 0, 0);
   2722  1.146    martin 				if (sa6_recoverscope(&sin6)) {
   2723   1.96    rpaulo 					log(LOG_ERR,
   2724   1.96    rpaulo 					    "scope error in "
   2725   1.96    rpaulo 					    "prefix list (%s)\n",
   2726   1.96    rpaulo 					    ip6_sprintf(&pfr->router->rtaddr));
   2727   1.96    rpaulo 				}
   2728   1.65    itojun 				advrtrs++;
   2729  1.146    martin 				memcpy(p, &sin6, sizeof(sin6));
   2730  1.146    martin 				p += sizeof(sin6);
   2731  1.146    martin 				l += sizeof(sin6);
   2732   1.65    itojun 			}
   2733  1.146    martin 			pfx.advrtrs = advrtrs;
   2734  1.146    martin 			memcpy(ps, &pfx, sizeof(pfx));
   2735   1.65    itojun 		}
   2736   1.65    itojun 		else {
   2737  1.146    martin 			l += sizeof(pfx);
   2738   1.65    itojun 			advrtrs = 0;
   2739  1.146    martin 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
   2740   1.65    itojun 				advrtrs++;
   2741  1.146    martin 				l += sizeof(sin6);
   2742  1.146    martin 			}
   2743   1.65    itojun 		}
   2744   1.65    itojun 	}
   2745   1.65    itojun 
   2746   1.65    itojun 	if (oldp) {
   2747  1.111  christos 		*oldlenp = l;	/* (void *)d - (void *)oldp */
   2748   1.65    itojun 		if (l > ol)
   2749   1.65    itojun 			error = ENOMEM;
   2750   1.65    itojun 	} else
   2751   1.65    itojun 		*oldlenp = l;
   2752   1.65    itojun 
   2753   1.65    itojun 	splx(s);
   2754   1.65    itojun 
   2755  1.112    dyoung 	return error;
   2756    1.2    itojun }
   2757