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