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