Import the kernel GPT and UUID header files from FreeBSD, and bring in
[dragonfly/vkernel-mp.git] / sys / netinet6 / in6_src.c
blobbccec6f86b31da24b6d784f7b6b7e1b186897ab8
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 19:02:56 victor Exp $ */
3 /* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 itojun Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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
31 * SUCH DAMAGE.
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
40 * are met:
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
64 * SUCH DAMAGE.
66 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/jail.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/errno.h>
81 #include <sys/time.h>
82 #include <sys/proc.h>
84 #include <net/if.h>
85 #include <net/route.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet6/in6_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/nd6.h>
97 #ifdef ENABLE_DEFAULT_SCOPE
98 #include <netinet6/scope6_var.h>
99 #endif
101 #include <net/net_osdep.h>
103 #include "use_loop.h"
106 * Return an IPv6 address, which is the most appropriate for a given
107 * destination and user specified options.
108 * If necessary, this function lookups the routing table and returns
109 * an entry to the caller for later use.
111 struct in6_addr *
112 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
113 struct ip6_moptions *mopts, struct route_in6 *ro,
114 struct in6_addr *laddr, int *errorp, struct thread *td)
116 struct sockaddr_in6 jsin6;
117 struct ucred *cred = NULL;
118 struct in6_addr *dst;
119 struct in6_ifaddr *ia6 = 0;
120 struct in6_pktinfo *pi = NULL;
121 int jailed = 0;
123 if (td && td->td_proc && td->td_proc->p_ucred)
124 cred = td->td_proc->p_ucred;
125 if (cred && cred->cr_prison)
126 jailed = 1;
127 jsin6.sin6_family = AF_INET6;
128 dst = &dstsock->sin6_addr;
129 *errorp = 0;
132 * If the source address is explicitly specified by the caller,
133 * use it.
135 if (opts && (pi = opts->ip6po_pktinfo) &&
136 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
137 jsin6.sin6_addr = pi->ipi6_addr;
138 if (jailed && !jailed_ip(cred->cr_prison,
139 (struct sockaddr *)&jsin6)) {
140 return(0);
141 } else {
142 return (&pi->ipi6_addr);
147 * If the source address is not specified but the socket(if any)
148 * is already bound, use the bound address.
150 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
151 jsin6.sin6_addr = *laddr;
152 if (jailed && !jailed_ip(cred->cr_prison,
153 (struct sockaddr *)&jsin6)) {
154 return(0);
155 } else {
156 return (laddr);
161 * If the caller doesn't specify the source address but
162 * the outgoing interface, use an address associated with
163 * the interface.
165 if (pi && pi->ipi6_ifindex) {
166 /* XXX boundary check is assumed to be already done. */
167 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
168 dst);
170 if (ia6 && jailed) {
171 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
172 if (!jailed_ip(cred->cr_prison,
173 (struct sockaddr *)&jsin6))
174 ia6 = 0;
177 if (ia6 == 0) {
178 *errorp = EADDRNOTAVAIL;
179 return (0);
181 return (&satosin6(&ia6->ia_addr)->sin6_addr);
185 * If the destination address is a link-local unicast address or
186 * a multicast address, and if the outgoing interface is specified
187 * by the sin6_scope_id filed, use an address associated with the
188 * interface.
189 * XXX: We're now trying to define more specific semantics of
190 * sin6_scope_id field, so this part will be rewritten in
191 * the near future.
193 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
194 dstsock->sin6_scope_id) {
196 * I'm not sure if boundary check for scope_id is done
197 * somewhere...
199 if (dstsock->sin6_scope_id < 0 ||
200 if_index < dstsock->sin6_scope_id) {
201 *errorp = ENXIO; /* XXX: better error? */
202 return (0);
204 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
205 dst);
207 if (ia6 && jailed) {
208 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
209 if (!jailed_ip(cred->cr_prison,
210 (struct sockaddr *)&jsin6))
211 ia6 = 0;
214 if (ia6 == 0) {
215 *errorp = EADDRNOTAVAIL;
216 return (0);
218 return (&satosin6(&ia6->ia_addr)->sin6_addr);
222 * If the destination address is a multicast address and
223 * the outgoing interface for the address is specified
224 * by the caller, use an address associated with the interface.
225 * There is a sanity check here; if the destination has node-local
226 * scope, the outgoing interfacde should be a loopback address.
227 * Even if the outgoing interface is not specified, we also
228 * choose a loopback interface as the outgoing interface.
230 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
231 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
233 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
234 ifp = &loif[0];
237 if (ifp) {
238 ia6 = in6_ifawithscope(ifp, dst);
239 if (ia6 == 0) {
240 *errorp = EADDRNOTAVAIL;
241 return (0);
243 return (&satosin6(&ia6->ia_addr)->sin6_addr);
248 * If the next hop address for the packet is specified
249 * by caller, use an address associated with the route
250 * to the next hop.
253 struct sockaddr_in6 *sin6_next;
254 struct rtentry *rt;
256 if (opts && opts->ip6po_nexthop) {
257 sin6_next = satosin6(opts->ip6po_nexthop);
258 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
259 if (rt) {
260 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
261 if (ia6 == 0)
262 ia6 = ifatoia6(rt->rt_ifa);
264 if (ia6 && jailed) {
265 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
266 if (!jailed_ip(cred->cr_prison,
267 (struct sockaddr *)&jsin6))
268 ia6 = 0;
271 if (ia6 == 0) {
272 *errorp = EADDRNOTAVAIL;
273 return (0);
275 return (&satosin6(&ia6->ia_addr)->sin6_addr);
280 * If route is known or can be allocated now,
281 * our src addr is taken from the i/f, else punt.
283 if (ro) {
284 if (ro->ro_rt &&
285 (!(ro->ro_rt->rt_flags & RTF_UP) ||
286 satosin6(&ro->ro_dst)->sin6_family != AF_INET6 ||
287 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr,
288 dst))) {
289 RTFREE(ro->ro_rt);
290 ro->ro_rt = NULL;
292 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
293 struct sockaddr_in6 *sa6;
295 /* No route yet, so try to acquire one */
296 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
297 sa6 = &ro->ro_dst;
298 sa6->sin6_family = AF_INET6;
299 sa6->sin6_len = sizeof(struct sockaddr_in6);
300 sa6->sin6_addr = *dst;
301 sa6->sin6_scope_id = dstsock->sin6_scope_id;
302 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
303 ro->ro_rt =
304 rtpurelookup((struct sockaddr *)&ro->ro_dst);
305 } else {
306 rtalloc((struct route *)ro);
311 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
312 * the address. But we don't know why it does so.
313 * It is necessary to ensure the scope even for lo0
314 * so doesn't check out IFF_LOOPBACK.
317 if (ro->ro_rt) {
318 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
319 if (ia6 && jailed) {
320 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
321 if (!jailed_ip(cred->cr_prison,
322 (struct sockaddr *)&jsin6))
323 ia6 = 0;
326 if (ia6 == 0) /* xxx scope error ?*/
327 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
329 if (ia6 && jailed) {
330 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
331 if (!jailed_ip(cred->cr_prison,
332 (struct sockaddr *)&jsin6))
333 ia6 = 0;
336 #if 0
338 * xxx The followings are necessary? (kazu)
339 * I don't think so.
340 * It's for SO_DONTROUTE option in IPv4.(jinmei)
342 if (ia6 == 0) {
343 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
345 sin6->sin6_addr = *dst;
347 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
348 if (ia6 == 0)
349 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
350 if (ia6 == 0)
351 return (0);
352 return (&satosin6(&ia6->ia_addr)->sin6_addr);
354 #endif /* 0 */
355 if (ia6 == 0) {
356 *errorp = EHOSTUNREACH; /* no route */
357 return (0);
359 return (&satosin6(&ia6->ia_addr)->sin6_addr);
362 *errorp = EADDRNOTAVAIL;
363 return (0);
367 * Default hop limit selection. The precedence is as follows:
368 * 1. Hoplimit value specified via ioctl.
369 * 2. (If the outgoing interface is detected) the current
370 * hop limit of the interface specified by router advertisement.
371 * 3. The system default hoplimit.
374 in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
376 if (in6p && in6p->in6p_hops >= 0)
377 return (in6p->in6p_hops);
378 else if (ifp)
379 return (ND_IFINFO(ifp)->chlim);
380 else
381 return (ip6_defhlim);
385 * XXX: this is borrowed from in6_pcbbind(). If possible, we should
386 * share this function by all *bsd*...
389 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct thread *td)
391 struct socket *so = inp->inp_socket;
392 u_int16_t lport = 0, first, last, *lastport;
393 int count, error = 0, wild = 0;
394 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
395 struct ucred *cred = NULL;
397 /* XXX: this is redundant when called from in6_pcbbind */
398 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
399 wild = INPLOOKUP_WILDCARD;
400 if (td->td_proc && td->td_proc->p_ucred)
401 cred = td->td_proc->p_ucred;
403 inp->inp_flags |= INP_ANONPORT;
405 if (inp->inp_flags & INP_HIGHPORT) {
406 first = ipport_hifirstauto; /* sysctl */
407 last = ipport_hilastauto;
408 lastport = &pcbinfo->lasthi;
409 } else if (inp->inp_flags & INP_LOWPORT) {
410 if ((error = suser(td)) != 0)
411 return error;
412 first = ipport_lowfirstauto; /* 1023 */
413 last = ipport_lowlastauto; /* 600 */
414 lastport = &pcbinfo->lastlow;
415 } else {
416 first = ipport_firstauto; /* sysctl */
417 last = ipport_lastauto;
418 lastport = &pcbinfo->lastport;
421 * Simple check to ensure all ports are not used up causing
422 * a deadlock here.
424 * We split the two cases (up and down) so that the direction
425 * is not being tested on each round of the loop.
427 if (first > last) {
429 * counting down
431 count = first - last;
433 do {
434 if (count-- < 0) { /* completely used? */
436 * Undo any address bind that may have
437 * occurred above.
439 inp->in6p_laddr = kin6addr_any;
440 return (EAGAIN);
442 --*lastport;
443 if (*lastport > first || *lastport < last)
444 *lastport = first;
445 lport = htons(*lastport);
446 } while (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr,
447 lport, wild, cred));
448 } else {
450 * counting up
452 count = last - first;
454 do {
455 if (count-- < 0) { /* completely used? */
457 * Undo any address bind that may have
458 * occurred above.
460 inp->in6p_laddr = kin6addr_any;
461 return (EAGAIN);
463 ++*lastport;
464 if (*lastport < first || *lastport > last)
465 *lastport = first;
466 lport = htons(*lastport);
467 } while (in6_pcblookup_local(pcbinfo, &inp->in6p_laddr,
468 lport, wild, cred));
471 inp->inp_lport = lport;
472 if (in_pcbinsporthash(inp) != 0) {
473 inp->in6p_laddr = kin6addr_any;
474 inp->inp_lport = 0;
475 return (EAGAIN);
478 return (0);
482 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
483 * If the address scope of is link-local, embed the interface index in the
484 * address. The routine determines our precedence
485 * between advanced API scope/interface specification and basic API
486 * specification.
488 * this function should be nuked in the future, when we get rid of
489 * embedded scopeid thing.
491 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
492 * there can be multiple interfaces that belong to a particular scope zone
493 * (in specification, we have 1:N mapping between a scope zone and interfaces).
494 * we may want to change the function to return something other than ifp.
497 in6_embedscope(struct in6_addr *in6,
498 const struct sockaddr_in6 *sin6,
499 #ifdef HAVE_NRL_INPCB
500 struct inpcb *in6p,
501 #define in6p_outputopts inp_outputopts6
502 #define in6p_moptions inp_moptions6
503 #else
504 struct in6pcb *in6p,
505 #endif
506 struct ifnet **ifpp)
508 struct ifnet *ifp = NULL;
509 u_int32_t scopeid;
511 *in6 = sin6->sin6_addr;
512 scopeid = sin6->sin6_scope_id;
513 if (ifpp)
514 *ifpp = NULL;
517 * don't try to read sin6->sin6_addr beyond here, since the caller may
518 * ask us to overwrite existing sockaddr_in6
521 #ifdef ENABLE_DEFAULT_SCOPE
522 if (scopeid == 0)
523 scopeid = scope6_addr2default(in6);
524 #endif
526 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
527 struct in6_pktinfo *pi;
530 * KAME assumption: link id == interface id
533 if (in6p && in6p->in6p_outputopts &&
534 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
535 pi->ipi6_ifindex) {
536 ifp = ifindex2ifnet[pi->ipi6_ifindex];
537 in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
538 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
539 in6p->in6p_moptions &&
540 in6p->in6p_moptions->im6o_multicast_ifp) {
541 ifp = in6p->in6p_moptions->im6o_multicast_ifp;
542 in6->s6_addr16[1] = htons(ifp->if_index);
543 } else if (scopeid) {
544 /* boundary check */
545 if (scopeid < 0 || if_index < scopeid)
546 return ENXIO; /* XXX EINVAL? */
547 ifp = ifindex2ifnet[scopeid];
548 /*XXX assignment to 16bit from 32bit variable */
549 in6->s6_addr16[1] = htons(scopeid & 0xffff);
552 if (ifpp)
553 *ifpp = ifp;
556 return 0;
558 #ifdef HAVE_NRL_INPCB
559 #undef in6p_outputopts
560 #undef in6p_moptions
561 #endif
564 * generate standard sockaddr_in6 from embedded form.
565 * touches sin6_addr and sin6_scope_id only.
567 * this function should be nuked in the future, when we get rid of
568 * embedded scopeid thing.
571 in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6,
572 struct ifnet *ifp)
574 u_int32_t scopeid;
576 sin6->sin6_addr = *in6;
579 * don't try to read *in6 beyond here, since the caller may
580 * ask us to overwrite existing sockaddr_in6
583 sin6->sin6_scope_id = 0;
584 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
586 * KAME assumption: link id == interface id
588 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
589 if (scopeid) {
590 /* sanity check */
591 if (scopeid < 0 || if_index < scopeid)
592 return ENXIO;
593 if (ifp && ifp->if_index != scopeid)
594 return ENXIO;
595 sin6->sin6_addr.s6_addr16[1] = 0;
596 sin6->sin6_scope_id = scopeid;
600 return 0;
604 * just clear the embedded scope identifer.
605 * XXX: currently used for bsdi4 only as a supplement function.
607 void
608 in6_clearscope(struct in6_addr *addr)
610 if (IN6_IS_SCOPE_LINKLOCAL(addr))
611 addr->s6_addr16[1] = 0;