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