]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/nd6_rtr.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / nd6_rtr.c
CommitLineData
2d21ac55 1/*
eb6b6ca3 2 * Copyright (c) 2003-2020 Apple Inc. All rights reserved.
2d21ac55
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
1c79356b
A
29/*
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
1c79356b
A
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/malloc.h>
60#include <sys/mbuf.h>
61#include <sys/socket.h>
62#include <sys/sockio.h>
63#include <sys/time.h>
64#include <sys/kernel.h>
65#include <sys/errno.h>
1c79356b 66#include <sys/syslog.h>
9bccf70c 67#include <sys/queue.h>
6d2010ae 68#include <sys/mcache.h>
39236c6e
A
69#include <sys/protosw.h>
70
71#include <dev/random/randomdev.h>
6d2010ae 72
fe8ab488 73#include <kern/locks.h>
6d2010ae
A
74#include <kern/zalloc.h>
75#include <machine/machine_routines.h>
1c79356b
A
76
77#include <net/if.h>
39037602 78#include <net/if_var.h>
1c79356b
A
79#include <net/if_types.h>
80#include <net/if_dl.h>
81#include <net/route.h>
82#include <net/radix.h>
83
84#include <netinet/in.h>
85#include <netinet6/in6_var.h>
9bccf70c 86#include <netinet6/in6_ifattach.h>
1c79356b
A
87#include <netinet/ip6.h>
88#include <netinet6/ip6_var.h>
89#include <netinet6/nd6.h>
90#include <netinet/icmp6.h>
9bccf70c 91#include <netinet6/scope6_var.h>
1c79356b
A
92
93#include <net/net_osdep.h>
94
f427ee49
A
95static void defrouter_addreq(struct nd_defrouter *, struct nd_route_info *, boolean_t);
96static void defrouter_delreq(struct nd_defrouter *, struct nd_route_info *);
6d2010ae 97static struct nd_defrouter *defrtrlist_update_common(struct nd_defrouter *,
f427ee49 98 struct nd_drhead *, boolean_t);
91447636 99static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
0a7de745 100 struct nd_defrouter *);
91447636 101static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
39236c6e 102static void pfxrtr_del(struct nd_pfxrouter *, struct nd_prefix *);
91447636 103static struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *);
f427ee49 104static void nd6_rtmsg(u_char, struct rtentry *);
1c79356b 105
6d2010ae
A
106static int nd6_prefix_onlink_common(struct nd_prefix *, boolean_t,
107 unsigned int);
108static struct nd_prefix *nd6_prefix_equal_lookup(struct nd_prefix *, boolean_t);
109static void nd6_prefix_sync(struct ifnet *);
110
111static void in6_init_address_ltimes(struct nd_prefix *,
39236c6e 112 struct in6_addrlifetime *);
1c79356b 113
91447636 114static int rt6_deleteroute(struct radix_node *, void *);
1c79356b 115
f427ee49 116static struct nd_defrouter *nddr_alloc(zalloc_flags_t);
6d2010ae
A
117static void nddr_free(struct nd_defrouter *);
118static void nddr_trace(struct nd_defrouter *, int);
119
120static struct nd_prefix *ndpr_alloc(int);
121static void ndpr_free(struct nd_prefix *);
122static void ndpr_trace(struct nd_prefix *, int);
123
1c79356b
A
124extern int nd6_recalc_reachtm_interval;
125
39037602
A
126static struct ifnet *nd6_defifp = NULL;
127int nd6_defifindex = 0;
6d2010ae
A
128static unsigned int nd6_defrouter_genid;
129
f427ee49 130int ip6_use_tempaddr = IP6_USE_TMPADDR_DEFAULT; /* use temp addr by default for testing now */
1c79356b 131
6d2010ae 132int nd6_accept_6to4 = 1;
1c79356b 133
9bccf70c
A
134int ip6_desync_factor;
135u_int32_t ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME;
136u_int32_t ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME;
137/*
138 * shorter lifetimes for debugging purposes.
39236c6e
A
139 * u_int32_t ip6_temp_preferred_lifetime = 800;
140 * static u_int32_t ip6_temp_valid_lifetime = 1800;
141 */
9bccf70c 142int ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE;
1c79356b 143
91447636
A
144extern lck_mtx_t *nd6_mutex;
145
6d2010ae
A
146/* Serialization variables for single thread access to nd_prefix */
147static boolean_t nd_prefix_busy;
148static void *nd_prefix_waitchan = &nd_prefix_busy;
149static int nd_prefix_waiters = 0;
150
151/* Serialization variables for single thread access to nd_defrouter */
152static boolean_t nd_defrouter_busy;
153static void *nd_defrouter_waitchan = &nd_defrouter_busy;
154static int nd_defrouter_waiters = 0;
155
f427ee49 156#define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
6d2010ae 157/* RTPREF_MEDIUM has to be 0! */
0a7de745
A
158#define RTPREF_HIGH 1
159#define RTPREF_MEDIUM 0
160#define RTPREF_LOW (-1)
161#define RTPREF_RESERVED (-2)
162#define RTPREF_INVALID (-3) /* internal */
6d2010ae 163
0a7de745 164#define NDPR_TRACE_HIST_SIZE 32 /* size of trace history */
6d2010ae
A
165
166/* For gdb */
167__private_extern__ unsigned int ndpr_trace_hist_size = NDPR_TRACE_HIST_SIZE;
168
169struct nd_prefix_dbg {
0a7de745
A
170 struct nd_prefix ndpr_pr; /* nd_prefix */
171 u_int16_t ndpr_refhold_cnt; /* # of ref */
172 u_int16_t ndpr_refrele_cnt; /* # of rele */
6d2010ae
A
173 /*
174 * Circular lists of ndpr_addref and ndpr_remref callers.
175 */
0a7de745
A
176 ctrace_t ndpr_refhold[NDPR_TRACE_HIST_SIZE];
177 ctrace_t ndpr_refrele[NDPR_TRACE_HIST_SIZE];
6d2010ae
A
178};
179
0a7de745 180static unsigned int ndpr_debug; /* debug flags */
0a7de745 181static struct zone *ndpr_zone; /* zone for nd_prefix */
0a7de745 182#define NDPR_ZONE_NAME "nd6_prefix" /* zone name */
6d2010ae 183
0a7de745 184#define NDDR_TRACE_HIST_SIZE 32 /* size of trace history */
6d2010ae
A
185
186/* For gdb */
187__private_extern__ unsigned int nddr_trace_hist_size = NDDR_TRACE_HIST_SIZE;
188
189struct nd_defrouter_dbg {
0a7de745
A
190 struct nd_defrouter nddr_dr; /* nd_defrouter */
191 uint16_t nddr_refhold_cnt; /* # of ref */
192 uint16_t nddr_refrele_cnt; /* # of rele */
6d2010ae 193 /*
f427ee49 194 * Circular lists of nddr_addref and nddr_remref callers.
6d2010ae 195 */
0a7de745
A
196 ctrace_t nddr_refhold[NDDR_TRACE_HIST_SIZE];
197 ctrace_t nddr_refrele[NDDR_TRACE_HIST_SIZE];
6d2010ae
A
198};
199
0a7de745 200static unsigned int nddr_debug; /* debug flags */
0a7de745 201static struct zone *nddr_zone; /* zone for nd_defrouter */
0a7de745 202#define NDDR_ZONE_NAME "nd6_defrouter" /* zone name */
6d2010ae 203
f427ee49
A
204static ZONE_DECLARE(ndprtr_zone, "nd6_pfxrouter",
205 sizeof(struct nd_pfxrouter), ZC_NONE);
6d2010ae 206
f427ee49
A
207#define TWOHOUR (120*60)
208extern int nd6_process_rti; /* Default to 0 for now */
6d2010ae 209
f427ee49
A
210
211static void
212nd6_prefix_glb_init(void)
6d2010ae 213{
0a7de745 214 PE_parse_boot_argn("ifa_debug", &ndpr_debug, sizeof(ndpr_debug));
f427ee49 215 vm_size_t ndpr_size = (ndpr_debug == 0) ? sizeof(struct nd_prefix) :
0a7de745 216 sizeof(struct nd_prefix_dbg);
f427ee49
A
217 ndpr_zone = zone_create(NDPR_ZONE_NAME, ndpr_size, ZC_ZFREE_CLEARMEM);
218}
6d2010ae 219
f427ee49
A
220static void
221nd6_defrouter_glb_init(void)
222{
223 PE_parse_boot_argn("ifa_debug", &nddr_debug, sizeof(nddr_debug));
224 vm_size_t nddr_size = (nddr_debug == 0) ? sizeof(struct nd_defrouter) :
0a7de745 225 sizeof(struct nd_defrouter_dbg);
f427ee49
A
226 nddr_zone = zone_create(NDDR_ZONE_NAME, nddr_size, ZC_ZFREE_CLEARMEM);
227}
6d2010ae 228
f427ee49
A
229void
230nd6_rtr_init(void)
231{
232 nd6_prefix_glb_init();
233 nd6_defrouter_glb_init();
6d2010ae
A
234}
235
1c79356b
A
236/*
237 * Receive Router Solicitation Message - just for routers.
238 * Router solicitation/advertisement is mostly managed by userland program
239 * (rtadvd) so here we have no function like nd6_ra_output().
240 *
241 * Based on RFC 2461
242 */
243void
91447636 244nd6_rs_input(
0a7de745 245 struct mbuf *m,
91447636
A
246 int off,
247 int icmp6len)
1c79356b
A
248{
249 struct ifnet *ifp = m->m_pkthdr.rcvif;
250 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
eb6b6ca3 251 struct nd_router_solicit *nd_rs = NULL;
1c79356b 252 struct in6_addr saddr6 = ip6->ip6_src;
1c79356b
A
253 char *lladdr = NULL;
254 int lladdrlen = 0;
eb6b6ca3 255 union nd_opts ndopts = {};
1c79356b 256
316670eb
A
257 /* Expect 32-bit aligned data pointer on strict-align platforms */
258 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
259
1c79356b 260 /* If I'm not a router, ignore it. */
f427ee49 261 if (!ip6_forwarding || ifp->if_ipv6_router_mode == IPV6_ROUTER_MODE_DISABLED) {
1c79356b 262 goto freeit;
0a7de745 263 }
1c79356b
A
264
265 /* Sanity checks */
cb323159
A
266 if (ip6->ip6_hlim != IPV6_MAXHLIM) {
267 nd6log(error,
9bccf70c
A
268 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
269 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
cb323159 270 ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
9bccf70c 271 goto bad;
1c79356b
A
272 }
273
274 /*
6d2010ae
A
275 * Don't update the neighbor cache, if src = :: or a non-neighbor.
276 * The former case indicates that the src has no IP address assigned
277 * yet. See nd6_ns_input() for the latter case.
316670eb
A
278 */
279 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1c79356b 280 goto freeit;
316670eb 281 } else {
6d2010ae
A
282 struct sockaddr_in6 src_sa6;
283
0a7de745 284 bzero(&src_sa6, sizeof(src_sa6));
6d2010ae 285 src_sa6.sin6_family = AF_INET6;
0a7de745 286 src_sa6.sin6_len = sizeof(src_sa6);
6d2010ae
A
287 src_sa6.sin6_addr = ip6->ip6_src;
288 if (!nd6_is_addr_neighbor(&src_sa6, ifp, 0)) {
cb323159
A
289 nd6log(info, "nd6_rs_input: "
290 "RS packet from non-neighbor\n");
6d2010ae
A
291 goto freeit;
292 }
293 }
1c79356b 294
0a7de745 295 IP6_EXTHDR_CHECK(m, off, icmp6len, return );
eb6b6ca3 296 ip6 = mtod(m, struct ip6_hdr *);
1c79356b 297 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
0a7de745 298 icmp6len -= sizeof(*nd_rs);
1c79356b
A
299 nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
300 if (nd6_options(&ndopts) < 0) {
cb323159
A
301 nd6log(info,
302 "nd6_rs_input: invalid ND option, ignored\n");
9bccf70c 303 /* nd6_options have incremented stats */
1c79356b
A
304 goto freeit;
305 }
306
307 if (ndopts.nd_opts_src_lladdr) {
308 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
309 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
310 }
311
312 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
cb323159 313 nd6log(info,
1c79356b
A
314 "nd6_rs_input: lladdrlen mismatch for %s "
315 "(if %d, RS packet %d)\n",
cb323159 316 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2);
9bccf70c 317 goto bad;
1c79356b
A
318 }
319
320 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
321
39236c6e 322freeit:
1c79356b 323 m_freem(m);
9bccf70c
A
324 return;
325
39236c6e 326bad:
9bccf70c
A
327 icmp6stat.icp6s_badrs++;
328 m_freem(m);
1c79356b
A
329}
330
f427ee49
A
331#define ND_OPT_LEN_TO_BYTE_SCALE 3 /* ND opt len is in units of 8 octets */
332
333#define ND_OPT_LEN_RTI_MIN 1
334#define ND_OPT_LEN_RTI_MAX 3
335#define ND_OPT_RTI_PFXLEN_MAX 128
1c79356b
A
336/*
337 * Receive Router Advertisement Message.
338 *
339 * Based on RFC 2461
340 * TODO: on-link bit on prefix information
341 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
342 */
343void
91447636 344nd6_ra_input(
0a7de745 345 struct mbuf *m,
316670eb 346 int off,
91447636 347 int icmp6len)
1c79356b
A
348{
349 struct ifnet *ifp = m->m_pkthdr.rcvif;
b0d623f7 350 struct nd_ifinfo *ndi = NULL;
1c79356b
A
351 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
352 struct nd_router_advert *nd_ra;
353 struct in6_addr saddr6 = ip6->ip6_src;
6d2010ae 354 int mcast = 0;
1c79356b 355 union nd_opts ndopts;
6d2010ae 356 struct nd_defrouter *dr = NULL;
316670eb
A
357 u_int32_t mtu = 0;
358 char *lladdr = NULL;
359 u_int32_t lladdrlen = 0;
360 struct nd_prefix_list *nd_prefix_list_head = NULL;
361 u_int32_t nd_prefix_list_length = 0;
362 struct in6_ifaddr *ia6 = NULL;
39236c6e
A
363 struct nd_prefix_list *prfl;
364 struct nd_defrouter dr0;
365 u_int32_t advreachable;
f427ee49 366 boolean_t rti_defrtr_processed = FALSE;
39236c6e 367
5ba3f43e 368#if (DEVELOPMENT || DEBUG)
0a7de745 369 if (ip6_accept_rtadv == 0) {
5ba3f43e 370 goto freeit;
0a7de745 371 }
5ba3f43e 372#endif /* (DEVELOPMENT || DEBUG) */
316670eb
A
373 /* Expect 32-bit aligned data pointer on strict-align platforms */
374 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
91447636 375
316670eb 376 /*
f427ee49
A
377 * Accept the RA if IFEF_ACCEPT_RTADV is set, or when
378 * we're acting as a router and the RA is locally generated.
379 * For convenience, we allow locally generated (rtadvd)
316670eb
A
380 * RAs to be processed on the advertising interface, as a router.
381 *
382 * Note that we don't test against ip6_forwarding as we could be
383 * both a host and a router on different interfaces, hence the
384 * check against the per-interface flags.
385 */
f427ee49
A
386 if ((ifp->if_eflags & IFEF_ACCEPT_RTADV) == 0) {
387 if (ifp->if_ipv6_router_mode == IPV6_ROUTER_MODE_EXCLUSIVE &&
388 (ia6 = ifa_foraddr6(&saddr6)) != NULL) {
389 /* accept locally generated RA */
390 } else {
391 goto freeit;
392 }
0a7de745 393 }
1c79356b 394
316670eb
A
395 if (ia6 != NULL) {
396 IFA_REMREF(&ia6->ia_ifa);
397 ia6 = NULL;
398 }
399
cb323159
A
400 if (ip6->ip6_hlim != IPV6_MAXHLIM) {
401 nd6log(error,
9bccf70c
A
402 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
403 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
cb323159 404 ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
9bccf70c 405 goto bad;
1c79356b
A
406 }
407
408 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
cb323159 409 nd6log(error,
1c79356b 410 "nd6_ra_input: src %s is not link-local\n",
cb323159 411 ip6_sprintf(&saddr6));
9bccf70c 412 goto bad;
1c79356b
A
413 }
414
0a7de745 415 IP6_EXTHDR_CHECK(m, off, icmp6len, return );
eb6b6ca3 416 ip6 = mtod(m, struct ip6_hdr *);
1c79356b 417 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
1c79356b 418
0a7de745 419 icmp6len -= sizeof(*nd_ra);
1c79356b
A
420 nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
421 if (nd6_options(&ndopts) < 0) {
cb323159
A
422 nd6log(info,
423 "nd6_ra_input: invalid ND option, ignored\n");
9bccf70c 424 /* nd6_options have incremented stats */
1c79356b
A
425 goto freeit;
426 }
427
39236c6e 428 advreachable = nd_ra->nd_ra_reachable;
1c79356b 429
6d2010ae 430 /* remember if this is a multicasted advertisement */
0a7de745 431 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
6d2010ae 432 mcast = 1;
0a7de745 433 }
6d2010ae 434
3e170ce0 435 ndi = ND_IFINFO(ifp);
f427ee49 436 VERIFY(NULL != ndi && TRUE == ndi->initialized);
316670eb 437 lck_mtx_lock(&ndi->lock);
1c79356b
A
438 /* unspecified or not? (RFC 2461 6.3.4) */
439 if (advreachable) {
55e303ae 440 advreachable = ntohl(advreachable);
1c79356b
A
441 if (advreachable <= MAX_REACHABLE_TIME &&
442 ndi->basereachable != advreachable) {
443 ndi->basereachable = advreachable;
444 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
445 ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
446 }
447 }
0a7de745 448 if (nd_ra->nd_ra_retransmit) {
1c79356b 449 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
0a7de745 450 }
3e170ce0
A
451 if (nd_ra->nd_ra_curhoplimit) {
452 if (ndi->chlim < nd_ra->nd_ra_curhoplimit) {
453 ndi->chlim = nd_ra->nd_ra_curhoplimit;
454 } else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
cb323159 455 nd6log(error,
3e170ce0
A
456 "RA with a lower CurHopLimit sent from "
457 "%s on %s (current = %d, received = %d). "
458 "Ignored.\n", ip6_sprintf(&ip6->ip6_src),
459 if_name(ifp), ndi->chlim,
cb323159 460 nd_ra->nd_ra_curhoplimit);
3e170ce0
A
461 }
462 }
316670eb 463 lck_mtx_unlock(&ndi->lock);
f427ee49
A
464
465 /* Initialize nd_defrouter invariants for RA processing */
466 bzero(&dr0, sizeof(dr0));
467 dr0.rtaddr = saddr6;
468 dr0.ifp = ifp;
469
470 /*
471 * Route Information Option
472 */
473 if (ndopts.nd_opts_rti && IFNET_IS_ETHERNET(ifp)) {
474 struct nd_opt_hdr *rt = NULL;
475 struct sockaddr_in6 rti_gateway = {0};
476
477 rti_gateway.sin6_family = AF_INET6;
478 rti_gateway.sin6_len = sizeof(rti_gateway);
479 memcpy(&rti_gateway.sin6_addr, &saddr6, sizeof(rti_gateway.sin6_addr));
480
481 for (rt = (struct nd_opt_hdr *)ndopts.nd_opts_rti;
482 rt <= (struct nd_opt_hdr *)ndopts.nd_opts_rti_end;
483 rt = (struct nd_opt_hdr *)((caddr_t)rt +
484 (rt->nd_opt_len << ND_OPT_LEN_TO_BYTE_SCALE))) {
485 struct sockaddr_in6 rti_prefix = {};
486 struct nd_route_info rti = {};
487 struct nd_opt_route_info *rti_opt = NULL;
488 u_int32_t rounded_prefix_bytes = 0;
489
490 if (rt->nd_opt_type != ND_OPT_ROUTE_INFO) {
491 continue;
492 }
493
494 rti_opt = (struct nd_opt_route_info *)rt;
495 if ((rti_opt->nd_opt_rti_len < ND_OPT_LEN_RTI_MIN) ||
496 (rti_opt->nd_opt_rti_len > ND_OPT_LEN_RTI_MAX)) {
497 nd6log(info,
498 "%s: invalid option "
499 "len %d for route information option, "
500 "ignored\n", __func__,
501 rti_opt->nd_opt_rti_len);
502 continue;
503 }
504
505 if (rti_opt->nd_opt_rti_prefixlen > ND_OPT_RTI_PFXLEN_MAX) {
506 nd6log(info,
507 "%s: invalid prefix length %d "
508 "in the route information option, "
509 "ignored\n", __func__, rti_opt->nd_opt_rti_prefixlen);
510 continue;
511 }
512
513 if (rti_opt->nd_opt_rti_prefixlen != 0 &&
514 rti_opt->nd_opt_rti_prefixlen <= 64 &&
515 rti_opt->nd_opt_rti_len == ND_OPT_LEN_RTI_MIN) {
516 nd6log(info,
517 "%s: invalid prefix "
518 "len %d is OOB for route information option, "
519 "with total option length of %d. Ignored.\n",
520 __func__, rti_opt->nd_opt_rti_prefixlen,
521 rti_opt->nd_opt_rti_len);
522 continue;
523 }
524
525 if (rti_opt->nd_opt_rti_prefixlen > 64 &&
526 rti_opt->nd_opt_rti_len != ND_OPT_LEN_RTI_MAX) {
527 nd6log(info,
528 "%s: invalid prefix "
529 "len %d is OOB for route information option, "
530 "with total option length of %d. Ignored.\n",
531 __func__, rti_opt->nd_opt_rti_prefixlen,
532 rti_opt->nd_opt_rti_len);
533 continue;
534 }
535
536 if ((rti_opt->nd_opt_rti_flags & ND_RA_FLAG_RTPREF_MASK) ==
537 ND_RA_FLAG_RTPREF_RSV) {
538 nd6log(info,
539 "%s: using reserved preference mask, "
540 "ignored\n", __func__);
541 continue;
542 }
543
544 rti_prefix.sin6_family = AF_INET6;
545 rti_prefix.sin6_len = sizeof(rti_prefix);
546
547 rounded_prefix_bytes = rti_opt->nd_opt_rti_prefixlen >> 3;
548 if (rti_opt->nd_opt_rti_prefixlen & 0x7) {
549 rounded_prefix_bytes++;
550 }
551 memcpy(&rti_prefix.sin6_addr, rti_opt + 1, rounded_prefix_bytes);
552
553 nd6log(info, "%s: received RA with route opt, "
554 "prefix %s/%u pref %u lifetime %u\n", __func__,
555 ip6_sprintf(&rti_prefix.sin6_addr),
556 rti_opt->nd_opt_rti_prefixlen,
557 rti_opt->nd_opt_rti_flags,
558 ntohl(rti_opt->nd_opt_rti_lifetime));
559
560 dr0.flags = rti_opt->nd_opt_rti_flags;
561 dr0.stateflags = 0;
562
563 /*
564 * https://tools.ietf.org/html/rfc4191#section-3.1
565 * Type C Host requirements:
566 * The Router Preference and Lifetime values in a
567 * ::/0 Route Information Option override the
568 * preference and lifetime values in the Router
569 * Advertisement header.
570 */
571 if (IN6_IS_ADDR_UNSPECIFIED(&rti_prefix.sin6_addr)) {
572 rti_defrtr_processed = TRUE;
573 /*
574 * If the router lifetime is 0, set the state flag
575 * to dummy, so that it is skipped and not used as a
576 * default router.
577 * Set the lifetime to 2 hrs to make sure we get rid
578 * of the router eventually if this was indeed for a router
579 * going away.
580 *
581 * We partly have to do this to ensure advertised prefixes
582 * stay onlink.
583 * A periodic RA would also keep refreshing the cached
584 * neighbor cache entry if it contains source link layer
585 * information.
586 */
587 if (rti_opt->nd_opt_rti_lifetime == 0) {
588 dr0.rtlifetime = TWOHOUR;
589 dr0.stateflags |= NDDRF_INELIGIBLE;
590 } else {
591 dr0.rtlifetime = ntohl(rti_opt->nd_opt_rti_lifetime);
592 }
593 dr0.expire = net_uptime() + dr0.rtlifetime;
594
595 lck_mtx_lock(nd6_mutex);
596 dr = defrtrlist_update(&dr0, NULL);
597 lck_mtx_unlock(nd6_mutex);
598 continue;
599 }
600
601 dr0.rtlifetime = ntohl(rti_opt->nd_opt_rti_lifetime);
602 dr0.expire = net_uptime() + dr0.rtlifetime;
603 bzero(&rti, sizeof(rti));
604 rti.nd_rti_prefixlen = rti_opt->nd_opt_rti_prefixlen;
605 rti.nd_rti_prefix = rti_prefix.sin6_addr;
606 nd6_rtilist_update(&rti, &dr0);
607 }
608 }
609
610 if (!rti_defrtr_processed) {
611 dr0.flags = nd_ra->nd_ra_flags_reserved;
612 dr0.stateflags = 0;
613 /*
614 * If the router lifetime is 0, set the state flag
615 * to dummy, so that it is skipped and not used as a
616 * default router.
617 * Set the lifetime to 2 hrs to make sure we get rid
618 * of the router eventually if this was indeed for a router
619 * going away.
620 *
621 * We partly have to do this to ensure advertised prefixes
622 * stay onlink.
623 * A periodic RA would also keep refreshing the cached
624 * neighbor cache entry if it contains source link layer
625 * information.
626 */
627 if (nd_ra->nd_ra_router_lifetime == 0) {
628 dr0.rtlifetime = TWOHOUR;
629 dr0.stateflags |= NDDRF_INELIGIBLE;
630 } else {
631 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
632 }
633 dr0.expire = net_uptime() + dr0.rtlifetime;
634 lck_mtx_lock(nd6_mutex);
635 dr = defrtrlist_update(&dr0, NULL);
636 lck_mtx_unlock(nd6_mutex);
637 }
1c79356b
A
638
639 /*
640 * prefix
641 */
642 if (ndopts.nd_opts_pi) {
643 struct nd_opt_hdr *pt;
9bccf70c 644 struct nd_opt_prefix_info *pi = NULL;
1c79356b
A
645 struct nd_prefix pr;
646
647 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
39236c6e
A
648 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
649 pt = (struct nd_opt_hdr *)((caddr_t)pt +
f427ee49 650 (pt->nd_opt_len << ND_OPT_LEN_TO_BYTE_SCALE))) {
39037602
A
651 struct in6_addr pi_mask;
652 bzero(&pi_mask, sizeof(pi_mask));
653
0a7de745 654 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) {
1c79356b 655 continue;
0a7de745 656 }
1c79356b
A
657 pi = (struct nd_opt_prefix_info *)pt;
658
659 if (pi->nd_opt_pi_len != 4) {
cb323159 660 nd6log(info,
9bccf70c
A
661 "nd6_ra_input: invalid option "
662 "len %d for prefix information option, "
cb323159 663 "ignored\n", pi->nd_opt_pi_len);
1c79356b
A
664 continue;
665 }
666
667 if (128 < pi->nd_opt_pi_prefix_len) {
cb323159 668 nd6log(info,
9bccf70c
A
669 "nd6_ra_input: invalid prefix "
670 "len %d for prefix information option, "
cb323159 671 "ignored\n", pi->nd_opt_pi_prefix_len);
1c79356b
A
672 continue;
673 }
674
39037602
A
675 /*
676 * To ignore ::/64 make sure bits beyond prefixlen
677 * are set to zero
678 */
679 in6_prefixlen2mask(&pi_mask, pi->nd_opt_pi_prefix_len);
680 pi->nd_opt_pi_prefix.s6_addr32[0] &= pi_mask.s6_addr32[0];
681 pi->nd_opt_pi_prefix.s6_addr32[1] &= pi_mask.s6_addr32[1];
682 pi->nd_opt_pi_prefix.s6_addr32[2] &= pi_mask.s6_addr32[2];
683 pi->nd_opt_pi_prefix.s6_addr32[3] &= pi_mask.s6_addr32[3];
684
685 if (IN6_IS_ADDR_UNSPECIFIED(&pi->nd_opt_pi_prefix) ||
686 IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) ||
39236c6e 687 IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
cb323159 688 nd6log(info,
39236c6e
A
689 "%s: invalid prefix %s, ignored\n",
690 __func__,
cb323159 691 ip6_sprintf(&pi->nd_opt_pi_prefix));
1c79356b
A
692 continue;
693 }
694
0a7de745 695 bzero(&pr, sizeof(pr));
6d2010ae
A
696 lck_mtx_init(&pr.ndpr_lock, ifa_mtx_grp, ifa_mtx_attr);
697 NDPR_LOCK(&pr);
1c79356b 698 pr.ndpr_prefix.sin6_family = AF_INET6;
0a7de745 699 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
1c79356b 700 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
91447636 701 pr.ndpr_ifp = m->m_pkthdr.rcvif;
1c79356b
A
702
703 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
39236c6e 704 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
1c79356b 705 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
39236c6e 706 ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
1c79356b
A
707 pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
708 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
709 pr.ndpr_pltime =
0a7de745 710 ntohl(pi->nd_opt_pi_preferred_time);
1c79356b 711
6d2010ae
A
712 /*
713 * Exceptions to stateless autoconfiguration processing:
714 * + nd6_accept_6to4 == 0 && address has 6to4 prefix
316670eb
A
715 * + ip6_only_allow_rfc4193_prefix != 0 &&
716 * address not RFC 4193
6d2010ae
A
717 */
718 if (ip6_only_allow_rfc4193_prefix &&
719 !IN6_IS_ADDR_UNIQUE_LOCAL(&pi->nd_opt_pi_prefix)) {
cb323159 720 nd6log(info,
316670eb
A
721 "nd6_ra_input: no SLAAC on prefix %s "
722 "[not RFC 4193]\n",
cb323159 723 ip6_sprintf(&pi->nd_opt_pi_prefix));
6d2010ae 724 pr.ndpr_raf_auto = 0;
39236c6e
A
725 } else if (!nd6_accept_6to4 &&
726 IN6_IS_ADDR_6TO4(&pi->nd_opt_pi_prefix)) {
cb323159 727 nd6log(info,
39236c6e
A
728 "%s: no SLAAC on prefix %s "
729 "[6to4]\n", __func__,
cb323159 730 ip6_sprintf(&pi->nd_opt_pi_prefix));
6d2010ae
A
731 pr.ndpr_raf_auto = 0;
732 }
1c79356b 733
6d2010ae
A
734 if (in6_init_prefix_ltimes(&pr)) {
735 NDPR_UNLOCK(&pr);
736 lck_mtx_destroy(&pr.ndpr_lock, ifa_mtx_grp);
737 continue; /* prefix lifetime init failed */
738 } else {
739 NDPR_UNLOCK(&pr);
740 }
39236c6e 741 (void) prelist_update(&pr, dr, m, mcast);
6d2010ae 742 lck_mtx_destroy(&pr.ndpr_lock, ifa_mtx_grp);
316670eb
A
743
744 /*
745 * We have to copy the values out after the
746 * prelist_update call since some of these values won't
747 * be properly set until after the router advertisement
748 * updating can vet the values.
749 */
39236c6e 750 prfl = NULL;
0a7de745 751 MALLOC(prfl, struct nd_prefix_list *, sizeof(*prfl),
316670eb
A
752 M_TEMP, M_WAITOK | M_ZERO);
753
754 if (prfl == NULL) {
755 log(LOG_DEBUG, "%s: unable to MALLOC RA prefix "
756 "structure\n", __func__);
757 continue;
758 }
759
39236c6e 760 /* this is only for nd6_post_msg(), otherwise unused */
316670eb 761 bcopy(&pr.ndpr_prefix, &prfl->pr.ndpr_prefix,
0a7de745 762 sizeof(prfl->pr.ndpr_prefix));
316670eb
A
763 prfl->pr.ndpr_raf = pr.ndpr_raf;
764 prfl->pr.ndpr_plen = pr.ndpr_plen;
765 prfl->pr.ndpr_vltime = pr.ndpr_vltime;
766 prfl->pr.ndpr_pltime = pr.ndpr_pltime;
767 prfl->pr.ndpr_expire = pr.ndpr_expire;
39236c6e
A
768 prfl->pr.ndpr_base_calendartime =
769 pr.ndpr_base_calendartime;
770 prfl->pr.ndpr_base_uptime = pr.ndpr_base_uptime;
316670eb
A
771 prfl->pr.ndpr_stateflags = pr.ndpr_stateflags;
772 prfl->pr.ndpr_addrcnt = pr.ndpr_addrcnt;
773 prfl->pr.ndpr_ifp = pr.ndpr_ifp;
774
775 prfl->next = nd_prefix_list_head;
776 nd_prefix_list_head = prfl;
777 nd_prefix_list_length++;
1c79356b
A
778 }
779 }
780
f427ee49 781
1c79356b
A
782 /*
783 * MTU
784 */
785 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
316670eb 786 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
1c79356b
A
787 /* lower bound */
788 if (mtu < IPV6_MMTU) {
cb323159 789 nd6log(info, "nd6_ra_input: bogus mtu option "
1c79356b 790 "mtu=%d sent from %s, ignoring\n",
cb323159 791 mtu, ip6_sprintf(&ip6->ip6_src));
1c79356b
A
792 goto skip;
793 }
794
316670eb 795 lck_mtx_lock(&ndi->lock);
1c79356b
A
796 /* upper bound */
797 if (ndi->maxmtu) {
798 if (mtu <= ndi->maxmtu) {
799 int change = (ndi->linkmtu != mtu);
800
801 ndi->linkmtu = mtu;
316670eb 802 lck_mtx_unlock(&ndi->lock);
0a7de745 803 if (change) { /* in6_maxmtu may change */
1c79356b 804 in6_setmaxmtu();
0a7de745 805 }
1c79356b 806 } else {
cb323159 807 nd6log(info, "nd6_ra_input: bogus mtu "
1c79356b
A
808 "mtu=%d sent from %s; "
809 "exceeds maxmtu %d, ignoring\n",
810 mtu, ip6_sprintf(&ip6->ip6_src),
cb323159 811 ndi->maxmtu);
316670eb 812 lck_mtx_unlock(&ndi->lock);
1c79356b
A
813 }
814 } else {
316670eb 815 lck_mtx_unlock(&ndi->lock);
cb323159 816 nd6log(info, "nd6_ra_input: mtu option "
1c79356b
A
817 "mtu=%d sent from %s; maxmtu unknown, "
818 "ignoring\n",
cb323159 819 mtu, ip6_sprintf(&ip6->ip6_src));
1c79356b
A
820 }
821 }
822
39236c6e 823skip:
316670eb 824
1c79356b 825 /*
55e303ae 826 * Source link layer address
1c79356b 827 */
1c79356b
A
828 if (ndopts.nd_opts_src_lladdr) {
829 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
830 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
831 }
832
833 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
cb323159 834 nd6log(info,
1c79356b
A
835 "nd6_ra_input: lladdrlen mismatch for %s "
836 "(if %d, RA packet %d)\n",
cb323159 837 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2);
9bccf70c 838 goto bad;
1c79356b
A
839 }
840
0a7de745 841 if (dr && dr->stateflags & NDDRF_MAPPED) {
39037602 842 saddr6 = dr->rtaddr_mapped;
0a7de745 843 }
39037602 844
316670eb
A
845 nd6_cache_lladdr(ifp, &saddr6, lladdr, (int)lladdrlen,
846 ND_ROUTER_ADVERT, 0);
847
848 /* Post message */
849 nd6_post_msg(KEV_ND6_RA, nd_prefix_list_head, nd_prefix_list_length,
5ba3f43e 850 mtu);
1c79356b
A
851
852 /*
853 * Installing a link-layer address might change the state of the
854 * router's neighbor cache, which might also affect our on-link
855 * detection of adveritsed prefixes.
856 */
6d2010ae
A
857 lck_mtx_lock(nd6_mutex);
858 pfxlist_onlink_check();
859 lck_mtx_unlock(nd6_mutex);
1c79356b 860
39236c6e 861freeit:
9bccf70c 862 m_freem(m);
0a7de745 863 if (dr) {
6d2010ae 864 NDDR_REMREF(dr);
0a7de745 865 }
316670eb 866
39236c6e 867 prfl = NULL;
316670eb
A
868 while ((prfl = nd_prefix_list_head) != NULL) {
869 nd_prefix_list_head = prfl->next;
870 FREE(prfl, M_TEMP);
871 }
316670eb 872
9bccf70c 873 return;
1c79356b 874
39236c6e 875bad:
9bccf70c 876 icmp6stat.icp6s_badra++;
6d2010ae 877 goto freeit;
1c79356b
A
878}
879
880/*
881 * default router list proccessing sub routines
882 */
9bccf70c
A
883
884/* tell the change to user processes watching the routing socket. */
885static void
f427ee49 886nd6_rtmsg(u_char cmd, struct rtentry *rt)
9bccf70c
A
887{
888 struct rt_addrinfo info;
b0d623f7 889 struct ifnet *ifp = rt->rt_ifp;
9bccf70c 890
b0d623f7 891 RT_LOCK_ASSERT_HELD(rt);
91447636 892
0a7de745 893 bzero((caddr_t)&info, sizeof(info));
39236c6e 894 /* It's not necessary to lock ifp for if_lladdr */
9bccf70c
A
895 info.rti_info[RTAX_DST] = rt_key(rt);
896 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
897 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
6d2010ae
A
898 /*
899 * ifa_addr pointers for both should always be valid
900 * in this context; no need to hold locks.
901 */
902 info.rti_info[RTAX_IFP] = ifp->if_lladdr->ifa_addr;
9bccf70c
A
903 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
904
905 rt_missmsg(cmd, &info, rt->rt_flags, 0);
906}
907
316670eb 908static void
f427ee49 909defrouter_addreq(struct nd_defrouter *new, struct nd_route_info *rti, boolean_t scoped)
1c79356b 910{
f427ee49 911 struct sockaddr_in6 key, mask, gate;
9bccf70c 912 struct rtentry *newrt = NULL;
6d2010ae
A
913 unsigned int ifscope;
914 int err;
39037602 915 struct nd_ifinfo *ndi = ND_IFINFO(new->ifp);
f427ee49 916 int rtflags = RTF_GATEWAY;
6d2010ae 917
5ba3f43e 918 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
39236c6e
A
919 NDDR_LOCK_ASSERT_NOTHELD(new);
920 /*
0a7de745 921 * We're free to lock and unlock NDDR because our callers
39236c6e
A
922 * are holding an extra reference for us.
923 */
6d2010ae 924
39236c6e 925 NDDR_LOCK(new);
0a7de745 926 if (new->stateflags & NDDRF_INSTALLED) {
39236c6e 927 goto out;
0a7de745 928 }
f427ee49 929 if (new->ifp->if_ipv6_router_mode == IPV6_ROUTER_MODE_EXCLUSIVE) {
cb323159 930 nd6log2(info, "%s: ignoring router %s, scoped=%d, "
316670eb
A
931 "static=%d on advertising interface\n", if_name(new->ifp),
932 ip6_sprintf(&new->rtaddr), scoped,
cb323159 933 (new->stateflags & NDDRF_STATIC) ? 1 : 0);
39236c6e 934 goto out;
316670eb
A
935 }
936
cb323159 937 nd6log2(info, "%s: adding default router %s, scoped=%d, "
6d2010ae 938 "static=%d\n", if_name(new->ifp), ip6_sprintf(&new->rtaddr),
cb323159 939 scoped, (new->stateflags & NDDRF_STATIC) ? 1 : 0);
1c79356b 940
f427ee49 941 Bzero(&key, sizeof(key));
0a7de745
A
942 Bzero(&mask, sizeof(mask));
943 Bzero(&gate, sizeof(gate));
1c79356b 944
f427ee49 945 key.sin6_len = mask.sin6_len = gate.sin6_len
0a7de745 946 = sizeof(struct sockaddr_in6);
f427ee49
A
947 key.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
948
949 if (rti != NULL) {
950 key.sin6_addr = rti->nd_rti_prefix;
951 in6_len2mask(&mask.sin6_addr, rti->nd_rti_prefixlen);
952 if (rti->nd_rti_prefixlen == ND_OPT_RTI_PFXLEN_MAX) {
953 rtflags |= RTF_HOST;
954 } else {
955 rtflags |= RTF_PRCLONING;
956 }
957 }
39037602 958
0a7de745 959 if (new->stateflags & NDDRF_MAPPED) {
39037602 960 gate.sin6_addr = new->rtaddr_mapped;
0a7de745 961 } else {
39037602 962 gate.sin6_addr = new->rtaddr;
0a7de745 963 }
1c79356b 964
6d2010ae 965 ifscope = scoped ? new->ifp->if_index : IFSCOPE_NONE;
39236c6e 966 NDDR_UNLOCK(new);
6d2010ae 967
39037602
A
968 /*
969 * Cellular networks may have buggy deployments
970 * with gateway IPv6 link local address with same
971 * interface identifier as the one that has been
972 * assigned for the cellular context.
973 * If gateway is same as locally configured link local
974 * interface on cellular interface, generated a different one
975 * and store it in the nd_defrouter entry and use it to work
976 * on routing table
977 */
978 if (new->ifp->if_type == IFT_CELLULAR &&
979 !(new->stateflags & NDDRF_STATIC) &&
980 !(new->stateflags & NDDRF_MAPPED) &&
981 IN6_IS_ADDR_LINKLOCAL(&gate.sin6_addr) &&
982 ndi && !(ndi->flags & ND6_IFF_PERFORMNUD)) {
983 struct in6_ifaddr *tmp_ia6 = in6ifa_ifpforlinklocal(new->ifp, 0);
984
985 if (tmp_ia6 != NULL &&
986 !(tmp_ia6->ia6_flags & IN6_IFF_NOTMANUAL) &&
987 IN6_ARE_ADDR_EQUAL(&tmp_ia6->ia_addr.sin6_addr,
0a7de745 988 &gate.sin6_addr)) {
39037602
A
989 gate.sin6_addr.s6_addr8[15] += 1;
990 new->rtaddr_mapped = gate.sin6_addr;
991 new->stateflags |= NDDRF_MAPPED;
992
cb323159
A
993 nd6log(info, "%s: Default router %s mapped "
994 "to ", if_name(new->ifp), ip6_sprintf(&new->rtaddr));
995 nd6log(info, "%s\n", ip6_sprintf(&new->rtaddr_mapped));
39037602
A
996 }
997 }
998
f427ee49 999 err = rtrequest_scoped(RTM_ADD, (struct sockaddr *)&key,
b0d623f7 1000 (struct sockaddr *)&gate, (struct sockaddr *)&mask,
f427ee49 1001 rtflags, &newrt, ifscope);
6d2010ae 1002
9bccf70c 1003 if (newrt) {
b0d623f7 1004 RT_LOCK(newrt);
0a7de745 1005 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
b0d623f7
A
1006 RT_REMREF_LOCKED(newrt);
1007 RT_UNLOCK(newrt);
39236c6e 1008 NDDR_LOCK(new);
6d2010ae 1009 new->stateflags |= NDDRF_INSTALLED;
0a7de745 1010 if (ifscope != IFSCOPE_NONE) {
6d2010ae 1011 new->stateflags |= NDDRF_IFSCOPE;
0a7de745 1012 }
9bccf70c 1013 } else {
cb323159 1014 nd6log(error, "%s: failed to add default router "
6d2010ae
A
1015 "%s on %s scoped %d (errno = %d)\n", __func__,
1016 ip6_sprintf(&gate.sin6_addr), if_name(new->ifp),
cb323159 1017 (ifscope != IFSCOPE_NONE), err);
39236c6e 1018 NDDR_LOCK(new);
1c79356b 1019 }
6d2010ae 1020 new->err = err;
39236c6e
A
1021
1022out:
1023 NDDR_UNLOCK(new);
1c79356b
A
1024}
1025
1026struct nd_defrouter *
91447636 1027defrouter_lookup(
f427ee49 1028 struct nd_drhead *nd_router_listp,
91447636
A
1029 struct in6_addr *addr,
1030 struct ifnet *ifp)
1c79356b
A
1031{
1032 struct nd_defrouter *dr;
1033
5ba3f43e 1034 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
91447636 1035
f427ee49
A
1036 if (nd_router_listp == NULL) {
1037 nd_router_listp = &nd_defrouter_list;
1038 }
1039
1040 for (dr = TAILQ_FIRST(nd_router_listp); dr;
39236c6e 1041 dr = TAILQ_NEXT(dr, dr_entry)) {
6d2010ae
A
1042 NDDR_LOCK(dr);
1043 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
f427ee49 1044 NDDR_ADDREF(dr);
6d2010ae 1045 NDDR_UNLOCK(dr);
0a7de745 1046 return dr;
6d2010ae
A
1047 }
1048 NDDR_UNLOCK(dr);
1c79356b
A
1049 }
1050
0a7de745 1051 return NULL; /* search failed */
1c79356b
A
1052}
1053
6d2010ae
A
1054/*
1055 * Remove the default route for a given router.
1056 * This is just a subroutine function for defrouter_select(), and should
1057 * not be called from anywhere else.
1058 */
316670eb 1059static void
f427ee49 1060defrouter_delreq(struct nd_defrouter *dr, struct nd_route_info *rti)
1c79356b 1061{
f427ee49 1062 struct sockaddr_in6 key, mask, gate;
9bccf70c 1063 struct rtentry *oldrt = NULL;
6d2010ae
A
1064 unsigned int ifscope;
1065 int err;
1066
5ba3f43e 1067 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
39236c6e
A
1068 NDDR_LOCK_ASSERT_NOTHELD(dr);
1069 /*
0a7de745 1070 * We're free to lock and unlock NDDR because our callers
39236c6e
A
1071 * are holding an extra reference for us.
1072 */
1073 NDDR_LOCK(dr);
6d2010ae 1074 /* ifp would be NULL for the "drany" case */
0a7de745 1075 if (dr->ifp != NULL && !(dr->stateflags & NDDRF_INSTALLED)) {
39236c6e 1076 goto out;
0a7de745 1077 }
6d2010ae 1078
cb323159 1079 nd6log2(info, "%s: removing default router %s, scoped=%d, "
6d2010ae
A
1080 "static=%d\n", dr->ifp != NULL ? if_name(dr->ifp) : "ANY",
1081 ip6_sprintf(&dr->rtaddr), (dr->stateflags & NDDRF_IFSCOPE) ? 1 : 0,
cb323159 1082 (dr->stateflags & NDDRF_STATIC) ? 1 : 0);
1c79356b 1083
f427ee49 1084 Bzero(&key, sizeof(key));
0a7de745
A
1085 Bzero(&mask, sizeof(mask));
1086 Bzero(&gate, sizeof(gate));
1c79356b 1087
f427ee49 1088 key.sin6_len = mask.sin6_len = gate.sin6_len
0a7de745 1089 = sizeof(struct sockaddr_in6);
f427ee49 1090 key.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
39037602 1091
f427ee49
A
1092
1093 if (rti != NULL) {
1094 key.sin6_addr = rti->nd_rti_prefix;
1095 in6_len2mask(&mask.sin6_addr, rti->nd_rti_prefixlen);
1096 }
39037602
A
1097 /*
1098 * The router entry may be mapped to a different address.
1099 * If that is the case, use the mapped address as gateway
1100 * to do operation on the routing table.
1101 * To get more context, read the related comment in
1102 * defrouter_addreq
1103 */
0a7de745 1104 if (dr->stateflags & NDDRF_MAPPED) {
39037602 1105 gate.sin6_addr = dr->rtaddr_mapped;
0a7de745 1106 } else {
39037602 1107 gate.sin6_addr = dr->rtaddr;
0a7de745 1108 }
1c79356b 1109
6d2010ae
A
1110 if (dr->ifp != NULL) {
1111 ifscope = (dr->stateflags & NDDRF_IFSCOPE) ?
1112 dr->ifp->if_index : IFSCOPE_NONE;
1113 } else {
1114 ifscope = IFSCOPE_NONE;
1115 }
39236c6e 1116 NDDR_UNLOCK(dr);
39037602 1117
6d2010ae 1118 err = rtrequest_scoped(RTM_DELETE,
f427ee49 1119 (struct sockaddr *)&key, (struct sockaddr *)&gate,
6d2010ae
A
1120 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, ifscope);
1121
9bccf70c 1122 if (oldrt) {
b0d623f7 1123 RT_LOCK(oldrt);
9bccf70c 1124 nd6_rtmsg(RTM_DELETE, oldrt);
b0d623f7
A
1125 RT_UNLOCK(oldrt);
1126 rtfree(oldrt);
6d2010ae 1127 } else if (err != ESRCH) {
cb323159 1128 nd6log(error, "%s: failed to delete default router "
6d2010ae
A
1129 "%s on %s scoped %d (errno = %d)\n", __func__,
1130 ip6_sprintf(&gate.sin6_addr), dr->ifp != NULL ?
cb323159 1131 if_name(dr->ifp) : "ANY", (ifscope != IFSCOPE_NONE), err);
6d2010ae 1132 }
39236c6e 1133 NDDR_LOCK(dr);
6d2010ae
A
1134 /* ESRCH means it's no longer in the routing table; ignore it */
1135 if (oldrt != NULL || err == ESRCH) {
1136 dr->stateflags &= ~NDDRF_INSTALLED;
0a7de745 1137 if (ifscope != IFSCOPE_NONE) {
6d2010ae 1138 dr->stateflags &= ~NDDRF_IFSCOPE;
0a7de745 1139 }
6d2010ae
A
1140 }
1141 dr->err = 0;
39236c6e
A
1142out:
1143 NDDR_UNLOCK(dr);
6d2010ae
A
1144}
1145
1146
1147/*
1148 * remove all default routes from default router list
1149 */
1150void
1151defrouter_reset(void)
1152{
1153 struct nd_defrouter *dr, drany;
1154
5ba3f43e 1155 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae 1156
f427ee49 1157 dr = TAILQ_FIRST(&nd_defrouter_list);
6d2010ae
A
1158 while (dr) {
1159 NDDR_LOCK(dr);
1160 if (dr->stateflags & NDDRF_INSTALLED) {
f427ee49 1161 NDDR_ADDREF(dr);
6d2010ae
A
1162 NDDR_UNLOCK(dr);
1163 lck_mtx_unlock(nd6_mutex);
f427ee49 1164 defrouter_delreq(dr, NULL);
6d2010ae
A
1165 lck_mtx_lock(nd6_mutex);
1166 NDDR_REMREF(dr);
f427ee49 1167 dr = TAILQ_FIRST(&nd_defrouter_list);
6d2010ae
A
1168 } else {
1169 NDDR_UNLOCK(dr);
1170 dr = TAILQ_NEXT(dr, dr_entry);
1171 }
1172 }
1173
1174 /* Nuke primary (non-scoped) default router */
0a7de745 1175 bzero(&drany, sizeof(drany));
39037602
A
1176 lck_mtx_init(&drany.nddr_lock, ifa_mtx_grp, ifa_mtx_attr);
1177 lck_mtx_unlock(nd6_mutex);
f427ee49 1178 defrouter_delreq(&drany, NULL);
39037602
A
1179 lck_mtx_destroy(&drany.nddr_lock, ifa_mtx_grp);
1180 lck_mtx_lock(nd6_mutex);
6d2010ae
A
1181}
1182
1183int
1184defrtrlist_ioctl(u_long cmd, caddr_t data)
1185{
6d2010ae
A
1186 struct nd_defrouter dr0;
1187 unsigned int ifindex;
1188 struct ifnet *dr_ifp;
1189 int error = 0, add = 0;
1190
39037602 1191 /* XXX Handle mapped default router entries */
6d2010ae 1192 switch (cmd) {
0a7de745
A
1193 case SIOCDRADD_IN6_32: /* struct in6_defrouter_32 */
1194 case SIOCDRADD_IN6_64: /* struct in6_defrouter_64 */
6d2010ae 1195 ++add;
f427ee49 1196 OS_FALLTHROUGH;
0a7de745
A
1197 case SIOCDRDEL_IN6_32: /* struct in6_defrouter_32 */
1198 case SIOCDRDEL_IN6_64: /* struct in6_defrouter_64 */
1199 bzero(&dr0, sizeof(dr0));
6d2010ae 1200 if (cmd == SIOCDRADD_IN6_64 || cmd == SIOCDRDEL_IN6_64) {
316670eb
A
1201 struct in6_defrouter_64 *r_64 =
1202 (struct in6_defrouter_64 *)(void *)data;
1203 u_int16_t i;
1204
1205 bcopy(&r_64->rtaddr.sin6_addr, &dr0.rtaddr,
0a7de745 1206 sizeof(dr0.rtaddr));
6d2010ae 1207 dr0.flags = r_64->flags;
0a7de745 1208 bcopy(&r_64->if_index, &i, sizeof(i));
316670eb 1209 ifindex = i;
6d2010ae 1210 } else {
316670eb
A
1211 struct in6_defrouter_32 *r_32 =
1212 (struct in6_defrouter_32 *)(void *)data;
1213 u_int16_t i;
1214
1215 bcopy(&r_32->rtaddr.sin6_addr, &dr0.rtaddr,
0a7de745 1216 sizeof(dr0.rtaddr));
6d2010ae 1217 dr0.flags = r_32->flags;
0a7de745 1218 bcopy(&r_32->if_index, &i, sizeof(i));
316670eb 1219 ifindex = i;
6d2010ae
A
1220 }
1221 ifnet_head_lock_shared();
1222 /* Don't need to check is ifindex is < 0 since it's unsigned */
1223 if (if_index < ifindex ||
1224 (dr_ifp = ifindex2ifnet[ifindex]) == NULL) {
1225 ifnet_head_done();
1226 error = EINVAL;
1227 break;
1228 }
1229 dr0.ifp = dr_ifp;
1230 ifnet_head_done();
1231
4d15aeb1
A
1232 if (ND_IFINFO(dr_ifp) == NULL ||
1233 !ND_IFINFO(dr_ifp)->initialized) {
1234 error = ENXIO;
1235 break;
1236 }
1237
6d2010ae
A
1238 if (IN6_IS_SCOPE_EMBED(&dr0.rtaddr)) {
1239 uint16_t *scope = &dr0.rtaddr.s6_addr16[1];
1240
1241 if (*scope == 0) {
1242 *scope = htons(dr_ifp->if_index);
1243 } else if (*scope != htons(dr_ifp->if_index)) {
1244 error = EINVAL;
1245 break;
1246 }
1247 }
1248
0a7de745 1249 if (add) {
6d2010ae 1250 error = defrtrlist_add_static(&dr0);
0a7de745 1251 }
6d2010ae
A
1252 if (!add || error != 0) {
1253 int err = defrtrlist_del_static(&dr0);
0a7de745 1254 if (!add) {
6d2010ae 1255 error = err;
0a7de745 1256 }
6d2010ae
A
1257 }
1258 break;
1259
1260 default:
1261 error = EOPNOTSUPP; /* check for safety */
1262 break;
9bccf70c 1263 }
1c79356b 1264
0a7de745 1265 return error;
1c79356b
A
1266}
1267
39037602
A
1268/*
1269 * XXX Please make sure to remove dr from the
1270 * global default router tailq list before this
1271 * function call.
1272 * Also ensure that you release the list reference
1273 * only after calling this routine.
1274 */
1c79356b 1275void
f427ee49 1276defrtrlist_del(struct nd_defrouter *dr, struct nd_drhead *nd_router_listp)
1c79356b 1277{
39037602
A
1278#if (DEVELOPMENT || DEBUG)
1279 struct nd_defrouter *dr_itr = NULL;
1280#endif
1c79356b 1281 struct nd_prefix *pr;
b0d623f7 1282 struct ifnet *ifp = dr->ifp;
3e170ce0 1283 struct nd_ifinfo *ndi = NULL;
f427ee49
A
1284 boolean_t resetmtu = FALSE;
1285 struct nd_route_info *rti = NULL;
1c79356b 1286
5ba3f43e 1287 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae 1288
f427ee49
A
1289 if (nd_router_listp == NULL) {
1290 nd_router_listp = &nd_defrouter_list;
1291 }
1292
1293 if (nd_router_listp != &nd_defrouter_list) {
1294 rti = (struct nd_route_info *)nd_router_listp;
1295 }
1296
39037602
A
1297#if (DEVELOPMENT || DEBUG)
1298 /*
1299 * Verify that the router is not in the global default
1300 * router list.
1301 * Can't use defrouter_lookup here because that just works
1302 * with address and ifp pointer.
1303 * We have to compare the memory here.
1304 * Also we can't use ASSERT here as that is not defined
1305 * for development builds.
1306 */
f427ee49 1307 TAILQ_FOREACH(dr_itr, nd_router_listp, dr_entry)
0a7de745 1308 VERIFY(dr != dr_itr);
39037602 1309#endif
39236c6e 1310 ++nd6_defrouter_genid;
1c79356b
A
1311 /*
1312 * Flush all the routing table entries that use the router
1313 * as a next hop.
f427ee49
A
1314 *
1315 * XXX Note that for a router advertising itself as default router
1316 * and also advertising route information option, the following
1317 * code will have the default router entry and router entry of
1318 * RTI step over each other.
1319 * The following therefore may not be efficient but won't be
1320 * causing blocking issues.
1c79356b 1321 */
39037602
A
1322 NDDR_ADDREF(dr);
1323 lck_mtx_unlock(nd6_mutex);
0a7de745 1324 if (dr->stateflags & NDDRF_MAPPED) {
39037602 1325 rt6_flush(&dr->rtaddr_mapped, ifp);
0a7de745 1326 } else {
b0d623f7 1327 rt6_flush(&dr->rtaddr, ifp);
0a7de745 1328 }
39037602
A
1329 lck_mtx_lock(nd6_mutex);
1330 NDDR_REMREF(dr);
f427ee49
A
1331 nd6log2(info, "%s: freeing route to %s with gateway %s\n", if_name(dr->ifp),
1332 (rti == NULL)? "::" : ip6_sprintf(&rti->nd_rti_prefix),
cb323159 1333 ip6_sprintf(&dr->rtaddr));
6d2010ae
A
1334 /*
1335 * Delete it from the routing table.
1336 */
1337 NDDR_ADDREF(dr);
1338 lck_mtx_unlock(nd6_mutex);
f427ee49 1339 defrouter_delreq(dr, rti);
6d2010ae
A
1340 lck_mtx_lock(nd6_mutex);
1341 NDDR_REMREF(dr);
1c79356b
A
1342
1343 /*
f427ee49
A
1344 * The following should mostly be limited to when we are working
1345 * with a default router entry and not a router entry from
1346 * rti router list.
1c79356b 1347 */
f427ee49
A
1348 if (rti == NULL) {
1349 /*
1350 * Also delete all the pointers to the router in each prefix lists.
1351 */
1352 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
1353 struct nd_pfxrouter *pfxrtr;
6d2010ae 1354
f427ee49
A
1355 NDPR_LOCK(pr);
1356 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) {
1357 pfxrtr_del(pfxrtr, pr);
1358 }
1359 NDPR_UNLOCK(pr);
0a7de745 1360 }
f427ee49 1361 pfxlist_onlink_check();
1c79356b 1362 }
3e170ce0 1363 ndi = ND_IFINFO(ifp);
f427ee49 1364 VERIFY(NULL != ndi && TRUE == ndi->initialized);
3e170ce0
A
1365 lck_mtx_lock(&ndi->lock);
1366 VERIFY(ndi->ndefrouters >= 0);
1367 if (ndi->ndefrouters > 0 && --ndi->ndefrouters == 0) {
1368 nd6_ifreset(ifp);
1369 resetmtu = TRUE;
e2fac8b1 1370 }
3e170ce0 1371 lck_mtx_unlock(&ndi->lock);
39037602
A
1372 /*
1373 * If the router is the primary one, choose a new one.
1374 * We always try to pick another eligible router
1375 * on this interface as we do scoped routing
1376 */
f427ee49 1377 defrouter_select(ifp, nd_router_listp);
39037602 1378
0a7de745 1379 if (resetmtu) {
39236c6e 1380 nd6_setmtu(ifp);
0a7de745 1381 }
6d2010ae
A
1382}
1383
1384int
1385defrtrlist_add_static(struct nd_defrouter *new)
1386{
1387 struct nd_defrouter *dr;
1388 int err = 0;
1389
1390 new->rtlifetime = -1;
1391 new->stateflags |= NDDRF_STATIC;
1392
1393 /* we only want the preference level */
1394 new->flags &= ND_RA_FLAG_RTPREF_MASK;
1395
1396 lck_mtx_lock(nd6_mutex);
f427ee49 1397 dr = defrouter_lookup(NULL, &new->rtaddr, new->ifp);
6d2010ae
A
1398 if (dr != NULL && !(dr->stateflags & NDDRF_STATIC)) {
1399 err = EINVAL;
1400 } else {
0a7de745 1401 if (dr != NULL) {
6d2010ae 1402 NDDR_REMREF(dr);
0a7de745 1403 }
f427ee49 1404 dr = defrtrlist_update(new, NULL);
0a7de745 1405 if (dr != NULL) {
6d2010ae 1406 err = dr->err;
0a7de745 1407 } else {
6d2010ae 1408 err = ENOMEM;
0a7de745 1409 }
6d2010ae 1410 }
0a7de745 1411 if (dr != NULL) {
6d2010ae 1412 NDDR_REMREF(dr);
0a7de745 1413 }
6d2010ae
A
1414 lck_mtx_unlock(nd6_mutex);
1415
0a7de745 1416 return err;
6d2010ae
A
1417}
1418
1419int
1420defrtrlist_del_static(struct nd_defrouter *new)
1421{
1422 struct nd_defrouter *dr;
1423
1424 lck_mtx_lock(nd6_mutex);
f427ee49 1425 dr = defrouter_lookup(NULL, &new->rtaddr, new->ifp);
6d2010ae 1426 if (dr == NULL || !(dr->stateflags & NDDRF_STATIC)) {
0a7de745 1427 if (dr != NULL) {
6d2010ae 1428 NDDR_REMREF(dr);
0a7de745 1429 }
6d2010ae
A
1430 dr = NULL;
1431 } else {
f427ee49
A
1432 TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry);
1433 defrtrlist_del(dr, NULL);
39037602 1434 NDDR_REMREF(dr); /* remove list reference */
6d2010ae
A
1435 NDDR_REMREF(dr);
1436 }
1437 lck_mtx_unlock(nd6_mutex);
91447636 1438
0a7de745 1439 return dr != NULL ? 0 : EINVAL;
6d2010ae
A
1440}
1441
1442/*
1443 * for default router selection
1444 * regards router-preference field as a 2-bit signed integer
1445 */
1446static int
1447rtpref(struct nd_defrouter *dr)
1448{
1449 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) {
1450 case ND_RA_FLAG_RTPREF_HIGH:
0a7de745 1451 return RTPREF_HIGH;
6d2010ae
A
1452 case ND_RA_FLAG_RTPREF_MEDIUM:
1453 case ND_RA_FLAG_RTPREF_RSV:
0a7de745 1454 return RTPREF_MEDIUM;
6d2010ae 1455 case ND_RA_FLAG_RTPREF_LOW:
0a7de745 1456 return RTPREF_LOW;
6d2010ae
A
1457 default:
1458 /*
1459 * This case should never happen. If it did, it would mean a
1460 * serious bug of kernel internal. We thus always bark here.
1461 * Or, can we even panic?
1462 */
1463 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->flags);
0a7de745 1464 return RTPREF_INVALID;
6d2010ae
A
1465 }
1466 /* NOTREACHED */
1c79356b
A
1467}
1468
1469/*
316670eb 1470 * Default Router Selection according to Section 6.3.6 of RFC 2461 and RFC 4191:
6d2010ae
A
1471 *
1472 * 1) Routers that are reachable or probably reachable should be preferred.
1473 * If we have more than one (probably) reachable router, prefer ones
1474 * with the highest router preference.
1c79356b
A
1475 * 2) When no routers on the list are known to be reachable or
1476 * probably reachable, routers SHOULD be selected in a round-robin
6d2010ae 1477 * fashion, regardless of router preference values.
1c79356b
A
1478 * 3) If the Default Router List is empty, assume that all
1479 * destinations are on-link.
6d2010ae
A
1480 *
1481 * When Scoped Routing is enabled, the selection logic is amended as follows:
1482 *
1483 * a) When a default interface is specified, the primary/non-scoped default
1484 * router will be set to the reachable router on that link (if any) with
1485 * the highest router preference.
1486 * b) When there are more than one routers on the same link, the one with
1487 * the highest router preference will be installed, either as scoped or
1488 * non-scoped route entry. If they all share the same preference value,
1489 * the one installed will be the static or the first encountered reachable
1490 * router, i.e. static one wins over dynamic.
1491 * c) When no routers on the list are known to be reachable, or probably
1492 * reachable, no round-robin selection will take place when the default
1493 * interface is set.
1494 *
1495 * We assume nd_defrouter is sorted by router preference value.
1496 * Since the code below covers both with and without router preference cases,
1497 * we do not need to classify the cases by ifdef.
1c79356b 1498 */
316670eb 1499void
f427ee49 1500defrouter_select(struct ifnet *ifp, struct nd_drhead *nd_router_listp)
1c79356b 1501{
39037602
A
1502 struct nd_defrouter *dr = NULL;
1503 struct nd_defrouter *selected_dr = NULL;
1504 struct nd_defrouter *installed_dr = NULL;
1c79356b 1505 struct llinfo_nd6 *ln = NULL;
39037602
A
1506 struct rtentry *rt = NULL;
1507 struct nd_ifinfo *ndi = NULL;
1508 unsigned int genid = 0;
1509 boolean_t is_installed_reachable = FALSE;
f427ee49
A
1510 struct nd_route_info *rti = NULL;
1511 boolean_t scoped = TRUE;
1512 boolean_t is_rti_rtrlist = FALSE;
6d2010ae 1513
5ba3f43e 1514 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
1c79356b 1515
f427ee49
A
1516 if (nd_router_listp == NULL) {
1517 nd_router_listp = &nd_defrouter_list;
1518 }
1519
1520 if (nd_router_listp != &nd_defrouter_list) {
1521 rti = (struct nd_route_info *)nd_router_listp;
1522 /* XXX For now we treat RTI routes as un-scoped */
1523 scoped = FALSE;
1524 is_rti_rtrlist = TRUE;
1525 }
1526
1527
39037602 1528 if (ifp == NULL) {
cb323159
A
1529 ifp = nd6_defifp;
1530 if (ifp == NULL) {
1531 nd6log2(info,
1532 "%s:%d: Return early. NULL interface",
1533 __func__, __LINE__);
1534 return;
1535 }
1536 nd6log2(info,
1537 "%s:%d: NULL interface passed. Setting to default interface %s.\n",
1538 __func__, __LINE__, if_name(ifp));
39037602
A
1539 }
1540
f427ee49
A
1541 /*
1542 * When we are working with RTI router list, the nd6_defifp may be
1543 * NULL. That is the scenario when the network may not have WAN
1544 * v6 connectivity and the only RAs we may be getting are with lifetime
1545 * 0.
1546 */
1547 if (ifp == lo_ifp && !is_rti_rtrlist) {
cb323159 1548 nd6log2(info,
39037602
A
1549 "%s:%d: Return early. "
1550 "Default router select called for loopback.\n",
cb323159 1551 __func__, __LINE__);
39037602
A
1552 return;
1553 }
1554
f427ee49 1555 if (ifp->if_ipv6_router_mode == IPV6_ROUTER_MODE_EXCLUSIVE) {
cb323159 1556 nd6log2(info,
39037602
A
1557 "%s:%d: Return early. "
1558 "Default router select called for interface"
f427ee49 1559 " %s in IPV6_ROUTER_MODE_EXCLUSIVE\n",
cb323159 1560 __func__, __LINE__, if_name(ifp));
6d2010ae
A
1561 return;
1562 }
1563
1564 /*
1565 * Let's handle easy case (3) first:
1566 * If default router list is empty, there's nothing to be done.
1567 */
f427ee49 1568 if (!TAILQ_FIRST(nd_router_listp)) {
cb323159 1569 nd6log2(info,
39037602 1570 "%s:%d: Return early. "
cb323159 1571 "Default router is empty.\n", __func__, __LINE__);
39037602
A
1572 return;
1573 }
1574
1575 /*
1576 * Take an early exit if number of routers in nd_ifinfo is
1577 * 0 for the interface.
1578 */
1579 ndi = ND_IFINFO(ifp);
1580 if (!ndi || !ndi->initialized) {
cb323159 1581 nd6log2(info,
39037602
A
1582 "%s:%d: Return early. "
1583 "Interface %s's nd_ifinfo not initialized.\n",
cb323159 1584 __func__, __LINE__, if_name(ifp));
6d2010ae 1585 return;
39037602
A
1586 }
1587
f427ee49
A
1588 /*
1589 * RTI router list routes are installed as unscoped.
1590 * Since there can be only one unscoped route, we need to
1591 * go through the entire list and consider all interfaces.
1592 * Further, for now, RTI option is only processed on Ethernet
1593 * type interfaces only.
1594 */
1595 if (ndi->ndefrouters == 0 && !is_rti_rtrlist) {
cb323159 1596 nd6log2(info,
39037602
A
1597 "%s:%d: Return early. "
1598 "%s does not have any default routers.\n",
cb323159 1599 __func__, __LINE__, if_name(ifp));
39037602
A
1600 return;
1601 }
6d2010ae
A
1602
1603 /*
1604 * Due to the number of times we drop nd6_mutex, we need to
1605 * serialize this function.
1606 */
1607 while (nd_defrouter_busy) {
1608 nd_defrouter_waiters++;
0a7de745 1609 msleep(nd_defrouter_waitchan, nd6_mutex, (PZERO - 1),
6d2010ae 1610 __func__, NULL);
5ba3f43e 1611 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae
A
1612 }
1613 nd_defrouter_busy = TRUE;
91447636 1614
6d2010ae
A
1615 /*
1616 * Search for a (probably) reachable router from the list.
1617 * We just pick up the first reachable one (if any), assuming that
1618 * the ordering rule of the list described in defrtrlist_update().
1619 *
1620 * For all intents and purposes of Scoped Routing:
1621 * selected_dr = candidate for primary router
1622 * installed_dr = currently installed primary router
1623 */
39037602 1624 genid = nd6_defrouter_genid;
f427ee49 1625 dr = TAILQ_FIRST(nd_router_listp);
39037602
A
1626
1627 while (dr != NULL) {
316670eb 1628 struct in6_addr rtaddr;
39037602
A
1629 struct ifnet *drifp = NULL;
1630 struct nd_defrouter *drrele = NULL;
6d2010ae 1631
316670eb 1632 NDDR_LOCK(dr);
316670eb 1633 drifp = dr->ifp;
f427ee49
A
1634 if (drifp != ifp && !is_rti_rtrlist) {
1635 NDDR_UNLOCK(dr);
1636 dr = TAILQ_NEXT(dr, dr_entry);
1637 continue;
1638 }
1639
1640 if (dr->stateflags & NDDRF_INELIGIBLE) {
39037602
A
1641 NDDR_UNLOCK(dr);
1642 dr = TAILQ_NEXT(dr, dr_entry);
f427ee49 1643 nd6log(info, "Ignoring dummy entry for default router.");
39037602
A
1644 continue;
1645 }
1646
1647 /*
1648 * Optimize for the common case.
1649 * When the interface has only one default router
1650 * there's no point checking for reachability as
1651 * there's nothing else to choose from.
1652 */
f427ee49 1653 if (ndi->ndefrouters == 1 && !is_rti_rtrlist) {
cb323159 1654 nd6log2(info,
39037602
A
1655 "%s:%d: Fast forward default router selection "
1656 "as interface %s has learned only one default "
1657 "router and there's nothing else to choose from.\n",
cb323159 1658 __func__, __LINE__, if_name(ifp));
39037602
A
1659 VERIFY(selected_dr == NULL && installed_dr == NULL);
1660 selected_dr = dr;
0a7de745 1661 if (dr->stateflags & NDDRF_INSTALLED) {
39037602 1662 installed_dr = dr;
0a7de745 1663 }
f427ee49 1664 NDDR_ADDREF(selected_dr);
39037602
A
1665 NDDR_UNLOCK(dr);
1666 goto install_route;
1667 }
1668
0a7de745 1669 if (dr->stateflags & NDDRF_MAPPED) {
39037602 1670 rtaddr = dr->rtaddr_mapped;
0a7de745 1671 } else {
39037602 1672 rtaddr = dr->rtaddr;
0a7de745 1673 }
39037602 1674
f427ee49 1675 NDDR_ADDREF(dr); /* for this for loop */
316670eb
A
1676 NDDR_UNLOCK(dr);
1677
316670eb 1678 /* Callee returns a locked route upon success */
39037602
A
1679 if (selected_dr == NULL) {
1680 lck_mtx_unlock(nd6_mutex);
1681 if ((rt = nd6_lookup(&rtaddr, 0, drifp, 0)) != NULL &&
1682 (ln = rt->rt_llinfo) != NULL &&
b0d623f7 1683 ND6_IS_LLINFO_PROBREACH(ln)) {
39037602
A
1684 RT_LOCK_ASSERT_HELD(rt);
1685 selected_dr = dr;
1686 NDDR_ADDREF(selected_dr);
b0d623f7 1687 }
39037602
A
1688 lck_mtx_lock(nd6_mutex);
1689 }
1690
1691 if (rt) {
b0d623f7
A
1692 RT_REMREF_LOCKED(rt);
1693 RT_UNLOCK(rt);
6d2010ae 1694 rt = NULL;
1c79356b 1695 }
1c79356b 1696
39037602
A
1697 /*
1698 * Handle case (b)
1699 * When there are more than one routers on the same link, the one with
1700 * the highest router preference will be installed.
1701 * Since the list is in decreasing order of preference:
1702 * 1) If selected_dr is not NULL, only use dr if it is static and has
1703 * equal preference and selected_dr is not static.
1704 * 2) Else if selected_dr is NULL, and dr is static make selected_dr = dr
1705 */
316670eb 1706 NDDR_LOCK(dr);
39037602 1707 if (((selected_dr && (rtpref(dr) >= rtpref(selected_dr)) &&
0a7de745
A
1708 !(selected_dr->stateflags & NDDRF_STATIC)) ||
1709 (selected_dr == NULL)) &&
39037602 1710 (dr->stateflags & NDDRF_STATIC)) {
316670eb
A
1711 if (selected_dr) {
1712 /* Release it later on */
1713 VERIFY(drrele == NULL);
1714 drrele = selected_dr;
1715 }
6d2010ae 1716 selected_dr = dr;
f427ee49 1717 NDDR_ADDREF(selected_dr);
6d2010ae
A
1718 }
1719
39037602
A
1720 /* Record the currently installed router */
1721 if (dr->stateflags & NDDRF_INSTALLED) {
6d2010ae
A
1722 if (installed_dr == NULL) {
1723 installed_dr = dr;
f427ee49 1724 NDDR_ADDREF(installed_dr);
0a7de745 1725 if (dr->stateflags & NDDRF_MAPPED) {
39037602 1726 rtaddr = installed_dr->rtaddr_mapped;
0a7de745 1727 } else {
39037602 1728 rtaddr = installed_dr->rtaddr;
0a7de745 1729 }
d190cdc3 1730 NDDR_UNLOCK(dr);
39037602
A
1731 lck_mtx_unlock(nd6_mutex);
1732 /* Callee returns a locked route upon success */
f427ee49 1733 if ((rt = nd6_lookup(&rtaddr, 0, installed_dr->ifp, 0)) != NULL) {
39037602
A
1734 RT_LOCK_ASSERT_HELD(rt);
1735 if ((ln = rt->rt_llinfo) != NULL &&
0a7de745 1736 ND6_IS_LLINFO_PROBREACH(ln)) {
39037602 1737 is_installed_reachable = TRUE;
0a7de745 1738 }
39037602
A
1739
1740 RT_REMREF_LOCKED(rt);
1741 RT_UNLOCK(rt);
1742 rt = NULL;
1743 }
1744 lck_mtx_lock(nd6_mutex);
9bccf70c 1745 } else {
6d2010ae 1746 /* this should not happen; warn for diagnosis */
cb323159 1747 nd6log(error, "defrouter_select: more than one "
39037602 1748 "default router is installed for interface :%s.\n",
f427ee49 1749 if_name(installed_dr->ifp));
d190cdc3 1750 NDDR_UNLOCK(dr);
9bccf70c 1751 }
0a7de745 1752 } else {
d190cdc3 1753 NDDR_UNLOCK(dr);
0a7de745 1754 }
d190cdc3 1755
0a7de745
A
1756 NDDR_REMREF(dr); /* for this for loop */
1757 if (drrele != NULL) {
316670eb 1758 NDDR_REMREF(drrele);
0a7de745 1759 }
6d2010ae 1760
39037602
A
1761 /*
1762 * Check if the list changed when we gave up
1763 * the nd6_mutex lock
1764 */
0a7de745 1765 if (genid != nd6_defrouter_genid) {
39037602
A
1766 if (selected_dr) {
1767 NDDR_REMREF(selected_dr);
1768 selected_dr = NULL;
6d2010ae
A
1769 }
1770
39037602 1771 if (installed_dr) {
d190cdc3 1772 NDDR_REMREF(installed_dr);
39037602 1773 installed_dr = NULL;
6d2010ae 1774 }
6d2010ae 1775
f427ee49 1776 if (ndi->ndefrouters == 0 && !is_rti_rtrlist) {
cb323159 1777 nd6log2(info,
39037602
A
1778 "%s:%d: Interface %s no longer "
1779 "has any default routers. Abort.\n",
cb323159 1780 __func__, __LINE__, if_name(ifp));
39037602
A
1781 goto out;
1782 }
cb323159 1783 nd6log2(info,
39037602
A
1784 "%s:%d: Iterate default router list again "
1785 "for interface %s, as the list seems to have "
1786 "changed during release-reaquire of global "
1787 "nd6_mutex lock.\n",
cb323159 1788 __func__, __LINE__, if_name(ifp));
39037602
A
1789
1790 is_installed_reachable = FALSE;
1791 genid = nd6_defrouter_genid;
f427ee49 1792 dr = TAILQ_FIRST(nd_router_listp);
39037602
A
1793 } else {
1794 dr = TAILQ_NEXT(dr, dr_entry);
6d2010ae 1795 }
6d2010ae
A
1796 }
1797
1798 /*
1799 * If none of the default routers was found to be reachable,
39037602
A
1800 * round-robin the list regardless of preference.
1801 * Please note selected_dr equal to NULL implies that even
1802 * installed default router is not reachable
6d2010ae 1803 */
39037602
A
1804 if (selected_dr == NULL) {
1805 if (installed_dr) {
1806 for (dr = TAILQ_NEXT(installed_dr, dr_entry); dr;
1807 dr = TAILQ_NEXT(dr, dr_entry)) {
f427ee49
A
1808 if (installed_dr->ifp != dr->ifp && !is_rti_rtrlist) {
1809 continue;
1810 }
1811 if (dr->stateflags & NDDRF_INELIGIBLE) {
39037602 1812 continue;
0a7de745 1813 }
39037602
A
1814 selected_dr = dr;
1815 break;
6d2010ae 1816 }
6d2010ae 1817 }
6d2010ae 1818
6d2010ae 1819 /*
39037602
A
1820 * If none was installed or the installed one if the last
1821 * one on the list, select the first one from the list
6d2010ae 1822 */
39037602 1823 if ((installed_dr == NULL) || (selected_dr == NULL)) {
f427ee49 1824 for (dr = TAILQ_FIRST(nd_router_listp); dr;
39037602 1825 dr = TAILQ_NEXT(dr, dr_entry)) {
f427ee49
A
1826 if (dr->stateflags & NDDRF_INELIGIBLE) {
1827 continue;
1828 }
1829 if (dr->ifp == ifp || is_rti_rtrlist) {
39037602
A
1830 selected_dr = dr;
1831 break;
1832 }
6d2010ae 1833 }
6d2010ae 1834 }
6d2010ae 1835
39037602 1836 if ((selected_dr == NULL) && (installed_dr == NULL)) {
cb323159 1837 nd6log2(info,
39037602
A
1838 "%s:%d: Between release and reaquire of global "
1839 "nd6_mutex lock, the list seems to have changed "
1840 "and it does not have any default routers for "
1841 "interface %s.\n",
cb323159 1842 __func__, __LINE__, if_name(ifp));
6d2010ae 1843 goto out;
6d2010ae 1844 }
39037602 1845
0a7de745 1846 if (selected_dr != installed_dr) {
39037602 1847 NDDR_ADDREF(selected_dr);
0a7de745 1848 }
39037602
A
1849 } else if (installed_dr != NULL) {
1850 if (installed_dr != selected_dr) {
316670eb 1851 /*
39037602
A
1852 * This means that selected default router is reachable
1853 * while installed one may or may not be.
1854 * Static router should always be considered as reachable
1855 * for router selection process.
6d2010ae 1856 */
39037602
A
1857 if ((installed_dr->stateflags & NDDRF_STATIC) &&
1858 rtpref(installed_dr) >= rtpref(selected_dr)) {
1859 NDDR_REMREF(selected_dr);
1860 selected_dr = installed_dr;
1861 } else if (is_installed_reachable) {
1862 if (rtpref(selected_dr) <= rtpref(installed_dr)) {
1863 NDDR_REMREF(selected_dr);
1864 selected_dr = installed_dr;
1865 }
6d2010ae 1866 }
39037602
A
1867 } else {
1868 NDDR_REMREF(selected_dr);
6d2010ae 1869 }
6d2010ae
A
1870 }
1871
0a7de745 1872install_route:
6d2010ae 1873 /*
39037602
A
1874 * If the selected router is different than the installed one,
1875 * remove the installed router and install the selected one.
1876 * Note that the selected router is never NULL here.
1877 * Else check if the route entry scope has to be changed.
6d2010ae 1878 */
39037602
A
1879 lck_mtx_unlock(nd6_mutex);
1880 if (installed_dr != selected_dr) {
cb323159 1881 nd6log(info,
39037602
A
1882 "%s:%d: Found a better router for interface "
1883 "%s. Installing new default route.\n",
cb323159 1884 __func__, __LINE__, if_name(ifp));
39037602 1885 if (installed_dr != NULL) {
f427ee49 1886 defrouter_delreq(installed_dr, rti);
6d2010ae 1887 }
39037602
A
1888 /*
1889 * Install scoped route if the interface is not
1890 * the default nd6 interface.
1891 */
f427ee49
A
1892 defrouter_addreq(selected_dr, rti,
1893 scoped && (selected_dr->ifp != nd6_defifp));
39037602 1894 } else if (((installed_dr->stateflags & NDDRF_IFSCOPE) &&
0a7de745 1895 (installed_dr->ifp == nd6_defifp)) ||
f427ee49 1896 (scoped && !(installed_dr->stateflags & NDDRF_IFSCOPE) &&
0a7de745 1897 (installed_dr->ifp != nd6_defifp))) {
cb323159 1898 nd6log(info,
39037602
A
1899 "%s:%d: Need to reinstall default route for interface "
1900 "%s as its scope has changed.\n",
cb323159 1901 __func__, __LINE__, if_name(ifp));
f427ee49
A
1902 defrouter_delreq(installed_dr, rti);
1903 defrouter_addreq(installed_dr, rti,
1904 scoped && (installed_dr->ifp != nd6_defifp));
39037602 1905 } else {
cb323159 1906 nd6log2(info,
39037602
A
1907 "%s:%d: No need to change the default "
1908 "route for interface %s.\n",
cb323159 1909 __func__, __LINE__, if_name(ifp));
6d2010ae 1910 }
39037602 1911 lck_mtx_lock(nd6_mutex);
6d2010ae 1912out:
0a7de745 1913 if (selected_dr && (selected_dr != installed_dr)) {
6d2010ae 1914 NDDR_REMREF(selected_dr);
0a7de745
A
1915 }
1916 if (installed_dr) {
6d2010ae 1917 NDDR_REMREF(installed_dr);
0a7de745 1918 }
5ba3f43e 1919 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae
A
1920 VERIFY(nd_defrouter_busy);
1921 nd_defrouter_busy = FALSE;
1922 if (nd_defrouter_waiters > 0) {
1923 nd_defrouter_waiters = 0;
1924 wakeup(nd_defrouter_waitchan);
1925 }
1926}
1927
6d2010ae 1928static struct nd_defrouter *
f427ee49 1929defrtrlist_update_common(struct nd_defrouter *new, struct nd_drhead *nd_router_listp, boolean_t scoped)
6d2010ae
A
1930{
1931 struct nd_defrouter *dr, *n;
1932 struct ifnet *ifp = new->ifp;
316670eb 1933 struct nd_ifinfo *ndi = NULL;
39236c6e 1934 struct timeval caltime;
6d2010ae 1935
5ba3f43e 1936 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae 1937
f427ee49
A
1938 if (nd_router_listp == NULL) {
1939 nd_router_listp = &nd_defrouter_list;
1940 }
1941
1942 /*
1943 * If we are not operating on default router list,
1944 * it implies we are operating on RTI's router list.
1945 * XXX For now we manage RTI routes un-scoped.
1946 */
1947 if (nd_router_listp != &nd_defrouter_list) {
1948 scoped = FALSE;
1949 }
1950
1951 if ((dr = defrouter_lookup(nd_router_listp, &new->rtaddr, ifp)) != NULL) {
6d2010ae 1952 /* entry exists */
f427ee49
A
1953 /*
1954 * 1. If previous entry was not dummy and new is,
1955 * delete it and return NULL.
1956 * 2. If previous entry was dummy and the new one
1957 * is also dummy, simply return dr.
1958 * 3. If previous was dummy but new one is not,
1959 * make sure we perform default router selection again.
1960 */
1961 /* If the router was not added as a dummy and there's
1962 * been a change (lifetime advertised was 0, communicated
1963 * as NDDRF_INELIGIBLE flag), remove the entry.
1964 */
1965 if ((new->stateflags & NDDRF_INELIGIBLE) != 0 &&
1966 (dr->stateflags & NDDRF_INELIGIBLE) == 0) {
1967 TAILQ_REMOVE(nd_router_listp, dr, dr_entry);
1968 defrtrlist_del(dr, nd_router_listp);
39037602 1969 NDDR_REMREF(dr); /* remove list reference */
6d2010ae
A
1970 NDDR_REMREF(dr);
1971 dr = NULL;
f427ee49 1972 return NULL;
6d2010ae
A
1973 } else {
1974 int oldpref = rtpref(dr);
39037602 1975 struct nd_defrouter *p = NULL;
f427ee49
A
1976 boolean_t dummy_change = FALSE;
1977 /*
1978 * If new one is not dummy but the old one was,
1979 * reset the stateflag.
1980 */
1981 if ((new->stateflags & NDDRF_INELIGIBLE) == 0 &&
1982 (dr->stateflags & NDDRF_INELIGIBLE) != 0) {
1983 dummy_change = TRUE;
1984 dr->stateflags &= ~NDDRF_INELIGIBLE;
1985 }
1986
6d2010ae
A
1987 /* override */
1988 dr->flags = new->flags; /* xxx flag check */
1c79356b
A
1989 dr->rtlifetime = new->rtlifetime;
1990 dr->expire = new->expire;
6d2010ae
A
1991
1992 /*
1993 * If the preference does not change, there's no need
1994 * to sort the entries. If Scoped Routing is enabled,
1995 * put the primary/non-scoped router at the top of the
1996 * list of routers in the same preference band, unless
1997 * it's already at that position.
1998 */
39037602 1999 /* same preference and scoped; just return */
f427ee49 2000 if (rtpref(new) == oldpref && scoped && dummy_change == FALSE) {
0a7de745
A
2001 return dr;
2002 }
39037602 2003
f427ee49 2004 n = TAILQ_FIRST(nd_router_listp);
39037602
A
2005 while (n != NULL) {
2006 /* preference changed; sort it */
0a7de745 2007 if (rtpref(new) != oldpref) {
39037602 2008 break;
0a7de745 2009 }
39037602
A
2010
2011 /* not at the top of band; sort it */
2012 if (n != dr && rtpref(n) == oldpref &&
0a7de745 2013 (!p || rtpref(p) > rtpref(n))) {
39037602 2014 break;
0a7de745 2015 }
39037602
A
2016
2017 p = n;
2018 n = TAILQ_NEXT(n, dr_entry);
6d2010ae
A
2019 }
2020
39037602
A
2021 /* nothing has changed, just return */
2022 if (n == NULL && (scoped ||
f427ee49 2023 !(dr->stateflags & NDDRF_IFSCOPE)) && dummy_change == FALSE) {
0a7de745
A
2024 return dr;
2025 }
39037602 2026
6d2010ae
A
2027 /*
2028 * preferred router may be changed, so relocate
2029 * this router.
2030 * XXX: calling TAILQ_REMOVE directly is a bad manner.
2031 * However, since defrtrlist_del() has many side
2032 * effects, we intentionally do so here.
2033 * defrouter_select() below will handle routing
2034 * changes later.
2035 */
f427ee49 2036 TAILQ_REMOVE(nd_router_listp, dr, dr_entry);
6d2010ae 2037 new->stateflags = dr->stateflags;
6d2010ae 2038
6d2010ae
A
2039 n = dr;
2040 goto insert;
1c79356b 2041 }
1c79356b
A
2042 }
2043
6d2010ae 2044 VERIFY(dr == NULL);
f427ee49 2045 n = nddr_alloc(Z_WAITOK);
b0d623f7 2046
3e170ce0
A
2047 ndi = ND_IFINFO(ifp);
2048 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
316670eb 2049 lck_mtx_lock(&ndi->lock);
f427ee49 2050
e2fac8b1
A
2051 if (ip6_maxifdefrouters >= 0 &&
2052 ndi->ndefrouters >= ip6_maxifdefrouters) {
316670eb 2053 lck_mtx_unlock(&ndi->lock);
6d2010ae 2054 nddr_free(n);
f427ee49
A
2055 nd6log(error, "%s: ignoring router addition as we have hit the "
2056 "max limit of %d for max default routers.\n", __func__,
2057 ip6_maxifdefrouters);
0a7de745 2058 return NULL;
e2fac8b1 2059 }
6d2010ae 2060
0a7de745
A
2061 NDDR_ADDREF(n); /* for the nd_defrouter list */
2062 NDDR_ADDREF(n); /* for the caller */
6d2010ae
A
2063
2064 ++nd6_defrouter_genid;
316670eb
A
2065 ndi->ndefrouters++;
2066 VERIFY(ndi->ndefrouters != 0);
2067 lck_mtx_unlock(&ndi->lock);
e2fac8b1 2068
cb323159
A
2069 nd6log2(info, "%s: allocating defrouter %s\n", if_name(ifp),
2070 ip6_sprintf(&new->rtaddr));
6d2010ae 2071
39236c6e 2072 getmicrotime(&caltime);
6d2010ae 2073 NDDR_LOCK(n);
0a7de745 2074 memcpy(&n->rtaddr, &new->rtaddr, sizeof(n->rtaddr));
6d2010ae
A
2075 n->flags = new->flags;
2076 n->stateflags = new->stateflags;
6d2010ae
A
2077 n->rtlifetime = new->rtlifetime;
2078 n->expire = new->expire;
39236c6e
A
2079 n->base_calendartime = caltime.tv_sec;
2080 n->base_uptime = net_uptime();
6d2010ae 2081 n->ifp = new->ifp;
6d2010ae
A
2082 n->err = new->err;
2083 NDDR_UNLOCK(n);
2084insert:
39236c6e
A
2085 /* get nd6_service() to be scheduled as soon as it's convenient */
2086 ++nd6_sched_timeout_want;
1c79356b
A
2087
2088 /*
6d2010ae
A
2089 * Insert the new router in the Default Router List;
2090 * The Default Router List should be in the descending order
2091 * of router-preferece. When Scoped Routing is disabled, routers
2092 * with the same preference are sorted in the arriving time order;
2093 * otherwise, the first entry in the list of routers having the same
2094 * preference is the primary default router, when the interface used
2095 * by the entry is the default interface.
1c79356b 2096 */
b0d623f7 2097
6d2010ae 2098 /* insert at the end of the group */
f427ee49 2099 for (dr = TAILQ_FIRST(nd_router_listp); dr;
39236c6e 2100 dr = TAILQ_NEXT(dr, dr_entry)) {
6d2010ae 2101 if (rtpref(n) > rtpref(dr) ||
0a7de745 2102 (!scoped && rtpref(n) == rtpref(dr))) {
6d2010ae 2103 break;
0a7de745 2104 }
6d2010ae 2105 }
0a7de745 2106 if (dr) {
6d2010ae 2107 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
0a7de745 2108 } else {
f427ee49 2109 TAILQ_INSERT_TAIL(nd_router_listp, n, dr_entry);
0a7de745 2110 }
6d2010ae 2111
f427ee49 2112 defrouter_select(ifp, nd_router_listp);
6d2010ae 2113
0a7de745 2114 return n;
6d2010ae
A
2115}
2116
f427ee49
A
2117struct nd_defrouter *
2118defrtrlist_update(struct nd_defrouter *new, struct nd_drhead *nd_router_list)
6d2010ae
A
2119{
2120 struct nd_defrouter *dr;
2121
5ba3f43e 2122 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
f427ee49 2123 dr = defrtrlist_update_common(new, nd_router_list,
6d2010ae
A
2124 (nd6_defifp != NULL && new->ifp != nd6_defifp));
2125
0a7de745 2126 return dr;
6d2010ae
A
2127}
2128
9bccf70c 2129static struct nd_pfxrouter *
6d2010ae 2130pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
1c79356b
A
2131{
2132 struct nd_pfxrouter *search;
6d2010ae 2133
5ba3f43e 2134 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae
A
2135 NDPR_LOCK_ASSERT_HELD(pr);
2136
2137 for (search = pr->ndpr_advrtrs.lh_first; search;
2138 search = search->pfr_next) {
0a7de745 2139 if (search->router == dr) {
1c79356b 2140 break;
0a7de745 2141 }
1c79356b
A
2142 }
2143
0a7de745 2144 return search;
1c79356b
A
2145}
2146
2147static void
6d2010ae 2148pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
1c79356b
A
2149{
2150 struct nd_pfxrouter *new;
2151
5ba3f43e 2152 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae 2153 NDPR_LOCK_ASSERT_NOTHELD(pr);
91447636 2154
6d2010ae 2155 new = zalloc(ndprtr_zone);
0a7de745 2156 if (new == NULL) {
1c79356b 2157 return;
0a7de745
A
2158 }
2159 bzero(new, sizeof(*new));
1c79356b
A
2160 new->router = dr;
2161
6d2010ae 2162 NDPR_LOCK(pr);
1c79356b 2163 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
39236c6e 2164 pr->ndpr_genid++;
6d2010ae 2165 NDPR_UNLOCK(pr);
39236c6e 2166
6d2010ae 2167 pfxlist_onlink_check();
1c79356b
A
2168}
2169
2170static void
39236c6e 2171pfxrtr_del(struct nd_pfxrouter *pfr, struct nd_prefix *pr)
1c79356b 2172{
5ba3f43e 2173 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
39236c6e
A
2174 NDPR_LOCK_ASSERT_HELD(pr);
2175 pr->ndpr_genid++;
1c79356b 2176 LIST_REMOVE(pfr, pfr_entry);
6d2010ae 2177 zfree(ndprtr_zone, pfr);
1c79356b
A
2178}
2179
3e170ce0
A
2180/*
2181 * The routine has been modified to atomically refresh expiry
2182 * time for nd6 prefix as the part of lookup.
39037602 2183 * There's a corner case where a system going
3e170ce0
A
2184 * in sleep gets rid of manual addresses configured in the system
2185 * and then schedules the prefix for deletion.
2186 * However before the prefix gets deleted, if system comes out
2187 * from sleep and configures same address before prefix deletion
2188 * , the later prefix deletion will remove the prefix route and
2189 * the system will not be able to communicate with other IPv6
2190 * neighbor nodes in the same subnet.
2191 */
1c79356b 2192struct nd_prefix *
3e170ce0 2193nd6_prefix_lookup(struct nd_prefix *pr, int nd6_prefix_expiry)
1c79356b
A
2194{
2195 struct nd_prefix *search;
2196
91447636 2197 lck_mtx_lock(nd6_mutex);
1c79356b 2198 for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
6d2010ae 2199 NDPR_LOCK(search);
1c79356b
A
2200 if (pr->ndpr_ifp == search->ndpr_ifp &&
2201 pr->ndpr_plen == search->ndpr_plen &&
2202 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
6d2010ae 2203 &search->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
3e170ce0
A
2204 if (nd6_prefix_expiry != ND6_PREFIX_EXPIRY_UNSPEC) {
2205 search->ndpr_expire = nd6_prefix_expiry;
2206 }
f427ee49 2207 NDPR_ADDREF(search);
6d2010ae 2208 NDPR_UNLOCK(search);
1c79356b
A
2209 break;
2210 }
6d2010ae 2211 NDPR_UNLOCK(search);
1c79356b 2212 }
91447636 2213 lck_mtx_unlock(nd6_mutex);
1c79356b 2214
0a7de745 2215 return search;
1c79356b
A
2216}
2217
9bccf70c 2218int
6d2010ae
A
2219nd6_prelist_add(struct nd_prefix *pr, struct nd_defrouter *dr,
2220 struct nd_prefix **newp, boolean_t force_scoped)
1c79356b 2221{
9bccf70c 2222 struct nd_prefix *new = NULL;
b0d623f7
A
2223 struct ifnet *ifp = pr->ndpr_ifp;
2224 struct nd_ifinfo *ndi = NULL;
6d2010ae 2225 int i, error;
e2fac8b1
A
2226
2227 if (ip6_maxifprefixes >= 0) {
3e170ce0
A
2228 ndi = ND_IFINFO(ifp);
2229 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
316670eb 2230 lck_mtx_lock(&ndi->lock);
b0d623f7 2231 if (ndi->nprefixes >= ip6_maxifprefixes) {
316670eb 2232 lck_mtx_unlock(&ndi->lock);
0a7de745 2233 return ENOMEM;
b0d623f7 2234 }
316670eb 2235 lck_mtx_unlock(&ndi->lock);
e2fac8b1 2236 }
1c79356b 2237
6d2010ae 2238 new = ndpr_alloc(M_WAITOK);
0a7de745
A
2239 if (new == NULL) {
2240 return ENOMEM;
2241 }
1c79356b 2242
6d2010ae
A
2243 NDPR_LOCK(new);
2244 NDPR_LOCK(pr);
2245 new->ndpr_ifp = pr->ndpr_ifp;
2246 new->ndpr_prefix = pr->ndpr_prefix;
2247 new->ndpr_plen = pr->ndpr_plen;
2248 new->ndpr_vltime = pr->ndpr_vltime;
2249 new->ndpr_pltime = pr->ndpr_pltime;
2250 new->ndpr_flags = pr->ndpr_flags;
0a7de745 2251 if (pr->ndpr_stateflags & NDPRF_STATIC) {
6d2010ae 2252 new->ndpr_stateflags |= NDPRF_STATIC;
0a7de745 2253 }
6d2010ae
A
2254 NDPR_UNLOCK(pr);
2255 if ((error = in6_init_prefix_ltimes(new)) != 0) {
2256 NDPR_UNLOCK(new);
2257 ndpr_free(new);
0a7de745 2258 return error;
6d2010ae 2259 }
39236c6e 2260 new->ndpr_lastupdate = net_uptime();
6d2010ae
A
2261 if (newp != NULL) {
2262 *newp = new;
f427ee49 2263 NDPR_ADDREF(new); /* for caller */
6d2010ae
A
2264 }
2265 /* initialization */
1c79356b
A
2266 LIST_INIT(&new->ndpr_advrtrs);
2267 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
2268 /* make prefix in the canonical form */
0a7de745 2269 for (i = 0; i < 4; i++) {
1c79356b 2270 new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
0a7de745
A
2271 new->ndpr_mask.s6_addr32[i];
2272 }
1c79356b 2273
6d2010ae
A
2274 NDPR_UNLOCK(new);
2275
39236c6e
A
2276 /* get nd6_service() to be scheduled as soon as it's convenient */
2277 ++nd6_sched_timeout_want;
2278
91447636 2279 lck_mtx_lock(nd6_mutex);
6d2010ae 2280 /* link ndpr_entry to nd_prefix list */
1c79356b 2281 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
6d2010ae 2282 new->ndpr_debug |= IFD_ATTACHED;
0a7de745 2283 NDPR_ADDREF(new); /* for nd_prefix list */
2d21ac55 2284
39037602
A
2285 lck_mtx_lock(&ndi->lock);
2286 ndi->nprefixes++;
2287 VERIFY(ndi->nprefixes != 0);
2288 lck_mtx_unlock(&ndi->lock);
2289
9bccf70c
A
2290 /* ND_OPT_PI_FLAG_ONLINK processing */
2291 if (new->ndpr_raf_onlink) {
2292 int e;
2293
6d2010ae
A
2294 if ((e = nd6_prefix_onlink_common(new, force_scoped,
2295 new->ndpr_ifp->if_index)) != 0) {
cb323159 2296 nd6log(error, "nd6_prelist_add: failed to make "
6d2010ae
A
2297 "the prefix %s/%d on-link %s on %s (errno=%d)\n",
2298 ip6_sprintf(&new->ndpr_prefix.sin6_addr),
2299 new->ndpr_plen, force_scoped ? "scoped" :
cb323159 2300 "non-scoped", if_name(ifp), e);
9bccf70c
A
2301 /* proceed anyway. XXX: is it correct? */
2302 }
2303 }
2304
1c79356b
A
2305 if (dr) {
2306 pfxrtr_add(new, dr);
1c79356b 2307 }
e2fac8b1 2308
91447636 2309 lck_mtx_unlock(nd6_mutex);
1c79356b 2310
0a7de745 2311 return 0;
1c79356b
A
2312}
2313
6d2010ae
A
2314/*
2315 * Caller must have held an extra reference on nd_prefix.
2316 */
1c79356b 2317void
6d2010ae 2318prelist_remove(struct nd_prefix *pr)
1c79356b
A
2319{
2320 struct nd_pfxrouter *pfr, *next;
b0d623f7 2321 struct ifnet *ifp = pr->ndpr_ifp;
91447636 2322 int e;
3e170ce0 2323 struct nd_ifinfo *ndi = NULL;
1c79356b 2324
5ba3f43e 2325 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae
A
2326 NDPR_LOCK_ASSERT_HELD(pr);
2327
0a7de745 2328 if (pr->ndpr_stateflags & NDPRF_DEFUNCT) {
fe8ab488 2329 return;
0a7de745 2330 }
fe8ab488
A
2331
2332 /*
2333 * If there are no more addresses, defunct the prefix. This is needed
2334 * because we don't want multiple threads calling prelist_remove() for
2335 * the same prefix and this might happen because we unlock nd6_mutex
2336 * down below.
2337 */
0a7de745 2338 if (pr->ndpr_addrcnt == 0) {
fe8ab488 2339 pr->ndpr_stateflags |= NDPRF_DEFUNCT;
0a7de745 2340 }
fe8ab488 2341
9bccf70c
A
2342 /* make sure to invalidate the prefix until it is really freed. */
2343 pr->ndpr_vltime = 0;
2344 pr->ndpr_pltime = 0;
6d2010ae 2345
9bccf70c
A
2346 /*
2347 * Though these flags are now meaningless, we'd rather keep the value
6d2010ae
A
2348 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
2349 * when executing "ndp -p".
9bccf70c 2350 */
fe8ab488 2351 if (pr->ndpr_stateflags & NDPRF_ONLINK) {
f427ee49 2352 NDPR_ADDREF(pr);
6d2010ae
A
2353 NDPR_UNLOCK(pr);
2354 lck_mtx_unlock(nd6_mutex);
2355 if ((e = nd6_prefix_offlink(pr)) != 0) {
cb323159 2356 nd6log(error, "prelist_remove: failed to make "
6d2010ae
A
2357 "%s/%d offlink on %s, errno=%d\n",
2358 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
cb323159 2359 pr->ndpr_plen, if_name(ifp), e);
6d2010ae
A
2360 /* what should we do? */
2361 }
91447636 2362 lck_mtx_lock(nd6_mutex);
6d2010ae 2363 NDPR_LOCK(pr);
f427ee49 2364 if (NDPR_REMREF(pr) == NULL) {
6d2010ae 2365 return;
0a7de745 2366 }
6d2010ae 2367 }
2d21ac55 2368
fe8ab488
A
2369 if (pr->ndpr_addrcnt > 0) {
2370 /*
2371 * The state might have changed if we called
2372 * nd6_prefix_offlink().
2373 */
2374 pr->ndpr_stateflags &= ~NDPRF_DEFUNCT;
2375 return; /* notice here? */
2376 }
2d21ac55 2377
1c79356b
A
2378 /* unlink ndpr_entry from nd_prefix list */
2379 LIST_REMOVE(pr, ndpr_entry);
6d2010ae 2380 pr->ndpr_debug &= ~IFD_ATTACHED;
1c79356b
A
2381
2382 /* free list of routers that adversed the prefix */
2383 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
2384 next = pfr->pfr_next;
39236c6e 2385 pfxrtr_del(pfr, pr);
1c79356b 2386 }
9bccf70c 2387
3e170ce0
A
2388 ndi = ND_IFINFO(ifp);
2389 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
2390 lck_mtx_lock(&ndi->lock);
2391 VERIFY(ndi->nprefixes > 0);
2392 ndi->nprefixes--;
2393 lck_mtx_unlock(&ndi->lock);
e2fac8b1 2394
6d2010ae 2395 /* This must not be the last reference to the nd_prefix */
f427ee49 2396 if (NDPR_REMREF(pr) == NULL) {
6d2010ae
A
2397 panic("%s: unexpected (missing) refcnt ndpr=%p", __func__, pr);
2398 /* NOTREACHED */
2399 }
1c79356b 2400
39236c6e
A
2401 /*
2402 * Don't call pfxlist_onlink_check() here because we are
2403 * holding the NDPR lock and this could cause a deadlock when
2404 * there are multiple threads executing pfxlist_onlink_check().
2405 */
1c79356b
A
2406}
2407
1c79356b 2408int
91447636
A
2409prelist_update(
2410 struct nd_prefix *new,
2411 struct nd_defrouter *dr, /* may be NULL */
6d2010ae
A
2412 struct mbuf *m,
2413 int mcast)
1c79356b 2414{
9bccf70c
A
2415 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
2416 struct ifaddr *ifa;
2417 struct ifnet *ifp = new->ndpr_ifp;
1c79356b 2418 struct nd_prefix *pr;
1c79356b 2419 int error = 0;
9bccf70c 2420 int newprefix = 0;
1c79356b 2421 int auth;
9bccf70c 2422 struct in6_addrlifetime lt6_tmp;
39236c6e 2423 uint64_t timenow = net_uptime();
1c79356b 2424
6d2010ae
A
2425 /* no need to lock "new" here, as it is local to the caller */
2426 NDPR_LOCK_ASSERT_NOTHELD(new);
2427
1c79356b
A
2428 auth = 0;
2429 if (m) {
2430 /*
2431 * Authenticity for NA consists authentication for
2432 * both IP header and IP datagrams, doesn't it ?
2433 */
2434#if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
39236c6e 2435 auth = (m->m_flags & M_AUTHIPHDR) && (m->m_flags & M_AUTHIPDGM);
1c79356b
A
2436#endif
2437 }
2438
3e170ce0 2439 if ((pr = nd6_prefix_lookup(new, ND6_PREFIX_EXPIRY_UNSPEC)) != NULL) {
9bccf70c
A
2440 /*
2441 * nd6_prefix_lookup() ensures that pr and new have the same
2442 * prefix on a same interface.
2443 */
2444
2445 /*
2446 * Update prefix information. Note that the on-link (L) bit
2447 * and the autonomous (A) bit should NOT be changed from 1
2448 * to 0.
2449 */
6d2010ae
A
2450 lck_mtx_lock(nd6_mutex);
2451 NDPR_LOCK(pr);
0a7de745 2452 if (new->ndpr_raf_onlink == 1) {
9bccf70c 2453 pr->ndpr_raf_onlink = 1;
0a7de745
A
2454 }
2455 if (new->ndpr_raf_auto == 1) {
9bccf70c 2456 pr->ndpr_raf_auto = 1;
0a7de745 2457 }
9bccf70c
A
2458 if (new->ndpr_raf_onlink) {
2459 pr->ndpr_vltime = new->ndpr_vltime;
2460 pr->ndpr_pltime = new->ndpr_pltime;
39236c6e
A
2461 (void) in6_init_prefix_ltimes(pr); /* XXX error case? */
2462 pr->ndpr_lastupdate = net_uptime();
1c79356b
A
2463 }
2464
f427ee49 2465 NDPR_ADDREF(pr);
9bccf70c
A
2466 if (new->ndpr_raf_onlink &&
2467 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
2468 int e;
2469
6d2010ae
A
2470 NDPR_UNLOCK(pr);
2471 if ((e = nd6_prefix_onlink(pr)) != 0) {
cb323159 2472 nd6log(error,
9bccf70c
A
2473 "prelist_update: failed to make "
2474 "the prefix %s/%d on-link on %s "
2475 "(errno=%d)\n",
2476 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
cb323159 2477 pr->ndpr_plen, if_name(pr->ndpr_ifp), e);
9bccf70c
A
2478 /* proceed anyway. XXX: is it correct? */
2479 }
6d2010ae 2480 NDPR_LOCK(pr);
1c79356b 2481 }
6d2010ae
A
2482
2483 if (dr && pfxrtr_lookup(pr, dr) == NULL) {
2484 NDPR_UNLOCK(pr);
9bccf70c 2485 pfxrtr_add(pr, dr);
6d2010ae
A
2486 } else {
2487 NDPR_UNLOCK(pr);
2488 }
fe8ab488 2489 NDPR_REMREF(pr);
91447636 2490 lck_mtx_unlock(nd6_mutex);
9bccf70c 2491 } else {
9bccf70c
A
2492 newprefix = 1;
2493
0a7de745 2494 if (new->ndpr_vltime == 0) {
9bccf70c 2495 goto end;
0a7de745
A
2496 }
2497 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0) {
9bccf70c 2498 goto end;
0a7de745 2499 }
9bccf70c 2500
0a7de745 2501 bzero(&new->ndpr_addr, sizeof(struct in6_addr));
9bccf70c 2502
d190cdc3
A
2503 error = nd6_prelist_add(new, dr, &pr, FALSE);
2504 if (error != 0 || pr == NULL) {
cb323159 2505 nd6log(info, "prelist_update: "
9bccf70c 2506 "nd6_prelist_add failed for %s/%d on %s "
39236c6e 2507 "errno=%d, returnpr=0x%llx\n",
9bccf70c 2508 ip6_sprintf(&new->ndpr_prefix.sin6_addr),
39236c6e 2509 new->ndpr_plen, if_name(new->ndpr_ifp),
cb323159 2510 error, (uint64_t)VM_KERNEL_ADDRPERM(pr));
9bccf70c
A
2511 goto end; /* we should just give up in this case. */
2512 }
9bccf70c 2513 }
1c79356b 2514
9bccf70c 2515 /*
39236c6e 2516 * Address autoconfiguration based on Section 5.5.3 of RFC 4862.
9bccf70c
A
2517 * Note that pr must be non NULL at this point.
2518 */
1c79356b 2519
9bccf70c 2520 /* 5.5.3 (a). Ignore the prefix without the A bit set. */
0a7de745 2521 if (!new->ndpr_raf_auto) {
d190cdc3 2522 goto end;
0a7de745 2523 }
1c79356b 2524
9bccf70c
A
2525 /*
2526 * 5.5.3 (b). the link-local prefix should have been ignored in
2527 * nd6_ra_input.
2528 */
1c79356b 2529
6d2010ae
A
2530 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
2531 if (new->ndpr_pltime > new->ndpr_vltime) {
0a7de745 2532 error = EINVAL; /* XXX: won't be used */
6d2010ae
A
2533 goto end;
2534 }
2535
9bccf70c 2536 /*
6d2010ae
A
2537 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of
2538 * an address configured by stateless autoconfiguration already in the
2539 * list of addresses associated with the interface, and the Valid
2540 * Lifetime is not 0, form an address. We first check if we have
2541 * a matching prefix.
9bccf70c 2542 */
6d2010ae 2543 ifnet_lock_shared(ifp);
39236c6e 2544 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
9bccf70c 2545 struct in6_ifaddr *ifa6;
6d2010ae 2546 u_int32_t remaininglifetime;
1c79356b 2547
6d2010ae
A
2548 IFA_LOCK(ifa);
2549 if (ifa->ifa_addr->sa_family != AF_INET6) {
2550 IFA_UNLOCK(ifa);
9bccf70c 2551 continue;
6d2010ae 2552 }
9bccf70c 2553 ifa6 = (struct in6_ifaddr *)ifa;
1c79356b 2554
6d2010ae 2555 /*
39236c6e 2556 * We only consider autoconfigured addresses as per RFC 4862.
6d2010ae
A
2557 */
2558 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF)) {
2559 IFA_UNLOCK(ifa);
2560 continue;
2561 }
9bccf70c
A
2562 /*
2563 * Spec is not clear here, but I believe we should concentrate
2564 * on unicast (i.e. not anycast) addresses.
2565 * XXX: other ia6_flags? detached or duplicated?
2566 */
6d2010ae
A
2567 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0) {
2568 IFA_UNLOCK(ifa);
9bccf70c 2569 continue;
6d2010ae
A
2570 }
2571 /*
2572 * Ignore the address if it is not associated with a prefix
2573 * or is associated with a prefix that is different from this
2574 * one. (pr is never NULL here)
2575 */
2576 if (ifa6->ia6_ndpr != pr) {
2577 IFA_UNLOCK(ifa);
9bccf70c 2578 continue;
6d2010ae 2579 }
1c79356b 2580
6d2010ae 2581 if (ia6_match == NULL) { /* remember the first one */
9bccf70c 2582 ia6_match = ifa6;
0a7de745 2583 IFA_ADDREF_LOCKED(ifa); /* for ia6_match */
6d2010ae 2584 }
1c79356b 2585
9bccf70c
A
2586 /*
2587 * An already autoconfigured address matched. Now that we
2588 * are sure there is at least one matched address, we can
2589 * proceed to 5.5.3. (e): update the lifetimes according to the
2590 * "two hours" rule and the privacy extension.
2591 */
39236c6e
A
2592 /* retrieve time as uptime (last arg is 0) */
2593 in6ifa_getlifetime(ifa6, &lt6_tmp, 0);
9bccf70c 2594
0a7de745 2595 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME) {
6d2010ae 2596 remaininglifetime = ND6_INFINITE_LIFETIME;
0a7de745 2597 } else if (timenow - ifa6->ia6_updatetime > lt6_tmp.ia6t_vltime) {
6d2010ae
A
2598 /*
2599 * The case of "invalid" address. We should usually
2600 * not see this case.
2601 */
2602 remaininglifetime = 0;
39236c6e 2603 } else {
6d2010ae 2604 remaininglifetime = lt6_tmp.ia6t_vltime -
f427ee49 2605 (uint32_t)(timenow - ifa6->ia6_updatetime);
39236c6e 2606 }
6d2010ae
A
2607 /* when not updating, keep the current stored lifetime. */
2608 lt6_tmp.ia6t_vltime = remaininglifetime;
1c79356b 2609
9bccf70c 2610 if (TWOHOUR < new->ndpr_vltime ||
6d2010ae 2611 remaininglifetime < new->ndpr_vltime) {
9bccf70c 2612 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
6d2010ae 2613 } else if (remaininglifetime <= TWOHOUR) {
9bccf70c
A
2614 if (auth) {
2615 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
2616 }
2617 } else {
1c79356b 2618 /*
9bccf70c 2619 * new->ndpr_vltime <= TWOHOUR &&
6d2010ae 2620 * TWOHOUR < remaininglifetime
1c79356b 2621 */
9bccf70c 2622 lt6_tmp.ia6t_vltime = TWOHOUR;
1c79356b 2623 }
1c79356b 2624
9bccf70c
A
2625 /* The 2 hour rule is not imposed for preferred lifetime. */
2626 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
1c79356b 2627
6d2010ae 2628 /* Special handling for lifetimes of temporary addresses. */
9bccf70c 2629 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
6d2010ae 2630 u_int32_t maxvltime, maxpltime;
39236c6e 2631
6d2010ae 2632 /* Constrain lifetimes to system limits. */
0a7de745 2633 if (lt6_tmp.ia6t_vltime > ip6_temp_valid_lifetime) {
6d2010ae 2634 lt6_tmp.ia6t_vltime = ip6_temp_valid_lifetime;
0a7de745
A
2635 }
2636 if (lt6_tmp.ia6t_pltime > ip6_temp_preferred_lifetime) {
6d2010ae
A
2637 lt6_tmp.ia6t_pltime =
2638 ip6_temp_preferred_lifetime -
2639 ip6_desync_factor;
0a7de745 2640 }
6d2010ae
A
2641
2642 /*
2643 * According to RFC 4941, section 3.3 (1), we only
2644 * update the lifetimes when they are in the maximum
2645 * intervals.
2646 */
2647 if (ip6_temp_valid_lifetime >
39236c6e 2648 (u_int32_t)((timenow - ifa6->ia6_createtime) +
6d2010ae
A
2649 ip6_desync_factor)) {
2650 maxvltime = ip6_temp_valid_lifetime -
f427ee49
A
2651 (uint32_t)((timenow - ifa6->ia6_createtime) +
2652 ip6_desync_factor);
0a7de745 2653 } else {
6d2010ae 2654 maxvltime = 0;
0a7de745 2655 }
6d2010ae 2656 if (ip6_temp_preferred_lifetime >
39236c6e 2657 (u_int32_t)((timenow - ifa6->ia6_createtime) +
6d2010ae
A
2658 ip6_desync_factor)) {
2659 maxpltime = ip6_temp_preferred_lifetime -
f427ee49
A
2660 (uint32_t)((timenow - ifa6->ia6_createtime) +
2661 ip6_desync_factor);
0a7de745 2662 } else {
6d2010ae 2663 maxpltime = 0;
0a7de745 2664 }
6d2010ae 2665
39236c6e 2666 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
0a7de745 2667 lt6_tmp.ia6t_vltime > maxvltime) {
6d2010ae 2668 lt6_tmp.ia6t_vltime = maxvltime;
0a7de745 2669 }
39236c6e
A
2670
2671 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
0a7de745 2672 lt6_tmp.ia6t_pltime > maxpltime) {
6d2010ae 2673 lt6_tmp.ia6t_pltime = maxpltime;
0a7de745 2674 }
1c79356b 2675 }
1c79356b 2676
39236c6e
A
2677 in6_init_address_ltimes(pr, &lt6_tmp);
2678
2679 in6ifa_setlifetime(ifa6, &lt6_tmp);
2680 ifa6->ia6_updatetime = timenow;
6d2010ae 2681 IFA_UNLOCK(ifa);
9bccf70c 2682 }
91447636 2683 ifnet_lock_done(ifp);
9bccf70c
A
2684 if (ia6_match == NULL && new->ndpr_vltime) {
2685 /*
6d2010ae 2686 * 5.5.3 (d) (continued)
9bccf70c
A
2687 * No address matched and the valid lifetime is non-zero.
2688 * Create a new address.
2689 */
f427ee49 2690 if ((ia6 = in6_pfx_newpersistaddr(new, mcast, &error, FALSE, 0))
39236c6e 2691 != NULL) {
9bccf70c
A
2692 /*
2693 * note that we should use pr (not new) for reference.
2694 */
6d2010ae
A
2695 IFA_LOCK(&ia6->ia_ifa);
2696 NDPR_LOCK(pr);
9bccf70c 2697 ia6->ia6_ndpr = pr;
f427ee49 2698 NDPR_ADDREF(pr); /* for addr reference */
6d2010ae
A
2699 pr->ndpr_addrcnt++;
2700 VERIFY(pr->ndpr_addrcnt != 0);
2701 NDPR_UNLOCK(pr);
2702 IFA_UNLOCK(&ia6->ia_ifa);
1c79356b 2703
9bccf70c 2704 /*
6d2010ae 2705 * RFC 4941 3.3 (2).
9bccf70c 2706 * When a new public address is created as described
39236c6e 2707 * in RFC 4862, also create a new temporary address.
9bccf70c 2708 *
6d2010ae 2709 * RFC 4941 3.5.
9bccf70c
A
2710 * When an interface connects to a new link, a new
2711 * randomized interface identifier should be generated
2712 * immediately together with a new set of temporary
2713 * addresses. Thus, we specifiy 1 as the 2nd arg of
2714 * in6_tmpifadd().
2715 */
2716 if (ip6_use_tempaddr) {
2717 int e;
39236c6e 2718 if ((e = in6_tmpifadd(ia6, 1)) != 0) {
cb323159 2719 nd6log(info, "prelist_update: "
9bccf70c
A
2720 "failed to create a temporary "
2721 "address, errno=%d\n",
cb323159 2722 e);
9bccf70c
A
2723 }
2724 }
6d2010ae 2725 IFA_REMREF(&ia6->ia_ifa);
b0d623f7 2726 ia6 = NULL;
1c79356b 2727
d9a64523
A
2728 /*
2729 * If the interface is marked for CLAT46 configuration
2730 * try and configure the reserved IPv6 address for
2731 * stateless translation.
2732 */
2733 if (IS_INTF_CLAT46(ifp)) {
f427ee49
A
2734 if ((ia6 = in6_pfx_newpersistaddr(new, mcast,
2735 &error, TRUE, CLAT46_COLLISION_COUNT_OFFSET))
2736 != NULL) {
d9a64523
A
2737 IFA_LOCK(&ia6->ia_ifa);
2738 NDPR_LOCK(pr);
2739 ia6->ia6_ndpr = pr;
f427ee49 2740 NDPR_ADDREF(pr); /* for addr reference */
d9a64523
A
2741 pr->ndpr_addrcnt++;
2742 VERIFY(pr->ndpr_addrcnt != 0);
2743 pr->ndpr_stateflags |= NDPRF_CLAT46;
2744 NDPR_UNLOCK(pr);
2745 IFA_UNLOCK(&ia6->ia_ifa);
2746 IFA_REMREF(&ia6->ia_ifa);
2747 ia6 = NULL;
2748 } else if (error != EEXIST) {
2749 uuid_t tmp_uuid = {};
2750 /*
2751 * Only report the error if it is not
2752 * EEXIST.
2753 */
2754 ip6stat.ip6s_clat464_v6addr_conffail++;
2755 in6_clat46_event_enqueue_nwk_wq_entry(
0a7de745
A
2756 IN6_CLAT46_EVENT_V6_ADDR_CONFFAIL,
2757 0,
2758 tmp_uuid);
cb323159 2759 nd6log0(error, "Could not configure CLAT46 address on interface %s.\n", ifp->if_xname);
d9a64523
A
2760 }
2761 /*
2762 * Reset the error as we do not want to
2763 * treat failure of CLAT46 address configuration
2764 * as complete failure in prelist update path.
2765 */
2766 error = 0;
2767 }
2768
6d2010ae
A
2769 /*
2770 * A newly added address might affect the status
2771 * of other addresses, so we check and update it.
2772 * XXX: what if address duplication happens?
2773 */
2774 lck_mtx_lock(nd6_mutex);
2775 pfxlist_onlink_check();
2776 lck_mtx_unlock(nd6_mutex);
6d2010ae
A
2777 }
2778 }
6d2010ae 2779end:
0a7de745 2780 if (pr != NULL) {
6d2010ae 2781 NDPR_REMREF(pr);
0a7de745
A
2782 }
2783 if (ia6_match != NULL) {
6d2010ae 2784 IFA_REMREF(&ia6_match->ia_ifa);
0a7de745
A
2785 }
2786 return error;
6d2010ae
A
2787}
2788
2789/*
2790 * Neighbor Discover Default Router structure reference counting routines.
2791 */
2792static struct nd_defrouter *
f427ee49 2793nddr_alloc(zalloc_flags_t how)
6d2010ae
A
2794{
2795 struct nd_defrouter *dr;
2796
f427ee49
A
2797 dr = zalloc_flags(nddr_zone, how | Z_ZERO);
2798 if (dr) {
6d2010ae 2799 lck_mtx_init(&dr->nddr_lock, ifa_mtx_grp, ifa_mtx_attr);
f427ee49 2800 lck_mtx_init(&dr->nddr_ref_lock, ifa_mtx_grp, ifa_mtx_attr);
6d2010ae
A
2801 dr->nddr_debug |= IFD_ALLOC;
2802 if (nddr_debug != 0) {
2803 dr->nddr_debug |= IFD_DEBUG;
2804 dr->nddr_trace = nddr_trace;
2805 }
2806 }
0a7de745 2807 return dr;
6d2010ae
A
2808}
2809
2810static void
2811nddr_free(struct nd_defrouter *dr)
2812{
6d2010ae
A
2813 if (dr->nddr_debug & IFD_ATTACHED) {
2814 panic("%s: attached nddr %p is being freed", __func__, dr);
2815 /* NOTREACHED */
2816 } else if (!(dr->nddr_debug & IFD_ALLOC)) {
2817 panic("%s: nddr %p cannot be freed", __func__, dr);
2818 /* NOTREACHED */
2819 }
2820 dr->nddr_debug &= ~IFD_ALLOC;
6d2010ae 2821 lck_mtx_destroy(&dr->nddr_lock, ifa_mtx_grp);
f427ee49 2822 lck_mtx_destroy(&dr->nddr_ref_lock, ifa_mtx_grp);
6d2010ae
A
2823 zfree(nddr_zone, dr);
2824}
2825
2826static void
2827nddr_trace(struct nd_defrouter *dr, int refhold)
2828{
2829 struct nd_defrouter_dbg *dr_dbg = (struct nd_defrouter_dbg *)dr;
2830 ctrace_t *tr;
2831 uint32_t idx;
2832 uint16_t *cnt;
2833
2834 if (!(dr->nddr_debug & IFD_DEBUG)) {
2835 panic("%s: nddr %p has no debug structure", __func__, dr);
2836 /* NOTREACHED */
2837 }
2838 if (refhold) {
2839 cnt = &dr_dbg->nddr_refhold_cnt;
2840 tr = dr_dbg->nddr_refhold;
2841 } else {
2842 cnt = &dr_dbg->nddr_refrele_cnt;
2843 tr = dr_dbg->nddr_refrele;
2844 }
2845
2846 idx = atomic_add_16_ov(cnt, 1) % NDDR_TRACE_HIST_SIZE;
2847 ctrace_record(&tr[idx]);
2848}
2849
2850void
f427ee49 2851nddr_addref(struct nd_defrouter *nddr)
6d2010ae 2852{
f427ee49 2853 NDDR_REF_LOCK_SPIN(nddr);
6d2010ae
A
2854 if (++nddr->nddr_refcount == 0) {
2855 panic("%s: nddr %p wraparound refcnt\n", __func__, nddr);
2856 /* NOTREACHED */
2857 } else if (nddr->nddr_trace != NULL) {
2858 (*nddr->nddr_trace)(nddr, TRUE);
2859 }
f427ee49 2860 NDDR_REF_UNLOCK(nddr);
6d2010ae
A
2861}
2862
2863struct nd_defrouter *
f427ee49 2864nddr_remref(struct nd_defrouter *nddr)
6d2010ae 2865{
f427ee49 2866 NDDR_REF_LOCK_SPIN(nddr);
6d2010ae
A
2867 if (nddr->nddr_refcount == 0) {
2868 panic("%s: nddr %p negative refcnt\n", __func__, nddr);
2869 /* NOTREACHED */
2870 } else if (nddr->nddr_trace != NULL) {
2871 (*nddr->nddr_trace)(nddr, FALSE);
2872 }
2873
2874 if (--nddr->nddr_refcount == 0) {
f427ee49 2875 NDDR_REF_UNLOCK(nddr);
6d2010ae
A
2876 nddr_free(nddr);
2877 nddr = NULL;
f427ee49
A
2878 } else {
2879 NDDR_REF_UNLOCK(nddr);
6d2010ae 2880 }
0a7de745 2881 return nddr;
6d2010ae
A
2882}
2883
39236c6e
A
2884uint64_t
2885nddr_getexpire(struct nd_defrouter *dr)
2886{
2887 struct timeval caltime;
2888 uint64_t expiry;
2889
2890 if (dr->expire != 0) {
2891 /* account for system time change */
2892 getmicrotime(&caltime);
2893
2894 dr->base_calendartime +=
2895 NET_CALCULATE_CLOCKSKEW(caltime,
2896 dr->base_calendartime, net_uptime(), dr->base_uptime);
2897
2898 expiry = dr->base_calendartime +
2899 dr->expire - dr->base_uptime;
2900 } else {
2901 expiry = 0;
2902 }
0a7de745 2903 return expiry;
39236c6e
A
2904}
2905
6d2010ae
A
2906/*
2907 * Neighbor Discover Prefix structure reference counting routines.
2908 */
2909static struct nd_prefix *
2910ndpr_alloc(int how)
2911{
2912 struct nd_prefix *pr;
2913
f427ee49 2914 pr = zalloc_flags(ndpr_zone, how | Z_ZERO);
6d2010ae 2915 if (pr != NULL) {
6d2010ae 2916 lck_mtx_init(&pr->ndpr_lock, ifa_mtx_grp, ifa_mtx_attr);
f427ee49 2917 lck_mtx_init(&pr->ndpr_ref_lock, ifa_mtx_grp, ifa_mtx_attr);
316670eb 2918 RB_INIT(&pr->ndpr_prproxy_sols);
6d2010ae
A
2919 pr->ndpr_debug |= IFD_ALLOC;
2920 if (ndpr_debug != 0) {
2921 pr->ndpr_debug |= IFD_DEBUG;
2922 pr->ndpr_trace = ndpr_trace;
2923 }
2924 }
0a7de745 2925 return pr;
6d2010ae
A
2926}
2927
2928static void
2929ndpr_free(struct nd_prefix *pr)
2930{
6d2010ae
A
2931 if (pr->ndpr_debug & IFD_ATTACHED) {
2932 panic("%s: attached ndpr %p is being freed", __func__, pr);
2933 /* NOTREACHED */
2934 } else if (!(pr->ndpr_debug & IFD_ALLOC)) {
2935 panic("%s: ndpr %p cannot be freed", __func__, pr);
2936 /* NOTREACHED */
316670eb
A
2937 } else if (pr->ndpr_rt != NULL) {
2938 panic("%s: ndpr %p route %p not freed", __func__, pr,
2939 pr->ndpr_rt);
2940 /* NOTREACHED */
2941 } else if (pr->ndpr_prproxy_sols_cnt != 0) {
2942 panic("%s: ndpr %p non-zero solicitors count (%d)",
2943 __func__, pr, pr->ndpr_prproxy_sols_cnt);
2944 /* NOTREACHED */
2945 } else if (!RB_EMPTY(&pr->ndpr_prproxy_sols)) {
2946 panic("%s: ndpr %p non-empty solicitors tree", __func__, pr);
2947 /* NOTREACHED */
6d2010ae
A
2948 }
2949 pr->ndpr_debug &= ~IFD_ALLOC;
6d2010ae 2950 lck_mtx_destroy(&pr->ndpr_lock, ifa_mtx_grp);
f427ee49 2951 lck_mtx_destroy(&pr->ndpr_ref_lock, ifa_mtx_grp);
6d2010ae
A
2952 zfree(ndpr_zone, pr);
2953}
2954
2955static void
2956ndpr_trace(struct nd_prefix *pr, int refhold)
2957{
2958 struct nd_prefix_dbg *pr_dbg = (struct nd_prefix_dbg *)pr;
2959 ctrace_t *tr;
2960 u_int32_t idx;
2961 u_int16_t *cnt;
2962
2963 if (!(pr->ndpr_debug & IFD_DEBUG)) {
2964 panic("%s: ndpr %p has no debug structure", __func__, pr);
2965 /* NOTREACHED */
2966 }
2967 if (refhold) {
2968 cnt = &pr_dbg->ndpr_refhold_cnt;
2969 tr = pr_dbg->ndpr_refhold;
2970 } else {
2971 cnt = &pr_dbg->ndpr_refrele_cnt;
2972 tr = pr_dbg->ndpr_refrele;
2973 }
2974
2975 idx = atomic_add_16_ov(cnt, 1) % NDPR_TRACE_HIST_SIZE;
2976 ctrace_record(&tr[idx]);
2977}
2978
2979void
f427ee49 2980ndpr_addref(struct nd_prefix *ndpr)
6d2010ae 2981{
f427ee49 2982 NDPR_REF_LOCK_SPIN(ndpr);
6d2010ae
A
2983 if (++ndpr->ndpr_refcount == 0) {
2984 panic("%s: ndpr %p wraparound refcnt\n", __func__, ndpr);
2985 /* NOTREACHED */
2986 } else if (ndpr->ndpr_trace != NULL) {
2987 (*ndpr->ndpr_trace)(ndpr, TRUE);
2988 }
f427ee49 2989 NDPR_REF_UNLOCK(ndpr);
6d2010ae
A
2990}
2991
2992struct nd_prefix *
f427ee49 2993ndpr_remref(struct nd_prefix *ndpr)
6d2010ae 2994{
f427ee49 2995 NDPR_REF_LOCK_SPIN(ndpr);
6d2010ae
A
2996 if (ndpr->ndpr_refcount == 0) {
2997 panic("%s: ndpr %p negative refcnt\n", __func__, ndpr);
2998 /* NOTREACHED */
2999 } else if (ndpr->ndpr_trace != NULL) {
3000 (*ndpr->ndpr_trace)(ndpr, FALSE);
1c79356b
A
3001 }
3002
6d2010ae
A
3003 if (--ndpr->ndpr_refcount == 0) {
3004 if (ndpr->ndpr_addrcnt != 0) {
3005 panic("%s: freeing ndpr %p with outstanding address "
3006 "reference (%d)", __func__, ndpr,
3007 ndpr->ndpr_addrcnt);
3008 /* NOTREACHED */
3009 }
f427ee49 3010 NDPR_REF_UNLOCK(ndpr);
6d2010ae
A
3011 ndpr_free(ndpr);
3012 ndpr = NULL;
f427ee49
A
3013 } else {
3014 NDPR_REF_UNLOCK(ndpr);
6d2010ae 3015 }
0a7de745 3016 return ndpr;
1c79356b
A
3017}
3018
39236c6e
A
3019uint64_t
3020ndpr_getexpire(struct nd_prefix *pr)
3021{
3022 struct timeval caltime;
3023 uint64_t expiry;
3024
3025 if (pr->ndpr_expire != 0 && pr->ndpr_vltime != ND6_INFINITE_LIFETIME) {
3026 /* account for system time change */
3027 getmicrotime(&caltime);
3028
3029 pr->ndpr_base_calendartime +=
3030 NET_CALCULATE_CLOCKSKEW(caltime,
3031 pr->ndpr_base_calendartime, net_uptime(),
3032 pr->ndpr_base_uptime);
3033
3034 expiry = pr->ndpr_base_calendartime +
3035 pr->ndpr_expire - pr->ndpr_base_uptime;
3036 } else {
3037 expiry = 0;
3038 }
0a7de745 3039 return expiry;
39236c6e
A
3040}
3041
1c79356b
A
3042/*
3043 * A supplement function used in the on-link detection below;
3044 * detect if a given prefix has a (probably) reachable advertising router.
3045 * XXX: lengthy function name...
fe8ab488
A
3046 *
3047 * Callers *must* increase the reference count of nd_prefix.
1c79356b 3048 */
9bccf70c 3049static struct nd_pfxrouter *
6d2010ae 3050find_pfxlist_reachable_router(struct nd_prefix *pr)
1c79356b
A
3051{
3052 struct nd_pfxrouter *pfxrtr;
3053 struct rtentry *rt;
3054 struct llinfo_nd6 *ln;
39236c6e
A
3055 struct ifnet *ifp;
3056 struct in6_addr rtaddr;
3057 unsigned int genid;
1c79356b 3058
5ba3f43e 3059 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae 3060 NDPR_LOCK_ASSERT_HELD(pr);
91447636 3061
39236c6e
A
3062 genid = pr->ndpr_genid;
3063 pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs);
3064 while (pfxrtr) {
3065 ifp = pfxrtr->router->ifp;
0a7de745 3066 if (pfxrtr->router->stateflags & NDDRF_MAPPED) {
39037602 3067 rtaddr = pfxrtr->router->rtaddr_mapped;
0a7de745 3068 } else {
39037602 3069 rtaddr = pfxrtr->router->rtaddr;
0a7de745 3070 }
39037602 3071
6d2010ae
A
3072 NDPR_UNLOCK(pr);
3073 lck_mtx_unlock(nd6_mutex);
b0d623f7 3074 /* Callee returns a locked route upon success */
39236c6e 3075 if ((rt = nd6_lookup(&rtaddr, 0, ifp, 0)) != NULL) {
b0d623f7
A
3076 RT_LOCK_ASSERT_HELD(rt);
3077 if ((ln = rt->rt_llinfo) != NULL &&
3078 ND6_IS_LLINFO_PROBREACH(ln)) {
3079 RT_REMREF_LOCKED(rt);
3080 RT_UNLOCK(rt);
6d2010ae
A
3081 lck_mtx_lock(nd6_mutex);
3082 NDPR_LOCK(pr);
0a7de745 3083 break; /* found */
b0d623f7
A
3084 }
3085 RT_REMREF_LOCKED(rt);
3086 RT_UNLOCK(rt);
3087 }
6d2010ae
A
3088 lck_mtx_lock(nd6_mutex);
3089 NDPR_LOCK(pr);
39236c6e
A
3090 if (pr->ndpr_genid != genid) {
3091 pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs);
3092 genid = pr->ndpr_genid;
0a7de745 3093 } else {
39236c6e 3094 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry);
0a7de745 3095 }
1c79356b 3096 }
6d2010ae 3097 NDPR_LOCK_ASSERT_HELD(pr);
1c79356b 3098
0a7de745 3099 return pfxrtr;
1c79356b
A
3100}
3101
3102/*
3103 * Check if each prefix in the prefix list has at least one available router
9bccf70c
A
3104 * that advertised the prefix (a router is "available" if its neighbor cache
3105 * entry is reachable or probably reachable).
1c79356b
A
3106 * If the check fails, the prefix may be off-link, because, for example,
3107 * we have moved from the network but the lifetime of the prefix has not
9bccf70c
A
3108 * expired yet. So we should not use the prefix if there is another prefix
3109 * that has an available router.
3110 * But, if there is no prefix that has an available router, we still regards
3111 * all the prefixes as on-link. This is because we can't tell if all the
1c79356b
A
3112 * routers are simply dead or if we really moved from the network and there
3113 * is no router around us.
3114 */
3115void
6d2010ae 3116pfxlist_onlink_check(void)
1c79356b 3117{
6d2010ae 3118 struct nd_prefix *pr, *prclear;
9bccf70c 3119 struct in6_ifaddr *ifa;
6d2010ae
A
3120 struct nd_defrouter *dr;
3121 struct nd_pfxrouter *pfxrtr = NULL;
316670eb
A
3122 int err, i, found = 0;
3123 struct ifaddr **ifap = NULL;
3124 struct nd_prefix *ndpr;
6d2010ae 3125
5ba3f43e 3126 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae
A
3127
3128 while (nd_prefix_busy) {
3129 nd_prefix_waiters++;
0a7de745 3130 msleep(nd_prefix_waitchan, nd6_mutex, (PZERO - 1),
6d2010ae 3131 __func__, NULL);
5ba3f43e 3132 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae
A
3133 }
3134 nd_prefix_busy = TRUE;
1c79356b
A
3135
3136 /*
3137 * Check if there is a prefix that has a reachable advertising
3138 * router.
3139 */
6d2010ae
A
3140 pr = nd_prefix.lh_first;
3141 while (pr) {
3142 NDPR_LOCK(pr);
39236c6e 3143 if (pr->ndpr_stateflags & NDPRF_PROCESSED_ONLINK) {
6d2010ae
A
3144 NDPR_UNLOCK(pr);
3145 pr = pr->ndpr_next;
3146 continue;
3147 }
f427ee49 3148 NDPR_ADDREF(pr);
6d2010ae
A
3149 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr) &&
3150 (pr->ndpr_debug & IFD_ATTACHED)) {
f427ee49 3151 if (NDPR_REMREF(pr) == NULL) {
fe8ab488 3152 pr = NULL;
0a7de745 3153 } else {
fe8ab488 3154 NDPR_UNLOCK(pr);
0a7de745 3155 }
1c79356b 3156 break;
6d2010ae 3157 }
39236c6e 3158 pr->ndpr_stateflags |= NDPRF_PROCESSED_ONLINK;
6d2010ae
A
3159 NDPR_UNLOCK(pr);
3160 NDPR_REMREF(pr);
3161 /*
3162 * Since find_pfxlist_reachable_router() drops the nd6_mutex, we
39236c6e
A
3163 * have to start over, but the NDPRF_PROCESSED_ONLINK flag will
3164 * stop us from checking the same prefix twice.
6d2010ae
A
3165 */
3166 pr = nd_prefix.lh_first;
3167 }
3168 LIST_FOREACH(prclear, &nd_prefix, ndpr_entry) {
3169 NDPR_LOCK(prclear);
39236c6e 3170 prclear->ndpr_stateflags &= ~NDPRF_PROCESSED_ONLINK;
6d2010ae 3171 NDPR_UNLOCK(prclear);
1c79356b 3172 }
6d2010ae
A
3173 /*
3174 * If we have no such prefix, check whether we still have a router
3175 * that does not advertise any prefixes.
3176 */
3177 if (pr == NULL) {
f427ee49 3178 for (dr = TAILQ_FIRST(&nd_defrouter_list); dr;
6d2010ae
A
3179 dr = TAILQ_NEXT(dr, dr_entry)) {
3180 struct nd_prefix *pr0;
3181
3182 for (pr0 = nd_prefix.lh_first; pr0;
3183 pr0 = pr0->ndpr_next) {
3184 NDPR_LOCK(pr0);
3185 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL) {
3186 NDPR_UNLOCK(pr0);
3187 break;
3188 }
3189 NDPR_UNLOCK(pr0);
3190 }
0a7de745 3191 if (pfxrtr != NULL) {
6d2010ae 3192 break;
0a7de745 3193 }
6d2010ae
A
3194 }
3195 }
f427ee49 3196 if (pr != NULL || (TAILQ_FIRST(&nd_defrouter_list) && pfxrtr == NULL)) {
1c79356b 3197 /*
6d2010ae
A
3198 * There is at least one prefix that has a reachable router,
3199 * or at least a router which probably does not advertise
3200 * any prefixes. The latter would be the case when we move
3201 * to a new link where we have a router that does not provide
3202 * prefixes and we configure an address by hand.
9bccf70c
A
3203 * Detach prefixes which have no reachable advertising
3204 * router, and attach other prefixes.
1c79356b 3205 */
6d2010ae
A
3206 pr = nd_prefix.lh_first;
3207 while (pr) {
3208 NDPR_LOCK(pr);
9bccf70c 3209 /*
6d2010ae
A
3210 * We aren't interested prefixes already processed,
3211 * nor in prefixes without the L bit
3212 * set nor in static prefixes
9bccf70c 3213 */
6d2010ae 3214 if (pr->ndpr_raf_onlink == 0 ||
39236c6e 3215 pr->ndpr_stateflags & NDPRF_PROCESSED_ONLINK ||
6d2010ae
A
3216 pr->ndpr_stateflags & NDPRF_STATIC) {
3217 NDPR_UNLOCK(pr);
3218 pr = pr->ndpr_next;
9bccf70c 3219 continue;
6d2010ae 3220 }
f427ee49 3221 NDPR_ADDREF(pr);
9bccf70c 3222 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
6d2010ae 3223 find_pfxlist_reachable_router(pr) == NULL &&
0a7de745 3224 (pr->ndpr_debug & IFD_ATTACHED)) {
9bccf70c 3225 pr->ndpr_stateflags |= NDPRF_DETACHED;
0a7de745 3226 }
9bccf70c 3227 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
6d2010ae 3228 find_pfxlist_reachable_router(pr) != NULL &&
0a7de745 3229 (pr->ndpr_debug & IFD_ATTACHED)) {
9bccf70c 3230 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
0a7de745 3231 }
39236c6e 3232 pr->ndpr_stateflags |= NDPRF_PROCESSED_ONLINK;
6d2010ae
A
3233 NDPR_UNLOCK(pr);
3234 NDPR_REMREF(pr);
3235 /*
3236 * Since find_pfxlist_reachable_router() drops the
3237 * nd6_mutex, we have to start over, but the
39236c6e
A
3238 * NDPRF_PROCESSED_ONLINK flag will stop us from
3239 * checking the same prefix twice.
6d2010ae
A
3240 */
3241 pr = nd_prefix.lh_first;
1c79356b 3242 }
9bccf70c
A
3243 } else {
3244 /* there is no prefix that has a reachable router */
1c79356b 3245 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
6d2010ae
A
3246 NDPR_LOCK(pr);
3247 if (pr->ndpr_raf_onlink == 0 ||
3248 pr->ndpr_stateflags & NDPRF_STATIC) {
3249 NDPR_UNLOCK(pr);
9bccf70c 3250 continue;
6d2010ae 3251 }
0a7de745 3252 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0) {
9bccf70c 3253 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
0a7de745 3254 }
6d2010ae 3255 NDPR_UNLOCK(pr);
9bccf70c
A
3256 }
3257 }
6d2010ae
A
3258 LIST_FOREACH(prclear, &nd_prefix, ndpr_entry) {
3259 NDPR_LOCK(prclear);
39236c6e 3260 prclear->ndpr_stateflags &= ~NDPRF_PROCESSED_ONLINK;
6d2010ae
A
3261 NDPR_UNLOCK(prclear);
3262 }
9bccf70c
A
3263 /*
3264 * Remove each interface route associated with a (just) detached
3265 * prefix, and reinstall the interface route for a (just) attached
3266 * prefix. Note that all attempt of reinstallation does not
3267 * necessarily success, when a same prefix is shared among multiple
3268 * interfaces. Such cases will be handled in nd6_prefix_onlink,
3269 * so we don't have to care about them.
3270 */
6d2010ae
A
3271 pr = nd_prefix.lh_first;
3272 while (pr) {
9bccf70c
A
3273 int e;
3274
6d2010ae
A
3275 NDPR_LOCK(pr);
3276 if (pr->ndpr_raf_onlink == 0 ||
fe8ab488
A
3277 pr->ndpr_stateflags & NDPRF_STATIC ||
3278 pr->ndpr_stateflags & NDPRF_PROCESSED_ONLINK ||
3279 pr->ndpr_stateflags & NDPRF_DEFUNCT) {
6d2010ae
A
3280 NDPR_UNLOCK(pr);
3281 pr = pr->ndpr_next;
9bccf70c 3282 continue;
6d2010ae 3283 }
fe8ab488 3284 pr->ndpr_stateflags |= NDPRF_PROCESSED_ONLINK;
f427ee49 3285 NDPR_ADDREF(pr);
9bccf70c
A
3286 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
3287 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
6d2010ae
A
3288 NDPR_UNLOCK(pr);
3289 lck_mtx_unlock(nd6_mutex);
9bccf70c 3290 if ((e = nd6_prefix_offlink(pr)) != 0) {
cb323159 3291 nd6log(error,
9bccf70c
A
3292 "pfxlist_onlink_check: failed to "
3293 "make %s/%d offlink, errno=%d\n",
3294 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
cb323159 3295 pr->ndpr_plen, e);
9bccf70c 3296 }
6d2010ae 3297 lck_mtx_lock(nd6_mutex);
fe8ab488 3298 NDPR_REMREF(pr);
6d2010ae
A
3299 pr = nd_prefix.lh_first;
3300 continue;
9bccf70c
A
3301 }
3302 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
3303 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 &&
3304 pr->ndpr_raf_onlink) {
6d2010ae
A
3305 NDPR_UNLOCK(pr);
3306 if ((e = nd6_prefix_onlink(pr)) != 0) {
cb323159 3307 nd6log(error,
9bccf70c
A
3308 "pfxlist_onlink_check: failed to "
3309 "make %s/%d offlink, errno=%d\n",
3310 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
cb323159 3311 pr->ndpr_plen, e);
9bccf70c 3312 }
fe8ab488
A
3313 NDPR_REMREF(pr);
3314 pr = nd_prefix.lh_first;
3315 continue;
6d2010ae
A
3316 } else {
3317 NDPR_UNLOCK(pr);
9bccf70c 3318 }
fe8ab488 3319 NDPR_REMREF(pr);
6d2010ae 3320 pr = pr->ndpr_next;
9bccf70c 3321 }
fe8ab488
A
3322 LIST_FOREACH(prclear, &nd_prefix, ndpr_entry) {
3323 NDPR_LOCK(prclear);
3324 prclear->ndpr_stateflags &= ~NDPRF_PROCESSED_ONLINK;
3325 NDPR_UNLOCK(prclear);
3326 }
3327 VERIFY(nd_prefix_busy);
3328 nd_prefix_busy = FALSE;
3329 if (nd_prefix_waiters > 0) {
3330 nd_prefix_waiters = 0;
3331 wakeup(nd_prefix_waitchan);
3332 }
9bccf70c
A
3333
3334 /*
3335 * Changes on the prefix status might affect address status as well.
3336 * Make sure that all addresses derived from an attached prefix are
3337 * attached, and that all addresses derived from a detached prefix are
3338 * detached. Note, however, that a manually configured address should
3339 * always be attached.
3340 * The precise detection logic is same as the one for prefixes.
316670eb
A
3341 *
3342 * ifnet_get_address_list_family_internal() may fail due to memory
3343 * pressure, but we will eventually be called again when we receive
3344 * another NA, RA, or when the link status changes.
9bccf70c 3345 */
316670eb 3346 err = ifnet_get_address_list_family_internal(NULL, &ifap, AF_INET6, 0,
39236c6e 3347 M_NOWAIT, 0);
316670eb 3348 if (err != 0 || ifap == NULL) {
cb323159
A
3349 nd6log(error, "%s: ifnet_get_address_list_family_internal "
3350 "failed", __func__);
316670eb
A
3351 return;
3352 }
3353 for (i = 0; ifap[i]; i++) {
3354 ifa = ifatoia6(ifap[i]);
6d2010ae 3355 IFA_LOCK(&ifa->ia_ifa);
316670eb
A
3356 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0 ||
3357 (ifap[i]->ifa_debug & IFD_ATTACHED) == 0) {
6d2010ae
A
3358 IFA_UNLOCK(&ifa->ia_ifa);
3359 continue;
3360 }
3361 if ((ndpr = ifa->ia6_ndpr) == NULL) {
9bccf70c
A
3362 /*
3363 * This can happen when we first configure the address
3364 * (i.e. the address exists, but the prefix does not).
3365 * XXX: complicated relationships...
3366 */
6d2010ae 3367 IFA_UNLOCK(&ifa->ia_ifa);
9bccf70c
A
3368 continue;
3369 }
6d2010ae 3370 IFA_UNLOCK(&ifa->ia_ifa);
9bccf70c 3371
6d2010ae 3372 NDPR_LOCK(ndpr);
f427ee49 3373 NDPR_ADDREF(ndpr);
6d2010ae 3374 if (find_pfxlist_reachable_router(ndpr)) {
f427ee49 3375 if (NDPR_REMREF(ndpr) == NULL) {
fe8ab488
A
3376 found = 0;
3377 } else {
3378 NDPR_UNLOCK(ndpr);
3379 found = 1;
3380 }
9bccf70c 3381 break;
6d2010ae
A
3382 }
3383 NDPR_UNLOCK(ndpr);
3384 NDPR_REMREF(ndpr);
9bccf70c 3385 }
316670eb
A
3386 if (found) {
3387 for (i = 0; ifap[i]; i++) {
3388 ifa = ifatoia6(ifap[i]);
6d2010ae 3389 IFA_LOCK(&ifa->ia_ifa);
316670eb
A
3390 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0 ||
3391 (ifap[i]->ifa_debug & IFD_ATTACHED) == 0) {
6d2010ae 3392 IFA_UNLOCK(&ifa->ia_ifa);
9bccf70c 3393 continue;
6d2010ae
A
3394 }
3395 if ((ndpr = ifa->ia6_ndpr) == NULL) {
3396 /* XXX: see above. */
3397 IFA_UNLOCK(&ifa->ia_ifa);
3398 continue;
3399 }
6d2010ae
A
3400 IFA_UNLOCK(&ifa->ia_ifa);
3401 NDPR_LOCK(ndpr);
f427ee49 3402 NDPR_ADDREF(ndpr);
6d2010ae
A
3403 if (find_pfxlist_reachable_router(ndpr)) {
3404 NDPR_UNLOCK(ndpr);
3405 IFA_LOCK(&ifa->ia_ifa);
3406 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
3407 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
39037602 3408 in6_ifaddr_set_dadprogress((struct in6_ifaddr *)ifa);
6d2010ae
A
3409 IFA_UNLOCK(&ifa->ia_ifa);
3410 nd6_dad_start((struct ifaddr *)ifa, 0);
3411 } else {
3412 IFA_UNLOCK(&ifa->ia_ifa);
3413 }
3414 } else {
3415 NDPR_UNLOCK(ndpr);
3416 IFA_LOCK(&ifa->ia_ifa);
a39ff7e2
A
3417 if ((ifa->ia6_flags & IN6_IFF_DETACHED) == 0) {
3418 ifa->ia6_flags |= IN6_IFF_DETACHED;
3419 in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DETACHED,
3420 ifa->ia_ifa.ifa_ifp, &ifa->ia_addr.sin6_addr,
3421 0);
3422 }
6d2010ae
A
3423 IFA_UNLOCK(&ifa->ia_ifa);
3424 }
3425 NDPR_REMREF(ndpr);
1c79356b 3426 }
316670eb
A
3427 } else {
3428 for (i = 0; ifap[i]; i++) {
3429 ifa = ifatoia6(ifap[i]);
6d2010ae
A
3430 IFA_LOCK(&ifa->ia_ifa);
3431 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) {
3432 IFA_UNLOCK(&ifa->ia_ifa);
9bccf70c 3433 continue;
6d2010ae
A
3434 }
3435 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
3436 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
39037602 3437 in6_ifaddr_set_dadprogress((struct in6_ifaddr *)ifa);
6d2010ae
A
3438 IFA_UNLOCK(&ifa->ia_ifa);
3439 /* Do we need a delay in this case? */
3440 nd6_dad_start((struct ifaddr *)ifa, 0);
3441 } else {
3442 IFA_UNLOCK(&ifa->ia_ifa);
3443 }
3444 }
3445 }
316670eb 3446 ifnet_free_address_list(ifap);
6d2010ae
A
3447}
3448
3449static struct nd_prefix *
3450nd6_prefix_equal_lookup(struct nd_prefix *pr, boolean_t primary_only)
3451{
3452 struct nd_prefix *opr;
3453
5ba3f43e 3454 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae
A
3455
3456 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) {
0a7de745 3457 if (opr == pr) {
6d2010ae 3458 continue;
0a7de745 3459 }
6d2010ae
A
3460
3461 NDPR_LOCK(opr);
3462 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
3463 NDPR_UNLOCK(opr);
3464 continue;
3465 }
3466 if (opr->ndpr_plen == pr->ndpr_plen &&
3467 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
3468 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen) &&
3469 (!primary_only ||
3470 !(opr->ndpr_stateflags & NDPRF_IFSCOPE))) {
f427ee49 3471 NDPR_ADDREF(opr);
6d2010ae 3472 NDPR_UNLOCK(opr);
0a7de745 3473 return opr;
6d2010ae
A
3474 }
3475 NDPR_UNLOCK(opr);
3476 }
0a7de745 3477 return NULL;
6d2010ae
A
3478}
3479
3480/*
3481 * Synchronize the interface routes of similar prefixes on different
3482 * interfaces; the one using the default interface would be (re)installed
3483 * as a primary/non-scoped entry, and the rest as scoped entri(es).
3484 */
3485static void
3486nd6_prefix_sync(struct ifnet *ifp)
3487{
3488 struct nd_prefix *pr, *opr;
3489 int err = 0;
3490
5ba3f43e 3491 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae 3492
0a7de745 3493 if (ifp == NULL) {
6d2010ae 3494 return;
0a7de745 3495 }
9bccf70c 3496
6d2010ae
A
3497 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
3498 NDPR_LOCK(pr);
3499 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
3500 NDPR_UNLOCK(pr);
3501 continue;
3502 }
3503 if (pr->ndpr_ifp == ifp &&
3504 (pr->ndpr_stateflags & NDPRF_IFSCOPE) &&
3505 !IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) {
3506 NDPR_UNLOCK(pr);
3507 break;
9bccf70c 3508 }
6d2010ae 3509 NDPR_UNLOCK(pr);
1c79356b 3510 }
6d2010ae 3511
0a7de745 3512 if (pr == NULL) {
6d2010ae 3513 return;
0a7de745 3514 }
6d2010ae
A
3515
3516 /* Remove conflicting entries */
3517 opr = nd6_prefix_equal_lookup(pr, TRUE);
3518 if (opr != NULL) {
91447636 3519 lck_mtx_unlock(nd6_mutex);
6d2010ae
A
3520 err = nd6_prefix_offlink(opr);
3521 lck_mtx_lock(nd6_mutex);
3522 if (err != 0) {
cb323159 3523 nd6log(error,
6d2010ae
A
3524 "%s: failed to make %s/%d offlink on %s, "
3525 "errno=%d\n", __func__,
3526 ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
cb323159 3527 opr->ndpr_plen, if_name(opr->ndpr_ifp), err);
6d2010ae
A
3528 }
3529 } else {
cb323159 3530 nd6log(error,
6d2010ae
A
3531 "%s: scoped %s/%d on %s has no matching unscoped prefix\n",
3532 __func__, ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
cb323159 3533 pr->ndpr_plen, if_name(pr->ndpr_ifp));
6d2010ae
A
3534 }
3535
3536 lck_mtx_unlock(nd6_mutex);
3537 err = nd6_prefix_offlink(pr);
3538 lck_mtx_lock(nd6_mutex);
3539 if (err != 0) {
cb323159 3540 nd6log(error,
6d2010ae
A
3541 "%s: failed to make %s/%d offlink on %s, errno=%d\n",
3542 __func__, ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
cb323159 3543 pr->ndpr_plen, if_name(pr->ndpr_ifp), err);
6d2010ae
A
3544 }
3545
3546 /* Add the entries back */
3547 if (opr != NULL) {
3548 err = nd6_prefix_onlink_scoped(opr, opr->ndpr_ifp->if_index);
3549 if (err != 0) {
cb323159 3550 nd6log(error,
6d2010ae
A
3551 "%s: failed to make %s/%d scoped onlink on %s, "
3552 "errno=%d\n", __func__,
3553 ip6_sprintf(&opr->ndpr_prefix.sin6_addr),
cb323159 3554 opr->ndpr_plen, if_name(opr->ndpr_ifp), err);
6d2010ae
A
3555 }
3556 }
3557
3558 err = nd6_prefix_onlink_scoped(pr, IFSCOPE_NONE);
3559 if (err != 0) {
cb323159 3560 nd6log(error,
6d2010ae
A
3561 "%s: failed to make %s/%d onlink on %s, errno=%d\n",
3562 __func__, ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
cb323159 3563 pr->ndpr_plen, if_name(pr->ndpr_ifp), err);
6d2010ae
A
3564 }
3565
3566 if (err != 0) {
cb323159 3567 nd6log(error,
6d2010ae
A
3568 "%s: error promoting %s/%d to %s from %s\n",
3569 __func__, ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
3570 pr->ndpr_plen, if_name(pr->ndpr_ifp),
cb323159 3571 (opr != NULL) ? if_name(opr->ndpr_ifp) : "NONE");
6d2010ae 3572 } else {
cb323159 3573 nd6log2(info,
6d2010ae
A
3574 "%s: %s/%d promoted, previously on %s\n",
3575 if_name(pr->ndpr_ifp),
3576 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen,
cb323159 3577 (opr != NULL) ? if_name(opr->ndpr_ifp) : "NONE");
6d2010ae
A
3578 }
3579
0a7de745 3580 if (opr != NULL) {
6d2010ae 3581 NDPR_REMREF(opr);
0a7de745 3582 }
1c79356b
A
3583}
3584
6d2010ae
A
3585static int
3586nd6_prefix_onlink_common(struct nd_prefix *pr, boolean_t force_scoped,
3587 unsigned int ifscope)
1c79356b 3588{
9bccf70c
A
3589 struct ifaddr *ifa;
3590 struct ifnet *ifp = pr->ndpr_ifp;
6d2010ae 3591 struct sockaddr_in6 mask6, prefix;
9bccf70c 3592 struct nd_prefix *opr;
b0d623f7 3593 u_int32_t rtflags;
316670eb 3594 int error = 0, prproxy = 0;
9bccf70c 3595 struct rtentry *rt = NULL;
f427ee49 3596 u_char prefix_len = 0;
9bccf70c 3597
5ba3f43e 3598 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_OWNED);
6d2010ae 3599
9bccf70c 3600 /* sanity check */
6d2010ae 3601 NDPR_LOCK(pr);
9bccf70c 3602 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
cb323159 3603 nd6log(error,
39236c6e
A
3604 "%s: %s/%d on %s scoped=%d is already on-link\n",
3605 __func__, ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
3606 pr->ndpr_plen, if_name(pr->ndpr_ifp),
3607 (pr->ndpr_stateflags & NDPRF_IFSCOPE) ? 1 : 0);
cb323159
A
3608 NDPR_UNLOCK(pr);
3609 return EEXIST;
9bccf70c 3610 }
6d2010ae 3611 NDPR_UNLOCK(pr);
9bccf70c
A
3612
3613 /*
3614 * Add the interface route associated with the prefix. Before
3615 * installing the route, check if there's the same prefix on another
3616 * interface, and the prefix has already installed the interface route.
9bccf70c 3617 */
6d2010ae 3618 opr = nd6_prefix_equal_lookup(pr, FALSE);
0a7de745 3619 if (opr != NULL) {
6d2010ae 3620 NDPR_REMREF(opr);
0a7de745 3621 }
6d2010ae 3622
39037602 3623 if (!force_scoped) {
6d2010ae
A
3624 /*
3625 * If a primary/non-scoped interface route already exists,
3626 * install the new one as a scoped entry. If the existing
3627 * interface route is scoped, install new as non-scoped.
3628 */
3629 ifscope = (opr != NULL) ? ifp->if_index : IFSCOPE_NONE;
3630 opr = nd6_prefix_equal_lookup(pr, TRUE);
0a7de745 3631 if (opr != NULL) {
6d2010ae 3632 NDPR_REMREF(opr);
0a7de745 3633 } else if (ifscope != IFSCOPE_NONE) {
6d2010ae 3634 ifscope = IFSCOPE_NONE;
0a7de745 3635 }
9bccf70c
A
3636 }
3637
3638 /*
6d2010ae 3639 * We prefer link-local addresses as the associated interface address.
9bccf70c
A
3640 */
3641 /* search for a link-local addr */
3642 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
39236c6e 3643 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
9bccf70c 3644 if (ifa == NULL) {
6d2010ae
A
3645 struct in6_ifaddr *ia6;
3646 ifnet_lock_shared(ifp);
3647 IFP_TO_IA6(ifp, ia6);
91447636 3648 ifnet_lock_done(ifp);
0a7de745 3649 if (ia6 != NULL) {
6d2010ae 3650 ifa = &ia6->ia_ifa;
0a7de745 3651 }
9bccf70c
A
3652 /* should we care about ia6_flags? */
3653 }
6d2010ae 3654 NDPR_LOCK(pr);
9bccf70c
A
3655 if (ifa == NULL) {
3656 /*
3657 * This can still happen, when, for example, we receive an RA
3658 * containing a prefix with the L bit set and the A bit clear,
3659 * after removing all IPv6 addresses on the receiving
3660 * interface. This should, of course, be rare though.
3661 */
cb323159 3662 nd6log(info,
9bccf70c
A
3663 "nd6_prefix_onlink: failed to find any ifaddr"
3664 " to add route for a prefix(%s/%d) on %s\n",
3665 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
cb323159 3666 pr->ndpr_plen, if_name(ifp));
6d2010ae 3667 NDPR_UNLOCK(pr);
0a7de745 3668 return 0;
9bccf70c 3669 }
1c79356b
A
3670
3671 /*
9bccf70c
A
3672 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
3673 * ifa->ifa_rtrequest = nd6_rtrequest;
1c79356b 3674 */
0a7de745
A
3675 bzero(&mask6, sizeof(mask6));
3676 mask6.sin6_len = sizeof(mask6);
9bccf70c 3677 mask6.sin6_addr = pr->ndpr_mask;
6d2010ae 3678 prefix = pr->ndpr_prefix;
f427ee49 3679 prefix_len = pr->ndpr_plen;
0a7de745 3680 if ((rt = pr->ndpr_rt) != NULL) {
316670eb 3681 pr->ndpr_rt = NULL;
0a7de745 3682 }
f427ee49 3683 NDPR_ADDREF(pr); /* keep reference for this routine */
6d2010ae 3684 NDPR_UNLOCK(pr);
91447636 3685
6d2010ae 3686 IFA_LOCK_SPIN(ifa);
9bccf70c 3687 rtflags = ifa->ifa_flags | RTF_CLONING | RTF_UP;
6d2010ae 3688 IFA_UNLOCK(ifa);
9bccf70c
A
3689 if (nd6_need_cache(ifp)) {
3690 /* explicitly set in case ifa_flags does not set the flag. */
3691 rtflags |= RTF_CLONING;
3692 } else {
3693 /*
3694 * explicitly clear the cloning bit in case ifa_flags sets it.
3695 */
3696 rtflags &= ~RTF_CLONING;
3697 }
6d2010ae
A
3698
3699 lck_mtx_unlock(nd6_mutex);
3700
f427ee49
A
3701 /*
3702 * check if it conflicts with a indirect prefix route added by RIO
3703 * if so, remove the rti entry.
3704 */
3705 if (ifscope == IFSCOPE_NONE) {
3706 struct rtentry *temp_route = NULL;
3707 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
3708 lck_mtx_lock(rnh_lock);
3709 temp_route = rt_lookup(TRUE, (struct sockaddr *)&prefix, (struct sockaddr *)&mask6, rt_tables[AF_INET6], IFSCOPE_NONE);
3710 lck_mtx_unlock(rnh_lock);
3711
3712 if (temp_route != NULL && temp_route->rt_flags & RTF_GATEWAY && temp_route->rt_ifp != NULL) {
3713 struct nd_route_info rti = {};
3714 bzero(&rti, sizeof(rti));
3715 rti.nd_rti_prefixlen = prefix_len;
3716 rti.nd_rti_prefix = prefix.sin6_addr;
3717 lck_mtx_lock(nd6_mutex);
3718 nd6_rti_purge(&rti);
3719 lck_mtx_unlock(nd6_mutex);
3720 }
3721 if (temp_route != NULL) {
3722 rtfree(temp_route);
3723 }
3724 }
3725
316670eb
A
3726 if (rt != NULL) {
3727 rtfree(rt);
3728 rt = NULL;
3729 }
3730
6d2010ae
A
3731 error = rtrequest_scoped(RTM_ADD, (struct sockaddr *)&prefix,
3732 ifa->ifa_addr, (struct sockaddr *)&mask6, rtflags, &rt,
3733 ifscope);
3734
316670eb
A
3735 /*
3736 * Serialize the setting of NDPRF_PRPROXY.
3737 */
3738 lck_mtx_lock(&proxy6_lock);
3739
6d2010ae
A
3740 if (rt != NULL) {
3741 RT_LOCK(rt);
3742 nd6_rtmsg(RTM_ADD, rt);
3743 RT_UNLOCK(rt);
316670eb 3744 NDPR_LOCK(pr);
6d2010ae
A
3745 } else {
3746 NDPR_LOCK(pr);
cb323159
A
3747 nd6log(error, "nd6_prefix_onlink: failed to add route for a"
3748 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%x,"
6d2010ae 3749 " scoped=%d, errno = %d\n",
9bccf70c
A
3750 ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
3751 pr->ndpr_plen, if_name(ifp),
316670eb
A
3752 ip6_sprintf(&((struct sockaddr_in6 *)
3753 (void *)ifa->ifa_addr)->sin6_addr),
6d2010ae 3754 ip6_sprintf(&mask6.sin6_addr), rtflags,
cb323159 3755 (ifscope != IFSCOPE_NONE), error);
9bccf70c 3756 }
316670eb 3757 NDPR_LOCK_ASSERT_HELD(pr);
9bccf70c 3758
316670eb 3759 pr->ndpr_stateflags &= ~(NDPRF_IFSCOPE | NDPRF_PRPROXY);
6d2010ae 3760
316670eb
A
3761 /*
3762 * TODO: If the prefix route exists, we should really find it and
3763 * refer the prefix to it; otherwise ndpr_rt is NULL.
3764 */
fe8ab488
A
3765 if (!(pr->ndpr_stateflags & NDPRF_DEFUNCT) &&
3766 (rt != NULL || error == EEXIST)) {
3e170ce0 3767 struct nd_ifinfo *ndi = NULL;
316670eb
A
3768
3769 VERIFY(pr->ndpr_prproxy_sols_cnt == 0);
3770 VERIFY(RB_EMPTY(&pr->ndpr_prproxy_sols));
3771
316670eb 3772 ndi = ND_IFINFO(ifp);
0a7de745 3773 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
316670eb
A
3774 lck_mtx_lock(&ndi->lock);
3775
0a7de745 3776 pr->ndpr_rt = rt; /* keep reference from rtrequest */
6d2010ae 3777 pr->ndpr_stateflags |= NDPRF_ONLINK;
316670eb 3778 if (ifscope != IFSCOPE_NONE) {
6d2010ae 3779 pr->ndpr_stateflags |= NDPRF_IFSCOPE;
316670eb
A
3780 } else if ((rtflags & RTF_CLONING) &&
3781 (ndi->flags & ND6_IFF_PROXY_PREFIXES) &&
3782 !IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) {
3783 /*
3784 * At present, in order for the prefix to be eligible
3785 * as a proxying/proxied prefix, we require that the
3786 * prefix route entry be marked as a cloning route with
3787 * RTF_PROXY; i.e. nd6_need_cache() needs to return
3788 * true for the interface type, hence the test for
3789 * RTF_CLONING above.
3790 */
3791 pr->ndpr_stateflags |= NDPRF_PRPROXY;
3792 }
3793
3794 lck_mtx_unlock(&ndi->lock);
0a7de745 3795 } else if (rt != NULL && pr->ndpr_stateflags & NDPRF_DEFUNCT) {
fe8ab488 3796 rtfree(rt);
0a7de745 3797 }
316670eb
A
3798
3799 prproxy = (pr->ndpr_stateflags & NDPRF_PRPROXY);
3800 VERIFY(!prproxy || !(pr->ndpr_stateflags & NDPRF_IFSCOPE));
6d2010ae 3801 NDPR_UNLOCK(pr);
9bccf70c 3802
6d2010ae 3803 IFA_REMREF(ifa);
b0d623f7 3804
316670eb
A
3805 /*
3806 * If this is an upstream prefix, find the downstream ones (if any)
3807 * and re-configure their prefix routes accordingly. Otherwise,
3808 * this could be potentially be a downstream prefix, and so find the
3809 * upstream prefix, if any.
3810 */
3811 nd6_prproxy_prelist_update(pr, prproxy ? pr : NULL);
3812
0a7de745 3813 NDPR_REMREF(pr); /* release reference for this routine */
316670eb
A
3814 lck_mtx_unlock(&proxy6_lock);
3815
3816 lck_mtx_lock(nd6_mutex);
3817
0a7de745 3818 return error;
6d2010ae 3819}
b0d623f7 3820
6d2010ae
A
3821int
3822nd6_prefix_onlink(struct nd_prefix *pr)
3823{
0a7de745 3824 return nd6_prefix_onlink_common(pr, FALSE, IFSCOPE_NONE);
9bccf70c
A
3825}
3826
3827int
6d2010ae 3828nd6_prefix_onlink_scoped(struct nd_prefix *pr, unsigned int ifscope)
9bccf70c 3829{
0a7de745 3830 return nd6_prefix_onlink_common(pr, TRUE, ifscope);
6d2010ae
A
3831}
3832
3833int
3834nd6_prefix_offlink(struct nd_prefix *pr)
3835{
316670eb 3836 int plen, error = 0, prproxy;
9bccf70c 3837 struct ifnet *ifp = pr->ndpr_ifp;
6d2010ae 3838 struct sockaddr_in6 sa6, mask6, prefix;
316670eb 3839 struct rtentry *rt = NULL, *ndpr_rt = NULL;
6d2010ae
A
3840 unsigned int ifscope;
3841
5ba3f43e 3842 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
9bccf70c
A
3843
3844 /* sanity check */
6d2010ae 3845 NDPR_LOCK(pr);
9bccf70c 3846 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
cb323159 3847 nd6log(error,
6d2010ae
A
3848 "nd6_prefix_offlink: %s/%d on %s scoped=%d is already "
3849 "off-link\n", ip6_sprintf(&pr->ndpr_prefix.sin6_addr),
3850 pr->ndpr_plen, if_name(pr->ndpr_ifp),
cb323159 3851 (pr->ndpr_stateflags & NDPRF_IFSCOPE) ? 1 : 0);
6d2010ae 3852 NDPR_UNLOCK(pr);
0a7de745 3853 return EEXIST;
9bccf70c
A
3854 }
3855
0a7de745 3856 bzero(&sa6, sizeof(sa6));
1c79356b 3857 sa6.sin6_family = AF_INET6;
0a7de745 3858 sa6.sin6_len = sizeof(sa6);
1c79356b 3859 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
0a7de745
A
3860 sizeof(struct in6_addr));
3861 bzero(&mask6, sizeof(mask6));
1c79356b 3862 mask6.sin6_family = AF_INET6;
0a7de745
A
3863 mask6.sin6_len = sizeof(sa6);
3864 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
6d2010ae
A
3865 prefix = pr->ndpr_prefix;
3866 plen = pr->ndpr_plen;
0a7de745 3867 if ((ndpr_rt = pr->ndpr_rt) != NULL) {
316670eb 3868 pr->ndpr_rt = NULL;
0a7de745 3869 }
f427ee49 3870 NDPR_ADDREF(pr); /* keep reference for this routine */
6d2010ae 3871 NDPR_UNLOCK(pr);
1c79356b 3872
6d2010ae
A
3873 ifscope = (pr->ndpr_stateflags & NDPRF_IFSCOPE) ?
3874 ifp->if_index : IFSCOPE_NONE;
3875
3876 error = rtrequest_scoped(RTM_DELETE, (struct sockaddr *)&sa6,
3877 NULL, (struct sockaddr *)&mask6, 0, &rt, ifscope);
3878
3879 if (rt != NULL) {
9bccf70c 3880 /* report the route deletion to the routing socket. */
6d2010ae
A
3881 RT_LOCK(rt);
3882 nd6_rtmsg(RTM_DELETE, rt);
3883 RT_UNLOCK(rt);
3884 rtfree(rt);
6d2010ae 3885 } else {
cb323159 3886 nd6log(error,
9bccf70c 3887 "nd6_prefix_offlink: failed to delete route: "
6d2010ae
A
3888 "%s/%d on %s, scoped %d, (errno = %d)\n",
3889 ip6_sprintf(&sa6.sin6_addr), plen, if_name(ifp),
cb323159 3890 (ifscope != IFSCOPE_NONE), error);
1c79356b
A
3891 }
3892
0a7de745 3893 if (ndpr_rt != NULL) {
316670eb 3894 rtfree(ndpr_rt);
0a7de745 3895 }
316670eb
A
3896
3897 lck_mtx_lock(&proxy6_lock);
3898
6d2010ae 3899 NDPR_LOCK(pr);
316670eb
A
3900 prproxy = (pr->ndpr_stateflags & NDPRF_PRPROXY);
3901 VERIFY(!prproxy || !(pr->ndpr_stateflags & NDPRF_IFSCOPE));
3902 pr->ndpr_stateflags &= ~(NDPRF_ONLINK | NDPRF_IFSCOPE | NDPRF_PRPROXY);
3903 if (pr->ndpr_prproxy_sols_cnt > 0) {
3904 VERIFY(prproxy);
3905 nd6_prproxy_sols_reap(pr);
3906 VERIFY(pr->ndpr_prproxy_sols_cnt == 0);
3907 VERIFY(RB_EMPTY(&pr->ndpr_prproxy_sols));
3908 }
6d2010ae 3909 NDPR_UNLOCK(pr);
9bccf70c 3910
316670eb
A
3911 /*
3912 * If this was an upstream prefix, find the downstream ones and do
3913 * some cleanups. If this was a downstream prefix, the prefix route
3914 * has been removed from the routing table above, but there may be
3915 * other tasks to perform.
3916 */
3917 nd6_prproxy_prelist_update(pr, prproxy ? pr : NULL);
3918
0a7de745 3919 NDPR_REMREF(pr); /* release reference for this routine */
316670eb
A
3920 lck_mtx_unlock(&proxy6_lock);
3921
0a7de745 3922 return error;
1c79356b
A
3923}
3924
d9a64523 3925struct in6_ifaddr *
f427ee49
A
3926in6_pfx_newpersistaddr(struct nd_prefix *pr, int mcast, int *errorp,
3927 boolean_t is_clat46, uint8_t collision_count)
1c79356b 3928{
3e170ce0
A
3929 struct in6_ifaddr *ia6 = NULL;
3930 struct ifnet *ifp = NULL;
3931 struct nd_ifinfo *ndi = NULL;
1c79356b 3932 struct in6_addr mask;
39236c6e
A
3933 struct in6_aliasreq ifra;
3934 int error, ifaupdate, iidlen, notcga;
3935
3936 VERIFY(pr != NULL);
3937 VERIFY(errorp != NULL);
3938
3939 NDPR_LOCK(pr);
3940 ifp = pr->ndpr_ifp;
3941 ia6 = NULL;
3942 error = 0;
1c79356b 3943
9bccf70c 3944 /*
39236c6e
A
3945 * Prefix Length check:
3946 * If the sum of the prefix length and interface identifier
3947 * length does not equal 128 bits, the Prefix Information
3948 * option MUST be ignored. The length of the interface
3949 * identifier is defined in a separate link-type specific
3950 * document.
9bccf70c 3951 */
39236c6e
A
3952 iidlen = in6_if2idlen(ifp);
3953 if (iidlen < 0) {
3954 error = EADDRNOTAVAIL;
3955 /* this should not happen, so we always log it. */
3956 log(LOG_ERR, "%s: IID length undefined (%s)\n",
3957 __func__, if_name(ifp));
3958 goto unlock1;
3959 } else if (iidlen != 64) {
3960 error = EADDRNOTAVAIL;
3961 /*
3962 * stateless autoconfiguration not yet well-defined for IID
3963 * lengths other than 64 octets. Just give up for now.
3964 */
cb323159
A
3965 nd6log(info, "%s: IID length not 64 octets (%s)\n",
3966 __func__, if_name(ifp));
39236c6e
A
3967 goto unlock1;
3968 }
1c79356b 3969
39236c6e
A
3970 if (iidlen + pr->ndpr_plen != 128) {
3971 error = EADDRNOTAVAIL;
cb323159 3972 nd6log(info,
39236c6e 3973 "%s: invalid prefix length %d for %s, ignored\n",
cb323159 3974 __func__, pr->ndpr_plen, if_name(ifp));
39236c6e 3975 goto unlock1;
1c79356b
A
3976 }
3977
0a7de745
A
3978 bzero(&ifra, sizeof(ifra));
3979 strlcpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
9bccf70c 3980 ifra.ifra_addr.sin6_family = AF_INET6;
0a7de745 3981 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
39236c6e 3982
9bccf70c
A
3983 /* prefix */
3984 bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr,
0a7de745 3985 sizeof(ifra.ifra_addr.sin6_addr));
39236c6e 3986 in6_len2mask(&mask, pr->ndpr_plen);
9bccf70c
A
3987 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
3988 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
3989 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
3990 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
1c79356b 3991
3e170ce0 3992 ndi = ND_IFINFO(ifp);
39236c6e
A
3993 VERIFY(ndi->initialized);
3994 lck_mtx_lock(&ndi->lock);
3995
3996 notcga = nd6_send_opstate == ND6_SEND_OPMODE_DISABLED ||
3997 (ndi->flags & ND6_IFF_INSECURE) != 0;
3998
3999 lck_mtx_unlock(&ndi->lock);
39236c6e
A
4000 NDPR_UNLOCK(pr);
4001
d9a64523 4002 if (notcga && !is_clat46) {
39236c6e
A
4003 ia6 = in6ifa_ifpforlinklocal(ifp, 0);
4004 if (ia6 == NULL) {
4005 error = EADDRNOTAVAIL;
cb323159
A
4006 nd6log(info, "%s: no link-local address (%s)\n",
4007 __func__, if_name(ifp));
39236c6e
A
4008 goto done;
4009 }
4010
4011 IFA_LOCK(&ia6->ia_ifa);
4012 ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
4013 (ia6->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
4014 ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
4015 (ia6->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
4016 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
4017 (ia6->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
4018 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
4019 (ia6->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
4020 IFA_UNLOCK(&ia6->ia_ifa);
4021 IFA_REMREF(&ia6->ia_ifa);
4022 ia6 = NULL;
4023 } else {
39037602 4024 struct in6_cga_prepare local_cga_prepare;
f427ee49
A
4025 struct in6_cga_prepare *prepare_p;
4026
4027
4028 in6_cga_node_lock();
39037602
A
4029
4030 if (ndi->cga_initialized) {
4031 bcopy(&(ndi->local_cga_modifier),
4032 &(local_cga_prepare.cga_modifier),
4033 sizeof(local_cga_prepare.cga_modifier));
f427ee49 4034 prepare_p = &local_cga_prepare;
39037602 4035 } else {
f427ee49 4036 prepare_p = NULL;
39037602 4037 }
f427ee49
A
4038 error = in6_cga_generate(prepare_p, collision_count,
4039 &ifra.ifra_addr.sin6_addr, ifp);
39236c6e 4040 in6_cga_node_unlock();
d9a64523 4041 if (error == 0) {
39236c6e 4042 ifra.ifra_flags |= IN6_IFF_SECURED;
0a7de745 4043 if (is_clat46) {
d9a64523 4044 ifra.ifra_flags |= IN6_IFF_CLAT46;
0a7de745 4045 }
d9a64523 4046 } else {
0a7de745 4047 if (!is_clat46) {
cb323159
A
4048 nd6log(error, "%s: no CGA available (%s)\n",
4049 __func__, if_name(ifp));
0a7de745 4050 } else {
cb323159
A
4051 nd6log(error, "%s: no CLAT46 available (%s)\n",
4052 __func__, if_name(ifp));
0a7de745 4053 }
39236c6e
A
4054 goto done;
4055 }
4056 }
4057
4058 VERIFY(ia6 == NULL);
6d2010ae 4059
9bccf70c 4060 /* new prefix mask. */
0a7de745 4061 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
9bccf70c
A
4062 ifra.ifra_prefixmask.sin6_family = AF_INET6;
4063 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr,
0a7de745 4064 sizeof(ifra.ifra_prefixmask.sin6_addr));
1c79356b 4065
6d2010ae 4066 /* lifetimes. */
9bccf70c
A
4067 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
4068 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
1c79356b 4069
39236c6e 4070 /* address flags */
9bccf70c 4071 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
6d2010ae 4072
9bccf70c 4073 /*
6d2010ae
A
4074 * Make sure that we do not have this address already. This should
4075 * usually not happen, but we can still see this case, e.g., if we
4076 * have manually configured the exact address to be configured.
9bccf70c 4077 */
39236c6e
A
4078 if ((ia6 = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr))
4079 != NULL) {
d9a64523 4080 error = EEXIST;
39236c6e
A
4081 IFA_REMREF(&ia6->ia_ifa);
4082 ia6 = NULL;
4083
6d2010ae 4084 /* this should be rare enough to make an explicit log */
39236c6e
A
4085 log(LOG_INFO, "%s: %s is already configured!\n",
4086 __func__, ip6_sprintf(&ifra.ifra_addr.sin6_addr));
4087 goto done;
6d2010ae 4088 }
9bccf70c
A
4089
4090 /*
6d2010ae
A
4091 * Allocate ifaddr structure, link into chain, etc.
4092 * If we are going to create a new address upon receiving a multicasted
4093 * RA, we need to impose a random delay before starting DAD.
39236c6e 4094 * [RFC 4862, Section 5.4.2]
9bccf70c 4095 */
39236c6e 4096 ifaupdate = IN6_IFAUPDATE_NOWAIT;
0a7de745 4097 if (mcast) {
39236c6e 4098 ifaupdate |= IN6_IFAUPDATE_DADDELAY;
0a7de745 4099 }
39236c6e 4100 error = in6_update_ifa(ifp, &ifra, ifaupdate, &ia6);
6d2010ae 4101 if (error != 0) {
cb323159 4102 nd6log(error,
39236c6e
A
4103 "%s: failed to make ifaddr %s on %s (errno=%d)\n",
4104 __func__, ip6_sprintf(&ifra.ifra_addr.sin6_addr),
cb323159 4105 if_name(ifp), error);
39236c6e
A
4106 error = EADDRNOTAVAIL;
4107 goto done;
f427ee49
A
4108 } else {
4109 /* remember the collision count */
4110 ia6->ia6_cga_collision_count = collision_count;
1c79356b 4111 }
1c79356b 4112
39236c6e 4113 VERIFY(ia6 != NULL);
fe8ab488 4114 in6_post_msg(ifp, KEV_INET6_NEW_RTADV_ADDR, ia6, NULL);
39236c6e
A
4115 goto done;
4116
39236c6e
A
4117unlock1:
4118 NDPR_UNLOCK(pr);
1c79356b 4119
39236c6e
A
4120done:
4121 *errorp = error;
0a7de745 4122 return ia6;
9bccf70c 4123}
1c79356b 4124
0a7de745 4125#define IA6_NONCONST(i) ((struct in6_ifaddr *)(uintptr_t)(i))
6d2010ae 4126
9bccf70c 4127int
39236c6e 4128in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen)
9bccf70c
A
4129{
4130 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
b0d623f7 4131 struct in6_ifaddr *ia, *newia;
9bccf70c 4132 struct in6_aliasreq ifra;
39236c6e 4133 int i, error, ifaupdate;
0a7de745 4134 int trylimit = 3; /* XXX: adhoc value */
9bccf70c 4135 u_int32_t randid[2];
f427ee49 4136 uint32_t vltime0, pltime0;
39236c6e 4137 uint64_t timenow = net_uptime();
6d2010ae
A
4138 struct in6_addr addr;
4139 struct nd_prefix *ndpr;
91447636 4140
0a7de745
A
4141 bzero(&ifra, sizeof(ifra));
4142 strlcpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name));
6d2010ae 4143 IFA_LOCK(&IA6_NONCONST(ia0)->ia_ifa);
9bccf70c
A
4144 ifra.ifra_addr = ia0->ia_addr;
4145 /* copy prefix mask */
4146 ifra.ifra_prefixmask = ia0->ia_prefixmask;
4147 /* clear the old IFID */
4148 for (i = 0; i < 4; i++) {
4149 ifra.ifra_addr.sin6_addr.s6_addr32[i]
0a7de745 4150 &= ifra.ifra_prefixmask.sin6_addr.s6_addr32[i];
1c79356b 4151 }
6d2010ae
A
4152 addr = ia0->ia_addr.sin6_addr;
4153 IFA_UNLOCK(&IA6_NONCONST(ia0)->ia_ifa);
1c79356b 4154
6d2010ae 4155again:
39236c6e 4156 in6_iid_mktmp(ifp, (u_int8_t *)randid,
6d2010ae
A
4157 (const u_int8_t *)&addr.s6_addr[8], forcegen);
4158
4159 ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
4160 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
4161 ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
4162 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
1c79356b
A
4163
4164 /*
39236c6e 4165 * in6_iid_mktmp() quite likely provided a unique interface ID.
6d2010ae
A
4166 * However, we may still have a chance to see collision, because
4167 * there may be a time lag between generation of the ID and generation
4168 * of the address. So, we'll do one more sanity check.
1c79356b 4169 */
b0d623f7 4170 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr)) != NULL) {
6d2010ae 4171 IFA_REMREF(&ia->ia_ifa);
9bccf70c 4172 if (trylimit-- == 0) {
cb323159
A
4173 nd6log(info, "in6_tmpifadd: failed to find "
4174 "a unique random IFID\n");
0a7de745 4175 return EEXIST;
9bccf70c
A
4176 }
4177 forcegen = 1;
4178 goto again;
1c79356b 4179 }
1c79356b 4180
9bccf70c
A
4181 /*
4182 * The Valid Lifetime is the lower of the Valid Lifetime of the
39236c6e 4183 * public address or TEMP_VALID_LIFETIME.
9bccf70c 4184 * The Preferred Lifetime is the lower of the Preferred Lifetime
39236c6e
A
4185 * of the public address or TEMP_PREFERRED_LIFETIME -
4186 * DESYNC_FACTOR.
9bccf70c 4187 */
6d2010ae 4188 IFA_LOCK(&IA6_NONCONST(ia0)->ia_ifa);
39236c6e
A
4189 if (ia0->ia6_lifetime.ia6ti_vltime != ND6_INFINITE_LIFETIME) {
4190 vltime0 = IFA6_IS_INVALID(ia0, timenow) ? 0 :
4191 (ia0->ia6_lifetime.ia6ti_vltime -
f427ee49 4192 (uint32_t)(timenow - ia0->ia6_updatetime));
0a7de745 4193 if (vltime0 > ip6_temp_valid_lifetime) {
39236c6e 4194 vltime0 = ip6_temp_valid_lifetime;
0a7de745 4195 }
39236c6e 4196 } else {
9bccf70c 4197 vltime0 = ip6_temp_valid_lifetime;
39236c6e
A
4198 }
4199 if (ia0->ia6_lifetime.ia6ti_pltime != ND6_INFINITE_LIFETIME) {
4200 pltime0 = IFA6_IS_DEPRECATED(ia0, timenow) ? 0 :
4201 (ia0->ia6_lifetime.ia6ti_pltime -
f427ee49 4202 (uint32_t)(timenow - ia0->ia6_updatetime));
0a7de745 4203 if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor) {
39236c6e
A
4204 pltime0 = ip6_temp_preferred_lifetime -
4205 ip6_desync_factor;
0a7de745 4206 }
39236c6e 4207 } else {
9bccf70c 4208 pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor;
39236c6e 4209 }
9bccf70c
A
4210 ifra.ifra_lifetime.ia6t_vltime = vltime0;
4211 ifra.ifra_lifetime.ia6t_pltime = pltime0;
6d2010ae 4212 IFA_UNLOCK(&IA6_NONCONST(ia0)->ia_ifa);
9bccf70c
A
4213 /*
4214 * A temporary address is created only if this calculated Preferred
4215 * Lifetime is greater than REGEN_ADVANCE time units.
4216 */
0a7de745
A
4217 if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance) {
4218 return 0;
4219 }
1c79356b 4220
9bccf70c 4221 /* XXX: scope zone ID? */
1c79356b 4222
0a7de745 4223 ifra.ifra_flags |= (IN6_IFF_AUTOCONF | IN6_IFF_TEMPORARY);
1c79356b 4224
9bccf70c 4225 /* allocate ifaddr structure, link into chain, etc. */
39236c6e
A
4226 ifaupdate = IN6_IFAUPDATE_NOWAIT | IN6_IFAUPDATE_DADDELAY;
4227 error = in6_update_ifa(ifp, &ifra, ifaupdate, &newia);
4228 if (error != 0) {
cb323159 4229 nd6log(error, "in6_tmpifadd: failed to add address.\n");
0a7de745 4230 return error;
1c79356b 4231 }
39236c6e
A
4232 VERIFY(newia != NULL);
4233
6d2010ae
A
4234 IFA_LOCK(&IA6_NONCONST(ia0)->ia_ifa);
4235 ndpr = ia0->ia6_ndpr;
4236 if (ndpr == NULL) {
4237 /*
4238 * We lost the race with another thread that has purged
4239 * ia0 address; in this case, purge the tmp addr as well.
4240 */
cb323159 4241 nd6log(error, "in6_tmpifadd: no public address\n");
6d2010ae
A
4242 VERIFY(!(ia0->ia6_flags & IN6_IFF_AUTOCONF));
4243 IFA_UNLOCK(&IA6_NONCONST(ia0)->ia_ifa);
4244 in6_purgeaddr(&newia->ia_ifa);
4245 IFA_REMREF(&newia->ia_ifa);
0a7de745 4246 return EADDRNOTAVAIL;
6d2010ae 4247 }
0a7de745 4248 NDPR_ADDREF(ndpr); /* for us */
6d2010ae
A
4249 IFA_UNLOCK(&IA6_NONCONST(ia0)->ia_ifa);
4250 IFA_LOCK(&newia->ia_ifa);
4251 if (newia->ia6_ndpr != NULL) {
4252 NDPR_LOCK(newia->ia6_ndpr);
4253 VERIFY(newia->ia6_ndpr->ndpr_addrcnt != 0);
4254 newia->ia6_ndpr->ndpr_addrcnt--;
4255 NDPR_UNLOCK(newia->ia6_ndpr);
0a7de745 4256 NDPR_REMREF(newia->ia6_ndpr); /* release addr reference */
6d2010ae
A
4257 }
4258 newia->ia6_ndpr = ndpr;
4259 NDPR_LOCK(newia->ia6_ndpr);
4260 newia->ia6_ndpr->ndpr_addrcnt++;
4261 VERIFY(newia->ia6_ndpr->ndpr_addrcnt != 0);
f427ee49 4262 NDPR_ADDREF(newia->ia6_ndpr); /* for addr reference */
6d2010ae
A
4263 NDPR_UNLOCK(newia->ia6_ndpr);
4264 IFA_UNLOCK(&newia->ia_ifa);
55e303ae
A
4265 /*
4266 * A newly added address might affect the status of other addresses.
4267 * XXX: when the temporary address is generated with a new public
4268 * address, the onlink check is redundant. However, it would be safe
4269 * to do the check explicitly everywhere a new address is generated,
4270 * and, in fact, we surely need the check when we create a new
4271 * temporary address due to deprecation of an old temporary address.
4272 */
6d2010ae
A
4273 lck_mtx_lock(nd6_mutex);
4274 pfxlist_onlink_check();
2d21ac55 4275 lck_mtx_unlock(nd6_mutex);
6d2010ae
A
4276 IFA_REMREF(&newia->ia_ifa);
4277
4278 /* remove our reference */
4279 NDPR_REMREF(ndpr);
55e303ae 4280
0a7de745 4281 return 0;
6d2010ae
A
4282}
4283#undef IA6_NONCONST
1c79356b
A
4284
4285int
4286in6_init_prefix_ltimes(struct nd_prefix *ndpr)
4287{
39236c6e
A
4288 struct timeval caltime;
4289 u_int64_t timenow = net_uptime();
91447636 4290
6d2010ae
A
4291 NDPR_LOCK_ASSERT_HELD(ndpr);
4292
39236c6e
A
4293 getmicrotime(&caltime);
4294 ndpr->ndpr_base_calendartime = caltime.tv_sec;
4295 ndpr->ndpr_base_uptime = timenow;
4296
4297 /* check if preferred lifetime > valid lifetime. RFC 4862 5.5.3 (c) */
1c79356b 4298 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
cb323159 4299 nd6log(info, "in6_init_prefix_ltimes: preferred lifetime"
1c79356b 4300 "(%d) is greater than valid lifetime(%d)\n",
cb323159 4301 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime);
0a7de745 4302 return EINVAL;
1c79356b 4303 }
0a7de745 4304 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) {
1c79356b 4305 ndpr->ndpr_preferred = 0;
0a7de745 4306 } else {
39236c6e 4307 ndpr->ndpr_preferred = timenow + ndpr->ndpr_pltime;
0a7de745
A
4308 }
4309 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) {
1c79356b 4310 ndpr->ndpr_expire = 0;
0a7de745 4311 } else {
39236c6e 4312 ndpr->ndpr_expire = timenow + ndpr->ndpr_vltime;
0a7de745 4313 }
1c79356b 4314
0a7de745 4315 return 0;
1c79356b
A
4316}
4317
4318static void
39236c6e 4319in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
1c79356b 4320{
39236c6e
A
4321#pragma unused(new)
4322 uint64_t timenow = net_uptime();
91447636 4323
1c79356b 4324 /* Valid lifetime must not be updated unless explicitly specified. */
9bccf70c 4325 /* init ia6t_expire */
39236c6e 4326 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) {
9bccf70c 4327 lt6->ia6t_expire = 0;
39236c6e
A
4328 } else {
4329 lt6->ia6t_expire = timenow;
9bccf70c 4330 lt6->ia6t_expire += lt6->ia6t_vltime;
1c79356b
A
4331 }
4332
4333 /* init ia6t_preferred */
39236c6e 4334 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) {
1c79356b 4335 lt6->ia6t_preferred = 0;
39236c6e
A
4336 } else {
4337 lt6->ia6t_preferred = timenow;
1c79356b
A
4338 lt6->ia6t_preferred += lt6->ia6t_pltime;
4339 }
1c79356b
A
4340}
4341
4342/*
4343 * Delete all the routing table entries that use the specified gateway.
4344 * XXX: this function causes search through all entries of routing table, so
4345 * it shouldn't be called when acting as a router.
f427ee49
A
4346 *
4347 * This should really be working on entries that have a specific
4348 * parent route.
1c79356b
A
4349 */
4350void
91447636
A
4351rt6_flush(
4352 struct in6_addr *gateway,
4353 struct ifnet *ifp)
1c79356b
A
4354{
4355 struct radix_node_head *rnh = rt_tables[AF_INET6];
1c79356b
A
4356
4357 /* We'll care only link-local addresses */
4358 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
1c79356b
A
4359 return;
4360 }
b0d623f7 4361 lck_mtx_lock(rnh_lock);
1c79356b
A
4362 /* XXX: hack for KAME's link-local address kludge */
4363 gateway->s6_addr16[1] = htons(ifp->if_index);
4364
4365 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
b0d623f7 4366 lck_mtx_unlock(rnh_lock);
1c79356b
A
4367}
4368
4369static int
91447636
A
4370rt6_deleteroute(
4371 struct radix_node *rn,
4372 void *arg)
1c79356b 4373{
1c79356b
A
4374 struct rtentry *rt = (struct rtentry *)rn;
4375 struct in6_addr *gate = (struct in6_addr *)arg;
4376
5ba3f43e 4377 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_OWNED);
91447636 4378
b0d623f7
A
4379 RT_LOCK(rt);
4380 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) {
4381 RT_UNLOCK(rt);
0a7de745 4382 return 0;
b0d623f7 4383 }
1c79356b 4384
b0d623f7
A
4385 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
4386 RT_UNLOCK(rt);
0a7de745 4387 return 0;
b0d623f7 4388 }
9bccf70c
A
4389 /*
4390 * Do not delete a static route.
4391 * XXX: this seems to be a bit ad-hoc. Should we consider the
4392 * 'cloned' bit instead?
4393 */
b0d623f7
A
4394 if ((rt->rt_flags & RTF_STATIC) != 0) {
4395 RT_UNLOCK(rt);
0a7de745 4396 return 0;
b0d623f7 4397 }
1c79356b
A
4398 /*
4399 * We delete only host route. This means, in particular, we don't
4400 * delete default route.
4401 */
b0d623f7
A
4402 if ((rt->rt_flags & RTF_HOST) == 0) {
4403 RT_UNLOCK(rt);
0a7de745 4404 return 0;
b0d623f7 4405 }
1c79356b 4406
b0d623f7
A
4407 /*
4408 * Safe to drop rt_lock and use rt_key, rt_gateway, since holding
4409 * rnh_lock here prevents another thread from calling rt_setgate()
4410 * on this route.
4411 */
4412 RT_UNLOCK(rt);
0a7de745
A
4413 return rtrequest_locked(RTM_DELETE, rt_key(rt), rt->rt_gateway,
4414 rt_mask(rt), rt->rt_flags, 0);
1c79356b
A
4415}
4416
4417int
91447636
A
4418nd6_setdefaultiface(
4419 int ifindex)
1c79356b
A
4420{
4421 int error = 0;
b0d623f7 4422 ifnet_t def_ifp = NULL;
316670eb 4423
5ba3f43e 4424 LCK_MTX_ASSERT(nd6_mutex, LCK_MTX_ASSERT_NOTOWNED);
1c79356b 4425
b0d623f7
A
4426 ifnet_head_lock_shared();
4427 if (ifindex < 0 || if_index < ifindex) {
4428 ifnet_head_done();
0a7de745 4429 return EINVAL;
b0d623f7
A
4430 }
4431 def_ifp = ifindex2ifnet[ifindex];
4432 ifnet_head_done();
1c79356b 4433
91447636 4434 lck_mtx_lock(nd6_mutex);
1c79356b 4435 if (nd6_defifindex != ifindex) {
6d2010ae
A
4436 struct ifnet *odef_ifp = nd6_defifp;
4437
1c79356b 4438 nd6_defifindex = ifindex;
0a7de745 4439 if (nd6_defifindex > 0) {
b0d623f7 4440 nd6_defifp = def_ifp;
0a7de745 4441 } else {
1c79356b 4442 nd6_defifp = NULL;
0a7de745 4443 }
1c79356b 4444
0a7de745 4445 if (nd6_defifp != NULL) {
cb323159 4446 nd6log(info, "%s: is now the default "
6d2010ae 4447 "interface (was %s)\n", if_name(nd6_defifp),
cb323159 4448 odef_ifp != NULL ? if_name(odef_ifp) : "NONE");
0a7de745 4449 } else {
cb323159 4450 nd6log(info, "No default interface set\n");
0a7de745 4451 }
6d2010ae 4452
1c79356b
A
4453 /*
4454 * If the Default Router List is empty, install a route
4455 * to the specified interface as default or remove the default
4456 * route when the default interface becomes canceled.
4457 * The check for the queue is actually redundant, but
4458 * we do this here to avoid re-install the default route
4459 * if the list is NOT empty.
4460 */
39037602 4461 if (odef_ifp != NULL) {
f427ee49 4462 defrouter_select(odef_ifp, NULL);
39037602
A
4463 }
4464
4465 if (nd6_defifp != NULL) {
f427ee49 4466 defrouter_select(nd6_defifp, NULL);
6d2010ae
A
4467 nd6_prefix_sync(nd6_defifp);
4468 }
9bccf70c 4469
f427ee49
A
4470 /*
4471 * XXX For now we managed RTI routes as un-scoped.
4472 * Therefore we ignore the change in nd6_defifindex
4473 * for RTI routes for now.
4474 */
9bccf70c 4475 /*
fe8ab488 4476 * Our current implementation assumes one-to-one mapping between
9bccf70c
A
4477 * interfaces and links, so it would be natural to use the
4478 * default interface as the default link.
4479 */
4480 scope6_setdefault(nd6_defifp);
1c79356b 4481 }
91447636 4482 lck_mtx_unlock(nd6_mutex);
0a7de745 4483 return error;
1c79356b 4484}