]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_pcb.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_pcb.c
1 /*
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
25 * All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. Neither the name of the project nor the names of its contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 *
51 */
52
53 /*
54 * Copyright (c) 1982, 1986, 1991, 1993
55 * The Regents of the University of California. All rights reserved.
56 *
57 * Redistribution and use in source and binary forms, with or without
58 * modification, are permitted provided that the following conditions
59 * are met:
60 * 1. Redistributions of source code must retain the above copyright
61 * notice, this list of conditions and the following disclaimer.
62 * 2. Redistributions in binary form must reproduce the above copyright
63 * notice, this list of conditions and the following disclaimer in the
64 * documentation and/or other materials provided with the distribution.
65 * 3. All advertising materials mentioning features or use of this software
66 * must display the following acknowledgement:
67 * This product includes software developed by the University of
68 * California, Berkeley and its contributors.
69 * 4. Neither the name of the University nor the names of its contributors
70 * may be used to endorse or promote products derived from this software
71 * without specific prior written permission.
72 *
73 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
74 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
78 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
79 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
81 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
82 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * SUCH DAMAGE.
84 *
85 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
86 */
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/malloc.h>
91 #include <sys/mbuf.h>
92 #include <sys/domain.h>
93 #include <sys/protosw.h>
94 #include <sys/socket.h>
95 #include <sys/socketvar.h>
96 #include <sys/sockio.h>
97 #include <sys/errno.h>
98 #include <sys/time.h>
99 #include <sys/proc.h>
100
101 #include <net/if.h>
102 #include <net/if_types.h>
103 #include <net/route.h>
104
105 #include <netinet/in.h>
106 #include <netinet/in_var.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/ip6.h>
109 #include <netinet/ip_var.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/nd6.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet6/in6_pcb.h>
114 #include <net/if_types.h>
115
116 #include <kern/kern_types.h>
117 #include <kern/zalloc.h>
118
119 #include "faith.h"
120 #if defined(NFAITH) && NFAITH > 0
121 #include <net/if_faith.h>
122 #endif
123
124 #if IPSEC
125 #include <netinet6/ipsec.h>
126 #if INET6
127 #include <netinet6/ipsec6.h>
128 #endif
129 #include <netinet6/ah.h>
130 #if INET6
131 #include <netinet6/ah6.h>
132 #endif
133 #include <netkey/key.h>
134 extern lck_mtx_t *sadb_mutex;
135 #endif /* IPSEC */
136
137 struct in6_addr zeroin6_addr;
138
139 int
140 in6_pcbbind(
141 struct inpcb *inp,
142 struct sockaddr *nam,
143 struct proc *p)
144 {
145 struct socket *so = inp->inp_socket;
146 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
147 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
148 u_short lport = 0;
149 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
150
151 if (!in6_ifaddrs) /* XXX broken! */
152 return (EADDRNOTAVAIL);
153 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
154 return(EINVAL);
155 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
156 wild = 1;
157 socket_unlock(so, 0); /* keep reference */
158 lck_rw_lock_exclusive(pcbinfo->mtx);
159 if (nam) {
160 sin6 = (struct sockaddr_in6 *)nam;
161 if (nam->sa_len != sizeof(*sin6)) {
162 lck_rw_done(pcbinfo->mtx);
163 socket_lock(so, 0);
164 return(EINVAL);
165 }
166 /*
167 * family check.
168 */
169 if (nam->sa_family != AF_INET6) {
170 lck_rw_done(pcbinfo->mtx);
171 socket_lock(so, 0);
172 return(EAFNOSUPPORT);
173 }
174
175 /* KAME hack: embed scopeid */
176 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) {
177 lck_rw_done(pcbinfo->mtx);
178 socket_lock(so, 0);
179 return EINVAL;
180 }
181 /* this must be cleared for ifa_ifwithaddr() */
182 sin6->sin6_scope_id = 0;
183
184 lport = sin6->sin6_port;
185 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
186 /*
187 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
188 * allow compepte duplication of binding if
189 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
190 * and a multicast address is bound on both
191 * new and duplicated sockets.
192 */
193 if (so->so_options & SO_REUSEADDR)
194 reuseport = SO_REUSEADDR|SO_REUSEPORT;
195 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
196 struct ifaddr *ia = NULL;
197
198 sin6->sin6_port = 0; /* yech... */
199 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) {
200 lck_rw_done(pcbinfo->mtx);
201 socket_lock(so, 0);
202 return(EADDRNOTAVAIL);
203 }
204
205 /*
206 * XXX: bind to an anycast address might accidentally
207 * cause sending a packet with anycast source address.
208 * We should allow to bind to a deprecated address, since
209 * the application dare to use it.
210 */
211 if (ia &&
212 ((struct in6_ifaddr *)ia)->ia6_flags &
213 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
214 ifafree(ia);
215 lck_rw_done(pcbinfo->mtx);
216 socket_lock(so, 0);
217 return(EADDRNOTAVAIL);
218 }
219 ifafree(ia);
220 ia = NULL;
221 }
222 if (lport) {
223 struct inpcb *t;
224
225 /* GROSS */
226 if (ntohs(lport) < IPV6PORT_RESERVED && p &&
227 ((so->so_state & SS_PRIV) == 0)) {
228 lck_rw_done(pcbinfo->mtx);
229 socket_lock(so, 0);
230 return(EACCES);
231 }
232
233 if (so->so_uid &&
234 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
235 t = in6_pcblookup_local(pcbinfo,
236 &sin6->sin6_addr, lport,
237 INPLOOKUP_WILDCARD);
238 if (t &&
239 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
240 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
241 (t->inp_socket->so_options &
242 SO_REUSEPORT) == 0) &&
243 so->so_uid != t->inp_socket->so_uid) {
244 lck_rw_done(pcbinfo->mtx);
245 socket_lock(so, 0);
246 return (EADDRINUSE);
247 }
248 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
249 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
250 struct sockaddr_in sin;
251
252 in6_sin6_2_sin(&sin, sin6);
253 t = in_pcblookup_local(pcbinfo,
254 sin.sin_addr, lport,
255 INPLOOKUP_WILDCARD);
256 if (t &&
257 (so->so_uid !=
258 t->inp_socket->so_uid) &&
259 (ntohl(t->inp_laddr.s_addr) !=
260 INADDR_ANY ||
261 INP_SOCKAF(so) ==
262 INP_SOCKAF(t->inp_socket))) {
263
264 lck_rw_done(pcbinfo->mtx);
265 socket_lock(so, 0);
266 return (EADDRINUSE);
267 }
268 }
269 }
270 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
271 lport, wild);
272 if (t && (reuseport & t->inp_socket->so_options) == 0) {
273 lck_rw_done(pcbinfo->mtx);
274 socket_lock(so, 0);
275 return(EADDRINUSE);
276 }
277 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
278 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
279 struct sockaddr_in sin;
280
281 in6_sin6_2_sin(&sin, sin6);
282 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
283 lport, wild);
284 if (t &&
285 (reuseport & t->inp_socket->so_options)
286 == 0 &&
287 (ntohl(t->inp_laddr.s_addr)
288 != INADDR_ANY ||
289 INP_SOCKAF(so) ==
290 INP_SOCKAF(t->inp_socket))) {
291 lck_rw_done(pcbinfo->mtx);
292 socket_lock(so, 0);
293 return (EADDRINUSE);
294 }
295 }
296 }
297 inp->in6p_laddr = sin6->sin6_addr;
298 }
299 socket_lock(so, 0);
300 if (lport == 0) {
301 int e;
302 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, p, 1)) != 0) {
303 lck_rw_done(pcbinfo->mtx);
304 return(e);
305 }
306 }
307 else {
308 inp->inp_lport = lport;
309 if (in_pcbinshash(inp, 1) != 0) {
310 inp->in6p_laddr = in6addr_any;
311 inp->inp_lport = 0;
312 lck_rw_done(pcbinfo->mtx);
313 return (EAGAIN);
314 }
315 }
316 lck_rw_done(pcbinfo->mtx);
317 return(0);
318 }
319
320 /*
321 * Transform old in6_pcbconnect() into an inner subroutine for new
322 * in6_pcbconnect(): Do some validity-checking on the remote
323 * address (in mbuf 'nam') and then determine local host address
324 * (i.e., which interface) to use to access that remote host.
325 *
326 * This preserves definition of in6_pcbconnect(), while supporting a
327 * slightly different version for T/TCP. (This is more than
328 * a bit of a kludge, but cleaning up the internal interfaces would
329 * have forced minor changes in every protocol).
330 */
331
332 int
333 in6_pcbladdr(
334 struct inpcb *inp,
335 struct sockaddr *nam,
336 struct in6_addr *plocal_addr6)
337 {
338 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
339 struct in6_addr *addr6 = NULL;
340 struct in6_addr src_storage;
341
342 struct ifnet *ifp = NULL;
343 int error = 0;
344
345 if (nam->sa_len != sizeof (*sin6))
346 return (EINVAL);
347 if (sin6->sin6_family != AF_INET6)
348 return (EAFNOSUPPORT);
349 if (sin6->sin6_port == 0)
350 return (EADDRNOTAVAIL);
351
352 /* KAME hack: embed scopeid */
353 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
354 return EINVAL;
355
356 if (in6_ifaddrs) {
357 /*
358 * If the destination address is UNSPECIFIED addr,
359 * use the loopback addr, e.g ::1.
360 */
361 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
362 sin6->sin6_addr = in6addr_loopback;
363 }
364 {
365 /*
366 * XXX: in6_selectsrc might replace the bound local address
367 * with the address specified by setsockopt(IPV6_PKTINFO).
368 * Is it the intended behavior?
369 */
370 addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
371 inp->in6p_moptions,
372 &inp->in6p_route,
373 &inp->in6p_laddr, &src_storage, &error);
374 if (addr6 == 0) {
375 if (error == 0)
376 error = EADDRNOTAVAIL;
377 return(error);
378 }
379 *plocal_addr6 = *addr6;
380 /*
381 * Don't do pcblookup call here; return interface in
382 * plocal_addr6
383 * and exit to caller, that will do the lookup.
384 */
385 }
386
387 if (inp->in6p_route.ro_rt)
388 ifp = inp->in6p_route.ro_rt->rt_ifp;
389
390 return(0);
391 }
392
393 /*
394 * Outer subroutine:
395 * Connect from a socket to a specified address.
396 * Both address and port must be specified in argument sin.
397 * If don't have a local address for this socket yet,
398 * then pick one.
399 */
400 int
401 in6_pcbconnect(inp, nam, p)
402 struct inpcb *inp;
403 struct sockaddr *nam;
404 struct proc *p;
405 {
406 struct in6_addr addr6;
407 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
408 struct inpcb *pcb;
409 int error;
410
411 /*
412 * Call inner routine, to assign local interface address.
413 * in6_pcbladdr() may automatically fill in sin6_scope_id.
414 */
415 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
416 return(error);
417 socket_unlock(inp->inp_socket, 0);
418 pcb = in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
419 sin6->sin6_port,
420 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
421 ? &addr6 : &inp->in6p_laddr,
422 inp->inp_lport, 0, NULL);
423 socket_lock(inp->inp_socket, 0);
424 if (pcb != NULL) {
425 in_pcb_checkstate(pcb, WNT_RELEASE, 0);
426 return (EADDRINUSE);
427 }
428 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
429 if (inp->inp_lport == 0) {
430 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
431 if (error)
432 return (error);
433 }
434 inp->in6p_laddr = addr6;
435 }
436 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
437 /*lock inversion issue, mostly with udp multicast packets */
438 socket_unlock(inp->inp_socket, 0);
439 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
440 socket_lock(inp->inp_socket, 0);
441 }
442 inp->in6p_faddr = sin6->sin6_addr;
443 inp->inp_fport = sin6->sin6_port;
444 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
445 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
446 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
447 inp->in6p_flowinfo |=
448 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
449
450 in_pcbrehash(inp);
451 lck_rw_done(inp->inp_pcbinfo->mtx);
452 return (0);
453 }
454
455 #if 0
456 /*
457 * Return an IPv6 address, which is the most appropriate for given
458 * destination and user specified options.
459 * If necessary, this function lookups the routing table and return
460 * an entry to the caller for later use.
461 */
462 struct in6_addr *
463 in6_selectsrc(
464 struct sockaddr_in6 *dstsock,
465 struct ip6_pktopts *opts,
466 struct ip6_moptions *mopts,
467 struct route_in6 *ro,
468 struct in6_addr *laddr,
469 struct in6_addr *src_storage,
470 int *errorp)
471 {
472 struct in6_addr *dst;
473 struct in6_ifaddr *ia6 = 0;
474 struct in6_pktinfo *pi = NULL;
475
476 dst = &dstsock->sin6_addr;
477 *errorp = 0;
478
479 /*
480 * If the source address is explicitly specified by the caller,
481 * use it.
482 */
483 if (opts && (pi = opts->ip6po_pktinfo) &&
484 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
485 return(&pi->ipi6_addr);
486
487 /*
488 * If the source address is not specified but the socket(if any)
489 * is already bound, use the bound address.
490 */
491 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
492 return(laddr);
493
494 /*
495 * If the caller doesn't specify the source address but
496 * the outgoing interface, use an address associated with
497 * the interface.
498 */
499 if (pi && pi->ipi6_ifindex) {
500 /* XXX boundary check is assumed to be already done. */
501 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
502 dst);
503 if (ia6 == 0) {
504 *errorp = EADDRNOTAVAIL;
505 return(0);
506 }
507 *src_storage = satosin6(&ia6->ia_addr)->sin6_addr;
508 ifafree(&ia6->ia_ifa);
509 return(src_storage);
510 }
511
512 /*
513 * If the destination address is a link-local unicast address or
514 * a multicast address, and if the outgoing interface is specified
515 * by the sin6_scope_id filed, use an address associated with the
516 * interface.
517 * XXX: We're now trying to define more specific semantics of
518 * sin6_scope_id field, so this part will be rewritten in
519 * the near future.
520 */
521 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
522 dstsock->sin6_scope_id) {
523 /*
524 * I'm not sure if boundary check for scope_id is done
525 * somewhere...
526 */
527 if (dstsock->sin6_scope_id < 0 ||
528 if_index < dstsock->sin6_scope_id) {
529 *errorp = ENXIO; /* XXX: better error? */
530 return(0);
531 }
532 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
533 dst);
534 if (ia6 == 0) {
535 *errorp = EADDRNOTAVAIL;
536 return(0);
537 }
538 *src_storage = satosin6(&ia6->ia_addr)->sin6_addr;
539 ifafree(&ia6->ia_ifa);
540 return(src_storage);
541 }
542
543 /*
544 * If the destination address is a multicast address and
545 * the outgoing interface for the address is specified
546 * by the caller, use an address associated with the interface.
547 * There is a sanity check here; if the destination has node-local
548 * scope, the outgoing interfacde should be a loopback address.
549 * Even if the outgoing interface is not specified, we also
550 * choose a loopback interface as the outgoing interface.
551 */
552 if (IN6_IS_ADDR_MULTICAST(dst)) {
553 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
554
555 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
556 ifp = &loif[0];
557 }
558
559 if (ifp) {
560 ia6 = in6_ifawithscope(ifp, dst);
561 if (ia6 == 0) {
562 *errorp = EADDRNOTAVAIL;
563 return(0);
564 }
565 *src_storage = ia6->ia_addr.sin6_addr;
566 ifafree(&ia6->ia_ifa);
567 return(src_storage);
568 }
569 }
570
571 /*
572 * If the next hop address for the packet is specified
573 * by caller, use an address associated with the route
574 * to the next hop.
575 */
576 {
577 struct sockaddr_in6 *sin6_next;
578 struct rtentry *rt;
579
580 if (opts && opts->ip6po_nexthop) {
581 sin6_next = satosin6(opts->ip6po_nexthop);
582 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL, 0);
583 if (rt) {
584 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
585 if (ia6 == 0) {
586 ifaref(&rt->rt_ifa);
587 ia6 = ifatoia6(rt->rt_ifa);
588 }
589 }
590 if (ia6 == 0) {
591 *errorp = EADDRNOTAVAIL;
592 return(0);
593 }
594 *src_storage = satosin6(&ia6->ia_addr)->sin6_addr;
595 ifaref(&rt->rt_ifa);
596 return(src_storage);
597 }
598 }
599
600 /*
601 * If route is known or can be allocated now,
602 * our src addr is taken from the i/f, else punt.
603 */
604 if (ro) {
605 if (ro->ro_rt &&
606 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
607 rtfree(ro->ro_rt);
608 ro->ro_rt = (struct rtentry *)0;
609 }
610 if (ro->ro_rt == (struct rtentry *)0 ||
611 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
612 struct sockaddr_in6 *dst6;
613
614 /* No route yet, so try to acquire one */
615 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
616 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
617 dst6->sin6_family = AF_INET6;
618 dst6->sin6_len = sizeof(struct sockaddr_in6);
619 dst6->sin6_addr = *dst;
620 if (IN6_IS_ADDR_MULTICAST(dst)) {
621 ro->ro_rt = rtalloc1(&((struct route *)ro)
622 ->ro_dst, 0, 0UL);
623 } else {
624 rtalloc((struct route *)ro);
625 }
626 }
627
628 /*
629 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
630 * the address. But we don't know why it does so.
631 * It is necessary to ensure the scope even for lo0
632 * so doesn't check out IFF_LOOPBACK.
633 */
634
635 if (ro->ro_rt) {
636 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
637 if (ia6 == 0) { /* xxx scope error ?*/
638 ifaref(ro->ro_rt->rt_ifa);
639 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
640 }
641 }
642 if (ia6 == 0) {
643 *errorp = EHOSTUNREACH; /* no route */
644 return(0);
645 }
646 *src_storage = satosin6(&ia6->ia_addr)->sin6_addr;
647 ifaref(&rt->rt_ifa);
648 return(src_storage);
649 }
650
651 *errorp = EADDRNOTAVAIL;
652 return(0);
653 }
654
655 /*
656 * Default hop limit selection. The precedence is as follows:
657 * 1. Hoplimit valued specified via ioctl.
658 * 2. (If the outgoing interface is detected) the current
659 * hop limit of the interface specified by router advertisement.
660 * 3. The system default hoplimit.
661 */
662 int
663 in6_selecthlim(
664 struct in6pcb *in6p,
665 struct ifnet *ifp)
666 {
667 if (in6p && in6p->in6p_hops >= 0)
668 return(in6p->in6p_hops);
669 else if (ifp)
670 return(nd_ifinfo[ifp->if_index].chlim);
671 else
672 return(ip6_defhlim);
673 }
674 #endif
675
676 void
677 in6_pcbdisconnect(inp)
678 struct inpcb *inp;
679 {
680 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
681 /*lock inversion issue, mostly with udp multicast packets */
682 socket_unlock(inp->inp_socket, 0);
683 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
684 socket_lock(inp->inp_socket, 0);
685 }
686 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
687 inp->inp_fport = 0;
688 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
689 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
690 in_pcbrehash(inp);
691 lck_rw_done(inp->inp_pcbinfo->mtx);
692 if (inp->inp_socket->so_state & SS_NOFDREF)
693 in6_pcbdetach(inp);
694 }
695
696 void
697 in6_pcbdetach(inp)
698 struct inpcb *inp;
699 {
700 struct socket *so = inp->inp_socket;
701 struct inpcbinfo *ipi = inp->inp_pcbinfo;
702
703 #if IPSEC
704 if (inp->in6p_sp != NULL) {
705 lck_mtx_lock(sadb_mutex);
706 ipsec6_delete_pcbpolicy(inp);
707 lck_mtx_unlock(sadb_mutex);
708 }
709 #endif /* IPSEC */
710
711 if (in_pcb_checkstate(inp, WNT_STOPUSING, 1) != WNT_STOPUSING)
712 printf("in6_pcbdetach so=%x can't be marked dead ok\n", so);
713
714 inp->inp_state = INPCB_STATE_DEAD;
715
716 if ((so->so_flags & SOF_PCBCLEARING) == 0) {
717 inp->inp_vflag = 0;
718 so->so_flags |= SOF_PCBCLEARING;
719 inp->inp_gencnt = ++ipi->ipi_gencnt;
720 if (inp->in6p_options)
721 m_freem(inp->in6p_options);
722 ip6_freepcbopts(inp->in6p_outputopts);
723 ip6_freemoptions(inp->in6p_moptions);
724 if (inp->in6p_route.ro_rt)
725 rtfree(inp->in6p_route.ro_rt);
726 /* Check and free IPv4 related resources in case of mapped addr */
727 if (inp->inp_options)
728 (void)m_free(inp->inp_options);
729 ip_freemoptions(inp->inp_moptions);
730 inp->inp_moptions = NULL;
731
732 }
733 }
734
735 struct sockaddr *
736 in6_sockaddr(port, addr_p)
737 in_port_t port;
738 struct in6_addr *addr_p;
739 {
740 struct sockaddr_in6 *sin6;
741
742 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
743 bzero(sin6, sizeof *sin6);
744 sin6->sin6_family = AF_INET6;
745 sin6->sin6_len = sizeof(*sin6);
746 sin6->sin6_port = port;
747 sin6->sin6_addr = *addr_p;
748 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
749 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
750 else
751 sin6->sin6_scope_id = 0; /*XXX*/
752 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
753 sin6->sin6_addr.s6_addr16[1] = 0;
754
755 return (struct sockaddr *)sin6;
756 }
757
758 struct sockaddr *
759 in6_v4mapsin6_sockaddr(port, addr_p)
760 in_port_t port;
761 struct in_addr *addr_p;
762 {
763 struct sockaddr_in sin;
764 struct sockaddr_in6 *sin6_p;
765
766 bzero(&sin, sizeof sin);
767 sin.sin_family = AF_INET;
768 sin.sin_len = sizeof(sin);
769 sin.sin_port = port;
770 sin.sin_addr = *addr_p;
771
772 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
773 M_WAITOK);
774 in6_sin_2_v4mapsin6(&sin, sin6_p);
775
776 return (struct sockaddr *)sin6_p;
777 }
778
779 /*
780 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
781 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
782 * in struct pr_usrreqs, so that protocols can just reference then directly
783 * without the need for a wrapper function. The socket must have a valid
784 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
785 * except through a kernel programming error, so it is acceptable to panic
786 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
787 * because there actually /is/ a programming error somewhere... XXX)
788 */
789 int
790 in6_setsockaddr(so, nam)
791 struct socket *so;
792 struct sockaddr **nam;
793 {
794 struct inpcb *inp;
795 struct in6_addr addr;
796 in_port_t port;
797
798 inp = sotoinpcb(so);
799 if (!inp) {
800 return EINVAL;
801 }
802 port = inp->inp_lport;
803 addr = inp->in6p_laddr;
804
805 *nam = in6_sockaddr(port, &addr);
806 return 0;
807 }
808
809 int
810 in6_setpeeraddr(so, nam)
811 struct socket *so;
812 struct sockaddr **nam;
813 {
814 struct inpcb *inp;
815 struct in6_addr addr;
816 in_port_t port;
817
818 inp = sotoinpcb(so);
819 if (!inp) {
820 return EINVAL;
821 }
822 port = inp->inp_fport;
823 addr = inp->in6p_faddr;
824
825 *nam = in6_sockaddr(port, &addr);
826 return 0;
827 }
828
829 int
830 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
831 {
832 struct inpcb *inp = sotoinpcb(so);
833 int error;
834
835 if (inp == NULL)
836 return EINVAL;
837 if (inp->inp_vflag & INP_IPV4) {
838 error = in_setsockaddr(so, nam);
839 if (error == 0)
840 in6_sin_2_v4mapsin6_in_sock(nam);
841 } else
842 /* scope issues will be handled in in6_setsockaddr(). */
843 error = in6_setsockaddr(so, nam);
844
845 return error;
846 }
847
848 int
849 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
850 {
851 struct inpcb *inp = sotoinpcb(so);
852 int error;
853
854 if (inp == NULL)
855 return EINVAL;
856 if (inp->inp_vflag & INP_IPV4) {
857 error = in_setpeeraddr(so, nam);
858 if (error == 0)
859 in6_sin_2_v4mapsin6_in_sock(nam);
860 } else
861 /* scope issues will be handled in in6_setpeeraddr(). */
862 error = in6_setpeeraddr(so, nam);
863
864 return error;
865 }
866
867 /*
868 * Pass some notification to all connections of a protocol
869 * associated with address dst. The local address and/or port numbers
870 * may be specified to limit the search. The "usual action" will be
871 * taken, depending on the ctlinput cmd. The caller must filter any
872 * cmds that are uninteresting (e.g., no error in the map).
873 * Call the protocol specific routine (if any) to report
874 * any errors for each matching socket.
875 *
876 * Must be called at splnet.
877 */
878 void
879 in6_pcbnotify(pcbinfo, dst, fport_arg, src, lport_arg, cmd, notify)
880 struct inpcbinfo *pcbinfo;
881 struct sockaddr *dst;
882 const struct sockaddr *src;
883 u_int fport_arg, lport_arg;
884 int cmd;
885 // struct inpcb *(*notify)(struct inpcb *, int);
886 void (*notify)(struct inpcb *, int);
887 {
888 struct inpcb *inp, *ninp;
889 struct sockaddr_in6 sa6_src, *sa6_dst;
890 u_short fport = fport_arg, lport = lport_arg;
891 u_int32_t flowinfo;
892 int errno;
893 struct inpcbhead *head = pcbinfo->listhead;
894
895 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
896 return;
897
898 sa6_dst = (struct sockaddr_in6 *)dst;
899 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
900 return;
901
902 /*
903 * note that src can be NULL when we get notify by local fragmentation.
904 */
905 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
906 flowinfo = sa6_src.sin6_flowinfo;
907
908 /*
909 * Redirects go to all references to the destination,
910 * and use in6_rtchange to invalidate the route cache.
911 * Dead host indications: also use in6_rtchange to invalidate
912 * the cache, and deliver the error to all the sockets.
913 * Otherwise, if we have knowledge of the local port and address,
914 * deliver only to that socket.
915 */
916 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
917 fport = 0;
918 lport = 0;
919 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
920
921 if (cmd != PRC_HOSTDEAD)
922 notify = in6_rtchange;
923 }
924 errno = inet6ctlerrmap[cmd];
925 lck_rw_lock_shared(pcbinfo->mtx);
926 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
927 ninp = LIST_NEXT(inp, inp_list);
928
929 if ((inp->inp_vflag & INP_IPV6) == 0)
930 continue;
931
932 /*
933 * Detect if we should notify the error. If no source and
934 * destination ports are specifed, but non-zero flowinfo and
935 * local address match, notify the error. This is the case
936 * when the error is delivered with an encrypted buffer
937 * by ESP. Otherwise, just compare addresses and ports
938 * as usual.
939 */
940 if (lport == 0 && fport == 0 && flowinfo &&
941 inp->inp_socket != NULL &&
942 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
943 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
944 goto do_notify;
945 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
946 &sa6_dst->sin6_addr) ||
947 inp->inp_socket == 0 ||
948 (lport && inp->inp_lport != lport) ||
949 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
950 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
951 &sa6_src.sin6_addr)) ||
952 (fport && inp->inp_fport != fport))
953 continue;
954
955
956 do_notify:
957 if (notify) {
958 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING)
959 continue;
960 socket_lock(inp->inp_socket, 1);
961 (*notify)(inp, errno);
962 (void)in_pcb_checkstate(inp, WNT_RELEASE, 1);
963 socket_unlock(inp->inp_socket, 1);
964 }
965 }
966 lck_rw_done(pcbinfo->mtx);
967 }
968
969 /*
970 * Lookup a PCB based on the local address and port.
971 */
972 struct inpcb *
973 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
974 struct inpcbinfo *pcbinfo;
975 struct in6_addr *laddr;
976 u_int lport_arg;
977 int wild_okay;
978 {
979 struct inpcb *inp;
980 int matchwild = 3, wildcard;
981 u_short lport = lport_arg;
982
983 if (!wild_okay) {
984 struct inpcbhead *head;
985 /*
986 * Look for an unconnected (wildcard foreign addr) PCB that
987 * matches the local address and port we're looking for.
988 */
989 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
990 pcbinfo->hashmask)];
991 LIST_FOREACH(inp, head, inp_hash) {
992 if ((inp->inp_vflag & INP_IPV6) == 0)
993 continue;
994 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
995 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
996 inp->inp_lport == lport) {
997 /*
998 * Found.
999 */
1000 return (inp);
1001 }
1002 }
1003 /*
1004 * Not found.
1005 */
1006 return (NULL);
1007 } else {
1008 struct inpcbporthead *porthash;
1009 struct inpcbport *phd;
1010 struct inpcb *match = NULL;
1011 /*
1012 * Best fit PCB lookup.
1013 *
1014 * First see if this local port is in use by looking on the
1015 * port hash list.
1016 */
1017 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
1018 pcbinfo->porthashmask)];
1019 LIST_FOREACH(phd, porthash, phd_hash) {
1020 if (phd->phd_port == lport)
1021 break;
1022 }
1023 if (phd != NULL) {
1024 /*
1025 * Port is in use by one or more PCBs. Look for best
1026 * fit.
1027 */
1028 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1029 wildcard = 0;
1030 if ((inp->inp_vflag & INP_IPV6) == 0)
1031 continue;
1032 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
1033 wildcard++;
1034 if (!IN6_IS_ADDR_UNSPECIFIED(
1035 &inp->in6p_laddr)) {
1036 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
1037 wildcard++;
1038 else if (!IN6_ARE_ADDR_EQUAL(
1039 &inp->in6p_laddr, laddr))
1040 continue;
1041 } else {
1042 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
1043 wildcard++;
1044 }
1045 if (wildcard < matchwild) {
1046 match = inp;
1047 matchwild = wildcard;
1048 if (matchwild == 0) {
1049 break;
1050 }
1051 }
1052 }
1053 }
1054 return (match);
1055 }
1056 }
1057 #ifndef APPLE
1058 /* this is not used in Darwin */
1059 void
1060 in6_pcbpurgeif0(
1061 struct in6pcb *head,
1062 struct ifnet *ifp)
1063 {
1064 struct in6pcb *in6p;
1065 struct ip6_moptions *im6o;
1066 struct in6_multi_mship *imm, *nimm;
1067
1068 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
1069 im6o = in6p->in6p_moptions;
1070 if ((in6p->inp_vflag & INP_IPV6) &&
1071 im6o) {
1072 /*
1073 * Unselect the outgoing interface if it is being
1074 * detached.
1075 */
1076 if (im6o->im6o_multicast_ifp == ifp)
1077 im6o->im6o_multicast_ifp = NULL;
1078
1079 /*
1080 * Drop multicast group membership if we joined
1081 * through the interface being detached.
1082 * XXX controversial - is it really legal for kernel
1083 * to force this?
1084 */
1085 for (imm = im6o->im6o_memberships.lh_first;
1086 imm != NULL; imm = nimm) {
1087 nimm = imm->i6mm_chain.le_next;
1088 if (imm->i6mm_maddr->in6m_ifp == ifp) {
1089 LIST_REMOVE(imm, i6mm_chain);
1090 in6_delmulti(imm->i6mm_maddr);
1091 FREE(imm, M_IPMADDR);
1092 }
1093 }
1094 }
1095 }
1096 }
1097 #endif
1098
1099 /*
1100 * Check for alternatives when higher level complains
1101 * about service problems. For now, invalidate cached
1102 * routing information. If the route was created dynamically
1103 * (by a redirect), time to try a default gateway again.
1104 */
1105 void
1106 in6_losing(in6p)
1107 struct inpcb *in6p;
1108 {
1109 struct rtentry *rt;
1110 struct rt_addrinfo info;
1111
1112 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1113 in6p->in6p_route.ro_rt = 0;
1114 bzero((caddr_t)&info, sizeof(info));
1115 info.rti_info[RTAX_DST] =
1116 (struct sockaddr *)&in6p->in6p_route.ro_dst;
1117 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1118 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1119 lck_mtx_lock(rt_mtx);
1120 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
1121 if (rt->rt_flags & RTF_DYNAMIC)
1122 (void)rtrequest_locked(RTM_DELETE, rt_key(rt),
1123 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
1124 (struct rtentry **)0);
1125 else
1126 /*
1127 * A new route can be allocated
1128 * the next time output is attempted.
1129 */
1130 rtfree_locked(rt);
1131 lck_mtx_unlock(rt_mtx);
1132 }
1133 }
1134
1135 /*
1136 * After a routing change, flush old routing
1137 * and allocate a (hopefully) better one.
1138 */
1139 void
1140 in6_rtchange(
1141 struct inpcb *inp,
1142 int errno)
1143 {
1144 if (inp->in6p_route.ro_rt) {
1145 rtfree(inp->in6p_route.ro_rt);
1146 inp->in6p_route.ro_rt = 0;
1147 /*
1148 * A new route can be allocated the next time
1149 * output is attempted.
1150 */
1151 }
1152 }
1153
1154 /*
1155 * Lookup PCB in hash list.
1156 */
1157 struct inpcb *
1158 in6_pcblookup_hash(
1159 struct inpcbinfo *pcbinfo,
1160 struct in6_addr *faddr,
1161 u_int fport_arg,
1162 struct in6_addr *laddr,
1163 u_int lport_arg,
1164 int wildcard,
1165 struct ifnet *ifp)
1166 {
1167 struct inpcbhead *head;
1168 struct inpcb *inp;
1169 u_short fport = fport_arg, lport = lport_arg;
1170 int faith;
1171
1172 #if defined(NFAITH) && NFAITH > 0
1173 faith = faithprefix(laddr);
1174 #else
1175 faith = 0;
1176 #endif
1177
1178 lck_rw_lock_shared(pcbinfo->mtx);
1179
1180 /*
1181 * First look for an exact match.
1182 */
1183 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1184 lport, fport,
1185 pcbinfo->hashmask)];
1186 LIST_FOREACH(inp, head, inp_hash) {
1187 if ((inp->inp_vflag & INP_IPV6) == 0)
1188 continue;
1189 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1190 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1191 inp->inp_fport == fport &&
1192 inp->inp_lport == lport) {
1193 /*
1194 * Found. Check if pcb is still valid
1195 */
1196 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1197 lck_rw_done(pcbinfo->mtx);
1198 return (inp);
1199 }
1200 else { /* it's there but dead, say it isn't found */
1201 lck_rw_done(pcbinfo->mtx);
1202 return(NULL);
1203 }
1204 }
1205 }
1206 if (wildcard) {
1207 struct inpcb *local_wild = NULL;
1208
1209 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1210 pcbinfo->hashmask)];
1211 LIST_FOREACH(inp, head, inp_hash) {
1212 if ((inp->inp_vflag & INP_IPV6) == 0)
1213 continue;
1214 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1215 inp->inp_lport == lport) {
1216 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1217 continue;
1218 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1219 laddr)) {
1220 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1221 lck_rw_done(pcbinfo->mtx);
1222 return (inp);
1223 }
1224 else { /* it's there but dead, say it isn't found */
1225 lck_rw_done(pcbinfo->mtx);
1226 return(NULL);
1227 }
1228 }
1229 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1230 local_wild = inp;
1231 }
1232 }
1233 if (local_wild && in_pcb_checkstate(local_wild, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1234 lck_rw_done(pcbinfo->mtx);
1235 return (local_wild);
1236 }
1237 else {
1238 lck_rw_done(pcbinfo->mtx);
1239 return (NULL);
1240 }
1241 }
1242
1243 /*
1244 * Not found.
1245 */
1246 lck_rw_done(pcbinfo->mtx);
1247 return (NULL);
1248 }
1249
1250 void
1251 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1252 {
1253 struct ip6_hdr *ip;
1254
1255 ip = mtod(m, struct ip6_hdr *);
1256 bzero(sin6, sizeof(*sin6));
1257 sin6->sin6_len = sizeof(*sin6);
1258 sin6->sin6_family = AF_INET6;
1259 sin6->sin6_addr = ip->ip6_src;
1260 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1261 sin6->sin6_addr.s6_addr16[1] = 0;
1262 sin6->sin6_scope_id =
1263 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1264 ? m->m_pkthdr.rcvif->if_index : 0;
1265
1266 return;
1267 }