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