xref: /freebsd/sys/netinet6/nd6_rtr.c (revision a61b5cfbbfd54c51c9ba6cc9aa19907911fb3aa9)
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 int rtpref(struct nd_defrouter *);
779233d8f3SDavid E. O'Brien static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
786f56329aSXin LI static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
796f56329aSXin LI     struct mbuf *, int);
809233d8f3SDavid E. O'Brien static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int);
816f56329aSXin LI static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
826f56329aSXin LI     struct nd_defrouter *);
839233d8f3SDavid E. O'Brien static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
849233d8f3SDavid E. O'Brien static void pfxrtr_del(struct nd_pfxrouter *);
857782accfSMark Johnston static struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *);
869233d8f3SDavid E. O'Brien static void defrouter_delreq(struct nd_defrouter *);
879233d8f3SDavid E. O'Brien static void nd6_rtmsg(int, struct rtentry *);
8882cd038dSYoshinobu Inoue 
899233d8f3SDavid E. O'Brien static int in6_init_prefix_ltimes(struct nd_prefix *);
906f56329aSXin LI static void in6_init_address_ltimes(struct nd_prefix *,
916f56329aSXin LI     struct in6_addrlifetime *);
9282cd038dSYoshinobu Inoue 
93e8b0643eSAlexander V. Chernikov static int rt6_deleteroute(const struct rtentry *, void *);
9482cd038dSYoshinobu Inoue 
95ad675b32SBjoern A. Zeeb VNET_DEFINE_STATIC(struct nd_drhead, nd_defrouter);
96ad675b32SBjoern A. Zeeb #define	V_nd_defrouter			VNET(nd_defrouter)
97ad675b32SBjoern A. Zeeb 
98eddfbb76SRobert Watson VNET_DECLARE(int, nd6_recalc_reachtm_interval);
991e77c105SRobert Watson #define	V_nd6_recalc_reachtm_interval	VNET(nd6_recalc_reachtm_interval)
10082cd038dSYoshinobu Inoue 
1015f901c92SAndrew Turner VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
10282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_defifindex);
1031e77c105SRobert Watson #define	V_nd6_defifp			VNET(nd6_defifp)
104686cdd19SJun-ichiro itojun Hagino 
10582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_use_tempaddr) = 0;
10682cea7e6SBjoern A. Zeeb 
107eddfbb76SRobert Watson VNET_DEFINE(int, ip6_desync_factor);
10882cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
10982cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
11082cea7e6SBjoern A. Zeeb 
11182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
11233841545SHajimu UMEMOTO 
11321231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL
11421231a7aSBjoern A. Zeeb VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1;
11521231a7aSBjoern A. Zeeb #endif
11621231a7aSBjoern A. Zeeb 
117743eee66SSUZUKI Shinsuke /* RTPREF_MEDIUM has to be 0! */
118743eee66SSUZUKI Shinsuke #define RTPREF_HIGH	1
119743eee66SSUZUKI Shinsuke #define RTPREF_MEDIUM	0
120743eee66SSUZUKI Shinsuke #define RTPREF_LOW	(-1)
121743eee66SSUZUKI Shinsuke #define RTPREF_RESERVED	(-2)
122743eee66SSUZUKI Shinsuke #define RTPREF_INVALID	(-3)	/* internal */
123743eee66SSUZUKI Shinsuke 
124ad675b32SBjoern A. Zeeb static void
125ad675b32SBjoern A. Zeeb defrouter_ref(struct nd_defrouter *dr)
126ad675b32SBjoern A. Zeeb {
127ad675b32SBjoern A. Zeeb 
128ad675b32SBjoern A. Zeeb 	refcount_acquire(&dr->refcnt);
129ad675b32SBjoern A. Zeeb }
130ad675b32SBjoern A. Zeeb 
131ad675b32SBjoern A. Zeeb void
132ad675b32SBjoern A. Zeeb defrouter_rele(struct nd_defrouter *dr)
133ad675b32SBjoern A. Zeeb {
134ad675b32SBjoern A. Zeeb 
135ad675b32SBjoern A. Zeeb 	if (refcount_release(&dr->refcnt))
136ad675b32SBjoern A. Zeeb 		free(dr, M_IP6NDP);
137ad675b32SBjoern A. Zeeb }
138ad675b32SBjoern A. Zeeb 
139ad675b32SBjoern A. Zeeb /*
140ad675b32SBjoern A. Zeeb  * Remove a router from the global list and optionally stash it in a
141ad675b32SBjoern A. Zeeb  * caller-supplied queue.
142ad675b32SBjoern A. Zeeb  */
143ad675b32SBjoern A. Zeeb static void
144ad675b32SBjoern A. Zeeb defrouter_unlink(struct nd_defrouter *dr, struct nd_drhead *drq)
145ad675b32SBjoern A. Zeeb {
146ad675b32SBjoern A. Zeeb 
147ad675b32SBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
148ad675b32SBjoern A. Zeeb 
149ad675b32SBjoern A. Zeeb 	TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
150ad675b32SBjoern A. Zeeb 	V_nd6_list_genid++;
151ad675b32SBjoern A. Zeeb 	if (drq != NULL)
152ad675b32SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(drq, dr, dr_entry);
153ad675b32SBjoern A. Zeeb }
154ad675b32SBjoern A. Zeeb 
15582cd038dSYoshinobu Inoue /*
15682cd038dSYoshinobu Inoue  * Receive Router Solicitation Message - just for routers.
15782cd038dSYoshinobu Inoue  * Router solicitation/advertisement is mostly managed by userland program
15882cd038dSYoshinobu Inoue  * (rtadvd) so here we have no function like nd6_ra_output().
15982cd038dSYoshinobu Inoue  *
16082cd038dSYoshinobu Inoue  * Based on RFC 2461
16182cd038dSYoshinobu Inoue  */
16282cd038dSYoshinobu Inoue void
1631272577eSXin LI nd6_rs_input(struct mbuf *m, int off, int icmp6len)
16482cd038dSYoshinobu Inoue {
165503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
166503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
167686cdd19SJun-ichiro itojun Hagino 	struct nd_router_solicit *nd_rs;
168503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
16982cd038dSYoshinobu Inoue 	union nd_opts ndopts;
1701d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
171503f4e47SBjoern A. Zeeb 	char *lladdr;
172503f4e47SBjoern A. Zeeb 	int lladdrlen;
173503f4e47SBjoern A. Zeeb 
174503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
17582cd038dSYoshinobu Inoue 
176e7fa8d0aSHiroki Sato 	/*
177e7fa8d0aSHiroki Sato 	 * Accept RS only when V_ip6_forwarding=1 and the interface has
178e7fa8d0aSHiroki Sato 	 * no ND6_IFF_ACCEPT_RTADV.
179e7fa8d0aSHiroki Sato 	 */
180e7fa8d0aSHiroki Sato 	if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
181686cdd19SJun-ichiro itojun Hagino 		goto freeit;
18282cd038dSYoshinobu Inoue 
183a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
184a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
185a4061289SAndrey V. Elsukov 		goto freeit;
186a4061289SAndrey V. Elsukov 
18782cd038dSYoshinobu Inoue 	/* Sanity checks */
188503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
18982cd038dSYoshinobu Inoue 	if (ip6->ip6_hlim != 255) {
19033841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
19133841545SHajimu UMEMOTO 		    "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
1921d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
1931d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
19433841545SHajimu UMEMOTO 		goto bad;
19582cd038dSYoshinobu Inoue 	}
19682cd038dSYoshinobu Inoue 
19782cd038dSYoshinobu Inoue 	/*
19882cd038dSYoshinobu Inoue 	 * Don't update the neighbor cache, if src = ::.
19982cd038dSYoshinobu Inoue 	 * This indicates that the src has no IP address assigned yet.
20082cd038dSYoshinobu Inoue 	 */
201503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
20282cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
203686cdd19SJun-ichiro itojun Hagino 		goto freeit;
204686cdd19SJun-ichiro itojun Hagino 
205*a61b5cfbSBjoern A. Zeeb 	m = m_pullup(m, off + icmp6len);
206*a61b5cfbSBjoern A. Zeeb 	if (m == NULL) {
207*a61b5cfbSBjoern A. Zeeb 		IP6STAT_INC(ip6s_exthdrtoolong);
20882cd038dSYoshinobu Inoue 		return;
209686cdd19SJun-ichiro itojun Hagino 	}
210*a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
211*a61b5cfbSBjoern A. Zeeb 	nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
21282cd038dSYoshinobu Inoue 
21382cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_rs);
21482cd038dSYoshinobu Inoue 	nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
21582cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
21633841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
21733841545SHajimu UMEMOTO 		    "nd6_rs_input: invalid ND option, ignored\n"));
21833841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
219686cdd19SJun-ichiro itojun Hagino 		goto freeit;
22082cd038dSYoshinobu Inoue 	}
22182cd038dSYoshinobu Inoue 
222503f4e47SBjoern A. Zeeb 	lladdr = NULL;
223503f4e47SBjoern A. Zeeb 	lladdrlen = 0;
22482cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
22582cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
22682cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
22782cd038dSYoshinobu Inoue 	}
22882cd038dSYoshinobu Inoue 
22982cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
23033841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
23182cd038dSYoshinobu Inoue 		    "nd6_rs_input: lladdrlen mismatch for %s "
23282cd038dSYoshinobu Inoue 		    "(if %d, RS packet %d)\n",
2331d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6),
23407eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
23533841545SHajimu UMEMOTO 		goto bad;
23682cd038dSYoshinobu Inoue 	}
23782cd038dSYoshinobu Inoue 
23882cd038dSYoshinobu Inoue 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
239686cdd19SJun-ichiro itojun Hagino 
240686cdd19SJun-ichiro itojun Hagino  freeit:
241686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
24233841545SHajimu UMEMOTO 	return;
24333841545SHajimu UMEMOTO 
24433841545SHajimu UMEMOTO  bad:
245e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badrs);
24633841545SHajimu UMEMOTO 	m_freem(m);
24782cd038dSYoshinobu Inoue }
24882cd038dSYoshinobu Inoue 
249201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
250201100c5SBjoern A. Zeeb /*
251201100c5SBjoern A. Zeeb  * An initial update routine for draft-ietf-6man-ipv6only-flag.
252201100c5SBjoern A. Zeeb  * We need to iterate over all default routers for the given
25321231a7aSBjoern A. Zeeb  * interface to see whether they are all advertising the "S"
254201100c5SBjoern A. Zeeb  * (IPv6-Only) flag.  If they do set, otherwise unset, the
255201100c5SBjoern A. Zeeb  * interface flag we later use to filter on.
256201100c5SBjoern A. Zeeb  */
257201100c5SBjoern A. Zeeb static void
258201100c5SBjoern A. Zeeb defrtr_ipv6_only_ifp(struct ifnet *ifp)
259201100c5SBjoern A. Zeeb {
260201100c5SBjoern A. Zeeb 	struct nd_defrouter *dr;
26121231a7aSBjoern A. Zeeb 	bool ipv6_only, ipv6_only_old;
26221231a7aSBjoern A. Zeeb #ifdef INET
26321231a7aSBjoern A. Zeeb 	struct epoch_tracker et;
26421231a7aSBjoern A. Zeeb 	struct ifaddr *ifa;
26521231a7aSBjoern A. Zeeb 	bool has_ipv4_addr;
26621231a7aSBjoern A. Zeeb #endif
26721231a7aSBjoern A. Zeeb 
26821231a7aSBjoern A. Zeeb 	if (V_nd6_ignore_ipv6_only_ra != 0)
26921231a7aSBjoern A. Zeeb 		return;
270201100c5SBjoern A. Zeeb 
271201100c5SBjoern A. Zeeb 	ipv6_only = true;
272201100c5SBjoern A. Zeeb 	ND6_RLOCK();
273201100c5SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
274201100c5SBjoern A. Zeeb 		if (dr->ifp == ifp &&
275201100c5SBjoern A. Zeeb 		    (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0)
276201100c5SBjoern A. Zeeb 			ipv6_only = false;
277201100c5SBjoern A. Zeeb 	ND6_RUNLOCK();
278201100c5SBjoern A. Zeeb 
279201100c5SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
28021231a7aSBjoern A. Zeeb 	ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY;
28121231a7aSBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
28221231a7aSBjoern A. Zeeb 
28321231a7aSBjoern A. Zeeb 	/* If nothing changed, we have an early exit. */
28421231a7aSBjoern A. Zeeb 	if (ipv6_only == ipv6_only_old)
28521231a7aSBjoern A. Zeeb 		return;
28621231a7aSBjoern A. Zeeb 
28721231a7aSBjoern A. Zeeb #ifdef INET
28821231a7aSBjoern A. Zeeb 	/*
28921231a7aSBjoern A. Zeeb 	 * Should we want to set the IPV6-ONLY flag, check if the
29021231a7aSBjoern A. Zeeb 	 * interface has a non-0/0 and non-link-local IPv4 address
29121231a7aSBjoern A. Zeeb 	 * configured on it.  If it has we will assume working
29221231a7aSBjoern A. Zeeb 	 * IPv4 operations and will clear the interface flag.
29321231a7aSBjoern A. Zeeb 	 */
29421231a7aSBjoern A. Zeeb 	has_ipv4_addr = false;
29521231a7aSBjoern A. Zeeb 	if (ipv6_only) {
29621231a7aSBjoern A. Zeeb 		NET_EPOCH_ENTER(et);
29721231a7aSBjoern A. Zeeb 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
29821231a7aSBjoern A. Zeeb 			if (ifa->ifa_addr->sa_family != AF_INET)
29921231a7aSBjoern A. Zeeb 				continue;
30021231a7aSBjoern A. Zeeb 			if (in_canforward(
30121231a7aSBjoern A. Zeeb 			    satosin(ifa->ifa_addr)->sin_addr)) {
30221231a7aSBjoern A. Zeeb 				has_ipv4_addr = true;
30321231a7aSBjoern A. Zeeb 				break;
30421231a7aSBjoern A. Zeeb 			}
30521231a7aSBjoern A. Zeeb 		}
30621231a7aSBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
30721231a7aSBjoern A. Zeeb 	}
30821231a7aSBjoern A. Zeeb 	if (ipv6_only && has_ipv4_addr) {
30921231a7aSBjoern A. Zeeb 		log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
31021231a7aSBjoern A. Zeeb 		    "configured, ignoring IPv6-Only flag.\n", ifp->if_xname);
31121231a7aSBjoern A. Zeeb 		ipv6_only = false;
31221231a7aSBjoern A. Zeeb 	}
31321231a7aSBjoern A. Zeeb #endif
31421231a7aSBjoern A. Zeeb 
31521231a7aSBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
316201100c5SBjoern A. Zeeb 	if (ipv6_only)
317201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY;
318201100c5SBjoern A. Zeeb 	else
319201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
320201100c5SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
32121231a7aSBjoern A. Zeeb 
32221231a7aSBjoern A. Zeeb #ifdef notyet
32321231a7aSBjoern A. Zeeb 	/* Send notification of flag change. */
324201100c5SBjoern A. Zeeb #endif
32521231a7aSBjoern A. Zeeb }
32630b45077SBjoern A. Zeeb 
32730b45077SBjoern A. Zeeb static void
32830b45077SBjoern A. Zeeb defrtr_ipv6_only_ipf_down(struct ifnet *ifp)
32930b45077SBjoern A. Zeeb {
33030b45077SBjoern A. Zeeb 
33130b45077SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
33230b45077SBjoern A. Zeeb 	ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
33330b45077SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
33430b45077SBjoern A. Zeeb }
33521231a7aSBjoern A. Zeeb #endif	/* EXPERIMENTAL */
336201100c5SBjoern A. Zeeb 
33730b45077SBjoern A. Zeeb void
33830b45077SBjoern A. Zeeb nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate)
33930b45077SBjoern A. Zeeb {
34030b45077SBjoern A. Zeeb 
34130b45077SBjoern A. Zeeb 	/*
34230b45077SBjoern A. Zeeb 	 * XXX-BZ we might want to trigger re-evaluation of our default router
34330b45077SBjoern A. Zeeb 	 * availability. E.g., on link down the default router might be
34430b45077SBjoern A. Zeeb 	 * unreachable but a different interface might still have connectivity.
34530b45077SBjoern A. Zeeb 	 */
34630b45077SBjoern A. Zeeb 
34730b45077SBjoern A. Zeeb #ifdef EXPERIMENTAL
34830b45077SBjoern A. Zeeb 	if (linkstate == LINK_STATE_DOWN)
34930b45077SBjoern A. Zeeb 		defrtr_ipv6_only_ipf_down(ifp);
35030b45077SBjoern A. Zeeb #endif
35130b45077SBjoern A. Zeeb }
35230b45077SBjoern A. Zeeb 
35382cd038dSYoshinobu Inoue /*
35482cd038dSYoshinobu Inoue  * Receive Router Advertisement Message.
35582cd038dSYoshinobu Inoue  *
35682cd038dSYoshinobu Inoue  * Based on RFC 2461
35782cd038dSYoshinobu Inoue  * TODO: on-link bit on prefix information
35882cd038dSYoshinobu Inoue  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
35982cd038dSYoshinobu Inoue  */
36082cd038dSYoshinobu Inoue void
3611272577eSXin LI nd6_ra_input(struct mbuf *m, int off, int icmp6len)
36282cd038dSYoshinobu Inoue {
363503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
364503f4e47SBjoern A. Zeeb 	struct nd_ifinfo *ndi;
365503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
366686cdd19SJun-ichiro itojun Hagino 	struct nd_router_advert *nd_ra;
367503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
36882cd038dSYoshinobu Inoue 	struct nd_defrouter *dr;
369503f4e47SBjoern A. Zeeb 	union nd_opts ndopts;
3701d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
371503f4e47SBjoern A. Zeeb 	int mcast;
3724de485feSMark Johnston 
37307cf047dSHajimu UMEMOTO 	/*
374e7fa8d0aSHiroki Sato 	 * We only accept RAs only when the per-interface flag
375e7fa8d0aSHiroki Sato 	 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
37607cf047dSHajimu UMEMOTO 	 */
377503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
378503f4e47SBjoern A. Zeeb 	ndi = ND_IFINFO(ifp);
379e7fa8d0aSHiroki Sato 	if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
38007cf047dSHajimu UMEMOTO 		goto freeit;
38182cd038dSYoshinobu Inoue 
382a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
383a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
384a4061289SAndrey V. Elsukov 		goto freeit;
385a4061289SAndrey V. Elsukov 
386503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
38782cd038dSYoshinobu Inoue 	if (ip6->ip6_hlim != 255) {
38833841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
38933841545SHajimu UMEMOTO 		    "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
3901d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
3911d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
39233841545SHajimu UMEMOTO 		goto bad;
39382cd038dSYoshinobu Inoue 	}
39482cd038dSYoshinobu Inoue 
395503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
39682cd038dSYoshinobu Inoue 	if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
39733841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
39882cd038dSYoshinobu Inoue 		    "nd6_ra_input: src %s is not link-local\n",
3991d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6)));
40033841545SHajimu UMEMOTO 		goto bad;
401686cdd19SJun-ichiro itojun Hagino 	}
402686cdd19SJun-ichiro itojun Hagino 
403*a61b5cfbSBjoern A. Zeeb 	m = m_pullup(m, off + icmp6len);
404*a61b5cfbSBjoern A. Zeeb 	if (m == NULL) {
405*a61b5cfbSBjoern A. Zeeb 		IP6STAT_INC(ip6s_exthdrtoolong);
40682cd038dSYoshinobu Inoue 		return;
40782cd038dSYoshinobu Inoue 	}
408*a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
409*a61b5cfbSBjoern A. Zeeb 	nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
41082cd038dSYoshinobu Inoue 
41182cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_ra);
41282cd038dSYoshinobu Inoue 	nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
41382cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
41433841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
41533841545SHajimu UMEMOTO 		    "nd6_ra_input: invalid ND option, ignored\n"));
41633841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
417686cdd19SJun-ichiro itojun Hagino 		goto freeit;
41882cd038dSYoshinobu Inoue 	}
41982cd038dSYoshinobu Inoue 
420503f4e47SBjoern A. Zeeb 	mcast = 0;
421503f4e47SBjoern A. Zeeb 	dr = NULL;
42282cd038dSYoshinobu Inoue     {
42382cd038dSYoshinobu Inoue 	struct nd_defrouter dr0;
42482cd038dSYoshinobu Inoue 	u_int32_t advreachable = nd_ra->nd_ra_reachable;
42582cd038dSYoshinobu Inoue 
426743eee66SSUZUKI Shinsuke 	/* remember if this is a multicasted advertisement */
427743eee66SSUZUKI Shinsuke 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
428743eee66SSUZUKI Shinsuke 		mcast = 1;
429743eee66SSUZUKI Shinsuke 
430743eee66SSUZUKI Shinsuke 	bzero(&dr0, sizeof(dr0));
43182cd038dSYoshinobu Inoue 	dr0.rtaddr = saddr6;
43201869be5SMark Johnston 	dr0.raflags = nd_ra->nd_ra_flags_reserved;
433e7fa8d0aSHiroki Sato 	/*
434049087a0SHiroki Sato 	 * Effectively-disable routes from RA messages when
435049087a0SHiroki Sato 	 * ND6_IFF_NO_RADR enabled on the receiving interface or
436049087a0SHiroki Sato 	 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
437e7fa8d0aSHiroki Sato 	 */
438049087a0SHiroki Sato 	if (ndi->flags & ND6_IFF_NO_RADR)
439049087a0SHiroki Sato 		dr0.rtlifetime = 0;
440049087a0SHiroki Sato 	else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
441e7fa8d0aSHiroki Sato 		dr0.rtlifetime = 0;
442e7fa8d0aSHiroki Sato 	else
44382cd038dSYoshinobu Inoue 		dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
4447d26db17SHiroki Sato 	dr0.expire = time_uptime + dr0.rtlifetime;
44582cd038dSYoshinobu Inoue 	dr0.ifp = ifp;
44682cd038dSYoshinobu Inoue 	/* unspecified or not? (RFC 2461 6.3.4) */
44782cd038dSYoshinobu Inoue 	if (advreachable) {
448fd8e4ebcSMike Barcroft 		advreachable = ntohl(advreachable);
44982cd038dSYoshinobu Inoue 		if (advreachable <= MAX_REACHABLE_TIME &&
45082cd038dSYoshinobu Inoue 		    ndi->basereachable != advreachable) {
45182cd038dSYoshinobu Inoue 			ndi->basereachable = advreachable;
45282cd038dSYoshinobu Inoue 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
453603724d3SBjoern A. Zeeb 			ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
45482cd038dSYoshinobu Inoue 		}
45582cd038dSYoshinobu Inoue 	}
45682cd038dSYoshinobu Inoue 	if (nd_ra->nd_ra_retransmit)
45782cd038dSYoshinobu Inoue 		ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
458dd385660SXin LI 	if (nd_ra->nd_ra_curhoplimit) {
459dd385660SXin LI 		if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
46082cd038dSYoshinobu Inoue 			ndi->chlim = nd_ra->nd_ra_curhoplimit;
461dd385660SXin LI 		else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
462dd385660SXin LI 			log(LOG_ERR, "RA with a lower CurHopLimit sent from "
463dd385660SXin LI 			    "%s on %s (current = %d, received = %d). "
464dd385660SXin LI 			    "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
465dd385660SXin LI 			    if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
466dd385660SXin LI 		}
467dd385660SXin LI 	}
46882cd038dSYoshinobu Inoue 	dr = defrtrlist_update(&dr0);
469201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
470201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(ifp);
471201100c5SBjoern A. Zeeb #endif
47282cd038dSYoshinobu Inoue     }
47382cd038dSYoshinobu Inoue 
47482cd038dSYoshinobu Inoue 	/*
47582cd038dSYoshinobu Inoue 	 * prefix
47682cd038dSYoshinobu Inoue 	 */
47782cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_pi) {
47882cd038dSYoshinobu Inoue 		struct nd_opt_hdr *pt;
47933841545SHajimu UMEMOTO 		struct nd_opt_prefix_info *pi = NULL;
480743eee66SSUZUKI Shinsuke 		struct nd_prefixctl pr;
48182cd038dSYoshinobu Inoue 
48282cd038dSYoshinobu Inoue 		for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
48382cd038dSYoshinobu Inoue 		     pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
48482cd038dSYoshinobu Inoue 		     pt = (struct nd_opt_hdr *)((caddr_t)pt +
48582cd038dSYoshinobu Inoue 						(pt->nd_opt_len << 3))) {
48682cd038dSYoshinobu Inoue 			if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
48782cd038dSYoshinobu Inoue 				continue;
48882cd038dSYoshinobu Inoue 			pi = (struct nd_opt_prefix_info *)pt;
48982cd038dSYoshinobu Inoue 
49082cd038dSYoshinobu Inoue 			if (pi->nd_opt_pi_len != 4) {
49133841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
49233841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid option "
49382cd038dSYoshinobu Inoue 				    "len %d for prefix information option, "
49433841545SHajimu UMEMOTO 				    "ignored\n", pi->nd_opt_pi_len));
49582cd038dSYoshinobu Inoue 				continue;
49682cd038dSYoshinobu Inoue 			}
49782cd038dSYoshinobu Inoue 
49882cd038dSYoshinobu Inoue 			if (128 < pi->nd_opt_pi_prefix_len) {
49933841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
50033841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid prefix "
50182cd038dSYoshinobu Inoue 				    "len %d for prefix information option, "
50233841545SHajimu UMEMOTO 				    "ignored\n", pi->nd_opt_pi_prefix_len));
50382cd038dSYoshinobu Inoue 				continue;
50482cd038dSYoshinobu Inoue 			}
50582cd038dSYoshinobu Inoue 
50682cd038dSYoshinobu Inoue 			if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
50782cd038dSYoshinobu Inoue 			 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
50833841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
50933841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid prefix "
51082cd038dSYoshinobu Inoue 				    "%s, ignored\n",
5111d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6bufs,
5121d54aa3bSBjoern A. Zeeb 					&pi->nd_opt_pi_prefix)));
51382cd038dSYoshinobu Inoue 				continue;
51482cd038dSYoshinobu Inoue 			}
51582cd038dSYoshinobu Inoue 
51682cd038dSYoshinobu Inoue 			bzero(&pr, sizeof(pr));
51782cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_family = AF_INET6;
51882cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
51982cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
52082cd038dSYoshinobu Inoue 			pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
52182cd038dSYoshinobu Inoue 
52282cd038dSYoshinobu Inoue 			pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
52382cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
52482cd038dSYoshinobu Inoue 			pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
52582cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
52682cd038dSYoshinobu Inoue 			pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
52782cd038dSYoshinobu Inoue 			pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
52807eb2995SHajimu UMEMOTO 			pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
529743eee66SSUZUKI Shinsuke 			(void)prelist_update(&pr, dr, m, mcast);
53082cd038dSYoshinobu Inoue 		}
53182cd038dSYoshinobu Inoue 	}
5324de485feSMark Johnston 	if (dr != NULL) {
5334de485feSMark Johnston 		defrouter_rele(dr);
5344de485feSMark Johnston 		dr = NULL;
5354de485feSMark Johnston 	}
53682cd038dSYoshinobu Inoue 
53782cd038dSYoshinobu Inoue 	/*
53882cd038dSYoshinobu Inoue 	 * MTU
53982cd038dSYoshinobu Inoue 	 */
54082cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
54131b3783cSHajimu UMEMOTO 		u_long mtu;
54231b3783cSHajimu UMEMOTO 		u_long maxmtu;
54307eb2995SHajimu UMEMOTO 
54431b3783cSHajimu UMEMOTO 		mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
54582cd038dSYoshinobu Inoue 
54682cd038dSYoshinobu Inoue 		/* lower bound */
54782cd038dSYoshinobu Inoue 		if (mtu < IPV6_MMTU) {
54833841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
54931b3783cSHajimu UMEMOTO 			    "mtu=%lu sent from %s, ignoring\n",
5501d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
55182cd038dSYoshinobu Inoue 			goto skip;
55282cd038dSYoshinobu Inoue 		}
55382cd038dSYoshinobu Inoue 
55482cd038dSYoshinobu Inoue 		/* upper bound */
55531b3783cSHajimu UMEMOTO 		maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
55631b3783cSHajimu UMEMOTO 		    ? ndi->maxmtu : ifp->if_mtu;
55731b3783cSHajimu UMEMOTO 		if (mtu <= maxmtu) {
55882cd038dSYoshinobu Inoue 			int change = (ndi->linkmtu != mtu);
55982cd038dSYoshinobu Inoue 
56082cd038dSYoshinobu Inoue 			ndi->linkmtu = mtu;
5610bbfb20fSEric van Gyzen 			if (change) {
5620bbfb20fSEric van Gyzen 				/* in6_maxmtu may change */
56382cd038dSYoshinobu Inoue 				in6_setmaxmtu();
5640bbfb20fSEric van Gyzen 				rt_updatemtu(ifp);
5650bbfb20fSEric van Gyzen 			}
56682cd038dSYoshinobu Inoue 		} else {
56733841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
56831b3783cSHajimu UMEMOTO 			    "mtu=%lu sent from %s; "
56931b3783cSHajimu UMEMOTO 			    "exceeds maxmtu %lu, ignoring\n",
5701d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
57182cd038dSYoshinobu Inoue 		}
57282cd038dSYoshinobu Inoue 	}
57382cd038dSYoshinobu Inoue 
57482cd038dSYoshinobu Inoue  skip:
57582cd038dSYoshinobu Inoue 
57682cd038dSYoshinobu Inoue 	/*
57788ff5695SSUZUKI Shinsuke 	 * Source link layer address
57882cd038dSYoshinobu Inoue 	 */
57982cd038dSYoshinobu Inoue     {
58082cd038dSYoshinobu Inoue 	char *lladdr = NULL;
58182cd038dSYoshinobu Inoue 	int lladdrlen = 0;
58282cd038dSYoshinobu Inoue 
58382cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
58482cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
58582cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
58682cd038dSYoshinobu Inoue 	}
58782cd038dSYoshinobu Inoue 
58882cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
58933841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
59082cd038dSYoshinobu Inoue 		    "nd6_ra_input: lladdrlen mismatch for %s "
5911d54aa3bSBjoern A. Zeeb 		    "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
59207eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
59333841545SHajimu UMEMOTO 		goto bad;
59482cd038dSYoshinobu Inoue 	}
59582cd038dSYoshinobu Inoue 
59607eb2995SHajimu UMEMOTO 	nd6_cache_lladdr(ifp, &saddr6, lladdr,
59707eb2995SHajimu UMEMOTO 	    lladdrlen, ND_ROUTER_ADVERT, 0);
598686cdd19SJun-ichiro itojun Hagino 
599686cdd19SJun-ichiro itojun Hagino 	/*
600686cdd19SJun-ichiro itojun Hagino 	 * Installing a link-layer address might change the state of the
601686cdd19SJun-ichiro itojun Hagino 	 * router's neighbor cache, which might also affect our on-link
602686cdd19SJun-ichiro itojun Hagino 	 * detection of adveritsed prefixes.
603686cdd19SJun-ichiro itojun Hagino 	 */
604686cdd19SJun-ichiro itojun Hagino 	pfxlist_onlink_check();
60582cd038dSYoshinobu Inoue     }
606686cdd19SJun-ichiro itojun Hagino 
607686cdd19SJun-ichiro itojun Hagino  freeit:
608686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
60933841545SHajimu UMEMOTO 	return;
61033841545SHajimu UMEMOTO 
61133841545SHajimu UMEMOTO  bad:
612e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badra);
61333841545SHajimu UMEMOTO 	m_freem(m);
61482cd038dSYoshinobu Inoue }
61582cd038dSYoshinobu Inoue 
616686cdd19SJun-ichiro itojun Hagino /* tell the change to user processes watching the routing socket. */
617686cdd19SJun-ichiro itojun Hagino static void
6181272577eSXin LI nd6_rtmsg(int cmd, struct rtentry *rt)
619686cdd19SJun-ichiro itojun Hagino {
620686cdd19SJun-ichiro itojun Hagino 	struct rt_addrinfo info;
6211e6a4139SRobert Watson 	struct ifnet *ifp;
6228c0fec80SRobert Watson 	struct ifaddr *ifa;
623686cdd19SJun-ichiro itojun Hagino 
624686cdd19SJun-ichiro itojun Hagino 	bzero((caddr_t)&info, sizeof(info));
625686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_DST] = rt_key(rt);
626686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
627686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
6281e6a4139SRobert Watson 	ifp = rt->rt_ifp;
6291e6a4139SRobert Watson 	if (ifp != NULL) {
630a68cc388SGleb Smirnoff 		struct epoch_tracker et;
631a68cc388SGleb Smirnoff 
632a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
633d7c5a620SMatt Macy 		ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
6348c0fec80SRobert Watson 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
6358c0fec80SRobert Watson 		ifa_ref(ifa);
636a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
63733841545SHajimu UMEMOTO 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
6388c0fec80SRobert Watson 	} else
6398c0fec80SRobert Watson 		ifa = NULL;
640686cdd19SJun-ichiro itojun Hagino 
64181d5d46bSBjoern A. Zeeb 	rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
6428c0fec80SRobert Watson 	if (ifa != NULL)
6438c0fec80SRobert Watson 		ifa_free(ifa);
644686cdd19SJun-ichiro itojun Hagino }
645686cdd19SJun-ichiro itojun Hagino 
6464de485feSMark Johnston /*
647a4641f4eSPedro F. Giffuni  * default router list processing sub routines
6484de485feSMark Johnston  */
6494de485feSMark Johnston 
6509d9b92f2SBjoern A. Zeeb static void
6511272577eSXin LI defrouter_addreq(struct nd_defrouter *new)
65282cd038dSYoshinobu Inoue {
65382cd038dSYoshinobu Inoue 	struct sockaddr_in6 def, mask, gate;
654686cdd19SJun-ichiro itojun Hagino 	struct rtentry *newrt = NULL;
655743eee66SSUZUKI Shinsuke 	int error;
65682cd038dSYoshinobu Inoue 
657056c7327SLuigi Rizzo 	bzero(&def, sizeof(def));
658056c7327SLuigi Rizzo 	bzero(&mask, sizeof(mask));
659056c7327SLuigi Rizzo 	bzero(&gate, sizeof(gate));
66082cd038dSYoshinobu Inoue 
66107eb2995SHajimu UMEMOTO 	def.sin6_len = mask.sin6_len = gate.sin6_len =
66207eb2995SHajimu UMEMOTO 	    sizeof(struct sockaddr_in6);
663743eee66SSUZUKI Shinsuke 	def.sin6_family = gate.sin6_family = AF_INET6;
66482cd038dSYoshinobu Inoue 	gate.sin6_addr = new->rtaddr;
66582cd038dSYoshinobu Inoue 
66681d5d46bSBjoern A. Zeeb 	error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
66782cd038dSYoshinobu Inoue 	    (struct sockaddr *)&gate, (struct sockaddr *)&mask,
668559b4296SAlan Somers 	    RTF_GATEWAY, &newrt, new->ifp->if_fib);
669686cdd19SJun-ichiro itojun Hagino 	if (newrt) {
67033841545SHajimu UMEMOTO 		nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
6716e6b3f7cSQing Li 		RTFREE(newrt);
672686cdd19SJun-ichiro itojun Hagino 	}
673743eee66SSUZUKI Shinsuke 	if (error == 0)
674743eee66SSUZUKI Shinsuke 		new->installed = 1;
67582cd038dSYoshinobu Inoue }
67682cd038dSYoshinobu Inoue 
67782cd038dSYoshinobu Inoue struct nd_defrouter *
6784de485feSMark Johnston defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
6794de485feSMark Johnston {
6804de485feSMark Johnston 	struct nd_defrouter *dr;
6814de485feSMark Johnston 
6824de485feSMark Johnston 	ND6_LOCK_ASSERT();
6834de485feSMark Johnston 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
6844de485feSMark Johnston 		if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
6854de485feSMark Johnston 			defrouter_ref(dr);
6864de485feSMark Johnston 			return (dr);
6874de485feSMark Johnston 		}
6884de485feSMark Johnston 	return (NULL);
6894de485feSMark Johnston }
6904de485feSMark Johnston 
6914de485feSMark Johnston struct nd_defrouter *
6921272577eSXin LI defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
69382cd038dSYoshinobu Inoue {
69482cd038dSYoshinobu Inoue 	struct nd_defrouter *dr;
69582cd038dSYoshinobu Inoue 
6964de485feSMark Johnston 	ND6_RLOCK();
6974de485feSMark Johnston 	dr = defrouter_lookup_locked(addr, ifp);
6984de485feSMark Johnston 	ND6_RUNLOCK();
69982cd038dSYoshinobu Inoue 	return (dr);
700686cdd19SJun-ichiro itojun Hagino }
70182cd038dSYoshinobu Inoue 
702743eee66SSUZUKI Shinsuke /*
703743eee66SSUZUKI Shinsuke  * Remove the default route for a given router.
704559b4296SAlan Somers  * This is just a subroutine function for defrouter_select_fib(), and
705559b4296SAlan Somers  * should not be called from anywhere else.
706743eee66SSUZUKI Shinsuke  */
707743eee66SSUZUKI Shinsuke static void
7081272577eSXin LI defrouter_delreq(struct nd_defrouter *dr)
70982cd038dSYoshinobu Inoue {
71082cd038dSYoshinobu Inoue 	struct sockaddr_in6 def, mask, gate;
711686cdd19SJun-ichiro itojun Hagino 	struct rtentry *oldrt = NULL;
71282cd038dSYoshinobu Inoue 
713056c7327SLuigi Rizzo 	bzero(&def, sizeof(def));
714056c7327SLuigi Rizzo 	bzero(&mask, sizeof(mask));
715056c7327SLuigi Rizzo 	bzero(&gate, sizeof(gate));
71682cd038dSYoshinobu Inoue 
71707eb2995SHajimu UMEMOTO 	def.sin6_len = mask.sin6_len = gate.sin6_len =
71807eb2995SHajimu UMEMOTO 	    sizeof(struct sockaddr_in6);
719743eee66SSUZUKI Shinsuke 	def.sin6_family = gate.sin6_family = AF_INET6;
72082cd038dSYoshinobu Inoue 	gate.sin6_addr = dr->rtaddr;
72182cd038dSYoshinobu Inoue 
72281d5d46bSBjoern A. Zeeb 	in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
72382cd038dSYoshinobu Inoue 	    (struct sockaddr *)&gate,
724559b4296SAlan Somers 	    (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, dr->ifp->if_fib);
725686cdd19SJun-ichiro itojun Hagino 	if (oldrt) {
72633841545SHajimu UMEMOTO 		nd6_rtmsg(RTM_DELETE, oldrt);
72771eba915SRuslan Ermilov 		RTFREE(oldrt);
72820220070SJun-ichiro itojun Hagino 	}
72982cd038dSYoshinobu Inoue 
730743eee66SSUZUKI Shinsuke 	dr->installed = 0;
731743eee66SSUZUKI Shinsuke }
732743eee66SSUZUKI Shinsuke 
733743eee66SSUZUKI Shinsuke /*
7344de485feSMark Johnston  * Remove all default routes from default router list.
735743eee66SSUZUKI Shinsuke  */
736743eee66SSUZUKI Shinsuke void
7371272577eSXin LI defrouter_reset(void)
738743eee66SSUZUKI Shinsuke {
7394de485feSMark Johnston 	struct nd_defrouter *dr, **dra;
7404de485feSMark Johnston 	int count, i;
741743eee66SSUZUKI Shinsuke 
7424de485feSMark Johnston 	count = i = 0;
7434de485feSMark Johnston 
7444de485feSMark Johnston 	/*
7454de485feSMark Johnston 	 * We can't delete routes with the ND lock held, so make a copy of the
7464de485feSMark Johnston 	 * current default router list and use that when deleting routes.
7474de485feSMark Johnston 	 */
7484de485feSMark Johnston 	ND6_RLOCK();
7493b0b2840SJohn Baldwin 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
7504de485feSMark Johnston 		count++;
7514de485feSMark Johnston 	ND6_RUNLOCK();
7524de485feSMark Johnston 
7534de485feSMark Johnston 	dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
7544de485feSMark Johnston 
7554de485feSMark Johnston 	ND6_RLOCK();
7564de485feSMark Johnston 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
7574de485feSMark Johnston 		if (i == count)
7584de485feSMark Johnston 			break;
7594de485feSMark Johnston 		defrouter_ref(dr);
7604de485feSMark Johnston 		dra[i++] = dr;
7614de485feSMark Johnston 	}
7624de485feSMark Johnston 	ND6_RUNLOCK();
7634de485feSMark Johnston 
7644de485feSMark Johnston 	for (i = 0; i < count && dra[i] != NULL; i++) {
7654de485feSMark Johnston 		defrouter_delreq(dra[i]);
7664de485feSMark Johnston 		defrouter_rele(dra[i]);
7674de485feSMark Johnston 	}
7684de485feSMark Johnston 	free(dra, M_TEMP);
769743eee66SSUZUKI Shinsuke 
770743eee66SSUZUKI Shinsuke 	/*
771743eee66SSUZUKI Shinsuke 	 * XXX should we also nuke any default routers in the kernel, by
772743eee66SSUZUKI Shinsuke 	 * going through them by rtalloc1()?
773743eee66SSUZUKI Shinsuke 	 */
77482cd038dSYoshinobu Inoue }
77582cd038dSYoshinobu Inoue 
776ad675b32SBjoern A. Zeeb static void
7774de485feSMark Johnston defrouter_del(struct nd_defrouter *dr)
77882cd038dSYoshinobu Inoue {
77982cd038dSYoshinobu Inoue 	struct nd_defrouter *deldr = NULL;
78082cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
781d748f7efSMark Johnston 	struct nd_pfxrouter *pfxrtr;
78282cd038dSYoshinobu Inoue 
7834de485feSMark Johnston 	ND6_UNLOCK_ASSERT();
7844de485feSMark Johnston 
78582cd038dSYoshinobu Inoue 	/*
78682cd038dSYoshinobu Inoue 	 * Flush all the routing table entries that use the router
78782cd038dSYoshinobu Inoue 	 * as a next hop.
78882cd038dSYoshinobu Inoue 	 */
789e7fa8d0aSHiroki Sato 	if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
79082cd038dSYoshinobu Inoue 		rt6_flush(&dr->rtaddr, dr->ifp);
79182cd038dSYoshinobu Inoue 
792201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
793201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(dr->ifp);
794201100c5SBjoern A. Zeeb #endif
795201100c5SBjoern A. Zeeb 
796743eee66SSUZUKI Shinsuke 	if (dr->installed) {
797743eee66SSUZUKI Shinsuke 		deldr = dr;
798743eee66SSUZUKI Shinsuke 		defrouter_delreq(dr);
799743eee66SSUZUKI Shinsuke 	}
80082cd038dSYoshinobu Inoue 
80182cd038dSYoshinobu Inoue 	/*
80282cd038dSYoshinobu Inoue 	 * Also delete all the pointers to the router in each prefix lists.
80382cd038dSYoshinobu Inoue 	 */
804d748f7efSMark Johnston 	ND6_WLOCK();
8053b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
80682cd038dSYoshinobu Inoue 		if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
80782cd038dSYoshinobu Inoue 			pfxrtr_del(pfxrtr);
80882cd038dSYoshinobu Inoue 	}
809d748f7efSMark Johnston 	ND6_WUNLOCK();
810d748f7efSMark Johnston 
81182cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
81282cd038dSYoshinobu Inoue 
81382cd038dSYoshinobu Inoue 	/*
814686cdd19SJun-ichiro itojun Hagino 	 * If the router is the primary one, choose a new one.
815559b4296SAlan Somers 	 * Note that defrouter_select_fib() will remove the current
816559b4296SAlan Somers          * gateway from the routing table.
81782cd038dSYoshinobu Inoue 	 */
81882cd038dSYoshinobu Inoue 	if (deldr)
819559b4296SAlan Somers 		defrouter_select_fib(deldr->ifp->if_fib);
820686cdd19SJun-ichiro itojun Hagino 
8214de485feSMark Johnston 	/*
8224de485feSMark Johnston 	 * Release the list reference.
8234de485feSMark Johnston 	 */
8244de485feSMark Johnston 	defrouter_rele(dr);
82582cd038dSYoshinobu Inoue }
82682cd038dSYoshinobu Inoue 
827ad675b32SBjoern A. Zeeb 
828ad675b32SBjoern A. Zeeb /*
829ad675b32SBjoern A. Zeeb  * Look up a matching default router list entry and remove it. Returns true if a
830ad675b32SBjoern A. Zeeb  * matching entry was found, false otherwise.
831ad675b32SBjoern A. Zeeb  */
832ad675b32SBjoern A. Zeeb bool
833ad675b32SBjoern A. Zeeb defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
834ad675b32SBjoern A. Zeeb {
835ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
836ad675b32SBjoern A. Zeeb 
837ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
838ad675b32SBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
839ad675b32SBjoern A. Zeeb 	if (dr == NULL) {
840ad675b32SBjoern A. Zeeb 		ND6_WUNLOCK();
841ad675b32SBjoern A. Zeeb 		return (false);
842ad675b32SBjoern A. Zeeb 	}
843ad675b32SBjoern A. Zeeb 
844ad675b32SBjoern A. Zeeb 	defrouter_unlink(dr, NULL);
845ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
846ad675b32SBjoern A. Zeeb 	defrouter_del(dr);
847ad675b32SBjoern A. Zeeb 	defrouter_rele(dr);
848ad675b32SBjoern A. Zeeb 	return (true);
849ad675b32SBjoern A. Zeeb }
850ad675b32SBjoern A. Zeeb 
851686cdd19SJun-ichiro itojun Hagino /*
852743eee66SSUZUKI Shinsuke  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
853743eee66SSUZUKI Shinsuke  * draft-ietf-ipngwg-router-selection:
854743eee66SSUZUKI Shinsuke  * 1) Routers that are reachable or probably reachable should be preferred.
855743eee66SSUZUKI Shinsuke  *    If we have more than one (probably) reachable router, prefer ones
856743eee66SSUZUKI Shinsuke  *    with the highest router preference.
857686cdd19SJun-ichiro itojun Hagino  * 2) When no routers on the list are known to be reachable or
858686cdd19SJun-ichiro itojun Hagino  *    probably reachable, routers SHOULD be selected in a round-robin
859743eee66SSUZUKI Shinsuke  *    fashion, regardless of router preference values.
860686cdd19SJun-ichiro itojun Hagino  * 3) If the Default Router List is empty, assume that all
861686cdd19SJun-ichiro itojun Hagino  *    destinations are on-link.
862743eee66SSUZUKI Shinsuke  *
863743eee66SSUZUKI Shinsuke  * We assume nd_defrouter is sorted by router preference value.
864743eee66SSUZUKI Shinsuke  * Since the code below covers both with and without router preference cases,
865743eee66SSUZUKI Shinsuke  * we do not need to classify the cases by ifdef.
866743eee66SSUZUKI Shinsuke  *
867743eee66SSUZUKI Shinsuke  * At this moment, we do not try to install more than one default router,
868743eee66SSUZUKI Shinsuke  * even when the multipath routing is available, because we're not sure about
869743eee66SSUZUKI Shinsuke  * the benefits for stub hosts comparing to the risk of making the code
870743eee66SSUZUKI Shinsuke  * complicated and the possibility of introducing bugs.
871559b4296SAlan Somers  *
872559b4296SAlan Somers  * We maintain a single list of routers for multiple FIBs, only considering one
873559b4296SAlan Somers  * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
874559b4296SAlan Somers  * we do the whole thing multiple times.
875686cdd19SJun-ichiro itojun Hagino  */
876686cdd19SJun-ichiro itojun Hagino void
877559b4296SAlan Somers defrouter_select_fib(int fibnum)
878686cdd19SJun-ichiro itojun Hagino {
879a68cc388SGleb Smirnoff 	struct epoch_tracker et;
8804de485feSMark Johnston 	struct nd_defrouter *dr, *selected_dr, *installed_dr;
8816e6b3f7cSQing Li 	struct llentry *ln = NULL;
882686cdd19SJun-ichiro itojun Hagino 
883559b4296SAlan Somers 	if (fibnum == RT_ALL_FIBS) {
884559b4296SAlan Somers 		for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
885559b4296SAlan Somers 			defrouter_select_fib(fibnum);
886559b4296SAlan Somers 		}
887559b4296SAlan Somers 	}
888559b4296SAlan Somers 
8894de485feSMark Johnston 	ND6_RLOCK();
890686cdd19SJun-ichiro itojun Hagino 	/*
891743eee66SSUZUKI Shinsuke 	 * Let's handle easy case (3) first:
892743eee66SSUZUKI Shinsuke 	 * If default router list is empty, there's nothing to be done.
893743eee66SSUZUKI Shinsuke 	 */
8944de485feSMark Johnston 	if (TAILQ_EMPTY(&V_nd_defrouter)) {
8954de485feSMark Johnston 		ND6_RUNLOCK();
896743eee66SSUZUKI Shinsuke 		return;
8974de485feSMark Johnston 	}
898743eee66SSUZUKI Shinsuke 
899743eee66SSUZUKI Shinsuke 	/*
900686cdd19SJun-ichiro itojun Hagino 	 * Search for a (probably) reachable router from the list.
901743eee66SSUZUKI Shinsuke 	 * We just pick up the first reachable one (if any), assuming that
902743eee66SSUZUKI Shinsuke 	 * the ordering rule of the list described in defrtrlist_update().
903686cdd19SJun-ichiro itojun Hagino 	 */
9044de485feSMark Johnston 	selected_dr = installed_dr = NULL;
9053b0b2840SJohn Baldwin 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
906a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
907559b4296SAlan Somers 		if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
9086e6b3f7cSQing Li 		    (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
909686cdd19SJun-ichiro itojun Hagino 		    ND6_IS_LLINFO_PROBREACH(ln)) {
910743eee66SSUZUKI Shinsuke 			selected_dr = dr;
9114de485feSMark Johnston 			defrouter_ref(selected_dr);
912686cdd19SJun-ichiro itojun Hagino 		}
913a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
9145f16e341SBjoern A. Zeeb 		if (ln != NULL) {
915c0641cc0SKip Macy 			LLE_RUNLOCK(ln);
9165f16e341SBjoern A. Zeeb 			ln = NULL;
9175f16e341SBjoern A. Zeeb 		}
918686cdd19SJun-ichiro itojun Hagino 
919559b4296SAlan Somers 		if (dr->installed && dr->ifp->if_fib == fibnum) {
9204de485feSMark Johnston 			if (installed_dr == NULL) {
921743eee66SSUZUKI Shinsuke 				installed_dr = dr;
9224de485feSMark Johnston 				defrouter_ref(installed_dr);
9234de485feSMark Johnston 			} else {
924559b4296SAlan Somers 				/*
925559b4296SAlan Somers 				 * this should not happen.
926559b4296SAlan Somers 				 * warn for diagnosis.
927559b4296SAlan Somers 				 */
928559b4296SAlan Somers 				log(LOG_ERR, "defrouter_select_fib: more than "
929559b4296SAlan Somers 				             "one router is installed\n");
9304de485feSMark Johnston 			}
93133841545SHajimu UMEMOTO 		}
932686cdd19SJun-ichiro itojun Hagino 	}
933743eee66SSUZUKI Shinsuke 	/*
934743eee66SSUZUKI Shinsuke 	 * If none of the default routers was found to be reachable,
935743eee66SSUZUKI Shinsuke 	 * round-robin the list regardless of preference.
936743eee66SSUZUKI Shinsuke 	 * Otherwise, if we have an installed router, check if the selected
937743eee66SSUZUKI Shinsuke 	 * (reachable) router should really be preferred to the installed one.
938743eee66SSUZUKI Shinsuke 	 * We only prefer the new router when the old one is not reachable
939743eee66SSUZUKI Shinsuke 	 * or when the new one has a really higher preference value.
940743eee66SSUZUKI Shinsuke 	 */
941743eee66SSUZUKI Shinsuke 	if (selected_dr == NULL) {
9424de485feSMark Johnston 		if (installed_dr == NULL ||
9434de485feSMark Johnston 		    TAILQ_NEXT(installed_dr, dr_entry) == NULL)
944559b4296SAlan Somers 			dr = TAILQ_FIRST(&V_nd_defrouter);
945743eee66SSUZUKI Shinsuke 		else
946559b4296SAlan Somers 			dr = TAILQ_NEXT(installed_dr, dr_entry);
947559b4296SAlan Somers 
948559b4296SAlan Somers 		/* Ensure we select a router for this FIB. */
949559b4296SAlan Somers 		TAILQ_FOREACH_FROM(dr, &V_nd_defrouter, dr_entry) {
950559b4296SAlan Somers 			if (dr->ifp->if_fib == fibnum) {
951559b4296SAlan Somers 				selected_dr = dr;
9524de485feSMark Johnston 				defrouter_ref(selected_dr);
953559b4296SAlan Somers 				break;
954559b4296SAlan Somers 			}
955559b4296SAlan Somers 		}
9564de485feSMark Johnston 	} else if (installed_dr != NULL) {
957a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
958559b4296SAlan Somers 		if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
959559b4296SAlan Somers 		                     installed_dr->ifp)) &&
960743eee66SSUZUKI Shinsuke 		    ND6_IS_LLINFO_PROBREACH(ln) &&
961559b4296SAlan Somers 		    installed_dr->ifp->if_fib == fibnum &&
962743eee66SSUZUKI Shinsuke 		    rtpref(selected_dr) <= rtpref(installed_dr)) {
9634de485feSMark Johnston 			defrouter_rele(selected_dr);
964743eee66SSUZUKI Shinsuke 			selected_dr = installed_dr;
965743eee66SSUZUKI Shinsuke 		}
966a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
967c0641cc0SKip Macy 		if (ln != NULL)
968c0641cc0SKip Macy 			LLE_RUNLOCK(ln);
9696e6b3f7cSQing Li 	}
9704de485feSMark Johnston 	ND6_RUNLOCK();
971743eee66SSUZUKI Shinsuke 
972743eee66SSUZUKI Shinsuke 	/*
973559b4296SAlan Somers 	 * If we selected a router for this FIB and it's different
974559b4296SAlan Somers 	 * than the installed one, remove the installed router and
975559b4296SAlan Somers 	 * install the selected one in its place.
976743eee66SSUZUKI Shinsuke 	 */
977743eee66SSUZUKI Shinsuke 	if (installed_dr != selected_dr) {
9784de485feSMark Johnston 		if (installed_dr != NULL) {
979743eee66SSUZUKI Shinsuke 			defrouter_delreq(installed_dr);
9804de485feSMark Johnston 			defrouter_rele(installed_dr);
9814de485feSMark Johnston 		}
982559b4296SAlan Somers 		if (selected_dr != NULL)
983743eee66SSUZUKI Shinsuke 			defrouter_addreq(selected_dr);
984686cdd19SJun-ichiro itojun Hagino 	}
985559b4296SAlan Somers 	if (selected_dr != NULL)
9864de485feSMark Johnston 		defrouter_rele(selected_dr);
987686cdd19SJun-ichiro itojun Hagino }
988686cdd19SJun-ichiro itojun Hagino 
989743eee66SSUZUKI Shinsuke /*
990559b4296SAlan Somers  * Maintain old KPI for default router selection.
991559b4296SAlan Somers  * If unspecified, we can re-select routers for all FIBs.
992559b4296SAlan Somers  */
993559b4296SAlan Somers void
994559b4296SAlan Somers defrouter_select(void)
995559b4296SAlan Somers {
996559b4296SAlan Somers 	defrouter_select_fib(RT_ALL_FIBS);
997559b4296SAlan Somers }
998559b4296SAlan Somers 
999559b4296SAlan Somers /*
1000743eee66SSUZUKI Shinsuke  * for default router selection
1001743eee66SSUZUKI Shinsuke  * regards router-preference field as a 2-bit signed integer
1002743eee66SSUZUKI Shinsuke  */
1003743eee66SSUZUKI Shinsuke static int
1004743eee66SSUZUKI Shinsuke rtpref(struct nd_defrouter *dr)
1005743eee66SSUZUKI Shinsuke {
100601869be5SMark Johnston 	switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
1007743eee66SSUZUKI Shinsuke 	case ND_RA_FLAG_RTPREF_HIGH:
1008743eee66SSUZUKI Shinsuke 		return (RTPREF_HIGH);
1009743eee66SSUZUKI Shinsuke 	case ND_RA_FLAG_RTPREF_MEDIUM:
1010743eee66SSUZUKI Shinsuke 	case ND_RA_FLAG_RTPREF_RSV:
1011d3693a63SSUZUKI Shinsuke 		return (RTPREF_MEDIUM);
1012743eee66SSUZUKI Shinsuke 	case ND_RA_FLAG_RTPREF_LOW:
1013743eee66SSUZUKI Shinsuke 		return (RTPREF_LOW);
1014743eee66SSUZUKI Shinsuke 	default:
1015743eee66SSUZUKI Shinsuke 		/*
1016743eee66SSUZUKI Shinsuke 		 * This case should never happen.  If it did, it would mean a
1017743eee66SSUZUKI Shinsuke 		 * serious bug of kernel internal.  We thus always bark here.
1018743eee66SSUZUKI Shinsuke 		 * Or, can we even panic?
1019743eee66SSUZUKI Shinsuke 		 */
102001869be5SMark Johnston 		log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
1021743eee66SSUZUKI Shinsuke 		return (RTPREF_INVALID);
1022743eee66SSUZUKI Shinsuke 	}
1023743eee66SSUZUKI Shinsuke 	/* NOTREACHED */
1024743eee66SSUZUKI Shinsuke }
1025743eee66SSUZUKI Shinsuke 
102682cd038dSYoshinobu Inoue static struct nd_defrouter *
10271272577eSXin LI defrtrlist_update(struct nd_defrouter *new)
102882cd038dSYoshinobu Inoue {
102982cd038dSYoshinobu Inoue 	struct nd_defrouter *dr, *n;
1030d748f7efSMark Johnston 	uint64_t genid;
1031a4a7c825SMark Johnston 	int oldpref;
1032d748f7efSMark Johnston 	bool writelocked;
103382cd038dSYoshinobu Inoue 
103482cd038dSYoshinobu Inoue 	if (new->rtlifetime == 0) {
1035ff63037dSMark Johnston 		defrouter_remove(&new->rtaddr, new->ifp);
1036a4a7c825SMark Johnston 		return (NULL);
1037a4a7c825SMark Johnston 	}
1038a4a7c825SMark Johnston 
1039d748f7efSMark Johnston 	ND6_RLOCK();
1040d748f7efSMark Johnston 	writelocked = false;
1041d748f7efSMark Johnston restart:
1042ff63037dSMark Johnston 	dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
1043ff63037dSMark Johnston 	if (dr != NULL) {
1044a4a7c825SMark Johnston 		oldpref = rtpref(dr);
1045743eee66SSUZUKI Shinsuke 
104682cd038dSYoshinobu Inoue 		/* override */
104701869be5SMark Johnston 		dr->raflags = new->raflags; /* XXX flag check */
104882cd038dSYoshinobu Inoue 		dr->rtlifetime = new->rtlifetime;
104982cd038dSYoshinobu Inoue 		dr->expire = new->expire;
1050743eee66SSUZUKI Shinsuke 
1051743eee66SSUZUKI Shinsuke 		/*
1052743eee66SSUZUKI Shinsuke 		 * If the preference does not change, there's no need
10530f1aca65SQing Li 		 * to sort the entries. Also make sure the selected
10540f1aca65SQing Li 		 * router is still installed in the kernel.
1055743eee66SSUZUKI Shinsuke 		 */
10564de485feSMark Johnston 		if (dr->installed && rtpref(new) == oldpref) {
1057d748f7efSMark Johnston 			if (writelocked)
10584de485feSMark Johnston 				ND6_WUNLOCK();
1059d748f7efSMark Johnston 			else
1060d748f7efSMark Johnston 				ND6_RUNLOCK();
1061743eee66SSUZUKI Shinsuke 			return (dr);
10624de485feSMark Johnston 		}
1063d748f7efSMark Johnston 	}
1064743eee66SSUZUKI Shinsuke 
1065743eee66SSUZUKI Shinsuke 	/*
1066d748f7efSMark Johnston 	 * The router needs to be reinserted into the default router
1067d748f7efSMark Johnston 	 * list, so upgrade to a write lock. If that fails and the list
1068d748f7efSMark Johnston 	 * has potentially changed while the lock was dropped, we'll
1069d748f7efSMark Johnston 	 * redo the lookup with the write lock held.
1070d748f7efSMark Johnston 	 */
1071d748f7efSMark Johnston 	if (!writelocked) {
1072d748f7efSMark Johnston 		writelocked = true;
1073d748f7efSMark Johnston 		if (!ND6_TRY_UPGRADE()) {
1074d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1075d748f7efSMark Johnston 			ND6_RUNLOCK();
1076d748f7efSMark Johnston 			ND6_WLOCK();
1077d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1078d748f7efSMark Johnston 				goto restart;
1079d748f7efSMark Johnston 		}
1080d748f7efSMark Johnston 	}
1081d748f7efSMark Johnston 
1082d748f7efSMark Johnston 	if (dr != NULL) {
1083d748f7efSMark Johnston 		/*
1084a4a7c825SMark Johnston 		 * The preferred router may have changed, so relocate this
1085a4a7c825SMark Johnston 		 * router.
1086743eee66SSUZUKI Shinsuke 		 */
1087603724d3SBjoern A. Zeeb 		TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
1088743eee66SSUZUKI Shinsuke 		n = dr;
1089ff63037dSMark Johnston 	} else {
1090fc315641SMark Johnston 		n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
10914de485feSMark Johnston 		if (n == NULL) {
10924de485feSMark Johnston 			ND6_WUNLOCK();
109382cd038dSYoshinobu Inoue 			return (NULL);
10944de485feSMark Johnston 		}
1095fc315641SMark Johnston 		memcpy(n, new, sizeof(*n));
10964de485feSMark Johnston 		/* Initialize with an extra reference for the caller. */
10974de485feSMark Johnston 		refcount_init(&n->refcnt, 2);
1098ff63037dSMark Johnston 	}
1099686cdd19SJun-ichiro itojun Hagino 
1100686cdd19SJun-ichiro itojun Hagino 	/*
1101743eee66SSUZUKI Shinsuke 	 * Insert the new router in the Default Router List;
1102743eee66SSUZUKI Shinsuke 	 * The Default Router List should be in the descending order
1103743eee66SSUZUKI Shinsuke 	 * of router-preferece.  Routers with the same preference are
1104743eee66SSUZUKI Shinsuke 	 * sorted in the arriving time order.
1105686cdd19SJun-ichiro itojun Hagino 	 */
1106743eee66SSUZUKI Shinsuke 
1107743eee66SSUZUKI Shinsuke 	/* insert at the end of the group */
11083b0b2840SJohn Baldwin 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1109743eee66SSUZUKI Shinsuke 		if (rtpref(n) > rtpref(dr))
1110743eee66SSUZUKI Shinsuke 			break;
1111743eee66SSUZUKI Shinsuke 	}
11124de485feSMark Johnston 	if (dr != NULL)
1113743eee66SSUZUKI Shinsuke 		TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1114743eee66SSUZUKI Shinsuke 	else
1115603724d3SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
1116d748f7efSMark Johnston 	V_nd6_list_genid++;
11174de485feSMark Johnston 	ND6_WUNLOCK();
1118743eee66SSUZUKI Shinsuke 
1119559b4296SAlan Somers 	defrouter_select_fib(new->ifp->if_fib);
1120743eee66SSUZUKI Shinsuke 
112182cd038dSYoshinobu Inoue 	return (n);
112282cd038dSYoshinobu Inoue }
112382cd038dSYoshinobu Inoue 
112482cd038dSYoshinobu Inoue static struct nd_pfxrouter *
11251272577eSXin LI pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
112682cd038dSYoshinobu Inoue {
112782cd038dSYoshinobu Inoue 	struct nd_pfxrouter *search;
112882cd038dSYoshinobu Inoue 
1129d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1130d748f7efSMark Johnston 
11313b0b2840SJohn Baldwin 	LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
113282cd038dSYoshinobu Inoue 		if (search->router == dr)
113382cd038dSYoshinobu Inoue 			break;
113482cd038dSYoshinobu Inoue 	}
113582cd038dSYoshinobu Inoue 	return (search);
113682cd038dSYoshinobu Inoue }
113782cd038dSYoshinobu Inoue 
113882cd038dSYoshinobu Inoue static void
11391272577eSXin LI pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
114082cd038dSYoshinobu Inoue {
114182cd038dSYoshinobu Inoue 	struct nd_pfxrouter *new;
1142d748f7efSMark Johnston 	bool update;
1143d748f7efSMark Johnston 
1144d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
1145d748f7efSMark Johnston 
1146d748f7efSMark Johnston 	ND6_RLOCK();
1147d748f7efSMark Johnston 	if (pfxrtr_lookup(pr, dr) != NULL) {
1148d748f7efSMark Johnston 		ND6_RUNLOCK();
1149d748f7efSMark Johnston 		return;
1150d748f7efSMark Johnston 	}
1151d748f7efSMark Johnston 	ND6_RUNLOCK();
115282cd038dSYoshinobu Inoue 
1153fc315641SMark Johnston 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
115482cd038dSYoshinobu Inoue 	if (new == NULL)
115582cd038dSYoshinobu Inoue 		return;
11564de485feSMark Johnston 	defrouter_ref(dr);
1157d748f7efSMark Johnston 	new->router = dr;
115882cd038dSYoshinobu Inoue 
1159d748f7efSMark Johnston 	ND6_WLOCK();
1160d748f7efSMark Johnston 	if (pfxrtr_lookup(pr, dr) == NULL) {
116182cd038dSYoshinobu Inoue 		LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
1162d748f7efSMark Johnston 		update = true;
1163d748f7efSMark Johnston 	} else {
1164d748f7efSMark Johnston 		/* We lost a race to add the reference. */
1165d748f7efSMark Johnston 		defrouter_rele(dr);
1166d748f7efSMark Johnston 		free(new, M_IP6NDP);
1167d748f7efSMark Johnston 		update = false;
1168d748f7efSMark Johnston 	}
1169d748f7efSMark Johnston 	ND6_WUNLOCK();
117082cd038dSYoshinobu Inoue 
1171d748f7efSMark Johnston 	if (update)
117282cd038dSYoshinobu Inoue 		pfxlist_onlink_check();
117382cd038dSYoshinobu Inoue }
117482cd038dSYoshinobu Inoue 
117582cd038dSYoshinobu Inoue static void
11761272577eSXin LI pfxrtr_del(struct nd_pfxrouter *pfr)
117782cd038dSYoshinobu Inoue {
11784de485feSMark Johnston 
1179d748f7efSMark Johnston 	ND6_WLOCK_ASSERT();
1180d748f7efSMark Johnston 
118182cd038dSYoshinobu Inoue 	LIST_REMOVE(pfr, pfr_entry);
11824de485feSMark Johnston 	defrouter_rele(pfr->router);
118382cd038dSYoshinobu Inoue 	free(pfr, M_IP6NDP);
118482cd038dSYoshinobu Inoue }
118582cd038dSYoshinobu Inoue 
1186d748f7efSMark Johnston static struct nd_prefix *
1187d748f7efSMark Johnston nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1188d748f7efSMark Johnston {
1189d748f7efSMark Johnston 	struct nd_prefix *search;
1190d748f7efSMark Johnston 
1191d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1192d748f7efSMark Johnston 
1193d748f7efSMark Johnston 	LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1194d748f7efSMark Johnston 		if (key->ndpr_ifp == search->ndpr_ifp &&
1195d748f7efSMark Johnston 		    key->ndpr_plen == search->ndpr_plen &&
1196d748f7efSMark Johnston 		    in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1197d748f7efSMark Johnston 		    &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1198d748f7efSMark Johnston 			nd6_prefix_ref(search);
1199d748f7efSMark Johnston 			break;
1200d748f7efSMark Johnston 		}
1201d748f7efSMark Johnston 	}
1202d748f7efSMark Johnston 	return (search);
1203d748f7efSMark Johnston }
1204d748f7efSMark Johnston 
120533841545SHajimu UMEMOTO struct nd_prefix *
12061272577eSXin LI nd6_prefix_lookup(struct nd_prefixctl *key)
120782cd038dSYoshinobu Inoue {
120882cd038dSYoshinobu Inoue 	struct nd_prefix *search;
120982cd038dSYoshinobu Inoue 
1210d748f7efSMark Johnston 	ND6_RLOCK();
1211d748f7efSMark Johnston 	search = nd6_prefix_lookup_locked(key);
1212d748f7efSMark Johnston 	ND6_RUNLOCK();
1213d748f7efSMark Johnston 	return (search);
121482cd038dSYoshinobu Inoue }
121582cd038dSYoshinobu Inoue 
1216d748f7efSMark Johnston void
1217d748f7efSMark Johnston nd6_prefix_ref(struct nd_prefix *pr)
1218d748f7efSMark Johnston {
1219d748f7efSMark Johnston 
1220d748f7efSMark Johnston 	refcount_acquire(&pr->ndpr_refcnt);
1221d748f7efSMark Johnston }
1222d748f7efSMark Johnston 
1223d748f7efSMark Johnston void
1224d748f7efSMark Johnston nd6_prefix_rele(struct nd_prefix *pr)
1225d748f7efSMark Johnston {
1226d748f7efSMark Johnston 
1227d748f7efSMark Johnston 	if (refcount_release(&pr->ndpr_refcnt)) {
1228d748f7efSMark Johnston 		KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1229d748f7efSMark Johnston 		    ("prefix %p has advertising routers", pr));
1230d748f7efSMark Johnston 		free(pr, M_IP6NDP);
1231d748f7efSMark Johnston 	}
123282cd038dSYoshinobu Inoue }
123382cd038dSYoshinobu Inoue 
123433841545SHajimu UMEMOTO int
12351272577eSXin LI nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
12361272577eSXin LI     struct nd_prefix **newp)
123782cd038dSYoshinobu Inoue {
1238d748f7efSMark Johnston 	struct nd_prefix *new;
12391d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1240d748f7efSMark Johnston 	int error;
124182cd038dSYoshinobu Inoue 
1242fc315641SMark Johnston 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
124382cd038dSYoshinobu Inoue 	if (new == NULL)
124407eb2995SHajimu UMEMOTO 		return (ENOMEM);
1245d748f7efSMark Johnston 	refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1246743eee66SSUZUKI Shinsuke 	new->ndpr_ifp = pr->ndpr_ifp;
1247743eee66SSUZUKI Shinsuke 	new->ndpr_prefix = pr->ndpr_prefix;
1248743eee66SSUZUKI Shinsuke 	new->ndpr_plen = pr->ndpr_plen;
1249743eee66SSUZUKI Shinsuke 	new->ndpr_vltime = pr->ndpr_vltime;
1250743eee66SSUZUKI Shinsuke 	new->ndpr_pltime = pr->ndpr_pltime;
1251743eee66SSUZUKI Shinsuke 	new->ndpr_flags = pr->ndpr_flags;
1252743eee66SSUZUKI Shinsuke 	if ((error = in6_init_prefix_ltimes(new)) != 0) {
1253743eee66SSUZUKI Shinsuke 		free(new, M_IP6NDP);
1254743eee66SSUZUKI Shinsuke 		return (error);
1255743eee66SSUZUKI Shinsuke 	}
12567d26db17SHiroki Sato 	new->ndpr_lastupdate = time_uptime;
125782cd038dSYoshinobu Inoue 
125807eb2995SHajimu UMEMOTO 	/* initialization */
125982cd038dSYoshinobu Inoue 	LIST_INIT(&new->ndpr_advrtrs);
126082cd038dSYoshinobu Inoue 	in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
126182cd038dSYoshinobu Inoue 	/* make prefix in the canonical form */
12621dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
126382cd038dSYoshinobu Inoue 
1264d748f7efSMark Johnston 	ND6_WLOCK();
1265603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1266d748f7efSMark Johnston 	V_nd6_list_genid++;
1267d748f7efSMark Johnston 	ND6_WUNLOCK();
126882cd038dSYoshinobu Inoue 
126933841545SHajimu UMEMOTO 	/* ND_OPT_PI_FLAG_ONLINK processing */
127033841545SHajimu UMEMOTO 	if (new->ndpr_raf_onlink) {
1271b8a6e03fSGleb Smirnoff 		struct epoch_tracker et;
1272b8a6e03fSGleb Smirnoff 
1273d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1274b8a6e03fSGleb Smirnoff 		NET_EPOCH_ENTER(et);
1275d748f7efSMark Johnston 		if ((error = nd6_prefix_onlink(new)) != 0) {
127633841545SHajimu UMEMOTO 			nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
127733841545SHajimu UMEMOTO 			    "the prefix %s/%d on-link on %s (errno=%d)\n",
12781d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1279d748f7efSMark Johnston 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
128033841545SHajimu UMEMOTO 			/* proceed anyway. XXX: is it correct? */
128133841545SHajimu UMEMOTO 		}
1282b8a6e03fSGleb Smirnoff 		NET_EPOCH_EXIT(et);
1283d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
128433841545SHajimu UMEMOTO 	}
128533841545SHajimu UMEMOTO 
128620b5f022SMark Johnston 	if (dr != NULL)
128782cd038dSYoshinobu Inoue 		pfxrtr_add(new, dr);
128820b5f022SMark Johnston 	if (newp != NULL)
128920b5f022SMark Johnston 		*newp = new;
129020b5f022SMark Johnston 	return (0);
129182cd038dSYoshinobu Inoue }
129282cd038dSYoshinobu Inoue 
1293d748f7efSMark Johnston /*
1294d748f7efSMark Johnston  * Remove a prefix from the prefix list and optionally stash it in a
1295d748f7efSMark Johnston  * caller-provided list.
1296d748f7efSMark Johnston  *
1297d748f7efSMark Johnston  * The ND6 lock must be held.
1298d748f7efSMark Johnston  */
129982cd038dSYoshinobu Inoue void
1300d748f7efSMark Johnston nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1301d748f7efSMark Johnston {
1302d748f7efSMark Johnston 
1303d748f7efSMark Johnston 	ND6_WLOCK_ASSERT();
1304d748f7efSMark Johnston 
1305d748f7efSMark Johnston 	LIST_REMOVE(pr, ndpr_entry);
1306d748f7efSMark Johnston 	V_nd6_list_genid++;
1307d748f7efSMark Johnston 	if (list != NULL)
1308d748f7efSMark Johnston 		LIST_INSERT_HEAD(list, pr, ndpr_entry);
1309d748f7efSMark Johnston }
1310d748f7efSMark Johnston 
1311d748f7efSMark Johnston /*
1312d748f7efSMark Johnston  * Free an unlinked prefix, first marking it off-link if necessary.
1313d748f7efSMark Johnston  */
1314d748f7efSMark Johnston void
1315d748f7efSMark Johnston nd6_prefix_del(struct nd_prefix *pr)
131682cd038dSYoshinobu Inoue {
131782cd038dSYoshinobu Inoue 	struct nd_pfxrouter *pfr, *next;
1318c9b652e3SAndre Oppermann 	int e;
13191d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
132033841545SHajimu UMEMOTO 
1321d748f7efSMark Johnston 	KASSERT(pr->ndpr_addrcnt == 0,
1322d748f7efSMark Johnston 	    ("prefix %p has referencing addresses", pr));
1323d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
1324743eee66SSUZUKI Shinsuke 
132533841545SHajimu UMEMOTO 	/*
132633841545SHajimu UMEMOTO 	 * Though these flags are now meaningless, we'd rather keep the value
13277aa59493SSUZUKI Shinsuke 	 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
13287aa59493SSUZUKI Shinsuke 	 * when executing "ndp -p".
132933841545SHajimu UMEMOTO 	 */
1330d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1331d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1332d748f7efSMark Johnston 		if ((e = nd6_prefix_offlink(pr)) != 0) {
1333d748f7efSMark Johnston 			nd6log((LOG_ERR,
1334d748f7efSMark Johnston 			    "nd6_prefix_del: failed to make %s/%d offlink "
133533841545SHajimu UMEMOTO 			    "on %s, errno=%d\n",
13361d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
133733841545SHajimu UMEMOTO 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
133833841545SHajimu UMEMOTO 			/* what should we do? */
133933841545SHajimu UMEMOTO 		}
1340d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
134182cd038dSYoshinobu Inoue 	}
1342d748f7efSMark Johnston 
1343d748f7efSMark Johnston 	/* Release references to routers that have advertised this prefix. */
1344d748f7efSMark Johnston 	ND6_WLOCK();
1345d748f7efSMark Johnston 	LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1346d748f7efSMark Johnston 		pfxrtr_del(pfr);
1347d748f7efSMark Johnston 	ND6_WUNLOCK();
1348d748f7efSMark Johnston 
1349d748f7efSMark Johnston 	nd6_prefix_rele(pr);
135082cd038dSYoshinobu Inoue 
135182cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
135282cd038dSYoshinobu Inoue }
135382cd038dSYoshinobu Inoue 
1354743eee66SSUZUKI Shinsuke static int
13551272577eSXin LI prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
13561272577eSXin LI     struct mbuf *m, int mcast)
135782cd038dSYoshinobu Inoue {
135833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
135933841545SHajimu UMEMOTO 	struct ifaddr *ifa;
136033841545SHajimu UMEMOTO 	struct ifnet *ifp = new->ndpr_ifp;
136182cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
136282cd038dSYoshinobu Inoue 	int error = 0;
136382cd038dSYoshinobu Inoue 	int auth;
136433841545SHajimu UMEMOTO 	struct in6_addrlifetime lt6_tmp;
13651d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1366b8a6e03fSGleb Smirnoff 
1367b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
136882cd038dSYoshinobu Inoue 
136982cd038dSYoshinobu Inoue 	auth = 0;
137082cd038dSYoshinobu Inoue 	if (m) {
137182cd038dSYoshinobu Inoue 		/*
137282cd038dSYoshinobu Inoue 		 * Authenticity for NA consists authentication for
137382cd038dSYoshinobu Inoue 		 * both IP header and IP datagrams, doesn't it ?
137482cd038dSYoshinobu Inoue 		 */
137582cd038dSYoshinobu Inoue #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
137607eb2995SHajimu UMEMOTO 		auth = ((m->m_flags & M_AUTHIPHDR) &&
137707eb2995SHajimu UMEMOTO 		    (m->m_flags & M_AUTHIPDGM));
137882cd038dSYoshinobu Inoue #endif
137982cd038dSYoshinobu Inoue 	}
138082cd038dSYoshinobu Inoue 
138133841545SHajimu UMEMOTO 	if ((pr = nd6_prefix_lookup(new)) != NULL) {
138233841545SHajimu UMEMOTO 		/*
138333841545SHajimu UMEMOTO 		 * nd6_prefix_lookup() ensures that pr and new have the same
138433841545SHajimu UMEMOTO 		 * prefix on a same interface.
138533841545SHajimu UMEMOTO 		 */
138633841545SHajimu UMEMOTO 
138733841545SHajimu UMEMOTO 		/*
138833841545SHajimu UMEMOTO 		 * Update prefix information.  Note that the on-link (L) bit
138933841545SHajimu UMEMOTO 		 * and the autonomous (A) bit should NOT be changed from 1
139033841545SHajimu UMEMOTO 		 * to 0.
139133841545SHajimu UMEMOTO 		 */
139233841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 1)
139333841545SHajimu UMEMOTO 			pr->ndpr_raf_onlink = 1;
139433841545SHajimu UMEMOTO 		if (new->ndpr_raf_auto == 1)
139533841545SHajimu UMEMOTO 			pr->ndpr_raf_auto = 1;
139633841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink) {
139782cd038dSYoshinobu Inoue 			pr->ndpr_vltime = new->ndpr_vltime;
139882cd038dSYoshinobu Inoue 			pr->ndpr_pltime = new->ndpr_pltime;
1399743eee66SSUZUKI Shinsuke 			(void)in6_init_prefix_ltimes(pr); /* XXX error case? */
14007d26db17SHiroki Sato 			pr->ndpr_lastupdate = time_uptime;
140182cd038dSYoshinobu Inoue 		}
140282cd038dSYoshinobu Inoue 
140333841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink &&
140433841545SHajimu UMEMOTO 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1405d748f7efSMark Johnston 			ND6_ONLINK_LOCK();
1406d748f7efSMark Johnston 			if ((error = nd6_prefix_onlink(pr)) != 0) {
140733841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
140833841545SHajimu UMEMOTO 				    "prelist_update: failed to make "
140933841545SHajimu UMEMOTO 				    "the prefix %s/%d on-link on %s "
141033841545SHajimu UMEMOTO 				    "(errno=%d)\n",
14111d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
14121d54aa3bSBjoern A. Zeeb 				        &pr->ndpr_prefix.sin6_addr),
1413d748f7efSMark Johnston 				    pr->ndpr_plen, if_name(pr->ndpr_ifp),
1414d748f7efSMark Johnston 				    error));
141533841545SHajimu UMEMOTO 				/* proceed anyway. XXX: is it correct? */
1416686cdd19SJun-ichiro itojun Hagino 			}
1417d748f7efSMark Johnston 			ND6_ONLINK_UNLOCK();
141882cd038dSYoshinobu Inoue 		}
141982cd038dSYoshinobu Inoue 
1420d748f7efSMark Johnston 		if (dr != NULL)
142182cd038dSYoshinobu Inoue 			pfxrtr_add(pr, dr);
142282cd038dSYoshinobu Inoue 	} else {
142333841545SHajimu UMEMOTO 		if (new->ndpr_vltime == 0)
142433841545SHajimu UMEMOTO 			goto end;
142533841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
142633841545SHajimu UMEMOTO 			goto end;
142782cd038dSYoshinobu Inoue 
142883631b16SMark Johnston 		error = nd6_prelist_add(new, dr, &pr);
142920b5f022SMark Johnston 		if (error != 0) {
143033841545SHajimu UMEMOTO 			nd6log((LOG_NOTICE, "prelist_update: "
143120b5f022SMark Johnston 			    "nd6_prelist_add failed for %s/%d on %s errno=%d\n",
14321d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
143320b5f022SMark Johnston 			    new->ndpr_plen, if_name(new->ndpr_ifp), error));
143433841545SHajimu UMEMOTO 			goto end; /* we should just give up in this case. */
143533841545SHajimu UMEMOTO 		}
143633841545SHajimu UMEMOTO 
143782cd038dSYoshinobu Inoue 		/*
143833841545SHajimu UMEMOTO 		 * XXX: from the ND point of view, we can ignore a prefix
143933841545SHajimu UMEMOTO 		 * with the on-link bit being zero.  However, we need a
144033841545SHajimu UMEMOTO 		 * prefix structure for references from autoconfigured
144107eb2995SHajimu UMEMOTO 		 * addresses.  Thus, we explicitly make sure that the prefix
144233841545SHajimu UMEMOTO 		 * itself expires now.
144382cd038dSYoshinobu Inoue 		 */
144483631b16SMark Johnston 		if (pr->ndpr_raf_onlink == 0) {
144583631b16SMark Johnston 			pr->ndpr_vltime = 0;
144683631b16SMark Johnston 			pr->ndpr_pltime = 0;
144783631b16SMark Johnston 			in6_init_prefix_ltimes(pr);
144833841545SHajimu UMEMOTO 		}
144933841545SHajimu UMEMOTO 	}
145033841545SHajimu UMEMOTO 
145133841545SHajimu UMEMOTO 	/*
145233841545SHajimu UMEMOTO 	 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
145333841545SHajimu UMEMOTO 	 * Note that pr must be non NULL at this point.
145433841545SHajimu UMEMOTO 	 */
145533841545SHajimu UMEMOTO 
145633841545SHajimu UMEMOTO 	/* 5.5.3 (a). Ignore the prefix without the A bit set. */
145782cd038dSYoshinobu Inoue 	if (!new->ndpr_raf_auto)
1458743eee66SSUZUKI Shinsuke 		goto end;
145982cd038dSYoshinobu Inoue 
146033841545SHajimu UMEMOTO 	/*
146133841545SHajimu UMEMOTO 	 * 5.5.3 (b). the link-local prefix should have been ignored in
146233841545SHajimu UMEMOTO 	 * nd6_ra_input.
146333841545SHajimu UMEMOTO 	 */
146433841545SHajimu UMEMOTO 
1465743eee66SSUZUKI Shinsuke 	/* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1466743eee66SSUZUKI Shinsuke 	if (new->ndpr_pltime > new->ndpr_vltime) {
1467743eee66SSUZUKI Shinsuke 		error = EINVAL;	/* XXX: won't be used */
1468743eee66SSUZUKI Shinsuke 		goto end;
1469743eee66SSUZUKI Shinsuke 	}
147033841545SHajimu UMEMOTO 
147133841545SHajimu UMEMOTO 	/*
1472743eee66SSUZUKI Shinsuke 	 * 5.5.3 (d).  If the prefix advertised is not equal to the prefix of
1473743eee66SSUZUKI Shinsuke 	 * an address configured by stateless autoconfiguration already in the
1474743eee66SSUZUKI Shinsuke 	 * list of addresses associated with the interface, and the Valid
1475743eee66SSUZUKI Shinsuke 	 * Lifetime is not 0, form an address.  We first check if we have
1476743eee66SSUZUKI Shinsuke 	 * a matching prefix.
1477743eee66SSUZUKI Shinsuke 	 * Note: we apply a clarification in rfc2462bis-02 here.  We only
1478743eee66SSUZUKI Shinsuke 	 * consider autoconfigured addresses while RFC2462 simply said
1479743eee66SSUZUKI Shinsuke 	 * "address".
148033841545SHajimu UMEMOTO 	 */
1481d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
148233841545SHajimu UMEMOTO 		struct in6_ifaddr *ifa6;
1483743eee66SSUZUKI Shinsuke 		u_int32_t remaininglifetime;
148433841545SHajimu UMEMOTO 
148533841545SHajimu UMEMOTO 		if (ifa->ifa_addr->sa_family != AF_INET6)
148633841545SHajimu UMEMOTO 			continue;
148733841545SHajimu UMEMOTO 
148833841545SHajimu UMEMOTO 		ifa6 = (struct in6_ifaddr *)ifa;
148933841545SHajimu UMEMOTO 
149033841545SHajimu UMEMOTO 		/*
1491743eee66SSUZUKI Shinsuke 		 * We only consider autoconfigured addresses as per rfc2462bis.
1492743eee66SSUZUKI Shinsuke 		 */
1493743eee66SSUZUKI Shinsuke 		if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1494743eee66SSUZUKI Shinsuke 			continue;
1495743eee66SSUZUKI Shinsuke 
1496743eee66SSUZUKI Shinsuke 		/*
149733841545SHajimu UMEMOTO 		 * Spec is not clear here, but I believe we should concentrate
149833841545SHajimu UMEMOTO 		 * on unicast (i.e. not anycast) addresses.
149933841545SHajimu UMEMOTO 		 * XXX: other ia6_flags? detached or duplicated?
150033841545SHajimu UMEMOTO 		 */
150133841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
150233841545SHajimu UMEMOTO 			continue;
150333841545SHajimu UMEMOTO 
1504743eee66SSUZUKI Shinsuke 		/*
1505743eee66SSUZUKI Shinsuke 		 * Ignore the address if it is not associated with a prefix
1506743eee66SSUZUKI Shinsuke 		 * or is associated with a prefix that is different from this
1507743eee66SSUZUKI Shinsuke 		 * one.  (pr is never NULL here)
1508743eee66SSUZUKI Shinsuke 		 */
1509743eee66SSUZUKI Shinsuke 		if (ifa6->ia6_ndpr != pr)
151033841545SHajimu UMEMOTO 			continue;
151133841545SHajimu UMEMOTO 
151233841545SHajimu UMEMOTO 		if (ia6_match == NULL) /* remember the first one */
151333841545SHajimu UMEMOTO 			ia6_match = ifa6;
151433841545SHajimu UMEMOTO 
151533841545SHajimu UMEMOTO 		/*
151633841545SHajimu UMEMOTO 		 * An already autoconfigured address matched.  Now that we
151733841545SHajimu UMEMOTO 		 * are sure there is at least one matched address, we can
151833841545SHajimu UMEMOTO 		 * proceed to 5.5.3. (e): update the lifetimes according to the
151933841545SHajimu UMEMOTO 		 * "two hours" rule and the privacy extension.
1520743eee66SSUZUKI Shinsuke 		 * We apply some clarifications in rfc2462bis:
1521743eee66SSUZUKI Shinsuke 		 * - use remaininglifetime instead of storedlifetime as a
1522743eee66SSUZUKI Shinsuke 		 *   variable name
1523743eee66SSUZUKI Shinsuke 		 * - remove the dead code in the "two-hour" rule
152433841545SHajimu UMEMOTO 		 */
152533841545SHajimu UMEMOTO #define TWOHOUR		(120*60)
152633841545SHajimu UMEMOTO 		lt6_tmp = ifa6->ia6_lifetime;
152733841545SHajimu UMEMOTO 
152811f3a6e2SHajimu UMEMOTO 		if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1529743eee66SSUZUKI Shinsuke 			remaininglifetime = ND6_INFINITE_LIFETIME;
15307d26db17SHiroki Sato 		else if (time_uptime - ifa6->ia6_updatetime >
1531743eee66SSUZUKI Shinsuke 			 lt6_tmp.ia6t_vltime) {
1532743eee66SSUZUKI Shinsuke 			/*
1533743eee66SSUZUKI Shinsuke 			 * The case of "invalid" address.  We should usually
1534743eee66SSUZUKI Shinsuke 			 * not see this case.
1535743eee66SSUZUKI Shinsuke 			 */
1536743eee66SSUZUKI Shinsuke 			remaininglifetime = 0;
1537743eee66SSUZUKI Shinsuke 		} else
1538743eee66SSUZUKI Shinsuke 			remaininglifetime = lt6_tmp.ia6t_vltime -
15397d26db17SHiroki Sato 			    (time_uptime - ifa6->ia6_updatetime);
154011f3a6e2SHajimu UMEMOTO 
154111f3a6e2SHajimu UMEMOTO 		/* when not updating, keep the current stored lifetime. */
1542743eee66SSUZUKI Shinsuke 		lt6_tmp.ia6t_vltime = remaininglifetime;
154333841545SHajimu UMEMOTO 
154433841545SHajimu UMEMOTO 		if (TWOHOUR < new->ndpr_vltime ||
1545743eee66SSUZUKI Shinsuke 		    remaininglifetime < new->ndpr_vltime) {
154633841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1547743eee66SSUZUKI Shinsuke 		} else if (remaininglifetime <= TWOHOUR) {
154833841545SHajimu UMEMOTO 			if (auth) {
154933841545SHajimu UMEMOTO 				lt6_tmp.ia6t_vltime = new->ndpr_vltime;
155082cd038dSYoshinobu Inoue 			}
155133841545SHajimu UMEMOTO 		} else {
155233841545SHajimu UMEMOTO 			/*
155333841545SHajimu UMEMOTO 			 * new->ndpr_vltime <= TWOHOUR &&
1554743eee66SSUZUKI Shinsuke 			 * TWOHOUR < remaininglifetime
155533841545SHajimu UMEMOTO 			 */
155633841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = TWOHOUR;
155782cd038dSYoshinobu Inoue 		}
155882cd038dSYoshinobu Inoue 
155933841545SHajimu UMEMOTO 		/* The 2 hour rule is not imposed for preferred lifetime. */
156033841545SHajimu UMEMOTO 		lt6_tmp.ia6t_pltime = new->ndpr_pltime;
156133841545SHajimu UMEMOTO 
156233841545SHajimu UMEMOTO 		in6_init_address_ltimes(pr, &lt6_tmp);
156333841545SHajimu UMEMOTO 
156433841545SHajimu UMEMOTO 		/*
1565743eee66SSUZUKI Shinsuke 		 * We need to treat lifetimes for temporary addresses
1566743eee66SSUZUKI Shinsuke 		 * differently, according to
1567743eee66SSUZUKI Shinsuke 		 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1568743eee66SSUZUKI Shinsuke 		 * we only update the lifetimes when they are in the maximum
1569743eee66SSUZUKI Shinsuke 		 * intervals.
157033841545SHajimu UMEMOTO 		 */
157133841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1572743eee66SSUZUKI Shinsuke 			u_int32_t maxvltime, maxpltime;
157333841545SHajimu UMEMOTO 
1574603724d3SBjoern A. Zeeb 			if (V_ip6_temp_valid_lifetime >
15757d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1576603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1577603724d3SBjoern A. Zeeb 				maxvltime = V_ip6_temp_valid_lifetime -
15787d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1579603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1580743eee66SSUZUKI Shinsuke 			} else
1581743eee66SSUZUKI Shinsuke 				maxvltime = 0;
1582603724d3SBjoern A. Zeeb 			if (V_ip6_temp_preferred_lifetime >
15837d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1584603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1585603724d3SBjoern A. Zeeb 				maxpltime = V_ip6_temp_preferred_lifetime -
15867d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1587603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1588743eee66SSUZUKI Shinsuke 			} else
1589743eee66SSUZUKI Shinsuke 				maxpltime = 0;
1590743eee66SSUZUKI Shinsuke 
1591743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1592743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_vltime > maxvltime) {
1593743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_vltime = maxvltime;
1594743eee66SSUZUKI Shinsuke 			}
1595743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1596743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_pltime > maxpltime) {
1597743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_pltime = maxpltime;
1598743eee66SSUZUKI Shinsuke 			}
1599743eee66SSUZUKI Shinsuke 		}
160033841545SHajimu UMEMOTO 		ifa6->ia6_lifetime = lt6_tmp;
16017d26db17SHiroki Sato 		ifa6->ia6_updatetime = time_uptime;
160233841545SHajimu UMEMOTO 	}
160333841545SHajimu UMEMOTO 	if (ia6_match == NULL && new->ndpr_vltime) {
1604743eee66SSUZUKI Shinsuke 		int ifidlen;
1605743eee66SSUZUKI Shinsuke 
160633841545SHajimu UMEMOTO 		/*
1607743eee66SSUZUKI Shinsuke 		 * 5.5.3 (d) (continued)
160833841545SHajimu UMEMOTO 		 * No address matched and the valid lifetime is non-zero.
160933841545SHajimu UMEMOTO 		 * Create a new address.
161033841545SHajimu UMEMOTO 		 */
1611743eee66SSUZUKI Shinsuke 
1612743eee66SSUZUKI Shinsuke 		/*
1613743eee66SSUZUKI Shinsuke 		 * Prefix Length check:
1614743eee66SSUZUKI Shinsuke 		 * If the sum of the prefix length and interface identifier
1615743eee66SSUZUKI Shinsuke 		 * length does not equal 128 bits, the Prefix Information
1616743eee66SSUZUKI Shinsuke 		 * option MUST be ignored.  The length of the interface
1617743eee66SSUZUKI Shinsuke 		 * identifier is defined in a separate link-type specific
1618743eee66SSUZUKI Shinsuke 		 * document.
1619743eee66SSUZUKI Shinsuke 		 */
1620743eee66SSUZUKI Shinsuke 		ifidlen = in6_if2idlen(ifp);
1621743eee66SSUZUKI Shinsuke 		if (ifidlen < 0) {
1622743eee66SSUZUKI Shinsuke 			/* this should not happen, so we always log it. */
1623743eee66SSUZUKI Shinsuke 			log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1624743eee66SSUZUKI Shinsuke 			    if_name(ifp));
1625743eee66SSUZUKI Shinsuke 			goto end;
1626743eee66SSUZUKI Shinsuke 		}
1627743eee66SSUZUKI Shinsuke 		if (ifidlen + pr->ndpr_plen != 128) {
1628743eee66SSUZUKI Shinsuke 			nd6log((LOG_INFO,
1629743eee66SSUZUKI Shinsuke 			    "prelist_update: invalid prefixlen "
1630743eee66SSUZUKI Shinsuke 			    "%d for %s, ignored\n",
1631743eee66SSUZUKI Shinsuke 			    pr->ndpr_plen, if_name(ifp)));
1632743eee66SSUZUKI Shinsuke 			goto end;
1633743eee66SSUZUKI Shinsuke 		}
1634743eee66SSUZUKI Shinsuke 
1635743eee66SSUZUKI Shinsuke 		if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
163633841545SHajimu UMEMOTO 			/*
163733841545SHajimu UMEMOTO 			 * note that we should use pr (not new) for reference.
163833841545SHajimu UMEMOTO 			 */
16390bbf244eSMark Johnston 			pr->ndpr_addrcnt++;
164033841545SHajimu UMEMOTO 			ia6->ia6_ndpr = pr;
164133841545SHajimu UMEMOTO 
164233841545SHajimu UMEMOTO 			/*
164333841545SHajimu UMEMOTO 			 * RFC 3041 3.3 (2).
164433841545SHajimu UMEMOTO 			 * When a new public address is created as described
164533841545SHajimu UMEMOTO 			 * in RFC2462, also create a new temporary address.
164633841545SHajimu UMEMOTO 			 *
164733841545SHajimu UMEMOTO 			 * RFC 3041 3.5.
164833841545SHajimu UMEMOTO 			 * When an interface connects to a new link, a new
164933841545SHajimu UMEMOTO 			 * randomized interface identifier should be generated
165033841545SHajimu UMEMOTO 			 * immediately together with a new set of temporary
165133841545SHajimu UMEMOTO 			 * addresses.  Thus, we specifiy 1 as the 2nd arg of
165233841545SHajimu UMEMOTO 			 * in6_tmpifadd().
165333841545SHajimu UMEMOTO 			 */
1654603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr) {
165533841545SHajimu UMEMOTO 				int e;
1656743eee66SSUZUKI Shinsuke 				if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
165733841545SHajimu UMEMOTO 					nd6log((LOG_NOTICE, "prelist_update: "
165833841545SHajimu UMEMOTO 					    "failed to create a temporary "
165933841545SHajimu UMEMOTO 					    "address, errno=%d\n",
166033841545SHajimu UMEMOTO 					    e));
166133841545SHajimu UMEMOTO 				}
166233841545SHajimu UMEMOTO 			}
16638c0fec80SRobert Watson 			ifa_free(&ia6->ia_ifa);
166433841545SHajimu UMEMOTO 
166533841545SHajimu UMEMOTO 			/*
166633841545SHajimu UMEMOTO 			 * A newly added address might affect the status
166733841545SHajimu UMEMOTO 			 * of other addresses, so we check and update it.
166833841545SHajimu UMEMOTO 			 * XXX: what if address duplication happens?
166933841545SHajimu UMEMOTO 			 */
167033841545SHajimu UMEMOTO 			pfxlist_onlink_check();
167133841545SHajimu UMEMOTO 		} else {
167233841545SHajimu UMEMOTO 			/* just set an error. do not bark here. */
167333841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL; /* XXX: might be unused. */
167433841545SHajimu UMEMOTO 		}
167533841545SHajimu UMEMOTO 	}
167633841545SHajimu UMEMOTO 
167782cd038dSYoshinobu Inoue end:
1678d748f7efSMark Johnston 	if (pr != NULL)
1679d748f7efSMark Johnston 		nd6_prefix_rele(pr);
1680d748f7efSMark Johnston 	return (error);
168182cd038dSYoshinobu Inoue }
168282cd038dSYoshinobu Inoue 
168382cd038dSYoshinobu Inoue /*
1684686cdd19SJun-ichiro itojun Hagino  * A supplement function used in the on-link detection below;
1685686cdd19SJun-ichiro itojun Hagino  * detect if a given prefix has a (probably) reachable advertising router.
1686686cdd19SJun-ichiro itojun Hagino  * XXX: lengthy function name...
1687686cdd19SJun-ichiro itojun Hagino  */
168833841545SHajimu UMEMOTO static struct nd_pfxrouter *
16891272577eSXin LI find_pfxlist_reachable_router(struct nd_prefix *pr)
1690686cdd19SJun-ichiro itojun Hagino {
1691a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1692686cdd19SJun-ichiro itojun Hagino 	struct nd_pfxrouter *pfxrtr;
16936e6b3f7cSQing Li 	struct llentry *ln;
1694d78be3a9SKip Macy 	int canreach;
1695686cdd19SJun-ichiro itojun Hagino 
1696d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1697d748f7efSMark Johnston 
1698a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1699b8a6e03fSGleb Smirnoff 	LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1700d78be3a9SKip Macy 		ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1701a6146780SKip Macy 		if (ln == NULL)
1702a6146780SKip Macy 			continue;
1703d78be3a9SKip Macy 		canreach = ND6_IS_LLINFO_PROBREACH(ln);
1704d78be3a9SKip Macy 		LLE_RUNLOCK(ln);
1705d78be3a9SKip Macy 		if (canreach)
1706d78be3a9SKip Macy 			break;
17076e6b3f7cSQing Li 	}
1708b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
1709686cdd19SJun-ichiro itojun Hagino 	return (pfxrtr);
1710686cdd19SJun-ichiro itojun Hagino }
1711686cdd19SJun-ichiro itojun Hagino 
1712686cdd19SJun-ichiro itojun Hagino /*
171382cd038dSYoshinobu Inoue  * Check if each prefix in the prefix list has at least one available router
171433841545SHajimu UMEMOTO  * that advertised the prefix (a router is "available" if its neighbor cache
171533841545SHajimu UMEMOTO  * entry is reachable or probably reachable).
1716686cdd19SJun-ichiro itojun Hagino  * If the check fails, the prefix may be off-link, because, for example,
171782cd038dSYoshinobu Inoue  * we have moved from the network but the lifetime of the prefix has not
171833841545SHajimu UMEMOTO  * expired yet.  So we should not use the prefix if there is another prefix
171933841545SHajimu UMEMOTO  * that has an available router.
172007c1f959SMark Johnston  * But, if there is no prefix that has an available router, we still regard
172182cd038dSYoshinobu Inoue  * all the prefixes as on-link.  This is because we can't tell if all the
172282cd038dSYoshinobu Inoue  * routers are simply dead or if we really moved from the network and there
172382cd038dSYoshinobu Inoue  * is no router around us.
172482cd038dSYoshinobu Inoue  */
1725686cdd19SJun-ichiro itojun Hagino void
17261b28988bSMark Johnston pfxlist_onlink_check(void)
172782cd038dSYoshinobu Inoue {
172882cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
172933841545SHajimu UMEMOTO 	struct in6_ifaddr *ifa;
1730743eee66SSUZUKI Shinsuke 	struct nd_defrouter *dr;
1731743eee66SSUZUKI Shinsuke 	struct nd_pfxrouter *pfxrtr = NULL;
17321b28988bSMark Johnston 	struct rm_priotracker in6_ifa_tracker;
1733d748f7efSMark Johnston 	uint64_t genid;
1734d748f7efSMark Johnston 	uint32_t flags;
1735d748f7efSMark Johnston 
1736d748f7efSMark Johnston 	ND6_ONLINK_LOCK();
1737d748f7efSMark Johnston 	ND6_RLOCK();
173882cd038dSYoshinobu Inoue 
1739686cdd19SJun-ichiro itojun Hagino 	/*
1740686cdd19SJun-ichiro itojun Hagino 	 * Check if there is a prefix that has a reachable advertising
1741686cdd19SJun-ichiro itojun Hagino 	 * router.
1742686cdd19SJun-ichiro itojun Hagino 	 */
17433b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
174433841545SHajimu UMEMOTO 		if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
174582cd038dSYoshinobu Inoue 			break;
1746686cdd19SJun-ichiro itojun Hagino 	}
174782cd038dSYoshinobu Inoue 
174882cd038dSYoshinobu Inoue 	/*
1749743eee66SSUZUKI Shinsuke 	 * If we have no such prefix, check whether we still have a router
1750743eee66SSUZUKI Shinsuke 	 * that does not advertise any prefixes.
1751743eee66SSUZUKI Shinsuke 	 */
1752743eee66SSUZUKI Shinsuke 	if (pr == NULL) {
17533b0b2840SJohn Baldwin 		TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1754743eee66SSUZUKI Shinsuke 			struct nd_prefix *pr0;
1755743eee66SSUZUKI Shinsuke 
17563b0b2840SJohn Baldwin 			LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1757743eee66SSUZUKI Shinsuke 				if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1758743eee66SSUZUKI Shinsuke 					break;
1759743eee66SSUZUKI Shinsuke 			}
1760743eee66SSUZUKI Shinsuke 			if (pfxrtr != NULL)
1761743eee66SSUZUKI Shinsuke 				break;
1762743eee66SSUZUKI Shinsuke 		}
1763743eee66SSUZUKI Shinsuke 	}
17643b0b2840SJohn Baldwin 	if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) {
1765743eee66SSUZUKI Shinsuke 		/*
1766743eee66SSUZUKI Shinsuke 		 * There is at least one prefix that has a reachable router,
1767743eee66SSUZUKI Shinsuke 		 * or at least a router which probably does not advertise
1768743eee66SSUZUKI Shinsuke 		 * any prefixes.  The latter would be the case when we move
1769743eee66SSUZUKI Shinsuke 		 * to a new link where we have a router that does not provide
1770743eee66SSUZUKI Shinsuke 		 * prefixes and we configure an address by hand.
177133841545SHajimu UMEMOTO 		 * Detach prefixes which have no reachable advertising
177233841545SHajimu UMEMOTO 		 * router, and attach other prefixes.
177382cd038dSYoshinobu Inoue 		 */
17743b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
177533841545SHajimu UMEMOTO 			/* XXX: a link-local prefix should never be detached */
1776a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1777a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1778a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
177942cb3aa4SQing Li 				continue;
178042cb3aa4SQing Li 
178133841545SHajimu UMEMOTO 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
178233841545SHajimu UMEMOTO 			    find_pfxlist_reachable_router(pr) == NULL)
178333841545SHajimu UMEMOTO 				pr->ndpr_stateflags |= NDPRF_DETACHED;
1784a88d6d7eSMark Johnston 			else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1785155d72c4SPedro F. Giffuni 			    find_pfxlist_reachable_router(pr) != NULL)
178633841545SHajimu UMEMOTO 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
178782cd038dSYoshinobu Inoue 		}
178833841545SHajimu UMEMOTO 	} else {
178933841545SHajimu UMEMOTO 		/* there is no prefix that has a reachable router */
17903b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1791a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1792a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1793a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
179433841545SHajimu UMEMOTO 				continue;
179533841545SHajimu UMEMOTO 			pr->ndpr_stateflags &= ~NDPRF_DETACHED;
179633841545SHajimu UMEMOTO 		}
179733841545SHajimu UMEMOTO 	}
179833841545SHajimu UMEMOTO 
179933841545SHajimu UMEMOTO 	/*
180033841545SHajimu UMEMOTO 	 * Remove each interface route associated with a (just) detached
180133841545SHajimu UMEMOTO 	 * prefix, and reinstall the interface route for a (just) attached
180233841545SHajimu UMEMOTO 	 * prefix.  Note that all attempt of reinstallation does not
180333841545SHajimu UMEMOTO 	 * necessarily success, when a same prefix is shared among multiple
180433841545SHajimu UMEMOTO 	 * interfaces.  Such cases will be handled in nd6_prefix_onlink,
180533841545SHajimu UMEMOTO 	 * so we don't have to care about them.
180633841545SHajimu UMEMOTO 	 */
1807d748f7efSMark Johnston restart:
18083b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
18091d54aa3bSBjoern A. Zeeb 		char ip6buf[INET6_ADDRSTRLEN];
1810a88d6d7eSMark Johnston 		int e;
181133841545SHajimu UMEMOTO 
1812a88d6d7eSMark Johnston 		if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1813a88d6d7eSMark Johnston 		    pr->ndpr_raf_onlink == 0 ||
1814a88d6d7eSMark Johnston 		    pr->ndpr_raf_auto == 0)
181542cb3aa4SQing Li 			continue;
181642cb3aa4SQing Li 
1817d748f7efSMark Johnston 		flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1818d748f7efSMark Johnston 		if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1819d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1820d748f7efSMark Johnston 			ND6_RUNLOCK();
1821d748f7efSMark Johnston 			if ((flags & NDPRF_ONLINK) != 0 &&
1822d748f7efSMark Johnston 			    (e = nd6_prefix_offlink(pr)) != 0) {
182333841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
182433841545SHajimu UMEMOTO 				    "pfxlist_onlink_check: failed to "
18257aa59493SSUZUKI Shinsuke 				    "make %s/%d offlink, errno=%d\n",
18261d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
18271d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
182833841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
1829d748f7efSMark Johnston 			} else if ((flags & NDPRF_ONLINK) == 0 &&
1830d748f7efSMark Johnston 			    (e = nd6_prefix_onlink(pr)) != 0) {
183133841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
183233841545SHajimu UMEMOTO 				    "pfxlist_onlink_check: failed to "
18337aa59493SSUZUKI Shinsuke 				    "make %s/%d onlink, errno=%d\n",
18341d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
18351d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
183633841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
183733841545SHajimu UMEMOTO 			}
1838d748f7efSMark Johnston 			ND6_RLOCK();
1839d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1840d748f7efSMark Johnston 				goto restart;
184133841545SHajimu UMEMOTO 		}
184233841545SHajimu UMEMOTO 	}
184333841545SHajimu UMEMOTO 
184433841545SHajimu UMEMOTO 	/*
184533841545SHajimu UMEMOTO 	 * Changes on the prefix status might affect address status as well.
184633841545SHajimu UMEMOTO 	 * Make sure that all addresses derived from an attached prefix are
184733841545SHajimu UMEMOTO 	 * attached, and that all addresses derived from a detached prefix are
184833841545SHajimu UMEMOTO 	 * detached.  Note, however, that a manually configured address should
184933841545SHajimu UMEMOTO 	 * always be attached.
185033841545SHajimu UMEMOTO 	 * The precise detection logic is same as the one for prefixes.
185133841545SHajimu UMEMOTO 	 */
18521b28988bSMark Johnston 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1853d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
185407eb2995SHajimu UMEMOTO 		if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
185533841545SHajimu UMEMOTO 			continue;
185633841545SHajimu UMEMOTO 
185733841545SHajimu UMEMOTO 		if (ifa->ia6_ndpr == NULL) {
185833841545SHajimu UMEMOTO 			/*
185933841545SHajimu UMEMOTO 			 * This can happen when we first configure the address
186033841545SHajimu UMEMOTO 			 * (i.e. the address exists, but the prefix does not).
186133841545SHajimu UMEMOTO 			 * XXX: complicated relationships...
186233841545SHajimu UMEMOTO 			 */
186333841545SHajimu UMEMOTO 			continue;
186433841545SHajimu UMEMOTO 		}
186533841545SHajimu UMEMOTO 
186633841545SHajimu UMEMOTO 		if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
186733841545SHajimu UMEMOTO 			break;
186833841545SHajimu UMEMOTO 	}
186933841545SHajimu UMEMOTO 	if (ifa) {
1870d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
187133841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
187233841545SHajimu UMEMOTO 				continue;
187333841545SHajimu UMEMOTO 
187433841545SHajimu UMEMOTO 			if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
187533841545SHajimu UMEMOTO 				continue;
187633841545SHajimu UMEMOTO 
1877743eee66SSUZUKI Shinsuke 			if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1878743eee66SSUZUKI Shinsuke 				if (ifa->ia6_flags & IN6_IFF_DETACHED) {
187933841545SHajimu UMEMOTO 					ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1880743eee66SSUZUKI Shinsuke 					ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1881743eee66SSUZUKI Shinsuke 					nd6_dad_start((struct ifaddr *)ifa, 0);
1882743eee66SSUZUKI Shinsuke 				}
1883743eee66SSUZUKI Shinsuke 			} else {
188433841545SHajimu UMEMOTO 				ifa->ia6_flags |= IN6_IFF_DETACHED;
188582cd038dSYoshinobu Inoue 			}
1886686cdd19SJun-ichiro itojun Hagino 		}
18871b28988bSMark Johnston 	} else {
1888d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
188933841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
189033841545SHajimu UMEMOTO 				continue;
189133841545SHajimu UMEMOTO 
1892743eee66SSUZUKI Shinsuke 			if (ifa->ia6_flags & IN6_IFF_DETACHED) {
189333841545SHajimu UMEMOTO 				ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1894743eee66SSUZUKI Shinsuke 				ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1895743eee66SSUZUKI Shinsuke 				/* Do we need a delay in this case? */
1896743eee66SSUZUKI Shinsuke 				nd6_dad_start((struct ifaddr *)ifa, 0);
1897743eee66SSUZUKI Shinsuke 			}
189833841545SHajimu UMEMOTO 		}
189982cd038dSYoshinobu Inoue 	}
19001b28988bSMark Johnston 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1901d748f7efSMark Johnston 	ND6_RUNLOCK();
1902d748f7efSMark Johnston 	ND6_ONLINK_UNLOCK();
190382cd038dSYoshinobu Inoue }
190482cd038dSYoshinobu Inoue 
19059d9b92f2SBjoern A. Zeeb static int
190681d5d46bSBjoern A. Zeeb nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
190781d5d46bSBjoern A. Zeeb {
1908563ab4e4SAlexander V. Chernikov 	struct sockaddr_dl sdl;
190981d5d46bSBjoern A. Zeeb 	struct rtentry *rt;
191081d5d46bSBjoern A. Zeeb 	struct sockaddr_in6 mask6;
191181d5d46bSBjoern A. Zeeb 	u_long rtflags;
1912559b4296SAlan Somers 	int error, a_failure, fibnum, maxfib;
191381d5d46bSBjoern A. Zeeb 
191481d5d46bSBjoern A. Zeeb 	/*
191581d5d46bSBjoern A. Zeeb 	 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
191681d5d46bSBjoern A. Zeeb 	 * ifa->ifa_rtrequest = nd6_rtrequest;
191781d5d46bSBjoern A. Zeeb 	 */
191881d5d46bSBjoern A. Zeeb 	bzero(&mask6, sizeof(mask6));
191981d5d46bSBjoern A. Zeeb 	mask6.sin6_len = sizeof(mask6);
192081d5d46bSBjoern A. Zeeb 	mask6.sin6_addr = pr->ndpr_mask;
192181d5d46bSBjoern A. Zeeb 	rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
192281d5d46bSBjoern A. Zeeb 
1923563ab4e4SAlexander V. Chernikov 	bzero(&sdl, sizeof(struct sockaddr_dl));
1924563ab4e4SAlexander V. Chernikov 	sdl.sdl_len = sizeof(struct sockaddr_dl);
1925563ab4e4SAlexander V. Chernikov 	sdl.sdl_family = AF_LINK;
1926563ab4e4SAlexander V. Chernikov 	sdl.sdl_type = ifa->ifa_ifp->if_type;
1927563ab4e4SAlexander V. Chernikov 	sdl.sdl_index = ifa->ifa_ifp->if_index;
1928563ab4e4SAlexander V. Chernikov 
1929559b4296SAlan Somers 	if(V_rt_add_addr_allfibs) {
1930559b4296SAlan Somers 		fibnum = 0;
1931559b4296SAlan Somers 		maxfib = rt_numfibs;
1932559b4296SAlan Somers 	} else {
1933559b4296SAlan Somers 		fibnum = ifa->ifa_ifp->if_fib;
1934559b4296SAlan Somers 		maxfib = fibnum + 1;
1935559b4296SAlan Somers 	}
193681d5d46bSBjoern A. Zeeb 	a_failure = 0;
1937559b4296SAlan Somers 	for (; fibnum < maxfib; fibnum++) {
193881d5d46bSBjoern A. Zeeb 
193981d5d46bSBjoern A. Zeeb 		rt = NULL;
194081d5d46bSBjoern A. Zeeb 		error = in6_rtrequest(RTM_ADD,
1941563ab4e4SAlexander V. Chernikov 		    (struct sockaddr *)&pr->ndpr_prefix, (struct sockaddr *)&sdl,
194281d5d46bSBjoern A. Zeeb 		    (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
194381d5d46bSBjoern A. Zeeb 		if (error == 0) {
194481d5d46bSBjoern A. Zeeb 			KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
194581d5d46bSBjoern A. Zeeb 			    "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
194681d5d46bSBjoern A. Zeeb 			    error, pr, ifa));
194781d5d46bSBjoern A. Zeeb 			RT_LOCK(rt);
194881d5d46bSBjoern A. Zeeb 			nd6_rtmsg(RTM_ADD, rt);
194981d5d46bSBjoern A. Zeeb 			RT_UNLOCK(rt);
195081d5d46bSBjoern A. Zeeb 			pr->ndpr_stateflags |= NDPRF_ONLINK;
195181d5d46bSBjoern A. Zeeb 		} else {
195281d5d46bSBjoern A. Zeeb 			char ip6buf[INET6_ADDRSTRLEN];
195381d5d46bSBjoern A. Zeeb 			char ip6bufg[INET6_ADDRSTRLEN];
195481d5d46bSBjoern A. Zeeb 			char ip6bufm[INET6_ADDRSTRLEN];
195581d5d46bSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
195681d5d46bSBjoern A. Zeeb 
195781d5d46bSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
195881d5d46bSBjoern A. Zeeb 			nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
195981d5d46bSBjoern A. Zeeb 			    "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
196081d5d46bSBjoern A. Zeeb 			    "flags=%lx errno = %d\n",
196181d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
196281d5d46bSBjoern A. Zeeb 			    pr->ndpr_plen, if_name(pr->ndpr_ifp),
196381d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufg, &sin6->sin6_addr),
196481d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufm, &mask6.sin6_addr),
196581d5d46bSBjoern A. Zeeb 			    rtflags, error));
196681d5d46bSBjoern A. Zeeb 
196781d5d46bSBjoern A. Zeeb 			/* Save last error to return, see rtinit(). */
196881d5d46bSBjoern A. Zeeb 			a_failure = error;
196981d5d46bSBjoern A. Zeeb 		}
197081d5d46bSBjoern A. Zeeb 
197181d5d46bSBjoern A. Zeeb 		if (rt != NULL) {
197281d5d46bSBjoern A. Zeeb 			RT_LOCK(rt);
197381d5d46bSBjoern A. Zeeb 			RT_REMREF(rt);
197481d5d46bSBjoern A. Zeeb 			RT_UNLOCK(rt);
197581d5d46bSBjoern A. Zeeb 		}
197681d5d46bSBjoern A. Zeeb 	}
197781d5d46bSBjoern A. Zeeb 
197881d5d46bSBjoern A. Zeeb 	/* Return the last error we got. */
197981d5d46bSBjoern A. Zeeb 	return (a_failure);
198081d5d46bSBjoern A. Zeeb }
198181d5d46bSBjoern A. Zeeb 
1982d748f7efSMark Johnston int
19831272577eSXin LI nd6_prefix_onlink(struct nd_prefix *pr)
198482cd038dSYoshinobu Inoue {
1985751d8d15SGleb Smirnoff 	struct epoch_tracker et;
198633841545SHajimu UMEMOTO 	struct ifaddr *ifa;
198733841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
198833841545SHajimu UMEMOTO 	struct nd_prefix *opr;
19891d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1990d748f7efSMark Johnston 	int error;
199133841545SHajimu UMEMOTO 
1992d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
1993d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
1994d748f7efSMark Johnston 
1995d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
19962ce62dceSSUZUKI Shinsuke 		return (EEXIST);
199782cd038dSYoshinobu Inoue 
199882cd038dSYoshinobu Inoue 	/*
199933841545SHajimu UMEMOTO 	 * Add the interface route associated with the prefix.  Before
200033841545SHajimu UMEMOTO 	 * installing the route, check if there's the same prefix on another
200133841545SHajimu UMEMOTO 	 * interface, and the prefix has already installed the interface route.
200233841545SHajimu UMEMOTO 	 * Although such a configuration is expected to be rare, we explicitly
200333841545SHajimu UMEMOTO 	 * allow it.
200482cd038dSYoshinobu Inoue 	 */
2005d748f7efSMark Johnston 	ND6_RLOCK();
20063b0b2840SJohn Baldwin 	LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
200733841545SHajimu UMEMOTO 		if (opr == pr)
200833841545SHajimu UMEMOTO 			continue;
200933841545SHajimu UMEMOTO 
201033841545SHajimu UMEMOTO 		if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
201133841545SHajimu UMEMOTO 			continue;
201233841545SHajimu UMEMOTO 
2013559b4296SAlan Somers 		if (!V_rt_add_addr_allfibs &&
2014559b4296SAlan Somers 		    opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2015559b4296SAlan Somers 			continue;
2016559b4296SAlan Somers 
201733841545SHajimu UMEMOTO 		if (opr->ndpr_plen == pr->ndpr_plen &&
201833841545SHajimu UMEMOTO 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2019d748f7efSMark Johnston 		    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2020d748f7efSMark Johnston 			ND6_RUNLOCK();
202133841545SHajimu UMEMOTO 			return (0);
202233841545SHajimu UMEMOTO 		}
2023d748f7efSMark Johnston 	}
2024d748f7efSMark Johnston 	ND6_RUNLOCK();
202533841545SHajimu UMEMOTO 
202633841545SHajimu UMEMOTO 	/*
202733841545SHajimu UMEMOTO 	 * We prefer link-local addresses as the associated interface address.
202833841545SHajimu UMEMOTO 	 */
202933841545SHajimu UMEMOTO 	/* search for a link-local addr */
2030751d8d15SGleb Smirnoff 	NET_EPOCH_ENTER(et);
203133841545SHajimu UMEMOTO 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
203207eb2995SHajimu UMEMOTO 	    IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
203333841545SHajimu UMEMOTO 	if (ifa == NULL) {
203433841545SHajimu UMEMOTO 		/* XXX: freebsd does not have ifa_ifwithaf */
2035d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2036c2615844SMark Johnston 			if (ifa->ifa_addr->sa_family == AF_INET6) {
2037c2615844SMark Johnston 				ifa_ref(ifa);
203833841545SHajimu UMEMOTO 				break;
203933841545SHajimu UMEMOTO 			}
2040c2615844SMark Johnston 		}
204133841545SHajimu UMEMOTO 		/* should we care about ia6_flags? */
204233841545SHajimu UMEMOTO 	}
2043751d8d15SGleb Smirnoff 	NET_EPOCH_EXIT(et);
204433841545SHajimu UMEMOTO 	if (ifa == NULL) {
204533841545SHajimu UMEMOTO 		/*
204633841545SHajimu UMEMOTO 		 * This can still happen, when, for example, we receive an RA
204733841545SHajimu UMEMOTO 		 * containing a prefix with the L bit set and the A bit clear,
204833841545SHajimu UMEMOTO 		 * after removing all IPv6 addresses on the receiving
204933841545SHajimu UMEMOTO 		 * interface.  This should, of course, be rare though.
205033841545SHajimu UMEMOTO 		 */
205133841545SHajimu UMEMOTO 		nd6log((LOG_NOTICE,
205233841545SHajimu UMEMOTO 		    "nd6_prefix_onlink: failed to find any ifaddr"
205333841545SHajimu UMEMOTO 		    " to add route for a prefix(%s/%d) on %s\n",
20541d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
205533841545SHajimu UMEMOTO 		    pr->ndpr_plen, if_name(ifp)));
205633841545SHajimu UMEMOTO 		return (0);
205733841545SHajimu UMEMOTO 	}
205833841545SHajimu UMEMOTO 
205981d5d46bSBjoern A. Zeeb 	error = nd6_prefix_onlink_rtrequest(pr, ifa);
206033841545SHajimu UMEMOTO 
20618c0fec80SRobert Watson 	if (ifa != NULL)
20628c0fec80SRobert Watson 		ifa_free(ifa);
206333841545SHajimu UMEMOTO 
206433841545SHajimu UMEMOTO 	return (error);
206533841545SHajimu UMEMOTO }
206633841545SHajimu UMEMOTO 
2067d748f7efSMark Johnston int
20681272577eSXin LI nd6_prefix_offlink(struct nd_prefix *pr)
206933841545SHajimu UMEMOTO {
207033841545SHajimu UMEMOTO 	int error = 0;
207133841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
207233841545SHajimu UMEMOTO 	struct nd_prefix *opr;
207333841545SHajimu UMEMOTO 	struct sockaddr_in6 sa6, mask6;
207481d5d46bSBjoern A. Zeeb 	struct rtentry *rt;
20751d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2076d748f7efSMark Johnston 	uint64_t genid;
2077559b4296SAlan Somers 	int fibnum, maxfib, a_failure;
207833841545SHajimu UMEMOTO 
2079d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2080d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2081d748f7efSMark Johnston 
2082d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
208333841545SHajimu UMEMOTO 		return (EEXIST);
208433841545SHajimu UMEMOTO 
208582cd038dSYoshinobu Inoue 	bzero(&sa6, sizeof(sa6));
208682cd038dSYoshinobu Inoue 	sa6.sin6_family = AF_INET6;
208782cd038dSYoshinobu Inoue 	sa6.sin6_len = sizeof(sa6);
208882cd038dSYoshinobu Inoue 	bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
208982cd038dSYoshinobu Inoue 	    sizeof(struct in6_addr));
209082cd038dSYoshinobu Inoue 	bzero(&mask6, sizeof(mask6));
209182cd038dSYoshinobu Inoue 	mask6.sin6_family = AF_INET6;
209282cd038dSYoshinobu Inoue 	mask6.sin6_len = sizeof(sa6);
209382cd038dSYoshinobu Inoue 	bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
209433841545SHajimu UMEMOTO 
2095559b4296SAlan Somers 	if (V_rt_add_addr_allfibs) {
2096559b4296SAlan Somers 		fibnum = 0;
2097559b4296SAlan Somers 		maxfib = rt_numfibs;
2098559b4296SAlan Somers 	} else {
2099559b4296SAlan Somers 		fibnum = ifp->if_fib;
2100559b4296SAlan Somers 		maxfib = fibnum + 1;
2101559b4296SAlan Somers 	}
2102559b4296SAlan Somers 
210381d5d46bSBjoern A. Zeeb 	a_failure = 0;
2104559b4296SAlan Somers 	for (; fibnum < maxfib; fibnum++) {
210581d5d46bSBjoern A. Zeeb 		rt = NULL;
210681d5d46bSBjoern A. Zeeb 		error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
210781d5d46bSBjoern A. Zeeb 		    (struct sockaddr *)&mask6, 0, &rt, fibnum);
210881d5d46bSBjoern A. Zeeb 		if (error == 0) {
210933841545SHajimu UMEMOTO 			/* report the route deletion to the routing socket. */
211033841545SHajimu UMEMOTO 			if (rt != NULL)
211133841545SHajimu UMEMOTO 				nd6_rtmsg(RTM_DELETE, rt);
211281d5d46bSBjoern A. Zeeb 		} else {
211381d5d46bSBjoern A. Zeeb 			/* Save last error to return, see rtinit(). */
211481d5d46bSBjoern A. Zeeb 			a_failure = error;
211581d5d46bSBjoern A. Zeeb 		}
211681d5d46bSBjoern A. Zeeb 		if (rt != NULL) {
211781d5d46bSBjoern A. Zeeb 			RTFREE(rt);
211881d5d46bSBjoern A. Zeeb 		}
211981d5d46bSBjoern A. Zeeb 	}
212081d5d46bSBjoern A. Zeeb 	error = a_failure;
2121378aa8d8SQing Li 	a_failure = 1;
212281d5d46bSBjoern A. Zeeb 	if (error == 0) {
212381d5d46bSBjoern A. Zeeb 		pr->ndpr_stateflags &= ~NDPRF_ONLINK;
212433841545SHajimu UMEMOTO 
212533841545SHajimu UMEMOTO 		/*
212633841545SHajimu UMEMOTO 		 * There might be the same prefix on another interface,
212733841545SHajimu UMEMOTO 		 * the prefix which could not be on-link just because we have
212833841545SHajimu UMEMOTO 		 * the interface route (see comments in nd6_prefix_onlink).
212933841545SHajimu UMEMOTO 		 * If there's one, try to make the prefix on-link on the
213033841545SHajimu UMEMOTO 		 * interface.
213133841545SHajimu UMEMOTO 		 */
2132d748f7efSMark Johnston 		ND6_RLOCK();
2133d748f7efSMark Johnston restart:
21343b0b2840SJohn Baldwin 		LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
213533841545SHajimu UMEMOTO 			/*
213633841545SHajimu UMEMOTO 			 * KAME specific: detached prefixes should not be
213733841545SHajimu UMEMOTO 			 * on-link.
213833841545SHajimu UMEMOTO 			 */
21397b0e84b7SMark Johnston 			if (opr == pr || (opr->ndpr_stateflags &
21407b0e84b7SMark Johnston 			    (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
214133841545SHajimu UMEMOTO 				continue;
214233841545SHajimu UMEMOTO 
214333841545SHajimu UMEMOTO 			if (opr->ndpr_plen == pr->ndpr_plen &&
214433841545SHajimu UMEMOTO 			    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
214507eb2995SHajimu UMEMOTO 			    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
214682cd038dSYoshinobu Inoue 				int e;
214782cd038dSYoshinobu Inoue 
2148d748f7efSMark Johnston 				genid = V_nd6_list_genid;
2149d748f7efSMark Johnston 				ND6_RUNLOCK();
215033841545SHajimu UMEMOTO 				if ((e = nd6_prefix_onlink(opr)) != 0) {
215133841545SHajimu UMEMOTO 					nd6log((LOG_ERR,
215233841545SHajimu UMEMOTO 					    "nd6_prefix_offlink: failed to "
215333841545SHajimu UMEMOTO 					    "recover a prefix %s/%d from %s "
215433841545SHajimu UMEMOTO 					    "to %s (errno = %d)\n",
21551d54aa3bSBjoern A. Zeeb 					    ip6_sprintf(ip6buf,
21561d54aa3bSBjoern A. Zeeb 						&opr->ndpr_prefix.sin6_addr),
215733841545SHajimu UMEMOTO 					    opr->ndpr_plen, if_name(ifp),
215833841545SHajimu UMEMOTO 					    if_name(opr->ndpr_ifp), e));
2159378aa8d8SQing Li 				} else
2160378aa8d8SQing Li 					a_failure = 0;
2161d748f7efSMark Johnston 				ND6_RLOCK();
2162d748f7efSMark Johnston 				if (genid != V_nd6_list_genid)
2163d748f7efSMark Johnston 					goto restart;
216482cd038dSYoshinobu Inoue 			}
216582cd038dSYoshinobu Inoue 		}
2166d748f7efSMark Johnston 		ND6_RUNLOCK();
216707eb2995SHajimu UMEMOTO 	} else {
216833841545SHajimu UMEMOTO 		/* XXX: can we still set the NDPRF_ONLINK flag? */
216933841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
217033841545SHajimu UMEMOTO 		    "nd6_prefix_offlink: failed to delete route: "
217133841545SHajimu UMEMOTO 		    "%s/%d on %s (errno = %d)\n",
21721d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
21731d54aa3bSBjoern A. Zeeb 		    if_name(ifp), error));
217433841545SHajimu UMEMOTO 	}
217582cd038dSYoshinobu Inoue 
2176378aa8d8SQing Li 	if (a_failure)
2177378aa8d8SQing Li 		lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
2178378aa8d8SQing Li 		    (struct sockaddr *)&mask6, LLE_STATIC);
2179378aa8d8SQing Li 
218033841545SHajimu UMEMOTO 	return (error);
218182cd038dSYoshinobu Inoue }
218282cd038dSYoshinobu Inoue 
218382cd038dSYoshinobu Inoue static struct in6_ifaddr *
21841272577eSXin LI in6_ifadd(struct nd_prefixctl *pr, int mcast)
218582cd038dSYoshinobu Inoue {
218633841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
218782cd038dSYoshinobu Inoue 	struct ifaddr *ifa;
218833841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
218933841545SHajimu UMEMOTO 	struct in6_ifaddr *ia, *ib;
219033841545SHajimu UMEMOTO 	int error, plen0;
219182cd038dSYoshinobu Inoue 	struct in6_addr mask;
219233841545SHajimu UMEMOTO 	int prefixlen = pr->ndpr_plen;
2193743eee66SSUZUKI Shinsuke 	int updateflags;
21941d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
219582cd038dSYoshinobu Inoue 
2196ae360dddSHajimu UMEMOTO 	in6_prefixlen2mask(&mask, prefixlen);
219782cd038dSYoshinobu Inoue 
219833841545SHajimu UMEMOTO 	/*
219933841545SHajimu UMEMOTO 	 * find a link-local address (will be interface ID).
220033841545SHajimu UMEMOTO 	 * Is it really mandatory? Theoretically, a global or a site-local
220133841545SHajimu UMEMOTO 	 * address can be configured without a link-local address, if we
220233841545SHajimu UMEMOTO 	 * have a unique interface identifier...
220333841545SHajimu UMEMOTO 	 *
220433841545SHajimu UMEMOTO 	 * it is not mandatory to have a link-local address, we can generate
220533841545SHajimu UMEMOTO 	 * interface identifier on the fly.  we do this because:
220633841545SHajimu UMEMOTO 	 * (1) it should be the easiest way to find interface identifier.
220733841545SHajimu UMEMOTO 	 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
220833841545SHajimu UMEMOTO 	 * for multiple addresses on a single interface, and possible shortcut
220933841545SHajimu UMEMOTO 	 * of DAD.  we omitted DAD for this reason in the past.
221033841545SHajimu UMEMOTO 	 * (3) a user can prevent autoconfiguration of global address
221133841545SHajimu UMEMOTO 	 * by removing link-local address by hand (this is partly because we
22129d5abbddSJens Schweikhardt 	 * don't have other way to control the use of IPv6 on an interface.
221333841545SHajimu UMEMOTO 	 * this has been our design choice - cf. NRL's "ifconfig auto").
221433841545SHajimu UMEMOTO 	 * (4) it is easier to manage when an interface has addresses
221533841545SHajimu UMEMOTO 	 * with the same interface identifier, than to have multiple addresses
221633841545SHajimu UMEMOTO 	 * with different interface identifiers.
221733841545SHajimu UMEMOTO 	 */
2218686cdd19SJun-ichiro itojun Hagino 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
221982cd038dSYoshinobu Inoue 	if (ifa)
222082cd038dSYoshinobu Inoue 		ib = (struct in6_ifaddr *)ifa;
222182cd038dSYoshinobu Inoue 	else
222282cd038dSYoshinobu Inoue 		return NULL;
222382cd038dSYoshinobu Inoue 
222482cd038dSYoshinobu Inoue 	/* prefixlen + ifidlen must be equal to 128 */
222533841545SHajimu UMEMOTO 	plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
222633841545SHajimu UMEMOTO 	if (prefixlen != plen0) {
22278c0fec80SRobert Watson 		ifa_free(ifa);
222833841545SHajimu UMEMOTO 		nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
222933841545SHajimu UMEMOTO 		    "(prefix=%d ifid=%d)\n",
223033841545SHajimu UMEMOTO 		    if_name(ifp), prefixlen, 128 - plen0));
223182cd038dSYoshinobu Inoue 		return NULL;
223282cd038dSYoshinobu Inoue 	}
223382cd038dSYoshinobu Inoue 
223482cd038dSYoshinobu Inoue 	/* make ifaddr */
22359080e7d0SAlexander V. Chernikov 	in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
223682cd038dSYoshinobu Inoue 
22371dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
223882cd038dSYoshinobu Inoue 	/* interface ID */
223907eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
2240743eee66SSUZUKI Shinsuke 	    (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
224107eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
2242743eee66SSUZUKI Shinsuke 	    (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
224307eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
2244743eee66SSUZUKI Shinsuke 	    (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
224507eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
2246743eee66SSUZUKI Shinsuke 	    (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
22478c0fec80SRobert Watson 	ifa_free(ifa);
224882cd038dSYoshinobu Inoue 
2249743eee66SSUZUKI Shinsuke 	/* lifetimes. */
225033841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
225133841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
225233841545SHajimu UMEMOTO 
225333841545SHajimu UMEMOTO 	/* XXX: scope zone ID? */
225433841545SHajimu UMEMOTO 
225533841545SHajimu UMEMOTO 	ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
225633841545SHajimu UMEMOTO 
225733841545SHajimu UMEMOTO 	/*
2258743eee66SSUZUKI Shinsuke 	 * Make sure that we do not have this address already.  This should
2259743eee66SSUZUKI Shinsuke 	 * usually not happen, but we can still see this case, e.g., if we
2260743eee66SSUZUKI Shinsuke 	 * have manually configured the exact address to be configured.
226133841545SHajimu UMEMOTO 	 */
22628c0fec80SRobert Watson 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
22638c0fec80SRobert Watson 	    &ifra.ifra_addr.sin6_addr);
22648c0fec80SRobert Watson 	if (ifa != NULL) {
22658c0fec80SRobert Watson 		ifa_free(ifa);
2266743eee66SSUZUKI Shinsuke 		/* this should be rare enough to make an explicit log */
2267743eee66SSUZUKI Shinsuke 		log(LOG_INFO, "in6_ifadd: %s is already configured\n",
22681d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
2269743eee66SSUZUKI Shinsuke 		return (NULL);
2270743eee66SSUZUKI Shinsuke 	}
227133841545SHajimu UMEMOTO 
2272743eee66SSUZUKI Shinsuke 	/*
2273743eee66SSUZUKI Shinsuke 	 * Allocate ifaddr structure, link into chain, etc.
2274743eee66SSUZUKI Shinsuke 	 * If we are going to create a new address upon receiving a multicasted
2275743eee66SSUZUKI Shinsuke 	 * RA, we need to impose a random delay before starting DAD.
2276743eee66SSUZUKI Shinsuke 	 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
2277743eee66SSUZUKI Shinsuke 	 */
2278743eee66SSUZUKI Shinsuke 	updateflags = 0;
2279743eee66SSUZUKI Shinsuke 	if (mcast)
2280743eee66SSUZUKI Shinsuke 		updateflags |= IN6_IFAUPDATE_DADDELAY;
2281743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
228233841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
228333841545SHajimu UMEMOTO 		    "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
22841d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
22851d54aa3bSBjoern A. Zeeb 		    if_name(ifp), error));
228633841545SHajimu UMEMOTO 		return (NULL);	/* ifaddr must not have been allocated. */
228782cd038dSYoshinobu Inoue 	}
228882cd038dSYoshinobu Inoue 
228933841545SHajimu UMEMOTO 	ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
22908c0fec80SRobert Watson 	/*
22918c0fec80SRobert Watson 	 * XXXRW: Assumption of non-NULLness here might not be true with
22928c0fec80SRobert Watson 	 * fine-grained locking -- should we validate it?  Or just return
22938c0fec80SRobert Watson 	 * earlier ifa rather than looking it up again?
22948c0fec80SRobert Watson 	 */
22958c0fec80SRobert Watson 	return (ia);		/* this is always non-NULL  and referenced. */
229682cd038dSYoshinobu Inoue }
229782cd038dSYoshinobu Inoue 
22981272577eSXin LI /*
22991272577eSXin LI  * ia0 - corresponding public address
23001272577eSXin LI  */
230182cd038dSYoshinobu Inoue int
23021272577eSXin LI in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
230382cd038dSYoshinobu Inoue {
230433841545SHajimu UMEMOTO 	struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
23050c5d4bdeSAlexander V. Chernikov 	struct in6_ifaddr *newia;
230633841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
23071dc8f6a8SAlexander V. Chernikov 	int error;
230833841545SHajimu UMEMOTO 	int trylimit = 3;	/* XXX: adhoc value */
2309743eee66SSUZUKI Shinsuke 	int updateflags;
231033841545SHajimu UMEMOTO 	u_int32_t randid[2];
231133841545SHajimu UMEMOTO 	time_t vltime0, pltime0;
231282cd038dSYoshinobu Inoue 
23139080e7d0SAlexander V. Chernikov 	in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
23149080e7d0SAlexander V. Chernikov 	    &ia0->ia_prefixmask.sin6_addr);
23159080e7d0SAlexander V. Chernikov 
23169080e7d0SAlexander V. Chernikov 	ifra.ifra_addr = ia0->ia_addr;	/* XXX: do we need this ? */
231733841545SHajimu UMEMOTO 	/* clear the old IFID */
23181dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
23191dc8f6a8SAlexander V. Chernikov 	    &ifra.ifra_prefixmask.sin6_addr);
232082cd038dSYoshinobu Inoue 
232133841545SHajimu UMEMOTO   again:
2322743eee66SSUZUKI Shinsuke 	if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2323743eee66SSUZUKI Shinsuke 	    (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2324743eee66SSUZUKI Shinsuke 		nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
2325743eee66SSUZUKI Shinsuke 		    "random IFID\n"));
2326743eee66SSUZUKI Shinsuke 		return (EINVAL);
2327743eee66SSUZUKI Shinsuke 	}
232807eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
232907eb2995SHajimu UMEMOTO 	    (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
233007eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
233107eb2995SHajimu UMEMOTO 	    (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
233282cd038dSYoshinobu Inoue 
233382cd038dSYoshinobu Inoue 	/*
2334743eee66SSUZUKI Shinsuke 	 * in6_get_tmpifid() quite likely provided a unique interface ID.
2335743eee66SSUZUKI Shinsuke 	 * However, we may still have a chance to see collision, because
2336743eee66SSUZUKI Shinsuke 	 * there may be a time lag between generation of the ID and generation
2337743eee66SSUZUKI Shinsuke 	 * of the address.  So, we'll do one more sanity check.
233882cd038dSYoshinobu Inoue 	 */
23390c5d4bdeSAlexander V. Chernikov 
23400c5d4bdeSAlexander V. Chernikov 	if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
23410c5d4bdeSAlexander V. Chernikov 		if (trylimit-- > 0) {
23420c5d4bdeSAlexander V. Chernikov 			forcegen = 1;
23430c5d4bdeSAlexander V. Chernikov 			goto again;
23440c5d4bdeSAlexander V. Chernikov 		}
23450c5d4bdeSAlexander V. Chernikov 
23460c5d4bdeSAlexander V. Chernikov 		/* Give up.  Something strange should have happened.  */
2347743eee66SSUZUKI Shinsuke 		nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
2348743eee66SSUZUKI Shinsuke 		    "find a unique random IFID\n"));
234933841545SHajimu UMEMOTO 		return (EEXIST);
235033841545SHajimu UMEMOTO 	}
235182cd038dSYoshinobu Inoue 
235233841545SHajimu UMEMOTO 	/*
235333841545SHajimu UMEMOTO 	 * The Valid Lifetime is the lower of the Valid Lifetime of the
235433841545SHajimu UMEMOTO          * public address or TEMP_VALID_LIFETIME.
235533841545SHajimu UMEMOTO 	 * The Preferred Lifetime is the lower of the Preferred Lifetime
235633841545SHajimu UMEMOTO          * of the public address or TEMP_PREFERRED_LIFETIME -
235733841545SHajimu UMEMOTO          * DESYNC_FACTOR.
235882cd038dSYoshinobu Inoue 	 */
2359743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
236033841545SHajimu UMEMOTO 		vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2361743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_vltime -
23627d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2363603724d3SBjoern A. Zeeb 		if (vltime0 > V_ip6_temp_valid_lifetime)
2364603724d3SBjoern A. Zeeb 			vltime0 = V_ip6_temp_valid_lifetime;
236533841545SHajimu UMEMOTO 	} else
2366603724d3SBjoern A. Zeeb 		vltime0 = V_ip6_temp_valid_lifetime;
2367743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
236833841545SHajimu UMEMOTO 		pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2369743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_pltime -
23707d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2371603724d3SBjoern A. Zeeb 		if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2372603724d3SBjoern A. Zeeb 			pltime0 = V_ip6_temp_preferred_lifetime -
2373603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor;
237433841545SHajimu UMEMOTO 		}
237533841545SHajimu UMEMOTO 	} else
2376603724d3SBjoern A. Zeeb 		pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
237733841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = vltime0;
237833841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = pltime0;
237933841545SHajimu UMEMOTO 
238033841545SHajimu UMEMOTO 	/*
238133841545SHajimu UMEMOTO 	 * A temporary address is created only if this calculated Preferred
238233841545SHajimu UMEMOTO 	 * Lifetime is greater than REGEN_ADVANCE time units.
238333841545SHajimu UMEMOTO 	 */
2384603724d3SBjoern A. Zeeb 	if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
238533841545SHajimu UMEMOTO 		return (0);
238633841545SHajimu UMEMOTO 
238733841545SHajimu UMEMOTO 	/* XXX: scope zone ID? */
238833841545SHajimu UMEMOTO 
238933841545SHajimu UMEMOTO 	ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
239033841545SHajimu UMEMOTO 
239133841545SHajimu UMEMOTO 	/* allocate ifaddr structure, link into chain, etc. */
2392743eee66SSUZUKI Shinsuke 	updateflags = 0;
2393743eee66SSUZUKI Shinsuke 	if (delay)
2394743eee66SSUZUKI Shinsuke 		updateflags |= IN6_IFAUPDATE_DADDELAY;
2395743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
239633841545SHajimu UMEMOTO 		return (error);
239733841545SHajimu UMEMOTO 
239833841545SHajimu UMEMOTO 	newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
239933841545SHajimu UMEMOTO 	if (newia == NULL) {	/* XXX: can it happen? */
240033841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
240133841545SHajimu UMEMOTO 		    "in6_tmpifadd: ifa update succeeded, but we got "
240233841545SHajimu UMEMOTO 		    "no ifaddr\n"));
240333841545SHajimu UMEMOTO 		return (EINVAL); /* XXX */
240433841545SHajimu UMEMOTO 	}
240533841545SHajimu UMEMOTO 	newia->ia6_ndpr = ia0->ia6_ndpr;
24060bbf244eSMark Johnston 	newia->ia6_ndpr->ndpr_addrcnt++;
24078c0fec80SRobert Watson 	ifa_free(&newia->ia_ifa);
240833841545SHajimu UMEMOTO 
2409c3aacd9eSHajimu UMEMOTO 	/*
2410c3aacd9eSHajimu UMEMOTO 	 * A newly added address might affect the status of other addresses.
2411c3aacd9eSHajimu UMEMOTO 	 * XXX: when the temporary address is generated with a new public
2412c3aacd9eSHajimu UMEMOTO 	 * address, the onlink check is redundant.  However, it would be safe
2413c3aacd9eSHajimu UMEMOTO 	 * to do the check explicitly everywhere a new address is generated,
2414c3aacd9eSHajimu UMEMOTO 	 * and, in fact, we surely need the check when we create a new
2415c3aacd9eSHajimu UMEMOTO 	 * temporary address due to deprecation of an old temporary address.
2416c3aacd9eSHajimu UMEMOTO 	 */
2417c3aacd9eSHajimu UMEMOTO 	pfxlist_onlink_check();
2418c3aacd9eSHajimu UMEMOTO 
241933841545SHajimu UMEMOTO 	return (0);
242082cd038dSYoshinobu Inoue }
242182cd038dSYoshinobu Inoue 
2422743eee66SSUZUKI Shinsuke static int
242382cd038dSYoshinobu Inoue in6_init_prefix_ltimes(struct nd_prefix *ndpr)
242482cd038dSYoshinobu Inoue {
242582cd038dSYoshinobu Inoue 	if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
242682cd038dSYoshinobu Inoue 		ndpr->ndpr_preferred = 0;
242782cd038dSYoshinobu Inoue 	else
24287d26db17SHiroki Sato 		ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
242982cd038dSYoshinobu Inoue 	if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
243082cd038dSYoshinobu Inoue 		ndpr->ndpr_expire = 0;
243182cd038dSYoshinobu Inoue 	else
24327d26db17SHiroki Sato 		ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
243382cd038dSYoshinobu Inoue 
243482cd038dSYoshinobu Inoue 	return 0;
243582cd038dSYoshinobu Inoue }
243682cd038dSYoshinobu Inoue 
243782cd038dSYoshinobu Inoue static void
243833841545SHajimu UMEMOTO in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
243982cd038dSYoshinobu Inoue {
244082cd038dSYoshinobu Inoue 	/* init ia6t_expire */
244182cd038dSYoshinobu Inoue 	if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
244282cd038dSYoshinobu Inoue 		lt6->ia6t_expire = 0;
244382cd038dSYoshinobu Inoue 	else {
24447d26db17SHiroki Sato 		lt6->ia6t_expire = time_uptime;
244582cd038dSYoshinobu Inoue 		lt6->ia6t_expire += lt6->ia6t_vltime;
244682cd038dSYoshinobu Inoue 	}
2447686cdd19SJun-ichiro itojun Hagino 
244882cd038dSYoshinobu Inoue 	/* init ia6t_preferred */
244982cd038dSYoshinobu Inoue 	if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
245082cd038dSYoshinobu Inoue 		lt6->ia6t_preferred = 0;
245182cd038dSYoshinobu Inoue 	else {
24527d26db17SHiroki Sato 		lt6->ia6t_preferred = time_uptime;
245382cd038dSYoshinobu Inoue 		lt6->ia6t_preferred += lt6->ia6t_pltime;
245482cd038dSYoshinobu Inoue 	}
245582cd038dSYoshinobu Inoue }
245682cd038dSYoshinobu Inoue 
245782cd038dSYoshinobu Inoue /*
245882cd038dSYoshinobu Inoue  * Delete all the routing table entries that use the specified gateway.
245982cd038dSYoshinobu Inoue  * XXX: this function causes search through all entries of routing table, so
246082cd038dSYoshinobu Inoue  * it shouldn't be called when acting as a router.
246182cd038dSYoshinobu Inoue  */
246282cd038dSYoshinobu Inoue void
24631272577eSXin LI rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
246482cd038dSYoshinobu Inoue {
246582cd038dSYoshinobu Inoue 
246682cd038dSYoshinobu Inoue 	/* We'll care only link-local addresses */
2467c9b652e3SAndre Oppermann 	if (!IN6_IS_ADDR_LINKLOCAL(gateway))
246882cd038dSYoshinobu Inoue 		return;
246982cd038dSYoshinobu Inoue 
247081d5d46bSBjoern A. Zeeb 	/* XXX Do we really need to walk any but the default FIB? */
2471e8b0643eSAlexander V. Chernikov 	rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
247282cd038dSYoshinobu Inoue }
247382cd038dSYoshinobu Inoue 
247482cd038dSYoshinobu Inoue static int
2475e8b0643eSAlexander V. Chernikov rt6_deleteroute(const struct rtentry *rt, void *arg)
247682cd038dSYoshinobu Inoue {
247782cd038dSYoshinobu Inoue #define SIN6(s)	((struct sockaddr_in6 *)s)
247882cd038dSYoshinobu Inoue 	struct in6_addr *gate = (struct in6_addr *)arg;
247982cd038dSYoshinobu Inoue 
248082cd038dSYoshinobu Inoue 	if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
248182cd038dSYoshinobu Inoue 		return (0);
248282cd038dSYoshinobu Inoue 
248307eb2995SHajimu UMEMOTO 	if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
248482cd038dSYoshinobu Inoue 		return (0);
248507eb2995SHajimu UMEMOTO 	}
248682cd038dSYoshinobu Inoue 
248782cd038dSYoshinobu Inoue 	/*
248833841545SHajimu UMEMOTO 	 * Do not delete a static route.
248933841545SHajimu UMEMOTO 	 * XXX: this seems to be a bit ad-hoc. Should we consider the
249033841545SHajimu UMEMOTO 	 * 'cloned' bit instead?
249133841545SHajimu UMEMOTO 	 */
249233841545SHajimu UMEMOTO 	if ((rt->rt_flags & RTF_STATIC) != 0)
249333841545SHajimu UMEMOTO 		return (0);
249433841545SHajimu UMEMOTO 
249533841545SHajimu UMEMOTO 	/*
249682cd038dSYoshinobu Inoue 	 * We delete only host route. This means, in particular, we don't
249782cd038dSYoshinobu Inoue 	 * delete default route.
249882cd038dSYoshinobu Inoue 	 */
249982cd038dSYoshinobu Inoue 	if ((rt->rt_flags & RTF_HOST) == 0)
250082cd038dSYoshinobu Inoue 		return (0);
250182cd038dSYoshinobu Inoue 
2502e8b0643eSAlexander V. Chernikov 	return (1);
250382cd038dSYoshinobu Inoue #undef SIN6
250482cd038dSYoshinobu Inoue }
2505686cdd19SJun-ichiro itojun Hagino 
2506686cdd19SJun-ichiro itojun Hagino int
25071272577eSXin LI nd6_setdefaultiface(int ifindex)
2508686cdd19SJun-ichiro itojun Hagino {
2509686cdd19SJun-ichiro itojun Hagino 	int error = 0;
2510686cdd19SJun-ichiro itojun Hagino 
2511603724d3SBjoern A. Zeeb 	if (ifindex < 0 || V_if_index < ifindex)
2512686cdd19SJun-ichiro itojun Hagino 		return (EINVAL);
2513743eee66SSUZUKI Shinsuke 	if (ifindex != 0 && !ifnet_byindex(ifindex))
2514743eee66SSUZUKI Shinsuke 		return (EINVAL);
2515686cdd19SJun-ichiro itojun Hagino 
2516603724d3SBjoern A. Zeeb 	if (V_nd6_defifindex != ifindex) {
2517603724d3SBjoern A. Zeeb 		V_nd6_defifindex = ifindex;
2518603724d3SBjoern A. Zeeb 		if (V_nd6_defifindex > 0)
2519603724d3SBjoern A. Zeeb 			V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2520686cdd19SJun-ichiro itojun Hagino 		else
2521603724d3SBjoern A. Zeeb 			V_nd6_defifp = NULL;
2522686cdd19SJun-ichiro itojun Hagino 
2523686cdd19SJun-ichiro itojun Hagino 		/*
2524686cdd19SJun-ichiro itojun Hagino 		 * Our current implementation assumes one-to-one maping between
2525686cdd19SJun-ichiro itojun Hagino 		 * interfaces and links, so it would be natural to use the
2526686cdd19SJun-ichiro itojun Hagino 		 * default interface as the default link.
2527686cdd19SJun-ichiro itojun Hagino 		 */
2528603724d3SBjoern A. Zeeb 		scope6_setdefault(V_nd6_defifp);
2529686cdd19SJun-ichiro itojun Hagino 	}
2530686cdd19SJun-ichiro itojun Hagino 
2531686cdd19SJun-ichiro itojun Hagino 	return (error);
2532686cdd19SJun-ichiro itojun Hagino }
2533ad675b32SBjoern A. Zeeb 
2534ad675b32SBjoern A. Zeeb static int
2535ad675b32SBjoern A. Zeeb nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2536ad675b32SBjoern A. Zeeb {
2537ad675b32SBjoern A. Zeeb 	struct in6_defrouter d;
2538ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
2539ad675b32SBjoern A. Zeeb 	int error;
2540ad675b32SBjoern A. Zeeb 
2541ad675b32SBjoern A. Zeeb 	if (req->newptr != NULL)
2542ad675b32SBjoern A. Zeeb 		return (EPERM);
2543ad675b32SBjoern A. Zeeb 
2544ad675b32SBjoern A. Zeeb 	error = sysctl_wire_old_buffer(req, 0);
2545ad675b32SBjoern A. Zeeb 	if (error != 0)
2546ad675b32SBjoern A. Zeeb 		return (error);
2547ad675b32SBjoern A. Zeeb 
2548ad675b32SBjoern A. Zeeb 	bzero(&d, sizeof(d));
2549ad675b32SBjoern A. Zeeb 	d.rtaddr.sin6_family = AF_INET6;
2550ad675b32SBjoern A. Zeeb 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
2551ad675b32SBjoern A. Zeeb 
2552ad675b32SBjoern A. Zeeb 	ND6_RLOCK();
2553ad675b32SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
2554ad675b32SBjoern A. Zeeb 		d.rtaddr.sin6_addr = dr->rtaddr;
2555ad675b32SBjoern A. Zeeb 		error = sa6_recoverscope(&d.rtaddr);
2556ad675b32SBjoern A. Zeeb 		if (error != 0)
2557ad675b32SBjoern A. Zeeb 			break;
2558ad675b32SBjoern A. Zeeb 		d.flags = dr->raflags;
2559ad675b32SBjoern A. Zeeb 		d.rtlifetime = dr->rtlifetime;
2560ad675b32SBjoern A. Zeeb 		d.expire = dr->expire + (time_second - time_uptime);
2561ad675b32SBjoern A. Zeeb 		d.if_index = dr->ifp->if_index;
2562ad675b32SBjoern A. Zeeb 		error = SYSCTL_OUT(req, &d, sizeof(d));
2563ad675b32SBjoern A. Zeeb 		if (error != 0)
2564ad675b32SBjoern A. Zeeb 			break;
2565ad675b32SBjoern A. Zeeb 	}
2566ad675b32SBjoern A. Zeeb 	ND6_RUNLOCK();
2567ad675b32SBjoern A. Zeeb 	return (error);
2568ad675b32SBjoern A. Zeeb }
2569ad675b32SBjoern A. Zeeb SYSCTL_DECL(_net_inet6_icmp6);
2570ad675b32SBjoern A. Zeeb SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2571ad675b32SBjoern A. Zeeb 	CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2572ad675b32SBjoern A. Zeeb 	NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2573ad675b32SBjoern A. Zeeb 	"NDP default router list");
2574ad675b32SBjoern A. Zeeb 
2575ad675b32SBjoern A. Zeeb bool
2576ad675b32SBjoern A. Zeeb nd6_defrouter_list_empty(void)
2577ad675b32SBjoern A. Zeeb {
2578ad675b32SBjoern A. Zeeb 
2579ad675b32SBjoern A. Zeeb 	return (TAILQ_EMPTY(&V_nd_defrouter));
2580ad675b32SBjoern A. Zeeb }
2581ad675b32SBjoern A. Zeeb 
2582ad675b32SBjoern A. Zeeb void
2583ad675b32SBjoern A. Zeeb nd6_defrouter_timer(void)
2584ad675b32SBjoern A. Zeeb {
2585ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2586ad675b32SBjoern A. Zeeb 	struct nd_drhead drq;
2587ad675b32SBjoern A. Zeeb 
2588ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2589ad675b32SBjoern A. Zeeb 
2590ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2591ad675b32SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr)
2592ad675b32SBjoern A. Zeeb 		if (dr->expire && dr->expire < time_uptime)
2593ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2594ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2595ad675b32SBjoern A. Zeeb 
2596ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2597ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2598ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2599ad675b32SBjoern A. Zeeb 	}
2600ad675b32SBjoern A. Zeeb }
2601ad675b32SBjoern A. Zeeb 
2602ad675b32SBjoern A. Zeeb /*
2603ad675b32SBjoern A. Zeeb  * Nuke default router list entries toward ifp.
2604ad675b32SBjoern A. Zeeb  * We defer removal of default router list entries that is installed in the
2605ad675b32SBjoern A. Zeeb  * routing table, in order to keep additional side effects as small as possible.
2606ad675b32SBjoern A. Zeeb  */
2607ad675b32SBjoern A. Zeeb void
2608ad675b32SBjoern A. Zeeb nd6_defrouter_purge(struct ifnet *ifp)
2609ad675b32SBjoern A. Zeeb {
2610ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2611ad675b32SBjoern A. Zeeb 	struct nd_drhead drq;
2612ad675b32SBjoern A. Zeeb 
2613ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2614ad675b32SBjoern A. Zeeb 
2615ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2616ad675b32SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
2617ad675b32SBjoern A. Zeeb 		if (dr->installed)
2618ad675b32SBjoern A. Zeeb 			continue;
2619ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2620ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2621ad675b32SBjoern A. Zeeb 	}
2622ad675b32SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
2623ad675b32SBjoern A. Zeeb 		if (!dr->installed)
2624ad675b32SBjoern A. Zeeb 			continue;
2625ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2626ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2627ad675b32SBjoern A. Zeeb 	}
2628ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2629ad675b32SBjoern A. Zeeb 
2630ad675b32SBjoern A. Zeeb 	/* Delete the unlinked router objects. */
2631ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2632ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2633ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2634ad675b32SBjoern A. Zeeb 	}
2635ad675b32SBjoern A. Zeeb }
2636ad675b32SBjoern A. Zeeb 
2637ad675b32SBjoern A. Zeeb void
2638ad675b32SBjoern A. Zeeb nd6_defrouter_flush_all(void)
2639ad675b32SBjoern A. Zeeb {
2640ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
2641ad675b32SBjoern A. Zeeb 	struct nd_drhead drq;
2642ad675b32SBjoern A. Zeeb 
2643ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2644ad675b32SBjoern A. Zeeb 
2645ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2646ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&V_nd_defrouter)) != NULL)
2647ad675b32SBjoern A. Zeeb 		defrouter_unlink(dr, &drq);
2648ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2649ad675b32SBjoern A. Zeeb 
2650ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2651ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2652ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2653ad675b32SBjoern A. Zeeb 	}
2654ad675b32SBjoern A. Zeeb }
2655ad675b32SBjoern A. Zeeb 
2656ad675b32SBjoern A. Zeeb void
2657ad675b32SBjoern A. Zeeb nd6_defrouter_init(void)
2658ad675b32SBjoern A. Zeeb {
2659ad675b32SBjoern A. Zeeb 
2660ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&V_nd_defrouter);
2661ad675b32SBjoern A. Zeeb }
2662