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