1 /* $FreeBSD: src/sys/netinet6/in6_src.c,v 1.1.2.3 2002/02/26 18:02:06 ume Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6_src.c,v 1.13 2006/12/29 18:02:56 victor Exp $ */
3 /* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
70 #include "opt_inet6.h"
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/sockio.h>
82 #include <sys/sysctl.h>
83 #include <sys/errno.h>
88 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #ifdef ENABLE_DEFAULT_SCOPE
101 #include <netinet6/scope6_var.h>
104 #include <net/net_osdep.h>
106 #include "use_loop.h"
108 #define ADDR_LABEL_NOTAPP (-1)
109 struct in6_addrpolicy defaultaddrpolicy;
111 static void init_policy_queue(void);
112 static int add_addrsel_policyent(struct in6_addrpolicy *);
113 static int delete_addrsel_policyent(struct in6_addrpolicy *);
114 static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
116 static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
120 * Return an IPv6 address, which is the most appropriate for a given
121 * destination and user specified options.
122 * If necessary, this function lookups the routing table and returns
123 * an entry to the caller for later use.
126 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
127 struct ip6_moptions *mopts, struct route_in6 *ro,
128 struct in6_addr *laddr, int *errorp, struct thread *td)
130 struct sockaddr_in6 jsin6;
131 struct ucred *cred = NULL;
132 struct in6_addr *dst;
133 struct in6_ifaddr *ia6 = 0;
134 struct in6_pktinfo *pi = NULL;
137 if (td && td->td_proc && td->td_proc->p_ucred)
138 cred = td->td_proc->p_ucred;
139 if (cred && cred->cr_prison)
141 jsin6.sin6_family = AF_INET6;
142 dst = &dstsock->sin6_addr;
146 * If the source address is explicitly specified by the caller,
149 if (opts && (pi = opts->ip6po_pktinfo) &&
150 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
151 jsin6.sin6_addr = pi->ipi6_addr;
152 if (jailed && !jailed_ip(cred->cr_prison,
153 (struct sockaddr *)&jsin6)) {
156 return (&pi->ipi6_addr);
161 * If the source address is not specified but the socket(if any)
162 * is already bound, use the bound address.
164 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
165 jsin6.sin6_addr = *laddr;
166 if (jailed && !jailed_ip(cred->cr_prison,
167 (struct sockaddr *)&jsin6)) {
175 * If the caller doesn't specify the source address but
176 * the outgoing interface, use an address associated with
179 if (pi && pi->ipi6_ifindex) {
180 /* XXX boundary check is assumed to be already done. */
181 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
185 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
186 if (!jailed_ip(cred->cr_prison,
187 (struct sockaddr *)&jsin6))
192 *errorp = EADDRNOTAVAIL;
195 return (&satosin6(&ia6->ia_addr)->sin6_addr);
199 * If the destination address is a link-local unicast address or
200 * a multicast address, and if the outgoing interface is specified
201 * by the sin6_scope_id filed, use an address associated with the
203 * XXX: We're now trying to define more specific semantics of
204 * sin6_scope_id field, so this part will be rewritten in
207 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
208 dstsock->sin6_scope_id) {
210 * I'm not sure if boundary check for scope_id is done
213 if (dstsock->sin6_scope_id < 0 ||
214 if_index < dstsock->sin6_scope_id) {
215 *errorp = ENXIO; /* XXX: better error? */
218 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
222 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
223 if (!jailed_ip(cred->cr_prison,
224 (struct sockaddr *)&jsin6))
229 *errorp = EADDRNOTAVAIL;
232 return (&satosin6(&ia6->ia_addr)->sin6_addr);
236 * If the destination address is a multicast address and
237 * the outgoing interface for the address is specified
238 * by the caller, use an address associated with the interface.
239 * There is a sanity check here; if the destination has node-local
240 * scope, the outgoing interfacde should be a loopback address.
241 * Even if the outgoing interface is not specified, we also
242 * choose a loopback interface as the outgoing interface.
244 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
245 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
247 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
252 ia6 = in6_ifawithscope(ifp, dst);
254 *errorp = EADDRNOTAVAIL;
257 return (&satosin6(&ia6->ia_addr)->sin6_addr);
262 * If the next hop address for the packet is specified
263 * by caller, use an address associated with the route
267 struct sockaddr_in6 *sin6_next;
270 if (opts && opts->ip6po_nexthop) {
271 sin6_next = satosin6(opts->ip6po_nexthop);
272 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
274 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
276 ia6 = ifatoia6(rt->rt_ifa);
279 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
280 if (!jailed_ip(cred->cr_prison,
281 (struct sockaddr *)&jsin6))
286 *errorp = EADDRNOTAVAIL;
289 return (&satosin6(&ia6->ia_addr)->sin6_addr);
294 * If route is known or can be allocated now,
295 * our src addr is taken from the i/f, else punt.
299 (!(ro->ro_rt->rt_flags & RTF_UP) ||
300 satosin6(&ro->ro_dst)->sin6_family != AF_INET6 ||
301 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr,
306 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
307 struct sockaddr_in6 *sa6;
309 /* No route yet, so try to acquire one */
310 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
312 sa6->sin6_family = AF_INET6;
313 sa6->sin6_len = sizeof(struct sockaddr_in6);
314 sa6->sin6_addr = *dst;
315 sa6->sin6_scope_id = dstsock->sin6_scope_id;
316 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
318 rtpurelookup((struct sockaddr *)&ro->ro_dst);
320 rtalloc((struct route *)ro);
325 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
326 * the address. But we don't know why it does so.
327 * It is necessary to ensure the scope even for lo0
328 * so doesn't check out IFF_LOOPBACK.
332 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
334 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
335 if (!jailed_ip(cred->cr_prison,
336 (struct sockaddr *)&jsin6))
340 if (ia6 == 0) /* xxx scope error ?*/
341 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
344 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
345 if (!jailed_ip(cred->cr_prison,
346 (struct sockaddr *)&jsin6))
352 * xxx The followings are necessary? (kazu)
354 * It's for SO_DONTROUTE option in IPv4.(jinmei)
357 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
359 sin6->sin6_addr = *dst;
361 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
363 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
366 return (&satosin6(&ia6->ia_addr)->sin6_addr);
370 *errorp = EHOSTUNREACH; /* no route */
373 return (&satosin6(&ia6->ia_addr)->sin6_addr);
376 *errorp = EADDRNOTAVAIL;
381 * Default hop limit selection. The precedence is as follows:
382 * 1. Hoplimit value specified via ioctl.
383 * 2. (If the outgoing interface is detected) the current
384 * hop limit of the interface specified by router advertisement.
385 * 3. The system default hoplimit.
388 in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
390 if (in6p && in6p->in6p_hops >= 0)
391 return (in6p->in6p_hops);
393 return (ND_IFINFO(ifp)->chlim);
395 return (ip6_defhlim);
399 * XXX: this is borrowed from in6_pcbbind(). If possible, we should
400 * share this function by all *bsd*...
403 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct thread *td)
405 struct socket *so = inp->inp_socket;
406 u_int16_t lport = 0, first, last, *lastport;
407 int count, error = 0, wild = 0;
408 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
409 struct ucred *cred = NULL;
411 /* XXX: this is redundant when called from in6_pcbbind */
412 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
413 wild = INPLOOKUP_WILDCARD;
414 if (td->td_proc && td->td_proc->p_ucred)
415 cred = td->td_proc->p_ucred;
417 inp->inp_flags |= INP_ANONPORT;
419 if (inp->inp_flags & INP_HIGHPORT) {
420 first = ipport_hifirstauto; /* sysctl */
421 last = ipport_hilastauto;
422 lastport = &pcbinfo->lasthi;
423 } else if (inp->inp_flags & INP_LOWPORT) {
424 if ((error = suser(td)) != 0)
426 first = ipport_lowfirstauto; /* 1023 */
427 last = ipport_lowlastauto; /* 600 */
428 lastport = &pcbinfo->lastlow;
430 first = ipport_firstauto; /* sysctl */
431 last = ipport_lastauto;
432 lastport = &pcbinfo->lastport;
435 * Simple check to ensure all ports are not used up causing
438 * We split the two cases (up and down) so that the direction
439 * is not being tested on each round of the loop.
445 count = first - last;
448 if (count-- < 0) { /* completely used? */
450 * Undo any address bind that may have
453 inp->in6p_laddr = kin6addr_any;
457 if (*lastport > first || *lastport < last)
459 lport = htons(*lastport);
460 } while (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr,
466 count = last - first;
469 if (count-- < 0) { /* completely used? */
471 * Undo any address bind that may have
474 inp->in6p_laddr = kin6addr_any;
478 if (*lastport < first || *lastport > last)
480 lport = htons(*lastport);
481 } while (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr,
485 inp->inp_lport = lport;
486 if (in_pcbinsporthash(inp) != 0) {
487 inp->in6p_laddr = kin6addr_any;
496 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
497 * If the address scope of is link-local, embed the interface index in the
498 * address. The routine determines our precedence
499 * between advanced API scope/interface specification and basic API
502 * this function should be nuked in the future, when we get rid of
503 * embedded scopeid thing.
505 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
506 * there can be multiple interfaces that belong to a particular scope zone
507 * (in specification, we have 1:N mapping between a scope zone and interfaces).
508 * we may want to change the function to return something other than ifp.
511 in6_embedscope(struct in6_addr *in6,
512 const struct sockaddr_in6 *sin6,
513 #ifdef HAVE_NRL_INPCB
515 #define in6p_outputopts inp_outputopts6
516 #define in6p_moptions inp_moptions6
522 struct ifnet *ifp = NULL;
525 *in6 = sin6->sin6_addr;
526 scopeid = sin6->sin6_scope_id;
531 * don't try to read sin6->sin6_addr beyond here, since the caller may
532 * ask us to overwrite existing sockaddr_in6
535 #ifdef ENABLE_DEFAULT_SCOPE
537 scopeid = scope6_addr2default(in6);
540 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
541 struct in6_pktinfo *pi;
544 * KAME assumption: link id == interface id
547 if (in6p && in6p->in6p_outputopts &&
548 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
550 ifp = ifindex2ifnet[pi->ipi6_ifindex];
551 in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
552 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
553 in6p->in6p_moptions &&
554 in6p->in6p_moptions->im6o_multicast_ifp) {
555 ifp = in6p->in6p_moptions->im6o_multicast_ifp;
556 in6->s6_addr16[1] = htons(ifp->if_index);
557 } else if (scopeid) {
559 if (scopeid < 0 || if_index < scopeid)
560 return ENXIO; /* XXX EINVAL? */
561 ifp = ifindex2ifnet[scopeid];
562 /*XXX assignment to 16bit from 32bit variable */
563 in6->s6_addr16[1] = htons(scopeid & 0xffff);
572 #ifdef HAVE_NRL_INPCB
573 #undef in6p_outputopts
578 * generate standard sockaddr_in6 from embedded form.
579 * touches sin6_addr and sin6_scope_id only.
581 * this function should be nuked in the future, when we get rid of
582 * embedded scopeid thing.
585 in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6,
590 sin6->sin6_addr = *in6;
593 * don't try to read *in6 beyond here, since the caller may
594 * ask us to overwrite existing sockaddr_in6
597 sin6->sin6_scope_id = 0;
598 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
600 * KAME assumption: link id == interface id
602 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
605 if (scopeid < 0 || if_index < scopeid)
607 if (ifp && ifp->if_index != scopeid)
609 sin6->sin6_addr.s6_addr16[1] = 0;
610 sin6->sin6_scope_id = scopeid;
618 * just clear the embedded scope identifer.
619 * XXX: currently used for bsdi4 only as a supplement function.
622 in6_clearscope(struct in6_addr *addr)
624 if (IN6_IS_SCOPE_LINKLOCAL(addr))
625 addr->s6_addr16[1] = 0;
629 addrsel_policy_init(void)
634 /* initialize the "last resort" policy */
635 bzero(&defaultaddrpolicy, sizeof(defaultaddrpolicy));
636 defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
640 * Subroutines to manage the address selection policy table via sysctl.
643 struct sysctl_req *w_req;
646 static int in6_src_sysctl(SYSCTL_HANDLER_ARGS);
647 SYSCTL_DECL(_net_inet6_ip6);
648 SYSCTL_NODE(_net_inet6_ip6, IPV6CTL_ADDRCTLPOLICY, addrctlpolicy,
649 CTLFLAG_RD, in6_src_sysctl, "");
652 in6_src_sysctl(SYSCTL_HANDLER_ARGS)
659 bzero(&w, sizeof(w));
662 return (walk_addrsel_policy(dump_addrsel_policyent, &w));
666 in6_src_ioctl(u_long cmd, caddr_t data)
669 struct in6_addrpolicy ent0;
671 if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
672 return (EOPNOTSUPP); /* check for safety */
674 ent0 = *(struct in6_addrpolicy *)data;
676 if (ent0.label == ADDR_LABEL_NOTAPP)
678 /* check if the prefix mask is consecutive. */
679 if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
681 /* clear trailing garbages (if any) of the prefix address. */
682 for (i = 0; i < 4; i++) {
683 ent0.addr.sin6_addr.s6_addr32[i] &=
684 ent0.addrmask.sin6_addr.s6_addr32[i];
689 case SIOCAADDRCTL_POLICY:
690 return (add_addrsel_policyent(&ent0));
691 case SIOCDADDRCTL_POLICY:
692 return (delete_addrsel_policyent(&ent0));
695 return (0); /* XXX: compromise compilers */
699 * The followings are implementation of the policy table using a
701 * XXX such details should be hidden.
702 * XXX implementation using binary tree should be more efficient.
704 struct addrsel_policyent {
705 TAILQ_ENTRY(addrsel_policyent) ape_entry;
706 struct in6_addrpolicy ape_policy;
709 TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
711 struct addrsel_policyhead addrsel_policytab;
714 init_policy_queue(void)
716 TAILQ_INIT(&addrsel_policytab);
720 add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
722 struct addrsel_policyent *new, *pol;
724 /* duplication check */
725 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
726 pol = TAILQ_NEXT(pol, ape_entry)) {
727 if (SA6_ARE_ADDR_EQUAL(&newpolicy->addr,
728 &pol->ape_policy.addr) &&
729 SA6_ARE_ADDR_EQUAL(&newpolicy->addrmask,
730 &pol->ape_policy.addrmask)) {
731 return (EEXIST); /* or override it? */
735 new = kmalloc(sizeof(*new), M_IFADDR, M_WAITOK | M_ZERO);
737 /* XXX: should validate entry */
738 new->ape_policy = *newpolicy;
740 TAILQ_INSERT_TAIL(&addrsel_policytab, new, ape_entry);
746 delete_addrsel_policyent(struct in6_addrpolicy *key)
748 struct addrsel_policyent *pol;
750 /* search for the entry in the table */
751 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
752 pol = TAILQ_NEXT(pol, ape_entry)) {
753 if (SA6_ARE_ADDR_EQUAL(&key->addr, &pol->ape_policy.addr) &&
754 SA6_ARE_ADDR_EQUAL(&key->addrmask,
755 &pol->ape_policy.addrmask)) {
762 TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry);
763 kfree(pol, M_IFADDR);
769 walk_addrsel_policy(int(*callback)(struct in6_addrpolicy *, void *), void *w)
771 struct addrsel_policyent *pol;
774 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
775 pol = TAILQ_NEXT(pol, ape_entry)) {
776 if ((error = (*callback)(&pol->ape_policy, w)) != 0)
784 dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
787 struct walkarg *w = arg;
789 error = SYSCTL_OUT(w->w_req, pol, sizeof(*pol));