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