1 /* $KAME: in6_src.c,v 1.10 2000/03/28 09:02:23 k-sugyou Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
64 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
68 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
73 #include "opt_ipsec.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/malloc.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 4)
84 #include <sys/ioctl.h>
86 #include <sys/errno.h>
91 #include <net/route.h>
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet6/in6_var.h>
99 #include <netinet/ip6.h>
100 #if !(defined(__OpenBSD__) || (defined(__bsdi__) && _BSDI_VERSION >= 199802))
101 #include <netinet6/in6_pcb.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/nd6.h>
106 #include <net/net_osdep.h>
111 #if defined(__NetBSD__) || defined(__OpenBSD__)
112 extern struct ifnet loif
[NLOOP
];
116 #include <netinet6/mip6.h>
117 #include <netinet6/mip6_common.h>
119 extern struct nd_prefix
*(*mip6_get_home_prefix_hook
) __P((void));
123 * Return an IPv6 address, which is the most appropriate for given
124 * destination and user specified options.
125 * If necessary, this function lookups the routing table and return
126 * an entry to the caller for later use.
129 in6_selectsrc(dstsock
, opts
, mopts
, ro
, laddr
, errorp
)
130 struct sockaddr_in6
*dstsock
;
131 struct ip6_pktopts
*opts
;
132 struct ip6_moptions
*mopts
;
133 struct route_in6
*ro
;
134 struct in6_addr
*laddr
;
137 struct in6_addr
*dst
;
138 struct in6_ifaddr
*ia6
= 0;
139 struct in6_pktinfo
*pi
= NULL
;
141 dst
= &dstsock
->sin6_addr
;
145 * If the source address is explicitly specified by the caller,
148 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
149 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
))
150 return(&pi
->ipi6_addr
);
153 * If the source address is not specified but the socket(if any)
154 * is already bound, use the bound address.
156 if (laddr
&& !IN6_IS_ADDR_UNSPECIFIED(laddr
))
160 * If the caller doesn't specify the source address but
161 * the outgoing interface, use an address associated with
164 if (pi
&& pi
->ipi6_ifindex
) {
165 /* XXX boundary check is assumed to be already done. */
166 ia6
= in6_ifawithscope(ifindex2ifnet
[pi
->ipi6_ifindex
],
169 *errorp
= EADDRNOTAVAIL
;
172 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
176 * If the destination address is a link-local unicast address or
177 * a multicast address, and if the outgoing interface is specified
178 * by the sin6_scope_id filed, use an address associated with the
180 * XXX: We're now trying to define more specific semantics of
181 * sin6_scope_id field, so this part will be rewritten in
184 if ((IN6_IS_ADDR_LINKLOCAL(dst
) || IN6_IS_ADDR_MULTICAST(dst
)) &&
185 dstsock
->sin6_scope_id
) {
187 * I'm not sure if boundary check for scope_id is done
190 if (dstsock
->sin6_scope_id
< 0 ||
191 if_index
< dstsock
->sin6_scope_id
) {
192 *errorp
= ENXIO
; /* XXX: better error? */
195 ia6
= in6_ifawithscope(ifindex2ifnet
[dstsock
->sin6_scope_id
],
198 *errorp
= EADDRNOTAVAIL
;
201 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
205 * If the destination address is a multicast address and
206 * the outgoing interface for the address is specified
207 * by the caller, use an address associated with the interface.
208 * There is a sanity check here; if the destination has node-local
209 * scope, the outgoing interfacde should be a loopback address.
210 * Even if the outgoing interface is not specified, we also
211 * choose a loopback interface as the outgoing interface.
213 if (IN6_IS_ADDR_MULTICAST(dst
)) {
214 struct ifnet
*ifp
= mopts
? mopts
->im6o_multicast_ifp
: NULL
;
216 #if _BSDI_VERSION >= 199802
217 extern struct ifnet
*loifp
;
219 extern struct ifnet loif
;
220 struct ifnet
*loifp
= &loif
;
224 if (ifp
== NULL
&& IN6_IS_ADDR_MC_NODELOCAL(dst
)) {
233 ia6
= in6_ifawithscope(ifp
, dst
);
235 *errorp
= EADDRNOTAVAIL
;
238 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
243 * If the next hop address for the packet is specified
244 * by caller, use an address associated with the route
248 struct sockaddr_in6
*sin6_next
;
251 if (opts
&& opts
->ip6po_nexthop
) {
252 sin6_next
= satosin6(opts
->ip6po_nexthop
);
253 rt
= nd6_lookup(&sin6_next
->sin6_addr
, 1, NULL
);
255 ia6
= in6_ifawithscope(rt
->rt_ifp
, dst
);
257 ia6
= ifatoia6(rt
->rt_ifa
);
260 *errorp
= EADDRNOTAVAIL
;
263 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
269 * This is needed to assure that the Home Address is used for
270 * outgoing packets when not at home. We can't choose any other
271 * address if we want to keep connections up during movement.
273 if (mip6_get_home_prefix_hook
) { /* Only Mobile Node */
274 struct nd_prefix
*pr
;
275 if ((pr
= (*mip6_get_home_prefix_hook
)()) &&
276 !IN6_IS_ADDR_UNSPECIFIED(&pr
->ndpr_addr
)) {
277 if (in6_addrscope(dst
) ==
278 in6_addrscope(&pr
->ndpr_addr
)) {
280 /* Noisy but useful */
281 mip6_debug("%s: Local address %s is chosen "
282 "for pcb to dest %s.\n",
284 ip6_sprintf(&pr
->ndpr_addr
),
287 return(&pr
->ndpr_addr
);
294 * If route is known or can be allocated now,
295 * our src addr is taken from the i/f, else punt.
299 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
, dst
)) {
301 ro
->ro_rt
= (struct rtentry
*)0;
303 if (ro
->ro_rt
== (struct rtentry
*)0 ||
304 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0) {
305 /* No route yet, so try to acquire one */
306 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in6
));
307 ro
->ro_dst
.sin6_family
= AF_INET6
;
308 ro
->ro_dst
.sin6_len
= sizeof(struct sockaddr_in6
);
309 ro
->ro_dst
.sin6_addr
= *dst
;
310 if (IN6_IS_ADDR_MULTICAST(dst
)) {
311 #if defined( __FreeBSD__) || defined (__APPLE__)
312 ro
->ro_rt
= rtalloc1(&((struct route
*)ro
)
315 ro
->ro_rt
= rtalloc1(&((struct route
*)ro
)
317 #endif /*__FreeBSD__*/
319 #ifdef __bsdi__ /* bsdi needs rtcalloc to make a host route */
320 rtcalloc((struct route
*)ro
);
322 rtalloc((struct route
*)ro
);
328 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
329 * the address. But we don't know why it does so.
330 * It is necessary to ensure the scope even for lo0
331 * so doesn't check out IFF_LOOPBACK.
335 ia6
= in6_ifawithscope(ro
->ro_rt
->rt_ifa
->ifa_ifp
, dst
);
336 if (ia6
== 0) /* xxx scope error ?*/
337 ia6
= ifatoia6(ro
->ro_rt
->rt_ifa
);
341 * xxx The followings are necessary? (kazu)
343 * It's for SO_DONTROUTE option in IPv4.(jinmei)
346 struct sockaddr_in6 sin6
= {sizeof(sin6
), AF_INET6
, 0};
348 sin6
->sin6_addr
= *dst
;
350 ia6
= ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6
)));
352 ia6
= ifatoia6(ifa_ifwithnet(sin6tosa(&sin6
)));
355 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
359 *errorp
= EHOSTUNREACH
; /* no route */
362 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
365 *errorp
= EADDRNOTAVAIL
;
370 * Default hop limit selection. The precedence is as follows:
371 * 1. Hoplimit value specified via ioctl.
372 * 2. (If the outgoing interface is detected) the current
373 * hop limit of the interface specified by router advertisement.
374 * 3. The system default hoplimit.
378 #define in6p_hops inp_hops
381 in6_selecthlim(in6p
, ifp
)
385 if (in6p
&& in6p
->in6p_hops
>= 0)
386 return(in6p
->in6p_hops
);
388 return(nd_ifinfo
[ifp
->if_index
].chlim
);
397 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__OpenBSD__) && !defined(__APPLE__)
399 * Find an empty port and set it to the specified PCB.
401 #if HAVE_NRL_INPCB /* XXX: I really hate such ugly macros...(jinmei) */
403 #define in6p_socket inp_socket
404 #define in6p_lport inp_lport
405 #define in6p_head inp_head
406 #define in6p_flags inp_flags
407 #define IN6PLOOKUP_WILDCARD INPLOOKUP_WILDCARD
410 in6_pcbsetport(laddr
, in6p
)
411 struct in6_addr
*laddr
;
414 struct socket
*so
= in6p
->in6p_socket
;
415 struct in6pcb
*head
= in6p
->in6p_head
;
416 u_int16_t last_port
, lport
= 0;
421 struct proc
*p
= curproc
; /* XXX */
424 /* XXX: this is redundant when called from in6_pcbbind */
425 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0 &&
426 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) == 0 ||
427 (so
->so_options
& SO_ACCEPTCONN
) == 0))
428 wild
= IN6PLOOKUP_WILDCARD
;
430 if (in6p
->in6p_flags
& IN6P_LOWPORT
) {
432 if (p
== 0 || (suser(p
->p_ucred
, &p
->p_acflag
) != 0))
435 if ((so
->so_state
& SS_PRIV
) == 0)
438 min
= IPV6PORT_RESERVEDMIN
;
439 max
= IPV6PORT_RESERVEDMAX
;
441 min
= IPV6PORT_ANONMIN
;
442 max
= IPV6PORT_ANONMAX
;
445 /* value out of range */
446 if (head
->in6p_lport
< min
)
447 head
->in6p_lport
= min
;
448 else if (head
->in6p_lport
> max
)
449 head
->in6p_lport
= min
;
450 last_port
= head
->in6p_lport
;
451 goto startover
; /*to randomize*/
453 lport
= htons(head
->in6p_lport
);
454 if (IN6_IS_ADDR_V4MAPPED(laddr
)) {
456 t
= in_pcblookup_bind(&tcbtable
,
457 (struct in_addr
*)&in6p
->in6p_laddr
.s6_addr32
[3],
464 /* XXX: ugly cast... */
465 t
= in_pcblookup(head
, (struct in_addr
*)&zeroin6_addr
,
466 0, (struct in_addr
*)laddr
,
467 lport
, wild
| INPLOOKUP_IPV6
);
469 t
= in6_pcblookup(head
, &zeroin6_addr
, 0, laddr
,
476 if (head
->in6p_lport
>= max
)
477 head
->in6p_lport
= min
;
480 if (head
->in6p_lport
== last_port
)
484 in6p
->in6p_lport
= lport
;
485 return(0); /* success */
493 #undef IN6PLOOKUP_WILDCARD
495 #endif /* !FreeBSD3 && !OpenBSD*/