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