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