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