xref: /freebsd/sys/netinet6/nd6_rtr.c (revision 4f96be33fe7676c69c5abb476bb09bba0c63a3f4)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue  * All rights reserved.
682cd038dSYoshinobu Inoue  *
782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue  * are met:
1082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue  *    without specific prior written permission.
1882cd038dSYoshinobu Inoue  *
1982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
3282cd038dSYoshinobu Inoue  */
3382cd038dSYoshinobu Inoue 
34b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
35686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
36686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
37686cdd19SJun-ichiro itojun Hagino 
3882cd038dSYoshinobu Inoue #include <sys/param.h>
3982cd038dSYoshinobu Inoue #include <sys/systm.h>
4082cd038dSYoshinobu Inoue #include <sys/malloc.h>
4182cd038dSYoshinobu Inoue #include <sys/mbuf.h>
424de485feSMark Johnston #include <sys/refcount.h>
4382cd038dSYoshinobu Inoue #include <sys/socket.h>
4482cd038dSYoshinobu Inoue #include <sys/sockio.h>
4582cd038dSYoshinobu Inoue #include <sys/time.h>
4633841545SHajimu UMEMOTO #include <sys/kernel.h>
47609ff41fSWarner Losh #include <sys/lock.h>
4882cd038dSYoshinobu Inoue #include <sys/errno.h>
491b28988bSMark Johnston #include <sys/rmlock.h>
503120b9d4SKip Macy #include <sys/rwlock.h>
51ad675b32SBjoern A. Zeeb #include <sys/sysctl.h>
5282cd038dSYoshinobu Inoue #include <sys/syslog.h>
5333841545SHajimu UMEMOTO #include <sys/queue.h>
5482cd038dSYoshinobu Inoue 
5582cd038dSYoshinobu Inoue #include <net/if.h>
5676039bc8SGleb Smirnoff #include <net/if_var.h>
573d0d5b21SJustin Hibbits #include <net/if_private.h>
5882cd038dSYoshinobu Inoue #include <net/if_types.h>
5982cd038dSYoshinobu Inoue #include <net/if_dl.h>
6082cd038dSYoshinobu Inoue #include <net/route.h>
61539642a2SAlexander V. Chernikov #include <net/route/nhop.h>
62da187ddbSAlexander V. Chernikov #include <net/route/route_ctl.h>
6382cd038dSYoshinobu Inoue #include <net/radix.h>
644b79449eSBjoern A. Zeeb #include <net/vnet.h>
6582cd038dSYoshinobu Inoue 
6682cd038dSYoshinobu Inoue #include <netinet/in.h>
676e6b3f7cSQing Li #include <net/if_llatbl.h>
6882cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
6933841545SHajimu UMEMOTO #include <netinet6/in6_ifattach.h>
70686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7182cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
7282cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
74686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h>
7582cd038dSYoshinobu Inoue 
769233d8f3SDavid E. O'Brien static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
776f56329aSXin LI static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
786f56329aSXin LI     struct mbuf *, int);
7925ebfe33SBjoern A. Zeeb static int nd6_prefix_onlink(struct nd_prefix *);
8082cd038dSYoshinobu Inoue 
81f592d0c3SBjoern A. Zeeb TAILQ_HEAD(nd6_drhead, nd_defrouter);
82f592d0c3SBjoern A. Zeeb VNET_DEFINE_STATIC(struct nd6_drhead, nd6_defrouter);
83f592d0c3SBjoern A. Zeeb #define	V_nd6_defrouter			VNET(nd6_defrouter)
84ad675b32SBjoern A. Zeeb 
85eddfbb76SRobert Watson VNET_DECLARE(int, nd6_recalc_reachtm_interval);
861e77c105SRobert Watson #define	V_nd6_recalc_reachtm_interval	VNET(nd6_recalc_reachtm_interval)
8782cd038dSYoshinobu Inoue 
885f901c92SAndrew Turner VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
8982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_defifindex);
901e77c105SRobert Watson #define	V_nd6_defifp			VNET(nd6_defifp)
91686cdd19SJun-ichiro itojun Hagino 
9282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_use_tempaddr) = 0;
9382cea7e6SBjoern A. Zeeb 
94eddfbb76SRobert Watson VNET_DEFINE(int, ip6_desync_factor);
9582cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
9682cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
9782cea7e6SBjoern A. Zeeb 
9882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
9933841545SHajimu UMEMOTO 
10021231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL
101*4f96be33SGleb Smirnoff VNET_DEFINE_STATIC(int, nd6_ignore_ipv6_only_ra) = 1;
102*4f96be33SGleb Smirnoff #define	V_nd6_ignore_ipv6_only_ra	VNET(nd6_ignore_ipv6_only_ra)
103*4f96be33SGleb Smirnoff SYSCTL_INT(_net_inet6_icmp6, OID_AUTO,
104*4f96be33SGleb Smirnoff     nd6_ignore_ipv6_only_ra, CTLFLAG_VNET | CTLFLAG_RW,
105*4f96be33SGleb Smirnoff     &VNET_NAME(nd6_ignore_ipv6_only_ra), 0,
106*4f96be33SGleb Smirnoff     "Ignore the 'IPv6-Only flag' in RA messages in compliance with "
107*4f96be33SGleb Smirnoff     "draft-ietf-6man-ipv6only-flag");
10821231a7aSBjoern A. Zeeb #endif
10921231a7aSBjoern A. Zeeb 
110743eee66SSUZUKI Shinsuke /* RTPREF_MEDIUM has to be 0! */
111743eee66SSUZUKI Shinsuke #define RTPREF_HIGH	1
112743eee66SSUZUKI Shinsuke #define RTPREF_MEDIUM	0
113743eee66SSUZUKI Shinsuke #define RTPREF_LOW	(-1)
114743eee66SSUZUKI Shinsuke #define RTPREF_RESERVED	(-2)
115743eee66SSUZUKI Shinsuke #define RTPREF_INVALID	(-3)	/* internal */
116743eee66SSUZUKI Shinsuke 
117ad675b32SBjoern A. Zeeb static void
defrouter_ref(struct nd_defrouter * dr)118ad675b32SBjoern A. Zeeb defrouter_ref(struct nd_defrouter *dr)
119ad675b32SBjoern A. Zeeb {
120ad675b32SBjoern A. Zeeb 
121ad675b32SBjoern A. Zeeb 	refcount_acquire(&dr->refcnt);
122ad675b32SBjoern A. Zeeb }
123ad675b32SBjoern A. Zeeb 
124ad675b32SBjoern A. Zeeb void
defrouter_rele(struct nd_defrouter * dr)125ad675b32SBjoern A. Zeeb defrouter_rele(struct nd_defrouter *dr)
126ad675b32SBjoern A. Zeeb {
127ad675b32SBjoern A. Zeeb 
128ad675b32SBjoern A. Zeeb 	if (refcount_release(&dr->refcnt))
129ad675b32SBjoern A. Zeeb 		free(dr, M_IP6NDP);
130ad675b32SBjoern A. Zeeb }
131ad675b32SBjoern A. Zeeb 
132ad675b32SBjoern A. Zeeb /*
133ad675b32SBjoern A. Zeeb  * Remove a router from the global list and optionally stash it in a
134ad675b32SBjoern A. Zeeb  * caller-supplied queue.
135ad675b32SBjoern A. Zeeb  */
136ad675b32SBjoern A. Zeeb static void
defrouter_unlink(struct nd_defrouter * dr,struct nd6_drhead * drq)137f592d0c3SBjoern A. Zeeb defrouter_unlink(struct nd_defrouter *dr, struct nd6_drhead *drq)
138ad675b32SBjoern A. Zeeb {
139ad675b32SBjoern A. Zeeb 
140ad675b32SBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
141ad675b32SBjoern A. Zeeb 
142f592d0c3SBjoern A. Zeeb 	TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
143ad675b32SBjoern A. Zeeb 	V_nd6_list_genid++;
144ad675b32SBjoern A. Zeeb 	if (drq != NULL)
145ad675b32SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(drq, dr, dr_entry);
146ad675b32SBjoern A. Zeeb }
147ad675b32SBjoern A. Zeeb 
14882cd038dSYoshinobu Inoue /*
14982cd038dSYoshinobu Inoue  * Receive Router Solicitation Message - just for routers.
15082cd038dSYoshinobu Inoue  * Router solicitation/advertisement is mostly managed by userland program
15182cd038dSYoshinobu Inoue  * (rtadvd) so here we have no function like nd6_ra_output().
15282cd038dSYoshinobu Inoue  *
15382cd038dSYoshinobu Inoue  * Based on RFC 2461
15482cd038dSYoshinobu Inoue  */
15582cd038dSYoshinobu Inoue void
nd6_rs_input(struct mbuf * m,int off,int icmp6len)1561272577eSXin LI nd6_rs_input(struct mbuf *m, int off, int icmp6len)
15782cd038dSYoshinobu Inoue {
158503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
159503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
160686cdd19SJun-ichiro itojun Hagino 	struct nd_router_solicit *nd_rs;
161503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
16282cd038dSYoshinobu Inoue 	union nd_opts ndopts;
1631d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
164503f4e47SBjoern A. Zeeb 	char *lladdr;
165503f4e47SBjoern A. Zeeb 	int lladdrlen;
166503f4e47SBjoern A. Zeeb 
167503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
16882cd038dSYoshinobu Inoue 
169e7fa8d0aSHiroki Sato 	/*
170e7fa8d0aSHiroki Sato 	 * Accept RS only when V_ip6_forwarding=1 and the interface has
171e7fa8d0aSHiroki Sato 	 * no ND6_IFF_ACCEPT_RTADV.
172e7fa8d0aSHiroki Sato 	 */
173e7fa8d0aSHiroki Sato 	if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
174686cdd19SJun-ichiro itojun Hagino 		goto freeit;
17582cd038dSYoshinobu Inoue 
176a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
177a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
178a4061289SAndrey V. Elsukov 		goto freeit;
179a4061289SAndrey V. Elsukov 
18082cd038dSYoshinobu Inoue 	/* Sanity checks */
181503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
1824caea9b1SMark Johnston 	if (__predict_false(ip6->ip6_hlim != 255)) {
1834caea9b1SMark Johnston 		ICMP6STAT_INC(icp6s_invlhlim);
18433841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
185c6feea3bSBjoern A. Zeeb 		    "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
1861d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
1871d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
18833841545SHajimu UMEMOTO 		goto bad;
18982cd038dSYoshinobu Inoue 	}
19082cd038dSYoshinobu Inoue 
19182cd038dSYoshinobu Inoue 	/*
19282cd038dSYoshinobu Inoue 	 * Don't update the neighbor cache, if src = ::.
19382cd038dSYoshinobu Inoue 	 * This indicates that the src has no IP address assigned yet.
19482cd038dSYoshinobu Inoue 	 */
195503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
19682cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
197686cdd19SJun-ichiro itojun Hagino 		goto freeit;
198686cdd19SJun-ichiro itojun Hagino 
199a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + icmp6len) {
200a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + icmp6len);
201a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
202a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
20382cd038dSYoshinobu Inoue 			return;
204686cdd19SJun-ichiro itojun Hagino 		}
205a4adf6ccSBjoern A. Zeeb 	}
206a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
207a61b5cfbSBjoern A. Zeeb 	nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
20882cd038dSYoshinobu Inoue 
20982cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_rs);
21082cd038dSYoshinobu Inoue 	nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
21182cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
21233841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
213c6feea3bSBjoern A. Zeeb 		    "%s: invalid ND option, ignored\n", __func__));
21433841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
215686cdd19SJun-ichiro itojun Hagino 		goto freeit;
21682cd038dSYoshinobu Inoue 	}
21782cd038dSYoshinobu Inoue 
218503f4e47SBjoern A. Zeeb 	lladdr = NULL;
219503f4e47SBjoern A. Zeeb 	lladdrlen = 0;
22082cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
22182cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
22282cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
22382cd038dSYoshinobu Inoue 	}
22482cd038dSYoshinobu Inoue 
22582cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
22633841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
227c6feea3bSBjoern A. Zeeb 		    "%s: lladdrlen mismatch for %s (if %d, RS packet %d)\n",
228c6feea3bSBjoern A. Zeeb 		    __func__, ip6_sprintf(ip6bufs, &saddr6),
22907eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
23033841545SHajimu UMEMOTO 		goto bad;
23182cd038dSYoshinobu Inoue 	}
23282cd038dSYoshinobu Inoue 
23382cd038dSYoshinobu Inoue 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
234686cdd19SJun-ichiro itojun Hagino 
235686cdd19SJun-ichiro itojun Hagino  freeit:
236686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
23733841545SHajimu UMEMOTO 	return;
23833841545SHajimu UMEMOTO 
23933841545SHajimu UMEMOTO  bad:
240e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badrs);
24133841545SHajimu UMEMOTO 	m_freem(m);
24282cd038dSYoshinobu Inoue }
24382cd038dSYoshinobu Inoue 
244201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
245201100c5SBjoern A. Zeeb /*
246201100c5SBjoern A. Zeeb  * An initial update routine for draft-ietf-6man-ipv6only-flag.
247201100c5SBjoern A. Zeeb  * We need to iterate over all default routers for the given
24821231a7aSBjoern A. Zeeb  * interface to see whether they are all advertising the "S"
249201100c5SBjoern A. Zeeb  * (IPv6-Only) flag.  If they do set, otherwise unset, the
250201100c5SBjoern A. Zeeb  * interface flag we later use to filter on.
251201100c5SBjoern A. Zeeb  */
252201100c5SBjoern A. Zeeb static void
defrtr_ipv6_only_ifp(struct ifnet * ifp)253201100c5SBjoern A. Zeeb defrtr_ipv6_only_ifp(struct ifnet *ifp)
254201100c5SBjoern A. Zeeb {
255201100c5SBjoern A. Zeeb 	struct nd_defrouter *dr;
25621231a7aSBjoern A. Zeeb 	bool ipv6_only, ipv6_only_old;
25721231a7aSBjoern A. Zeeb #ifdef INET
25821231a7aSBjoern A. Zeeb 	struct epoch_tracker et;
25921231a7aSBjoern A. Zeeb 	struct ifaddr *ifa;
26021231a7aSBjoern A. Zeeb 	bool has_ipv4_addr;
26121231a7aSBjoern A. Zeeb #endif
26221231a7aSBjoern A. Zeeb 
26321231a7aSBjoern A. Zeeb 	if (V_nd6_ignore_ipv6_only_ra != 0)
26421231a7aSBjoern A. Zeeb 		return;
265201100c5SBjoern A. Zeeb 
266201100c5SBjoern A. Zeeb 	ipv6_only = true;
267201100c5SBjoern A. Zeeb 	ND6_RLOCK();
268f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
269201100c5SBjoern A. Zeeb 		if (dr->ifp == ifp &&
270201100c5SBjoern A. Zeeb 		    (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0)
271201100c5SBjoern A. Zeeb 			ipv6_only = false;
272201100c5SBjoern A. Zeeb 	ND6_RUNLOCK();
273201100c5SBjoern A. Zeeb 
274201100c5SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
27521231a7aSBjoern A. Zeeb 	ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY;
27621231a7aSBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
27721231a7aSBjoern A. Zeeb 
27821231a7aSBjoern A. Zeeb 	/* If nothing changed, we have an early exit. */
27921231a7aSBjoern A. Zeeb 	if (ipv6_only == ipv6_only_old)
28021231a7aSBjoern A. Zeeb 		return;
28121231a7aSBjoern A. Zeeb 
28221231a7aSBjoern A. Zeeb #ifdef INET
28321231a7aSBjoern A. Zeeb 	/*
28421231a7aSBjoern A. Zeeb 	 * Should we want to set the IPV6-ONLY flag, check if the
28521231a7aSBjoern A. Zeeb 	 * interface has a non-0/0 and non-link-local IPv4 address
28621231a7aSBjoern A. Zeeb 	 * configured on it.  If it has we will assume working
28721231a7aSBjoern A. Zeeb 	 * IPv4 operations and will clear the interface flag.
28821231a7aSBjoern A. Zeeb 	 */
28921231a7aSBjoern A. Zeeb 	has_ipv4_addr = false;
29021231a7aSBjoern A. Zeeb 	if (ipv6_only) {
29121231a7aSBjoern A. Zeeb 		NET_EPOCH_ENTER(et);
29221231a7aSBjoern A. Zeeb 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
29321231a7aSBjoern A. Zeeb 			if (ifa->ifa_addr->sa_family != AF_INET)
29421231a7aSBjoern A. Zeeb 				continue;
29521231a7aSBjoern A. Zeeb 			if (in_canforward(
29621231a7aSBjoern A. Zeeb 			    satosin(ifa->ifa_addr)->sin_addr)) {
29721231a7aSBjoern A. Zeeb 				has_ipv4_addr = true;
29821231a7aSBjoern A. Zeeb 				break;
29921231a7aSBjoern A. Zeeb 			}
30021231a7aSBjoern A. Zeeb 		}
30121231a7aSBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
30221231a7aSBjoern A. Zeeb 	}
30321231a7aSBjoern A. Zeeb 	if (ipv6_only && has_ipv4_addr) {
30421231a7aSBjoern A. Zeeb 		log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
30521231a7aSBjoern A. Zeeb 		    "configured, ignoring IPv6-Only flag.\n", ifp->if_xname);
30621231a7aSBjoern A. Zeeb 		ipv6_only = false;
30721231a7aSBjoern A. Zeeb 	}
30821231a7aSBjoern A. Zeeb #endif
30921231a7aSBjoern A. Zeeb 
31021231a7aSBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
311201100c5SBjoern A. Zeeb 	if (ipv6_only)
312201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY;
313201100c5SBjoern A. Zeeb 	else
314201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
315201100c5SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
31621231a7aSBjoern A. Zeeb 
31721231a7aSBjoern A. Zeeb #ifdef notyet
31821231a7aSBjoern A. Zeeb 	/* Send notification of flag change. */
319201100c5SBjoern A. Zeeb #endif
32021231a7aSBjoern A. Zeeb }
32130b45077SBjoern A. Zeeb 
32230b45077SBjoern A. Zeeb static void
defrtr_ipv6_only_ipf_down(struct ifnet * ifp)32330b45077SBjoern A. Zeeb defrtr_ipv6_only_ipf_down(struct ifnet *ifp)
32430b45077SBjoern A. Zeeb {
32530b45077SBjoern A. Zeeb 
32630b45077SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
32730b45077SBjoern A. Zeeb 	ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
32830b45077SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
32930b45077SBjoern A. Zeeb }
33021231a7aSBjoern A. Zeeb #endif	/* EXPERIMENTAL */
331201100c5SBjoern A. Zeeb 
33230b45077SBjoern A. Zeeb void
nd6_ifnet_link_event(void * arg __unused,struct ifnet * ifp,int linkstate)33330b45077SBjoern A. Zeeb nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate)
33430b45077SBjoern A. Zeeb {
33530b45077SBjoern A. Zeeb 
33630b45077SBjoern A. Zeeb 	/*
33730b45077SBjoern A. Zeeb 	 * XXX-BZ we might want to trigger re-evaluation of our default router
33830b45077SBjoern A. Zeeb 	 * availability. E.g., on link down the default router might be
33930b45077SBjoern A. Zeeb 	 * unreachable but a different interface might still have connectivity.
34030b45077SBjoern A. Zeeb 	 */
34130b45077SBjoern A. Zeeb 
34230b45077SBjoern A. Zeeb #ifdef EXPERIMENTAL
34330b45077SBjoern A. Zeeb 	if (linkstate == LINK_STATE_DOWN)
34430b45077SBjoern A. Zeeb 		defrtr_ipv6_only_ipf_down(ifp);
34530b45077SBjoern A. Zeeb #endif
34630b45077SBjoern A. Zeeb }
34730b45077SBjoern A. Zeeb 
34882cd038dSYoshinobu Inoue /*
34982cd038dSYoshinobu Inoue  * Receive Router Advertisement Message.
35082cd038dSYoshinobu Inoue  *
35182cd038dSYoshinobu Inoue  * Based on RFC 2461
35282cd038dSYoshinobu Inoue  * TODO: on-link bit on prefix information
35382cd038dSYoshinobu Inoue  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
35482cd038dSYoshinobu Inoue  */
35582cd038dSYoshinobu Inoue void
nd6_ra_input(struct mbuf * m,int off,int icmp6len)3561272577eSXin LI nd6_ra_input(struct mbuf *m, int off, int icmp6len)
35782cd038dSYoshinobu Inoue {
358503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
359503f4e47SBjoern A. Zeeb 	struct nd_ifinfo *ndi;
360503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
361686cdd19SJun-ichiro itojun Hagino 	struct nd_router_advert *nd_ra;
362503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
36382cd038dSYoshinobu Inoue 	struct nd_defrouter *dr;
364503f4e47SBjoern A. Zeeb 	union nd_opts ndopts;
3651d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
366503f4e47SBjoern A. Zeeb 	int mcast;
3674de485feSMark Johnston 
36807cf047dSHajimu UMEMOTO 	/*
369e7fa8d0aSHiroki Sato 	 * We only accept RAs only when the per-interface flag
370e7fa8d0aSHiroki Sato 	 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
37107cf047dSHajimu UMEMOTO 	 */
372503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
373503f4e47SBjoern A. Zeeb 	ndi = ND_IFINFO(ifp);
374e7fa8d0aSHiroki Sato 	if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
37507cf047dSHajimu UMEMOTO 		goto freeit;
37682cd038dSYoshinobu Inoue 
377a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
378a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
379a4061289SAndrey V. Elsukov 		goto freeit;
380a4061289SAndrey V. Elsukov 
381503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
3824caea9b1SMark Johnston 	if (__predict_false(ip6->ip6_hlim != 255)) {
3834caea9b1SMark Johnston 		ICMP6STAT_INC(icp6s_invlhlim);
38433841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
385c6feea3bSBjoern A. Zeeb 		    "%s: invalid hlim (%d) from %s to %s on %s\n", __func__,
3861d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
3871d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
38833841545SHajimu UMEMOTO 		goto bad;
38982cd038dSYoshinobu Inoue 	}
39082cd038dSYoshinobu Inoue 
391503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
39282cd038dSYoshinobu Inoue 	if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
39333841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
394c6feea3bSBjoern A. Zeeb 		    "%s: src %s is not link-local\n", __func__,
3951d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6)));
39633841545SHajimu UMEMOTO 		goto bad;
397686cdd19SJun-ichiro itojun Hagino 	}
398686cdd19SJun-ichiro itojun Hagino 
399a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + icmp6len) {
400a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + icmp6len);
401a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
402a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
40382cd038dSYoshinobu Inoue 			return;
40482cd038dSYoshinobu Inoue 		}
405a4adf6ccSBjoern A. Zeeb 	}
406a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
407a61b5cfbSBjoern A. Zeeb 	nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
40882cd038dSYoshinobu Inoue 
40982cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_ra);
41082cd038dSYoshinobu Inoue 	nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
41182cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
41233841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
413c6feea3bSBjoern A. Zeeb 		    "%s: invalid ND option, ignored\n", __func__));
41433841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
415686cdd19SJun-ichiro itojun Hagino 		goto freeit;
41682cd038dSYoshinobu Inoue 	}
41782cd038dSYoshinobu Inoue 
418503f4e47SBjoern A. Zeeb 	mcast = 0;
419503f4e47SBjoern A. Zeeb 	dr = NULL;
42082cd038dSYoshinobu Inoue     {
42182cd038dSYoshinobu Inoue 	struct nd_defrouter dr0;
42282cd038dSYoshinobu Inoue 	u_int32_t advreachable = nd_ra->nd_ra_reachable;
42382cd038dSYoshinobu Inoue 
424743eee66SSUZUKI Shinsuke 	/* remember if this is a multicasted advertisement */
425743eee66SSUZUKI Shinsuke 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
426743eee66SSUZUKI Shinsuke 		mcast = 1;
427743eee66SSUZUKI Shinsuke 
428743eee66SSUZUKI Shinsuke 	bzero(&dr0, sizeof(dr0));
42982cd038dSYoshinobu Inoue 	dr0.rtaddr = saddr6;
43001869be5SMark Johnston 	dr0.raflags = nd_ra->nd_ra_flags_reserved;
431e7fa8d0aSHiroki Sato 	/*
432049087a0SHiroki Sato 	 * Effectively-disable routes from RA messages when
433049087a0SHiroki Sato 	 * ND6_IFF_NO_RADR enabled on the receiving interface or
434049087a0SHiroki Sato 	 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
435e7fa8d0aSHiroki Sato 	 */
436049087a0SHiroki Sato 	if (ndi->flags & ND6_IFF_NO_RADR)
437049087a0SHiroki Sato 		dr0.rtlifetime = 0;
438049087a0SHiroki Sato 	else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
439e7fa8d0aSHiroki Sato 		dr0.rtlifetime = 0;
440e7fa8d0aSHiroki Sato 	else
44182cd038dSYoshinobu Inoue 		dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
4427d26db17SHiroki Sato 	dr0.expire = time_uptime + dr0.rtlifetime;
44382cd038dSYoshinobu Inoue 	dr0.ifp = ifp;
44482cd038dSYoshinobu Inoue 	/* unspecified or not? (RFC 2461 6.3.4) */
44582cd038dSYoshinobu Inoue 	if (advreachable) {
446fd8e4ebcSMike Barcroft 		advreachable = ntohl(advreachable);
44782cd038dSYoshinobu Inoue 		if (advreachable <= MAX_REACHABLE_TIME &&
44882cd038dSYoshinobu Inoue 		    ndi->basereachable != advreachable) {
44982cd038dSYoshinobu Inoue 			ndi->basereachable = advreachable;
45082cd038dSYoshinobu Inoue 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
451603724d3SBjoern A. Zeeb 			ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
45282cd038dSYoshinobu Inoue 		}
45382cd038dSYoshinobu Inoue 	}
45482cd038dSYoshinobu Inoue 	if (nd_ra->nd_ra_retransmit)
45582cd038dSYoshinobu Inoue 		ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
456dd385660SXin LI 	if (nd_ra->nd_ra_curhoplimit) {
457dd385660SXin LI 		if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
45882cd038dSYoshinobu Inoue 			ndi->chlim = nd_ra->nd_ra_curhoplimit;
459dd385660SXin LI 		else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
460dd385660SXin LI 			log(LOG_ERR, "RA with a lower CurHopLimit sent from "
461dd385660SXin LI 			    "%s on %s (current = %d, received = %d). "
462dd385660SXin LI 			    "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
463dd385660SXin LI 			    if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
464dd385660SXin LI 		}
465dd385660SXin LI 	}
46682cd038dSYoshinobu Inoue 	dr = defrtrlist_update(&dr0);
467201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
468201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(ifp);
469201100c5SBjoern A. Zeeb #endif
47082cd038dSYoshinobu Inoue     }
47182cd038dSYoshinobu Inoue 
47282cd038dSYoshinobu Inoue 	/*
47382cd038dSYoshinobu Inoue 	 * prefix
47482cd038dSYoshinobu Inoue 	 */
47582cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_pi) {
47682cd038dSYoshinobu Inoue 		struct nd_opt_hdr *pt;
47733841545SHajimu UMEMOTO 		struct nd_opt_prefix_info *pi = NULL;
478743eee66SSUZUKI Shinsuke 		struct nd_prefixctl pr;
47982cd038dSYoshinobu Inoue 
48082cd038dSYoshinobu Inoue 		for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
48182cd038dSYoshinobu Inoue 		     pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
48282cd038dSYoshinobu Inoue 		     pt = (struct nd_opt_hdr *)((caddr_t)pt +
48382cd038dSYoshinobu Inoue 						(pt->nd_opt_len << 3))) {
48482cd038dSYoshinobu Inoue 			if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
48582cd038dSYoshinobu Inoue 				continue;
48682cd038dSYoshinobu Inoue 			pi = (struct nd_opt_prefix_info *)pt;
48782cd038dSYoshinobu Inoue 
48882cd038dSYoshinobu Inoue 			if (pi->nd_opt_pi_len != 4) {
48933841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
490c6feea3bSBjoern A. Zeeb 				    "%s: invalid option len %d for prefix "
491c6feea3bSBjoern A. Zeeb 				    "information option, ignored\n", __func__,
492c6feea3bSBjoern A. Zeeb 				    pi->nd_opt_pi_len));
49382cd038dSYoshinobu Inoue 				continue;
49482cd038dSYoshinobu Inoue 			}
49582cd038dSYoshinobu Inoue 
49682cd038dSYoshinobu Inoue 			if (128 < pi->nd_opt_pi_prefix_len) {
49733841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
498c6feea3bSBjoern A. Zeeb 				    "%s: invalid prefix len %d for prefix "
499c6feea3bSBjoern A. Zeeb 				    "information option, ignored\n", __func__,
500c6feea3bSBjoern A. Zeeb 				    pi->nd_opt_pi_prefix_len));
50182cd038dSYoshinobu Inoue 				continue;
50282cd038dSYoshinobu Inoue 			}
50382cd038dSYoshinobu Inoue 
50482cd038dSYoshinobu Inoue 			if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
50582cd038dSYoshinobu Inoue 			 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
50633841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
507c6feea3bSBjoern A. Zeeb 				    "%s: invalid prefix %s, ignored\n",
508c6feea3bSBjoern A. Zeeb 				    __func__, ip6_sprintf(ip6bufs,
5091d54aa3bSBjoern A. Zeeb 					&pi->nd_opt_pi_prefix)));
51082cd038dSYoshinobu Inoue 				continue;
51182cd038dSYoshinobu Inoue 			}
51282cd038dSYoshinobu Inoue 
51382cd038dSYoshinobu Inoue 			bzero(&pr, sizeof(pr));
51482cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_family = AF_INET6;
51582cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
51682cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
51782cd038dSYoshinobu Inoue 			pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
51882cd038dSYoshinobu Inoue 
51982cd038dSYoshinobu Inoue 			pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
52082cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
52182cd038dSYoshinobu Inoue 			pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
52282cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
5238036234cSAlexander V. Chernikov 			pr.ndpr_raf_ra_derived = 1;
52482cd038dSYoshinobu Inoue 			pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
52582cd038dSYoshinobu Inoue 			pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
52607eb2995SHajimu UMEMOTO 			pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
527743eee66SSUZUKI Shinsuke 			(void)prelist_update(&pr, dr, m, mcast);
52882cd038dSYoshinobu Inoue 		}
52982cd038dSYoshinobu Inoue 	}
5304de485feSMark Johnston 	if (dr != NULL) {
5314de485feSMark Johnston 		defrouter_rele(dr);
5324de485feSMark Johnston 		dr = NULL;
5334de485feSMark Johnston 	}
53482cd038dSYoshinobu Inoue 
53582cd038dSYoshinobu Inoue 	/*
53682cd038dSYoshinobu Inoue 	 * MTU
53782cd038dSYoshinobu Inoue 	 */
53882cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
53931b3783cSHajimu UMEMOTO 		u_long mtu;
54031b3783cSHajimu UMEMOTO 		u_long maxmtu;
54107eb2995SHajimu UMEMOTO 
54231b3783cSHajimu UMEMOTO 		mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
54382cd038dSYoshinobu Inoue 
54482cd038dSYoshinobu Inoue 		/* lower bound */
54582cd038dSYoshinobu Inoue 		if (mtu < IPV6_MMTU) {
546c6feea3bSBjoern A. Zeeb 			nd6log((LOG_INFO, "%s: bogus mtu option mtu=%lu sent "
547c6feea3bSBjoern A. Zeeb 			    "from %s, ignoring\n", __func__,
5481d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
54982cd038dSYoshinobu Inoue 			goto skip;
55082cd038dSYoshinobu Inoue 		}
55182cd038dSYoshinobu Inoue 
55282cd038dSYoshinobu Inoue 		/* upper bound */
55331b3783cSHajimu UMEMOTO 		maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
55431b3783cSHajimu UMEMOTO 		    ? ndi->maxmtu : ifp->if_mtu;
55531b3783cSHajimu UMEMOTO 		if (mtu <= maxmtu) {
55682cd038dSYoshinobu Inoue 			int change = (ndi->linkmtu != mtu);
55782cd038dSYoshinobu Inoue 
55882cd038dSYoshinobu Inoue 			ndi->linkmtu = mtu;
5590bbfb20fSEric van Gyzen 			if (change) {
5600bbfb20fSEric van Gyzen 				/* in6_maxmtu may change */
56182cd038dSYoshinobu Inoue 				in6_setmaxmtu();
5620bbfb20fSEric van Gyzen 				rt_updatemtu(ifp);
5630bbfb20fSEric van Gyzen 			}
56482cd038dSYoshinobu Inoue 		} else {
565c6feea3bSBjoern A. Zeeb 			nd6log((LOG_INFO, "%s: bogus mtu=%lu sent from %s; "
566c6feea3bSBjoern A. Zeeb 			    "exceeds maxmtu %lu, ignoring\n", __func__,
5671d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
56882cd038dSYoshinobu Inoue 		}
56982cd038dSYoshinobu Inoue 	}
57082cd038dSYoshinobu Inoue 
57182cd038dSYoshinobu Inoue  skip:
57282cd038dSYoshinobu Inoue 
57382cd038dSYoshinobu Inoue 	/*
57488ff5695SSUZUKI Shinsuke 	 * Source link layer address
57582cd038dSYoshinobu Inoue 	 */
57682cd038dSYoshinobu Inoue     {
57782cd038dSYoshinobu Inoue 	char *lladdr = NULL;
57882cd038dSYoshinobu Inoue 	int lladdrlen = 0;
57982cd038dSYoshinobu Inoue 
58082cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
58182cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
58282cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
58382cd038dSYoshinobu Inoue 	}
58482cd038dSYoshinobu Inoue 
58582cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
58633841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
587c6feea3bSBjoern A. Zeeb 		    "%s: lladdrlen mismatch for %s (if %d, RA packet %d)\n",
588c6feea3bSBjoern A. Zeeb 		    __func__, ip6_sprintf(ip6bufs, &saddr6),
58907eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
59033841545SHajimu UMEMOTO 		goto bad;
59182cd038dSYoshinobu Inoue 	}
59282cd038dSYoshinobu Inoue 
59307eb2995SHajimu UMEMOTO 	nd6_cache_lladdr(ifp, &saddr6, lladdr,
59407eb2995SHajimu UMEMOTO 	    lladdrlen, ND_ROUTER_ADVERT, 0);
595686cdd19SJun-ichiro itojun Hagino 
596686cdd19SJun-ichiro itojun Hagino 	/*
597686cdd19SJun-ichiro itojun Hagino 	 * Installing a link-layer address might change the state of the
598686cdd19SJun-ichiro itojun Hagino 	 * router's neighbor cache, which might also affect our on-link
599686cdd19SJun-ichiro itojun Hagino 	 * detection of adveritsed prefixes.
600686cdd19SJun-ichiro itojun Hagino 	 */
601686cdd19SJun-ichiro itojun Hagino 	pfxlist_onlink_check();
60282cd038dSYoshinobu Inoue     }
603686cdd19SJun-ichiro itojun Hagino 
604686cdd19SJun-ichiro itojun Hagino  freeit:
605686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
60633841545SHajimu UMEMOTO 	return;
60733841545SHajimu UMEMOTO 
60833841545SHajimu UMEMOTO  bad:
609e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badra);
61033841545SHajimu UMEMOTO 	m_freem(m);
61182cd038dSYoshinobu Inoue }
61282cd038dSYoshinobu Inoue 
613f77a6dbdSBjoern A. Zeeb /* PFXRTR */
614f77a6dbdSBjoern A. Zeeb static struct nd_pfxrouter *
pfxrtr_lookup(struct nd_prefix * pr,struct nd_defrouter * dr)615f77a6dbdSBjoern A. Zeeb pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
616f77a6dbdSBjoern A. Zeeb {
617f77a6dbdSBjoern A. Zeeb 	struct nd_pfxrouter *search;
6184de485feSMark Johnston 
619f77a6dbdSBjoern A. Zeeb 	ND6_LOCK_ASSERT();
620f77a6dbdSBjoern A. Zeeb 
621f77a6dbdSBjoern A. Zeeb 	LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
622f77a6dbdSBjoern A. Zeeb 		if (search->router == dr)
623f77a6dbdSBjoern A. Zeeb 			break;
624f77a6dbdSBjoern A. Zeeb 	}
625f77a6dbdSBjoern A. Zeeb 	return (search);
626f77a6dbdSBjoern A. Zeeb }
627f77a6dbdSBjoern A. Zeeb 
628f77a6dbdSBjoern A. Zeeb static void
pfxrtr_add(struct nd_prefix * pr,struct nd_defrouter * dr)629f77a6dbdSBjoern A. Zeeb pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
630f77a6dbdSBjoern A. Zeeb {
631f77a6dbdSBjoern A. Zeeb 	struct nd_pfxrouter *new;
632f77a6dbdSBjoern A. Zeeb 	bool update;
633f77a6dbdSBjoern A. Zeeb 
634f77a6dbdSBjoern A. Zeeb 	ND6_UNLOCK_ASSERT();
635f77a6dbdSBjoern A. Zeeb 
636f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
637f77a6dbdSBjoern A. Zeeb 	if (pfxrtr_lookup(pr, dr) != NULL) {
638f77a6dbdSBjoern A. Zeeb 		ND6_RUNLOCK();
639f77a6dbdSBjoern A. Zeeb 		return;
640f77a6dbdSBjoern A. Zeeb 	}
641f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
642f77a6dbdSBjoern A. Zeeb 
643f77a6dbdSBjoern A. Zeeb 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
644f77a6dbdSBjoern A. Zeeb 	if (new == NULL)
645f77a6dbdSBjoern A. Zeeb 		return;
646f77a6dbdSBjoern A. Zeeb 	defrouter_ref(dr);
647f77a6dbdSBjoern A. Zeeb 	new->router = dr;
648f77a6dbdSBjoern A. Zeeb 
649f77a6dbdSBjoern A. Zeeb 	ND6_WLOCK();
650f77a6dbdSBjoern A. Zeeb 	if (pfxrtr_lookup(pr, dr) == NULL) {
651f77a6dbdSBjoern A. Zeeb 		LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
652f77a6dbdSBjoern A. Zeeb 		update = true;
653f77a6dbdSBjoern A. Zeeb 	} else {
654f77a6dbdSBjoern A. Zeeb 		/* We lost a race to add the reference. */
655f77a6dbdSBjoern A. Zeeb 		defrouter_rele(dr);
656f77a6dbdSBjoern A. Zeeb 		free(new, M_IP6NDP);
657f77a6dbdSBjoern A. Zeeb 		update = false;
658f77a6dbdSBjoern A. Zeeb 	}
659f77a6dbdSBjoern A. Zeeb 	ND6_WUNLOCK();
660f77a6dbdSBjoern A. Zeeb 
661f77a6dbdSBjoern A. Zeeb 	if (update)
662f77a6dbdSBjoern A. Zeeb 		pfxlist_onlink_check();
663f77a6dbdSBjoern A. Zeeb }
664f77a6dbdSBjoern A. Zeeb 
665f77a6dbdSBjoern A. Zeeb static void
pfxrtr_del(struct nd_pfxrouter * pfr)666f77a6dbdSBjoern A. Zeeb pfxrtr_del(struct nd_pfxrouter *pfr)
667f77a6dbdSBjoern A. Zeeb {
668f77a6dbdSBjoern A. Zeeb 
669f77a6dbdSBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
670f77a6dbdSBjoern A. Zeeb 
671f77a6dbdSBjoern A. Zeeb 	LIST_REMOVE(pfr, pfr_entry);
672f77a6dbdSBjoern A. Zeeb 	defrouter_rele(pfr->router);
673f77a6dbdSBjoern A. Zeeb 	free(pfr, M_IP6NDP);
674f77a6dbdSBjoern A. Zeeb }
675f77a6dbdSBjoern A. Zeeb 
676f77a6dbdSBjoern A. Zeeb /* Default router list processing sub routines. */
6779d9b92f2SBjoern A. Zeeb static void
defrouter_addreq(struct nd_defrouter * new)6781272577eSXin LI defrouter_addreq(struct nd_defrouter *new)
67982cd038dSYoshinobu Inoue {
680835a611eSAlexander V. Chernikov 	uint32_t fibnum = new->ifp->if_fib;
681835a611eSAlexander V. Chernikov 	struct rib_cmd_info rc = {};
682835a611eSAlexander V. Chernikov 	int error = 0;
683e1c05fd2SAlexander V. Chernikov 
684e1c05fd2SAlexander V. Chernikov 	NET_EPOCH_ASSERT();
685835a611eSAlexander V. Chernikov 
686835a611eSAlexander V. Chernikov 	struct sockaddr_in6 gw = {
687835a611eSAlexander V. Chernikov 		.sin6_family = AF_INET6,
688835a611eSAlexander V. Chernikov 		.sin6_len = sizeof(struct sockaddr_in6),
689835a611eSAlexander V. Chernikov 		.sin6_addr = new->rtaddr,
690835a611eSAlexander V. Chernikov 	};
691835a611eSAlexander V. Chernikov 
692835a611eSAlexander V. Chernikov 	error = rib_add_default_route(fibnum, AF_INET6, new->ifp,
693835a611eSAlexander V. Chernikov 	    (struct sockaddr *)&gw, &rc);
694835a611eSAlexander V. Chernikov 
6959c4a8d24SAlexander V. Chernikov 	if (error == 0) {
696cc3fa1e2SAlexander V. Chernikov 		struct nhop_object *nh = nhop_select_func(rc.rc_nh_new, 0);
697d68cf57bSAlexander V. Chernikov 		rt_routemsg(RTM_ADD, rc.rc_rt, nh, fibnum);
698743eee66SSUZUKI Shinsuke 		new->installed = 1;
69982cd038dSYoshinobu Inoue 	}
7009c4a8d24SAlexander V. Chernikov }
70182cd038dSYoshinobu Inoue 
702743eee66SSUZUKI Shinsuke /*
703743eee66SSUZUKI Shinsuke  * Remove the default route for a given router.
704559b4296SAlan Somers  * This is just a subroutine function for defrouter_select_fib(), and
705559b4296SAlan Somers  * should not be called from anywhere else.
706743eee66SSUZUKI Shinsuke  */
707743eee66SSUZUKI Shinsuke static void
defrouter_delreq(struct nd_defrouter * dr)7081272577eSXin LI defrouter_delreq(struct nd_defrouter *dr)
70982cd038dSYoshinobu Inoue {
710835a611eSAlexander V. Chernikov 	uint32_t fibnum = dr->ifp->if_fib;
7112bbab0afSAlexander V. Chernikov 	struct epoch_tracker et;
712835a611eSAlexander V. Chernikov 	struct rib_cmd_info rc;
7139c4a8d24SAlexander V. Chernikov 	int error;
71482cd038dSYoshinobu Inoue 
715835a611eSAlexander V. Chernikov 	struct sockaddr_in6 dst = {
716835a611eSAlexander V. Chernikov 		.sin6_family = AF_INET6,
717835a611eSAlexander V. Chernikov 		.sin6_len = sizeof(struct sockaddr_in6),
718835a611eSAlexander V. Chernikov 	};
71982cd038dSYoshinobu Inoue 
720835a611eSAlexander V. Chernikov 	struct sockaddr_in6 gw = {
721835a611eSAlexander V. Chernikov 		.sin6_family = AF_INET6,
722835a611eSAlexander V. Chernikov 		.sin6_len = sizeof(struct sockaddr_in6),
723835a611eSAlexander V. Chernikov 		.sin6_addr = dr->rtaddr,
724835a611eSAlexander V. Chernikov 	};
725e1c05fd2SAlexander V. Chernikov 
7262bbab0afSAlexander V. Chernikov 	NET_EPOCH_ENTER(et);
727835a611eSAlexander V. Chernikov 	error = rib_del_route_px(fibnum, (struct sockaddr *)&dst, 0,
728835a611eSAlexander V. Chernikov 		    rib_match_gw, (struct sockaddr *)&gw, 0, &rc);
7299c4a8d24SAlexander V. Chernikov 	if (error == 0) {
730cc3fa1e2SAlexander V. Chernikov 		struct nhop_object *nh = nhop_select_func(rc.rc_nh_old, 0);
731d68cf57bSAlexander V. Chernikov 		rt_routemsg(RTM_DELETE, rc.rc_rt, nh, fibnum);
732d68cf57bSAlexander V. Chernikov 	}
7332bbab0afSAlexander V. Chernikov 	NET_EPOCH_EXIT(et);
73482cd038dSYoshinobu Inoue 
735743eee66SSUZUKI Shinsuke 	dr->installed = 0;
736743eee66SSUZUKI Shinsuke }
737743eee66SSUZUKI Shinsuke 
738ad675b32SBjoern A. Zeeb static void
defrouter_del(struct nd_defrouter * dr)7394de485feSMark Johnston defrouter_del(struct nd_defrouter *dr)
74082cd038dSYoshinobu Inoue {
74182cd038dSYoshinobu Inoue 	struct nd_defrouter *deldr = NULL;
74282cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
743d748f7efSMark Johnston 	struct nd_pfxrouter *pfxrtr;
74482cd038dSYoshinobu Inoue 
7454de485feSMark Johnston 	ND6_UNLOCK_ASSERT();
7464de485feSMark Johnston 
74782cd038dSYoshinobu Inoue 	/*
74882cd038dSYoshinobu Inoue 	 * Flush all the routing table entries that use the router
74982cd038dSYoshinobu Inoue 	 * as a next hop.
75082cd038dSYoshinobu Inoue 	 */
751e7fa8d0aSHiroki Sato 	if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
75282cd038dSYoshinobu Inoue 		rt6_flush(&dr->rtaddr, dr->ifp);
75382cd038dSYoshinobu Inoue 
754201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
755201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(dr->ifp);
756201100c5SBjoern A. Zeeb #endif
757201100c5SBjoern A. Zeeb 
758743eee66SSUZUKI Shinsuke 	if (dr->installed) {
759743eee66SSUZUKI Shinsuke 		deldr = dr;
760743eee66SSUZUKI Shinsuke 		defrouter_delreq(dr);
761743eee66SSUZUKI Shinsuke 	}
76282cd038dSYoshinobu Inoue 
76382cd038dSYoshinobu Inoue 	/*
76482cd038dSYoshinobu Inoue 	 * Also delete all the pointers to the router in each prefix lists.
76582cd038dSYoshinobu Inoue 	 */
766d748f7efSMark Johnston 	ND6_WLOCK();
7673b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
76882cd038dSYoshinobu Inoue 		if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
76982cd038dSYoshinobu Inoue 			pfxrtr_del(pfxrtr);
77082cd038dSYoshinobu Inoue 	}
771d748f7efSMark Johnston 	ND6_WUNLOCK();
772d748f7efSMark Johnston 
77382cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
77482cd038dSYoshinobu Inoue 
77582cd038dSYoshinobu Inoue 	/*
776686cdd19SJun-ichiro itojun Hagino 	 * If the router is the primary one, choose a new one.
777559b4296SAlan Somers 	 * Note that defrouter_select_fib() will remove the current
778559b4296SAlan Somers          * gateway from the routing table.
77982cd038dSYoshinobu Inoue 	 */
78082cd038dSYoshinobu Inoue 	if (deldr)
781559b4296SAlan Somers 		defrouter_select_fib(deldr->ifp->if_fib);
782686cdd19SJun-ichiro itojun Hagino 
7834de485feSMark Johnston 	/*
7844de485feSMark Johnston 	 * Release the list reference.
7854de485feSMark Johnston 	 */
7864de485feSMark Johnston 	defrouter_rele(dr);
78782cd038dSYoshinobu Inoue }
78882cd038dSYoshinobu Inoue 
789f77a6dbdSBjoern A. Zeeb struct nd_defrouter *
defrouter_lookup_locked(const struct in6_addr * addr,struct ifnet * ifp)790aaad3c4fSAlexander V. Chernikov defrouter_lookup_locked(const struct in6_addr *addr, struct ifnet *ifp)
791f77a6dbdSBjoern A. Zeeb {
792f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
793f77a6dbdSBjoern A. Zeeb 
794f77a6dbdSBjoern A. Zeeb 	ND6_LOCK_ASSERT();
795f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
796f77a6dbdSBjoern A. Zeeb 		if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
797f77a6dbdSBjoern A. Zeeb 			defrouter_ref(dr);
798f77a6dbdSBjoern A. Zeeb 			return (dr);
799f77a6dbdSBjoern A. Zeeb 		}
800f77a6dbdSBjoern A. Zeeb 	return (NULL);
801f77a6dbdSBjoern A. Zeeb }
802f77a6dbdSBjoern A. Zeeb 
803f77a6dbdSBjoern A. Zeeb struct nd_defrouter *
defrouter_lookup(const struct in6_addr * addr,struct ifnet * ifp)804aaad3c4fSAlexander V. Chernikov defrouter_lookup(const struct in6_addr *addr, struct ifnet *ifp)
805f77a6dbdSBjoern A. Zeeb {
806f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
807f77a6dbdSBjoern A. Zeeb 
808f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
809f77a6dbdSBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
810f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
811f77a6dbdSBjoern A. Zeeb 	return (dr);
812f77a6dbdSBjoern A. Zeeb }
813f77a6dbdSBjoern A. Zeeb 
814f77a6dbdSBjoern A. Zeeb /*
815f77a6dbdSBjoern A. Zeeb  * Remove all default routes from default router list.
816f77a6dbdSBjoern A. Zeeb  */
817f77a6dbdSBjoern A. Zeeb void
defrouter_reset(void)818f77a6dbdSBjoern A. Zeeb defrouter_reset(void)
819f77a6dbdSBjoern A. Zeeb {
820f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr, **dra;
821f77a6dbdSBjoern A. Zeeb 	int count, i;
822f77a6dbdSBjoern A. Zeeb 
823f77a6dbdSBjoern A. Zeeb 	count = i = 0;
824f77a6dbdSBjoern A. Zeeb 
825f77a6dbdSBjoern A. Zeeb 	/*
826f77a6dbdSBjoern A. Zeeb 	 * We can't delete routes with the ND lock held, so make a copy of the
827f77a6dbdSBjoern A. Zeeb 	 * current default router list and use that when deleting routes.
828f77a6dbdSBjoern A. Zeeb 	 */
829f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
830f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
831f77a6dbdSBjoern A. Zeeb 		count++;
832f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
833f77a6dbdSBjoern A. Zeeb 
834f77a6dbdSBjoern A. Zeeb 	dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
835f77a6dbdSBjoern A. Zeeb 
836f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
837f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
838f77a6dbdSBjoern A. Zeeb 		if (i == count)
839f77a6dbdSBjoern A. Zeeb 			break;
840f77a6dbdSBjoern A. Zeeb 		defrouter_ref(dr);
841f77a6dbdSBjoern A. Zeeb 		dra[i++] = dr;
842f77a6dbdSBjoern A. Zeeb 	}
843f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
844f77a6dbdSBjoern A. Zeeb 
845f77a6dbdSBjoern A. Zeeb 	for (i = 0; i < count && dra[i] != NULL; i++) {
846f77a6dbdSBjoern A. Zeeb 		defrouter_delreq(dra[i]);
847f77a6dbdSBjoern A. Zeeb 		defrouter_rele(dra[i]);
848f77a6dbdSBjoern A. Zeeb 	}
849f77a6dbdSBjoern A. Zeeb 	free(dra, M_TEMP);
850f77a6dbdSBjoern A. Zeeb 
851f77a6dbdSBjoern A. Zeeb 	/*
852f77a6dbdSBjoern A. Zeeb 	 * XXX should we also nuke any default routers in the kernel, by
853f77a6dbdSBjoern A. Zeeb 	 * going through them by rtalloc1()?
854f77a6dbdSBjoern A. Zeeb 	 */
855f77a6dbdSBjoern A. Zeeb }
856f77a6dbdSBjoern A. Zeeb 
857ad675b32SBjoern A. Zeeb /*
858ad675b32SBjoern A. Zeeb  * Look up a matching default router list entry and remove it. Returns true if a
859ad675b32SBjoern A. Zeeb  * matching entry was found, false otherwise.
860ad675b32SBjoern A. Zeeb  */
861ad675b32SBjoern A. Zeeb bool
defrouter_remove(struct in6_addr * addr,struct ifnet * ifp)862ad675b32SBjoern A. Zeeb defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
863ad675b32SBjoern A. Zeeb {
864ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
865ad675b32SBjoern A. Zeeb 
866ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
867ad675b32SBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
868ad675b32SBjoern A. Zeeb 	if (dr == NULL) {
869ad675b32SBjoern A. Zeeb 		ND6_WUNLOCK();
870ad675b32SBjoern A. Zeeb 		return (false);
871ad675b32SBjoern A. Zeeb 	}
872ad675b32SBjoern A. Zeeb 
873ad675b32SBjoern A. Zeeb 	defrouter_unlink(dr, NULL);
874ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
875ad675b32SBjoern A. Zeeb 	defrouter_del(dr);
876ad675b32SBjoern A. Zeeb 	defrouter_rele(dr);
877ad675b32SBjoern A. Zeeb 	return (true);
878ad675b32SBjoern A. Zeeb }
879ad675b32SBjoern A. Zeeb 
880686cdd19SJun-ichiro itojun Hagino /*
881f77a6dbdSBjoern A. Zeeb  * for default router selection
882f77a6dbdSBjoern A. Zeeb  * regards router-preference field as a 2-bit signed integer
883f77a6dbdSBjoern A. Zeeb  */
884f77a6dbdSBjoern A. Zeeb static int
rtpref(struct nd_defrouter * dr)885f77a6dbdSBjoern A. Zeeb rtpref(struct nd_defrouter *dr)
886f77a6dbdSBjoern A. Zeeb {
887f77a6dbdSBjoern A. Zeeb 	switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
888f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_HIGH:
889f77a6dbdSBjoern A. Zeeb 		return (RTPREF_HIGH);
890f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_MEDIUM:
891f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_RSV:
892f77a6dbdSBjoern A. Zeeb 		return (RTPREF_MEDIUM);
893f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_LOW:
894f77a6dbdSBjoern A. Zeeb 		return (RTPREF_LOW);
895f77a6dbdSBjoern A. Zeeb 	default:
896f77a6dbdSBjoern A. Zeeb 		/*
897f77a6dbdSBjoern A. Zeeb 		 * This case should never happen.  If it did, it would mean a
898f77a6dbdSBjoern A. Zeeb 		 * serious bug of kernel internal.  We thus always bark here.
899f77a6dbdSBjoern A. Zeeb 		 * Or, can we even panic?
900f77a6dbdSBjoern A. Zeeb 		 */
901f77a6dbdSBjoern A. Zeeb 		log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
902f77a6dbdSBjoern A. Zeeb 		return (RTPREF_INVALID);
903f77a6dbdSBjoern A. Zeeb 	}
904f77a6dbdSBjoern A. Zeeb 	/* NOTREACHED */
905f77a6dbdSBjoern A. Zeeb }
906f77a6dbdSBjoern A. Zeeb 
9079d16275cSAlexander V. Chernikov static bool
is_dr_reachable(const struct nd_defrouter * dr)9089d16275cSAlexander V. Chernikov is_dr_reachable(const struct nd_defrouter *dr) {
9099d16275cSAlexander V. Chernikov 	struct llentry *ln = NULL;
9109d16275cSAlexander V. Chernikov 
9119d16275cSAlexander V. Chernikov 	ln = nd6_lookup(&dr->rtaddr, LLE_SF(AF_INET6, 0), dr->ifp);
9129d16275cSAlexander V. Chernikov 	if (ln == NULL)
9139d16275cSAlexander V. Chernikov 		return (false);
9149d16275cSAlexander V. Chernikov 	bool reachable = ND6_IS_LLINFO_PROBREACH(ln);
9159d16275cSAlexander V. Chernikov 	LLE_RUNLOCK(ln);
9169d16275cSAlexander V. Chernikov 	return reachable;
9179d16275cSAlexander V. Chernikov }
9189d16275cSAlexander V. Chernikov 
919f77a6dbdSBjoern A. Zeeb /*
920743eee66SSUZUKI Shinsuke  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
921743eee66SSUZUKI Shinsuke  * draft-ietf-ipngwg-router-selection:
922743eee66SSUZUKI Shinsuke  * 1) Routers that are reachable or probably reachable should be preferred.
923743eee66SSUZUKI Shinsuke  *    If we have more than one (probably) reachable router, prefer ones
924743eee66SSUZUKI Shinsuke  *    with the highest router preference.
925686cdd19SJun-ichiro itojun Hagino  * 2) When no routers on the list are known to be reachable or
926686cdd19SJun-ichiro itojun Hagino  *    probably reachable, routers SHOULD be selected in a round-robin
927743eee66SSUZUKI Shinsuke  *    fashion, regardless of router preference values.
928686cdd19SJun-ichiro itojun Hagino  * 3) If the Default Router List is empty, assume that all
929686cdd19SJun-ichiro itojun Hagino  *    destinations are on-link.
930743eee66SSUZUKI Shinsuke  *
931743eee66SSUZUKI Shinsuke  * We assume nd_defrouter is sorted by router preference value.
932743eee66SSUZUKI Shinsuke  * Since the code below covers both with and without router preference cases,
933743eee66SSUZUKI Shinsuke  * we do not need to classify the cases by ifdef.
934743eee66SSUZUKI Shinsuke  *
935743eee66SSUZUKI Shinsuke  * At this moment, we do not try to install more than one default router,
936743eee66SSUZUKI Shinsuke  * even when the multipath routing is available, because we're not sure about
937743eee66SSUZUKI Shinsuke  * the benefits for stub hosts comparing to the risk of making the code
938743eee66SSUZUKI Shinsuke  * complicated and the possibility of introducing bugs.
939559b4296SAlan Somers  *
940559b4296SAlan Somers  * We maintain a single list of routers for multiple FIBs, only considering one
941559b4296SAlan Somers  * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
942559b4296SAlan Somers  * we do the whole thing multiple times.
943686cdd19SJun-ichiro itojun Hagino  */
944686cdd19SJun-ichiro itojun Hagino void
defrouter_select_fib(int fibnum)945559b4296SAlan Somers defrouter_select_fib(int fibnum)
946686cdd19SJun-ichiro itojun Hagino {
947a68cc388SGleb Smirnoff 	struct epoch_tracker et;
9484de485feSMark Johnston 	struct nd_defrouter *dr, *selected_dr, *installed_dr;
949686cdd19SJun-ichiro itojun Hagino 
950559b4296SAlan Somers 	if (fibnum == RT_ALL_FIBS) {
951559b4296SAlan Somers 		for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
952559b4296SAlan Somers 			defrouter_select_fib(fibnum);
953559b4296SAlan Somers 		}
9549d16275cSAlexander V. Chernikov 		return;
955559b4296SAlan Somers 	}
956559b4296SAlan Somers 
9574de485feSMark Johnston 	ND6_RLOCK();
958686cdd19SJun-ichiro itojun Hagino 	/*
959743eee66SSUZUKI Shinsuke 	 * Let's handle easy case (3) first:
960743eee66SSUZUKI Shinsuke 	 * If default router list is empty, there's nothing to be done.
961743eee66SSUZUKI Shinsuke 	 */
962f592d0c3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_nd6_defrouter)) {
9634de485feSMark Johnston 		ND6_RUNLOCK();
964743eee66SSUZUKI Shinsuke 		return;
9654de485feSMark Johnston 	}
966743eee66SSUZUKI Shinsuke 
967743eee66SSUZUKI Shinsuke 	/*
968686cdd19SJun-ichiro itojun Hagino 	 * Search for a (probably) reachable router from the list.
969743eee66SSUZUKI Shinsuke 	 * We just pick up the first reachable one (if any), assuming that
970743eee66SSUZUKI Shinsuke 	 * the ordering rule of the list described in defrtrlist_update().
971686cdd19SJun-ichiro itojun Hagino 	 */
9724de485feSMark Johnston 	selected_dr = installed_dr = NULL;
973a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
9749d16275cSAlexander V. Chernikov 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
9759d16275cSAlexander V. Chernikov 		if (dr->ifp->if_fib != fibnum)
9769d16275cSAlexander V. Chernikov 			continue;
9779d16275cSAlexander V. Chernikov 
9789d16275cSAlexander V. Chernikov 		if (selected_dr == NULL && is_dr_reachable(dr)) {
979743eee66SSUZUKI Shinsuke 			selected_dr = dr;
9804de485feSMark Johnston 			defrouter_ref(selected_dr);
981686cdd19SJun-ichiro itojun Hagino 		}
982686cdd19SJun-ichiro itojun Hagino 
9839d16275cSAlexander V. Chernikov 		if (dr->installed) {
9844de485feSMark Johnston 			if (installed_dr == NULL) {
985743eee66SSUZUKI Shinsuke 				installed_dr = dr;
9864de485feSMark Johnston 				defrouter_ref(installed_dr);
9874de485feSMark Johnston 			} else {
988559b4296SAlan Somers 				/*
989559b4296SAlan Somers 				 * this should not happen.
990559b4296SAlan Somers 				 * warn for diagnosis.
991559b4296SAlan Somers 				 */
992559b4296SAlan Somers 				log(LOG_ERR, "defrouter_select_fib: more than "
993559b4296SAlan Somers 				             "one router is installed\n");
9944de485feSMark Johnston 			}
99533841545SHajimu UMEMOTO 		}
996686cdd19SJun-ichiro itojun Hagino 	}
9979d16275cSAlexander V. Chernikov 
998743eee66SSUZUKI Shinsuke 	/*
999743eee66SSUZUKI Shinsuke 	 * If none of the default routers was found to be reachable,
1000743eee66SSUZUKI Shinsuke 	 * round-robin the list regardless of preference.
1001743eee66SSUZUKI Shinsuke 	 * Otherwise, if we have an installed router, check if the selected
1002743eee66SSUZUKI Shinsuke 	 * (reachable) router should really be preferred to the installed one.
1003743eee66SSUZUKI Shinsuke 	 * We only prefer the new router when the old one is not reachable
1004743eee66SSUZUKI Shinsuke 	 * or when the new one has a really higher preference value.
1005743eee66SSUZUKI Shinsuke 	 */
1006743eee66SSUZUKI Shinsuke 	if (selected_dr == NULL) {
10074de485feSMark Johnston 		if (installed_dr == NULL ||
10084de485feSMark Johnston 		    TAILQ_NEXT(installed_dr, dr_entry) == NULL)
1009f592d0c3SBjoern A. Zeeb 			dr = TAILQ_FIRST(&V_nd6_defrouter);
1010743eee66SSUZUKI Shinsuke 		else
1011559b4296SAlan Somers 			dr = TAILQ_NEXT(installed_dr, dr_entry);
1012559b4296SAlan Somers 
1013559b4296SAlan Somers 		/* Ensure we select a router for this FIB. */
1014f592d0c3SBjoern A. Zeeb 		TAILQ_FOREACH_FROM(dr, &V_nd6_defrouter, dr_entry) {
1015559b4296SAlan Somers 			if (dr->ifp->if_fib == fibnum) {
1016559b4296SAlan Somers 				selected_dr = dr;
10174de485feSMark Johnston 				defrouter_ref(selected_dr);
1018559b4296SAlan Somers 				break;
1019559b4296SAlan Somers 			}
1020559b4296SAlan Somers 		}
10214de485feSMark Johnston 	} else if (installed_dr != NULL) {
10229d16275cSAlexander V. Chernikov 		if (is_dr_reachable(installed_dr) &&
1023743eee66SSUZUKI Shinsuke 		    rtpref(selected_dr) <= rtpref(installed_dr)) {
10244de485feSMark Johnston 			defrouter_rele(selected_dr);
1025743eee66SSUZUKI Shinsuke 			selected_dr = installed_dr;
1026743eee66SSUZUKI Shinsuke 		}
10276e6b3f7cSQing Li 	}
10284de485feSMark Johnston 	ND6_RUNLOCK();
1029743eee66SSUZUKI Shinsuke 
1030743eee66SSUZUKI Shinsuke 	/*
1031559b4296SAlan Somers 	 * If we selected a router for this FIB and it's different
1032559b4296SAlan Somers 	 * than the installed one, remove the installed router and
1033559b4296SAlan Somers 	 * install the selected one in its place.
1034743eee66SSUZUKI Shinsuke 	 */
1035743eee66SSUZUKI Shinsuke 	if (installed_dr != selected_dr) {
10364de485feSMark Johnston 		if (installed_dr != NULL) {
1037743eee66SSUZUKI Shinsuke 			defrouter_delreq(installed_dr);
10384de485feSMark Johnston 			defrouter_rele(installed_dr);
10394de485feSMark Johnston 		}
1040559b4296SAlan Somers 		if (selected_dr != NULL)
1041743eee66SSUZUKI Shinsuke 			defrouter_addreq(selected_dr);
1042686cdd19SJun-ichiro itojun Hagino 	}
1043559b4296SAlan Somers 	if (selected_dr != NULL)
10444de485feSMark Johnston 		defrouter_rele(selected_dr);
10452bbab0afSAlexander V. Chernikov 	NET_EPOCH_EXIT(et);
1046686cdd19SJun-ichiro itojun Hagino }
1047686cdd19SJun-ichiro itojun Hagino 
104882cd038dSYoshinobu Inoue static struct nd_defrouter *
defrtrlist_update(struct nd_defrouter * new)10491272577eSXin LI defrtrlist_update(struct nd_defrouter *new)
105082cd038dSYoshinobu Inoue {
105182cd038dSYoshinobu Inoue 	struct nd_defrouter *dr, *n;
1052d748f7efSMark Johnston 	uint64_t genid;
1053a4a7c825SMark Johnston 	int oldpref;
1054d748f7efSMark Johnston 	bool writelocked;
105582cd038dSYoshinobu Inoue 
105682cd038dSYoshinobu Inoue 	if (new->rtlifetime == 0) {
1057ff63037dSMark Johnston 		defrouter_remove(&new->rtaddr, new->ifp);
1058a4a7c825SMark Johnston 		return (NULL);
1059a4a7c825SMark Johnston 	}
1060a4a7c825SMark Johnston 
1061d748f7efSMark Johnston 	ND6_RLOCK();
1062d748f7efSMark Johnston 	writelocked = false;
1063d748f7efSMark Johnston restart:
1064ff63037dSMark Johnston 	dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
1065ff63037dSMark Johnston 	if (dr != NULL) {
1066a4a7c825SMark Johnston 		oldpref = rtpref(dr);
1067743eee66SSUZUKI Shinsuke 
106882cd038dSYoshinobu Inoue 		/* override */
106901869be5SMark Johnston 		dr->raflags = new->raflags; /* XXX flag check */
107082cd038dSYoshinobu Inoue 		dr->rtlifetime = new->rtlifetime;
107182cd038dSYoshinobu Inoue 		dr->expire = new->expire;
1072743eee66SSUZUKI Shinsuke 
1073743eee66SSUZUKI Shinsuke 		/*
1074743eee66SSUZUKI Shinsuke 		 * If the preference does not change, there's no need
10750f1aca65SQing Li 		 * to sort the entries. Also make sure the selected
10760f1aca65SQing Li 		 * router is still installed in the kernel.
1077743eee66SSUZUKI Shinsuke 		 */
10784de485feSMark Johnston 		if (dr->installed && rtpref(new) == oldpref) {
1079d748f7efSMark Johnston 			if (writelocked)
10804de485feSMark Johnston 				ND6_WUNLOCK();
1081d748f7efSMark Johnston 			else
1082d748f7efSMark Johnston 				ND6_RUNLOCK();
1083743eee66SSUZUKI Shinsuke 			return (dr);
10844de485feSMark Johnston 		}
1085d748f7efSMark Johnston 	}
1086743eee66SSUZUKI Shinsuke 
1087743eee66SSUZUKI Shinsuke 	/*
1088d748f7efSMark Johnston 	 * The router needs to be reinserted into the default router
1089d748f7efSMark Johnston 	 * list, so upgrade to a write lock. If that fails and the list
1090d748f7efSMark Johnston 	 * has potentially changed while the lock was dropped, we'll
1091d748f7efSMark Johnston 	 * redo the lookup with the write lock held.
1092d748f7efSMark Johnston 	 */
1093d748f7efSMark Johnston 	if (!writelocked) {
1094d748f7efSMark Johnston 		writelocked = true;
1095d748f7efSMark Johnston 		if (!ND6_TRY_UPGRADE()) {
1096d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1097d748f7efSMark Johnston 			ND6_RUNLOCK();
1098d748f7efSMark Johnston 			ND6_WLOCK();
1099d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1100d748f7efSMark Johnston 				goto restart;
1101d748f7efSMark Johnston 		}
1102d748f7efSMark Johnston 	}
1103d748f7efSMark Johnston 
1104d748f7efSMark Johnston 	if (dr != NULL) {
1105d748f7efSMark Johnston 		/*
1106a4a7c825SMark Johnston 		 * The preferred router may have changed, so relocate this
1107a4a7c825SMark Johnston 		 * router.
1108743eee66SSUZUKI Shinsuke 		 */
1109f592d0c3SBjoern A. Zeeb 		TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
1110743eee66SSUZUKI Shinsuke 		n = dr;
1111ff63037dSMark Johnston 	} else {
1112fc315641SMark Johnston 		n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
11134de485feSMark Johnston 		if (n == NULL) {
11144de485feSMark Johnston 			ND6_WUNLOCK();
111582cd038dSYoshinobu Inoue 			return (NULL);
11164de485feSMark Johnston 		}
1117fc315641SMark Johnston 		memcpy(n, new, sizeof(*n));
11184de485feSMark Johnston 		/* Initialize with an extra reference for the caller. */
11194de485feSMark Johnston 		refcount_init(&n->refcnt, 2);
1120ff63037dSMark Johnston 	}
1121686cdd19SJun-ichiro itojun Hagino 
1122686cdd19SJun-ichiro itojun Hagino 	/*
1123743eee66SSUZUKI Shinsuke 	 * Insert the new router in the Default Router List;
1124743eee66SSUZUKI Shinsuke 	 * The Default Router List should be in the descending order
1125743eee66SSUZUKI Shinsuke 	 * of router-preferece.  Routers with the same preference are
1126743eee66SSUZUKI Shinsuke 	 * sorted in the arriving time order.
1127686cdd19SJun-ichiro itojun Hagino 	 */
1128743eee66SSUZUKI Shinsuke 
1129743eee66SSUZUKI Shinsuke 	/* insert at the end of the group */
1130f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1131743eee66SSUZUKI Shinsuke 		if (rtpref(n) > rtpref(dr))
1132743eee66SSUZUKI Shinsuke 			break;
1133743eee66SSUZUKI Shinsuke 	}
11344de485feSMark Johnston 	if (dr != NULL)
1135743eee66SSUZUKI Shinsuke 		TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1136743eee66SSUZUKI Shinsuke 	else
1137f592d0c3SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(&V_nd6_defrouter, n, dr_entry);
1138d748f7efSMark Johnston 	V_nd6_list_genid++;
11394de485feSMark Johnston 	ND6_WUNLOCK();
1140743eee66SSUZUKI Shinsuke 
1141559b4296SAlan Somers 	defrouter_select_fib(new->ifp->if_fib);
1142743eee66SSUZUKI Shinsuke 
114382cd038dSYoshinobu Inoue 	return (n);
114482cd038dSYoshinobu Inoue }
114582cd038dSYoshinobu Inoue 
1146f77a6dbdSBjoern A. Zeeb static int
in6_init_prefix_ltimes(struct nd_prefix * ndpr)1147f77a6dbdSBjoern A. Zeeb in6_init_prefix_ltimes(struct nd_prefix *ndpr)
114882cd038dSYoshinobu Inoue {
1149f77a6dbdSBjoern A. Zeeb 	if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1150f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_preferred = 0;
1151f77a6dbdSBjoern A. Zeeb 	else
1152f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
1153f77a6dbdSBjoern A. Zeeb 	if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1154f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_expire = 0;
1155f77a6dbdSBjoern A. Zeeb 	else
1156f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
115782cd038dSYoshinobu Inoue 
1158f77a6dbdSBjoern A. Zeeb 	return 0;
115982cd038dSYoshinobu Inoue }
116082cd038dSYoshinobu Inoue 
116182cd038dSYoshinobu Inoue static void
in6_init_address_ltimes(struct nd_prefix * new,struct in6_addrlifetime * lt6)1162f77a6dbdSBjoern A. Zeeb in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
116382cd038dSYoshinobu Inoue {
1164f77a6dbdSBjoern A. Zeeb 	/* init ia6t_expire */
1165f77a6dbdSBjoern A. Zeeb 	if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1166f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire = 0;
1167f77a6dbdSBjoern A. Zeeb 	else {
1168f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire = time_uptime;
1169f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire += lt6->ia6t_vltime;
117082cd038dSYoshinobu Inoue 	}
117182cd038dSYoshinobu Inoue 
1172f77a6dbdSBjoern A. Zeeb 	/* init ia6t_preferred */
1173f77a6dbdSBjoern A. Zeeb 	if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1174f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred = 0;
1175f77a6dbdSBjoern A. Zeeb 	else {
1176f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred = time_uptime;
1177f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred += lt6->ia6t_pltime;
1178f77a6dbdSBjoern A. Zeeb 	}
1179f77a6dbdSBjoern A. Zeeb }
1180f77a6dbdSBjoern A. Zeeb 
1181f77a6dbdSBjoern A. Zeeb static struct in6_ifaddr *
in6_ifadd(struct nd_prefixctl * pr,int mcast)1182f77a6dbdSBjoern A. Zeeb in6_ifadd(struct nd_prefixctl *pr, int mcast)
118382cd038dSYoshinobu Inoue {
1184f77a6dbdSBjoern A. Zeeb 	struct ifnet *ifp = pr->ndpr_ifp;
1185f77a6dbdSBjoern A. Zeeb 	struct ifaddr *ifa;
1186f77a6dbdSBjoern A. Zeeb 	struct in6_aliasreq ifra;
1187f77a6dbdSBjoern A. Zeeb 	struct in6_ifaddr *ia, *ib;
1188f77a6dbdSBjoern A. Zeeb 	int error, plen0;
1189f77a6dbdSBjoern A. Zeeb 	struct in6_addr mask;
1190f77a6dbdSBjoern A. Zeeb 	int prefixlen = pr->ndpr_plen;
1191f77a6dbdSBjoern A. Zeeb 	int updateflags;
1192f77a6dbdSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
11934de485feSMark Johnston 
1194f77a6dbdSBjoern A. Zeeb 	in6_prefixlen2mask(&mask, prefixlen);
1195d748f7efSMark Johnston 
1196f77a6dbdSBjoern A. Zeeb 	/*
1197f77a6dbdSBjoern A. Zeeb 	 * find a link-local address (will be interface ID).
1198f77a6dbdSBjoern A. Zeeb 	 * Is it really mandatory? Theoretically, a global or a site-local
1199f77a6dbdSBjoern A. Zeeb 	 * address can be configured without a link-local address, if we
1200f77a6dbdSBjoern A. Zeeb 	 * have a unique interface identifier...
1201f77a6dbdSBjoern A. Zeeb 	 *
1202f77a6dbdSBjoern A. Zeeb 	 * it is not mandatory to have a link-local address, we can generate
1203f77a6dbdSBjoern A. Zeeb 	 * interface identifier on the fly.  we do this because:
1204f77a6dbdSBjoern A. Zeeb 	 * (1) it should be the easiest way to find interface identifier.
1205f77a6dbdSBjoern A. Zeeb 	 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1206f77a6dbdSBjoern A. Zeeb 	 * for multiple addresses on a single interface, and possible shortcut
1207f77a6dbdSBjoern A. Zeeb 	 * of DAD.  we omitted DAD for this reason in the past.
1208f77a6dbdSBjoern A. Zeeb 	 * (3) a user can prevent autoconfiguration of global address
1209f77a6dbdSBjoern A. Zeeb 	 * by removing link-local address by hand (this is partly because we
1210f77a6dbdSBjoern A. Zeeb 	 * don't have other way to control the use of IPv6 on an interface.
1211f77a6dbdSBjoern A. Zeeb 	 * this has been our design choice - cf. NRL's "ifconfig auto").
1212f77a6dbdSBjoern A. Zeeb 	 * (4) it is easier to manage when an interface has addresses
1213f77a6dbdSBjoern A. Zeeb 	 * with the same interface identifier, than to have multiple addresses
1214f77a6dbdSBjoern A. Zeeb 	 * with different interface identifiers.
1215f77a6dbdSBjoern A. Zeeb 	 */
1216f77a6dbdSBjoern A. Zeeb 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1217f77a6dbdSBjoern A. Zeeb 	if (ifa)
1218f77a6dbdSBjoern A. Zeeb 		ib = (struct in6_ifaddr *)ifa;
1219f77a6dbdSBjoern A. Zeeb 	else
1220f77a6dbdSBjoern A. Zeeb 		return NULL;
1221f77a6dbdSBjoern A. Zeeb 
1222f77a6dbdSBjoern A. Zeeb 	/* prefixlen + ifidlen must be equal to 128 */
1223f77a6dbdSBjoern A. Zeeb 	plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1224f77a6dbdSBjoern A. Zeeb 	if (prefixlen != plen0) {
1225f77a6dbdSBjoern A. Zeeb 		ifa_free(ifa);
1226c6feea3bSBjoern A. Zeeb 		nd6log((LOG_INFO,
1227c6feea3bSBjoern A. Zeeb 		    "%s: wrong prefixlen for %s (prefix=%d ifid=%d)\n",
1228c6feea3bSBjoern A. Zeeb 		    __func__, if_name(ifp), prefixlen, 128 - plen0));
1229f77a6dbdSBjoern A. Zeeb 		return NULL;
1230f77a6dbdSBjoern A. Zeeb 	}
1231f77a6dbdSBjoern A. Zeeb 
1232f77a6dbdSBjoern A. Zeeb 	/* make ifaddr */
1233f77a6dbdSBjoern A. Zeeb 	in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
1234f77a6dbdSBjoern A. Zeeb 
1235f77a6dbdSBjoern A. Zeeb 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
1236f77a6dbdSBjoern A. Zeeb 	/* interface ID */
1237f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1238f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1239f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1240f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1241f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1242f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1243f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1244f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1245f77a6dbdSBjoern A. Zeeb 	ifa_free(ifa);
1246f77a6dbdSBjoern A. Zeeb 
1247f77a6dbdSBjoern A. Zeeb 	/* lifetimes. */
1248f77a6dbdSBjoern A. Zeeb 	ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1249f77a6dbdSBjoern A. Zeeb 	ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1250f77a6dbdSBjoern A. Zeeb 
1251f77a6dbdSBjoern A. Zeeb 	/* XXX: scope zone ID? */
1252f77a6dbdSBjoern A. Zeeb 
1253f77a6dbdSBjoern A. Zeeb 	ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1254f77a6dbdSBjoern A. Zeeb 
1255f77a6dbdSBjoern A. Zeeb 	/*
1256f77a6dbdSBjoern A. Zeeb 	 * Make sure that we do not have this address already.  This should
1257f77a6dbdSBjoern A. Zeeb 	 * usually not happen, but we can still see this case, e.g., if we
1258f77a6dbdSBjoern A. Zeeb 	 * have manually configured the exact address to be configured.
1259f77a6dbdSBjoern A. Zeeb 	 */
1260f77a6dbdSBjoern A. Zeeb 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1261f77a6dbdSBjoern A. Zeeb 	    &ifra.ifra_addr.sin6_addr);
1262f77a6dbdSBjoern A. Zeeb 	if (ifa != NULL) {
1263f77a6dbdSBjoern A. Zeeb 		ifa_free(ifa);
1264f77a6dbdSBjoern A. Zeeb 		/* this should be rare enough to make an explicit log */
1265f77a6dbdSBjoern A. Zeeb 		log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1266f77a6dbdSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1267f77a6dbdSBjoern A. Zeeb 		return (NULL);
1268f77a6dbdSBjoern A. Zeeb 	}
1269f77a6dbdSBjoern A. Zeeb 
1270f77a6dbdSBjoern A. Zeeb 	/*
1271f77a6dbdSBjoern A. Zeeb 	 * Allocate ifaddr structure, link into chain, etc.
1272f77a6dbdSBjoern A. Zeeb 	 * If we are going to create a new address upon receiving a multicasted
1273f77a6dbdSBjoern A. Zeeb 	 * RA, we need to impose a random delay before starting DAD.
1274f77a6dbdSBjoern A. Zeeb 	 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1275f77a6dbdSBjoern A. Zeeb 	 */
1276f77a6dbdSBjoern A. Zeeb 	updateflags = 0;
1277f77a6dbdSBjoern A. Zeeb 	if (mcast)
1278f77a6dbdSBjoern A. Zeeb 		updateflags |= IN6_IFAUPDATE_DADDELAY;
1279f77a6dbdSBjoern A. Zeeb 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1280f77a6dbdSBjoern A. Zeeb 		nd6log((LOG_ERR,
1281c6feea3bSBjoern A. Zeeb 		    "%s: failed to make ifaddr %s on %s (errno=%d)\n", __func__,
1282f77a6dbdSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1283f77a6dbdSBjoern A. Zeeb 		    if_name(ifp), error));
1284f77a6dbdSBjoern A. Zeeb 		return (NULL);	/* ifaddr must not have been allocated. */
1285f77a6dbdSBjoern A. Zeeb 	}
1286f77a6dbdSBjoern A. Zeeb 
1287f77a6dbdSBjoern A. Zeeb 	ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1288f77a6dbdSBjoern A. Zeeb 	/*
1289f77a6dbdSBjoern A. Zeeb 	 * XXXRW: Assumption of non-NULLness here might not be true with
1290f77a6dbdSBjoern A. Zeeb 	 * fine-grained locking -- should we validate it?  Or just return
1291f77a6dbdSBjoern A. Zeeb 	 * earlier ifa rather than looking it up again?
1292f77a6dbdSBjoern A. Zeeb 	 */
1293f77a6dbdSBjoern A. Zeeb 	return (ia);		/* this is always non-NULL  and referenced. */
129482cd038dSYoshinobu Inoue }
129582cd038dSYoshinobu Inoue 
1296d748f7efSMark Johnston static struct nd_prefix *
nd6_prefix_lookup_locked(struct nd_prefixctl * key)1297d748f7efSMark Johnston nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1298d748f7efSMark Johnston {
1299d748f7efSMark Johnston 	struct nd_prefix *search;
1300d748f7efSMark Johnston 
1301d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1302d748f7efSMark Johnston 
1303d748f7efSMark Johnston 	LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1304d748f7efSMark Johnston 		if (key->ndpr_ifp == search->ndpr_ifp &&
1305d748f7efSMark Johnston 		    key->ndpr_plen == search->ndpr_plen &&
1306d748f7efSMark Johnston 		    in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1307d748f7efSMark Johnston 		    &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1308d748f7efSMark Johnston 			nd6_prefix_ref(search);
1309d748f7efSMark Johnston 			break;
1310d748f7efSMark Johnston 		}
1311d748f7efSMark Johnston 	}
1312d748f7efSMark Johnston 	return (search);
1313d748f7efSMark Johnston }
1314d748f7efSMark Johnston 
131533841545SHajimu UMEMOTO struct nd_prefix *
nd6_prefix_lookup(struct nd_prefixctl * key)13161272577eSXin LI nd6_prefix_lookup(struct nd_prefixctl *key)
131782cd038dSYoshinobu Inoue {
131882cd038dSYoshinobu Inoue 	struct nd_prefix *search;
131982cd038dSYoshinobu Inoue 
1320d748f7efSMark Johnston 	ND6_RLOCK();
1321d748f7efSMark Johnston 	search = nd6_prefix_lookup_locked(key);
1322d748f7efSMark Johnston 	ND6_RUNLOCK();
1323d748f7efSMark Johnston 	return (search);
132482cd038dSYoshinobu Inoue }
132582cd038dSYoshinobu Inoue 
1326d748f7efSMark Johnston void
nd6_prefix_ref(struct nd_prefix * pr)1327d748f7efSMark Johnston nd6_prefix_ref(struct nd_prefix *pr)
1328d748f7efSMark Johnston {
1329d748f7efSMark Johnston 
1330d748f7efSMark Johnston 	refcount_acquire(&pr->ndpr_refcnt);
1331d748f7efSMark Johnston }
1332d748f7efSMark Johnston 
1333d748f7efSMark Johnston void
nd6_prefix_rele(struct nd_prefix * pr)1334d748f7efSMark Johnston nd6_prefix_rele(struct nd_prefix *pr)
1335d748f7efSMark Johnston {
1336d748f7efSMark Johnston 
1337d748f7efSMark Johnston 	if (refcount_release(&pr->ndpr_refcnt)) {
1338d748f7efSMark Johnston 		KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1339d748f7efSMark Johnston 		    ("prefix %p has advertising routers", pr));
1340d748f7efSMark Johnston 		free(pr, M_IP6NDP);
1341d748f7efSMark Johnston 	}
134282cd038dSYoshinobu Inoue }
134382cd038dSYoshinobu Inoue 
134433841545SHajimu UMEMOTO int
nd6_prelist_add(struct nd_prefixctl * pr,struct nd_defrouter * dr,struct nd_prefix ** newp)13451272577eSXin LI nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
13461272577eSXin LI     struct nd_prefix **newp)
134782cd038dSYoshinobu Inoue {
1348d748f7efSMark Johnston 	struct nd_prefix *new;
13491d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1350d748f7efSMark Johnston 	int error;
135182cd038dSYoshinobu Inoue 
1352fc315641SMark Johnston 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
135382cd038dSYoshinobu Inoue 	if (new == NULL)
135407eb2995SHajimu UMEMOTO 		return (ENOMEM);
1355d748f7efSMark Johnston 	refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1356743eee66SSUZUKI Shinsuke 	new->ndpr_ifp = pr->ndpr_ifp;
1357743eee66SSUZUKI Shinsuke 	new->ndpr_prefix = pr->ndpr_prefix;
1358743eee66SSUZUKI Shinsuke 	new->ndpr_plen = pr->ndpr_plen;
1359743eee66SSUZUKI Shinsuke 	new->ndpr_vltime = pr->ndpr_vltime;
1360743eee66SSUZUKI Shinsuke 	new->ndpr_pltime = pr->ndpr_pltime;
1361743eee66SSUZUKI Shinsuke 	new->ndpr_flags = pr->ndpr_flags;
1362743eee66SSUZUKI Shinsuke 	if ((error = in6_init_prefix_ltimes(new)) != 0) {
1363743eee66SSUZUKI Shinsuke 		free(new, M_IP6NDP);
1364743eee66SSUZUKI Shinsuke 		return (error);
1365743eee66SSUZUKI Shinsuke 	}
13667d26db17SHiroki Sato 	new->ndpr_lastupdate = time_uptime;
136782cd038dSYoshinobu Inoue 
136807eb2995SHajimu UMEMOTO 	/* initialization */
136982cd038dSYoshinobu Inoue 	LIST_INIT(&new->ndpr_advrtrs);
137082cd038dSYoshinobu Inoue 	in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
137182cd038dSYoshinobu Inoue 	/* make prefix in the canonical form */
13721dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
137382cd038dSYoshinobu Inoue 
1374d748f7efSMark Johnston 	ND6_WLOCK();
1375603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1376d748f7efSMark Johnston 	V_nd6_list_genid++;
1377d748f7efSMark Johnston 	ND6_WUNLOCK();
137882cd038dSYoshinobu Inoue 
137933841545SHajimu UMEMOTO 	/* ND_OPT_PI_FLAG_ONLINK processing */
138033841545SHajimu UMEMOTO 	if (new->ndpr_raf_onlink) {
1381b8a6e03fSGleb Smirnoff 		struct epoch_tracker et;
1382b8a6e03fSGleb Smirnoff 
1383d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1384b8a6e03fSGleb Smirnoff 		NET_EPOCH_ENTER(et);
1385d748f7efSMark Johnston 		if ((error = nd6_prefix_onlink(new)) != 0) {
1386c6feea3bSBjoern A. Zeeb 			nd6log((LOG_ERR, "%s: failed to make the prefix %s/%d "
1387c6feea3bSBjoern A. Zeeb 			    "on-link on %s (errno=%d)\n", __func__,
13881d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1389d748f7efSMark Johnston 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
139033841545SHajimu UMEMOTO 			/* proceed anyway. XXX: is it correct? */
139133841545SHajimu UMEMOTO 		}
1392b8a6e03fSGleb Smirnoff 		NET_EPOCH_EXIT(et);
1393d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
139433841545SHajimu UMEMOTO 	}
139533841545SHajimu UMEMOTO 
139620b5f022SMark Johnston 	if (dr != NULL)
139782cd038dSYoshinobu Inoue 		pfxrtr_add(new, dr);
139820b5f022SMark Johnston 	if (newp != NULL)
139920b5f022SMark Johnston 		*newp = new;
140020b5f022SMark Johnston 	return (0);
140182cd038dSYoshinobu Inoue }
140282cd038dSYoshinobu Inoue 
1403d748f7efSMark Johnston /*
1404d748f7efSMark Johnston  * Remove a prefix from the prefix list and optionally stash it in a
1405d748f7efSMark Johnston  * caller-provided list.
1406d748f7efSMark Johnston  *
1407d748f7efSMark Johnston  * The ND6 lock must be held.
1408d748f7efSMark Johnston  */
140982cd038dSYoshinobu Inoue void
nd6_prefix_unlink(struct nd_prefix * pr,struct nd_prhead * list)1410d748f7efSMark Johnston nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1411d748f7efSMark Johnston {
1412d748f7efSMark Johnston 
1413d748f7efSMark Johnston 	ND6_WLOCK_ASSERT();
1414d748f7efSMark Johnston 
1415d748f7efSMark Johnston 	LIST_REMOVE(pr, ndpr_entry);
1416d748f7efSMark Johnston 	V_nd6_list_genid++;
1417d748f7efSMark Johnston 	if (list != NULL)
1418d748f7efSMark Johnston 		LIST_INSERT_HEAD(list, pr, ndpr_entry);
1419d748f7efSMark Johnston }
1420d748f7efSMark Johnston 
1421d748f7efSMark Johnston /*
1422d748f7efSMark Johnston  * Free an unlinked prefix, first marking it off-link if necessary.
1423d748f7efSMark Johnston  */
1424d748f7efSMark Johnston void
nd6_prefix_del(struct nd_prefix * pr)1425d748f7efSMark Johnston nd6_prefix_del(struct nd_prefix *pr)
142682cd038dSYoshinobu Inoue {
142782cd038dSYoshinobu Inoue 	struct nd_pfxrouter *pfr, *next;
1428c9b652e3SAndre Oppermann 	int e;
14291d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
143033841545SHajimu UMEMOTO 
1431d748f7efSMark Johnston 	KASSERT(pr->ndpr_addrcnt == 0,
1432d748f7efSMark Johnston 	    ("prefix %p has referencing addresses", pr));
1433d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
1434743eee66SSUZUKI Shinsuke 
143533841545SHajimu UMEMOTO 	/*
143633841545SHajimu UMEMOTO 	 * Though these flags are now meaningless, we'd rather keep the value
14377aa59493SSUZUKI Shinsuke 	 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
14387aa59493SSUZUKI Shinsuke 	 * when executing "ndp -p".
143933841545SHajimu UMEMOTO 	 */
1440d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1441d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1442d748f7efSMark Johnston 		if ((e = nd6_prefix_offlink(pr)) != 0) {
1443d748f7efSMark Johnston 			nd6log((LOG_ERR,
1444c6feea3bSBjoern A. Zeeb 			    "%s: failed to make the prefix %s/%d offlink on %s "
1445c6feea3bSBjoern A. Zeeb 			    "(errno=%d)\n", __func__,
14461d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
144733841545SHajimu UMEMOTO 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
144833841545SHajimu UMEMOTO 			/* what should we do? */
144933841545SHajimu UMEMOTO 		}
1450d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
145182cd038dSYoshinobu Inoue 	}
1452d748f7efSMark Johnston 
1453d748f7efSMark Johnston 	/* Release references to routers that have advertised this prefix. */
1454d748f7efSMark Johnston 	ND6_WLOCK();
1455d748f7efSMark Johnston 	LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1456d748f7efSMark Johnston 		pfxrtr_del(pfr);
1457d748f7efSMark Johnston 	ND6_WUNLOCK();
1458d748f7efSMark Johnston 
1459d748f7efSMark Johnston 	nd6_prefix_rele(pr);
146082cd038dSYoshinobu Inoue 
146182cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
146282cd038dSYoshinobu Inoue }
146382cd038dSYoshinobu Inoue 
1464743eee66SSUZUKI Shinsuke static int
prelist_update(struct nd_prefixctl * new,struct nd_defrouter * dr,struct mbuf * m,int mcast)14651272577eSXin LI prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
14661272577eSXin LI     struct mbuf *m, int mcast)
146782cd038dSYoshinobu Inoue {
146833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
146933841545SHajimu UMEMOTO 	struct ifaddr *ifa;
147033841545SHajimu UMEMOTO 	struct ifnet *ifp = new->ndpr_ifp;
147182cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
147282cd038dSYoshinobu Inoue 	int error = 0;
147382cd038dSYoshinobu Inoue 	int auth;
147433841545SHajimu UMEMOTO 	struct in6_addrlifetime lt6_tmp;
14751d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1476b8a6e03fSGleb Smirnoff 
1477b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
147882cd038dSYoshinobu Inoue 
147982cd038dSYoshinobu Inoue 	auth = 0;
148082cd038dSYoshinobu Inoue 	if (m) {
148182cd038dSYoshinobu Inoue 		/*
148282cd038dSYoshinobu Inoue 		 * Authenticity for NA consists authentication for
148382cd038dSYoshinobu Inoue 		 * both IP header and IP datagrams, doesn't it ?
148482cd038dSYoshinobu Inoue 		 */
148582cd038dSYoshinobu Inoue #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
148607eb2995SHajimu UMEMOTO 		auth = ((m->m_flags & M_AUTHIPHDR) &&
148707eb2995SHajimu UMEMOTO 		    (m->m_flags & M_AUTHIPDGM));
148882cd038dSYoshinobu Inoue #endif
148982cd038dSYoshinobu Inoue 	}
149082cd038dSYoshinobu Inoue 
149133841545SHajimu UMEMOTO 	if ((pr = nd6_prefix_lookup(new)) != NULL) {
149233841545SHajimu UMEMOTO 		/*
149333841545SHajimu UMEMOTO 		 * nd6_prefix_lookup() ensures that pr and new have the same
149433841545SHajimu UMEMOTO 		 * prefix on a same interface.
149533841545SHajimu UMEMOTO 		 */
149633841545SHajimu UMEMOTO 
149733841545SHajimu UMEMOTO 		/*
149833841545SHajimu UMEMOTO 		 * Update prefix information.  Note that the on-link (L) bit
149933841545SHajimu UMEMOTO 		 * and the autonomous (A) bit should NOT be changed from 1
150033841545SHajimu UMEMOTO 		 * to 0.
150133841545SHajimu UMEMOTO 		 */
150233841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 1)
150333841545SHajimu UMEMOTO 			pr->ndpr_raf_onlink = 1;
150433841545SHajimu UMEMOTO 		if (new->ndpr_raf_auto == 1)
150533841545SHajimu UMEMOTO 			pr->ndpr_raf_auto = 1;
150633841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink) {
150782cd038dSYoshinobu Inoue 			pr->ndpr_vltime = new->ndpr_vltime;
150882cd038dSYoshinobu Inoue 			pr->ndpr_pltime = new->ndpr_pltime;
1509743eee66SSUZUKI Shinsuke 			(void)in6_init_prefix_ltimes(pr); /* XXX error case? */
15107d26db17SHiroki Sato 			pr->ndpr_lastupdate = time_uptime;
151182cd038dSYoshinobu Inoue 		}
151282cd038dSYoshinobu Inoue 
151333841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink &&
151433841545SHajimu UMEMOTO 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1515d748f7efSMark Johnston 			ND6_ONLINK_LOCK();
1516d748f7efSMark Johnston 			if ((error = nd6_prefix_onlink(pr)) != 0) {
151733841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
1518c6feea3bSBjoern A. Zeeb 				    "%s: failed to make the prefix %s/%d "
1519c6feea3bSBjoern A. Zeeb 				    "on-link on %s (errno=%d)\n", __func__,
15201d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
15211d54aa3bSBjoern A. Zeeb 				        &pr->ndpr_prefix.sin6_addr),
1522d748f7efSMark Johnston 				    pr->ndpr_plen, if_name(pr->ndpr_ifp),
1523d748f7efSMark Johnston 				    error));
152433841545SHajimu UMEMOTO 				/* proceed anyway. XXX: is it correct? */
1525686cdd19SJun-ichiro itojun Hagino 			}
1526d748f7efSMark Johnston 			ND6_ONLINK_UNLOCK();
152782cd038dSYoshinobu Inoue 		}
152882cd038dSYoshinobu Inoue 
1529d748f7efSMark Johnston 		if (dr != NULL)
153082cd038dSYoshinobu Inoue 			pfxrtr_add(pr, dr);
153182cd038dSYoshinobu Inoue 	} else {
153233841545SHajimu UMEMOTO 		if (new->ndpr_vltime == 0)
153333841545SHajimu UMEMOTO 			goto end;
153433841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
153533841545SHajimu UMEMOTO 			goto end;
153682cd038dSYoshinobu Inoue 
153783631b16SMark Johnston 		error = nd6_prelist_add(new, dr, &pr);
153820b5f022SMark Johnston 		if (error != 0) {
1539c6feea3bSBjoern A. Zeeb 			nd6log((LOG_NOTICE, "%s: nd6_prelist_add() failed for "
1540c6feea3bSBjoern A. Zeeb 			    "the prefix %s/%d on %s (errno=%d)\n", __func__,
15411d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
154220b5f022SMark Johnston 			    new->ndpr_plen, if_name(new->ndpr_ifp), error));
154333841545SHajimu UMEMOTO 			goto end; /* we should just give up in this case. */
154433841545SHajimu UMEMOTO 		}
154533841545SHajimu UMEMOTO 
154682cd038dSYoshinobu Inoue 		/*
154733841545SHajimu UMEMOTO 		 * XXX: from the ND point of view, we can ignore a prefix
154833841545SHajimu UMEMOTO 		 * with the on-link bit being zero.  However, we need a
154933841545SHajimu UMEMOTO 		 * prefix structure for references from autoconfigured
155007eb2995SHajimu UMEMOTO 		 * addresses.  Thus, we explicitly make sure that the prefix
155133841545SHajimu UMEMOTO 		 * itself expires now.
155282cd038dSYoshinobu Inoue 		 */
155383631b16SMark Johnston 		if (pr->ndpr_raf_onlink == 0) {
155483631b16SMark Johnston 			pr->ndpr_vltime = 0;
155583631b16SMark Johnston 			pr->ndpr_pltime = 0;
155683631b16SMark Johnston 			in6_init_prefix_ltimes(pr);
155733841545SHajimu UMEMOTO 		}
155833841545SHajimu UMEMOTO 	}
155933841545SHajimu UMEMOTO 
156033841545SHajimu UMEMOTO 	/*
156133841545SHajimu UMEMOTO 	 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
156233841545SHajimu UMEMOTO 	 * Note that pr must be non NULL at this point.
156333841545SHajimu UMEMOTO 	 */
156433841545SHajimu UMEMOTO 
156533841545SHajimu UMEMOTO 	/* 5.5.3 (a). Ignore the prefix without the A bit set. */
156682cd038dSYoshinobu Inoue 	if (!new->ndpr_raf_auto)
1567743eee66SSUZUKI Shinsuke 		goto end;
156882cd038dSYoshinobu Inoue 
156933841545SHajimu UMEMOTO 	/*
157033841545SHajimu UMEMOTO 	 * 5.5.3 (b). the link-local prefix should have been ignored in
157133841545SHajimu UMEMOTO 	 * nd6_ra_input.
157233841545SHajimu UMEMOTO 	 */
157333841545SHajimu UMEMOTO 
1574743eee66SSUZUKI Shinsuke 	/* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1575743eee66SSUZUKI Shinsuke 	if (new->ndpr_pltime > new->ndpr_vltime) {
1576743eee66SSUZUKI Shinsuke 		error = EINVAL;	/* XXX: won't be used */
1577743eee66SSUZUKI Shinsuke 		goto end;
1578743eee66SSUZUKI Shinsuke 	}
157933841545SHajimu UMEMOTO 
158033841545SHajimu UMEMOTO 	/*
1581743eee66SSUZUKI Shinsuke 	 * 5.5.3 (d).  If the prefix advertised is not equal to the prefix of
1582743eee66SSUZUKI Shinsuke 	 * an address configured by stateless autoconfiguration already in the
1583743eee66SSUZUKI Shinsuke 	 * list of addresses associated with the interface, and the Valid
1584743eee66SSUZUKI Shinsuke 	 * Lifetime is not 0, form an address.  We first check if we have
1585743eee66SSUZUKI Shinsuke 	 * a matching prefix.
1586743eee66SSUZUKI Shinsuke 	 * Note: we apply a clarification in rfc2462bis-02 here.  We only
1587743eee66SSUZUKI Shinsuke 	 * consider autoconfigured addresses while RFC2462 simply said
1588743eee66SSUZUKI Shinsuke 	 * "address".
158933841545SHajimu UMEMOTO 	 */
1590d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
159133841545SHajimu UMEMOTO 		struct in6_ifaddr *ifa6;
1592743eee66SSUZUKI Shinsuke 		u_int32_t remaininglifetime;
159333841545SHajimu UMEMOTO 
159433841545SHajimu UMEMOTO 		if (ifa->ifa_addr->sa_family != AF_INET6)
159533841545SHajimu UMEMOTO 			continue;
159633841545SHajimu UMEMOTO 
159733841545SHajimu UMEMOTO 		ifa6 = (struct in6_ifaddr *)ifa;
159833841545SHajimu UMEMOTO 
159933841545SHajimu UMEMOTO 		/*
1600743eee66SSUZUKI Shinsuke 		 * We only consider autoconfigured addresses as per rfc2462bis.
1601743eee66SSUZUKI Shinsuke 		 */
1602743eee66SSUZUKI Shinsuke 		if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1603743eee66SSUZUKI Shinsuke 			continue;
1604743eee66SSUZUKI Shinsuke 
1605743eee66SSUZUKI Shinsuke 		/*
160633841545SHajimu UMEMOTO 		 * Spec is not clear here, but I believe we should concentrate
160733841545SHajimu UMEMOTO 		 * on unicast (i.e. not anycast) addresses.
160833841545SHajimu UMEMOTO 		 * XXX: other ia6_flags? detached or duplicated?
160933841545SHajimu UMEMOTO 		 */
161033841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
161133841545SHajimu UMEMOTO 			continue;
161233841545SHajimu UMEMOTO 
1613743eee66SSUZUKI Shinsuke 		/*
1614743eee66SSUZUKI Shinsuke 		 * Ignore the address if it is not associated with a prefix
1615743eee66SSUZUKI Shinsuke 		 * or is associated with a prefix that is different from this
1616743eee66SSUZUKI Shinsuke 		 * one.  (pr is never NULL here)
1617743eee66SSUZUKI Shinsuke 		 */
1618743eee66SSUZUKI Shinsuke 		if (ifa6->ia6_ndpr != pr)
161933841545SHajimu UMEMOTO 			continue;
162033841545SHajimu UMEMOTO 
162133841545SHajimu UMEMOTO 		if (ia6_match == NULL) /* remember the first one */
162233841545SHajimu UMEMOTO 			ia6_match = ifa6;
162333841545SHajimu UMEMOTO 
162433841545SHajimu UMEMOTO 		/*
162533841545SHajimu UMEMOTO 		 * An already autoconfigured address matched.  Now that we
162633841545SHajimu UMEMOTO 		 * are sure there is at least one matched address, we can
162733841545SHajimu UMEMOTO 		 * proceed to 5.5.3. (e): update the lifetimes according to the
162833841545SHajimu UMEMOTO 		 * "two hours" rule and the privacy extension.
1629743eee66SSUZUKI Shinsuke 		 * We apply some clarifications in rfc2462bis:
1630743eee66SSUZUKI Shinsuke 		 * - use remaininglifetime instead of storedlifetime as a
1631743eee66SSUZUKI Shinsuke 		 *   variable name
1632743eee66SSUZUKI Shinsuke 		 * - remove the dead code in the "two-hour" rule
163333841545SHajimu UMEMOTO 		 */
163433841545SHajimu UMEMOTO #define TWOHOUR		(120*60)
163533841545SHajimu UMEMOTO 		lt6_tmp = ifa6->ia6_lifetime;
163633841545SHajimu UMEMOTO 
163711f3a6e2SHajimu UMEMOTO 		if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1638743eee66SSUZUKI Shinsuke 			remaininglifetime = ND6_INFINITE_LIFETIME;
16397d26db17SHiroki Sato 		else if (time_uptime - ifa6->ia6_updatetime >
1640743eee66SSUZUKI Shinsuke 			 lt6_tmp.ia6t_vltime) {
1641743eee66SSUZUKI Shinsuke 			/*
1642743eee66SSUZUKI Shinsuke 			 * The case of "invalid" address.  We should usually
1643743eee66SSUZUKI Shinsuke 			 * not see this case.
1644743eee66SSUZUKI Shinsuke 			 */
1645743eee66SSUZUKI Shinsuke 			remaininglifetime = 0;
1646743eee66SSUZUKI Shinsuke 		} else
1647743eee66SSUZUKI Shinsuke 			remaininglifetime = lt6_tmp.ia6t_vltime -
16487d26db17SHiroki Sato 			    (time_uptime - ifa6->ia6_updatetime);
164911f3a6e2SHajimu UMEMOTO 
165011f3a6e2SHajimu UMEMOTO 		/* when not updating, keep the current stored lifetime. */
1651743eee66SSUZUKI Shinsuke 		lt6_tmp.ia6t_vltime = remaininglifetime;
165233841545SHajimu UMEMOTO 
165333841545SHajimu UMEMOTO 		if (TWOHOUR < new->ndpr_vltime ||
1654743eee66SSUZUKI Shinsuke 		    remaininglifetime < new->ndpr_vltime) {
165533841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1656743eee66SSUZUKI Shinsuke 		} else if (remaininglifetime <= TWOHOUR) {
165733841545SHajimu UMEMOTO 			if (auth) {
165833841545SHajimu UMEMOTO 				lt6_tmp.ia6t_vltime = new->ndpr_vltime;
165982cd038dSYoshinobu Inoue 			}
166033841545SHajimu UMEMOTO 		} else {
166133841545SHajimu UMEMOTO 			/*
166233841545SHajimu UMEMOTO 			 * new->ndpr_vltime <= TWOHOUR &&
1663743eee66SSUZUKI Shinsuke 			 * TWOHOUR < remaininglifetime
166433841545SHajimu UMEMOTO 			 */
166533841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = TWOHOUR;
166682cd038dSYoshinobu Inoue 		}
166782cd038dSYoshinobu Inoue 
166833841545SHajimu UMEMOTO 		/* The 2 hour rule is not imposed for preferred lifetime. */
166933841545SHajimu UMEMOTO 		lt6_tmp.ia6t_pltime = new->ndpr_pltime;
167033841545SHajimu UMEMOTO 
167133841545SHajimu UMEMOTO 		in6_init_address_ltimes(pr, &lt6_tmp);
167233841545SHajimu UMEMOTO 
167333841545SHajimu UMEMOTO 		/*
1674743eee66SSUZUKI Shinsuke 		 * We need to treat lifetimes for temporary addresses
1675743eee66SSUZUKI Shinsuke 		 * differently, according to
1676743eee66SSUZUKI Shinsuke 		 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1677743eee66SSUZUKI Shinsuke 		 * we only update the lifetimes when they are in the maximum
1678743eee66SSUZUKI Shinsuke 		 * intervals.
167933841545SHajimu UMEMOTO 		 */
168033841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1681743eee66SSUZUKI Shinsuke 			u_int32_t maxvltime, maxpltime;
168233841545SHajimu UMEMOTO 
1683603724d3SBjoern A. Zeeb 			if (V_ip6_temp_valid_lifetime >
16847d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1685603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1686603724d3SBjoern A. Zeeb 				maxvltime = V_ip6_temp_valid_lifetime -
16877d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1688603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1689743eee66SSUZUKI Shinsuke 			} else
1690743eee66SSUZUKI Shinsuke 				maxvltime = 0;
1691603724d3SBjoern A. Zeeb 			if (V_ip6_temp_preferred_lifetime >
16927d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1693603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1694603724d3SBjoern A. Zeeb 				maxpltime = V_ip6_temp_preferred_lifetime -
16957d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1696603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1697743eee66SSUZUKI Shinsuke 			} else
1698743eee66SSUZUKI Shinsuke 				maxpltime = 0;
1699743eee66SSUZUKI Shinsuke 
1700743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1701743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_vltime > maxvltime) {
1702743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_vltime = maxvltime;
1703743eee66SSUZUKI Shinsuke 			}
1704743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1705743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_pltime > maxpltime) {
1706743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_pltime = maxpltime;
1707743eee66SSUZUKI Shinsuke 			}
1708743eee66SSUZUKI Shinsuke 		}
170933841545SHajimu UMEMOTO 		ifa6->ia6_lifetime = lt6_tmp;
17107d26db17SHiroki Sato 		ifa6->ia6_updatetime = time_uptime;
171133841545SHajimu UMEMOTO 	}
171233841545SHajimu UMEMOTO 	if (ia6_match == NULL && new->ndpr_vltime) {
1713743eee66SSUZUKI Shinsuke 		int ifidlen;
1714743eee66SSUZUKI Shinsuke 
171533841545SHajimu UMEMOTO 		/*
1716743eee66SSUZUKI Shinsuke 		 * 5.5.3 (d) (continued)
171733841545SHajimu UMEMOTO 		 * No address matched and the valid lifetime is non-zero.
171833841545SHajimu UMEMOTO 		 * Create a new address.
171933841545SHajimu UMEMOTO 		 */
1720743eee66SSUZUKI Shinsuke 
1721743eee66SSUZUKI Shinsuke 		/*
1722743eee66SSUZUKI Shinsuke 		 * Prefix Length check:
1723743eee66SSUZUKI Shinsuke 		 * If the sum of the prefix length and interface identifier
1724743eee66SSUZUKI Shinsuke 		 * length does not equal 128 bits, the Prefix Information
1725743eee66SSUZUKI Shinsuke 		 * option MUST be ignored.  The length of the interface
1726743eee66SSUZUKI Shinsuke 		 * identifier is defined in a separate link-type specific
1727743eee66SSUZUKI Shinsuke 		 * document.
1728743eee66SSUZUKI Shinsuke 		 */
1729743eee66SSUZUKI Shinsuke 		ifidlen = in6_if2idlen(ifp);
1730743eee66SSUZUKI Shinsuke 		if (ifidlen < 0) {
1731743eee66SSUZUKI Shinsuke 			/* this should not happen, so we always log it. */
1732743eee66SSUZUKI Shinsuke 			log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1733743eee66SSUZUKI Shinsuke 			    if_name(ifp));
1734743eee66SSUZUKI Shinsuke 			goto end;
1735743eee66SSUZUKI Shinsuke 		}
1736743eee66SSUZUKI Shinsuke 		if (ifidlen + pr->ndpr_plen != 128) {
1737743eee66SSUZUKI Shinsuke 			nd6log((LOG_INFO,
1738c6feea3bSBjoern A. Zeeb 			    "%s: invalid prefixlen %d for %s, ignored\n",
1739c6feea3bSBjoern A. Zeeb 			    __func__, pr->ndpr_plen, if_name(ifp)));
1740743eee66SSUZUKI Shinsuke 			goto end;
1741743eee66SSUZUKI Shinsuke 		}
1742743eee66SSUZUKI Shinsuke 
1743743eee66SSUZUKI Shinsuke 		if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
174433841545SHajimu UMEMOTO 			/*
174533841545SHajimu UMEMOTO 			 * note that we should use pr (not new) for reference.
174633841545SHajimu UMEMOTO 			 */
17470bbf244eSMark Johnston 			pr->ndpr_addrcnt++;
174833841545SHajimu UMEMOTO 			ia6->ia6_ndpr = pr;
174933841545SHajimu UMEMOTO 
175033841545SHajimu UMEMOTO 			/*
175133841545SHajimu UMEMOTO 			 * RFC 3041 3.3 (2).
175233841545SHajimu UMEMOTO 			 * When a new public address is created as described
175333841545SHajimu UMEMOTO 			 * in RFC2462, also create a new temporary address.
175433841545SHajimu UMEMOTO 			 *
175533841545SHajimu UMEMOTO 			 * RFC 3041 3.5.
175633841545SHajimu UMEMOTO 			 * When an interface connects to a new link, a new
175733841545SHajimu UMEMOTO 			 * randomized interface identifier should be generated
175833841545SHajimu UMEMOTO 			 * immediately together with a new set of temporary
175933841545SHajimu UMEMOTO 			 * addresses.  Thus, we specifiy 1 as the 2nd arg of
176033841545SHajimu UMEMOTO 			 * in6_tmpifadd().
176133841545SHajimu UMEMOTO 			 */
1762603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr) {
176333841545SHajimu UMEMOTO 				int e;
1764743eee66SSUZUKI Shinsuke 				if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
1765c6feea3bSBjoern A. Zeeb 					nd6log((LOG_NOTICE, "%s: failed to "
1766c6feea3bSBjoern A. Zeeb 					    "create a temporary address "
1767c6feea3bSBjoern A. Zeeb 					    "(errno=%d)\n", __func__, e));
176833841545SHajimu UMEMOTO 				}
176933841545SHajimu UMEMOTO 			}
17708c0fec80SRobert Watson 			ifa_free(&ia6->ia_ifa);
177133841545SHajimu UMEMOTO 
177233841545SHajimu UMEMOTO 			/*
177333841545SHajimu UMEMOTO 			 * A newly added address might affect the status
177433841545SHajimu UMEMOTO 			 * of other addresses, so we check and update it.
177533841545SHajimu UMEMOTO 			 * XXX: what if address duplication happens?
177633841545SHajimu UMEMOTO 			 */
177733841545SHajimu UMEMOTO 			pfxlist_onlink_check();
177833841545SHajimu UMEMOTO 		} else {
177933841545SHajimu UMEMOTO 			/* just set an error. do not bark here. */
178033841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL; /* XXX: might be unused. */
178133841545SHajimu UMEMOTO 		}
178233841545SHajimu UMEMOTO 	}
178333841545SHajimu UMEMOTO 
178482cd038dSYoshinobu Inoue end:
1785d748f7efSMark Johnston 	if (pr != NULL)
1786d748f7efSMark Johnston 		nd6_prefix_rele(pr);
1787d748f7efSMark Johnston 	return (error);
178882cd038dSYoshinobu Inoue }
178982cd038dSYoshinobu Inoue 
179082cd038dSYoshinobu Inoue /*
1791686cdd19SJun-ichiro itojun Hagino  * A supplement function used in the on-link detection below;
1792686cdd19SJun-ichiro itojun Hagino  * detect if a given prefix has a (probably) reachable advertising router.
1793686cdd19SJun-ichiro itojun Hagino  * XXX: lengthy function name...
1794686cdd19SJun-ichiro itojun Hagino  */
179533841545SHajimu UMEMOTO static struct nd_pfxrouter *
find_pfxlist_reachable_router(struct nd_prefix * pr)17961272577eSXin LI find_pfxlist_reachable_router(struct nd_prefix *pr)
1797686cdd19SJun-ichiro itojun Hagino {
1798a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1799686cdd19SJun-ichiro itojun Hagino 	struct nd_pfxrouter *pfxrtr;
1800686cdd19SJun-ichiro itojun Hagino 
1801d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1802d748f7efSMark Johnston 
1803a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1804b8a6e03fSGleb Smirnoff 	LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
18059d16275cSAlexander V. Chernikov 		if (is_dr_reachable(pfxrtr->router))
1806d78be3a9SKip Macy 			break;
18076e6b3f7cSQing Li 	}
1808b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
1809686cdd19SJun-ichiro itojun Hagino 	return (pfxrtr);
1810686cdd19SJun-ichiro itojun Hagino }
1811686cdd19SJun-ichiro itojun Hagino 
1812686cdd19SJun-ichiro itojun Hagino /*
181382cd038dSYoshinobu Inoue  * Check if each prefix in the prefix list has at least one available router
181433841545SHajimu UMEMOTO  * that advertised the prefix (a router is "available" if its neighbor cache
181533841545SHajimu UMEMOTO  * entry is reachable or probably reachable).
1816686cdd19SJun-ichiro itojun Hagino  * If the check fails, the prefix may be off-link, because, for example,
181782cd038dSYoshinobu Inoue  * we have moved from the network but the lifetime of the prefix has not
181833841545SHajimu UMEMOTO  * expired yet.  So we should not use the prefix if there is another prefix
181933841545SHajimu UMEMOTO  * that has an available router.
182007c1f959SMark Johnston  * But, if there is no prefix that has an available router, we still regard
182182cd038dSYoshinobu Inoue  * all the prefixes as on-link.  This is because we can't tell if all the
182282cd038dSYoshinobu Inoue  * routers are simply dead or if we really moved from the network and there
182382cd038dSYoshinobu Inoue  * is no router around us.
182482cd038dSYoshinobu Inoue  */
1825686cdd19SJun-ichiro itojun Hagino void
pfxlist_onlink_check(void)18261b28988bSMark Johnston pfxlist_onlink_check(void)
182782cd038dSYoshinobu Inoue {
182882cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
182933841545SHajimu UMEMOTO 	struct in6_ifaddr *ifa;
1830743eee66SSUZUKI Shinsuke 	struct nd_defrouter *dr;
1831743eee66SSUZUKI Shinsuke 	struct nd_pfxrouter *pfxrtr = NULL;
18321b28988bSMark Johnston 	struct rm_priotracker in6_ifa_tracker;
1833d748f7efSMark Johnston 	uint64_t genid;
1834d748f7efSMark Johnston 	uint32_t flags;
1835d748f7efSMark Johnston 
1836d748f7efSMark Johnston 	ND6_ONLINK_LOCK();
1837d748f7efSMark Johnston 	ND6_RLOCK();
183882cd038dSYoshinobu Inoue 
1839686cdd19SJun-ichiro itojun Hagino 	/*
1840686cdd19SJun-ichiro itojun Hagino 	 * Check if there is a prefix that has a reachable advertising
1841686cdd19SJun-ichiro itojun Hagino 	 * router.
1842686cdd19SJun-ichiro itojun Hagino 	 */
18433b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
184433841545SHajimu UMEMOTO 		if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
184582cd038dSYoshinobu Inoue 			break;
1846686cdd19SJun-ichiro itojun Hagino 	}
184782cd038dSYoshinobu Inoue 
184882cd038dSYoshinobu Inoue 	/*
1849743eee66SSUZUKI Shinsuke 	 * If we have no such prefix, check whether we still have a router
1850743eee66SSUZUKI Shinsuke 	 * that does not advertise any prefixes.
1851743eee66SSUZUKI Shinsuke 	 */
1852743eee66SSUZUKI Shinsuke 	if (pr == NULL) {
1853f592d0c3SBjoern A. Zeeb 		TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1854743eee66SSUZUKI Shinsuke 			struct nd_prefix *pr0;
1855743eee66SSUZUKI Shinsuke 
18563b0b2840SJohn Baldwin 			LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1857743eee66SSUZUKI Shinsuke 				if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1858743eee66SSUZUKI Shinsuke 					break;
1859743eee66SSUZUKI Shinsuke 			}
1860743eee66SSUZUKI Shinsuke 			if (pfxrtr != NULL)
1861743eee66SSUZUKI Shinsuke 				break;
1862743eee66SSUZUKI Shinsuke 		}
1863743eee66SSUZUKI Shinsuke 	}
1864f592d0c3SBjoern A. Zeeb 	if (pr != NULL || (!TAILQ_EMPTY(&V_nd6_defrouter) && pfxrtr == NULL)) {
1865743eee66SSUZUKI Shinsuke 		/*
1866743eee66SSUZUKI Shinsuke 		 * There is at least one prefix that has a reachable router,
1867743eee66SSUZUKI Shinsuke 		 * or at least a router which probably does not advertise
1868743eee66SSUZUKI Shinsuke 		 * any prefixes.  The latter would be the case when we move
1869743eee66SSUZUKI Shinsuke 		 * to a new link where we have a router that does not provide
1870743eee66SSUZUKI Shinsuke 		 * prefixes and we configure an address by hand.
187133841545SHajimu UMEMOTO 		 * Detach prefixes which have no reachable advertising
187233841545SHajimu UMEMOTO 		 * router, and attach other prefixes.
187382cd038dSYoshinobu Inoue 		 */
18743b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
187533841545SHajimu UMEMOTO 			/* XXX: a link-local prefix should never be detached */
1876a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1877a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1878a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
187942cb3aa4SQing Li 				continue;
188042cb3aa4SQing Li 
188133841545SHajimu UMEMOTO 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
188233841545SHajimu UMEMOTO 			    find_pfxlist_reachable_router(pr) == NULL)
188333841545SHajimu UMEMOTO 				pr->ndpr_stateflags |= NDPRF_DETACHED;
1884a88d6d7eSMark Johnston 			else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1885155d72c4SPedro F. Giffuni 			    find_pfxlist_reachable_router(pr) != NULL)
188633841545SHajimu UMEMOTO 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
188782cd038dSYoshinobu Inoue 		}
188833841545SHajimu UMEMOTO 	} else {
188933841545SHajimu UMEMOTO 		/* there is no prefix that has a reachable router */
18903b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1891a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1892a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1893a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
189433841545SHajimu UMEMOTO 				continue;
189533841545SHajimu UMEMOTO 			pr->ndpr_stateflags &= ~NDPRF_DETACHED;
189633841545SHajimu UMEMOTO 		}
189733841545SHajimu UMEMOTO 	}
189833841545SHajimu UMEMOTO 
189933841545SHajimu UMEMOTO 	/*
190033841545SHajimu UMEMOTO 	 * Remove each interface route associated with a (just) detached
190133841545SHajimu UMEMOTO 	 * prefix, and reinstall the interface route for a (just) attached
190233841545SHajimu UMEMOTO 	 * prefix.  Note that all attempt of reinstallation does not
190333841545SHajimu UMEMOTO 	 * necessarily success, when a same prefix is shared among multiple
190433841545SHajimu UMEMOTO 	 * interfaces.  Such cases will be handled in nd6_prefix_onlink,
190533841545SHajimu UMEMOTO 	 * so we don't have to care about them.
190633841545SHajimu UMEMOTO 	 */
1907d748f7efSMark Johnston restart:
19083b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
19091d54aa3bSBjoern A. Zeeb 		char ip6buf[INET6_ADDRSTRLEN];
1910a88d6d7eSMark Johnston 		int e;
191133841545SHajimu UMEMOTO 
1912a88d6d7eSMark Johnston 		if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1913a88d6d7eSMark Johnston 		    pr->ndpr_raf_onlink == 0 ||
1914a88d6d7eSMark Johnston 		    pr->ndpr_raf_auto == 0)
191542cb3aa4SQing Li 			continue;
191642cb3aa4SQing Li 
1917d748f7efSMark Johnston 		flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1918d748f7efSMark Johnston 		if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1919d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1920d748f7efSMark Johnston 			ND6_RUNLOCK();
1921d748f7efSMark Johnston 			if ((flags & NDPRF_ONLINK) != 0 &&
1922d748f7efSMark Johnston 			    (e = nd6_prefix_offlink(pr)) != 0) {
192333841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
1924c6feea3bSBjoern A. Zeeb 				    "%s: failed to make %s/%d offlink "
1925c6feea3bSBjoern A. Zeeb 				    "(errno=%d)\n", __func__,
19261d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
19271d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
192833841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
1929d748f7efSMark Johnston 			} else if ((flags & NDPRF_ONLINK) == 0 &&
1930d748f7efSMark Johnston 			    (e = nd6_prefix_onlink(pr)) != 0) {
193133841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
1932c6feea3bSBjoern A. Zeeb 				    "%s: failed to make %s/%d onlink "
1933c6feea3bSBjoern A. Zeeb 				    "(errno=%d)\n", __func__,
19341d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
19351d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
193633841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
193733841545SHajimu UMEMOTO 			}
1938d748f7efSMark Johnston 			ND6_RLOCK();
1939d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1940d748f7efSMark Johnston 				goto restart;
194133841545SHajimu UMEMOTO 		}
194233841545SHajimu UMEMOTO 	}
194333841545SHajimu UMEMOTO 
194433841545SHajimu UMEMOTO 	/*
194533841545SHajimu UMEMOTO 	 * Changes on the prefix status might affect address status as well.
194633841545SHajimu UMEMOTO 	 * Make sure that all addresses derived from an attached prefix are
194733841545SHajimu UMEMOTO 	 * attached, and that all addresses derived from a detached prefix are
194833841545SHajimu UMEMOTO 	 * detached.  Note, however, that a manually configured address should
194933841545SHajimu UMEMOTO 	 * always be attached.
195033841545SHajimu UMEMOTO 	 * The precise detection logic is same as the one for prefixes.
195133841545SHajimu UMEMOTO 	 */
19521b28988bSMark Johnston 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1953d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
195407eb2995SHajimu UMEMOTO 		if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
195533841545SHajimu UMEMOTO 			continue;
195633841545SHajimu UMEMOTO 
195733841545SHajimu UMEMOTO 		if (ifa->ia6_ndpr == NULL) {
195833841545SHajimu UMEMOTO 			/*
195933841545SHajimu UMEMOTO 			 * This can happen when we first configure the address
196033841545SHajimu UMEMOTO 			 * (i.e. the address exists, but the prefix does not).
196133841545SHajimu UMEMOTO 			 * XXX: complicated relationships...
196233841545SHajimu UMEMOTO 			 */
196333841545SHajimu UMEMOTO 			continue;
196433841545SHajimu UMEMOTO 		}
196533841545SHajimu UMEMOTO 
196633841545SHajimu UMEMOTO 		if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
196733841545SHajimu UMEMOTO 			break;
196833841545SHajimu UMEMOTO 	}
196933841545SHajimu UMEMOTO 	if (ifa) {
1970d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
197133841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
197233841545SHajimu UMEMOTO 				continue;
197333841545SHajimu UMEMOTO 
197433841545SHajimu UMEMOTO 			if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
197533841545SHajimu UMEMOTO 				continue;
197633841545SHajimu UMEMOTO 
1977743eee66SSUZUKI Shinsuke 			if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1978743eee66SSUZUKI Shinsuke 				if (ifa->ia6_flags & IN6_IFF_DETACHED) {
197933841545SHajimu UMEMOTO 					ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1980743eee66SSUZUKI Shinsuke 					ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1981743eee66SSUZUKI Shinsuke 					nd6_dad_start((struct ifaddr *)ifa, 0);
1982743eee66SSUZUKI Shinsuke 				}
1983743eee66SSUZUKI Shinsuke 			} else {
198433841545SHajimu UMEMOTO 				ifa->ia6_flags |= IN6_IFF_DETACHED;
198582cd038dSYoshinobu Inoue 			}
1986686cdd19SJun-ichiro itojun Hagino 		}
19871b28988bSMark Johnston 	} else {
1988d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
198933841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
199033841545SHajimu UMEMOTO 				continue;
199133841545SHajimu UMEMOTO 
1992743eee66SSUZUKI Shinsuke 			if (ifa->ia6_flags & IN6_IFF_DETACHED) {
199333841545SHajimu UMEMOTO 				ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1994743eee66SSUZUKI Shinsuke 				ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1995743eee66SSUZUKI Shinsuke 				/* Do we need a delay in this case? */
1996743eee66SSUZUKI Shinsuke 				nd6_dad_start((struct ifaddr *)ifa, 0);
1997743eee66SSUZUKI Shinsuke 			}
199833841545SHajimu UMEMOTO 		}
199982cd038dSYoshinobu Inoue 	}
20001b28988bSMark Johnston 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
2001d748f7efSMark Johnston 	ND6_RUNLOCK();
2002d748f7efSMark Johnston 	ND6_ONLINK_UNLOCK();
200382cd038dSYoshinobu Inoue }
200482cd038dSYoshinobu Inoue 
200581728a53SAlexander V. Chernikov /*
200681728a53SAlexander V. Chernikov  * Add or remove interface route specified by @dst, @netmask and @ifp.
200781728a53SAlexander V. Chernikov  * ifa can be NULL.
200881728a53SAlexander V. Chernikov  * Returns 0 on success
200981728a53SAlexander V. Chernikov  */
201081728a53SAlexander V. Chernikov static int
nd6_prefix_rtrequest(uint32_t fibnum,int cmd,struct sockaddr_in6 * dst,struct sockaddr_in6 * netmask,struct ifnet * ifp,struct ifaddr * ifa)20117b3440fcSAlexander V. Chernikov nd6_prefix_rtrequest(uint32_t fibnum, int cmd, struct sockaddr_in6 *dst,
20127b3440fcSAlexander V. Chernikov     struct sockaddr_in6 *netmask, struct ifnet *ifp, struct ifaddr *ifa)
201381728a53SAlexander V. Chernikov {
201481728a53SAlexander V. Chernikov 	struct epoch_tracker et;
201581728a53SAlexander V. Chernikov 	int error;
201681728a53SAlexander V. Chernikov 
20177b3440fcSAlexander V. Chernikov 	/* Prepare gateway */
20187b3440fcSAlexander V. Chernikov 	struct sockaddr_dl_short sdl = {
20197b3440fcSAlexander V. Chernikov 		.sdl_family = AF_LINK,
20207b3440fcSAlexander V. Chernikov 		.sdl_len = sizeof(struct sockaddr_dl_short),
20217b3440fcSAlexander V. Chernikov 		.sdl_type = ifp->if_type,
20227b3440fcSAlexander V. Chernikov 		.sdl_index = ifp->if_index,
20237b3440fcSAlexander V. Chernikov 	};
20247b3440fcSAlexander V. Chernikov 
20257b3440fcSAlexander V. Chernikov 	struct rt_addrinfo info = {
20267b3440fcSAlexander V. Chernikov 		.rti_ifa = ifa,
20277b3440fcSAlexander V. Chernikov 		.rti_ifp = ifp,
20287b3440fcSAlexander V. Chernikov 		.rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
20297b3440fcSAlexander V. Chernikov 		.rti_info = {
20307b3440fcSAlexander V. Chernikov 			[RTAX_DST] = (struct sockaddr *)dst,
20317b3440fcSAlexander V. Chernikov 			[RTAX_NETMASK] = (struct sockaddr *)netmask,
20327b3440fcSAlexander V. Chernikov 			[RTAX_GATEWAY] = (struct sockaddr *)&sdl,
20337b3440fcSAlexander V. Chernikov 		},
20347b3440fcSAlexander V. Chernikov 	};
20357b3440fcSAlexander V. Chernikov 	/* Don't set additional per-gw filters on removal */
20367b3440fcSAlexander V. Chernikov 
203781728a53SAlexander V. Chernikov 	NET_EPOCH_ENTER(et);
20387b3440fcSAlexander V. Chernikov 	error = rib_handle_ifaddr_info(fibnum, cmd, &info);
203981728a53SAlexander V. Chernikov 	NET_EPOCH_EXIT(et);
204081728a53SAlexander V. Chernikov 	return (error);
204181728a53SAlexander V. Chernikov }
204281728a53SAlexander V. Chernikov 
20439d9b92f2SBjoern A. Zeeb static int
nd6_prefix_onlink_rtrequest(struct nd_prefix * pr,struct ifaddr * ifa)204481d5d46bSBjoern A. Zeeb nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
204581d5d46bSBjoern A. Zeeb {
204681728a53SAlexander V. Chernikov 	int error;
204781d5d46bSBjoern A. Zeeb 
20487b3440fcSAlexander V. Chernikov 	struct sockaddr_in6 mask6 = {
20497b3440fcSAlexander V. Chernikov 		.sin6_family = AF_INET6,
20507b3440fcSAlexander V. Chernikov 		.sin6_len = sizeof(struct sockaddr_in6),
20517b3440fcSAlexander V. Chernikov 		.sin6_addr = pr->ndpr_mask,
20527b3440fcSAlexander V. Chernikov 	};
20537b3440fcSAlexander V. Chernikov 	struct sockaddr_in6 *pmask6 = (pr->ndpr_plen != 128) ? &mask6 : NULL;
20547b3440fcSAlexander V. Chernikov 
205581728a53SAlexander V. Chernikov 	error = nd6_prefix_rtrequest(pr->ndpr_ifp->if_fib, RTM_ADD,
20567b3440fcSAlexander V. Chernikov 	    &pr->ndpr_prefix, pmask6, pr->ndpr_ifp, ifa);
205781728a53SAlexander V. Chernikov 	if (error == 0)
2058dd4776f0SAlexander V. Chernikov 		pr->ndpr_stateflags |= NDPRF_ONLINK;
205981d5d46bSBjoern A. Zeeb 
206081728a53SAlexander V. Chernikov 	return (error);
206181d5d46bSBjoern A. Zeeb }
206281d5d46bSBjoern A. Zeeb 
206325ebfe33SBjoern A. Zeeb static int
nd6_prefix_onlink(struct nd_prefix * pr)20641272577eSXin LI nd6_prefix_onlink(struct nd_prefix *pr)
206582cd038dSYoshinobu Inoue {
2066751d8d15SGleb Smirnoff 	struct epoch_tracker et;
206733841545SHajimu UMEMOTO 	struct ifaddr *ifa;
206833841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
206933841545SHajimu UMEMOTO 	struct nd_prefix *opr;
20701d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2071d748f7efSMark Johnston 	int error;
207233841545SHajimu UMEMOTO 
2073d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2074d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2075d748f7efSMark Johnston 
2076d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
20772ce62dceSSUZUKI Shinsuke 		return (EEXIST);
207882cd038dSYoshinobu Inoue 
207982cd038dSYoshinobu Inoue 	/*
208033841545SHajimu UMEMOTO 	 * Add the interface route associated with the prefix.  Before
208133841545SHajimu UMEMOTO 	 * installing the route, check if there's the same prefix on another
208233841545SHajimu UMEMOTO 	 * interface, and the prefix has already installed the interface route.
208333841545SHajimu UMEMOTO 	 * Although such a configuration is expected to be rare, we explicitly
208433841545SHajimu UMEMOTO 	 * allow it.
208582cd038dSYoshinobu Inoue 	 */
2086d748f7efSMark Johnston 	ND6_RLOCK();
20873b0b2840SJohn Baldwin 	LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
208833841545SHajimu UMEMOTO 		if (opr == pr)
208933841545SHajimu UMEMOTO 			continue;
209033841545SHajimu UMEMOTO 
209133841545SHajimu UMEMOTO 		if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
209233841545SHajimu UMEMOTO 			continue;
209333841545SHajimu UMEMOTO 
2094559b4296SAlan Somers 		if (!V_rt_add_addr_allfibs &&
2095559b4296SAlan Somers 		    opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2096559b4296SAlan Somers 			continue;
2097559b4296SAlan Somers 
209833841545SHajimu UMEMOTO 		if (opr->ndpr_plen == pr->ndpr_plen &&
209933841545SHajimu UMEMOTO 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2100d748f7efSMark Johnston 		    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2101d748f7efSMark Johnston 			ND6_RUNLOCK();
210233841545SHajimu UMEMOTO 			return (0);
210333841545SHajimu UMEMOTO 		}
2104d748f7efSMark Johnston 	}
2105d748f7efSMark Johnston 	ND6_RUNLOCK();
210633841545SHajimu UMEMOTO 
210733841545SHajimu UMEMOTO 	/*
210833841545SHajimu UMEMOTO 	 * We prefer link-local addresses as the associated interface address.
210933841545SHajimu UMEMOTO 	 */
211033841545SHajimu UMEMOTO 	/* search for a link-local addr */
2111751d8d15SGleb Smirnoff 	NET_EPOCH_ENTER(et);
211233841545SHajimu UMEMOTO 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
211307eb2995SHajimu UMEMOTO 	    IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
211433841545SHajimu UMEMOTO 	if (ifa == NULL) {
211533841545SHajimu UMEMOTO 		/* XXX: freebsd does not have ifa_ifwithaf */
2116d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2117c2615844SMark Johnston 			if (ifa->ifa_addr->sa_family == AF_INET6) {
2118c2615844SMark Johnston 				ifa_ref(ifa);
211933841545SHajimu UMEMOTO 				break;
212033841545SHajimu UMEMOTO 			}
2121c2615844SMark Johnston 		}
212233841545SHajimu UMEMOTO 		/* should we care about ia6_flags? */
212333841545SHajimu UMEMOTO 	}
212433841545SHajimu UMEMOTO 	if (ifa == NULL) {
212533841545SHajimu UMEMOTO 		/*
212633841545SHajimu UMEMOTO 		 * This can still happen, when, for example, we receive an RA
212733841545SHajimu UMEMOTO 		 * containing a prefix with the L bit set and the A bit clear,
212833841545SHajimu UMEMOTO 		 * after removing all IPv6 addresses on the receiving
212933841545SHajimu UMEMOTO 		 * interface.  This should, of course, be rare though.
213033841545SHajimu UMEMOTO 		 */
213133841545SHajimu UMEMOTO 		nd6log((LOG_NOTICE,
2132c6feea3bSBjoern A. Zeeb 		    "%s: failed to find any ifaddr to add route for a "
2133c6feea3bSBjoern A. Zeeb 		    "prefix(%s/%d) on %s\n", __func__,
21341d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
213533841545SHajimu UMEMOTO 		    pr->ndpr_plen, if_name(ifp)));
21362bbab0afSAlexander V. Chernikov 		error = 0;
21372bbab0afSAlexander V. Chernikov 	} else {
213881d5d46bSBjoern A. Zeeb 		error = nd6_prefix_onlink_rtrequest(pr, ifa);
21398c0fec80SRobert Watson 		ifa_free(ifa);
21402bbab0afSAlexander V. Chernikov 	}
21412bbab0afSAlexander V. Chernikov 	NET_EPOCH_EXIT(et);
214233841545SHajimu UMEMOTO 
214333841545SHajimu UMEMOTO 	return (error);
214433841545SHajimu UMEMOTO }
214533841545SHajimu UMEMOTO 
2146d748f7efSMark Johnston int
nd6_prefix_offlink(struct nd_prefix * pr)21471272577eSXin LI nd6_prefix_offlink(struct nd_prefix *pr)
214833841545SHajimu UMEMOTO {
214933841545SHajimu UMEMOTO 	int error = 0;
215033841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
215133841545SHajimu UMEMOTO 	struct nd_prefix *opr;
21521d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2153d748f7efSMark Johnston 	uint64_t genid;
215481728a53SAlexander V. Chernikov 	int a_failure;
215533841545SHajimu UMEMOTO 
2156d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2157d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2158d748f7efSMark Johnston 
2159d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
216033841545SHajimu UMEMOTO 		return (EEXIST);
216133841545SHajimu UMEMOTO 
216281728a53SAlexander V. Chernikov 	struct sockaddr_in6 mask6 = {
216381728a53SAlexander V. Chernikov 		.sin6_family = AF_INET6,
216481728a53SAlexander V. Chernikov 		.sin6_len = sizeof(struct sockaddr_in6),
216581728a53SAlexander V. Chernikov 		.sin6_addr = pr->ndpr_mask,
216681728a53SAlexander V. Chernikov 	};
21677b3440fcSAlexander V. Chernikov 	struct sockaddr_in6 *pmask6 = (pr->ndpr_plen != 128) ? &mask6 : NULL;
216833841545SHajimu UMEMOTO 
216981728a53SAlexander V. Chernikov 	error = nd6_prefix_rtrequest(ifp->if_fib, RTM_DELETE,
21707b3440fcSAlexander V. Chernikov 	    &pr->ndpr_prefix, pmask6, ifp, NULL);
2171559b4296SAlan Somers 
2172378aa8d8SQing Li 	a_failure = 1;
217381d5d46bSBjoern A. Zeeb 	if (error == 0) {
217481d5d46bSBjoern A. Zeeb 		pr->ndpr_stateflags &= ~NDPRF_ONLINK;
217533841545SHajimu UMEMOTO 
217633841545SHajimu UMEMOTO 		/*
217733841545SHajimu UMEMOTO 		 * There might be the same prefix on another interface,
217833841545SHajimu UMEMOTO 		 * the prefix which could not be on-link just because we have
217933841545SHajimu UMEMOTO 		 * the interface route (see comments in nd6_prefix_onlink).
218033841545SHajimu UMEMOTO 		 * If there's one, try to make the prefix on-link on the
218133841545SHajimu UMEMOTO 		 * interface.
218233841545SHajimu UMEMOTO 		 */
2183d748f7efSMark Johnston 		ND6_RLOCK();
2184d748f7efSMark Johnston restart:
21853b0b2840SJohn Baldwin 		LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
218633841545SHajimu UMEMOTO 			/*
218733841545SHajimu UMEMOTO 			 * KAME specific: detached prefixes should not be
218833841545SHajimu UMEMOTO 			 * on-link.
218933841545SHajimu UMEMOTO 			 */
21907b0e84b7SMark Johnston 			if (opr == pr || (opr->ndpr_stateflags &
21917b0e84b7SMark Johnston 			    (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
219233841545SHajimu UMEMOTO 				continue;
219333841545SHajimu UMEMOTO 
219433841545SHajimu UMEMOTO 			if (opr->ndpr_plen == pr->ndpr_plen &&
219533841545SHajimu UMEMOTO 			    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
219607eb2995SHajimu UMEMOTO 			    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
219782cd038dSYoshinobu Inoue 				int e;
219882cd038dSYoshinobu Inoue 
2199d748f7efSMark Johnston 				genid = V_nd6_list_genid;
2200d748f7efSMark Johnston 				ND6_RUNLOCK();
220133841545SHajimu UMEMOTO 				if ((e = nd6_prefix_onlink(opr)) != 0) {
220233841545SHajimu UMEMOTO 					nd6log((LOG_ERR,
2203c6feea3bSBjoern A. Zeeb 					    "%s: failed to recover a prefix "
2204c6feea3bSBjoern A. Zeeb 					    "%s/%d from %s to %s (errno=%d)\n",
2205c6feea3bSBjoern A. Zeeb 					    __func__, ip6_sprintf(ip6buf,
22061d54aa3bSBjoern A. Zeeb 						&opr->ndpr_prefix.sin6_addr),
220733841545SHajimu UMEMOTO 					    opr->ndpr_plen, if_name(ifp),
220833841545SHajimu UMEMOTO 					    if_name(opr->ndpr_ifp), e));
2209378aa8d8SQing Li 				} else
2210378aa8d8SQing Li 					a_failure = 0;
2211d748f7efSMark Johnston 				ND6_RLOCK();
2212d748f7efSMark Johnston 				if (genid != V_nd6_list_genid)
2213d748f7efSMark Johnston 					goto restart;
221482cd038dSYoshinobu Inoue 			}
221582cd038dSYoshinobu Inoue 		}
2216d748f7efSMark Johnston 		ND6_RUNLOCK();
221707eb2995SHajimu UMEMOTO 	} else {
221833841545SHajimu UMEMOTO 		/* XXX: can we still set the NDPRF_ONLINK flag? */
221933841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
2220c6feea3bSBjoern A. Zeeb 		    "%s: failed to delete route: %s/%d on %s (errno=%d)\n",
222181728a53SAlexander V. Chernikov 		    __func__, ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
2222c6feea3bSBjoern A. Zeeb 		    pr->ndpr_plen, if_name(ifp), error));
222333841545SHajimu UMEMOTO 	}
222482cd038dSYoshinobu Inoue 
2225378aa8d8SQing Li 	if (a_failure)
2226c1dd4d64SMark Johnston 		lltable_prefix_free(AF_INET6,
2227c1dd4d64SMark Johnston 		    (struct sockaddr *)&pr->ndpr_prefix,
2228378aa8d8SQing Li 		    (struct sockaddr *)&mask6, LLE_STATIC);
2229378aa8d8SQing Li 
223033841545SHajimu UMEMOTO 	return (error);
223182cd038dSYoshinobu Inoue }
223282cd038dSYoshinobu Inoue 
22331272577eSXin LI /*
22341272577eSXin LI  * ia0 - corresponding public address
22351272577eSXin LI  */
223682cd038dSYoshinobu Inoue int
in6_tmpifadd(const struct in6_ifaddr * ia0,int forcegen,int delay)22371272577eSXin LI in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
223882cd038dSYoshinobu Inoue {
223933841545SHajimu UMEMOTO 	struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
22400c5d4bdeSAlexander V. Chernikov 	struct in6_ifaddr *newia;
224133841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
22421dc8f6a8SAlexander V. Chernikov 	int error;
224333841545SHajimu UMEMOTO 	int trylimit = 3;	/* XXX: adhoc value */
2244743eee66SSUZUKI Shinsuke 	int updateflags;
224533841545SHajimu UMEMOTO 	u_int32_t randid[2];
224633841545SHajimu UMEMOTO 	time_t vltime0, pltime0;
224782cd038dSYoshinobu Inoue 
22489080e7d0SAlexander V. Chernikov 	in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
22499080e7d0SAlexander V. Chernikov 	    &ia0->ia_prefixmask.sin6_addr);
22509080e7d0SAlexander V. Chernikov 
22519080e7d0SAlexander V. Chernikov 	ifra.ifra_addr = ia0->ia_addr;	/* XXX: do we need this ? */
225233841545SHajimu UMEMOTO 	/* clear the old IFID */
22531dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
22541dc8f6a8SAlexander V. Chernikov 	    &ifra.ifra_prefixmask.sin6_addr);
225582cd038dSYoshinobu Inoue 
225633841545SHajimu UMEMOTO   again:
2257743eee66SSUZUKI Shinsuke 	if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2258743eee66SSUZUKI Shinsuke 	    (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2259c6feea3bSBjoern A. Zeeb 		nd6log((LOG_NOTICE, "%s: failed to find a good random IFID\n",
2260c6feea3bSBjoern A. Zeeb 		    __func__));
2261743eee66SSUZUKI Shinsuke 		return (EINVAL);
2262743eee66SSUZUKI Shinsuke 	}
226307eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
226407eb2995SHajimu UMEMOTO 	    (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
226507eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
226607eb2995SHajimu UMEMOTO 	    (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
226782cd038dSYoshinobu Inoue 
226882cd038dSYoshinobu Inoue 	/*
2269743eee66SSUZUKI Shinsuke 	 * in6_get_tmpifid() quite likely provided a unique interface ID.
2270743eee66SSUZUKI Shinsuke 	 * However, we may still have a chance to see collision, because
2271743eee66SSUZUKI Shinsuke 	 * there may be a time lag between generation of the ID and generation
2272743eee66SSUZUKI Shinsuke 	 * of the address.  So, we'll do one more sanity check.
227382cd038dSYoshinobu Inoue 	 */
22740c5d4bdeSAlexander V. Chernikov 
22750c5d4bdeSAlexander V. Chernikov 	if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
22760c5d4bdeSAlexander V. Chernikov 		if (trylimit-- > 0) {
22770c5d4bdeSAlexander V. Chernikov 			forcegen = 1;
22780c5d4bdeSAlexander V. Chernikov 			goto again;
22790c5d4bdeSAlexander V. Chernikov 		}
22800c5d4bdeSAlexander V. Chernikov 
22810c5d4bdeSAlexander V. Chernikov 		/* Give up.  Something strange should have happened.  */
2282c6feea3bSBjoern A. Zeeb 		nd6log((LOG_NOTICE, "%s: failed to find a unique random IFID\n",
2283c6feea3bSBjoern A. Zeeb 		    __func__));
228433841545SHajimu UMEMOTO 		return (EEXIST);
228533841545SHajimu UMEMOTO 	}
228682cd038dSYoshinobu Inoue 
228733841545SHajimu UMEMOTO 	/*
228833841545SHajimu UMEMOTO 	 * The Valid Lifetime is the lower of the Valid Lifetime of the
228933841545SHajimu UMEMOTO          * public address or TEMP_VALID_LIFETIME.
229033841545SHajimu UMEMOTO 	 * The Preferred Lifetime is the lower of the Preferred Lifetime
229133841545SHajimu UMEMOTO          * of the public address or TEMP_PREFERRED_LIFETIME -
229233841545SHajimu UMEMOTO          * DESYNC_FACTOR.
229382cd038dSYoshinobu Inoue 	 */
2294743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
229533841545SHajimu UMEMOTO 		vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2296743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_vltime -
22977d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2298603724d3SBjoern A. Zeeb 		if (vltime0 > V_ip6_temp_valid_lifetime)
2299603724d3SBjoern A. Zeeb 			vltime0 = V_ip6_temp_valid_lifetime;
230033841545SHajimu UMEMOTO 	} else
2301603724d3SBjoern A. Zeeb 		vltime0 = V_ip6_temp_valid_lifetime;
2302743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
230333841545SHajimu UMEMOTO 		pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2304743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_pltime -
23057d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2306603724d3SBjoern A. Zeeb 		if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2307603724d3SBjoern A. Zeeb 			pltime0 = V_ip6_temp_preferred_lifetime -
2308603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor;
230933841545SHajimu UMEMOTO 		}
231033841545SHajimu UMEMOTO 	} else
2311603724d3SBjoern A. Zeeb 		pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
231233841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = vltime0;
231333841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = pltime0;
231433841545SHajimu UMEMOTO 
231533841545SHajimu UMEMOTO 	/*
231633841545SHajimu UMEMOTO 	 * A temporary address is created only if this calculated Preferred
231733841545SHajimu UMEMOTO 	 * Lifetime is greater than REGEN_ADVANCE time units.
231833841545SHajimu UMEMOTO 	 */
2319603724d3SBjoern A. Zeeb 	if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
232033841545SHajimu UMEMOTO 		return (0);
232133841545SHajimu UMEMOTO 
232233841545SHajimu UMEMOTO 	/* XXX: scope zone ID? */
232333841545SHajimu UMEMOTO 
232433841545SHajimu UMEMOTO 	ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
232533841545SHajimu UMEMOTO 
232633841545SHajimu UMEMOTO 	/* allocate ifaddr structure, link into chain, etc. */
2327743eee66SSUZUKI Shinsuke 	updateflags = 0;
2328743eee66SSUZUKI Shinsuke 	if (delay)
2329743eee66SSUZUKI Shinsuke 		updateflags |= IN6_IFAUPDATE_DADDELAY;
2330743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
233133841545SHajimu UMEMOTO 		return (error);
233233841545SHajimu UMEMOTO 
233333841545SHajimu UMEMOTO 	newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
233433841545SHajimu UMEMOTO 	if (newia == NULL) {	/* XXX: can it happen? */
233533841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
2336c6feea3bSBjoern A. Zeeb 		    "%s: ifa update succeeded, but we got no ifaddr\n",
2337c6feea3bSBjoern A. Zeeb 		    __func__));
233833841545SHajimu UMEMOTO 		return (EINVAL); /* XXX */
233933841545SHajimu UMEMOTO 	}
234033841545SHajimu UMEMOTO 	newia->ia6_ndpr = ia0->ia6_ndpr;
23410bbf244eSMark Johnston 	newia->ia6_ndpr->ndpr_addrcnt++;
23428c0fec80SRobert Watson 	ifa_free(&newia->ia_ifa);
234333841545SHajimu UMEMOTO 
2344c3aacd9eSHajimu UMEMOTO 	/*
2345c3aacd9eSHajimu UMEMOTO 	 * A newly added address might affect the status of other addresses.
2346c3aacd9eSHajimu UMEMOTO 	 * XXX: when the temporary address is generated with a new public
2347c3aacd9eSHajimu UMEMOTO 	 * address, the onlink check is redundant.  However, it would be safe
2348c3aacd9eSHajimu UMEMOTO 	 * to do the check explicitly everywhere a new address is generated,
2349c3aacd9eSHajimu UMEMOTO 	 * and, in fact, we surely need the check when we create a new
2350c3aacd9eSHajimu UMEMOTO 	 * temporary address due to deprecation of an old temporary address.
2351c3aacd9eSHajimu UMEMOTO 	 */
2352c3aacd9eSHajimu UMEMOTO 	pfxlist_onlink_check();
2353c3aacd9eSHajimu UMEMOTO 
235433841545SHajimu UMEMOTO 	return (0);
235582cd038dSYoshinobu Inoue }
235682cd038dSYoshinobu Inoue 
2357743eee66SSUZUKI Shinsuke static int
rt6_deleteroute(const struct rtentry * rt,const struct nhop_object * nh,void * arg)2358539642a2SAlexander V. Chernikov rt6_deleteroute(const struct rtentry *rt, const struct nhop_object *nh,
2359539642a2SAlexander V. Chernikov     void *arg)
236082cd038dSYoshinobu Inoue {
236182cd038dSYoshinobu Inoue 	struct in6_addr *gate = (struct in6_addr *)arg;
2362539642a2SAlexander V. Chernikov 	int nh_rt_flags;
236382cd038dSYoshinobu Inoue 
2364539642a2SAlexander V. Chernikov 	if (nh->gw_sa.sa_family != AF_INET6)
236582cd038dSYoshinobu Inoue 		return (0);
236682cd038dSYoshinobu Inoue 
2367539642a2SAlexander V. Chernikov 	if (!IN6_ARE_ADDR_EQUAL(gate, &nh->gw6_sa.sin6_addr)) {
236882cd038dSYoshinobu Inoue 		return (0);
236907eb2995SHajimu UMEMOTO 	}
237082cd038dSYoshinobu Inoue 
237182cd038dSYoshinobu Inoue 	/*
237233841545SHajimu UMEMOTO 	 * Do not delete a static route.
237333841545SHajimu UMEMOTO 	 * XXX: this seems to be a bit ad-hoc. Should we consider the
237433841545SHajimu UMEMOTO 	 * 'cloned' bit instead?
237533841545SHajimu UMEMOTO 	 */
2376539642a2SAlexander V. Chernikov 	nh_rt_flags = nhop_get_rtflags(nh);
2377539642a2SAlexander V. Chernikov 	if ((nh_rt_flags & RTF_STATIC) != 0)
237833841545SHajimu UMEMOTO 		return (0);
237933841545SHajimu UMEMOTO 
238033841545SHajimu UMEMOTO 	/*
238182cd038dSYoshinobu Inoue 	 * We delete only host route. This means, in particular, we don't
238282cd038dSYoshinobu Inoue 	 * delete default route.
238382cd038dSYoshinobu Inoue 	 */
2384539642a2SAlexander V. Chernikov 	if ((nh_rt_flags & RTF_HOST) == 0)
238582cd038dSYoshinobu Inoue 		return (0);
238682cd038dSYoshinobu Inoue 
2387e8b0643eSAlexander V. Chernikov 	return (1);
238882cd038dSYoshinobu Inoue #undef SIN6
238982cd038dSYoshinobu Inoue }
2390686cdd19SJun-ichiro itojun Hagino 
2391f77a6dbdSBjoern A. Zeeb /*
2392f77a6dbdSBjoern A. Zeeb  * Delete all the routing table entries that use the specified gateway.
2393f77a6dbdSBjoern A. Zeeb  * XXX: this function causes search through all entries of routing table, so
2394f77a6dbdSBjoern A. Zeeb  * it shouldn't be called when acting as a router.
2395f77a6dbdSBjoern A. Zeeb  */
2396f77a6dbdSBjoern A. Zeeb void
rt6_flush(struct in6_addr * gateway,struct ifnet * ifp)2397f77a6dbdSBjoern A. Zeeb rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2398f77a6dbdSBjoern A. Zeeb {
2399f77a6dbdSBjoern A. Zeeb 
2400f77a6dbdSBjoern A. Zeeb 	/* We'll care only link-local addresses */
2401f77a6dbdSBjoern A. Zeeb 	if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2402f77a6dbdSBjoern A. Zeeb 		return;
2403f77a6dbdSBjoern A. Zeeb 
2404f77a6dbdSBjoern A. Zeeb 	/* XXX Do we really need to walk any but the default FIB? */
24057511a638SAlexander V. Chernikov 	rib_foreach_table_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2406f77a6dbdSBjoern A. Zeeb }
2407f77a6dbdSBjoern A. Zeeb 
2408686cdd19SJun-ichiro itojun Hagino int
nd6_setdefaultiface(int ifindex)24091272577eSXin LI nd6_setdefaultiface(int ifindex)
2410686cdd19SJun-ichiro itojun Hagino {
2411686cdd19SJun-ichiro itojun Hagino 
2412603724d3SBjoern A. Zeeb 	if (V_nd6_defifindex != ifindex) {
2413603724d3SBjoern A. Zeeb 		V_nd6_defifindex = ifindex;
2414d74b7baeSGleb Smirnoff 		if (V_nd6_defifindex != 0) {
2415d74b7baeSGleb Smirnoff 			struct epoch_tracker et;
2416d74b7baeSGleb Smirnoff 
2417d74b7baeSGleb Smirnoff 			/*
2418d74b7baeSGleb Smirnoff 			 * XXXGL: this function should use ifnet_byindex_ref!
2419d74b7baeSGleb Smirnoff 			 */
2420d74b7baeSGleb Smirnoff 			NET_EPOCH_ENTER(et);
2421603724d3SBjoern A. Zeeb 			V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2422d74b7baeSGleb Smirnoff 			NET_EPOCH_EXIT(et);
2423d74b7baeSGleb Smirnoff 			if (V_nd6_defifp == NULL)
2424d74b7baeSGleb Smirnoff 				return (EINVAL);
2425d74b7baeSGleb Smirnoff 		} else
2426603724d3SBjoern A. Zeeb 			V_nd6_defifp = NULL;
2427686cdd19SJun-ichiro itojun Hagino 
2428686cdd19SJun-ichiro itojun Hagino 		/*
2429c55ecce1SGordon Bergling 		 * Our current implementation assumes one-to-one mapping between
2430686cdd19SJun-ichiro itojun Hagino 		 * interfaces and links, so it would be natural to use the
2431686cdd19SJun-ichiro itojun Hagino 		 * default interface as the default link.
2432686cdd19SJun-ichiro itojun Hagino 		 */
2433603724d3SBjoern A. Zeeb 		scope6_setdefault(V_nd6_defifp);
2434686cdd19SJun-ichiro itojun Hagino 	}
2435686cdd19SJun-ichiro itojun Hagino 
2436d74b7baeSGleb Smirnoff 	return (0);
2437686cdd19SJun-ichiro itojun Hagino }
2438ad675b32SBjoern A. Zeeb 
2439ad675b32SBjoern A. Zeeb bool
nd6_defrouter_list_empty(void)2440ad675b32SBjoern A. Zeeb nd6_defrouter_list_empty(void)
2441ad675b32SBjoern A. Zeeb {
2442ad675b32SBjoern A. Zeeb 
2443f592d0c3SBjoern A. Zeeb 	return (TAILQ_EMPTY(&V_nd6_defrouter));
2444ad675b32SBjoern A. Zeeb }
2445ad675b32SBjoern A. Zeeb 
2446ad675b32SBjoern A. Zeeb void
nd6_defrouter_timer(void)2447ad675b32SBjoern A. Zeeb nd6_defrouter_timer(void)
2448ad675b32SBjoern A. Zeeb {
2449ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2450f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2451ad675b32SBjoern A. Zeeb 
2452ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2453ad675b32SBjoern A. Zeeb 
2454ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2455f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr)
2456ad675b32SBjoern A. Zeeb 		if (dr->expire && dr->expire < time_uptime)
2457ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2458ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2459ad675b32SBjoern A. Zeeb 
2460ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2461ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2462ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2463ad675b32SBjoern A. Zeeb 	}
2464ad675b32SBjoern A. Zeeb }
2465ad675b32SBjoern A. Zeeb 
2466ad675b32SBjoern A. Zeeb /*
2467ad675b32SBjoern A. Zeeb  * Nuke default router list entries toward ifp.
2468ad675b32SBjoern A. Zeeb  * We defer removal of default router list entries that is installed in the
2469ad675b32SBjoern A. Zeeb  * routing table, in order to keep additional side effects as small as possible.
2470ad675b32SBjoern A. Zeeb  */
2471ad675b32SBjoern A. Zeeb void
nd6_defrouter_purge(struct ifnet * ifp)2472ad675b32SBjoern A. Zeeb nd6_defrouter_purge(struct ifnet *ifp)
2473ad675b32SBjoern A. Zeeb {
2474ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2475f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2476ad675b32SBjoern A. Zeeb 
2477ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2478ad675b32SBjoern A. Zeeb 
2479ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2480f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2481ad675b32SBjoern A. Zeeb 		if (dr->installed)
2482ad675b32SBjoern A. Zeeb 			continue;
2483ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2484ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2485ad675b32SBjoern A. Zeeb 	}
2486f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2487ad675b32SBjoern A. Zeeb 		if (!dr->installed)
2488ad675b32SBjoern A. Zeeb 			continue;
2489ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2490ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2491ad675b32SBjoern A. Zeeb 	}
2492ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2493ad675b32SBjoern A. Zeeb 
2494ad675b32SBjoern A. Zeeb 	/* Delete the unlinked router objects. */
2495ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2496ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2497ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2498ad675b32SBjoern A. Zeeb 	}
2499ad675b32SBjoern A. Zeeb }
2500ad675b32SBjoern A. Zeeb 
2501ad675b32SBjoern A. Zeeb void
nd6_defrouter_flush_all(void)2502ad675b32SBjoern A. Zeeb nd6_defrouter_flush_all(void)
2503ad675b32SBjoern A. Zeeb {
2504ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
2505f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2506ad675b32SBjoern A. Zeeb 
2507ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2508ad675b32SBjoern A. Zeeb 
2509ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2510f592d0c3SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&V_nd6_defrouter)) != NULL)
2511ad675b32SBjoern A. Zeeb 		defrouter_unlink(dr, &drq);
2512ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2513ad675b32SBjoern A. Zeeb 
2514ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2515ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2516ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2517ad675b32SBjoern A. Zeeb 	}
2518ad675b32SBjoern A. Zeeb }
2519ad675b32SBjoern A. Zeeb 
2520ad675b32SBjoern A. Zeeb void
nd6_defrouter_init(void)2521ad675b32SBjoern A. Zeeb nd6_defrouter_init(void)
2522ad675b32SBjoern A. Zeeb {
2523ad675b32SBjoern A. Zeeb 
2524f592d0c3SBjoern A. Zeeb 	TAILQ_INIT(&V_nd6_defrouter);
2525ad675b32SBjoern A. Zeeb }
2526f77a6dbdSBjoern A. Zeeb 
2527f77a6dbdSBjoern A. Zeeb static int
nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)2528f77a6dbdSBjoern A. Zeeb nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2529f77a6dbdSBjoern A. Zeeb {
2530f77a6dbdSBjoern A. Zeeb 	struct in6_defrouter d;
2531f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
2532f77a6dbdSBjoern A. Zeeb 	int error;
2533f77a6dbdSBjoern A. Zeeb 
2534f77a6dbdSBjoern A. Zeeb 	if (req->newptr != NULL)
2535f77a6dbdSBjoern A. Zeeb 		return (EPERM);
2536f77a6dbdSBjoern A. Zeeb 
2537f77a6dbdSBjoern A. Zeeb 	error = sysctl_wire_old_buffer(req, 0);
2538f77a6dbdSBjoern A. Zeeb 	if (error != 0)
2539f77a6dbdSBjoern A. Zeeb 		return (error);
2540f77a6dbdSBjoern A. Zeeb 
2541f77a6dbdSBjoern A. Zeeb 	bzero(&d, sizeof(d));
2542f77a6dbdSBjoern A. Zeeb 	d.rtaddr.sin6_family = AF_INET6;
2543f77a6dbdSBjoern A. Zeeb 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
2544f77a6dbdSBjoern A. Zeeb 
2545f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
2546f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
2547f77a6dbdSBjoern A. Zeeb 		d.rtaddr.sin6_addr = dr->rtaddr;
2548f77a6dbdSBjoern A. Zeeb 		error = sa6_recoverscope(&d.rtaddr);
2549f77a6dbdSBjoern A. Zeeb 		if (error != 0)
2550f77a6dbdSBjoern A. Zeeb 			break;
2551f77a6dbdSBjoern A. Zeeb 		d.flags = dr->raflags;
2552f77a6dbdSBjoern A. Zeeb 		d.rtlifetime = dr->rtlifetime;
2553f77a6dbdSBjoern A. Zeeb 		d.expire = dr->expire + (time_second - time_uptime);
2554f77a6dbdSBjoern A. Zeeb 		d.if_index = dr->ifp->if_index;
2555f77a6dbdSBjoern A. Zeeb 		error = SYSCTL_OUT(req, &d, sizeof(d));
2556f77a6dbdSBjoern A. Zeeb 		if (error != 0)
2557f77a6dbdSBjoern A. Zeeb 			break;
2558f77a6dbdSBjoern A. Zeeb 	}
2559f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
2560f77a6dbdSBjoern A. Zeeb 	return (error);
2561f77a6dbdSBjoern A. Zeeb }
2562f77a6dbdSBjoern A. Zeeb SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2563f77a6dbdSBjoern A. Zeeb 	CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2564f77a6dbdSBjoern A. Zeeb 	NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2565f77a6dbdSBjoern A. Zeeb 	"NDP default router list");
2566