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