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