in6_src.c revision 1.37 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.37 2007/05/02 20:40:26 dyoung 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
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/ip.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet6/in6_var.h>
103 #include <netinet/ip6.h>
104 #ifndef __OpenBSD__
105 #include <netinet6/in6_pcb.h>
106 #endif
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(struct sockaddr_in6 *, struct ip6_pktopts *,
137 struct ip6_moptions *, struct route *, struct ifnet **,
138 struct rtentry **, int, int);
139 static int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
140 struct ip6_moptions *, struct route *, struct ifnet **);
141
142 static struct in6_addrpolicy *lookup_addrsel_policy(struct sockaddr_in6 *);
143
144 static void init_policy_queue(void);
145 static int add_addrsel_policyent(struct in6_addrpolicy *);
146 static int delete_addrsel_policyent(struct in6_addrpolicy *);
147 static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
148 void *);
149 static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
150 static struct in6_addrpolicy *match_addrsel_policy(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 *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 struct route *ro;
586 struct ifnet **retifp;
587 struct rtentry **retrt;
588 int clone;
589 int norouteok;
590 {
591 int error = 0;
592 struct ifnet *ifp = NULL;
593 struct rtentry *rt = NULL;
594 struct sockaddr_in6 *sin6_next;
595 struct in6_pktinfo *pi = NULL;
596 struct in6_addr *dst;
597
598 dst = &dstsock->sin6_addr;
599
600 #if 0
601 if (dstsock->sin6_addr.s6_addr32[0] == 0 &&
602 dstsock->sin6_addr.s6_addr32[1] == 0 &&
603 !IN6_IS_ADDR_LOOPBACK(&dstsock->sin6_addr)) {
604 printf("in6_selectroute: strange destination %s\n",
605 ip6_sprintf(&dstsock->sin6_addr));
606 } else {
607 printf("in6_selectroute: destination = %s%%%d\n",
608 ip6_sprintf(&dstsock->sin6_addr),
609 dstsock->sin6_scope_id); /* for debug */
610 }
611 #endif
612
613 /* If the caller specify the outgoing interface explicitly, use it. */
614 if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
615 /* XXX boundary check is assumed to be already done. */
616 #ifdef __FreeBSD__
617 ifp = ifnet_byindex(pi->ipi6_ifindex);
618 #else
619 ifp = ifindex2ifnet[pi->ipi6_ifindex];
620 #endif
621 if (ifp != NULL &&
622 (norouteok || retrt == NULL ||
623 IN6_IS_ADDR_MULTICAST(dst))) {
624 /*
625 * we do not have to check or get the route for
626 * multicast.
627 */
628 goto done;
629 } else
630 goto getroute;
631 }
632
633 /*
634 * If the destination address is a multicast address and the outgoing
635 * interface for the address is specified by the caller, use it.
636 */
637 if (IN6_IS_ADDR_MULTICAST(dst) &&
638 mopts != NULL && (ifp = mopts->im6o_multicast_ifp) != NULL) {
639 goto done; /* we do not need a route for multicast. */
640 }
641
642 getroute:
643 /*
644 * If the next hop address for the packet is specified by the caller,
645 * use it as the gateway.
646 */
647 if (opts && opts->ip6po_nexthop) {
648 struct route *ron;
649
650 sin6_next = satosin6(opts->ip6po_nexthop);
651
652 /* at this moment, we only support AF_INET6 next hops */
653 if (sin6_next->sin6_family != AF_INET6) {
654 error = EAFNOSUPPORT; /* or should we proceed? */
655 goto done;
656 }
657
658 /*
659 * If the next hop is an IPv6 address, then the node identified
660 * by that address must be a neighbor of the sending host.
661 */
662 ron = &opts->ip6po_nextroute;
663 if ((rt = rtcache_lookup(ron, sin6tosa(sin6_next))) == NULL ||
664 (rt->rt_flags & RTF_GATEWAY) != 0 ||
665 !nd6_is_addr_neighbor(sin6_next, rt->rt_ifp)) {
666 rtcache_free(ron);
667 error = EHOSTUNREACH;
668 goto done;
669 }
670 ifp = rt->rt_ifp;
671
672 /*
673 * When cloning is required, try to allocate a route to the
674 * destination so that the caller can store path MTU
675 * information.
676 */
677 if (!clone)
678 goto done;
679 }
680
681 /*
682 * Use a cached route if it exists and is valid, else try to allocate
683 * a new one. Note that we should check the address family of the
684 * cached destination, in case of sharing the cache with IPv4.
685 */
686 if (ro != NULL) {
687 union {
688 struct sockaddr dst;
689 struct sockaddr_in6 dst6;
690 } u;
691
692 /* No route yet, so try to acquire one */
693 u.dst6 = *dstsock;
694 u.dst6.sin6_scope_id = 0;
695 rt = rtcache_lookup1(ro, &u.dst, clone);
696
697 /*
698 * do not care about the result if we have the nexthop
699 * explicitly specified.
700 */
701 if (opts && opts->ip6po_nexthop)
702 goto done;
703
704 if (rt == NULL)
705 error = EHOSTUNREACH;
706 else
707 ifp = rt->rt_ifp;
708
709 /*
710 * Check if the outgoing interface conflicts with
711 * the interface specified by ipi6_ifindex (if specified).
712 * Note that loopback interface is always okay.
713 * (this may happen when we are sending a packet to one of
714 * our own addresses.)
715 */
716 if (opts && opts->ip6po_pktinfo &&
717 opts->ip6po_pktinfo->ipi6_ifindex) {
718 if (!(ifp->if_flags & IFF_LOOPBACK) &&
719 ifp->if_index !=
720 opts->ip6po_pktinfo->ipi6_ifindex) {
721 error = EHOSTUNREACH;
722 goto done;
723 }
724 }
725 }
726
727 done:
728 if (ifp == NULL && rt == NULL) {
729 /*
730 * This can happen if the caller did not pass a cached route
731 * nor any other hints. We treat this case an error.
732 */
733 error = EHOSTUNREACH;
734 }
735 if (error == EHOSTUNREACH)
736 ip6stat.ip6s_noroute++;
737
738 if (retifp != NULL)
739 *retifp = ifp;
740 if (retrt != NULL)
741 *retrt = rt; /* rt may be NULL */
742
743 return (error);
744 }
745
746 static int
747 in6_selectif(dstsock, opts, mopts, ro, retifp)
748 struct sockaddr_in6 *dstsock;
749 struct ip6_pktopts *opts;
750 struct ip6_moptions *mopts;
751 struct route *ro;
752 struct ifnet **retifp;
753 {
754 int error, clone;
755 struct rtentry *rt = NULL;
756
757 clone = IN6_IS_ADDR_MULTICAST(&dstsock->sin6_addr) ? 0 : 1;
758 if ((error = selectroute(dstsock, opts, mopts, ro, retifp,
759 &rt, clone, 1)) != 0) {
760 return (error);
761 }
762
763 /*
764 * do not use a rejected or black hole route.
765 * XXX: this check should be done in the L2 output routine.
766 * However, if we skipped this check here, we'd see the following
767 * scenario:
768 * - install a rejected route for a scoped address prefix
769 * (like fe80::/10)
770 * - send a packet to a destination that matches the scoped prefix,
771 * with ambiguity about the scope zone.
772 * - pick the outgoing interface from the route, and disambiguate the
773 * scope zone with the interface.
774 * - ip6_output() would try to get another route with the "new"
775 * destination, which may be valid.
776 * - we'd see no error on output.
777 * Although this may not be very harmful, it should still be confusing.
778 * We thus reject the case here.
779 */
780 if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE)))
781 return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
782
783 /*
784 * Adjust the "outgoing" interface. If we're going to loop the packet
785 * back to ourselves, the ifp would be the loopback interface.
786 * However, we'd rather know the interface associated to the
787 * destination address (which should probably be one of our own
788 * addresses.)
789 */
790 if (rt && rt->rt_ifa && rt->rt_ifa->ifa_ifp)
791 *retifp = rt->rt_ifa->ifa_ifp;
792
793 return (0);
794 }
795
796 int
797 in6_selectroute(dstsock, opts, mopts, ro, retifp, retrt, clone)
798 struct sockaddr_in6 *dstsock;
799 struct ip6_pktopts *opts;
800 struct ip6_moptions *mopts;
801 struct route *ro;
802 struct ifnet **retifp;
803 struct rtentry **retrt;
804 int clone; /* meaningful only for bsdi and freebsd. */
805 {
806 return selectroute(dstsock, opts, mopts, ro, retifp,
807 retrt, clone, 0);
808 }
809
810 /*
811 * Default hop limit selection. The precedence is as follows:
812 * 1. Hoplimit value specified via ioctl.
813 * 2. (If the outgoing interface is detected) the current
814 * hop limit of the interface specified by router advertisement.
815 * 3. The system default hoplimit.
816 */
817 int
818 in6_selecthlim(in6p, ifp)
819 struct in6pcb *in6p;
820 struct ifnet *ifp;
821 {
822 if (in6p && in6p->in6p_hops >= 0)
823 return (in6p->in6p_hops);
824 else if (ifp)
825 return (ND_IFINFO(ifp)->chlim);
826 else
827 return (ip6_defhlim);
828 }
829
830 /*
831 * Find an empty port and set it to the specified PCB.
832 */
833 int
834 in6_pcbsetport(laddr, in6p, l)
835 struct in6_addr *laddr;
836 struct in6pcb *in6p;
837 struct lwp *l;
838 {
839 struct socket *so = in6p->in6p_socket;
840 struct inpcbtable *table = in6p->in6p_table;
841 int cnt;
842 u_int16_t minport, maxport;
843 u_int16_t lport, *lastport;
844 int wild = 0;
845 void *t;
846
847 /* XXX: this is redundant when called from in6_pcbbind */
848 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
849 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
850 (so->so_options & SO_ACCEPTCONN) == 0))
851 wild = 1;
852
853 if (in6p->in6p_flags & IN6P_LOWPORT) {
854 #ifndef IPNOPRIVPORTS
855 if (l == 0 || (kauth_authorize_generic(l->l_cred,
856 KAUTH_GENERIC_ISSUSER, NULL) != 0))
857 return (EACCES);
858 #endif
859 minport = ip6_lowportmin;
860 maxport = ip6_lowportmax;
861 lastport = &table->inpt_lastlow;
862 } else {
863 minport = ip6_anonportmin;
864 maxport = ip6_anonportmax;
865 lastport = &table->inpt_lastport;
866 }
867
868 if (minport > maxport) { /* sanity check */
869 u_int16_t swp;
870
871 swp = minport;
872 minport = maxport;
873 maxport = swp;
874 }
875
876 lport = *lastport - 1;
877 for (cnt = maxport - minport + 1; cnt; cnt--, lport--) {
878 if (lport < minport || lport > maxport)
879 lport = maxport;
880 #ifdef INET
881 if (IN6_IS_ADDR_V4MAPPED(laddr)) {
882 t = in_pcblookup_port(table,
883 *(struct in_addr *)&laddr->s6_addr32[3],
884 lport, wild);
885 } else
886 #endif
887 {
888 t = in6_pcblookup_port(table, laddr, lport, wild);
889 }
890 if (t == 0)
891 goto found;
892 }
893
894 return (EAGAIN);
895
896 found:
897 in6p->in6p_flags |= IN6P_ANONPORT;
898 *lastport = lport;
899 in6p->in6p_lport = htons(lport);
900 in6_pcbstate(in6p, IN6P_BOUND);
901 return (0); /* success */
902 }
903
904 void
905 addrsel_policy_init()
906 {
907 init_policy_queue();
908
909 /* initialize the "last resort" policy */
910 bzero(&defaultaddrpolicy, sizeof(defaultaddrpolicy));
911 defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
912 }
913
914 static struct in6_addrpolicy *
915 lookup_addrsel_policy(key)
916 struct sockaddr_in6 *key;
917 {
918 struct in6_addrpolicy *match = NULL;
919
920 match = match_addrsel_policy(key);
921
922 if (match == NULL)
923 match = &defaultaddrpolicy;
924 else
925 match->use++;
926
927 return (match);
928 }
929
930 /*
931 * Subroutines to manage the address selection policy table via sysctl.
932 */
933 struct walkarg {
934 size_t w_total;
935 size_t w_given;
936 void * w_where;
937 void *w_limit;
938 };
939
940 int
941 in6_src_sysctl(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
942 {
943 int error = 0;
944 int s;
945
946 s = splsoftnet();
947
948 if (newp) {
949 error = EPERM;
950 goto end;
951 }
952 if (oldp && oldlenp == NULL) {
953 error = EINVAL;
954 goto end;
955 }
956 if (oldp || oldlenp) {
957 struct walkarg w;
958 size_t oldlen = *oldlenp;
959
960 memset(&w, 0, sizeof(w));
961 w.w_given = oldlen;
962 w.w_where = oldp;
963 if (oldp)
964 w.w_limit = (char *)oldp + oldlen;
965
966 error = walk_addrsel_policy(dump_addrsel_policyent, &w);
967
968 *oldlenp = w.w_total;
969 if (oldp && w.w_total > oldlen && error == 0)
970 error = ENOMEM;
971 }
972
973 end:
974 splx(s);
975
976 return (error);
977 }
978
979 int
980 in6_src_ioctl(cmd, data)
981 u_long cmd;
982 void *data;
983 {
984 int i;
985 struct in6_addrpolicy ent0;
986
987 if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
988 return (EOPNOTSUPP); /* check for safety */
989
990 ent0 = *(struct in6_addrpolicy *)data;
991
992 if (ent0.label == ADDR_LABEL_NOTAPP)
993 return (EINVAL);
994 /* check if the prefix mask is consecutive. */
995 if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
996 return (EINVAL);
997 /* clear trailing garbages (if any) of the prefix address. */
998 for (i = 0; i < 4; i++) {
999 ent0.addr.sin6_addr.s6_addr32[i] &=
1000 ent0.addrmask.sin6_addr.s6_addr32[i];
1001 }
1002 ent0.use = 0;
1003
1004 switch (cmd) {
1005 case SIOCAADDRCTL_POLICY:
1006 return (add_addrsel_policyent(&ent0));
1007 case SIOCDADDRCTL_POLICY:
1008 return (delete_addrsel_policyent(&ent0));
1009 }
1010
1011 return (0); /* XXX: compromise compilers */
1012 }
1013
1014 /*
1015 * The followings are implementation of the policy table using a
1016 * simple tail queue.
1017 * XXX such details should be hidden.
1018 * XXX implementation using binary tree should be more efficient.
1019 */
1020 struct addrsel_policyent {
1021 TAILQ_ENTRY(addrsel_policyent) ape_entry;
1022 struct in6_addrpolicy ape_policy;
1023 };
1024
1025 TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
1026
1027 struct addrsel_policyhead addrsel_policytab;
1028
1029 static void
1030 init_policy_queue()
1031 {
1032 TAILQ_INIT(&addrsel_policytab);
1033 }
1034
1035 static int
1036 add_addrsel_policyent(newpolicy)
1037 struct in6_addrpolicy *newpolicy;
1038 {
1039 struct addrsel_policyent *new, *pol;
1040
1041 /* duplication check */
1042 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
1043 pol = TAILQ_NEXT(pol, ape_entry)) {
1044 if (IN6_ARE_ADDR_EQUAL(&newpolicy->addr.sin6_addr,
1045 &pol->ape_policy.addr.sin6_addr) &&
1046 IN6_ARE_ADDR_EQUAL(&newpolicy->addrmask.sin6_addr,
1047 &pol->ape_policy.addrmask.sin6_addr)) {
1048 return (EEXIST); /* or override it? */
1049 }
1050 }
1051
1052 MALLOC(new, struct addrsel_policyent *, sizeof(*new), M_IFADDR,
1053 M_WAITOK);
1054 bzero(new, sizeof(*new));
1055
1056 /* XXX: should validate entry */
1057 new->ape_policy = *newpolicy;
1058
1059 TAILQ_INSERT_TAIL(&addrsel_policytab, new, ape_entry);
1060
1061 return (0);
1062 }
1063
1064 static int
1065 delete_addrsel_policyent(key)
1066 struct in6_addrpolicy *key;
1067 {
1068 struct addrsel_policyent *pol;
1069
1070 /* search for the entry in the table */
1071 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
1072 pol = TAILQ_NEXT(pol, ape_entry)) {
1073 if (IN6_ARE_ADDR_EQUAL(&key->addr.sin6_addr,
1074 &pol->ape_policy.addr.sin6_addr) &&
1075 IN6_ARE_ADDR_EQUAL(&key->addrmask.sin6_addr,
1076 &pol->ape_policy.addrmask.sin6_addr)) {
1077 break;
1078 }
1079 }
1080 if (pol == NULL) {
1081 return (ESRCH);
1082 }
1083
1084 TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry);
1085
1086 return (0);
1087 }
1088
1089 static int
1090 walk_addrsel_policy(int (*callback)(struct in6_addrpolicy *, void *), void *w)
1091 {
1092 struct addrsel_policyent *pol;
1093 int error = 0;
1094
1095 TAILQ_FOREACH(pol, &addrsel_policytab, ape_entry) {
1096 if ((error = (*callback)(&pol->ape_policy, w)) != 0)
1097 return error;
1098 }
1099
1100 return error;
1101 }
1102
1103 static int
1104 dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
1105 {
1106 int error = 0;
1107 struct walkarg *w = arg;
1108
1109 if (w->w_where && (char *)w->w_where + sizeof(*pol) <= (char *)w->w_limit) {
1110 if ((error = copyout(pol, w->w_where, sizeof(*pol))) != 0)
1111 return error;
1112 w->w_where = (char *)w->w_where + sizeof(*pol);
1113 }
1114 w->w_total += sizeof(*pol);
1115
1116 return error;
1117 }
1118
1119 static struct in6_addrpolicy *
1120 match_addrsel_policy(key)
1121 struct sockaddr_in6 *key;
1122 {
1123 struct addrsel_policyent *pent;
1124 struct in6_addrpolicy *bestpol = NULL, *pol;
1125 int matchlen, bestmatchlen = -1;
1126 u_char *mp, *ep, *k, *p, m;
1127
1128 for (pent = TAILQ_FIRST(&addrsel_policytab); pent;
1129 pent = TAILQ_NEXT(pent, ape_entry)) {
1130 matchlen = 0;
1131
1132 pol = &pent->ape_policy;
1133 mp = (u_char *)&pol->addrmask.sin6_addr;
1134 ep = mp + 16; /* XXX: scope field? */
1135 k = (u_char *)&key->sin6_addr;
1136 p = (u_char *)&pol->addr.sin6_addr;
1137 for (; mp < ep && *mp; mp++, k++, p++) {
1138 m = *mp;
1139 if ((*k & m) != *p)
1140 goto next; /* not match */
1141 if (m == 0xff) /* short cut for a typical case */
1142 matchlen += 8;
1143 else {
1144 while (m >= 0x80) {
1145 matchlen++;
1146 m <<= 1;
1147 }
1148 }
1149 }
1150
1151 /* matched. check if this is better than the current best. */
1152 if (bestpol == NULL ||
1153 matchlen > bestmatchlen) {
1154 bestpol = pol;
1155 bestmatchlen = matchlen;
1156 }
1157
1158 next:
1159 continue;
1160 }
1161
1162 return (bestpol);
1163 }
1164