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