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