xref: /freebsd/sys/netinet6/nd6_rtr.c (revision 25ebfe33508aa4319192ca2a7c296600d6f76ce8)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue  * All rights reserved.
682cd038dSYoshinobu Inoue  *
782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue  * are met:
1082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue  *    without specific prior written permission.
1882cd038dSYoshinobu Inoue  *
1982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
3282cd038dSYoshinobu Inoue  */
3382cd038dSYoshinobu Inoue 
34b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
35b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
36b48287a3SDavid E. O'Brien 
37686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
38686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
39686cdd19SJun-ichiro itojun Hagino 
4082cd038dSYoshinobu Inoue #include <sys/param.h>
4182cd038dSYoshinobu Inoue #include <sys/systm.h>
4282cd038dSYoshinobu Inoue #include <sys/malloc.h>
4382cd038dSYoshinobu Inoue #include <sys/mbuf.h>
444de485feSMark Johnston #include <sys/refcount.h>
4582cd038dSYoshinobu Inoue #include <sys/socket.h>
4682cd038dSYoshinobu Inoue #include <sys/sockio.h>
4782cd038dSYoshinobu Inoue #include <sys/time.h>
4833841545SHajimu UMEMOTO #include <sys/kernel.h>
49609ff41fSWarner Losh #include <sys/lock.h>
5082cd038dSYoshinobu Inoue #include <sys/errno.h>
511b28988bSMark Johnston #include <sys/rmlock.h>
523120b9d4SKip Macy #include <sys/rwlock.h>
53ad675b32SBjoern A. Zeeb #include <sys/sysctl.h>
5482cd038dSYoshinobu Inoue #include <sys/syslog.h>
5533841545SHajimu UMEMOTO #include <sys/queue.h>
5682cd038dSYoshinobu Inoue 
5782cd038dSYoshinobu Inoue #include <net/if.h>
5876039bc8SGleb Smirnoff #include <net/if_var.h>
5982cd038dSYoshinobu Inoue #include <net/if_types.h>
6082cd038dSYoshinobu Inoue #include <net/if_dl.h>
6182cd038dSYoshinobu Inoue #include <net/route.h>
6261eee0e2SAlexander V. Chernikov #include <net/route_var.h>
6382cd038dSYoshinobu Inoue #include <net/radix.h>
644b79449eSBjoern A. Zeeb #include <net/vnet.h>
6582cd038dSYoshinobu Inoue 
6682cd038dSYoshinobu Inoue #include <netinet/in.h>
676e6b3f7cSQing Li #include <net/if_llatbl.h>
6882cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
6933841545SHajimu UMEMOTO #include <netinet6/in6_ifattach.h>
70686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7182cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
7282cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
74686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h>
7582cd038dSYoshinobu Inoue 
769233d8f3SDavid E. O'Brien static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
776f56329aSXin LI static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
786f56329aSXin LI     struct mbuf *, int);
79*25ebfe33SBjoern A. Zeeb static int nd6_prefix_onlink(struct nd_prefix *);
8082cd038dSYoshinobu Inoue 
81f592d0c3SBjoern A. Zeeb TAILQ_HEAD(nd6_drhead, nd_defrouter);
82f592d0c3SBjoern A. Zeeb VNET_DEFINE_STATIC(struct nd6_drhead, nd6_defrouter);
83f592d0c3SBjoern A. Zeeb #define	V_nd6_defrouter			VNET(nd6_defrouter)
84ad675b32SBjoern A. Zeeb 
85eddfbb76SRobert Watson VNET_DECLARE(int, nd6_recalc_reachtm_interval);
861e77c105SRobert Watson #define	V_nd6_recalc_reachtm_interval	VNET(nd6_recalc_reachtm_interval)
8782cd038dSYoshinobu Inoue 
885f901c92SAndrew Turner VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
8982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_defifindex);
901e77c105SRobert Watson #define	V_nd6_defifp			VNET(nd6_defifp)
91686cdd19SJun-ichiro itojun Hagino 
9282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_use_tempaddr) = 0;
9382cea7e6SBjoern A. Zeeb 
94eddfbb76SRobert Watson VNET_DEFINE(int, ip6_desync_factor);
9582cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
9682cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
9782cea7e6SBjoern A. Zeeb 
9882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
9933841545SHajimu UMEMOTO 
10021231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL
10121231a7aSBjoern A. Zeeb VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1;
10221231a7aSBjoern A. Zeeb #endif
10321231a7aSBjoern A. Zeeb 
104f77a6dbdSBjoern A. Zeeb SYSCTL_DECL(_net_inet6_icmp6);
105f77a6dbdSBjoern A. Zeeb 
106743eee66SSUZUKI Shinsuke /* RTPREF_MEDIUM has to be 0! */
107743eee66SSUZUKI Shinsuke #define RTPREF_HIGH	1
108743eee66SSUZUKI Shinsuke #define RTPREF_MEDIUM	0
109743eee66SSUZUKI Shinsuke #define RTPREF_LOW	(-1)
110743eee66SSUZUKI Shinsuke #define RTPREF_RESERVED	(-2)
111743eee66SSUZUKI Shinsuke #define RTPREF_INVALID	(-3)	/* internal */
112743eee66SSUZUKI Shinsuke 
113ad675b32SBjoern A. Zeeb static void
114ad675b32SBjoern A. Zeeb defrouter_ref(struct nd_defrouter *dr)
115ad675b32SBjoern A. Zeeb {
116ad675b32SBjoern A. Zeeb 
117ad675b32SBjoern A. Zeeb 	refcount_acquire(&dr->refcnt);
118ad675b32SBjoern A. Zeeb }
119ad675b32SBjoern A. Zeeb 
120ad675b32SBjoern A. Zeeb void
121ad675b32SBjoern A. Zeeb defrouter_rele(struct nd_defrouter *dr)
122ad675b32SBjoern A. Zeeb {
123ad675b32SBjoern A. Zeeb 
124ad675b32SBjoern A. Zeeb 	if (refcount_release(&dr->refcnt))
125ad675b32SBjoern A. Zeeb 		free(dr, M_IP6NDP);
126ad675b32SBjoern A. Zeeb }
127ad675b32SBjoern A. Zeeb 
128ad675b32SBjoern A. Zeeb /*
129ad675b32SBjoern A. Zeeb  * Remove a router from the global list and optionally stash it in a
130ad675b32SBjoern A. Zeeb  * caller-supplied queue.
131ad675b32SBjoern A. Zeeb  */
132ad675b32SBjoern A. Zeeb static void
133f592d0c3SBjoern A. Zeeb defrouter_unlink(struct nd_defrouter *dr, struct nd6_drhead *drq)
134ad675b32SBjoern A. Zeeb {
135ad675b32SBjoern A. Zeeb 
136ad675b32SBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
137ad675b32SBjoern A. Zeeb 
138f592d0c3SBjoern A. Zeeb 	TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
139ad675b32SBjoern A. Zeeb 	V_nd6_list_genid++;
140ad675b32SBjoern A. Zeeb 	if (drq != NULL)
141ad675b32SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(drq, dr, dr_entry);
142ad675b32SBjoern A. Zeeb }
143ad675b32SBjoern A. Zeeb 
14482cd038dSYoshinobu Inoue /*
14582cd038dSYoshinobu Inoue  * Receive Router Solicitation Message - just for routers.
14682cd038dSYoshinobu Inoue  * Router solicitation/advertisement is mostly managed by userland program
14782cd038dSYoshinobu Inoue  * (rtadvd) so here we have no function like nd6_ra_output().
14882cd038dSYoshinobu Inoue  *
14982cd038dSYoshinobu Inoue  * Based on RFC 2461
15082cd038dSYoshinobu Inoue  */
15182cd038dSYoshinobu Inoue void
1521272577eSXin LI nd6_rs_input(struct mbuf *m, int off, int icmp6len)
15382cd038dSYoshinobu Inoue {
154503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
155503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
156686cdd19SJun-ichiro itojun Hagino 	struct nd_router_solicit *nd_rs;
157503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
15882cd038dSYoshinobu Inoue 	union nd_opts ndopts;
1591d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
160503f4e47SBjoern A. Zeeb 	char *lladdr;
161503f4e47SBjoern A. Zeeb 	int lladdrlen;
162503f4e47SBjoern A. Zeeb 
163503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
16482cd038dSYoshinobu Inoue 
165e7fa8d0aSHiroki Sato 	/*
166e7fa8d0aSHiroki Sato 	 * Accept RS only when V_ip6_forwarding=1 and the interface has
167e7fa8d0aSHiroki Sato 	 * no ND6_IFF_ACCEPT_RTADV.
168e7fa8d0aSHiroki Sato 	 */
169e7fa8d0aSHiroki Sato 	if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
170686cdd19SJun-ichiro itojun Hagino 		goto freeit;
17182cd038dSYoshinobu Inoue 
172a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
173a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
174a4061289SAndrey V. Elsukov 		goto freeit;
175a4061289SAndrey V. Elsukov 
17682cd038dSYoshinobu Inoue 	/* Sanity checks */
177503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
17882cd038dSYoshinobu Inoue 	if (ip6->ip6_hlim != 255) {
17933841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
18033841545SHajimu UMEMOTO 		    "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
1811d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
1821d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
18333841545SHajimu UMEMOTO 		goto bad;
18482cd038dSYoshinobu Inoue 	}
18582cd038dSYoshinobu Inoue 
18682cd038dSYoshinobu Inoue 	/*
18782cd038dSYoshinobu Inoue 	 * Don't update the neighbor cache, if src = ::.
18882cd038dSYoshinobu Inoue 	 * This indicates that the src has no IP address assigned yet.
18982cd038dSYoshinobu Inoue 	 */
190503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
19182cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
192686cdd19SJun-ichiro itojun Hagino 		goto freeit;
193686cdd19SJun-ichiro itojun Hagino 
194a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + icmp6len) {
195a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + icmp6len);
196a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
197a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
19882cd038dSYoshinobu Inoue 			return;
199686cdd19SJun-ichiro itojun Hagino 		}
200a4adf6ccSBjoern A. Zeeb 	}
201a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
202a61b5cfbSBjoern A. Zeeb 	nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
20382cd038dSYoshinobu Inoue 
20482cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_rs);
20582cd038dSYoshinobu Inoue 	nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
20682cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
20733841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
20833841545SHajimu UMEMOTO 		    "nd6_rs_input: invalid ND option, ignored\n"));
20933841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
210686cdd19SJun-ichiro itojun Hagino 		goto freeit;
21182cd038dSYoshinobu Inoue 	}
21282cd038dSYoshinobu Inoue 
213503f4e47SBjoern A. Zeeb 	lladdr = NULL;
214503f4e47SBjoern A. Zeeb 	lladdrlen = 0;
21582cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
21682cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
21782cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
21882cd038dSYoshinobu Inoue 	}
21982cd038dSYoshinobu Inoue 
22082cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
22133841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
22282cd038dSYoshinobu Inoue 		    "nd6_rs_input: lladdrlen mismatch for %s "
22382cd038dSYoshinobu Inoue 		    "(if %d, RS packet %d)\n",
2241d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6),
22507eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
22633841545SHajimu UMEMOTO 		goto bad;
22782cd038dSYoshinobu Inoue 	}
22882cd038dSYoshinobu Inoue 
22982cd038dSYoshinobu Inoue 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
230686cdd19SJun-ichiro itojun Hagino 
231686cdd19SJun-ichiro itojun Hagino  freeit:
232686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
23333841545SHajimu UMEMOTO 	return;
23433841545SHajimu UMEMOTO 
23533841545SHajimu UMEMOTO  bad:
236e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badrs);
23733841545SHajimu UMEMOTO 	m_freem(m);
23882cd038dSYoshinobu Inoue }
23982cd038dSYoshinobu Inoue 
240201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
241201100c5SBjoern A. Zeeb /*
242201100c5SBjoern A. Zeeb  * An initial update routine for draft-ietf-6man-ipv6only-flag.
243201100c5SBjoern A. Zeeb  * We need to iterate over all default routers for the given
24421231a7aSBjoern A. Zeeb  * interface to see whether they are all advertising the "S"
245201100c5SBjoern A. Zeeb  * (IPv6-Only) flag.  If they do set, otherwise unset, the
246201100c5SBjoern A. Zeeb  * interface flag we later use to filter on.
247201100c5SBjoern A. Zeeb  */
248201100c5SBjoern A. Zeeb static void
249201100c5SBjoern A. Zeeb defrtr_ipv6_only_ifp(struct ifnet *ifp)
250201100c5SBjoern A. Zeeb {
251201100c5SBjoern A. Zeeb 	struct nd_defrouter *dr;
25221231a7aSBjoern A. Zeeb 	bool ipv6_only, ipv6_only_old;
25321231a7aSBjoern A. Zeeb #ifdef INET
25421231a7aSBjoern A. Zeeb 	struct epoch_tracker et;
25521231a7aSBjoern A. Zeeb 	struct ifaddr *ifa;
25621231a7aSBjoern A. Zeeb 	bool has_ipv4_addr;
25721231a7aSBjoern A. Zeeb #endif
25821231a7aSBjoern A. Zeeb 
25921231a7aSBjoern A. Zeeb 	if (V_nd6_ignore_ipv6_only_ra != 0)
26021231a7aSBjoern A. Zeeb 		return;
261201100c5SBjoern A. Zeeb 
262201100c5SBjoern A. Zeeb 	ipv6_only = true;
263201100c5SBjoern A. Zeeb 	ND6_RLOCK();
264f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
265201100c5SBjoern A. Zeeb 		if (dr->ifp == ifp &&
266201100c5SBjoern A. Zeeb 		    (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0)
267201100c5SBjoern A. Zeeb 			ipv6_only = false;
268201100c5SBjoern A. Zeeb 	ND6_RUNLOCK();
269201100c5SBjoern A. Zeeb 
270201100c5SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
27121231a7aSBjoern A. Zeeb 	ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY;
27221231a7aSBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
27321231a7aSBjoern A. Zeeb 
27421231a7aSBjoern A. Zeeb 	/* If nothing changed, we have an early exit. */
27521231a7aSBjoern A. Zeeb 	if (ipv6_only == ipv6_only_old)
27621231a7aSBjoern A. Zeeb 		return;
27721231a7aSBjoern A. Zeeb 
27821231a7aSBjoern A. Zeeb #ifdef INET
27921231a7aSBjoern A. Zeeb 	/*
28021231a7aSBjoern A. Zeeb 	 * Should we want to set the IPV6-ONLY flag, check if the
28121231a7aSBjoern A. Zeeb 	 * interface has a non-0/0 and non-link-local IPv4 address
28221231a7aSBjoern A. Zeeb 	 * configured on it.  If it has we will assume working
28321231a7aSBjoern A. Zeeb 	 * IPv4 operations and will clear the interface flag.
28421231a7aSBjoern A. Zeeb 	 */
28521231a7aSBjoern A. Zeeb 	has_ipv4_addr = false;
28621231a7aSBjoern A. Zeeb 	if (ipv6_only) {
28721231a7aSBjoern A. Zeeb 		NET_EPOCH_ENTER(et);
28821231a7aSBjoern A. Zeeb 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
28921231a7aSBjoern A. Zeeb 			if (ifa->ifa_addr->sa_family != AF_INET)
29021231a7aSBjoern A. Zeeb 				continue;
29121231a7aSBjoern A. Zeeb 			if (in_canforward(
29221231a7aSBjoern A. Zeeb 			    satosin(ifa->ifa_addr)->sin_addr)) {
29321231a7aSBjoern A. Zeeb 				has_ipv4_addr = true;
29421231a7aSBjoern A. Zeeb 				break;
29521231a7aSBjoern A. Zeeb 			}
29621231a7aSBjoern A. Zeeb 		}
29721231a7aSBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
29821231a7aSBjoern A. Zeeb 	}
29921231a7aSBjoern A. Zeeb 	if (ipv6_only && has_ipv4_addr) {
30021231a7aSBjoern A. Zeeb 		log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
30121231a7aSBjoern A. Zeeb 		    "configured, ignoring IPv6-Only flag.\n", ifp->if_xname);
30221231a7aSBjoern A. Zeeb 		ipv6_only = false;
30321231a7aSBjoern A. Zeeb 	}
30421231a7aSBjoern A. Zeeb #endif
30521231a7aSBjoern A. Zeeb 
30621231a7aSBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
307201100c5SBjoern A. Zeeb 	if (ipv6_only)
308201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY;
309201100c5SBjoern A. Zeeb 	else
310201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
311201100c5SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
31221231a7aSBjoern A. Zeeb 
31321231a7aSBjoern A. Zeeb #ifdef notyet
31421231a7aSBjoern A. Zeeb 	/* Send notification of flag change. */
315201100c5SBjoern A. Zeeb #endif
31621231a7aSBjoern A. Zeeb }
31730b45077SBjoern A. Zeeb 
31830b45077SBjoern A. Zeeb static void
31930b45077SBjoern A. Zeeb defrtr_ipv6_only_ipf_down(struct ifnet *ifp)
32030b45077SBjoern A. Zeeb {
32130b45077SBjoern A. Zeeb 
32230b45077SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
32330b45077SBjoern A. Zeeb 	ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
32430b45077SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
32530b45077SBjoern A. Zeeb }
32621231a7aSBjoern A. Zeeb #endif	/* EXPERIMENTAL */
327201100c5SBjoern A. Zeeb 
32830b45077SBjoern A. Zeeb void
32930b45077SBjoern A. Zeeb nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate)
33030b45077SBjoern A. Zeeb {
33130b45077SBjoern A. Zeeb 
33230b45077SBjoern A. Zeeb 	/*
33330b45077SBjoern A. Zeeb 	 * XXX-BZ we might want to trigger re-evaluation of our default router
33430b45077SBjoern A. Zeeb 	 * availability. E.g., on link down the default router might be
33530b45077SBjoern A. Zeeb 	 * unreachable but a different interface might still have connectivity.
33630b45077SBjoern A. Zeeb 	 */
33730b45077SBjoern A. Zeeb 
33830b45077SBjoern A. Zeeb #ifdef EXPERIMENTAL
33930b45077SBjoern A. Zeeb 	if (linkstate == LINK_STATE_DOWN)
34030b45077SBjoern A. Zeeb 		defrtr_ipv6_only_ipf_down(ifp);
34130b45077SBjoern A. Zeeb #endif
34230b45077SBjoern A. Zeeb }
34330b45077SBjoern A. Zeeb 
34482cd038dSYoshinobu Inoue /*
34582cd038dSYoshinobu Inoue  * Receive Router Advertisement Message.
34682cd038dSYoshinobu Inoue  *
34782cd038dSYoshinobu Inoue  * Based on RFC 2461
34882cd038dSYoshinobu Inoue  * TODO: on-link bit on prefix information
34982cd038dSYoshinobu Inoue  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
35082cd038dSYoshinobu Inoue  */
35182cd038dSYoshinobu Inoue void
3521272577eSXin LI nd6_ra_input(struct mbuf *m, int off, int icmp6len)
35382cd038dSYoshinobu Inoue {
354503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
355503f4e47SBjoern A. Zeeb 	struct nd_ifinfo *ndi;
356503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
357686cdd19SJun-ichiro itojun Hagino 	struct nd_router_advert *nd_ra;
358503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
35982cd038dSYoshinobu Inoue 	struct nd_defrouter *dr;
360503f4e47SBjoern A. Zeeb 	union nd_opts ndopts;
3611d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
362503f4e47SBjoern A. Zeeb 	int mcast;
3634de485feSMark Johnston 
36407cf047dSHajimu UMEMOTO 	/*
365e7fa8d0aSHiroki Sato 	 * We only accept RAs only when the per-interface flag
366e7fa8d0aSHiroki Sato 	 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
36707cf047dSHajimu UMEMOTO 	 */
368503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
369503f4e47SBjoern A. Zeeb 	ndi = ND_IFINFO(ifp);
370e7fa8d0aSHiroki Sato 	if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
37107cf047dSHajimu UMEMOTO 		goto freeit;
37282cd038dSYoshinobu Inoue 
373a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
374a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
375a4061289SAndrey V. Elsukov 		goto freeit;
376a4061289SAndrey V. Elsukov 
377503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
37882cd038dSYoshinobu Inoue 	if (ip6->ip6_hlim != 255) {
37933841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
38033841545SHajimu UMEMOTO 		    "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
3811d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
3821d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
38333841545SHajimu UMEMOTO 		goto bad;
38482cd038dSYoshinobu Inoue 	}
38582cd038dSYoshinobu Inoue 
386503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
38782cd038dSYoshinobu Inoue 	if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
38833841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
38982cd038dSYoshinobu Inoue 		    "nd6_ra_input: src %s is not link-local\n",
3901d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6)));
39133841545SHajimu UMEMOTO 		goto bad;
392686cdd19SJun-ichiro itojun Hagino 	}
393686cdd19SJun-ichiro itojun Hagino 
394a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + icmp6len) {
395a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + icmp6len);
396a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
397a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
39882cd038dSYoshinobu Inoue 			return;
39982cd038dSYoshinobu Inoue 		}
400a4adf6ccSBjoern A. Zeeb 	}
401a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
402a61b5cfbSBjoern A. Zeeb 	nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
40382cd038dSYoshinobu Inoue 
40482cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_ra);
40582cd038dSYoshinobu Inoue 	nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
40682cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
40733841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
40833841545SHajimu UMEMOTO 		    "nd6_ra_input: invalid ND option, ignored\n"));
40933841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
410686cdd19SJun-ichiro itojun Hagino 		goto freeit;
41182cd038dSYoshinobu Inoue 	}
41282cd038dSYoshinobu Inoue 
413503f4e47SBjoern A. Zeeb 	mcast = 0;
414503f4e47SBjoern A. Zeeb 	dr = NULL;
41582cd038dSYoshinobu Inoue     {
41682cd038dSYoshinobu Inoue 	struct nd_defrouter dr0;
41782cd038dSYoshinobu Inoue 	u_int32_t advreachable = nd_ra->nd_ra_reachable;
41882cd038dSYoshinobu Inoue 
419743eee66SSUZUKI Shinsuke 	/* remember if this is a multicasted advertisement */
420743eee66SSUZUKI Shinsuke 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
421743eee66SSUZUKI Shinsuke 		mcast = 1;
422743eee66SSUZUKI Shinsuke 
423743eee66SSUZUKI Shinsuke 	bzero(&dr0, sizeof(dr0));
42482cd038dSYoshinobu Inoue 	dr0.rtaddr = saddr6;
42501869be5SMark Johnston 	dr0.raflags = nd_ra->nd_ra_flags_reserved;
426e7fa8d0aSHiroki Sato 	/*
427049087a0SHiroki Sato 	 * Effectively-disable routes from RA messages when
428049087a0SHiroki Sato 	 * ND6_IFF_NO_RADR enabled on the receiving interface or
429049087a0SHiroki Sato 	 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
430e7fa8d0aSHiroki Sato 	 */
431049087a0SHiroki Sato 	if (ndi->flags & ND6_IFF_NO_RADR)
432049087a0SHiroki Sato 		dr0.rtlifetime = 0;
433049087a0SHiroki Sato 	else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
434e7fa8d0aSHiroki Sato 		dr0.rtlifetime = 0;
435e7fa8d0aSHiroki Sato 	else
43682cd038dSYoshinobu Inoue 		dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
4377d26db17SHiroki Sato 	dr0.expire = time_uptime + dr0.rtlifetime;
43882cd038dSYoshinobu Inoue 	dr0.ifp = ifp;
43982cd038dSYoshinobu Inoue 	/* unspecified or not? (RFC 2461 6.3.4) */
44082cd038dSYoshinobu Inoue 	if (advreachable) {
441fd8e4ebcSMike Barcroft 		advreachable = ntohl(advreachable);
44282cd038dSYoshinobu Inoue 		if (advreachable <= MAX_REACHABLE_TIME &&
44382cd038dSYoshinobu Inoue 		    ndi->basereachable != advreachable) {
44482cd038dSYoshinobu Inoue 			ndi->basereachable = advreachable;
44582cd038dSYoshinobu Inoue 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
446603724d3SBjoern A. Zeeb 			ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
44782cd038dSYoshinobu Inoue 		}
44882cd038dSYoshinobu Inoue 	}
44982cd038dSYoshinobu Inoue 	if (nd_ra->nd_ra_retransmit)
45082cd038dSYoshinobu Inoue 		ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
451dd385660SXin LI 	if (nd_ra->nd_ra_curhoplimit) {
452dd385660SXin LI 		if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
45382cd038dSYoshinobu Inoue 			ndi->chlim = nd_ra->nd_ra_curhoplimit;
454dd385660SXin LI 		else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
455dd385660SXin LI 			log(LOG_ERR, "RA with a lower CurHopLimit sent from "
456dd385660SXin LI 			    "%s on %s (current = %d, received = %d). "
457dd385660SXin LI 			    "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
458dd385660SXin LI 			    if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
459dd385660SXin LI 		}
460dd385660SXin LI 	}
46182cd038dSYoshinobu Inoue 	dr = defrtrlist_update(&dr0);
462201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
463201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(ifp);
464201100c5SBjoern A. Zeeb #endif
46582cd038dSYoshinobu Inoue     }
46682cd038dSYoshinobu Inoue 
46782cd038dSYoshinobu Inoue 	/*
46882cd038dSYoshinobu Inoue 	 * prefix
46982cd038dSYoshinobu Inoue 	 */
47082cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_pi) {
47182cd038dSYoshinobu Inoue 		struct nd_opt_hdr *pt;
47233841545SHajimu UMEMOTO 		struct nd_opt_prefix_info *pi = NULL;
473743eee66SSUZUKI Shinsuke 		struct nd_prefixctl pr;
47482cd038dSYoshinobu Inoue 
47582cd038dSYoshinobu Inoue 		for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
47682cd038dSYoshinobu Inoue 		     pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
47782cd038dSYoshinobu Inoue 		     pt = (struct nd_opt_hdr *)((caddr_t)pt +
47882cd038dSYoshinobu Inoue 						(pt->nd_opt_len << 3))) {
47982cd038dSYoshinobu Inoue 			if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
48082cd038dSYoshinobu Inoue 				continue;
48182cd038dSYoshinobu Inoue 			pi = (struct nd_opt_prefix_info *)pt;
48282cd038dSYoshinobu Inoue 
48382cd038dSYoshinobu Inoue 			if (pi->nd_opt_pi_len != 4) {
48433841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
48533841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid option "
48682cd038dSYoshinobu Inoue 				    "len %d for prefix information option, "
48733841545SHajimu UMEMOTO 				    "ignored\n", pi->nd_opt_pi_len));
48882cd038dSYoshinobu Inoue 				continue;
48982cd038dSYoshinobu Inoue 			}
49082cd038dSYoshinobu Inoue 
49182cd038dSYoshinobu Inoue 			if (128 < pi->nd_opt_pi_prefix_len) {
49233841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
49333841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid prefix "
49482cd038dSYoshinobu Inoue 				    "len %d for prefix information option, "
49533841545SHajimu UMEMOTO 				    "ignored\n", pi->nd_opt_pi_prefix_len));
49682cd038dSYoshinobu Inoue 				continue;
49782cd038dSYoshinobu Inoue 			}
49882cd038dSYoshinobu Inoue 
49982cd038dSYoshinobu Inoue 			if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
50082cd038dSYoshinobu Inoue 			 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
50133841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
50233841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid prefix "
50382cd038dSYoshinobu Inoue 				    "%s, ignored\n",
5041d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6bufs,
5051d54aa3bSBjoern A. Zeeb 					&pi->nd_opt_pi_prefix)));
50682cd038dSYoshinobu Inoue 				continue;
50782cd038dSYoshinobu Inoue 			}
50882cd038dSYoshinobu Inoue 
50982cd038dSYoshinobu Inoue 			bzero(&pr, sizeof(pr));
51082cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_family = AF_INET6;
51182cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
51282cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
51382cd038dSYoshinobu Inoue 			pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
51482cd038dSYoshinobu Inoue 
51582cd038dSYoshinobu Inoue 			pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
51682cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
51782cd038dSYoshinobu Inoue 			pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
51882cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
51982cd038dSYoshinobu Inoue 			pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
52082cd038dSYoshinobu Inoue 			pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
52107eb2995SHajimu UMEMOTO 			pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
522743eee66SSUZUKI Shinsuke 			(void)prelist_update(&pr, dr, m, mcast);
52382cd038dSYoshinobu Inoue 		}
52482cd038dSYoshinobu Inoue 	}
5254de485feSMark Johnston 	if (dr != NULL) {
5264de485feSMark Johnston 		defrouter_rele(dr);
5274de485feSMark Johnston 		dr = NULL;
5284de485feSMark Johnston 	}
52982cd038dSYoshinobu Inoue 
53082cd038dSYoshinobu Inoue 	/*
53182cd038dSYoshinobu Inoue 	 * MTU
53282cd038dSYoshinobu Inoue 	 */
53382cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
53431b3783cSHajimu UMEMOTO 		u_long mtu;
53531b3783cSHajimu UMEMOTO 		u_long maxmtu;
53607eb2995SHajimu UMEMOTO 
53731b3783cSHajimu UMEMOTO 		mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
53882cd038dSYoshinobu Inoue 
53982cd038dSYoshinobu Inoue 		/* lower bound */
54082cd038dSYoshinobu Inoue 		if (mtu < IPV6_MMTU) {
54133841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
54231b3783cSHajimu UMEMOTO 			    "mtu=%lu sent from %s, ignoring\n",
5431d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
54482cd038dSYoshinobu Inoue 			goto skip;
54582cd038dSYoshinobu Inoue 		}
54682cd038dSYoshinobu Inoue 
54782cd038dSYoshinobu Inoue 		/* upper bound */
54831b3783cSHajimu UMEMOTO 		maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
54931b3783cSHajimu UMEMOTO 		    ? ndi->maxmtu : ifp->if_mtu;
55031b3783cSHajimu UMEMOTO 		if (mtu <= maxmtu) {
55182cd038dSYoshinobu Inoue 			int change = (ndi->linkmtu != mtu);
55282cd038dSYoshinobu Inoue 
55382cd038dSYoshinobu Inoue 			ndi->linkmtu = mtu;
5540bbfb20fSEric van Gyzen 			if (change) {
5550bbfb20fSEric van Gyzen 				/* in6_maxmtu may change */
55682cd038dSYoshinobu Inoue 				in6_setmaxmtu();
5570bbfb20fSEric van Gyzen 				rt_updatemtu(ifp);
5580bbfb20fSEric van Gyzen 			}
55982cd038dSYoshinobu Inoue 		} else {
56033841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
56131b3783cSHajimu UMEMOTO 			    "mtu=%lu sent from %s; "
56231b3783cSHajimu UMEMOTO 			    "exceeds maxmtu %lu, ignoring\n",
5631d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
56482cd038dSYoshinobu Inoue 		}
56582cd038dSYoshinobu Inoue 	}
56682cd038dSYoshinobu Inoue 
56782cd038dSYoshinobu Inoue  skip:
56882cd038dSYoshinobu Inoue 
56982cd038dSYoshinobu Inoue 	/*
57088ff5695SSUZUKI Shinsuke 	 * Source link layer address
57182cd038dSYoshinobu Inoue 	 */
57282cd038dSYoshinobu Inoue     {
57382cd038dSYoshinobu Inoue 	char *lladdr = NULL;
57482cd038dSYoshinobu Inoue 	int lladdrlen = 0;
57582cd038dSYoshinobu Inoue 
57682cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
57782cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
57882cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
57982cd038dSYoshinobu Inoue 	}
58082cd038dSYoshinobu Inoue 
58182cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
58233841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
58382cd038dSYoshinobu Inoue 		    "nd6_ra_input: lladdrlen mismatch for %s "
5841d54aa3bSBjoern A. Zeeb 		    "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
58507eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
58633841545SHajimu UMEMOTO 		goto bad;
58782cd038dSYoshinobu Inoue 	}
58882cd038dSYoshinobu Inoue 
58907eb2995SHajimu UMEMOTO 	nd6_cache_lladdr(ifp, &saddr6, lladdr,
59007eb2995SHajimu UMEMOTO 	    lladdrlen, ND_ROUTER_ADVERT, 0);
591686cdd19SJun-ichiro itojun Hagino 
592686cdd19SJun-ichiro itojun Hagino 	/*
593686cdd19SJun-ichiro itojun Hagino 	 * Installing a link-layer address might change the state of the
594686cdd19SJun-ichiro itojun Hagino 	 * router's neighbor cache, which might also affect our on-link
595686cdd19SJun-ichiro itojun Hagino 	 * detection of adveritsed prefixes.
596686cdd19SJun-ichiro itojun Hagino 	 */
597686cdd19SJun-ichiro itojun Hagino 	pfxlist_onlink_check();
59882cd038dSYoshinobu Inoue     }
599686cdd19SJun-ichiro itojun Hagino 
600686cdd19SJun-ichiro itojun Hagino  freeit:
601686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
60233841545SHajimu UMEMOTO 	return;
60333841545SHajimu UMEMOTO 
60433841545SHajimu UMEMOTO  bad:
605e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badra);
60633841545SHajimu UMEMOTO 	m_freem(m);
60782cd038dSYoshinobu Inoue }
60882cd038dSYoshinobu Inoue 
609686cdd19SJun-ichiro itojun Hagino /* tell the change to user processes watching the routing socket. */
610686cdd19SJun-ichiro itojun Hagino static void
6111272577eSXin LI nd6_rtmsg(int cmd, struct rtentry *rt)
612686cdd19SJun-ichiro itojun Hagino {
613686cdd19SJun-ichiro itojun Hagino 
614e02d3fe7SAlexander V. Chernikov 	rt_routemsg(cmd, rt, rt->rt_ifp, 0, rt->rt_fibnum);
615686cdd19SJun-ichiro itojun Hagino }
616686cdd19SJun-ichiro itojun Hagino 
617f77a6dbdSBjoern A. Zeeb /* PFXRTR */
618f77a6dbdSBjoern A. Zeeb static struct nd_pfxrouter *
619f77a6dbdSBjoern A. Zeeb pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
620f77a6dbdSBjoern A. Zeeb {
621f77a6dbdSBjoern A. Zeeb 	struct nd_pfxrouter *search;
6224de485feSMark Johnston 
623f77a6dbdSBjoern A. Zeeb 	ND6_LOCK_ASSERT();
624f77a6dbdSBjoern A. Zeeb 
625f77a6dbdSBjoern A. Zeeb 	LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
626f77a6dbdSBjoern A. Zeeb 		if (search->router == dr)
627f77a6dbdSBjoern A. Zeeb 			break;
628f77a6dbdSBjoern A. Zeeb 	}
629f77a6dbdSBjoern A. Zeeb 	return (search);
630f77a6dbdSBjoern A. Zeeb }
631f77a6dbdSBjoern A. Zeeb 
632f77a6dbdSBjoern A. Zeeb static void
633f77a6dbdSBjoern A. Zeeb pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
634f77a6dbdSBjoern A. Zeeb {
635f77a6dbdSBjoern A. Zeeb 	struct nd_pfxrouter *new;
636f77a6dbdSBjoern A. Zeeb 	bool update;
637f77a6dbdSBjoern A. Zeeb 
638f77a6dbdSBjoern A. Zeeb 	ND6_UNLOCK_ASSERT();
639f77a6dbdSBjoern A. Zeeb 
640f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
641f77a6dbdSBjoern A. Zeeb 	if (pfxrtr_lookup(pr, dr) != NULL) {
642f77a6dbdSBjoern A. Zeeb 		ND6_RUNLOCK();
643f77a6dbdSBjoern A. Zeeb 		return;
644f77a6dbdSBjoern A. Zeeb 	}
645f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
646f77a6dbdSBjoern A. Zeeb 
647f77a6dbdSBjoern A. Zeeb 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
648f77a6dbdSBjoern A. Zeeb 	if (new == NULL)
649f77a6dbdSBjoern A. Zeeb 		return;
650f77a6dbdSBjoern A. Zeeb 	defrouter_ref(dr);
651f77a6dbdSBjoern A. Zeeb 	new->router = dr;
652f77a6dbdSBjoern A. Zeeb 
653f77a6dbdSBjoern A. Zeeb 	ND6_WLOCK();
654f77a6dbdSBjoern A. Zeeb 	if (pfxrtr_lookup(pr, dr) == NULL) {
655f77a6dbdSBjoern A. Zeeb 		LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
656f77a6dbdSBjoern A. Zeeb 		update = true;
657f77a6dbdSBjoern A. Zeeb 	} else {
658f77a6dbdSBjoern A. Zeeb 		/* We lost a race to add the reference. */
659f77a6dbdSBjoern A. Zeeb 		defrouter_rele(dr);
660f77a6dbdSBjoern A. Zeeb 		free(new, M_IP6NDP);
661f77a6dbdSBjoern A. Zeeb 		update = false;
662f77a6dbdSBjoern A. Zeeb 	}
663f77a6dbdSBjoern A. Zeeb 	ND6_WUNLOCK();
664f77a6dbdSBjoern A. Zeeb 
665f77a6dbdSBjoern A. Zeeb 	if (update)
666f77a6dbdSBjoern A. Zeeb 		pfxlist_onlink_check();
667f77a6dbdSBjoern A. Zeeb }
668f77a6dbdSBjoern A. Zeeb 
669f77a6dbdSBjoern A. Zeeb static void
670f77a6dbdSBjoern A. Zeeb pfxrtr_del(struct nd_pfxrouter *pfr)
671f77a6dbdSBjoern A. Zeeb {
672f77a6dbdSBjoern A. Zeeb 
673f77a6dbdSBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
674f77a6dbdSBjoern A. Zeeb 
675f77a6dbdSBjoern A. Zeeb 	LIST_REMOVE(pfr, pfr_entry);
676f77a6dbdSBjoern A. Zeeb 	defrouter_rele(pfr->router);
677f77a6dbdSBjoern A. Zeeb 	free(pfr, M_IP6NDP);
678f77a6dbdSBjoern A. Zeeb }
679f77a6dbdSBjoern A. Zeeb 
680f77a6dbdSBjoern A. Zeeb 
681f77a6dbdSBjoern A. Zeeb /* Default router list processing sub routines. */
6829d9b92f2SBjoern A. Zeeb static void
6831272577eSXin LI defrouter_addreq(struct nd_defrouter *new)
68482cd038dSYoshinobu Inoue {
68582cd038dSYoshinobu Inoue 	struct sockaddr_in6 def, mask, gate;
686686cdd19SJun-ichiro itojun Hagino 	struct rtentry *newrt = NULL;
687743eee66SSUZUKI Shinsuke 	int error;
68882cd038dSYoshinobu Inoue 
689056c7327SLuigi Rizzo 	bzero(&def, sizeof(def));
690056c7327SLuigi Rizzo 	bzero(&mask, sizeof(mask));
691056c7327SLuigi Rizzo 	bzero(&gate, sizeof(gate));
69282cd038dSYoshinobu Inoue 
69307eb2995SHajimu UMEMOTO 	def.sin6_len = mask.sin6_len = gate.sin6_len =
69407eb2995SHajimu UMEMOTO 	    sizeof(struct sockaddr_in6);
695743eee66SSUZUKI Shinsuke 	def.sin6_family = gate.sin6_family = AF_INET6;
69682cd038dSYoshinobu Inoue 	gate.sin6_addr = new->rtaddr;
69782cd038dSYoshinobu Inoue 
69881d5d46bSBjoern A. Zeeb 	error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
69982cd038dSYoshinobu Inoue 	    (struct sockaddr *)&gate, (struct sockaddr *)&mask,
700559b4296SAlan Somers 	    RTF_GATEWAY, &newrt, new->ifp->if_fib);
701686cdd19SJun-ichiro itojun Hagino 	if (newrt) {
70233841545SHajimu UMEMOTO 		nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
7036e6b3f7cSQing Li 		RTFREE(newrt);
704686cdd19SJun-ichiro itojun Hagino 	}
705743eee66SSUZUKI Shinsuke 	if (error == 0)
706743eee66SSUZUKI Shinsuke 		new->installed = 1;
70782cd038dSYoshinobu Inoue }
70882cd038dSYoshinobu Inoue 
709743eee66SSUZUKI Shinsuke /*
710743eee66SSUZUKI Shinsuke  * Remove the default route for a given router.
711559b4296SAlan Somers  * This is just a subroutine function for defrouter_select_fib(), and
712559b4296SAlan Somers  * should not be called from anywhere else.
713743eee66SSUZUKI Shinsuke  */
714743eee66SSUZUKI Shinsuke static void
7151272577eSXin LI defrouter_delreq(struct nd_defrouter *dr)
71682cd038dSYoshinobu Inoue {
71782cd038dSYoshinobu Inoue 	struct sockaddr_in6 def, mask, gate;
718686cdd19SJun-ichiro itojun Hagino 	struct rtentry *oldrt = NULL;
71982cd038dSYoshinobu Inoue 
720056c7327SLuigi Rizzo 	bzero(&def, sizeof(def));
721056c7327SLuigi Rizzo 	bzero(&mask, sizeof(mask));
722056c7327SLuigi Rizzo 	bzero(&gate, sizeof(gate));
72382cd038dSYoshinobu Inoue 
72407eb2995SHajimu UMEMOTO 	def.sin6_len = mask.sin6_len = gate.sin6_len =
72507eb2995SHajimu UMEMOTO 	    sizeof(struct sockaddr_in6);
726743eee66SSUZUKI Shinsuke 	def.sin6_family = gate.sin6_family = AF_INET6;
72782cd038dSYoshinobu Inoue 	gate.sin6_addr = dr->rtaddr;
72882cd038dSYoshinobu Inoue 
72981d5d46bSBjoern A. Zeeb 	in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
73082cd038dSYoshinobu Inoue 	    (struct sockaddr *)&gate,
731559b4296SAlan Somers 	    (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, dr->ifp->if_fib);
732686cdd19SJun-ichiro itojun Hagino 	if (oldrt) {
73333841545SHajimu UMEMOTO 		nd6_rtmsg(RTM_DELETE, oldrt);
73471eba915SRuslan Ermilov 		RTFREE(oldrt);
73520220070SJun-ichiro itojun Hagino 	}
73682cd038dSYoshinobu Inoue 
737743eee66SSUZUKI Shinsuke 	dr->installed = 0;
738743eee66SSUZUKI Shinsuke }
739743eee66SSUZUKI Shinsuke 
740ad675b32SBjoern A. Zeeb static void
7414de485feSMark Johnston defrouter_del(struct nd_defrouter *dr)
74282cd038dSYoshinobu Inoue {
74382cd038dSYoshinobu Inoue 	struct nd_defrouter *deldr = NULL;
74482cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
745d748f7efSMark Johnston 	struct nd_pfxrouter *pfxrtr;
74682cd038dSYoshinobu Inoue 
7474de485feSMark Johnston 	ND6_UNLOCK_ASSERT();
7484de485feSMark Johnston 
74982cd038dSYoshinobu Inoue 	/*
75082cd038dSYoshinobu Inoue 	 * Flush all the routing table entries that use the router
75182cd038dSYoshinobu Inoue 	 * as a next hop.
75282cd038dSYoshinobu Inoue 	 */
753e7fa8d0aSHiroki Sato 	if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
75482cd038dSYoshinobu Inoue 		rt6_flush(&dr->rtaddr, dr->ifp);
75582cd038dSYoshinobu Inoue 
756201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
757201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(dr->ifp);
758201100c5SBjoern A. Zeeb #endif
759201100c5SBjoern A. Zeeb 
760743eee66SSUZUKI Shinsuke 	if (dr->installed) {
761743eee66SSUZUKI Shinsuke 		deldr = dr;
762743eee66SSUZUKI Shinsuke 		defrouter_delreq(dr);
763743eee66SSUZUKI Shinsuke 	}
76482cd038dSYoshinobu Inoue 
76582cd038dSYoshinobu Inoue 	/*
76682cd038dSYoshinobu Inoue 	 * Also delete all the pointers to the router in each prefix lists.
76782cd038dSYoshinobu Inoue 	 */
768d748f7efSMark Johnston 	ND6_WLOCK();
7693b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
77082cd038dSYoshinobu Inoue 		if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
77182cd038dSYoshinobu Inoue 			pfxrtr_del(pfxrtr);
77282cd038dSYoshinobu Inoue 	}
773d748f7efSMark Johnston 	ND6_WUNLOCK();
774d748f7efSMark Johnston 
77582cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
77682cd038dSYoshinobu Inoue 
77782cd038dSYoshinobu Inoue 	/*
778686cdd19SJun-ichiro itojun Hagino 	 * If the router is the primary one, choose a new one.
779559b4296SAlan Somers 	 * Note that defrouter_select_fib() will remove the current
780559b4296SAlan Somers          * gateway from the routing table.
78182cd038dSYoshinobu Inoue 	 */
78282cd038dSYoshinobu Inoue 	if (deldr)
783559b4296SAlan Somers 		defrouter_select_fib(deldr->ifp->if_fib);
784686cdd19SJun-ichiro itojun Hagino 
7854de485feSMark Johnston 	/*
7864de485feSMark Johnston 	 * Release the list reference.
7874de485feSMark Johnston 	 */
7884de485feSMark Johnston 	defrouter_rele(dr);
78982cd038dSYoshinobu Inoue }
79082cd038dSYoshinobu Inoue 
791ad675b32SBjoern A. Zeeb 
792f77a6dbdSBjoern A. Zeeb struct nd_defrouter *
793f77a6dbdSBjoern A. Zeeb defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
794f77a6dbdSBjoern A. Zeeb {
795f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
796f77a6dbdSBjoern A. Zeeb 
797f77a6dbdSBjoern A. Zeeb 	ND6_LOCK_ASSERT();
798f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
799f77a6dbdSBjoern A. Zeeb 		if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
800f77a6dbdSBjoern A. Zeeb 			defrouter_ref(dr);
801f77a6dbdSBjoern A. Zeeb 			return (dr);
802f77a6dbdSBjoern A. Zeeb 		}
803f77a6dbdSBjoern A. Zeeb 	return (NULL);
804f77a6dbdSBjoern A. Zeeb }
805f77a6dbdSBjoern A. Zeeb 
806f77a6dbdSBjoern A. Zeeb struct nd_defrouter *
807f77a6dbdSBjoern A. Zeeb defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
808f77a6dbdSBjoern A. Zeeb {
809f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
810f77a6dbdSBjoern A. Zeeb 
811f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
812f77a6dbdSBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
813f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
814f77a6dbdSBjoern A. Zeeb 	return (dr);
815f77a6dbdSBjoern A. Zeeb }
816f77a6dbdSBjoern A. Zeeb 
817f77a6dbdSBjoern A. Zeeb /*
818f77a6dbdSBjoern A. Zeeb  * Remove all default routes from default router list.
819f77a6dbdSBjoern A. Zeeb  */
820f77a6dbdSBjoern A. Zeeb void
821f77a6dbdSBjoern A. Zeeb defrouter_reset(void)
822f77a6dbdSBjoern A. Zeeb {
823f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr, **dra;
824f77a6dbdSBjoern A. Zeeb 	int count, i;
825f77a6dbdSBjoern A. Zeeb 
826f77a6dbdSBjoern A. Zeeb 	count = i = 0;
827f77a6dbdSBjoern A. Zeeb 
828f77a6dbdSBjoern A. Zeeb 	/*
829f77a6dbdSBjoern A. Zeeb 	 * We can't delete routes with the ND lock held, so make a copy of the
830f77a6dbdSBjoern A. Zeeb 	 * current default router list and use that when deleting routes.
831f77a6dbdSBjoern A. Zeeb 	 */
832f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
833f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
834f77a6dbdSBjoern A. Zeeb 		count++;
835f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
836f77a6dbdSBjoern A. Zeeb 
837f77a6dbdSBjoern A. Zeeb 	dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
838f77a6dbdSBjoern A. Zeeb 
839f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
840f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
841f77a6dbdSBjoern A. Zeeb 		if (i == count)
842f77a6dbdSBjoern A. Zeeb 			break;
843f77a6dbdSBjoern A. Zeeb 		defrouter_ref(dr);
844f77a6dbdSBjoern A. Zeeb 		dra[i++] = dr;
845f77a6dbdSBjoern A. Zeeb 	}
846f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
847f77a6dbdSBjoern A. Zeeb 
848f77a6dbdSBjoern A. Zeeb 	for (i = 0; i < count && dra[i] != NULL; i++) {
849f77a6dbdSBjoern A. Zeeb 		defrouter_delreq(dra[i]);
850f77a6dbdSBjoern A. Zeeb 		defrouter_rele(dra[i]);
851f77a6dbdSBjoern A. Zeeb 	}
852f77a6dbdSBjoern A. Zeeb 	free(dra, M_TEMP);
853f77a6dbdSBjoern A. Zeeb 
854f77a6dbdSBjoern A. Zeeb 	/*
855f77a6dbdSBjoern A. Zeeb 	 * XXX should we also nuke any default routers in the kernel, by
856f77a6dbdSBjoern A. Zeeb 	 * going through them by rtalloc1()?
857f77a6dbdSBjoern A. Zeeb 	 */
858f77a6dbdSBjoern A. Zeeb }
859f77a6dbdSBjoern A. Zeeb 
860ad675b32SBjoern A. Zeeb /*
861ad675b32SBjoern A. Zeeb  * Look up a matching default router list entry and remove it. Returns true if a
862ad675b32SBjoern A. Zeeb  * matching entry was found, false otherwise.
863ad675b32SBjoern A. Zeeb  */
864ad675b32SBjoern A. Zeeb bool
865ad675b32SBjoern A. Zeeb defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
866ad675b32SBjoern A. Zeeb {
867ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
868ad675b32SBjoern A. Zeeb 
869ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
870ad675b32SBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
871ad675b32SBjoern A. Zeeb 	if (dr == NULL) {
872ad675b32SBjoern A. Zeeb 		ND6_WUNLOCK();
873ad675b32SBjoern A. Zeeb 		return (false);
874ad675b32SBjoern A. Zeeb 	}
875ad675b32SBjoern A. Zeeb 
876ad675b32SBjoern A. Zeeb 	defrouter_unlink(dr, NULL);
877ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
878ad675b32SBjoern A. Zeeb 	defrouter_del(dr);
879ad675b32SBjoern A. Zeeb 	defrouter_rele(dr);
880ad675b32SBjoern A. Zeeb 	return (true);
881ad675b32SBjoern A. Zeeb }
882ad675b32SBjoern A. Zeeb 
883686cdd19SJun-ichiro itojun Hagino /*
884f77a6dbdSBjoern A. Zeeb  * for default router selection
885f77a6dbdSBjoern A. Zeeb  * regards router-preference field as a 2-bit signed integer
886f77a6dbdSBjoern A. Zeeb  */
887f77a6dbdSBjoern A. Zeeb static int
888f77a6dbdSBjoern A. Zeeb rtpref(struct nd_defrouter *dr)
889f77a6dbdSBjoern A. Zeeb {
890f77a6dbdSBjoern A. Zeeb 	switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
891f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_HIGH:
892f77a6dbdSBjoern A. Zeeb 		return (RTPREF_HIGH);
893f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_MEDIUM:
894f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_RSV:
895f77a6dbdSBjoern A. Zeeb 		return (RTPREF_MEDIUM);
896f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_LOW:
897f77a6dbdSBjoern A. Zeeb 		return (RTPREF_LOW);
898f77a6dbdSBjoern A. Zeeb 	default:
899f77a6dbdSBjoern A. Zeeb 		/*
900f77a6dbdSBjoern A. Zeeb 		 * This case should never happen.  If it did, it would mean a
901f77a6dbdSBjoern A. Zeeb 		 * serious bug of kernel internal.  We thus always bark here.
902f77a6dbdSBjoern A. Zeeb 		 * Or, can we even panic?
903f77a6dbdSBjoern A. Zeeb 		 */
904f77a6dbdSBjoern A. Zeeb 		log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
905f77a6dbdSBjoern A. Zeeb 		return (RTPREF_INVALID);
906f77a6dbdSBjoern A. Zeeb 	}
907f77a6dbdSBjoern A. Zeeb 	/* NOTREACHED */
908f77a6dbdSBjoern A. Zeeb }
909f77a6dbdSBjoern A. Zeeb 
910f77a6dbdSBjoern A. Zeeb /*
911743eee66SSUZUKI Shinsuke  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
912743eee66SSUZUKI Shinsuke  * draft-ietf-ipngwg-router-selection:
913743eee66SSUZUKI Shinsuke  * 1) Routers that are reachable or probably reachable should be preferred.
914743eee66SSUZUKI Shinsuke  *    If we have more than one (probably) reachable router, prefer ones
915743eee66SSUZUKI Shinsuke  *    with the highest router preference.
916686cdd19SJun-ichiro itojun Hagino  * 2) When no routers on the list are known to be reachable or
917686cdd19SJun-ichiro itojun Hagino  *    probably reachable, routers SHOULD be selected in a round-robin
918743eee66SSUZUKI Shinsuke  *    fashion, regardless of router preference values.
919686cdd19SJun-ichiro itojun Hagino  * 3) If the Default Router List is empty, assume that all
920686cdd19SJun-ichiro itojun Hagino  *    destinations are on-link.
921743eee66SSUZUKI Shinsuke  *
922743eee66SSUZUKI Shinsuke  * We assume nd_defrouter is sorted by router preference value.
923743eee66SSUZUKI Shinsuke  * Since the code below covers both with and without router preference cases,
924743eee66SSUZUKI Shinsuke  * we do not need to classify the cases by ifdef.
925743eee66SSUZUKI Shinsuke  *
926743eee66SSUZUKI Shinsuke  * At this moment, we do not try to install more than one default router,
927743eee66SSUZUKI Shinsuke  * even when the multipath routing is available, because we're not sure about
928743eee66SSUZUKI Shinsuke  * the benefits for stub hosts comparing to the risk of making the code
929743eee66SSUZUKI Shinsuke  * complicated and the possibility of introducing bugs.
930559b4296SAlan Somers  *
931559b4296SAlan Somers  * We maintain a single list of routers for multiple FIBs, only considering one
932559b4296SAlan Somers  * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
933559b4296SAlan Somers  * we do the whole thing multiple times.
934686cdd19SJun-ichiro itojun Hagino  */
935686cdd19SJun-ichiro itojun Hagino void
936559b4296SAlan Somers defrouter_select_fib(int fibnum)
937686cdd19SJun-ichiro itojun Hagino {
938a68cc388SGleb Smirnoff 	struct epoch_tracker et;
9394de485feSMark Johnston 	struct nd_defrouter *dr, *selected_dr, *installed_dr;
9406e6b3f7cSQing Li 	struct llentry *ln = NULL;
941686cdd19SJun-ichiro itojun Hagino 
942559b4296SAlan Somers 	if (fibnum == RT_ALL_FIBS) {
943559b4296SAlan Somers 		for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
944559b4296SAlan Somers 			defrouter_select_fib(fibnum);
945559b4296SAlan Somers 		}
946559b4296SAlan Somers 	}
947559b4296SAlan Somers 
9484de485feSMark Johnston 	ND6_RLOCK();
949686cdd19SJun-ichiro itojun Hagino 	/*
950743eee66SSUZUKI Shinsuke 	 * Let's handle easy case (3) first:
951743eee66SSUZUKI Shinsuke 	 * If default router list is empty, there's nothing to be done.
952743eee66SSUZUKI Shinsuke 	 */
953f592d0c3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_nd6_defrouter)) {
9544de485feSMark Johnston 		ND6_RUNLOCK();
955743eee66SSUZUKI Shinsuke 		return;
9564de485feSMark Johnston 	}
957743eee66SSUZUKI Shinsuke 
958743eee66SSUZUKI Shinsuke 	/*
959686cdd19SJun-ichiro itojun Hagino 	 * Search for a (probably) reachable router from the list.
960743eee66SSUZUKI Shinsuke 	 * We just pick up the first reachable one (if any), assuming that
961743eee66SSUZUKI Shinsuke 	 * the ordering rule of the list described in defrtrlist_update().
962686cdd19SJun-ichiro itojun Hagino 	 */
9634de485feSMark Johnston 	selected_dr = installed_dr = NULL;
964f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
965a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
966559b4296SAlan Somers 		if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
9676e6b3f7cSQing Li 		    (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
968686cdd19SJun-ichiro itojun Hagino 		    ND6_IS_LLINFO_PROBREACH(ln)) {
969743eee66SSUZUKI Shinsuke 			selected_dr = dr;
9704de485feSMark Johnston 			defrouter_ref(selected_dr);
971686cdd19SJun-ichiro itojun Hagino 		}
972a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
9735f16e341SBjoern A. Zeeb 		if (ln != NULL) {
974c0641cc0SKip Macy 			LLE_RUNLOCK(ln);
9755f16e341SBjoern A. Zeeb 			ln = NULL;
9765f16e341SBjoern A. Zeeb 		}
977686cdd19SJun-ichiro itojun Hagino 
978559b4296SAlan Somers 		if (dr->installed && dr->ifp->if_fib == fibnum) {
9794de485feSMark Johnston 			if (installed_dr == NULL) {
980743eee66SSUZUKI Shinsuke 				installed_dr = dr;
9814de485feSMark Johnston 				defrouter_ref(installed_dr);
9824de485feSMark Johnston 			} else {
983559b4296SAlan Somers 				/*
984559b4296SAlan Somers 				 * this should not happen.
985559b4296SAlan Somers 				 * warn for diagnosis.
986559b4296SAlan Somers 				 */
987559b4296SAlan Somers 				log(LOG_ERR, "defrouter_select_fib: more than "
988559b4296SAlan Somers 				             "one router is installed\n");
9894de485feSMark Johnston 			}
99033841545SHajimu UMEMOTO 		}
991686cdd19SJun-ichiro itojun Hagino 	}
992743eee66SSUZUKI Shinsuke 	/*
993743eee66SSUZUKI Shinsuke 	 * If none of the default routers was found to be reachable,
994743eee66SSUZUKI Shinsuke 	 * round-robin the list regardless of preference.
995743eee66SSUZUKI Shinsuke 	 * Otherwise, if we have an installed router, check if the selected
996743eee66SSUZUKI Shinsuke 	 * (reachable) router should really be preferred to the installed one.
997743eee66SSUZUKI Shinsuke 	 * We only prefer the new router when the old one is not reachable
998743eee66SSUZUKI Shinsuke 	 * or when the new one has a really higher preference value.
999743eee66SSUZUKI Shinsuke 	 */
1000743eee66SSUZUKI Shinsuke 	if (selected_dr == NULL) {
10014de485feSMark Johnston 		if (installed_dr == NULL ||
10024de485feSMark Johnston 		    TAILQ_NEXT(installed_dr, dr_entry) == NULL)
1003f592d0c3SBjoern A. Zeeb 			dr = TAILQ_FIRST(&V_nd6_defrouter);
1004743eee66SSUZUKI Shinsuke 		else
1005559b4296SAlan Somers 			dr = TAILQ_NEXT(installed_dr, dr_entry);
1006559b4296SAlan Somers 
1007559b4296SAlan Somers 		/* Ensure we select a router for this FIB. */
1008f592d0c3SBjoern A. Zeeb 		TAILQ_FOREACH_FROM(dr, &V_nd6_defrouter, dr_entry) {
1009559b4296SAlan Somers 			if (dr->ifp->if_fib == fibnum) {
1010559b4296SAlan Somers 				selected_dr = dr;
10114de485feSMark Johnston 				defrouter_ref(selected_dr);
1012559b4296SAlan Somers 				break;
1013559b4296SAlan Somers 			}
1014559b4296SAlan Somers 		}
10154de485feSMark Johnston 	} else if (installed_dr != NULL) {
1016a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
1017559b4296SAlan Somers 		if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
1018559b4296SAlan Somers 		                     installed_dr->ifp)) &&
1019743eee66SSUZUKI Shinsuke 		    ND6_IS_LLINFO_PROBREACH(ln) &&
1020559b4296SAlan Somers 		    installed_dr->ifp->if_fib == fibnum &&
1021743eee66SSUZUKI Shinsuke 		    rtpref(selected_dr) <= rtpref(installed_dr)) {
10224de485feSMark Johnston 			defrouter_rele(selected_dr);
1023743eee66SSUZUKI Shinsuke 			selected_dr = installed_dr;
1024743eee66SSUZUKI Shinsuke 		}
1025a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1026c0641cc0SKip Macy 		if (ln != NULL)
1027c0641cc0SKip Macy 			LLE_RUNLOCK(ln);
10286e6b3f7cSQing Li 	}
10294de485feSMark Johnston 	ND6_RUNLOCK();
1030743eee66SSUZUKI Shinsuke 
1031743eee66SSUZUKI Shinsuke 	/*
1032559b4296SAlan Somers 	 * If we selected a router for this FIB and it's different
1033559b4296SAlan Somers 	 * than the installed one, remove the installed router and
1034559b4296SAlan Somers 	 * install the selected one in its place.
1035743eee66SSUZUKI Shinsuke 	 */
1036743eee66SSUZUKI Shinsuke 	if (installed_dr != selected_dr) {
10374de485feSMark Johnston 		if (installed_dr != NULL) {
1038743eee66SSUZUKI Shinsuke 			defrouter_delreq(installed_dr);
10394de485feSMark Johnston 			defrouter_rele(installed_dr);
10404de485feSMark Johnston 		}
1041559b4296SAlan Somers 		if (selected_dr != NULL)
1042743eee66SSUZUKI Shinsuke 			defrouter_addreq(selected_dr);
1043686cdd19SJun-ichiro itojun Hagino 	}
1044559b4296SAlan Somers 	if (selected_dr != NULL)
10454de485feSMark Johnston 		defrouter_rele(selected_dr);
1046686cdd19SJun-ichiro itojun Hagino }
1047686cdd19SJun-ichiro itojun Hagino 
104882cd038dSYoshinobu Inoue static struct nd_defrouter *
10491272577eSXin LI defrtrlist_update(struct nd_defrouter *new)
105082cd038dSYoshinobu Inoue {
105182cd038dSYoshinobu Inoue 	struct nd_defrouter *dr, *n;
1052d748f7efSMark Johnston 	uint64_t genid;
1053a4a7c825SMark Johnston 	int oldpref;
1054d748f7efSMark Johnston 	bool writelocked;
105582cd038dSYoshinobu Inoue 
105682cd038dSYoshinobu Inoue 	if (new->rtlifetime == 0) {
1057ff63037dSMark Johnston 		defrouter_remove(&new->rtaddr, new->ifp);
1058a4a7c825SMark Johnston 		return (NULL);
1059a4a7c825SMark Johnston 	}
1060a4a7c825SMark Johnston 
1061d748f7efSMark Johnston 	ND6_RLOCK();
1062d748f7efSMark Johnston 	writelocked = false;
1063d748f7efSMark Johnston restart:
1064ff63037dSMark Johnston 	dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
1065ff63037dSMark Johnston 	if (dr != NULL) {
1066a4a7c825SMark Johnston 		oldpref = rtpref(dr);
1067743eee66SSUZUKI Shinsuke 
106882cd038dSYoshinobu Inoue 		/* override */
106901869be5SMark Johnston 		dr->raflags = new->raflags; /* XXX flag check */
107082cd038dSYoshinobu Inoue 		dr->rtlifetime = new->rtlifetime;
107182cd038dSYoshinobu Inoue 		dr->expire = new->expire;
1072743eee66SSUZUKI Shinsuke 
1073743eee66SSUZUKI Shinsuke 		/*
1074743eee66SSUZUKI Shinsuke 		 * If the preference does not change, there's no need
10750f1aca65SQing Li 		 * to sort the entries. Also make sure the selected
10760f1aca65SQing Li 		 * router is still installed in the kernel.
1077743eee66SSUZUKI Shinsuke 		 */
10784de485feSMark Johnston 		if (dr->installed && rtpref(new) == oldpref) {
1079d748f7efSMark Johnston 			if (writelocked)
10804de485feSMark Johnston 				ND6_WUNLOCK();
1081d748f7efSMark Johnston 			else
1082d748f7efSMark Johnston 				ND6_RUNLOCK();
1083743eee66SSUZUKI Shinsuke 			return (dr);
10844de485feSMark Johnston 		}
1085d748f7efSMark Johnston 	}
1086743eee66SSUZUKI Shinsuke 
1087743eee66SSUZUKI Shinsuke 	/*
1088d748f7efSMark Johnston 	 * The router needs to be reinserted into the default router
1089d748f7efSMark Johnston 	 * list, so upgrade to a write lock. If that fails and the list
1090d748f7efSMark Johnston 	 * has potentially changed while the lock was dropped, we'll
1091d748f7efSMark Johnston 	 * redo the lookup with the write lock held.
1092d748f7efSMark Johnston 	 */
1093d748f7efSMark Johnston 	if (!writelocked) {
1094d748f7efSMark Johnston 		writelocked = true;
1095d748f7efSMark Johnston 		if (!ND6_TRY_UPGRADE()) {
1096d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1097d748f7efSMark Johnston 			ND6_RUNLOCK();
1098d748f7efSMark Johnston 			ND6_WLOCK();
1099d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1100d748f7efSMark Johnston 				goto restart;
1101d748f7efSMark Johnston 		}
1102d748f7efSMark Johnston 	}
1103d748f7efSMark Johnston 
1104d748f7efSMark Johnston 	if (dr != NULL) {
1105d748f7efSMark Johnston 		/*
1106a4a7c825SMark Johnston 		 * The preferred router may have changed, so relocate this
1107a4a7c825SMark Johnston 		 * router.
1108743eee66SSUZUKI Shinsuke 		 */
1109f592d0c3SBjoern A. Zeeb 		TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
1110743eee66SSUZUKI Shinsuke 		n = dr;
1111ff63037dSMark Johnston 	} else {
1112fc315641SMark Johnston 		n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
11134de485feSMark Johnston 		if (n == NULL) {
11144de485feSMark Johnston 			ND6_WUNLOCK();
111582cd038dSYoshinobu Inoue 			return (NULL);
11164de485feSMark Johnston 		}
1117fc315641SMark Johnston 		memcpy(n, new, sizeof(*n));
11184de485feSMark Johnston 		/* Initialize with an extra reference for the caller. */
11194de485feSMark Johnston 		refcount_init(&n->refcnt, 2);
1120ff63037dSMark Johnston 	}
1121686cdd19SJun-ichiro itojun Hagino 
1122686cdd19SJun-ichiro itojun Hagino 	/*
1123743eee66SSUZUKI Shinsuke 	 * Insert the new router in the Default Router List;
1124743eee66SSUZUKI Shinsuke 	 * The Default Router List should be in the descending order
1125743eee66SSUZUKI Shinsuke 	 * of router-preferece.  Routers with the same preference are
1126743eee66SSUZUKI Shinsuke 	 * sorted in the arriving time order.
1127686cdd19SJun-ichiro itojun Hagino 	 */
1128743eee66SSUZUKI Shinsuke 
1129743eee66SSUZUKI Shinsuke 	/* insert at the end of the group */
1130f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1131743eee66SSUZUKI Shinsuke 		if (rtpref(n) > rtpref(dr))
1132743eee66SSUZUKI Shinsuke 			break;
1133743eee66SSUZUKI Shinsuke 	}
11344de485feSMark Johnston 	if (dr != NULL)
1135743eee66SSUZUKI Shinsuke 		TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1136743eee66SSUZUKI Shinsuke 	else
1137f592d0c3SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(&V_nd6_defrouter, n, dr_entry);
1138d748f7efSMark Johnston 	V_nd6_list_genid++;
11394de485feSMark Johnston 	ND6_WUNLOCK();
1140743eee66SSUZUKI Shinsuke 
1141559b4296SAlan Somers 	defrouter_select_fib(new->ifp->if_fib);
1142743eee66SSUZUKI Shinsuke 
114382cd038dSYoshinobu Inoue 	return (n);
114482cd038dSYoshinobu Inoue }
114582cd038dSYoshinobu Inoue 
1146f77a6dbdSBjoern A. Zeeb static int
1147f77a6dbdSBjoern A. Zeeb in6_init_prefix_ltimes(struct nd_prefix *ndpr)
114882cd038dSYoshinobu Inoue {
1149f77a6dbdSBjoern A. Zeeb 	if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1150f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_preferred = 0;
1151f77a6dbdSBjoern A. Zeeb 	else
1152f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
1153f77a6dbdSBjoern A. Zeeb 	if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1154f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_expire = 0;
1155f77a6dbdSBjoern A. Zeeb 	else
1156f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
115782cd038dSYoshinobu Inoue 
1158f77a6dbdSBjoern A. Zeeb 	return 0;
115982cd038dSYoshinobu Inoue }
116082cd038dSYoshinobu Inoue 
116182cd038dSYoshinobu Inoue static void
1162f77a6dbdSBjoern A. Zeeb in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
116382cd038dSYoshinobu Inoue {
1164f77a6dbdSBjoern A. Zeeb 	/* init ia6t_expire */
1165f77a6dbdSBjoern A. Zeeb 	if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1166f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire = 0;
1167f77a6dbdSBjoern A. Zeeb 	else {
1168f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire = time_uptime;
1169f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire += lt6->ia6t_vltime;
117082cd038dSYoshinobu Inoue 	}
117182cd038dSYoshinobu Inoue 
1172f77a6dbdSBjoern A. Zeeb 	/* init ia6t_preferred */
1173f77a6dbdSBjoern A. Zeeb 	if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1174f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred = 0;
1175f77a6dbdSBjoern A. Zeeb 	else {
1176f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred = time_uptime;
1177f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred += lt6->ia6t_pltime;
1178f77a6dbdSBjoern A. Zeeb 	}
1179f77a6dbdSBjoern A. Zeeb }
1180f77a6dbdSBjoern A. Zeeb 
1181f77a6dbdSBjoern A. Zeeb static struct in6_ifaddr *
1182f77a6dbdSBjoern A. Zeeb in6_ifadd(struct nd_prefixctl *pr, int mcast)
118382cd038dSYoshinobu Inoue {
1184f77a6dbdSBjoern A. Zeeb 	struct ifnet *ifp = pr->ndpr_ifp;
1185f77a6dbdSBjoern A. Zeeb 	struct ifaddr *ifa;
1186f77a6dbdSBjoern A. Zeeb 	struct in6_aliasreq ifra;
1187f77a6dbdSBjoern A. Zeeb 	struct in6_ifaddr *ia, *ib;
1188f77a6dbdSBjoern A. Zeeb 	int error, plen0;
1189f77a6dbdSBjoern A. Zeeb 	struct in6_addr mask;
1190f77a6dbdSBjoern A. Zeeb 	int prefixlen = pr->ndpr_plen;
1191f77a6dbdSBjoern A. Zeeb 	int updateflags;
1192f77a6dbdSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
11934de485feSMark Johnston 
1194f77a6dbdSBjoern A. Zeeb 	in6_prefixlen2mask(&mask, prefixlen);
1195d748f7efSMark Johnston 
1196f77a6dbdSBjoern A. Zeeb 	/*
1197f77a6dbdSBjoern A. Zeeb 	 * find a link-local address (will be interface ID).
1198f77a6dbdSBjoern A. Zeeb 	 * Is it really mandatory? Theoretically, a global or a site-local
1199f77a6dbdSBjoern A. Zeeb 	 * address can be configured without a link-local address, if we
1200f77a6dbdSBjoern A. Zeeb 	 * have a unique interface identifier...
1201f77a6dbdSBjoern A. Zeeb 	 *
1202f77a6dbdSBjoern A. Zeeb 	 * it is not mandatory to have a link-local address, we can generate
1203f77a6dbdSBjoern A. Zeeb 	 * interface identifier on the fly.  we do this because:
1204f77a6dbdSBjoern A. Zeeb 	 * (1) it should be the easiest way to find interface identifier.
1205f77a6dbdSBjoern A. Zeeb 	 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1206f77a6dbdSBjoern A. Zeeb 	 * for multiple addresses on a single interface, and possible shortcut
1207f77a6dbdSBjoern A. Zeeb 	 * of DAD.  we omitted DAD for this reason in the past.
1208f77a6dbdSBjoern A. Zeeb 	 * (3) a user can prevent autoconfiguration of global address
1209f77a6dbdSBjoern A. Zeeb 	 * by removing link-local address by hand (this is partly because we
1210f77a6dbdSBjoern A. Zeeb 	 * don't have other way to control the use of IPv6 on an interface.
1211f77a6dbdSBjoern A. Zeeb 	 * this has been our design choice - cf. NRL's "ifconfig auto").
1212f77a6dbdSBjoern A. Zeeb 	 * (4) it is easier to manage when an interface has addresses
1213f77a6dbdSBjoern A. Zeeb 	 * with the same interface identifier, than to have multiple addresses
1214f77a6dbdSBjoern A. Zeeb 	 * with different interface identifiers.
1215f77a6dbdSBjoern A. Zeeb 	 */
1216f77a6dbdSBjoern A. Zeeb 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1217f77a6dbdSBjoern A. Zeeb 	if (ifa)
1218f77a6dbdSBjoern A. Zeeb 		ib = (struct in6_ifaddr *)ifa;
1219f77a6dbdSBjoern A. Zeeb 	else
1220f77a6dbdSBjoern A. Zeeb 		return NULL;
1221f77a6dbdSBjoern A. Zeeb 
1222f77a6dbdSBjoern A. Zeeb 	/* prefixlen + ifidlen must be equal to 128 */
1223f77a6dbdSBjoern A. Zeeb 	plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1224f77a6dbdSBjoern A. Zeeb 	if (prefixlen != plen0) {
1225f77a6dbdSBjoern A. Zeeb 		ifa_free(ifa);
1226f77a6dbdSBjoern A. Zeeb 		nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1227f77a6dbdSBjoern A. Zeeb 		    "(prefix=%d ifid=%d)\n",
1228f77a6dbdSBjoern A. Zeeb 		    if_name(ifp), prefixlen, 128 - plen0));
1229f77a6dbdSBjoern A. Zeeb 		return NULL;
1230f77a6dbdSBjoern A. Zeeb 	}
1231f77a6dbdSBjoern A. Zeeb 
1232f77a6dbdSBjoern A. Zeeb 	/* make ifaddr */
1233f77a6dbdSBjoern A. Zeeb 	in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
1234f77a6dbdSBjoern A. Zeeb 
1235f77a6dbdSBjoern A. Zeeb 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
1236f77a6dbdSBjoern A. Zeeb 	/* interface ID */
1237f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1238f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1239f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1240f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1241f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1242f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1243f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1244f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1245f77a6dbdSBjoern A. Zeeb 	ifa_free(ifa);
1246f77a6dbdSBjoern A. Zeeb 
1247f77a6dbdSBjoern A. Zeeb 	/* lifetimes. */
1248f77a6dbdSBjoern A. Zeeb 	ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1249f77a6dbdSBjoern A. Zeeb 	ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1250f77a6dbdSBjoern A. Zeeb 
1251f77a6dbdSBjoern A. Zeeb 	/* XXX: scope zone ID? */
1252f77a6dbdSBjoern A. Zeeb 
1253f77a6dbdSBjoern A. Zeeb 	ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1254f77a6dbdSBjoern A. Zeeb 
1255f77a6dbdSBjoern A. Zeeb 	/*
1256f77a6dbdSBjoern A. Zeeb 	 * Make sure that we do not have this address already.  This should
1257f77a6dbdSBjoern A. Zeeb 	 * usually not happen, but we can still see this case, e.g., if we
1258f77a6dbdSBjoern A. Zeeb 	 * have manually configured the exact address to be configured.
1259f77a6dbdSBjoern A. Zeeb 	 */
1260f77a6dbdSBjoern A. Zeeb 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1261f77a6dbdSBjoern A. Zeeb 	    &ifra.ifra_addr.sin6_addr);
1262f77a6dbdSBjoern A. Zeeb 	if (ifa != NULL) {
1263f77a6dbdSBjoern A. Zeeb 		ifa_free(ifa);
1264f77a6dbdSBjoern A. Zeeb 		/* this should be rare enough to make an explicit log */
1265f77a6dbdSBjoern A. Zeeb 		log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1266f77a6dbdSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1267f77a6dbdSBjoern A. Zeeb 		return (NULL);
1268f77a6dbdSBjoern A. Zeeb 	}
1269f77a6dbdSBjoern A. Zeeb 
1270f77a6dbdSBjoern A. Zeeb 	/*
1271f77a6dbdSBjoern A. Zeeb 	 * Allocate ifaddr structure, link into chain, etc.
1272f77a6dbdSBjoern A. Zeeb 	 * If we are going to create a new address upon receiving a multicasted
1273f77a6dbdSBjoern A. Zeeb 	 * RA, we need to impose a random delay before starting DAD.
1274f77a6dbdSBjoern A. Zeeb 	 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1275f77a6dbdSBjoern A. Zeeb 	 */
1276f77a6dbdSBjoern A. Zeeb 	updateflags = 0;
1277f77a6dbdSBjoern A. Zeeb 	if (mcast)
1278f77a6dbdSBjoern A. Zeeb 		updateflags |= IN6_IFAUPDATE_DADDELAY;
1279f77a6dbdSBjoern A. Zeeb 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1280f77a6dbdSBjoern A. Zeeb 		nd6log((LOG_ERR,
1281f77a6dbdSBjoern A. Zeeb 		    "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1282f77a6dbdSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1283f77a6dbdSBjoern A. Zeeb 		    if_name(ifp), error));
1284f77a6dbdSBjoern A. Zeeb 		return (NULL);	/* ifaddr must not have been allocated. */
1285f77a6dbdSBjoern A. Zeeb 	}
1286f77a6dbdSBjoern A. Zeeb 
1287f77a6dbdSBjoern A. Zeeb 	ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1288f77a6dbdSBjoern A. Zeeb 	/*
1289f77a6dbdSBjoern A. Zeeb 	 * XXXRW: Assumption of non-NULLness here might not be true with
1290f77a6dbdSBjoern A. Zeeb 	 * fine-grained locking -- should we validate it?  Or just return
1291f77a6dbdSBjoern A. Zeeb 	 * earlier ifa rather than looking it up again?
1292f77a6dbdSBjoern A. Zeeb 	 */
1293f77a6dbdSBjoern A. Zeeb 	return (ia);		/* this is always non-NULL  and referenced. */
129482cd038dSYoshinobu Inoue }
129582cd038dSYoshinobu Inoue 
1296d748f7efSMark Johnston static struct nd_prefix *
1297d748f7efSMark Johnston nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1298d748f7efSMark Johnston {
1299d748f7efSMark Johnston 	struct nd_prefix *search;
1300d748f7efSMark Johnston 
1301d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1302d748f7efSMark Johnston 
1303d748f7efSMark Johnston 	LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1304d748f7efSMark Johnston 		if (key->ndpr_ifp == search->ndpr_ifp &&
1305d748f7efSMark Johnston 		    key->ndpr_plen == search->ndpr_plen &&
1306d748f7efSMark Johnston 		    in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1307d748f7efSMark Johnston 		    &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1308d748f7efSMark Johnston 			nd6_prefix_ref(search);
1309d748f7efSMark Johnston 			break;
1310d748f7efSMark Johnston 		}
1311d748f7efSMark Johnston 	}
1312d748f7efSMark Johnston 	return (search);
1313d748f7efSMark Johnston }
1314d748f7efSMark Johnston 
131533841545SHajimu UMEMOTO struct nd_prefix *
13161272577eSXin LI nd6_prefix_lookup(struct nd_prefixctl *key)
131782cd038dSYoshinobu Inoue {
131882cd038dSYoshinobu Inoue 	struct nd_prefix *search;
131982cd038dSYoshinobu Inoue 
1320d748f7efSMark Johnston 	ND6_RLOCK();
1321d748f7efSMark Johnston 	search = nd6_prefix_lookup_locked(key);
1322d748f7efSMark Johnston 	ND6_RUNLOCK();
1323d748f7efSMark Johnston 	return (search);
132482cd038dSYoshinobu Inoue }
132582cd038dSYoshinobu Inoue 
1326d748f7efSMark Johnston void
1327d748f7efSMark Johnston nd6_prefix_ref(struct nd_prefix *pr)
1328d748f7efSMark Johnston {
1329d748f7efSMark Johnston 
1330d748f7efSMark Johnston 	refcount_acquire(&pr->ndpr_refcnt);
1331d748f7efSMark Johnston }
1332d748f7efSMark Johnston 
1333d748f7efSMark Johnston void
1334d748f7efSMark Johnston nd6_prefix_rele(struct nd_prefix *pr)
1335d748f7efSMark Johnston {
1336d748f7efSMark Johnston 
1337d748f7efSMark Johnston 	if (refcount_release(&pr->ndpr_refcnt)) {
1338d748f7efSMark Johnston 		KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1339d748f7efSMark Johnston 		    ("prefix %p has advertising routers", pr));
1340d748f7efSMark Johnston 		free(pr, M_IP6NDP);
1341d748f7efSMark Johnston 	}
134282cd038dSYoshinobu Inoue }
134382cd038dSYoshinobu Inoue 
134433841545SHajimu UMEMOTO int
13451272577eSXin LI nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
13461272577eSXin LI     struct nd_prefix **newp)
134782cd038dSYoshinobu Inoue {
1348d748f7efSMark Johnston 	struct nd_prefix *new;
13491d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1350d748f7efSMark Johnston 	int error;
135182cd038dSYoshinobu Inoue 
1352fc315641SMark Johnston 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
135382cd038dSYoshinobu Inoue 	if (new == NULL)
135407eb2995SHajimu UMEMOTO 		return (ENOMEM);
1355d748f7efSMark Johnston 	refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1356743eee66SSUZUKI Shinsuke 	new->ndpr_ifp = pr->ndpr_ifp;
1357743eee66SSUZUKI Shinsuke 	new->ndpr_prefix = pr->ndpr_prefix;
1358743eee66SSUZUKI Shinsuke 	new->ndpr_plen = pr->ndpr_plen;
1359743eee66SSUZUKI Shinsuke 	new->ndpr_vltime = pr->ndpr_vltime;
1360743eee66SSUZUKI Shinsuke 	new->ndpr_pltime = pr->ndpr_pltime;
1361743eee66SSUZUKI Shinsuke 	new->ndpr_flags = pr->ndpr_flags;
1362743eee66SSUZUKI Shinsuke 	if ((error = in6_init_prefix_ltimes(new)) != 0) {
1363743eee66SSUZUKI Shinsuke 		free(new, M_IP6NDP);
1364743eee66SSUZUKI Shinsuke 		return (error);
1365743eee66SSUZUKI Shinsuke 	}
13667d26db17SHiroki Sato 	new->ndpr_lastupdate = time_uptime;
136782cd038dSYoshinobu Inoue 
136807eb2995SHajimu UMEMOTO 	/* initialization */
136982cd038dSYoshinobu Inoue 	LIST_INIT(&new->ndpr_advrtrs);
137082cd038dSYoshinobu Inoue 	in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
137182cd038dSYoshinobu Inoue 	/* make prefix in the canonical form */
13721dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
137382cd038dSYoshinobu Inoue 
1374d748f7efSMark Johnston 	ND6_WLOCK();
1375603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1376d748f7efSMark Johnston 	V_nd6_list_genid++;
1377d748f7efSMark Johnston 	ND6_WUNLOCK();
137882cd038dSYoshinobu Inoue 
137933841545SHajimu UMEMOTO 	/* ND_OPT_PI_FLAG_ONLINK processing */
138033841545SHajimu UMEMOTO 	if (new->ndpr_raf_onlink) {
1381b8a6e03fSGleb Smirnoff 		struct epoch_tracker et;
1382b8a6e03fSGleb Smirnoff 
1383d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1384b8a6e03fSGleb Smirnoff 		NET_EPOCH_ENTER(et);
1385d748f7efSMark Johnston 		if ((error = nd6_prefix_onlink(new)) != 0) {
138633841545SHajimu UMEMOTO 			nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
138733841545SHajimu UMEMOTO 			    "the prefix %s/%d on-link on %s (errno=%d)\n",
13881d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1389d748f7efSMark Johnston 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
139033841545SHajimu UMEMOTO 			/* proceed anyway. XXX: is it correct? */
139133841545SHajimu UMEMOTO 		}
1392b8a6e03fSGleb Smirnoff 		NET_EPOCH_EXIT(et);
1393d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
139433841545SHajimu UMEMOTO 	}
139533841545SHajimu UMEMOTO 
139620b5f022SMark Johnston 	if (dr != NULL)
139782cd038dSYoshinobu Inoue 		pfxrtr_add(new, dr);
139820b5f022SMark Johnston 	if (newp != NULL)
139920b5f022SMark Johnston 		*newp = new;
140020b5f022SMark Johnston 	return (0);
140182cd038dSYoshinobu Inoue }
140282cd038dSYoshinobu Inoue 
1403d748f7efSMark Johnston /*
1404d748f7efSMark Johnston  * Remove a prefix from the prefix list and optionally stash it in a
1405d748f7efSMark Johnston  * caller-provided list.
1406d748f7efSMark Johnston  *
1407d748f7efSMark Johnston  * The ND6 lock must be held.
1408d748f7efSMark Johnston  */
140982cd038dSYoshinobu Inoue void
1410d748f7efSMark Johnston nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1411d748f7efSMark Johnston {
1412d748f7efSMark Johnston 
1413d748f7efSMark Johnston 	ND6_WLOCK_ASSERT();
1414d748f7efSMark Johnston 
1415d748f7efSMark Johnston 	LIST_REMOVE(pr, ndpr_entry);
1416d748f7efSMark Johnston 	V_nd6_list_genid++;
1417d748f7efSMark Johnston 	if (list != NULL)
1418d748f7efSMark Johnston 		LIST_INSERT_HEAD(list, pr, ndpr_entry);
1419d748f7efSMark Johnston }
1420d748f7efSMark Johnston 
1421d748f7efSMark Johnston /*
1422d748f7efSMark Johnston  * Free an unlinked prefix, first marking it off-link if necessary.
1423d748f7efSMark Johnston  */
1424d748f7efSMark Johnston void
1425d748f7efSMark Johnston nd6_prefix_del(struct nd_prefix *pr)
142682cd038dSYoshinobu Inoue {
142782cd038dSYoshinobu Inoue 	struct nd_pfxrouter *pfr, *next;
1428c9b652e3SAndre Oppermann 	int e;
14291d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
143033841545SHajimu UMEMOTO 
1431d748f7efSMark Johnston 	KASSERT(pr->ndpr_addrcnt == 0,
1432d748f7efSMark Johnston 	    ("prefix %p has referencing addresses", pr));
1433d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
1434743eee66SSUZUKI Shinsuke 
143533841545SHajimu UMEMOTO 	/*
143633841545SHajimu UMEMOTO 	 * Though these flags are now meaningless, we'd rather keep the value
14377aa59493SSUZUKI Shinsuke 	 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
14387aa59493SSUZUKI Shinsuke 	 * when executing "ndp -p".
143933841545SHajimu UMEMOTO 	 */
1440d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1441d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1442d748f7efSMark Johnston 		if ((e = nd6_prefix_offlink(pr)) != 0) {
1443d748f7efSMark Johnston 			nd6log((LOG_ERR,
1444d748f7efSMark Johnston 			    "nd6_prefix_del: failed to make %s/%d offlink "
144533841545SHajimu UMEMOTO 			    "on %s, errno=%d\n",
14461d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
144733841545SHajimu UMEMOTO 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
144833841545SHajimu UMEMOTO 			/* what should we do? */
144933841545SHajimu UMEMOTO 		}
1450d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
145182cd038dSYoshinobu Inoue 	}
1452d748f7efSMark Johnston 
1453d748f7efSMark Johnston 	/* Release references to routers that have advertised this prefix. */
1454d748f7efSMark Johnston 	ND6_WLOCK();
1455d748f7efSMark Johnston 	LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1456d748f7efSMark Johnston 		pfxrtr_del(pfr);
1457d748f7efSMark Johnston 	ND6_WUNLOCK();
1458d748f7efSMark Johnston 
1459d748f7efSMark Johnston 	nd6_prefix_rele(pr);
146082cd038dSYoshinobu Inoue 
146182cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
146282cd038dSYoshinobu Inoue }
146382cd038dSYoshinobu Inoue 
1464743eee66SSUZUKI Shinsuke static int
14651272577eSXin LI prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
14661272577eSXin LI     struct mbuf *m, int mcast)
146782cd038dSYoshinobu Inoue {
146833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
146933841545SHajimu UMEMOTO 	struct ifaddr *ifa;
147033841545SHajimu UMEMOTO 	struct ifnet *ifp = new->ndpr_ifp;
147182cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
147282cd038dSYoshinobu Inoue 	int error = 0;
147382cd038dSYoshinobu Inoue 	int auth;
147433841545SHajimu UMEMOTO 	struct in6_addrlifetime lt6_tmp;
14751d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1476b8a6e03fSGleb Smirnoff 
1477b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
147882cd038dSYoshinobu Inoue 
147982cd038dSYoshinobu Inoue 	auth = 0;
148082cd038dSYoshinobu Inoue 	if (m) {
148182cd038dSYoshinobu Inoue 		/*
148282cd038dSYoshinobu Inoue 		 * Authenticity for NA consists authentication for
148382cd038dSYoshinobu Inoue 		 * both IP header and IP datagrams, doesn't it ?
148482cd038dSYoshinobu Inoue 		 */
148582cd038dSYoshinobu Inoue #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
148607eb2995SHajimu UMEMOTO 		auth = ((m->m_flags & M_AUTHIPHDR) &&
148707eb2995SHajimu UMEMOTO 		    (m->m_flags & M_AUTHIPDGM));
148882cd038dSYoshinobu Inoue #endif
148982cd038dSYoshinobu Inoue 	}
149082cd038dSYoshinobu Inoue 
149133841545SHajimu UMEMOTO 	if ((pr = nd6_prefix_lookup(new)) != NULL) {
149233841545SHajimu UMEMOTO 		/*
149333841545SHajimu UMEMOTO 		 * nd6_prefix_lookup() ensures that pr and new have the same
149433841545SHajimu UMEMOTO 		 * prefix on a same interface.
149533841545SHajimu UMEMOTO 		 */
149633841545SHajimu UMEMOTO 
149733841545SHajimu UMEMOTO 		/*
149833841545SHajimu UMEMOTO 		 * Update prefix information.  Note that the on-link (L) bit
149933841545SHajimu UMEMOTO 		 * and the autonomous (A) bit should NOT be changed from 1
150033841545SHajimu UMEMOTO 		 * to 0.
150133841545SHajimu UMEMOTO 		 */
150233841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 1)
150333841545SHajimu UMEMOTO 			pr->ndpr_raf_onlink = 1;
150433841545SHajimu UMEMOTO 		if (new->ndpr_raf_auto == 1)
150533841545SHajimu UMEMOTO 			pr->ndpr_raf_auto = 1;
150633841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink) {
150782cd038dSYoshinobu Inoue 			pr->ndpr_vltime = new->ndpr_vltime;
150882cd038dSYoshinobu Inoue 			pr->ndpr_pltime = new->ndpr_pltime;
1509743eee66SSUZUKI Shinsuke 			(void)in6_init_prefix_ltimes(pr); /* XXX error case? */
15107d26db17SHiroki Sato 			pr->ndpr_lastupdate = time_uptime;
151182cd038dSYoshinobu Inoue 		}
151282cd038dSYoshinobu Inoue 
151333841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink &&
151433841545SHajimu UMEMOTO 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1515d748f7efSMark Johnston 			ND6_ONLINK_LOCK();
1516d748f7efSMark Johnston 			if ((error = nd6_prefix_onlink(pr)) != 0) {
151733841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
151833841545SHajimu UMEMOTO 				    "prelist_update: failed to make "
151933841545SHajimu UMEMOTO 				    "the prefix %s/%d on-link on %s "
152033841545SHajimu UMEMOTO 				    "(errno=%d)\n",
15211d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
15221d54aa3bSBjoern A. Zeeb 				        &pr->ndpr_prefix.sin6_addr),
1523d748f7efSMark Johnston 				    pr->ndpr_plen, if_name(pr->ndpr_ifp),
1524d748f7efSMark Johnston 				    error));
152533841545SHajimu UMEMOTO 				/* proceed anyway. XXX: is it correct? */
1526686cdd19SJun-ichiro itojun Hagino 			}
1527d748f7efSMark Johnston 			ND6_ONLINK_UNLOCK();
152882cd038dSYoshinobu Inoue 		}
152982cd038dSYoshinobu Inoue 
1530d748f7efSMark Johnston 		if (dr != NULL)
153182cd038dSYoshinobu Inoue 			pfxrtr_add(pr, dr);
153282cd038dSYoshinobu Inoue 	} else {
153333841545SHajimu UMEMOTO 		if (new->ndpr_vltime == 0)
153433841545SHajimu UMEMOTO 			goto end;
153533841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
153633841545SHajimu UMEMOTO 			goto end;
153782cd038dSYoshinobu Inoue 
153883631b16SMark Johnston 		error = nd6_prelist_add(new, dr, &pr);
153920b5f022SMark Johnston 		if (error != 0) {
154033841545SHajimu UMEMOTO 			nd6log((LOG_NOTICE, "prelist_update: "
154120b5f022SMark Johnston 			    "nd6_prelist_add failed for %s/%d on %s errno=%d\n",
15421d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
154320b5f022SMark Johnston 			    new->ndpr_plen, if_name(new->ndpr_ifp), error));
154433841545SHajimu UMEMOTO 			goto end; /* we should just give up in this case. */
154533841545SHajimu UMEMOTO 		}
154633841545SHajimu UMEMOTO 
154782cd038dSYoshinobu Inoue 		/*
154833841545SHajimu UMEMOTO 		 * XXX: from the ND point of view, we can ignore a prefix
154933841545SHajimu UMEMOTO 		 * with the on-link bit being zero.  However, we need a
155033841545SHajimu UMEMOTO 		 * prefix structure for references from autoconfigured
155107eb2995SHajimu UMEMOTO 		 * addresses.  Thus, we explicitly make sure that the prefix
155233841545SHajimu UMEMOTO 		 * itself expires now.
155382cd038dSYoshinobu Inoue 		 */
155483631b16SMark Johnston 		if (pr->ndpr_raf_onlink == 0) {
155583631b16SMark Johnston 			pr->ndpr_vltime = 0;
155683631b16SMark Johnston 			pr->ndpr_pltime = 0;
155783631b16SMark Johnston 			in6_init_prefix_ltimes(pr);
155833841545SHajimu UMEMOTO 		}
155933841545SHajimu UMEMOTO 	}
156033841545SHajimu UMEMOTO 
156133841545SHajimu UMEMOTO 	/*
156233841545SHajimu UMEMOTO 	 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
156333841545SHajimu UMEMOTO 	 * Note that pr must be non NULL at this point.
156433841545SHajimu UMEMOTO 	 */
156533841545SHajimu UMEMOTO 
156633841545SHajimu UMEMOTO 	/* 5.5.3 (a). Ignore the prefix without the A bit set. */
156782cd038dSYoshinobu Inoue 	if (!new->ndpr_raf_auto)
1568743eee66SSUZUKI Shinsuke 		goto end;
156982cd038dSYoshinobu Inoue 
157033841545SHajimu UMEMOTO 	/*
157133841545SHajimu UMEMOTO 	 * 5.5.3 (b). the link-local prefix should have been ignored in
157233841545SHajimu UMEMOTO 	 * nd6_ra_input.
157333841545SHajimu UMEMOTO 	 */
157433841545SHajimu UMEMOTO 
1575743eee66SSUZUKI Shinsuke 	/* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1576743eee66SSUZUKI Shinsuke 	if (new->ndpr_pltime > new->ndpr_vltime) {
1577743eee66SSUZUKI Shinsuke 		error = EINVAL;	/* XXX: won't be used */
1578743eee66SSUZUKI Shinsuke 		goto end;
1579743eee66SSUZUKI Shinsuke 	}
158033841545SHajimu UMEMOTO 
158133841545SHajimu UMEMOTO 	/*
1582743eee66SSUZUKI Shinsuke 	 * 5.5.3 (d).  If the prefix advertised is not equal to the prefix of
1583743eee66SSUZUKI Shinsuke 	 * an address configured by stateless autoconfiguration already in the
1584743eee66SSUZUKI Shinsuke 	 * list of addresses associated with the interface, and the Valid
1585743eee66SSUZUKI Shinsuke 	 * Lifetime is not 0, form an address.  We first check if we have
1586743eee66SSUZUKI Shinsuke 	 * a matching prefix.
1587743eee66SSUZUKI Shinsuke 	 * Note: we apply a clarification in rfc2462bis-02 here.  We only
1588743eee66SSUZUKI Shinsuke 	 * consider autoconfigured addresses while RFC2462 simply said
1589743eee66SSUZUKI Shinsuke 	 * "address".
159033841545SHajimu UMEMOTO 	 */
1591d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
159233841545SHajimu UMEMOTO 		struct in6_ifaddr *ifa6;
1593743eee66SSUZUKI Shinsuke 		u_int32_t remaininglifetime;
159433841545SHajimu UMEMOTO 
159533841545SHajimu UMEMOTO 		if (ifa->ifa_addr->sa_family != AF_INET6)
159633841545SHajimu UMEMOTO 			continue;
159733841545SHajimu UMEMOTO 
159833841545SHajimu UMEMOTO 		ifa6 = (struct in6_ifaddr *)ifa;
159933841545SHajimu UMEMOTO 
160033841545SHajimu UMEMOTO 		/*
1601743eee66SSUZUKI Shinsuke 		 * We only consider autoconfigured addresses as per rfc2462bis.
1602743eee66SSUZUKI Shinsuke 		 */
1603743eee66SSUZUKI Shinsuke 		if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1604743eee66SSUZUKI Shinsuke 			continue;
1605743eee66SSUZUKI Shinsuke 
1606743eee66SSUZUKI Shinsuke 		/*
160733841545SHajimu UMEMOTO 		 * Spec is not clear here, but I believe we should concentrate
160833841545SHajimu UMEMOTO 		 * on unicast (i.e. not anycast) addresses.
160933841545SHajimu UMEMOTO 		 * XXX: other ia6_flags? detached or duplicated?
161033841545SHajimu UMEMOTO 		 */
161133841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
161233841545SHajimu UMEMOTO 			continue;
161333841545SHajimu UMEMOTO 
1614743eee66SSUZUKI Shinsuke 		/*
1615743eee66SSUZUKI Shinsuke 		 * Ignore the address if it is not associated with a prefix
1616743eee66SSUZUKI Shinsuke 		 * or is associated with a prefix that is different from this
1617743eee66SSUZUKI Shinsuke 		 * one.  (pr is never NULL here)
1618743eee66SSUZUKI Shinsuke 		 */
1619743eee66SSUZUKI Shinsuke 		if (ifa6->ia6_ndpr != pr)
162033841545SHajimu UMEMOTO 			continue;
162133841545SHajimu UMEMOTO 
162233841545SHajimu UMEMOTO 		if (ia6_match == NULL) /* remember the first one */
162333841545SHajimu UMEMOTO 			ia6_match = ifa6;
162433841545SHajimu UMEMOTO 
162533841545SHajimu UMEMOTO 		/*
162633841545SHajimu UMEMOTO 		 * An already autoconfigured address matched.  Now that we
162733841545SHajimu UMEMOTO 		 * are sure there is at least one matched address, we can
162833841545SHajimu UMEMOTO 		 * proceed to 5.5.3. (e): update the lifetimes according to the
162933841545SHajimu UMEMOTO 		 * "two hours" rule and the privacy extension.
1630743eee66SSUZUKI Shinsuke 		 * We apply some clarifications in rfc2462bis:
1631743eee66SSUZUKI Shinsuke 		 * - use remaininglifetime instead of storedlifetime as a
1632743eee66SSUZUKI Shinsuke 		 *   variable name
1633743eee66SSUZUKI Shinsuke 		 * - remove the dead code in the "two-hour" rule
163433841545SHajimu UMEMOTO 		 */
163533841545SHajimu UMEMOTO #define TWOHOUR		(120*60)
163633841545SHajimu UMEMOTO 		lt6_tmp = ifa6->ia6_lifetime;
163733841545SHajimu UMEMOTO 
163811f3a6e2SHajimu UMEMOTO 		if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1639743eee66SSUZUKI Shinsuke 			remaininglifetime = ND6_INFINITE_LIFETIME;
16407d26db17SHiroki Sato 		else if (time_uptime - ifa6->ia6_updatetime >
1641743eee66SSUZUKI Shinsuke 			 lt6_tmp.ia6t_vltime) {
1642743eee66SSUZUKI Shinsuke 			/*
1643743eee66SSUZUKI Shinsuke 			 * The case of "invalid" address.  We should usually
1644743eee66SSUZUKI Shinsuke 			 * not see this case.
1645743eee66SSUZUKI Shinsuke 			 */
1646743eee66SSUZUKI Shinsuke 			remaininglifetime = 0;
1647743eee66SSUZUKI Shinsuke 		} else
1648743eee66SSUZUKI Shinsuke 			remaininglifetime = lt6_tmp.ia6t_vltime -
16497d26db17SHiroki Sato 			    (time_uptime - ifa6->ia6_updatetime);
165011f3a6e2SHajimu UMEMOTO 
165111f3a6e2SHajimu UMEMOTO 		/* when not updating, keep the current stored lifetime. */
1652743eee66SSUZUKI Shinsuke 		lt6_tmp.ia6t_vltime = remaininglifetime;
165333841545SHajimu UMEMOTO 
165433841545SHajimu UMEMOTO 		if (TWOHOUR < new->ndpr_vltime ||
1655743eee66SSUZUKI Shinsuke 		    remaininglifetime < new->ndpr_vltime) {
165633841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1657743eee66SSUZUKI Shinsuke 		} else if (remaininglifetime <= TWOHOUR) {
165833841545SHajimu UMEMOTO 			if (auth) {
165933841545SHajimu UMEMOTO 				lt6_tmp.ia6t_vltime = new->ndpr_vltime;
166082cd038dSYoshinobu Inoue 			}
166133841545SHajimu UMEMOTO 		} else {
166233841545SHajimu UMEMOTO 			/*
166333841545SHajimu UMEMOTO 			 * new->ndpr_vltime <= TWOHOUR &&
1664743eee66SSUZUKI Shinsuke 			 * TWOHOUR < remaininglifetime
166533841545SHajimu UMEMOTO 			 */
166633841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = TWOHOUR;
166782cd038dSYoshinobu Inoue 		}
166882cd038dSYoshinobu Inoue 
166933841545SHajimu UMEMOTO 		/* The 2 hour rule is not imposed for preferred lifetime. */
167033841545SHajimu UMEMOTO 		lt6_tmp.ia6t_pltime = new->ndpr_pltime;
167133841545SHajimu UMEMOTO 
167233841545SHajimu UMEMOTO 		in6_init_address_ltimes(pr, &lt6_tmp);
167333841545SHajimu UMEMOTO 
167433841545SHajimu UMEMOTO 		/*
1675743eee66SSUZUKI Shinsuke 		 * We need to treat lifetimes for temporary addresses
1676743eee66SSUZUKI Shinsuke 		 * differently, according to
1677743eee66SSUZUKI Shinsuke 		 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1678743eee66SSUZUKI Shinsuke 		 * we only update the lifetimes when they are in the maximum
1679743eee66SSUZUKI Shinsuke 		 * intervals.
168033841545SHajimu UMEMOTO 		 */
168133841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1682743eee66SSUZUKI Shinsuke 			u_int32_t maxvltime, maxpltime;
168333841545SHajimu UMEMOTO 
1684603724d3SBjoern A. Zeeb 			if (V_ip6_temp_valid_lifetime >
16857d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1686603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1687603724d3SBjoern A. Zeeb 				maxvltime = V_ip6_temp_valid_lifetime -
16887d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1689603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1690743eee66SSUZUKI Shinsuke 			} else
1691743eee66SSUZUKI Shinsuke 				maxvltime = 0;
1692603724d3SBjoern A. Zeeb 			if (V_ip6_temp_preferred_lifetime >
16937d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1694603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1695603724d3SBjoern A. Zeeb 				maxpltime = V_ip6_temp_preferred_lifetime -
16967d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1697603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1698743eee66SSUZUKI Shinsuke 			} else
1699743eee66SSUZUKI Shinsuke 				maxpltime = 0;
1700743eee66SSUZUKI Shinsuke 
1701743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1702743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_vltime > maxvltime) {
1703743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_vltime = maxvltime;
1704743eee66SSUZUKI Shinsuke 			}
1705743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1706743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_pltime > maxpltime) {
1707743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_pltime = maxpltime;
1708743eee66SSUZUKI Shinsuke 			}
1709743eee66SSUZUKI Shinsuke 		}
171033841545SHajimu UMEMOTO 		ifa6->ia6_lifetime = lt6_tmp;
17117d26db17SHiroki Sato 		ifa6->ia6_updatetime = time_uptime;
171233841545SHajimu UMEMOTO 	}
171333841545SHajimu UMEMOTO 	if (ia6_match == NULL && new->ndpr_vltime) {
1714743eee66SSUZUKI Shinsuke 		int ifidlen;
1715743eee66SSUZUKI Shinsuke 
171633841545SHajimu UMEMOTO 		/*
1717743eee66SSUZUKI Shinsuke 		 * 5.5.3 (d) (continued)
171833841545SHajimu UMEMOTO 		 * No address matched and the valid lifetime is non-zero.
171933841545SHajimu UMEMOTO 		 * Create a new address.
172033841545SHajimu UMEMOTO 		 */
1721743eee66SSUZUKI Shinsuke 
1722743eee66SSUZUKI Shinsuke 		/*
1723743eee66SSUZUKI Shinsuke 		 * Prefix Length check:
1724743eee66SSUZUKI Shinsuke 		 * If the sum of the prefix length and interface identifier
1725743eee66SSUZUKI Shinsuke 		 * length does not equal 128 bits, the Prefix Information
1726743eee66SSUZUKI Shinsuke 		 * option MUST be ignored.  The length of the interface
1727743eee66SSUZUKI Shinsuke 		 * identifier is defined in a separate link-type specific
1728743eee66SSUZUKI Shinsuke 		 * document.
1729743eee66SSUZUKI Shinsuke 		 */
1730743eee66SSUZUKI Shinsuke 		ifidlen = in6_if2idlen(ifp);
1731743eee66SSUZUKI Shinsuke 		if (ifidlen < 0) {
1732743eee66SSUZUKI Shinsuke 			/* this should not happen, so we always log it. */
1733743eee66SSUZUKI Shinsuke 			log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1734743eee66SSUZUKI Shinsuke 			    if_name(ifp));
1735743eee66SSUZUKI Shinsuke 			goto end;
1736743eee66SSUZUKI Shinsuke 		}
1737743eee66SSUZUKI Shinsuke 		if (ifidlen + pr->ndpr_plen != 128) {
1738743eee66SSUZUKI Shinsuke 			nd6log((LOG_INFO,
1739743eee66SSUZUKI Shinsuke 			    "prelist_update: invalid prefixlen "
1740743eee66SSUZUKI Shinsuke 			    "%d for %s, ignored\n",
1741743eee66SSUZUKI Shinsuke 			    pr->ndpr_plen, if_name(ifp)));
1742743eee66SSUZUKI Shinsuke 			goto end;
1743743eee66SSUZUKI Shinsuke 		}
1744743eee66SSUZUKI Shinsuke 
1745743eee66SSUZUKI Shinsuke 		if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
174633841545SHajimu UMEMOTO 			/*
174733841545SHajimu UMEMOTO 			 * note that we should use pr (not new) for reference.
174833841545SHajimu UMEMOTO 			 */
17490bbf244eSMark Johnston 			pr->ndpr_addrcnt++;
175033841545SHajimu UMEMOTO 			ia6->ia6_ndpr = pr;
175133841545SHajimu UMEMOTO 
175233841545SHajimu UMEMOTO 			/*
175333841545SHajimu UMEMOTO 			 * RFC 3041 3.3 (2).
175433841545SHajimu UMEMOTO 			 * When a new public address is created as described
175533841545SHajimu UMEMOTO 			 * in RFC2462, also create a new temporary address.
175633841545SHajimu UMEMOTO 			 *
175733841545SHajimu UMEMOTO 			 * RFC 3041 3.5.
175833841545SHajimu UMEMOTO 			 * When an interface connects to a new link, a new
175933841545SHajimu UMEMOTO 			 * randomized interface identifier should be generated
176033841545SHajimu UMEMOTO 			 * immediately together with a new set of temporary
176133841545SHajimu UMEMOTO 			 * addresses.  Thus, we specifiy 1 as the 2nd arg of
176233841545SHajimu UMEMOTO 			 * in6_tmpifadd().
176333841545SHajimu UMEMOTO 			 */
1764603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr) {
176533841545SHajimu UMEMOTO 				int e;
1766743eee66SSUZUKI Shinsuke 				if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
176733841545SHajimu UMEMOTO 					nd6log((LOG_NOTICE, "prelist_update: "
176833841545SHajimu UMEMOTO 					    "failed to create a temporary "
176933841545SHajimu UMEMOTO 					    "address, errno=%d\n",
177033841545SHajimu UMEMOTO 					    e));
177133841545SHajimu UMEMOTO 				}
177233841545SHajimu UMEMOTO 			}
17738c0fec80SRobert Watson 			ifa_free(&ia6->ia_ifa);
177433841545SHajimu UMEMOTO 
177533841545SHajimu UMEMOTO 			/*
177633841545SHajimu UMEMOTO 			 * A newly added address might affect the status
177733841545SHajimu UMEMOTO 			 * of other addresses, so we check and update it.
177833841545SHajimu UMEMOTO 			 * XXX: what if address duplication happens?
177933841545SHajimu UMEMOTO 			 */
178033841545SHajimu UMEMOTO 			pfxlist_onlink_check();
178133841545SHajimu UMEMOTO 		} else {
178233841545SHajimu UMEMOTO 			/* just set an error. do not bark here. */
178333841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL; /* XXX: might be unused. */
178433841545SHajimu UMEMOTO 		}
178533841545SHajimu UMEMOTO 	}
178633841545SHajimu UMEMOTO 
178782cd038dSYoshinobu Inoue end:
1788d748f7efSMark Johnston 	if (pr != NULL)
1789d748f7efSMark Johnston 		nd6_prefix_rele(pr);
1790d748f7efSMark Johnston 	return (error);
179182cd038dSYoshinobu Inoue }
179282cd038dSYoshinobu Inoue 
179382cd038dSYoshinobu Inoue /*
1794686cdd19SJun-ichiro itojun Hagino  * A supplement function used in the on-link detection below;
1795686cdd19SJun-ichiro itojun Hagino  * detect if a given prefix has a (probably) reachable advertising router.
1796686cdd19SJun-ichiro itojun Hagino  * XXX: lengthy function name...
1797686cdd19SJun-ichiro itojun Hagino  */
179833841545SHajimu UMEMOTO static struct nd_pfxrouter *
17991272577eSXin LI find_pfxlist_reachable_router(struct nd_prefix *pr)
1800686cdd19SJun-ichiro itojun Hagino {
1801a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1802686cdd19SJun-ichiro itojun Hagino 	struct nd_pfxrouter *pfxrtr;
18036e6b3f7cSQing Li 	struct llentry *ln;
1804d78be3a9SKip Macy 	int canreach;
1805686cdd19SJun-ichiro itojun Hagino 
1806d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1807d748f7efSMark Johnston 
1808a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1809b8a6e03fSGleb Smirnoff 	LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1810d78be3a9SKip Macy 		ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1811a6146780SKip Macy 		if (ln == NULL)
1812a6146780SKip Macy 			continue;
1813d78be3a9SKip Macy 		canreach = ND6_IS_LLINFO_PROBREACH(ln);
1814d78be3a9SKip Macy 		LLE_RUNLOCK(ln);
1815d78be3a9SKip Macy 		if (canreach)
1816d78be3a9SKip Macy 			break;
18176e6b3f7cSQing Li 	}
1818b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
1819686cdd19SJun-ichiro itojun Hagino 	return (pfxrtr);
1820686cdd19SJun-ichiro itojun Hagino }
1821686cdd19SJun-ichiro itojun Hagino 
1822686cdd19SJun-ichiro itojun Hagino /*
182382cd038dSYoshinobu Inoue  * Check if each prefix in the prefix list has at least one available router
182433841545SHajimu UMEMOTO  * that advertised the prefix (a router is "available" if its neighbor cache
182533841545SHajimu UMEMOTO  * entry is reachable or probably reachable).
1826686cdd19SJun-ichiro itojun Hagino  * If the check fails, the prefix may be off-link, because, for example,
182782cd038dSYoshinobu Inoue  * we have moved from the network but the lifetime of the prefix has not
182833841545SHajimu UMEMOTO  * expired yet.  So we should not use the prefix if there is another prefix
182933841545SHajimu UMEMOTO  * that has an available router.
183007c1f959SMark Johnston  * But, if there is no prefix that has an available router, we still regard
183182cd038dSYoshinobu Inoue  * all the prefixes as on-link.  This is because we can't tell if all the
183282cd038dSYoshinobu Inoue  * routers are simply dead or if we really moved from the network and there
183382cd038dSYoshinobu Inoue  * is no router around us.
183482cd038dSYoshinobu Inoue  */
1835686cdd19SJun-ichiro itojun Hagino void
18361b28988bSMark Johnston pfxlist_onlink_check(void)
183782cd038dSYoshinobu Inoue {
183882cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
183933841545SHajimu UMEMOTO 	struct in6_ifaddr *ifa;
1840743eee66SSUZUKI Shinsuke 	struct nd_defrouter *dr;
1841743eee66SSUZUKI Shinsuke 	struct nd_pfxrouter *pfxrtr = NULL;
18421b28988bSMark Johnston 	struct rm_priotracker in6_ifa_tracker;
1843d748f7efSMark Johnston 	uint64_t genid;
1844d748f7efSMark Johnston 	uint32_t flags;
1845d748f7efSMark Johnston 
1846d748f7efSMark Johnston 	ND6_ONLINK_LOCK();
1847d748f7efSMark Johnston 	ND6_RLOCK();
184882cd038dSYoshinobu Inoue 
1849686cdd19SJun-ichiro itojun Hagino 	/*
1850686cdd19SJun-ichiro itojun Hagino 	 * Check if there is a prefix that has a reachable advertising
1851686cdd19SJun-ichiro itojun Hagino 	 * router.
1852686cdd19SJun-ichiro itojun Hagino 	 */
18533b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
185433841545SHajimu UMEMOTO 		if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
185582cd038dSYoshinobu Inoue 			break;
1856686cdd19SJun-ichiro itojun Hagino 	}
185782cd038dSYoshinobu Inoue 
185882cd038dSYoshinobu Inoue 	/*
1859743eee66SSUZUKI Shinsuke 	 * If we have no such prefix, check whether we still have a router
1860743eee66SSUZUKI Shinsuke 	 * that does not advertise any prefixes.
1861743eee66SSUZUKI Shinsuke 	 */
1862743eee66SSUZUKI Shinsuke 	if (pr == NULL) {
1863f592d0c3SBjoern A. Zeeb 		TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1864743eee66SSUZUKI Shinsuke 			struct nd_prefix *pr0;
1865743eee66SSUZUKI Shinsuke 
18663b0b2840SJohn Baldwin 			LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1867743eee66SSUZUKI Shinsuke 				if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1868743eee66SSUZUKI Shinsuke 					break;
1869743eee66SSUZUKI Shinsuke 			}
1870743eee66SSUZUKI Shinsuke 			if (pfxrtr != NULL)
1871743eee66SSUZUKI Shinsuke 				break;
1872743eee66SSUZUKI Shinsuke 		}
1873743eee66SSUZUKI Shinsuke 	}
1874f592d0c3SBjoern A. Zeeb 	if (pr != NULL || (!TAILQ_EMPTY(&V_nd6_defrouter) && pfxrtr == NULL)) {
1875743eee66SSUZUKI Shinsuke 		/*
1876743eee66SSUZUKI Shinsuke 		 * There is at least one prefix that has a reachable router,
1877743eee66SSUZUKI Shinsuke 		 * or at least a router which probably does not advertise
1878743eee66SSUZUKI Shinsuke 		 * any prefixes.  The latter would be the case when we move
1879743eee66SSUZUKI Shinsuke 		 * to a new link where we have a router that does not provide
1880743eee66SSUZUKI Shinsuke 		 * prefixes and we configure an address by hand.
188133841545SHajimu UMEMOTO 		 * Detach prefixes which have no reachable advertising
188233841545SHajimu UMEMOTO 		 * router, and attach other prefixes.
188382cd038dSYoshinobu Inoue 		 */
18843b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
188533841545SHajimu UMEMOTO 			/* XXX: a link-local prefix should never be detached */
1886a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1887a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1888a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
188942cb3aa4SQing Li 				continue;
189042cb3aa4SQing Li 
189133841545SHajimu UMEMOTO 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
189233841545SHajimu UMEMOTO 			    find_pfxlist_reachable_router(pr) == NULL)
189333841545SHajimu UMEMOTO 				pr->ndpr_stateflags |= NDPRF_DETACHED;
1894a88d6d7eSMark Johnston 			else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1895155d72c4SPedro F. Giffuni 			    find_pfxlist_reachable_router(pr) != NULL)
189633841545SHajimu UMEMOTO 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
189782cd038dSYoshinobu Inoue 		}
189833841545SHajimu UMEMOTO 	} else {
189933841545SHajimu UMEMOTO 		/* there is no prefix that has a reachable router */
19003b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1901a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1902a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1903a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
190433841545SHajimu UMEMOTO 				continue;
190533841545SHajimu UMEMOTO 			pr->ndpr_stateflags &= ~NDPRF_DETACHED;
190633841545SHajimu UMEMOTO 		}
190733841545SHajimu UMEMOTO 	}
190833841545SHajimu UMEMOTO 
190933841545SHajimu UMEMOTO 	/*
191033841545SHajimu UMEMOTO 	 * Remove each interface route associated with a (just) detached
191133841545SHajimu UMEMOTO 	 * prefix, and reinstall the interface route for a (just) attached
191233841545SHajimu UMEMOTO 	 * prefix.  Note that all attempt of reinstallation does not
191333841545SHajimu UMEMOTO 	 * necessarily success, when a same prefix is shared among multiple
191433841545SHajimu UMEMOTO 	 * interfaces.  Such cases will be handled in nd6_prefix_onlink,
191533841545SHajimu UMEMOTO 	 * so we don't have to care about them.
191633841545SHajimu UMEMOTO 	 */
1917d748f7efSMark Johnston restart:
19183b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
19191d54aa3bSBjoern A. Zeeb 		char ip6buf[INET6_ADDRSTRLEN];
1920a88d6d7eSMark Johnston 		int e;
192133841545SHajimu UMEMOTO 
1922a88d6d7eSMark Johnston 		if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1923a88d6d7eSMark Johnston 		    pr->ndpr_raf_onlink == 0 ||
1924a88d6d7eSMark Johnston 		    pr->ndpr_raf_auto == 0)
192542cb3aa4SQing Li 			continue;
192642cb3aa4SQing Li 
1927d748f7efSMark Johnston 		flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1928d748f7efSMark Johnston 		if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1929d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1930d748f7efSMark Johnston 			ND6_RUNLOCK();
1931d748f7efSMark Johnston 			if ((flags & NDPRF_ONLINK) != 0 &&
1932d748f7efSMark Johnston 			    (e = nd6_prefix_offlink(pr)) != 0) {
193333841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
193433841545SHajimu UMEMOTO 				    "pfxlist_onlink_check: failed to "
19357aa59493SSUZUKI Shinsuke 				    "make %s/%d offlink, errno=%d\n",
19361d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
19371d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
193833841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
1939d748f7efSMark Johnston 			} else if ((flags & NDPRF_ONLINK) == 0 &&
1940d748f7efSMark Johnston 			    (e = nd6_prefix_onlink(pr)) != 0) {
194133841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
194233841545SHajimu UMEMOTO 				    "pfxlist_onlink_check: failed to "
19437aa59493SSUZUKI Shinsuke 				    "make %s/%d onlink, errno=%d\n",
19441d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
19451d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
194633841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
194733841545SHajimu UMEMOTO 			}
1948d748f7efSMark Johnston 			ND6_RLOCK();
1949d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1950d748f7efSMark Johnston 				goto restart;
195133841545SHajimu UMEMOTO 		}
195233841545SHajimu UMEMOTO 	}
195333841545SHajimu UMEMOTO 
195433841545SHajimu UMEMOTO 	/*
195533841545SHajimu UMEMOTO 	 * Changes on the prefix status might affect address status as well.
195633841545SHajimu UMEMOTO 	 * Make sure that all addresses derived from an attached prefix are
195733841545SHajimu UMEMOTO 	 * attached, and that all addresses derived from a detached prefix are
195833841545SHajimu UMEMOTO 	 * detached.  Note, however, that a manually configured address should
195933841545SHajimu UMEMOTO 	 * always be attached.
196033841545SHajimu UMEMOTO 	 * The precise detection logic is same as the one for prefixes.
196133841545SHajimu UMEMOTO 	 */
19621b28988bSMark Johnston 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1963d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
196407eb2995SHajimu UMEMOTO 		if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
196533841545SHajimu UMEMOTO 			continue;
196633841545SHajimu UMEMOTO 
196733841545SHajimu UMEMOTO 		if (ifa->ia6_ndpr == NULL) {
196833841545SHajimu UMEMOTO 			/*
196933841545SHajimu UMEMOTO 			 * This can happen when we first configure the address
197033841545SHajimu UMEMOTO 			 * (i.e. the address exists, but the prefix does not).
197133841545SHajimu UMEMOTO 			 * XXX: complicated relationships...
197233841545SHajimu UMEMOTO 			 */
197333841545SHajimu UMEMOTO 			continue;
197433841545SHajimu UMEMOTO 		}
197533841545SHajimu UMEMOTO 
197633841545SHajimu UMEMOTO 		if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
197733841545SHajimu UMEMOTO 			break;
197833841545SHajimu UMEMOTO 	}
197933841545SHajimu UMEMOTO 	if (ifa) {
1980d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
198133841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
198233841545SHajimu UMEMOTO 				continue;
198333841545SHajimu UMEMOTO 
198433841545SHajimu UMEMOTO 			if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
198533841545SHajimu UMEMOTO 				continue;
198633841545SHajimu UMEMOTO 
1987743eee66SSUZUKI Shinsuke 			if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
1988743eee66SSUZUKI Shinsuke 				if (ifa->ia6_flags & IN6_IFF_DETACHED) {
198933841545SHajimu UMEMOTO 					ifa->ia6_flags &= ~IN6_IFF_DETACHED;
1990743eee66SSUZUKI Shinsuke 					ifa->ia6_flags |= IN6_IFF_TENTATIVE;
1991743eee66SSUZUKI Shinsuke 					nd6_dad_start((struct ifaddr *)ifa, 0);
1992743eee66SSUZUKI Shinsuke 				}
1993743eee66SSUZUKI Shinsuke 			} else {
199433841545SHajimu UMEMOTO 				ifa->ia6_flags |= IN6_IFF_DETACHED;
199582cd038dSYoshinobu Inoue 			}
1996686cdd19SJun-ichiro itojun Hagino 		}
19971b28988bSMark Johnston 	} else {
1998d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
199933841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
200033841545SHajimu UMEMOTO 				continue;
200133841545SHajimu UMEMOTO 
2002743eee66SSUZUKI Shinsuke 			if (ifa->ia6_flags & IN6_IFF_DETACHED) {
200333841545SHajimu UMEMOTO 				ifa->ia6_flags &= ~IN6_IFF_DETACHED;
2004743eee66SSUZUKI Shinsuke 				ifa->ia6_flags |= IN6_IFF_TENTATIVE;
2005743eee66SSUZUKI Shinsuke 				/* Do we need a delay in this case? */
2006743eee66SSUZUKI Shinsuke 				nd6_dad_start((struct ifaddr *)ifa, 0);
2007743eee66SSUZUKI Shinsuke 			}
200833841545SHajimu UMEMOTO 		}
200982cd038dSYoshinobu Inoue 	}
20101b28988bSMark Johnston 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
2011d748f7efSMark Johnston 	ND6_RUNLOCK();
2012d748f7efSMark Johnston 	ND6_ONLINK_UNLOCK();
201382cd038dSYoshinobu Inoue }
201482cd038dSYoshinobu Inoue 
20159d9b92f2SBjoern A. Zeeb static int
201681d5d46bSBjoern A. Zeeb nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
201781d5d46bSBjoern A. Zeeb {
2018563ab4e4SAlexander V. Chernikov 	struct sockaddr_dl sdl;
201981d5d46bSBjoern A. Zeeb 	struct rtentry *rt;
202081d5d46bSBjoern A. Zeeb 	struct sockaddr_in6 mask6;
202181d5d46bSBjoern A. Zeeb 	u_long rtflags;
2022559b4296SAlan Somers 	int error, a_failure, fibnum, maxfib;
202381d5d46bSBjoern A. Zeeb 
202481d5d46bSBjoern A. Zeeb 	/*
202581d5d46bSBjoern A. Zeeb 	 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
202681d5d46bSBjoern A. Zeeb 	 * ifa->ifa_rtrequest = nd6_rtrequest;
202781d5d46bSBjoern A. Zeeb 	 */
202881d5d46bSBjoern A. Zeeb 	bzero(&mask6, sizeof(mask6));
202981d5d46bSBjoern A. Zeeb 	mask6.sin6_len = sizeof(mask6);
203081d5d46bSBjoern A. Zeeb 	mask6.sin6_addr = pr->ndpr_mask;
203181d5d46bSBjoern A. Zeeb 	rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
203281d5d46bSBjoern A. Zeeb 
2033563ab4e4SAlexander V. Chernikov 	bzero(&sdl, sizeof(struct sockaddr_dl));
2034563ab4e4SAlexander V. Chernikov 	sdl.sdl_len = sizeof(struct sockaddr_dl);
2035563ab4e4SAlexander V. Chernikov 	sdl.sdl_family = AF_LINK;
2036563ab4e4SAlexander V. Chernikov 	sdl.sdl_type = ifa->ifa_ifp->if_type;
2037563ab4e4SAlexander V. Chernikov 	sdl.sdl_index = ifa->ifa_ifp->if_index;
2038563ab4e4SAlexander V. Chernikov 
2039559b4296SAlan Somers 	if(V_rt_add_addr_allfibs) {
2040559b4296SAlan Somers 		fibnum = 0;
2041559b4296SAlan Somers 		maxfib = rt_numfibs;
2042559b4296SAlan Somers 	} else {
2043559b4296SAlan Somers 		fibnum = ifa->ifa_ifp->if_fib;
2044559b4296SAlan Somers 		maxfib = fibnum + 1;
2045559b4296SAlan Somers 	}
204681d5d46bSBjoern A. Zeeb 	a_failure = 0;
2047559b4296SAlan Somers 	for (; fibnum < maxfib; fibnum++) {
204881d5d46bSBjoern A. Zeeb 
204981d5d46bSBjoern A. Zeeb 		rt = NULL;
205081d5d46bSBjoern A. Zeeb 		error = in6_rtrequest(RTM_ADD,
2051563ab4e4SAlexander V. Chernikov 		    (struct sockaddr *)&pr->ndpr_prefix, (struct sockaddr *)&sdl,
205281d5d46bSBjoern A. Zeeb 		    (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
205381d5d46bSBjoern A. Zeeb 		if (error == 0) {
205481d5d46bSBjoern A. Zeeb 			KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
205581d5d46bSBjoern A. Zeeb 			    "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
205681d5d46bSBjoern A. Zeeb 			    error, pr, ifa));
205781d5d46bSBjoern A. Zeeb 			RT_LOCK(rt);
205881d5d46bSBjoern A. Zeeb 			nd6_rtmsg(RTM_ADD, rt);
205981d5d46bSBjoern A. Zeeb 			RT_UNLOCK(rt);
206081d5d46bSBjoern A. Zeeb 			pr->ndpr_stateflags |= NDPRF_ONLINK;
206181d5d46bSBjoern A. Zeeb 		} else {
206281d5d46bSBjoern A. Zeeb 			char ip6buf[INET6_ADDRSTRLEN];
206381d5d46bSBjoern A. Zeeb 			char ip6bufg[INET6_ADDRSTRLEN];
206481d5d46bSBjoern A. Zeeb 			char ip6bufm[INET6_ADDRSTRLEN];
206581d5d46bSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
206681d5d46bSBjoern A. Zeeb 
206781d5d46bSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
206881d5d46bSBjoern A. Zeeb 			nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
206981d5d46bSBjoern A. Zeeb 			    "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
207081d5d46bSBjoern A. Zeeb 			    "flags=%lx errno = %d\n",
207181d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
207281d5d46bSBjoern A. Zeeb 			    pr->ndpr_plen, if_name(pr->ndpr_ifp),
207381d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufg, &sin6->sin6_addr),
207481d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufm, &mask6.sin6_addr),
207581d5d46bSBjoern A. Zeeb 			    rtflags, error));
207681d5d46bSBjoern A. Zeeb 
207781d5d46bSBjoern A. Zeeb 			/* Save last error to return, see rtinit(). */
207881d5d46bSBjoern A. Zeeb 			a_failure = error;
207981d5d46bSBjoern A. Zeeb 		}
208081d5d46bSBjoern A. Zeeb 
208181d5d46bSBjoern A. Zeeb 		if (rt != NULL) {
208281d5d46bSBjoern A. Zeeb 			RT_LOCK(rt);
208381d5d46bSBjoern A. Zeeb 			RT_REMREF(rt);
208481d5d46bSBjoern A. Zeeb 			RT_UNLOCK(rt);
208581d5d46bSBjoern A. Zeeb 		}
208681d5d46bSBjoern A. Zeeb 	}
208781d5d46bSBjoern A. Zeeb 
208881d5d46bSBjoern A. Zeeb 	/* Return the last error we got. */
208981d5d46bSBjoern A. Zeeb 	return (a_failure);
209081d5d46bSBjoern A. Zeeb }
209181d5d46bSBjoern A. Zeeb 
2092*25ebfe33SBjoern A. Zeeb static int
20931272577eSXin LI nd6_prefix_onlink(struct nd_prefix *pr)
209482cd038dSYoshinobu Inoue {
2095751d8d15SGleb Smirnoff 	struct epoch_tracker et;
209633841545SHajimu UMEMOTO 	struct ifaddr *ifa;
209733841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
209833841545SHajimu UMEMOTO 	struct nd_prefix *opr;
20991d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2100d748f7efSMark Johnston 	int error;
210133841545SHajimu UMEMOTO 
2102d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2103d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2104d748f7efSMark Johnston 
2105d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
21062ce62dceSSUZUKI Shinsuke 		return (EEXIST);
210782cd038dSYoshinobu Inoue 
210882cd038dSYoshinobu Inoue 	/*
210933841545SHajimu UMEMOTO 	 * Add the interface route associated with the prefix.  Before
211033841545SHajimu UMEMOTO 	 * installing the route, check if there's the same prefix on another
211133841545SHajimu UMEMOTO 	 * interface, and the prefix has already installed the interface route.
211233841545SHajimu UMEMOTO 	 * Although such a configuration is expected to be rare, we explicitly
211333841545SHajimu UMEMOTO 	 * allow it.
211482cd038dSYoshinobu Inoue 	 */
2115d748f7efSMark Johnston 	ND6_RLOCK();
21163b0b2840SJohn Baldwin 	LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
211733841545SHajimu UMEMOTO 		if (opr == pr)
211833841545SHajimu UMEMOTO 			continue;
211933841545SHajimu UMEMOTO 
212033841545SHajimu UMEMOTO 		if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
212133841545SHajimu UMEMOTO 			continue;
212233841545SHajimu UMEMOTO 
2123559b4296SAlan Somers 		if (!V_rt_add_addr_allfibs &&
2124559b4296SAlan Somers 		    opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2125559b4296SAlan Somers 			continue;
2126559b4296SAlan Somers 
212733841545SHajimu UMEMOTO 		if (opr->ndpr_plen == pr->ndpr_plen &&
212833841545SHajimu UMEMOTO 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2129d748f7efSMark Johnston 		    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2130d748f7efSMark Johnston 			ND6_RUNLOCK();
213133841545SHajimu UMEMOTO 			return (0);
213233841545SHajimu UMEMOTO 		}
2133d748f7efSMark Johnston 	}
2134d748f7efSMark Johnston 	ND6_RUNLOCK();
213533841545SHajimu UMEMOTO 
213633841545SHajimu UMEMOTO 	/*
213733841545SHajimu UMEMOTO 	 * We prefer link-local addresses as the associated interface address.
213833841545SHajimu UMEMOTO 	 */
213933841545SHajimu UMEMOTO 	/* search for a link-local addr */
2140751d8d15SGleb Smirnoff 	NET_EPOCH_ENTER(et);
214133841545SHajimu UMEMOTO 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
214207eb2995SHajimu UMEMOTO 	    IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
214333841545SHajimu UMEMOTO 	if (ifa == NULL) {
214433841545SHajimu UMEMOTO 		/* XXX: freebsd does not have ifa_ifwithaf */
2145d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2146c2615844SMark Johnston 			if (ifa->ifa_addr->sa_family == AF_INET6) {
2147c2615844SMark Johnston 				ifa_ref(ifa);
214833841545SHajimu UMEMOTO 				break;
214933841545SHajimu UMEMOTO 			}
2150c2615844SMark Johnston 		}
215133841545SHajimu UMEMOTO 		/* should we care about ia6_flags? */
215233841545SHajimu UMEMOTO 	}
2153751d8d15SGleb Smirnoff 	NET_EPOCH_EXIT(et);
215433841545SHajimu UMEMOTO 	if (ifa == NULL) {
215533841545SHajimu UMEMOTO 		/*
215633841545SHajimu UMEMOTO 		 * This can still happen, when, for example, we receive an RA
215733841545SHajimu UMEMOTO 		 * containing a prefix with the L bit set and the A bit clear,
215833841545SHajimu UMEMOTO 		 * after removing all IPv6 addresses on the receiving
215933841545SHajimu UMEMOTO 		 * interface.  This should, of course, be rare though.
216033841545SHajimu UMEMOTO 		 */
216133841545SHajimu UMEMOTO 		nd6log((LOG_NOTICE,
216233841545SHajimu UMEMOTO 		    "nd6_prefix_onlink: failed to find any ifaddr"
216333841545SHajimu UMEMOTO 		    " to add route for a prefix(%s/%d) on %s\n",
21641d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
216533841545SHajimu UMEMOTO 		    pr->ndpr_plen, if_name(ifp)));
216633841545SHajimu UMEMOTO 		return (0);
216733841545SHajimu UMEMOTO 	}
216833841545SHajimu UMEMOTO 
216981d5d46bSBjoern A. Zeeb 	error = nd6_prefix_onlink_rtrequest(pr, ifa);
217033841545SHajimu UMEMOTO 
21718c0fec80SRobert Watson 	if (ifa != NULL)
21728c0fec80SRobert Watson 		ifa_free(ifa);
217333841545SHajimu UMEMOTO 
217433841545SHajimu UMEMOTO 	return (error);
217533841545SHajimu UMEMOTO }
217633841545SHajimu UMEMOTO 
2177d748f7efSMark Johnston int
21781272577eSXin LI nd6_prefix_offlink(struct nd_prefix *pr)
217933841545SHajimu UMEMOTO {
218033841545SHajimu UMEMOTO 	int error = 0;
218133841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
218233841545SHajimu UMEMOTO 	struct nd_prefix *opr;
218333841545SHajimu UMEMOTO 	struct sockaddr_in6 sa6, mask6;
218481d5d46bSBjoern A. Zeeb 	struct rtentry *rt;
21851d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2186d748f7efSMark Johnston 	uint64_t genid;
2187559b4296SAlan Somers 	int fibnum, maxfib, a_failure;
218833841545SHajimu UMEMOTO 
2189d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2190d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2191d748f7efSMark Johnston 
2192d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
219333841545SHajimu UMEMOTO 		return (EEXIST);
219433841545SHajimu UMEMOTO 
219582cd038dSYoshinobu Inoue 	bzero(&sa6, sizeof(sa6));
219682cd038dSYoshinobu Inoue 	sa6.sin6_family = AF_INET6;
219782cd038dSYoshinobu Inoue 	sa6.sin6_len = sizeof(sa6);
219882cd038dSYoshinobu Inoue 	bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
219982cd038dSYoshinobu Inoue 	    sizeof(struct in6_addr));
220082cd038dSYoshinobu Inoue 	bzero(&mask6, sizeof(mask6));
220182cd038dSYoshinobu Inoue 	mask6.sin6_family = AF_INET6;
220282cd038dSYoshinobu Inoue 	mask6.sin6_len = sizeof(sa6);
220382cd038dSYoshinobu Inoue 	bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
220433841545SHajimu UMEMOTO 
2205559b4296SAlan Somers 	if (V_rt_add_addr_allfibs) {
2206559b4296SAlan Somers 		fibnum = 0;
2207559b4296SAlan Somers 		maxfib = rt_numfibs;
2208559b4296SAlan Somers 	} else {
2209559b4296SAlan Somers 		fibnum = ifp->if_fib;
2210559b4296SAlan Somers 		maxfib = fibnum + 1;
2211559b4296SAlan Somers 	}
2212559b4296SAlan Somers 
221381d5d46bSBjoern A. Zeeb 	a_failure = 0;
2214559b4296SAlan Somers 	for (; fibnum < maxfib; fibnum++) {
221581d5d46bSBjoern A. Zeeb 		rt = NULL;
221681d5d46bSBjoern A. Zeeb 		error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
221781d5d46bSBjoern A. Zeeb 		    (struct sockaddr *)&mask6, 0, &rt, fibnum);
221881d5d46bSBjoern A. Zeeb 		if (error == 0) {
221933841545SHajimu UMEMOTO 			/* report the route deletion to the routing socket. */
222033841545SHajimu UMEMOTO 			if (rt != NULL)
222133841545SHajimu UMEMOTO 				nd6_rtmsg(RTM_DELETE, rt);
222281d5d46bSBjoern A. Zeeb 		} else {
222381d5d46bSBjoern A. Zeeb 			/* Save last error to return, see rtinit(). */
222481d5d46bSBjoern A. Zeeb 			a_failure = error;
222581d5d46bSBjoern A. Zeeb 		}
222681d5d46bSBjoern A. Zeeb 		if (rt != NULL) {
222781d5d46bSBjoern A. Zeeb 			RTFREE(rt);
222881d5d46bSBjoern A. Zeeb 		}
222981d5d46bSBjoern A. Zeeb 	}
223081d5d46bSBjoern A. Zeeb 	error = a_failure;
2231378aa8d8SQing Li 	a_failure = 1;
223281d5d46bSBjoern A. Zeeb 	if (error == 0) {
223381d5d46bSBjoern A. Zeeb 		pr->ndpr_stateflags &= ~NDPRF_ONLINK;
223433841545SHajimu UMEMOTO 
223533841545SHajimu UMEMOTO 		/*
223633841545SHajimu UMEMOTO 		 * There might be the same prefix on another interface,
223733841545SHajimu UMEMOTO 		 * the prefix which could not be on-link just because we have
223833841545SHajimu UMEMOTO 		 * the interface route (see comments in nd6_prefix_onlink).
223933841545SHajimu UMEMOTO 		 * If there's one, try to make the prefix on-link on the
224033841545SHajimu UMEMOTO 		 * interface.
224133841545SHajimu UMEMOTO 		 */
2242d748f7efSMark Johnston 		ND6_RLOCK();
2243d748f7efSMark Johnston restart:
22443b0b2840SJohn Baldwin 		LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
224533841545SHajimu UMEMOTO 			/*
224633841545SHajimu UMEMOTO 			 * KAME specific: detached prefixes should not be
224733841545SHajimu UMEMOTO 			 * on-link.
224833841545SHajimu UMEMOTO 			 */
22497b0e84b7SMark Johnston 			if (opr == pr || (opr->ndpr_stateflags &
22507b0e84b7SMark Johnston 			    (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
225133841545SHajimu UMEMOTO 				continue;
225233841545SHajimu UMEMOTO 
225333841545SHajimu UMEMOTO 			if (opr->ndpr_plen == pr->ndpr_plen &&
225433841545SHajimu UMEMOTO 			    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
225507eb2995SHajimu UMEMOTO 			    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
225682cd038dSYoshinobu Inoue 				int e;
225782cd038dSYoshinobu Inoue 
2258d748f7efSMark Johnston 				genid = V_nd6_list_genid;
2259d748f7efSMark Johnston 				ND6_RUNLOCK();
226033841545SHajimu UMEMOTO 				if ((e = nd6_prefix_onlink(opr)) != 0) {
226133841545SHajimu UMEMOTO 					nd6log((LOG_ERR,
226233841545SHajimu UMEMOTO 					    "nd6_prefix_offlink: failed to "
226333841545SHajimu UMEMOTO 					    "recover a prefix %s/%d from %s "
226433841545SHajimu UMEMOTO 					    "to %s (errno = %d)\n",
22651d54aa3bSBjoern A. Zeeb 					    ip6_sprintf(ip6buf,
22661d54aa3bSBjoern A. Zeeb 						&opr->ndpr_prefix.sin6_addr),
226733841545SHajimu UMEMOTO 					    opr->ndpr_plen, if_name(ifp),
226833841545SHajimu UMEMOTO 					    if_name(opr->ndpr_ifp), e));
2269378aa8d8SQing Li 				} else
2270378aa8d8SQing Li 					a_failure = 0;
2271d748f7efSMark Johnston 				ND6_RLOCK();
2272d748f7efSMark Johnston 				if (genid != V_nd6_list_genid)
2273d748f7efSMark Johnston 					goto restart;
227482cd038dSYoshinobu Inoue 			}
227582cd038dSYoshinobu Inoue 		}
2276d748f7efSMark Johnston 		ND6_RUNLOCK();
227707eb2995SHajimu UMEMOTO 	} else {
227833841545SHajimu UMEMOTO 		/* XXX: can we still set the NDPRF_ONLINK flag? */
227933841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
228033841545SHajimu UMEMOTO 		    "nd6_prefix_offlink: failed to delete route: "
228133841545SHajimu UMEMOTO 		    "%s/%d on %s (errno = %d)\n",
22821d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
22831d54aa3bSBjoern A. Zeeb 		    if_name(ifp), error));
228433841545SHajimu UMEMOTO 	}
228582cd038dSYoshinobu Inoue 
2286378aa8d8SQing Li 	if (a_failure)
2287378aa8d8SQing Li 		lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
2288378aa8d8SQing Li 		    (struct sockaddr *)&mask6, LLE_STATIC);
2289378aa8d8SQing Li 
229033841545SHajimu UMEMOTO 	return (error);
229182cd038dSYoshinobu Inoue }
229282cd038dSYoshinobu Inoue 
22931272577eSXin LI /*
22941272577eSXin LI  * ia0 - corresponding public address
22951272577eSXin LI  */
229682cd038dSYoshinobu Inoue int
22971272577eSXin LI in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
229882cd038dSYoshinobu Inoue {
229933841545SHajimu UMEMOTO 	struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
23000c5d4bdeSAlexander V. Chernikov 	struct in6_ifaddr *newia;
230133841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
23021dc8f6a8SAlexander V. Chernikov 	int error;
230333841545SHajimu UMEMOTO 	int trylimit = 3;	/* XXX: adhoc value */
2304743eee66SSUZUKI Shinsuke 	int updateflags;
230533841545SHajimu UMEMOTO 	u_int32_t randid[2];
230633841545SHajimu UMEMOTO 	time_t vltime0, pltime0;
230782cd038dSYoshinobu Inoue 
23089080e7d0SAlexander V. Chernikov 	in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
23099080e7d0SAlexander V. Chernikov 	    &ia0->ia_prefixmask.sin6_addr);
23109080e7d0SAlexander V. Chernikov 
23119080e7d0SAlexander V. Chernikov 	ifra.ifra_addr = ia0->ia_addr;	/* XXX: do we need this ? */
231233841545SHajimu UMEMOTO 	/* clear the old IFID */
23131dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
23141dc8f6a8SAlexander V. Chernikov 	    &ifra.ifra_prefixmask.sin6_addr);
231582cd038dSYoshinobu Inoue 
231633841545SHajimu UMEMOTO   again:
2317743eee66SSUZUKI Shinsuke 	if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2318743eee66SSUZUKI Shinsuke 	    (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2319743eee66SSUZUKI Shinsuke 		nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
2320743eee66SSUZUKI Shinsuke 		    "random IFID\n"));
2321743eee66SSUZUKI Shinsuke 		return (EINVAL);
2322743eee66SSUZUKI Shinsuke 	}
232307eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
232407eb2995SHajimu UMEMOTO 	    (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
232507eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
232607eb2995SHajimu UMEMOTO 	    (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
232782cd038dSYoshinobu Inoue 
232882cd038dSYoshinobu Inoue 	/*
2329743eee66SSUZUKI Shinsuke 	 * in6_get_tmpifid() quite likely provided a unique interface ID.
2330743eee66SSUZUKI Shinsuke 	 * However, we may still have a chance to see collision, because
2331743eee66SSUZUKI Shinsuke 	 * there may be a time lag between generation of the ID and generation
2332743eee66SSUZUKI Shinsuke 	 * of the address.  So, we'll do one more sanity check.
233382cd038dSYoshinobu Inoue 	 */
23340c5d4bdeSAlexander V. Chernikov 
23350c5d4bdeSAlexander V. Chernikov 	if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
23360c5d4bdeSAlexander V. Chernikov 		if (trylimit-- > 0) {
23370c5d4bdeSAlexander V. Chernikov 			forcegen = 1;
23380c5d4bdeSAlexander V. Chernikov 			goto again;
23390c5d4bdeSAlexander V. Chernikov 		}
23400c5d4bdeSAlexander V. Chernikov 
23410c5d4bdeSAlexander V. Chernikov 		/* Give up.  Something strange should have happened.  */
2342743eee66SSUZUKI Shinsuke 		nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
2343743eee66SSUZUKI Shinsuke 		    "find a unique random IFID\n"));
234433841545SHajimu UMEMOTO 		return (EEXIST);
234533841545SHajimu UMEMOTO 	}
234682cd038dSYoshinobu Inoue 
234733841545SHajimu UMEMOTO 	/*
234833841545SHajimu UMEMOTO 	 * The Valid Lifetime is the lower of the Valid Lifetime of the
234933841545SHajimu UMEMOTO          * public address or TEMP_VALID_LIFETIME.
235033841545SHajimu UMEMOTO 	 * The Preferred Lifetime is the lower of the Preferred Lifetime
235133841545SHajimu UMEMOTO          * of the public address or TEMP_PREFERRED_LIFETIME -
235233841545SHajimu UMEMOTO          * DESYNC_FACTOR.
235382cd038dSYoshinobu Inoue 	 */
2354743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
235533841545SHajimu UMEMOTO 		vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2356743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_vltime -
23577d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2358603724d3SBjoern A. Zeeb 		if (vltime0 > V_ip6_temp_valid_lifetime)
2359603724d3SBjoern A. Zeeb 			vltime0 = V_ip6_temp_valid_lifetime;
236033841545SHajimu UMEMOTO 	} else
2361603724d3SBjoern A. Zeeb 		vltime0 = V_ip6_temp_valid_lifetime;
2362743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
236333841545SHajimu UMEMOTO 		pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2364743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_pltime -
23657d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2366603724d3SBjoern A. Zeeb 		if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2367603724d3SBjoern A. Zeeb 			pltime0 = V_ip6_temp_preferred_lifetime -
2368603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor;
236933841545SHajimu UMEMOTO 		}
237033841545SHajimu UMEMOTO 	} else
2371603724d3SBjoern A. Zeeb 		pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
237233841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = vltime0;
237333841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = pltime0;
237433841545SHajimu UMEMOTO 
237533841545SHajimu UMEMOTO 	/*
237633841545SHajimu UMEMOTO 	 * A temporary address is created only if this calculated Preferred
237733841545SHajimu UMEMOTO 	 * Lifetime is greater than REGEN_ADVANCE time units.
237833841545SHajimu UMEMOTO 	 */
2379603724d3SBjoern A. Zeeb 	if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
238033841545SHajimu UMEMOTO 		return (0);
238133841545SHajimu UMEMOTO 
238233841545SHajimu UMEMOTO 	/* XXX: scope zone ID? */
238333841545SHajimu UMEMOTO 
238433841545SHajimu UMEMOTO 	ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
238533841545SHajimu UMEMOTO 
238633841545SHajimu UMEMOTO 	/* allocate ifaddr structure, link into chain, etc. */
2387743eee66SSUZUKI Shinsuke 	updateflags = 0;
2388743eee66SSUZUKI Shinsuke 	if (delay)
2389743eee66SSUZUKI Shinsuke 		updateflags |= IN6_IFAUPDATE_DADDELAY;
2390743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
239133841545SHajimu UMEMOTO 		return (error);
239233841545SHajimu UMEMOTO 
239333841545SHajimu UMEMOTO 	newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
239433841545SHajimu UMEMOTO 	if (newia == NULL) {	/* XXX: can it happen? */
239533841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
239633841545SHajimu UMEMOTO 		    "in6_tmpifadd: ifa update succeeded, but we got "
239733841545SHajimu UMEMOTO 		    "no ifaddr\n"));
239833841545SHajimu UMEMOTO 		return (EINVAL); /* XXX */
239933841545SHajimu UMEMOTO 	}
240033841545SHajimu UMEMOTO 	newia->ia6_ndpr = ia0->ia6_ndpr;
24010bbf244eSMark Johnston 	newia->ia6_ndpr->ndpr_addrcnt++;
24028c0fec80SRobert Watson 	ifa_free(&newia->ia_ifa);
240333841545SHajimu UMEMOTO 
2404c3aacd9eSHajimu UMEMOTO 	/*
2405c3aacd9eSHajimu UMEMOTO 	 * A newly added address might affect the status of other addresses.
2406c3aacd9eSHajimu UMEMOTO 	 * XXX: when the temporary address is generated with a new public
2407c3aacd9eSHajimu UMEMOTO 	 * address, the onlink check is redundant.  However, it would be safe
2408c3aacd9eSHajimu UMEMOTO 	 * to do the check explicitly everywhere a new address is generated,
2409c3aacd9eSHajimu UMEMOTO 	 * and, in fact, we surely need the check when we create a new
2410c3aacd9eSHajimu UMEMOTO 	 * temporary address due to deprecation of an old temporary address.
2411c3aacd9eSHajimu UMEMOTO 	 */
2412c3aacd9eSHajimu UMEMOTO 	pfxlist_onlink_check();
2413c3aacd9eSHajimu UMEMOTO 
241433841545SHajimu UMEMOTO 	return (0);
241582cd038dSYoshinobu Inoue }
241682cd038dSYoshinobu Inoue 
2417743eee66SSUZUKI Shinsuke static int
2418e8b0643eSAlexander V. Chernikov rt6_deleteroute(const struct rtentry *rt, void *arg)
241982cd038dSYoshinobu Inoue {
242082cd038dSYoshinobu Inoue #define SIN6(s)	((struct sockaddr_in6 *)s)
242182cd038dSYoshinobu Inoue 	struct in6_addr *gate = (struct in6_addr *)arg;
242282cd038dSYoshinobu Inoue 
242382cd038dSYoshinobu Inoue 	if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
242482cd038dSYoshinobu Inoue 		return (0);
242582cd038dSYoshinobu Inoue 
242607eb2995SHajimu UMEMOTO 	if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
242782cd038dSYoshinobu Inoue 		return (0);
242807eb2995SHajimu UMEMOTO 	}
242982cd038dSYoshinobu Inoue 
243082cd038dSYoshinobu Inoue 	/*
243133841545SHajimu UMEMOTO 	 * Do not delete a static route.
243233841545SHajimu UMEMOTO 	 * XXX: this seems to be a bit ad-hoc. Should we consider the
243333841545SHajimu UMEMOTO 	 * 'cloned' bit instead?
243433841545SHajimu UMEMOTO 	 */
243533841545SHajimu UMEMOTO 	if ((rt->rt_flags & RTF_STATIC) != 0)
243633841545SHajimu UMEMOTO 		return (0);
243733841545SHajimu UMEMOTO 
243833841545SHajimu UMEMOTO 	/*
243982cd038dSYoshinobu Inoue 	 * We delete only host route. This means, in particular, we don't
244082cd038dSYoshinobu Inoue 	 * delete default route.
244182cd038dSYoshinobu Inoue 	 */
244282cd038dSYoshinobu Inoue 	if ((rt->rt_flags & RTF_HOST) == 0)
244382cd038dSYoshinobu Inoue 		return (0);
244482cd038dSYoshinobu Inoue 
2445e8b0643eSAlexander V. Chernikov 	return (1);
244682cd038dSYoshinobu Inoue #undef SIN6
244782cd038dSYoshinobu Inoue }
2448686cdd19SJun-ichiro itojun Hagino 
2449f77a6dbdSBjoern A. Zeeb /*
2450f77a6dbdSBjoern A. Zeeb  * Delete all the routing table entries that use the specified gateway.
2451f77a6dbdSBjoern A. Zeeb  * XXX: this function causes search through all entries of routing table, so
2452f77a6dbdSBjoern A. Zeeb  * it shouldn't be called when acting as a router.
2453f77a6dbdSBjoern A. Zeeb  */
2454f77a6dbdSBjoern A. Zeeb void
2455f77a6dbdSBjoern A. Zeeb rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2456f77a6dbdSBjoern A. Zeeb {
2457f77a6dbdSBjoern A. Zeeb 
2458f77a6dbdSBjoern A. Zeeb 	/* We'll care only link-local addresses */
2459f77a6dbdSBjoern A. Zeeb 	if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2460f77a6dbdSBjoern A. Zeeb 		return;
2461f77a6dbdSBjoern A. Zeeb 
2462f77a6dbdSBjoern A. Zeeb 	/* XXX Do we really need to walk any but the default FIB? */
2463f77a6dbdSBjoern A. Zeeb 	rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2464f77a6dbdSBjoern A. Zeeb }
2465f77a6dbdSBjoern A. Zeeb 
2466686cdd19SJun-ichiro itojun Hagino int
24671272577eSXin LI nd6_setdefaultiface(int ifindex)
2468686cdd19SJun-ichiro itojun Hagino {
2469686cdd19SJun-ichiro itojun Hagino 	int error = 0;
2470686cdd19SJun-ichiro itojun Hagino 
2471603724d3SBjoern A. Zeeb 	if (ifindex < 0 || V_if_index < ifindex)
2472686cdd19SJun-ichiro itojun Hagino 		return (EINVAL);
2473743eee66SSUZUKI Shinsuke 	if (ifindex != 0 && !ifnet_byindex(ifindex))
2474743eee66SSUZUKI Shinsuke 		return (EINVAL);
2475686cdd19SJun-ichiro itojun Hagino 
2476603724d3SBjoern A. Zeeb 	if (V_nd6_defifindex != ifindex) {
2477603724d3SBjoern A. Zeeb 		V_nd6_defifindex = ifindex;
2478603724d3SBjoern A. Zeeb 		if (V_nd6_defifindex > 0)
2479603724d3SBjoern A. Zeeb 			V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2480686cdd19SJun-ichiro itojun Hagino 		else
2481603724d3SBjoern A. Zeeb 			V_nd6_defifp = NULL;
2482686cdd19SJun-ichiro itojun Hagino 
2483686cdd19SJun-ichiro itojun Hagino 		/*
2484686cdd19SJun-ichiro itojun Hagino 		 * Our current implementation assumes one-to-one maping between
2485686cdd19SJun-ichiro itojun Hagino 		 * interfaces and links, so it would be natural to use the
2486686cdd19SJun-ichiro itojun Hagino 		 * default interface as the default link.
2487686cdd19SJun-ichiro itojun Hagino 		 */
2488603724d3SBjoern A. Zeeb 		scope6_setdefault(V_nd6_defifp);
2489686cdd19SJun-ichiro itojun Hagino 	}
2490686cdd19SJun-ichiro itojun Hagino 
2491686cdd19SJun-ichiro itojun Hagino 	return (error);
2492686cdd19SJun-ichiro itojun Hagino }
2493ad675b32SBjoern A. Zeeb 
2494ad675b32SBjoern A. Zeeb bool
2495ad675b32SBjoern A. Zeeb nd6_defrouter_list_empty(void)
2496ad675b32SBjoern A. Zeeb {
2497ad675b32SBjoern A. Zeeb 
2498f592d0c3SBjoern A. Zeeb 	return (TAILQ_EMPTY(&V_nd6_defrouter));
2499ad675b32SBjoern A. Zeeb }
2500ad675b32SBjoern A. Zeeb 
2501ad675b32SBjoern A. Zeeb void
2502ad675b32SBjoern A. Zeeb nd6_defrouter_timer(void)
2503ad675b32SBjoern A. Zeeb {
2504ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2505f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2506ad675b32SBjoern A. Zeeb 
2507ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2508ad675b32SBjoern A. Zeeb 
2509ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2510f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr)
2511ad675b32SBjoern A. Zeeb 		if (dr->expire && dr->expire < time_uptime)
2512ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2513ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2514ad675b32SBjoern A. Zeeb 
2515ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2516ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2517ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2518ad675b32SBjoern A. Zeeb 	}
2519ad675b32SBjoern A. Zeeb }
2520ad675b32SBjoern A. Zeeb 
2521ad675b32SBjoern A. Zeeb /*
2522ad675b32SBjoern A. Zeeb  * Nuke default router list entries toward ifp.
2523ad675b32SBjoern A. Zeeb  * We defer removal of default router list entries that is installed in the
2524ad675b32SBjoern A. Zeeb  * routing table, in order to keep additional side effects as small as possible.
2525ad675b32SBjoern A. Zeeb  */
2526ad675b32SBjoern A. Zeeb void
2527ad675b32SBjoern A. Zeeb nd6_defrouter_purge(struct ifnet *ifp)
2528ad675b32SBjoern A. Zeeb {
2529ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2530f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2531ad675b32SBjoern A. Zeeb 
2532ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2533ad675b32SBjoern A. Zeeb 
2534ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2535f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2536ad675b32SBjoern A. Zeeb 		if (dr->installed)
2537ad675b32SBjoern A. Zeeb 			continue;
2538ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2539ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2540ad675b32SBjoern A. Zeeb 	}
2541f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2542ad675b32SBjoern A. Zeeb 		if (!dr->installed)
2543ad675b32SBjoern A. Zeeb 			continue;
2544ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2545ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2546ad675b32SBjoern A. Zeeb 	}
2547ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2548ad675b32SBjoern A. Zeeb 
2549ad675b32SBjoern A. Zeeb 	/* Delete the unlinked router objects. */
2550ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2551ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2552ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2553ad675b32SBjoern A. Zeeb 	}
2554ad675b32SBjoern A. Zeeb }
2555ad675b32SBjoern A. Zeeb 
2556ad675b32SBjoern A. Zeeb void
2557ad675b32SBjoern A. Zeeb nd6_defrouter_flush_all(void)
2558ad675b32SBjoern A. Zeeb {
2559ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
2560f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2561ad675b32SBjoern A. Zeeb 
2562ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2563ad675b32SBjoern A. Zeeb 
2564ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2565f592d0c3SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&V_nd6_defrouter)) != NULL)
2566ad675b32SBjoern A. Zeeb 		defrouter_unlink(dr, &drq);
2567ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2568ad675b32SBjoern A. Zeeb 
2569ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2570ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2571ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2572ad675b32SBjoern A. Zeeb 	}
2573ad675b32SBjoern A. Zeeb }
2574ad675b32SBjoern A. Zeeb 
2575ad675b32SBjoern A. Zeeb void
2576ad675b32SBjoern A. Zeeb nd6_defrouter_init(void)
2577ad675b32SBjoern A. Zeeb {
2578ad675b32SBjoern A. Zeeb 
2579f592d0c3SBjoern A. Zeeb 	TAILQ_INIT(&V_nd6_defrouter);
2580ad675b32SBjoern A. Zeeb }
2581f77a6dbdSBjoern A. Zeeb 
2582f77a6dbdSBjoern A. Zeeb static int
2583f77a6dbdSBjoern A. Zeeb nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2584f77a6dbdSBjoern A. Zeeb {
2585f77a6dbdSBjoern A. Zeeb 	struct in6_defrouter d;
2586f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
2587f77a6dbdSBjoern A. Zeeb 	int error;
2588f77a6dbdSBjoern A. Zeeb 
2589f77a6dbdSBjoern A. Zeeb 	if (req->newptr != NULL)
2590f77a6dbdSBjoern A. Zeeb 		return (EPERM);
2591f77a6dbdSBjoern A. Zeeb 
2592f77a6dbdSBjoern A. Zeeb 	error = sysctl_wire_old_buffer(req, 0);
2593f77a6dbdSBjoern A. Zeeb 	if (error != 0)
2594f77a6dbdSBjoern A. Zeeb 		return (error);
2595f77a6dbdSBjoern A. Zeeb 
2596f77a6dbdSBjoern A. Zeeb 	bzero(&d, sizeof(d));
2597f77a6dbdSBjoern A. Zeeb 	d.rtaddr.sin6_family = AF_INET6;
2598f77a6dbdSBjoern A. Zeeb 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
2599f77a6dbdSBjoern A. Zeeb 
2600f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
2601f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
2602f77a6dbdSBjoern A. Zeeb 		d.rtaddr.sin6_addr = dr->rtaddr;
2603f77a6dbdSBjoern A. Zeeb 		error = sa6_recoverscope(&d.rtaddr);
2604f77a6dbdSBjoern A. Zeeb 		if (error != 0)
2605f77a6dbdSBjoern A. Zeeb 			break;
2606f77a6dbdSBjoern A. Zeeb 		d.flags = dr->raflags;
2607f77a6dbdSBjoern A. Zeeb 		d.rtlifetime = dr->rtlifetime;
2608f77a6dbdSBjoern A. Zeeb 		d.expire = dr->expire + (time_second - time_uptime);
2609f77a6dbdSBjoern A. Zeeb 		d.if_index = dr->ifp->if_index;
2610f77a6dbdSBjoern A. Zeeb 		error = SYSCTL_OUT(req, &d, sizeof(d));
2611f77a6dbdSBjoern A. Zeeb 		if (error != 0)
2612f77a6dbdSBjoern A. Zeeb 			break;
2613f77a6dbdSBjoern A. Zeeb 	}
2614f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
2615f77a6dbdSBjoern A. Zeeb 	return (error);
2616f77a6dbdSBjoern A. Zeeb }
2617f77a6dbdSBjoern A. Zeeb SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2618f77a6dbdSBjoern A. Zeeb 	CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2619f77a6dbdSBjoern A. Zeeb 	NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2620f77a6dbdSBjoern A. Zeeb 	"NDP default router list");
2621