xref: /freebsd/sys/netinet6/nd6_rtr.c (revision f77a6dbd1ec9e1307189ab3c68b9a8c7fcb40ace)
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>
35b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
36b48287a3SDavid E. O'Brien 
37686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
38686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
39686cdd19SJun-ichiro itojun Hagino 
4082cd038dSYoshinobu Inoue #include <sys/param.h>
4182cd038dSYoshinobu Inoue #include <sys/systm.h>
4282cd038dSYoshinobu Inoue #include <sys/malloc.h>
4382cd038dSYoshinobu Inoue #include <sys/mbuf.h>
444de485feSMark Johnston #include <sys/refcount.h>
4582cd038dSYoshinobu Inoue #include <sys/socket.h>
4682cd038dSYoshinobu Inoue #include <sys/sockio.h>
4782cd038dSYoshinobu Inoue #include <sys/time.h>
4833841545SHajimu UMEMOTO #include <sys/kernel.h>
49609ff41fSWarner Losh #include <sys/lock.h>
5082cd038dSYoshinobu Inoue #include <sys/errno.h>
511b28988bSMark Johnston #include <sys/rmlock.h>
523120b9d4SKip Macy #include <sys/rwlock.h>
53ad675b32SBjoern A. Zeeb #include <sys/sysctl.h>
5482cd038dSYoshinobu Inoue #include <sys/syslog.h>
5533841545SHajimu UMEMOTO #include <sys/queue.h>
5682cd038dSYoshinobu Inoue 
5782cd038dSYoshinobu Inoue #include <net/if.h>
5876039bc8SGleb Smirnoff #include <net/if_var.h>
5982cd038dSYoshinobu Inoue #include <net/if_types.h>
6082cd038dSYoshinobu Inoue #include <net/if_dl.h>
6182cd038dSYoshinobu Inoue #include <net/route.h>
6261eee0e2SAlexander V. Chernikov #include <net/route_var.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);
7982cd038dSYoshinobu Inoue 
80f592d0c3SBjoern A. Zeeb TAILQ_HEAD(nd6_drhead, nd_defrouter);
81f592d0c3SBjoern A. Zeeb VNET_DEFINE_STATIC(struct nd6_drhead, nd6_defrouter);
82f592d0c3SBjoern A. Zeeb #define	V_nd6_defrouter			VNET(nd6_defrouter)
83ad675b32SBjoern A. Zeeb 
84eddfbb76SRobert Watson VNET_DECLARE(int, nd6_recalc_reachtm_interval);
851e77c105SRobert Watson #define	V_nd6_recalc_reachtm_interval	VNET(nd6_recalc_reachtm_interval)
8682cd038dSYoshinobu Inoue 
875f901c92SAndrew Turner VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
8882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_defifindex);
891e77c105SRobert Watson #define	V_nd6_defifp			VNET(nd6_defifp)
90686cdd19SJun-ichiro itojun Hagino 
9182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_use_tempaddr) = 0;
9282cea7e6SBjoern A. Zeeb 
93eddfbb76SRobert Watson VNET_DEFINE(int, ip6_desync_factor);
9482cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
9582cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
9682cea7e6SBjoern A. Zeeb 
9782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
9833841545SHajimu UMEMOTO 
9921231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL
10021231a7aSBjoern A. Zeeb VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1;
10121231a7aSBjoern A. Zeeb #endif
10221231a7aSBjoern A. Zeeb 
103*f77a6dbdSBjoern A. Zeeb SYSCTL_DECL(_net_inet6_icmp6);
104*f77a6dbdSBjoern A. Zeeb 
105743eee66SSUZUKI Shinsuke /* RTPREF_MEDIUM has to be 0! */
106743eee66SSUZUKI Shinsuke #define RTPREF_HIGH	1
107743eee66SSUZUKI Shinsuke #define RTPREF_MEDIUM	0
108743eee66SSUZUKI Shinsuke #define RTPREF_LOW	(-1)
109743eee66SSUZUKI Shinsuke #define RTPREF_RESERVED	(-2)
110743eee66SSUZUKI Shinsuke #define RTPREF_INVALID	(-3)	/* internal */
111743eee66SSUZUKI Shinsuke 
112ad675b32SBjoern A. Zeeb static void
113ad675b32SBjoern A. Zeeb defrouter_ref(struct nd_defrouter *dr)
114ad675b32SBjoern A. Zeeb {
115ad675b32SBjoern A. Zeeb 
116ad675b32SBjoern A. Zeeb 	refcount_acquire(&dr->refcnt);
117ad675b32SBjoern A. Zeeb }
118ad675b32SBjoern A. Zeeb 
119ad675b32SBjoern A. Zeeb void
120ad675b32SBjoern A. Zeeb defrouter_rele(struct nd_defrouter *dr)
121ad675b32SBjoern A. Zeeb {
122ad675b32SBjoern A. Zeeb 
123ad675b32SBjoern A. Zeeb 	if (refcount_release(&dr->refcnt))
124ad675b32SBjoern A. Zeeb 		free(dr, M_IP6NDP);
125ad675b32SBjoern A. Zeeb }
126ad675b32SBjoern A. Zeeb 
127ad675b32SBjoern A. Zeeb /*
128ad675b32SBjoern A. Zeeb  * Remove a router from the global list and optionally stash it in a
129ad675b32SBjoern A. Zeeb  * caller-supplied queue.
130ad675b32SBjoern A. Zeeb  */
131ad675b32SBjoern A. Zeeb static void
132f592d0c3SBjoern A. Zeeb defrouter_unlink(struct nd_defrouter *dr, struct nd6_drhead *drq)
133ad675b32SBjoern A. Zeeb {
134ad675b32SBjoern A. Zeeb 
135ad675b32SBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
136ad675b32SBjoern A. Zeeb 
137f592d0c3SBjoern A. Zeeb 	TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
138ad675b32SBjoern A. Zeeb 	V_nd6_list_genid++;
139ad675b32SBjoern A. Zeeb 	if (drq != NULL)
140ad675b32SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(drq, dr, dr_entry);
141ad675b32SBjoern A. Zeeb }
142ad675b32SBjoern A. Zeeb 
14382cd038dSYoshinobu Inoue /*
14482cd038dSYoshinobu Inoue  * Receive Router Solicitation Message - just for routers.
14582cd038dSYoshinobu Inoue  * Router solicitation/advertisement is mostly managed by userland program
14682cd038dSYoshinobu Inoue  * (rtadvd) so here we have no function like nd6_ra_output().
14782cd038dSYoshinobu Inoue  *
14882cd038dSYoshinobu Inoue  * Based on RFC 2461
14982cd038dSYoshinobu Inoue  */
15082cd038dSYoshinobu Inoue void
1511272577eSXin LI nd6_rs_input(struct mbuf *m, int off, int icmp6len)
15282cd038dSYoshinobu Inoue {
153503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
154503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
155686cdd19SJun-ichiro itojun Hagino 	struct nd_router_solicit *nd_rs;
156503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
15782cd038dSYoshinobu Inoue 	union nd_opts ndopts;
1581d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
159503f4e47SBjoern A. Zeeb 	char *lladdr;
160503f4e47SBjoern A. Zeeb 	int lladdrlen;
161503f4e47SBjoern A. Zeeb 
162503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
16382cd038dSYoshinobu Inoue 
164e7fa8d0aSHiroki Sato 	/*
165e7fa8d0aSHiroki Sato 	 * Accept RS only when V_ip6_forwarding=1 and the interface has
166e7fa8d0aSHiroki Sato 	 * no ND6_IFF_ACCEPT_RTADV.
167e7fa8d0aSHiroki Sato 	 */
168e7fa8d0aSHiroki Sato 	if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
169686cdd19SJun-ichiro itojun Hagino 		goto freeit;
17082cd038dSYoshinobu Inoue 
171a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
172a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
173a4061289SAndrey V. Elsukov 		goto freeit;
174a4061289SAndrey V. Elsukov 
17582cd038dSYoshinobu Inoue 	/* Sanity checks */
176503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
17782cd038dSYoshinobu Inoue 	if (ip6->ip6_hlim != 255) {
17833841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
17933841545SHajimu UMEMOTO 		    "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
1801d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
1811d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
18233841545SHajimu UMEMOTO 		goto bad;
18382cd038dSYoshinobu Inoue 	}
18482cd038dSYoshinobu Inoue 
18582cd038dSYoshinobu Inoue 	/*
18682cd038dSYoshinobu Inoue 	 * Don't update the neighbor cache, if src = ::.
18782cd038dSYoshinobu Inoue 	 * This indicates that the src has no IP address assigned yet.
18882cd038dSYoshinobu Inoue 	 */
189503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
19082cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
191686cdd19SJun-ichiro itojun Hagino 		goto freeit;
192686cdd19SJun-ichiro itojun Hagino 
193a61b5cfbSBjoern A. Zeeb 	m = m_pullup(m, off + icmp6len);
194a61b5cfbSBjoern A. Zeeb 	if (m == NULL) {
195a61b5cfbSBjoern A. Zeeb 		IP6STAT_INC(ip6s_exthdrtoolong);
19682cd038dSYoshinobu Inoue 		return;
197686cdd19SJun-ichiro itojun Hagino 	}
198a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
199a61b5cfbSBjoern A. Zeeb 	nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
20082cd038dSYoshinobu Inoue 
20182cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_rs);
20282cd038dSYoshinobu Inoue 	nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
20382cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
20433841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
20533841545SHajimu UMEMOTO 		    "nd6_rs_input: invalid ND option, ignored\n"));
20633841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
207686cdd19SJun-ichiro itojun Hagino 		goto freeit;
20882cd038dSYoshinobu Inoue 	}
20982cd038dSYoshinobu Inoue 
210503f4e47SBjoern A. Zeeb 	lladdr = NULL;
211503f4e47SBjoern A. Zeeb 	lladdrlen = 0;
21282cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
21382cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
21482cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
21582cd038dSYoshinobu Inoue 	}
21682cd038dSYoshinobu Inoue 
21782cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
21833841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
21982cd038dSYoshinobu Inoue 		    "nd6_rs_input: lladdrlen mismatch for %s "
22082cd038dSYoshinobu Inoue 		    "(if %d, RS packet %d)\n",
2211d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6),
22207eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
22333841545SHajimu UMEMOTO 		goto bad;
22482cd038dSYoshinobu Inoue 	}
22582cd038dSYoshinobu Inoue 
22682cd038dSYoshinobu Inoue 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
227686cdd19SJun-ichiro itojun Hagino 
228686cdd19SJun-ichiro itojun Hagino  freeit:
229686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
23033841545SHajimu UMEMOTO 	return;
23133841545SHajimu UMEMOTO 
23233841545SHajimu UMEMOTO  bad:
233e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badrs);
23433841545SHajimu UMEMOTO 	m_freem(m);
23582cd038dSYoshinobu Inoue }
23682cd038dSYoshinobu Inoue 
237201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
238201100c5SBjoern A. Zeeb /*
239201100c5SBjoern A. Zeeb  * An initial update routine for draft-ietf-6man-ipv6only-flag.
240201100c5SBjoern A. Zeeb  * We need to iterate over all default routers for the given
24121231a7aSBjoern A. Zeeb  * interface to see whether they are all advertising the "S"
242201100c5SBjoern A. Zeeb  * (IPv6-Only) flag.  If they do set, otherwise unset, the
243201100c5SBjoern A. Zeeb  * interface flag we later use to filter on.
244201100c5SBjoern A. Zeeb  */
245201100c5SBjoern A. Zeeb static void
246201100c5SBjoern A. Zeeb defrtr_ipv6_only_ifp(struct ifnet *ifp)
247201100c5SBjoern A. Zeeb {
248201100c5SBjoern A. Zeeb 	struct nd_defrouter *dr;
24921231a7aSBjoern A. Zeeb 	bool ipv6_only, ipv6_only_old;
25021231a7aSBjoern A. Zeeb #ifdef INET
25121231a7aSBjoern A. Zeeb 	struct epoch_tracker et;
25221231a7aSBjoern A. Zeeb 	struct ifaddr *ifa;
25321231a7aSBjoern A. Zeeb 	bool has_ipv4_addr;
25421231a7aSBjoern A. Zeeb #endif
25521231a7aSBjoern A. Zeeb 
25621231a7aSBjoern A. Zeeb 	if (V_nd6_ignore_ipv6_only_ra != 0)
25721231a7aSBjoern A. Zeeb 		return;
258201100c5SBjoern A. Zeeb 
259201100c5SBjoern A. Zeeb 	ipv6_only = true;
260201100c5SBjoern A. Zeeb 	ND6_RLOCK();
261f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
262201100c5SBjoern A. Zeeb 		if (dr->ifp == ifp &&
263201100c5SBjoern A. Zeeb 		    (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0)
264201100c5SBjoern A. Zeeb 			ipv6_only = false;
265201100c5SBjoern A. Zeeb 	ND6_RUNLOCK();
266201100c5SBjoern A. Zeeb 
267201100c5SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
26821231a7aSBjoern A. Zeeb 	ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY;
26921231a7aSBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
27021231a7aSBjoern A. Zeeb 
27121231a7aSBjoern A. Zeeb 	/* If nothing changed, we have an early exit. */
27221231a7aSBjoern A. Zeeb 	if (ipv6_only == ipv6_only_old)
27321231a7aSBjoern A. Zeeb 		return;
27421231a7aSBjoern A. Zeeb 
27521231a7aSBjoern A. Zeeb #ifdef INET
27621231a7aSBjoern A. Zeeb 	/*
27721231a7aSBjoern A. Zeeb 	 * Should we want to set the IPV6-ONLY flag, check if the
27821231a7aSBjoern A. Zeeb 	 * interface has a non-0/0 and non-link-local IPv4 address
27921231a7aSBjoern A. Zeeb 	 * configured on it.  If it has we will assume working
28021231a7aSBjoern A. Zeeb 	 * IPv4 operations and will clear the interface flag.
28121231a7aSBjoern A. Zeeb 	 */
28221231a7aSBjoern A. Zeeb 	has_ipv4_addr = false;
28321231a7aSBjoern A. Zeeb 	if (ipv6_only) {
28421231a7aSBjoern A. Zeeb 		NET_EPOCH_ENTER(et);
28521231a7aSBjoern A. Zeeb 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
28621231a7aSBjoern A. Zeeb 			if (ifa->ifa_addr->sa_family != AF_INET)
28721231a7aSBjoern A. Zeeb 				continue;
28821231a7aSBjoern A. Zeeb 			if (in_canforward(
28921231a7aSBjoern A. Zeeb 			    satosin(ifa->ifa_addr)->sin_addr)) {
29021231a7aSBjoern A. Zeeb 				has_ipv4_addr = true;
29121231a7aSBjoern A. Zeeb 				break;
29221231a7aSBjoern A. Zeeb 			}
29321231a7aSBjoern A. Zeeb 		}
29421231a7aSBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
29521231a7aSBjoern A. Zeeb 	}
29621231a7aSBjoern A. Zeeb 	if (ipv6_only && has_ipv4_addr) {
29721231a7aSBjoern A. Zeeb 		log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
29821231a7aSBjoern A. Zeeb 		    "configured, ignoring IPv6-Only flag.\n", ifp->if_xname);
29921231a7aSBjoern A. Zeeb 		ipv6_only = false;
30021231a7aSBjoern A. Zeeb 	}
30121231a7aSBjoern A. Zeeb #endif
30221231a7aSBjoern A. Zeeb 
30321231a7aSBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
304201100c5SBjoern A. Zeeb 	if (ipv6_only)
305201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY;
306201100c5SBjoern A. Zeeb 	else
307201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
308201100c5SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
30921231a7aSBjoern A. Zeeb 
31021231a7aSBjoern A. Zeeb #ifdef notyet
31121231a7aSBjoern A. Zeeb 	/* Send notification of flag change. */
312201100c5SBjoern A. Zeeb #endif
31321231a7aSBjoern A. Zeeb }
31430b45077SBjoern A. Zeeb 
31530b45077SBjoern A. Zeeb static void
31630b45077SBjoern A. Zeeb defrtr_ipv6_only_ipf_down(struct ifnet *ifp)
31730b45077SBjoern A. Zeeb {
31830b45077SBjoern A. Zeeb 
31930b45077SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
32030b45077SBjoern A. Zeeb 	ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
32130b45077SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
32230b45077SBjoern A. Zeeb }
32321231a7aSBjoern A. Zeeb #endif	/* EXPERIMENTAL */
324201100c5SBjoern A. Zeeb 
32530b45077SBjoern A. Zeeb void
32630b45077SBjoern A. Zeeb nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate)
32730b45077SBjoern A. Zeeb {
32830b45077SBjoern A. Zeeb 
32930b45077SBjoern A. Zeeb 	/*
33030b45077SBjoern A. Zeeb 	 * XXX-BZ we might want to trigger re-evaluation of our default router
33130b45077SBjoern A. Zeeb 	 * availability. E.g., on link down the default router might be
33230b45077SBjoern A. Zeeb 	 * unreachable but a different interface might still have connectivity.
33330b45077SBjoern A. Zeeb 	 */
33430b45077SBjoern A. Zeeb 
33530b45077SBjoern A. Zeeb #ifdef EXPERIMENTAL
33630b45077SBjoern A. Zeeb 	if (linkstate == LINK_STATE_DOWN)
33730b45077SBjoern A. Zeeb 		defrtr_ipv6_only_ipf_down(ifp);
33830b45077SBjoern A. Zeeb #endif
33930b45077SBjoern A. Zeeb }
34030b45077SBjoern A. Zeeb 
34182cd038dSYoshinobu Inoue /*
34282cd038dSYoshinobu Inoue  * Receive Router Advertisement Message.
34382cd038dSYoshinobu Inoue  *
34482cd038dSYoshinobu Inoue  * Based on RFC 2461
34582cd038dSYoshinobu Inoue  * TODO: on-link bit on prefix information
34682cd038dSYoshinobu Inoue  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
34782cd038dSYoshinobu Inoue  */
34882cd038dSYoshinobu Inoue void
3491272577eSXin LI nd6_ra_input(struct mbuf *m, int off, int icmp6len)
35082cd038dSYoshinobu Inoue {
351503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
352503f4e47SBjoern A. Zeeb 	struct nd_ifinfo *ndi;
353503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
354686cdd19SJun-ichiro itojun Hagino 	struct nd_router_advert *nd_ra;
355503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
35682cd038dSYoshinobu Inoue 	struct nd_defrouter *dr;
357503f4e47SBjoern A. Zeeb 	union nd_opts ndopts;
3581d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
359503f4e47SBjoern A. Zeeb 	int mcast;
3604de485feSMark Johnston 
36107cf047dSHajimu UMEMOTO 	/*
362e7fa8d0aSHiroki Sato 	 * We only accept RAs only when the per-interface flag
363e7fa8d0aSHiroki Sato 	 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
36407cf047dSHajimu UMEMOTO 	 */
365503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
366503f4e47SBjoern A. Zeeb 	ndi = ND_IFINFO(ifp);
367e7fa8d0aSHiroki Sato 	if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
36807cf047dSHajimu UMEMOTO 		goto freeit;
36982cd038dSYoshinobu Inoue 
370a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
371a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
372a4061289SAndrey V. Elsukov 		goto freeit;
373a4061289SAndrey V. Elsukov 
374503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
37582cd038dSYoshinobu Inoue 	if (ip6->ip6_hlim != 255) {
37633841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
37733841545SHajimu UMEMOTO 		    "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
3781d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
3791d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
38033841545SHajimu UMEMOTO 		goto bad;
38182cd038dSYoshinobu Inoue 	}
38282cd038dSYoshinobu Inoue 
383503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
38482cd038dSYoshinobu Inoue 	if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
38533841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
38682cd038dSYoshinobu Inoue 		    "nd6_ra_input: src %s is not link-local\n",
3871d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6)));
38833841545SHajimu UMEMOTO 		goto bad;
389686cdd19SJun-ichiro itojun Hagino 	}
390686cdd19SJun-ichiro itojun Hagino 
391a61b5cfbSBjoern A. Zeeb 	m = m_pullup(m, off + icmp6len);
392a61b5cfbSBjoern A. Zeeb 	if (m == NULL) {
393a61b5cfbSBjoern A. Zeeb 		IP6STAT_INC(ip6s_exthdrtoolong);
39482cd038dSYoshinobu Inoue 		return;
39582cd038dSYoshinobu Inoue 	}
396a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
397a61b5cfbSBjoern A. Zeeb 	nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
39882cd038dSYoshinobu Inoue 
39982cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_ra);
40082cd038dSYoshinobu Inoue 	nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
40182cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
40233841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
40333841545SHajimu UMEMOTO 		    "nd6_ra_input: invalid ND option, ignored\n"));
40433841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
405686cdd19SJun-ichiro itojun Hagino 		goto freeit;
40682cd038dSYoshinobu Inoue 	}
40782cd038dSYoshinobu Inoue 
408503f4e47SBjoern A. Zeeb 	mcast = 0;
409503f4e47SBjoern A. Zeeb 	dr = NULL;
41082cd038dSYoshinobu Inoue     {
41182cd038dSYoshinobu Inoue 	struct nd_defrouter dr0;
41282cd038dSYoshinobu Inoue 	u_int32_t advreachable = nd_ra->nd_ra_reachable;
41382cd038dSYoshinobu Inoue 
414743eee66SSUZUKI Shinsuke 	/* remember if this is a multicasted advertisement */
415743eee66SSUZUKI Shinsuke 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
416743eee66SSUZUKI Shinsuke 		mcast = 1;
417743eee66SSUZUKI Shinsuke 
418743eee66SSUZUKI Shinsuke 	bzero(&dr0, sizeof(dr0));
41982cd038dSYoshinobu Inoue 	dr0.rtaddr = saddr6;
42001869be5SMark Johnston 	dr0.raflags = nd_ra->nd_ra_flags_reserved;
421e7fa8d0aSHiroki Sato 	/*
422049087a0SHiroki Sato 	 * Effectively-disable routes from RA messages when
423049087a0SHiroki Sato 	 * ND6_IFF_NO_RADR enabled on the receiving interface or
424049087a0SHiroki Sato 	 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
425e7fa8d0aSHiroki Sato 	 */
426049087a0SHiroki Sato 	if (ndi->flags & ND6_IFF_NO_RADR)
427049087a0SHiroki Sato 		dr0.rtlifetime = 0;
428049087a0SHiroki Sato 	else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
429e7fa8d0aSHiroki Sato 		dr0.rtlifetime = 0;
430e7fa8d0aSHiroki Sato 	else
43182cd038dSYoshinobu Inoue 		dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
4327d26db17SHiroki Sato 	dr0.expire = time_uptime + dr0.rtlifetime;
43382cd038dSYoshinobu Inoue 	dr0.ifp = ifp;
43482cd038dSYoshinobu Inoue 	/* unspecified or not? (RFC 2461 6.3.4) */
43582cd038dSYoshinobu Inoue 	if (advreachable) {
436fd8e4ebcSMike Barcroft 		advreachable = ntohl(advreachable);
43782cd038dSYoshinobu Inoue 		if (advreachable <= MAX_REACHABLE_TIME &&
43882cd038dSYoshinobu Inoue 		    ndi->basereachable != advreachable) {
43982cd038dSYoshinobu Inoue 			ndi->basereachable = advreachable;
44082cd038dSYoshinobu Inoue 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
441603724d3SBjoern A. Zeeb 			ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
44282cd038dSYoshinobu Inoue 		}
44382cd038dSYoshinobu Inoue 	}
44482cd038dSYoshinobu Inoue 	if (nd_ra->nd_ra_retransmit)
44582cd038dSYoshinobu Inoue 		ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
446dd385660SXin LI 	if (nd_ra->nd_ra_curhoplimit) {
447dd385660SXin LI 		if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
44882cd038dSYoshinobu Inoue 			ndi->chlim = nd_ra->nd_ra_curhoplimit;
449dd385660SXin LI 		else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
450dd385660SXin LI 			log(LOG_ERR, "RA with a lower CurHopLimit sent from "
451dd385660SXin LI 			    "%s on %s (current = %d, received = %d). "
452dd385660SXin LI 			    "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
453dd385660SXin LI 			    if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
454dd385660SXin LI 		}
455dd385660SXin LI 	}
45682cd038dSYoshinobu Inoue 	dr = defrtrlist_update(&dr0);
457201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
458201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(ifp);
459201100c5SBjoern A. Zeeb #endif
46082cd038dSYoshinobu Inoue     }
46182cd038dSYoshinobu Inoue 
46282cd038dSYoshinobu Inoue 	/*
46382cd038dSYoshinobu Inoue 	 * prefix
46482cd038dSYoshinobu Inoue 	 */
46582cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_pi) {
46682cd038dSYoshinobu Inoue 		struct nd_opt_hdr *pt;
46733841545SHajimu UMEMOTO 		struct nd_opt_prefix_info *pi = NULL;
468743eee66SSUZUKI Shinsuke 		struct nd_prefixctl pr;
46982cd038dSYoshinobu Inoue 
47082cd038dSYoshinobu Inoue 		for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
47182cd038dSYoshinobu Inoue 		     pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
47282cd038dSYoshinobu Inoue 		     pt = (struct nd_opt_hdr *)((caddr_t)pt +
47382cd038dSYoshinobu Inoue 						(pt->nd_opt_len << 3))) {
47482cd038dSYoshinobu Inoue 			if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
47582cd038dSYoshinobu Inoue 				continue;
47682cd038dSYoshinobu Inoue 			pi = (struct nd_opt_prefix_info *)pt;
47782cd038dSYoshinobu Inoue 
47882cd038dSYoshinobu Inoue 			if (pi->nd_opt_pi_len != 4) {
47933841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
48033841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid option "
48182cd038dSYoshinobu Inoue 				    "len %d for prefix information option, "
48233841545SHajimu UMEMOTO 				    "ignored\n", pi->nd_opt_pi_len));
48382cd038dSYoshinobu Inoue 				continue;
48482cd038dSYoshinobu Inoue 			}
48582cd038dSYoshinobu Inoue 
48682cd038dSYoshinobu Inoue 			if (128 < pi->nd_opt_pi_prefix_len) {
48733841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
48833841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid prefix "
48982cd038dSYoshinobu Inoue 				    "len %d for prefix information option, "
49033841545SHajimu UMEMOTO 				    "ignored\n", pi->nd_opt_pi_prefix_len));
49182cd038dSYoshinobu Inoue 				continue;
49282cd038dSYoshinobu Inoue 			}
49382cd038dSYoshinobu Inoue 
49482cd038dSYoshinobu Inoue 			if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
49582cd038dSYoshinobu Inoue 			 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
49633841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
49733841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid prefix "
49882cd038dSYoshinobu Inoue 				    "%s, ignored\n",
4991d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6bufs,
5001d54aa3bSBjoern A. Zeeb 					&pi->nd_opt_pi_prefix)));
50182cd038dSYoshinobu Inoue 				continue;
50282cd038dSYoshinobu Inoue 			}
50382cd038dSYoshinobu Inoue 
50482cd038dSYoshinobu Inoue 			bzero(&pr, sizeof(pr));
50582cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_family = AF_INET6;
50682cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
50782cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
50882cd038dSYoshinobu Inoue 			pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
50982cd038dSYoshinobu Inoue 
51082cd038dSYoshinobu Inoue 			pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
51182cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
51282cd038dSYoshinobu Inoue 			pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
51382cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
51482cd038dSYoshinobu Inoue 			pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
51582cd038dSYoshinobu Inoue 			pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
51607eb2995SHajimu UMEMOTO 			pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
517743eee66SSUZUKI Shinsuke 			(void)prelist_update(&pr, dr, m, mcast);
51882cd038dSYoshinobu Inoue 		}
51982cd038dSYoshinobu Inoue 	}
5204de485feSMark Johnston 	if (dr != NULL) {
5214de485feSMark Johnston 		defrouter_rele(dr);
5224de485feSMark Johnston 		dr = NULL;
5234de485feSMark Johnston 	}
52482cd038dSYoshinobu Inoue 
52582cd038dSYoshinobu Inoue 	/*
52682cd038dSYoshinobu Inoue 	 * MTU
52782cd038dSYoshinobu Inoue 	 */
52882cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
52931b3783cSHajimu UMEMOTO 		u_long mtu;
53031b3783cSHajimu UMEMOTO 		u_long maxmtu;
53107eb2995SHajimu UMEMOTO 
53231b3783cSHajimu UMEMOTO 		mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
53382cd038dSYoshinobu Inoue 
53482cd038dSYoshinobu Inoue 		/* lower bound */
53582cd038dSYoshinobu Inoue 		if (mtu < IPV6_MMTU) {
53633841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
53731b3783cSHajimu UMEMOTO 			    "mtu=%lu sent from %s, ignoring\n",
5381d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
53982cd038dSYoshinobu Inoue 			goto skip;
54082cd038dSYoshinobu Inoue 		}
54182cd038dSYoshinobu Inoue 
54282cd038dSYoshinobu Inoue 		/* upper bound */
54331b3783cSHajimu UMEMOTO 		maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
54431b3783cSHajimu UMEMOTO 		    ? ndi->maxmtu : ifp->if_mtu;
54531b3783cSHajimu UMEMOTO 		if (mtu <= maxmtu) {
54682cd038dSYoshinobu Inoue 			int change = (ndi->linkmtu != mtu);
54782cd038dSYoshinobu Inoue 
54882cd038dSYoshinobu Inoue 			ndi->linkmtu = mtu;
5490bbfb20fSEric van Gyzen 			if (change) {
5500bbfb20fSEric van Gyzen 				/* in6_maxmtu may change */
55182cd038dSYoshinobu Inoue 				in6_setmaxmtu();
5520bbfb20fSEric van Gyzen 				rt_updatemtu(ifp);
5530bbfb20fSEric van Gyzen 			}
55482cd038dSYoshinobu Inoue 		} else {
55533841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
55631b3783cSHajimu UMEMOTO 			    "mtu=%lu sent from %s; "
55731b3783cSHajimu UMEMOTO 			    "exceeds maxmtu %lu, ignoring\n",
5581d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
55982cd038dSYoshinobu Inoue 		}
56082cd038dSYoshinobu Inoue 	}
56182cd038dSYoshinobu Inoue 
56282cd038dSYoshinobu Inoue  skip:
56382cd038dSYoshinobu Inoue 
56482cd038dSYoshinobu Inoue 	/*
56588ff5695SSUZUKI Shinsuke 	 * Source link layer address
56682cd038dSYoshinobu Inoue 	 */
56782cd038dSYoshinobu Inoue     {
56882cd038dSYoshinobu Inoue 	char *lladdr = NULL;
56982cd038dSYoshinobu Inoue 	int lladdrlen = 0;
57082cd038dSYoshinobu Inoue 
57182cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
57282cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
57382cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
57482cd038dSYoshinobu Inoue 	}
57582cd038dSYoshinobu Inoue 
57682cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
57733841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
57882cd038dSYoshinobu Inoue 		    "nd6_ra_input: lladdrlen mismatch for %s "
5791d54aa3bSBjoern A. Zeeb 		    "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
58007eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
58133841545SHajimu UMEMOTO 		goto bad;
58282cd038dSYoshinobu Inoue 	}
58382cd038dSYoshinobu Inoue 
58407eb2995SHajimu UMEMOTO 	nd6_cache_lladdr(ifp, &saddr6, lladdr,
58507eb2995SHajimu UMEMOTO 	    lladdrlen, ND_ROUTER_ADVERT, 0);
586686cdd19SJun-ichiro itojun Hagino 
587686cdd19SJun-ichiro itojun Hagino 	/*
588686cdd19SJun-ichiro itojun Hagino 	 * Installing a link-layer address might change the state of the
589686cdd19SJun-ichiro itojun Hagino 	 * router's neighbor cache, which might also affect our on-link
590686cdd19SJun-ichiro itojun Hagino 	 * detection of adveritsed prefixes.
591686cdd19SJun-ichiro itojun Hagino 	 */
592686cdd19SJun-ichiro itojun Hagino 	pfxlist_onlink_check();
59382cd038dSYoshinobu Inoue     }
594686cdd19SJun-ichiro itojun Hagino 
595686cdd19SJun-ichiro itojun Hagino  freeit:
596686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
59733841545SHajimu UMEMOTO 	return;
59833841545SHajimu UMEMOTO 
59933841545SHajimu UMEMOTO  bad:
600e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badra);
60133841545SHajimu UMEMOTO 	m_freem(m);
60282cd038dSYoshinobu Inoue }
60382cd038dSYoshinobu Inoue 
604686cdd19SJun-ichiro itojun Hagino /* tell the change to user processes watching the routing socket. */
605686cdd19SJun-ichiro itojun Hagino static void
6061272577eSXin LI nd6_rtmsg(int cmd, struct rtentry *rt)
607686cdd19SJun-ichiro itojun Hagino {
608686cdd19SJun-ichiro itojun Hagino 	struct rt_addrinfo info;
6091e6a4139SRobert Watson 	struct ifnet *ifp;
6108c0fec80SRobert Watson 	struct ifaddr *ifa;
611686cdd19SJun-ichiro itojun Hagino 
612686cdd19SJun-ichiro itojun Hagino 	bzero((caddr_t)&info, sizeof(info));
613686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_DST] = rt_key(rt);
614686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
615686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
6161e6a4139SRobert Watson 	ifp = rt->rt_ifp;
6171e6a4139SRobert Watson 	if (ifp != NULL) {
618a68cc388SGleb Smirnoff 		struct epoch_tracker et;
619a68cc388SGleb Smirnoff 
620a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
621d7c5a620SMatt Macy 		ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
6228c0fec80SRobert Watson 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
6238c0fec80SRobert Watson 		ifa_ref(ifa);
624a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
62533841545SHajimu UMEMOTO 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
6268c0fec80SRobert Watson 	} else
6278c0fec80SRobert Watson 		ifa = NULL;
628686cdd19SJun-ichiro itojun Hagino 
62981d5d46bSBjoern A. Zeeb 	rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
6308c0fec80SRobert Watson 	if (ifa != NULL)
6318c0fec80SRobert Watson 		ifa_free(ifa);
632686cdd19SJun-ichiro itojun Hagino }
633686cdd19SJun-ichiro itojun Hagino 
634*f77a6dbdSBjoern A. Zeeb /* PFXRTR */
635*f77a6dbdSBjoern A. Zeeb static struct nd_pfxrouter *
636*f77a6dbdSBjoern A. Zeeb pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
637*f77a6dbdSBjoern A. Zeeb {
638*f77a6dbdSBjoern A. Zeeb 	struct nd_pfxrouter *search;
6394de485feSMark Johnston 
640*f77a6dbdSBjoern A. Zeeb 	ND6_LOCK_ASSERT();
641*f77a6dbdSBjoern A. Zeeb 
642*f77a6dbdSBjoern A. Zeeb 	LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
643*f77a6dbdSBjoern A. Zeeb 		if (search->router == dr)
644*f77a6dbdSBjoern A. Zeeb 			break;
645*f77a6dbdSBjoern A. Zeeb 	}
646*f77a6dbdSBjoern A. Zeeb 	return (search);
647*f77a6dbdSBjoern A. Zeeb }
648*f77a6dbdSBjoern A. Zeeb 
649*f77a6dbdSBjoern A. Zeeb static void
650*f77a6dbdSBjoern A. Zeeb pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
651*f77a6dbdSBjoern A. Zeeb {
652*f77a6dbdSBjoern A. Zeeb 	struct nd_pfxrouter *new;
653*f77a6dbdSBjoern A. Zeeb 	bool update;
654*f77a6dbdSBjoern A. Zeeb 
655*f77a6dbdSBjoern A. Zeeb 	ND6_UNLOCK_ASSERT();
656*f77a6dbdSBjoern A. Zeeb 
657*f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
658*f77a6dbdSBjoern A. Zeeb 	if (pfxrtr_lookup(pr, dr) != NULL) {
659*f77a6dbdSBjoern A. Zeeb 		ND6_RUNLOCK();
660*f77a6dbdSBjoern A. Zeeb 		return;
661*f77a6dbdSBjoern A. Zeeb 	}
662*f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
663*f77a6dbdSBjoern A. Zeeb 
664*f77a6dbdSBjoern A. Zeeb 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
665*f77a6dbdSBjoern A. Zeeb 	if (new == NULL)
666*f77a6dbdSBjoern A. Zeeb 		return;
667*f77a6dbdSBjoern A. Zeeb 	defrouter_ref(dr);
668*f77a6dbdSBjoern A. Zeeb 	new->router = dr;
669*f77a6dbdSBjoern A. Zeeb 
670*f77a6dbdSBjoern A. Zeeb 	ND6_WLOCK();
671*f77a6dbdSBjoern A. Zeeb 	if (pfxrtr_lookup(pr, dr) == NULL) {
672*f77a6dbdSBjoern A. Zeeb 		LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
673*f77a6dbdSBjoern A. Zeeb 		update = true;
674*f77a6dbdSBjoern A. Zeeb 	} else {
675*f77a6dbdSBjoern A. Zeeb 		/* We lost a race to add the reference. */
676*f77a6dbdSBjoern A. Zeeb 		defrouter_rele(dr);
677*f77a6dbdSBjoern A. Zeeb 		free(new, M_IP6NDP);
678*f77a6dbdSBjoern A. Zeeb 		update = false;
679*f77a6dbdSBjoern A. Zeeb 	}
680*f77a6dbdSBjoern A. Zeeb 	ND6_WUNLOCK();
681*f77a6dbdSBjoern A. Zeeb 
682*f77a6dbdSBjoern A. Zeeb 	if (update)
683*f77a6dbdSBjoern A. Zeeb 		pfxlist_onlink_check();
684*f77a6dbdSBjoern A. Zeeb }
685*f77a6dbdSBjoern A. Zeeb 
686*f77a6dbdSBjoern A. Zeeb static void
687*f77a6dbdSBjoern A. Zeeb pfxrtr_del(struct nd_pfxrouter *pfr)
688*f77a6dbdSBjoern A. Zeeb {
689*f77a6dbdSBjoern A. Zeeb 
690*f77a6dbdSBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
691*f77a6dbdSBjoern A. Zeeb 
692*f77a6dbdSBjoern A. Zeeb 	LIST_REMOVE(pfr, pfr_entry);
693*f77a6dbdSBjoern A. Zeeb 	defrouter_rele(pfr->router);
694*f77a6dbdSBjoern A. Zeeb 	free(pfr, M_IP6NDP);
695*f77a6dbdSBjoern A. Zeeb }
696*f77a6dbdSBjoern A. Zeeb 
697*f77a6dbdSBjoern A. Zeeb 
698*f77a6dbdSBjoern A. Zeeb /* Default router list processing sub routines. */
6999d9b92f2SBjoern A. Zeeb static void
7001272577eSXin LI defrouter_addreq(struct nd_defrouter *new)
70182cd038dSYoshinobu Inoue {
70282cd038dSYoshinobu Inoue 	struct sockaddr_in6 def, mask, gate;
703686cdd19SJun-ichiro itojun Hagino 	struct rtentry *newrt = NULL;
704743eee66SSUZUKI Shinsuke 	int error;
70582cd038dSYoshinobu Inoue 
706056c7327SLuigi Rizzo 	bzero(&def, sizeof(def));
707056c7327SLuigi Rizzo 	bzero(&mask, sizeof(mask));
708056c7327SLuigi Rizzo 	bzero(&gate, sizeof(gate));
70982cd038dSYoshinobu Inoue 
71007eb2995SHajimu UMEMOTO 	def.sin6_len = mask.sin6_len = gate.sin6_len =
71107eb2995SHajimu UMEMOTO 	    sizeof(struct sockaddr_in6);
712743eee66SSUZUKI Shinsuke 	def.sin6_family = gate.sin6_family = AF_INET6;
71382cd038dSYoshinobu Inoue 	gate.sin6_addr = new->rtaddr;
71482cd038dSYoshinobu Inoue 
71581d5d46bSBjoern A. Zeeb 	error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
71682cd038dSYoshinobu Inoue 	    (struct sockaddr *)&gate, (struct sockaddr *)&mask,
717559b4296SAlan Somers 	    RTF_GATEWAY, &newrt, new->ifp->if_fib);
718686cdd19SJun-ichiro itojun Hagino 	if (newrt) {
71933841545SHajimu UMEMOTO 		nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
7206e6b3f7cSQing Li 		RTFREE(newrt);
721686cdd19SJun-ichiro itojun Hagino 	}
722743eee66SSUZUKI Shinsuke 	if (error == 0)
723743eee66SSUZUKI Shinsuke 		new->installed = 1;
72482cd038dSYoshinobu Inoue }
72582cd038dSYoshinobu Inoue 
726743eee66SSUZUKI Shinsuke /*
727743eee66SSUZUKI Shinsuke  * Remove the default route for a given router.
728559b4296SAlan Somers  * This is just a subroutine function for defrouter_select_fib(), and
729559b4296SAlan Somers  * should not be called from anywhere else.
730743eee66SSUZUKI Shinsuke  */
731743eee66SSUZUKI Shinsuke static void
7321272577eSXin LI defrouter_delreq(struct nd_defrouter *dr)
73382cd038dSYoshinobu Inoue {
73482cd038dSYoshinobu Inoue 	struct sockaddr_in6 def, mask, gate;
735686cdd19SJun-ichiro itojun Hagino 	struct rtentry *oldrt = NULL;
73682cd038dSYoshinobu Inoue 
737056c7327SLuigi Rizzo 	bzero(&def, sizeof(def));
738056c7327SLuigi Rizzo 	bzero(&mask, sizeof(mask));
739056c7327SLuigi Rizzo 	bzero(&gate, sizeof(gate));
74082cd038dSYoshinobu Inoue 
74107eb2995SHajimu UMEMOTO 	def.sin6_len = mask.sin6_len = gate.sin6_len =
74207eb2995SHajimu UMEMOTO 	    sizeof(struct sockaddr_in6);
743743eee66SSUZUKI Shinsuke 	def.sin6_family = gate.sin6_family = AF_INET6;
74482cd038dSYoshinobu Inoue 	gate.sin6_addr = dr->rtaddr;
74582cd038dSYoshinobu Inoue 
74681d5d46bSBjoern A. Zeeb 	in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
74782cd038dSYoshinobu Inoue 	    (struct sockaddr *)&gate,
748559b4296SAlan Somers 	    (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, dr->ifp->if_fib);
749686cdd19SJun-ichiro itojun Hagino 	if (oldrt) {
75033841545SHajimu UMEMOTO 		nd6_rtmsg(RTM_DELETE, oldrt);
75171eba915SRuslan Ermilov 		RTFREE(oldrt);
75220220070SJun-ichiro itojun Hagino 	}
75382cd038dSYoshinobu Inoue 
754743eee66SSUZUKI Shinsuke 	dr->installed = 0;
755743eee66SSUZUKI Shinsuke }
756743eee66SSUZUKI Shinsuke 
757ad675b32SBjoern A. Zeeb static void
7584de485feSMark Johnston defrouter_del(struct nd_defrouter *dr)
75982cd038dSYoshinobu Inoue {
76082cd038dSYoshinobu Inoue 	struct nd_defrouter *deldr = NULL;
76182cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
762d748f7efSMark Johnston 	struct nd_pfxrouter *pfxrtr;
76382cd038dSYoshinobu Inoue 
7644de485feSMark Johnston 	ND6_UNLOCK_ASSERT();
7654de485feSMark Johnston 
76682cd038dSYoshinobu Inoue 	/*
76782cd038dSYoshinobu Inoue 	 * Flush all the routing table entries that use the router
76882cd038dSYoshinobu Inoue 	 * as a next hop.
76982cd038dSYoshinobu Inoue 	 */
770e7fa8d0aSHiroki Sato 	if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
77182cd038dSYoshinobu Inoue 		rt6_flush(&dr->rtaddr, dr->ifp);
77282cd038dSYoshinobu Inoue 
773201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
774201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(dr->ifp);
775201100c5SBjoern A. Zeeb #endif
776201100c5SBjoern A. Zeeb 
777743eee66SSUZUKI Shinsuke 	if (dr->installed) {
778743eee66SSUZUKI Shinsuke 		deldr = dr;
779743eee66SSUZUKI Shinsuke 		defrouter_delreq(dr);
780743eee66SSUZUKI Shinsuke 	}
78182cd038dSYoshinobu Inoue 
78282cd038dSYoshinobu Inoue 	/*
78382cd038dSYoshinobu Inoue 	 * Also delete all the pointers to the router in each prefix lists.
78482cd038dSYoshinobu Inoue 	 */
785d748f7efSMark Johnston 	ND6_WLOCK();
7863b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
78782cd038dSYoshinobu Inoue 		if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
78882cd038dSYoshinobu Inoue 			pfxrtr_del(pfxrtr);
78982cd038dSYoshinobu Inoue 	}
790d748f7efSMark Johnston 	ND6_WUNLOCK();
791d748f7efSMark Johnston 
79282cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
79382cd038dSYoshinobu Inoue 
79482cd038dSYoshinobu Inoue 	/*
795686cdd19SJun-ichiro itojun Hagino 	 * If the router is the primary one, choose a new one.
796559b4296SAlan Somers 	 * Note that defrouter_select_fib() will remove the current
797559b4296SAlan Somers          * gateway from the routing table.
79882cd038dSYoshinobu Inoue 	 */
79982cd038dSYoshinobu Inoue 	if (deldr)
800559b4296SAlan Somers 		defrouter_select_fib(deldr->ifp->if_fib);
801686cdd19SJun-ichiro itojun Hagino 
8024de485feSMark Johnston 	/*
8034de485feSMark Johnston 	 * Release the list reference.
8044de485feSMark Johnston 	 */
8054de485feSMark Johnston 	defrouter_rele(dr);
80682cd038dSYoshinobu Inoue }
80782cd038dSYoshinobu Inoue 
808ad675b32SBjoern A. Zeeb 
809*f77a6dbdSBjoern A. Zeeb struct nd_defrouter *
810*f77a6dbdSBjoern A. Zeeb defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
811*f77a6dbdSBjoern A. Zeeb {
812*f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
813*f77a6dbdSBjoern A. Zeeb 
814*f77a6dbdSBjoern A. Zeeb 	ND6_LOCK_ASSERT();
815*f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
816*f77a6dbdSBjoern A. Zeeb 		if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
817*f77a6dbdSBjoern A. Zeeb 			defrouter_ref(dr);
818*f77a6dbdSBjoern A. Zeeb 			return (dr);
819*f77a6dbdSBjoern A. Zeeb 		}
820*f77a6dbdSBjoern A. Zeeb 	return (NULL);
821*f77a6dbdSBjoern A. Zeeb }
822*f77a6dbdSBjoern A. Zeeb 
823*f77a6dbdSBjoern A. Zeeb struct nd_defrouter *
824*f77a6dbdSBjoern A. Zeeb defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
825*f77a6dbdSBjoern A. Zeeb {
826*f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
827*f77a6dbdSBjoern A. Zeeb 
828*f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
829*f77a6dbdSBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
830*f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
831*f77a6dbdSBjoern A. Zeeb 	return (dr);
832*f77a6dbdSBjoern A. Zeeb }
833*f77a6dbdSBjoern A. Zeeb 
834*f77a6dbdSBjoern A. Zeeb /*
835*f77a6dbdSBjoern A. Zeeb  * Remove all default routes from default router list.
836*f77a6dbdSBjoern A. Zeeb  */
837*f77a6dbdSBjoern A. Zeeb void
838*f77a6dbdSBjoern A. Zeeb defrouter_reset(void)
839*f77a6dbdSBjoern A. Zeeb {
840*f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr, **dra;
841*f77a6dbdSBjoern A. Zeeb 	int count, i;
842*f77a6dbdSBjoern A. Zeeb 
843*f77a6dbdSBjoern A. Zeeb 	count = i = 0;
844*f77a6dbdSBjoern A. Zeeb 
845*f77a6dbdSBjoern A. Zeeb 	/*
846*f77a6dbdSBjoern A. Zeeb 	 * We can't delete routes with the ND lock held, so make a copy of the
847*f77a6dbdSBjoern A. Zeeb 	 * current default router list and use that when deleting routes.
848*f77a6dbdSBjoern A. Zeeb 	 */
849*f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
850*f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
851*f77a6dbdSBjoern A. Zeeb 		count++;
852*f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
853*f77a6dbdSBjoern A. Zeeb 
854*f77a6dbdSBjoern A. Zeeb 	dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
855*f77a6dbdSBjoern A. Zeeb 
856*f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
857*f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
858*f77a6dbdSBjoern A. Zeeb 		if (i == count)
859*f77a6dbdSBjoern A. Zeeb 			break;
860*f77a6dbdSBjoern A. Zeeb 		defrouter_ref(dr);
861*f77a6dbdSBjoern A. Zeeb 		dra[i++] = dr;
862*f77a6dbdSBjoern A. Zeeb 	}
863*f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
864*f77a6dbdSBjoern A. Zeeb 
865*f77a6dbdSBjoern A. Zeeb 	for (i = 0; i < count && dra[i] != NULL; i++) {
866*f77a6dbdSBjoern A. Zeeb 		defrouter_delreq(dra[i]);
867*f77a6dbdSBjoern A. Zeeb 		defrouter_rele(dra[i]);
868*f77a6dbdSBjoern A. Zeeb 	}
869*f77a6dbdSBjoern A. Zeeb 	free(dra, M_TEMP);
870*f77a6dbdSBjoern A. Zeeb 
871*f77a6dbdSBjoern A. Zeeb 	/*
872*f77a6dbdSBjoern A. Zeeb 	 * XXX should we also nuke any default routers in the kernel, by
873*f77a6dbdSBjoern A. Zeeb 	 * going through them by rtalloc1()?
874*f77a6dbdSBjoern A. Zeeb 	 */
875*f77a6dbdSBjoern A. Zeeb }
876*f77a6dbdSBjoern A. Zeeb 
877ad675b32SBjoern A. Zeeb /*
878ad675b32SBjoern A. Zeeb  * Look up a matching default router list entry and remove it. Returns true if a
879ad675b32SBjoern A. Zeeb  * matching entry was found, false otherwise.
880ad675b32SBjoern A. Zeeb  */
881ad675b32SBjoern A. Zeeb bool
882ad675b32SBjoern A. Zeeb defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
883ad675b32SBjoern A. Zeeb {
884ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
885ad675b32SBjoern A. Zeeb 
886ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
887ad675b32SBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
888ad675b32SBjoern A. Zeeb 	if (dr == NULL) {
889ad675b32SBjoern A. Zeeb 		ND6_WUNLOCK();
890ad675b32SBjoern A. Zeeb 		return (false);
891ad675b32SBjoern A. Zeeb 	}
892ad675b32SBjoern A. Zeeb 
893ad675b32SBjoern A. Zeeb 	defrouter_unlink(dr, NULL);
894ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
895ad675b32SBjoern A. Zeeb 	defrouter_del(dr);
896ad675b32SBjoern A. Zeeb 	defrouter_rele(dr);
897ad675b32SBjoern A. Zeeb 	return (true);
898ad675b32SBjoern A. Zeeb }
899ad675b32SBjoern A. Zeeb 
900686cdd19SJun-ichiro itojun Hagino /*
901*f77a6dbdSBjoern A. Zeeb  * for default router selection
902*f77a6dbdSBjoern A. Zeeb  * regards router-preference field as a 2-bit signed integer
903*f77a6dbdSBjoern A. Zeeb  */
904*f77a6dbdSBjoern A. Zeeb static int
905*f77a6dbdSBjoern A. Zeeb rtpref(struct nd_defrouter *dr)
906*f77a6dbdSBjoern A. Zeeb {
907*f77a6dbdSBjoern A. Zeeb 	switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
908*f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_HIGH:
909*f77a6dbdSBjoern A. Zeeb 		return (RTPREF_HIGH);
910*f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_MEDIUM:
911*f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_RSV:
912*f77a6dbdSBjoern A. Zeeb 		return (RTPREF_MEDIUM);
913*f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_LOW:
914*f77a6dbdSBjoern A. Zeeb 		return (RTPREF_LOW);
915*f77a6dbdSBjoern A. Zeeb 	default:
916*f77a6dbdSBjoern A. Zeeb 		/*
917*f77a6dbdSBjoern A. Zeeb 		 * This case should never happen.  If it did, it would mean a
918*f77a6dbdSBjoern A. Zeeb 		 * serious bug of kernel internal.  We thus always bark here.
919*f77a6dbdSBjoern A. Zeeb 		 * Or, can we even panic?
920*f77a6dbdSBjoern A. Zeeb 		 */
921*f77a6dbdSBjoern A. Zeeb 		log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
922*f77a6dbdSBjoern A. Zeeb 		return (RTPREF_INVALID);
923*f77a6dbdSBjoern A. Zeeb 	}
924*f77a6dbdSBjoern A. Zeeb 	/* NOTREACHED */
925*f77a6dbdSBjoern A. Zeeb }
926*f77a6dbdSBjoern A. Zeeb 
927*f77a6dbdSBjoern A. Zeeb /*
928743eee66SSUZUKI Shinsuke  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
929743eee66SSUZUKI Shinsuke  * draft-ietf-ipngwg-router-selection:
930743eee66SSUZUKI Shinsuke  * 1) Routers that are reachable or probably reachable should be preferred.
931743eee66SSUZUKI Shinsuke  *    If we have more than one (probably) reachable router, prefer ones
932743eee66SSUZUKI Shinsuke  *    with the highest router preference.
933686cdd19SJun-ichiro itojun Hagino  * 2) When no routers on the list are known to be reachable or
934686cdd19SJun-ichiro itojun Hagino  *    probably reachable, routers SHOULD be selected in a round-robin
935743eee66SSUZUKI Shinsuke  *    fashion, regardless of router preference values.
936686cdd19SJun-ichiro itojun Hagino  * 3) If the Default Router List is empty, assume that all
937686cdd19SJun-ichiro itojun Hagino  *    destinations are on-link.
938743eee66SSUZUKI Shinsuke  *
939743eee66SSUZUKI Shinsuke  * We assume nd_defrouter is sorted by router preference value.
940743eee66SSUZUKI Shinsuke  * Since the code below covers both with and without router preference cases,
941743eee66SSUZUKI Shinsuke  * we do not need to classify the cases by ifdef.
942743eee66SSUZUKI Shinsuke  *
943743eee66SSUZUKI Shinsuke  * At this moment, we do not try to install more than one default router,
944743eee66SSUZUKI Shinsuke  * even when the multipath routing is available, because we're not sure about
945743eee66SSUZUKI Shinsuke  * the benefits for stub hosts comparing to the risk of making the code
946743eee66SSUZUKI Shinsuke  * complicated and the possibility of introducing bugs.
947559b4296SAlan Somers  *
948559b4296SAlan Somers  * We maintain a single list of routers for multiple FIBs, only considering one
949559b4296SAlan Somers  * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
950559b4296SAlan Somers  * we do the whole thing multiple times.
951686cdd19SJun-ichiro itojun Hagino  */
952686cdd19SJun-ichiro itojun Hagino void
953559b4296SAlan Somers defrouter_select_fib(int fibnum)
954686cdd19SJun-ichiro itojun Hagino {
955a68cc388SGleb Smirnoff 	struct epoch_tracker et;
9564de485feSMark Johnston 	struct nd_defrouter *dr, *selected_dr, *installed_dr;
9576e6b3f7cSQing Li 	struct llentry *ln = NULL;
958686cdd19SJun-ichiro itojun Hagino 
959559b4296SAlan Somers 	if (fibnum == RT_ALL_FIBS) {
960559b4296SAlan Somers 		for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
961559b4296SAlan Somers 			defrouter_select_fib(fibnum);
962559b4296SAlan Somers 		}
963559b4296SAlan Somers 	}
964559b4296SAlan Somers 
9654de485feSMark Johnston 	ND6_RLOCK();
966686cdd19SJun-ichiro itojun Hagino 	/*
967743eee66SSUZUKI Shinsuke 	 * Let's handle easy case (3) first:
968743eee66SSUZUKI Shinsuke 	 * If default router list is empty, there's nothing to be done.
969743eee66SSUZUKI Shinsuke 	 */
970f592d0c3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_nd6_defrouter)) {
9714de485feSMark Johnston 		ND6_RUNLOCK();
972743eee66SSUZUKI Shinsuke 		return;
9734de485feSMark Johnston 	}
974743eee66SSUZUKI Shinsuke 
975743eee66SSUZUKI Shinsuke 	/*
976686cdd19SJun-ichiro itojun Hagino 	 * Search for a (probably) reachable router from the list.
977743eee66SSUZUKI Shinsuke 	 * We just pick up the first reachable one (if any), assuming that
978743eee66SSUZUKI Shinsuke 	 * the ordering rule of the list described in defrtrlist_update().
979686cdd19SJun-ichiro itojun Hagino 	 */
9804de485feSMark Johnston 	selected_dr = installed_dr = NULL;
981f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
982a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
983559b4296SAlan Somers 		if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
9846e6b3f7cSQing Li 		    (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
985686cdd19SJun-ichiro itojun Hagino 		    ND6_IS_LLINFO_PROBREACH(ln)) {
986743eee66SSUZUKI Shinsuke 			selected_dr = dr;
9874de485feSMark Johnston 			defrouter_ref(selected_dr);
988686cdd19SJun-ichiro itojun Hagino 		}
989a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
9905f16e341SBjoern A. Zeeb 		if (ln != NULL) {
991c0641cc0SKip Macy 			LLE_RUNLOCK(ln);
9925f16e341SBjoern A. Zeeb 			ln = NULL;
9935f16e341SBjoern A. Zeeb 		}
994686cdd19SJun-ichiro itojun Hagino 
995559b4296SAlan Somers 		if (dr->installed && dr->ifp->if_fib == fibnum) {
9964de485feSMark Johnston 			if (installed_dr == NULL) {
997743eee66SSUZUKI Shinsuke 				installed_dr = dr;
9984de485feSMark Johnston 				defrouter_ref(installed_dr);
9994de485feSMark Johnston 			} else {
1000559b4296SAlan Somers 				/*
1001559b4296SAlan Somers 				 * this should not happen.
1002559b4296SAlan Somers 				 * warn for diagnosis.
1003559b4296SAlan Somers 				 */
1004559b4296SAlan Somers 				log(LOG_ERR, "defrouter_select_fib: more than "
1005559b4296SAlan Somers 				             "one router is installed\n");
10064de485feSMark Johnston 			}
100733841545SHajimu UMEMOTO 		}
1008686cdd19SJun-ichiro itojun Hagino 	}
1009743eee66SSUZUKI Shinsuke 	/*
1010743eee66SSUZUKI Shinsuke 	 * If none of the default routers was found to be reachable,
1011743eee66SSUZUKI Shinsuke 	 * round-robin the list regardless of preference.
1012743eee66SSUZUKI Shinsuke 	 * Otherwise, if we have an installed router, check if the selected
1013743eee66SSUZUKI Shinsuke 	 * (reachable) router should really be preferred to the installed one.
1014743eee66SSUZUKI Shinsuke 	 * We only prefer the new router when the old one is not reachable
1015743eee66SSUZUKI Shinsuke 	 * or when the new one has a really higher preference value.
1016743eee66SSUZUKI Shinsuke 	 */
1017743eee66SSUZUKI Shinsuke 	if (selected_dr == NULL) {
10184de485feSMark Johnston 		if (installed_dr == NULL ||
10194de485feSMark Johnston 		    TAILQ_NEXT(installed_dr, dr_entry) == NULL)
1020f592d0c3SBjoern A. Zeeb 			dr = TAILQ_FIRST(&V_nd6_defrouter);
1021743eee66SSUZUKI Shinsuke 		else
1022559b4296SAlan Somers 			dr = TAILQ_NEXT(installed_dr, dr_entry);
1023559b4296SAlan Somers 
1024559b4296SAlan Somers 		/* Ensure we select a router for this FIB. */
1025f592d0c3SBjoern A. Zeeb 		TAILQ_FOREACH_FROM(dr, &V_nd6_defrouter, dr_entry) {
1026559b4296SAlan Somers 			if (dr->ifp->if_fib == fibnum) {
1027559b4296SAlan Somers 				selected_dr = dr;
10284de485feSMark Johnston 				defrouter_ref(selected_dr);
1029559b4296SAlan Somers 				break;
1030559b4296SAlan Somers 			}
1031559b4296SAlan Somers 		}
10324de485feSMark Johnston 	} else if (installed_dr != NULL) {
1033a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
1034559b4296SAlan Somers 		if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
1035559b4296SAlan Somers 		                     installed_dr->ifp)) &&
1036743eee66SSUZUKI Shinsuke 		    ND6_IS_LLINFO_PROBREACH(ln) &&
1037559b4296SAlan Somers 		    installed_dr->ifp->if_fib == fibnum &&
1038743eee66SSUZUKI Shinsuke 		    rtpref(selected_dr) <= rtpref(installed_dr)) {
10394de485feSMark Johnston 			defrouter_rele(selected_dr);
1040743eee66SSUZUKI Shinsuke 			selected_dr = installed_dr;
1041743eee66SSUZUKI Shinsuke 		}
1042a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1043c0641cc0SKip Macy 		if (ln != NULL)
1044c0641cc0SKip Macy 			LLE_RUNLOCK(ln);
10456e6b3f7cSQing Li 	}
10464de485feSMark Johnston 	ND6_RUNLOCK();
1047743eee66SSUZUKI Shinsuke 
1048743eee66SSUZUKI Shinsuke 	/*
1049559b4296SAlan Somers 	 * If we selected a router for this FIB and it's different
1050559b4296SAlan Somers 	 * than the installed one, remove the installed router and
1051559b4296SAlan Somers 	 * install the selected one in its place.
1052743eee66SSUZUKI Shinsuke 	 */
1053743eee66SSUZUKI Shinsuke 	if (installed_dr != selected_dr) {
10544de485feSMark Johnston 		if (installed_dr != NULL) {
1055743eee66SSUZUKI Shinsuke 			defrouter_delreq(installed_dr);
10564de485feSMark Johnston 			defrouter_rele(installed_dr);
10574de485feSMark Johnston 		}
1058559b4296SAlan Somers 		if (selected_dr != NULL)
1059743eee66SSUZUKI Shinsuke 			defrouter_addreq(selected_dr);
1060686cdd19SJun-ichiro itojun Hagino 	}
1061559b4296SAlan Somers 	if (selected_dr != NULL)
10624de485feSMark Johnston 		defrouter_rele(selected_dr);
1063686cdd19SJun-ichiro itojun Hagino }
1064686cdd19SJun-ichiro itojun Hagino 
106582cd038dSYoshinobu Inoue static struct nd_defrouter *
10661272577eSXin LI defrtrlist_update(struct nd_defrouter *new)
106782cd038dSYoshinobu Inoue {
106882cd038dSYoshinobu Inoue 	struct nd_defrouter *dr, *n;
1069d748f7efSMark Johnston 	uint64_t genid;
1070a4a7c825SMark Johnston 	int oldpref;
1071d748f7efSMark Johnston 	bool writelocked;
107282cd038dSYoshinobu Inoue 
107382cd038dSYoshinobu Inoue 	if (new->rtlifetime == 0) {
1074ff63037dSMark Johnston 		defrouter_remove(&new->rtaddr, new->ifp);
1075a4a7c825SMark Johnston 		return (NULL);
1076a4a7c825SMark Johnston 	}
1077a4a7c825SMark Johnston 
1078d748f7efSMark Johnston 	ND6_RLOCK();
1079d748f7efSMark Johnston 	writelocked = false;
1080d748f7efSMark Johnston restart:
1081ff63037dSMark Johnston 	dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
1082ff63037dSMark Johnston 	if (dr != NULL) {
1083a4a7c825SMark Johnston 		oldpref = rtpref(dr);
1084743eee66SSUZUKI Shinsuke 
108582cd038dSYoshinobu Inoue 		/* override */
108601869be5SMark Johnston 		dr->raflags = new->raflags; /* XXX flag check */
108782cd038dSYoshinobu Inoue 		dr->rtlifetime = new->rtlifetime;
108882cd038dSYoshinobu Inoue 		dr->expire = new->expire;
1089743eee66SSUZUKI Shinsuke 
1090743eee66SSUZUKI Shinsuke 		/*
1091743eee66SSUZUKI Shinsuke 		 * If the preference does not change, there's no need
10920f1aca65SQing Li 		 * to sort the entries. Also make sure the selected
10930f1aca65SQing Li 		 * router is still installed in the kernel.
1094743eee66SSUZUKI Shinsuke 		 */
10954de485feSMark Johnston 		if (dr->installed && rtpref(new) == oldpref) {
1096d748f7efSMark Johnston 			if (writelocked)
10974de485feSMark Johnston 				ND6_WUNLOCK();
1098d748f7efSMark Johnston 			else
1099d748f7efSMark Johnston 				ND6_RUNLOCK();
1100743eee66SSUZUKI Shinsuke 			return (dr);
11014de485feSMark Johnston 		}
1102d748f7efSMark Johnston 	}
1103743eee66SSUZUKI Shinsuke 
1104743eee66SSUZUKI Shinsuke 	/*
1105d748f7efSMark Johnston 	 * The router needs to be reinserted into the default router
1106d748f7efSMark Johnston 	 * list, so upgrade to a write lock. If that fails and the list
1107d748f7efSMark Johnston 	 * has potentially changed while the lock was dropped, we'll
1108d748f7efSMark Johnston 	 * redo the lookup with the write lock held.
1109d748f7efSMark Johnston 	 */
1110d748f7efSMark Johnston 	if (!writelocked) {
1111d748f7efSMark Johnston 		writelocked = true;
1112d748f7efSMark Johnston 		if (!ND6_TRY_UPGRADE()) {
1113d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1114d748f7efSMark Johnston 			ND6_RUNLOCK();
1115d748f7efSMark Johnston 			ND6_WLOCK();
1116d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1117d748f7efSMark Johnston 				goto restart;
1118d748f7efSMark Johnston 		}
1119d748f7efSMark Johnston 	}
1120d748f7efSMark Johnston 
1121d748f7efSMark Johnston 	if (dr != NULL) {
1122d748f7efSMark Johnston 		/*
1123a4a7c825SMark Johnston 		 * The preferred router may have changed, so relocate this
1124a4a7c825SMark Johnston 		 * router.
1125743eee66SSUZUKI Shinsuke 		 */
1126f592d0c3SBjoern A. Zeeb 		TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
1127743eee66SSUZUKI Shinsuke 		n = dr;
1128ff63037dSMark Johnston 	} else {
1129fc315641SMark Johnston 		n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
11304de485feSMark Johnston 		if (n == NULL) {
11314de485feSMark Johnston 			ND6_WUNLOCK();
113282cd038dSYoshinobu Inoue 			return (NULL);
11334de485feSMark Johnston 		}
1134fc315641SMark Johnston 		memcpy(n, new, sizeof(*n));
11354de485feSMark Johnston 		/* Initialize with an extra reference for the caller. */
11364de485feSMark Johnston 		refcount_init(&n->refcnt, 2);
1137ff63037dSMark Johnston 	}
1138686cdd19SJun-ichiro itojun Hagino 
1139686cdd19SJun-ichiro itojun Hagino 	/*
1140743eee66SSUZUKI Shinsuke 	 * Insert the new router in the Default Router List;
1141743eee66SSUZUKI Shinsuke 	 * The Default Router List should be in the descending order
1142743eee66SSUZUKI Shinsuke 	 * of router-preferece.  Routers with the same preference are
1143743eee66SSUZUKI Shinsuke 	 * sorted in the arriving time order.
1144686cdd19SJun-ichiro itojun Hagino 	 */
1145743eee66SSUZUKI Shinsuke 
1146743eee66SSUZUKI Shinsuke 	/* insert at the end of the group */
1147f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1148743eee66SSUZUKI Shinsuke 		if (rtpref(n) > rtpref(dr))
1149743eee66SSUZUKI Shinsuke 			break;
1150743eee66SSUZUKI Shinsuke 	}
11514de485feSMark Johnston 	if (dr != NULL)
1152743eee66SSUZUKI Shinsuke 		TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1153743eee66SSUZUKI Shinsuke 	else
1154f592d0c3SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(&V_nd6_defrouter, n, dr_entry);
1155d748f7efSMark Johnston 	V_nd6_list_genid++;
11564de485feSMark Johnston 	ND6_WUNLOCK();
1157743eee66SSUZUKI Shinsuke 
1158559b4296SAlan Somers 	defrouter_select_fib(new->ifp->if_fib);
1159743eee66SSUZUKI Shinsuke 
116082cd038dSYoshinobu Inoue 	return (n);
116182cd038dSYoshinobu Inoue }
116282cd038dSYoshinobu Inoue 
1163*f77a6dbdSBjoern A. Zeeb static int
1164*f77a6dbdSBjoern A. Zeeb in6_init_prefix_ltimes(struct nd_prefix *ndpr)
116582cd038dSYoshinobu Inoue {
1166*f77a6dbdSBjoern A. Zeeb 	if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1167*f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_preferred = 0;
1168*f77a6dbdSBjoern A. Zeeb 	else
1169*f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
1170*f77a6dbdSBjoern A. Zeeb 	if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1171*f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_expire = 0;
1172*f77a6dbdSBjoern A. Zeeb 	else
1173*f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
117482cd038dSYoshinobu Inoue 
1175*f77a6dbdSBjoern A. Zeeb 	return 0;
117682cd038dSYoshinobu Inoue }
117782cd038dSYoshinobu Inoue 
117882cd038dSYoshinobu Inoue static void
1179*f77a6dbdSBjoern A. Zeeb in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
118082cd038dSYoshinobu Inoue {
1181*f77a6dbdSBjoern A. Zeeb 	/* init ia6t_expire */
1182*f77a6dbdSBjoern A. Zeeb 	if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1183*f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire = 0;
1184*f77a6dbdSBjoern A. Zeeb 	else {
1185*f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire = time_uptime;
1186*f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire += lt6->ia6t_vltime;
118782cd038dSYoshinobu Inoue 	}
118882cd038dSYoshinobu Inoue 
1189*f77a6dbdSBjoern A. Zeeb 	/* init ia6t_preferred */
1190*f77a6dbdSBjoern A. Zeeb 	if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1191*f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred = 0;
1192*f77a6dbdSBjoern A. Zeeb 	else {
1193*f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred = time_uptime;
1194*f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred += lt6->ia6t_pltime;
1195*f77a6dbdSBjoern A. Zeeb 	}
1196*f77a6dbdSBjoern A. Zeeb }
1197*f77a6dbdSBjoern A. Zeeb 
1198*f77a6dbdSBjoern A. Zeeb static struct in6_ifaddr *
1199*f77a6dbdSBjoern A. Zeeb in6_ifadd(struct nd_prefixctl *pr, int mcast)
120082cd038dSYoshinobu Inoue {
1201*f77a6dbdSBjoern A. Zeeb 	struct ifnet *ifp = pr->ndpr_ifp;
1202*f77a6dbdSBjoern A. Zeeb 	struct ifaddr *ifa;
1203*f77a6dbdSBjoern A. Zeeb 	struct in6_aliasreq ifra;
1204*f77a6dbdSBjoern A. Zeeb 	struct in6_ifaddr *ia, *ib;
1205*f77a6dbdSBjoern A. Zeeb 	int error, plen0;
1206*f77a6dbdSBjoern A. Zeeb 	struct in6_addr mask;
1207*f77a6dbdSBjoern A. Zeeb 	int prefixlen = pr->ndpr_plen;
1208*f77a6dbdSBjoern A. Zeeb 	int updateflags;
1209*f77a6dbdSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
12104de485feSMark Johnston 
1211*f77a6dbdSBjoern A. Zeeb 	in6_prefixlen2mask(&mask, prefixlen);
1212d748f7efSMark Johnston 
1213*f77a6dbdSBjoern A. Zeeb 	/*
1214*f77a6dbdSBjoern A. Zeeb 	 * find a link-local address (will be interface ID).
1215*f77a6dbdSBjoern A. Zeeb 	 * Is it really mandatory? Theoretically, a global or a site-local
1216*f77a6dbdSBjoern A. Zeeb 	 * address can be configured without a link-local address, if we
1217*f77a6dbdSBjoern A. Zeeb 	 * have a unique interface identifier...
1218*f77a6dbdSBjoern A. Zeeb 	 *
1219*f77a6dbdSBjoern A. Zeeb 	 * it is not mandatory to have a link-local address, we can generate
1220*f77a6dbdSBjoern A. Zeeb 	 * interface identifier on the fly.  we do this because:
1221*f77a6dbdSBjoern A. Zeeb 	 * (1) it should be the easiest way to find interface identifier.
1222*f77a6dbdSBjoern A. Zeeb 	 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1223*f77a6dbdSBjoern A. Zeeb 	 * for multiple addresses on a single interface, and possible shortcut
1224*f77a6dbdSBjoern A. Zeeb 	 * of DAD.  we omitted DAD for this reason in the past.
1225*f77a6dbdSBjoern A. Zeeb 	 * (3) a user can prevent autoconfiguration of global address
1226*f77a6dbdSBjoern A. Zeeb 	 * by removing link-local address by hand (this is partly because we
1227*f77a6dbdSBjoern A. Zeeb 	 * don't have other way to control the use of IPv6 on an interface.
1228*f77a6dbdSBjoern A. Zeeb 	 * this has been our design choice - cf. NRL's "ifconfig auto").
1229*f77a6dbdSBjoern A. Zeeb 	 * (4) it is easier to manage when an interface has addresses
1230*f77a6dbdSBjoern A. Zeeb 	 * with the same interface identifier, than to have multiple addresses
1231*f77a6dbdSBjoern A. Zeeb 	 * with different interface identifiers.
1232*f77a6dbdSBjoern A. Zeeb 	 */
1233*f77a6dbdSBjoern A. Zeeb 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1234*f77a6dbdSBjoern A. Zeeb 	if (ifa)
1235*f77a6dbdSBjoern A. Zeeb 		ib = (struct in6_ifaddr *)ifa;
1236*f77a6dbdSBjoern A. Zeeb 	else
1237*f77a6dbdSBjoern A. Zeeb 		return NULL;
1238*f77a6dbdSBjoern A. Zeeb 
1239*f77a6dbdSBjoern A. Zeeb 	/* prefixlen + ifidlen must be equal to 128 */
1240*f77a6dbdSBjoern A. Zeeb 	plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1241*f77a6dbdSBjoern A. Zeeb 	if (prefixlen != plen0) {
1242*f77a6dbdSBjoern A. Zeeb 		ifa_free(ifa);
1243*f77a6dbdSBjoern A. Zeeb 		nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1244*f77a6dbdSBjoern A. Zeeb 		    "(prefix=%d ifid=%d)\n",
1245*f77a6dbdSBjoern A. Zeeb 		    if_name(ifp), prefixlen, 128 - plen0));
1246*f77a6dbdSBjoern A. Zeeb 		return NULL;
1247*f77a6dbdSBjoern A. Zeeb 	}
1248*f77a6dbdSBjoern A. Zeeb 
1249*f77a6dbdSBjoern A. Zeeb 	/* make ifaddr */
1250*f77a6dbdSBjoern A. Zeeb 	in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
1251*f77a6dbdSBjoern A. Zeeb 
1252*f77a6dbdSBjoern A. Zeeb 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
1253*f77a6dbdSBjoern A. Zeeb 	/* interface ID */
1254*f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1255*f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1256*f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1257*f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1258*f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1259*f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1260*f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1261*f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1262*f77a6dbdSBjoern A. Zeeb 	ifa_free(ifa);
1263*f77a6dbdSBjoern A. Zeeb 
1264*f77a6dbdSBjoern A. Zeeb 	/* lifetimes. */
1265*f77a6dbdSBjoern A. Zeeb 	ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1266*f77a6dbdSBjoern A. Zeeb 	ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1267*f77a6dbdSBjoern A. Zeeb 
1268*f77a6dbdSBjoern A. Zeeb 	/* XXX: scope zone ID? */
1269*f77a6dbdSBjoern A. Zeeb 
1270*f77a6dbdSBjoern A. Zeeb 	ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1271*f77a6dbdSBjoern A. Zeeb 
1272*f77a6dbdSBjoern A. Zeeb 	/*
1273*f77a6dbdSBjoern A. Zeeb 	 * Make sure that we do not have this address already.  This should
1274*f77a6dbdSBjoern A. Zeeb 	 * usually not happen, but we can still see this case, e.g., if we
1275*f77a6dbdSBjoern A. Zeeb 	 * have manually configured the exact address to be configured.
1276*f77a6dbdSBjoern A. Zeeb 	 */
1277*f77a6dbdSBjoern A. Zeeb 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1278*f77a6dbdSBjoern A. Zeeb 	    &ifra.ifra_addr.sin6_addr);
1279*f77a6dbdSBjoern A. Zeeb 	if (ifa != NULL) {
1280*f77a6dbdSBjoern A. Zeeb 		ifa_free(ifa);
1281*f77a6dbdSBjoern A. Zeeb 		/* this should be rare enough to make an explicit log */
1282*f77a6dbdSBjoern A. Zeeb 		log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1283*f77a6dbdSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1284*f77a6dbdSBjoern A. Zeeb 		return (NULL);
1285*f77a6dbdSBjoern A. Zeeb 	}
1286*f77a6dbdSBjoern A. Zeeb 
1287*f77a6dbdSBjoern A. Zeeb 	/*
1288*f77a6dbdSBjoern A. Zeeb 	 * Allocate ifaddr structure, link into chain, etc.
1289*f77a6dbdSBjoern A. Zeeb 	 * If we are going to create a new address upon receiving a multicasted
1290*f77a6dbdSBjoern A. Zeeb 	 * RA, we need to impose a random delay before starting DAD.
1291*f77a6dbdSBjoern A. Zeeb 	 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1292*f77a6dbdSBjoern A. Zeeb 	 */
1293*f77a6dbdSBjoern A. Zeeb 	updateflags = 0;
1294*f77a6dbdSBjoern A. Zeeb 	if (mcast)
1295*f77a6dbdSBjoern A. Zeeb 		updateflags |= IN6_IFAUPDATE_DADDELAY;
1296*f77a6dbdSBjoern A. Zeeb 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1297*f77a6dbdSBjoern A. Zeeb 		nd6log((LOG_ERR,
1298*f77a6dbdSBjoern A. Zeeb 		    "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1299*f77a6dbdSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1300*f77a6dbdSBjoern A. Zeeb 		    if_name(ifp), error));
1301*f77a6dbdSBjoern A. Zeeb 		return (NULL);	/* ifaddr must not have been allocated. */
1302*f77a6dbdSBjoern A. Zeeb 	}
1303*f77a6dbdSBjoern A. Zeeb 
1304*f77a6dbdSBjoern A. Zeeb 	ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1305*f77a6dbdSBjoern A. Zeeb 	/*
1306*f77a6dbdSBjoern A. Zeeb 	 * XXXRW: Assumption of non-NULLness here might not be true with
1307*f77a6dbdSBjoern A. Zeeb 	 * fine-grained locking -- should we validate it?  Or just return
1308*f77a6dbdSBjoern A. Zeeb 	 * earlier ifa rather than looking it up again?
1309*f77a6dbdSBjoern A. Zeeb 	 */
1310*f77a6dbdSBjoern A. Zeeb 	return (ia);		/* this is always non-NULL  and referenced. */
131182cd038dSYoshinobu Inoue }
131282cd038dSYoshinobu Inoue 
1313d748f7efSMark Johnston static struct nd_prefix *
1314d748f7efSMark Johnston nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1315d748f7efSMark Johnston {
1316d748f7efSMark Johnston 	struct nd_prefix *search;
1317d748f7efSMark Johnston 
1318d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1319d748f7efSMark Johnston 
1320d748f7efSMark Johnston 	LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1321d748f7efSMark Johnston 		if (key->ndpr_ifp == search->ndpr_ifp &&
1322d748f7efSMark Johnston 		    key->ndpr_plen == search->ndpr_plen &&
1323d748f7efSMark Johnston 		    in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1324d748f7efSMark Johnston 		    &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1325d748f7efSMark Johnston 			nd6_prefix_ref(search);
1326d748f7efSMark Johnston 			break;
1327d748f7efSMark Johnston 		}
1328d748f7efSMark Johnston 	}
1329d748f7efSMark Johnston 	return (search);
1330d748f7efSMark Johnston }
1331d748f7efSMark Johnston 
133233841545SHajimu UMEMOTO struct nd_prefix *
13331272577eSXin LI nd6_prefix_lookup(struct nd_prefixctl *key)
133482cd038dSYoshinobu Inoue {
133582cd038dSYoshinobu Inoue 	struct nd_prefix *search;
133682cd038dSYoshinobu Inoue 
1337d748f7efSMark Johnston 	ND6_RLOCK();
1338d748f7efSMark Johnston 	search = nd6_prefix_lookup_locked(key);
1339d748f7efSMark Johnston 	ND6_RUNLOCK();
1340d748f7efSMark Johnston 	return (search);
134182cd038dSYoshinobu Inoue }
134282cd038dSYoshinobu Inoue 
1343d748f7efSMark Johnston void
1344d748f7efSMark Johnston nd6_prefix_ref(struct nd_prefix *pr)
1345d748f7efSMark Johnston {
1346d748f7efSMark Johnston 
1347d748f7efSMark Johnston 	refcount_acquire(&pr->ndpr_refcnt);
1348d748f7efSMark Johnston }
1349d748f7efSMark Johnston 
1350d748f7efSMark Johnston void
1351d748f7efSMark Johnston nd6_prefix_rele(struct nd_prefix *pr)
1352d748f7efSMark Johnston {
1353d748f7efSMark Johnston 
1354d748f7efSMark Johnston 	if (refcount_release(&pr->ndpr_refcnt)) {
1355d748f7efSMark Johnston 		KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1356d748f7efSMark Johnston 		    ("prefix %p has advertising routers", pr));
1357d748f7efSMark Johnston 		free(pr, M_IP6NDP);
1358d748f7efSMark Johnston 	}
135982cd038dSYoshinobu Inoue }
136082cd038dSYoshinobu Inoue 
136133841545SHajimu UMEMOTO int
13621272577eSXin LI nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
13631272577eSXin LI     struct nd_prefix **newp)
136482cd038dSYoshinobu Inoue {
1365d748f7efSMark Johnston 	struct nd_prefix *new;
13661d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1367d748f7efSMark Johnston 	int error;
136882cd038dSYoshinobu Inoue 
1369fc315641SMark Johnston 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
137082cd038dSYoshinobu Inoue 	if (new == NULL)
137107eb2995SHajimu UMEMOTO 		return (ENOMEM);
1372d748f7efSMark Johnston 	refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1373743eee66SSUZUKI Shinsuke 	new->ndpr_ifp = pr->ndpr_ifp;
1374743eee66SSUZUKI Shinsuke 	new->ndpr_prefix = pr->ndpr_prefix;
1375743eee66SSUZUKI Shinsuke 	new->ndpr_plen = pr->ndpr_plen;
1376743eee66SSUZUKI Shinsuke 	new->ndpr_vltime = pr->ndpr_vltime;
1377743eee66SSUZUKI Shinsuke 	new->ndpr_pltime = pr->ndpr_pltime;
1378743eee66SSUZUKI Shinsuke 	new->ndpr_flags = pr->ndpr_flags;
1379743eee66SSUZUKI Shinsuke 	if ((error = in6_init_prefix_ltimes(new)) != 0) {
1380743eee66SSUZUKI Shinsuke 		free(new, M_IP6NDP);
1381743eee66SSUZUKI Shinsuke 		return (error);
1382743eee66SSUZUKI Shinsuke 	}
13837d26db17SHiroki Sato 	new->ndpr_lastupdate = time_uptime;
138482cd038dSYoshinobu Inoue 
138507eb2995SHajimu UMEMOTO 	/* initialization */
138682cd038dSYoshinobu Inoue 	LIST_INIT(&new->ndpr_advrtrs);
138782cd038dSYoshinobu Inoue 	in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
138882cd038dSYoshinobu Inoue 	/* make prefix in the canonical form */
13891dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
139082cd038dSYoshinobu Inoue 
1391d748f7efSMark Johnston 	ND6_WLOCK();
1392603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1393d748f7efSMark Johnston 	V_nd6_list_genid++;
1394d748f7efSMark Johnston 	ND6_WUNLOCK();
139582cd038dSYoshinobu Inoue 
139633841545SHajimu UMEMOTO 	/* ND_OPT_PI_FLAG_ONLINK processing */
139733841545SHajimu UMEMOTO 	if (new->ndpr_raf_onlink) {
1398b8a6e03fSGleb Smirnoff 		struct epoch_tracker et;
1399b8a6e03fSGleb Smirnoff 
1400d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1401b8a6e03fSGleb Smirnoff 		NET_EPOCH_ENTER(et);
1402d748f7efSMark Johnston 		if ((error = nd6_prefix_onlink(new)) != 0) {
140333841545SHajimu UMEMOTO 			nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
140433841545SHajimu UMEMOTO 			    "the prefix %s/%d on-link on %s (errno=%d)\n",
14051d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1406d748f7efSMark Johnston 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
140733841545SHajimu UMEMOTO 			/* proceed anyway. XXX: is it correct? */
140833841545SHajimu UMEMOTO 		}
1409b8a6e03fSGleb Smirnoff 		NET_EPOCH_EXIT(et);
1410d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
141133841545SHajimu UMEMOTO 	}
141233841545SHajimu UMEMOTO 
141320b5f022SMark Johnston 	if (dr != NULL)
141482cd038dSYoshinobu Inoue 		pfxrtr_add(new, dr);
141520b5f022SMark Johnston 	if (newp != NULL)
141620b5f022SMark Johnston 		*newp = new;
141720b5f022SMark Johnston 	return (0);
141882cd038dSYoshinobu Inoue }
141982cd038dSYoshinobu Inoue 
1420d748f7efSMark Johnston /*
1421d748f7efSMark Johnston  * Remove a prefix from the prefix list and optionally stash it in a
1422d748f7efSMark Johnston  * caller-provided list.
1423d748f7efSMark Johnston  *
1424d748f7efSMark Johnston  * The ND6 lock must be held.
1425d748f7efSMark Johnston  */
142682cd038dSYoshinobu Inoue void
1427d748f7efSMark Johnston nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1428d748f7efSMark Johnston {
1429d748f7efSMark Johnston 
1430d748f7efSMark Johnston 	ND6_WLOCK_ASSERT();
1431d748f7efSMark Johnston 
1432d748f7efSMark Johnston 	LIST_REMOVE(pr, ndpr_entry);
1433d748f7efSMark Johnston 	V_nd6_list_genid++;
1434d748f7efSMark Johnston 	if (list != NULL)
1435d748f7efSMark Johnston 		LIST_INSERT_HEAD(list, pr, ndpr_entry);
1436d748f7efSMark Johnston }
1437d748f7efSMark Johnston 
1438d748f7efSMark Johnston /*
1439d748f7efSMark Johnston  * Free an unlinked prefix, first marking it off-link if necessary.
1440d748f7efSMark Johnston  */
1441d748f7efSMark Johnston void
1442d748f7efSMark Johnston nd6_prefix_del(struct nd_prefix *pr)
144382cd038dSYoshinobu Inoue {
144482cd038dSYoshinobu Inoue 	struct nd_pfxrouter *pfr, *next;
1445c9b652e3SAndre Oppermann 	int e;
14461d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
144733841545SHajimu UMEMOTO 
1448d748f7efSMark Johnston 	KASSERT(pr->ndpr_addrcnt == 0,
1449d748f7efSMark Johnston 	    ("prefix %p has referencing addresses", pr));
1450d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
1451743eee66SSUZUKI Shinsuke 
145233841545SHajimu UMEMOTO 	/*
145333841545SHajimu UMEMOTO 	 * Though these flags are now meaningless, we'd rather keep the value
14547aa59493SSUZUKI Shinsuke 	 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
14557aa59493SSUZUKI Shinsuke 	 * when executing "ndp -p".
145633841545SHajimu UMEMOTO 	 */
1457d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1458d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1459d748f7efSMark Johnston 		if ((e = nd6_prefix_offlink(pr)) != 0) {
1460d748f7efSMark Johnston 			nd6log((LOG_ERR,
1461d748f7efSMark Johnston 			    "nd6_prefix_del: failed to make %s/%d offlink "
146233841545SHajimu UMEMOTO 			    "on %s, errno=%d\n",
14631d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
146433841545SHajimu UMEMOTO 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
146533841545SHajimu UMEMOTO 			/* what should we do? */
146633841545SHajimu UMEMOTO 		}
1467d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
146882cd038dSYoshinobu Inoue 	}
1469d748f7efSMark Johnston 
1470d748f7efSMark Johnston 	/* Release references to routers that have advertised this prefix. */
1471d748f7efSMark Johnston 	ND6_WLOCK();
1472d748f7efSMark Johnston 	LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1473d748f7efSMark Johnston 		pfxrtr_del(pfr);
1474d748f7efSMark Johnston 	ND6_WUNLOCK();
1475d748f7efSMark Johnston 
1476d748f7efSMark Johnston 	nd6_prefix_rele(pr);
147782cd038dSYoshinobu Inoue 
147882cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
147982cd038dSYoshinobu Inoue }
148082cd038dSYoshinobu Inoue 
1481743eee66SSUZUKI Shinsuke static int
14821272577eSXin LI prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
14831272577eSXin LI     struct mbuf *m, int mcast)
148482cd038dSYoshinobu Inoue {
148533841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
148633841545SHajimu UMEMOTO 	struct ifaddr *ifa;
148733841545SHajimu UMEMOTO 	struct ifnet *ifp = new->ndpr_ifp;
148882cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
148982cd038dSYoshinobu Inoue 	int error = 0;
149082cd038dSYoshinobu Inoue 	int auth;
149133841545SHajimu UMEMOTO 	struct in6_addrlifetime lt6_tmp;
14921d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1493b8a6e03fSGleb Smirnoff 
1494b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
149582cd038dSYoshinobu Inoue 
149682cd038dSYoshinobu Inoue 	auth = 0;
149782cd038dSYoshinobu Inoue 	if (m) {
149882cd038dSYoshinobu Inoue 		/*
149982cd038dSYoshinobu Inoue 		 * Authenticity for NA consists authentication for
150082cd038dSYoshinobu Inoue 		 * both IP header and IP datagrams, doesn't it ?
150182cd038dSYoshinobu Inoue 		 */
150282cd038dSYoshinobu Inoue #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
150307eb2995SHajimu UMEMOTO 		auth = ((m->m_flags & M_AUTHIPHDR) &&
150407eb2995SHajimu UMEMOTO 		    (m->m_flags & M_AUTHIPDGM));
150582cd038dSYoshinobu Inoue #endif
150682cd038dSYoshinobu Inoue 	}
150782cd038dSYoshinobu Inoue 
150833841545SHajimu UMEMOTO 	if ((pr = nd6_prefix_lookup(new)) != NULL) {
150933841545SHajimu UMEMOTO 		/*
151033841545SHajimu UMEMOTO 		 * nd6_prefix_lookup() ensures that pr and new have the same
151133841545SHajimu UMEMOTO 		 * prefix on a same interface.
151233841545SHajimu UMEMOTO 		 */
151333841545SHajimu UMEMOTO 
151433841545SHajimu UMEMOTO 		/*
151533841545SHajimu UMEMOTO 		 * Update prefix information.  Note that the on-link (L) bit
151633841545SHajimu UMEMOTO 		 * and the autonomous (A) bit should NOT be changed from 1
151733841545SHajimu UMEMOTO 		 * to 0.
151833841545SHajimu UMEMOTO 		 */
151933841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 1)
152033841545SHajimu UMEMOTO 			pr->ndpr_raf_onlink = 1;
152133841545SHajimu UMEMOTO 		if (new->ndpr_raf_auto == 1)
152233841545SHajimu UMEMOTO 			pr->ndpr_raf_auto = 1;
152333841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink) {
152482cd038dSYoshinobu Inoue 			pr->ndpr_vltime = new->ndpr_vltime;
152582cd038dSYoshinobu Inoue 			pr->ndpr_pltime = new->ndpr_pltime;
1526743eee66SSUZUKI Shinsuke 			(void)in6_init_prefix_ltimes(pr); /* XXX error case? */
15277d26db17SHiroki Sato 			pr->ndpr_lastupdate = time_uptime;
152882cd038dSYoshinobu Inoue 		}
152982cd038dSYoshinobu Inoue 
153033841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink &&
153133841545SHajimu UMEMOTO 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1532d748f7efSMark Johnston 			ND6_ONLINK_LOCK();
1533d748f7efSMark Johnston 			if ((error = nd6_prefix_onlink(pr)) != 0) {
153433841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
153533841545SHajimu UMEMOTO 				    "prelist_update: failed to make "
153633841545SHajimu UMEMOTO 				    "the prefix %s/%d on-link on %s "
153733841545SHajimu UMEMOTO 				    "(errno=%d)\n",
15381d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
15391d54aa3bSBjoern A. Zeeb 				        &pr->ndpr_prefix.sin6_addr),
1540d748f7efSMark Johnston 				    pr->ndpr_plen, if_name(pr->ndpr_ifp),
1541d748f7efSMark Johnston 				    error));
154233841545SHajimu UMEMOTO 				/* proceed anyway. XXX: is it correct? */
1543686cdd19SJun-ichiro itojun Hagino 			}
1544d748f7efSMark Johnston 			ND6_ONLINK_UNLOCK();
154582cd038dSYoshinobu Inoue 		}
154682cd038dSYoshinobu Inoue 
1547d748f7efSMark Johnston 		if (dr != NULL)
154882cd038dSYoshinobu Inoue 			pfxrtr_add(pr, dr);
154982cd038dSYoshinobu Inoue 	} else {
155033841545SHajimu UMEMOTO 		if (new->ndpr_vltime == 0)
155133841545SHajimu UMEMOTO 			goto end;
155233841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
155333841545SHajimu UMEMOTO 			goto end;
155482cd038dSYoshinobu Inoue 
155583631b16SMark Johnston 		error = nd6_prelist_add(new, dr, &pr);
155620b5f022SMark Johnston 		if (error != 0) {
155733841545SHajimu UMEMOTO 			nd6log((LOG_NOTICE, "prelist_update: "
155820b5f022SMark Johnston 			    "nd6_prelist_add failed for %s/%d on %s errno=%d\n",
15591d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
156020b5f022SMark Johnston 			    new->ndpr_plen, if_name(new->ndpr_ifp), error));
156133841545SHajimu UMEMOTO 			goto end; /* we should just give up in this case. */
156233841545SHajimu UMEMOTO 		}
156333841545SHajimu UMEMOTO 
156482cd038dSYoshinobu Inoue 		/*
156533841545SHajimu UMEMOTO 		 * XXX: from the ND point of view, we can ignore a prefix
156633841545SHajimu UMEMOTO 		 * with the on-link bit being zero.  However, we need a
156733841545SHajimu UMEMOTO 		 * prefix structure for references from autoconfigured
156807eb2995SHajimu UMEMOTO 		 * addresses.  Thus, we explicitly make sure that the prefix
156933841545SHajimu UMEMOTO 		 * itself expires now.
157082cd038dSYoshinobu Inoue 		 */
157183631b16SMark Johnston 		if (pr->ndpr_raf_onlink == 0) {
157283631b16SMark Johnston 			pr->ndpr_vltime = 0;
157383631b16SMark Johnston 			pr->ndpr_pltime = 0;
157483631b16SMark Johnston 			in6_init_prefix_ltimes(pr);
157533841545SHajimu UMEMOTO 		}
157633841545SHajimu UMEMOTO 	}
157733841545SHajimu UMEMOTO 
157833841545SHajimu UMEMOTO 	/*
157933841545SHajimu UMEMOTO 	 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
158033841545SHajimu UMEMOTO 	 * Note that pr must be non NULL at this point.
158133841545SHajimu UMEMOTO 	 */
158233841545SHajimu UMEMOTO 
158333841545SHajimu UMEMOTO 	/* 5.5.3 (a). Ignore the prefix without the A bit set. */
158482cd038dSYoshinobu Inoue 	if (!new->ndpr_raf_auto)
1585743eee66SSUZUKI Shinsuke 		goto end;
158682cd038dSYoshinobu Inoue 
158733841545SHajimu UMEMOTO 	/*
158833841545SHajimu UMEMOTO 	 * 5.5.3 (b). the link-local prefix should have been ignored in
158933841545SHajimu UMEMOTO 	 * nd6_ra_input.
159033841545SHajimu UMEMOTO 	 */
159133841545SHajimu UMEMOTO 
1592743eee66SSUZUKI Shinsuke 	/* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1593743eee66SSUZUKI Shinsuke 	if (new->ndpr_pltime > new->ndpr_vltime) {
1594743eee66SSUZUKI Shinsuke 		error = EINVAL;	/* XXX: won't be used */
1595743eee66SSUZUKI Shinsuke 		goto end;
1596743eee66SSUZUKI Shinsuke 	}
159733841545SHajimu UMEMOTO 
159833841545SHajimu UMEMOTO 	/*
1599743eee66SSUZUKI Shinsuke 	 * 5.5.3 (d).  If the prefix advertised is not equal to the prefix of
1600743eee66SSUZUKI Shinsuke 	 * an address configured by stateless autoconfiguration already in the
1601743eee66SSUZUKI Shinsuke 	 * list of addresses associated with the interface, and the Valid
1602743eee66SSUZUKI Shinsuke 	 * Lifetime is not 0, form an address.  We first check if we have
1603743eee66SSUZUKI Shinsuke 	 * a matching prefix.
1604743eee66SSUZUKI Shinsuke 	 * Note: we apply a clarification in rfc2462bis-02 here.  We only
1605743eee66SSUZUKI Shinsuke 	 * consider autoconfigured addresses while RFC2462 simply said
1606743eee66SSUZUKI Shinsuke 	 * "address".
160733841545SHajimu UMEMOTO 	 */
1608d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
160933841545SHajimu UMEMOTO 		struct in6_ifaddr *ifa6;
1610743eee66SSUZUKI Shinsuke 		u_int32_t remaininglifetime;
161133841545SHajimu UMEMOTO 
161233841545SHajimu UMEMOTO 		if (ifa->ifa_addr->sa_family != AF_INET6)
161333841545SHajimu UMEMOTO 			continue;
161433841545SHajimu UMEMOTO 
161533841545SHajimu UMEMOTO 		ifa6 = (struct in6_ifaddr *)ifa;
161633841545SHajimu UMEMOTO 
161733841545SHajimu UMEMOTO 		/*
1618743eee66SSUZUKI Shinsuke 		 * We only consider autoconfigured addresses as per rfc2462bis.
1619743eee66SSUZUKI Shinsuke 		 */
1620743eee66SSUZUKI Shinsuke 		if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1621743eee66SSUZUKI Shinsuke 			continue;
1622743eee66SSUZUKI Shinsuke 
1623743eee66SSUZUKI Shinsuke 		/*
162433841545SHajimu UMEMOTO 		 * Spec is not clear here, but I believe we should concentrate
162533841545SHajimu UMEMOTO 		 * on unicast (i.e. not anycast) addresses.
162633841545SHajimu UMEMOTO 		 * XXX: other ia6_flags? detached or duplicated?
162733841545SHajimu UMEMOTO 		 */
162833841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
162933841545SHajimu UMEMOTO 			continue;
163033841545SHajimu UMEMOTO 
1631743eee66SSUZUKI Shinsuke 		/*
1632743eee66SSUZUKI Shinsuke 		 * Ignore the address if it is not associated with a prefix
1633743eee66SSUZUKI Shinsuke 		 * or is associated with a prefix that is different from this
1634743eee66SSUZUKI Shinsuke 		 * one.  (pr is never NULL here)
1635743eee66SSUZUKI Shinsuke 		 */
1636743eee66SSUZUKI Shinsuke 		if (ifa6->ia6_ndpr != pr)
163733841545SHajimu UMEMOTO 			continue;
163833841545SHajimu UMEMOTO 
163933841545SHajimu UMEMOTO 		if (ia6_match == NULL) /* remember the first one */
164033841545SHajimu UMEMOTO 			ia6_match = ifa6;
164133841545SHajimu UMEMOTO 
164233841545SHajimu UMEMOTO 		/*
164333841545SHajimu UMEMOTO 		 * An already autoconfigured address matched.  Now that we
164433841545SHajimu UMEMOTO 		 * are sure there is at least one matched address, we can
164533841545SHajimu UMEMOTO 		 * proceed to 5.5.3. (e): update the lifetimes according to the
164633841545SHajimu UMEMOTO 		 * "two hours" rule and the privacy extension.
1647743eee66SSUZUKI Shinsuke 		 * We apply some clarifications in rfc2462bis:
1648743eee66SSUZUKI Shinsuke 		 * - use remaininglifetime instead of storedlifetime as a
1649743eee66SSUZUKI Shinsuke 		 *   variable name
1650743eee66SSUZUKI Shinsuke 		 * - remove the dead code in the "two-hour" rule
165133841545SHajimu UMEMOTO 		 */
165233841545SHajimu UMEMOTO #define TWOHOUR		(120*60)
165333841545SHajimu UMEMOTO 		lt6_tmp = ifa6->ia6_lifetime;
165433841545SHajimu UMEMOTO 
165511f3a6e2SHajimu UMEMOTO 		if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1656743eee66SSUZUKI Shinsuke 			remaininglifetime = ND6_INFINITE_LIFETIME;
16577d26db17SHiroki Sato 		else if (time_uptime - ifa6->ia6_updatetime >
1658743eee66SSUZUKI Shinsuke 			 lt6_tmp.ia6t_vltime) {
1659743eee66SSUZUKI Shinsuke 			/*
1660743eee66SSUZUKI Shinsuke 			 * The case of "invalid" address.  We should usually
1661743eee66SSUZUKI Shinsuke 			 * not see this case.
1662743eee66SSUZUKI Shinsuke 			 */
1663743eee66SSUZUKI Shinsuke 			remaininglifetime = 0;
1664743eee66SSUZUKI Shinsuke 		} else
1665743eee66SSUZUKI Shinsuke 			remaininglifetime = lt6_tmp.ia6t_vltime -
16667d26db17SHiroki Sato 			    (time_uptime - ifa6->ia6_updatetime);
166711f3a6e2SHajimu UMEMOTO 
166811f3a6e2SHajimu UMEMOTO 		/* when not updating, keep the current stored lifetime. */
1669743eee66SSUZUKI Shinsuke 		lt6_tmp.ia6t_vltime = remaininglifetime;
167033841545SHajimu UMEMOTO 
167133841545SHajimu UMEMOTO 		if (TWOHOUR < new->ndpr_vltime ||
1672743eee66SSUZUKI Shinsuke 		    remaininglifetime < new->ndpr_vltime) {
167333841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1674743eee66SSUZUKI Shinsuke 		} else if (remaininglifetime <= TWOHOUR) {
167533841545SHajimu UMEMOTO 			if (auth) {
167633841545SHajimu UMEMOTO 				lt6_tmp.ia6t_vltime = new->ndpr_vltime;
167782cd038dSYoshinobu Inoue 			}
167833841545SHajimu UMEMOTO 		} else {
167933841545SHajimu UMEMOTO 			/*
168033841545SHajimu UMEMOTO 			 * new->ndpr_vltime <= TWOHOUR &&
1681743eee66SSUZUKI Shinsuke 			 * TWOHOUR < remaininglifetime
168233841545SHajimu UMEMOTO 			 */
168333841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = TWOHOUR;
168482cd038dSYoshinobu Inoue 		}
168582cd038dSYoshinobu Inoue 
168633841545SHajimu UMEMOTO 		/* The 2 hour rule is not imposed for preferred lifetime. */
168733841545SHajimu UMEMOTO 		lt6_tmp.ia6t_pltime = new->ndpr_pltime;
168833841545SHajimu UMEMOTO 
168933841545SHajimu UMEMOTO 		in6_init_address_ltimes(pr, &lt6_tmp);
169033841545SHajimu UMEMOTO 
169133841545SHajimu UMEMOTO 		/*
1692743eee66SSUZUKI Shinsuke 		 * We need to treat lifetimes for temporary addresses
1693743eee66SSUZUKI Shinsuke 		 * differently, according to
1694743eee66SSUZUKI Shinsuke 		 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1695743eee66SSUZUKI Shinsuke 		 * we only update the lifetimes when they are in the maximum
1696743eee66SSUZUKI Shinsuke 		 * intervals.
169733841545SHajimu UMEMOTO 		 */
169833841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1699743eee66SSUZUKI Shinsuke 			u_int32_t maxvltime, maxpltime;
170033841545SHajimu UMEMOTO 
1701603724d3SBjoern A. Zeeb 			if (V_ip6_temp_valid_lifetime >
17027d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1703603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1704603724d3SBjoern A. Zeeb 				maxvltime = V_ip6_temp_valid_lifetime -
17057d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1706603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1707743eee66SSUZUKI Shinsuke 			} else
1708743eee66SSUZUKI Shinsuke 				maxvltime = 0;
1709603724d3SBjoern A. Zeeb 			if (V_ip6_temp_preferred_lifetime >
17107d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1711603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1712603724d3SBjoern A. Zeeb 				maxpltime = V_ip6_temp_preferred_lifetime -
17137d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1714603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1715743eee66SSUZUKI Shinsuke 			} else
1716743eee66SSUZUKI Shinsuke 				maxpltime = 0;
1717743eee66SSUZUKI Shinsuke 
1718743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1719743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_vltime > maxvltime) {
1720743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_vltime = maxvltime;
1721743eee66SSUZUKI Shinsuke 			}
1722743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1723743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_pltime > maxpltime) {
1724743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_pltime = maxpltime;
1725743eee66SSUZUKI Shinsuke 			}
1726743eee66SSUZUKI Shinsuke 		}
172733841545SHajimu UMEMOTO 		ifa6->ia6_lifetime = lt6_tmp;
17287d26db17SHiroki Sato 		ifa6->ia6_updatetime = time_uptime;
172933841545SHajimu UMEMOTO 	}
173033841545SHajimu UMEMOTO 	if (ia6_match == NULL && new->ndpr_vltime) {
1731743eee66SSUZUKI Shinsuke 		int ifidlen;
1732743eee66SSUZUKI Shinsuke 
173333841545SHajimu UMEMOTO 		/*
1734743eee66SSUZUKI Shinsuke 		 * 5.5.3 (d) (continued)
173533841545SHajimu UMEMOTO 		 * No address matched and the valid lifetime is non-zero.
173633841545SHajimu UMEMOTO 		 * Create a new address.
173733841545SHajimu UMEMOTO 		 */
1738743eee66SSUZUKI Shinsuke 
1739743eee66SSUZUKI Shinsuke 		/*
1740743eee66SSUZUKI Shinsuke 		 * Prefix Length check:
1741743eee66SSUZUKI Shinsuke 		 * If the sum of the prefix length and interface identifier
1742743eee66SSUZUKI Shinsuke 		 * length does not equal 128 bits, the Prefix Information
1743743eee66SSUZUKI Shinsuke 		 * option MUST be ignored.  The length of the interface
1744743eee66SSUZUKI Shinsuke 		 * identifier is defined in a separate link-type specific
1745743eee66SSUZUKI Shinsuke 		 * document.
1746743eee66SSUZUKI Shinsuke 		 */
1747743eee66SSUZUKI Shinsuke 		ifidlen = in6_if2idlen(ifp);
1748743eee66SSUZUKI Shinsuke 		if (ifidlen < 0) {
1749743eee66SSUZUKI Shinsuke 			/* this should not happen, so we always log it. */
1750743eee66SSUZUKI Shinsuke 			log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1751743eee66SSUZUKI Shinsuke 			    if_name(ifp));
1752743eee66SSUZUKI Shinsuke 			goto end;
1753743eee66SSUZUKI Shinsuke 		}
1754743eee66SSUZUKI Shinsuke 		if (ifidlen + pr->ndpr_plen != 128) {
1755743eee66SSUZUKI Shinsuke 			nd6log((LOG_INFO,
1756743eee66SSUZUKI Shinsuke 			    "prelist_update: invalid prefixlen "
1757743eee66SSUZUKI Shinsuke 			    "%d for %s, ignored\n",
1758743eee66SSUZUKI Shinsuke 			    pr->ndpr_plen, if_name(ifp)));
1759743eee66SSUZUKI Shinsuke 			goto end;
1760743eee66SSUZUKI Shinsuke 		}
1761743eee66SSUZUKI Shinsuke 
1762743eee66SSUZUKI Shinsuke 		if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
176333841545SHajimu UMEMOTO 			/*
176433841545SHajimu UMEMOTO 			 * note that we should use pr (not new) for reference.
176533841545SHajimu UMEMOTO 			 */
17660bbf244eSMark Johnston 			pr->ndpr_addrcnt++;
176733841545SHajimu UMEMOTO 			ia6->ia6_ndpr = pr;
176833841545SHajimu UMEMOTO 
176933841545SHajimu UMEMOTO 			/*
177033841545SHajimu UMEMOTO 			 * RFC 3041 3.3 (2).
177133841545SHajimu UMEMOTO 			 * When a new public address is created as described
177233841545SHajimu UMEMOTO 			 * in RFC2462, also create a new temporary address.
177333841545SHajimu UMEMOTO 			 *
177433841545SHajimu UMEMOTO 			 * RFC 3041 3.5.
177533841545SHajimu UMEMOTO 			 * When an interface connects to a new link, a new
177633841545SHajimu UMEMOTO 			 * randomized interface identifier should be generated
177733841545SHajimu UMEMOTO 			 * immediately together with a new set of temporary
177833841545SHajimu UMEMOTO 			 * addresses.  Thus, we specifiy 1 as the 2nd arg of
177933841545SHajimu UMEMOTO 			 * in6_tmpifadd().
178033841545SHajimu UMEMOTO 			 */
1781603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr) {
178233841545SHajimu UMEMOTO 				int e;
1783743eee66SSUZUKI Shinsuke 				if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
178433841545SHajimu UMEMOTO 					nd6log((LOG_NOTICE, "prelist_update: "
178533841545SHajimu UMEMOTO 					    "failed to create a temporary "
178633841545SHajimu UMEMOTO 					    "address, errno=%d\n",
178733841545SHajimu UMEMOTO 					    e));
178833841545SHajimu UMEMOTO 				}
178933841545SHajimu UMEMOTO 			}
17908c0fec80SRobert Watson 			ifa_free(&ia6->ia_ifa);
179133841545SHajimu UMEMOTO 
179233841545SHajimu UMEMOTO 			/*
179333841545SHajimu UMEMOTO 			 * A newly added address might affect the status
179433841545SHajimu UMEMOTO 			 * of other addresses, so we check and update it.
179533841545SHajimu UMEMOTO 			 * XXX: what if address duplication happens?
179633841545SHajimu UMEMOTO 			 */
179733841545SHajimu UMEMOTO 			pfxlist_onlink_check();
179833841545SHajimu UMEMOTO 		} else {
179933841545SHajimu UMEMOTO 			/* just set an error. do not bark here. */
180033841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL; /* XXX: might be unused. */
180133841545SHajimu UMEMOTO 		}
180233841545SHajimu UMEMOTO 	}
180333841545SHajimu UMEMOTO 
180482cd038dSYoshinobu Inoue end:
1805d748f7efSMark Johnston 	if (pr != NULL)
1806d748f7efSMark Johnston 		nd6_prefix_rele(pr);
1807d748f7efSMark Johnston 	return (error);
180882cd038dSYoshinobu Inoue }
180982cd038dSYoshinobu Inoue 
181082cd038dSYoshinobu Inoue /*
1811686cdd19SJun-ichiro itojun Hagino  * A supplement function used in the on-link detection below;
1812686cdd19SJun-ichiro itojun Hagino  * detect if a given prefix has a (probably) reachable advertising router.
1813686cdd19SJun-ichiro itojun Hagino  * XXX: lengthy function name...
1814686cdd19SJun-ichiro itojun Hagino  */
181533841545SHajimu UMEMOTO static struct nd_pfxrouter *
18161272577eSXin LI find_pfxlist_reachable_router(struct nd_prefix *pr)
1817686cdd19SJun-ichiro itojun Hagino {
1818a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1819686cdd19SJun-ichiro itojun Hagino 	struct nd_pfxrouter *pfxrtr;
18206e6b3f7cSQing Li 	struct llentry *ln;
1821d78be3a9SKip Macy 	int canreach;
1822686cdd19SJun-ichiro itojun Hagino 
1823d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1824d748f7efSMark Johnston 
1825a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1826b8a6e03fSGleb Smirnoff 	LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1827d78be3a9SKip Macy 		ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1828a6146780SKip Macy 		if (ln == NULL)
1829a6146780SKip Macy 			continue;
1830d78be3a9SKip Macy 		canreach = ND6_IS_LLINFO_PROBREACH(ln);
1831d78be3a9SKip Macy 		LLE_RUNLOCK(ln);
1832d78be3a9SKip Macy 		if (canreach)
1833d78be3a9SKip Macy 			break;
18346e6b3f7cSQing Li 	}
1835b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
1836686cdd19SJun-ichiro itojun Hagino 	return (pfxrtr);
1837686cdd19SJun-ichiro itojun Hagino }
1838686cdd19SJun-ichiro itojun Hagino 
1839686cdd19SJun-ichiro itojun Hagino /*
184082cd038dSYoshinobu Inoue  * Check if each prefix in the prefix list has at least one available router
184133841545SHajimu UMEMOTO  * that advertised the prefix (a router is "available" if its neighbor cache
184233841545SHajimu UMEMOTO  * entry is reachable or probably reachable).
1843686cdd19SJun-ichiro itojun Hagino  * If the check fails, the prefix may be off-link, because, for example,
184482cd038dSYoshinobu Inoue  * we have moved from the network but the lifetime of the prefix has not
184533841545SHajimu UMEMOTO  * expired yet.  So we should not use the prefix if there is another prefix
184633841545SHajimu UMEMOTO  * that has an available router.
184707c1f959SMark Johnston  * But, if there is no prefix that has an available router, we still regard
184882cd038dSYoshinobu Inoue  * all the prefixes as on-link.  This is because we can't tell if all the
184982cd038dSYoshinobu Inoue  * routers are simply dead or if we really moved from the network and there
185082cd038dSYoshinobu Inoue  * is no router around us.
185182cd038dSYoshinobu Inoue  */
1852686cdd19SJun-ichiro itojun Hagino void
18531b28988bSMark Johnston pfxlist_onlink_check(void)
185482cd038dSYoshinobu Inoue {
185582cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
185633841545SHajimu UMEMOTO 	struct in6_ifaddr *ifa;
1857743eee66SSUZUKI Shinsuke 	struct nd_defrouter *dr;
1858743eee66SSUZUKI Shinsuke 	struct nd_pfxrouter *pfxrtr = NULL;
18591b28988bSMark Johnston 	struct rm_priotracker in6_ifa_tracker;
1860d748f7efSMark Johnston 	uint64_t genid;
1861d748f7efSMark Johnston 	uint32_t flags;
1862d748f7efSMark Johnston 
1863d748f7efSMark Johnston 	ND6_ONLINK_LOCK();
1864d748f7efSMark Johnston 	ND6_RLOCK();
186582cd038dSYoshinobu Inoue 
1866686cdd19SJun-ichiro itojun Hagino 	/*
1867686cdd19SJun-ichiro itojun Hagino 	 * Check if there is a prefix that has a reachable advertising
1868686cdd19SJun-ichiro itojun Hagino 	 * router.
1869686cdd19SJun-ichiro itojun Hagino 	 */
18703b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
187133841545SHajimu UMEMOTO 		if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
187282cd038dSYoshinobu Inoue 			break;
1873686cdd19SJun-ichiro itojun Hagino 	}
187482cd038dSYoshinobu Inoue 
187582cd038dSYoshinobu Inoue 	/*
1876743eee66SSUZUKI Shinsuke 	 * If we have no such prefix, check whether we still have a router
1877743eee66SSUZUKI Shinsuke 	 * that does not advertise any prefixes.
1878743eee66SSUZUKI Shinsuke 	 */
1879743eee66SSUZUKI Shinsuke 	if (pr == NULL) {
1880f592d0c3SBjoern A. Zeeb 		TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1881743eee66SSUZUKI Shinsuke 			struct nd_prefix *pr0;
1882743eee66SSUZUKI Shinsuke 
18833b0b2840SJohn Baldwin 			LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1884743eee66SSUZUKI Shinsuke 				if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1885743eee66SSUZUKI Shinsuke 					break;
1886743eee66SSUZUKI Shinsuke 			}
1887743eee66SSUZUKI Shinsuke 			if (pfxrtr != NULL)
1888743eee66SSUZUKI Shinsuke 				break;
1889743eee66SSUZUKI Shinsuke 		}
1890743eee66SSUZUKI Shinsuke 	}
1891f592d0c3SBjoern A. Zeeb 	if (pr != NULL || (!TAILQ_EMPTY(&V_nd6_defrouter) && pfxrtr == NULL)) {
1892743eee66SSUZUKI Shinsuke 		/*
1893743eee66SSUZUKI Shinsuke 		 * There is at least one prefix that has a reachable router,
1894743eee66SSUZUKI Shinsuke 		 * or at least a router which probably does not advertise
1895743eee66SSUZUKI Shinsuke 		 * any prefixes.  The latter would be the case when we move
1896743eee66SSUZUKI Shinsuke 		 * to a new link where we have a router that does not provide
1897743eee66SSUZUKI Shinsuke 		 * prefixes and we configure an address by hand.
189833841545SHajimu UMEMOTO 		 * Detach prefixes which have no reachable advertising
189933841545SHajimu UMEMOTO 		 * router, and attach other prefixes.
190082cd038dSYoshinobu Inoue 		 */
19013b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
190233841545SHajimu UMEMOTO 			/* XXX: a link-local prefix should never be detached */
1903a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1904a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1905a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
190642cb3aa4SQing Li 				continue;
190742cb3aa4SQing Li 
190833841545SHajimu UMEMOTO 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
190933841545SHajimu UMEMOTO 			    find_pfxlist_reachable_router(pr) == NULL)
191033841545SHajimu UMEMOTO 				pr->ndpr_stateflags |= NDPRF_DETACHED;
1911a88d6d7eSMark Johnston 			else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1912155d72c4SPedro F. Giffuni 			    find_pfxlist_reachable_router(pr) != NULL)
191333841545SHajimu UMEMOTO 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
191482cd038dSYoshinobu Inoue 		}
191533841545SHajimu UMEMOTO 	} else {
191633841545SHajimu UMEMOTO 		/* there is no prefix that has a reachable router */
19173b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1918a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1919a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1920a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
192133841545SHajimu UMEMOTO 				continue;
192233841545SHajimu UMEMOTO 			pr->ndpr_stateflags &= ~NDPRF_DETACHED;
192333841545SHajimu UMEMOTO 		}
192433841545SHajimu UMEMOTO 	}
192533841545SHajimu UMEMOTO 
192633841545SHajimu UMEMOTO 	/*
192733841545SHajimu UMEMOTO 	 * Remove each interface route associated with a (just) detached
192833841545SHajimu UMEMOTO 	 * prefix, and reinstall the interface route for a (just) attached
192933841545SHajimu UMEMOTO 	 * prefix.  Note that all attempt of reinstallation does not
193033841545SHajimu UMEMOTO 	 * necessarily success, when a same prefix is shared among multiple
193133841545SHajimu UMEMOTO 	 * interfaces.  Such cases will be handled in nd6_prefix_onlink,
193233841545SHajimu UMEMOTO 	 * so we don't have to care about them.
193333841545SHajimu UMEMOTO 	 */
1934d748f7efSMark Johnston restart:
19353b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
19361d54aa3bSBjoern A. Zeeb 		char ip6buf[INET6_ADDRSTRLEN];
1937a88d6d7eSMark Johnston 		int e;
193833841545SHajimu UMEMOTO 
1939a88d6d7eSMark Johnston 		if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1940a88d6d7eSMark Johnston 		    pr->ndpr_raf_onlink == 0 ||
1941a88d6d7eSMark Johnston 		    pr->ndpr_raf_auto == 0)
194242cb3aa4SQing Li 			continue;
194342cb3aa4SQing Li 
1944d748f7efSMark Johnston 		flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1945d748f7efSMark Johnston 		if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1946d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1947d748f7efSMark Johnston 			ND6_RUNLOCK();
1948d748f7efSMark Johnston 			if ((flags & NDPRF_ONLINK) != 0 &&
1949d748f7efSMark Johnston 			    (e = nd6_prefix_offlink(pr)) != 0) {
195033841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
195133841545SHajimu UMEMOTO 				    "pfxlist_onlink_check: failed to "
19527aa59493SSUZUKI Shinsuke 				    "make %s/%d offlink, errno=%d\n",
19531d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
19541d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
195533841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
1956d748f7efSMark Johnston 			} else if ((flags & NDPRF_ONLINK) == 0 &&
1957d748f7efSMark Johnston 			    (e = nd6_prefix_onlink(pr)) != 0) {
195833841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
195933841545SHajimu UMEMOTO 				    "pfxlist_onlink_check: failed to "
19607aa59493SSUZUKI Shinsuke 				    "make %s/%d onlink, errno=%d\n",
19611d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
19621d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
196333841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
196433841545SHajimu UMEMOTO 			}
1965d748f7efSMark Johnston 			ND6_RLOCK();
1966d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1967d748f7efSMark Johnston 				goto restart;
196833841545SHajimu UMEMOTO 		}
196933841545SHajimu UMEMOTO 	}
197033841545SHajimu UMEMOTO 
197133841545SHajimu UMEMOTO 	/*
197233841545SHajimu UMEMOTO 	 * Changes on the prefix status might affect address status as well.
197333841545SHajimu UMEMOTO 	 * Make sure that all addresses derived from an attached prefix are
197433841545SHajimu UMEMOTO 	 * attached, and that all addresses derived from a detached prefix are
197533841545SHajimu UMEMOTO 	 * detached.  Note, however, that a manually configured address should
197633841545SHajimu UMEMOTO 	 * always be attached.
197733841545SHajimu UMEMOTO 	 * The precise detection logic is same as the one for prefixes.
197833841545SHajimu UMEMOTO 	 */
19791b28988bSMark Johnston 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1980d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
198107eb2995SHajimu UMEMOTO 		if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
198233841545SHajimu UMEMOTO 			continue;
198333841545SHajimu UMEMOTO 
198433841545SHajimu UMEMOTO 		if (ifa->ia6_ndpr == NULL) {
198533841545SHajimu UMEMOTO 			/*
198633841545SHajimu UMEMOTO 			 * This can happen when we first configure the address
198733841545SHajimu UMEMOTO 			 * (i.e. the address exists, but the prefix does not).
198833841545SHajimu UMEMOTO 			 * XXX: complicated relationships...
198933841545SHajimu UMEMOTO 			 */
199033841545SHajimu UMEMOTO 			continue;
199133841545SHajimu UMEMOTO 		}
199233841545SHajimu UMEMOTO 
199333841545SHajimu UMEMOTO 		if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
199433841545SHajimu UMEMOTO 			break;
199533841545SHajimu UMEMOTO 	}
199633841545SHajimu UMEMOTO 	if (ifa) {
1997d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
199833841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
199933841545SHajimu UMEMOTO 				continue;
200033841545SHajimu UMEMOTO 
200133841545SHajimu UMEMOTO 			if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
200233841545SHajimu UMEMOTO 				continue;
200333841545SHajimu UMEMOTO 
2004743eee66SSUZUKI Shinsuke 			if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
2005743eee66SSUZUKI Shinsuke 				if (ifa->ia6_flags & IN6_IFF_DETACHED) {
200633841545SHajimu UMEMOTO 					ifa->ia6_flags &= ~IN6_IFF_DETACHED;
2007743eee66SSUZUKI Shinsuke 					ifa->ia6_flags |= IN6_IFF_TENTATIVE;
2008743eee66SSUZUKI Shinsuke 					nd6_dad_start((struct ifaddr *)ifa, 0);
2009743eee66SSUZUKI Shinsuke 				}
2010743eee66SSUZUKI Shinsuke 			} else {
201133841545SHajimu UMEMOTO 				ifa->ia6_flags |= IN6_IFF_DETACHED;
201282cd038dSYoshinobu Inoue 			}
2013686cdd19SJun-ichiro itojun Hagino 		}
20141b28988bSMark Johnston 	} else {
2015d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
201633841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
201733841545SHajimu UMEMOTO 				continue;
201833841545SHajimu UMEMOTO 
2019743eee66SSUZUKI Shinsuke 			if (ifa->ia6_flags & IN6_IFF_DETACHED) {
202033841545SHajimu UMEMOTO 				ifa->ia6_flags &= ~IN6_IFF_DETACHED;
2021743eee66SSUZUKI Shinsuke 				ifa->ia6_flags |= IN6_IFF_TENTATIVE;
2022743eee66SSUZUKI Shinsuke 				/* Do we need a delay in this case? */
2023743eee66SSUZUKI Shinsuke 				nd6_dad_start((struct ifaddr *)ifa, 0);
2024743eee66SSUZUKI Shinsuke 			}
202533841545SHajimu UMEMOTO 		}
202682cd038dSYoshinobu Inoue 	}
20271b28988bSMark Johnston 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
2028d748f7efSMark Johnston 	ND6_RUNLOCK();
2029d748f7efSMark Johnston 	ND6_ONLINK_UNLOCK();
203082cd038dSYoshinobu Inoue }
203182cd038dSYoshinobu Inoue 
20329d9b92f2SBjoern A. Zeeb static int
203381d5d46bSBjoern A. Zeeb nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
203481d5d46bSBjoern A. Zeeb {
2035563ab4e4SAlexander V. Chernikov 	struct sockaddr_dl sdl;
203681d5d46bSBjoern A. Zeeb 	struct rtentry *rt;
203781d5d46bSBjoern A. Zeeb 	struct sockaddr_in6 mask6;
203881d5d46bSBjoern A. Zeeb 	u_long rtflags;
2039559b4296SAlan Somers 	int error, a_failure, fibnum, maxfib;
204081d5d46bSBjoern A. Zeeb 
204181d5d46bSBjoern A. Zeeb 	/*
204281d5d46bSBjoern A. Zeeb 	 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
204381d5d46bSBjoern A. Zeeb 	 * ifa->ifa_rtrequest = nd6_rtrequest;
204481d5d46bSBjoern A. Zeeb 	 */
204581d5d46bSBjoern A. Zeeb 	bzero(&mask6, sizeof(mask6));
204681d5d46bSBjoern A. Zeeb 	mask6.sin6_len = sizeof(mask6);
204781d5d46bSBjoern A. Zeeb 	mask6.sin6_addr = pr->ndpr_mask;
204881d5d46bSBjoern A. Zeeb 	rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
204981d5d46bSBjoern A. Zeeb 
2050563ab4e4SAlexander V. Chernikov 	bzero(&sdl, sizeof(struct sockaddr_dl));
2051563ab4e4SAlexander V. Chernikov 	sdl.sdl_len = sizeof(struct sockaddr_dl);
2052563ab4e4SAlexander V. Chernikov 	sdl.sdl_family = AF_LINK;
2053563ab4e4SAlexander V. Chernikov 	sdl.sdl_type = ifa->ifa_ifp->if_type;
2054563ab4e4SAlexander V. Chernikov 	sdl.sdl_index = ifa->ifa_ifp->if_index;
2055563ab4e4SAlexander V. Chernikov 
2056559b4296SAlan Somers 	if(V_rt_add_addr_allfibs) {
2057559b4296SAlan Somers 		fibnum = 0;
2058559b4296SAlan Somers 		maxfib = rt_numfibs;
2059559b4296SAlan Somers 	} else {
2060559b4296SAlan Somers 		fibnum = ifa->ifa_ifp->if_fib;
2061559b4296SAlan Somers 		maxfib = fibnum + 1;
2062559b4296SAlan Somers 	}
206381d5d46bSBjoern A. Zeeb 	a_failure = 0;
2064559b4296SAlan Somers 	for (; fibnum < maxfib; fibnum++) {
206581d5d46bSBjoern A. Zeeb 
206681d5d46bSBjoern A. Zeeb 		rt = NULL;
206781d5d46bSBjoern A. Zeeb 		error = in6_rtrequest(RTM_ADD,
2068563ab4e4SAlexander V. Chernikov 		    (struct sockaddr *)&pr->ndpr_prefix, (struct sockaddr *)&sdl,
206981d5d46bSBjoern A. Zeeb 		    (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
207081d5d46bSBjoern A. Zeeb 		if (error == 0) {
207181d5d46bSBjoern A. Zeeb 			KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
207281d5d46bSBjoern A. Zeeb 			    "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
207381d5d46bSBjoern A. Zeeb 			    error, pr, ifa));
207481d5d46bSBjoern A. Zeeb 			RT_LOCK(rt);
207581d5d46bSBjoern A. Zeeb 			nd6_rtmsg(RTM_ADD, rt);
207681d5d46bSBjoern A. Zeeb 			RT_UNLOCK(rt);
207781d5d46bSBjoern A. Zeeb 			pr->ndpr_stateflags |= NDPRF_ONLINK;
207881d5d46bSBjoern A. Zeeb 		} else {
207981d5d46bSBjoern A. Zeeb 			char ip6buf[INET6_ADDRSTRLEN];
208081d5d46bSBjoern A. Zeeb 			char ip6bufg[INET6_ADDRSTRLEN];
208181d5d46bSBjoern A. Zeeb 			char ip6bufm[INET6_ADDRSTRLEN];
208281d5d46bSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
208381d5d46bSBjoern A. Zeeb 
208481d5d46bSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
208581d5d46bSBjoern A. Zeeb 			nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
208681d5d46bSBjoern A. Zeeb 			    "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
208781d5d46bSBjoern A. Zeeb 			    "flags=%lx errno = %d\n",
208881d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
208981d5d46bSBjoern A. Zeeb 			    pr->ndpr_plen, if_name(pr->ndpr_ifp),
209081d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufg, &sin6->sin6_addr),
209181d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufm, &mask6.sin6_addr),
209281d5d46bSBjoern A. Zeeb 			    rtflags, error));
209381d5d46bSBjoern A. Zeeb 
209481d5d46bSBjoern A. Zeeb 			/* Save last error to return, see rtinit(). */
209581d5d46bSBjoern A. Zeeb 			a_failure = error;
209681d5d46bSBjoern A. Zeeb 		}
209781d5d46bSBjoern A. Zeeb 
209881d5d46bSBjoern A. Zeeb 		if (rt != NULL) {
209981d5d46bSBjoern A. Zeeb 			RT_LOCK(rt);
210081d5d46bSBjoern A. Zeeb 			RT_REMREF(rt);
210181d5d46bSBjoern A. Zeeb 			RT_UNLOCK(rt);
210281d5d46bSBjoern A. Zeeb 		}
210381d5d46bSBjoern A. Zeeb 	}
210481d5d46bSBjoern A. Zeeb 
210581d5d46bSBjoern A. Zeeb 	/* Return the last error we got. */
210681d5d46bSBjoern A. Zeeb 	return (a_failure);
210781d5d46bSBjoern A. Zeeb }
210881d5d46bSBjoern A. Zeeb 
2109d748f7efSMark Johnston int
21101272577eSXin LI nd6_prefix_onlink(struct nd_prefix *pr)
211182cd038dSYoshinobu Inoue {
2112751d8d15SGleb Smirnoff 	struct epoch_tracker et;
211333841545SHajimu UMEMOTO 	struct ifaddr *ifa;
211433841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
211533841545SHajimu UMEMOTO 	struct nd_prefix *opr;
21161d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2117d748f7efSMark Johnston 	int error;
211833841545SHajimu UMEMOTO 
2119d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2120d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2121d748f7efSMark Johnston 
2122d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
21232ce62dceSSUZUKI Shinsuke 		return (EEXIST);
212482cd038dSYoshinobu Inoue 
212582cd038dSYoshinobu Inoue 	/*
212633841545SHajimu UMEMOTO 	 * Add the interface route associated with the prefix.  Before
212733841545SHajimu UMEMOTO 	 * installing the route, check if there's the same prefix on another
212833841545SHajimu UMEMOTO 	 * interface, and the prefix has already installed the interface route.
212933841545SHajimu UMEMOTO 	 * Although such a configuration is expected to be rare, we explicitly
213033841545SHajimu UMEMOTO 	 * allow it.
213182cd038dSYoshinobu Inoue 	 */
2132d748f7efSMark Johnston 	ND6_RLOCK();
21333b0b2840SJohn Baldwin 	LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
213433841545SHajimu UMEMOTO 		if (opr == pr)
213533841545SHajimu UMEMOTO 			continue;
213633841545SHajimu UMEMOTO 
213733841545SHajimu UMEMOTO 		if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
213833841545SHajimu UMEMOTO 			continue;
213933841545SHajimu UMEMOTO 
2140559b4296SAlan Somers 		if (!V_rt_add_addr_allfibs &&
2141559b4296SAlan Somers 		    opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2142559b4296SAlan Somers 			continue;
2143559b4296SAlan Somers 
214433841545SHajimu UMEMOTO 		if (opr->ndpr_plen == pr->ndpr_plen &&
214533841545SHajimu UMEMOTO 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2146d748f7efSMark Johnston 		    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2147d748f7efSMark Johnston 			ND6_RUNLOCK();
214833841545SHajimu UMEMOTO 			return (0);
214933841545SHajimu UMEMOTO 		}
2150d748f7efSMark Johnston 	}
2151d748f7efSMark Johnston 	ND6_RUNLOCK();
215233841545SHajimu UMEMOTO 
215333841545SHajimu UMEMOTO 	/*
215433841545SHajimu UMEMOTO 	 * We prefer link-local addresses as the associated interface address.
215533841545SHajimu UMEMOTO 	 */
215633841545SHajimu UMEMOTO 	/* search for a link-local addr */
2157751d8d15SGleb Smirnoff 	NET_EPOCH_ENTER(et);
215833841545SHajimu UMEMOTO 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
215907eb2995SHajimu UMEMOTO 	    IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
216033841545SHajimu UMEMOTO 	if (ifa == NULL) {
216133841545SHajimu UMEMOTO 		/* XXX: freebsd does not have ifa_ifwithaf */
2162d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2163c2615844SMark Johnston 			if (ifa->ifa_addr->sa_family == AF_INET6) {
2164c2615844SMark Johnston 				ifa_ref(ifa);
216533841545SHajimu UMEMOTO 				break;
216633841545SHajimu UMEMOTO 			}
2167c2615844SMark Johnston 		}
216833841545SHajimu UMEMOTO 		/* should we care about ia6_flags? */
216933841545SHajimu UMEMOTO 	}
2170751d8d15SGleb Smirnoff 	NET_EPOCH_EXIT(et);
217133841545SHajimu UMEMOTO 	if (ifa == NULL) {
217233841545SHajimu UMEMOTO 		/*
217333841545SHajimu UMEMOTO 		 * This can still happen, when, for example, we receive an RA
217433841545SHajimu UMEMOTO 		 * containing a prefix with the L bit set and the A bit clear,
217533841545SHajimu UMEMOTO 		 * after removing all IPv6 addresses on the receiving
217633841545SHajimu UMEMOTO 		 * interface.  This should, of course, be rare though.
217733841545SHajimu UMEMOTO 		 */
217833841545SHajimu UMEMOTO 		nd6log((LOG_NOTICE,
217933841545SHajimu UMEMOTO 		    "nd6_prefix_onlink: failed to find any ifaddr"
218033841545SHajimu UMEMOTO 		    " to add route for a prefix(%s/%d) on %s\n",
21811d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
218233841545SHajimu UMEMOTO 		    pr->ndpr_plen, if_name(ifp)));
218333841545SHajimu UMEMOTO 		return (0);
218433841545SHajimu UMEMOTO 	}
218533841545SHajimu UMEMOTO 
218681d5d46bSBjoern A. Zeeb 	error = nd6_prefix_onlink_rtrequest(pr, ifa);
218733841545SHajimu UMEMOTO 
21888c0fec80SRobert Watson 	if (ifa != NULL)
21898c0fec80SRobert Watson 		ifa_free(ifa);
219033841545SHajimu UMEMOTO 
219133841545SHajimu UMEMOTO 	return (error);
219233841545SHajimu UMEMOTO }
219333841545SHajimu UMEMOTO 
2194d748f7efSMark Johnston int
21951272577eSXin LI nd6_prefix_offlink(struct nd_prefix *pr)
219633841545SHajimu UMEMOTO {
219733841545SHajimu UMEMOTO 	int error = 0;
219833841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
219933841545SHajimu UMEMOTO 	struct nd_prefix *opr;
220033841545SHajimu UMEMOTO 	struct sockaddr_in6 sa6, mask6;
220181d5d46bSBjoern A. Zeeb 	struct rtentry *rt;
22021d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2203d748f7efSMark Johnston 	uint64_t genid;
2204559b4296SAlan Somers 	int fibnum, maxfib, a_failure;
220533841545SHajimu UMEMOTO 
2206d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2207d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2208d748f7efSMark Johnston 
2209d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
221033841545SHajimu UMEMOTO 		return (EEXIST);
221133841545SHajimu UMEMOTO 
221282cd038dSYoshinobu Inoue 	bzero(&sa6, sizeof(sa6));
221382cd038dSYoshinobu Inoue 	sa6.sin6_family = AF_INET6;
221482cd038dSYoshinobu Inoue 	sa6.sin6_len = sizeof(sa6);
221582cd038dSYoshinobu Inoue 	bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
221682cd038dSYoshinobu Inoue 	    sizeof(struct in6_addr));
221782cd038dSYoshinobu Inoue 	bzero(&mask6, sizeof(mask6));
221882cd038dSYoshinobu Inoue 	mask6.sin6_family = AF_INET6;
221982cd038dSYoshinobu Inoue 	mask6.sin6_len = sizeof(sa6);
222082cd038dSYoshinobu Inoue 	bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
222133841545SHajimu UMEMOTO 
2222559b4296SAlan Somers 	if (V_rt_add_addr_allfibs) {
2223559b4296SAlan Somers 		fibnum = 0;
2224559b4296SAlan Somers 		maxfib = rt_numfibs;
2225559b4296SAlan Somers 	} else {
2226559b4296SAlan Somers 		fibnum = ifp->if_fib;
2227559b4296SAlan Somers 		maxfib = fibnum + 1;
2228559b4296SAlan Somers 	}
2229559b4296SAlan Somers 
223081d5d46bSBjoern A. Zeeb 	a_failure = 0;
2231559b4296SAlan Somers 	for (; fibnum < maxfib; fibnum++) {
223281d5d46bSBjoern A. Zeeb 		rt = NULL;
223381d5d46bSBjoern A. Zeeb 		error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
223481d5d46bSBjoern A. Zeeb 		    (struct sockaddr *)&mask6, 0, &rt, fibnum);
223581d5d46bSBjoern A. Zeeb 		if (error == 0) {
223633841545SHajimu UMEMOTO 			/* report the route deletion to the routing socket. */
223733841545SHajimu UMEMOTO 			if (rt != NULL)
223833841545SHajimu UMEMOTO 				nd6_rtmsg(RTM_DELETE, rt);
223981d5d46bSBjoern A. Zeeb 		} else {
224081d5d46bSBjoern A. Zeeb 			/* Save last error to return, see rtinit(). */
224181d5d46bSBjoern A. Zeeb 			a_failure = error;
224281d5d46bSBjoern A. Zeeb 		}
224381d5d46bSBjoern A. Zeeb 		if (rt != NULL) {
224481d5d46bSBjoern A. Zeeb 			RTFREE(rt);
224581d5d46bSBjoern A. Zeeb 		}
224681d5d46bSBjoern A. Zeeb 	}
224781d5d46bSBjoern A. Zeeb 	error = a_failure;
2248378aa8d8SQing Li 	a_failure = 1;
224981d5d46bSBjoern A. Zeeb 	if (error == 0) {
225081d5d46bSBjoern A. Zeeb 		pr->ndpr_stateflags &= ~NDPRF_ONLINK;
225133841545SHajimu UMEMOTO 
225233841545SHajimu UMEMOTO 		/*
225333841545SHajimu UMEMOTO 		 * There might be the same prefix on another interface,
225433841545SHajimu UMEMOTO 		 * the prefix which could not be on-link just because we have
225533841545SHajimu UMEMOTO 		 * the interface route (see comments in nd6_prefix_onlink).
225633841545SHajimu UMEMOTO 		 * If there's one, try to make the prefix on-link on the
225733841545SHajimu UMEMOTO 		 * interface.
225833841545SHajimu UMEMOTO 		 */
2259d748f7efSMark Johnston 		ND6_RLOCK();
2260d748f7efSMark Johnston restart:
22613b0b2840SJohn Baldwin 		LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
226233841545SHajimu UMEMOTO 			/*
226333841545SHajimu UMEMOTO 			 * KAME specific: detached prefixes should not be
226433841545SHajimu UMEMOTO 			 * on-link.
226533841545SHajimu UMEMOTO 			 */
22667b0e84b7SMark Johnston 			if (opr == pr || (opr->ndpr_stateflags &
22677b0e84b7SMark Johnston 			    (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
226833841545SHajimu UMEMOTO 				continue;
226933841545SHajimu UMEMOTO 
227033841545SHajimu UMEMOTO 			if (opr->ndpr_plen == pr->ndpr_plen &&
227133841545SHajimu UMEMOTO 			    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
227207eb2995SHajimu UMEMOTO 			    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
227382cd038dSYoshinobu Inoue 				int e;
227482cd038dSYoshinobu Inoue 
2275d748f7efSMark Johnston 				genid = V_nd6_list_genid;
2276d748f7efSMark Johnston 				ND6_RUNLOCK();
227733841545SHajimu UMEMOTO 				if ((e = nd6_prefix_onlink(opr)) != 0) {
227833841545SHajimu UMEMOTO 					nd6log((LOG_ERR,
227933841545SHajimu UMEMOTO 					    "nd6_prefix_offlink: failed to "
228033841545SHajimu UMEMOTO 					    "recover a prefix %s/%d from %s "
228133841545SHajimu UMEMOTO 					    "to %s (errno = %d)\n",
22821d54aa3bSBjoern A. Zeeb 					    ip6_sprintf(ip6buf,
22831d54aa3bSBjoern A. Zeeb 						&opr->ndpr_prefix.sin6_addr),
228433841545SHajimu UMEMOTO 					    opr->ndpr_plen, if_name(ifp),
228533841545SHajimu UMEMOTO 					    if_name(opr->ndpr_ifp), e));
2286378aa8d8SQing Li 				} else
2287378aa8d8SQing Li 					a_failure = 0;
2288d748f7efSMark Johnston 				ND6_RLOCK();
2289d748f7efSMark Johnston 				if (genid != V_nd6_list_genid)
2290d748f7efSMark Johnston 					goto restart;
229182cd038dSYoshinobu Inoue 			}
229282cd038dSYoshinobu Inoue 		}
2293d748f7efSMark Johnston 		ND6_RUNLOCK();
229407eb2995SHajimu UMEMOTO 	} else {
229533841545SHajimu UMEMOTO 		/* XXX: can we still set the NDPRF_ONLINK flag? */
229633841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
229733841545SHajimu UMEMOTO 		    "nd6_prefix_offlink: failed to delete route: "
229833841545SHajimu UMEMOTO 		    "%s/%d on %s (errno = %d)\n",
22991d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
23001d54aa3bSBjoern A. Zeeb 		    if_name(ifp), error));
230133841545SHajimu UMEMOTO 	}
230282cd038dSYoshinobu Inoue 
2303378aa8d8SQing Li 	if (a_failure)
2304378aa8d8SQing Li 		lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
2305378aa8d8SQing Li 		    (struct sockaddr *)&mask6, LLE_STATIC);
2306378aa8d8SQing Li 
230733841545SHajimu UMEMOTO 	return (error);
230882cd038dSYoshinobu Inoue }
230982cd038dSYoshinobu Inoue 
23101272577eSXin LI /*
23111272577eSXin LI  * ia0 - corresponding public address
23121272577eSXin LI  */
231382cd038dSYoshinobu Inoue int
23141272577eSXin LI in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
231582cd038dSYoshinobu Inoue {
231633841545SHajimu UMEMOTO 	struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
23170c5d4bdeSAlexander V. Chernikov 	struct in6_ifaddr *newia;
231833841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
23191dc8f6a8SAlexander V. Chernikov 	int error;
232033841545SHajimu UMEMOTO 	int trylimit = 3;	/* XXX: adhoc value */
2321743eee66SSUZUKI Shinsuke 	int updateflags;
232233841545SHajimu UMEMOTO 	u_int32_t randid[2];
232333841545SHajimu UMEMOTO 	time_t vltime0, pltime0;
232482cd038dSYoshinobu Inoue 
23259080e7d0SAlexander V. Chernikov 	in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
23269080e7d0SAlexander V. Chernikov 	    &ia0->ia_prefixmask.sin6_addr);
23279080e7d0SAlexander V. Chernikov 
23289080e7d0SAlexander V. Chernikov 	ifra.ifra_addr = ia0->ia_addr;	/* XXX: do we need this ? */
232933841545SHajimu UMEMOTO 	/* clear the old IFID */
23301dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
23311dc8f6a8SAlexander V. Chernikov 	    &ifra.ifra_prefixmask.sin6_addr);
233282cd038dSYoshinobu Inoue 
233333841545SHajimu UMEMOTO   again:
2334743eee66SSUZUKI Shinsuke 	if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2335743eee66SSUZUKI Shinsuke 	    (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2336743eee66SSUZUKI Shinsuke 		nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
2337743eee66SSUZUKI Shinsuke 		    "random IFID\n"));
2338743eee66SSUZUKI Shinsuke 		return (EINVAL);
2339743eee66SSUZUKI Shinsuke 	}
234007eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
234107eb2995SHajimu UMEMOTO 	    (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
234207eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
234307eb2995SHajimu UMEMOTO 	    (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
234482cd038dSYoshinobu Inoue 
234582cd038dSYoshinobu Inoue 	/*
2346743eee66SSUZUKI Shinsuke 	 * in6_get_tmpifid() quite likely provided a unique interface ID.
2347743eee66SSUZUKI Shinsuke 	 * However, we may still have a chance to see collision, because
2348743eee66SSUZUKI Shinsuke 	 * there may be a time lag between generation of the ID and generation
2349743eee66SSUZUKI Shinsuke 	 * of the address.  So, we'll do one more sanity check.
235082cd038dSYoshinobu Inoue 	 */
23510c5d4bdeSAlexander V. Chernikov 
23520c5d4bdeSAlexander V. Chernikov 	if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
23530c5d4bdeSAlexander V. Chernikov 		if (trylimit-- > 0) {
23540c5d4bdeSAlexander V. Chernikov 			forcegen = 1;
23550c5d4bdeSAlexander V. Chernikov 			goto again;
23560c5d4bdeSAlexander V. Chernikov 		}
23570c5d4bdeSAlexander V. Chernikov 
23580c5d4bdeSAlexander V. Chernikov 		/* Give up.  Something strange should have happened.  */
2359743eee66SSUZUKI Shinsuke 		nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
2360743eee66SSUZUKI Shinsuke 		    "find a unique random IFID\n"));
236133841545SHajimu UMEMOTO 		return (EEXIST);
236233841545SHajimu UMEMOTO 	}
236382cd038dSYoshinobu Inoue 
236433841545SHajimu UMEMOTO 	/*
236533841545SHajimu UMEMOTO 	 * The Valid Lifetime is the lower of the Valid Lifetime of the
236633841545SHajimu UMEMOTO          * public address or TEMP_VALID_LIFETIME.
236733841545SHajimu UMEMOTO 	 * The Preferred Lifetime is the lower of the Preferred Lifetime
236833841545SHajimu UMEMOTO          * of the public address or TEMP_PREFERRED_LIFETIME -
236933841545SHajimu UMEMOTO          * DESYNC_FACTOR.
237082cd038dSYoshinobu Inoue 	 */
2371743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
237233841545SHajimu UMEMOTO 		vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2373743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_vltime -
23747d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2375603724d3SBjoern A. Zeeb 		if (vltime0 > V_ip6_temp_valid_lifetime)
2376603724d3SBjoern A. Zeeb 			vltime0 = V_ip6_temp_valid_lifetime;
237733841545SHajimu UMEMOTO 	} else
2378603724d3SBjoern A. Zeeb 		vltime0 = V_ip6_temp_valid_lifetime;
2379743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
238033841545SHajimu UMEMOTO 		pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2381743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_pltime -
23827d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2383603724d3SBjoern A. Zeeb 		if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2384603724d3SBjoern A. Zeeb 			pltime0 = V_ip6_temp_preferred_lifetime -
2385603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor;
238633841545SHajimu UMEMOTO 		}
238733841545SHajimu UMEMOTO 	} else
2388603724d3SBjoern A. Zeeb 		pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
238933841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = vltime0;
239033841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = pltime0;
239133841545SHajimu UMEMOTO 
239233841545SHajimu UMEMOTO 	/*
239333841545SHajimu UMEMOTO 	 * A temporary address is created only if this calculated Preferred
239433841545SHajimu UMEMOTO 	 * Lifetime is greater than REGEN_ADVANCE time units.
239533841545SHajimu UMEMOTO 	 */
2396603724d3SBjoern A. Zeeb 	if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
239733841545SHajimu UMEMOTO 		return (0);
239833841545SHajimu UMEMOTO 
239933841545SHajimu UMEMOTO 	/* XXX: scope zone ID? */
240033841545SHajimu UMEMOTO 
240133841545SHajimu UMEMOTO 	ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
240233841545SHajimu UMEMOTO 
240333841545SHajimu UMEMOTO 	/* allocate ifaddr structure, link into chain, etc. */
2404743eee66SSUZUKI Shinsuke 	updateflags = 0;
2405743eee66SSUZUKI Shinsuke 	if (delay)
2406743eee66SSUZUKI Shinsuke 		updateflags |= IN6_IFAUPDATE_DADDELAY;
2407743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
240833841545SHajimu UMEMOTO 		return (error);
240933841545SHajimu UMEMOTO 
241033841545SHajimu UMEMOTO 	newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
241133841545SHajimu UMEMOTO 	if (newia == NULL) {	/* XXX: can it happen? */
241233841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
241333841545SHajimu UMEMOTO 		    "in6_tmpifadd: ifa update succeeded, but we got "
241433841545SHajimu UMEMOTO 		    "no ifaddr\n"));
241533841545SHajimu UMEMOTO 		return (EINVAL); /* XXX */
241633841545SHajimu UMEMOTO 	}
241733841545SHajimu UMEMOTO 	newia->ia6_ndpr = ia0->ia6_ndpr;
24180bbf244eSMark Johnston 	newia->ia6_ndpr->ndpr_addrcnt++;
24198c0fec80SRobert Watson 	ifa_free(&newia->ia_ifa);
242033841545SHajimu UMEMOTO 
2421c3aacd9eSHajimu UMEMOTO 	/*
2422c3aacd9eSHajimu UMEMOTO 	 * A newly added address might affect the status of other addresses.
2423c3aacd9eSHajimu UMEMOTO 	 * XXX: when the temporary address is generated with a new public
2424c3aacd9eSHajimu UMEMOTO 	 * address, the onlink check is redundant.  However, it would be safe
2425c3aacd9eSHajimu UMEMOTO 	 * to do the check explicitly everywhere a new address is generated,
2426c3aacd9eSHajimu UMEMOTO 	 * and, in fact, we surely need the check when we create a new
2427c3aacd9eSHajimu UMEMOTO 	 * temporary address due to deprecation of an old temporary address.
2428c3aacd9eSHajimu UMEMOTO 	 */
2429c3aacd9eSHajimu UMEMOTO 	pfxlist_onlink_check();
2430c3aacd9eSHajimu UMEMOTO 
243133841545SHajimu UMEMOTO 	return (0);
243282cd038dSYoshinobu Inoue }
243382cd038dSYoshinobu Inoue 
2434743eee66SSUZUKI Shinsuke static int
2435e8b0643eSAlexander V. Chernikov rt6_deleteroute(const struct rtentry *rt, void *arg)
243682cd038dSYoshinobu Inoue {
243782cd038dSYoshinobu Inoue #define SIN6(s)	((struct sockaddr_in6 *)s)
243882cd038dSYoshinobu Inoue 	struct in6_addr *gate = (struct in6_addr *)arg;
243982cd038dSYoshinobu Inoue 
244082cd038dSYoshinobu Inoue 	if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
244182cd038dSYoshinobu Inoue 		return (0);
244282cd038dSYoshinobu Inoue 
244307eb2995SHajimu UMEMOTO 	if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
244482cd038dSYoshinobu Inoue 		return (0);
244507eb2995SHajimu UMEMOTO 	}
244682cd038dSYoshinobu Inoue 
244782cd038dSYoshinobu Inoue 	/*
244833841545SHajimu UMEMOTO 	 * Do not delete a static route.
244933841545SHajimu UMEMOTO 	 * XXX: this seems to be a bit ad-hoc. Should we consider the
245033841545SHajimu UMEMOTO 	 * 'cloned' bit instead?
245133841545SHajimu UMEMOTO 	 */
245233841545SHajimu UMEMOTO 	if ((rt->rt_flags & RTF_STATIC) != 0)
245333841545SHajimu UMEMOTO 		return (0);
245433841545SHajimu UMEMOTO 
245533841545SHajimu UMEMOTO 	/*
245682cd038dSYoshinobu Inoue 	 * We delete only host route. This means, in particular, we don't
245782cd038dSYoshinobu Inoue 	 * delete default route.
245882cd038dSYoshinobu Inoue 	 */
245982cd038dSYoshinobu Inoue 	if ((rt->rt_flags & RTF_HOST) == 0)
246082cd038dSYoshinobu Inoue 		return (0);
246182cd038dSYoshinobu Inoue 
2462e8b0643eSAlexander V. Chernikov 	return (1);
246382cd038dSYoshinobu Inoue #undef SIN6
246482cd038dSYoshinobu Inoue }
2465686cdd19SJun-ichiro itojun Hagino 
2466*f77a6dbdSBjoern A. Zeeb /*
2467*f77a6dbdSBjoern A. Zeeb  * Delete all the routing table entries that use the specified gateway.
2468*f77a6dbdSBjoern A. Zeeb  * XXX: this function causes search through all entries of routing table, so
2469*f77a6dbdSBjoern A. Zeeb  * it shouldn't be called when acting as a router.
2470*f77a6dbdSBjoern A. Zeeb  */
2471*f77a6dbdSBjoern A. Zeeb void
2472*f77a6dbdSBjoern A. Zeeb rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2473*f77a6dbdSBjoern A. Zeeb {
2474*f77a6dbdSBjoern A. Zeeb 
2475*f77a6dbdSBjoern A. Zeeb 	/* We'll care only link-local addresses */
2476*f77a6dbdSBjoern A. Zeeb 	if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2477*f77a6dbdSBjoern A. Zeeb 		return;
2478*f77a6dbdSBjoern A. Zeeb 
2479*f77a6dbdSBjoern A. Zeeb 	/* XXX Do we really need to walk any but the default FIB? */
2480*f77a6dbdSBjoern A. Zeeb 	rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2481*f77a6dbdSBjoern A. Zeeb }
2482*f77a6dbdSBjoern A. Zeeb 
2483686cdd19SJun-ichiro itojun Hagino int
24841272577eSXin LI nd6_setdefaultiface(int ifindex)
2485686cdd19SJun-ichiro itojun Hagino {
2486686cdd19SJun-ichiro itojun Hagino 	int error = 0;
2487686cdd19SJun-ichiro itojun Hagino 
2488603724d3SBjoern A. Zeeb 	if (ifindex < 0 || V_if_index < ifindex)
2489686cdd19SJun-ichiro itojun Hagino 		return (EINVAL);
2490743eee66SSUZUKI Shinsuke 	if (ifindex != 0 && !ifnet_byindex(ifindex))
2491743eee66SSUZUKI Shinsuke 		return (EINVAL);
2492686cdd19SJun-ichiro itojun Hagino 
2493603724d3SBjoern A. Zeeb 	if (V_nd6_defifindex != ifindex) {
2494603724d3SBjoern A. Zeeb 		V_nd6_defifindex = ifindex;
2495603724d3SBjoern A. Zeeb 		if (V_nd6_defifindex > 0)
2496603724d3SBjoern A. Zeeb 			V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2497686cdd19SJun-ichiro itojun Hagino 		else
2498603724d3SBjoern A. Zeeb 			V_nd6_defifp = NULL;
2499686cdd19SJun-ichiro itojun Hagino 
2500686cdd19SJun-ichiro itojun Hagino 		/*
2501686cdd19SJun-ichiro itojun Hagino 		 * Our current implementation assumes one-to-one maping between
2502686cdd19SJun-ichiro itojun Hagino 		 * interfaces and links, so it would be natural to use the
2503686cdd19SJun-ichiro itojun Hagino 		 * default interface as the default link.
2504686cdd19SJun-ichiro itojun Hagino 		 */
2505603724d3SBjoern A. Zeeb 		scope6_setdefault(V_nd6_defifp);
2506686cdd19SJun-ichiro itojun Hagino 	}
2507686cdd19SJun-ichiro itojun Hagino 
2508686cdd19SJun-ichiro itojun Hagino 	return (error);
2509686cdd19SJun-ichiro itojun Hagino }
2510ad675b32SBjoern A. Zeeb 
2511ad675b32SBjoern A. Zeeb bool
2512ad675b32SBjoern A. Zeeb nd6_defrouter_list_empty(void)
2513ad675b32SBjoern A. Zeeb {
2514ad675b32SBjoern A. Zeeb 
2515f592d0c3SBjoern A. Zeeb 	return (TAILQ_EMPTY(&V_nd6_defrouter));
2516ad675b32SBjoern A. Zeeb }
2517ad675b32SBjoern A. Zeeb 
2518ad675b32SBjoern A. Zeeb void
2519ad675b32SBjoern A. Zeeb nd6_defrouter_timer(void)
2520ad675b32SBjoern A. Zeeb {
2521ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2522f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2523ad675b32SBjoern A. Zeeb 
2524ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2525ad675b32SBjoern A. Zeeb 
2526ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2527f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr)
2528ad675b32SBjoern A. Zeeb 		if (dr->expire && dr->expire < time_uptime)
2529ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2530ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2531ad675b32SBjoern A. Zeeb 
2532ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2533ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2534ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2535ad675b32SBjoern A. Zeeb 	}
2536ad675b32SBjoern A. Zeeb }
2537ad675b32SBjoern A. Zeeb 
2538ad675b32SBjoern A. Zeeb /*
2539ad675b32SBjoern A. Zeeb  * Nuke default router list entries toward ifp.
2540ad675b32SBjoern A. Zeeb  * We defer removal of default router list entries that is installed in the
2541ad675b32SBjoern A. Zeeb  * routing table, in order to keep additional side effects as small as possible.
2542ad675b32SBjoern A. Zeeb  */
2543ad675b32SBjoern A. Zeeb void
2544ad675b32SBjoern A. Zeeb nd6_defrouter_purge(struct ifnet *ifp)
2545ad675b32SBjoern A. Zeeb {
2546ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2547f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2548ad675b32SBjoern A. Zeeb 
2549ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2550ad675b32SBjoern A. Zeeb 
2551ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2552f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2553ad675b32SBjoern A. Zeeb 		if (dr->installed)
2554ad675b32SBjoern A. Zeeb 			continue;
2555ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2556ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2557ad675b32SBjoern A. Zeeb 	}
2558f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2559ad675b32SBjoern A. Zeeb 		if (!dr->installed)
2560ad675b32SBjoern A. Zeeb 			continue;
2561ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2562ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2563ad675b32SBjoern A. Zeeb 	}
2564ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2565ad675b32SBjoern A. Zeeb 
2566ad675b32SBjoern A. Zeeb 	/* Delete the unlinked router objects. */
2567ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2568ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2569ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2570ad675b32SBjoern A. Zeeb 	}
2571ad675b32SBjoern A. Zeeb }
2572ad675b32SBjoern A. Zeeb 
2573ad675b32SBjoern A. Zeeb void
2574ad675b32SBjoern A. Zeeb nd6_defrouter_flush_all(void)
2575ad675b32SBjoern A. Zeeb {
2576ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
2577f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2578ad675b32SBjoern A. Zeeb 
2579ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2580ad675b32SBjoern A. Zeeb 
2581ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2582f592d0c3SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&V_nd6_defrouter)) != NULL)
2583ad675b32SBjoern A. Zeeb 		defrouter_unlink(dr, &drq);
2584ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2585ad675b32SBjoern A. Zeeb 
2586ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2587ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2588ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2589ad675b32SBjoern A. Zeeb 	}
2590ad675b32SBjoern A. Zeeb }
2591ad675b32SBjoern A. Zeeb 
2592ad675b32SBjoern A. Zeeb void
2593ad675b32SBjoern A. Zeeb nd6_defrouter_init(void)
2594ad675b32SBjoern A. Zeeb {
2595ad675b32SBjoern A. Zeeb 
2596f592d0c3SBjoern A. Zeeb 	TAILQ_INIT(&V_nd6_defrouter);
2597ad675b32SBjoern A. Zeeb }
2598*f77a6dbdSBjoern A. Zeeb 
2599*f77a6dbdSBjoern A. Zeeb static int
2600*f77a6dbdSBjoern A. Zeeb nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2601*f77a6dbdSBjoern A. Zeeb {
2602*f77a6dbdSBjoern A. Zeeb 	struct in6_defrouter d;
2603*f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
2604*f77a6dbdSBjoern A. Zeeb 	int error;
2605*f77a6dbdSBjoern A. Zeeb 
2606*f77a6dbdSBjoern A. Zeeb 	if (req->newptr != NULL)
2607*f77a6dbdSBjoern A. Zeeb 		return (EPERM);
2608*f77a6dbdSBjoern A. Zeeb 
2609*f77a6dbdSBjoern A. Zeeb 	error = sysctl_wire_old_buffer(req, 0);
2610*f77a6dbdSBjoern A. Zeeb 	if (error != 0)
2611*f77a6dbdSBjoern A. Zeeb 		return (error);
2612*f77a6dbdSBjoern A. Zeeb 
2613*f77a6dbdSBjoern A. Zeeb 	bzero(&d, sizeof(d));
2614*f77a6dbdSBjoern A. Zeeb 	d.rtaddr.sin6_family = AF_INET6;
2615*f77a6dbdSBjoern A. Zeeb 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
2616*f77a6dbdSBjoern A. Zeeb 
2617*f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
2618*f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
2619*f77a6dbdSBjoern A. Zeeb 		d.rtaddr.sin6_addr = dr->rtaddr;
2620*f77a6dbdSBjoern A. Zeeb 		error = sa6_recoverscope(&d.rtaddr);
2621*f77a6dbdSBjoern A. Zeeb 		if (error != 0)
2622*f77a6dbdSBjoern A. Zeeb 			break;
2623*f77a6dbdSBjoern A. Zeeb 		d.flags = dr->raflags;
2624*f77a6dbdSBjoern A. Zeeb 		d.rtlifetime = dr->rtlifetime;
2625*f77a6dbdSBjoern A. Zeeb 		d.expire = dr->expire + (time_second - time_uptime);
2626*f77a6dbdSBjoern A. Zeeb 		d.if_index = dr->ifp->if_index;
2627*f77a6dbdSBjoern A. Zeeb 		error = SYSCTL_OUT(req, &d, sizeof(d));
2628*f77a6dbdSBjoern A. Zeeb 		if (error != 0)
2629*f77a6dbdSBjoern A. Zeeb 			break;
2630*f77a6dbdSBjoern A. Zeeb 	}
2631*f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
2632*f77a6dbdSBjoern A. Zeeb 	return (error);
2633*f77a6dbdSBjoern A. Zeeb }
2634*f77a6dbdSBjoern A. Zeeb SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2635*f77a6dbdSBjoern A. Zeeb 	CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2636*f77a6dbdSBjoern A. Zeeb 	NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2637*f77a6dbdSBjoern A. Zeeb 	"NDP default router list");
2638