xref: /freebsd/sys/netinet6/nd6_rtr.c (revision a4adf6cc653fa8ed9b1d06052f00b2481688d711)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue  * All rights reserved.
682cd038dSYoshinobu Inoue  *
782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue  * are met:
1082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue  *    without specific prior written permission.
1882cd038dSYoshinobu Inoue  *
1982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
3282cd038dSYoshinobu Inoue  */
3382cd038dSYoshinobu Inoue 
34b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
35b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$");
36b48287a3SDavid E. O'Brien 
37686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
38686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
39686cdd19SJun-ichiro itojun Hagino 
4082cd038dSYoshinobu Inoue #include <sys/param.h>
4182cd038dSYoshinobu Inoue #include <sys/systm.h>
4282cd038dSYoshinobu Inoue #include <sys/malloc.h>
4382cd038dSYoshinobu Inoue #include <sys/mbuf.h>
444de485feSMark Johnston #include <sys/refcount.h>
4582cd038dSYoshinobu Inoue #include <sys/socket.h>
4682cd038dSYoshinobu Inoue #include <sys/sockio.h>
4782cd038dSYoshinobu Inoue #include <sys/time.h>
4833841545SHajimu UMEMOTO #include <sys/kernel.h>
49609ff41fSWarner Losh #include <sys/lock.h>
5082cd038dSYoshinobu Inoue #include <sys/errno.h>
511b28988bSMark Johnston #include <sys/rmlock.h>
523120b9d4SKip Macy #include <sys/rwlock.h>
53ad675b32SBjoern A. Zeeb #include <sys/sysctl.h>
5482cd038dSYoshinobu Inoue #include <sys/syslog.h>
5533841545SHajimu UMEMOTO #include <sys/queue.h>
5682cd038dSYoshinobu Inoue 
5782cd038dSYoshinobu Inoue #include <net/if.h>
5876039bc8SGleb Smirnoff #include <net/if_var.h>
5982cd038dSYoshinobu Inoue #include <net/if_types.h>
6082cd038dSYoshinobu Inoue #include <net/if_dl.h>
6182cd038dSYoshinobu Inoue #include <net/route.h>
6261eee0e2SAlexander V. Chernikov #include <net/route_var.h>
6382cd038dSYoshinobu Inoue #include <net/radix.h>
644b79449eSBjoern A. Zeeb #include <net/vnet.h>
6582cd038dSYoshinobu Inoue 
6682cd038dSYoshinobu Inoue #include <netinet/in.h>
676e6b3f7cSQing Li #include <net/if_llatbl.h>
6882cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
6933841545SHajimu UMEMOTO #include <netinet6/in6_ifattach.h>
70686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7182cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
7282cd038dSYoshinobu Inoue #include <netinet6/nd6.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
74686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h>
7582cd038dSYoshinobu Inoue 
769233d8f3SDavid E. O'Brien static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
776f56329aSXin LI static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
786f56329aSXin LI     struct mbuf *, int);
7982cd038dSYoshinobu Inoue 
80f592d0c3SBjoern A. Zeeb TAILQ_HEAD(nd6_drhead, nd_defrouter);
81f592d0c3SBjoern A. Zeeb VNET_DEFINE_STATIC(struct nd6_drhead, nd6_defrouter);
82f592d0c3SBjoern A. Zeeb #define	V_nd6_defrouter			VNET(nd6_defrouter)
83ad675b32SBjoern A. Zeeb 
84eddfbb76SRobert Watson VNET_DECLARE(int, nd6_recalc_reachtm_interval);
851e77c105SRobert Watson #define	V_nd6_recalc_reachtm_interval	VNET(nd6_recalc_reachtm_interval)
8682cd038dSYoshinobu Inoue 
875f901c92SAndrew Turner VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
8882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, nd6_defifindex);
891e77c105SRobert Watson #define	V_nd6_defifp			VNET(nd6_defifp)
90686cdd19SJun-ichiro itojun Hagino 
9182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_use_tempaddr) = 0;
9282cea7e6SBjoern A. Zeeb 
93eddfbb76SRobert Watson VNET_DEFINE(int, ip6_desync_factor);
9482cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
9582cea7e6SBjoern A. Zeeb VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
9682cea7e6SBjoern A. Zeeb 
9782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
9833841545SHajimu UMEMOTO 
9921231a7aSBjoern A. Zeeb #ifdef EXPERIMENTAL
10021231a7aSBjoern A. Zeeb VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1;
10121231a7aSBjoern A. Zeeb #endif
10221231a7aSBjoern A. Zeeb 
103f77a6dbdSBjoern A. Zeeb SYSCTL_DECL(_net_inet6_icmp6);
104f77a6dbdSBjoern A. Zeeb 
105743eee66SSUZUKI Shinsuke /* RTPREF_MEDIUM has to be 0! */
106743eee66SSUZUKI Shinsuke #define RTPREF_HIGH	1
107743eee66SSUZUKI Shinsuke #define RTPREF_MEDIUM	0
108743eee66SSUZUKI Shinsuke #define RTPREF_LOW	(-1)
109743eee66SSUZUKI Shinsuke #define RTPREF_RESERVED	(-2)
110743eee66SSUZUKI Shinsuke #define RTPREF_INVALID	(-3)	/* internal */
111743eee66SSUZUKI Shinsuke 
112ad675b32SBjoern A. Zeeb static void
113ad675b32SBjoern A. Zeeb defrouter_ref(struct nd_defrouter *dr)
114ad675b32SBjoern A. Zeeb {
115ad675b32SBjoern A. Zeeb 
116ad675b32SBjoern A. Zeeb 	refcount_acquire(&dr->refcnt);
117ad675b32SBjoern A. Zeeb }
118ad675b32SBjoern A. Zeeb 
119ad675b32SBjoern A. Zeeb void
120ad675b32SBjoern A. Zeeb defrouter_rele(struct nd_defrouter *dr)
121ad675b32SBjoern A. Zeeb {
122ad675b32SBjoern A. Zeeb 
123ad675b32SBjoern A. Zeeb 	if (refcount_release(&dr->refcnt))
124ad675b32SBjoern A. Zeeb 		free(dr, M_IP6NDP);
125ad675b32SBjoern A. Zeeb }
126ad675b32SBjoern A. Zeeb 
127ad675b32SBjoern A. Zeeb /*
128ad675b32SBjoern A. Zeeb  * Remove a router from the global list and optionally stash it in a
129ad675b32SBjoern A. Zeeb  * caller-supplied queue.
130ad675b32SBjoern A. Zeeb  */
131ad675b32SBjoern A. Zeeb static void
132f592d0c3SBjoern A. Zeeb defrouter_unlink(struct nd_defrouter *dr, struct nd6_drhead *drq)
133ad675b32SBjoern A. Zeeb {
134ad675b32SBjoern A. Zeeb 
135ad675b32SBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
136ad675b32SBjoern A. Zeeb 
137f592d0c3SBjoern A. Zeeb 	TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
138ad675b32SBjoern A. Zeeb 	V_nd6_list_genid++;
139ad675b32SBjoern A. Zeeb 	if (drq != NULL)
140ad675b32SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(drq, dr, dr_entry);
141ad675b32SBjoern A. Zeeb }
142ad675b32SBjoern A. Zeeb 
14382cd038dSYoshinobu Inoue /*
14482cd038dSYoshinobu Inoue  * Receive Router Solicitation Message - just for routers.
14582cd038dSYoshinobu Inoue  * Router solicitation/advertisement is mostly managed by userland program
14682cd038dSYoshinobu Inoue  * (rtadvd) so here we have no function like nd6_ra_output().
14782cd038dSYoshinobu Inoue  *
14882cd038dSYoshinobu Inoue  * Based on RFC 2461
14982cd038dSYoshinobu Inoue  */
15082cd038dSYoshinobu Inoue void
1511272577eSXin LI nd6_rs_input(struct mbuf *m, int off, int icmp6len)
15282cd038dSYoshinobu Inoue {
153503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
154503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
155686cdd19SJun-ichiro itojun Hagino 	struct nd_router_solicit *nd_rs;
156503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
15782cd038dSYoshinobu Inoue 	union nd_opts ndopts;
1581d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
159503f4e47SBjoern A. Zeeb 	char *lladdr;
160503f4e47SBjoern A. Zeeb 	int lladdrlen;
161503f4e47SBjoern A. Zeeb 
162503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
16382cd038dSYoshinobu Inoue 
164e7fa8d0aSHiroki Sato 	/*
165e7fa8d0aSHiroki Sato 	 * Accept RS only when V_ip6_forwarding=1 and the interface has
166e7fa8d0aSHiroki Sato 	 * no ND6_IFF_ACCEPT_RTADV.
167e7fa8d0aSHiroki Sato 	 */
168e7fa8d0aSHiroki Sato 	if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
169686cdd19SJun-ichiro itojun Hagino 		goto freeit;
17082cd038dSYoshinobu Inoue 
171a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
172a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
173a4061289SAndrey V. Elsukov 		goto freeit;
174a4061289SAndrey V. Elsukov 
17582cd038dSYoshinobu Inoue 	/* Sanity checks */
176503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
17782cd038dSYoshinobu Inoue 	if (ip6->ip6_hlim != 255) {
17833841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
17933841545SHajimu UMEMOTO 		    "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
1801d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
1811d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
18233841545SHajimu UMEMOTO 		goto bad;
18382cd038dSYoshinobu Inoue 	}
18482cd038dSYoshinobu Inoue 
18582cd038dSYoshinobu Inoue 	/*
18682cd038dSYoshinobu Inoue 	 * Don't update the neighbor cache, if src = ::.
18782cd038dSYoshinobu Inoue 	 * This indicates that the src has no IP address assigned yet.
18882cd038dSYoshinobu Inoue 	 */
189503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
19082cd038dSYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
191686cdd19SJun-ichiro itojun Hagino 		goto freeit;
192686cdd19SJun-ichiro itojun Hagino 
193*a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + icmp6len) {
194a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + icmp6len);
195a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
196a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
19782cd038dSYoshinobu Inoue 			return;
198686cdd19SJun-ichiro itojun Hagino 		}
199*a4adf6ccSBjoern A. Zeeb 	}
200a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
201a61b5cfbSBjoern A. Zeeb 	nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
20282cd038dSYoshinobu Inoue 
20382cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_rs);
20482cd038dSYoshinobu Inoue 	nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
20582cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
20633841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
20733841545SHajimu UMEMOTO 		    "nd6_rs_input: invalid ND option, ignored\n"));
20833841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
209686cdd19SJun-ichiro itojun Hagino 		goto freeit;
21082cd038dSYoshinobu Inoue 	}
21182cd038dSYoshinobu Inoue 
212503f4e47SBjoern A. Zeeb 	lladdr = NULL;
213503f4e47SBjoern A. Zeeb 	lladdrlen = 0;
21482cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
21582cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
21682cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
21782cd038dSYoshinobu Inoue 	}
21882cd038dSYoshinobu Inoue 
21982cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
22033841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
22182cd038dSYoshinobu Inoue 		    "nd6_rs_input: lladdrlen mismatch for %s "
22282cd038dSYoshinobu Inoue 		    "(if %d, RS packet %d)\n",
2231d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6),
22407eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
22533841545SHajimu UMEMOTO 		goto bad;
22682cd038dSYoshinobu Inoue 	}
22782cd038dSYoshinobu Inoue 
22882cd038dSYoshinobu Inoue 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
229686cdd19SJun-ichiro itojun Hagino 
230686cdd19SJun-ichiro itojun Hagino  freeit:
231686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
23233841545SHajimu UMEMOTO 	return;
23333841545SHajimu UMEMOTO 
23433841545SHajimu UMEMOTO  bad:
235e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badrs);
23633841545SHajimu UMEMOTO 	m_freem(m);
23782cd038dSYoshinobu Inoue }
23882cd038dSYoshinobu Inoue 
239201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
240201100c5SBjoern A. Zeeb /*
241201100c5SBjoern A. Zeeb  * An initial update routine for draft-ietf-6man-ipv6only-flag.
242201100c5SBjoern A. Zeeb  * We need to iterate over all default routers for the given
24321231a7aSBjoern A. Zeeb  * interface to see whether they are all advertising the "S"
244201100c5SBjoern A. Zeeb  * (IPv6-Only) flag.  If they do set, otherwise unset, the
245201100c5SBjoern A. Zeeb  * interface flag we later use to filter on.
246201100c5SBjoern A. Zeeb  */
247201100c5SBjoern A. Zeeb static void
248201100c5SBjoern A. Zeeb defrtr_ipv6_only_ifp(struct ifnet *ifp)
249201100c5SBjoern A. Zeeb {
250201100c5SBjoern A. Zeeb 	struct nd_defrouter *dr;
25121231a7aSBjoern A. Zeeb 	bool ipv6_only, ipv6_only_old;
25221231a7aSBjoern A. Zeeb #ifdef INET
25321231a7aSBjoern A. Zeeb 	struct epoch_tracker et;
25421231a7aSBjoern A. Zeeb 	struct ifaddr *ifa;
25521231a7aSBjoern A. Zeeb 	bool has_ipv4_addr;
25621231a7aSBjoern A. Zeeb #endif
25721231a7aSBjoern A. Zeeb 
25821231a7aSBjoern A. Zeeb 	if (V_nd6_ignore_ipv6_only_ra != 0)
25921231a7aSBjoern A. Zeeb 		return;
260201100c5SBjoern A. Zeeb 
261201100c5SBjoern A. Zeeb 	ipv6_only = true;
262201100c5SBjoern A. Zeeb 	ND6_RLOCK();
263f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
264201100c5SBjoern A. Zeeb 		if (dr->ifp == ifp &&
265201100c5SBjoern A. Zeeb 		    (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0)
266201100c5SBjoern A. Zeeb 			ipv6_only = false;
267201100c5SBjoern A. Zeeb 	ND6_RUNLOCK();
268201100c5SBjoern A. Zeeb 
269201100c5SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
27021231a7aSBjoern A. Zeeb 	ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY;
27121231a7aSBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
27221231a7aSBjoern A. Zeeb 
27321231a7aSBjoern A. Zeeb 	/* If nothing changed, we have an early exit. */
27421231a7aSBjoern A. Zeeb 	if (ipv6_only == ipv6_only_old)
27521231a7aSBjoern A. Zeeb 		return;
27621231a7aSBjoern A. Zeeb 
27721231a7aSBjoern A. Zeeb #ifdef INET
27821231a7aSBjoern A. Zeeb 	/*
27921231a7aSBjoern A. Zeeb 	 * Should we want to set the IPV6-ONLY flag, check if the
28021231a7aSBjoern A. Zeeb 	 * interface has a non-0/0 and non-link-local IPv4 address
28121231a7aSBjoern A. Zeeb 	 * configured on it.  If it has we will assume working
28221231a7aSBjoern A. Zeeb 	 * IPv4 operations and will clear the interface flag.
28321231a7aSBjoern A. Zeeb 	 */
28421231a7aSBjoern A. Zeeb 	has_ipv4_addr = false;
28521231a7aSBjoern A. Zeeb 	if (ipv6_only) {
28621231a7aSBjoern A. Zeeb 		NET_EPOCH_ENTER(et);
28721231a7aSBjoern A. Zeeb 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
28821231a7aSBjoern A. Zeeb 			if (ifa->ifa_addr->sa_family != AF_INET)
28921231a7aSBjoern A. Zeeb 				continue;
29021231a7aSBjoern A. Zeeb 			if (in_canforward(
29121231a7aSBjoern A. Zeeb 			    satosin(ifa->ifa_addr)->sin_addr)) {
29221231a7aSBjoern A. Zeeb 				has_ipv4_addr = true;
29321231a7aSBjoern A. Zeeb 				break;
29421231a7aSBjoern A. Zeeb 			}
29521231a7aSBjoern A. Zeeb 		}
29621231a7aSBjoern A. Zeeb 		NET_EPOCH_EXIT(et);
29721231a7aSBjoern A. Zeeb 	}
29821231a7aSBjoern A. Zeeb 	if (ipv6_only && has_ipv4_addr) {
29921231a7aSBjoern A. Zeeb 		log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 "
30021231a7aSBjoern A. Zeeb 		    "configured, ignoring IPv6-Only flag.\n", ifp->if_xname);
30121231a7aSBjoern A. Zeeb 		ipv6_only = false;
30221231a7aSBjoern A. Zeeb 	}
30321231a7aSBjoern A. Zeeb #endif
30421231a7aSBjoern A. Zeeb 
30521231a7aSBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
306201100c5SBjoern A. Zeeb 	if (ipv6_only)
307201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY;
308201100c5SBjoern A. Zeeb 	else
309201100c5SBjoern A. Zeeb 		ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
310201100c5SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
31121231a7aSBjoern A. Zeeb 
31221231a7aSBjoern A. Zeeb #ifdef notyet
31321231a7aSBjoern A. Zeeb 	/* Send notification of flag change. */
314201100c5SBjoern A. Zeeb #endif
31521231a7aSBjoern A. Zeeb }
31630b45077SBjoern A. Zeeb 
31730b45077SBjoern A. Zeeb static void
31830b45077SBjoern A. Zeeb defrtr_ipv6_only_ipf_down(struct ifnet *ifp)
31930b45077SBjoern A. Zeeb {
32030b45077SBjoern A. Zeeb 
32130b45077SBjoern A. Zeeb 	IF_AFDATA_WLOCK(ifp);
32230b45077SBjoern A. Zeeb 	ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY;
32330b45077SBjoern A. Zeeb 	IF_AFDATA_WUNLOCK(ifp);
32430b45077SBjoern A. Zeeb }
32521231a7aSBjoern A. Zeeb #endif	/* EXPERIMENTAL */
326201100c5SBjoern A. Zeeb 
32730b45077SBjoern A. Zeeb void
32830b45077SBjoern A. Zeeb nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate)
32930b45077SBjoern A. Zeeb {
33030b45077SBjoern A. Zeeb 
33130b45077SBjoern A. Zeeb 	/*
33230b45077SBjoern A. Zeeb 	 * XXX-BZ we might want to trigger re-evaluation of our default router
33330b45077SBjoern A. Zeeb 	 * availability. E.g., on link down the default router might be
33430b45077SBjoern A. Zeeb 	 * unreachable but a different interface might still have connectivity.
33530b45077SBjoern A. Zeeb 	 */
33630b45077SBjoern A. Zeeb 
33730b45077SBjoern A. Zeeb #ifdef EXPERIMENTAL
33830b45077SBjoern A. Zeeb 	if (linkstate == LINK_STATE_DOWN)
33930b45077SBjoern A. Zeeb 		defrtr_ipv6_only_ipf_down(ifp);
34030b45077SBjoern A. Zeeb #endif
34130b45077SBjoern A. Zeeb }
34230b45077SBjoern A. Zeeb 
34382cd038dSYoshinobu Inoue /*
34482cd038dSYoshinobu Inoue  * Receive Router Advertisement Message.
34582cd038dSYoshinobu Inoue  *
34682cd038dSYoshinobu Inoue  * Based on RFC 2461
34782cd038dSYoshinobu Inoue  * TODO: on-link bit on prefix information
34882cd038dSYoshinobu Inoue  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
34982cd038dSYoshinobu Inoue  */
35082cd038dSYoshinobu Inoue void
3511272577eSXin LI nd6_ra_input(struct mbuf *m, int off, int icmp6len)
35282cd038dSYoshinobu Inoue {
353503f4e47SBjoern A. Zeeb 	struct ifnet *ifp;
354503f4e47SBjoern A. Zeeb 	struct nd_ifinfo *ndi;
355503f4e47SBjoern A. Zeeb 	struct ip6_hdr *ip6;
356686cdd19SJun-ichiro itojun Hagino 	struct nd_router_advert *nd_ra;
357503f4e47SBjoern A. Zeeb 	struct in6_addr saddr6;
35882cd038dSYoshinobu Inoue 	struct nd_defrouter *dr;
359503f4e47SBjoern A. Zeeb 	union nd_opts ndopts;
3601d54aa3bSBjoern A. Zeeb 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
361503f4e47SBjoern A. Zeeb 	int mcast;
3624de485feSMark Johnston 
36307cf047dSHajimu UMEMOTO 	/*
364e7fa8d0aSHiroki Sato 	 * We only accept RAs only when the per-interface flag
365e7fa8d0aSHiroki Sato 	 * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
36607cf047dSHajimu UMEMOTO 	 */
367503f4e47SBjoern A. Zeeb 	ifp = m->m_pkthdr.rcvif;
368503f4e47SBjoern A. Zeeb 	ndi = ND_IFINFO(ifp);
369e7fa8d0aSHiroki Sato 	if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
37007cf047dSHajimu UMEMOTO 		goto freeit;
37182cd038dSYoshinobu Inoue 
372a4061289SAndrey V. Elsukov 	/* RFC 6980: Nodes MUST silently ignore fragments */
373a4061289SAndrey V. Elsukov 	if(m->m_flags & M_FRAGMENTED)
374a4061289SAndrey V. Elsukov 		goto freeit;
375a4061289SAndrey V. Elsukov 
376503f4e47SBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
37782cd038dSYoshinobu Inoue 	if (ip6->ip6_hlim != 255) {
37833841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
37933841545SHajimu UMEMOTO 		    "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
3801d54aa3bSBjoern A. Zeeb 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
3811d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
38233841545SHajimu UMEMOTO 		goto bad;
38382cd038dSYoshinobu Inoue 	}
38482cd038dSYoshinobu Inoue 
385503f4e47SBjoern A. Zeeb 	saddr6 = ip6->ip6_src;
38682cd038dSYoshinobu Inoue 	if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
38733841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
38882cd038dSYoshinobu Inoue 		    "nd6_ra_input: src %s is not link-local\n",
3891d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6bufs, &saddr6)));
39033841545SHajimu UMEMOTO 		goto bad;
391686cdd19SJun-ichiro itojun Hagino 	}
392686cdd19SJun-ichiro itojun Hagino 
393*a4adf6ccSBjoern A. Zeeb 	if (m->m_len < off + icmp6len) {
394a61b5cfbSBjoern A. Zeeb 		m = m_pullup(m, off + icmp6len);
395a61b5cfbSBjoern A. Zeeb 		if (m == NULL) {
396a61b5cfbSBjoern A. Zeeb 			IP6STAT_INC(ip6s_exthdrtoolong);
39782cd038dSYoshinobu Inoue 			return;
39882cd038dSYoshinobu Inoue 		}
399*a4adf6ccSBjoern A. Zeeb 	}
400a61b5cfbSBjoern A. Zeeb 	ip6 = mtod(m, struct ip6_hdr *);
401a61b5cfbSBjoern A. Zeeb 	nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
40282cd038dSYoshinobu Inoue 
40382cd038dSYoshinobu Inoue 	icmp6len -= sizeof(*nd_ra);
40482cd038dSYoshinobu Inoue 	nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
40582cd038dSYoshinobu Inoue 	if (nd6_options(&ndopts) < 0) {
40633841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
40733841545SHajimu UMEMOTO 		    "nd6_ra_input: invalid ND option, ignored\n"));
40833841545SHajimu UMEMOTO 		/* nd6_options have incremented stats */
409686cdd19SJun-ichiro itojun Hagino 		goto freeit;
41082cd038dSYoshinobu Inoue 	}
41182cd038dSYoshinobu Inoue 
412503f4e47SBjoern A. Zeeb 	mcast = 0;
413503f4e47SBjoern A. Zeeb 	dr = NULL;
41482cd038dSYoshinobu Inoue     {
41582cd038dSYoshinobu Inoue 	struct nd_defrouter dr0;
41682cd038dSYoshinobu Inoue 	u_int32_t advreachable = nd_ra->nd_ra_reachable;
41782cd038dSYoshinobu Inoue 
418743eee66SSUZUKI Shinsuke 	/* remember if this is a multicasted advertisement */
419743eee66SSUZUKI Shinsuke 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
420743eee66SSUZUKI Shinsuke 		mcast = 1;
421743eee66SSUZUKI Shinsuke 
422743eee66SSUZUKI Shinsuke 	bzero(&dr0, sizeof(dr0));
42382cd038dSYoshinobu Inoue 	dr0.rtaddr = saddr6;
42401869be5SMark Johnston 	dr0.raflags = nd_ra->nd_ra_flags_reserved;
425e7fa8d0aSHiroki Sato 	/*
426049087a0SHiroki Sato 	 * Effectively-disable routes from RA messages when
427049087a0SHiroki Sato 	 * ND6_IFF_NO_RADR enabled on the receiving interface or
428049087a0SHiroki Sato 	 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
429e7fa8d0aSHiroki Sato 	 */
430049087a0SHiroki Sato 	if (ndi->flags & ND6_IFF_NO_RADR)
431049087a0SHiroki Sato 		dr0.rtlifetime = 0;
432049087a0SHiroki Sato 	else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
433e7fa8d0aSHiroki Sato 		dr0.rtlifetime = 0;
434e7fa8d0aSHiroki Sato 	else
43582cd038dSYoshinobu Inoue 		dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
4367d26db17SHiroki Sato 	dr0.expire = time_uptime + dr0.rtlifetime;
43782cd038dSYoshinobu Inoue 	dr0.ifp = ifp;
43882cd038dSYoshinobu Inoue 	/* unspecified or not? (RFC 2461 6.3.4) */
43982cd038dSYoshinobu Inoue 	if (advreachable) {
440fd8e4ebcSMike Barcroft 		advreachable = ntohl(advreachable);
44182cd038dSYoshinobu Inoue 		if (advreachable <= MAX_REACHABLE_TIME &&
44282cd038dSYoshinobu Inoue 		    ndi->basereachable != advreachable) {
44382cd038dSYoshinobu Inoue 			ndi->basereachable = advreachable;
44482cd038dSYoshinobu Inoue 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
445603724d3SBjoern A. Zeeb 			ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
44682cd038dSYoshinobu Inoue 		}
44782cd038dSYoshinobu Inoue 	}
44882cd038dSYoshinobu Inoue 	if (nd_ra->nd_ra_retransmit)
44982cd038dSYoshinobu Inoue 		ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
450dd385660SXin LI 	if (nd_ra->nd_ra_curhoplimit) {
451dd385660SXin LI 		if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
45282cd038dSYoshinobu Inoue 			ndi->chlim = nd_ra->nd_ra_curhoplimit;
453dd385660SXin LI 		else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
454dd385660SXin LI 			log(LOG_ERR, "RA with a lower CurHopLimit sent from "
455dd385660SXin LI 			    "%s on %s (current = %d, received = %d). "
456dd385660SXin LI 			    "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
457dd385660SXin LI 			    if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
458dd385660SXin LI 		}
459dd385660SXin LI 	}
46082cd038dSYoshinobu Inoue 	dr = defrtrlist_update(&dr0);
461201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
462201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(ifp);
463201100c5SBjoern A. Zeeb #endif
46482cd038dSYoshinobu Inoue     }
46582cd038dSYoshinobu Inoue 
46682cd038dSYoshinobu Inoue 	/*
46782cd038dSYoshinobu Inoue 	 * prefix
46882cd038dSYoshinobu Inoue 	 */
46982cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_pi) {
47082cd038dSYoshinobu Inoue 		struct nd_opt_hdr *pt;
47133841545SHajimu UMEMOTO 		struct nd_opt_prefix_info *pi = NULL;
472743eee66SSUZUKI Shinsuke 		struct nd_prefixctl pr;
47382cd038dSYoshinobu Inoue 
47482cd038dSYoshinobu Inoue 		for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
47582cd038dSYoshinobu Inoue 		     pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
47682cd038dSYoshinobu Inoue 		     pt = (struct nd_opt_hdr *)((caddr_t)pt +
47782cd038dSYoshinobu Inoue 						(pt->nd_opt_len << 3))) {
47882cd038dSYoshinobu Inoue 			if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
47982cd038dSYoshinobu Inoue 				continue;
48082cd038dSYoshinobu Inoue 			pi = (struct nd_opt_prefix_info *)pt;
48182cd038dSYoshinobu Inoue 
48282cd038dSYoshinobu Inoue 			if (pi->nd_opt_pi_len != 4) {
48333841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
48433841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid option "
48582cd038dSYoshinobu Inoue 				    "len %d for prefix information option, "
48633841545SHajimu UMEMOTO 				    "ignored\n", pi->nd_opt_pi_len));
48782cd038dSYoshinobu Inoue 				continue;
48882cd038dSYoshinobu Inoue 			}
48982cd038dSYoshinobu Inoue 
49082cd038dSYoshinobu Inoue 			if (128 < pi->nd_opt_pi_prefix_len) {
49133841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
49233841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid prefix "
49382cd038dSYoshinobu Inoue 				    "len %d for prefix information option, "
49433841545SHajimu UMEMOTO 				    "ignored\n", pi->nd_opt_pi_prefix_len));
49582cd038dSYoshinobu Inoue 				continue;
49682cd038dSYoshinobu Inoue 			}
49782cd038dSYoshinobu Inoue 
49882cd038dSYoshinobu Inoue 			if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
49982cd038dSYoshinobu Inoue 			 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
50033841545SHajimu UMEMOTO 				nd6log((LOG_INFO,
50133841545SHajimu UMEMOTO 				    "nd6_ra_input: invalid prefix "
50282cd038dSYoshinobu Inoue 				    "%s, ignored\n",
5031d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6bufs,
5041d54aa3bSBjoern A. Zeeb 					&pi->nd_opt_pi_prefix)));
50582cd038dSYoshinobu Inoue 				continue;
50682cd038dSYoshinobu Inoue 			}
50782cd038dSYoshinobu Inoue 
50882cd038dSYoshinobu Inoue 			bzero(&pr, sizeof(pr));
50982cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_family = AF_INET6;
51082cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
51182cd038dSYoshinobu Inoue 			pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
51282cd038dSYoshinobu Inoue 			pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
51382cd038dSYoshinobu Inoue 
51482cd038dSYoshinobu Inoue 			pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
51582cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
51682cd038dSYoshinobu Inoue 			pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
51782cd038dSYoshinobu Inoue 			    ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
51882cd038dSYoshinobu Inoue 			pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
51982cd038dSYoshinobu Inoue 			pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
52007eb2995SHajimu UMEMOTO 			pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
521743eee66SSUZUKI Shinsuke 			(void)prelist_update(&pr, dr, m, mcast);
52282cd038dSYoshinobu Inoue 		}
52382cd038dSYoshinobu Inoue 	}
5244de485feSMark Johnston 	if (dr != NULL) {
5254de485feSMark Johnston 		defrouter_rele(dr);
5264de485feSMark Johnston 		dr = NULL;
5274de485feSMark Johnston 	}
52882cd038dSYoshinobu Inoue 
52982cd038dSYoshinobu Inoue 	/*
53082cd038dSYoshinobu Inoue 	 * MTU
53182cd038dSYoshinobu Inoue 	 */
53282cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
53331b3783cSHajimu UMEMOTO 		u_long mtu;
53431b3783cSHajimu UMEMOTO 		u_long maxmtu;
53507eb2995SHajimu UMEMOTO 
53631b3783cSHajimu UMEMOTO 		mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
53782cd038dSYoshinobu Inoue 
53882cd038dSYoshinobu Inoue 		/* lower bound */
53982cd038dSYoshinobu Inoue 		if (mtu < IPV6_MMTU) {
54033841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
54131b3783cSHajimu UMEMOTO 			    "mtu=%lu sent from %s, ignoring\n",
5421d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
54382cd038dSYoshinobu Inoue 			goto skip;
54482cd038dSYoshinobu Inoue 		}
54582cd038dSYoshinobu Inoue 
54682cd038dSYoshinobu Inoue 		/* upper bound */
54731b3783cSHajimu UMEMOTO 		maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
54831b3783cSHajimu UMEMOTO 		    ? ndi->maxmtu : ifp->if_mtu;
54931b3783cSHajimu UMEMOTO 		if (mtu <= maxmtu) {
55082cd038dSYoshinobu Inoue 			int change = (ndi->linkmtu != mtu);
55182cd038dSYoshinobu Inoue 
55282cd038dSYoshinobu Inoue 			ndi->linkmtu = mtu;
5530bbfb20fSEric van Gyzen 			if (change) {
5540bbfb20fSEric van Gyzen 				/* in6_maxmtu may change */
55582cd038dSYoshinobu Inoue 				in6_setmaxmtu();
5560bbfb20fSEric van Gyzen 				rt_updatemtu(ifp);
5570bbfb20fSEric van Gyzen 			}
55882cd038dSYoshinobu Inoue 		} else {
55933841545SHajimu UMEMOTO 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
56031b3783cSHajimu UMEMOTO 			    "mtu=%lu sent from %s; "
56131b3783cSHajimu UMEMOTO 			    "exceeds maxmtu %lu, ignoring\n",
5621d54aa3bSBjoern A. Zeeb 			    mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
56382cd038dSYoshinobu Inoue 		}
56482cd038dSYoshinobu Inoue 	}
56582cd038dSYoshinobu Inoue 
56682cd038dSYoshinobu Inoue  skip:
56782cd038dSYoshinobu Inoue 
56882cd038dSYoshinobu Inoue 	/*
56988ff5695SSUZUKI Shinsuke 	 * Source link layer address
57082cd038dSYoshinobu Inoue 	 */
57182cd038dSYoshinobu Inoue     {
57282cd038dSYoshinobu Inoue 	char *lladdr = NULL;
57382cd038dSYoshinobu Inoue 	int lladdrlen = 0;
57482cd038dSYoshinobu Inoue 
57582cd038dSYoshinobu Inoue 	if (ndopts.nd_opts_src_lladdr) {
57682cd038dSYoshinobu Inoue 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
57782cd038dSYoshinobu Inoue 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
57882cd038dSYoshinobu Inoue 	}
57982cd038dSYoshinobu Inoue 
58082cd038dSYoshinobu Inoue 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
58133841545SHajimu UMEMOTO 		nd6log((LOG_INFO,
58282cd038dSYoshinobu Inoue 		    "nd6_ra_input: lladdrlen mismatch for %s "
5831d54aa3bSBjoern A. Zeeb 		    "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
58407eb2995SHajimu UMEMOTO 		    ifp->if_addrlen, lladdrlen - 2));
58533841545SHajimu UMEMOTO 		goto bad;
58682cd038dSYoshinobu Inoue 	}
58782cd038dSYoshinobu Inoue 
58807eb2995SHajimu UMEMOTO 	nd6_cache_lladdr(ifp, &saddr6, lladdr,
58907eb2995SHajimu UMEMOTO 	    lladdrlen, ND_ROUTER_ADVERT, 0);
590686cdd19SJun-ichiro itojun Hagino 
591686cdd19SJun-ichiro itojun Hagino 	/*
592686cdd19SJun-ichiro itojun Hagino 	 * Installing a link-layer address might change the state of the
593686cdd19SJun-ichiro itojun Hagino 	 * router's neighbor cache, which might also affect our on-link
594686cdd19SJun-ichiro itojun Hagino 	 * detection of adveritsed prefixes.
595686cdd19SJun-ichiro itojun Hagino 	 */
596686cdd19SJun-ichiro itojun Hagino 	pfxlist_onlink_check();
59782cd038dSYoshinobu Inoue     }
598686cdd19SJun-ichiro itojun Hagino 
599686cdd19SJun-ichiro itojun Hagino  freeit:
600686cdd19SJun-ichiro itojun Hagino 	m_freem(m);
60133841545SHajimu UMEMOTO 	return;
60233841545SHajimu UMEMOTO 
60333841545SHajimu UMEMOTO  bad:
604e27b0c87SRobert Watson 	ICMP6STAT_INC(icp6s_badra);
60533841545SHajimu UMEMOTO 	m_freem(m);
60682cd038dSYoshinobu Inoue }
60782cd038dSYoshinobu Inoue 
608686cdd19SJun-ichiro itojun Hagino /* tell the change to user processes watching the routing socket. */
609686cdd19SJun-ichiro itojun Hagino static void
6101272577eSXin LI nd6_rtmsg(int cmd, struct rtentry *rt)
611686cdd19SJun-ichiro itojun Hagino {
612686cdd19SJun-ichiro itojun Hagino 	struct rt_addrinfo info;
6131e6a4139SRobert Watson 	struct ifnet *ifp;
6148c0fec80SRobert Watson 	struct ifaddr *ifa;
615686cdd19SJun-ichiro itojun Hagino 
616686cdd19SJun-ichiro itojun Hagino 	bzero((caddr_t)&info, sizeof(info));
617686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_DST] = rt_key(rt);
618686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
619686cdd19SJun-ichiro itojun Hagino 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
6201e6a4139SRobert Watson 	ifp = rt->rt_ifp;
6211e6a4139SRobert Watson 	if (ifp != NULL) {
622a68cc388SGleb Smirnoff 		struct epoch_tracker et;
623a68cc388SGleb Smirnoff 
624a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
625d7c5a620SMatt Macy 		ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
6268c0fec80SRobert Watson 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
6278c0fec80SRobert Watson 		ifa_ref(ifa);
628a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
62933841545SHajimu UMEMOTO 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
6308c0fec80SRobert Watson 	} else
6318c0fec80SRobert Watson 		ifa = NULL;
632686cdd19SJun-ichiro itojun Hagino 
63381d5d46bSBjoern A. Zeeb 	rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
6348c0fec80SRobert Watson 	if (ifa != NULL)
6358c0fec80SRobert Watson 		ifa_free(ifa);
636686cdd19SJun-ichiro itojun Hagino }
637686cdd19SJun-ichiro itojun Hagino 
638f77a6dbdSBjoern A. Zeeb /* PFXRTR */
639f77a6dbdSBjoern A. Zeeb static struct nd_pfxrouter *
640f77a6dbdSBjoern A. Zeeb pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
641f77a6dbdSBjoern A. Zeeb {
642f77a6dbdSBjoern A. Zeeb 	struct nd_pfxrouter *search;
6434de485feSMark Johnston 
644f77a6dbdSBjoern A. Zeeb 	ND6_LOCK_ASSERT();
645f77a6dbdSBjoern A. Zeeb 
646f77a6dbdSBjoern A. Zeeb 	LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
647f77a6dbdSBjoern A. Zeeb 		if (search->router == dr)
648f77a6dbdSBjoern A. Zeeb 			break;
649f77a6dbdSBjoern A. Zeeb 	}
650f77a6dbdSBjoern A. Zeeb 	return (search);
651f77a6dbdSBjoern A. Zeeb }
652f77a6dbdSBjoern A. Zeeb 
653f77a6dbdSBjoern A. Zeeb static void
654f77a6dbdSBjoern A. Zeeb pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
655f77a6dbdSBjoern A. Zeeb {
656f77a6dbdSBjoern A. Zeeb 	struct nd_pfxrouter *new;
657f77a6dbdSBjoern A. Zeeb 	bool update;
658f77a6dbdSBjoern A. Zeeb 
659f77a6dbdSBjoern A. Zeeb 	ND6_UNLOCK_ASSERT();
660f77a6dbdSBjoern A. Zeeb 
661f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
662f77a6dbdSBjoern A. Zeeb 	if (pfxrtr_lookup(pr, dr) != NULL) {
663f77a6dbdSBjoern A. Zeeb 		ND6_RUNLOCK();
664f77a6dbdSBjoern A. Zeeb 		return;
665f77a6dbdSBjoern A. Zeeb 	}
666f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
667f77a6dbdSBjoern A. Zeeb 
668f77a6dbdSBjoern A. Zeeb 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
669f77a6dbdSBjoern A. Zeeb 	if (new == NULL)
670f77a6dbdSBjoern A. Zeeb 		return;
671f77a6dbdSBjoern A. Zeeb 	defrouter_ref(dr);
672f77a6dbdSBjoern A. Zeeb 	new->router = dr;
673f77a6dbdSBjoern A. Zeeb 
674f77a6dbdSBjoern A. Zeeb 	ND6_WLOCK();
675f77a6dbdSBjoern A. Zeeb 	if (pfxrtr_lookup(pr, dr) == NULL) {
676f77a6dbdSBjoern A. Zeeb 		LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
677f77a6dbdSBjoern A. Zeeb 		update = true;
678f77a6dbdSBjoern A. Zeeb 	} else {
679f77a6dbdSBjoern A. Zeeb 		/* We lost a race to add the reference. */
680f77a6dbdSBjoern A. Zeeb 		defrouter_rele(dr);
681f77a6dbdSBjoern A. Zeeb 		free(new, M_IP6NDP);
682f77a6dbdSBjoern A. Zeeb 		update = false;
683f77a6dbdSBjoern A. Zeeb 	}
684f77a6dbdSBjoern A. Zeeb 	ND6_WUNLOCK();
685f77a6dbdSBjoern A. Zeeb 
686f77a6dbdSBjoern A. Zeeb 	if (update)
687f77a6dbdSBjoern A. Zeeb 		pfxlist_onlink_check();
688f77a6dbdSBjoern A. Zeeb }
689f77a6dbdSBjoern A. Zeeb 
690f77a6dbdSBjoern A. Zeeb static void
691f77a6dbdSBjoern A. Zeeb pfxrtr_del(struct nd_pfxrouter *pfr)
692f77a6dbdSBjoern A. Zeeb {
693f77a6dbdSBjoern A. Zeeb 
694f77a6dbdSBjoern A. Zeeb 	ND6_WLOCK_ASSERT();
695f77a6dbdSBjoern A. Zeeb 
696f77a6dbdSBjoern A. Zeeb 	LIST_REMOVE(pfr, pfr_entry);
697f77a6dbdSBjoern A. Zeeb 	defrouter_rele(pfr->router);
698f77a6dbdSBjoern A. Zeeb 	free(pfr, M_IP6NDP);
699f77a6dbdSBjoern A. Zeeb }
700f77a6dbdSBjoern A. Zeeb 
701f77a6dbdSBjoern A. Zeeb 
702f77a6dbdSBjoern A. Zeeb /* Default router list processing sub routines. */
7039d9b92f2SBjoern A. Zeeb static void
7041272577eSXin LI defrouter_addreq(struct nd_defrouter *new)
70582cd038dSYoshinobu Inoue {
70682cd038dSYoshinobu Inoue 	struct sockaddr_in6 def, mask, gate;
707686cdd19SJun-ichiro itojun Hagino 	struct rtentry *newrt = NULL;
708743eee66SSUZUKI Shinsuke 	int error;
70982cd038dSYoshinobu Inoue 
710056c7327SLuigi Rizzo 	bzero(&def, sizeof(def));
711056c7327SLuigi Rizzo 	bzero(&mask, sizeof(mask));
712056c7327SLuigi Rizzo 	bzero(&gate, sizeof(gate));
71382cd038dSYoshinobu Inoue 
71407eb2995SHajimu UMEMOTO 	def.sin6_len = mask.sin6_len = gate.sin6_len =
71507eb2995SHajimu UMEMOTO 	    sizeof(struct sockaddr_in6);
716743eee66SSUZUKI Shinsuke 	def.sin6_family = gate.sin6_family = AF_INET6;
71782cd038dSYoshinobu Inoue 	gate.sin6_addr = new->rtaddr;
71882cd038dSYoshinobu Inoue 
71981d5d46bSBjoern A. Zeeb 	error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
72082cd038dSYoshinobu Inoue 	    (struct sockaddr *)&gate, (struct sockaddr *)&mask,
721559b4296SAlan Somers 	    RTF_GATEWAY, &newrt, new->ifp->if_fib);
722686cdd19SJun-ichiro itojun Hagino 	if (newrt) {
72333841545SHajimu UMEMOTO 		nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
7246e6b3f7cSQing Li 		RTFREE(newrt);
725686cdd19SJun-ichiro itojun Hagino 	}
726743eee66SSUZUKI Shinsuke 	if (error == 0)
727743eee66SSUZUKI Shinsuke 		new->installed = 1;
72882cd038dSYoshinobu Inoue }
72982cd038dSYoshinobu Inoue 
730743eee66SSUZUKI Shinsuke /*
731743eee66SSUZUKI Shinsuke  * Remove the default route for a given router.
732559b4296SAlan Somers  * This is just a subroutine function for defrouter_select_fib(), and
733559b4296SAlan Somers  * should not be called from anywhere else.
734743eee66SSUZUKI Shinsuke  */
735743eee66SSUZUKI Shinsuke static void
7361272577eSXin LI defrouter_delreq(struct nd_defrouter *dr)
73782cd038dSYoshinobu Inoue {
73882cd038dSYoshinobu Inoue 	struct sockaddr_in6 def, mask, gate;
739686cdd19SJun-ichiro itojun Hagino 	struct rtentry *oldrt = NULL;
74082cd038dSYoshinobu Inoue 
741056c7327SLuigi Rizzo 	bzero(&def, sizeof(def));
742056c7327SLuigi Rizzo 	bzero(&mask, sizeof(mask));
743056c7327SLuigi Rizzo 	bzero(&gate, sizeof(gate));
74482cd038dSYoshinobu Inoue 
74507eb2995SHajimu UMEMOTO 	def.sin6_len = mask.sin6_len = gate.sin6_len =
74607eb2995SHajimu UMEMOTO 	    sizeof(struct sockaddr_in6);
747743eee66SSUZUKI Shinsuke 	def.sin6_family = gate.sin6_family = AF_INET6;
74882cd038dSYoshinobu Inoue 	gate.sin6_addr = dr->rtaddr;
74982cd038dSYoshinobu Inoue 
75081d5d46bSBjoern A. Zeeb 	in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
75182cd038dSYoshinobu Inoue 	    (struct sockaddr *)&gate,
752559b4296SAlan Somers 	    (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, dr->ifp->if_fib);
753686cdd19SJun-ichiro itojun Hagino 	if (oldrt) {
75433841545SHajimu UMEMOTO 		nd6_rtmsg(RTM_DELETE, oldrt);
75571eba915SRuslan Ermilov 		RTFREE(oldrt);
75620220070SJun-ichiro itojun Hagino 	}
75782cd038dSYoshinobu Inoue 
758743eee66SSUZUKI Shinsuke 	dr->installed = 0;
759743eee66SSUZUKI Shinsuke }
760743eee66SSUZUKI Shinsuke 
761ad675b32SBjoern A. Zeeb static void
7624de485feSMark Johnston defrouter_del(struct nd_defrouter *dr)
76382cd038dSYoshinobu Inoue {
76482cd038dSYoshinobu Inoue 	struct nd_defrouter *deldr = NULL;
76582cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
766d748f7efSMark Johnston 	struct nd_pfxrouter *pfxrtr;
76782cd038dSYoshinobu Inoue 
7684de485feSMark Johnston 	ND6_UNLOCK_ASSERT();
7694de485feSMark Johnston 
77082cd038dSYoshinobu Inoue 	/*
77182cd038dSYoshinobu Inoue 	 * Flush all the routing table entries that use the router
77282cd038dSYoshinobu Inoue 	 * as a next hop.
77382cd038dSYoshinobu Inoue 	 */
774e7fa8d0aSHiroki Sato 	if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
77582cd038dSYoshinobu Inoue 		rt6_flush(&dr->rtaddr, dr->ifp);
77682cd038dSYoshinobu Inoue 
777201100c5SBjoern A. Zeeb #ifdef EXPERIMENTAL
778201100c5SBjoern A. Zeeb 	defrtr_ipv6_only_ifp(dr->ifp);
779201100c5SBjoern A. Zeeb #endif
780201100c5SBjoern A. Zeeb 
781743eee66SSUZUKI Shinsuke 	if (dr->installed) {
782743eee66SSUZUKI Shinsuke 		deldr = dr;
783743eee66SSUZUKI Shinsuke 		defrouter_delreq(dr);
784743eee66SSUZUKI Shinsuke 	}
78582cd038dSYoshinobu Inoue 
78682cd038dSYoshinobu Inoue 	/*
78782cd038dSYoshinobu Inoue 	 * Also delete all the pointers to the router in each prefix lists.
78882cd038dSYoshinobu Inoue 	 */
789d748f7efSMark Johnston 	ND6_WLOCK();
7903b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
79182cd038dSYoshinobu Inoue 		if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
79282cd038dSYoshinobu Inoue 			pfxrtr_del(pfxrtr);
79382cd038dSYoshinobu Inoue 	}
794d748f7efSMark Johnston 	ND6_WUNLOCK();
795d748f7efSMark Johnston 
79682cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
79782cd038dSYoshinobu Inoue 
79882cd038dSYoshinobu Inoue 	/*
799686cdd19SJun-ichiro itojun Hagino 	 * If the router is the primary one, choose a new one.
800559b4296SAlan Somers 	 * Note that defrouter_select_fib() will remove the current
801559b4296SAlan Somers          * gateway from the routing table.
80282cd038dSYoshinobu Inoue 	 */
80382cd038dSYoshinobu Inoue 	if (deldr)
804559b4296SAlan Somers 		defrouter_select_fib(deldr->ifp->if_fib);
805686cdd19SJun-ichiro itojun Hagino 
8064de485feSMark Johnston 	/*
8074de485feSMark Johnston 	 * Release the list reference.
8084de485feSMark Johnston 	 */
8094de485feSMark Johnston 	defrouter_rele(dr);
81082cd038dSYoshinobu Inoue }
81182cd038dSYoshinobu Inoue 
812ad675b32SBjoern A. Zeeb 
813f77a6dbdSBjoern A. Zeeb struct nd_defrouter *
814f77a6dbdSBjoern A. Zeeb defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
815f77a6dbdSBjoern A. Zeeb {
816f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
817f77a6dbdSBjoern A. Zeeb 
818f77a6dbdSBjoern A. Zeeb 	ND6_LOCK_ASSERT();
819f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
820f77a6dbdSBjoern A. Zeeb 		if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
821f77a6dbdSBjoern A. Zeeb 			defrouter_ref(dr);
822f77a6dbdSBjoern A. Zeeb 			return (dr);
823f77a6dbdSBjoern A. Zeeb 		}
824f77a6dbdSBjoern A. Zeeb 	return (NULL);
825f77a6dbdSBjoern A. Zeeb }
826f77a6dbdSBjoern A. Zeeb 
827f77a6dbdSBjoern A. Zeeb struct nd_defrouter *
828f77a6dbdSBjoern A. Zeeb defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
829f77a6dbdSBjoern A. Zeeb {
830f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
831f77a6dbdSBjoern A. Zeeb 
832f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
833f77a6dbdSBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
834f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
835f77a6dbdSBjoern A. Zeeb 	return (dr);
836f77a6dbdSBjoern A. Zeeb }
837f77a6dbdSBjoern A. Zeeb 
838f77a6dbdSBjoern A. Zeeb /*
839f77a6dbdSBjoern A. Zeeb  * Remove all default routes from default router list.
840f77a6dbdSBjoern A. Zeeb  */
841f77a6dbdSBjoern A. Zeeb void
842f77a6dbdSBjoern A. Zeeb defrouter_reset(void)
843f77a6dbdSBjoern A. Zeeb {
844f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr, **dra;
845f77a6dbdSBjoern A. Zeeb 	int count, i;
846f77a6dbdSBjoern A. Zeeb 
847f77a6dbdSBjoern A. Zeeb 	count = i = 0;
848f77a6dbdSBjoern A. Zeeb 
849f77a6dbdSBjoern A. Zeeb 	/*
850f77a6dbdSBjoern A. Zeeb 	 * We can't delete routes with the ND lock held, so make a copy of the
851f77a6dbdSBjoern A. Zeeb 	 * current default router list and use that when deleting routes.
852f77a6dbdSBjoern A. Zeeb 	 */
853f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
854f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry)
855f77a6dbdSBjoern A. Zeeb 		count++;
856f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
857f77a6dbdSBjoern A. Zeeb 
858f77a6dbdSBjoern A. Zeeb 	dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
859f77a6dbdSBjoern A. Zeeb 
860f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
861f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
862f77a6dbdSBjoern A. Zeeb 		if (i == count)
863f77a6dbdSBjoern A. Zeeb 			break;
864f77a6dbdSBjoern A. Zeeb 		defrouter_ref(dr);
865f77a6dbdSBjoern A. Zeeb 		dra[i++] = dr;
866f77a6dbdSBjoern A. Zeeb 	}
867f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
868f77a6dbdSBjoern A. Zeeb 
869f77a6dbdSBjoern A. Zeeb 	for (i = 0; i < count && dra[i] != NULL; i++) {
870f77a6dbdSBjoern A. Zeeb 		defrouter_delreq(dra[i]);
871f77a6dbdSBjoern A. Zeeb 		defrouter_rele(dra[i]);
872f77a6dbdSBjoern A. Zeeb 	}
873f77a6dbdSBjoern A. Zeeb 	free(dra, M_TEMP);
874f77a6dbdSBjoern A. Zeeb 
875f77a6dbdSBjoern A. Zeeb 	/*
876f77a6dbdSBjoern A. Zeeb 	 * XXX should we also nuke any default routers in the kernel, by
877f77a6dbdSBjoern A. Zeeb 	 * going through them by rtalloc1()?
878f77a6dbdSBjoern A. Zeeb 	 */
879f77a6dbdSBjoern A. Zeeb }
880f77a6dbdSBjoern A. Zeeb 
881ad675b32SBjoern A. Zeeb /*
882ad675b32SBjoern A. Zeeb  * Look up a matching default router list entry and remove it. Returns true if a
883ad675b32SBjoern A. Zeeb  * matching entry was found, false otherwise.
884ad675b32SBjoern A. Zeeb  */
885ad675b32SBjoern A. Zeeb bool
886ad675b32SBjoern A. Zeeb defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
887ad675b32SBjoern A. Zeeb {
888ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
889ad675b32SBjoern A. Zeeb 
890ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
891ad675b32SBjoern A. Zeeb 	dr = defrouter_lookup_locked(addr, ifp);
892ad675b32SBjoern A. Zeeb 	if (dr == NULL) {
893ad675b32SBjoern A. Zeeb 		ND6_WUNLOCK();
894ad675b32SBjoern A. Zeeb 		return (false);
895ad675b32SBjoern A. Zeeb 	}
896ad675b32SBjoern A. Zeeb 
897ad675b32SBjoern A. Zeeb 	defrouter_unlink(dr, NULL);
898ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
899ad675b32SBjoern A. Zeeb 	defrouter_del(dr);
900ad675b32SBjoern A. Zeeb 	defrouter_rele(dr);
901ad675b32SBjoern A. Zeeb 	return (true);
902ad675b32SBjoern A. Zeeb }
903ad675b32SBjoern A. Zeeb 
904686cdd19SJun-ichiro itojun Hagino /*
905f77a6dbdSBjoern A. Zeeb  * for default router selection
906f77a6dbdSBjoern A. Zeeb  * regards router-preference field as a 2-bit signed integer
907f77a6dbdSBjoern A. Zeeb  */
908f77a6dbdSBjoern A. Zeeb static int
909f77a6dbdSBjoern A. Zeeb rtpref(struct nd_defrouter *dr)
910f77a6dbdSBjoern A. Zeeb {
911f77a6dbdSBjoern A. Zeeb 	switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
912f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_HIGH:
913f77a6dbdSBjoern A. Zeeb 		return (RTPREF_HIGH);
914f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_MEDIUM:
915f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_RSV:
916f77a6dbdSBjoern A. Zeeb 		return (RTPREF_MEDIUM);
917f77a6dbdSBjoern A. Zeeb 	case ND_RA_FLAG_RTPREF_LOW:
918f77a6dbdSBjoern A. Zeeb 		return (RTPREF_LOW);
919f77a6dbdSBjoern A. Zeeb 	default:
920f77a6dbdSBjoern A. Zeeb 		/*
921f77a6dbdSBjoern A. Zeeb 		 * This case should never happen.  If it did, it would mean a
922f77a6dbdSBjoern A. Zeeb 		 * serious bug of kernel internal.  We thus always bark here.
923f77a6dbdSBjoern A. Zeeb 		 * Or, can we even panic?
924f77a6dbdSBjoern A. Zeeb 		 */
925f77a6dbdSBjoern A. Zeeb 		log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
926f77a6dbdSBjoern A. Zeeb 		return (RTPREF_INVALID);
927f77a6dbdSBjoern A. Zeeb 	}
928f77a6dbdSBjoern A. Zeeb 	/* NOTREACHED */
929f77a6dbdSBjoern A. Zeeb }
930f77a6dbdSBjoern A. Zeeb 
931f77a6dbdSBjoern A. Zeeb /*
932743eee66SSUZUKI Shinsuke  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
933743eee66SSUZUKI Shinsuke  * draft-ietf-ipngwg-router-selection:
934743eee66SSUZUKI Shinsuke  * 1) Routers that are reachable or probably reachable should be preferred.
935743eee66SSUZUKI Shinsuke  *    If we have more than one (probably) reachable router, prefer ones
936743eee66SSUZUKI Shinsuke  *    with the highest router preference.
937686cdd19SJun-ichiro itojun Hagino  * 2) When no routers on the list are known to be reachable or
938686cdd19SJun-ichiro itojun Hagino  *    probably reachable, routers SHOULD be selected in a round-robin
939743eee66SSUZUKI Shinsuke  *    fashion, regardless of router preference values.
940686cdd19SJun-ichiro itojun Hagino  * 3) If the Default Router List is empty, assume that all
941686cdd19SJun-ichiro itojun Hagino  *    destinations are on-link.
942743eee66SSUZUKI Shinsuke  *
943743eee66SSUZUKI Shinsuke  * We assume nd_defrouter is sorted by router preference value.
944743eee66SSUZUKI Shinsuke  * Since the code below covers both with and without router preference cases,
945743eee66SSUZUKI Shinsuke  * we do not need to classify the cases by ifdef.
946743eee66SSUZUKI Shinsuke  *
947743eee66SSUZUKI Shinsuke  * At this moment, we do not try to install more than one default router,
948743eee66SSUZUKI Shinsuke  * even when the multipath routing is available, because we're not sure about
949743eee66SSUZUKI Shinsuke  * the benefits for stub hosts comparing to the risk of making the code
950743eee66SSUZUKI Shinsuke  * complicated and the possibility of introducing bugs.
951559b4296SAlan Somers  *
952559b4296SAlan Somers  * We maintain a single list of routers for multiple FIBs, only considering one
953559b4296SAlan Somers  * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
954559b4296SAlan Somers  * we do the whole thing multiple times.
955686cdd19SJun-ichiro itojun Hagino  */
956686cdd19SJun-ichiro itojun Hagino void
957559b4296SAlan Somers defrouter_select_fib(int fibnum)
958686cdd19SJun-ichiro itojun Hagino {
959a68cc388SGleb Smirnoff 	struct epoch_tracker et;
9604de485feSMark Johnston 	struct nd_defrouter *dr, *selected_dr, *installed_dr;
9616e6b3f7cSQing Li 	struct llentry *ln = NULL;
962686cdd19SJun-ichiro itojun Hagino 
963559b4296SAlan Somers 	if (fibnum == RT_ALL_FIBS) {
964559b4296SAlan Somers 		for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
965559b4296SAlan Somers 			defrouter_select_fib(fibnum);
966559b4296SAlan Somers 		}
967559b4296SAlan Somers 	}
968559b4296SAlan Somers 
9694de485feSMark Johnston 	ND6_RLOCK();
970686cdd19SJun-ichiro itojun Hagino 	/*
971743eee66SSUZUKI Shinsuke 	 * Let's handle easy case (3) first:
972743eee66SSUZUKI Shinsuke 	 * If default router list is empty, there's nothing to be done.
973743eee66SSUZUKI Shinsuke 	 */
974f592d0c3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_nd6_defrouter)) {
9754de485feSMark Johnston 		ND6_RUNLOCK();
976743eee66SSUZUKI Shinsuke 		return;
9774de485feSMark Johnston 	}
978743eee66SSUZUKI Shinsuke 
979743eee66SSUZUKI Shinsuke 	/*
980686cdd19SJun-ichiro itojun Hagino 	 * Search for a (probably) reachable router from the list.
981743eee66SSUZUKI Shinsuke 	 * We just pick up the first reachable one (if any), assuming that
982743eee66SSUZUKI Shinsuke 	 * the ordering rule of the list described in defrtrlist_update().
983686cdd19SJun-ichiro itojun Hagino 	 */
9844de485feSMark Johnston 	selected_dr = installed_dr = NULL;
985f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
986a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
987559b4296SAlan Somers 		if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
9886e6b3f7cSQing Li 		    (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
989686cdd19SJun-ichiro itojun Hagino 		    ND6_IS_LLINFO_PROBREACH(ln)) {
990743eee66SSUZUKI Shinsuke 			selected_dr = dr;
9914de485feSMark Johnston 			defrouter_ref(selected_dr);
992686cdd19SJun-ichiro itojun Hagino 		}
993a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
9945f16e341SBjoern A. Zeeb 		if (ln != NULL) {
995c0641cc0SKip Macy 			LLE_RUNLOCK(ln);
9965f16e341SBjoern A. Zeeb 			ln = NULL;
9975f16e341SBjoern A. Zeeb 		}
998686cdd19SJun-ichiro itojun Hagino 
999559b4296SAlan Somers 		if (dr->installed && dr->ifp->if_fib == fibnum) {
10004de485feSMark Johnston 			if (installed_dr == NULL) {
1001743eee66SSUZUKI Shinsuke 				installed_dr = dr;
10024de485feSMark Johnston 				defrouter_ref(installed_dr);
10034de485feSMark Johnston 			} else {
1004559b4296SAlan Somers 				/*
1005559b4296SAlan Somers 				 * this should not happen.
1006559b4296SAlan Somers 				 * warn for diagnosis.
1007559b4296SAlan Somers 				 */
1008559b4296SAlan Somers 				log(LOG_ERR, "defrouter_select_fib: more than "
1009559b4296SAlan Somers 				             "one router is installed\n");
10104de485feSMark Johnston 			}
101133841545SHajimu UMEMOTO 		}
1012686cdd19SJun-ichiro itojun Hagino 	}
1013743eee66SSUZUKI Shinsuke 	/*
1014743eee66SSUZUKI Shinsuke 	 * If none of the default routers was found to be reachable,
1015743eee66SSUZUKI Shinsuke 	 * round-robin the list regardless of preference.
1016743eee66SSUZUKI Shinsuke 	 * Otherwise, if we have an installed router, check if the selected
1017743eee66SSUZUKI Shinsuke 	 * (reachable) router should really be preferred to the installed one.
1018743eee66SSUZUKI Shinsuke 	 * We only prefer the new router when the old one is not reachable
1019743eee66SSUZUKI Shinsuke 	 * or when the new one has a really higher preference value.
1020743eee66SSUZUKI Shinsuke 	 */
1021743eee66SSUZUKI Shinsuke 	if (selected_dr == NULL) {
10224de485feSMark Johnston 		if (installed_dr == NULL ||
10234de485feSMark Johnston 		    TAILQ_NEXT(installed_dr, dr_entry) == NULL)
1024f592d0c3SBjoern A. Zeeb 			dr = TAILQ_FIRST(&V_nd6_defrouter);
1025743eee66SSUZUKI Shinsuke 		else
1026559b4296SAlan Somers 			dr = TAILQ_NEXT(installed_dr, dr_entry);
1027559b4296SAlan Somers 
1028559b4296SAlan Somers 		/* Ensure we select a router for this FIB. */
1029f592d0c3SBjoern A. Zeeb 		TAILQ_FOREACH_FROM(dr, &V_nd6_defrouter, dr_entry) {
1030559b4296SAlan Somers 			if (dr->ifp->if_fib == fibnum) {
1031559b4296SAlan Somers 				selected_dr = dr;
10324de485feSMark Johnston 				defrouter_ref(selected_dr);
1033559b4296SAlan Somers 				break;
1034559b4296SAlan Somers 			}
1035559b4296SAlan Somers 		}
10364de485feSMark Johnston 	} else if (installed_dr != NULL) {
1037a68cc388SGleb Smirnoff 		NET_EPOCH_ENTER(et);
1038559b4296SAlan Somers 		if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
1039559b4296SAlan Somers 		                     installed_dr->ifp)) &&
1040743eee66SSUZUKI Shinsuke 		    ND6_IS_LLINFO_PROBREACH(ln) &&
1041559b4296SAlan Somers 		    installed_dr->ifp->if_fib == fibnum &&
1042743eee66SSUZUKI Shinsuke 		    rtpref(selected_dr) <= rtpref(installed_dr)) {
10434de485feSMark Johnston 			defrouter_rele(selected_dr);
1044743eee66SSUZUKI Shinsuke 			selected_dr = installed_dr;
1045743eee66SSUZUKI Shinsuke 		}
1046a68cc388SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1047c0641cc0SKip Macy 		if (ln != NULL)
1048c0641cc0SKip Macy 			LLE_RUNLOCK(ln);
10496e6b3f7cSQing Li 	}
10504de485feSMark Johnston 	ND6_RUNLOCK();
1051743eee66SSUZUKI Shinsuke 
1052743eee66SSUZUKI Shinsuke 	/*
1053559b4296SAlan Somers 	 * If we selected a router for this FIB and it's different
1054559b4296SAlan Somers 	 * than the installed one, remove the installed router and
1055559b4296SAlan Somers 	 * install the selected one in its place.
1056743eee66SSUZUKI Shinsuke 	 */
1057743eee66SSUZUKI Shinsuke 	if (installed_dr != selected_dr) {
10584de485feSMark Johnston 		if (installed_dr != NULL) {
1059743eee66SSUZUKI Shinsuke 			defrouter_delreq(installed_dr);
10604de485feSMark Johnston 			defrouter_rele(installed_dr);
10614de485feSMark Johnston 		}
1062559b4296SAlan Somers 		if (selected_dr != NULL)
1063743eee66SSUZUKI Shinsuke 			defrouter_addreq(selected_dr);
1064686cdd19SJun-ichiro itojun Hagino 	}
1065559b4296SAlan Somers 	if (selected_dr != NULL)
10664de485feSMark Johnston 		defrouter_rele(selected_dr);
1067686cdd19SJun-ichiro itojun Hagino }
1068686cdd19SJun-ichiro itojun Hagino 
106982cd038dSYoshinobu Inoue static struct nd_defrouter *
10701272577eSXin LI defrtrlist_update(struct nd_defrouter *new)
107182cd038dSYoshinobu Inoue {
107282cd038dSYoshinobu Inoue 	struct nd_defrouter *dr, *n;
1073d748f7efSMark Johnston 	uint64_t genid;
1074a4a7c825SMark Johnston 	int oldpref;
1075d748f7efSMark Johnston 	bool writelocked;
107682cd038dSYoshinobu Inoue 
107782cd038dSYoshinobu Inoue 	if (new->rtlifetime == 0) {
1078ff63037dSMark Johnston 		defrouter_remove(&new->rtaddr, new->ifp);
1079a4a7c825SMark Johnston 		return (NULL);
1080a4a7c825SMark Johnston 	}
1081a4a7c825SMark Johnston 
1082d748f7efSMark Johnston 	ND6_RLOCK();
1083d748f7efSMark Johnston 	writelocked = false;
1084d748f7efSMark Johnston restart:
1085ff63037dSMark Johnston 	dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
1086ff63037dSMark Johnston 	if (dr != NULL) {
1087a4a7c825SMark Johnston 		oldpref = rtpref(dr);
1088743eee66SSUZUKI Shinsuke 
108982cd038dSYoshinobu Inoue 		/* override */
109001869be5SMark Johnston 		dr->raflags = new->raflags; /* XXX flag check */
109182cd038dSYoshinobu Inoue 		dr->rtlifetime = new->rtlifetime;
109282cd038dSYoshinobu Inoue 		dr->expire = new->expire;
1093743eee66SSUZUKI Shinsuke 
1094743eee66SSUZUKI Shinsuke 		/*
1095743eee66SSUZUKI Shinsuke 		 * If the preference does not change, there's no need
10960f1aca65SQing Li 		 * to sort the entries. Also make sure the selected
10970f1aca65SQing Li 		 * router is still installed in the kernel.
1098743eee66SSUZUKI Shinsuke 		 */
10994de485feSMark Johnston 		if (dr->installed && rtpref(new) == oldpref) {
1100d748f7efSMark Johnston 			if (writelocked)
11014de485feSMark Johnston 				ND6_WUNLOCK();
1102d748f7efSMark Johnston 			else
1103d748f7efSMark Johnston 				ND6_RUNLOCK();
1104743eee66SSUZUKI Shinsuke 			return (dr);
11054de485feSMark Johnston 		}
1106d748f7efSMark Johnston 	}
1107743eee66SSUZUKI Shinsuke 
1108743eee66SSUZUKI Shinsuke 	/*
1109d748f7efSMark Johnston 	 * The router needs to be reinserted into the default router
1110d748f7efSMark Johnston 	 * list, so upgrade to a write lock. If that fails and the list
1111d748f7efSMark Johnston 	 * has potentially changed while the lock was dropped, we'll
1112d748f7efSMark Johnston 	 * redo the lookup with the write lock held.
1113d748f7efSMark Johnston 	 */
1114d748f7efSMark Johnston 	if (!writelocked) {
1115d748f7efSMark Johnston 		writelocked = true;
1116d748f7efSMark Johnston 		if (!ND6_TRY_UPGRADE()) {
1117d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1118d748f7efSMark Johnston 			ND6_RUNLOCK();
1119d748f7efSMark Johnston 			ND6_WLOCK();
1120d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1121d748f7efSMark Johnston 				goto restart;
1122d748f7efSMark Johnston 		}
1123d748f7efSMark Johnston 	}
1124d748f7efSMark Johnston 
1125d748f7efSMark Johnston 	if (dr != NULL) {
1126d748f7efSMark Johnston 		/*
1127a4a7c825SMark Johnston 		 * The preferred router may have changed, so relocate this
1128a4a7c825SMark Johnston 		 * router.
1129743eee66SSUZUKI Shinsuke 		 */
1130f592d0c3SBjoern A. Zeeb 		TAILQ_REMOVE(&V_nd6_defrouter, dr, dr_entry);
1131743eee66SSUZUKI Shinsuke 		n = dr;
1132ff63037dSMark Johnston 	} else {
1133fc315641SMark Johnston 		n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
11344de485feSMark Johnston 		if (n == NULL) {
11354de485feSMark Johnston 			ND6_WUNLOCK();
113682cd038dSYoshinobu Inoue 			return (NULL);
11374de485feSMark Johnston 		}
1138fc315641SMark Johnston 		memcpy(n, new, sizeof(*n));
11394de485feSMark Johnston 		/* Initialize with an extra reference for the caller. */
11404de485feSMark Johnston 		refcount_init(&n->refcnt, 2);
1141ff63037dSMark Johnston 	}
1142686cdd19SJun-ichiro itojun Hagino 
1143686cdd19SJun-ichiro itojun Hagino 	/*
1144743eee66SSUZUKI Shinsuke 	 * Insert the new router in the Default Router List;
1145743eee66SSUZUKI Shinsuke 	 * The Default Router List should be in the descending order
1146743eee66SSUZUKI Shinsuke 	 * of router-preferece.  Routers with the same preference are
1147743eee66SSUZUKI Shinsuke 	 * sorted in the arriving time order.
1148686cdd19SJun-ichiro itojun Hagino 	 */
1149743eee66SSUZUKI Shinsuke 
1150743eee66SSUZUKI Shinsuke 	/* insert at the end of the group */
1151f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1152743eee66SSUZUKI Shinsuke 		if (rtpref(n) > rtpref(dr))
1153743eee66SSUZUKI Shinsuke 			break;
1154743eee66SSUZUKI Shinsuke 	}
11554de485feSMark Johnston 	if (dr != NULL)
1156743eee66SSUZUKI Shinsuke 		TAILQ_INSERT_BEFORE(dr, n, dr_entry);
1157743eee66SSUZUKI Shinsuke 	else
1158f592d0c3SBjoern A. Zeeb 		TAILQ_INSERT_TAIL(&V_nd6_defrouter, n, dr_entry);
1159d748f7efSMark Johnston 	V_nd6_list_genid++;
11604de485feSMark Johnston 	ND6_WUNLOCK();
1161743eee66SSUZUKI Shinsuke 
1162559b4296SAlan Somers 	defrouter_select_fib(new->ifp->if_fib);
1163743eee66SSUZUKI Shinsuke 
116482cd038dSYoshinobu Inoue 	return (n);
116582cd038dSYoshinobu Inoue }
116682cd038dSYoshinobu Inoue 
1167f77a6dbdSBjoern A. Zeeb static int
1168f77a6dbdSBjoern A. Zeeb in6_init_prefix_ltimes(struct nd_prefix *ndpr)
116982cd038dSYoshinobu Inoue {
1170f77a6dbdSBjoern A. Zeeb 	if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
1171f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_preferred = 0;
1172f77a6dbdSBjoern A. Zeeb 	else
1173f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
1174f77a6dbdSBjoern A. Zeeb 	if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1175f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_expire = 0;
1176f77a6dbdSBjoern A. Zeeb 	else
1177f77a6dbdSBjoern A. Zeeb 		ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
117882cd038dSYoshinobu Inoue 
1179f77a6dbdSBjoern A. Zeeb 	return 0;
118082cd038dSYoshinobu Inoue }
118182cd038dSYoshinobu Inoue 
118282cd038dSYoshinobu Inoue static void
1183f77a6dbdSBjoern A. Zeeb in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
118482cd038dSYoshinobu Inoue {
1185f77a6dbdSBjoern A. Zeeb 	/* init ia6t_expire */
1186f77a6dbdSBjoern A. Zeeb 	if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
1187f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire = 0;
1188f77a6dbdSBjoern A. Zeeb 	else {
1189f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire = time_uptime;
1190f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_expire += lt6->ia6t_vltime;
119182cd038dSYoshinobu Inoue 	}
119282cd038dSYoshinobu Inoue 
1193f77a6dbdSBjoern A. Zeeb 	/* init ia6t_preferred */
1194f77a6dbdSBjoern A. Zeeb 	if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
1195f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred = 0;
1196f77a6dbdSBjoern A. Zeeb 	else {
1197f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred = time_uptime;
1198f77a6dbdSBjoern A. Zeeb 		lt6->ia6t_preferred += lt6->ia6t_pltime;
1199f77a6dbdSBjoern A. Zeeb 	}
1200f77a6dbdSBjoern A. Zeeb }
1201f77a6dbdSBjoern A. Zeeb 
1202f77a6dbdSBjoern A. Zeeb static struct in6_ifaddr *
1203f77a6dbdSBjoern A. Zeeb in6_ifadd(struct nd_prefixctl *pr, int mcast)
120482cd038dSYoshinobu Inoue {
1205f77a6dbdSBjoern A. Zeeb 	struct ifnet *ifp = pr->ndpr_ifp;
1206f77a6dbdSBjoern A. Zeeb 	struct ifaddr *ifa;
1207f77a6dbdSBjoern A. Zeeb 	struct in6_aliasreq ifra;
1208f77a6dbdSBjoern A. Zeeb 	struct in6_ifaddr *ia, *ib;
1209f77a6dbdSBjoern A. Zeeb 	int error, plen0;
1210f77a6dbdSBjoern A. Zeeb 	struct in6_addr mask;
1211f77a6dbdSBjoern A. Zeeb 	int prefixlen = pr->ndpr_plen;
1212f77a6dbdSBjoern A. Zeeb 	int updateflags;
1213f77a6dbdSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
12144de485feSMark Johnston 
1215f77a6dbdSBjoern A. Zeeb 	in6_prefixlen2mask(&mask, prefixlen);
1216d748f7efSMark Johnston 
1217f77a6dbdSBjoern A. Zeeb 	/*
1218f77a6dbdSBjoern A. Zeeb 	 * find a link-local address (will be interface ID).
1219f77a6dbdSBjoern A. Zeeb 	 * Is it really mandatory? Theoretically, a global or a site-local
1220f77a6dbdSBjoern A. Zeeb 	 * address can be configured without a link-local address, if we
1221f77a6dbdSBjoern A. Zeeb 	 * have a unique interface identifier...
1222f77a6dbdSBjoern A. Zeeb 	 *
1223f77a6dbdSBjoern A. Zeeb 	 * it is not mandatory to have a link-local address, we can generate
1224f77a6dbdSBjoern A. Zeeb 	 * interface identifier on the fly.  we do this because:
1225f77a6dbdSBjoern A. Zeeb 	 * (1) it should be the easiest way to find interface identifier.
1226f77a6dbdSBjoern A. Zeeb 	 * (2) RFC2462 5.4 suggesting the use of the same interface identifier
1227f77a6dbdSBjoern A. Zeeb 	 * for multiple addresses on a single interface, and possible shortcut
1228f77a6dbdSBjoern A. Zeeb 	 * of DAD.  we omitted DAD for this reason in the past.
1229f77a6dbdSBjoern A. Zeeb 	 * (3) a user can prevent autoconfiguration of global address
1230f77a6dbdSBjoern A. Zeeb 	 * by removing link-local address by hand (this is partly because we
1231f77a6dbdSBjoern A. Zeeb 	 * don't have other way to control the use of IPv6 on an interface.
1232f77a6dbdSBjoern A. Zeeb 	 * this has been our design choice - cf. NRL's "ifconfig auto").
1233f77a6dbdSBjoern A. Zeeb 	 * (4) it is easier to manage when an interface has addresses
1234f77a6dbdSBjoern A. Zeeb 	 * with the same interface identifier, than to have multiple addresses
1235f77a6dbdSBjoern A. Zeeb 	 * with different interface identifiers.
1236f77a6dbdSBjoern A. Zeeb 	 */
1237f77a6dbdSBjoern A. Zeeb 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
1238f77a6dbdSBjoern A. Zeeb 	if (ifa)
1239f77a6dbdSBjoern A. Zeeb 		ib = (struct in6_ifaddr *)ifa;
1240f77a6dbdSBjoern A. Zeeb 	else
1241f77a6dbdSBjoern A. Zeeb 		return NULL;
1242f77a6dbdSBjoern A. Zeeb 
1243f77a6dbdSBjoern A. Zeeb 	/* prefixlen + ifidlen must be equal to 128 */
1244f77a6dbdSBjoern A. Zeeb 	plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
1245f77a6dbdSBjoern A. Zeeb 	if (prefixlen != plen0) {
1246f77a6dbdSBjoern A. Zeeb 		ifa_free(ifa);
1247f77a6dbdSBjoern A. Zeeb 		nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
1248f77a6dbdSBjoern A. Zeeb 		    "(prefix=%d ifid=%d)\n",
1249f77a6dbdSBjoern A. Zeeb 		    if_name(ifp), prefixlen, 128 - plen0));
1250f77a6dbdSBjoern A. Zeeb 		return NULL;
1251f77a6dbdSBjoern A. Zeeb 	}
1252f77a6dbdSBjoern A. Zeeb 
1253f77a6dbdSBjoern A. Zeeb 	/* make ifaddr */
1254f77a6dbdSBjoern A. Zeeb 	in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
1255f77a6dbdSBjoern A. Zeeb 
1256f77a6dbdSBjoern A. Zeeb 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
1257f77a6dbdSBjoern A. Zeeb 	/* interface ID */
1258f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
1259f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
1260f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
1261f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
1262f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
1263f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
1264f77a6dbdSBjoern A. Zeeb 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
1265f77a6dbdSBjoern A. Zeeb 	    (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
1266f77a6dbdSBjoern A. Zeeb 	ifa_free(ifa);
1267f77a6dbdSBjoern A. Zeeb 
1268f77a6dbdSBjoern A. Zeeb 	/* lifetimes. */
1269f77a6dbdSBjoern A. Zeeb 	ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
1270f77a6dbdSBjoern A. Zeeb 	ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1271f77a6dbdSBjoern A. Zeeb 
1272f77a6dbdSBjoern A. Zeeb 	/* XXX: scope zone ID? */
1273f77a6dbdSBjoern A. Zeeb 
1274f77a6dbdSBjoern A. Zeeb 	ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
1275f77a6dbdSBjoern A. Zeeb 
1276f77a6dbdSBjoern A. Zeeb 	/*
1277f77a6dbdSBjoern A. Zeeb 	 * Make sure that we do not have this address already.  This should
1278f77a6dbdSBjoern A. Zeeb 	 * usually not happen, but we can still see this case, e.g., if we
1279f77a6dbdSBjoern A. Zeeb 	 * have manually configured the exact address to be configured.
1280f77a6dbdSBjoern A. Zeeb 	 */
1281f77a6dbdSBjoern A. Zeeb 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1282f77a6dbdSBjoern A. Zeeb 	    &ifra.ifra_addr.sin6_addr);
1283f77a6dbdSBjoern A. Zeeb 	if (ifa != NULL) {
1284f77a6dbdSBjoern A. Zeeb 		ifa_free(ifa);
1285f77a6dbdSBjoern A. Zeeb 		/* this should be rare enough to make an explicit log */
1286f77a6dbdSBjoern A. Zeeb 		log(LOG_INFO, "in6_ifadd: %s is already configured\n",
1287f77a6dbdSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
1288f77a6dbdSBjoern A. Zeeb 		return (NULL);
1289f77a6dbdSBjoern A. Zeeb 	}
1290f77a6dbdSBjoern A. Zeeb 
1291f77a6dbdSBjoern A. Zeeb 	/*
1292f77a6dbdSBjoern A. Zeeb 	 * Allocate ifaddr structure, link into chain, etc.
1293f77a6dbdSBjoern A. Zeeb 	 * If we are going to create a new address upon receiving a multicasted
1294f77a6dbdSBjoern A. Zeeb 	 * RA, we need to impose a random delay before starting DAD.
1295f77a6dbdSBjoern A. Zeeb 	 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
1296f77a6dbdSBjoern A. Zeeb 	 */
1297f77a6dbdSBjoern A. Zeeb 	updateflags = 0;
1298f77a6dbdSBjoern A. Zeeb 	if (mcast)
1299f77a6dbdSBjoern A. Zeeb 		updateflags |= IN6_IFAUPDATE_DADDELAY;
1300f77a6dbdSBjoern A. Zeeb 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
1301f77a6dbdSBjoern A. Zeeb 		nd6log((LOG_ERR,
1302f77a6dbdSBjoern A. Zeeb 		    "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
1303f77a6dbdSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
1304f77a6dbdSBjoern A. Zeeb 		    if_name(ifp), error));
1305f77a6dbdSBjoern A. Zeeb 		return (NULL);	/* ifaddr must not have been allocated. */
1306f77a6dbdSBjoern A. Zeeb 	}
1307f77a6dbdSBjoern A. Zeeb 
1308f77a6dbdSBjoern A. Zeeb 	ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
1309f77a6dbdSBjoern A. Zeeb 	/*
1310f77a6dbdSBjoern A. Zeeb 	 * XXXRW: Assumption of non-NULLness here might not be true with
1311f77a6dbdSBjoern A. Zeeb 	 * fine-grained locking -- should we validate it?  Or just return
1312f77a6dbdSBjoern A. Zeeb 	 * earlier ifa rather than looking it up again?
1313f77a6dbdSBjoern A. Zeeb 	 */
1314f77a6dbdSBjoern A. Zeeb 	return (ia);		/* this is always non-NULL  and referenced. */
131582cd038dSYoshinobu Inoue }
131682cd038dSYoshinobu Inoue 
1317d748f7efSMark Johnston static struct nd_prefix *
1318d748f7efSMark Johnston nd6_prefix_lookup_locked(struct nd_prefixctl *key)
1319d748f7efSMark Johnston {
1320d748f7efSMark Johnston 	struct nd_prefix *search;
1321d748f7efSMark Johnston 
1322d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1323d748f7efSMark Johnston 
1324d748f7efSMark Johnston 	LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
1325d748f7efSMark Johnston 		if (key->ndpr_ifp == search->ndpr_ifp &&
1326d748f7efSMark Johnston 		    key->ndpr_plen == search->ndpr_plen &&
1327d748f7efSMark Johnston 		    in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
1328d748f7efSMark Johnston 		    &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
1329d748f7efSMark Johnston 			nd6_prefix_ref(search);
1330d748f7efSMark Johnston 			break;
1331d748f7efSMark Johnston 		}
1332d748f7efSMark Johnston 	}
1333d748f7efSMark Johnston 	return (search);
1334d748f7efSMark Johnston }
1335d748f7efSMark Johnston 
133633841545SHajimu UMEMOTO struct nd_prefix *
13371272577eSXin LI nd6_prefix_lookup(struct nd_prefixctl *key)
133882cd038dSYoshinobu Inoue {
133982cd038dSYoshinobu Inoue 	struct nd_prefix *search;
134082cd038dSYoshinobu Inoue 
1341d748f7efSMark Johnston 	ND6_RLOCK();
1342d748f7efSMark Johnston 	search = nd6_prefix_lookup_locked(key);
1343d748f7efSMark Johnston 	ND6_RUNLOCK();
1344d748f7efSMark Johnston 	return (search);
134582cd038dSYoshinobu Inoue }
134682cd038dSYoshinobu Inoue 
1347d748f7efSMark Johnston void
1348d748f7efSMark Johnston nd6_prefix_ref(struct nd_prefix *pr)
1349d748f7efSMark Johnston {
1350d748f7efSMark Johnston 
1351d748f7efSMark Johnston 	refcount_acquire(&pr->ndpr_refcnt);
1352d748f7efSMark Johnston }
1353d748f7efSMark Johnston 
1354d748f7efSMark Johnston void
1355d748f7efSMark Johnston nd6_prefix_rele(struct nd_prefix *pr)
1356d748f7efSMark Johnston {
1357d748f7efSMark Johnston 
1358d748f7efSMark Johnston 	if (refcount_release(&pr->ndpr_refcnt)) {
1359d748f7efSMark Johnston 		KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
1360d748f7efSMark Johnston 		    ("prefix %p has advertising routers", pr));
1361d748f7efSMark Johnston 		free(pr, M_IP6NDP);
1362d748f7efSMark Johnston 	}
136382cd038dSYoshinobu Inoue }
136482cd038dSYoshinobu Inoue 
136533841545SHajimu UMEMOTO int
13661272577eSXin LI nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
13671272577eSXin LI     struct nd_prefix **newp)
136882cd038dSYoshinobu Inoue {
1369d748f7efSMark Johnston 	struct nd_prefix *new;
13701d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1371d748f7efSMark Johnston 	int error;
137282cd038dSYoshinobu Inoue 
1373fc315641SMark Johnston 	new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
137482cd038dSYoshinobu Inoue 	if (new == NULL)
137507eb2995SHajimu UMEMOTO 		return (ENOMEM);
1376d748f7efSMark Johnston 	refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
1377743eee66SSUZUKI Shinsuke 	new->ndpr_ifp = pr->ndpr_ifp;
1378743eee66SSUZUKI Shinsuke 	new->ndpr_prefix = pr->ndpr_prefix;
1379743eee66SSUZUKI Shinsuke 	new->ndpr_plen = pr->ndpr_plen;
1380743eee66SSUZUKI Shinsuke 	new->ndpr_vltime = pr->ndpr_vltime;
1381743eee66SSUZUKI Shinsuke 	new->ndpr_pltime = pr->ndpr_pltime;
1382743eee66SSUZUKI Shinsuke 	new->ndpr_flags = pr->ndpr_flags;
1383743eee66SSUZUKI Shinsuke 	if ((error = in6_init_prefix_ltimes(new)) != 0) {
1384743eee66SSUZUKI Shinsuke 		free(new, M_IP6NDP);
1385743eee66SSUZUKI Shinsuke 		return (error);
1386743eee66SSUZUKI Shinsuke 	}
13877d26db17SHiroki Sato 	new->ndpr_lastupdate = time_uptime;
138882cd038dSYoshinobu Inoue 
138907eb2995SHajimu UMEMOTO 	/* initialization */
139082cd038dSYoshinobu Inoue 	LIST_INIT(&new->ndpr_advrtrs);
139182cd038dSYoshinobu Inoue 	in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
139282cd038dSYoshinobu Inoue 	/* make prefix in the canonical form */
13931dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
139482cd038dSYoshinobu Inoue 
1395d748f7efSMark Johnston 	ND6_WLOCK();
1396603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
1397d748f7efSMark Johnston 	V_nd6_list_genid++;
1398d748f7efSMark Johnston 	ND6_WUNLOCK();
139982cd038dSYoshinobu Inoue 
140033841545SHajimu UMEMOTO 	/* ND_OPT_PI_FLAG_ONLINK processing */
140133841545SHajimu UMEMOTO 	if (new->ndpr_raf_onlink) {
1402b8a6e03fSGleb Smirnoff 		struct epoch_tracker et;
1403b8a6e03fSGleb Smirnoff 
1404d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1405b8a6e03fSGleb Smirnoff 		NET_EPOCH_ENTER(et);
1406d748f7efSMark Johnston 		if ((error = nd6_prefix_onlink(new)) != 0) {
140733841545SHajimu UMEMOTO 			nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
140833841545SHajimu UMEMOTO 			    "the prefix %s/%d on-link on %s (errno=%d)\n",
14091d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
1410d748f7efSMark Johnston 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
141133841545SHajimu UMEMOTO 			/* proceed anyway. XXX: is it correct? */
141233841545SHajimu UMEMOTO 		}
1413b8a6e03fSGleb Smirnoff 		NET_EPOCH_EXIT(et);
1414d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
141533841545SHajimu UMEMOTO 	}
141633841545SHajimu UMEMOTO 
141720b5f022SMark Johnston 	if (dr != NULL)
141882cd038dSYoshinobu Inoue 		pfxrtr_add(new, dr);
141920b5f022SMark Johnston 	if (newp != NULL)
142020b5f022SMark Johnston 		*newp = new;
142120b5f022SMark Johnston 	return (0);
142282cd038dSYoshinobu Inoue }
142382cd038dSYoshinobu Inoue 
1424d748f7efSMark Johnston /*
1425d748f7efSMark Johnston  * Remove a prefix from the prefix list and optionally stash it in a
1426d748f7efSMark Johnston  * caller-provided list.
1427d748f7efSMark Johnston  *
1428d748f7efSMark Johnston  * The ND6 lock must be held.
1429d748f7efSMark Johnston  */
143082cd038dSYoshinobu Inoue void
1431d748f7efSMark Johnston nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
1432d748f7efSMark Johnston {
1433d748f7efSMark Johnston 
1434d748f7efSMark Johnston 	ND6_WLOCK_ASSERT();
1435d748f7efSMark Johnston 
1436d748f7efSMark Johnston 	LIST_REMOVE(pr, ndpr_entry);
1437d748f7efSMark Johnston 	V_nd6_list_genid++;
1438d748f7efSMark Johnston 	if (list != NULL)
1439d748f7efSMark Johnston 		LIST_INSERT_HEAD(list, pr, ndpr_entry);
1440d748f7efSMark Johnston }
1441d748f7efSMark Johnston 
1442d748f7efSMark Johnston /*
1443d748f7efSMark Johnston  * Free an unlinked prefix, first marking it off-link if necessary.
1444d748f7efSMark Johnston  */
1445d748f7efSMark Johnston void
1446d748f7efSMark Johnston nd6_prefix_del(struct nd_prefix *pr)
144782cd038dSYoshinobu Inoue {
144882cd038dSYoshinobu Inoue 	struct nd_pfxrouter *pfr, *next;
1449c9b652e3SAndre Oppermann 	int e;
14501d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
145133841545SHajimu UMEMOTO 
1452d748f7efSMark Johnston 	KASSERT(pr->ndpr_addrcnt == 0,
1453d748f7efSMark Johnston 	    ("prefix %p has referencing addresses", pr));
1454d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
1455743eee66SSUZUKI Shinsuke 
145633841545SHajimu UMEMOTO 	/*
145733841545SHajimu UMEMOTO 	 * Though these flags are now meaningless, we'd rather keep the value
14587aa59493SSUZUKI Shinsuke 	 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
14597aa59493SSUZUKI Shinsuke 	 * when executing "ndp -p".
146033841545SHajimu UMEMOTO 	 */
1461d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1462d748f7efSMark Johnston 		ND6_ONLINK_LOCK();
1463d748f7efSMark Johnston 		if ((e = nd6_prefix_offlink(pr)) != 0) {
1464d748f7efSMark Johnston 			nd6log((LOG_ERR,
1465d748f7efSMark Johnston 			    "nd6_prefix_del: failed to make %s/%d offlink "
146633841545SHajimu UMEMOTO 			    "on %s, errno=%d\n",
14671d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
146833841545SHajimu UMEMOTO 			    pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
146933841545SHajimu UMEMOTO 			/* what should we do? */
147033841545SHajimu UMEMOTO 		}
1471d748f7efSMark Johnston 		ND6_ONLINK_UNLOCK();
147282cd038dSYoshinobu Inoue 	}
1473d748f7efSMark Johnston 
1474d748f7efSMark Johnston 	/* Release references to routers that have advertised this prefix. */
1475d748f7efSMark Johnston 	ND6_WLOCK();
1476d748f7efSMark Johnston 	LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
1477d748f7efSMark Johnston 		pfxrtr_del(pfr);
1478d748f7efSMark Johnston 	ND6_WUNLOCK();
1479d748f7efSMark Johnston 
1480d748f7efSMark Johnston 	nd6_prefix_rele(pr);
148182cd038dSYoshinobu Inoue 
148282cd038dSYoshinobu Inoue 	pfxlist_onlink_check();
148382cd038dSYoshinobu Inoue }
148482cd038dSYoshinobu Inoue 
1485743eee66SSUZUKI Shinsuke static int
14861272577eSXin LI prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
14871272577eSXin LI     struct mbuf *m, int mcast)
148882cd038dSYoshinobu Inoue {
148933841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
149033841545SHajimu UMEMOTO 	struct ifaddr *ifa;
149133841545SHajimu UMEMOTO 	struct ifnet *ifp = new->ndpr_ifp;
149282cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
149382cd038dSYoshinobu Inoue 	int error = 0;
149482cd038dSYoshinobu Inoue 	int auth;
149533841545SHajimu UMEMOTO 	struct in6_addrlifetime lt6_tmp;
14961d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
1497b8a6e03fSGleb Smirnoff 
1498b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
149982cd038dSYoshinobu Inoue 
150082cd038dSYoshinobu Inoue 	auth = 0;
150182cd038dSYoshinobu Inoue 	if (m) {
150282cd038dSYoshinobu Inoue 		/*
150382cd038dSYoshinobu Inoue 		 * Authenticity for NA consists authentication for
150482cd038dSYoshinobu Inoue 		 * both IP header and IP datagrams, doesn't it ?
150582cd038dSYoshinobu Inoue 		 */
150682cd038dSYoshinobu Inoue #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
150707eb2995SHajimu UMEMOTO 		auth = ((m->m_flags & M_AUTHIPHDR) &&
150807eb2995SHajimu UMEMOTO 		    (m->m_flags & M_AUTHIPDGM));
150982cd038dSYoshinobu Inoue #endif
151082cd038dSYoshinobu Inoue 	}
151182cd038dSYoshinobu Inoue 
151233841545SHajimu UMEMOTO 	if ((pr = nd6_prefix_lookup(new)) != NULL) {
151333841545SHajimu UMEMOTO 		/*
151433841545SHajimu UMEMOTO 		 * nd6_prefix_lookup() ensures that pr and new have the same
151533841545SHajimu UMEMOTO 		 * prefix on a same interface.
151633841545SHajimu UMEMOTO 		 */
151733841545SHajimu UMEMOTO 
151833841545SHajimu UMEMOTO 		/*
151933841545SHajimu UMEMOTO 		 * Update prefix information.  Note that the on-link (L) bit
152033841545SHajimu UMEMOTO 		 * and the autonomous (A) bit should NOT be changed from 1
152133841545SHajimu UMEMOTO 		 * to 0.
152233841545SHajimu UMEMOTO 		 */
152333841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 1)
152433841545SHajimu UMEMOTO 			pr->ndpr_raf_onlink = 1;
152533841545SHajimu UMEMOTO 		if (new->ndpr_raf_auto == 1)
152633841545SHajimu UMEMOTO 			pr->ndpr_raf_auto = 1;
152733841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink) {
152882cd038dSYoshinobu Inoue 			pr->ndpr_vltime = new->ndpr_vltime;
152982cd038dSYoshinobu Inoue 			pr->ndpr_pltime = new->ndpr_pltime;
1530743eee66SSUZUKI Shinsuke 			(void)in6_init_prefix_ltimes(pr); /* XXX error case? */
15317d26db17SHiroki Sato 			pr->ndpr_lastupdate = time_uptime;
153282cd038dSYoshinobu Inoue 		}
153382cd038dSYoshinobu Inoue 
153433841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink &&
153533841545SHajimu UMEMOTO 		    (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
1536d748f7efSMark Johnston 			ND6_ONLINK_LOCK();
1537d748f7efSMark Johnston 			if ((error = nd6_prefix_onlink(pr)) != 0) {
153833841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
153933841545SHajimu UMEMOTO 				    "prelist_update: failed to make "
154033841545SHajimu UMEMOTO 				    "the prefix %s/%d on-link on %s "
154133841545SHajimu UMEMOTO 				    "(errno=%d)\n",
15421d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
15431d54aa3bSBjoern A. Zeeb 				        &pr->ndpr_prefix.sin6_addr),
1544d748f7efSMark Johnston 				    pr->ndpr_plen, if_name(pr->ndpr_ifp),
1545d748f7efSMark Johnston 				    error));
154633841545SHajimu UMEMOTO 				/* proceed anyway. XXX: is it correct? */
1547686cdd19SJun-ichiro itojun Hagino 			}
1548d748f7efSMark Johnston 			ND6_ONLINK_UNLOCK();
154982cd038dSYoshinobu Inoue 		}
155082cd038dSYoshinobu Inoue 
1551d748f7efSMark Johnston 		if (dr != NULL)
155282cd038dSYoshinobu Inoue 			pfxrtr_add(pr, dr);
155382cd038dSYoshinobu Inoue 	} else {
155433841545SHajimu UMEMOTO 		if (new->ndpr_vltime == 0)
155533841545SHajimu UMEMOTO 			goto end;
155633841545SHajimu UMEMOTO 		if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
155733841545SHajimu UMEMOTO 			goto end;
155882cd038dSYoshinobu Inoue 
155983631b16SMark Johnston 		error = nd6_prelist_add(new, dr, &pr);
156020b5f022SMark Johnston 		if (error != 0) {
156133841545SHajimu UMEMOTO 			nd6log((LOG_NOTICE, "prelist_update: "
156220b5f022SMark Johnston 			    "nd6_prelist_add failed for %s/%d on %s errno=%d\n",
15631d54aa3bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
156420b5f022SMark Johnston 			    new->ndpr_plen, if_name(new->ndpr_ifp), error));
156533841545SHajimu UMEMOTO 			goto end; /* we should just give up in this case. */
156633841545SHajimu UMEMOTO 		}
156733841545SHajimu UMEMOTO 
156882cd038dSYoshinobu Inoue 		/*
156933841545SHajimu UMEMOTO 		 * XXX: from the ND point of view, we can ignore a prefix
157033841545SHajimu UMEMOTO 		 * with the on-link bit being zero.  However, we need a
157133841545SHajimu UMEMOTO 		 * prefix structure for references from autoconfigured
157207eb2995SHajimu UMEMOTO 		 * addresses.  Thus, we explicitly make sure that the prefix
157333841545SHajimu UMEMOTO 		 * itself expires now.
157482cd038dSYoshinobu Inoue 		 */
157583631b16SMark Johnston 		if (pr->ndpr_raf_onlink == 0) {
157683631b16SMark Johnston 			pr->ndpr_vltime = 0;
157783631b16SMark Johnston 			pr->ndpr_pltime = 0;
157883631b16SMark Johnston 			in6_init_prefix_ltimes(pr);
157933841545SHajimu UMEMOTO 		}
158033841545SHajimu UMEMOTO 	}
158133841545SHajimu UMEMOTO 
158233841545SHajimu UMEMOTO 	/*
158333841545SHajimu UMEMOTO 	 * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
158433841545SHajimu UMEMOTO 	 * Note that pr must be non NULL at this point.
158533841545SHajimu UMEMOTO 	 */
158633841545SHajimu UMEMOTO 
158733841545SHajimu UMEMOTO 	/* 5.5.3 (a). Ignore the prefix without the A bit set. */
158882cd038dSYoshinobu Inoue 	if (!new->ndpr_raf_auto)
1589743eee66SSUZUKI Shinsuke 		goto end;
159082cd038dSYoshinobu Inoue 
159133841545SHajimu UMEMOTO 	/*
159233841545SHajimu UMEMOTO 	 * 5.5.3 (b). the link-local prefix should have been ignored in
159333841545SHajimu UMEMOTO 	 * nd6_ra_input.
159433841545SHajimu UMEMOTO 	 */
159533841545SHajimu UMEMOTO 
1596743eee66SSUZUKI Shinsuke 	/* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
1597743eee66SSUZUKI Shinsuke 	if (new->ndpr_pltime > new->ndpr_vltime) {
1598743eee66SSUZUKI Shinsuke 		error = EINVAL;	/* XXX: won't be used */
1599743eee66SSUZUKI Shinsuke 		goto end;
1600743eee66SSUZUKI Shinsuke 	}
160133841545SHajimu UMEMOTO 
160233841545SHajimu UMEMOTO 	/*
1603743eee66SSUZUKI Shinsuke 	 * 5.5.3 (d).  If the prefix advertised is not equal to the prefix of
1604743eee66SSUZUKI Shinsuke 	 * an address configured by stateless autoconfiguration already in the
1605743eee66SSUZUKI Shinsuke 	 * list of addresses associated with the interface, and the Valid
1606743eee66SSUZUKI Shinsuke 	 * Lifetime is not 0, form an address.  We first check if we have
1607743eee66SSUZUKI Shinsuke 	 * a matching prefix.
1608743eee66SSUZUKI Shinsuke 	 * Note: we apply a clarification in rfc2462bis-02 here.  We only
1609743eee66SSUZUKI Shinsuke 	 * consider autoconfigured addresses while RFC2462 simply said
1610743eee66SSUZUKI Shinsuke 	 * "address".
161133841545SHajimu UMEMOTO 	 */
1612d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
161333841545SHajimu UMEMOTO 		struct in6_ifaddr *ifa6;
1614743eee66SSUZUKI Shinsuke 		u_int32_t remaininglifetime;
161533841545SHajimu UMEMOTO 
161633841545SHajimu UMEMOTO 		if (ifa->ifa_addr->sa_family != AF_INET6)
161733841545SHajimu UMEMOTO 			continue;
161833841545SHajimu UMEMOTO 
161933841545SHajimu UMEMOTO 		ifa6 = (struct in6_ifaddr *)ifa;
162033841545SHajimu UMEMOTO 
162133841545SHajimu UMEMOTO 		/*
1622743eee66SSUZUKI Shinsuke 		 * We only consider autoconfigured addresses as per rfc2462bis.
1623743eee66SSUZUKI Shinsuke 		 */
1624743eee66SSUZUKI Shinsuke 		if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
1625743eee66SSUZUKI Shinsuke 			continue;
1626743eee66SSUZUKI Shinsuke 
1627743eee66SSUZUKI Shinsuke 		/*
162833841545SHajimu UMEMOTO 		 * Spec is not clear here, but I believe we should concentrate
162933841545SHajimu UMEMOTO 		 * on unicast (i.e. not anycast) addresses.
163033841545SHajimu UMEMOTO 		 * XXX: other ia6_flags? detached or duplicated?
163133841545SHajimu UMEMOTO 		 */
163233841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
163333841545SHajimu UMEMOTO 			continue;
163433841545SHajimu UMEMOTO 
1635743eee66SSUZUKI Shinsuke 		/*
1636743eee66SSUZUKI Shinsuke 		 * Ignore the address if it is not associated with a prefix
1637743eee66SSUZUKI Shinsuke 		 * or is associated with a prefix that is different from this
1638743eee66SSUZUKI Shinsuke 		 * one.  (pr is never NULL here)
1639743eee66SSUZUKI Shinsuke 		 */
1640743eee66SSUZUKI Shinsuke 		if (ifa6->ia6_ndpr != pr)
164133841545SHajimu UMEMOTO 			continue;
164233841545SHajimu UMEMOTO 
164333841545SHajimu UMEMOTO 		if (ia6_match == NULL) /* remember the first one */
164433841545SHajimu UMEMOTO 			ia6_match = ifa6;
164533841545SHajimu UMEMOTO 
164633841545SHajimu UMEMOTO 		/*
164733841545SHajimu UMEMOTO 		 * An already autoconfigured address matched.  Now that we
164833841545SHajimu UMEMOTO 		 * are sure there is at least one matched address, we can
164933841545SHajimu UMEMOTO 		 * proceed to 5.5.3. (e): update the lifetimes according to the
165033841545SHajimu UMEMOTO 		 * "two hours" rule and the privacy extension.
1651743eee66SSUZUKI Shinsuke 		 * We apply some clarifications in rfc2462bis:
1652743eee66SSUZUKI Shinsuke 		 * - use remaininglifetime instead of storedlifetime as a
1653743eee66SSUZUKI Shinsuke 		 *   variable name
1654743eee66SSUZUKI Shinsuke 		 * - remove the dead code in the "two-hour" rule
165533841545SHajimu UMEMOTO 		 */
165633841545SHajimu UMEMOTO #define TWOHOUR		(120*60)
165733841545SHajimu UMEMOTO 		lt6_tmp = ifa6->ia6_lifetime;
165833841545SHajimu UMEMOTO 
165911f3a6e2SHajimu UMEMOTO 		if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
1660743eee66SSUZUKI Shinsuke 			remaininglifetime = ND6_INFINITE_LIFETIME;
16617d26db17SHiroki Sato 		else if (time_uptime - ifa6->ia6_updatetime >
1662743eee66SSUZUKI Shinsuke 			 lt6_tmp.ia6t_vltime) {
1663743eee66SSUZUKI Shinsuke 			/*
1664743eee66SSUZUKI Shinsuke 			 * The case of "invalid" address.  We should usually
1665743eee66SSUZUKI Shinsuke 			 * not see this case.
1666743eee66SSUZUKI Shinsuke 			 */
1667743eee66SSUZUKI Shinsuke 			remaininglifetime = 0;
1668743eee66SSUZUKI Shinsuke 		} else
1669743eee66SSUZUKI Shinsuke 			remaininglifetime = lt6_tmp.ia6t_vltime -
16707d26db17SHiroki Sato 			    (time_uptime - ifa6->ia6_updatetime);
167111f3a6e2SHajimu UMEMOTO 
167211f3a6e2SHajimu UMEMOTO 		/* when not updating, keep the current stored lifetime. */
1673743eee66SSUZUKI Shinsuke 		lt6_tmp.ia6t_vltime = remaininglifetime;
167433841545SHajimu UMEMOTO 
167533841545SHajimu UMEMOTO 		if (TWOHOUR < new->ndpr_vltime ||
1676743eee66SSUZUKI Shinsuke 		    remaininglifetime < new->ndpr_vltime) {
167733841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = new->ndpr_vltime;
1678743eee66SSUZUKI Shinsuke 		} else if (remaininglifetime <= TWOHOUR) {
167933841545SHajimu UMEMOTO 			if (auth) {
168033841545SHajimu UMEMOTO 				lt6_tmp.ia6t_vltime = new->ndpr_vltime;
168182cd038dSYoshinobu Inoue 			}
168233841545SHajimu UMEMOTO 		} else {
168333841545SHajimu UMEMOTO 			/*
168433841545SHajimu UMEMOTO 			 * new->ndpr_vltime <= TWOHOUR &&
1685743eee66SSUZUKI Shinsuke 			 * TWOHOUR < remaininglifetime
168633841545SHajimu UMEMOTO 			 */
168733841545SHajimu UMEMOTO 			lt6_tmp.ia6t_vltime = TWOHOUR;
168882cd038dSYoshinobu Inoue 		}
168982cd038dSYoshinobu Inoue 
169033841545SHajimu UMEMOTO 		/* The 2 hour rule is not imposed for preferred lifetime. */
169133841545SHajimu UMEMOTO 		lt6_tmp.ia6t_pltime = new->ndpr_pltime;
169233841545SHajimu UMEMOTO 
169333841545SHajimu UMEMOTO 		in6_init_address_ltimes(pr, &lt6_tmp);
169433841545SHajimu UMEMOTO 
169533841545SHajimu UMEMOTO 		/*
1696743eee66SSUZUKI Shinsuke 		 * We need to treat lifetimes for temporary addresses
1697743eee66SSUZUKI Shinsuke 		 * differently, according to
1698743eee66SSUZUKI Shinsuke 		 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
1699743eee66SSUZUKI Shinsuke 		 * we only update the lifetimes when they are in the maximum
1700743eee66SSUZUKI Shinsuke 		 * intervals.
170133841545SHajimu UMEMOTO 		 */
170233841545SHajimu UMEMOTO 		if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
1703743eee66SSUZUKI Shinsuke 			u_int32_t maxvltime, maxpltime;
170433841545SHajimu UMEMOTO 
1705603724d3SBjoern A. Zeeb 			if (V_ip6_temp_valid_lifetime >
17067d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1707603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1708603724d3SBjoern A. Zeeb 				maxvltime = V_ip6_temp_valid_lifetime -
17097d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1710603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1711743eee66SSUZUKI Shinsuke 			} else
1712743eee66SSUZUKI Shinsuke 				maxvltime = 0;
1713603724d3SBjoern A. Zeeb 			if (V_ip6_temp_preferred_lifetime >
17147d26db17SHiroki Sato 			    (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
1715603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor)) {
1716603724d3SBjoern A. Zeeb 				maxpltime = V_ip6_temp_preferred_lifetime -
17177d26db17SHiroki Sato 				    (time_uptime - ifa6->ia6_createtime) -
1718603724d3SBjoern A. Zeeb 				    V_ip6_desync_factor;
1719743eee66SSUZUKI Shinsuke 			} else
1720743eee66SSUZUKI Shinsuke 				maxpltime = 0;
1721743eee66SSUZUKI Shinsuke 
1722743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
1723743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_vltime > maxvltime) {
1724743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_vltime = maxvltime;
1725743eee66SSUZUKI Shinsuke 			}
1726743eee66SSUZUKI Shinsuke 			if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
1727743eee66SSUZUKI Shinsuke 			    lt6_tmp.ia6t_pltime > maxpltime) {
1728743eee66SSUZUKI Shinsuke 				lt6_tmp.ia6t_pltime = maxpltime;
1729743eee66SSUZUKI Shinsuke 			}
1730743eee66SSUZUKI Shinsuke 		}
173133841545SHajimu UMEMOTO 		ifa6->ia6_lifetime = lt6_tmp;
17327d26db17SHiroki Sato 		ifa6->ia6_updatetime = time_uptime;
173333841545SHajimu UMEMOTO 	}
173433841545SHajimu UMEMOTO 	if (ia6_match == NULL && new->ndpr_vltime) {
1735743eee66SSUZUKI Shinsuke 		int ifidlen;
1736743eee66SSUZUKI Shinsuke 
173733841545SHajimu UMEMOTO 		/*
1738743eee66SSUZUKI Shinsuke 		 * 5.5.3 (d) (continued)
173933841545SHajimu UMEMOTO 		 * No address matched and the valid lifetime is non-zero.
174033841545SHajimu UMEMOTO 		 * Create a new address.
174133841545SHajimu UMEMOTO 		 */
1742743eee66SSUZUKI Shinsuke 
1743743eee66SSUZUKI Shinsuke 		/*
1744743eee66SSUZUKI Shinsuke 		 * Prefix Length check:
1745743eee66SSUZUKI Shinsuke 		 * If the sum of the prefix length and interface identifier
1746743eee66SSUZUKI Shinsuke 		 * length does not equal 128 bits, the Prefix Information
1747743eee66SSUZUKI Shinsuke 		 * option MUST be ignored.  The length of the interface
1748743eee66SSUZUKI Shinsuke 		 * identifier is defined in a separate link-type specific
1749743eee66SSUZUKI Shinsuke 		 * document.
1750743eee66SSUZUKI Shinsuke 		 */
1751743eee66SSUZUKI Shinsuke 		ifidlen = in6_if2idlen(ifp);
1752743eee66SSUZUKI Shinsuke 		if (ifidlen < 0) {
1753743eee66SSUZUKI Shinsuke 			/* this should not happen, so we always log it. */
1754743eee66SSUZUKI Shinsuke 			log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
1755743eee66SSUZUKI Shinsuke 			    if_name(ifp));
1756743eee66SSUZUKI Shinsuke 			goto end;
1757743eee66SSUZUKI Shinsuke 		}
1758743eee66SSUZUKI Shinsuke 		if (ifidlen + pr->ndpr_plen != 128) {
1759743eee66SSUZUKI Shinsuke 			nd6log((LOG_INFO,
1760743eee66SSUZUKI Shinsuke 			    "prelist_update: invalid prefixlen "
1761743eee66SSUZUKI Shinsuke 			    "%d for %s, ignored\n",
1762743eee66SSUZUKI Shinsuke 			    pr->ndpr_plen, if_name(ifp)));
1763743eee66SSUZUKI Shinsuke 			goto end;
1764743eee66SSUZUKI Shinsuke 		}
1765743eee66SSUZUKI Shinsuke 
1766743eee66SSUZUKI Shinsuke 		if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
176733841545SHajimu UMEMOTO 			/*
176833841545SHajimu UMEMOTO 			 * note that we should use pr (not new) for reference.
176933841545SHajimu UMEMOTO 			 */
17700bbf244eSMark Johnston 			pr->ndpr_addrcnt++;
177133841545SHajimu UMEMOTO 			ia6->ia6_ndpr = pr;
177233841545SHajimu UMEMOTO 
177333841545SHajimu UMEMOTO 			/*
177433841545SHajimu UMEMOTO 			 * RFC 3041 3.3 (2).
177533841545SHajimu UMEMOTO 			 * When a new public address is created as described
177633841545SHajimu UMEMOTO 			 * in RFC2462, also create a new temporary address.
177733841545SHajimu UMEMOTO 			 *
177833841545SHajimu UMEMOTO 			 * RFC 3041 3.5.
177933841545SHajimu UMEMOTO 			 * When an interface connects to a new link, a new
178033841545SHajimu UMEMOTO 			 * randomized interface identifier should be generated
178133841545SHajimu UMEMOTO 			 * immediately together with a new set of temporary
178233841545SHajimu UMEMOTO 			 * addresses.  Thus, we specifiy 1 as the 2nd arg of
178333841545SHajimu UMEMOTO 			 * in6_tmpifadd().
178433841545SHajimu UMEMOTO 			 */
1785603724d3SBjoern A. Zeeb 			if (V_ip6_use_tempaddr) {
178633841545SHajimu UMEMOTO 				int e;
1787743eee66SSUZUKI Shinsuke 				if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
178833841545SHajimu UMEMOTO 					nd6log((LOG_NOTICE, "prelist_update: "
178933841545SHajimu UMEMOTO 					    "failed to create a temporary "
179033841545SHajimu UMEMOTO 					    "address, errno=%d\n",
179133841545SHajimu UMEMOTO 					    e));
179233841545SHajimu UMEMOTO 				}
179333841545SHajimu UMEMOTO 			}
17948c0fec80SRobert Watson 			ifa_free(&ia6->ia_ifa);
179533841545SHajimu UMEMOTO 
179633841545SHajimu UMEMOTO 			/*
179733841545SHajimu UMEMOTO 			 * A newly added address might affect the status
179833841545SHajimu UMEMOTO 			 * of other addresses, so we check and update it.
179933841545SHajimu UMEMOTO 			 * XXX: what if address duplication happens?
180033841545SHajimu UMEMOTO 			 */
180133841545SHajimu UMEMOTO 			pfxlist_onlink_check();
180233841545SHajimu UMEMOTO 		} else {
180333841545SHajimu UMEMOTO 			/* just set an error. do not bark here. */
180433841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL; /* XXX: might be unused. */
180533841545SHajimu UMEMOTO 		}
180633841545SHajimu UMEMOTO 	}
180733841545SHajimu UMEMOTO 
180882cd038dSYoshinobu Inoue end:
1809d748f7efSMark Johnston 	if (pr != NULL)
1810d748f7efSMark Johnston 		nd6_prefix_rele(pr);
1811d748f7efSMark Johnston 	return (error);
181282cd038dSYoshinobu Inoue }
181382cd038dSYoshinobu Inoue 
181482cd038dSYoshinobu Inoue /*
1815686cdd19SJun-ichiro itojun Hagino  * A supplement function used in the on-link detection below;
1816686cdd19SJun-ichiro itojun Hagino  * detect if a given prefix has a (probably) reachable advertising router.
1817686cdd19SJun-ichiro itojun Hagino  * XXX: lengthy function name...
1818686cdd19SJun-ichiro itojun Hagino  */
181933841545SHajimu UMEMOTO static struct nd_pfxrouter *
18201272577eSXin LI find_pfxlist_reachable_router(struct nd_prefix *pr)
1821686cdd19SJun-ichiro itojun Hagino {
1822a68cc388SGleb Smirnoff 	struct epoch_tracker et;
1823686cdd19SJun-ichiro itojun Hagino 	struct nd_pfxrouter *pfxrtr;
18246e6b3f7cSQing Li 	struct llentry *ln;
1825d78be3a9SKip Macy 	int canreach;
1826686cdd19SJun-ichiro itojun Hagino 
1827d748f7efSMark Johnston 	ND6_LOCK_ASSERT();
1828d748f7efSMark Johnston 
1829a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
1830b8a6e03fSGleb Smirnoff 	LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
1831d78be3a9SKip Macy 		ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
1832a6146780SKip Macy 		if (ln == NULL)
1833a6146780SKip Macy 			continue;
1834d78be3a9SKip Macy 		canreach = ND6_IS_LLINFO_PROBREACH(ln);
1835d78be3a9SKip Macy 		LLE_RUNLOCK(ln);
1836d78be3a9SKip Macy 		if (canreach)
1837d78be3a9SKip Macy 			break;
18386e6b3f7cSQing Li 	}
1839b8a6e03fSGleb Smirnoff 	NET_EPOCH_EXIT(et);
1840686cdd19SJun-ichiro itojun Hagino 	return (pfxrtr);
1841686cdd19SJun-ichiro itojun Hagino }
1842686cdd19SJun-ichiro itojun Hagino 
1843686cdd19SJun-ichiro itojun Hagino /*
184482cd038dSYoshinobu Inoue  * Check if each prefix in the prefix list has at least one available router
184533841545SHajimu UMEMOTO  * that advertised the prefix (a router is "available" if its neighbor cache
184633841545SHajimu UMEMOTO  * entry is reachable or probably reachable).
1847686cdd19SJun-ichiro itojun Hagino  * If the check fails, the prefix may be off-link, because, for example,
184882cd038dSYoshinobu Inoue  * we have moved from the network but the lifetime of the prefix has not
184933841545SHajimu UMEMOTO  * expired yet.  So we should not use the prefix if there is another prefix
185033841545SHajimu UMEMOTO  * that has an available router.
185107c1f959SMark Johnston  * But, if there is no prefix that has an available router, we still regard
185282cd038dSYoshinobu Inoue  * all the prefixes as on-link.  This is because we can't tell if all the
185382cd038dSYoshinobu Inoue  * routers are simply dead or if we really moved from the network and there
185482cd038dSYoshinobu Inoue  * is no router around us.
185582cd038dSYoshinobu Inoue  */
1856686cdd19SJun-ichiro itojun Hagino void
18571b28988bSMark Johnston pfxlist_onlink_check(void)
185882cd038dSYoshinobu Inoue {
185982cd038dSYoshinobu Inoue 	struct nd_prefix *pr;
186033841545SHajimu UMEMOTO 	struct in6_ifaddr *ifa;
1861743eee66SSUZUKI Shinsuke 	struct nd_defrouter *dr;
1862743eee66SSUZUKI Shinsuke 	struct nd_pfxrouter *pfxrtr = NULL;
18631b28988bSMark Johnston 	struct rm_priotracker in6_ifa_tracker;
1864d748f7efSMark Johnston 	uint64_t genid;
1865d748f7efSMark Johnston 	uint32_t flags;
1866d748f7efSMark Johnston 
1867d748f7efSMark Johnston 	ND6_ONLINK_LOCK();
1868d748f7efSMark Johnston 	ND6_RLOCK();
186982cd038dSYoshinobu Inoue 
1870686cdd19SJun-ichiro itojun Hagino 	/*
1871686cdd19SJun-ichiro itojun Hagino 	 * Check if there is a prefix that has a reachable advertising
1872686cdd19SJun-ichiro itojun Hagino 	 * router.
1873686cdd19SJun-ichiro itojun Hagino 	 */
18743b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
187533841545SHajimu UMEMOTO 		if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
187682cd038dSYoshinobu Inoue 			break;
1877686cdd19SJun-ichiro itojun Hagino 	}
187882cd038dSYoshinobu Inoue 
187982cd038dSYoshinobu Inoue 	/*
1880743eee66SSUZUKI Shinsuke 	 * If we have no such prefix, check whether we still have a router
1881743eee66SSUZUKI Shinsuke 	 * that does not advertise any prefixes.
1882743eee66SSUZUKI Shinsuke 	 */
1883743eee66SSUZUKI Shinsuke 	if (pr == NULL) {
1884f592d0c3SBjoern A. Zeeb 		TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
1885743eee66SSUZUKI Shinsuke 			struct nd_prefix *pr0;
1886743eee66SSUZUKI Shinsuke 
18873b0b2840SJohn Baldwin 			LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
1888743eee66SSUZUKI Shinsuke 				if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
1889743eee66SSUZUKI Shinsuke 					break;
1890743eee66SSUZUKI Shinsuke 			}
1891743eee66SSUZUKI Shinsuke 			if (pfxrtr != NULL)
1892743eee66SSUZUKI Shinsuke 				break;
1893743eee66SSUZUKI Shinsuke 		}
1894743eee66SSUZUKI Shinsuke 	}
1895f592d0c3SBjoern A. Zeeb 	if (pr != NULL || (!TAILQ_EMPTY(&V_nd6_defrouter) && pfxrtr == NULL)) {
1896743eee66SSUZUKI Shinsuke 		/*
1897743eee66SSUZUKI Shinsuke 		 * There is at least one prefix that has a reachable router,
1898743eee66SSUZUKI Shinsuke 		 * or at least a router which probably does not advertise
1899743eee66SSUZUKI Shinsuke 		 * any prefixes.  The latter would be the case when we move
1900743eee66SSUZUKI Shinsuke 		 * to a new link where we have a router that does not provide
1901743eee66SSUZUKI Shinsuke 		 * prefixes and we configure an address by hand.
190233841545SHajimu UMEMOTO 		 * Detach prefixes which have no reachable advertising
190333841545SHajimu UMEMOTO 		 * router, and attach other prefixes.
190482cd038dSYoshinobu Inoue 		 */
19053b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
190633841545SHajimu UMEMOTO 			/* XXX: a link-local prefix should never be detached */
1907a88d6d7eSMark Johnston 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1908a88d6d7eSMark Johnston 			    pr->ndpr_raf_onlink == 0 ||
1909a88d6d7eSMark Johnston 			    pr->ndpr_raf_auto == 0)
191042cb3aa4SQing Li 				continue;
191142cb3aa4SQing Li 
191233841545SHajimu UMEMOTO 			if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
191333841545SHajimu UMEMOTO 			    find_pfxlist_reachable_router(pr) == NULL)
191433841545SHajimu UMEMOTO 				pr->ndpr_stateflags |= NDPRF_DETACHED;
1915a88d6d7eSMark Johnston 			else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
1916155d72c4SPedro F. Giffuni 			    find_pfxlist_reachable_router(pr) != NULL)
191733841545SHajimu UMEMOTO 				pr->ndpr_stateflags &= ~NDPRF_DETACHED;
191882cd038dSYoshinobu Inoue 		}
191933841545SHajimu UMEMOTO 	} else {
192033841545SHajimu UMEMOTO 		/* there is no prefix that has a reachable router */
19213b0b2840SJohn Baldwin 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
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)
192533841545SHajimu UMEMOTO 				continue;
192633841545SHajimu UMEMOTO 			pr->ndpr_stateflags &= ~NDPRF_DETACHED;
192733841545SHajimu UMEMOTO 		}
192833841545SHajimu UMEMOTO 	}
192933841545SHajimu UMEMOTO 
193033841545SHajimu UMEMOTO 	/*
193133841545SHajimu UMEMOTO 	 * Remove each interface route associated with a (just) detached
193233841545SHajimu UMEMOTO 	 * prefix, and reinstall the interface route for a (just) attached
193333841545SHajimu UMEMOTO 	 * prefix.  Note that all attempt of reinstallation does not
193433841545SHajimu UMEMOTO 	 * necessarily success, when a same prefix is shared among multiple
193533841545SHajimu UMEMOTO 	 * interfaces.  Such cases will be handled in nd6_prefix_onlink,
193633841545SHajimu UMEMOTO 	 * so we don't have to care about them.
193733841545SHajimu UMEMOTO 	 */
1938d748f7efSMark Johnston restart:
19393b0b2840SJohn Baldwin 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
19401d54aa3bSBjoern A. Zeeb 		char ip6buf[INET6_ADDRSTRLEN];
1941a88d6d7eSMark Johnston 		int e;
194233841545SHajimu UMEMOTO 
1943a88d6d7eSMark Johnston 		if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
1944a88d6d7eSMark Johnston 		    pr->ndpr_raf_onlink == 0 ||
1945a88d6d7eSMark Johnston 		    pr->ndpr_raf_auto == 0)
194642cb3aa4SQing Li 			continue;
194742cb3aa4SQing Li 
1948d748f7efSMark Johnston 		flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
1949d748f7efSMark Johnston 		if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
1950d748f7efSMark Johnston 			genid = V_nd6_list_genid;
1951d748f7efSMark Johnston 			ND6_RUNLOCK();
1952d748f7efSMark Johnston 			if ((flags & NDPRF_ONLINK) != 0 &&
1953d748f7efSMark Johnston 			    (e = nd6_prefix_offlink(pr)) != 0) {
195433841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
195533841545SHajimu UMEMOTO 				    "pfxlist_onlink_check: failed to "
19567aa59493SSUZUKI Shinsuke 				    "make %s/%d offlink, errno=%d\n",
19571d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
19581d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
195933841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
1960d748f7efSMark Johnston 			} else if ((flags & NDPRF_ONLINK) == 0 &&
1961d748f7efSMark Johnston 			    (e = nd6_prefix_onlink(pr)) != 0) {
196233841545SHajimu UMEMOTO 				nd6log((LOG_ERR,
196333841545SHajimu UMEMOTO 				    "pfxlist_onlink_check: failed to "
19647aa59493SSUZUKI Shinsuke 				    "make %s/%d onlink, errno=%d\n",
19651d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf,
19661d54aa3bSBjoern A. Zeeb 					    &pr->ndpr_prefix.sin6_addr),
196733841545SHajimu UMEMOTO 					    pr->ndpr_plen, e));
196833841545SHajimu UMEMOTO 			}
1969d748f7efSMark Johnston 			ND6_RLOCK();
1970d748f7efSMark Johnston 			if (genid != V_nd6_list_genid)
1971d748f7efSMark Johnston 				goto restart;
197233841545SHajimu UMEMOTO 		}
197333841545SHajimu UMEMOTO 	}
197433841545SHajimu UMEMOTO 
197533841545SHajimu UMEMOTO 	/*
197633841545SHajimu UMEMOTO 	 * Changes on the prefix status might affect address status as well.
197733841545SHajimu UMEMOTO 	 * Make sure that all addresses derived from an attached prefix are
197833841545SHajimu UMEMOTO 	 * attached, and that all addresses derived from a detached prefix are
197933841545SHajimu UMEMOTO 	 * detached.  Note, however, that a manually configured address should
198033841545SHajimu UMEMOTO 	 * always be attached.
198133841545SHajimu UMEMOTO 	 * The precise detection logic is same as the one for prefixes.
198233841545SHajimu UMEMOTO 	 */
19831b28988bSMark Johnston 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1984d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
198507eb2995SHajimu UMEMOTO 		if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
198633841545SHajimu UMEMOTO 			continue;
198733841545SHajimu UMEMOTO 
198833841545SHajimu UMEMOTO 		if (ifa->ia6_ndpr == NULL) {
198933841545SHajimu UMEMOTO 			/*
199033841545SHajimu UMEMOTO 			 * This can happen when we first configure the address
199133841545SHajimu UMEMOTO 			 * (i.e. the address exists, but the prefix does not).
199233841545SHajimu UMEMOTO 			 * XXX: complicated relationships...
199333841545SHajimu UMEMOTO 			 */
199433841545SHajimu UMEMOTO 			continue;
199533841545SHajimu UMEMOTO 		}
199633841545SHajimu UMEMOTO 
199733841545SHajimu UMEMOTO 		if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
199833841545SHajimu UMEMOTO 			break;
199933841545SHajimu UMEMOTO 	}
200033841545SHajimu UMEMOTO 	if (ifa) {
2001d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
200233841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
200333841545SHajimu UMEMOTO 				continue;
200433841545SHajimu UMEMOTO 
200533841545SHajimu UMEMOTO 			if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
200633841545SHajimu UMEMOTO 				continue;
200733841545SHajimu UMEMOTO 
2008743eee66SSUZUKI Shinsuke 			if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
2009743eee66SSUZUKI Shinsuke 				if (ifa->ia6_flags & IN6_IFF_DETACHED) {
201033841545SHajimu UMEMOTO 					ifa->ia6_flags &= ~IN6_IFF_DETACHED;
2011743eee66SSUZUKI Shinsuke 					ifa->ia6_flags |= IN6_IFF_TENTATIVE;
2012743eee66SSUZUKI Shinsuke 					nd6_dad_start((struct ifaddr *)ifa, 0);
2013743eee66SSUZUKI Shinsuke 				}
2014743eee66SSUZUKI Shinsuke 			} else {
201533841545SHajimu UMEMOTO 				ifa->ia6_flags |= IN6_IFF_DETACHED;
201682cd038dSYoshinobu Inoue 			}
2017686cdd19SJun-ichiro itojun Hagino 		}
20181b28988bSMark Johnston 	} else {
2019d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
202033841545SHajimu UMEMOTO 			if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
202133841545SHajimu UMEMOTO 				continue;
202233841545SHajimu UMEMOTO 
2023743eee66SSUZUKI Shinsuke 			if (ifa->ia6_flags & IN6_IFF_DETACHED) {
202433841545SHajimu UMEMOTO 				ifa->ia6_flags &= ~IN6_IFF_DETACHED;
2025743eee66SSUZUKI Shinsuke 				ifa->ia6_flags |= IN6_IFF_TENTATIVE;
2026743eee66SSUZUKI Shinsuke 				/* Do we need a delay in this case? */
2027743eee66SSUZUKI Shinsuke 				nd6_dad_start((struct ifaddr *)ifa, 0);
2028743eee66SSUZUKI Shinsuke 			}
202933841545SHajimu UMEMOTO 		}
203082cd038dSYoshinobu Inoue 	}
20311b28988bSMark Johnston 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
2032d748f7efSMark Johnston 	ND6_RUNLOCK();
2033d748f7efSMark Johnston 	ND6_ONLINK_UNLOCK();
203482cd038dSYoshinobu Inoue }
203582cd038dSYoshinobu Inoue 
20369d9b92f2SBjoern A. Zeeb static int
203781d5d46bSBjoern A. Zeeb nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
203881d5d46bSBjoern A. Zeeb {
2039563ab4e4SAlexander V. Chernikov 	struct sockaddr_dl sdl;
204081d5d46bSBjoern A. Zeeb 	struct rtentry *rt;
204181d5d46bSBjoern A. Zeeb 	struct sockaddr_in6 mask6;
204281d5d46bSBjoern A. Zeeb 	u_long rtflags;
2043559b4296SAlan Somers 	int error, a_failure, fibnum, maxfib;
204481d5d46bSBjoern A. Zeeb 
204581d5d46bSBjoern A. Zeeb 	/*
204681d5d46bSBjoern A. Zeeb 	 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
204781d5d46bSBjoern A. Zeeb 	 * ifa->ifa_rtrequest = nd6_rtrequest;
204881d5d46bSBjoern A. Zeeb 	 */
204981d5d46bSBjoern A. Zeeb 	bzero(&mask6, sizeof(mask6));
205081d5d46bSBjoern A. Zeeb 	mask6.sin6_len = sizeof(mask6);
205181d5d46bSBjoern A. Zeeb 	mask6.sin6_addr = pr->ndpr_mask;
205281d5d46bSBjoern A. Zeeb 	rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
205381d5d46bSBjoern A. Zeeb 
2054563ab4e4SAlexander V. Chernikov 	bzero(&sdl, sizeof(struct sockaddr_dl));
2055563ab4e4SAlexander V. Chernikov 	sdl.sdl_len = sizeof(struct sockaddr_dl);
2056563ab4e4SAlexander V. Chernikov 	sdl.sdl_family = AF_LINK;
2057563ab4e4SAlexander V. Chernikov 	sdl.sdl_type = ifa->ifa_ifp->if_type;
2058563ab4e4SAlexander V. Chernikov 	sdl.sdl_index = ifa->ifa_ifp->if_index;
2059563ab4e4SAlexander V. Chernikov 
2060559b4296SAlan Somers 	if(V_rt_add_addr_allfibs) {
2061559b4296SAlan Somers 		fibnum = 0;
2062559b4296SAlan Somers 		maxfib = rt_numfibs;
2063559b4296SAlan Somers 	} else {
2064559b4296SAlan Somers 		fibnum = ifa->ifa_ifp->if_fib;
2065559b4296SAlan Somers 		maxfib = fibnum + 1;
2066559b4296SAlan Somers 	}
206781d5d46bSBjoern A. Zeeb 	a_failure = 0;
2068559b4296SAlan Somers 	for (; fibnum < maxfib; fibnum++) {
206981d5d46bSBjoern A. Zeeb 
207081d5d46bSBjoern A. Zeeb 		rt = NULL;
207181d5d46bSBjoern A. Zeeb 		error = in6_rtrequest(RTM_ADD,
2072563ab4e4SAlexander V. Chernikov 		    (struct sockaddr *)&pr->ndpr_prefix, (struct sockaddr *)&sdl,
207381d5d46bSBjoern A. Zeeb 		    (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
207481d5d46bSBjoern A. Zeeb 		if (error == 0) {
207581d5d46bSBjoern A. Zeeb 			KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
207681d5d46bSBjoern A. Zeeb 			    "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
207781d5d46bSBjoern A. Zeeb 			    error, pr, ifa));
207881d5d46bSBjoern A. Zeeb 			RT_LOCK(rt);
207981d5d46bSBjoern A. Zeeb 			nd6_rtmsg(RTM_ADD, rt);
208081d5d46bSBjoern A. Zeeb 			RT_UNLOCK(rt);
208181d5d46bSBjoern A. Zeeb 			pr->ndpr_stateflags |= NDPRF_ONLINK;
208281d5d46bSBjoern A. Zeeb 		} else {
208381d5d46bSBjoern A. Zeeb 			char ip6buf[INET6_ADDRSTRLEN];
208481d5d46bSBjoern A. Zeeb 			char ip6bufg[INET6_ADDRSTRLEN];
208581d5d46bSBjoern A. Zeeb 			char ip6bufm[INET6_ADDRSTRLEN];
208681d5d46bSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
208781d5d46bSBjoern A. Zeeb 
208881d5d46bSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
208981d5d46bSBjoern A. Zeeb 			nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
209081d5d46bSBjoern A. Zeeb 			    "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
209181d5d46bSBjoern A. Zeeb 			    "flags=%lx errno = %d\n",
209281d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
209381d5d46bSBjoern A. Zeeb 			    pr->ndpr_plen, if_name(pr->ndpr_ifp),
209481d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufg, &sin6->sin6_addr),
209581d5d46bSBjoern A. Zeeb 			    ip6_sprintf(ip6bufm, &mask6.sin6_addr),
209681d5d46bSBjoern A. Zeeb 			    rtflags, error));
209781d5d46bSBjoern A. Zeeb 
209881d5d46bSBjoern A. Zeeb 			/* Save last error to return, see rtinit(). */
209981d5d46bSBjoern A. Zeeb 			a_failure = error;
210081d5d46bSBjoern A. Zeeb 		}
210181d5d46bSBjoern A. Zeeb 
210281d5d46bSBjoern A. Zeeb 		if (rt != NULL) {
210381d5d46bSBjoern A. Zeeb 			RT_LOCK(rt);
210481d5d46bSBjoern A. Zeeb 			RT_REMREF(rt);
210581d5d46bSBjoern A. Zeeb 			RT_UNLOCK(rt);
210681d5d46bSBjoern A. Zeeb 		}
210781d5d46bSBjoern A. Zeeb 	}
210881d5d46bSBjoern A. Zeeb 
210981d5d46bSBjoern A. Zeeb 	/* Return the last error we got. */
211081d5d46bSBjoern A. Zeeb 	return (a_failure);
211181d5d46bSBjoern A. Zeeb }
211281d5d46bSBjoern A. Zeeb 
2113d748f7efSMark Johnston int
21141272577eSXin LI nd6_prefix_onlink(struct nd_prefix *pr)
211582cd038dSYoshinobu Inoue {
2116751d8d15SGleb Smirnoff 	struct epoch_tracker et;
211733841545SHajimu UMEMOTO 	struct ifaddr *ifa;
211833841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
211933841545SHajimu UMEMOTO 	struct nd_prefix *opr;
21201d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2121d748f7efSMark Johnston 	int error;
212233841545SHajimu UMEMOTO 
2123d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2124d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2125d748f7efSMark Johnston 
2126d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
21272ce62dceSSUZUKI Shinsuke 		return (EEXIST);
212882cd038dSYoshinobu Inoue 
212982cd038dSYoshinobu Inoue 	/*
213033841545SHajimu UMEMOTO 	 * Add the interface route associated with the prefix.  Before
213133841545SHajimu UMEMOTO 	 * installing the route, check if there's the same prefix on another
213233841545SHajimu UMEMOTO 	 * interface, and the prefix has already installed the interface route.
213333841545SHajimu UMEMOTO 	 * Although such a configuration is expected to be rare, we explicitly
213433841545SHajimu UMEMOTO 	 * allow it.
213582cd038dSYoshinobu Inoue 	 */
2136d748f7efSMark Johnston 	ND6_RLOCK();
21373b0b2840SJohn Baldwin 	LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
213833841545SHajimu UMEMOTO 		if (opr == pr)
213933841545SHajimu UMEMOTO 			continue;
214033841545SHajimu UMEMOTO 
214133841545SHajimu UMEMOTO 		if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
214233841545SHajimu UMEMOTO 			continue;
214333841545SHajimu UMEMOTO 
2144559b4296SAlan Somers 		if (!V_rt_add_addr_allfibs &&
2145559b4296SAlan Somers 		    opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
2146559b4296SAlan Somers 			continue;
2147559b4296SAlan Somers 
214833841545SHajimu UMEMOTO 		if (opr->ndpr_plen == pr->ndpr_plen &&
214933841545SHajimu UMEMOTO 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
2150d748f7efSMark Johnston 		    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
2151d748f7efSMark Johnston 			ND6_RUNLOCK();
215233841545SHajimu UMEMOTO 			return (0);
215333841545SHajimu UMEMOTO 		}
2154d748f7efSMark Johnston 	}
2155d748f7efSMark Johnston 	ND6_RUNLOCK();
215633841545SHajimu UMEMOTO 
215733841545SHajimu UMEMOTO 	/*
215833841545SHajimu UMEMOTO 	 * We prefer link-local addresses as the associated interface address.
215933841545SHajimu UMEMOTO 	 */
216033841545SHajimu UMEMOTO 	/* search for a link-local addr */
2161751d8d15SGleb Smirnoff 	NET_EPOCH_ENTER(et);
216233841545SHajimu UMEMOTO 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
216307eb2995SHajimu UMEMOTO 	    IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
216433841545SHajimu UMEMOTO 	if (ifa == NULL) {
216533841545SHajimu UMEMOTO 		/* XXX: freebsd does not have ifa_ifwithaf */
2166d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2167c2615844SMark Johnston 			if (ifa->ifa_addr->sa_family == AF_INET6) {
2168c2615844SMark Johnston 				ifa_ref(ifa);
216933841545SHajimu UMEMOTO 				break;
217033841545SHajimu UMEMOTO 			}
2171c2615844SMark Johnston 		}
217233841545SHajimu UMEMOTO 		/* should we care about ia6_flags? */
217333841545SHajimu UMEMOTO 	}
2174751d8d15SGleb Smirnoff 	NET_EPOCH_EXIT(et);
217533841545SHajimu UMEMOTO 	if (ifa == NULL) {
217633841545SHajimu UMEMOTO 		/*
217733841545SHajimu UMEMOTO 		 * This can still happen, when, for example, we receive an RA
217833841545SHajimu UMEMOTO 		 * containing a prefix with the L bit set and the A bit clear,
217933841545SHajimu UMEMOTO 		 * after removing all IPv6 addresses on the receiving
218033841545SHajimu UMEMOTO 		 * interface.  This should, of course, be rare though.
218133841545SHajimu UMEMOTO 		 */
218233841545SHajimu UMEMOTO 		nd6log((LOG_NOTICE,
218333841545SHajimu UMEMOTO 		    "nd6_prefix_onlink: failed to find any ifaddr"
218433841545SHajimu UMEMOTO 		    " to add route for a prefix(%s/%d) on %s\n",
21851d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
218633841545SHajimu UMEMOTO 		    pr->ndpr_plen, if_name(ifp)));
218733841545SHajimu UMEMOTO 		return (0);
218833841545SHajimu UMEMOTO 	}
218933841545SHajimu UMEMOTO 
219081d5d46bSBjoern A. Zeeb 	error = nd6_prefix_onlink_rtrequest(pr, ifa);
219133841545SHajimu UMEMOTO 
21928c0fec80SRobert Watson 	if (ifa != NULL)
21938c0fec80SRobert Watson 		ifa_free(ifa);
219433841545SHajimu UMEMOTO 
219533841545SHajimu UMEMOTO 	return (error);
219633841545SHajimu UMEMOTO }
219733841545SHajimu UMEMOTO 
2198d748f7efSMark Johnston int
21991272577eSXin LI nd6_prefix_offlink(struct nd_prefix *pr)
220033841545SHajimu UMEMOTO {
220133841545SHajimu UMEMOTO 	int error = 0;
220233841545SHajimu UMEMOTO 	struct ifnet *ifp = pr->ndpr_ifp;
220333841545SHajimu UMEMOTO 	struct nd_prefix *opr;
220433841545SHajimu UMEMOTO 	struct sockaddr_in6 sa6, mask6;
220581d5d46bSBjoern A. Zeeb 	struct rtentry *rt;
22061d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
2207d748f7efSMark Johnston 	uint64_t genid;
2208559b4296SAlan Somers 	int fibnum, maxfib, a_failure;
220933841545SHajimu UMEMOTO 
2210d748f7efSMark Johnston 	ND6_ONLINK_LOCK_ASSERT();
2211d748f7efSMark Johnston 	ND6_UNLOCK_ASSERT();
2212d748f7efSMark Johnston 
2213d748f7efSMark Johnston 	if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
221433841545SHajimu UMEMOTO 		return (EEXIST);
221533841545SHajimu UMEMOTO 
221682cd038dSYoshinobu Inoue 	bzero(&sa6, sizeof(sa6));
221782cd038dSYoshinobu Inoue 	sa6.sin6_family = AF_INET6;
221882cd038dSYoshinobu Inoue 	sa6.sin6_len = sizeof(sa6);
221982cd038dSYoshinobu Inoue 	bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
222082cd038dSYoshinobu Inoue 	    sizeof(struct in6_addr));
222182cd038dSYoshinobu Inoue 	bzero(&mask6, sizeof(mask6));
222282cd038dSYoshinobu Inoue 	mask6.sin6_family = AF_INET6;
222382cd038dSYoshinobu Inoue 	mask6.sin6_len = sizeof(sa6);
222482cd038dSYoshinobu Inoue 	bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
222533841545SHajimu UMEMOTO 
2226559b4296SAlan Somers 	if (V_rt_add_addr_allfibs) {
2227559b4296SAlan Somers 		fibnum = 0;
2228559b4296SAlan Somers 		maxfib = rt_numfibs;
2229559b4296SAlan Somers 	} else {
2230559b4296SAlan Somers 		fibnum = ifp->if_fib;
2231559b4296SAlan Somers 		maxfib = fibnum + 1;
2232559b4296SAlan Somers 	}
2233559b4296SAlan Somers 
223481d5d46bSBjoern A. Zeeb 	a_failure = 0;
2235559b4296SAlan Somers 	for (; fibnum < maxfib; fibnum++) {
223681d5d46bSBjoern A. Zeeb 		rt = NULL;
223781d5d46bSBjoern A. Zeeb 		error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
223881d5d46bSBjoern A. Zeeb 		    (struct sockaddr *)&mask6, 0, &rt, fibnum);
223981d5d46bSBjoern A. Zeeb 		if (error == 0) {
224033841545SHajimu UMEMOTO 			/* report the route deletion to the routing socket. */
224133841545SHajimu UMEMOTO 			if (rt != NULL)
224233841545SHajimu UMEMOTO 				nd6_rtmsg(RTM_DELETE, rt);
224381d5d46bSBjoern A. Zeeb 		} else {
224481d5d46bSBjoern A. Zeeb 			/* Save last error to return, see rtinit(). */
224581d5d46bSBjoern A. Zeeb 			a_failure = error;
224681d5d46bSBjoern A. Zeeb 		}
224781d5d46bSBjoern A. Zeeb 		if (rt != NULL) {
224881d5d46bSBjoern A. Zeeb 			RTFREE(rt);
224981d5d46bSBjoern A. Zeeb 		}
225081d5d46bSBjoern A. Zeeb 	}
225181d5d46bSBjoern A. Zeeb 	error = a_failure;
2252378aa8d8SQing Li 	a_failure = 1;
225381d5d46bSBjoern A. Zeeb 	if (error == 0) {
225481d5d46bSBjoern A. Zeeb 		pr->ndpr_stateflags &= ~NDPRF_ONLINK;
225533841545SHajimu UMEMOTO 
225633841545SHajimu UMEMOTO 		/*
225733841545SHajimu UMEMOTO 		 * There might be the same prefix on another interface,
225833841545SHajimu UMEMOTO 		 * the prefix which could not be on-link just because we have
225933841545SHajimu UMEMOTO 		 * the interface route (see comments in nd6_prefix_onlink).
226033841545SHajimu UMEMOTO 		 * If there's one, try to make the prefix on-link on the
226133841545SHajimu UMEMOTO 		 * interface.
226233841545SHajimu UMEMOTO 		 */
2263d748f7efSMark Johnston 		ND6_RLOCK();
2264d748f7efSMark Johnston restart:
22653b0b2840SJohn Baldwin 		LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
226633841545SHajimu UMEMOTO 			/*
226733841545SHajimu UMEMOTO 			 * KAME specific: detached prefixes should not be
226833841545SHajimu UMEMOTO 			 * on-link.
226933841545SHajimu UMEMOTO 			 */
22707b0e84b7SMark Johnston 			if (opr == pr || (opr->ndpr_stateflags &
22717b0e84b7SMark Johnston 			    (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
227233841545SHajimu UMEMOTO 				continue;
227333841545SHajimu UMEMOTO 
227433841545SHajimu UMEMOTO 			if (opr->ndpr_plen == pr->ndpr_plen &&
227533841545SHajimu UMEMOTO 			    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
227607eb2995SHajimu UMEMOTO 			    &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
227782cd038dSYoshinobu Inoue 				int e;
227882cd038dSYoshinobu Inoue 
2279d748f7efSMark Johnston 				genid = V_nd6_list_genid;
2280d748f7efSMark Johnston 				ND6_RUNLOCK();
228133841545SHajimu UMEMOTO 				if ((e = nd6_prefix_onlink(opr)) != 0) {
228233841545SHajimu UMEMOTO 					nd6log((LOG_ERR,
228333841545SHajimu UMEMOTO 					    "nd6_prefix_offlink: failed to "
228433841545SHajimu UMEMOTO 					    "recover a prefix %s/%d from %s "
228533841545SHajimu UMEMOTO 					    "to %s (errno = %d)\n",
22861d54aa3bSBjoern A. Zeeb 					    ip6_sprintf(ip6buf,
22871d54aa3bSBjoern A. Zeeb 						&opr->ndpr_prefix.sin6_addr),
228833841545SHajimu UMEMOTO 					    opr->ndpr_plen, if_name(ifp),
228933841545SHajimu UMEMOTO 					    if_name(opr->ndpr_ifp), e));
2290378aa8d8SQing Li 				} else
2291378aa8d8SQing Li 					a_failure = 0;
2292d748f7efSMark Johnston 				ND6_RLOCK();
2293d748f7efSMark Johnston 				if (genid != V_nd6_list_genid)
2294d748f7efSMark Johnston 					goto restart;
229582cd038dSYoshinobu Inoue 			}
229682cd038dSYoshinobu Inoue 		}
2297d748f7efSMark Johnston 		ND6_RUNLOCK();
229807eb2995SHajimu UMEMOTO 	} else {
229933841545SHajimu UMEMOTO 		/* XXX: can we still set the NDPRF_ONLINK flag? */
230033841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
230133841545SHajimu UMEMOTO 		    "nd6_prefix_offlink: failed to delete route: "
230233841545SHajimu UMEMOTO 		    "%s/%d on %s (errno = %d)\n",
23031d54aa3bSBjoern A. Zeeb 		    ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
23041d54aa3bSBjoern A. Zeeb 		    if_name(ifp), error));
230533841545SHajimu UMEMOTO 	}
230682cd038dSYoshinobu Inoue 
2307378aa8d8SQing Li 	if (a_failure)
2308378aa8d8SQing Li 		lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
2309378aa8d8SQing Li 		    (struct sockaddr *)&mask6, LLE_STATIC);
2310378aa8d8SQing Li 
231133841545SHajimu UMEMOTO 	return (error);
231282cd038dSYoshinobu Inoue }
231382cd038dSYoshinobu Inoue 
23141272577eSXin LI /*
23151272577eSXin LI  * ia0 - corresponding public address
23161272577eSXin LI  */
231782cd038dSYoshinobu Inoue int
23181272577eSXin LI in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
231982cd038dSYoshinobu Inoue {
232033841545SHajimu UMEMOTO 	struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
23210c5d4bdeSAlexander V. Chernikov 	struct in6_ifaddr *newia;
232233841545SHajimu UMEMOTO 	struct in6_aliasreq ifra;
23231dc8f6a8SAlexander V. Chernikov 	int error;
232433841545SHajimu UMEMOTO 	int trylimit = 3;	/* XXX: adhoc value */
2325743eee66SSUZUKI Shinsuke 	int updateflags;
232633841545SHajimu UMEMOTO 	u_int32_t randid[2];
232733841545SHajimu UMEMOTO 	time_t vltime0, pltime0;
232882cd038dSYoshinobu Inoue 
23299080e7d0SAlexander V. Chernikov 	in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
23309080e7d0SAlexander V. Chernikov 	    &ia0->ia_prefixmask.sin6_addr);
23319080e7d0SAlexander V. Chernikov 
23329080e7d0SAlexander V. Chernikov 	ifra.ifra_addr = ia0->ia_addr;	/* XXX: do we need this ? */
233333841545SHajimu UMEMOTO 	/* clear the old IFID */
23341dc8f6a8SAlexander V. Chernikov 	IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
23351dc8f6a8SAlexander V. Chernikov 	    &ifra.ifra_prefixmask.sin6_addr);
233682cd038dSYoshinobu Inoue 
233733841545SHajimu UMEMOTO   again:
2338743eee66SSUZUKI Shinsuke 	if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
2339743eee66SSUZUKI Shinsuke 	    (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
2340743eee66SSUZUKI Shinsuke 		nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
2341743eee66SSUZUKI Shinsuke 		    "random IFID\n"));
2342743eee66SSUZUKI Shinsuke 		return (EINVAL);
2343743eee66SSUZUKI Shinsuke 	}
234407eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
234507eb2995SHajimu UMEMOTO 	    (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
234607eb2995SHajimu UMEMOTO 	ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
234707eb2995SHajimu UMEMOTO 	    (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
234882cd038dSYoshinobu Inoue 
234982cd038dSYoshinobu Inoue 	/*
2350743eee66SSUZUKI Shinsuke 	 * in6_get_tmpifid() quite likely provided a unique interface ID.
2351743eee66SSUZUKI Shinsuke 	 * However, we may still have a chance to see collision, because
2352743eee66SSUZUKI Shinsuke 	 * there may be a time lag between generation of the ID and generation
2353743eee66SSUZUKI Shinsuke 	 * of the address.  So, we'll do one more sanity check.
235482cd038dSYoshinobu Inoue 	 */
23550c5d4bdeSAlexander V. Chernikov 
23560c5d4bdeSAlexander V. Chernikov 	if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
23570c5d4bdeSAlexander V. Chernikov 		if (trylimit-- > 0) {
23580c5d4bdeSAlexander V. Chernikov 			forcegen = 1;
23590c5d4bdeSAlexander V. Chernikov 			goto again;
23600c5d4bdeSAlexander V. Chernikov 		}
23610c5d4bdeSAlexander V. Chernikov 
23620c5d4bdeSAlexander V. Chernikov 		/* Give up.  Something strange should have happened.  */
2363743eee66SSUZUKI Shinsuke 		nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
2364743eee66SSUZUKI Shinsuke 		    "find a unique random IFID\n"));
236533841545SHajimu UMEMOTO 		return (EEXIST);
236633841545SHajimu UMEMOTO 	}
236782cd038dSYoshinobu Inoue 
236833841545SHajimu UMEMOTO 	/*
236933841545SHajimu UMEMOTO 	 * The Valid Lifetime is the lower of the Valid Lifetime of the
237033841545SHajimu UMEMOTO          * public address or TEMP_VALID_LIFETIME.
237133841545SHajimu UMEMOTO 	 * The Preferred Lifetime is the lower of the Preferred Lifetime
237233841545SHajimu UMEMOTO          * of the public address or TEMP_PREFERRED_LIFETIME -
237333841545SHajimu UMEMOTO          * DESYNC_FACTOR.
237482cd038dSYoshinobu Inoue 	 */
2375743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
237633841545SHajimu UMEMOTO 		vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
2377743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_vltime -
23787d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2379603724d3SBjoern A. Zeeb 		if (vltime0 > V_ip6_temp_valid_lifetime)
2380603724d3SBjoern A. Zeeb 			vltime0 = V_ip6_temp_valid_lifetime;
238133841545SHajimu UMEMOTO 	} else
2382603724d3SBjoern A. Zeeb 		vltime0 = V_ip6_temp_valid_lifetime;
2383743eee66SSUZUKI Shinsuke 	if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
238433841545SHajimu UMEMOTO 		pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
2385743eee66SSUZUKI Shinsuke 		    (ia0->ia6_lifetime.ia6t_pltime -
23867d26db17SHiroki Sato 		    (time_uptime - ia0->ia6_updatetime));
2387603724d3SBjoern A. Zeeb 		if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
2388603724d3SBjoern A. Zeeb 			pltime0 = V_ip6_temp_preferred_lifetime -
2389603724d3SBjoern A. Zeeb 			    V_ip6_desync_factor;
239033841545SHajimu UMEMOTO 		}
239133841545SHajimu UMEMOTO 	} else
2392603724d3SBjoern A. Zeeb 		pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
239333841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_vltime = vltime0;
239433841545SHajimu UMEMOTO 	ifra.ifra_lifetime.ia6t_pltime = pltime0;
239533841545SHajimu UMEMOTO 
239633841545SHajimu UMEMOTO 	/*
239733841545SHajimu UMEMOTO 	 * A temporary address is created only if this calculated Preferred
239833841545SHajimu UMEMOTO 	 * Lifetime is greater than REGEN_ADVANCE time units.
239933841545SHajimu UMEMOTO 	 */
2400603724d3SBjoern A. Zeeb 	if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
240133841545SHajimu UMEMOTO 		return (0);
240233841545SHajimu UMEMOTO 
240333841545SHajimu UMEMOTO 	/* XXX: scope zone ID? */
240433841545SHajimu UMEMOTO 
240533841545SHajimu UMEMOTO 	ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
240633841545SHajimu UMEMOTO 
240733841545SHajimu UMEMOTO 	/* allocate ifaddr structure, link into chain, etc. */
2408743eee66SSUZUKI Shinsuke 	updateflags = 0;
2409743eee66SSUZUKI Shinsuke 	if (delay)
2410743eee66SSUZUKI Shinsuke 		updateflags |= IN6_IFAUPDATE_DADDELAY;
2411743eee66SSUZUKI Shinsuke 	if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
241233841545SHajimu UMEMOTO 		return (error);
241333841545SHajimu UMEMOTO 
241433841545SHajimu UMEMOTO 	newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
241533841545SHajimu UMEMOTO 	if (newia == NULL) {	/* XXX: can it happen? */
241633841545SHajimu UMEMOTO 		nd6log((LOG_ERR,
241733841545SHajimu UMEMOTO 		    "in6_tmpifadd: ifa update succeeded, but we got "
241833841545SHajimu UMEMOTO 		    "no ifaddr\n"));
241933841545SHajimu UMEMOTO 		return (EINVAL); /* XXX */
242033841545SHajimu UMEMOTO 	}
242133841545SHajimu UMEMOTO 	newia->ia6_ndpr = ia0->ia6_ndpr;
24220bbf244eSMark Johnston 	newia->ia6_ndpr->ndpr_addrcnt++;
24238c0fec80SRobert Watson 	ifa_free(&newia->ia_ifa);
242433841545SHajimu UMEMOTO 
2425c3aacd9eSHajimu UMEMOTO 	/*
2426c3aacd9eSHajimu UMEMOTO 	 * A newly added address might affect the status of other addresses.
2427c3aacd9eSHajimu UMEMOTO 	 * XXX: when the temporary address is generated with a new public
2428c3aacd9eSHajimu UMEMOTO 	 * address, the onlink check is redundant.  However, it would be safe
2429c3aacd9eSHajimu UMEMOTO 	 * to do the check explicitly everywhere a new address is generated,
2430c3aacd9eSHajimu UMEMOTO 	 * and, in fact, we surely need the check when we create a new
2431c3aacd9eSHajimu UMEMOTO 	 * temporary address due to deprecation of an old temporary address.
2432c3aacd9eSHajimu UMEMOTO 	 */
2433c3aacd9eSHajimu UMEMOTO 	pfxlist_onlink_check();
2434c3aacd9eSHajimu UMEMOTO 
243533841545SHajimu UMEMOTO 	return (0);
243682cd038dSYoshinobu Inoue }
243782cd038dSYoshinobu Inoue 
2438743eee66SSUZUKI Shinsuke static int
2439e8b0643eSAlexander V. Chernikov rt6_deleteroute(const struct rtentry *rt, void *arg)
244082cd038dSYoshinobu Inoue {
244182cd038dSYoshinobu Inoue #define SIN6(s)	((struct sockaddr_in6 *)s)
244282cd038dSYoshinobu Inoue 	struct in6_addr *gate = (struct in6_addr *)arg;
244382cd038dSYoshinobu Inoue 
244482cd038dSYoshinobu Inoue 	if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
244582cd038dSYoshinobu Inoue 		return (0);
244682cd038dSYoshinobu Inoue 
244707eb2995SHajimu UMEMOTO 	if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
244882cd038dSYoshinobu Inoue 		return (0);
244907eb2995SHajimu UMEMOTO 	}
245082cd038dSYoshinobu Inoue 
245182cd038dSYoshinobu Inoue 	/*
245233841545SHajimu UMEMOTO 	 * Do not delete a static route.
245333841545SHajimu UMEMOTO 	 * XXX: this seems to be a bit ad-hoc. Should we consider the
245433841545SHajimu UMEMOTO 	 * 'cloned' bit instead?
245533841545SHajimu UMEMOTO 	 */
245633841545SHajimu UMEMOTO 	if ((rt->rt_flags & RTF_STATIC) != 0)
245733841545SHajimu UMEMOTO 		return (0);
245833841545SHajimu UMEMOTO 
245933841545SHajimu UMEMOTO 	/*
246082cd038dSYoshinobu Inoue 	 * We delete only host route. This means, in particular, we don't
246182cd038dSYoshinobu Inoue 	 * delete default route.
246282cd038dSYoshinobu Inoue 	 */
246382cd038dSYoshinobu Inoue 	if ((rt->rt_flags & RTF_HOST) == 0)
246482cd038dSYoshinobu Inoue 		return (0);
246582cd038dSYoshinobu Inoue 
2466e8b0643eSAlexander V. Chernikov 	return (1);
246782cd038dSYoshinobu Inoue #undef SIN6
246882cd038dSYoshinobu Inoue }
2469686cdd19SJun-ichiro itojun Hagino 
2470f77a6dbdSBjoern A. Zeeb /*
2471f77a6dbdSBjoern A. Zeeb  * Delete all the routing table entries that use the specified gateway.
2472f77a6dbdSBjoern A. Zeeb  * XXX: this function causes search through all entries of routing table, so
2473f77a6dbdSBjoern A. Zeeb  * it shouldn't be called when acting as a router.
2474f77a6dbdSBjoern A. Zeeb  */
2475f77a6dbdSBjoern A. Zeeb void
2476f77a6dbdSBjoern A. Zeeb rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
2477f77a6dbdSBjoern A. Zeeb {
2478f77a6dbdSBjoern A. Zeeb 
2479f77a6dbdSBjoern A. Zeeb 	/* We'll care only link-local addresses */
2480f77a6dbdSBjoern A. Zeeb 	if (!IN6_IS_ADDR_LINKLOCAL(gateway))
2481f77a6dbdSBjoern A. Zeeb 		return;
2482f77a6dbdSBjoern A. Zeeb 
2483f77a6dbdSBjoern A. Zeeb 	/* XXX Do we really need to walk any but the default FIB? */
2484f77a6dbdSBjoern A. Zeeb 	rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
2485f77a6dbdSBjoern A. Zeeb }
2486f77a6dbdSBjoern A. Zeeb 
2487686cdd19SJun-ichiro itojun Hagino int
24881272577eSXin LI nd6_setdefaultiface(int ifindex)
2489686cdd19SJun-ichiro itojun Hagino {
2490686cdd19SJun-ichiro itojun Hagino 	int error = 0;
2491686cdd19SJun-ichiro itojun Hagino 
2492603724d3SBjoern A. Zeeb 	if (ifindex < 0 || V_if_index < ifindex)
2493686cdd19SJun-ichiro itojun Hagino 		return (EINVAL);
2494743eee66SSUZUKI Shinsuke 	if (ifindex != 0 && !ifnet_byindex(ifindex))
2495743eee66SSUZUKI Shinsuke 		return (EINVAL);
2496686cdd19SJun-ichiro itojun Hagino 
2497603724d3SBjoern A. Zeeb 	if (V_nd6_defifindex != ifindex) {
2498603724d3SBjoern A. Zeeb 		V_nd6_defifindex = ifindex;
2499603724d3SBjoern A. Zeeb 		if (V_nd6_defifindex > 0)
2500603724d3SBjoern A. Zeeb 			V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
2501686cdd19SJun-ichiro itojun Hagino 		else
2502603724d3SBjoern A. Zeeb 			V_nd6_defifp = NULL;
2503686cdd19SJun-ichiro itojun Hagino 
2504686cdd19SJun-ichiro itojun Hagino 		/*
2505686cdd19SJun-ichiro itojun Hagino 		 * Our current implementation assumes one-to-one maping between
2506686cdd19SJun-ichiro itojun Hagino 		 * interfaces and links, so it would be natural to use the
2507686cdd19SJun-ichiro itojun Hagino 		 * default interface as the default link.
2508686cdd19SJun-ichiro itojun Hagino 		 */
2509603724d3SBjoern A. Zeeb 		scope6_setdefault(V_nd6_defifp);
2510686cdd19SJun-ichiro itojun Hagino 	}
2511686cdd19SJun-ichiro itojun Hagino 
2512686cdd19SJun-ichiro itojun Hagino 	return (error);
2513686cdd19SJun-ichiro itojun Hagino }
2514ad675b32SBjoern A. Zeeb 
2515ad675b32SBjoern A. Zeeb bool
2516ad675b32SBjoern A. Zeeb nd6_defrouter_list_empty(void)
2517ad675b32SBjoern A. Zeeb {
2518ad675b32SBjoern A. Zeeb 
2519f592d0c3SBjoern A. Zeeb 	return (TAILQ_EMPTY(&V_nd6_defrouter));
2520ad675b32SBjoern A. Zeeb }
2521ad675b32SBjoern A. Zeeb 
2522ad675b32SBjoern A. Zeeb void
2523ad675b32SBjoern A. Zeeb nd6_defrouter_timer(void)
2524ad675b32SBjoern A. Zeeb {
2525ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2526f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2527ad675b32SBjoern A. Zeeb 
2528ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2529ad675b32SBjoern A. Zeeb 
2530ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2531f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr)
2532ad675b32SBjoern A. Zeeb 		if (dr->expire && dr->expire < time_uptime)
2533ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2534ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2535ad675b32SBjoern A. Zeeb 
2536ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2537ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2538ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2539ad675b32SBjoern A. Zeeb 	}
2540ad675b32SBjoern A. Zeeb }
2541ad675b32SBjoern A. Zeeb 
2542ad675b32SBjoern A. Zeeb /*
2543ad675b32SBjoern A. Zeeb  * Nuke default router list entries toward ifp.
2544ad675b32SBjoern A. Zeeb  * We defer removal of default router list entries that is installed in the
2545ad675b32SBjoern A. Zeeb  * routing table, in order to keep additional side effects as small as possible.
2546ad675b32SBjoern A. Zeeb  */
2547ad675b32SBjoern A. Zeeb void
2548ad675b32SBjoern A. Zeeb nd6_defrouter_purge(struct ifnet *ifp)
2549ad675b32SBjoern A. Zeeb {
2550ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr, *ndr;
2551f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2552ad675b32SBjoern A. Zeeb 
2553ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2554ad675b32SBjoern A. Zeeb 
2555ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2556f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2557ad675b32SBjoern A. Zeeb 		if (dr->installed)
2558ad675b32SBjoern A. Zeeb 			continue;
2559ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2560ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2561ad675b32SBjoern A. Zeeb 	}
2562f592d0c3SBjoern A. Zeeb 	TAILQ_FOREACH_SAFE(dr, &V_nd6_defrouter, dr_entry, ndr) {
2563ad675b32SBjoern A. Zeeb 		if (!dr->installed)
2564ad675b32SBjoern A. Zeeb 			continue;
2565ad675b32SBjoern A. Zeeb 		if (dr->ifp == ifp)
2566ad675b32SBjoern A. Zeeb 			defrouter_unlink(dr, &drq);
2567ad675b32SBjoern A. Zeeb 	}
2568ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2569ad675b32SBjoern A. Zeeb 
2570ad675b32SBjoern A. Zeeb 	/* Delete the unlinked router objects. */
2571ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2572ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2573ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2574ad675b32SBjoern A. Zeeb 	}
2575ad675b32SBjoern A. Zeeb }
2576ad675b32SBjoern A. Zeeb 
2577ad675b32SBjoern A. Zeeb void
2578ad675b32SBjoern A. Zeeb nd6_defrouter_flush_all(void)
2579ad675b32SBjoern A. Zeeb {
2580ad675b32SBjoern A. Zeeb 	struct nd_defrouter *dr;
2581f592d0c3SBjoern A. Zeeb 	struct nd6_drhead drq;
2582ad675b32SBjoern A. Zeeb 
2583ad675b32SBjoern A. Zeeb 	TAILQ_INIT(&drq);
2584ad675b32SBjoern A. Zeeb 
2585ad675b32SBjoern A. Zeeb 	ND6_WLOCK();
2586f592d0c3SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&V_nd6_defrouter)) != NULL)
2587ad675b32SBjoern A. Zeeb 		defrouter_unlink(dr, &drq);
2588ad675b32SBjoern A. Zeeb 	ND6_WUNLOCK();
2589ad675b32SBjoern A. Zeeb 
2590ad675b32SBjoern A. Zeeb 	while ((dr = TAILQ_FIRST(&drq)) != NULL) {
2591ad675b32SBjoern A. Zeeb 		TAILQ_REMOVE(&drq, dr, dr_entry);
2592ad675b32SBjoern A. Zeeb 		defrouter_del(dr);
2593ad675b32SBjoern A. Zeeb 	}
2594ad675b32SBjoern A. Zeeb }
2595ad675b32SBjoern A. Zeeb 
2596ad675b32SBjoern A. Zeeb void
2597ad675b32SBjoern A. Zeeb nd6_defrouter_init(void)
2598ad675b32SBjoern A. Zeeb {
2599ad675b32SBjoern A. Zeeb 
2600f592d0c3SBjoern A. Zeeb 	TAILQ_INIT(&V_nd6_defrouter);
2601ad675b32SBjoern A. Zeeb }
2602f77a6dbdSBjoern A. Zeeb 
2603f77a6dbdSBjoern A. Zeeb static int
2604f77a6dbdSBjoern A. Zeeb nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2605f77a6dbdSBjoern A. Zeeb {
2606f77a6dbdSBjoern A. Zeeb 	struct in6_defrouter d;
2607f77a6dbdSBjoern A. Zeeb 	struct nd_defrouter *dr;
2608f77a6dbdSBjoern A. Zeeb 	int error;
2609f77a6dbdSBjoern A. Zeeb 
2610f77a6dbdSBjoern A. Zeeb 	if (req->newptr != NULL)
2611f77a6dbdSBjoern A. Zeeb 		return (EPERM);
2612f77a6dbdSBjoern A. Zeeb 
2613f77a6dbdSBjoern A. Zeeb 	error = sysctl_wire_old_buffer(req, 0);
2614f77a6dbdSBjoern A. Zeeb 	if (error != 0)
2615f77a6dbdSBjoern A. Zeeb 		return (error);
2616f77a6dbdSBjoern A. Zeeb 
2617f77a6dbdSBjoern A. Zeeb 	bzero(&d, sizeof(d));
2618f77a6dbdSBjoern A. Zeeb 	d.rtaddr.sin6_family = AF_INET6;
2619f77a6dbdSBjoern A. Zeeb 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
2620f77a6dbdSBjoern A. Zeeb 
2621f77a6dbdSBjoern A. Zeeb 	ND6_RLOCK();
2622f77a6dbdSBjoern A. Zeeb 	TAILQ_FOREACH(dr, &V_nd6_defrouter, dr_entry) {
2623f77a6dbdSBjoern A. Zeeb 		d.rtaddr.sin6_addr = dr->rtaddr;
2624f77a6dbdSBjoern A. Zeeb 		error = sa6_recoverscope(&d.rtaddr);
2625f77a6dbdSBjoern A. Zeeb 		if (error != 0)
2626f77a6dbdSBjoern A. Zeeb 			break;
2627f77a6dbdSBjoern A. Zeeb 		d.flags = dr->raflags;
2628f77a6dbdSBjoern A. Zeeb 		d.rtlifetime = dr->rtlifetime;
2629f77a6dbdSBjoern A. Zeeb 		d.expire = dr->expire + (time_second - time_uptime);
2630f77a6dbdSBjoern A. Zeeb 		d.if_index = dr->ifp->if_index;
2631f77a6dbdSBjoern A. Zeeb 		error = SYSCTL_OUT(req, &d, sizeof(d));
2632f77a6dbdSBjoern A. Zeeb 		if (error != 0)
2633f77a6dbdSBjoern A. Zeeb 			break;
2634f77a6dbdSBjoern A. Zeeb 	}
2635f77a6dbdSBjoern A. Zeeb 	ND6_RUNLOCK();
2636f77a6dbdSBjoern A. Zeeb 	return (error);
2637f77a6dbdSBjoern A. Zeeb }
2638f77a6dbdSBjoern A. Zeeb SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2639f77a6dbdSBjoern A. Zeeb 	CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2640f77a6dbdSBjoern A. Zeeb 	NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter",
2641f77a6dbdSBjoern A. Zeeb 	"NDP default router list");
2642