in6_src.c revision 1.88 1 1.88 kardel /* $NetBSD: in6_src.c,v 1.88 2021/08/10 06:29:56 kardel Exp $ */
2 1.23 rpaulo /* $KAME: in6_src.c,v 1.159 2005/10/19 01:40:32 t-momose Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.1 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun /*
34 1.1 itojun * Copyright (c) 1982, 1986, 1991, 1993
35 1.1 itojun * The Regents of the University of California. All rights reserved.
36 1.1 itojun *
37 1.1 itojun * Redistribution and use in source and binary forms, with or without
38 1.1 itojun * modification, are permitted provided that the following conditions
39 1.1 itojun * are met:
40 1.1 itojun * 1. Redistributions of source code must retain the above copyright
41 1.1 itojun * notice, this list of conditions and the following disclaimer.
42 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 itojun * notice, this list of conditions and the following disclaimer in the
44 1.1 itojun * documentation and/or other materials provided with the distribution.
45 1.23 rpaulo * 3. All advertising materials mentioning features or use of this software
46 1.23 rpaulo * must display the following acknowledgement:
47 1.23 rpaulo * This product includes software developed by the University of
48 1.23 rpaulo * California, Berkeley and its contributors.
49 1.23 rpaulo * 4. Neither the name of the University nor the names of its contributors
50 1.1 itojun * may be used to endorse or promote products derived from this software
51 1.1 itojun * without specific prior written permission.
52 1.1 itojun *
53 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.1 itojun * SUCH DAMAGE.
64 1.1 itojun *
65 1.1 itojun * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
66 1.1 itojun */
67 1.9 lukem
68 1.9 lukem #include <sys/cdefs.h>
69 1.88 kardel __KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.88 2021/08/10 06:29:56 kardel Exp $");
70 1.1 itojun
71 1.58 pooka #ifdef _KERNEL_OPT
72 1.1 itojun #include "opt_inet.h"
73 1.58 pooka #endif
74 1.1 itojun
75 1.1 itojun #include <sys/param.h>
76 1.1 itojun #include <sys/systm.h>
77 1.1 itojun #include <sys/malloc.h>
78 1.1 itojun #include <sys/mbuf.h>
79 1.1 itojun #include <sys/protosw.h>
80 1.1 itojun #include <sys/socket.h>
81 1.1 itojun #include <sys/socketvar.h>
82 1.1 itojun #include <sys/ioctl.h>
83 1.1 itojun #include <sys/errno.h>
84 1.1 itojun #include <sys/time.h>
85 1.23 rpaulo #include <sys/kernel.h>
86 1.1 itojun #include <sys/proc.h>
87 1.26 elad #include <sys/kauth.h>
88 1.1 itojun
89 1.1 itojun #include <net/if.h>
90 1.23 rpaulo #include <net/if_types.h>
91 1.1 itojun #include <net/route.h>
92 1.1 itojun
93 1.1 itojun #include <netinet/in.h>
94 1.1 itojun #include <netinet/in_var.h>
95 1.1 itojun #include <netinet/in_systm.h>
96 1.1 itojun #include <netinet/ip.h>
97 1.1 itojun #include <netinet/in_pcb.h>
98 1.53 christos #include <netinet/portalgo.h>
99 1.1 itojun #include <netinet6/in6_var.h>
100 1.1 itojun #include <netinet/ip6.h>
101 1.1 itojun #include <netinet6/in6_pcb.h>
102 1.1 itojun #include <netinet6/ip6_var.h>
103 1.43 thorpej #include <netinet6/ip6_private.h>
104 1.1 itojun #include <netinet6/nd6.h>
105 1.13 itojun #include <netinet6/scope6_var.h>
106 1.23 rpaulo
107 1.23 rpaulo #ifdef MIP6
108 1.23 rpaulo #include <netinet6/mip6.h>
109 1.23 rpaulo #include <netinet6/mip6_var.h>
110 1.23 rpaulo #include "mip.h"
111 1.23 rpaulo #if NMIP > 0
112 1.23 rpaulo #include <net/if_mip.h>
113 1.23 rpaulo #endif /* NMIP > 0 */
114 1.23 rpaulo #endif /* MIP6 */
115 1.23 rpaulo
116 1.50 dyoung #include <netinet/tcp_vtw.h>
117 1.50 dyoung
118 1.23 rpaulo #define ADDR_LABEL_NOTAPP (-1)
119 1.23 rpaulo struct in6_addrpolicy defaultaddrpolicy;
120 1.23 rpaulo
121 1.23 rpaulo int ip6_prefer_tempaddr = 0;
122 1.23 rpaulo
123 1.35 dyoung static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
124 1.62 ozaki struct ip6_moptions *, struct route *, struct ifnet **, struct psref *);
125 1.35 dyoung
126 1.35 dyoung static struct in6_addrpolicy *lookup_addrsel_policy(struct sockaddr_in6 *);
127 1.35 dyoung
128 1.35 dyoung static void init_policy_queue(void);
129 1.35 dyoung static int add_addrsel_policyent(struct in6_addrpolicy *);
130 1.35 dyoung static int delete_addrsel_policyent(struct in6_addrpolicy *);
131 1.35 dyoung static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
132 1.35 dyoung void *);
133 1.35 dyoung static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
134 1.35 dyoung static struct in6_addrpolicy *match_addrsel_policy(struct sockaddr_in6 *);
135 1.1 itojun
136 1.84 roy #define IFA6_IS_VALIDATED(ia) \
137 1.84 roy (((ia)->ia6_flags & (IN6_IFF_TENTATIVE | IN6_IFF_DETACHED)) == 0)
138 1.84 roy
139 1.1 itojun /*
140 1.5 itojun * Return an IPv6 address, which is the most appropriate for a given
141 1.1 itojun * destination and user specified options.
142 1.5 itojun * If necessary, this function lookups the routing table and returns
143 1.1 itojun * an entry to the caller for later use.
144 1.1 itojun */
145 1.78 christos #if 0 /* disabled ad-hoc */
146 1.23 rpaulo #define REPLACE(r) do {\
147 1.78 christos char _buf1[INET6_ADDRSTRLEN], _buf2[INET6_ADDRSTRLEN]; \
148 1.23 rpaulo if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
149 1.23 rpaulo sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
150 1.23 rpaulo ip6stat.ip6s_sources_rule[(r)]++; \
151 1.78 christos printf("%s: replace %s with %s by %d\n", __func__, ia_best ? \
152 1.78 christos IN6_PRINT(_buf1, &ia_best->ia_addr.sin6_addr) : "none", \
153 1.78 christos IN6_PRINT(_buf2, &ia->ia_addr.sin6_addr), (r)); \
154 1.23 rpaulo goto replace; \
155 1.78 christos } while(/*CONSTCOND*/0)
156 1.23 rpaulo #define NEXT(r) do {\
157 1.23 rpaulo if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
158 1.23 rpaulo sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
159 1.23 rpaulo ip6stat.ip6s_sources_rule[(r)]++; \
160 1.78 christos printf("%s: keep %s against %s by %d\n", ia_best ? \
161 1.78 christos IN6_PRINT(_buf1, &ia_best->ia_addr.sin6_addr) : "none", \
162 1.78 christos IN6_PRINT(_buf2, &ia->ia_addr.sin6_addr), (r)); \
163 1.23 rpaulo goto next; /* XXX: we can't use 'continue' here */ \
164 1.78 christos } while(/*CONSTCOND*/0)
165 1.23 rpaulo #define BREAK(r) do { \
166 1.23 rpaulo if ((r) < sizeof(ip6stat.ip6s_sources_rule) / \
167 1.23 rpaulo sizeof(ip6stat.ip6s_sources_rule[0])) /* check for safety */ \
168 1.23 rpaulo ip6stat.ip6s_sources_rule[(r)]++; \
169 1.23 rpaulo goto out; /* XXX: we can't use 'break' here */ \
170 1.78 christos } while(/*CONSTCOND*/0)
171 1.23 rpaulo #else
172 1.23 rpaulo #define REPLACE(r) goto replace
173 1.23 rpaulo #define NEXT(r) goto next
174 1.23 rpaulo #define BREAK(r) goto out
175 1.23 rpaulo #endif
176 1.23 rpaulo
177 1.73 ozaki /*
178 1.73 ozaki * Called inside pserialize critical section. Don't sleep/block.
179 1.73 ozaki */
180 1.73 ozaki static struct in6_ifaddr *
181 1.73 ozaki in6_select_best_ia(struct sockaddr_in6 *dstsock, struct in6_addr *dst,
182 1.73 ozaki const struct ifnet *ifp, const struct ip6_pktopts *opts,
183 1.73 ozaki const u_int32_t odstzone)
184 1.1 itojun {
185 1.73 ozaki struct in6_ifaddr *ia, *ia_best = NULL;
186 1.23 rpaulo int dst_scope = -1, best_scope = -1, best_matchlen = -1;
187 1.23 rpaulo struct in6_addrpolicy *dst_policy = NULL, *best_policy = NULL;
188 1.1 itojun
189 1.63 ozaki IN6_ADDRLIST_READER_FOREACH(ia) {
190 1.23 rpaulo int new_scope = -1, new_matchlen = -1;
191 1.23 rpaulo struct in6_addrpolicy *new_policy = NULL;
192 1.23 rpaulo u_int32_t srczone, osrczone, dstzone;
193 1.23 rpaulo struct in6_addr src;
194 1.23 rpaulo struct ifnet *ifp1 = ia->ia_ifp;
195 1.73 ozaki int prefer_tempaddr;
196 1.23 rpaulo
197 1.1 itojun /*
198 1.23 rpaulo * We'll never take an address that breaks the scope zone
199 1.23 rpaulo * of the destination. We also skip an address if its zone
200 1.23 rpaulo * does not contain the outgoing interface.
201 1.23 rpaulo * XXX: we should probably use sin6_scope_id here.
202 1.1 itojun */
203 1.73 ozaki if (in6_setscope(dst, ifp1, &dstzone) ||
204 1.23 rpaulo odstzone != dstzone) {
205 1.23 rpaulo continue;
206 1.23 rpaulo }
207 1.23 rpaulo src = ia->ia_addr.sin6_addr;
208 1.81 christos
209 1.81 christos /* Skip the scope test in impossible cases */
210 1.81 christos if (!(ifp->if_flags & IFF_LOOPBACK) &&
211 1.81 christos IN6_IS_ADDR_LOOPBACK(&src))
212 1.81 christos continue;
213 1.81 christos
214 1.23 rpaulo if (in6_setscope(&src, ifp, &osrczone) ||
215 1.23 rpaulo in6_setscope(&src, ifp1, &srczone) ||
216 1.23 rpaulo osrczone != srczone) {
217 1.23 rpaulo continue;
218 1.23 rpaulo }
219 1.23 rpaulo
220 1.23 rpaulo /* avoid unusable addresses */
221 1.83 roy if ((ia->ia6_flags & (IN6_IFF_DUPLICATED | IN6_IFF_ANYCAST)))
222 1.83 roy continue;
223 1.23 rpaulo if (!ip6_use_deprecated && IFA6_IS_DEPRECATED(ia))
224 1.23 rpaulo continue;
225 1.23 rpaulo
226 1.23 rpaulo #if defined(MIP6) && NMIP > 0
227 1.23 rpaulo /* avoid unusable home addresses. */
228 1.23 rpaulo if ((ia->ia6_flags & IN6_IFF_HOME) &&
229 1.23 rpaulo !mip6_ifa6_is_addr_valid_hoa(ia))
230 1.23 rpaulo continue;
231 1.23 rpaulo #endif /* MIP6 && NMIP > 0 */
232 1.23 rpaulo
233 1.23 rpaulo /* Rule 1: Prefer same address */
234 1.73 ozaki if (IN6_ARE_ADDR_EQUAL(dst, &ia->ia_addr.sin6_addr)) {
235 1.23 rpaulo ia_best = ia;
236 1.23 rpaulo BREAK(1); /* there should be no better candidate */
237 1.1 itojun }
238 1.1 itojun
239 1.23 rpaulo if (ia_best == NULL)
240 1.83 roy REPLACE(1);
241 1.23 rpaulo
242 1.23 rpaulo /* Rule 2: Prefer appropriate scope */
243 1.23 rpaulo if (dst_scope < 0)
244 1.73 ozaki dst_scope = in6_addrscope(dst);
245 1.23 rpaulo new_scope = in6_addrscope(&ia->ia_addr.sin6_addr);
246 1.23 rpaulo if (IN6_ARE_SCOPE_CMP(best_scope, new_scope) < 0) {
247 1.23 rpaulo if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0)
248 1.23 rpaulo REPLACE(2);
249 1.23 rpaulo NEXT(2);
250 1.23 rpaulo } else if (IN6_ARE_SCOPE_CMP(new_scope, best_scope) < 0) {
251 1.23 rpaulo if (IN6_ARE_SCOPE_CMP(new_scope, dst_scope) < 0)
252 1.23 rpaulo NEXT(2);
253 1.23 rpaulo REPLACE(2);
254 1.23 rpaulo }
255 1.1 itojun
256 1.23 rpaulo /*
257 1.23 rpaulo * Rule 3: Avoid deprecated addresses. Note that the case of
258 1.23 rpaulo * !ip6_use_deprecated is already rejected above.
259 1.84 roy * Treat unvalidated addresses as deprecated here.
260 1.23 rpaulo */
261 1.84 roy if (IFA6_IS_VALIDATED(ia_best) && !IFA6_IS_VALIDATED(ia))
262 1.84 roy NEXT(3);
263 1.84 roy if (!IFA6_IS_VALIDATED(ia_best) && IFA6_IS_VALIDATED(ia))
264 1.84 roy REPLACE(3);
265 1.23 rpaulo if (!IFA6_IS_DEPRECATED(ia_best) && IFA6_IS_DEPRECATED(ia))
266 1.23 rpaulo NEXT(3);
267 1.23 rpaulo if (IFA6_IS_DEPRECATED(ia_best) && !IFA6_IS_DEPRECATED(ia))
268 1.23 rpaulo REPLACE(3);
269 1.23 rpaulo
270 1.23 rpaulo /* Rule 4: Prefer home addresses */
271 1.23 rpaulo #if defined(MIP6) && NMIP > 0
272 1.23 rpaulo if (!MIP6_IS_MN)
273 1.23 rpaulo goto skip_rule4;
274 1.23 rpaulo
275 1.23 rpaulo if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
276 1.23 rpaulo (ia->ia6_flags & IN6_IFF_HOME) == 0) {
277 1.23 rpaulo /* both address are not home addresses. */
278 1.23 rpaulo goto skip_rule4;
279 1.23 rpaulo }
280 1.1 itojun
281 1.23 rpaulo /*
282 1.23 rpaulo * If SA is simultaneously a home address and care-of
283 1.23 rpaulo * address and SB is not, then prefer SA. Similarly,
284 1.23 rpaulo * if SB is simultaneously a home address and care-of
285 1.23 rpaulo * address and SA is not, then prefer SB.
286 1.23 rpaulo */
287 1.23 rpaulo if (((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
288 1.23 rpaulo ia_best->ia_ifp->if_type != IFT_MIP)
289 1.23 rpaulo &&
290 1.23 rpaulo ((ia->ia6_flags & IN6_IFF_HOME) != 0 &&
291 1.23 rpaulo ia->ia_ifp->if_type == IFT_MIP))
292 1.23 rpaulo NEXT(4);
293 1.23 rpaulo if (((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
294 1.23 rpaulo ia_best->ia_ifp->if_type == IFT_MIP)
295 1.23 rpaulo &&
296 1.23 rpaulo ((ia->ia6_flags & IN6_IFF_HOME) != 0 &&
297 1.23 rpaulo ia->ia_ifp->if_type != IFT_MIP))
298 1.23 rpaulo REPLACE(4);
299 1.23 rpaulo if (ip6po_usecoa == 0) {
300 1.23 rpaulo /*
301 1.23 rpaulo * If SA is just a home address and SB is just
302 1.23 rpaulo * a care-of address, then prefer
303 1.23 rpaulo * SA. Similarly, if SB is just a home address
304 1.23 rpaulo * and SA is just a care-of address, then
305 1.23 rpaulo * prefer SB.
306 1.23 rpaulo */
307 1.23 rpaulo if ((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
308 1.23 rpaulo (ia->ia6_flags & IN6_IFF_HOME) == 0) {
309 1.23 rpaulo NEXT(4);
310 1.23 rpaulo }
311 1.23 rpaulo if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
312 1.23 rpaulo (ia->ia6_flags & IN6_IFF_HOME) != 0) {
313 1.23 rpaulo REPLACE(4);
314 1.23 rpaulo }
315 1.23 rpaulo } else {
316 1.23 rpaulo /*
317 1.23 rpaulo * a sender don't want to use a home address
318 1.23 rpaulo * because:
319 1.23 rpaulo *
320 1.23 rpaulo * 1) we cannot use. (ex. NS or NA to global
321 1.23 rpaulo * addresses.)
322 1.23 rpaulo *
323 1.23 rpaulo * 2) a user specified not to use.
324 1.23 rpaulo * (ex. mip6control -u)
325 1.23 rpaulo */
326 1.23 rpaulo if ((ia_best->ia6_flags & IN6_IFF_HOME) == 0 &&
327 1.23 rpaulo (ia->ia6_flags & IN6_IFF_HOME) != 0) {
328 1.23 rpaulo /* XXX breaks stat */
329 1.23 rpaulo NEXT(0);
330 1.23 rpaulo }
331 1.23 rpaulo if ((ia_best->ia6_flags & IN6_IFF_HOME) != 0 &&
332 1.23 rpaulo (ia->ia6_flags & IN6_IFF_HOME) == 0) {
333 1.23 rpaulo /* XXX breaks stat */
334 1.23 rpaulo REPLACE(0);
335 1.1 itojun }
336 1.68 roy }
337 1.23 rpaulo skip_rule4:
338 1.23 rpaulo #endif /* MIP6 && NMIP > 0 */
339 1.23 rpaulo
340 1.23 rpaulo /* Rule 5: Prefer outgoing interface */
341 1.23 rpaulo if (ia_best->ia_ifp == ifp && ia->ia_ifp != ifp)
342 1.23 rpaulo NEXT(5);
343 1.23 rpaulo if (ia_best->ia_ifp != ifp && ia->ia_ifp == ifp)
344 1.23 rpaulo REPLACE(5);
345 1.23 rpaulo
346 1.23 rpaulo /*
347 1.23 rpaulo * Rule 6: Prefer matching label
348 1.23 rpaulo * Note that best_policy should be non-NULL here.
349 1.23 rpaulo */
350 1.23 rpaulo if (dst_policy == NULL)
351 1.23 rpaulo dst_policy = lookup_addrsel_policy(dstsock);
352 1.23 rpaulo if (dst_policy->label != ADDR_LABEL_NOTAPP) {
353 1.23 rpaulo new_policy = lookup_addrsel_policy(&ia->ia_addr);
354 1.23 rpaulo if (dst_policy->label == best_policy->label &&
355 1.23 rpaulo dst_policy->label != new_policy->label)
356 1.23 rpaulo NEXT(6);
357 1.23 rpaulo if (dst_policy->label != best_policy->label &&
358 1.23 rpaulo dst_policy->label == new_policy->label)
359 1.23 rpaulo REPLACE(6);
360 1.23 rpaulo }
361 1.23 rpaulo
362 1.23 rpaulo /*
363 1.23 rpaulo * Rule 7: Prefer public addresses.
364 1.23 rpaulo * We allow users to reverse the logic by configuring
365 1.23 rpaulo * a sysctl variable, so that privacy conscious users can
366 1.23 rpaulo * always prefer temporary addresses.
367 1.23 rpaulo */
368 1.23 rpaulo if (opts == NULL ||
369 1.23 rpaulo opts->ip6po_prefer_tempaddr == IP6PO_TEMPADDR_SYSTEM) {
370 1.23 rpaulo prefer_tempaddr = ip6_prefer_tempaddr;
371 1.23 rpaulo } else if (opts->ip6po_prefer_tempaddr ==
372 1.23 rpaulo IP6PO_TEMPADDR_NOTPREFER) {
373 1.23 rpaulo prefer_tempaddr = 0;
374 1.23 rpaulo } else
375 1.23 rpaulo prefer_tempaddr = 1;
376 1.23 rpaulo if (!(ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
377 1.23 rpaulo (ia->ia6_flags & IN6_IFF_TEMPORARY)) {
378 1.23 rpaulo if (prefer_tempaddr)
379 1.23 rpaulo REPLACE(7);
380 1.23 rpaulo else
381 1.23 rpaulo NEXT(7);
382 1.23 rpaulo }
383 1.23 rpaulo if ((ia_best->ia6_flags & IN6_IFF_TEMPORARY) &&
384 1.23 rpaulo !(ia->ia6_flags & IN6_IFF_TEMPORARY)) {
385 1.23 rpaulo if (prefer_tempaddr)
386 1.23 rpaulo NEXT(7);
387 1.23 rpaulo else
388 1.23 rpaulo REPLACE(7);
389 1.1 itojun }
390 1.23 rpaulo
391 1.23 rpaulo /*
392 1.23 rpaulo * Rule 8: prefer addresses on alive interfaces.
393 1.23 rpaulo * This is a KAME specific rule.
394 1.23 rpaulo */
395 1.23 rpaulo if ((ia_best->ia_ifp->if_flags & IFF_UP) &&
396 1.23 rpaulo !(ia->ia_ifp->if_flags & IFF_UP))
397 1.23 rpaulo NEXT(8);
398 1.23 rpaulo if (!(ia_best->ia_ifp->if_flags & IFF_UP) &&
399 1.23 rpaulo (ia->ia_ifp->if_flags & IFF_UP))
400 1.23 rpaulo REPLACE(8);
401 1.23 rpaulo
402 1.23 rpaulo /*
403 1.23 rpaulo * Rule 9: prefer addresses on "preferred" interfaces.
404 1.23 rpaulo * This is a KAME specific rule.
405 1.23 rpaulo */
406 1.23 rpaulo #ifdef notyet /* until introducing address selection */
407 1.23 rpaulo #define NDI_BEST ND_IFINFO(ia_best->ia_ifp)
408 1.23 rpaulo #define NDI_NEW ND_IFINFO(ia->ia_ifp)
409 1.23 rpaulo if ((NDI_BEST->flags & ND6_IFF_PREFER_SOURCE) &&
410 1.23 rpaulo !(NDI_NEW->flags & ND6_IFF_PREFER_SOURCE))
411 1.23 rpaulo NEXT(9);
412 1.23 rpaulo if (!(NDI_BEST->flags & ND6_IFF_PREFER_SOURCE) &&
413 1.23 rpaulo (NDI_NEW->flags & ND6_IFF_PREFER_SOURCE))
414 1.23 rpaulo REPLACE(9);
415 1.23 rpaulo #undef NDI_BEST
416 1.23 rpaulo #undef NDI_NEW
417 1.23 rpaulo #endif
418 1.23 rpaulo
419 1.23 rpaulo /*
420 1.23 rpaulo * Rule 14: Use longest matching prefix.
421 1.23 rpaulo * Note: in the address selection draft, this rule is
422 1.23 rpaulo * documented as "Rule 8". However, since it is also
423 1.23 rpaulo * documented that this rule can be overridden, we assign
424 1.23 rpaulo * a large number so that it is easy to assign smaller numbers
425 1.23 rpaulo * to more preferred rules.
426 1.23 rpaulo */
427 1.73 ozaki new_matchlen = in6_matchlen(&ia->ia_addr.sin6_addr, dst);
428 1.23 rpaulo if (best_matchlen < new_matchlen)
429 1.23 rpaulo REPLACE(14);
430 1.23 rpaulo if (new_matchlen < best_matchlen)
431 1.23 rpaulo NEXT(14);
432 1.23 rpaulo
433 1.23 rpaulo /* Rule 15 is reserved. */
434 1.23 rpaulo
435 1.23 rpaulo /*
436 1.23 rpaulo * Last resort: just keep the current candidate.
437 1.23 rpaulo * Or, do we need more rules?
438 1.23 rpaulo */
439 1.23 rpaulo continue;
440 1.23 rpaulo
441 1.23 rpaulo replace:
442 1.23 rpaulo ia_best = ia;
443 1.23 rpaulo best_scope = (new_scope >= 0 ? new_scope :
444 1.23 rpaulo in6_addrscope(&ia_best->ia_addr.sin6_addr));
445 1.23 rpaulo best_policy = (new_policy ? new_policy :
446 1.23 rpaulo lookup_addrsel_policy(&ia_best->ia_addr));
447 1.23 rpaulo best_matchlen = (new_matchlen >= 0 ? new_matchlen :
448 1.23 rpaulo in6_matchlen(&ia_best->ia_addr.sin6_addr,
449 1.73 ozaki dst));
450 1.23 rpaulo
451 1.23 rpaulo next:
452 1.23 rpaulo continue;
453 1.23 rpaulo
454 1.23 rpaulo out:
455 1.23 rpaulo break;
456 1.23 rpaulo }
457 1.23 rpaulo
458 1.73 ozaki return ia_best;
459 1.73 ozaki }
460 1.73 ozaki #undef REPLACE
461 1.73 ozaki #undef BREAK
462 1.73 ozaki #undef NEXT
463 1.73 ozaki
464 1.73 ozaki int
465 1.73 ozaki in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
466 1.73 ozaki struct ip6_moptions *mopts, struct route *ro, struct in6_addr *laddr,
467 1.73 ozaki struct ifnet **ifpp, struct psref *psref, struct in6_addr *ret_ia6)
468 1.73 ozaki {
469 1.73 ozaki struct in6_addr dst;
470 1.73 ozaki struct ifnet *ifp = NULL;
471 1.73 ozaki struct in6_ifaddr *ia = NULL;
472 1.73 ozaki struct in6_pktinfo *pi = NULL;
473 1.73 ozaki u_int32_t odstzone;
474 1.80 christos int error = 0, iferror;
475 1.73 ozaki #if defined(MIP6) && NMIP > 0
476 1.73 ozaki u_int8_t ip6po_usecoa = 0;
477 1.73 ozaki #endif /* MIP6 && NMIP > 0 */
478 1.73 ozaki struct psref local_psref;
479 1.73 ozaki int bound = curlwp_bind();
480 1.73 ozaki #define PSREF (psref == NULL) ? &local_psref : psref
481 1.73 ozaki int s;
482 1.73 ozaki
483 1.73 ozaki KASSERT((ifpp != NULL && psref != NULL) ||
484 1.73 ozaki (ifpp == NULL && psref == NULL));
485 1.73 ozaki
486 1.73 ozaki dst = dstsock->sin6_addr; /* make a copy for local operation */
487 1.73 ozaki if (ifpp)
488 1.73 ozaki *ifpp = NULL;
489 1.73 ozaki
490 1.73 ozaki /*
491 1.73 ozaki * Try to determine the outgoing interface for the given destination.
492 1.73 ozaki * We do this regardless of whether the socket is bound, since the
493 1.73 ozaki * caller may need this information as a side effect of the call
494 1.73 ozaki * to this function (e.g., for identifying the appropriate scope zone
495 1.73 ozaki * ID).
496 1.73 ozaki */
497 1.80 christos iferror = in6_selectif(dstsock, opts, mopts, ro, &ifp, PSREF);
498 1.73 ozaki if (ifpp != NULL)
499 1.73 ozaki *ifpp = ifp;
500 1.73 ozaki
501 1.73 ozaki /*
502 1.73 ozaki * If the source address is explicitly specified by the caller,
503 1.73 ozaki * check if the requested source address is indeed a unicast address
504 1.73 ozaki * assigned to the node, and can be used as the packet's source
505 1.73 ozaki * address. If everything is okay, use the address as source.
506 1.73 ozaki */
507 1.73 ozaki if (opts && (pi = opts->ip6po_pktinfo) &&
508 1.73 ozaki !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
509 1.73 ozaki struct sockaddr_in6 srcsock;
510 1.73 ozaki struct in6_ifaddr *ia6;
511 1.73 ozaki int _s;
512 1.73 ozaki struct ifaddr *ifa;
513 1.73 ozaki
514 1.73 ozaki /*
515 1.73 ozaki * Determine the appropriate zone id of the source based on
516 1.73 ozaki * the zone of the destination and the outgoing interface.
517 1.73 ozaki * If the specified address is ambiguous wrt the scope zone,
518 1.73 ozaki * the interface must be specified; otherwise, ifa_ifwithaddr()
519 1.73 ozaki * will fail matching the address.
520 1.73 ozaki */
521 1.73 ozaki memset(&srcsock, 0, sizeof(srcsock));
522 1.73 ozaki srcsock.sin6_family = AF_INET6;
523 1.73 ozaki srcsock.sin6_len = sizeof(srcsock);
524 1.73 ozaki srcsock.sin6_addr = pi->ipi6_addr;
525 1.73 ozaki if (ifp) {
526 1.73 ozaki error = in6_setscope(&srcsock.sin6_addr, ifp, NULL);
527 1.73 ozaki if (error != 0)
528 1.73 ozaki goto exit;
529 1.73 ozaki }
530 1.73 ozaki
531 1.73 ozaki _s = pserialize_read_enter();
532 1.73 ozaki ifa = ifa_ifwithaddr(sin6tosa(&srcsock));
533 1.73 ozaki if ((ia6 = ifatoia6(ifa)) == NULL ||
534 1.73 ozaki ia6->ia6_flags &
535 1.73 ozaki (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY)) {
536 1.73 ozaki pserialize_read_exit(_s);
537 1.73 ozaki error = EADDRNOTAVAIL;
538 1.73 ozaki goto exit;
539 1.73 ozaki }
540 1.73 ozaki pi->ipi6_addr = srcsock.sin6_addr; /* XXX: this overrides pi */
541 1.73 ozaki if (ifpp)
542 1.73 ozaki *ifpp = ifp;
543 1.73 ozaki *ret_ia6 = ia6->ia_addr.sin6_addr;
544 1.73 ozaki pserialize_read_exit(_s);
545 1.73 ozaki goto exit;
546 1.73 ozaki }
547 1.73 ozaki
548 1.73 ozaki /*
549 1.73 ozaki * If the socket has already bound the source, just use it. We don't
550 1.73 ozaki * care at the moment whether in6_selectif() succeeded above, even
551 1.73 ozaki * though it would eventually cause an error.
552 1.73 ozaki */
553 1.73 ozaki if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
554 1.73 ozaki *ret_ia6 = *laddr;
555 1.73 ozaki goto exit;
556 1.73 ozaki }
557 1.73 ozaki
558 1.73 ozaki /*
559 1.73 ozaki * The outgoing interface is crucial in the general selection procedure
560 1.73 ozaki * below. If it is not known at this point, we fail.
561 1.73 ozaki */
562 1.80 christos if (ifp == NULL) {
563 1.80 christos error = iferror;
564 1.73 ozaki goto exit;
565 1.80 christos }
566 1.73 ozaki
567 1.73 ozaki /*
568 1.73 ozaki * If the address is not yet determined, choose the best one based on
569 1.73 ozaki * the outgoing interface and the destination address.
570 1.73 ozaki */
571 1.73 ozaki
572 1.73 ozaki #if defined(MIP6) && NMIP > 0
573 1.73 ozaki /*
574 1.73 ozaki * a caller can specify IP6PO_USECOA to not to use a home
575 1.73 ozaki * address. for example, the case that the neighbour
576 1.73 ozaki * unreachability detection to the global address.
577 1.73 ozaki */
578 1.73 ozaki if (opts != NULL &&
579 1.73 ozaki (opts->ip6po_flags & IP6PO_USECOA) != 0) {
580 1.73 ozaki ip6po_usecoa = 1;
581 1.73 ozaki }
582 1.73 ozaki #endif /* MIP6 && NMIP > 0 */
583 1.73 ozaki
584 1.73 ozaki error = in6_setscope(&dst, ifp, &odstzone);
585 1.73 ozaki if (error != 0)
586 1.73 ozaki goto exit;
587 1.73 ozaki
588 1.73 ozaki s = pserialize_read_enter();
589 1.73 ozaki
590 1.73 ozaki ia = in6_select_best_ia(dstsock, &dst, ifp, opts, odstzone);
591 1.73 ozaki if (ia == NULL) {
592 1.72 ozaki pserialize_read_exit(s);
593 1.72 ozaki error = EADDRNOTAVAIL;
594 1.62 ozaki goto exit;
595 1.23 rpaulo }
596 1.73 ozaki *ret_ia6 = ia->ia_addr.sin6_addr;
597 1.23 rpaulo
598 1.72 ozaki pserialize_read_exit(s);
599 1.62 ozaki exit:
600 1.62 ozaki if (ifpp == NULL)
601 1.62 ozaki if_put(ifp, PSREF);
602 1.62 ozaki curlwp_bindx(bound);
603 1.72 ozaki return error;
604 1.62 ozaki #undef PSREF
605 1.23 rpaulo }
606 1.23 rpaulo
607 1.74 ozaki int
608 1.74 ozaki in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
609 1.74 ozaki struct route **ro, struct rtentry **retrt, bool count_discard)
610 1.23 rpaulo {
611 1.23 rpaulo int error = 0;
612 1.23 rpaulo struct rtentry *rt = NULL;
613 1.71 ozaki union {
614 1.71 ozaki struct sockaddr dst;
615 1.88 kardel struct sockaddr_in dst4;
616 1.71 ozaki struct sockaddr_in6 dst6;
617 1.71 ozaki } u;
618 1.71 ozaki
619 1.71 ozaki KASSERT(ro != NULL);
620 1.76 ozaki KASSERT(*ro != NULL);
621 1.71 ozaki KASSERT(retrt != NULL);
622 1.23 rpaulo
623 1.23 rpaulo #if 0
624 1.23 rpaulo if (dstsock->sin6_addr.s6_addr32[0] == 0 &&
625 1.23 rpaulo dstsock->sin6_addr.s6_addr32[1] == 0 &&
626 1.23 rpaulo !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) {
627 1.77 ryo char ip6buf[INET6_ADDRSTRLEN];
628 1.81 christos printf("%s: strange destination %s\n", __func__,
629 1.78 christos IN6_PRINT(ip6buf, &dstsock->sin6_addr));
630 1.23 rpaulo } else {
631 1.77 ryo char ip6buf[INET6_ADDRSTRLEN];
632 1.81 christos printf("%s: destination = %s%%%d\n", __func__,
633 1.78 christos IN6_PRINT(ip6buf, &dstsock->sin6_addr),
634 1.23 rpaulo dstsock->sin6_scope_id); /* for debug */
635 1.23 rpaulo }
636 1.23 rpaulo #endif
637 1.23 rpaulo
638 1.23 rpaulo /*
639 1.23 rpaulo * If the next hop address for the packet is specified by the caller,
640 1.23 rpaulo * use it as the gateway.
641 1.23 rpaulo */
642 1.23 rpaulo if (opts && opts->ip6po_nexthop) {
643 1.35 dyoung struct route *ron;
644 1.74 ozaki struct sockaddr_in6 *sin6_next;
645 1.23 rpaulo
646 1.23 rpaulo sin6_next = satosin6(opts->ip6po_nexthop);
647 1.23 rpaulo
648 1.23 rpaulo /* at this moment, we only support AF_INET6 next hops */
649 1.23 rpaulo if (sin6_next->sin6_family != AF_INET6) {
650 1.87 ozaki IP6_STATINC(IP6_STAT_ODROPPED);
651 1.23 rpaulo error = EAFNOSUPPORT; /* or should we proceed? */
652 1.23 rpaulo goto done;
653 1.23 rpaulo }
654 1.23 rpaulo
655 1.23 rpaulo /*
656 1.23 rpaulo * If the next hop is an IPv6 address, then the node identified
657 1.23 rpaulo * by that address must be a neighbor of the sending host.
658 1.23 rpaulo */
659 1.37 dyoung ron = &opts->ip6po_nextroute;
660 1.74 ozaki rt = rtcache_lookup(ron, sin6tosa(sin6_next));
661 1.74 ozaki if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) != 0 ||
662 1.37 dyoung !nd6_is_addr_neighbor(sin6_next, rt->rt_ifp)) {
663 1.76 ozaki if (rt != NULL) {
664 1.76 ozaki if (count_discard)
665 1.76 ozaki in6_ifstat_inc(rt->rt_ifp,
666 1.76 ozaki ifs6_out_discard);
667 1.76 ozaki rtcache_unref(rt, ron);
668 1.76 ozaki rt = NULL;
669 1.76 ozaki }
670 1.35 dyoung rtcache_free(ron);
671 1.23 rpaulo error = EHOSTUNREACH;
672 1.23 rpaulo goto done;
673 1.23 rpaulo }
674 1.74 ozaki *ro = ron;
675 1.23 rpaulo
676 1.74 ozaki goto done;
677 1.1 itojun }
678 1.1 itojun
679 1.1 itojun /*
680 1.23 rpaulo * Use a cached route if it exists and is valid, else try to allocate
681 1.23 rpaulo * a new one. Note that we should check the address family of the
682 1.13 itojun * cached destination, in case of sharing the cache with IPv4.
683 1.88 kardel *
684 1.88 kardel * for V4 mapped addresses we want to pick up the v4 route
685 1.88 kardel * see PR kern/56348
686 1.1 itojun */
687 1.88 kardel if (IN6_IS_ADDR_V4MAPPED(&dstsock->sin6_addr)) {
688 1.88 kardel in6_sin6_2_sin(&u.dst4, dstsock);
689 1.88 kardel } else {
690 1.88 kardel u.dst6 = *dstsock;
691 1.88 kardel u.dst6.sin6_scope_id = 0;
692 1.88 kardel }
693 1.88 kardel
694 1.74 ozaki rt = rtcache_lookup1(*ro, &u.dst, 1);
695 1.1 itojun
696 1.71 ozaki if (rt == NULL)
697 1.71 ozaki error = EHOSTUNREACH;
698 1.23 rpaulo
699 1.71 ozaki /*
700 1.71 ozaki * Check if the outgoing interface conflicts with
701 1.71 ozaki * the interface specified by ipi6_ifindex (if specified).
702 1.71 ozaki * Note that loopback interface is always okay.
703 1.71 ozaki * (this may happen when we are sending a packet to one of
704 1.71 ozaki * our own addresses.)
705 1.71 ozaki */
706 1.71 ozaki if (opts && opts->ip6po_pktinfo && opts->ip6po_pktinfo->ipi6_ifindex) {
707 1.75 ozaki if (rt != NULL && !(rt->rt_ifp->if_flags & IFF_LOOPBACK) &&
708 1.74 ozaki rt->rt_ifp->if_index != opts->ip6po_pktinfo->ipi6_ifindex) {
709 1.75 ozaki if (count_discard)
710 1.74 ozaki in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
711 1.71 ozaki error = EHOSTUNREACH;
712 1.74 ozaki rt = NULL;
713 1.23 rpaulo }
714 1.23 rpaulo }
715 1.23 rpaulo
716 1.74 ozaki done:
717 1.23 rpaulo if (error == EHOSTUNREACH)
718 1.43 thorpej IP6_STATINC(IP6_STAT_NOROUTE);
719 1.74 ozaki *retrt = rt;
720 1.74 ozaki return error;
721 1.23 rpaulo }
722 1.23 rpaulo
723 1.23 rpaulo static int
724 1.38 christos in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
725 1.62 ozaki struct ip6_moptions *mopts, struct route *ro, struct ifnet **retifp,
726 1.62 ozaki struct psref *psref)
727 1.23 rpaulo {
728 1.76 ozaki int error = 0;
729 1.23 rpaulo struct rtentry *rt = NULL;
730 1.74 ozaki struct in6_addr *dst;
731 1.74 ozaki struct in6_pktinfo *pi = NULL;
732 1.23 rpaulo
733 1.62 ozaki KASSERT(retifp != NULL);
734 1.62 ozaki *retifp = NULL;
735 1.74 ozaki dst = &dstsock->sin6_addr;
736 1.62 ozaki
737 1.74 ozaki /* If the caller specify the outgoing interface explicitly, use it. */
738 1.74 ozaki if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
739 1.74 ozaki /* XXX boundary check is assumed to be already done. */
740 1.74 ozaki *retifp = if_get_byindex(pi->ipi6_ifindex, psref);
741 1.74 ozaki if (*retifp != NULL)
742 1.74 ozaki return 0;
743 1.74 ozaki goto getroute;
744 1.1 itojun }
745 1.1 itojun
746 1.23 rpaulo /*
747 1.74 ozaki * If the destination address is a multicast address and the outgoing
748 1.74 ozaki * interface for the address is specified by the caller, use it.
749 1.74 ozaki */
750 1.74 ozaki if (IN6_IS_ADDR_MULTICAST(dst) && mopts != NULL) {
751 1.74 ozaki *retifp = if_get_byindex(mopts->im6o_multicast_if_index, psref);
752 1.74 ozaki if (*retifp != NULL)
753 1.74 ozaki return 0; /* we do not need a route for multicast. */
754 1.74 ozaki }
755 1.74 ozaki
756 1.74 ozaki getroute:
757 1.74 ozaki error = in6_selectroute(dstsock, opts, &ro, &rt, false);
758 1.74 ozaki if (error != 0)
759 1.74 ozaki return error;
760 1.74 ozaki
761 1.74 ozaki *retifp = if_get_byindex(rt->rt_ifp->if_index, psref);
762 1.74 ozaki
763 1.74 ozaki /*
764 1.23 rpaulo * do not use a rejected or black hole route.
765 1.23 rpaulo * XXX: this check should be done in the L2 output routine.
766 1.23 rpaulo * However, if we skipped this check here, we'd see the following
767 1.23 rpaulo * scenario:
768 1.23 rpaulo * - install a rejected route for a scoped address prefix
769 1.23 rpaulo * (like fe80::/10)
770 1.23 rpaulo * - send a packet to a destination that matches the scoped prefix,
771 1.23 rpaulo * with ambiguity about the scope zone.
772 1.23 rpaulo * - pick the outgoing interface from the route, and disambiguate the
773 1.23 rpaulo * scope zone with the interface.
774 1.23 rpaulo * - ip6_output() would try to get another route with the "new"
775 1.23 rpaulo * destination, which may be valid.
776 1.23 rpaulo * - we'd see no error on output.
777 1.23 rpaulo * Although this may not be very harmful, it should still be confusing.
778 1.23 rpaulo * We thus reject the case here.
779 1.23 rpaulo */
780 1.76 ozaki if ((rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE))) {
781 1.76 ozaki error = (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
782 1.76 ozaki /* XXX: ifp can be returned with psref even if error */
783 1.76 ozaki goto out;
784 1.76 ozaki }
785 1.23 rpaulo
786 1.23 rpaulo /*
787 1.23 rpaulo * Adjust the "outgoing" interface. If we're going to loop the packet
788 1.23 rpaulo * back to ourselves, the ifp would be the loopback interface.
789 1.23 rpaulo * However, we'd rather know the interface associated to the
790 1.23 rpaulo * destination address (which should probably be one of our own
791 1.23 rpaulo * addresses.)
792 1.23 rpaulo */
793 1.86 ozaki if (rt->rt_ifa->ifa_ifp != *retifp &&
794 1.66 ozaki !if_is_deactivated(rt->rt_ifa->ifa_ifp)) {
795 1.62 ozaki if_put(*retifp, psref);
796 1.23 rpaulo *retifp = rt->rt_ifa->ifa_ifp;
797 1.79 ozaki if_acquire(*retifp, psref);
798 1.62 ozaki }
799 1.76 ozaki out:
800 1.76 ozaki rtcache_unref(rt, ro);
801 1.76 ozaki return error;
802 1.1 itojun }
803 1.1 itojun
804 1.38 christos /*
805 1.1 itojun * Default hop limit selection. The precedence is as follows:
806 1.1 itojun * 1. Hoplimit value specified via ioctl.
807 1.1 itojun * 2. (If the outgoing interface is detected) the current
808 1.1 itojun * hop limit of the interface specified by router advertisement.
809 1.1 itojun * 3. The system default hoplimit.
810 1.1 itojun */
811 1.1 itojun int
812 1.38 christos in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
813 1.1 itojun {
814 1.1 itojun if (in6p && in6p->in6p_hops >= 0)
815 1.15 itojun return (in6p->in6p_hops);
816 1.1 itojun else if (ifp)
817 1.15 itojun return (ND_IFINFO(ifp)->chlim);
818 1.1 itojun else
819 1.15 itojun return (ip6_defhlim);
820 1.1 itojun }
821 1.1 itojun
822 1.57 ozaki int
823 1.57 ozaki in6_selecthlim_rt(struct in6pcb *in6p)
824 1.57 ozaki {
825 1.57 ozaki struct rtentry *rt;
826 1.57 ozaki
827 1.57 ozaki if (in6p == NULL)
828 1.57 ozaki return in6_selecthlim(in6p, NULL);
829 1.57 ozaki
830 1.57 ozaki rt = rtcache_validate(&in6p->in6p_route);
831 1.76 ozaki if (rt != NULL) {
832 1.76 ozaki int ret = in6_selecthlim(in6p, rt->rt_ifp);
833 1.76 ozaki rtcache_unref(rt, &in6p->in6p_route);
834 1.76 ozaki return ret;
835 1.76 ozaki } else
836 1.57 ozaki return in6_selecthlim(in6p, NULL);
837 1.57 ozaki }
838 1.57 ozaki
839 1.1 itojun /*
840 1.1 itojun * Find an empty port and set it to the specified PCB.
841 1.1 itojun */
842 1.1 itojun int
843 1.47 elad in6_pcbsetport(struct sockaddr_in6 *sin6, struct in6pcb *in6p, struct lwp *l)
844 1.1 itojun {
845 1.1 itojun struct socket *so = in6p->in6p_socket;
846 1.17 itojun struct inpcbtable *table = in6p->in6p_table;
847 1.17 itojun u_int16_t lport, *lastport;
848 1.47 elad enum kauth_network_req req;
849 1.52 christos int error = 0;
850 1.68 roy
851 1.1 itojun if (in6p->in6p_flags & IN6P_LOWPORT) {
852 1.3 itojun #ifndef IPNOPRIVPORTS
853 1.47 elad req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
854 1.47 elad #else
855 1.47 elad req = KAUTH_REQ_NETWORK_BIND_PORT;
856 1.3 itojun #endif
857 1.17 itojun lastport = &table->inpt_lastlow;
858 1.1 itojun } else {
859 1.47 elad req = KAUTH_REQ_NETWORK_BIND_PORT;
860 1.47 elad
861 1.17 itojun lastport = &table->inpt_lastport;
862 1.1 itojun }
863 1.23 rpaulo
864 1.47 elad /* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */
865 1.47 elad error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND, req, so,
866 1.47 elad sin6, NULL);
867 1.47 elad if (error)
868 1.48 elad return (EACCES);
869 1.47 elad
870 1.52 christos /*
871 1.52 christos * Use RFC6056 randomized port selection
872 1.52 christos */
873 1.53 christos error = portalgo_randport(&lport, &in6p->in6p_head, l->l_cred);
874 1.52 christos if (error)
875 1.52 christos return error;
876 1.68 roy
877 1.17 itojun in6p->in6p_flags |= IN6P_ANONPORT;
878 1.17 itojun *lastport = lport;
879 1.17 itojun in6p->in6p_lport = htons(lport);
880 1.17 itojun in6_pcbstate(in6p, IN6P_BOUND);
881 1.15 itojun return (0); /* success */
882 1.2 itojun }
883 1.2 itojun
884 1.23 rpaulo void
885 1.39 dyoung addrsel_policy_init(void)
886 1.23 rpaulo {
887 1.23 rpaulo init_policy_queue();
888 1.23 rpaulo
889 1.23 rpaulo /* initialize the "last resort" policy */
890 1.46 cegger memset(&defaultaddrpolicy, 0, sizeof(defaultaddrpolicy));
891 1.23 rpaulo defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
892 1.23 rpaulo }
893 1.23 rpaulo
894 1.82 ozaki /*
895 1.82 ozaki * XXX: NOMPSAFE if a policy is set
896 1.82 ozaki */
897 1.23 rpaulo static struct in6_addrpolicy *
898 1.38 christos lookup_addrsel_policy(struct sockaddr_in6 *key)
899 1.23 rpaulo {
900 1.23 rpaulo struct in6_addrpolicy *match = NULL;
901 1.23 rpaulo
902 1.23 rpaulo match = match_addrsel_policy(key);
903 1.23 rpaulo
904 1.23 rpaulo if (match == NULL)
905 1.23 rpaulo match = &defaultaddrpolicy;
906 1.23 rpaulo else
907 1.23 rpaulo match->use++;
908 1.23 rpaulo
909 1.23 rpaulo return (match);
910 1.23 rpaulo }
911 1.23 rpaulo
912 1.2 itojun /*
913 1.23 rpaulo * Subroutines to manage the address selection policy table via sysctl.
914 1.2 itojun */
915 1.45 christos struct sel_walkarg {
916 1.23 rpaulo size_t w_total;
917 1.23 rpaulo size_t w_given;
918 1.36 christos void * w_where;
919 1.36 christos void *w_limit;
920 1.23 rpaulo };
921 1.23 rpaulo
922 1.59 christos int sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS);
923 1.23 rpaulo int
924 1.59 christos sysctl_net_inet6_addrctlpolicy(SYSCTLFN_ARGS)
925 1.23 rpaulo {
926 1.23 rpaulo int error = 0;
927 1.23 rpaulo int s;
928 1.23 rpaulo
929 1.23 rpaulo s = splsoftnet();
930 1.23 rpaulo
931 1.23 rpaulo if (newp) {
932 1.23 rpaulo error = EPERM;
933 1.23 rpaulo goto end;
934 1.23 rpaulo }
935 1.23 rpaulo if (oldp && oldlenp == NULL) {
936 1.23 rpaulo error = EINVAL;
937 1.23 rpaulo goto end;
938 1.23 rpaulo }
939 1.23 rpaulo if (oldp || oldlenp) {
940 1.45 christos struct sel_walkarg w;
941 1.24 christos size_t oldlen = *oldlenp;
942 1.23 rpaulo
943 1.36 christos memset(&w, 0, sizeof(w));
944 1.23 rpaulo w.w_given = oldlen;
945 1.23 rpaulo w.w_where = oldp;
946 1.23 rpaulo if (oldp)
947 1.36 christos w.w_limit = (char *)oldp + oldlen;
948 1.23 rpaulo
949 1.23 rpaulo error = walk_addrsel_policy(dump_addrsel_policyent, &w);
950 1.23 rpaulo
951 1.23 rpaulo *oldlenp = w.w_total;
952 1.23 rpaulo if (oldp && w.w_total > oldlen && error == 0)
953 1.23 rpaulo error = ENOMEM;
954 1.23 rpaulo }
955 1.23 rpaulo
956 1.23 rpaulo end:
957 1.23 rpaulo splx(s);
958 1.23 rpaulo
959 1.23 rpaulo return (error);
960 1.23 rpaulo }
961 1.23 rpaulo
962 1.2 itojun int
963 1.38 christos in6_src_ioctl(u_long cmd, void *data)
964 1.2 itojun {
965 1.23 rpaulo int i;
966 1.23 rpaulo struct in6_addrpolicy ent0;
967 1.2 itojun
968 1.23 rpaulo if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
969 1.23 rpaulo return (EOPNOTSUPP); /* check for safety */
970 1.2 itojun
971 1.23 rpaulo ent0 = *(struct in6_addrpolicy *)data;
972 1.2 itojun
973 1.23 rpaulo if (ent0.label == ADDR_LABEL_NOTAPP)
974 1.23 rpaulo return (EINVAL);
975 1.23 rpaulo /* check if the prefix mask is consecutive. */
976 1.23 rpaulo if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
977 1.23 rpaulo return (EINVAL);
978 1.23 rpaulo /* clear trailing garbages (if any) of the prefix address. */
979 1.23 rpaulo for (i = 0; i < 4; i++) {
980 1.23 rpaulo ent0.addr.sin6_addr.s6_addr32[i] &=
981 1.23 rpaulo ent0.addrmask.sin6_addr.s6_addr32[i];
982 1.23 rpaulo }
983 1.23 rpaulo ent0.use = 0;
984 1.2 itojun
985 1.23 rpaulo switch (cmd) {
986 1.23 rpaulo case SIOCAADDRCTL_POLICY:
987 1.23 rpaulo return (add_addrsel_policyent(&ent0));
988 1.23 rpaulo case SIOCDADDRCTL_POLICY:
989 1.23 rpaulo return (delete_addrsel_policyent(&ent0));
990 1.2 itojun }
991 1.2 itojun
992 1.23 rpaulo return (0); /* XXX: compromise compilers */
993 1.2 itojun }
994 1.2 itojun
995 1.2 itojun /*
996 1.23 rpaulo * The followings are implementation of the policy table using a
997 1.23 rpaulo * simple tail queue.
998 1.23 rpaulo * XXX such details should be hidden.
999 1.23 rpaulo * XXX implementation using binary tree should be more efficient.
1000 1.2 itojun */
1001 1.23 rpaulo struct addrsel_policyent {
1002 1.23 rpaulo TAILQ_ENTRY(addrsel_policyent) ape_entry;
1003 1.23 rpaulo struct in6_addrpolicy ape_policy;
1004 1.23 rpaulo };
1005 1.23 rpaulo
1006 1.23 rpaulo TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
1007 1.23 rpaulo
1008 1.23 rpaulo struct addrsel_policyhead addrsel_policytab;
1009 1.23 rpaulo
1010 1.23 rpaulo static void
1011 1.41 matt init_policy_queue(void)
1012 1.23 rpaulo {
1013 1.23 rpaulo TAILQ_INIT(&addrsel_policytab);
1014 1.23 rpaulo }
1015 1.23 rpaulo
1016 1.23 rpaulo static int
1017 1.38 christos add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
1018 1.23 rpaulo {
1019 1.55 matt struct addrsel_policyent *newpol, *pol;
1020 1.23 rpaulo
1021 1.23 rpaulo /* duplication check */
1022 1.55 matt TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) {
1023 1.23 rpaulo if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
1024 1.23 rpaulo &pol->ape_policy.addr.sin6_addr) &&
1025 1.23 rpaulo IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
1026 1.23 rpaulo &pol->ape_policy.addrmask.sin6_addr)) {
1027 1.23 rpaulo return (EEXIST); /* or override it? */
1028 1.23 rpaulo }
1029 1.23 rpaulo }
1030 1.23 rpaulo
1031 1.55 matt newpol = malloc(sizeof(*newpol), M_IFADDR, M_WAITOK|M_ZERO);
1032 1.23 rpaulo
1033 1.23 rpaulo /* XXX: should validate entry */
1034 1.55 matt newpol->ape_policy = *newpolicy;
1035 1.23 rpaulo
1036 1.55 matt TAILQ_INSERT_TAIL(&addrsel_policytab, newpol, ape_entry);
1037 1.23 rpaulo
1038 1.23 rpaulo return (0);
1039 1.23 rpaulo }
1040 1.23 rpaulo
1041 1.23 rpaulo static int
1042 1.38 christos delete_addrsel_policyent(struct in6_addrpolicy *key)
1043 1.2 itojun {
1044 1.23 rpaulo struct addrsel_policyent *pol;
1045 1.23 rpaulo
1046 1.23 rpaulo /* search for the entry in the table */
1047 1.23 rpaulo for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
1048 1.23 rpaulo pol = TAILQ_NEXT(pol, ape_entry)) {
1049 1.23 rpaulo if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr,
1050 1.23 rpaulo &pol->ape_policy.addr.sin6_addr) &&
1051 1.23 rpaulo IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr,
1052 1.23 rpaulo &pol->ape_policy.addrmask.sin6_addr)) {
1053 1.23 rpaulo break;
1054 1.23 rpaulo }
1055 1.23 rpaulo }
1056 1.23 rpaulo if (pol == NULL) {
1057 1.23 rpaulo return (ESRCH);
1058 1.23 rpaulo }
1059 1.2 itojun
1060 1.23 rpaulo TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry);
1061 1.2 itojun
1062 1.23 rpaulo return (0);
1063 1.23 rpaulo }
1064 1.23 rpaulo
1065 1.23 rpaulo static int
1066 1.35 dyoung walk_addrsel_policy(int (*callback)(struct in6_addrpolicy *, void *), void *w)
1067 1.23 rpaulo {
1068 1.23 rpaulo struct addrsel_policyent *pol;
1069 1.23 rpaulo int error = 0;
1070 1.2 itojun
1071 1.35 dyoung TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) {
1072 1.35 dyoung if ((error = (*callback)(&pol->ape_policy, w)) != 0)
1073 1.35 dyoung return error;
1074 1.2 itojun }
1075 1.2 itojun
1076 1.35 dyoung return error;
1077 1.5 itojun }
1078 1.5 itojun
1079 1.23 rpaulo static int
1080 1.35 dyoung dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
1081 1.23 rpaulo {
1082 1.23 rpaulo int error = 0;
1083 1.45 christos struct sel_walkarg *w = arg;
1084 1.23 rpaulo
1085 1.36 christos if (w->w_where && (char *)w->w_where + sizeof(*pol) <= (char *)w->w_limit) {
1086 1.23 rpaulo if ((error = copyout(pol, w->w_where, sizeof(*pol))) != 0)
1087 1.35 dyoung return error;
1088 1.36 christos w->w_where = (char *)w->w_where + sizeof(*pol);
1089 1.23 rpaulo }
1090 1.23 rpaulo w->w_total += sizeof(*pol);
1091 1.23 rpaulo
1092 1.35 dyoung return error;
1093 1.23 rpaulo }
1094 1.23 rpaulo
1095 1.23 rpaulo static struct in6_addrpolicy *
1096 1.38 christos match_addrsel_policy(struct sockaddr_in6 *key)
1097 1.5 itojun {
1098 1.23 rpaulo struct addrsel_policyent *pent;
1099 1.23 rpaulo struct in6_addrpolicy *bestpol = NULL, *pol;
1100 1.23 rpaulo int matchlen, bestmatchlen = -1;
1101 1.23 rpaulo u_char *mp, *ep, *k, *p, m;
1102 1.23 rpaulo
1103 1.23 rpaulo for (pent = TAILQ_FIRST(&addrsel_policytab); pent;
1104 1.23 rpaulo pent = TAILQ_NEXT(pent, ape_entry)) {
1105 1.23 rpaulo matchlen = 0;
1106 1.23 rpaulo
1107 1.23 rpaulo pol = &pent->ape_policy;
1108 1.23 rpaulo mp = (u_char *)&pol->addrmask.sin6_addr;
1109 1.23 rpaulo ep = mp + 16; /* XXX: scope field? */
1110 1.23 rpaulo k = (u_char *)&key->sin6_addr;
1111 1.23 rpaulo p = (u_char *)&pol->addr.sin6_addr;
1112 1.23 rpaulo for (; mp < ep && *mp; mp++, k++, p++) {
1113 1.23 rpaulo m = *mp;
1114 1.23 rpaulo if ((*k & m) != *p)
1115 1.23 rpaulo goto next; /* not match */
1116 1.23 rpaulo if (m == 0xff) /* short cut for a typical case */
1117 1.23 rpaulo matchlen += 8;
1118 1.23 rpaulo else {
1119 1.23 rpaulo while (m >= 0x80) {
1120 1.23 rpaulo matchlen++;
1121 1.23 rpaulo m <<= 1;
1122 1.23 rpaulo }
1123 1.23 rpaulo }
1124 1.23 rpaulo }
1125 1.23 rpaulo
1126 1.23 rpaulo /* matched. check if this is better than the current best. */
1127 1.23 rpaulo if (bestpol == NULL ||
1128 1.23 rpaulo matchlen > bestmatchlen) {
1129 1.23 rpaulo bestpol = pol;
1130 1.23 rpaulo bestmatchlen = matchlen;
1131 1.23 rpaulo }
1132 1.23 rpaulo
1133 1.23 rpaulo next:
1134 1.23 rpaulo continue;
1135 1.23 rpaulo }
1136 1.23 rpaulo
1137 1.23 rpaulo return (bestpol);
1138 1.1 itojun }
1139