2 * Copyright (c) 2003-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the project nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Copyright (c) 1982, 1986, 1991, 1993
60 * The Regents of the University of California. All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Berkeley and its contributors.
74 * 4. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
93 #include <sys/param.h>
94 #include <sys/systm.h>
95 #include <sys/malloc.h>
97 #include <sys/domain.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/sockio.h>
102 #include <sys/errno.h>
103 #include <sys/time.h>
104 #include <sys/proc.h>
105 #include <sys/kauth.h>
106 #include <sys/priv.h>
109 #include <net/if_types.h>
110 #include <net/route.h>
111 #include <net/ntstat.h>
112 #include <net/restricted_in_port.h>
114 #include <netinet/in.h>
115 #include <netinet/in_var.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/ip6.h>
118 #include <netinet/ip_var.h>
120 #include <netinet6/ip6_var.h>
121 #include <netinet6/nd6.h>
122 #include <netinet/in_pcb.h>
123 #include <netinet6/in6_pcb.h>
125 #include <net/if_types.h>
126 #include <net/if_var.h>
128 #include <kern/kern_types.h>
129 #include <kern/zalloc.h>
132 #include <netinet6/ipsec.h>
134 #include <netinet6/ipsec6.h>
136 #include <netinet6/ah.h>
138 #include <netinet6/ah6.h>
140 #include <netkey/key.h>
144 #include <net/necp.h>
148 * in6_pcblookup_local_and_cleanup does everything
149 * in6_pcblookup_local does but it checks for a socket
150 * that's going away. Since we know that the lock is
151 * held read+write when this function is called, we
152 * can safely dispose of this socket like the slow
153 * timer would usually do and return NULL. This is
156 static struct inpcb
*
157 in6_pcblookup_local_and_cleanup(struct inpcbinfo
*pcbinfo
,
158 struct in6_addr
*laddr
, u_int lport_arg
, int wild_okay
)
162 /* Perform normal lookup */
163 inp
= in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
);
165 /* Check if we found a match but it's waiting to be disposed */
166 if (inp
!= NULL
&& inp
->inp_wantcnt
== WNT_STOPUSING
) {
167 struct socket
*so
= inp
->inp_socket
;
171 if (so
->so_usecount
== 0) {
172 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
175 in_pcbdispose(inp
); /* will unlock & destroy */
178 socket_unlock(so
, 0);
186 * Bind an INPCB to an address and/or port. This routine should not alter
187 * the caller-supplied local address "nam".
190 in6_pcbbind(struct inpcb
*inp
, struct sockaddr
*nam
, struct proc
*p
)
192 struct socket
*so
= inp
->inp_socket
;
193 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
195 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
196 struct ifnet
*outif
= NULL
;
197 struct sockaddr_in6 sin6
;
201 #endif /* !CONFIG_EMBEDDED */
203 if (TAILQ_EMPTY(&in6_ifaddrhead
)) { /* XXX broken! */
204 return EADDRNOTAVAIL
;
206 if (!(so
->so_options
& (SO_REUSEADDR
| SO_REUSEPORT
))) {
210 socket_unlock(so
, 0); /* keep reference */
211 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
212 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
213 /* another thread completed the bind */
214 lck_rw_done(pcbinfo
->ipi_lock
);
219 bzero(&sin6
, sizeof(sin6
));
221 if (nam
->sa_len
!= sizeof(struct sockaddr_in6
)) {
222 lck_rw_done(pcbinfo
->ipi_lock
);
229 if (nam
->sa_family
!= AF_INET6
) {
230 lck_rw_done(pcbinfo
->ipi_lock
);
234 lport
= SIN6(nam
)->sin6_port
;
236 *(&sin6
) = *SIN6(nam
);
238 /* KAME hack: embed scopeid */
239 if (in6_embedscope(&sin6
.sin6_addr
, &sin6
, inp
, NULL
,
241 lck_rw_done(pcbinfo
->ipi_lock
);
246 /* Sanitize local copy for address searches */
247 sin6
.sin6_flowinfo
= 0;
248 sin6
.sin6_scope_id
= 0;
251 if (IN6_IS_ADDR_MULTICAST(&sin6
.sin6_addr
)) {
253 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
254 * allow compepte duplication of binding if
255 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
256 * and a multicast address is bound on both
257 * new and duplicated sockets.
259 if (so
->so_options
& SO_REUSEADDR
) {
260 reuseport
= SO_REUSEADDR
| SO_REUSEPORT
;
262 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
)) {
265 ifa
= ifa_ifwithaddr(SA(&sin6
));
267 lck_rw_done(pcbinfo
->ipi_lock
);
269 return EADDRNOTAVAIL
;
272 * XXX: bind to an anycast address might
273 * accidentally cause sending a packet with
274 * anycast source address. We should allow
275 * to bind to a deprecated address, since
276 * the application dare to use it.
279 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
280 (IN6_IFF_ANYCAST
| IN6_IFF_NOTREADY
|
281 IN6_IFF_DETACHED
| IN6_IFF_CLAT46
)) {
284 lck_rw_done(pcbinfo
->ipi_lock
);
286 return EADDRNOTAVAIL
;
289 * Opportunistically determine the outbound
290 * interface that may be used; this may not
291 * hold true if we end up using a route
292 * going over a different interface, e.g.
293 * when sending to a local address. This
294 * will get updated again after sending.
296 outif
= ifa
->ifa_ifp
;
308 if (ntohs(lport
) < IPV6PORT_RESERVED
&&
309 !IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
) &&
310 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
)) {
311 cred
= kauth_cred_proc_ref(p
);
312 error
= priv_check_cred(cred
,
313 PRIV_NETINET_RESERVEDPORT
, 0);
314 kauth_cred_unref(&cred
);
316 lck_rw_done(pcbinfo
->ipi_lock
);
321 #endif /* !CONFIG_EMBEDDED */
323 * Check wether the process is allowed to bind to a restricted port
325 if (!current_task_can_use_restricted_in_port(lport
,
326 so
->so_proto
->pr_protocol
, PORT_FLAGS_BSD
)) {
327 lck_rw_done(pcbinfo
->ipi_lock
);
332 if (!IN6_IS_ADDR_MULTICAST(&sin6
.sin6_addr
) &&
333 (u
= kauth_cred_getuid(so
->so_cred
)) != 0) {
334 t
= in6_pcblookup_local_and_cleanup(pcbinfo
,
335 &sin6
.sin6_addr
, lport
,
338 (!IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
) ||
339 !IN6_IS_ADDR_UNSPECIFIED(&t
->in6p_laddr
) ||
340 !(t
->inp_socket
->so_options
& SO_REUSEPORT
)) &&
341 (u
!= kauth_cred_getuid(t
->inp_socket
->so_cred
)) &&
342 !(t
->inp_socket
->so_flags
& SOF_REUSESHAREUID
) &&
343 (!(t
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
344 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
345 uuid_compare(t
->necp_client_uuid
, inp
->necp_client_uuid
) != 0)) {
346 lck_rw_done(pcbinfo
->ipi_lock
);
350 if (!(inp
->inp_flags
& IN6P_IPV6_V6ONLY
) &&
351 IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
)) {
352 struct sockaddr_in sin
;
354 in6_sin6_2_sin(&sin
, &sin6
);
355 t
= in_pcblookup_local_and_cleanup(
356 pcbinfo
, sin
.sin_addr
, lport
,
359 !(t
->inp_socket
->so_options
& SO_REUSEPORT
) &&
360 (kauth_cred_getuid(so
->so_cred
) !=
361 kauth_cred_getuid(t
->inp_socket
->so_cred
)) &&
362 (t
->inp_laddr
.s_addr
!= INADDR_ANY
||
363 SOCK_DOM(so
) == SOCK_DOM(t
->inp_socket
)) &&
364 (!(t
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
365 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
366 uuid_compare(t
->necp_client_uuid
, inp
->necp_client_uuid
) != 0)) {
367 lck_rw_done(pcbinfo
->ipi_lock
);
374 t
= in6_pcblookup_local_and_cleanup(pcbinfo
,
375 &sin6
.sin6_addr
, lport
, wild
);
377 (reuseport
& t
->inp_socket
->so_options
) == 0 &&
378 (!(t
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
379 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
380 uuid_compare(t
->necp_client_uuid
, inp
->necp_client_uuid
) != 0)) {
381 lck_rw_done(pcbinfo
->ipi_lock
);
385 if (!(inp
->inp_flags
& IN6P_IPV6_V6ONLY
) &&
386 IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
)) {
387 struct sockaddr_in sin
;
389 in6_sin6_2_sin(&sin
, &sin6
);
390 t
= in_pcblookup_local_and_cleanup(pcbinfo
,
391 sin
.sin_addr
, lport
, wild
);
392 if (t
!= NULL
&& (reuseport
&
393 t
->inp_socket
->so_options
) == 0 &&
394 (t
->inp_laddr
.s_addr
!= INADDR_ANY
||
395 SOCK_DOM(so
) == SOCK_DOM(t
->inp_socket
)) &&
396 (!(t
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
397 !(inp
->inp_flags2
& INP2_EXTERNAL_PORT
) ||
398 uuid_compare(t
->necp_client_uuid
, inp
->necp_client_uuid
) != 0)) {
399 lck_rw_done(pcbinfo
->ipi_lock
);
409 * We unlocked socket's protocol lock for a long time.
410 * The socket might have been dropped/defuncted.
411 * Checking if world has changed since.
413 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
414 lck_rw_done(pcbinfo
->ipi_lock
);
418 /* check if the socket got bound when the lock was released */
419 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
420 lck_rw_done(pcbinfo
->ipi_lock
);
424 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
)) {
425 inp
->in6p_laddr
= sin6
.sin6_addr
;
426 inp
->in6p_last_outifp
= outif
;
431 if ((e
= in6_pcbsetport(&inp
->in6p_laddr
, inp
, p
, 1)) != 0) {
432 /* Undo any address bind from above. */
433 inp
->in6p_laddr
= in6addr_any
;
434 inp
->in6p_last_outifp
= NULL
;
435 lck_rw_done(pcbinfo
->ipi_lock
);
439 inp
->inp_lport
= lport
;
440 if (in_pcbinshash(inp
, 1) != 0) {
441 inp
->in6p_laddr
= in6addr_any
;
443 inp
->in6p_last_outifp
= NULL
;
444 lck_rw_done(pcbinfo
->ipi_lock
);
448 lck_rw_done(pcbinfo
->ipi_lock
);
449 sflt_notify(so
, sock_evt_bound
, NULL
);
454 * Transform old in6_pcbconnect() into an inner subroutine for new
455 * in6_pcbconnect(); do some validity-checking on the remote address
456 * (in "nam") and then determine local host address (i.e., which
457 * interface) to use to access that remote host.
459 * This routine may alter the caller-supplied remote address "nam".
461 * This routine might return an ifp with a reference held if the caller
462 * provides a non-NULL outif, even in the error case. The caller is
463 * responsible for releasing its reference.
466 in6_pcbladdr(struct inpcb
*inp
, struct sockaddr
*nam
,
467 struct in6_addr
*plocal_addr6
, struct ifnet
**outif
)
469 struct in6_addr
*addr6
= NULL
;
470 struct in6_addr src_storage
;
472 unsigned int ifscope
;
477 if (nam
->sa_len
!= sizeof(struct sockaddr_in6
)) {
480 if (SIN6(nam
)->sin6_family
!= AF_INET6
) {
483 if (SIN6(nam
)->sin6_port
== 0) {
484 return EADDRNOTAVAIL
;
487 /* KAME hack: embed scopeid */
488 if (in6_embedscope(&SIN6(nam
)->sin6_addr
, SIN6(nam
), inp
, NULL
, NULL
) != 0) {
492 if (!TAILQ_EMPTY(&in6_ifaddrhead
)) {
494 * If the destination address is UNSPECIFIED addr,
495 * use the loopback addr, e.g ::1.
497 if (IN6_IS_ADDR_UNSPECIFIED(&SIN6(nam
)->sin6_addr
)) {
498 SIN6(nam
)->sin6_addr
= in6addr_loopback
;
502 ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
503 inp
->inp_boundifp
->if_index
: IFSCOPE_NONE
;
506 * XXX: in6_selectsrc might replace the bound local address
507 * with the address specified by setsockopt(IPV6_PKTINFO).
508 * Is it the intended behavior?
510 * in6_selectsrc() might return outif with its reference held
511 * even in the error case; caller always needs to release it
514 addr6
= in6_selectsrc(SIN6(nam
), inp
->in6p_outputopts
, inp
,
515 &inp
->in6p_route
, outif
, &src_storage
, ifscope
, &error
);
518 struct rtentry
*rt
= inp
->in6p_route
.ro_rt
;
520 * If in6_selectsrc() returns a route, it should be one
521 * which points to the same ifp as outif. Just in case
522 * it isn't, use the one from the route for consistency.
523 * Otherwise if there is no route, leave outif alone as
524 * it could still be useful to the caller.
526 if (rt
!= NULL
&& rt
->rt_ifp
!= *outif
) {
527 ifnet_reference(rt
->rt_ifp
); /* for caller */
528 if (*outif
!= NULL
) {
529 ifnet_release(*outif
);
536 if (outif
!= NULL
&& (*outif
) != NULL
&&
537 inp_restricted_send(inp
, *outif
)) {
538 soevent(inp
->inp_socket
,
539 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_IFDENIED
));
540 error
= EHOSTUNREACH
;
543 error
= EADDRNOTAVAIL
;
548 *plocal_addr6
= *addr6
;
550 * Don't do pcblookup call here; return interface in
551 * plocal_addr6 and exit to caller, that will do the lookup.
558 * Connect from a socket to a specified address.
559 * Both address and port must be specified in argument sin.
560 * If don't have a local address for this socket yet,
564 in6_pcbconnect(struct inpcb
*inp
, struct sockaddr
*nam
, struct proc
*p
)
566 struct in6_addr addr6
;
567 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)nam
;
570 struct ifnet
*outif
= NULL
;
571 struct socket
*so
= inp
->inp_socket
;
574 so
->so_state_change_cnt
++;
577 if (so
->so_proto
->pr_protocol
== IPPROTO_UDP
&&
578 sin6
->sin6_port
== htons(53) && !(so
->so_flags1
& SOF1_DNS_COUNTED
)) {
579 so
->so_flags1
|= SOF1_DNS_COUNTED
;
580 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_inet_dgram_dns
);
584 * Call inner routine, to assign local interface address.
585 * in6_pcbladdr() may automatically fill in sin6_scope_id.
587 * in6_pcbladdr() might return an ifp with its reference held
588 * even in the error case, so make sure that it's released
589 * whenever it's non-NULL.
591 if ((error
= in6_pcbladdr(inp
, nam
, &addr6
, &outif
)) != 0) {
592 if (outif
!= NULL
&& inp_restricted_send(inp
, outif
)) {
594 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_IFDENIED
));
598 socket_unlock(so
, 0);
599 pcb
= in6_pcblookup_hash(inp
->inp_pcbinfo
, &sin6
->sin6_addr
,
600 sin6
->sin6_port
, IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
) ?
601 &addr6
: &inp
->in6p_laddr
, inp
->inp_lport
, 0, NULL
);
604 in_pcb_checkstate(pcb
, WNT_RELEASE
, pcb
== inp
? 1 : 0);
608 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
609 if (inp
->inp_lport
== 0) {
610 error
= in6_pcbbind(inp
, NULL
, p
);
615 inp
->in6p_laddr
= addr6
;
616 inp
->in6p_last_outifp
= outif
; /* no reference needed */
617 inp
->in6p_flags
|= INP_IN6ADDR_ANY
;
619 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
620 /* lock inversion issue, mostly with udp multicast packets */
621 socket_unlock(so
, 0);
622 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
625 inp
->in6p_faddr
= sin6
->sin6_addr
;
626 inp
->inp_fport
= sin6
->sin6_port
;
627 if (nstat_collect
&& SOCK_PROTO(so
) == IPPROTO_UDP
) {
628 nstat_pcb_invalidate_cache(inp
);
631 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
635 ifnet_release(outif
);
642 in6_pcbdisconnect(struct inpcb
*inp
)
644 struct socket
*so
= inp
->inp_socket
;
648 so
->so_state_change_cnt
++;
652 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
653 /* lock inversion issue, mostly with udp multicast packets */
654 socket_unlock(so
, 0);
655 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
658 if (nstat_collect
&& SOCK_PROTO(so
) == IPPROTO_UDP
) {
659 nstat_pcb_cache(inp
);
661 bzero((caddr_t
)&inp
->in6p_faddr
, sizeof(inp
->in6p_faddr
));
663 /* clear flowinfo - RFC 6437 */
664 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
666 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
668 * A multipath subflow socket would have its SS_NOFDREF set by default,
669 * so check for SOF_MP_SUBFLOW socket flag before detaching the PCB;
670 * when the socket is closed for real, SOF_MP_SUBFLOW would be cleared.
672 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) && (so
->so_state
& SS_NOFDREF
)) {
678 in6_pcbdetach(struct inpcb
*inp
)
680 struct socket
*so
= inp
->inp_socket
;
682 if (so
->so_pcb
== NULL
) {
683 /* PCB has been disposed */
684 panic("%s: inp=%p so=%p proto=%d so_pcb is null!\n", __func__
,
685 inp
, so
, SOCK_PROTO(so
));
690 if (inp
->in6p_sp
!= NULL
) {
691 (void) ipsec6_delete_pcbpolicy(inp
);
695 if (inp
->inp_stat
!= NULL
&& SOCK_PROTO(so
) == IPPROTO_UDP
) {
696 if (inp
->inp_stat
->rxpackets
== 0 && inp
->inp_stat
->txpackets
== 0) {
697 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_inet6_dgram_no_data
);
702 * Let NetworkStatistics know this PCB is going away
703 * before we detach it.
706 (SOCK_PROTO(so
) == IPPROTO_TCP
|| SOCK_PROTO(so
) == IPPROTO_UDP
)) {
707 nstat_pcb_detach(inp
);
709 /* mark socket state as dead */
710 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
) {
711 panic("%s: so=%p proto=%d couldn't set to STOPUSING\n",
712 __func__
, so
, SOCK_PROTO(so
));
716 if (!(so
->so_flags
& SOF_PCBCLEARING
)) {
717 struct ip_moptions
*imo
;
718 struct ip6_moptions
*im6o
;
721 if (inp
->in6p_options
!= NULL
) {
722 m_freem(inp
->in6p_options
);
723 inp
->in6p_options
= NULL
;
725 ip6_freepcbopts(inp
->in6p_outputopts
);
726 inp
->in6p_outputopts
= NULL
;
727 ROUTE_RELEASE(&inp
->in6p_route
);
728 /* free IPv4 related resources in case of mapped addr */
729 if (inp
->inp_options
!= NULL
) {
730 (void) m_free(inp
->inp_options
);
731 inp
->inp_options
= NULL
;
733 im6o
= inp
->in6p_moptions
;
734 inp
->in6p_moptions
= NULL
;
736 imo
= inp
->inp_moptions
;
737 inp
->inp_moptions
= NULL
;
739 sofreelastref(so
, 0);
740 inp
->inp_state
= INPCB_STATE_DEAD
;
741 /* makes sure we're not called twice from so_close */
742 so
->so_flags
|= SOF_PCBCLEARING
;
744 inpcb_gc_sched(inp
->inp_pcbinfo
, INPCB_TIMER_FAST
);
747 * See inp_join_group() for why we need to unlock
749 if (im6o
!= NULL
|| imo
!= NULL
) {
750 socket_unlock(so
, 0);
763 in6_sockaddr(in_port_t port
, struct in6_addr
*addr_p
)
765 struct sockaddr_in6
*sin6
;
767 MALLOC(sin6
, struct sockaddr_in6
*, sizeof(*sin6
), M_SONAME
, M_WAITOK
);
771 bzero(sin6
, sizeof(*sin6
));
772 sin6
->sin6_family
= AF_INET6
;
773 sin6
->sin6_len
= sizeof(*sin6
);
774 sin6
->sin6_port
= port
;
775 sin6
->sin6_addr
= *addr_p
;
777 /* would be good to use sa6_recoverscope(), except for locking */
778 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
)) {
779 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
781 sin6
->sin6_scope_id
= 0; /* XXX */
783 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
)) {
784 sin6
->sin6_addr
.s6_addr16
[1] = 0;
787 return (struct sockaddr
*)sin6
;
791 in6_sockaddr_s(in_port_t port
, struct in6_addr
*addr_p
,
792 struct sockaddr_in6
*sin6
)
794 bzero(sin6
, sizeof(*sin6
));
795 sin6
->sin6_family
= AF_INET6
;
796 sin6
->sin6_len
= sizeof(*sin6
);
797 sin6
->sin6_port
= port
;
798 sin6
->sin6_addr
= *addr_p
;
800 /* would be good to use sa6_recoverscope(), except for locking */
801 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
)) {
802 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
804 sin6
->sin6_scope_id
= 0; /* XXX */
806 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
)) {
807 sin6
->sin6_addr
.s6_addr16
[1] = 0;
812 * The calling convention of in6_getsockaddr() and in6_getpeeraddr() was
813 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
814 * in struct pr_usrreqs, so that protocols can just reference then directly
815 * without the need for a wrapper function.
818 in6_getsockaddr(struct socket
*so
, struct sockaddr
**nam
)
821 struct in6_addr addr
;
824 if ((inp
= sotoinpcb(so
)) == NULL
) {
828 port
= inp
->inp_lport
;
829 addr
= inp
->in6p_laddr
;
831 *nam
= in6_sockaddr(port
, &addr
);
839 in6_getsockaddr_s(struct socket
*so
, struct sockaddr_in6
*ss
)
842 struct in6_addr addr
;
846 bzero(ss
, sizeof(*ss
));
848 if ((inp
= sotoinpcb(so
)) == NULL
) {
852 port
= inp
->inp_lport
;
853 addr
= inp
->in6p_laddr
;
855 in6_sockaddr_s(port
, &addr
, ss
);
860 in6_getpeeraddr(struct socket
*so
, struct sockaddr
**nam
)
863 struct in6_addr addr
;
866 if ((inp
= sotoinpcb(so
)) == NULL
) {
870 port
= inp
->inp_fport
;
871 addr
= inp
->in6p_faddr
;
873 *nam
= in6_sockaddr(port
, &addr
);
881 in6_mapped_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
883 struct inpcb
*inp
= sotoinpcb(so
);
889 if (inp
->inp_vflag
& INP_IPV4
) {
890 error
= in_getsockaddr(so
, nam
);
892 error
= in6_sin_2_v4mapsin6_in_sock(nam
);
895 /* scope issues will be handled in in6_getsockaddr(). */
896 error
= in6_getsockaddr(so
, nam
);
902 in6_mapped_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
904 struct inpcb
*inp
= sotoinpcb(so
);
910 if (inp
->inp_vflag
& INP_IPV4
) {
911 error
= in_getpeeraddr(so
, nam
);
913 error
= in6_sin_2_v4mapsin6_in_sock(nam
);
916 /* scope issues will be handled in in6_getpeeraddr(). */
917 error
= in6_getpeeraddr(so
, nam
);
923 * Pass some notification to all connections of a protocol
924 * associated with address dst. The local address and/or port numbers
925 * may be specified to limit the search. The "usual action" will be
926 * taken, depending on the ctlinput cmd. The caller must filter any
927 * cmds that are uninteresting (e.g., no error in the map).
928 * Call the protocol specific routine (if any) to report
929 * any errors for each matching socket.
932 in6_pcbnotify(struct inpcbinfo
*pcbinfo
, struct sockaddr
*dst
, u_int fport_arg
,
933 const struct sockaddr
*src
, u_int lport_arg
, int cmd
, void *cmdarg
,
934 void (*notify
)(struct inpcb
*, int))
936 struct inpcbhead
*head
= pcbinfo
->ipi_listhead
;
937 struct inpcb
*inp
, *ninp
;
938 struct sockaddr_in6 sa6_src
, *sa6_dst
;
939 u_short fport
= fport_arg
, lport
= lport_arg
;
943 if ((unsigned)cmd
>= PRC_NCMDS
|| dst
->sa_family
!= AF_INET6
) {
947 sa6_dst
= (struct sockaddr_in6
*)(void *)dst
;
948 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst
->sin6_addr
)) {
953 * note that src can be NULL when we get notify by local fragmentation.
955 sa6_src
= (src
== NULL
) ?
956 sa6_any
: *(struct sockaddr_in6
*)(uintptr_t)(size_t)src
;
957 flowinfo
= sa6_src
.sin6_flowinfo
;
960 * Redirects go to all references to the destination,
961 * and use in6_rtchange to invalidate the route cache.
962 * Dead host indications: also use in6_rtchange to invalidate
963 * the cache, and deliver the error to all the sockets.
964 * Otherwise, if we have knowledge of the local port and address,
965 * deliver only to that socket.
967 if (PRC_IS_REDIRECT(cmd
) || cmd
== PRC_HOSTDEAD
) {
970 bzero((caddr_t
)&sa6_src
.sin6_addr
, sizeof(sa6_src
.sin6_addr
));
972 if (cmd
!= PRC_HOSTDEAD
) {
973 notify
= in6_rtchange
;
976 errno
= inet6ctlerrmap
[cmd
];
977 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
978 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
979 ninp
= LIST_NEXT(inp
, inp_list
);
981 if (!(inp
->inp_vflag
& INP_IPV6
)) {
986 * If the error designates a new path MTU for a destination
987 * and the application (associated with this socket) wanted to
988 * know the value, notify. Note that we notify for all
989 * disconnected sockets if the corresponding application
990 * wanted. This is because some UDP applications keep sending
991 * sockets disconnected.
992 * XXX: should we avoid to notify the value to TCP sockets?
994 if (cmd
== PRC_MSGSIZE
) {
995 ip6_notify_pmtu(inp
, (struct sockaddr_in6
*)(void *)dst
,
996 (u_int32_t
*)cmdarg
);
1000 * Detect if we should notify the error. If no source and
1001 * destination ports are specifed, but non-zero flowinfo and
1002 * local address match, notify the error. This is the case
1003 * when the error is delivered with an encrypted buffer
1004 * by ESP. Otherwise, just compare addresses and ports
1007 if (lport
== 0 && fport
== 0 && flowinfo
&&
1008 inp
->inp_socket
!= NULL
&&
1009 flowinfo
== (inp
->inp_flow
& IPV6_FLOWLABEL_MASK
) &&
1010 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &sa6_src
.sin6_addr
)) {
1012 } else if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
,
1013 &sa6_dst
->sin6_addr
) || inp
->inp_socket
== NULL
||
1014 (lport
&& inp
->inp_lport
!= lport
) ||
1015 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src
.sin6_addr
) &&
1016 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1017 &sa6_src
.sin6_addr
)) || (fport
&& inp
->inp_fport
!= fport
)) {
1023 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) ==
1027 socket_lock(inp
->inp_socket
, 1);
1028 (*notify
)(inp
, errno
);
1029 (void) in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
1030 socket_unlock(inp
->inp_socket
, 1);
1033 lck_rw_done(pcbinfo
->ipi_lock
);
1037 * Lookup a PCB based on the local address and port.
1040 in6_pcblookup_local(struct inpcbinfo
*pcbinfo
, struct in6_addr
*laddr
,
1041 u_int lport_arg
, int wild_okay
)
1044 int matchwild
= 3, wildcard
;
1045 u_short lport
= lport_arg
;
1046 struct inpcbporthead
*porthash
;
1047 struct inpcb
*match
= NULL
;
1048 struct inpcbport
*phd
;
1051 struct inpcbhead
*head
;
1053 * Look for an unconnected (wildcard foreign addr) PCB that
1054 * matches the local address and port we're looking for.
1056 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1057 pcbinfo
->ipi_hashmask
)];
1058 LIST_FOREACH(inp
, head
, inp_hash
) {
1059 if (!(inp
->inp_vflag
& INP_IPV6
)) {
1062 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1063 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1064 inp
->inp_lport
== lport
) {
1077 * Best fit PCB lookup.
1079 * First see if this local port is in use by looking on the
1082 porthash
= &pcbinfo
->ipi_porthashbase
[INP_PCBPORTHASH(lport
,
1083 pcbinfo
->ipi_porthashmask
)];
1084 LIST_FOREACH(phd
, porthash
, phd_hash
) {
1085 if (phd
->phd_port
== lport
) {
1091 * Port is in use by one or more PCBs. Look for best
1094 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
1096 if (!(inp
->inp_vflag
& INP_IPV6
)) {
1099 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
)) {
1102 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
1103 if (IN6_IS_ADDR_UNSPECIFIED(laddr
)) {
1105 } else if (!IN6_ARE_ADDR_EQUAL(
1106 &inp
->in6p_laddr
, laddr
)) {
1110 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
)) {
1114 if (wildcard
< matchwild
) {
1116 matchwild
= wildcard
;
1117 if (matchwild
== 0) {
1127 * Check for alternatives when higher level complains
1128 * about service problems. For now, invalidate cached
1129 * routing information. If the route was created dynamically
1130 * (by a redirect), time to try a default gateway again.
1133 in6_losing(struct inpcb
*in6p
)
1137 if ((rt
= in6p
->in6p_route
.ro_rt
) != NULL
) {
1139 if (rt
->rt_flags
& RTF_DYNAMIC
) {
1141 * Prevent another thread from modifying rt_key,
1142 * rt_gateway via rt_setgate() after the rt_lock
1143 * is dropped by marking the route as defunct.
1145 rt
->rt_flags
|= RTF_CONDEMNED
;
1147 (void) rtrequest(RTM_DELETE
, rt_key(rt
),
1148 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
1153 * A new route can be allocated
1154 * the next time output is attempted.
1157 ROUTE_RELEASE(&in6p
->in6p_route
);
1161 * After a routing change, flush old routing
1162 * and allocate a (hopefully) better one.
1165 in6_rtchange(struct inpcb
*inp
, int errno
)
1167 #pragma unused(errno)
1169 * A new route can be allocated the next time
1170 * output is attempted.
1172 ROUTE_RELEASE(&inp
->in6p_route
);
1176 * Check if PCB exists hash list. Also returns uid and gid of socket
1179 in6_pcblookup_hash_exists(struct inpcbinfo
*pcbinfo
, struct in6_addr
*faddr
,
1180 u_int fport_arg
, struct in6_addr
*laddr
, u_int lport_arg
, int wildcard
,
1181 uid_t
*uid
, gid_t
*gid
, struct ifnet
*ifp
)
1183 struct inpcbhead
*head
;
1185 u_short fport
= fport_arg
, lport
= lport_arg
;
1191 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
1194 * First look for an exact match.
1196 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1197 lport
, fport
, pcbinfo
->ipi_hashmask
)];
1198 LIST_FOREACH(inp
, head
, inp_hash
) {
1199 if (!(inp
->inp_vflag
& INP_IPV6
)) {
1203 if (inp_restricted_recv(inp
, ifp
)) {
1208 if (!necp_socket_is_allowed_to_recv_on_interface(inp
, ifp
)) {
1213 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1214 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1215 inp
->inp_fport
== fport
&&
1216 inp
->inp_lport
== lport
) {
1217 if ((found
= (inp
->inp_socket
!= NULL
))) {
1219 * Found. Check if pcb is still valid
1221 *uid
= kauth_cred_getuid(
1222 inp
->inp_socket
->so_cred
);
1223 *gid
= kauth_cred_getgid(
1224 inp
->inp_socket
->so_cred
);
1226 lck_rw_done(pcbinfo
->ipi_lock
);
1231 struct inpcb
*local_wild
= NULL
;
1233 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1234 pcbinfo
->ipi_hashmask
)];
1235 LIST_FOREACH(inp
, head
, inp_hash
) {
1236 if (!(inp
->inp_vflag
& INP_IPV6
)) {
1240 if (inp_restricted_recv(inp
, ifp
)) {
1245 if (!necp_socket_is_allowed_to_recv_on_interface(inp
, ifp
)) {
1250 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1251 inp
->inp_lport
== lport
) {
1252 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1254 found
= (inp
->inp_socket
!= NULL
);
1256 *uid
= kauth_cred_getuid(
1257 inp
->inp_socket
->so_cred
);
1258 *gid
= kauth_cred_getgid(
1259 inp
->inp_socket
->so_cred
);
1261 lck_rw_done(pcbinfo
->ipi_lock
);
1263 } else if (IN6_IS_ADDR_UNSPECIFIED(
1264 &inp
->in6p_laddr
)) {
1270 if ((found
= (local_wild
->inp_socket
!= NULL
))) {
1271 *uid
= kauth_cred_getuid(
1272 local_wild
->inp_socket
->so_cred
);
1273 *gid
= kauth_cred_getgid(
1274 local_wild
->inp_socket
->so_cred
);
1276 lck_rw_done(pcbinfo
->ipi_lock
);
1284 lck_rw_done(pcbinfo
->ipi_lock
);
1289 * Lookup PCB in hash list.
1292 in6_pcblookup_hash(struct inpcbinfo
*pcbinfo
, struct in6_addr
*faddr
,
1293 u_int fport_arg
, struct in6_addr
*laddr
, u_int lport_arg
, int wildcard
,
1296 struct inpcbhead
*head
;
1298 u_short fport
= fport_arg
, lport
= lport_arg
;
1300 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
1303 * First look for an exact match.
1305 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1306 lport
, fport
, pcbinfo
->ipi_hashmask
)];
1307 LIST_FOREACH(inp
, head
, inp_hash
) {
1308 if (!(inp
->inp_vflag
& INP_IPV6
)) {
1312 if (inp_restricted_recv(inp
, ifp
)) {
1317 if (!necp_socket_is_allowed_to_recv_on_interface(inp
, ifp
)) {
1322 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1323 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1324 inp
->inp_fport
== fport
&&
1325 inp
->inp_lport
== lport
) {
1327 * Found. Check if pcb is still valid
1329 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) !=
1331 lck_rw_done(pcbinfo
->ipi_lock
);
1334 /* it's there but dead, say it isn't found */
1335 lck_rw_done(pcbinfo
->ipi_lock
);
1341 struct inpcb
*local_wild
= NULL
;
1343 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1344 pcbinfo
->ipi_hashmask
)];
1345 LIST_FOREACH(inp
, head
, inp_hash
) {
1346 if (!(inp
->inp_vflag
& INP_IPV6
)) {
1350 if (inp_restricted_recv(inp
, ifp
)) {
1355 if (!necp_socket_is_allowed_to_recv_on_interface(inp
, ifp
)) {
1360 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1361 inp
->inp_lport
== lport
) {
1362 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1364 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
,
1365 0) != WNT_STOPUSING
) {
1366 lck_rw_done(pcbinfo
->ipi_lock
);
1369 /* dead; say it isn't found */
1370 lck_rw_done(pcbinfo
->ipi_lock
);
1373 } else if (IN6_IS_ADDR_UNSPECIFIED(
1374 &inp
->in6p_laddr
)) {
1379 if (local_wild
&& in_pcb_checkstate(local_wild
,
1380 WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1381 lck_rw_done(pcbinfo
->ipi_lock
);
1384 lck_rw_done(pcbinfo
->ipi_lock
);
1392 lck_rw_done(pcbinfo
->ipi_lock
);
1397 init_sin6(struct sockaddr_in6
*sin6
, struct mbuf
*m
)
1401 ip
= mtod(m
, struct ip6_hdr
*);
1402 bzero(sin6
, sizeof(*sin6
));
1403 sin6
->sin6_len
= sizeof(*sin6
);
1404 sin6
->sin6_family
= AF_INET6
;
1405 sin6
->sin6_addr
= ip
->ip6_src
;
1406 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
)) {
1407 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1408 if ((m
->m_pkthdr
.pkt_flags
& (PKTF_LOOP
| PKTF_IFAINFO
)) ==
1409 (PKTF_LOOP
| PKTF_IFAINFO
)) {
1410 sin6
->sin6_scope_id
= m
->m_pkthdr
.src_ifindex
;
1411 } else if (m
->m_pkthdr
.rcvif
!= NULL
) {
1412 sin6
->sin6_scope_id
= m
->m_pkthdr
.rcvif
->if_index
;
1418 * The following routines implement this scheme:
1420 * Callers of ip6_output() that intend to cache the route in the inpcb pass
1421 * a local copy of the struct route to ip6_output(). Using a local copy of
1422 * the cached route significantly simplifies things as IP no longer has to
1423 * worry about having exclusive access to the passed in struct route, since
1424 * it's defined in the caller's stack; in essence, this allows for a lock-
1425 * less operation when updating the struct route at the IP level and below,
1426 * whenever necessary. The scheme works as follows:
1428 * Prior to dropping the socket's lock and calling ip6_output(), the caller
1429 * copies the struct route from the inpcb into its stack, and adds a reference
1430 * to the cached route entry, if there was any. The socket's lock is then
1431 * dropped and ip6_output() is called with a pointer to the copy of struct
1432 * route defined on the stack (not to the one in the inpcb.)
1434 * Upon returning from ip6_output(), the caller then acquires the socket's
1435 * lock and synchronizes the cache; if there is no route cached in the inpcb,
1436 * it copies the local copy of struct route (which may or may not contain any
1437 * route) back into the cache; otherwise, if the inpcb has a route cached in
1438 * it, the one in the local copy will be freed, if there's any. Trashing the
1439 * cached route in the inpcb can be avoided because ip6_output() is single-
1440 * threaded per-PCB (i.e. multiple transmits on a PCB are always serialized
1441 * by the socket/transport layer.)
1444 in6p_route_copyout(struct inpcb
*inp
, struct route_in6
*dst
)
1446 struct route_in6
*src
= &inp
->in6p_route
;
1448 socket_lock_assert_owned(inp
->inp_socket
);
1450 /* Minor sanity check */
1451 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET6
) {
1452 panic("%s: wrong or corrupted route: %p", __func__
, src
);
1455 route_copyout((struct route
*)dst
, (struct route
*)src
, sizeof(*dst
));
1459 in6p_route_copyin(struct inpcb
*inp
, struct route_in6
*src
)
1461 struct route_in6
*dst
= &inp
->in6p_route
;
1463 socket_lock_assert_owned(inp
->inp_socket
);
1465 /* Minor sanity check */
1466 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET6
) {
1467 panic("%s: wrong or corrupted route: %p", __func__
, src
);
1470 route_copyin((struct route
*)src
, (struct route
*)dst
, sizeof(*src
));