in6_src.c revision 1.4.2.4 1 1.4.2.4 bouyer /* $NetBSD: in6_src.c,v 1.4.2.4 2001/04/21 17:46:55 bouyer Exp $ */
2 1.4.2.3 bouyer /* $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun Exp $ */
3 1.4.2.2 bouyer
4 1.4.2.2 bouyer /*
5 1.4.2.2 bouyer * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.4.2.2 bouyer * All rights reserved.
7 1.4.2.2 bouyer *
8 1.4.2.2 bouyer * Redistribution and use in source and binary forms, with or without
9 1.4.2.2 bouyer * modification, are permitted provided that the following conditions
10 1.4.2.2 bouyer * are met:
11 1.4.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
12 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer.
13 1.4.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
14 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
15 1.4.2.2 bouyer * documentation and/or other materials provided with the distribution.
16 1.4.2.2 bouyer * 3. Neither the name of the project nor the names of its contributors
17 1.4.2.2 bouyer * may be used to endorse or promote products derived from this software
18 1.4.2.2 bouyer * without specific prior written permission.
19 1.4.2.2 bouyer *
20 1.4.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.4.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.4.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.4.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.4.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.4.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.4.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.4.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.4.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.4.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.4.2.2 bouyer * SUCH DAMAGE.
31 1.4.2.2 bouyer */
32 1.4.2.2 bouyer
33 1.4.2.2 bouyer /*
34 1.4.2.2 bouyer * Copyright (c) 1982, 1986, 1991, 1993
35 1.4.2.2 bouyer * The Regents of the University of California. All rights reserved.
36 1.4.2.2 bouyer *
37 1.4.2.2 bouyer * Redistribution and use in source and binary forms, with or without
38 1.4.2.2 bouyer * modification, are permitted provided that the following conditions
39 1.4.2.2 bouyer * are met:
40 1.4.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
41 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer.
42 1.4.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
43 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
44 1.4.2.2 bouyer * documentation and/or other materials provided with the distribution.
45 1.4.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
46 1.4.2.2 bouyer * must display the following acknowledgement:
47 1.4.2.2 bouyer * This product includes software developed by the University of
48 1.4.2.2 bouyer * California, Berkeley and its contributors.
49 1.4.2.2 bouyer * 4. Neither the name of the University nor the names of its contributors
50 1.4.2.2 bouyer * may be used to endorse or promote products derived from this software
51 1.4.2.2 bouyer * without specific prior written permission.
52 1.4.2.2 bouyer *
53 1.4.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.4.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.4.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.4.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.4.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.4.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.4.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.4.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.4.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.4.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.4.2.2 bouyer * SUCH DAMAGE.
64 1.4.2.2 bouyer *
65 1.4.2.2 bouyer * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
66 1.4.2.2 bouyer */
67 1.4.2.2 bouyer
68 1.4.2.2 bouyer #include "opt_inet.h"
69 1.4.2.2 bouyer
70 1.4.2.2 bouyer #include <sys/param.h>
71 1.4.2.2 bouyer #include <sys/systm.h>
72 1.4.2.2 bouyer #include <sys/malloc.h>
73 1.4.2.2 bouyer #include <sys/mbuf.h>
74 1.4.2.2 bouyer #include <sys/protosw.h>
75 1.4.2.2 bouyer #include <sys/socket.h>
76 1.4.2.2 bouyer #include <sys/socketvar.h>
77 1.4.2.2 bouyer #include <sys/ioctl.h>
78 1.4.2.2 bouyer #include <sys/errno.h>
79 1.4.2.2 bouyer #include <sys/time.h>
80 1.4.2.2 bouyer #include <sys/proc.h>
81 1.4.2.2 bouyer
82 1.4.2.2 bouyer #include <net/if.h>
83 1.4.2.2 bouyer #include <net/route.h>
84 1.4.2.2 bouyer
85 1.4.2.2 bouyer #include <netinet/in.h>
86 1.4.2.2 bouyer #include <netinet/in_var.h>
87 1.4.2.2 bouyer #include <netinet/in_systm.h>
88 1.4.2.2 bouyer #include <netinet/ip.h>
89 1.4.2.2 bouyer #include <netinet/in_pcb.h>
90 1.4.2.2 bouyer #include <netinet6/in6_var.h>
91 1.4.2.2 bouyer #include <netinet/ip6.h>
92 1.4.2.2 bouyer #include <netinet6/in6_pcb.h>
93 1.4.2.2 bouyer #include <netinet6/ip6_var.h>
94 1.4.2.2 bouyer #include <netinet6/nd6.h>
95 1.4.2.2 bouyer #ifdef ENABLE_DEFAULT_SCOPE
96 1.4.2.2 bouyer #include <netinet6/scope6_var.h>
97 1.4.2.2 bouyer #endif
98 1.4.2.2 bouyer
99 1.4.2.2 bouyer #include <net/net_osdep.h>
100 1.4.2.2 bouyer
101 1.4.2.2 bouyer #include "loop.h"
102 1.4.2.2 bouyer extern struct ifnet loif[NLOOP];
103 1.4.2.2 bouyer
104 1.4.2.2 bouyer /*
105 1.4.2.3 bouyer * Return an IPv6 address, which is the most appropriate for a given
106 1.4.2.2 bouyer * destination and user specified options.
107 1.4.2.3 bouyer * If necessary, this function lookups the routing table and returns
108 1.4.2.2 bouyer * an entry to the caller for later use.
109 1.4.2.2 bouyer */
110 1.4.2.2 bouyer struct in6_addr *
111 1.4.2.2 bouyer in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
112 1.4.2.2 bouyer struct sockaddr_in6 *dstsock;
113 1.4.2.2 bouyer struct ip6_pktopts *opts;
114 1.4.2.2 bouyer struct ip6_moptions *mopts;
115 1.4.2.2 bouyer struct route_in6 *ro;
116 1.4.2.2 bouyer struct in6_addr *laddr;
117 1.4.2.2 bouyer int *errorp;
118 1.4.2.2 bouyer {
119 1.4.2.2 bouyer struct in6_addr *dst;
120 1.4.2.2 bouyer struct in6_ifaddr *ia6 = 0;
121 1.4.2.2 bouyer struct in6_pktinfo *pi = NULL;
122 1.4.2.2 bouyer
123 1.4.2.2 bouyer dst = &dstsock->sin6_addr;
124 1.4.2.2 bouyer *errorp = 0;
125 1.4.2.2 bouyer
126 1.4.2.2 bouyer /*
127 1.4.2.2 bouyer * If the source address is explicitly specified by the caller,
128 1.4.2.2 bouyer * use it.
129 1.4.2.2 bouyer */
130 1.4.2.2 bouyer if (opts && (pi = opts->ip6po_pktinfo) &&
131 1.4.2.2 bouyer !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
132 1.4.2.2 bouyer return(&pi->ipi6_addr);
133 1.4.2.2 bouyer
134 1.4.2.2 bouyer /*
135 1.4.2.2 bouyer * If the source address is not specified but the socket(if any)
136 1.4.2.2 bouyer * is already bound, use the bound address.
137 1.4.2.2 bouyer */
138 1.4.2.2 bouyer if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
139 1.4.2.2 bouyer return(laddr);
140 1.4.2.2 bouyer
141 1.4.2.2 bouyer /*
142 1.4.2.2 bouyer * If the caller doesn't specify the source address but
143 1.4.2.2 bouyer * the outgoing interface, use an address associated with
144 1.4.2.2 bouyer * the interface.
145 1.4.2.2 bouyer */
146 1.4.2.2 bouyer if (pi && pi->ipi6_ifindex) {
147 1.4.2.2 bouyer /* XXX boundary check is assumed to be already done. */
148 1.4.2.2 bouyer ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
149 1.4.2.2 bouyer dst);
150 1.4.2.2 bouyer if (ia6 == 0) {
151 1.4.2.2 bouyer *errorp = EADDRNOTAVAIL;
152 1.4.2.2 bouyer return(0);
153 1.4.2.2 bouyer }
154 1.4.2.2 bouyer return(&satosin6(&ia6->ia_addr)->sin6_addr);
155 1.4.2.2 bouyer }
156 1.4.2.2 bouyer
157 1.4.2.2 bouyer /*
158 1.4.2.2 bouyer * If the destination address is a link-local unicast address or
159 1.4.2.2 bouyer * a multicast address, and if the outgoing interface is specified
160 1.4.2.2 bouyer * by the sin6_scope_id filed, use an address associated with the
161 1.4.2.2 bouyer * interface.
162 1.4.2.2 bouyer * XXX: We're now trying to define more specific semantics of
163 1.4.2.2 bouyer * sin6_scope_id field, so this part will be rewritten in
164 1.4.2.2 bouyer * the near future.
165 1.4.2.2 bouyer */
166 1.4.2.2 bouyer if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
167 1.4.2.2 bouyer dstsock->sin6_scope_id) {
168 1.4.2.2 bouyer /*
169 1.4.2.2 bouyer * I'm not sure if boundary check for scope_id is done
170 1.4.2.2 bouyer * somewhere...
171 1.4.2.2 bouyer */
172 1.4.2.2 bouyer if (dstsock->sin6_scope_id < 0 ||
173 1.4.2.2 bouyer if_index < dstsock->sin6_scope_id) {
174 1.4.2.2 bouyer *errorp = ENXIO; /* XXX: better error? */
175 1.4.2.2 bouyer return(0);
176 1.4.2.2 bouyer }
177 1.4.2.2 bouyer ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
178 1.4.2.2 bouyer dst);
179 1.4.2.2 bouyer if (ia6 == 0) {
180 1.4.2.2 bouyer *errorp = EADDRNOTAVAIL;
181 1.4.2.2 bouyer return(0);
182 1.4.2.2 bouyer }
183 1.4.2.2 bouyer return(&satosin6(&ia6->ia_addr)->sin6_addr);
184 1.4.2.2 bouyer }
185 1.4.2.2 bouyer
186 1.4.2.2 bouyer /*
187 1.4.2.2 bouyer * If the destination address is a multicast address and
188 1.4.2.2 bouyer * the outgoing interface for the address is specified
189 1.4.2.2 bouyer * by the caller, use an address associated with the interface.
190 1.4.2.2 bouyer * There is a sanity check here; if the destination has node-local
191 1.4.2.2 bouyer * scope, the outgoing interfacde should be a loopback address.
192 1.4.2.2 bouyer * Even if the outgoing interface is not specified, we also
193 1.4.2.2 bouyer * choose a loopback interface as the outgoing interface.
194 1.4.2.2 bouyer */
195 1.4.2.2 bouyer if (IN6_IS_ADDR_MULTICAST(dst)) {
196 1.4.2.2 bouyer struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
197 1.4.2.2 bouyer
198 1.4.2.2 bouyer if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
199 1.4.2.2 bouyer ifp = &loif[0];
200 1.4.2.2 bouyer }
201 1.4.2.2 bouyer
202 1.4.2.2 bouyer if (ifp) {
203 1.4.2.2 bouyer ia6 = in6_ifawithscope(ifp, dst);
204 1.4.2.2 bouyer if (ia6 == 0) {
205 1.4.2.2 bouyer *errorp = EADDRNOTAVAIL;
206 1.4.2.2 bouyer return(0);
207 1.4.2.2 bouyer }
208 1.4.2.2 bouyer return(&satosin6(&ia6->ia_addr)->sin6_addr);
209 1.4.2.2 bouyer }
210 1.4.2.2 bouyer }
211 1.4.2.2 bouyer
212 1.4.2.2 bouyer /*
213 1.4.2.2 bouyer * If the next hop address for the packet is specified
214 1.4.2.2 bouyer * by caller, use an address associated with the route
215 1.4.2.2 bouyer * to the next hop.
216 1.4.2.2 bouyer */
217 1.4.2.2 bouyer {
218 1.4.2.2 bouyer struct sockaddr_in6 *sin6_next;
219 1.4.2.2 bouyer struct rtentry *rt;
220 1.4.2.2 bouyer
221 1.4.2.2 bouyer if (opts && opts->ip6po_nexthop) {
222 1.4.2.2 bouyer sin6_next = satosin6(opts->ip6po_nexthop);
223 1.4.2.2 bouyer rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
224 1.4.2.2 bouyer if (rt) {
225 1.4.2.2 bouyer ia6 = in6_ifawithscope(rt->rt_ifp, dst);
226 1.4.2.2 bouyer if (ia6 == 0)
227 1.4.2.2 bouyer ia6 = ifatoia6(rt->rt_ifa);
228 1.4.2.2 bouyer }
229 1.4.2.2 bouyer if (ia6 == 0) {
230 1.4.2.2 bouyer *errorp = EADDRNOTAVAIL;
231 1.4.2.2 bouyer return(0);
232 1.4.2.2 bouyer }
233 1.4.2.2 bouyer return(&satosin6(&ia6->ia_addr)->sin6_addr);
234 1.4.2.2 bouyer }
235 1.4.2.2 bouyer }
236 1.4.2.2 bouyer
237 1.4.2.2 bouyer /*
238 1.4.2.2 bouyer * If route is known or can be allocated now,
239 1.4.2.2 bouyer * our src addr is taken from the i/f, else punt.
240 1.4.2.2 bouyer */
241 1.4.2.2 bouyer if (ro) {
242 1.4.2.2 bouyer if (ro->ro_rt &&
243 1.4.2.2 bouyer !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
244 1.4.2.2 bouyer RTFREE(ro->ro_rt);
245 1.4.2.2 bouyer ro->ro_rt = (struct rtentry *)0;
246 1.4.2.2 bouyer }
247 1.4.2.2 bouyer if (ro->ro_rt == (struct rtentry *)0 ||
248 1.4.2.2 bouyer ro->ro_rt->rt_ifp == (struct ifnet *)0) {
249 1.4.2.3 bouyer struct sockaddr_in6 *sa6;
250 1.4.2.3 bouyer
251 1.4.2.2 bouyer /* No route yet, so try to acquire one */
252 1.4.2.2 bouyer bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
253 1.4.2.3 bouyer sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
254 1.4.2.3 bouyer sa6->sin6_family = AF_INET6;
255 1.4.2.3 bouyer sa6->sin6_len = sizeof(struct sockaddr_in6);
256 1.4.2.3 bouyer sa6->sin6_addr = *dst;
257 1.4.2.3 bouyer sa6->sin6_scope_id = dstsock->sin6_scope_id;
258 1.4.2.2 bouyer if (IN6_IS_ADDR_MULTICAST(dst)) {
259 1.4.2.2 bouyer ro->ro_rt = rtalloc1(&((struct route *)ro)
260 1.4.2.2 bouyer ->ro_dst, 0);
261 1.4.2.2 bouyer } else {
262 1.4.2.2 bouyer rtalloc((struct route *)ro);
263 1.4.2.2 bouyer }
264 1.4.2.2 bouyer }
265 1.4.2.2 bouyer
266 1.4.2.2 bouyer /*
267 1.4.2.2 bouyer * in_pcbconnect() checks out IFF_LOOPBACK to skip using
268 1.4.2.2 bouyer * the address. But we don't know why it does so.
269 1.4.2.2 bouyer * It is necessary to ensure the scope even for lo0
270 1.4.2.2 bouyer * so doesn't check out IFF_LOOPBACK.
271 1.4.2.2 bouyer */
272 1.4.2.2 bouyer
273 1.4.2.2 bouyer if (ro->ro_rt) {
274 1.4.2.2 bouyer ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
275 1.4.2.2 bouyer if (ia6 == 0) /* xxx scope error ?*/
276 1.4.2.2 bouyer ia6 = ifatoia6(ro->ro_rt->rt_ifa);
277 1.4.2.2 bouyer }
278 1.4.2.2 bouyer #if 0
279 1.4.2.2 bouyer /*
280 1.4.2.2 bouyer * xxx The followings are necessary? (kazu)
281 1.4.2.2 bouyer * I don't think so.
282 1.4.2.2 bouyer * It's for SO_DONTROUTE option in IPv4.(jinmei)
283 1.4.2.2 bouyer */
284 1.4.2.2 bouyer if (ia6 == 0) {
285 1.4.2.2 bouyer struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
286 1.4.2.2 bouyer
287 1.4.2.2 bouyer sin6->sin6_addr = *dst;
288 1.4.2.2 bouyer
289 1.4.2.2 bouyer ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
290 1.4.2.2 bouyer if (ia6 == 0)
291 1.4.2.2 bouyer ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
292 1.4.2.2 bouyer if (ia6 == 0)
293 1.4.2.2 bouyer return(0);
294 1.4.2.2 bouyer return(&satosin6(&ia6->ia_addr)->sin6_addr);
295 1.4.2.2 bouyer }
296 1.4.2.2 bouyer #endif /* 0 */
297 1.4.2.2 bouyer if (ia6 == 0) {
298 1.4.2.2 bouyer *errorp = EHOSTUNREACH; /* no route */
299 1.4.2.2 bouyer return(0);
300 1.4.2.2 bouyer }
301 1.4.2.2 bouyer return(&satosin6(&ia6->ia_addr)->sin6_addr);
302 1.4.2.2 bouyer }
303 1.4.2.2 bouyer
304 1.4.2.2 bouyer *errorp = EADDRNOTAVAIL;
305 1.4.2.2 bouyer return(0);
306 1.4.2.2 bouyer }
307 1.4.2.2 bouyer
308 1.4.2.2 bouyer /*
309 1.4.2.2 bouyer * Default hop limit selection. The precedence is as follows:
310 1.4.2.2 bouyer * 1. Hoplimit value specified via ioctl.
311 1.4.2.2 bouyer * 2. (If the outgoing interface is detected) the current
312 1.4.2.2 bouyer * hop limit of the interface specified by router advertisement.
313 1.4.2.2 bouyer * 3. The system default hoplimit.
314 1.4.2.2 bouyer */
315 1.4.2.2 bouyer int
316 1.4.2.2 bouyer in6_selecthlim(in6p, ifp)
317 1.4.2.2 bouyer struct in6pcb *in6p;
318 1.4.2.2 bouyer struct ifnet *ifp;
319 1.4.2.2 bouyer {
320 1.4.2.2 bouyer if (in6p && in6p->in6p_hops >= 0)
321 1.4.2.2 bouyer return(in6p->in6p_hops);
322 1.4.2.2 bouyer else if (ifp)
323 1.4.2.2 bouyer return(nd_ifinfo[ifp->if_index].chlim);
324 1.4.2.2 bouyer else
325 1.4.2.2 bouyer return(ip6_defhlim);
326 1.4.2.2 bouyer }
327 1.4.2.2 bouyer
328 1.4.2.2 bouyer /*
329 1.4.2.2 bouyer * Find an empty port and set it to the specified PCB.
330 1.4.2.2 bouyer */
331 1.4.2.2 bouyer int
332 1.4.2.2 bouyer in6_pcbsetport(laddr, in6p)
333 1.4.2.2 bouyer struct in6_addr *laddr;
334 1.4.2.2 bouyer struct in6pcb *in6p;
335 1.4.2.2 bouyer {
336 1.4.2.2 bouyer struct socket *so = in6p->in6p_socket;
337 1.4.2.2 bouyer struct in6pcb *head = in6p->in6p_head;
338 1.4.2.2 bouyer u_int16_t last_port, lport = 0;
339 1.4.2.2 bouyer int wild = 0;
340 1.4.2.2 bouyer void *t;
341 1.4.2.2 bouyer u_int16_t min, max;
342 1.4.2.2 bouyer struct proc *p = curproc; /* XXX */
343 1.4.2.2 bouyer
344 1.4.2.2 bouyer /* XXX: this is redundant when called from in6_pcbbind */
345 1.4.2.2 bouyer if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
346 1.4.2.2 bouyer ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
347 1.4.2.2 bouyer (so->so_options & SO_ACCEPTCONN) == 0))
348 1.4.2.2 bouyer wild = IN6PLOOKUP_WILDCARD;
349 1.4.2.2 bouyer
350 1.4.2.2 bouyer if (in6p->in6p_flags & IN6P_LOWPORT) {
351 1.4.2.2 bouyer #ifndef IPNOPRIVPORTS
352 1.4.2.2 bouyer if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0))
353 1.4.2.2 bouyer return (EACCES);
354 1.4.2.2 bouyer #endif
355 1.4.2.2 bouyer min = ip6_lowportmin;
356 1.4.2.2 bouyer max = ip6_lowportmax;
357 1.4.2.2 bouyer } else {
358 1.4.2.2 bouyer min = ip6_anonportmin;
359 1.4.2.2 bouyer max = ip6_anonportmax;
360 1.4.2.2 bouyer }
361 1.4.2.2 bouyer
362 1.4.2.2 bouyer /* value out of range */
363 1.4.2.2 bouyer if (head->in6p_lport < min)
364 1.4.2.2 bouyer head->in6p_lport = min;
365 1.4.2.2 bouyer else if (head->in6p_lport > max)
366 1.4.2.2 bouyer head->in6p_lport = min;
367 1.4.2.2 bouyer last_port = head->in6p_lport;
368 1.4.2.2 bouyer goto startover; /*to randomize*/
369 1.4.2.2 bouyer for (;;) {
370 1.4.2.2 bouyer lport = htons(head->in6p_lport);
371 1.4.2.2 bouyer if (IN6_IS_ADDR_V4MAPPED(laddr)) {
372 1.4.2.2 bouyer #if 0
373 1.4.2.2 bouyer t = in_pcblookup_bind(&tcbtable,
374 1.4.2.2 bouyer (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
375 1.4.2.2 bouyer lport);
376 1.4.2.2 bouyer #else
377 1.4.2.2 bouyer t = NULL;
378 1.4.2.2 bouyer #endif
379 1.4.2.2 bouyer } else {
380 1.4.2.2 bouyer t = in6_pcblookup(head, &zeroin6_addr, 0, laddr,
381 1.4.2.2 bouyer lport, wild);
382 1.4.2.2 bouyer }
383 1.4.2.2 bouyer if (t == 0)
384 1.4.2.2 bouyer break;
385 1.4.2.2 bouyer startover:
386 1.4.2.2 bouyer if (head->in6p_lport >= max)
387 1.4.2.2 bouyer head->in6p_lport = min;
388 1.4.2.2 bouyer else
389 1.4.2.2 bouyer head->in6p_lport++;
390 1.4.2.2 bouyer if (head->in6p_lport == last_port)
391 1.4.2.2 bouyer return (EADDRINUSE);
392 1.4.2.2 bouyer }
393 1.4.2.2 bouyer
394 1.4.2.2 bouyer in6p->in6p_lport = lport;
395 1.4.2.2 bouyer return(0); /* success */
396 1.4.2.2 bouyer }
397 1.4.2.2 bouyer
398 1.4.2.2 bouyer /*
399 1.4.2.2 bouyer * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
400 1.4.2.2 bouyer * If the address scope of is link-local, embed the interface index in the
401 1.4.2.2 bouyer * address. The routine determines our precedence
402 1.4.2.2 bouyer * between advanced API scope/interface specification and basic API
403 1.4.2.2 bouyer * specification.
404 1.4.2.2 bouyer *
405 1.4.2.2 bouyer * this function should be nuked in the future, when we get rid of
406 1.4.2.2 bouyer * embedded scopeid thing.
407 1.4.2.2 bouyer *
408 1.4.2.2 bouyer * XXX actually, it is over-specification to return ifp against sin6_scope_id.
409 1.4.2.2 bouyer * there can be multiple interfaces that belong to a particular scope zone
410 1.4.2.2 bouyer * (in specification, we have 1:N mapping between a scope zone and interfaces).
411 1.4.2.2 bouyer * we may want to change the function to return something other than ifp.
412 1.4.2.2 bouyer */
413 1.4.2.2 bouyer int
414 1.4.2.2 bouyer in6_embedscope(in6, sin6, in6p, ifpp)
415 1.4.2.2 bouyer struct in6_addr *in6;
416 1.4.2.2 bouyer const struct sockaddr_in6 *sin6;
417 1.4.2.2 bouyer struct in6pcb *in6p;
418 1.4.2.2 bouyer struct ifnet **ifpp;
419 1.4.2.2 bouyer {
420 1.4.2.2 bouyer struct ifnet *ifp = NULL;
421 1.4.2.2 bouyer u_int32_t scopeid;
422 1.4.2.2 bouyer
423 1.4.2.2 bouyer *in6 = sin6->sin6_addr;
424 1.4.2.2 bouyer scopeid = sin6->sin6_scope_id;
425 1.4.2.2 bouyer if (ifpp)
426 1.4.2.2 bouyer *ifpp = NULL;
427 1.4.2.2 bouyer
428 1.4.2.2 bouyer /*
429 1.4.2.2 bouyer * don't try to read sin6->sin6_addr beyond here, since the caller may
430 1.4.2.2 bouyer * ask us to overwrite existing sockaddr_in6
431 1.4.2.2 bouyer */
432 1.4.2.2 bouyer
433 1.4.2.2 bouyer #ifdef ENABLE_DEFAULT_SCOPE
434 1.4.2.2 bouyer if (scopeid == 0)
435 1.4.2.2 bouyer scopeid = scope6_addr2default(in6);
436 1.4.2.2 bouyer #endif
437 1.4.2.2 bouyer
438 1.4.2.2 bouyer if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
439 1.4.2.2 bouyer struct in6_pktinfo *pi;
440 1.4.2.2 bouyer
441 1.4.2.2 bouyer /*
442 1.4.2.2 bouyer * KAME assumption: link id == interface id
443 1.4.2.2 bouyer */
444 1.4.2.2 bouyer
445 1.4.2.2 bouyer if (in6p && in6p->in6p_outputopts &&
446 1.4.2.2 bouyer (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
447 1.4.2.2 bouyer pi->ipi6_ifindex) {
448 1.4.2.2 bouyer ifp = ifindex2ifnet[pi->ipi6_ifindex];
449 1.4.2.2 bouyer in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
450 1.4.2.2 bouyer } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
451 1.4.2.2 bouyer in6p->in6p_moptions &&
452 1.4.2.2 bouyer in6p->in6p_moptions->im6o_multicast_ifp) {
453 1.4.2.2 bouyer ifp = in6p->in6p_moptions->im6o_multicast_ifp;
454 1.4.2.2 bouyer in6->s6_addr16[1] = htons(ifp->if_index);
455 1.4.2.2 bouyer } else if (scopeid) {
456 1.4.2.2 bouyer /* boundary check */
457 1.4.2.2 bouyer if (scopeid < 0 || if_index < scopeid)
458 1.4.2.2 bouyer return ENXIO; /* XXX EINVAL? */
459 1.4.2.2 bouyer ifp = ifindex2ifnet[scopeid];
460 1.4.2.2 bouyer /*XXX assignment to 16bit from 32bit variable */
461 1.4.2.2 bouyer in6->s6_addr16[1] = htons(scopeid & 0xffff);
462 1.4.2.2 bouyer }
463 1.4.2.2 bouyer
464 1.4.2.2 bouyer if (ifpp)
465 1.4.2.2 bouyer *ifpp = ifp;
466 1.4.2.2 bouyer }
467 1.4.2.2 bouyer
468 1.4.2.2 bouyer return 0;
469 1.4.2.2 bouyer }
470 1.4.2.2 bouyer
471 1.4.2.2 bouyer /*
472 1.4.2.2 bouyer * generate standard sockaddr_in6 from embedded form.
473 1.4.2.2 bouyer * touches sin6_addr and sin6_scope_id only.
474 1.4.2.2 bouyer *
475 1.4.2.2 bouyer * this function should be nuked in the future, when we get rid of
476 1.4.2.2 bouyer * embedded scopeid thing.
477 1.4.2.2 bouyer */
478 1.4.2.2 bouyer int
479 1.4.2.2 bouyer in6_recoverscope(sin6, in6, ifp)
480 1.4.2.2 bouyer struct sockaddr_in6 *sin6;
481 1.4.2.2 bouyer const struct in6_addr *in6;
482 1.4.2.2 bouyer struct ifnet *ifp;
483 1.4.2.2 bouyer {
484 1.4.2.2 bouyer u_int32_t scopeid;
485 1.4.2.2 bouyer
486 1.4.2.2 bouyer sin6->sin6_addr = *in6;
487 1.4.2.2 bouyer
488 1.4.2.2 bouyer /*
489 1.4.2.2 bouyer * don't try to read *in6 beyond here, since the caller may
490 1.4.2.2 bouyer * ask us to overwrite existing sockaddr_in6
491 1.4.2.2 bouyer */
492 1.4.2.2 bouyer
493 1.4.2.2 bouyer sin6->sin6_scope_id = 0;
494 1.4.2.2 bouyer if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
495 1.4.2.2 bouyer /*
496 1.4.2.2 bouyer * KAME assumption: link id == interface id
497 1.4.2.2 bouyer */
498 1.4.2.2 bouyer scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
499 1.4.2.2 bouyer if (scopeid) {
500 1.4.2.2 bouyer /* sanity check */
501 1.4.2.2 bouyer if (scopeid < 0 || if_index < scopeid)
502 1.4.2.2 bouyer return ENXIO;
503 1.4.2.2 bouyer if (ifp && ifp->if_index != scopeid)
504 1.4.2.2 bouyer return ENXIO;
505 1.4.2.2 bouyer sin6->sin6_addr.s6_addr16[1] = 0;
506 1.4.2.2 bouyer sin6->sin6_scope_id = scopeid;
507 1.4.2.2 bouyer }
508 1.4.2.2 bouyer }
509 1.4.2.2 bouyer
510 1.4.2.2 bouyer return 0;
511 1.4.2.2 bouyer }
512 1.4.2.3 bouyer
513 1.4.2.3 bouyer /*
514 1.4.2.3 bouyer * just clear the embedded scope identifer.
515 1.4.2.3 bouyer * XXX: currently used for bsdi4 only as a supplement function.
516 1.4.2.3 bouyer */
517 1.4.2.3 bouyer void
518 1.4.2.3 bouyer in6_clearscope(addr)
519 1.4.2.3 bouyer struct in6_addr *addr;
520 1.4.2.3 bouyer {
521 1.4.2.3 bouyer if (IN6_IS_SCOPE_LINKLOCAL(addr))
522 1.4.2.3 bouyer addr->s6_addr16[1] = 0;
523 1.4.2.3 bouyer }
524