2 * Copyright (c) 2003-2013 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>
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 #include <net/if_var.h>
124 #include <kern/kern_types.h>
125 #include <kern/zalloc.h>
128 #include <netinet6/ipsec.h>
130 #include <netinet6/ipsec6.h>
132 #include <netinet6/ah.h>
134 #include <netinet6/ah6.h>
136 #include <netkey/key.h>
140 * in6_pcblookup_local_and_cleanup does everything
141 * in6_pcblookup_local does but it checks for a socket
142 * that's going away. Since we know that the lock is
143 * held read+write when this function is called, we
144 * can safely dispose of this socket like the slow
145 * timer would usually do and return NULL. This is
148 static struct inpcb
*
149 in6_pcblookup_local_and_cleanup(struct inpcbinfo
*pcbinfo
,
150 struct in6_addr
*laddr
, u_int lport_arg
, int wild_okay
)
154 /* Perform normal lookup */
155 inp
= in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
);
157 /* Check if we found a match but it's waiting to be disposed */
158 if (inp
!= NULL
&& inp
->inp_wantcnt
== WNT_STOPUSING
) {
159 struct socket
*so
= inp
->inp_socket
;
161 lck_mtx_lock(&inp
->inpcb_mtx
);
163 if (so
->so_usecount
== 0) {
164 if (inp
->inp_state
!= INPCB_STATE_DEAD
)
166 in_pcbdispose(inp
); /* will unlock & destroy */
169 lck_mtx_unlock(&inp
->inpcb_mtx
);
177 * Bind an INPCB to an address and/or port. This routine should not alter
178 * the caller-supplied local address "nam".
181 in6_pcbbind(struct inpcb
*inp
, struct sockaddr
*nam
, struct proc
*p
)
183 struct socket
*so
= inp
->inp_socket
;
184 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
186 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
190 if (!in6_ifaddrs
) /* XXX broken! */
191 return (EADDRNOTAVAIL
);
192 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
194 if (!(so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)))
196 socket_unlock(so
, 0); /* keep reference */
197 lck_rw_lock_exclusive(pcbinfo
->ipi_lock
);
199 struct ifnet
*outif
= NULL
;
200 struct sockaddr_in6 sin6
;
202 if (nam
->sa_len
!= sizeof (struct sockaddr_in6
)) {
203 lck_rw_done(pcbinfo
->ipi_lock
);
210 if (nam
->sa_family
!= AF_INET6
) {
211 lck_rw_done(pcbinfo
->ipi_lock
);
213 return (EAFNOSUPPORT
);
215 lport
= SIN6(nam
)->sin6_port
;
217 bzero(&sin6
, sizeof (sin6
));
218 *(&sin6
) = *SIN6(nam
);
220 /* KAME hack: embed scopeid */
221 if (in6_embedscope(&sin6
.sin6_addr
, &sin6
, inp
, NULL
,
223 lck_rw_done(pcbinfo
->ipi_lock
);
228 /* Sanitize local copy for address searches */
229 sin6
.sin6_flowinfo
= 0;
230 sin6
.sin6_scope_id
= 0;
233 if (IN6_IS_ADDR_MULTICAST(&sin6
.sin6_addr
)) {
235 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
236 * allow compepte duplication of binding if
237 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
238 * and a multicast address is bound on both
239 * new and duplicated sockets.
241 if (so
->so_options
& SO_REUSEADDR
)
242 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
243 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
)) {
246 ifa
= ifa_ifwithaddr(SA(&sin6
));
248 lck_rw_done(pcbinfo
->ipi_lock
);
250 return (EADDRNOTAVAIL
);
253 * XXX: bind to an anycast address might
254 * accidentally cause sending a packet with
255 * anycast source address. We should allow
256 * to bind to a deprecated address, since
257 * the application dare to use it.
260 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
261 (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
|
265 lck_rw_done(pcbinfo
->ipi_lock
);
267 return (EADDRNOTAVAIL
);
270 * Opportunistically determine the outbound
271 * interface that may be used; this may not
272 * hold true if we end up using a route
273 * going over a different interface, e.g.
274 * when sending to a local address. This
275 * will get updated again after sending.
277 outif
= ifa
->ifa_ifp
;
287 if (ntohs(lport
) < IPV6PORT_RESERVED
) {
288 cred
= kauth_cred_proc_ref(p
);
289 error
= priv_check_cred(cred
,
290 PRIV_NETINET_RESERVEDPORT
, 0);
291 kauth_cred_unref(&cred
);
293 lck_rw_done(pcbinfo
->ipi_lock
);
298 if (!IN6_IS_ADDR_MULTICAST(&sin6
.sin6_addr
) &&
299 (u
= kauth_cred_getuid(so
->so_cred
)) != 0) {
300 t
= in6_pcblookup_local_and_cleanup(pcbinfo
,
301 &sin6
.sin6_addr
, lport
,
303 if (t
!= NULL
&& (!IN6_IS_ADDR_UNSPECIFIED(
305 !IN6_IS_ADDR_UNSPECIFIED(&t
->in6p_laddr
) ||
306 !(t
->inp_socket
->so_options
&
307 SO_REUSEPORT
)) && (u
!= kauth_cred_getuid(
308 t
->inp_socket
->so_cred
)) &&
309 !(t
->inp_socket
->so_flags
&
310 SOF_REUSESHAREUID
)) {
311 lck_rw_done(pcbinfo
->ipi_lock
);
315 if (!(inp
->inp_flags
& IN6P_IPV6_V6ONLY
) &&
316 IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
)) {
317 struct sockaddr_in sin
;
319 in6_sin6_2_sin(&sin
, &sin6
);
320 t
= in_pcblookup_local_and_cleanup(
321 pcbinfo
, sin
.sin_addr
, lport
,
324 !(t
->inp_socket
->so_options
&
326 (kauth_cred_getuid(so
->so_cred
) !=
327 kauth_cred_getuid(t
->inp_socket
->
328 so_cred
)) && (t
->inp_laddr
.s_addr
!=
329 INADDR_ANY
|| SOCK_DOM(so
) ==
330 SOCK_DOM(t
->inp_socket
))) {
331 lck_rw_done(pcbinfo
->ipi_lock
);
337 t
= in6_pcblookup_local_and_cleanup(pcbinfo
,
338 &sin6
.sin6_addr
, lport
, wild
);
340 (reuseport
& t
->inp_socket
->so_options
) == 0) {
341 lck_rw_done(pcbinfo
->ipi_lock
);
345 if (!(inp
->inp_flags
& IN6P_IPV6_V6ONLY
) &&
346 IN6_IS_ADDR_UNSPECIFIED(&sin6
.sin6_addr
)) {
347 struct sockaddr_in sin
;
349 in6_sin6_2_sin(&sin
, &sin6
);
350 t
= in_pcblookup_local_and_cleanup(pcbinfo
,
351 sin
.sin_addr
, lport
, wild
);
352 if (t
!= NULL
&& (reuseport
&
353 t
->inp_socket
->so_options
) == 0 &&
354 (t
->inp_laddr
.s_addr
!= INADDR_ANY
||
355 SOCK_DOM(so
) == SOCK_DOM(t
->inp_socket
))) {
356 lck_rw_done(pcbinfo
->ipi_lock
);
362 inp
->in6p_laddr
= sin6
.sin6_addr
;
363 inp
->in6p_last_outifp
= outif
;
368 if ((e
= in6_pcbsetport(&inp
->in6p_laddr
, inp
, p
, 1)) != 0) {
369 lck_rw_done(pcbinfo
->ipi_lock
);
373 inp
->inp_lport
= lport
;
374 if (in_pcbinshash(inp
, 1) != 0) {
375 inp
->in6p_laddr
= in6addr_any
;
377 inp
->in6p_last_outifp
= NULL
;
378 lck_rw_done(pcbinfo
->ipi_lock
);
382 lck_rw_done(pcbinfo
->ipi_lock
);
383 sflt_notify(so
, sock_evt_bound
, NULL
);
388 * Transform old in6_pcbconnect() into an inner subroutine for new
389 * in6_pcbconnect(); do some validity-checking on the remote address
390 * (in "nam") and then determine local host address (i.e., which
391 * interface) to use to access that remote host.
393 * This routine may alter the caller-supplied remote address "nam".
395 * This routine might return an ifp with a reference held if the caller
396 * provides a non-NULL outif, even in the error case. The caller is
397 * responsible for releasing its reference.
400 in6_pcbladdr(struct inpcb
*inp
, struct sockaddr
*nam
,
401 struct in6_addr
*plocal_addr6
, struct ifnet
**outif
)
403 struct in6_addr
*addr6
= NULL
;
404 struct in6_addr src_storage
;
406 unsigned int ifscope
;
410 if (nam
->sa_len
!= sizeof (struct sockaddr_in6
))
412 if (SIN6(nam
)->sin6_family
!= AF_INET6
)
413 return (EAFNOSUPPORT
);
414 if (SIN6(nam
)->sin6_port
== 0)
415 return (EADDRNOTAVAIL
);
417 /* KAME hack: embed scopeid */
418 if (in6_embedscope(&SIN6(nam
)->sin6_addr
, SIN6(nam
), inp
, NULL
, NULL
) != 0)
423 * If the destination address is UNSPECIFIED addr,
424 * use the loopback addr, e.g ::1.
426 if (IN6_IS_ADDR_UNSPECIFIED(&SIN6(nam
)->sin6_addr
))
427 SIN6(nam
)->sin6_addr
= in6addr_loopback
;
430 ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
431 inp
->inp_boundifp
->if_index
: IFSCOPE_NONE
;
434 * XXX: in6_selectsrc might replace the bound local address
435 * with the address specified by setsockopt(IPV6_PKTINFO).
436 * Is it the intended behavior?
438 * in6_selectsrc() might return outif with its reference held
439 * even in the error case; caller always needs to release it
442 addr6
= in6_selectsrc(SIN6(nam
), inp
->in6p_outputopts
, inp
,
443 &inp
->in6p_route
, outif
, &src_storage
, ifscope
, &error
);
446 struct rtentry
*rt
= inp
->in6p_route
.ro_rt
;
448 * If in6_selectsrc() returns a route, it should be one
449 * which points to the same ifp as outif. Just in case
450 * it isn't, use the one from the route for consistency.
451 * Otherwise if there is no route, leave outif alone as
452 * it could still be useful to the caller.
454 if (rt
!= NULL
&& rt
->rt_ifp
!= *outif
) {
455 ifnet_reference(rt
->rt_ifp
); /* for caller */
457 ifnet_release(*outif
);
463 if (outif
!= NULL
&& (*outif
) != NULL
&&
464 (inp
->inp_flags
& INP_NO_IFT_CELLULAR
) &&
465 IFNET_IS_CELLULAR(*outif
)) {
466 soevent(inp
->inp_socket
,
467 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_IFDENIED
));
468 error
= EHOSTUNREACH
;
471 error
= EADDRNOTAVAIL
;
475 *plocal_addr6
= *addr6
;
477 * Don't do pcblookup call here; return interface in
478 * plocal_addr6 and exit to caller, that will do the lookup.
485 * Connect from a socket to a specified address.
486 * Both address and port must be specified in argument sin.
487 * If don't have a local address for this socket yet,
491 in6_pcbconnect(struct inpcb
*inp
, struct sockaddr
*nam
, struct proc
*p
)
493 struct in6_addr addr6
;
494 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)nam
;
497 struct ifnet
*outif
= NULL
;
500 * Call inner routine, to assign local interface address.
501 * in6_pcbladdr() may automatically fill in sin6_scope_id.
503 * in6_pcbladdr() might return an ifp with its reference held
504 * even in the error case, so make sure that it's released
505 * whenever it's non-NULL.
507 if ((error
= in6_pcbladdr(inp
, nam
, &addr6
, &outif
)) != 0) {
508 if ((inp
->inp_flags
& INP_NO_IFT_CELLULAR
) && outif
!= NULL
&&
509 IFNET_IS_CELLULAR(outif
))
510 soevent(inp
->inp_socket
,
511 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_IFDENIED
));
514 socket_unlock(inp
->inp_socket
, 0);
515 pcb
= in6_pcblookup_hash(inp
->inp_pcbinfo
, &sin6
->sin6_addr
,
516 sin6
->sin6_port
, IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
) ?
517 &addr6
: &inp
->in6p_laddr
, inp
->inp_lport
, 0, NULL
);
518 socket_lock(inp
->inp_socket
, 0);
520 in_pcb_checkstate(pcb
, WNT_RELEASE
, pcb
== inp
? 1 : 0);
524 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
525 if (inp
->inp_lport
== 0) {
526 error
= in6_pcbbind(inp
, NULL
, p
);
530 inp
->in6p_laddr
= addr6
;
531 inp
->in6p_last_outifp
= outif
; /* no reference needed */
532 inp
->in6p_flags
|= INP_IN6ADDR_ANY
;
534 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
535 /* lock inversion issue, mostly with udp multicast packets */
536 socket_unlock(inp
->inp_socket
, 0);
537 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
538 socket_lock(inp
->inp_socket
, 0);
540 inp
->in6p_faddr
= sin6
->sin6_addr
;
541 inp
->inp_fport
= sin6
->sin6_port
;
544 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
548 ifnet_release(outif
);
554 in6_pcbdisconnect(struct inpcb
*inp
)
556 struct socket
*so
= inp
->inp_socket
;
558 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
559 /* lock inversion issue, mostly with udp multicast packets */
560 socket_unlock(so
, 0);
561 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
564 bzero((caddr_t
)&inp
->in6p_faddr
, sizeof (inp
->in6p_faddr
));
566 /* clear flowinfo - RFC 6437 */
567 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
569 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
571 * A multipath subflow socket would have its SS_NOFDREF set by default,
572 * so check for SOF_MP_SUBFLOW socket flag before detaching the PCB;
573 * when the socket is closed for real, SOF_MP_SUBFLOW would be cleared.
575 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) && (so
->so_state
& SS_NOFDREF
))
580 in6_pcbdetach(struct inpcb
*inp
)
582 struct socket
*so
= inp
->inp_socket
;
584 if (so
->so_pcb
== NULL
) {
585 /* PCB has been disposed */
586 panic("%s: inp=%p so=%p proto=%d so_pcb is null!\n", __func__
,
587 inp
, so
, SOCK_PROTO(so
));
592 if (inp
->in6p_sp
!= NULL
) {
593 (void) ipsec6_delete_pcbpolicy(inp
);
597 /* mark socket state as dead */
598 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
) {
599 panic("%s: so=%p proto=%d couldn't set to STOPUSING\n",
600 __func__
, so
, SOCK_PROTO(so
));
604 if (!(so
->so_flags
& SOF_PCBCLEARING
)) {
605 struct ip_moptions
*imo
;
606 struct ip6_moptions
*im6o
;
609 if (inp
->in6p_options
!= NULL
) {
610 m_freem(inp
->in6p_options
);
611 inp
->in6p_options
= NULL
;
613 ip6_freepcbopts(inp
->in6p_outputopts
);
614 ROUTE_RELEASE(&inp
->in6p_route
);
615 /* free IPv4 related resources in case of mapped addr */
616 if (inp
->inp_options
!= NULL
) {
617 (void) m_free(inp
->inp_options
);
618 inp
->inp_options
= NULL
;
620 im6o
= inp
->in6p_moptions
;
621 inp
->in6p_moptions
= NULL
;
625 imo
= inp
->inp_moptions
;
626 inp
->inp_moptions
= NULL
;
629 sofreelastref(so
, 0);
630 inp
->inp_state
= INPCB_STATE_DEAD
;
631 /* makes sure we're not called twice from so_close */
632 so
->so_flags
|= SOF_PCBCLEARING
;
634 inpcb_gc_sched(inp
->inp_pcbinfo
, INPCB_TIMER_FAST
);
639 in6_sockaddr(in_port_t port
, struct in6_addr
*addr_p
)
641 struct sockaddr_in6
*sin6
;
643 MALLOC(sin6
, struct sockaddr_in6
*, sizeof (*sin6
), M_SONAME
, M_WAITOK
);
646 bzero(sin6
, sizeof (*sin6
));
647 sin6
->sin6_family
= AF_INET6
;
648 sin6
->sin6_len
= sizeof (*sin6
);
649 sin6
->sin6_port
= port
;
650 sin6
->sin6_addr
= *addr_p
;
652 /* would be good to use sa6_recoverscope(), except for locking */
653 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
654 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
656 sin6
->sin6_scope_id
= 0; /* XXX */
657 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
658 sin6
->sin6_addr
.s6_addr16
[1] = 0;
660 return ((struct sockaddr
*)sin6
);
664 in6_sockaddr_s(in_port_t port
, struct in6_addr
*addr_p
,
665 struct sockaddr_in6
*sin6
)
667 bzero(sin6
, sizeof (*sin6
));
668 sin6
->sin6_family
= AF_INET6
;
669 sin6
->sin6_len
= sizeof (*sin6
);
670 sin6
->sin6_port
= port
;
671 sin6
->sin6_addr
= *addr_p
;
673 /* would be good to use sa6_recoverscope(), except for locking */
674 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
675 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
677 sin6
->sin6_scope_id
= 0; /* XXX */
678 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
679 sin6
->sin6_addr
.s6_addr16
[1] = 0;
683 * The calling convention of in6_getsockaddr() and in6_getpeeraddr() was
684 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
685 * in struct pr_usrreqs, so that protocols can just reference then directly
686 * without the need for a wrapper function.
689 in6_getsockaddr(struct socket
*so
, struct sockaddr
**nam
)
692 struct in6_addr addr
;
695 if ((inp
= sotoinpcb(so
)) == NULL
)
698 port
= inp
->inp_lport
;
699 addr
= inp
->in6p_laddr
;
701 *nam
= in6_sockaddr(port
, &addr
);
708 in6_getsockaddr_s(struct socket
*so
, struct sockaddr_storage
*ss
)
711 struct in6_addr addr
;
715 bzero(ss
, sizeof (*ss
));
717 if ((inp
= sotoinpcb(so
)) == NULL
||
718 (inp
->inp_flags2
& INP2_WANT_FLOW_DIVERT
))
719 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
721 port
= inp
->inp_lport
;
722 addr
= inp
->in6p_laddr
;
724 in6_sockaddr_s(port
, &addr
, SIN6(ss
));
729 in6_getpeeraddr(struct socket
*so
, struct sockaddr
**nam
)
732 struct in6_addr addr
;
735 if ((inp
= sotoinpcb(so
)) == NULL
)
738 port
= inp
->inp_fport
;
739 addr
= inp
->in6p_faddr
;
741 *nam
= in6_sockaddr(port
, &addr
);
748 in6_getpeeraddr_s(struct socket
*so
, struct sockaddr_storage
*ss
)
751 struct in6_addr addr
;
755 bzero(ss
, sizeof (*ss
));
757 if ((inp
= sotoinpcb(so
)) == NULL
||
758 (inp
->inp_flags2
& INP2_WANT_FLOW_DIVERT
))
759 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
761 port
= inp
->inp_fport
;
762 addr
= inp
->in6p_faddr
;
764 in6_sockaddr_s(port
, &addr
, SIN6(ss
));
769 in6_mapped_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
771 struct inpcb
*inp
= sotoinpcb(so
);
776 if (inp
->inp_vflag
& INP_IPV4
) {
777 error
= in_getsockaddr(so
, nam
);
779 error
= in6_sin_2_v4mapsin6_in_sock(nam
);
781 /* scope issues will be handled in in6_getsockaddr(). */
782 error
= in6_getsockaddr(so
, nam
);
788 in6_mapped_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
790 struct inpcb
*inp
= sotoinpcb(so
);
795 if (inp
->inp_vflag
& INP_IPV4
) {
796 error
= in_getpeeraddr(so
, nam
);
798 error
= in6_sin_2_v4mapsin6_in_sock(nam
);
800 /* scope issues will be handled in in6_getpeeraddr(). */
801 error
= in6_getpeeraddr(so
, nam
);
807 * Pass some notification to all connections of a protocol
808 * associated with address dst. The local address and/or port numbers
809 * may be specified to limit the search. The "usual action" will be
810 * taken, depending on the ctlinput cmd. The caller must filter any
811 * cmds that are uninteresting (e.g., no error in the map).
812 * Call the protocol specific routine (if any) to report
813 * any errors for each matching socket.
816 in6_pcbnotify(struct inpcbinfo
*pcbinfo
, struct sockaddr
*dst
, u_int fport_arg
,
817 const struct sockaddr
*src
, u_int lport_arg
, int cmd
, void *cmdarg
,
818 void (*notify
)(struct inpcb
*, int))
820 struct inpcbhead
*head
= pcbinfo
->ipi_listhead
;
821 struct inpcb
*inp
, *ninp
;
822 struct sockaddr_in6 sa6_src
, *sa6_dst
;
823 u_short fport
= fport_arg
, lport
= lport_arg
;
827 if ((unsigned)cmd
> PRC_NCMDS
|| dst
->sa_family
!= AF_INET6
)
830 sa6_dst
= (struct sockaddr_in6
*)(void *)dst
;
831 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst
->sin6_addr
))
835 * note that src can be NULL when we get notify by local fragmentation.
837 sa6_src
= (src
== NULL
) ?
838 sa6_any
: *(struct sockaddr_in6
*)(uintptr_t)(size_t)src
;
839 flowinfo
= sa6_src
.sin6_flowinfo
;
842 * Redirects go to all references to the destination,
843 * and use in6_rtchange to invalidate the route cache.
844 * Dead host indications: also use in6_rtchange to invalidate
845 * the cache, and deliver the error to all the sockets.
846 * Otherwise, if we have knowledge of the local port and address,
847 * deliver only to that socket.
849 if (PRC_IS_REDIRECT(cmd
) || cmd
== PRC_HOSTDEAD
) {
852 bzero((caddr_t
)&sa6_src
.sin6_addr
, sizeof (sa6_src
.sin6_addr
));
854 if (cmd
!= PRC_HOSTDEAD
)
855 notify
= in6_rtchange
;
857 errno
= inet6ctlerrmap
[cmd
];
858 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
859 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
860 ninp
= LIST_NEXT(inp
, inp_list
);
862 if (!(inp
->inp_vflag
& INP_IPV6
))
866 * If the error designates a new path MTU for a destination
867 * and the application (associated with this socket) wanted to
868 * know the value, notify. Note that we notify for all
869 * disconnected sockets if the corresponding application
870 * wanted. This is because some UDP applications keep sending
871 * sockets disconnected.
872 * XXX: should we avoid to notify the value to TCP sockets?
874 if (cmd
== PRC_MSGSIZE
&& (inp
->inp_flags
& IN6P_MTU
) != 0 &&
875 (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) ||
876 IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
,
877 &sa6_dst
->sin6_addr
))) {
878 ip6_notify_pmtu(inp
, (struct sockaddr_in6
*)(void *)dst
,
879 (u_int32_t
*)cmdarg
);
883 * Detect if we should notify the error. If no source and
884 * destination ports are specifed, but non-zero flowinfo and
885 * local address match, notify the error. This is the case
886 * when the error is delivered with an encrypted buffer
887 * by ESP. Otherwise, just compare addresses and ports
890 if (lport
== 0 && fport
== 0 && flowinfo
&&
891 inp
->inp_socket
!= NULL
&&
892 flowinfo
== (inp
->inp_flow
& IPV6_FLOWLABEL_MASK
) &&
893 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &sa6_src
.sin6_addr
))
895 else if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
,
896 &sa6_dst
->sin6_addr
) || inp
->inp_socket
== NULL
||
897 (lport
&& inp
->inp_lport
!= lport
) ||
898 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src
.sin6_addr
) &&
899 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
900 &sa6_src
.sin6_addr
)) || (fport
&& inp
->inp_fport
!= fport
))
905 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) ==
908 socket_lock(inp
->inp_socket
, 1);
909 (*notify
)(inp
, errno
);
910 (void) in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
911 socket_unlock(inp
->inp_socket
, 1);
914 lck_rw_done(pcbinfo
->ipi_lock
);
918 * Lookup a PCB based on the local address and port.
921 in6_pcblookup_local(struct inpcbinfo
*pcbinfo
, struct in6_addr
*laddr
,
922 u_int lport_arg
, int wild_okay
)
925 int matchwild
= 3, wildcard
;
926 u_short lport
= lport_arg
;
927 struct inpcbporthead
*porthash
;
928 struct inpcb
*match
= NULL
;
929 struct inpcbport
*phd
;
932 struct inpcbhead
*head
;
934 * Look for an unconnected (wildcard foreign addr) PCB that
935 * matches the local address and port we're looking for.
937 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
938 pcbinfo
->ipi_hashmask
)];
939 LIST_FOREACH(inp
, head
, inp_hash
) {
940 if (!(inp
->inp_vflag
& INP_IPV6
))
942 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
943 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
944 inp
->inp_lport
== lport
) {
957 * Best fit PCB lookup.
959 * First see if this local port is in use by looking on the
962 porthash
= &pcbinfo
->ipi_porthashbase
[INP_PCBPORTHASH(lport
,
963 pcbinfo
->ipi_porthashmask
)];
964 LIST_FOREACH(phd
, porthash
, phd_hash
) {
965 if (phd
->phd_port
== lport
)
970 * Port is in use by one or more PCBs. Look for best
973 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
975 if (!(inp
->inp_vflag
& INP_IPV6
))
977 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
979 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
980 if (IN6_IS_ADDR_UNSPECIFIED(laddr
))
982 else if (!IN6_ARE_ADDR_EQUAL(
983 &inp
->in6p_laddr
, laddr
))
986 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
))
989 if (wildcard
< matchwild
) {
991 matchwild
= wildcard
;
992 if (matchwild
== 0) {
1002 * Check for alternatives when higher level complains
1003 * about service problems. For now, invalidate cached
1004 * routing information. If the route was created dynamically
1005 * (by a redirect), time to try a default gateway again.
1008 in6_losing(struct inpcb
*in6p
)
1011 struct rt_addrinfo info
;
1013 if ((rt
= in6p
->in6p_route
.ro_rt
) != NULL
) {
1015 bzero((caddr_t
)&info
, sizeof (info
));
1016 info
.rti_info
[RTAX_DST
] =
1017 (struct sockaddr
*)&in6p
->in6p_route
.ro_dst
;
1018 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1019 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1020 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
1021 if (rt
->rt_flags
& RTF_DYNAMIC
) {
1023 * Prevent another thread from modifying rt_key,
1024 * rt_gateway via rt_setgate() after the rt_lock
1025 * is dropped by marking the route as defunct.
1027 rt
->rt_flags
|= RTF_CONDEMNED
;
1029 (void) rtrequest(RTM_DELETE
, rt_key(rt
),
1030 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
, NULL
);
1035 * A new route can be allocated
1036 * the next time output is attempted.
1039 ROUTE_RELEASE(&in6p
->in6p_route
);
1043 * After a routing change, flush old routing
1044 * and allocate a (hopefully) better one.
1047 in6_rtchange(struct inpcb
*inp
, int errno
)
1049 #pragma unused(errno)
1051 * A new route can be allocated the next time
1052 * output is attempted.
1054 ROUTE_RELEASE(&inp
->in6p_route
);
1058 * Check if PCB exists hash list. Also returns uid and gid of socket
1061 in6_pcblookup_hash_exists(struct inpcbinfo
*pcbinfo
, struct in6_addr
*faddr
,
1062 u_int fport_arg
, struct in6_addr
*laddr
, u_int lport_arg
, int wildcard
,
1063 uid_t
*uid
, gid_t
*gid
, struct ifnet
*ifp
)
1065 struct inpcbhead
*head
;
1067 u_short fport
= fport_arg
, lport
= lport_arg
;
1073 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
1076 * First look for an exact match.
1078 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1079 lport
, fport
, pcbinfo
->ipi_hashmask
)];
1080 LIST_FOREACH(inp
, head
, inp_hash
) {
1081 if (!(inp
->inp_vflag
& INP_IPV6
))
1084 if (inp_restricted(inp
, ifp
))
1087 if (ifp
!= NULL
&& IFNET_IS_CELLULAR(ifp
) &&
1088 (inp
->in6p_flags
& INP_NO_IFT_CELLULAR
))
1091 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1092 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1093 inp
->inp_fport
== fport
&&
1094 inp
->inp_lport
== lport
) {
1095 if ((found
= (inp
->inp_socket
!= NULL
))) {
1097 * Found. Check if pcb is still valid
1099 *uid
= kauth_cred_getuid(
1100 inp
->inp_socket
->so_cred
);
1101 *gid
= kauth_cred_getgid(
1102 inp
->inp_socket
->so_cred
);
1104 lck_rw_done(pcbinfo
->ipi_lock
);
1109 struct inpcb
*local_wild
= NULL
;
1111 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1112 pcbinfo
->ipi_hashmask
)];
1113 LIST_FOREACH(inp
, head
, inp_hash
) {
1114 if (!(inp
->inp_vflag
& INP_IPV6
))
1117 if (inp_restricted(inp
, ifp
))
1120 if (ifp
!= NULL
&& IFNET_IS_CELLULAR(ifp
) &&
1121 (inp
->in6p_flags
& INP_NO_IFT_CELLULAR
))
1124 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1125 inp
->inp_lport
== lport
) {
1126 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1128 found
= (inp
->inp_socket
!= NULL
);
1130 *uid
= kauth_cred_getuid(
1131 inp
->inp_socket
->so_cred
);
1132 *gid
= kauth_cred_getgid(
1133 inp
->inp_socket
->so_cred
);
1135 lck_rw_done(pcbinfo
->ipi_lock
);
1137 } else if (IN6_IS_ADDR_UNSPECIFIED(
1138 &inp
->in6p_laddr
)) {
1144 if ((found
= (local_wild
->inp_socket
!= NULL
))) {
1145 *uid
= kauth_cred_getuid(
1146 local_wild
->inp_socket
->so_cred
);
1147 *gid
= kauth_cred_getgid(
1148 local_wild
->inp_socket
->so_cred
);
1150 lck_rw_done(pcbinfo
->ipi_lock
);
1158 lck_rw_done(pcbinfo
->ipi_lock
);
1163 * Lookup PCB in hash list.
1166 in6_pcblookup_hash(struct inpcbinfo
*pcbinfo
, struct in6_addr
*faddr
,
1167 u_int fport_arg
, struct in6_addr
*laddr
, u_int lport_arg
, int wildcard
,
1170 struct inpcbhead
*head
;
1172 u_short fport
= fport_arg
, lport
= lport_arg
;
1174 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
1177 * First look for an exact match.
1179 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1180 lport
, fport
, pcbinfo
->ipi_hashmask
)];
1181 LIST_FOREACH(inp
, head
, inp_hash
) {
1182 if (!(inp
->inp_vflag
& INP_IPV6
))
1185 if (inp_restricted(inp
, ifp
))
1188 if (ifp
!= NULL
&& IFNET_IS_CELLULAR(ifp
) &&
1189 (inp
->in6p_flags
& INP_NO_IFT_CELLULAR
))
1192 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1193 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1194 inp
->inp_fport
== fport
&&
1195 inp
->inp_lport
== lport
) {
1197 * Found. Check if pcb is still valid
1199 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) !=
1201 lck_rw_done(pcbinfo
->ipi_lock
);
1204 /* it's there but dead, say it isn't found */
1205 lck_rw_done(pcbinfo
->ipi_lock
);
1211 struct inpcb
*local_wild
= NULL
;
1213 head
= &pcbinfo
->ipi_hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1214 pcbinfo
->ipi_hashmask
)];
1215 LIST_FOREACH(inp
, head
, inp_hash
) {
1216 if (!(inp
->inp_vflag
& INP_IPV6
))
1219 if (inp_restricted(inp
, ifp
))
1222 if (ifp
!= NULL
&& IFNET_IS_CELLULAR(ifp
) &&
1223 (inp
->in6p_flags
& INP_NO_IFT_CELLULAR
))
1226 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1227 inp
->inp_lport
== lport
) {
1228 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1230 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
,
1231 0) != WNT_STOPUSING
) {
1232 lck_rw_done(pcbinfo
->ipi_lock
);
1235 /* dead; say it isn't found */
1236 lck_rw_done(pcbinfo
->ipi_lock
);
1239 } else if (IN6_IS_ADDR_UNSPECIFIED(
1240 &inp
->in6p_laddr
)) {
1245 if (local_wild
&& in_pcb_checkstate(local_wild
,
1246 WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1247 lck_rw_done(pcbinfo
->ipi_lock
);
1248 return (local_wild
);
1250 lck_rw_done(pcbinfo
->ipi_lock
);
1258 lck_rw_done(pcbinfo
->ipi_lock
);
1263 init_sin6(struct sockaddr_in6
*sin6
, struct mbuf
*m
)
1267 ip
= mtod(m
, struct ip6_hdr
*);
1268 bzero(sin6
, sizeof (*sin6
));
1269 sin6
->sin6_len
= sizeof (*sin6
);
1270 sin6
->sin6_family
= AF_INET6
;
1271 sin6
->sin6_addr
= ip
->ip6_src
;
1272 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
)) {
1273 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1274 if ((m
->m_pkthdr
.pkt_flags
& (PKTF_LOOP
|PKTF_IFAINFO
)) ==
1275 (PKTF_LOOP
|PKTF_IFAINFO
))
1276 sin6
->sin6_scope_id
= m
->m_pkthdr
.src_ifindex
;
1277 else if (m
->m_pkthdr
.rcvif
!= NULL
)
1278 sin6
->sin6_scope_id
= m
->m_pkthdr
.rcvif
->if_index
;
1283 * The following routines implement this scheme:
1285 * Callers of ip6_output() that intend to cache the route in the inpcb pass
1286 * a local copy of the struct route to ip6_output(). Using a local copy of
1287 * the cached route significantly simplifies things as IP no longer has to
1288 * worry about having exclusive access to the passed in struct route, since
1289 * it's defined in the caller's stack; in essence, this allows for a lock-
1290 * less operation when updating the struct route at the IP level and below,
1291 * whenever necessary. The scheme works as follows:
1293 * Prior to dropping the socket's lock and calling ip6_output(), the caller
1294 * copies the struct route from the inpcb into its stack, and adds a reference
1295 * to the cached route entry, if there was any. The socket's lock is then
1296 * dropped and ip6_output() is called with a pointer to the copy of struct
1297 * route defined on the stack (not to the one in the inpcb.)
1299 * Upon returning from ip6_output(), the caller then acquires the socket's
1300 * lock and synchronizes the cache; if there is no route cached in the inpcb,
1301 * it copies the local copy of struct route (which may or may not contain any
1302 * route) back into the cache; otherwise, if the inpcb has a route cached in
1303 * it, the one in the local copy will be freed, if there's any. Trashing the
1304 * cached route in the inpcb can be avoided because ip6_output() is single-
1305 * threaded per-PCB (i.e. multiple transmits on a PCB are always serialized
1306 * by the socket/transport layer.)
1309 in6p_route_copyout(struct inpcb
*inp
, struct route_in6
*dst
)
1311 struct route_in6
*src
= &inp
->in6p_route
;
1313 lck_mtx_assert(&inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1315 /* Minor sanity check */
1316 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET6
)
1317 panic("%s: wrong or corrupted route: %p", __func__
, src
);
1319 route_copyout((struct route
*)dst
, (struct route
*)src
, sizeof (*dst
));
1323 in6p_route_copyin(struct inpcb
*inp
, struct route_in6
*src
)
1325 struct route_in6
*dst
= &inp
->in6p_route
;
1327 lck_mtx_assert(&inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1329 /* Minor sanity check */
1330 if (src
->ro_rt
!= NULL
&& rt_key(src
->ro_rt
)->sa_family
!= AF_INET6
)
1331 panic("%s: wrong or corrupted route: %p", __func__
, src
);
1333 route_copyin((struct route
*)src
, (struct route
*)dst
, sizeof (*src
));