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