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