1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * 3. Neither the name of the project nor the names of its contributors |
---|
16 | * may be used to endorse or promote products derived from this software |
---|
17 | * without specific prior written permission. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | * |
---|
31 | * $KAME: in6_src.c,v 1.132 2003/08/26 04:42:27 keiichi Exp $ |
---|
32 | */ |
---|
33 | |
---|
34 | /*- |
---|
35 | * Copyright (c) 1982, 1986, 1991, 1993 |
---|
36 | * The Regents of the University of California. All rights reserved. |
---|
37 | * |
---|
38 | * Redistribution and use in source and binary forms, with or without |
---|
39 | * modification, are permitted provided that the following conditions |
---|
40 | * are met: |
---|
41 | * 1. Redistributions of source code must retain the above copyright |
---|
42 | * notice, this list of conditions and the following disclaimer. |
---|
43 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
44 | * notice, this list of conditions and the following disclaimer in the |
---|
45 | * documentation and/or other materials provided with the distribution. |
---|
46 | * 4. Neither the name of the University nor the names of its contributors |
---|
47 | * may be used to endorse or promote products derived from this software |
---|
48 | * without specific prior written permission. |
---|
49 | * |
---|
50 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
51 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
52 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
53 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
54 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
55 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
56 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
57 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
58 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
59 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
60 | * SUCH DAMAGE. |
---|
61 | * |
---|
62 | * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94 |
---|
63 | */ |
---|
64 | |
---|
65 | #include <sys/cdefs.h> |
---|
66 | __FBSDID("$FreeBSD$"); |
---|
67 | |
---|
68 | #include <rtems/bsd/local/opt_inet.h> |
---|
69 | #include <rtems/bsd/local/opt_inet6.h> |
---|
70 | #include <rtems/bsd/local/opt_mpath.h> |
---|
71 | |
---|
72 | #include <rtems/bsd/sys/param.h> |
---|
73 | #include <sys/systm.h> |
---|
74 | #include <rtems/bsd/sys/lock.h> |
---|
75 | #include <sys/malloc.h> |
---|
76 | #include <sys/mbuf.h> |
---|
77 | #include <sys/priv.h> |
---|
78 | #include <sys/protosw.h> |
---|
79 | #include <sys/socket.h> |
---|
80 | #include <sys/socketvar.h> |
---|
81 | #include <sys/sockio.h> |
---|
82 | #include <sys/sysctl.h> |
---|
83 | #include <rtems/bsd/sys/errno.h> |
---|
84 | #include <sys/time.h> |
---|
85 | #include <sys/jail.h> |
---|
86 | #include <sys/kernel.h> |
---|
87 | #include <sys/sx.h> |
---|
88 | |
---|
89 | #include <net/if.h> |
---|
90 | #include <net/if_dl.h> |
---|
91 | #include <net/route.h> |
---|
92 | #include <net/if_llatbl.h> |
---|
93 | #ifdef RADIX_MPATH |
---|
94 | #include <net/radix_mpath.h> |
---|
95 | #endif |
---|
96 | |
---|
97 | #include <netinet/in.h> |
---|
98 | #include <netinet/in_var.h> |
---|
99 | #include <netinet/in_systm.h> |
---|
100 | #include <netinet/ip.h> |
---|
101 | #include <netinet/in_pcb.h> |
---|
102 | #include <netinet/ip_var.h> |
---|
103 | #include <netinet/udp.h> |
---|
104 | #include <netinet/udp_var.h> |
---|
105 | |
---|
106 | #include <netinet6/in6_var.h> |
---|
107 | #include <netinet/ip6.h> |
---|
108 | #include <netinet6/in6_pcb.h> |
---|
109 | #include <netinet6/ip6_var.h> |
---|
110 | #include <netinet6/scope6_var.h> |
---|
111 | #include <netinet6/nd6.h> |
---|
112 | |
---|
113 | static struct mtx addrsel_lock; |
---|
114 | #define ADDRSEL_LOCK_INIT() mtx_init(&addrsel_lock, "addrsel_lock", NULL, MTX_DEF) |
---|
115 | #define ADDRSEL_LOCK() mtx_lock(&addrsel_lock) |
---|
116 | #define ADDRSEL_UNLOCK() mtx_unlock(&addrsel_lock) |
---|
117 | #define ADDRSEL_LOCK_ASSERT() mtx_assert(&addrsel_lock, MA_OWNED) |
---|
118 | |
---|
119 | static struct sx addrsel_sxlock; |
---|
120 | #define ADDRSEL_SXLOCK_INIT() sx_init(&addrsel_sxlock, "addrsel_sxlock") |
---|
121 | #define ADDRSEL_SLOCK() sx_slock(&addrsel_sxlock) |
---|
122 | #define ADDRSEL_SUNLOCK() sx_sunlock(&addrsel_sxlock) |
---|
123 | #define ADDRSEL_XLOCK() sx_xlock(&addrsel_sxlock) |
---|
124 | #define ADDRSEL_XUNLOCK() sx_xunlock(&addrsel_sxlock) |
---|
125 | |
---|
126 | #define ADDR_LABEL_NOTAPP (-1) |
---|
127 | static VNET_DEFINE(struct in6_addrpolicy, defaultaddrpolicy); |
---|
128 | #define V_defaultaddrpolicy VNET(defaultaddrpolicy) |
---|
129 | |
---|
130 | VNET_DEFINE(int, ip6_prefer_tempaddr) = 0; |
---|
131 | |
---|
132 | static int selectroute(struct sockaddr_in6 *, struct ip6_pktopts *, |
---|
133 | struct ip6_moptions *, struct route_in6 *, struct ifnet **, |
---|
134 | struct rtentry **, int, u_int); |
---|
135 | static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *, |
---|
136 | struct ip6_moptions *, struct route_in6 *ro, struct ifnet **, |
---|
137 | struct ifnet *, u_int); |
---|
138 | |
---|
139 | static struct in6_addrpolicy *lookup_addrsel_policy(struct sockaddr_in6 *); |
---|
140 | |
---|
141 | static void init_policy_queue(void); |
---|
142 | static int add_addrsel_policyent(struct in6_addrpolicy *); |
---|
143 | static int delete_addrsel_policyent(struct in6_addrpolicy *); |
---|
144 | static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *), |
---|
145 | void *); |
---|
146 | static int dump_addrsel_policyent(struct in6_addrpolicy *, void *); |
---|
147 | static struct in6_addrpolicy *match_addrsel_policy(struct sockaddr_in6 *); |
---|
148 | |
---|
149 | /* |
---|
150 | * Return an IPv6 address, which is the most appropriate for a given |
---|
151 | * destination and user specified options. |
---|
152 | * If necessary, this function lookups the routing table and returns |
---|
153 | * an entry to the caller for later use. |
---|
154 | */ |
---|
155 | #define REPLACE(r) do {\ |
---|
156 | IP6STAT_INC(ip6s_sources_rule[(r)]); \ |
---|
157 | rule = (r); \ |
---|
158 | /* { \ |
---|
159 | char ip6buf[INET6_ADDRSTRLEN], ip6b[INET6_ADDRSTRLEN]; \ |
---|
160 | printf("in6_selectsrc: replace %s with %s by %d\n", ia_best ? ip6_sprintf(ip6buf, &ia_best->ia_addr.sin6_addr) : "none", ip6_sprintf(ip6b, &ia->ia_addr.sin6_addr), (r)); \ |
---|
161 | } */ \ |
---|
162 | goto replace; \ |
---|
163 | } while(0) |
---|
164 | #define NEXT(r) do {\ |
---|
165 | /* { \ |
---|
166 | char ip6buf[INET6_ADDRSTRLEN], ip6b[INET6_ADDRSTRLEN]; \ |
---|
167 | printf("in6_selectsrc: keep %s against %s by %d\n", ia_best ? ip6_sprintf(ip6buf, &ia_best->ia_addr.sin6_addr) : "none", ip6_sprintf(ip6b, &ia->ia_addr.sin6_addr), (r)); \ |
---|
168 | } */ \ |
---|
169 | goto next; /* XXX: we can't use 'continue' here */ \ |
---|
170 | } while(0) |
---|
171 | #define BREAK(r) do { \ |
---|
172 | IP6STAT_INC(ip6s_sources_rule[(r)]); \ |
---|
173 | rule = (r); \ |
---|
174 | goto out; /* XXX: we can't use 'break' here */ \ |
---|
175 | } while(0) |
---|
176 | |
---|
177 | int |
---|
178 | in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, |
---|
179 | struct inpcb *inp, struct route_in6 *ro, struct ucred *cred, |
---|
180 | struct ifnet **ifpp, struct in6_addr *srcp) |
---|
181 | { |
---|
182 | struct in6_addr dst, tmp; |
---|
183 | struct ifnet *ifp = NULL, *oifp = NULL; |
---|
184 | struct in6_ifaddr *ia = NULL, *ia_best = NULL; |
---|
185 | struct in6_pktinfo *pi = NULL; |
---|
186 | int dst_scope = -1, best_scope = -1, best_matchlen = -1; |
---|
187 | struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL; |
---|
188 | u_int32_t odstzone; |
---|
189 | int prefer_tempaddr; |
---|
190 | int error, rule; |
---|
191 | struct ip6_moptions *mopts; |
---|
192 | |
---|
193 | KASSERT(srcp != NULL, ("%s: srcp is NULL", __func__)); |
---|
194 | |
---|
195 | dst = dstsock->sin6_addr; /* make a copy for local operation */ |
---|
196 | if (ifpp) { |
---|
197 | /* |
---|
198 | * Save a possibly passed in ifp for in6_selectsrc. Only |
---|
199 | * neighbor discovery code should use this feature, where |
---|
200 | * we may know the interface but not the FIB number holding |
---|
201 | * the connected subnet in case someone deleted it from the |
---|
202 | * default FIB and we need to check the interface. |
---|
203 | */ |
---|
204 | if (*ifpp != NULL) |
---|
205 | oifp = *ifpp; |
---|
206 | *ifpp = NULL; |
---|
207 | } |
---|
208 | |
---|
209 | if (inp != NULL) { |
---|
210 | INP_LOCK_ASSERT(inp); |
---|
211 | mopts = inp->in6p_moptions; |
---|
212 | } else { |
---|
213 | mopts = NULL; |
---|
214 | } |
---|
215 | |
---|
216 | /* |
---|
217 | * If the source address is explicitly specified by the caller, |
---|
218 | * check if the requested source address is indeed a unicast address |
---|
219 | * assigned to the node, and can be used as the packet's source |
---|
220 | * address. If everything is okay, use the address as source. |
---|
221 | */ |
---|
222 | if (opts && (pi = opts->ip6po_pktinfo) && |
---|
223 | !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) { |
---|
224 | struct sockaddr_in6 srcsock; |
---|
225 | struct in6_ifaddr *ia6; |
---|
226 | |
---|
227 | /* get the outgoing interface */ |
---|
228 | if ((error = in6_selectif(dstsock, opts, mopts, ro, &ifp, oifp, |
---|
229 | (inp != NULL) ? inp->inp_inc.inc_fibnum : RT_DEFAULT_FIB)) |
---|
230 | != 0) |
---|
231 | return (error); |
---|
232 | |
---|
233 | /* |
---|
234 | * determine the appropriate zone id of the source based on |
---|
235 | * the zone of the destination and the outgoing interface. |
---|
236 | * If the specified address is ambiguous wrt the scope zone, |
---|
237 | * the interface must be specified; otherwise, ifa_ifwithaddr() |
---|
238 | * will fail matching the address. |
---|
239 | */ |
---|
240 | bzero(&srcsock, sizeof(srcsock)); |
---|
241 | srcsock.sin6_family = AF_INET6; |
---|
242 | srcsock.sin6_len = sizeof(srcsock); |
---|
243 | srcsock.sin6_addr = pi->ipi6_addr; |
---|
244 | if (ifp) { |
---|
245 | error = in6_setscope(&srcsock.sin6_addr, ifp, NULL); |
---|
246 | if (error) |
---|
247 | return (error); |
---|
248 | } |
---|
249 | if (cred != NULL && (error = prison_local_ip6(cred, |
---|
250 | &srcsock.sin6_addr, (inp != NULL && |
---|
251 | (inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) |
---|
252 | return (error); |
---|
253 | |
---|
254 | ia6 = (struct in6_ifaddr *)ifa_ifwithaddr( |
---|
255 | (struct sockaddr *)&srcsock); |
---|
256 | if (ia6 == NULL || |
---|
257 | (ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))) { |
---|
258 | if (ia6 != NULL) |
---|
259 | ifa_free(&ia6->ia_ifa); |
---|
260 | return (EADDRNOTAVAIL); |
---|
261 | } |
---|
262 | pi->ipi6_addr = srcsock.sin6_addr; /* XXX: this overrides pi */ |
---|
263 | if (ifpp) |
---|
264 | *ifpp = ifp; |
---|
265 | bcopy(&ia6->ia_addr.sin6_addr, srcp, sizeof(*srcp)); |
---|
266 | ifa_free(&ia6->ia_ifa); |
---|
267 | return (0); |
---|
268 | } |
---|
269 | |
---|
270 | /* |
---|
271 | * Otherwise, if the socket has already bound the source, just use it. |
---|
272 | */ |
---|
273 | if (inp != NULL && !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { |
---|
274 | if (cred != NULL && |
---|
275 | (error = prison_local_ip6(cred, &inp->in6p_laddr, |
---|
276 | ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0) |
---|
277 | return (error); |
---|
278 | bcopy(&inp->in6p_laddr, srcp, sizeof(*srcp)); |
---|
279 | return (0); |
---|
280 | } |
---|
281 | |
---|
282 | /* |
---|
283 | * Bypass source address selection and use the primary jail IP |
---|
284 | * if requested. |
---|
285 | */ |
---|
286 | if (cred != NULL && !prison_saddrsel_ip6(cred, srcp)) |
---|
287 | return (0); |
---|
288 | |
---|
289 | /* |
---|
290 | * If the address is not specified, choose the best one based on |
---|
291 | * the outgoing interface and the destination address. |
---|
292 | */ |
---|
293 | /* get the outgoing interface */ |
---|
294 | if ((error = in6_selectif(dstsock, opts, mopts, ro, &ifp, oifp, |
---|
295 | (inp != NULL) ? inp->inp_inc.inc_fibnum : RT_DEFAULT_FIB)) != 0) |
---|
296 | return (error); |
---|
297 | |
---|
298 | #ifdef DIAGNOSTIC |
---|
299 | if (ifp == NULL) /* this should not happen */ |
---|
300 | panic("in6_selectsrc: NULL ifp"); |
---|
301 | #endif |
---|
302 | error = in6_setscope(&dst, ifp, &odstzone); |
---|
303 | if (error) |
---|
304 | return (error); |
---|
305 | |
---|
306 | rule = 0; |
---|
307 | IN6_IFADDR_RLOCK(); |
---|
308 | TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { |
---|
309 | int new_scope = -1, new_matchlen = -1; |
---|
310 | struct in6_addrpolicy *new_policy = NULL; |
---|
311 | u_int32_t srczone, osrczone, dstzone; |
---|
312 | struct in6_addr src; |
---|
313 | struct ifnet *ifp1 = ia->ia_ifp; |
---|
314 | |
---|
315 | /* |
---|
316 | * We'll never take an address that breaks the scope zone |
---|
317 | * of the destination. We also skip an address if its zone |
---|
318 | * does not contain the outgoing interface. |
---|
319 | * XXX: we should probably use sin6_scope_id here. |
---|
320 | */ |
---|
321 | if (in6_setscope(&dst, ifp1, &dstzone) || |
---|
322 | odstzone != dstzone) { |
---|
323 | continue; |
---|
324 | } |
---|
325 | src = ia->ia_addr.sin6_addr; |
---|
326 | if (in6_setscope(&src, ifp, &osrczone) || |
---|
327 | in6_setscope(&src, ifp1, &srczone) || |
---|
328 | osrczone != srczone) { |
---|
329 | continue; |
---|
330 | } |
---|
331 | |
---|
332 | /* avoid unusable addresses */ |
---|
333 | if ((ia->ia6_flags & |
---|
334 | (IN6_IFF_NOTREADY | IN6_IFF_ANYCAST | IN6_IFF_DETACHED))) { |
---|
335 | continue; |
---|
336 | } |
---|
337 | if (!V_ip6_use_deprecated && IFA6_IS_DEPRECATED(ia)) |
---|
338 | continue; |
---|
339 | |
---|
340 | /* If jailed only take addresses of the jail into account. */ |
---|
341 | if (cred != NULL && |
---|
342 | prison_check_ip6(cred, &ia->ia_addr.sin6_addr) != 0) |
---|
343 | continue; |
---|
344 | |
---|
345 | /* Rule 1: Prefer same address */ |
---|
346 | if (IN6_ARE_ADDR_EQUAL(&dst, &ia->ia_addr.sin6_addr)) { |
---|
347 | ia_best = ia; |
---|
348 | BREAK(1); /* there should be no better candidate */ |
---|
349 | } |
---|
350 | |
---|
351 | if (ia_best == NULL) |
---|
352 | REPLACE(0); |
---|
353 | |
---|
354 | /* Rule 2: Prefer appropriate scope */ |
---|
355 | if (dst_scope < 0) |
---|
356 | dst_scope = in6_addrscope(&dst); |
---|
357 | new_scope = in6_addrscope(&ia->ia_addr.sin6_addr); |
---|
358 | if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) { |
---|
359 | if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0) |
---|
360 | REPLACE(2); |
---|
361 | NEXT(2); |
---|
362 | } else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) { |
---|
363 | if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0) |
---|
364 | NEXT(2); |
---|
365 | REPLACE(2); |
---|
366 | } |
---|
367 | |
---|
368 | /* |
---|
369 | * Rule 3: Avoid deprecated addresses. Note that the case of |
---|
370 | * !ip6_use_deprecated is already rejected above. |
---|
371 | */ |
---|
372 | if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia)) |
---|
373 | NEXT(3); |
---|
374 | if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia)) |
---|
375 | REPLACE(3); |
---|
376 | |
---|
377 | /* Rule 4: Prefer home addresses */ |
---|
378 | /* |
---|
379 | * XXX: This is a TODO. We should probably merge the MIP6 |
---|
380 | * case above. |
---|
381 | */ |
---|
382 | |
---|
383 | /* Rule 5: Prefer outgoing interface */ |
---|
384 | if (!(ND_IFINFO(ifp)->flags & ND6_IFF_NO_PREFER_IFACE)) { |
---|
385 | if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp) |
---|
386 | NEXT(5); |
---|
387 | if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp) |
---|
388 | REPLACE(5); |
---|
389 | } |
---|
390 | |
---|
391 | /* |
---|
392 | * Rule 6: Prefer matching label |
---|
393 | * Note that best_policy should be non-NULL here. |
---|
394 | */ |
---|
395 | if (dst_policy == NULL) |
---|
396 | dst_policy = lookup_addrsel_policy(dstsock); |
---|
397 | if (dst_policy->label != ADDR_LABEL_NOTAPP) { |
---|
398 | new_policy = lookup_addrsel_policy(&ia->ia_addr); |
---|
399 | if (dst_policy->label == best_policy->label && |
---|
400 | dst_policy->label != new_policy->label) |
---|
401 | NEXT(6); |
---|
402 | if (dst_policy->label != best_policy->label && |
---|
403 | dst_policy->label == new_policy->label) |
---|
404 | REPLACE(6); |
---|
405 | } |
---|
406 | |
---|
407 | /* |
---|
408 | * Rule 7: Prefer public addresses. |
---|
409 | * We allow users to reverse the logic by configuring |
---|
410 | * a sysctl variable, so that privacy conscious users can |
---|
411 | * always prefer temporary addresses. |
---|
412 | */ |
---|
413 | if (opts == NULL || |
---|
414 | opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) { |
---|
415 | prefer_tempaddr = V_ip6_prefer_tempaddr; |
---|
416 | } else if (opts->ip6po_prefer_tempaddr == |
---|
417 | IP6PO_TEMPADDR_NOTPREFER) { |
---|
418 | prefer_tempaddr = 0; |
---|
419 | } else |
---|
420 | prefer_tempaddr = 1; |
---|
421 | if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) && |
---|
422 | (ia->ia6_flags & IN6_IFF_TEMPORARY)) { |
---|
423 | if (prefer_tempaddr) |
---|
424 | REPLACE(7); |
---|
425 | else |
---|
426 | NEXT(7); |
---|
427 | } |
---|
428 | if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) && |
---|
429 | !(ia->ia6_flags & IN6_IFF_TEMPORARY)) { |
---|
430 | if (prefer_tempaddr) |
---|
431 | NEXT(7); |
---|
432 | else |
---|
433 | REPLACE(7); |
---|
434 | } |
---|
435 | |
---|
436 | /* |
---|
437 | * Rule 8: prefer addresses on alive interfaces. |
---|
438 | * This is a KAME specific rule. |
---|
439 | */ |
---|
440 | if ((ia_best->ia_ifp->if_flags & IFF_UP) && |
---|
441 | !(ia->ia_ifp->if_flags & IFF_UP)) |
---|
442 | NEXT(8); |
---|
443 | if (!(ia_best->ia_ifp->if_flags & IFF_UP) && |
---|
444 | (ia->ia_ifp->if_flags & IFF_UP)) |
---|
445 | REPLACE(8); |
---|
446 | |
---|
447 | /* |
---|
448 | * Rule 10: prefer address with `prefer_source' flag. |
---|
449 | */ |
---|
450 | if ((ia_best->ia6_flags & IN6_IFF_PREFER_SOURCE) == 0 && |
---|
451 | (ia->ia6_flags & IN6_IFF_PREFER_SOURCE) != 0) |
---|
452 | REPLACE(10); |
---|
453 | if ((ia_best->ia6_flags & IN6_IFF_PREFER_SOURCE) != 0 && |
---|
454 | (ia->ia6_flags & IN6_IFF_PREFER_SOURCE) == 0) |
---|
455 | NEXT(10); |
---|
456 | |
---|
457 | /* |
---|
458 | * Rule 14: Use longest matching prefix. |
---|
459 | * Note: in the address selection draft, this rule is |
---|
460 | * documented as "Rule 8". However, since it is also |
---|
461 | * documented that this rule can be overridden, we assign |
---|
462 | * a large number so that it is easy to assign smaller numbers |
---|
463 | * to more preferred rules. |
---|
464 | */ |
---|
465 | new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, &dst); |
---|
466 | if (best_matchlen < new_matchlen) |
---|
467 | REPLACE(14); |
---|
468 | if (new_matchlen < best_matchlen) |
---|
469 | NEXT(14); |
---|
470 | |
---|
471 | /* Rule 15 is reserved. */ |
---|
472 | |
---|
473 | /* |
---|
474 | * Last resort: just keep the current candidate. |
---|
475 | * Or, do we need more rules? |
---|
476 | */ |
---|
477 | continue; |
---|
478 | |
---|
479 | replace: |
---|
480 | ia_best = ia; |
---|
481 | best_scope = (new_scope >= 0 ? new_scope : |
---|
482 | in6_addrscope(&ia_best->ia_addr.sin6_addr)); |
---|
483 | best_policy = (new_policy ? new_policy : |
---|
484 | lookup_addrsel_policy(&ia_best->ia_addr)); |
---|
485 | best_matchlen = (new_matchlen >= 0 ? new_matchlen : |
---|
486 | in6_matchlen(&ia_best->ia_addr.sin6_addr, |
---|
487 | &dst)); |
---|
488 | |
---|
489 | next: |
---|
490 | continue; |
---|
491 | |
---|
492 | out: |
---|
493 | break; |
---|
494 | } |
---|
495 | |
---|
496 | if ((ia = ia_best) == NULL) { |
---|
497 | IN6_IFADDR_RUNLOCK(); |
---|
498 | IP6STAT_INC(ip6s_sources_none); |
---|
499 | return (EADDRNOTAVAIL); |
---|
500 | } |
---|
501 | |
---|
502 | /* |
---|
503 | * At this point at least one of the addresses belonged to the jail |
---|
504 | * but it could still be, that we want to further restrict it, e.g. |
---|
505 | * theoratically IN6_IS_ADDR_LOOPBACK. |
---|
506 | * It must not be IN6_IS_ADDR_UNSPECIFIED anymore. |
---|
507 | * prison_local_ip6() will fix an IN6_IS_ADDR_LOOPBACK but should |
---|
508 | * let all others previously selected pass. |
---|
509 | * Use tmp to not change ::1 on lo0 to the primary jail address. |
---|
510 | */ |
---|
511 | tmp = ia->ia_addr.sin6_addr; |
---|
512 | if (cred != NULL && prison_local_ip6(cred, &tmp, (inp != NULL && |
---|
513 | (inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)) != 0) { |
---|
514 | IN6_IFADDR_RUNLOCK(); |
---|
515 | IP6STAT_INC(ip6s_sources_none); |
---|
516 | return (EADDRNOTAVAIL); |
---|
517 | } |
---|
518 | |
---|
519 | if (ifpp) |
---|
520 | *ifpp = ifp; |
---|
521 | |
---|
522 | bcopy(&tmp, srcp, sizeof(*srcp)); |
---|
523 | if (ia->ia_ifp == ifp) |
---|
524 | IP6STAT_INC(ip6s_sources_sameif[best_scope]); |
---|
525 | else |
---|
526 | IP6STAT_INC(ip6s_sources_otherif[best_scope]); |
---|
527 | if (dst_scope == best_scope) |
---|
528 | IP6STAT_INC(ip6s_sources_samescope[best_scope]); |
---|
529 | else |
---|
530 | IP6STAT_INC(ip6s_sources_otherscope[best_scope]); |
---|
531 | if (IFA6_IS_DEPRECATED(ia)) |
---|
532 | IP6STAT_INC(ip6s_sources_deprecated[best_scope]); |
---|
533 | IN6_IFADDR_RUNLOCK(); |
---|
534 | return (0); |
---|
535 | } |
---|
536 | |
---|
537 | /* |
---|
538 | * clone - meaningful only for bsdi and freebsd |
---|
539 | */ |
---|
540 | static int |
---|
541 | selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, |
---|
542 | struct ip6_moptions *mopts, struct route_in6 *ro, |
---|
543 | struct ifnet **retifp, struct rtentry **retrt, int norouteok, u_int fibnum) |
---|
544 | { |
---|
545 | int error = 0; |
---|
546 | struct ifnet *ifp = NULL; |
---|
547 | struct rtentry *rt = NULL; |
---|
548 | struct sockaddr_in6 *sin6_next; |
---|
549 | struct in6_pktinfo *pi = NULL; |
---|
550 | struct in6_addr *dst = &dstsock->sin6_addr; |
---|
551 | #if 0 |
---|
552 | char ip6buf[INET6_ADDRSTRLEN]; |
---|
553 | |
---|
554 | if (dstsock->sin6_addr.s6_addr32[0] == 0 && |
---|
555 | dstsock->sin6_addr.s6_addr32[1] == 0 && |
---|
556 | !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) { |
---|
557 | printf("in6_selectroute: strange destination %s\n", |
---|
558 | ip6_sprintf(ip6buf, &dstsock->sin6_addr)); |
---|
559 | } else { |
---|
560 | printf("in6_selectroute: destination = %s%%%d\n", |
---|
561 | ip6_sprintf(ip6buf, &dstsock->sin6_addr), |
---|
562 | dstsock->sin6_scope_id); /* for debug */ |
---|
563 | } |
---|
564 | #endif |
---|
565 | |
---|
566 | /* If the caller specify the outgoing interface explicitly, use it. */ |
---|
567 | if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) { |
---|
568 | /* XXX boundary check is assumed to be already done. */ |
---|
569 | ifp = ifnet_byindex(pi->ipi6_ifindex); |
---|
570 | if (ifp != NULL && |
---|
571 | (norouteok || retrt == NULL || |
---|
572 | IN6_IS_ADDR_MULTICAST(dst))) { |
---|
573 | /* |
---|
574 | * we do not have to check or get the route for |
---|
575 | * multicast. |
---|
576 | */ |
---|
577 | goto done; |
---|
578 | } else |
---|
579 | goto getroute; |
---|
580 | } |
---|
581 | |
---|
582 | /* |
---|
583 | * If the destination address is a multicast address and the outgoing |
---|
584 | * interface for the address is specified by the caller, use it. |
---|
585 | */ |
---|
586 | if (IN6_IS_ADDR_MULTICAST(dst) && |
---|
587 | mopts != NULL && (ifp = mopts->im6o_multicast_ifp) != NULL) { |
---|
588 | goto done; /* we do not need a route for multicast. */ |
---|
589 | } |
---|
590 | |
---|
591 | getroute: |
---|
592 | /* |
---|
593 | * If the next hop address for the packet is specified by the caller, |
---|
594 | * use it as the gateway. |
---|
595 | */ |
---|
596 | if (opts && opts->ip6po_nexthop) { |
---|
597 | struct route_in6 *ron; |
---|
598 | struct llentry *la; |
---|
599 | |
---|
600 | sin6_next = satosin6(opts->ip6po_nexthop); |
---|
601 | |
---|
602 | /* at this moment, we only support AF_INET6 next hops */ |
---|
603 | if (sin6_next->sin6_family != AF_INET6) { |
---|
604 | error = EAFNOSUPPORT; /* or should we proceed? */ |
---|
605 | goto done; |
---|
606 | } |
---|
607 | |
---|
608 | /* |
---|
609 | * If the next hop is an IPv6 address, then the node identified |
---|
610 | * by that address must be a neighbor of the sending host. |
---|
611 | */ |
---|
612 | ron = &opts->ip6po_nextroute; |
---|
613 | /* |
---|
614 | * XXX what do we do here? |
---|
615 | * PLZ to be fixing |
---|
616 | */ |
---|
617 | |
---|
618 | |
---|
619 | if (ron->ro_rt == NULL) { |
---|
620 | in6_rtalloc(ron, fibnum); /* multi path case? */ |
---|
621 | if (ron->ro_rt == NULL) { |
---|
622 | if (ron->ro_rt) { |
---|
623 | RTFREE(ron->ro_rt); |
---|
624 | ron->ro_rt = NULL; |
---|
625 | } |
---|
626 | error = EHOSTUNREACH; |
---|
627 | goto done; |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | rt = ron->ro_rt; |
---|
632 | ifp = rt->rt_ifp; |
---|
633 | IF_AFDATA_RLOCK(ifp); |
---|
634 | la = lla_lookup(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6_next->sin6_addr); |
---|
635 | IF_AFDATA_RUNLOCK(ifp); |
---|
636 | if (la != NULL) |
---|
637 | LLE_RUNLOCK(la); |
---|
638 | else { |
---|
639 | error = EHOSTUNREACH; |
---|
640 | goto done; |
---|
641 | } |
---|
642 | #if 0 |
---|
643 | if ((ron->ro_rt && |
---|
644 | (ron->ro_rt->rt_flags & (RTF_UP | RTF_LLINFO)) != |
---|
645 | (RTF_UP | RTF_LLINFO)) || |
---|
646 | !IN6_ARE_ADDR_EQUAL(&satosin6(&ron->ro_dst)->sin6_addr, |
---|
647 | &sin6_next->sin6_addr)) { |
---|
648 | if (ron->ro_rt) { |
---|
649 | RTFREE(ron->ro_rt); |
---|
650 | ron->ro_rt = NULL; |
---|
651 | } |
---|
652 | *satosin6(&ron->ro_dst) = *sin6_next; |
---|
653 | } |
---|
654 | if (ron->ro_rt == NULL) { |
---|
655 | in6_rtalloc(ron, fibnum); /* multi path case? */ |
---|
656 | if (ron->ro_rt == NULL || |
---|
657 | !(ron->ro_rt->rt_flags & RTF_LLINFO)) { |
---|
658 | if (ron->ro_rt) { |
---|
659 | RTFREE(ron->ro_rt); |
---|
660 | ron->ro_rt = NULL; |
---|
661 | } |
---|
662 | error = EHOSTUNREACH; |
---|
663 | goto done; |
---|
664 | } |
---|
665 | } |
---|
666 | #endif |
---|
667 | |
---|
668 | /* |
---|
669 | * When cloning is required, try to allocate a route to the |
---|
670 | * destination so that the caller can store path MTU |
---|
671 | * information. |
---|
672 | */ |
---|
673 | goto done; |
---|
674 | } |
---|
675 | |
---|
676 | /* |
---|
677 | * Use a cached route if it exists and is valid, else try to allocate |
---|
678 | * a new one. Note that we should check the address family of the |
---|
679 | * cached destination, in case of sharing the cache with IPv4. |
---|
680 | */ |
---|
681 | if (ro) { |
---|
682 | if (ro->ro_rt && |
---|
683 | (!(ro->ro_rt->rt_flags & RTF_UP) || |
---|
684 | ((struct sockaddr *)(&ro->ro_dst))->sa_family != AF_INET6 || |
---|
685 | !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, |
---|
686 | dst))) { |
---|
687 | RTFREE(ro->ro_rt); |
---|
688 | ro->ro_rt = (struct rtentry *)NULL; |
---|
689 | } |
---|
690 | if (ro->ro_rt == (struct rtentry *)NULL) { |
---|
691 | struct sockaddr_in6 *sa6; |
---|
692 | |
---|
693 | /* No route yet, so try to acquire one */ |
---|
694 | bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); |
---|
695 | sa6 = (struct sockaddr_in6 *)&ro->ro_dst; |
---|
696 | *sa6 = *dstsock; |
---|
697 | sa6->sin6_scope_id = 0; |
---|
698 | |
---|
699 | #ifdef RADIX_MPATH |
---|
700 | rtalloc_mpath_fib((struct route *)ro, |
---|
701 | ntohl(sa6->sin6_addr.s6_addr32[3]), fibnum); |
---|
702 | #else |
---|
703 | ro->ro_rt = in6_rtalloc1((struct sockaddr *) |
---|
704 | &ro->ro_dst, 0, 0UL, fibnum); |
---|
705 | if (ro->ro_rt) |
---|
706 | RT_UNLOCK(ro->ro_rt); |
---|
707 | #endif |
---|
708 | } |
---|
709 | |
---|
710 | /* |
---|
711 | * do not care about the result if we have the nexthop |
---|
712 | * explicitly specified. |
---|
713 | */ |
---|
714 | if (opts && opts->ip6po_nexthop) |
---|
715 | goto done; |
---|
716 | |
---|
717 | if (ro->ro_rt) { |
---|
718 | ifp = ro->ro_rt->rt_ifp; |
---|
719 | |
---|
720 | if (ifp == NULL) { /* can this really happen? */ |
---|
721 | RTFREE(ro->ro_rt); |
---|
722 | ro->ro_rt = NULL; |
---|
723 | } |
---|
724 | } |
---|
725 | if (ro->ro_rt == NULL) |
---|
726 | error = EHOSTUNREACH; |
---|
727 | rt = ro->ro_rt; |
---|
728 | |
---|
729 | /* |
---|
730 | * Check if the outgoing interface conflicts with |
---|
731 | * the interface specified by ipi6_ifindex (if specified). |
---|
732 | * Note that loopback interface is always okay. |
---|
733 | * (this may happen when we are sending a packet to one of |
---|
734 | * our own addresses.) |
---|
735 | */ |
---|
736 | if (ifp && opts && opts->ip6po_pktinfo && |
---|
737 | opts->ip6po_pktinfo->ipi6_ifindex) { |
---|
738 | if (!(ifp->if_flags & IFF_LOOPBACK) && |
---|
739 | ifp->if_index != |
---|
740 | opts->ip6po_pktinfo->ipi6_ifindex) { |
---|
741 | error = EHOSTUNREACH; |
---|
742 | goto done; |
---|
743 | } |
---|
744 | } |
---|
745 | } |
---|
746 | |
---|
747 | done: |
---|
748 | if (ifp == NULL && rt == NULL) { |
---|
749 | /* |
---|
750 | * This can happen if the caller did not pass a cached route |
---|
751 | * nor any other hints. We treat this case an error. |
---|
752 | */ |
---|
753 | error = EHOSTUNREACH; |
---|
754 | } |
---|
755 | if (error == EHOSTUNREACH) |
---|
756 | IP6STAT_INC(ip6s_noroute); |
---|
757 | |
---|
758 | if (retifp != NULL) { |
---|
759 | *retifp = ifp; |
---|
760 | |
---|
761 | /* |
---|
762 | * Adjust the "outgoing" interface. If we're going to loop |
---|
763 | * the packet back to ourselves, the ifp would be the loopback |
---|
764 | * interface. However, we'd rather know the interface associated |
---|
765 | * to the destination address (which should probably be one of |
---|
766 | * our own addresses.) |
---|
767 | */ |
---|
768 | if (rt) { |
---|
769 | if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) && |
---|
770 | (rt->rt_gateway->sa_family == AF_LINK)) |
---|
771 | *retifp = |
---|
772 | ifnet_byindex(((struct sockaddr_dl *) |
---|
773 | rt->rt_gateway)->sdl_index); |
---|
774 | } |
---|
775 | } |
---|
776 | |
---|
777 | if (retrt != NULL) |
---|
778 | *retrt = rt; /* rt may be NULL */ |
---|
779 | |
---|
780 | return (error); |
---|
781 | } |
---|
782 | |
---|
783 | static int |
---|
784 | in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, |
---|
785 | struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp, |
---|
786 | struct ifnet *oifp, u_int fibnum) |
---|
787 | { |
---|
788 | int error; |
---|
789 | struct route_in6 sro; |
---|
790 | struct rtentry *rt = NULL; |
---|
791 | |
---|
792 | KASSERT(retifp != NULL, ("%s: retifp is NULL", __func__)); |
---|
793 | |
---|
794 | if (ro == NULL) { |
---|
795 | bzero(&sro, sizeof(sro)); |
---|
796 | ro = &sro; |
---|
797 | } |
---|
798 | |
---|
799 | if ((error = selectroute(dstsock, opts, mopts, ro, retifp, |
---|
800 | &rt, 1, fibnum)) != 0) { |
---|
801 | if (ro == &sro && rt && rt == sro.ro_rt) |
---|
802 | RTFREE(rt); |
---|
803 | /* Help ND. See oifp comment in in6_selectsrc(). */ |
---|
804 | if (oifp != NULL && fibnum == RT_DEFAULT_FIB) { |
---|
805 | *retifp = oifp; |
---|
806 | error = 0; |
---|
807 | } |
---|
808 | return (error); |
---|
809 | } |
---|
810 | |
---|
811 | /* |
---|
812 | * do not use a rejected or black hole route. |
---|
813 | * XXX: this check should be done in the L2 output routine. |
---|
814 | * However, if we skipped this check here, we'd see the following |
---|
815 | * scenario: |
---|
816 | * - install a rejected route for a scoped address prefix |
---|
817 | * (like fe80::/10) |
---|
818 | * - send a packet to a destination that matches the scoped prefix, |
---|
819 | * with ambiguity about the scope zone. |
---|
820 | * - pick the outgoing interface from the route, and disambiguate the |
---|
821 | * scope zone with the interface. |
---|
822 | * - ip6_output() would try to get another route with the "new" |
---|
823 | * destination, which may be valid. |
---|
824 | * - we'd see no error on output. |
---|
825 | * Although this may not be very harmful, it should still be confusing. |
---|
826 | * We thus reject the case here. |
---|
827 | */ |
---|
828 | if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE))) { |
---|
829 | int flags = (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); |
---|
830 | |
---|
831 | if (ro == &sro && rt && rt == sro.ro_rt) |
---|
832 | RTFREE(rt); |
---|
833 | return (flags); |
---|
834 | } |
---|
835 | |
---|
836 | if (ro == &sro && rt && rt == sro.ro_rt) |
---|
837 | RTFREE(rt); |
---|
838 | return (0); |
---|
839 | } |
---|
840 | |
---|
841 | /* |
---|
842 | * Public wrapper function to selectroute(). |
---|
843 | * |
---|
844 | * XXX-BZ in6_selectroute() should and will grow the FIB argument. The |
---|
845 | * in6_selectroute_fib() function is only there for backward compat on stable. |
---|
846 | */ |
---|
847 | int |
---|
848 | in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, |
---|
849 | struct ip6_moptions *mopts, struct route_in6 *ro, |
---|
850 | struct ifnet **retifp, struct rtentry **retrt) |
---|
851 | { |
---|
852 | |
---|
853 | return (selectroute(dstsock, opts, mopts, ro, retifp, |
---|
854 | retrt, 0, RT_DEFAULT_FIB)); |
---|
855 | } |
---|
856 | |
---|
857 | #ifndef BURN_BRIDGES |
---|
858 | int |
---|
859 | in6_selectroute_fib(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts, |
---|
860 | struct ip6_moptions *mopts, struct route_in6 *ro, |
---|
861 | struct ifnet **retifp, struct rtentry **retrt, u_int fibnum) |
---|
862 | { |
---|
863 | |
---|
864 | return (selectroute(dstsock, opts, mopts, ro, retifp, |
---|
865 | retrt, 0, fibnum)); |
---|
866 | } |
---|
867 | #endif |
---|
868 | |
---|
869 | /* |
---|
870 | * Default hop limit selection. The precedence is as follows: |
---|
871 | * 1. Hoplimit value specified via ioctl. |
---|
872 | * 2. (If the outgoing interface is detected) the current |
---|
873 | * hop limit of the interface specified by router advertisement. |
---|
874 | * 3. The system default hoplimit. |
---|
875 | */ |
---|
876 | int |
---|
877 | in6_selecthlim(struct inpcb *in6p, struct ifnet *ifp) |
---|
878 | { |
---|
879 | |
---|
880 | if (in6p && in6p->in6p_hops >= 0) |
---|
881 | return (in6p->in6p_hops); |
---|
882 | else if (ifp) |
---|
883 | return (ND_IFINFO(ifp)->chlim); |
---|
884 | else if (in6p && !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { |
---|
885 | struct route_in6 ro6; |
---|
886 | struct ifnet *lifp; |
---|
887 | |
---|
888 | bzero(&ro6, sizeof(ro6)); |
---|
889 | ro6.ro_dst.sin6_family = AF_INET6; |
---|
890 | ro6.ro_dst.sin6_len = sizeof(struct sockaddr_in6); |
---|
891 | ro6.ro_dst.sin6_addr = in6p->in6p_faddr; |
---|
892 | in6_rtalloc(&ro6, in6p->inp_inc.inc_fibnum); |
---|
893 | if (ro6.ro_rt) { |
---|
894 | lifp = ro6.ro_rt->rt_ifp; |
---|
895 | RTFREE(ro6.ro_rt); |
---|
896 | if (lifp) |
---|
897 | return (ND_IFINFO(lifp)->chlim); |
---|
898 | } |
---|
899 | } |
---|
900 | return (V_ip6_defhlim); |
---|
901 | } |
---|
902 | |
---|
903 | /* |
---|
904 | * XXX: this is borrowed from in6_pcbbind(). If possible, we should |
---|
905 | * share this function by all *bsd*... |
---|
906 | */ |
---|
907 | int |
---|
908 | in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred) |
---|
909 | { |
---|
910 | struct socket *so = inp->inp_socket; |
---|
911 | u_int16_t lport = 0; |
---|
912 | int error, lookupflags = 0; |
---|
913 | #ifdef INVARIANTS |
---|
914 | struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; |
---|
915 | #endif |
---|
916 | |
---|
917 | INP_WLOCK_ASSERT(inp); |
---|
918 | INP_HASH_WLOCK_ASSERT(pcbinfo); |
---|
919 | |
---|
920 | error = prison_local_ip6(cred, laddr, |
---|
921 | ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)); |
---|
922 | if (error) |
---|
923 | return(error); |
---|
924 | |
---|
925 | /* XXX: this is redundant when called from in6_pcbbind */ |
---|
926 | if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) |
---|
927 | lookupflags = INPLOOKUP_WILDCARD; |
---|
928 | |
---|
929 | inp->inp_flags |= INP_ANONPORT; |
---|
930 | |
---|
931 | error = in_pcb_lport(inp, NULL, &lport, cred, lookupflags); |
---|
932 | if (error != 0) |
---|
933 | return (error); |
---|
934 | |
---|
935 | inp->inp_lport = lport; |
---|
936 | if (in_pcbinshash(inp) != 0) { |
---|
937 | inp->in6p_laddr = in6addr_any; |
---|
938 | inp->inp_lport = 0; |
---|
939 | return (EAGAIN); |
---|
940 | } |
---|
941 | |
---|
942 | return (0); |
---|
943 | } |
---|
944 | |
---|
945 | void |
---|
946 | addrsel_policy_init(void) |
---|
947 | { |
---|
948 | |
---|
949 | init_policy_queue(); |
---|
950 | |
---|
951 | /* initialize the "last resort" policy */ |
---|
952 | bzero(&V_defaultaddrpolicy, sizeof(V_defaultaddrpolicy)); |
---|
953 | V_defaultaddrpolicy.label = ADDR_LABEL_NOTAPP; |
---|
954 | |
---|
955 | if (!IS_DEFAULT_VNET(curvnet)) |
---|
956 | return; |
---|
957 | |
---|
958 | ADDRSEL_LOCK_INIT(); |
---|
959 | ADDRSEL_SXLOCK_INIT(); |
---|
960 | } |
---|
961 | |
---|
962 | static struct in6_addrpolicy * |
---|
963 | lookup_addrsel_policy(struct sockaddr_in6 *key) |
---|
964 | { |
---|
965 | struct in6_addrpolicy *match = NULL; |
---|
966 | |
---|
967 | ADDRSEL_LOCK(); |
---|
968 | match = match_addrsel_policy(key); |
---|
969 | |
---|
970 | if (match == NULL) |
---|
971 | match = &V_defaultaddrpolicy; |
---|
972 | else |
---|
973 | match->use++; |
---|
974 | ADDRSEL_UNLOCK(); |
---|
975 | |
---|
976 | return (match); |
---|
977 | } |
---|
978 | |
---|
979 | /* |
---|
980 | * Subroutines to manage the address selection policy table via sysctl. |
---|
981 | */ |
---|
982 | struct walkarg { |
---|
983 | struct sysctl_req *w_req; |
---|
984 | }; |
---|
985 | |
---|
986 | static int in6_src_sysctl(SYSCTL_HANDLER_ARGS); |
---|
987 | SYSCTL_DECL(_net_inet6_ip6); |
---|
988 | static SYSCTL_NODE(_net_inet6_ip6, IPV6CTL_ADDRCTLPOLICY, addrctlpolicy, |
---|
989 | CTLFLAG_RD, in6_src_sysctl, ""); |
---|
990 | |
---|
991 | static int |
---|
992 | in6_src_sysctl(SYSCTL_HANDLER_ARGS) |
---|
993 | { |
---|
994 | struct walkarg w; |
---|
995 | |
---|
996 | if (req->newptr) |
---|
997 | return EPERM; |
---|
998 | |
---|
999 | bzero(&w, sizeof(w)); |
---|
1000 | w.w_req = req; |
---|
1001 | |
---|
1002 | return (walk_addrsel_policy(dump_addrsel_policyent, &w)); |
---|
1003 | } |
---|
1004 | |
---|
1005 | int |
---|
1006 | in6_src_ioctl(u_long cmd, caddr_t data) |
---|
1007 | { |
---|
1008 | int i; |
---|
1009 | struct in6_addrpolicy ent0; |
---|
1010 | |
---|
1011 | if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY) |
---|
1012 | return (EOPNOTSUPP); /* check for safety */ |
---|
1013 | |
---|
1014 | ent0 = *(struct in6_addrpolicy *)data; |
---|
1015 | |
---|
1016 | if (ent0.label == ADDR_LABEL_NOTAPP) |
---|
1017 | return (EINVAL); |
---|
1018 | /* check if the prefix mask is consecutive. */ |
---|
1019 | if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0) |
---|
1020 | return (EINVAL); |
---|
1021 | /* clear trailing garbages (if any) of the prefix address. */ |
---|
1022 | for (i = 0; i < 4; i++) { |
---|
1023 | ent0.addr.sin6_addr.s6_addr32[i] &= |
---|
1024 | ent0.addrmask.sin6_addr.s6_addr32[i]; |
---|
1025 | } |
---|
1026 | ent0.use = 0; |
---|
1027 | |
---|
1028 | switch (cmd) { |
---|
1029 | case SIOCAADDRCTL_POLICY: |
---|
1030 | return (add_addrsel_policyent(&ent0)); |
---|
1031 | case SIOCDADDRCTL_POLICY: |
---|
1032 | return (delete_addrsel_policyent(&ent0)); |
---|
1033 | } |
---|
1034 | |
---|
1035 | return (0); /* XXX: compromise compilers */ |
---|
1036 | } |
---|
1037 | |
---|
1038 | /* |
---|
1039 | * The followings are implementation of the policy table using a |
---|
1040 | * simple tail queue. |
---|
1041 | * XXX such details should be hidden. |
---|
1042 | * XXX implementation using binary tree should be more efficient. |
---|
1043 | */ |
---|
1044 | struct addrsel_policyent { |
---|
1045 | TAILQ_ENTRY(addrsel_policyent) ape_entry; |
---|
1046 | struct in6_addrpolicy ape_policy; |
---|
1047 | }; |
---|
1048 | |
---|
1049 | TAILQ_HEAD(addrsel_policyhead, addrsel_policyent); |
---|
1050 | |
---|
1051 | static VNET_DEFINE(struct addrsel_policyhead, addrsel_policytab); |
---|
1052 | #define V_addrsel_policytab VNET(addrsel_policytab) |
---|
1053 | |
---|
1054 | static void |
---|
1055 | init_policy_queue(void) |
---|
1056 | { |
---|
1057 | |
---|
1058 | TAILQ_INIT(&V_addrsel_policytab); |
---|
1059 | } |
---|
1060 | |
---|
1061 | static int |
---|
1062 | add_addrsel_policyent(struct in6_addrpolicy *newpolicy) |
---|
1063 | { |
---|
1064 | struct addrsel_policyent *new, *pol; |
---|
1065 | |
---|
1066 | new = malloc(sizeof(*new), M_IFADDR, |
---|
1067 | M_WAITOK); |
---|
1068 | ADDRSEL_XLOCK(); |
---|
1069 | ADDRSEL_LOCK(); |
---|
1070 | |
---|
1071 | /* duplication check */ |
---|
1072 | TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) { |
---|
1073 | if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr, |
---|
1074 | &pol->ape_policy.addr.sin6_addr) && |
---|
1075 | IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr, |
---|
1076 | &pol->ape_policy.addrmask.sin6_addr)) { |
---|
1077 | ADDRSEL_UNLOCK(); |
---|
1078 | ADDRSEL_XUNLOCK(); |
---|
1079 | free(new, M_IFADDR); |
---|
1080 | return (EEXIST); /* or override it? */ |
---|
1081 | } |
---|
1082 | } |
---|
1083 | |
---|
1084 | bzero(new, sizeof(*new)); |
---|
1085 | |
---|
1086 | /* XXX: should validate entry */ |
---|
1087 | new->ape_policy = *newpolicy; |
---|
1088 | |
---|
1089 | TAILQ_INSERT_TAIL(&V_addrsel_policytab, new, ape_entry); |
---|
1090 | ADDRSEL_UNLOCK(); |
---|
1091 | ADDRSEL_XUNLOCK(); |
---|
1092 | |
---|
1093 | return (0); |
---|
1094 | } |
---|
1095 | |
---|
1096 | static int |
---|
1097 | delete_addrsel_policyent(struct in6_addrpolicy *key) |
---|
1098 | { |
---|
1099 | struct addrsel_policyent *pol; |
---|
1100 | |
---|
1101 | ADDRSEL_XLOCK(); |
---|
1102 | ADDRSEL_LOCK(); |
---|
1103 | |
---|
1104 | /* search for the entry in the table */ |
---|
1105 | TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) { |
---|
1106 | if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr, |
---|
1107 | &pol->ape_policy.addr.sin6_addr) && |
---|
1108 | IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr, |
---|
1109 | &pol->ape_policy.addrmask.sin6_addr)) { |
---|
1110 | break; |
---|
1111 | } |
---|
1112 | } |
---|
1113 | if (pol == NULL) { |
---|
1114 | ADDRSEL_UNLOCK(); |
---|
1115 | ADDRSEL_XUNLOCK(); |
---|
1116 | return (ESRCH); |
---|
1117 | } |
---|
1118 | |
---|
1119 | TAILQ_REMOVE(&V_addrsel_policytab, pol, ape_entry); |
---|
1120 | ADDRSEL_UNLOCK(); |
---|
1121 | ADDRSEL_XUNLOCK(); |
---|
1122 | free(pol, M_IFADDR); |
---|
1123 | |
---|
1124 | return (0); |
---|
1125 | } |
---|
1126 | |
---|
1127 | static int |
---|
1128 | walk_addrsel_policy(int (*callback)(struct in6_addrpolicy *, void *), |
---|
1129 | void *w) |
---|
1130 | { |
---|
1131 | struct addrsel_policyent *pol; |
---|
1132 | int error = 0; |
---|
1133 | |
---|
1134 | ADDRSEL_SLOCK(); |
---|
1135 | TAILQ_FOREACH(pol, &V_addrsel_policytab, ape_entry) { |
---|
1136 | if ((error = (*callback)(&pol->ape_policy, w)) != 0) { |
---|
1137 | ADDRSEL_SUNLOCK(); |
---|
1138 | return (error); |
---|
1139 | } |
---|
1140 | } |
---|
1141 | ADDRSEL_SUNLOCK(); |
---|
1142 | return (error); |
---|
1143 | } |
---|
1144 | |
---|
1145 | static int |
---|
1146 | dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg) |
---|
1147 | { |
---|
1148 | int error = 0; |
---|
1149 | struct walkarg *w = arg; |
---|
1150 | |
---|
1151 | error = SYSCTL_OUT(w->w_req, pol, sizeof(*pol)); |
---|
1152 | |
---|
1153 | return (error); |
---|
1154 | } |
---|
1155 | |
---|
1156 | static struct in6_addrpolicy * |
---|
1157 | match_addrsel_policy(struct sockaddr_in6 *key) |
---|
1158 | { |
---|
1159 | struct addrsel_policyent *pent; |
---|
1160 | struct in6_addrpolicy *bestpol = NULL, *pol; |
---|
1161 | int matchlen, bestmatchlen = -1; |
---|
1162 | u_char *mp, *ep, *k, *p, m; |
---|
1163 | |
---|
1164 | TAILQ_FOREACH(pent, &V_addrsel_policytab, ape_entry) { |
---|
1165 | matchlen = 0; |
---|
1166 | |
---|
1167 | pol = &pent->ape_policy; |
---|
1168 | mp = (u_char *)&pol->addrmask.sin6_addr; |
---|
1169 | ep = mp + 16; /* XXX: scope field? */ |
---|
1170 | k = (u_char *)&key->sin6_addr; |
---|
1171 | p = (u_char *)&pol->addr.sin6_addr; |
---|
1172 | for (; mp < ep && *mp; mp++, k++, p++) { |
---|
1173 | m = *mp; |
---|
1174 | if ((*k & m) != *p) |
---|
1175 | goto next; /* not match */ |
---|
1176 | if (m == 0xff) /* short cut for a typical case */ |
---|
1177 | matchlen += 8; |
---|
1178 | else { |
---|
1179 | while (m >= 0x80) { |
---|
1180 | matchlen++; |
---|
1181 | m <<= 1; |
---|
1182 | } |
---|
1183 | } |
---|
1184 | } |
---|
1185 | |
---|
1186 | /* matched. check if this is better than the current best. */ |
---|
1187 | if (bestpol == NULL || |
---|
1188 | matchlen > bestmatchlen) { |
---|
1189 | bestpol = pol; |
---|
1190 | bestmatchlen = matchlen; |
---|
1191 | } |
---|
1192 | |
---|
1193 | next: |
---|
1194 | continue; |
---|
1195 | } |
---|
1196 | |
---|
1197 | return (bestpol); |
---|
1198 | } |
---|