Home | History | Annotate | Line # | Download | only in netinet6
nd6_rtr.c revision 1.121
      1  1.121     ozaki /*	$NetBSD: nd6_rtr.c,v 1.121 2016/12/11 07:36:55 ozaki-r Exp $	*/
      2   1.20    itojun /*	$KAME: nd6_rtr.c,v 1.95 2001/02/07 08:09:47 itojun Exp $	*/
      3    1.3   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.16    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.16    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.27     lukem 
     33   1.27     lukem #include <sys/cdefs.h>
     34  1.121     ozaki __KERNEL_RCSID(0, "$NetBSD: nd6_rtr.c,v 1.121 2016/12/11 07:36:55 ozaki-r Exp $");
     35    1.2    itojun 
     36    1.2    itojun #include <sys/param.h>
     37    1.2    itojun #include <sys/systm.h>
     38    1.2    itojun #include <sys/malloc.h>
     39    1.2    itojun #include <sys/mbuf.h>
     40    1.2    itojun #include <sys/socket.h>
     41    1.2    itojun #include <sys/sockio.h>
     42    1.2    itojun #include <sys/time.h>
     43    1.2    itojun #include <sys/kernel.h>
     44    1.2    itojun #include <sys/errno.h>
     45    1.2    itojun #include <sys/ioctl.h>
     46    1.2    itojun #include <sys/syslog.h>
     47   1.82       tls #include <sys/cprng.h>
     48    1.2    itojun 
     49    1.2    itojun #include <net/if.h>
     50    1.2    itojun #include <net/if_types.h>
     51    1.2    itojun #include <net/if_dl.h>
     52    1.2    itojun 
     53    1.2    itojun #include <netinet/in.h>
     54    1.2    itojun #include <netinet6/in6_var.h>
     55   1.56    rpaulo #include <netinet6/in6_ifattach.h>
     56   1.11    itojun #include <netinet/ip6.h>
     57    1.2    itojun #include <netinet6/ip6_var.h>
     58    1.2    itojun #include <netinet6/nd6.h>
     59   1.11    itojun #include <netinet/icmp6.h>
     60   1.75   thorpej #include <netinet6/icmp6_private.h>
     61   1.54    rpaulo #include <netinet6/scope6_var.h>
     62    1.2    itojun 
     63    1.7    itojun #include <net/net_osdep.h>
     64    1.2    itojun 
     65   1.68    dyoung static int rtpref(struct nd_defrouter *);
     66   1.68    dyoung static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
     67   1.68    dyoung static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
     68   1.68    dyoung     struct mbuf *, int);
     69  1.118     ozaki static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int, struct psref *);
     70   1.68    dyoung static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
     71   1.68    dyoung 	struct nd_defrouter *);
     72   1.68    dyoung static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
     73   1.68    dyoung static void pfxrtr_del(struct nd_pfxrouter *);
     74   1.32    itojun static struct nd_pfxrouter *find_pfxlist_reachable_router
     75   1.68    dyoung 	(struct nd_prefix *);
     76  1.121     ozaki 
     77  1.121     ozaki static void defrouter_addreq(struct nd_defrouter *);
     78   1.68    dyoung static void defrouter_delreq(struct nd_defrouter *);
     79   1.68    dyoung 
     80   1.68    dyoung static int in6_init_prefix_ltimes(struct nd_prefix *);
     81   1.83  christos static void in6_init_address_ltimes(struct nd_prefix *,
     82   1.83  christos 	struct in6_addrlifetime *);
     83   1.83  christos static void purge_detached(struct ifnet *);
     84    1.2    itojun 
     85  1.120     ozaki static int rt6_deleteroute_matcher(struct rtentry *, void *);
     86    1.2    itojun 
     87  1.121     ozaki static int nd6_prelist_add(struct nd_prefixctl *, struct nd_defrouter *,
     88  1.121     ozaki 	struct nd_prefix **);
     89  1.121     ozaki static int nd6_prefix_onlink(struct nd_prefix *);
     90  1.121     ozaki static int nd6_prefix_offlink(struct nd_prefix *);
     91  1.121     ozaki static struct nd_prefix *nd6_prefix_lookup(struct nd_prefixctl *);
     92  1.121     ozaki 
     93    1.2    itojun extern int nd6_recalc_reachtm_interval;
     94    1.2    itojun 
     95   1.28    itojun static struct ifnet *nd6_defifp;
     96    1.7    itojun int nd6_defifindex;
     97    1.7    itojun 
     98   1.56    rpaulo int ip6_use_tempaddr = 0;
     99   1.56    rpaulo 
    100   1.56    rpaulo int ip6_desync_factor;
    101   1.56    rpaulo u_int32_t ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME;
    102   1.56    rpaulo u_int32_t ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME;
    103   1.56    rpaulo int ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE;
    104   1.56    rpaulo 
    105   1.81       spz int nd6_numroutes = 0;
    106   1.81       spz 
    107   1.56    rpaulo /* RTPREF_MEDIUM has to be 0! */
    108   1.56    rpaulo #define RTPREF_HIGH	1
    109   1.56    rpaulo #define RTPREF_MEDIUM	0
    110   1.56    rpaulo #define RTPREF_LOW	(-1)
    111   1.56    rpaulo #define RTPREF_RESERVED	(-2)
    112   1.56    rpaulo #define RTPREF_INVALID	(-3)	/* internal */
    113   1.56    rpaulo 
    114  1.101     ozaki static inline bool
    115  1.101     ozaki nd6_is_llinfo_probreach(struct nd_defrouter *dr)
    116  1.101     ozaki {
    117  1.105     ozaki 	struct llentry *ln = NULL;
    118  1.101     ozaki 
    119  1.108     ozaki 	ln = nd6_lookup(&dr->rtaddr, dr->ifp, false);
    120  1.108     ozaki 	if (ln == NULL)
    121  1.101     ozaki 		return false;
    122  1.108     ozaki 
    123  1.108     ozaki 	if (!ND6_IS_LLINFO_PROBREACH(ln)) {
    124  1.108     ozaki 		LLE_RUNLOCK(ln);
    125  1.101     ozaki 		return false;
    126  1.108     ozaki 	}
    127  1.101     ozaki 
    128  1.108     ozaki 	LLE_RUNLOCK(ln);
    129  1.101     ozaki 	return true;
    130  1.101     ozaki }
    131  1.101     ozaki 
    132    1.2    itojun /*
    133    1.2    itojun  * Receive Router Solicitation Message - just for routers.
    134   1.81       spz  * Router solicitation/advertisement is mostly managed by a userland program
    135    1.2    itojun  * (rtadvd) so here we have no function like nd6_ra_output().
    136    1.2    itojun  *
    137    1.2    itojun  * Based on RFC 2461
    138    1.2    itojun  */
    139    1.2    itojun void
    140   1.64  christos nd6_rs_input(struct mbuf *m, int off, int icmp6len)
    141    1.2    itojun {
    142  1.111     ozaki 	struct ifnet *ifp;
    143  1.111     ozaki 	struct nd_ifinfo *ndi;
    144    1.2    itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    145   1.12    itojun 	struct nd_router_solicit *nd_rs;
    146    1.2    itojun 	struct in6_addr saddr6 = ip6->ip6_src;
    147    1.2    itojun 	char *lladdr = NULL;
    148    1.2    itojun 	int lladdrlen = 0;
    149    1.2    itojun 	union nd_opts ndopts;
    150  1.111     ozaki 	struct psref psref;
    151  1.111     ozaki 
    152  1.111     ozaki 	ifp = m_get_rcvif_psref(m, &psref);
    153  1.111     ozaki 	if (ifp == NULL)
    154  1.111     ozaki 		goto freeit;
    155  1.111     ozaki 
    156  1.111     ozaki 	ndi = ND_IFINFO(ifp);
    157    1.2    itojun 
    158    1.2    itojun 	/* If I'm not a router, ignore it. */
    159   1.80    dyoung 	if (nd6_accepts_rtadv(ndi) || !ip6_forwarding)
    160   1.12    itojun 		goto freeit;
    161    1.2    itojun 
    162    1.2    itojun 	/* Sanity checks */
    163    1.2    itojun 	if (ip6->ip6_hlim != 255) {
    164  1.107     ozaki 		nd6log(LOG_ERR, "invalid hlim (%d) from %s to %s on %s\n",
    165   1.20    itojun 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
    166  1.107     ozaki 		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
    167   1.20    itojun 		goto bad;
    168    1.2    itojun 	}
    169    1.2    itojun 
    170    1.2    itojun 	/*
    171    1.2    itojun 	 * Don't update the neighbor cache, if src = ::.
    172    1.2    itojun 	 * This indicates that the src has no IP address assigned yet.
    173    1.2    itojun 	 */
    174    1.2    itojun 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
    175   1.12    itojun 		goto freeit;
    176   1.12    itojun 
    177   1.12    itojun 	IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
    178   1.12    itojun 	if (nd_rs == NULL) {
    179   1.75   thorpej 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    180    1.2    itojun 		return;
    181   1.12    itojun 	}
    182    1.2    itojun 
    183    1.2    itojun 	icmp6len -= sizeof(*nd_rs);
    184    1.2    itojun 	nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
    185    1.2    itojun 	if (nd6_options(&ndopts) < 0) {
    186  1.107     ozaki 		nd6log(LOG_INFO, "invalid ND option, ignored\n");
    187   1.20    itojun 		/* nd6_options have incremented stats */
    188   1.12    itojun 		goto freeit;
    189    1.2    itojun 	}
    190    1.2    itojun 
    191    1.2    itojun 	if (ndopts.nd_opts_src_lladdr) {
    192    1.2    itojun 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
    193    1.2    itojun 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
    194    1.2    itojun 	}
    195    1.2    itojun 
    196    1.2    itojun 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    197  1.107     ozaki 		nd6log(LOG_INFO, "lladdrlen mismatch for %s "
    198    1.2    itojun 		    "(if %d, RS packet %d)\n",
    199  1.107     ozaki 		    ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2);
    200   1.20    itojun 		goto bad;
    201    1.2    itojun 	}
    202    1.2    itojun 
    203    1.6    itojun 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
    204   1.12    itojun 
    205   1.12    itojun  freeit:
    206  1.111     ozaki 	m_put_rcvif_psref(ifp, &psref);
    207   1.12    itojun 	m_freem(m);
    208   1.20    itojun 	return;
    209   1.20    itojun 
    210   1.20    itojun  bad:
    211   1.75   thorpej 	ICMP6_STATINC(ICMP6_STAT_BADRS);
    212  1.111     ozaki 	m_put_rcvif_psref(ifp, &psref);
    213   1.20    itojun 	m_freem(m);
    214    1.2    itojun }
    215    1.2    itojun 
    216    1.2    itojun /*
    217    1.2    itojun  * Receive Router Advertisement Message.
    218    1.2    itojun  *
    219    1.2    itojun  * Based on RFC 2461
    220    1.2    itojun  * TODO: on-link bit on prefix information
    221    1.2    itojun  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
    222    1.2    itojun  */
    223    1.2    itojun void
    224   1.64  christos nd6_ra_input(struct mbuf *m, int off, int icmp6len)
    225    1.2    itojun {
    226  1.111     ozaki 	struct ifnet *ifp;
    227  1.111     ozaki 	struct nd_ifinfo *ndi;
    228    1.2    itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    229   1.12    itojun 	struct nd_router_advert *nd_ra;
    230    1.2    itojun 	struct in6_addr saddr6 = ip6->ip6_src;
    231    1.2    itojun #if 0
    232    1.2    itojun 	struct in6_addr daddr6 = ip6->ip6_dst;
    233   1.12    itojun 	int flags; /* = nd_ra->nd_ra_flags_reserved; */
    234    1.2    itojun 	int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
    235    1.2    itojun 	int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
    236    1.2    itojun #endif
    237   1.56    rpaulo 	int mcast = 0;
    238    1.2    itojun 	union nd_opts ndopts;
    239    1.2    itojun 	struct nd_defrouter *dr;
    240  1.111     ozaki 	struct psref psref;
    241  1.111     ozaki 
    242  1.111     ozaki 	ifp = m_get_rcvif_psref(m, &psref);
    243  1.111     ozaki 	if (ifp == NULL)
    244  1.111     ozaki 		goto freeit;
    245    1.2    itojun 
    246  1.111     ozaki 	ndi = ND_IFINFO(ifp);
    247   1.32    itojun 	/*
    248   1.81       spz 	 * We only accept RAs when
    249   1.81       spz 	 * the system-wide variable allows the acceptance, and the
    250   1.33    itojun 	 * per-interface variable allows RAs on the receiving interface.
    251   1.32    itojun 	 */
    252   1.80    dyoung 	if (!nd6_accepts_rtadv(ndi))
    253   1.32    itojun 		goto freeit;
    254    1.2    itojun 
    255    1.2    itojun 	if (ip6->ip6_hlim != 255) {
    256  1.107     ozaki 		nd6log(LOG_ERR, "invalid hlim (%d) from %s to %s on %s\n",
    257   1.20    itojun 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
    258  1.107     ozaki 		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
    259   1.20    itojun 		goto bad;
    260    1.2    itojun 	}
    261    1.2    itojun 
    262    1.2    itojun 	if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
    263  1.107     ozaki 		nd6log(LOG_ERR, "src %s is not link-local\n",
    264  1.107     ozaki 		    ip6_sprintf(&saddr6));
    265   1.20    itojun 		goto bad;
    266   1.12    itojun 	}
    267   1.12    itojun 
    268   1.12    itojun 	IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
    269   1.12    itojun 	if (nd_ra == NULL) {
    270   1.75   thorpej 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    271  1.111     ozaki 		m_put_rcvif_psref(ifp, &psref);
    272    1.2    itojun 		return;
    273    1.2    itojun 	}
    274    1.2    itojun 
    275    1.2    itojun 	icmp6len -= sizeof(*nd_ra);
    276    1.2    itojun 	nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
    277    1.2    itojun 	if (nd6_options(&ndopts) < 0) {
    278  1.107     ozaki 		nd6log(LOG_INFO, "invalid ND option, ignored\n");
    279   1.20    itojun 		/* nd6_options have incremented stats */
    280   1.12    itojun 		goto freeit;
    281    1.2    itojun 	}
    282    1.2    itojun 
    283    1.2    itojun     {
    284   1.52  christos 	struct nd_defrouter drtr;
    285    1.2    itojun 	u_int32_t advreachable = nd_ra->nd_ra_reachable;
    286    1.2    itojun 
    287   1.56    rpaulo 	/* remember if this is a multicasted advertisement */
    288   1.56    rpaulo 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
    289   1.56    rpaulo 		mcast = 1;
    290   1.56    rpaulo 
    291   1.56    rpaulo 	memset(&drtr, 0, sizeof(drtr));
    292   1.52  christos 	drtr.rtaddr = saddr6;
    293   1.52  christos 	drtr.flags  = nd_ra->nd_ra_flags_reserved;
    294   1.58    rpaulo 	drtr.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
    295  1.102     ozaki 	drtr.expire = time_uptime + drtr.rtlifetime;
    296   1.52  christos 	drtr.ifp = ifp;
    297    1.2    itojun 	/* unspecified or not? (RFC 2461 6.3.4) */
    298    1.2    itojun 	if (advreachable) {
    299    1.2    itojun 		NTOHL(advreachable);
    300    1.2    itojun 		if (advreachable <= MAX_REACHABLE_TIME &&
    301    1.2    itojun 		    ndi->basereachable != advreachable) {
    302    1.2    itojun 			ndi->basereachable = advreachable;
    303    1.2    itojun 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
    304    1.2    itojun 			ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
    305    1.2    itojun 		}
    306    1.2    itojun 	}
    307    1.2    itojun 	if (nd_ra->nd_ra_retransmit)
    308    1.2    itojun 		ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
    309   1.99       roy 	if (nd_ra->nd_ra_curhoplimit) {
    310   1.99       roy 		if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
    311   1.99       roy 			ndi->chlim = nd_ra->nd_ra_curhoplimit;
    312   1.99       roy 		else if (ndi->chlim != nd_ra->nd_ra_curhoplimit)
    313   1.99       roy 			log(LOG_ERR, "nd_ra_input: lower CurHopLimit sent from "
    314   1.99       roy 			   "%s on %s (current=%d, received=%d), ignored\n",
    315   1.99       roy 			   ip6_sprintf(&ip6->ip6_src),
    316   1.99       roy 			   if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
    317   1.99       roy 	}
    318   1.52  christos 	dr = defrtrlist_update(&drtr);
    319    1.2    itojun     }
    320    1.2    itojun 
    321    1.2    itojun 	/*
    322    1.2    itojun 	 * prefix
    323    1.2    itojun 	 */
    324    1.2    itojun 	if (ndopts.nd_opts_pi) {
    325    1.2    itojun 		struct nd_opt_hdr *pt;
    326   1.32    itojun 		struct nd_opt_prefix_info *pi = NULL;
    327   1.94      matt 		struct nd_prefixctl prc;
    328    1.2    itojun 
    329    1.2    itojun 		for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
    330    1.2    itojun 		     pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
    331   1.63  christos 		     pt = (struct nd_opt_hdr *)((char *)pt +
    332    1.2    itojun 						(pt->nd_opt_len << 3))) {
    333    1.2    itojun 			if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
    334    1.2    itojun 				continue;
    335    1.2    itojun 			pi = (struct nd_opt_prefix_info *)pt;
    336    1.2    itojun 
    337    1.2    itojun 			if (pi->nd_opt_pi_len != 4) {
    338  1.107     ozaki 				nd6log(LOG_INFO, "invalid option "
    339   1.22    itojun 				    "len %d for prefix information option, "
    340  1.107     ozaki 				    "ignored\n", pi->nd_opt_pi_len);
    341    1.2    itojun 				continue;
    342    1.2    itojun 			}
    343    1.2    itojun 
    344    1.2    itojun 			if (128 < pi->nd_opt_pi_prefix_len) {
    345  1.107     ozaki 				nd6log(LOG_INFO, "invalid prefix "
    346   1.22    itojun 				    "len %d for prefix information option, "
    347  1.107     ozaki 				    "ignored\n", pi->nd_opt_pi_prefix_len);
    348    1.2    itojun 				continue;
    349    1.2    itojun 			}
    350    1.2    itojun 
    351    1.2    itojun 			if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
    352    1.2    itojun 			 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
    353  1.107     ozaki 				nd6log(LOG_INFO,
    354  1.107     ozaki 				    "invalid prefix %s, ignored\n",
    355  1.107     ozaki 				    ip6_sprintf(&pi->nd_opt_pi_prefix));
    356    1.2    itojun 				continue;
    357    1.2    itojun 			}
    358    1.2    itojun 
    359   1.94      matt 			memset(&prc, 0, sizeof(prc));
    360   1.94      matt 			sockaddr_in6_init(&prc.ndprc_prefix,
    361   1.69    dyoung 			    &pi->nd_opt_pi_prefix, 0, 0, 0);
    362  1.111     ozaki 			prc.ndprc_ifp = ifp;
    363    1.2    itojun 
    364   1.94      matt 			prc.ndprc_raf_onlink = (pi->nd_opt_pi_flags_reserved &
    365   1.56    rpaulo 			    ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
    366   1.94      matt 			prc.ndprc_raf_auto = (pi->nd_opt_pi_flags_reserved &
    367   1.56    rpaulo 			    ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
    368   1.94      matt 			prc.ndprc_plen = pi->nd_opt_pi_prefix_len;
    369   1.94      matt 			prc.ndprc_vltime = ntohl(pi->nd_opt_pi_valid_time);
    370   1.94      matt 			prc.ndprc_pltime = ntohl(pi->nd_opt_pi_preferred_time);
    371    1.2    itojun 
    372   1.94      matt 			(void)prelist_update(&prc, dr, m, mcast);
    373    1.2    itojun 		}
    374    1.2    itojun 	}
    375    1.2    itojun 
    376    1.2    itojun 	/*
    377    1.2    itojun 	 * MTU
    378    1.2    itojun 	 */
    379    1.2    itojun 	if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
    380   1.32    itojun 		u_long mtu;
    381   1.30    itojun 		u_long maxmtu;
    382    1.2    itojun 
    383   1.32    itojun 		mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
    384   1.32    itojun 
    385    1.2    itojun 		/* lower bound */
    386    1.2    itojun 		if (mtu < IPV6_MMTU) {
    387  1.107     ozaki 			nd6log(LOG_INFO, "bogus mtu option "
    388   1.32    itojun 			    "mtu=%lu sent from %s, ignoring\n",
    389  1.107     ozaki 			    mtu, ip6_sprintf(&ip6->ip6_src));
    390    1.2    itojun 			goto skip;
    391    1.2    itojun 		}
    392    1.2    itojun 
    393    1.2    itojun 		/* upper bound */
    394   1.30    itojun 		maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
    395   1.30    itojun 		    ? ndi->maxmtu : ifp->if_mtu;
    396   1.30    itojun 		if (mtu <= maxmtu) {
    397   1.30    itojun 			int change = (ndi->linkmtu != mtu);
    398   1.30    itojun 
    399   1.30    itojun 			ndi->linkmtu = mtu;
    400   1.30    itojun 			if (change) /* in6_maxmtu may change */
    401   1.30    itojun 				in6_setmaxmtu();
    402    1.2    itojun 		} else {
    403  1.107     ozaki 			nd6log(LOG_INFO,
    404  1.107     ozaki 			    "bogus mtu mtu=%lu sent from %s; "
    405   1.30    itojun 			    "exceeds maxmtu %lu, ignoring\n",
    406  1.107     ozaki 			    mtu, ip6_sprintf(&ip6->ip6_src), maxmtu);
    407    1.2    itojun 		}
    408    1.2    itojun 	}
    409    1.2    itojun 
    410    1.2    itojun  skip:
    411   1.33    itojun 
    412    1.2    itojun 	/*
    413   1.26    itojun 	 * Source link layer address
    414    1.2    itojun 	 */
    415    1.2    itojun     {
    416    1.2    itojun 	char *lladdr = NULL;
    417    1.2    itojun 	int lladdrlen = 0;
    418   1.33    itojun 
    419    1.2    itojun 	if (ndopts.nd_opts_src_lladdr) {
    420    1.2    itojun 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
    421    1.2    itojun 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
    422    1.2    itojun 	}
    423    1.2    itojun 
    424    1.2    itojun 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    425  1.107     ozaki 		nd6log(LOG_INFO, "lladdrlen mismatch for %s "
    426   1.30    itojun 		    "(if %d, RA packet %d)\n", ip6_sprintf(&saddr6),
    427  1.107     ozaki 		    ifp->if_addrlen, lladdrlen - 2);
    428   1.20    itojun 		goto bad;
    429    1.2    itojun 	}
    430    1.2    itojun 
    431    1.6    itojun 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
    432    1.7    itojun 
    433    1.7    itojun 	/*
    434    1.7    itojun 	 * Installing a link-layer address might change the state of the
    435    1.7    itojun 	 * router's neighbor cache, which might also affect our on-link
    436    1.7    itojun 	 * detection of adveritsed prefixes.
    437    1.7    itojun 	 */
    438    1.7    itojun 	pfxlist_onlink_check();
    439    1.2    itojun     }
    440   1.12    itojun 
    441   1.20    itojun  freeit:
    442  1.111     ozaki 	m_put_rcvif_psref(ifp, &psref);
    443   1.20    itojun 	m_freem(m);
    444   1.20    itojun 	return;
    445   1.20    itojun 
    446   1.20    itojun  bad:
    447   1.75   thorpej 	ICMP6_STATINC(ICMP6_STAT_BADRA);
    448  1.111     ozaki 	m_put_rcvif_psref(ifp, &psref);
    449   1.12    itojun 	m_freem(m);
    450    1.2    itojun }
    451    1.2    itojun 
    452    1.2    itojun /*
    453   1.45       wiz  * default router list processing sub routines
    454    1.2    itojun  */
    455  1.121     ozaki static void
    456   1.94      matt defrouter_addreq(struct nd_defrouter *newdr)
    457    1.2    itojun {
    458   1.76    dyoung 	union {
    459   1.76    dyoung 		struct sockaddr_in6 sin6;
    460   1.76    dyoung 		struct sockaddr sa;
    461   1.76    dyoung 	} def, mask, gate;
    462    1.2    itojun 	int s;
    463   1.32    itojun 	int error;
    464    1.2    itojun 
    465   1.56    rpaulo 	memset(&def, 0, sizeof(def));
    466   1.56    rpaulo 	memset(&mask, 0, sizeof(mask));
    467   1.56    rpaulo 	memset(&gate, 0,sizeof(gate)); /* for safety */
    468    1.2    itojun 
    469   1.76    dyoung 	def.sin6.sin6_len = mask.sin6.sin6_len = gate.sin6.sin6_len =
    470   1.32    itojun 	    sizeof(struct sockaddr_in6);
    471   1.76    dyoung 	def.sin6.sin6_family = mask.sin6.sin6_family = gate.sin6.sin6_family = AF_INET6;
    472   1.94      matt 	gate.sin6.sin6_addr = newdr->rtaddr;
    473   1.32    itojun #ifndef SCOPEDROUTING
    474   1.76    dyoung 	gate.sin6.sin6_scope_id = 0;	/* XXX */
    475   1.32    itojun #endif
    476    1.2    itojun 
    477    1.4    itojun 	s = splsoftnet();
    478  1.101     ozaki 	error = rtrequest_newmsg(RTM_ADD, &def.sa, &gate.sa, &mask.sa,
    479  1.101     ozaki 	    RTF_GATEWAY);
    480  1.101     ozaki 	if (error == 0) {
    481   1.81       spz 		nd6_numroutes++;
    482  1.101     ozaki 		newdr->installed = 1;
    483   1.32    itojun 	}
    484    1.2    itojun 	splx(s);
    485    1.2    itojun 	return;
    486    1.2    itojun }
    487    1.2    itojun 
    488    1.2    itojun struct nd_defrouter *
    489   1.66    dyoung defrouter_lookup(const struct in6_addr *addr, struct ifnet *ifp)
    490    1.2    itojun {
    491    1.2    itojun 	struct nd_defrouter *dr;
    492    1.2    itojun 
    493   1.66    dyoung 	TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
    494   1.66    dyoung 		if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
    495   1.66    dyoung 			break;
    496    1.7    itojun 	}
    497    1.2    itojun 
    498   1.66    dyoung 	return dr;		/* search failed */
    499    1.2    itojun }
    500    1.2    itojun 
    501    1.2    itojun void
    502   1.96    martin defrtrlist_del(struct nd_defrouter *dr, struct in6_ifextra *ext)
    503    1.2    itojun {
    504    1.2    itojun 	struct nd_defrouter *deldr = NULL;
    505    1.2    itojun 	struct nd_prefix *pr;
    506   1.96    martin 	struct nd_ifinfo *ndi;
    507   1.96    martin 
    508   1.96    martin 	if (ext == NULL)
    509   1.96    martin 		ext = dr->ifp->if_afdata[AF_INET6];
    510   1.96    martin 
    511   1.96    martin 	/* detach already in progress, can not do anything */
    512   1.96    martin 	if (ext == NULL)
    513   1.96    martin 		return;
    514   1.96    martin 
    515   1.96    martin 	ndi = ext->nd_ifinfo;
    516    1.2    itojun 
    517    1.2    itojun 	/*
    518    1.2    itojun 	 * Flush all the routing table entries that use the router
    519    1.2    itojun 	 * as a next hop.
    520    1.2    itojun 	 */
    521   1.79  tonnerre 	/* XXX: better condition? */
    522   1.80    dyoung 	if (!ip6_forwarding && nd6_accepts_rtadv(ndi))
    523    1.2    itojun 		rt6_flush(&dr->rtaddr, dr->ifp);
    524   1.32    itojun 
    525   1.32    itojun 	if (dr->installed) {
    526   1.32    itojun 		deldr = dr;
    527   1.32    itojun 		defrouter_delreq(dr);
    528    1.2    itojun 	}
    529    1.7    itojun 	TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
    530    1.2    itojun 
    531    1.2    itojun 	/*
    532    1.2    itojun 	 * Also delete all the pointers to the router in each prefix lists.
    533    1.2    itojun 	 */
    534   1.62    dyoung 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
    535    1.2    itojun 		struct nd_pfxrouter *pfxrtr;
    536    1.2    itojun 		if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
    537    1.2    itojun 			pfxrtr_del(pfxrtr);
    538    1.2    itojun 	}
    539    1.2    itojun 	pfxlist_onlink_check();
    540    1.2    itojun 
    541    1.2    itojun 	/*
    542    1.7    itojun 	 * If the router is the primary one, choose a new one.
    543    1.7    itojun 	 * Note that defrouter_select() will remove the current gateway
    544    1.7    itojun 	 * from the routing table.
    545    1.2    itojun 	 */
    546    1.2    itojun 	if (deldr)
    547    1.7    itojun 		defrouter_select();
    548    1.7    itojun 
    549   1.83  christos 	ext->ndefrouters--;
    550   1.83  christos 	if (ext->ndefrouters < 0) {
    551   1.83  christos 		log(LOG_WARNING, "defrtrlist_del: negative count on %s\n",
    552   1.83  christos 		    dr->ifp->if_xname);
    553   1.83  christos 	}
    554   1.83  christos 
    555    1.2    itojun 	free(dr, M_IP6NDP);
    556    1.2    itojun }
    557    1.2    itojun 
    558    1.7    itojun /*
    559   1.32    itojun  * Remove the default route for a given router.
    560   1.32    itojun  * This is just a subroutine function for defrouter_select(), and should
    561   1.32    itojun  * not be called from anywhere else.
    562   1.32    itojun  */
    563   1.32    itojun static void
    564   1.64  christos defrouter_delreq(struct nd_defrouter *dr)
    565   1.32    itojun {
    566   1.76    dyoung 	union {
    567   1.76    dyoung 		struct sockaddr_in6 sin6;
    568   1.76    dyoung 		struct sockaddr sa;
    569   1.76    dyoung 	} def, mask, gw;
    570  1.101     ozaki 	int error;
    571   1.32    itojun 
    572   1.35    itojun #ifdef DIAGNOSTIC
    573   1.56    rpaulo 	if (dr == NULL)
    574   1.35    itojun 		panic("dr == NULL in defrouter_delreq");
    575   1.35    itojun #endif
    576   1.35    itojun 
    577   1.76    dyoung 	memset(&def, 0, sizeof(def));
    578   1.76    dyoung 	memset(&mask, 0, sizeof(mask));
    579   1.76    dyoung 	memset(&gw, 0, sizeof(gw));	/* for safety */
    580   1.32    itojun 
    581   1.76    dyoung 	def.sin6.sin6_len = mask.sin6.sin6_len = gw.sin6.sin6_len =
    582   1.32    itojun 	    sizeof(struct sockaddr_in6);
    583   1.76    dyoung 	def.sin6.sin6_family = mask.sin6.sin6_family = gw.sin6.sin6_family = AF_INET6;
    584   1.76    dyoung 	gw.sin6.sin6_addr = dr->rtaddr;
    585   1.32    itojun #ifndef SCOPEDROUTING
    586   1.76    dyoung 	gw.sin6.sin6_scope_id = 0;	/* XXX */
    587   1.32    itojun #endif
    588   1.32    itojun 
    589  1.101     ozaki 	error = rtrequest_newmsg(RTM_DELETE, &def.sa, &gw.sa, &mask.sa,
    590  1.101     ozaki 	    RTF_GATEWAY);
    591  1.101     ozaki 	if (error == 0)
    592  1.100     ozaki 		nd6_numroutes--;
    593   1.32    itojun 
    594   1.35    itojun 	dr->installed = 0;
    595   1.32    itojun }
    596   1.32    itojun 
    597   1.32    itojun /*
    598   1.32    itojun  * remove all default routes from default router list
    599   1.32    itojun  */
    600   1.32    itojun void
    601   1.73      matt defrouter_reset(void)
    602   1.32    itojun {
    603   1.32    itojun 	struct nd_defrouter *dr;
    604   1.32    itojun 
    605   1.32    itojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
    606   1.32    itojun 	     dr = TAILQ_NEXT(dr, dr_entry))
    607   1.32    itojun 		defrouter_delreq(dr);
    608   1.32    itojun 
    609   1.32    itojun 	/*
    610   1.32    itojun 	 * XXX should we also nuke any default routers in the kernel, by
    611   1.32    itojun 	 * going through them by rtalloc1()?
    612   1.32    itojun 	 */
    613   1.32    itojun }
    614   1.32    itojun 
    615   1.32    itojun /*
    616   1.32    itojun  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
    617   1.32    itojun  * draft-ietf-ipngwg-router-selection:
    618   1.32    itojun  * 1) Routers that are reachable or probably reachable should be preferred.
    619   1.32    itojun  *    If we have more than one (probably) reachable router, prefer ones
    620   1.32    itojun  *    with the highest router preference.
    621    1.7    itojun  * 2) When no routers on the list are known to be reachable or
    622    1.7    itojun  *    probably reachable, routers SHOULD be selected in a round-robin
    623   1.32    itojun  *    fashion, regardless of router preference values.
    624    1.7    itojun  * 3) If the Default Router List is empty, assume that all
    625    1.7    itojun  *    destinations are on-link.
    626   1.32    itojun  *
    627   1.32    itojun  * We assume nd_defrouter is sorted by router preference value.
    628   1.32    itojun  * Since the code below covers both with and without router preference cases,
    629   1.32    itojun  * we do not need to classify the cases by ifdef.
    630   1.32    itojun  *
    631   1.32    itojun  * At this moment, we do not try to install more than one default router,
    632   1.32    itojun  * even when the multipath routing is available, because we're not sure about
    633   1.32    itojun  * the benefits for stub hosts comparing to the risk of making the code
    634   1.32    itojun  * complicated and the possibility of introducing bugs.
    635    1.7    itojun  */
    636    1.7    itojun void
    637   1.73      matt defrouter_select(void)
    638    1.7    itojun {
    639   1.79  tonnerre 	struct nd_ifinfo *ndi;
    640    1.7    itojun 	int s = splsoftnet();
    641   1.32    itojun 	struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL;
    642    1.7    itojun 
    643    1.7    itojun 	/*
    644   1.32    itojun 	 * This function should be called only when acting as an autoconfigured
    645   1.32    itojun 	 * host.  Although the remaining part of this function is not effective
    646   1.32    itojun 	 * if the node is not an autoconfigured host, we explicitly exclude
    647   1.32    itojun 	 * such cases here for safety.
    648    1.7    itojun 	 */
    649   1.79  tonnerre 	if (ip6_forwarding) {
    650  1.107     ozaki 		nd6log(LOG_WARNING, "called unexpectedly (forwarding=%d, "
    651  1.107     ozaki 		    "accept_rtadv=%d)\n", ip6_forwarding, ip6_accept_rtadv);
    652   1.32    itojun 		splx(s);
    653   1.32    itojun 		return;
    654    1.7    itojun 	}
    655    1.7    itojun 
    656   1.32    itojun 	/*
    657   1.32    itojun 	 * Let's handle easy case (3) first:
    658   1.49    itojun 	 * If default router list is empty, there's nothing to be done.
    659   1.32    itojun 	 */
    660   1.32    itojun 	if (!TAILQ_FIRST(&nd_defrouter)) {
    661   1.32    itojun 		splx(s);
    662   1.32    itojun 		return;
    663   1.32    itojun 	}
    664   1.32    itojun 
    665   1.32    itojun 	/*
    666   1.32    itojun 	 * Search for a (probably) reachable router from the list.
    667   1.32    itojun 	 * We just pick up the first reachable one (if any), assuming that
    668   1.32    itojun 	 * the ordering rule of the list described in defrtrlist_update().
    669   1.32    itojun 	 */
    670   1.32    itojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
    671   1.32    itojun 	     dr = TAILQ_NEXT(dr, dr_entry)) {
    672   1.79  tonnerre 		ndi = ND_IFINFO(dr->ifp);
    673   1.80    dyoung 		if (nd6_accepts_rtadv(ndi))
    674   1.79  tonnerre 			continue;
    675   1.79  tonnerre 
    676   1.56    rpaulo 		if (selected_dr == NULL &&
    677  1.101     ozaki 		    nd6_is_llinfo_probreach(dr))
    678   1.32    itojun 			selected_dr = dr;
    679   1.32    itojun 
    680   1.32    itojun 		if (dr->installed && !installed_dr)
    681   1.32    itojun 			installed_dr = dr;
    682   1.32    itojun 		else if (dr->installed && installed_dr) {
    683   1.32    itojun 			/* this should not happen.  warn for diagnosis. */
    684   1.32    itojun 			log(LOG_ERR, "defrouter_select: more than one router"
    685   1.32    itojun 			    " is installed\n");
    686   1.32    itojun 		}
    687   1.32    itojun 	}
    688   1.32    itojun 	/*
    689   1.32    itojun 	 * If none of the default routers was found to be reachable,
    690   1.32    itojun 	 * round-robin the list regardless of preference.
    691   1.32    itojun 	 * Otherwise, if we have an installed router, check if the selected
    692   1.32    itojun 	 * (reachable) router should really be preferred to the installed one.
    693   1.32    itojun 	 * We only prefer the new router when the old one is not reachable
    694   1.32    itojun 	 * or when the new one has a really higher preference value.
    695   1.32    itojun 	 */
    696   1.56    rpaulo 	if (selected_dr == NULL) {
    697   1.56    rpaulo 		if (installed_dr == NULL || !TAILQ_NEXT(installed_dr, dr_entry))
    698   1.32    itojun 			selected_dr = TAILQ_FIRST(&nd_defrouter);
    699   1.32    itojun 		else
    700   1.32    itojun 			selected_dr = TAILQ_NEXT(installed_dr, dr_entry);
    701   1.32    itojun 	} else if (installed_dr &&
    702  1.101     ozaki 	    nd6_is_llinfo_probreach(installed_dr) &&
    703   1.32    itojun 	    rtpref(selected_dr) <= rtpref(installed_dr)) {
    704   1.32    itojun 		selected_dr = installed_dr;
    705   1.32    itojun 	}
    706   1.32    itojun 
    707   1.32    itojun 	/*
    708   1.32    itojun 	 * If the selected router is different than the installed one,
    709   1.32    itojun 	 * remove the installed router and install the selected one.
    710   1.32    itojun 	 * Note that the selected router is never NULL here.
    711   1.32    itojun 	 */
    712   1.32    itojun 	if (installed_dr != selected_dr) {
    713   1.32    itojun 		if (installed_dr)
    714   1.32    itojun 			defrouter_delreq(installed_dr);
    715   1.32    itojun 		defrouter_addreq(selected_dr);
    716    1.7    itojun 	}
    717    1.7    itojun 
    718    1.7    itojun 	splx(s);
    719    1.7    itojun 	return;
    720    1.7    itojun }
    721    1.7    itojun 
    722   1.32    itojun /*
    723   1.32    itojun  * for default router selection
    724   1.32    itojun  * regards router-preference field as a 2-bit signed integer
    725   1.32    itojun  */
    726   1.32    itojun static int
    727   1.32    itojun rtpref(struct nd_defrouter *dr)
    728   1.32    itojun {
    729   1.32    itojun 	switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) {
    730   1.32    itojun 	case ND_RA_FLAG_RTPREF_HIGH:
    731   1.56    rpaulo 		return (RTPREF_HIGH);
    732   1.32    itojun 	case ND_RA_FLAG_RTPREF_MEDIUM:
    733   1.58    rpaulo 	case ND_RA_FLAG_RTPREF_RSV:
    734   1.56    rpaulo 		return (RTPREF_MEDIUM);
    735   1.32    itojun 	case ND_RA_FLAG_RTPREF_LOW:
    736   1.56    rpaulo 		return (RTPREF_LOW);
    737   1.32    itojun 	default:
    738   1.32    itojun 		/*
    739   1.32    itojun 		 * This case should never happen.  If it did, it would mean a
    740   1.32    itojun 		 * serious bug of kernel internal.  We thus always bark here.
    741   1.32    itojun 		 * Or, can we even panic?
    742   1.32    itojun 		 */
    743   1.56    rpaulo 		log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->flags);
    744   1.56    rpaulo 		return (RTPREF_INVALID);
    745   1.32    itojun 	}
    746   1.32    itojun 	/* NOTREACHED */
    747   1.32    itojun }
    748   1.32    itojun 
    749    1.2    itojun static struct nd_defrouter *
    750   1.94      matt defrtrlist_update(struct nd_defrouter *newdr)
    751    1.2    itojun {
    752    1.2    itojun 	struct nd_defrouter *dr, *n;
    753   1.94      matt 	struct in6_ifextra *ext = newdr->ifp->if_afdata[AF_INET6];
    754    1.4    itojun 	int s = splsoftnet();
    755    1.2    itojun 
    756   1.94      matt 	if ((dr = defrouter_lookup(&newdr->rtaddr, newdr->ifp)) != NULL) {
    757    1.2    itojun 		/* entry exists */
    758   1.94      matt 		if (newdr->rtlifetime == 0) {
    759   1.96    martin 			defrtrlist_del(dr, ext);
    760    1.2    itojun 			dr = NULL;
    761    1.2    itojun 		} else {
    762   1.32    itojun 			int oldpref = rtpref(dr);
    763   1.32    itojun 
    764    1.2    itojun 			/* override */
    765   1.94      matt 			dr->flags = newdr->flags; /* xxx flag check */
    766   1.94      matt 			dr->rtlifetime = newdr->rtlifetime;
    767   1.94      matt 			dr->expire = newdr->expire;
    768   1.32    itojun 
    769   1.32    itojun 			/*
    770   1.32    itojun 			 * If the preference does not change, there's no need
    771   1.32    itojun 			 * to sort the entries.
    772   1.32    itojun 			 */
    773   1.94      matt 			if (rtpref(newdr) == oldpref) {
    774   1.32    itojun 				splx(s);
    775   1.36    itojun 				return (dr);
    776   1.32    itojun 			}
    777   1.32    itojun 
    778   1.32    itojun 			/*
    779   1.32    itojun 			 * preferred router may be changed, so relocate
    780   1.32    itojun 			 * this router.
    781   1.32    itojun 			 * XXX: calling TAILQ_REMOVE directly is a bad manner.
    782   1.32    itojun 			 * However, since defrtrlist_del() has many side
    783   1.32    itojun 			 * effects, we intentionally do so here.
    784   1.32    itojun 			 * defrouter_select() below will handle routing
    785   1.32    itojun 			 * changes later.
    786   1.32    itojun 			 */
    787   1.32    itojun 			TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
    788   1.32    itojun 			n = dr;
    789   1.32    itojun 			goto insert;
    790    1.2    itojun 		}
    791    1.2    itojun 		splx(s);
    792   1.36    itojun 		return (dr);
    793    1.2    itojun 	}
    794    1.2    itojun 
    795   1.83  christos 	if (ip6_maxifdefrouters >= 0 &&
    796   1.83  christos 	    ext->ndefrouters >= ip6_maxifdefrouters) {
    797   1.83  christos 		splx(s);
    798   1.83  christos 		return (NULL);
    799   1.83  christos 	}
    800   1.83  christos 
    801    1.2    itojun 	/* entry does not exist */
    802   1.94      matt 	if (newdr->rtlifetime == 0) {
    803    1.2    itojun 		splx(s);
    804   1.36    itojun 		return (NULL);
    805    1.2    itojun 	}
    806    1.2    itojun 
    807   1.81       spz 	if (ip6_rtadv_maxroutes <= nd6_numroutes) {
    808   1.81       spz 		ICMP6_STATINC(ICMP6_STAT_DROPPED_RAROUTE);
    809   1.81       spz 		splx(s);
    810   1.81       spz 		return (NULL);
    811   1.81       spz 	}
    812   1.81       spz 
    813    1.2    itojun 	n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
    814    1.2    itojun 	if (n == NULL) {
    815    1.2    itojun 		splx(s);
    816   1.36    itojun 		return (NULL);
    817    1.2    itojun 	}
    818   1.78    cegger 	memset(n, 0, sizeof(*n));
    819   1.94      matt 	*n = *newdr;
    820    1.7    itojun 
    821   1.32    itojun insert:
    822    1.7    itojun 	/*
    823   1.32    itojun 	 * Insert the new router in the Default Router List;
    824   1.32    itojun 	 * The Default Router List should be in the descending order
    825   1.32    itojun 	 * of router-preferece.  Routers with the same preference are
    826   1.32    itojun 	 * sorted in the arriving time order.
    827    1.7    itojun 	 */
    828   1.32    itojun 
    829   1.32    itojun 	/* insert at the end of the group */
    830   1.32    itojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
    831   1.32    itojun 	     dr = TAILQ_NEXT(dr, dr_entry)) {
    832   1.32    itojun 		if (rtpref(n) > rtpref(dr))
    833   1.32    itojun 			break;
    834   1.32    itojun 	}
    835   1.32    itojun 	if (dr)
    836   1.32    itojun 		TAILQ_INSERT_BEFORE(dr, n, dr_entry);
    837   1.32    itojun 	else
    838   1.32    itojun 		TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
    839   1.32    itojun 
    840   1.32    itojun 	defrouter_select();
    841   1.32    itojun 
    842   1.83  christos 	ext->ndefrouters++;
    843   1.83  christos 
    844    1.2    itojun 	splx(s);
    845   1.33    itojun 
    846   1.36    itojun 	return (n);
    847    1.2    itojun }
    848    1.2    itojun 
    849    1.2    itojun static struct nd_pfxrouter *
    850   1.64  christos pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
    851    1.2    itojun {
    852    1.2    itojun 	struct nd_pfxrouter *search;
    853   1.33    itojun 
    854   1.62    dyoung 	LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
    855    1.2    itojun 		if (search->router == dr)
    856    1.2    itojun 			break;
    857    1.2    itojun 	}
    858    1.2    itojun 
    859   1.36    itojun 	return (search);
    860    1.2    itojun }
    861    1.2    itojun 
    862    1.2    itojun static void
    863   1.64  christos pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
    864    1.2    itojun {
    865   1.94      matt 	struct nd_pfxrouter *newpfr;
    866    1.2    itojun 
    867   1.94      matt 	newpfr = malloc(sizeof(*newpfr), M_IP6NDP, M_NOWAIT|M_ZERO);
    868   1.94      matt 	if (newpfr == NULL)
    869    1.2    itojun 		return;
    870   1.94      matt 	newpfr->router = dr;
    871    1.2    itojun 
    872   1.94      matt 	LIST_INSERT_HEAD(&pr->ndpr_advrtrs, newpfr, pfr_entry);
    873    1.2    itojun 
    874    1.2    itojun 	pfxlist_onlink_check();
    875    1.2    itojun }
    876    1.2    itojun 
    877    1.2    itojun static void
    878   1.64  christos pfxrtr_del(struct nd_pfxrouter *pfr)
    879    1.2    itojun {
    880    1.2    itojun 	LIST_REMOVE(pfr, pfr_entry);
    881    1.2    itojun 	free(pfr, M_IP6NDP);
    882    1.2    itojun }
    883    1.2    itojun 
    884  1.121     ozaki static struct nd_prefix *
    885   1.64  christos nd6_prefix_lookup(struct nd_prefixctl *key)
    886    1.2    itojun {
    887    1.2    itojun 	struct nd_prefix *search;
    888    1.2    itojun 
    889   1.62    dyoung 	LIST_FOREACH(search, &nd_prefix, ndpr_entry) {
    890   1.94      matt 		if (key->ndprc_ifp == search->ndpr_ifp &&
    891   1.94      matt 		    key->ndprc_plen == search->ndpr_plen &&
    892   1.94      matt 		    in6_are_prefix_equal(&key->ndprc_prefix.sin6_addr,
    893   1.94      matt 		    &search->ndpr_prefix.sin6_addr, key->ndprc_plen)) {
    894    1.2    itojun 			break;
    895    1.2    itojun 		}
    896    1.2    itojun 	}
    897    1.2    itojun 
    898   1.36    itojun 	return (search);
    899    1.2    itojun }
    900    1.2    itojun 
    901   1.83  christos static void
    902   1.83  christos purge_detached(struct ifnet *ifp)
    903   1.83  christos {
    904   1.83  christos 	struct nd_prefix *pr, *pr_next;
    905   1.83  christos 	struct in6_ifaddr *ia;
    906   1.93     ozaki 	struct ifaddr *ifa, *ifa_next;
    907   1.83  christos 
    908   1.83  christos 	for (pr = nd_prefix.lh_first; pr; pr = pr_next) {
    909  1.118     ozaki 		int s;
    910   1.83  christos 		pr_next = pr->ndpr_next;
    911   1.83  christos 
    912   1.83  christos 		/*
    913   1.83  christos 		 * This function is called when we need to make more room for
    914   1.83  christos 		 * new prefixes rather than keeping old, possibly stale ones.
    915   1.83  christos 		 * Detached prefixes would be a good candidate; if all routers
    916   1.83  christos 		 * that advertised the prefix expired, the prefix is also
    917   1.83  christos 		 * probably stale.
    918   1.83  christos 		 */
    919   1.83  christos 		if (pr->ndpr_ifp != ifp ||
    920   1.83  christos 		    IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
    921   1.83  christos 		    ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
    922   1.83  christos 		    !LIST_EMPTY(&pr->ndpr_advrtrs)))
    923   1.83  christos 			continue;
    924   1.83  christos 
    925  1.118     ozaki 	restart:
    926  1.118     ozaki 		s = pserialize_read_enter();
    927  1.115     ozaki 		for (ifa = IFADDR_READER_FIRST(ifp); ifa; ifa = ifa_next) {
    928  1.115     ozaki 			ifa_next = IFADDR_READER_NEXT(ifa);
    929   1.83  christos 			if (ifa->ifa_addr->sa_family != AF_INET6)
    930   1.83  christos 				continue;
    931   1.83  christos 			ia = (struct in6_ifaddr *)ifa;
    932   1.83  christos 			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) ==
    933   1.83  christos 			    IN6_IFF_AUTOCONF && ia->ia6_ndpr == pr) {
    934  1.118     ozaki 				pserialize_read_exit(s);
    935   1.83  christos 				in6_purgeaddr(ifa);
    936  1.118     ozaki 				goto restart;
    937   1.83  christos 			}
    938   1.83  christos 		}
    939  1.118     ozaki 		pserialize_read_exit(s);
    940  1.118     ozaki 
    941   1.83  christos 		if (pr->ndpr_refcnt == 0)
    942   1.83  christos 			prelist_remove(pr);
    943   1.83  christos 	}
    944   1.83  christos }
    945  1.121     ozaki 
    946  1.121     ozaki static int
    947   1.94      matt nd6_prelist_add(struct nd_prefixctl *prc, struct nd_defrouter *dr,
    948   1.64  christos 	struct nd_prefix **newp)
    949    1.2    itojun {
    950   1.94      matt 	struct nd_prefix *newpr = NULL;
    951    1.2    itojun 	int i, s;
    952   1.56    rpaulo 	int error;
    953   1.94      matt 	struct in6_ifextra *ext = prc->ndprc_ifp->if_afdata[AF_INET6];
    954   1.83  christos 
    955   1.83  christos 	if (ip6_maxifprefixes >= 0) {
    956   1.83  christos 		if (ext->nprefixes >= ip6_maxifprefixes / 2)
    957   1.94      matt 			purge_detached(prc->ndprc_ifp);
    958   1.83  christos 		if (ext->nprefixes >= ip6_maxifprefixes)
    959   1.83  christos 			return ENOMEM;
    960   1.83  christos 	}
    961    1.2    itojun 
    962   1.56    rpaulo 	error = 0;
    963   1.94      matt 	newpr = malloc(sizeof(*newpr), M_IP6NDP, M_NOWAIT|M_ZERO);
    964   1.94      matt 	if (newpr == NULL)
    965    1.2    itojun 		return ENOMEM;
    966   1.94      matt 	newpr->ndpr_ifp = prc->ndprc_ifp;
    967   1.94      matt 	newpr->ndpr_prefix = prc->ndprc_prefix;
    968   1.94      matt 	newpr->ndpr_plen = prc->ndprc_plen;
    969   1.94      matt 	newpr->ndpr_vltime = prc->ndprc_vltime;
    970   1.94      matt 	newpr->ndpr_pltime = prc->ndprc_pltime;
    971   1.94      matt 	newpr->ndpr_flags = prc->ndprc_flags;
    972   1.94      matt 	if ((error = in6_init_prefix_ltimes(newpr)) != 0) {
    973   1.94      matt 		free(newpr, M_IP6NDP);
    974   1.56    rpaulo 		return(error);
    975   1.56    rpaulo 	}
    976  1.102     ozaki 	newpr->ndpr_lastupdate = time_uptime;
    977   1.32    itojun 	if (newp != NULL)
    978   1.94      matt 		*newp = newpr;
    979    1.2    itojun 
    980   1.55    rpaulo 	/* initialization */
    981   1.94      matt 	LIST_INIT(&newpr->ndpr_advrtrs);
    982   1.94      matt 	in6_prefixlen2mask(&newpr->ndpr_mask, newpr->ndpr_plen);
    983    1.2    itojun 	/* make prefix in the canonical form */
    984   1.94      matt 	for (i = 0; i < 4; i++) {
    985   1.94      matt 		newpr->ndpr_prefix.sin6_addr.s6_addr32[i] &=
    986   1.94      matt 		    newpr->ndpr_mask.s6_addr32[i];
    987   1.94      matt 	}
    988    1.2    itojun 
    989    1.4    itojun 	s = splsoftnet();
    990    1.2    itojun 	/* link ndpr_entry to nd_prefix list */
    991   1.94      matt 	LIST_INSERT_HEAD(&nd_prefix, newpr, ndpr_entry);
    992    1.2    itojun 	splx(s);
    993    1.2    itojun 
    994   1.32    itojun 	/* ND_OPT_PI_FLAG_ONLINK processing */
    995   1.94      matt 	if (newpr->ndpr_raf_onlink) {
    996   1.32    itojun 		int e;
    997   1.32    itojun 
    998   1.94      matt 		if ((e = nd6_prefix_onlink(newpr)) != 0) {
    999  1.107     ozaki 			nd6log(LOG_ERR, "failed to make "
   1000   1.32    itojun 			    "the prefix %s/%d on-link on %s (errno=%d)\n",
   1001   1.94      matt 			    ip6_sprintf(&prc->ndprc_prefix.sin6_addr),
   1002  1.107     ozaki 			    prc->ndprc_plen, if_name(prc->ndprc_ifp), e);
   1003   1.32    itojun 			/* proceed anyway. XXX: is it correct? */
   1004   1.32    itojun 		}
   1005   1.32    itojun 	}
   1006   1.32    itojun 
   1007    1.2    itojun 	if (dr)
   1008   1.94      matt 		pfxrtr_add(newpr, dr);
   1009    1.2    itojun 
   1010   1.83  christos 	ext->nprefixes++;
   1011   1.83  christos 
   1012    1.2    itojun 	return 0;
   1013    1.2    itojun }
   1014    1.2    itojun 
   1015    1.2    itojun void
   1016   1.64  christos prelist_remove(struct nd_prefix *pr)
   1017    1.2    itojun {
   1018    1.2    itojun 	struct nd_pfxrouter *pfr, *next;
   1019   1.32    itojun 	int e, s;
   1020   1.83  christos 	struct in6_ifextra *ext = pr->ndpr_ifp->if_afdata[AF_INET6];
   1021   1.32    itojun 
   1022   1.32    itojun 	/* make sure to invalidate the prefix until it is really freed. */
   1023   1.32    itojun 	pr->ndpr_vltime = 0;
   1024   1.32    itojun 	pr->ndpr_pltime = 0;
   1025   1.32    itojun #if 0
   1026   1.32    itojun 	/*
   1027   1.32    itojun 	 * Though these flags are now meaningless, we'd rather keep the value
   1028   1.32    itojun 	 * not to confuse users when executing "ndp -p".
   1029   1.32    itojun 	 */
   1030   1.32    itojun 	pr->ndpr_raf_onlink = 0;
   1031   1.32    itojun 	pr->ndpr_raf_auto = 0;
   1032   1.32    itojun #endif
   1033   1.32    itojun 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 &&
   1034   1.32    itojun 	    (e = nd6_prefix_offlink(pr)) != 0) {
   1035  1.107     ozaki 		nd6log(LOG_ERR,
   1036  1.107     ozaki 		    "failed to make %s/%d offlink on %s, errno=%d\n",
   1037   1.32    itojun 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
   1038  1.107     ozaki 		    pr->ndpr_plen, if_name(pr->ndpr_ifp), e);
   1039   1.32    itojun 		/* what should we do? */
   1040   1.32    itojun 	}
   1041   1.32    itojun 
   1042   1.32    itojun 	if (pr->ndpr_refcnt > 0)
   1043   1.32    itojun 		return;		/* notice here? */
   1044    1.2    itojun 
   1045    1.4    itojun 	s = splsoftnet();
   1046    1.2    itojun 	/* unlink ndpr_entry from nd_prefix list */
   1047    1.2    itojun 	LIST_REMOVE(pr, ndpr_entry);
   1048    1.2    itojun 
   1049    1.2    itojun 	/* free list of routers that adversed the prefix */
   1050   1.62    dyoung 	for (pfr = LIST_FIRST(&pr->ndpr_advrtrs); pfr != NULL; pfr = next) {
   1051   1.62    dyoung 		next = LIST_NEXT(pfr, pfr_entry);
   1052    1.2    itojun 
   1053    1.2    itojun 		free(pfr, M_IP6NDP);
   1054    1.2    itojun 	}
   1055   1.83  christos 
   1056   1.86  christos 	if (ext) {
   1057   1.86  christos 		ext->nprefixes--;
   1058   1.86  christos 		if (ext->nprefixes < 0) {
   1059   1.86  christos 			log(LOG_WARNING, "prelist_remove: negative count on "
   1060   1.86  christos 			    "%s\n", pr->ndpr_ifp->if_xname);
   1061   1.86  christos 		}
   1062   1.83  christos 	}
   1063   1.21    itojun 	splx(s);
   1064   1.21    itojun 
   1065    1.2    itojun 	free(pr, M_IP6NDP);
   1066    1.2    itojun 
   1067    1.2    itojun 	pfxlist_onlink_check();
   1068    1.2    itojun }
   1069    1.2    itojun 
   1070   1.56    rpaulo static int
   1071   1.94      matt prelist_update(struct nd_prefixctl *newprc,
   1072   1.64  christos 	struct nd_defrouter *dr, /* may be NULL */
   1073   1.64  christos 	struct mbuf *m,
   1074   1.64  christos 	int mcast)
   1075    1.2    itojun {
   1076  1.114     ozaki 	struct in6_ifaddr *ia6_match = NULL;
   1077   1.32    itojun 	struct ifaddr *ifa;
   1078   1.94      matt 	struct ifnet *ifp = newprc->ndprc_ifp;
   1079    1.2    itojun 	struct nd_prefix *pr;
   1080    1.4    itojun 	int s = splsoftnet();
   1081    1.2    itojun 	int error = 0;
   1082    1.2    itojun 	int auth;
   1083   1.32    itojun 	struct in6_addrlifetime lt6_tmp;
   1084  1.118     ozaki 	int ss;
   1085    1.2    itojun 
   1086    1.7    itojun 	auth = 0;
   1087    1.2    itojun 	if (m) {
   1088    1.2    itojun 		/*
   1089    1.2    itojun 		 * Authenticity for NA consists authentication for
   1090    1.2    itojun 		 * both IP header and IP datagrams, doesn't it ?
   1091    1.2    itojun 		 */
   1092    1.7    itojun #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
   1093    1.2    itojun 		auth = (m->m_flags & M_AUTHIPHDR
   1094    1.2    itojun 		     && m->m_flags & M_AUTHIPDGM) ? 1 : 0;
   1095    1.7    itojun #endif
   1096    1.7    itojun 	}
   1097    1.2    itojun 
   1098   1.94      matt 	if ((pr = nd6_prefix_lookup(newprc)) != NULL) {
   1099   1.32    itojun 		/*
   1100   1.94      matt 		 * nd6_prefix_lookup() ensures that pr and newprc have the same
   1101   1.32    itojun 		 * prefix on a same interface.
   1102   1.32    itojun 		 */
   1103   1.32    itojun 
   1104   1.32    itojun 		/*
   1105   1.32    itojun 		 * Update prefix information.  Note that the on-link (L) bit
   1106   1.32    itojun 		 * and the autonomous (A) bit should NOT be changed from 1
   1107   1.32    itojun 		 * to 0.
   1108   1.32    itojun 		 */
   1109   1.94      matt 		if (newprc->ndprc_raf_onlink == 1)
   1110   1.32    itojun 			pr->ndpr_raf_onlink = 1;
   1111   1.94      matt 		if (newprc->ndprc_raf_auto == 1)
   1112   1.32    itojun 			pr->ndpr_raf_auto = 1;
   1113   1.94      matt 		if (newprc->ndprc_raf_onlink) {
   1114   1.94      matt 			pr->ndpr_vltime = newprc->ndprc_vltime;
   1115   1.94      matt 			pr->ndpr_pltime = newprc->ndprc_pltime;
   1116   1.56    rpaulo 			(void)in6_init_prefix_ltimes(pr); /* XXX error case? */
   1117  1.102     ozaki 			pr->ndpr_lastupdate = time_uptime;
   1118   1.32    itojun 		}
   1119   1.32    itojun 
   1120   1.94      matt 		if (newprc->ndprc_raf_onlink &&
   1121   1.32    itojun 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
   1122   1.32    itojun 			int e;
   1123   1.32    itojun 
   1124   1.32    itojun 			if ((e = nd6_prefix_onlink(pr)) != 0) {
   1125  1.107     ozaki 				nd6log(LOG_ERR,
   1126  1.107     ozaki 				    "failed to make "
   1127   1.32    itojun 				    "the prefix %s/%d on-link on %s "
   1128  1.107     ozaki 				    "(errno=%d)\n",
   1129   1.32    itojun 				    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
   1130  1.107     ozaki 				    pr->ndpr_plen, if_name(pr->ndpr_ifp), e);
   1131   1.32    itojun 				/* proceed anyway. XXX: is it correct? */
   1132   1.32    itojun 			}
   1133   1.32    itojun 		}
   1134   1.32    itojun 
   1135   1.32    itojun 		if (dr && pfxrtr_lookup(pr, dr) == NULL)
   1136   1.32    itojun 			pfxrtr_add(pr, dr);
   1137   1.32    itojun 	} else {
   1138   1.32    itojun 		struct nd_prefix *newpr = NULL;
   1139   1.32    itojun 
   1140   1.94      matt 		if (newprc->ndprc_vltime == 0)
   1141    1.2    itojun 			goto end;
   1142   1.94      matt 		if (newprc->ndprc_raf_onlink == 0 && newprc->ndprc_raf_auto == 0)
   1143   1.32    itojun 			goto end;
   1144   1.32    itojun 
   1145   1.81       spz 		if (ip6_rtadv_maxroutes <= nd6_numroutes) {
   1146   1.81       spz 			ICMP6_STATINC(ICMP6_STAT_DROPPED_RAROUTE);
   1147   1.81       spz 			goto end;
   1148   1.81       spz 		}
   1149   1.81       spz 
   1150   1.94      matt 		error = nd6_prelist_add(newprc, dr, &newpr);
   1151   1.32    itojun 		if (error != 0 || newpr == NULL) {
   1152  1.107     ozaki 			nd6log(LOG_NOTICE,
   1153  1.107     ozaki 			    "nd6_prelist_add failed for %s/%d on %s "
   1154  1.107     ozaki 			    "errno=%d, returnpr=%p\n",
   1155   1.94      matt 			    ip6_sprintf(&newprc->ndprc_prefix.sin6_addr),
   1156   1.94      matt 			    newprc->ndprc_plen, if_name(newprc->ndprc_ifp),
   1157  1.107     ozaki 			    error, newpr);
   1158   1.32    itojun 			goto end; /* we should just give up in this case. */
   1159    1.2    itojun 		}
   1160    1.2    itojun 
   1161    1.2    itojun 		/*
   1162   1.32    itojun 		 * XXX: from the ND point of view, we can ignore a prefix
   1163   1.32    itojun 		 * with the on-link bit being zero.  However, we need a
   1164   1.32    itojun 		 * prefix structure for references from autoconfigured
   1165   1.32    itojun 		 * addresses.  Thus, we explicitly make sure that the prefix
   1166   1.32    itojun 		 * itself expires now.
   1167    1.2    itojun 		 */
   1168   1.32    itojun 		if (newpr->ndpr_raf_onlink == 0) {
   1169   1.32    itojun 			newpr->ndpr_vltime = 0;
   1170   1.32    itojun 			newpr->ndpr_pltime = 0;
   1171   1.32    itojun 			in6_init_prefix_ltimes(newpr);
   1172   1.32    itojun 		}
   1173    1.2    itojun 
   1174   1.32    itojun 		pr = newpr;
   1175   1.32    itojun 	}
   1176   1.32    itojun 
   1177   1.32    itojun 	/*
   1178   1.32    itojun 	 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
   1179   1.32    itojun 	 * Note that pr must be non NULL at this point.
   1180   1.32    itojun 	 */
   1181   1.32    itojun 
   1182   1.32    itojun 	/* 5.5.3 (a). Ignore the prefix without the A bit set. */
   1183   1.94      matt 	if (!newprc->ndprc_raf_auto)
   1184   1.32    itojun 		goto end;
   1185    1.2    itojun 
   1186   1.32    itojun 	/*
   1187   1.32    itojun 	 * 5.5.3 (b). the link-local prefix should have been ignored in
   1188   1.32    itojun 	 * nd6_ra_input.
   1189   1.32    itojun 	 */
   1190    1.2    itojun 
   1191   1.56    rpaulo 	/* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
   1192   1.94      matt 	if (newprc->ndprc_pltime > newprc->ndprc_vltime) {
   1193   1.56    rpaulo 		error = EINVAL;	/* XXX: won't be used */
   1194   1.56    rpaulo 		goto end;
   1195   1.56    rpaulo 	}
   1196    1.2    itojun 
   1197   1.32    itojun 	/*
   1198   1.56    rpaulo 	 * 5.5.3 (d).  If the prefix advertised is not equal to the prefix of
   1199   1.56    rpaulo 	 * an address configured by stateless autoconfiguration already in the
   1200   1.56    rpaulo 	 * list of addresses associated with the interface, and the Valid
   1201   1.56    rpaulo 	 * Lifetime is not 0, form an address.  We first check if we have
   1202   1.56    rpaulo 	 * a matching prefix.
   1203   1.56    rpaulo 	 * Note: we apply a clarification in rfc2462bis-02 here.  We only
   1204   1.56    rpaulo 	 * consider autoconfigured addresses while RFC2462 simply said
   1205   1.56    rpaulo 	 * "address".
   1206   1.32    itojun 	 */
   1207  1.118     ozaki 	ss = pserialize_read_enter();
   1208  1.115     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   1209  1.114     ozaki 		struct in6_ifaddr *ia6;
   1210   1.56    rpaulo 		u_int32_t remaininglifetime;
   1211    1.2    itojun 
   1212   1.32    itojun 		if (ifa->ifa_addr->sa_family != AF_INET6)
   1213   1.32    itojun 			continue;
   1214    1.2    itojun 
   1215  1.114     ozaki 		ia6 = (struct in6_ifaddr *)ifa;
   1216    1.2    itojun 
   1217   1.32    itojun 		/*
   1218   1.56    rpaulo 		 * We only consider autoconfigured addresses as per rfc2462bis.
   1219   1.56    rpaulo 		 */
   1220  1.114     ozaki 		if (!(ia6->ia6_flags & IN6_IFF_AUTOCONF))
   1221   1.56    rpaulo 			continue;
   1222   1.56    rpaulo 
   1223   1.56    rpaulo 		/*
   1224   1.32    itojun 		 * Spec is not clear here, but I believe we should concentrate
   1225   1.32    itojun 		 * on unicast (i.e. not anycast) addresses.
   1226   1.32    itojun 		 * XXX: other ia6_flags? detached or duplicated?
   1227   1.32    itojun 		 */
   1228  1.114     ozaki 		if ((ia6->ia6_flags & IN6_IFF_ANYCAST) != 0)
   1229   1.32    itojun 			continue;
   1230   1.33    itojun 
   1231   1.56    rpaulo 		/*
   1232   1.56    rpaulo 		 * Ignore the address if it is not associated with a prefix
   1233   1.56    rpaulo 		 * or is associated with a prefix that is different from this
   1234   1.56    rpaulo 		 * one.  (pr is never NULL here)
   1235   1.56    rpaulo 		 */
   1236  1.114     ozaki 		if (ia6->ia6_ndpr != pr)
   1237   1.32    itojun 			continue;
   1238    1.7    itojun 
   1239   1.32    itojun 		if (ia6_match == NULL) /* remember the first one */
   1240  1.114     ozaki 			ia6_match = ia6;
   1241    1.2    itojun 
   1242   1.32    itojun 		/*
   1243   1.32    itojun 		 * An already autoconfigured address matched.  Now that we
   1244   1.32    itojun 		 * are sure there is at least one matched address, we can
   1245   1.32    itojun 		 * proceed to 5.5.3. (e): update the lifetimes according to the
   1246   1.32    itojun 		 * "two hours" rule and the privacy extension.
   1247   1.56    rpaulo 		 * We apply some clarifications in rfc2462bis:
   1248   1.56    rpaulo 		 * - use remaininglifetime instead of storedlifetime as a
   1249   1.56    rpaulo 		 *   variable name
   1250   1.56    rpaulo 		 * - remove the dead code in the "two-hour" rule
   1251   1.32    itojun 		 */
   1252   1.32    itojun #define TWOHOUR		(120*60)
   1253  1.114     ozaki 		lt6_tmp = ia6->ia6_lifetime;
   1254   1.32    itojun 		if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
   1255   1.56    rpaulo 			remaininglifetime = ND6_INFINITE_LIFETIME;
   1256  1.114     ozaki 		else if (time_uptime - ia6->ia6_updatetime >
   1257   1.32    itojun 			 lt6_tmp.ia6t_vltime) {
   1258   1.32    itojun 			/*
   1259   1.32    itojun 			 * The case of "invalid" address.  We should usually
   1260   1.32    itojun 			 * not see this case.
   1261   1.32    itojun 			 */
   1262   1.56    rpaulo 			remaininglifetime = 0;
   1263   1.32    itojun 		} else
   1264   1.56    rpaulo 			remaininglifetime = lt6_tmp.ia6t_vltime -
   1265  1.114     ozaki 			    (time_uptime - ia6->ia6_updatetime);
   1266   1.56    rpaulo 
   1267   1.56    rpaulo 		/* when not updating, keep the current stored lifetime. */
   1268   1.56    rpaulo 		lt6_tmp.ia6t_vltime = remaininglifetime;
   1269   1.56    rpaulo 
   1270   1.94      matt 		if (TWOHOUR < newprc->ndprc_vltime ||
   1271   1.94      matt 		    remaininglifetime < newprc->ndprc_vltime) {
   1272   1.94      matt 			lt6_tmp.ia6t_vltime = newprc->ndprc_vltime;
   1273   1.56    rpaulo 		} else if (remaininglifetime <= TWOHOUR) {
   1274   1.56    rpaulo 			if (auth)
   1275   1.94      matt 				lt6_tmp.ia6t_vltime = newprc->ndprc_vltime;
   1276   1.32    itojun 		} else {
   1277   1.32    itojun 			/*
   1278   1.94      matt 			 * newprc->ndprc_vltime <= TWOHOUR &&
   1279   1.56    rpaulo 			 * TWOHOUR < remaininglifetime
   1280   1.32    itojun 			 */
   1281   1.32    itojun 			lt6_tmp.ia6t_vltime = TWOHOUR;
   1282    1.2    itojun 		}
   1283    1.2    itojun 
   1284   1.32    itojun 		/* The 2 hour rule is not imposed for preferred lifetime. */
   1285   1.94      matt 		lt6_tmp.ia6t_pltime = newprc->ndprc_pltime;
   1286    1.2    itojun 
   1287   1.32    itojun 		in6_init_address_ltimes(pr, &lt6_tmp);
   1288    1.2    itojun 
   1289   1.56    rpaulo 		/*
   1290   1.56    rpaulo 		 * We need to treat lifetimes for temporary addresses
   1291   1.56    rpaulo 		 * differently, according to
   1292   1.56    rpaulo 		 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
   1293   1.56    rpaulo 		 * we only update the lifetimes when they are in the maximum
   1294   1.56    rpaulo 		 * intervals.
   1295   1.56    rpaulo 		 */
   1296  1.114     ozaki 		if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
   1297   1.56    rpaulo 			u_int32_t maxvltime, maxpltime;
   1298   1.56    rpaulo 
   1299   1.56    rpaulo 			if (ip6_temp_valid_lifetime >
   1300  1.114     ozaki 			    (u_int32_t)((time_uptime - ia6->ia6_createtime) +
   1301   1.56    rpaulo 			    ip6_desync_factor)) {
   1302   1.56    rpaulo 				maxvltime = ip6_temp_valid_lifetime -
   1303  1.114     ozaki 				    (time_uptime - ia6->ia6_createtime) -
   1304   1.56    rpaulo 				    ip6_desync_factor;
   1305   1.56    rpaulo 			} else
   1306   1.56    rpaulo 				maxvltime = 0;
   1307   1.56    rpaulo 			if (ip6_temp_preferred_lifetime >
   1308  1.114     ozaki 			    (u_int32_t)((time_uptime - ia6->ia6_createtime) +
   1309   1.56    rpaulo 			    ip6_desync_factor)) {
   1310   1.56    rpaulo 				maxpltime = ip6_temp_preferred_lifetime -
   1311  1.114     ozaki 				    (time_uptime - ia6->ia6_createtime) -
   1312   1.56    rpaulo 				    ip6_desync_factor;
   1313   1.56    rpaulo 			} else
   1314   1.56    rpaulo 				maxpltime = 0;
   1315   1.56    rpaulo 
   1316   1.56    rpaulo 			if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
   1317   1.56    rpaulo 			    lt6_tmp.ia6t_vltime > maxvltime) {
   1318   1.56    rpaulo 				lt6_tmp.ia6t_vltime = maxvltime;
   1319   1.56    rpaulo 			}
   1320   1.56    rpaulo 			if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
   1321   1.56    rpaulo 			    lt6_tmp.ia6t_pltime > maxpltime) {
   1322   1.56    rpaulo 				lt6_tmp.ia6t_pltime = maxpltime;
   1323   1.56    rpaulo 			}
   1324   1.56    rpaulo 		}
   1325   1.56    rpaulo 
   1326  1.114     ozaki 		ia6->ia6_lifetime = lt6_tmp;
   1327  1.114     ozaki 		ia6->ia6_updatetime = time_uptime;
   1328   1.32    itojun 	}
   1329  1.118     ozaki 	pserialize_read_exit(ss);
   1330  1.118     ozaki 
   1331   1.94      matt 	if (ia6_match == NULL && newprc->ndprc_vltime) {
   1332   1.56    rpaulo 		int ifidlen;
   1333  1.114     ozaki 		struct in6_ifaddr *ia6;
   1334  1.118     ozaki 		struct psref psref;
   1335   1.56    rpaulo 
   1336    1.2    itojun 		/*
   1337   1.56    rpaulo 		 * 5.5.3 (d) (continued)
   1338   1.32    itojun 		 * No address matched and the valid lifetime is non-zero.
   1339   1.32    itojun 		 * Create a new address.
   1340    1.2    itojun 		 */
   1341   1.56    rpaulo 
   1342   1.56    rpaulo 		/*
   1343   1.56    rpaulo 		 * Prefix Length check:
   1344   1.56    rpaulo 		 * If the sum of the prefix length and interface identifier
   1345   1.56    rpaulo 		 * length does not equal 128 bits, the Prefix Information
   1346   1.56    rpaulo 		 * option MUST be ignored.  The length of the interface
   1347   1.56    rpaulo 		 * identifier is defined in a separate link-type specific
   1348   1.56    rpaulo 		 * document.
   1349   1.56    rpaulo 		 */
   1350   1.56    rpaulo 		ifidlen = in6_if2idlen(ifp);
   1351   1.56    rpaulo 		if (ifidlen < 0) {
   1352   1.56    rpaulo 			/* this should not happen, so we always log it. */
   1353  1.106     ozaki 			log(LOG_ERR, "%s: IFID undefined (%s)\n",
   1354  1.106     ozaki 			    __func__, if_name(ifp));
   1355   1.56    rpaulo 			goto end;
   1356   1.56    rpaulo 		}
   1357   1.56    rpaulo 		if (ifidlen + pr->ndpr_plen != 128) {
   1358  1.107     ozaki 			nd6log(LOG_INFO,
   1359  1.107     ozaki 			    "invalid prefixlen %d for %s, ignored\n",
   1360  1.107     ozaki 			    pr->ndpr_plen, if_name(ifp));
   1361   1.56    rpaulo 			goto end;
   1362   1.56    rpaulo 		}
   1363   1.56    rpaulo 
   1364  1.118     ozaki 		if ((ia6 = in6_ifadd(newprc, mcast, &psref)) != NULL) {
   1365   1.32    itojun 			/*
   1366   1.94      matt 			 * note that we should use pr (not newprc) for reference.
   1367   1.32    itojun 			 */
   1368   1.32    itojun 			pr->ndpr_refcnt++;
   1369   1.32    itojun 			ia6->ia6_ndpr = pr;
   1370    1.2    itojun 
   1371  1.119       roy 			/* toggle onlink state if the address was assigned
   1372  1.119       roy 			 * a prefix route. */
   1373  1.119       roy 			if (ia6->ia_flags & IFA_ROUTE)
   1374  1.119       roy 				pr->ndpr_stateflags |= NDPRF_ONLINK;
   1375  1.119       roy 
   1376   1.32    itojun 			/*
   1377   1.56    rpaulo 			 * draft-ietf-ipngwg-temp-addresses-v2-00 3.3 (2).
   1378   1.56    rpaulo 			 * When a new public address is created as described
   1379   1.56    rpaulo 			 * in RFC2462, also create a new temporary address.
   1380   1.56    rpaulo 			 *
   1381   1.56    rpaulo 			 * draft-ietf-ipngwg-temp-addresses-v2-00 3.5.
   1382   1.56    rpaulo 			 * When an interface connects to a new link, a new
   1383   1.56    rpaulo 			 * randomized interface identifier should be generated
   1384   1.56    rpaulo 			 * immediately together with a new set of temporary
   1385   1.56    rpaulo 			 * addresses.  Thus, we specifiy 1 as the 2nd arg of
   1386   1.56    rpaulo 			 * in6_tmpifadd().
   1387   1.56    rpaulo 			 */
   1388   1.56    rpaulo 			if (ip6_use_tempaddr) {
   1389   1.56    rpaulo 				int e;
   1390   1.56    rpaulo 				if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
   1391  1.107     ozaki 					nd6log(LOG_NOTICE,
   1392  1.107     ozaki 					    "failed to create a temporary "
   1393  1.107     ozaki 					    "address, errno=%d\n", e);
   1394   1.56    rpaulo 				}
   1395   1.56    rpaulo 			}
   1396  1.118     ozaki 			ia6_release(ia6, &psref);
   1397   1.56    rpaulo 
   1398   1.56    rpaulo 			/*
   1399   1.32    itojun 			 * A newly added address might affect the status
   1400   1.32    itojun 			 * of other addresses, so we check and update it.
   1401   1.32    itojun 			 * XXX: what if address duplication happens?
   1402   1.32    itojun 			 */
   1403   1.32    itojun 			pfxlist_onlink_check();
   1404   1.32    itojun 		} else {
   1405   1.32    itojun 			/* just set an error. do not bark here. */
   1406   1.32    itojun 			error = EADDRNOTAVAIL; /* XXX: might be unused. */
   1407   1.32    itojun 		}
   1408    1.2    itojun 	}
   1409    1.2    itojun 
   1410    1.2    itojun  end:
   1411    1.2    itojun 	splx(s);
   1412    1.2    itojun 	return error;
   1413    1.2    itojun }
   1414    1.2    itojun 
   1415    1.2    itojun /*
   1416    1.7    itojun  * A supplement function used in the on-link detection below;
   1417    1.7    itojun  * detect if a given prefix has a (probably) reachable advertising router.
   1418    1.7    itojun  * XXX: lengthy function name...
   1419    1.7    itojun  */
   1420    1.7    itojun static struct nd_pfxrouter *
   1421   1.64  christos find_pfxlist_reachable_router(struct nd_prefix *pr)
   1422    1.7    itojun {
   1423    1.7    itojun 	struct nd_pfxrouter *pfxrtr;
   1424    1.7    itojun 
   1425    1.7    itojun 	for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
   1426    1.7    itojun 	     pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
   1427   1.89       roy 		if (pfxrtr->router->ifp->if_flags & IFF_UP &&
   1428   1.89       roy 		    pfxrtr->router->ifp->if_link_state != LINK_STATE_DOWN &&
   1429  1.101     ozaki 		    nd6_is_llinfo_probreach(pfxrtr->router))
   1430    1.7    itojun 			break;	/* found */
   1431    1.7    itojun 	}
   1432    1.7    itojun 
   1433   1.36    itojun 	return (pfxrtr);
   1434    1.7    itojun }
   1435    1.7    itojun 
   1436    1.7    itojun /*
   1437    1.2    itojun  * Check if each prefix in the prefix list has at least one available router
   1438   1.25    itojun  * that advertised the prefix (a router is "available" if its neighbor cache
   1439   1.25    itojun  * entry is reachable or probably reachable).
   1440    1.7    itojun  * If the check fails, the prefix may be off-link, because, for example,
   1441    1.2    itojun  * we have moved from the network but the lifetime of the prefix has not
   1442   1.25    itojun  * expired yet.  So we should not use the prefix if there is another prefix
   1443   1.25    itojun  * that has an available router.
   1444   1.25    itojun  * But, if there is no prefix that has an available router, we still regards
   1445   1.25    itojun  * all the prefixes as on-link.  This is because we can't tell if all the
   1446    1.2    itojun  * routers are simply dead or if we really moved from the network and there
   1447    1.2    itojun  * is no router around us.
   1448    1.2    itojun  */
   1449    1.7    itojun void
   1450   1.73      matt pfxlist_onlink_check(void)
   1451    1.2    itojun {
   1452    1.2    itojun 	struct nd_prefix *pr;
   1453  1.114     ozaki 	struct in6_ifaddr *ia;
   1454   1.56    rpaulo 	struct nd_defrouter *dr;
   1455   1.56    rpaulo 	struct nd_pfxrouter *pfxrtr = NULL;
   1456  1.118     ozaki 	int s;
   1457    1.2    itojun 
   1458    1.7    itojun 	/*
   1459    1.7    itojun 	 * Check if there is a prefix that has a reachable advertising
   1460    1.7    itojun 	 * router.
   1461    1.7    itojun 	 */
   1462   1.62    dyoung 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1463   1.32    itojun 		if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
   1464    1.2    itojun 			break;
   1465    1.7    itojun 	}
   1466   1.56    rpaulo 	/*
   1467   1.56    rpaulo 	 * If we have no such prefix, check whether we still have a router
   1468   1.56    rpaulo 	 * that does not advertise any prefixes.
   1469   1.56    rpaulo 	 */
   1470   1.56    rpaulo 	if (pr == NULL) {
   1471   1.62    dyoung 		TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
   1472   1.56    rpaulo 			struct nd_prefix *pr0;
   1473   1.56    rpaulo 
   1474   1.62    dyoung 			LIST_FOREACH(pr0, &nd_prefix, ndpr_entry) {
   1475   1.56    rpaulo 				if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
   1476   1.56    rpaulo 					break;
   1477   1.56    rpaulo 			}
   1478   1.56    rpaulo 			if (pfxrtr)
   1479   1.56    rpaulo 				break;
   1480   1.56    rpaulo 		}
   1481   1.56    rpaulo 	}
   1482   1.56    rpaulo 	if (pr != NULL || (TAILQ_FIRST(&nd_defrouter) && !pfxrtr)) {
   1483    1.2    itojun 		/*
   1484   1.32    itojun 		 * There is at least one prefix that has a reachable router,
   1485   1.32    itojun 		 * or at least a router which probably does not advertise
   1486   1.32    itojun 		 * any prefixes.  The latter would be the case when we move
   1487   1.32    itojun 		 * to a new link where we have a router that does not provide
   1488   1.32    itojun 		 * prefixes and we configure an address by hand.
   1489   1.32    itojun 		 * Detach prefixes which have no reachable advertising
   1490   1.32    itojun 		 * router, and attach other prefixes.
   1491    1.2    itojun 		 */
   1492   1.62    dyoung 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1493   1.32    itojun 			/* XXX: a link-local prefix should never be detached */
   1494   1.32    itojun 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
   1495   1.32    itojun 				continue;
   1496   1.32    itojun 
   1497   1.32    itojun 			/*
   1498   1.32    itojun 			 * we aren't interested in prefixes without the L bit
   1499   1.32    itojun 			 * set.
   1500   1.32    itojun 			 */
   1501   1.32    itojun 			if (pr->ndpr_raf_onlink == 0)
   1502   1.32    itojun 				continue;
   1503   1.32    itojun 
   1504   1.32    itojun 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
   1505   1.32    itojun 			    find_pfxlist_reachable_router(pr) == NULL)
   1506   1.32    itojun 				pr->ndpr_stateflags |= NDPRF_DETACHED;
   1507   1.32    itojun 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
   1508   1.32    itojun 			    find_pfxlist_reachable_router(pr) != 0)
   1509   1.32    itojun 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
   1510   1.32    itojun 		}
   1511   1.32    itojun 	} else {
   1512   1.32    itojun 		/* there is no prefix that has a reachable router */
   1513   1.62    dyoung 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1514   1.32    itojun 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
   1515   1.32    itojun 				continue;
   1516   1.32    itojun 
   1517   1.32    itojun 			if (pr->ndpr_raf_onlink == 0)
   1518   1.32    itojun 				continue;
   1519   1.32    itojun 
   1520   1.32    itojun 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0)
   1521   1.32    itojun 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
   1522   1.32    itojun 		}
   1523   1.32    itojun 	}
   1524   1.32    itojun 
   1525   1.32    itojun 	/*
   1526   1.32    itojun 	 * Remove each interface route associated with a (just) detached
   1527   1.32    itojun 	 * prefix, and reinstall the interface route for a (just) attached
   1528   1.32    itojun 	 * prefix.  Note that all attempt of reinstallation does not
   1529   1.32    itojun 	 * necessarily success, when a same prefix is shared among multiple
   1530   1.32    itojun 	 * interfaces.  Such cases will be handled in nd6_prefix_onlink,
   1531   1.32    itojun 	 * so we don't have to care about them.
   1532   1.32    itojun 	 */
   1533   1.62    dyoung 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1534   1.32    itojun 		int e;
   1535   1.32    itojun 
   1536   1.32    itojun 		if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
   1537   1.32    itojun 			continue;
   1538   1.32    itojun 
   1539   1.32    itojun 		if (pr->ndpr_raf_onlink == 0)
   1540   1.32    itojun 			continue;
   1541   1.32    itojun 
   1542   1.32    itojun 		if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
   1543   1.32    itojun 		    (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
   1544   1.32    itojun 			if ((e = nd6_prefix_offlink(pr)) != 0) {
   1545  1.107     ozaki 				nd6log(LOG_ERR,
   1546  1.107     ozaki 				    "failed to make %s/%d offlink, errno=%d\n",
   1547   1.32    itojun 				    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
   1548  1.107     ozaki 				    pr->ndpr_plen, e);
   1549    1.2    itojun 			}
   1550    1.2    itojun 		}
   1551   1.32    itojun 		if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
   1552   1.32    itojun 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
   1553   1.32    itojun 		    pr->ndpr_raf_onlink) {
   1554   1.32    itojun 			if ((e = nd6_prefix_onlink(pr)) != 0) {
   1555  1.107     ozaki 				nd6log(LOG_ERR,
   1556  1.107     ozaki 				    "failed to make %s/%d onlink, errno=%d\n",
   1557   1.32    itojun 				    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
   1558  1.107     ozaki 				    pr->ndpr_plen, e);
   1559   1.32    itojun 			}
   1560   1.32    itojun 		}
   1561   1.32    itojun 	}
   1562   1.32    itojun 
   1563   1.32    itojun 	/*
   1564   1.32    itojun 	 * Changes on the prefix status might affect address status as well.
   1565   1.32    itojun 	 * Make sure that all addresses derived from an attached prefix are
   1566   1.32    itojun 	 * attached, and that all addresses derived from a detached prefix are
   1567   1.32    itojun 	 * detached.  Note, however, that a manually configured address should
   1568   1.32    itojun 	 * always be attached.
   1569   1.32    itojun 	 * The precise detection logic is same as the one for prefixes.
   1570   1.32    itojun 	 */
   1571  1.118     ozaki 	s = pserialize_read_enter();
   1572  1.114     ozaki 	IN6_ADDRLIST_READER_FOREACH(ia) {
   1573  1.114     ozaki 		if (!(ia->ia6_flags & IN6_IFF_AUTOCONF))
   1574   1.32    itojun 			continue;
   1575   1.32    itojun 
   1576  1.114     ozaki 		if (ia->ia6_ndpr == NULL) {
   1577   1.32    itojun 			/*
   1578   1.32    itojun 			 * This can happen when we first configure the address
   1579   1.32    itojun 			 * (i.e. the address exists, but the prefix does not).
   1580   1.32    itojun 			 * XXX: complicated relationships...
   1581   1.32    itojun 			 */
   1582   1.32    itojun 			continue;
   1583   1.32    itojun 		}
   1584   1.32    itojun 
   1585  1.114     ozaki 		if (find_pfxlist_reachable_router(ia->ia6_ndpr))
   1586   1.32    itojun 			break;
   1587   1.32    itojun 	}
   1588  1.118     ozaki 	pserialize_read_exit(s);
   1589  1.114     ozaki 
   1590  1.114     ozaki 	if (ia) {
   1591  1.118     ozaki 		int bound = curlwp_bind();
   1592  1.118     ozaki 
   1593  1.118     ozaki 		s = pserialize_read_enter();
   1594  1.114     ozaki 		IN6_ADDRLIST_READER_FOREACH(ia) {
   1595  1.118     ozaki 			struct ifaddr *ifa = (struct ifaddr *)ia;
   1596  1.118     ozaki 			struct psref psref;
   1597  1.118     ozaki 
   1598  1.114     ozaki 			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
   1599   1.32    itojun 				continue;
   1600   1.32    itojun 
   1601  1.114     ozaki 			if (ia->ia6_ndpr == NULL) /* XXX: see above. */
   1602   1.32    itojun 				continue;
   1603   1.32    itojun 
   1604  1.118     ozaki 			ia6_acquire(ia, &psref);
   1605  1.118     ozaki 			pserialize_read_exit(s);
   1606  1.118     ozaki 
   1607  1.114     ozaki 			if (find_pfxlist_reachable_router(ia->ia6_ndpr)) {
   1608  1.114     ozaki 				if (ia->ia6_flags & IN6_IFF_DETACHED) {
   1609  1.114     ozaki 					ia->ia6_flags &= ~IN6_IFF_DETACHED;
   1610  1.114     ozaki 					ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1611  1.118     ozaki 					nd6_dad_start(ifa,
   1612   1.56    rpaulo 					    0);
   1613   1.87       roy 					/* We will notify the routing socket
   1614   1.87       roy 					 * of the DAD result, so no need to
   1615   1.87       roy 					 * here */
   1616   1.56    rpaulo 				}
   1617   1.56    rpaulo 			} else {
   1618  1.114     ozaki 				if ((ia->ia6_flags & IN6_IFF_DETACHED) == 0) {
   1619  1.114     ozaki 					ia->ia6_flags |= IN6_IFF_DETACHED;
   1620   1.97       roy 					rt_newaddrmsg(RTM_NEWADDR,
   1621  1.118     ozaki 					    ifa, 0, NULL);
   1622   1.56    rpaulo 				}
   1623   1.56    rpaulo 			}
   1624  1.118     ozaki 
   1625  1.118     ozaki 			s = pserialize_read_enter();
   1626  1.118     ozaki 			ia6_release(ia, &psref);
   1627    1.2    itojun 		}
   1628  1.118     ozaki 		pserialize_read_exit(s);
   1629  1.118     ozaki 		curlwp_bindx(bound);
   1630    1.2    itojun 	}
   1631    1.2    itojun 	else {
   1632  1.118     ozaki 		int bound = curlwp_bind();
   1633  1.118     ozaki 
   1634  1.118     ozaki 		s = pserialize_read_enter();
   1635  1.114     ozaki 		IN6_ADDRLIST_READER_FOREACH(ia) {
   1636  1.114     ozaki 			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
   1637   1.32    itojun 				continue;
   1638   1.32    itojun 
   1639  1.114     ozaki 			if (ia->ia6_flags & IN6_IFF_DETACHED) {
   1640  1.118     ozaki 				struct ifaddr *ifa = (struct ifaddr *)ia;
   1641  1.118     ozaki 				struct psref psref;
   1642  1.118     ozaki 
   1643  1.114     ozaki 				ia->ia6_flags &= ~IN6_IFF_DETACHED;
   1644  1.114     ozaki 				ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1645  1.118     ozaki 
   1646  1.118     ozaki 				ia6_acquire(ia, &psref);
   1647  1.118     ozaki 				pserialize_read_exit(s);
   1648  1.118     ozaki 
   1649   1.56    rpaulo 				/* Do we need a delay in this case? */
   1650  1.118     ozaki 				nd6_dad_start(ifa, 0);
   1651  1.118     ozaki 
   1652  1.118     ozaki 				s = pserialize_read_enter();
   1653  1.118     ozaki 				ia6_release(ia, &psref);
   1654   1.56    rpaulo 			}
   1655   1.32    itojun 		}
   1656  1.118     ozaki 		pserialize_read_exit(s);
   1657  1.118     ozaki 		curlwp_bindx(bound);
   1658    1.2    itojun 	}
   1659    1.2    itojun }
   1660    1.2    itojun 
   1661  1.121     ozaki static int
   1662   1.64  christos nd6_prefix_onlink(struct nd_prefix *pr)
   1663    1.2    itojun {
   1664   1.32    itojun 	struct ifaddr *ifa;
   1665   1.32    itojun 	struct ifnet *ifp = pr->ndpr_ifp;
   1666   1.32    itojun 	struct sockaddr_in6 mask6;
   1667   1.32    itojun 	struct nd_prefix *opr;
   1668   1.32    itojun 	u_long rtflags;
   1669   1.32    itojun 	int error = 0;
   1670  1.118     ozaki 	struct psref psref;
   1671  1.118     ozaki 	int bound;
   1672   1.32    itojun 
   1673   1.32    itojun 	/* sanity check */
   1674   1.32    itojun 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
   1675  1.107     ozaki 		nd6log(LOG_ERR, "%s/%d is already on-link\n",
   1676  1.107     ozaki 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen);
   1677   1.51    itojun 		return (EEXIST);
   1678   1.32    itojun 	}
   1679   1.32    itojun 
   1680   1.32    itojun 	/*
   1681   1.32    itojun 	 * Add the interface route associated with the prefix.  Before
   1682   1.32    itojun 	 * installing the route, check if there's the same prefix on another
   1683   1.32    itojun 	 * interface, and the prefix has already installed the interface route.
   1684   1.32    itojun 	 * Although such a configuration is expected to be rare, we explicitly
   1685   1.32    itojun 	 * allow it.
   1686   1.32    itojun 	 */
   1687   1.62    dyoung 	LIST_FOREACH(opr, &nd_prefix, ndpr_entry) {
   1688   1.32    itojun 		if (opr == pr)
   1689   1.32    itojun 			continue;
   1690   1.32    itojun 
   1691   1.32    itojun 		if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
   1692   1.32    itojun 			continue;
   1693   1.32    itojun 
   1694   1.32    itojun 		if (opr->ndpr_plen == pr->ndpr_plen &&
   1695   1.32    itojun 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
   1696   1.32    itojun 		    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen))
   1697   1.36    itojun 			return (0);
   1698   1.32    itojun 	}
   1699   1.32    itojun 
   1700   1.32    itojun 	/*
   1701   1.32    itojun 	 * We prefer link-local addresses as the associated interface address.
   1702   1.32    itojun 	 */
   1703   1.32    itojun 	/* search for a link-local addr */
   1704  1.118     ozaki 	bound = curlwp_bind();
   1705  1.118     ozaki 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal_psref(ifp,
   1706  1.118     ozaki 	    IN6_IFF_NOTREADY | IN6_IFF_ANYCAST, &psref);
   1707   1.32    itojun 	if (ifa == NULL) {
   1708  1.118     ozaki 		int s = pserialize_read_enter();
   1709  1.115     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
   1710   1.32    itojun 			if (ifa->ifa_addr->sa_family == AF_INET6)
   1711   1.32    itojun 				break;
   1712   1.32    itojun 		}
   1713  1.118     ozaki 		if (ifa != NULL)
   1714  1.118     ozaki 			ifa_acquire(ifa, &psref);
   1715  1.118     ozaki 		pserialize_read_exit(s);
   1716   1.32    itojun 		/* should we care about ia6_flags? */
   1717   1.32    itojun 	}
   1718   1.32    itojun 	if (ifa == NULL) {
   1719   1.32    itojun 		/*
   1720   1.32    itojun 		 * This can still happen, when, for example, we receive an RA
   1721   1.32    itojun 		 * containing a prefix with the L bit set and the A bit clear,
   1722   1.32    itojun 		 * after removing all IPv6 addresses on the receiving
   1723   1.32    itojun 		 * interface.  This should, of course, be rare though.
   1724   1.32    itojun 		 */
   1725  1.107     ozaki 		nd6log(LOG_NOTICE, "failed to find any ifaddr"
   1726   1.32    itojun 		    " to add route for a prefix(%s/%d) on %s\n",
   1727   1.32    itojun 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
   1728  1.107     ozaki 		    pr->ndpr_plen, if_name(ifp));
   1729  1.118     ozaki 		curlwp_bindx(bound);
   1730   1.36    itojun 		return (0);
   1731   1.32    itojun 	}
   1732    1.2    itojun 
   1733    1.2    itojun 	/*
   1734   1.32    itojun 	 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
   1735   1.32    itojun 	 * ifa->ifa_rtrequest = nd6_rtrequest;
   1736    1.2    itojun 	 */
   1737   1.78    cegger 	memset(&mask6, 0, sizeof(mask6));
   1738   1.85     joerg 	mask6.sin6_family = AF_INET6;
   1739   1.32    itojun 	mask6.sin6_len = sizeof(mask6);
   1740   1.32    itojun 	mask6.sin6_addr = pr->ndpr_mask;
   1741   1.32    itojun 	/* rtrequest() will probably set RTF_UP, but we're not sure. */
   1742   1.32    itojun 	rtflags = ifa->ifa_flags | RTF_UP;
   1743   1.32    itojun 	if (nd6_need_cache(ifp)) {
   1744   1.32    itojun 		/* explicitly set in case ifa_flags does not set the flag. */
   1745  1.108     ozaki 		rtflags |= RTF_CONNECTED;
   1746   1.32    itojun 	} else {
   1747   1.32    itojun 		/*
   1748   1.32    itojun 		 * explicitly clear the cloning bit in case ifa_flags sets it.
   1749   1.32    itojun 		 */
   1750  1.108     ozaki 		rtflags &= ~RTF_CONNECTED;
   1751   1.32    itojun 	}
   1752  1.116     ozaki 	error = rtrequest_newmsg(RTM_ADD, sin6tosa(&pr->ndpr_prefix),
   1753  1.116     ozaki 	    ifa->ifa_addr, sin6tosa(&mask6), rtflags);
   1754   1.32    itojun 	if (error == 0) {
   1755  1.101     ozaki 		nd6_numroutes++;
   1756   1.32    itojun 		pr->ndpr_stateflags |= NDPRF_ONLINK;
   1757   1.32    itojun 	} else {
   1758  1.107     ozaki 		nd6log(LOG_ERR, "failed to add route for a"
   1759   1.32    itojun 		    " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx "
   1760   1.32    itojun 		    "errno = %d\n",
   1761   1.32    itojun 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
   1762   1.32    itojun 		    pr->ndpr_plen, if_name(ifp),
   1763   1.32    itojun 		    ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr),
   1764  1.107     ozaki 		    ip6_sprintf(&mask6.sin6_addr), rtflags, error);
   1765   1.32    itojun 	}
   1766  1.118     ozaki 	ifa_release(ifa, &psref);
   1767  1.118     ozaki 	curlwp_bindx(bound);
   1768   1.32    itojun 
   1769   1.36    itojun 	return (error);
   1770   1.32    itojun }
   1771   1.32    itojun 
   1772  1.121     ozaki static int
   1773   1.64  christos nd6_prefix_offlink(struct nd_prefix *pr)
   1774   1.32    itojun {
   1775   1.32    itojun 	int error = 0;
   1776   1.32    itojun 	struct ifnet *ifp = pr->ndpr_ifp;
   1777   1.32    itojun 	struct nd_prefix *opr;
   1778   1.32    itojun 	struct sockaddr_in6 sa6, mask6;
   1779   1.32    itojun 
   1780   1.32    itojun 	/* sanity check */
   1781   1.32    itojun 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
   1782  1.107     ozaki 		nd6log(LOG_ERR, "%s/%d is already off-link\n",
   1783  1.107     ozaki 		    ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen);
   1784   1.36    itojun 		return (EEXIST);
   1785   1.32    itojun 	}
   1786   1.32    itojun 
   1787   1.69    dyoung 	sockaddr_in6_init(&sa6, &pr->ndpr_prefix.sin6_addr, 0, 0, 0);
   1788   1.69    dyoung 	sockaddr_in6_init(&mask6, &pr->ndpr_mask, 0, 0, 0);
   1789  1.116     ozaki 	error = rtrequest_newmsg(RTM_DELETE, sin6tosa(&sa6), NULL,
   1790  1.116     ozaki 	    sin6tosa(&mask6), 0);
   1791   1.32    itojun 	if (error == 0) {
   1792   1.32    itojun 		pr->ndpr_stateflags &= ~NDPRF_ONLINK;
   1793  1.101     ozaki 		nd6_numroutes--;
   1794    1.2    itojun 
   1795   1.32    itojun 		/*
   1796   1.32    itojun 		 * There might be the same prefix on another interface,
   1797   1.32    itojun 		 * the prefix which could not be on-link just because we have
   1798   1.32    itojun 		 * the interface route (see comments in nd6_prefix_onlink).
   1799   1.32    itojun 		 * If there's one, try to make the prefix on-link on the
   1800   1.32    itojun 		 * interface.
   1801   1.32    itojun 		 */
   1802   1.62    dyoung 		LIST_FOREACH(opr, &nd_prefix, ndpr_entry) {
   1803   1.32    itojun 			if (opr == pr)
   1804   1.32    itojun 				continue;
   1805    1.2    itojun 
   1806   1.32    itojun 			if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0)
   1807   1.32    itojun 				continue;
   1808    1.2    itojun 
   1809   1.32    itojun 			/*
   1810   1.32    itojun 			 * KAME specific: detached prefixes should not be
   1811   1.32    itojun 			 * on-link.
   1812   1.32    itojun 			 */
   1813   1.32    itojun 			if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0)
   1814   1.32    itojun 				continue;
   1815    1.2    itojun 
   1816   1.32    itojun 			if (opr->ndpr_plen == pr->ndpr_plen &&
   1817   1.32    itojun 			    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
   1818   1.32    itojun 			    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
   1819   1.32    itojun 				int e;
   1820   1.32    itojun 
   1821   1.32    itojun 				if ((e = nd6_prefix_onlink(opr)) != 0) {
   1822  1.107     ozaki 					nd6log(LOG_ERR, "failed to "
   1823   1.32    itojun 					    "recover a prefix %s/%d from %s "
   1824   1.32    itojun 					    "to %s (errno = %d)\n",
   1825   1.32    itojun 					    ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
   1826   1.32    itojun 					    opr->ndpr_plen, if_name(ifp),
   1827  1.107     ozaki 					    if_name(opr->ndpr_ifp), e);
   1828   1.32    itojun 				}
   1829   1.32    itojun 			}
   1830   1.32    itojun 		}
   1831   1.32    itojun 	} else {
   1832   1.32    itojun 		/* XXX: can we still set the NDPRF_ONLINK flag? */
   1833  1.107     ozaki 		nd6log(LOG_ERR, "failed to delete route: "
   1834   1.32    itojun 		    "%s/%d on %s (errno = %d)\n",
   1835   1.32    itojun 		    ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp),
   1836  1.107     ozaki 		    error);
   1837    1.2    itojun 	}
   1838    1.2    itojun 
   1839  1.101     ozaki 	return error;
   1840    1.2    itojun }
   1841    1.2    itojun 
   1842    1.2    itojun static struct in6_ifaddr *
   1843  1.118     ozaki in6_ifadd(struct nd_prefixctl *prc, int mcast, struct psref *psref)
   1844    1.2    itojun {
   1845   1.94      matt 	struct ifnet *ifp = prc->ndprc_ifp;
   1846    1.2    itojun 	struct ifaddr *ifa;
   1847   1.32    itojun 	struct in6_aliasreq ifra;
   1848   1.32    itojun 	struct in6_ifaddr *ia, *ib;
   1849   1.32    itojun 	int error, plen0;
   1850    1.2    itojun 	struct in6_addr mask;
   1851   1.94      matt 	int prefixlen = prc->ndprc_plen;
   1852   1.56    rpaulo 	int updateflags;
   1853  1.118     ozaki 	int s;
   1854    1.2    itojun 
   1855   1.31    itojun 	in6_prefixlen2mask(&mask, prefixlen);
   1856    1.2    itojun 
   1857   1.32    itojun 	/*
   1858   1.32    itojun 	 * find a link-local address (will be interface ID).
   1859   1.32    itojun 	 * Is it really mandatory? Theoretically, a global or a site-local
   1860   1.32    itojun 	 * address can be configured without a link-local address, if we
   1861   1.32    itojun 	 * have a unique interface identifier...
   1862   1.32    itojun 	 *
   1863   1.32    itojun 	 * it is not mandatory to have a link-local address, we can generate
   1864   1.32    itojun 	 * interface identifier on the fly.  we do this because:
   1865   1.32    itojun 	 * (1) it should be the easiest way to find interface identifier.
   1866   1.32    itojun 	 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
   1867   1.32    itojun 	 * for multiple addresses on a single interface, and possible shortcut
   1868   1.32    itojun 	 * of DAD.  we omitted DAD for this reason in the past.
   1869   1.32    itojun 	 * (3) a user can prevent autoconfiguration of global address
   1870   1.32    itojun 	 * by removing link-local address by hand (this is partly because we
   1871   1.55    rpaulo 	 * don't have other way to control the use of IPv6 on an interface.
   1872   1.32    itojun 	 * this has been our design choice - cf. NRL's "ifconfig auto").
   1873   1.32    itojun 	 * (4) it is easier to manage when an interface has addresses
   1874   1.32    itojun 	 * with the same interface identifier, than to have multiple addresses
   1875   1.32    itojun 	 * with different interface identifiers.
   1876   1.32    itojun 	 */
   1877  1.118     ozaki 	s = pserialize_read_enter();
   1878   1.32    itojun 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
   1879    1.2    itojun 	if (ifa)
   1880    1.2    itojun 		ib = (struct in6_ifaddr *)ifa;
   1881  1.118     ozaki 	else {
   1882  1.118     ozaki 		pserialize_read_exit(s);
   1883    1.2    itojun 		return NULL;
   1884  1.118     ozaki 	}
   1885    1.2    itojun 
   1886    1.2    itojun #if 0 /* don't care link local addr state, and always do DAD */
   1887    1.2    itojun 	/* if link-local address is not eligible, do not autoconfigure. */
   1888    1.2    itojun 	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
   1889    1.2    itojun 		printf("in6_ifadd: link-local address not ready\n");
   1890    1.2    itojun 		return NULL;
   1891    1.2    itojun 	}
   1892    1.2    itojun #endif
   1893    1.2    itojun 
   1894    1.2    itojun 	/* prefixlen + ifidlen must be equal to 128 */
   1895   1.32    itojun 	plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
   1896   1.32    itojun 	if (prefixlen != plen0) {
   1897  1.107     ozaki 		nd6log(LOG_INFO, "wrong prefixlen for %s "
   1898   1.32    itojun 		    "(prefix=%d ifid=%d)\n",
   1899  1.107     ozaki 		    if_name(ifp), prefixlen, 128 - plen0);
   1900  1.118     ozaki 		pserialize_read_exit(s);
   1901    1.2    itojun 		return NULL;
   1902    1.2    itojun 	}
   1903    1.2    itojun 
   1904    1.2    itojun 	/* make ifaddr */
   1905    1.2    itojun 
   1906   1.56    rpaulo 	memset(&ifra, 0, sizeof(ifra));
   1907   1.32    itojun 	/*
   1908   1.32    itojun 	 * in6_update_ifa() does not use ifra_name, but we accurately set it
   1909   1.32    itojun 	 * for safety.
   1910   1.32    itojun 	 */
   1911   1.32    itojun 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
   1912   1.94      matt 	sockaddr_in6_init(&ifra.ifra_addr, &prc->ndprc_prefix.sin6_addr, 0, 0, 0);
   1913   1.32    itojun 	/* prefix */
   1914   1.32    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
   1915   1.32    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
   1916   1.32    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
   1917   1.32    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
   1918    1.2    itojun 
   1919    1.2    itojun 	/* interface ID */
   1920   1.32    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
   1921   1.32    itojun 	    (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
   1922   1.32    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
   1923   1.32    itojun 	    (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
   1924   1.32    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
   1925   1.32    itojun 	    (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
   1926   1.32    itojun 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
   1927   1.32    itojun 	    (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
   1928  1.118     ozaki 	pserialize_read_exit(s);
   1929   1.33    itojun 
   1930   1.32    itojun 	/* new prefix mask. */
   1931   1.69    dyoung 	sockaddr_in6_init(&ifra.ifra_prefixmask, &mask, 0, 0, 0);
   1932    1.2    itojun 
   1933   1.56    rpaulo 	/* lifetimes */
   1934   1.94      matt 	ifra.ifra_lifetime.ia6t_vltime = prc->ndprc_vltime;
   1935   1.94      matt 	ifra.ifra_lifetime.ia6t_pltime = prc->ndprc_pltime;
   1936    1.2    itojun 
   1937   1.32    itojun 	/* XXX: scope zone ID? */
   1938    1.2    itojun 
   1939   1.32    itojun 	ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
   1940    1.2    itojun 
   1941   1.56    rpaulo 	/*
   1942   1.56    rpaulo 	 * Make sure that we do not have this address already.  This should
   1943   1.56    rpaulo 	 * usually not happen, but we can still see this case, e.g., if we
   1944   1.56    rpaulo 	 * have manually configured the exact address to be configured.
   1945   1.56    rpaulo 	 */
   1946  1.118     ozaki 	s = pserialize_read_enter();
   1947   1.56    rpaulo 	if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) {
   1948   1.56    rpaulo 		/* this should be rare enough to make an explicit log */
   1949   1.56    rpaulo 		log(LOG_INFO, "in6_ifadd: %s is already configured\n",
   1950   1.56    rpaulo 		    ip6_sprintf(&ifra.ifra_addr.sin6_addr));
   1951  1.118     ozaki 		pserialize_read_exit(s);
   1952   1.56    rpaulo 		return (NULL);
   1953   1.56    rpaulo 	}
   1954  1.118     ozaki 	pserialize_read_exit(s);
   1955   1.56    rpaulo 
   1956   1.56    rpaulo 	/*
   1957   1.56    rpaulo 	 * Allocate ifaddr structure, link into chain, etc.
   1958   1.56    rpaulo 	 * If we are going to create a new address upon receiving a multicasted
   1959   1.56    rpaulo 	 * RA, we need to impose a random delay before starting DAD.
   1960   1.56    rpaulo 	 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
   1961   1.56    rpaulo 	 */
   1962   1.56    rpaulo 	updateflags = 0;
   1963   1.56    rpaulo 	if (mcast)
   1964   1.56    rpaulo 		updateflags |= IN6_IFAUPDATE_DADDELAY;
   1965   1.56    rpaulo 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
   1966  1.107     ozaki 		nd6log(LOG_ERR, "failed to make ifaddr %s on %s (errno=%d)\n",
   1967   1.32    itojun 		    ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp),
   1968  1.107     ozaki 		    error);
   1969   1.36    itojun 		return (NULL);	/* ifaddr must not have been allocated. */
   1970   1.32    itojun 	}
   1971    1.2    itojun 
   1972  1.118     ozaki 	ia = in6ifa_ifpwithaddr_psref(ifp, &ifra.ifra_addr.sin6_addr, psref);
   1973    1.2    itojun 
   1974   1.36    itojun 	return (ia);		/* this is always non-NULL */
   1975    1.2    itojun }
   1976    1.2    itojun 
   1977    1.2    itojun int
   1978   1.64  christos in6_tmpifadd(
   1979   1.64  christos 	const struct in6_ifaddr *ia0, /* corresponding public address */
   1980   1.64  christos 	int forcegen,
   1981   1.64  christos 	int dad_delay)
   1982   1.56    rpaulo {
   1983   1.56    rpaulo 	struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
   1984   1.56    rpaulo 	struct in6_ifaddr *newia, *ia;
   1985   1.56    rpaulo 	struct in6_aliasreq ifra;
   1986   1.56    rpaulo 	int i, error;
   1987   1.56    rpaulo 	int trylimit = 3;	/* XXX: adhoc value */
   1988   1.56    rpaulo 	int updateflags;
   1989   1.56    rpaulo 	u_int32_t randid[2];
   1990   1.56    rpaulo 	u_int32_t vltime0, pltime0;
   1991  1.117     ozaki 	int s;
   1992   1.56    rpaulo 
   1993   1.56    rpaulo 	memset(&ifra, 0, sizeof(ifra));
   1994   1.56    rpaulo 	strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
   1995   1.56    rpaulo 	ifra.ifra_addr = ia0->ia_addr;
   1996   1.56    rpaulo 	/* copy prefix mask */
   1997   1.56    rpaulo 	ifra.ifra_prefixmask = ia0->ia_prefixmask;
   1998   1.56    rpaulo 	/* clear the old IFID */
   1999   1.56    rpaulo 	for (i = 0; i < 4; i++) {
   2000   1.56    rpaulo 		ifra.ifra_addr.sin6_addr.s6_addr32[i] &=
   2001   1.56    rpaulo 		    ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
   2002   1.56    rpaulo 	}
   2003   1.56    rpaulo 
   2004   1.56    rpaulo   again:
   2005   1.56    rpaulo 	if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
   2006   1.56    rpaulo 	    (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
   2007  1.107     ozaki 		nd6log(LOG_NOTICE, "failed to find a good random IFID\n");
   2008   1.56    rpaulo 		return (EINVAL);
   2009   1.56    rpaulo 	}
   2010   1.56    rpaulo 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
   2011   1.56    rpaulo 	    (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
   2012   1.56    rpaulo 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
   2013   1.56    rpaulo 	    (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
   2014   1.56    rpaulo 
   2015   1.56    rpaulo 	/*
   2016   1.56    rpaulo 	 * in6_get_tmpifid() quite likely provided a unique interface ID.
   2017   1.56    rpaulo 	 * However, we may still have a chance to see collision, because
   2018   1.56    rpaulo 	 * there may be a time lag between generation of the ID and generation
   2019   1.56    rpaulo 	 * of the address.  So, we'll do one more sanity check.
   2020   1.56    rpaulo 	 */
   2021  1.117     ozaki 	s = pserialize_read_enter();
   2022  1.113     ozaki 	IN6_ADDRLIST_READER_FOREACH(ia) {
   2023   1.56    rpaulo 		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
   2024   1.56    rpaulo 		    &ifra.ifra_addr.sin6_addr)) {
   2025  1.117     ozaki 			pserialize_read_exit(s);
   2026   1.56    rpaulo 			if (trylimit-- == 0) {
   2027   1.56    rpaulo 				/*
   2028   1.56    rpaulo 				 * Give up.  Something strange should have
   2029   1.56    rpaulo 				 * happened.
   2030   1.56    rpaulo 				 */
   2031  1.107     ozaki 				nd6log(LOG_NOTICE,
   2032  1.107     ozaki 				    "failed to find a unique random IFID\n");
   2033   1.56    rpaulo 				return (EEXIST);
   2034   1.56    rpaulo 			}
   2035   1.56    rpaulo 			forcegen = 1;
   2036   1.56    rpaulo 			goto again;
   2037   1.56    rpaulo 		}
   2038   1.56    rpaulo 	}
   2039  1.117     ozaki 	pserialize_read_exit(s);
   2040   1.56    rpaulo 
   2041   1.56    rpaulo 	/*
   2042   1.56    rpaulo 	 * The Valid Lifetime is the lower of the Valid Lifetime of the
   2043   1.56    rpaulo          * public address or TEMP_VALID_LIFETIME.
   2044   1.56    rpaulo 	 * The Preferred Lifetime is the lower of the Preferred Lifetime
   2045   1.56    rpaulo          * of the public address or TEMP_PREFERRED_LIFETIME -
   2046   1.56    rpaulo          * DESYNC_FACTOR.
   2047   1.56    rpaulo 	 */
   2048   1.56    rpaulo 	if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
   2049   1.56    rpaulo 		vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
   2050   1.56    rpaulo 		    (ia0->ia6_lifetime.ia6t_vltime -
   2051  1.102     ozaki 		    (time_uptime - ia0->ia6_updatetime));
   2052   1.56    rpaulo 		if (vltime0 > ip6_temp_valid_lifetime)
   2053   1.56    rpaulo 			vltime0 = ip6_temp_valid_lifetime;
   2054   1.56    rpaulo 	} else
   2055   1.56    rpaulo 		vltime0 = ip6_temp_valid_lifetime;
   2056   1.56    rpaulo 	if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
   2057   1.56    rpaulo 		pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
   2058   1.56    rpaulo 		    (ia0->ia6_lifetime.ia6t_pltime -
   2059  1.102     ozaki 		    (time_uptime - ia0->ia6_updatetime));
   2060   1.56    rpaulo 		if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor){
   2061   1.56    rpaulo 			pltime0 = ip6_temp_preferred_lifetime -
   2062   1.56    rpaulo 			    ip6_desync_factor;
   2063   1.56    rpaulo 		}
   2064   1.56    rpaulo 	} else
   2065   1.56    rpaulo 		pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor;
   2066   1.56    rpaulo 	ifra.ifra_lifetime.ia6t_vltime = vltime0;
   2067   1.56    rpaulo 	ifra.ifra_lifetime.ia6t_pltime = pltime0;
   2068   1.56    rpaulo 
   2069   1.56    rpaulo 	/*
   2070   1.56    rpaulo 	 * A temporary address is created only if this calculated Preferred
   2071   1.56    rpaulo 	 * Lifetime is greater than REGEN_ADVANCE time units.
   2072   1.56    rpaulo 	 */
   2073   1.56    rpaulo 	if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance)
   2074   1.56    rpaulo 		return (0);
   2075   1.56    rpaulo 
   2076   1.56    rpaulo 	/* XXX: scope zone ID? */
   2077   1.56    rpaulo 
   2078   1.56    rpaulo 	ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
   2079   1.56    rpaulo 
   2080   1.56    rpaulo 	/* allocate ifaddr structure, link into chain, etc. */
   2081   1.56    rpaulo 	updateflags = 0;
   2082   1.57    rpaulo 	if (dad_delay)
   2083   1.56    rpaulo 		updateflags |= IN6_IFAUPDATE_DADDELAY;
   2084   1.56    rpaulo 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
   2085   1.56    rpaulo 		return (error);
   2086   1.56    rpaulo 
   2087  1.118     ozaki 	s = pserialize_read_enter();
   2088   1.56    rpaulo 	newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
   2089   1.56    rpaulo 	if (newia == NULL) {	/* XXX: can it happen? */
   2090  1.118     ozaki 		pserialize_read_exit(s);
   2091  1.107     ozaki 		nd6log(LOG_ERR,
   2092  1.107     ozaki 		    "ifa update succeeded, but we got no ifaddr\n");
   2093   1.56    rpaulo 		return (EINVAL); /* XXX */
   2094   1.56    rpaulo 	}
   2095   1.56    rpaulo 	newia->ia6_ndpr = ia0->ia6_ndpr;
   2096   1.56    rpaulo 	newia->ia6_ndpr->ndpr_refcnt++;
   2097  1.118     ozaki 	pserialize_read_exit(s);
   2098   1.56    rpaulo 
   2099   1.56    rpaulo 	/*
   2100   1.56    rpaulo 	 * A newly added address might affect the status of other addresses.
   2101   1.56    rpaulo 	 * XXX: when the temporary address is generated with a new public
   2102   1.56    rpaulo 	 * address, the onlink check is redundant.  However, it would be safe
   2103   1.56    rpaulo 	 * to do the check explicitly everywhere a new address is generated,
   2104   1.56    rpaulo 	 * and, in fact, we surely need the check when we create a new
   2105   1.56    rpaulo 	 * temporary address due to deprecation of an old temporary address.
   2106   1.56    rpaulo 	 */
   2107   1.56    rpaulo 	pfxlist_onlink_check();
   2108   1.56    rpaulo 
   2109   1.56    rpaulo 	return (0);
   2110   1.56    rpaulo }
   2111   1.56    rpaulo 
   2112   1.56    rpaulo static int
   2113    1.2    itojun in6_init_prefix_ltimes(struct nd_prefix *ndpr)
   2114    1.2    itojun {
   2115    1.7    itojun 
   2116   1.26    itojun 	/* check if preferred lifetime > valid lifetime.  RFC2462 5.5.3 (c) */
   2117    1.2    itojun 	if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
   2118  1.107     ozaki 		nd6log(LOG_INFO, "preferred lifetime"
   2119    1.2    itojun 		    "(%d) is greater than valid lifetime(%d)\n",
   2120  1.107     ozaki 		    (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime);
   2121    1.2    itojun 		return (EINVAL);
   2122    1.2    itojun 	}
   2123    1.7    itojun 	if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
   2124    1.2    itojun 		ndpr->ndpr_preferred = 0;
   2125    1.2    itojun 	else
   2126  1.102     ozaki 		ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
   2127    1.7    itojun 	if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
   2128    1.2    itojun 		ndpr->ndpr_expire = 0;
   2129    1.2    itojun 	else
   2130  1.102     ozaki 		ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
   2131    1.2    itojun 
   2132    1.2    itojun 	return 0;
   2133    1.2    itojun }
   2134    1.2    itojun 
   2135    1.2    itojun static void
   2136   1.94      matt in6_init_address_ltimes(struct nd_prefix *newpr,
   2137   1.60  christos     struct in6_addrlifetime *lt6)
   2138    1.2    itojun {
   2139    1.7    itojun 
   2140    1.7    itojun 	/* Valid lifetime must not be updated unless explicitly specified. */
   2141   1.32    itojun 	/* init ia6t_expire */
   2142   1.32    itojun 	if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
   2143   1.32    itojun 		lt6->ia6t_expire = 0;
   2144   1.32    itojun 	else {
   2145  1.102     ozaki 		lt6->ia6t_expire = time_uptime;
   2146   1.32    itojun 		lt6->ia6t_expire += lt6->ia6t_vltime;
   2147    1.2    itojun 	}
   2148    1.7    itojun 
   2149    1.2    itojun 	/* init ia6t_preferred */
   2150    1.7    itojun 	if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
   2151    1.2    itojun 		lt6->ia6t_preferred = 0;
   2152    1.2    itojun 	else {
   2153  1.102     ozaki 		lt6->ia6t_preferred = time_uptime;
   2154    1.2    itojun 		lt6->ia6t_preferred += lt6->ia6t_pltime;
   2155    1.2    itojun 	}
   2156    1.2    itojun }
   2157    1.2    itojun 
   2158    1.2    itojun /*
   2159    1.2    itojun  * Delete all the routing table entries that use the specified gateway.
   2160    1.2    itojun  * XXX: this function causes search through all entries of routing table, so
   2161   1.16    itojun  * it shouldn't be called when acting as a router.
   2162    1.2    itojun  */
   2163    1.2    itojun void
   2164   1.61  christos rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
   2165    1.2    itojun {
   2166    1.4    itojun 	int s = splsoftnet();
   2167    1.2    itojun 
   2168    1.2    itojun 	/* We'll care only link-local addresses */
   2169    1.2    itojun 	if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
   2170    1.2    itojun 		splx(s);
   2171    1.2    itojun 		return;
   2172    1.2    itojun 	}
   2173    1.2    itojun 
   2174  1.120     ozaki 	rt_delete_matched_entries(AF_INET6, rt6_deleteroute_matcher, gateway);
   2175    1.2    itojun 	splx(s);
   2176    1.2    itojun }
   2177    1.2    itojun 
   2178    1.2    itojun static int
   2179  1.120     ozaki rt6_deleteroute_matcher(struct rtentry *rt, void *arg)
   2180    1.2    itojun {
   2181    1.2    itojun 	struct in6_addr *gate = (struct in6_addr *)arg;
   2182    1.2    itojun 
   2183    1.2    itojun 	if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
   2184   1.36    itojun 		return (0);
   2185    1.2    itojun 
   2186  1.104     ozaki 	if (!IN6_ARE_ADDR_EQUAL(gate, &satosin6(rt->rt_gateway)->sin6_addr))
   2187   1.36    itojun 		return (0);
   2188   1.26    itojun 
   2189   1.26    itojun 	/*
   2190   1.32    itojun 	 * Do not delete a static route.
   2191   1.32    itojun 	 * XXX: this seems to be a bit ad-hoc. Should we consider the
   2192   1.32    itojun 	 * 'cloned' bit instead?
   2193   1.32    itojun 	 */
   2194   1.32    itojun 	if ((rt->rt_flags & RTF_STATIC) != 0)
   2195   1.36    itojun 		return (0);
   2196   1.32    itojun 
   2197   1.32    itojun 	/*
   2198    1.2    itojun 	 * We delete only host route. This means, in particular, we don't
   2199    1.2    itojun 	 * delete default route.
   2200    1.2    itojun 	 */
   2201    1.2    itojun 	if ((rt->rt_flags & RTF_HOST) == 0)
   2202   1.36    itojun 		return (0);
   2203    1.2    itojun 
   2204  1.120     ozaki 	return 1;
   2205    1.2    itojun }
   2206    1.7    itojun 
   2207    1.7    itojun int
   2208   1.64  christos nd6_setdefaultiface(int ifindex)
   2209    1.7    itojun {
   2210   1.91     rmind 	ifnet_t *ifp;
   2211    1.7    itojun 	int error = 0;
   2212  1.112     ozaki 	int s;
   2213    1.7    itojun 
   2214  1.112     ozaki 	s = pserialize_read_enter();
   2215  1.112     ozaki 	ifp = if_byindex(ifindex);
   2216  1.112     ozaki 	if (ifp == NULL) {
   2217  1.112     ozaki 		pserialize_read_exit(s);
   2218   1.91     rmind 		return EINVAL;
   2219   1.91     rmind 	}
   2220    1.7    itojun 	if (nd6_defifindex != ifindex) {
   2221    1.7    itojun 		nd6_defifindex = ifindex;
   2222   1.91     rmind 		nd6_defifp = nd6_defifindex > 0 ? ifp : NULL;
   2223   1.54    rpaulo 
   2224   1.54    rpaulo 		/*
   2225   1.54    rpaulo 		 * Our current implementation assumes one-to-one maping between
   2226   1.54    rpaulo 		 * interfaces and links, so it would be natural to use the
   2227   1.54    rpaulo 		 * default interface as the default link.
   2228   1.54    rpaulo 		 */
   2229   1.54    rpaulo 		scope6_setdefault(nd6_defifp);
   2230    1.7    itojun 	}
   2231  1.112     ozaki 	pserialize_read_exit(s);
   2232    1.7    itojun 
   2233   1.36    itojun 	return (error);
   2234    1.7    itojun }
   2235