2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1991, 1993, 1995
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
63 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <libkern/OSAtomic.h>
82 #include <machine/limits.h>
85 #include <kern/zalloc.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip_var.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
104 #include <netinet6/ipsec.h>
105 #include <netkey/key.h>
108 #include <sys/kdebug.h>
111 extern int ipsec_bypass
;
112 extern lck_mtx_t
*sadb_mutex
;
115 extern u_long route_generation
;
117 #define DBG_FNC_PCB_LOOKUP NETDBG_CODE(DBG_NETTCP, (6 << 8))
118 #define DBG_FNC_PCB_HLOOKUP NETDBG_CODE(DBG_NETTCP, ((6 << 8) | 1))
120 struct in_addr zeroin_addr
;
123 * These configure the range of local port addresses assigned to
124 * "unspecified" outgoing connections/packets/whatever.
126 int ipport_lowfirstauto
= IPPORT_RESERVED
- 1; /* 1023 */
127 int ipport_lowlastauto
= IPPORT_RESERVEDSTART
; /* 600 */
129 int ipport_firstauto
= IPPORT_RESERVED
; /* 1024 */
130 int ipport_lastauto
= IPPORT_USERRESERVED
; /* 5000 */
132 int ipport_firstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
133 int ipport_lastauto
= IPPORT_HILASTAUTO
; /* 65535 */
135 int ipport_hifirstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
136 int ipport_hilastauto
= IPPORT_HILASTAUTO
; /* 65535 */
138 #define RANGECHK(var, min, max) \
139 if ((var) < (min)) { (var) = (min); } \
140 else if ((var) > (max)) { (var) = (max); }
143 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
145 int error
= sysctl_handle_int(oidp
,
146 oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
148 RANGECHK(ipport_lowfirstauto
, 1, IPPORT_RESERVED
- 1);
149 RANGECHK(ipport_lowlastauto
, 1, IPPORT_RESERVED
- 1);
150 RANGECHK(ipport_firstauto
, IPPORT_RESERVED
, USHRT_MAX
);
151 RANGECHK(ipport_lastauto
, IPPORT_RESERVED
, USHRT_MAX
);
152 RANGECHK(ipport_hifirstauto
, IPPORT_RESERVED
, USHRT_MAX
);
153 RANGECHK(ipport_hilastauto
, IPPORT_RESERVED
, USHRT_MAX
);
160 SYSCTL_NODE(_net_inet_ip
, IPPROTO_IP
, portrange
, CTLFLAG_RW
, 0, "IP Ports");
162 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowfirst
, CTLTYPE_INT
|CTLFLAG_RW
,
163 &ipport_lowfirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
164 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowlast
, CTLTYPE_INT
|CTLFLAG_RW
,
165 &ipport_lowlastauto
, 0, &sysctl_net_ipport_check
, "I", "");
166 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, first
, CTLTYPE_INT
|CTLFLAG_RW
,
167 &ipport_firstauto
, 0, &sysctl_net_ipport_check
, "I", "");
168 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, last
, CTLTYPE_INT
|CTLFLAG_RW
,
169 &ipport_lastauto
, 0, &sysctl_net_ipport_check
, "I", "");
170 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hifirst
, CTLTYPE_INT
|CTLFLAG_RW
,
171 &ipport_hifirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
172 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hilast
, CTLTYPE_INT
|CTLFLAG_RW
,
173 &ipport_hilastauto
, 0, &sysctl_net_ipport_check
, "I", "");
176 * in_pcb.c: manage the Protocol Control Blocks.
178 * NOTE: It is assumed that most of these functions will be called at
179 * splnet(). XXX - There are, unfortunately, a few exceptions to this
180 * rule that should be fixed.
184 * Allocate a PCB and associate it with the socket.
187 in_pcballoc(so
, pcbinfo
, p
)
189 struct inpcbinfo
*pcbinfo
;
192 register struct inpcb
*inp
;
200 if (so
->cached_in_sock_layer
== 0) {
202 printf("PCBALLOC calling zalloc for socket %x\n", so
);
204 inp
= (struct inpcb
*) zalloc(pcbinfo
->ipi_zone
);
207 bzero((caddr_t
)inp
, sizeof(*inp
));
211 printf("PCBALLOC reusing PCB for socket %x\n", so
);
213 inp
= (struct inpcb
*) so
->so_saved_pcb
;
214 temp
= inp
->inp_saved_ppcb
;
215 bzero((caddr_t
) inp
, sizeof(*inp
));
216 inp
->inp_saved_ppcb
= temp
;
219 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
220 inp
->inp_pcbinfo
= pcbinfo
;
221 inp
->inp_socket
= so
;
222 so
->so_pcb
= (caddr_t
)inp
;
224 if (so
->so_proto
->pr_flags
& PR_PCBLOCK
) {
225 inp
->inpcb_mtx
= lck_mtx_alloc_init(pcbinfo
->mtx_grp
, pcbinfo
->mtx_attr
);
226 if (inp
->inpcb_mtx
== NULL
) {
227 printf("in_pcballoc: can't alloc mutex! so=%x\n", so
);
234 if (ipsec_bypass
== 0) {
235 lck_mtx_lock(sadb_mutex
);
236 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
237 lck_mtx_unlock(sadb_mutex
);
239 zfree(pcbinfo
->ipi_zone
, inp
);
246 if (INP_SOCKAF(so
) == AF_INET6
&& !ip6_mapped_addr_on
)
247 inp
->inp_flags
|= IN6P_IPV6_V6ONLY
;
251 if (ip6_auto_flowlabel
)
252 inp
->inp_flags
|= IN6P_AUTOFLOWLABEL
;
254 lck_rw_lock_exclusive(pcbinfo
->mtx
);
255 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
256 LIST_INSERT_HEAD(pcbinfo
->listhead
, inp
, inp_list
);
257 pcbinfo
->ipi_count
++;
258 lck_rw_done(pcbinfo
->mtx
);
263 in_pcbbind(inp
, nam
, p
)
264 register struct inpcb
*inp
;
265 struct sockaddr
*nam
;
268 register struct socket
*so
= inp
->inp_socket
;
269 unsigned short *lastport
;
270 struct sockaddr_in
*sin
;
271 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
273 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
276 if (TAILQ_EMPTY(&in_ifaddrhead
)) /* XXX broken! */
277 return (EADDRNOTAVAIL
);
278 if (inp
->inp_lport
|| inp
->inp_laddr
.s_addr
!= INADDR_ANY
)
280 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
282 socket_unlock(so
, 0); /* keep reference on socket */
283 lck_rw_lock_exclusive(pcbinfo
->mtx
);
285 sin
= (struct sockaddr_in
*)nam
;
286 if (nam
->sa_len
!= sizeof (*sin
)) {
287 lck_rw_done(pcbinfo
->mtx
);
293 * We should check the family, but old programs
294 * incorrectly fail to initialize it.
296 if (sin
->sin_family
!= AF_INET
) {
297 lck_rw_done(pcbinfo
->mtx
);
299 return (EAFNOSUPPORT
);
302 lport
= sin
->sin_port
;
303 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
305 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
306 * allow complete duplication of binding if
307 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
308 * and a multicast address is bound on both
309 * new and duplicated sockets.
311 if (so
->so_options
& SO_REUSEADDR
)
312 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
313 } else if (sin
->sin_addr
.s_addr
!= INADDR_ANY
) {
315 sin
->sin_port
= 0; /* yech... */
316 if ((ifa
= ifa_ifwithaddr((struct sockaddr
*)sin
)) == 0) {
317 lck_rw_done(pcbinfo
->mtx
);
319 return (EADDRNOTAVAIL
);
329 if (ntohs(lport
) < IPPORT_RESERVED
&& p
&&
331 lck_rw_done(pcbinfo
->mtx
);
336 !IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
337 t
= in_pcblookup_local(inp
->inp_pcbinfo
,
338 sin
->sin_addr
, lport
, INPLOOKUP_WILDCARD
);
340 (ntohl(sin
->sin_addr
.s_addr
) != INADDR_ANY
||
341 ntohl(t
->inp_laddr
.s_addr
) != INADDR_ANY
||
342 (t
->inp_socket
->so_options
&
343 SO_REUSEPORT
) == 0) &&
344 (so
->so_uid
!= t
->inp_socket
->so_uid
)) {
346 if (ntohl(sin
->sin_addr
.s_addr
) !=
348 ntohl(t
->inp_laddr
.s_addr
) !=
351 INP_SOCKAF(t
->inp_socket
)) {
352 #endif /* defined(INET6) */
353 lck_rw_done(pcbinfo
->mtx
);
359 t
= in_pcblookup_local(pcbinfo
, sin
->sin_addr
,
362 (reuseport
& t
->inp_socket
->so_options
) == 0) {
364 if (ip6_mapped_addr_on
== 0 ||
365 ntohl(sin
->sin_addr
.s_addr
) !=
367 ntohl(t
->inp_laddr
.s_addr
) !=
370 INP_SOCKAF(t
->inp_socket
)) {
371 #endif /* defined(INET6) */
372 lck_rw_done(pcbinfo
->mtx
);
378 inp
->inp_laddr
= sin
->sin_addr
;
384 inp
->inp_flags
|= INP_ANONPORT
;
386 if (inp
->inp_flags
& INP_HIGHPORT
) {
387 first
= ipport_hifirstauto
; /* sysctl */
388 last
= ipport_hilastauto
;
389 lastport
= &pcbinfo
->lasthi
;
390 } else if (inp
->inp_flags
& INP_LOWPORT
) {
391 if (p
&& (error
= proc_suser(p
))) {
392 lck_rw_done(pcbinfo
->mtx
);
396 first
= ipport_lowfirstauto
; /* 1023 */
397 last
= ipport_lowlastauto
; /* 600 */
398 lastport
= &pcbinfo
->lastlow
;
400 first
= ipport_firstauto
; /* sysctl */
401 last
= ipport_lastauto
;
402 lastport
= &pcbinfo
->lastport
;
405 * Simple check to ensure all ports are not used up causing
408 * We split the two cases (up and down) so that the direction
409 * is not being tested on each round of the loop.
415 count
= first
- last
;
418 if (count
-- < 0) { /* completely used? */
419 lck_rw_done(pcbinfo
->mtx
);
421 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
422 return (EADDRNOTAVAIL
);
425 if (*lastport
> first
|| *lastport
< last
)
427 lport
= htons(*lastport
);
428 } while (in_pcblookup_local(pcbinfo
,
429 inp
->inp_laddr
, lport
, wild
));
434 count
= last
- first
;
437 if (count
-- < 0) { /* completely used? */
438 lck_rw_done(pcbinfo
->mtx
);
440 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
441 return (EADDRNOTAVAIL
);
444 if (*lastport
< first
|| *lastport
> last
)
446 lport
= htons(*lastport
);
447 } while (in_pcblookup_local(pcbinfo
,
448 inp
->inp_laddr
, lport
, wild
));
452 inp
->inp_lport
= lport
;
453 if (in_pcbinshash(inp
, 1) != 0) {
454 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
456 lck_rw_done(pcbinfo
->mtx
);
459 lck_rw_done(pcbinfo
->mtx
);
464 * Transform old in_pcbconnect() into an inner subroutine for new
465 * in_pcbconnect(): Do some validity-checking on the remote
466 * address (in mbuf 'nam') and then determine local host address
467 * (i.e., which interface) to use to access that remote host.
469 * This preserves definition of in_pcbconnect(), while supporting a
470 * slightly different version for T/TCP. (This is more than
471 * a bit of a kludge, but cleaning up the internal interfaces would
472 * have forced minor changes in every protocol).
476 in_pcbladdr(inp
, nam
, plocal_sin
)
477 register struct inpcb
*inp
;
478 struct sockaddr
*nam
;
479 struct sockaddr_in
**plocal_sin
;
481 struct in_ifaddr
*ia
;
482 register struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
484 if (nam
->sa_len
!= sizeof (*sin
))
486 if (sin
->sin_family
!= AF_INET
)
487 return (EAFNOSUPPORT
);
488 if (sin
->sin_port
== 0)
489 return (EADDRNOTAVAIL
);
490 lck_mtx_lock(rt_mtx
);
491 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
493 * If the destination address is INADDR_ANY,
494 * use the primary local address.
495 * If the supplied address is INADDR_BROADCAST,
496 * and the primary interface supports broadcast,
497 * choose the broadcast address for that interface.
499 #define satosin(sa) ((struct sockaddr_in *)(sa))
500 #define sintosa(sin) ((struct sockaddr *)(sin))
501 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
502 if (sin
->sin_addr
.s_addr
== INADDR_ANY
)
503 sin
->sin_addr
= IA_SIN(TAILQ_FIRST(&in_ifaddrhead
))->sin_addr
;
504 else if (sin
->sin_addr
.s_addr
== (u_long
)INADDR_BROADCAST
&&
505 (TAILQ_FIRST(&in_ifaddrhead
)->ia_ifp
->if_flags
& IFF_BROADCAST
))
506 sin
->sin_addr
= satosin(&TAILQ_FIRST(&in_ifaddrhead
)->ia_broadaddr
)->sin_addr
;
508 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
509 register struct route
*ro
;
511 ia
= (struct in_ifaddr
*)0;
513 * If route is known or can be allocated now,
514 * our src addr is taken from the i/f, else punt.
515 * Note that we should check the address family of the cached
516 * destination, in case of sharing the cache with IPv6.
518 ro
= &inp
->inp_route
;
520 (ro
->ro_dst
.sa_family
!= AF_INET
||
521 satosin(&ro
->ro_dst
)->sin_addr
.s_addr
!=
522 sin
->sin_addr
.s_addr
||
523 inp
->inp_socket
->so_options
& SO_DONTROUTE
||
524 ro
->ro_rt
->generation_id
!= route_generation
)) {
525 rtfree_locked(ro
->ro_rt
);
526 ro
->ro_rt
= (struct rtentry
*)0;
528 if ((inp
->inp_socket
->so_options
& SO_DONTROUTE
) == 0 && /*XXX*/
529 (ro
->ro_rt
== (struct rtentry
*)0 ||
530 ro
->ro_rt
->rt_ifp
== 0)) {
531 /* No route yet, so try to acquire one */
532 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in
));
533 ro
->ro_dst
.sa_family
= AF_INET
;
534 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
535 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
537 rtalloc_ign_locked(ro
, 0UL);
540 * If we found a route, use the address
541 * corresponding to the outgoing interface
542 * unless it is the loopback (in case a route
543 * to our address on another net goes to loopback).
545 if (ro
->ro_rt
&& !(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
546 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
551 u_short fport
= sin
->sin_port
;
554 ia
= ifatoia(ifa_ifwithdstaddr(sintosa(sin
)));
556 ia
= ifatoia(ifa_ifwithnet(sintosa(sin
)));
558 sin
->sin_port
= fport
;
560 ia
= TAILQ_FIRST(&in_ifaddrhead
);
565 lck_mtx_unlock(rt_mtx
);
566 return (EADDRNOTAVAIL
);
570 * If the destination address is multicast and an outgoing
571 * interface has been set as a multicast option, use the
572 * address of that interface as our source address.
574 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)) &&
575 inp
->inp_moptions
!= NULL
) {
576 struct ip_moptions
*imo
;
579 imo
= inp
->inp_moptions
;
580 if (imo
->imo_multicast_ifp
!= NULL
&& (ia
== NULL
||
581 ia
->ia_ifp
!= imo
->imo_multicast_ifp
)) {
582 ifp
= imo
->imo_multicast_ifp
;
584 ifafree(&ia
->ia_ifa
);
585 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
586 if (ia
->ia_ifp
== ifp
)
589 lck_mtx_unlock(rt_mtx
);
590 return (EADDRNOTAVAIL
);
596 * Don't do pcblookup call here; return interface in plocal_sin
597 * and exit to caller, that will do the lookup.
599 *plocal_sin
= &ia
->ia_addr
;
600 ifafree(&ia
->ia_ifa
);
602 lck_mtx_unlock(rt_mtx
);
608 * Connect from a socket to a specified address.
609 * Both address and port must be specified in argument sin.
610 * If don't have a local address for this socket yet,
614 in_pcbconnect(inp
, nam
, p
)
615 register struct inpcb
*inp
;
616 struct sockaddr
*nam
;
619 struct sockaddr_in
*ifaddr
;
620 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
625 * Call inner routine, to assign local interface address.
627 if ((error
= in_pcbladdr(inp
, nam
, &ifaddr
)) != 0)
630 socket_unlock(inp
->inp_socket
, 0);
631 pcb
= in_pcblookup_hash(inp
->inp_pcbinfo
, sin
->sin_addr
, sin
->sin_port
,
632 inp
->inp_laddr
.s_addr
? inp
->inp_laddr
: ifaddr
->sin_addr
,
633 inp
->inp_lport
, 0, NULL
);
634 socket_lock(inp
->inp_socket
, 0);
636 in_pcb_checkstate(pcb
, WNT_RELEASE
, 0);
639 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
640 if (inp
->inp_lport
== 0) {
641 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
645 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
646 /*lock inversion issue, mostly with udp multicast packets */
647 socket_unlock(inp
->inp_socket
, 0);
648 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
649 socket_lock(inp
->inp_socket
, 0);
651 inp
->inp_laddr
= ifaddr
->sin_addr
;
652 inp
->inp_flags
|= INP_INADDR_ANY
;
655 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
656 /*lock inversion issue, mostly with udp multicast packets */
657 socket_unlock(inp
->inp_socket
, 0);
658 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
659 socket_lock(inp
->inp_socket
, 0);
662 inp
->inp_faddr
= sin
->sin_addr
;
663 inp
->inp_fport
= sin
->sin_port
;
665 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
670 in_pcbdisconnect(inp
)
674 inp
->inp_faddr
.s_addr
= INADDR_ANY
;
677 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
678 /*lock inversion issue, mostly with udp multicast packets */
679 socket_unlock(inp
->inp_socket
, 0);
680 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
681 socket_lock(inp
->inp_socket
, 0);
685 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
687 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
695 struct socket
*so
= inp
->inp_socket
;
696 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
698 if (so
->so_pcb
== 0) { /* we've been called twice */
699 panic("in_pcbdetach: inp=%x so=%x proto=%x so_pcb is null!\n",
700 inp
, so
, so
->so_proto
->pr_protocol
);
704 if (ipsec_bypass
== 0) {
705 lck_mtx_lock(sadb_mutex
);
706 ipsec4_delete_pcbpolicy(inp
);
707 lck_mtx_unlock(sadb_mutex
);
711 /* mark socket state as dead */
712 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
)
713 panic("in_pcbdetach so=%x prot=%x couldn't set to STOPUSING\n", so
, so
->so_proto
->pr_protocol
);
716 if (so
->cached_in_sock_layer
)
717 printf("in_pcbdetach for cached socket %x flags=%x\n", so
, so
->so_flags
);
719 printf("in_pcbdetach for allocated socket %x flags=%x\n", so
, so
->so_flags
);
721 if ((so
->so_flags
& SOF_PCBCLEARING
) == 0) {
723 if (inp
->inp_options
)
724 (void)m_free(inp
->inp_options
);
727 * route deletion requires reference count to be <= zero
729 lck_mtx_lock(rt_mtx
);
730 if ((rt
->rt_flags
& RTF_DELCLONE
) &&
731 (rt
->rt_flags
& RTF_WASCLONED
) &&
732 (rt
->rt_refcnt
<= 1)) {
734 rt
->rt_flags
&= ~RTF_UP
;
735 rtrequest_locked(RTM_DELETE
, rt_key(rt
),
736 rt
->rt_gateway
, rt_mask(rt
),
737 rt
->rt_flags
, (struct rtentry
**)0);
741 inp
->inp_route
.ro_rt
= 0;
743 lck_mtx_unlock(rt_mtx
);
745 ip_freemoptions(inp
->inp_moptions
);
746 inp
->inp_moptions
= NULL
;
747 sofreelastref(so
, 0);
748 inp
->inp_state
= INPCB_STATE_DEAD
;
749 so
->so_flags
|= SOF_PCBCLEARING
; /* makes sure we're not called twice from so_close */
758 struct socket
*so
= inp
->inp_socket
;
759 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
762 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
763 printf("in_pcbdispose: not dead yet? so=%x\n", so
);
767 if (so
&& so
->so_usecount
!= 0)
768 panic("in_pcbdispose: use count=%x so=%x\n", so
->so_usecount
, so
);
771 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
772 /*### access ipi in in_pcbremlists */
776 if (so
->so_proto
->pr_flags
& PR_PCBLOCK
) {
777 sofreelastref(so
, 0);
778 if (so
->so_rcv
.sb_cc
|| so
->so_snd
.sb_cc
) {
780 printf("in_pcbdispose sb not cleaned up so=%x rc_cci=%x snd_cc=%x\n",
781 so
, so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
783 sbrelease(&so
->so_rcv
);
784 sbrelease(&so
->so_snd
);
786 if (so
->so_head
!= NULL
)
787 panic("in_pcbdispose, so=%x head still exist\n", so
);
788 lck_mtx_unlock(inp
->inpcb_mtx
);
789 lck_mtx_free(inp
->inpcb_mtx
, ipi
->mtx_grp
);
791 so
->so_flags
|= SOF_PCBCLEARING
; /* makes sure we're not called twice from so_close */
792 so
->so_saved_pcb
= (caddr_t
) inp
;
795 inp
->reserved
[0] = so
;
796 if (so
->cached_in_sock_layer
== 0) {
797 zfree(ipi
->ipi_zone
, inp
);
803 printf("in_pcbdispose: no socket for inp=%x\n", inp
);
808 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
809 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
810 * in struct pr_usrreqs, so that protocols can just reference then directly
811 * without the need for a wrapper function. The socket must have a valid
812 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
813 * except through a kernel programming error, so it is acceptable to panic
814 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
815 * because there actually /is/ a programming error somewhere... XXX)
818 in_setsockaddr(so
, nam
)
820 struct sockaddr
**nam
;
822 register struct inpcb
*inp
;
823 register struct sockaddr_in
*sin
;
826 * Do the malloc first in case it blocks.
828 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
831 bzero(sin
, sizeof *sin
);
832 sin
->sin_family
= AF_INET
;
833 sin
->sin_len
= sizeof(*sin
);
840 sin
->sin_port
= inp
->inp_lport
;
841 sin
->sin_addr
= inp
->inp_laddr
;
843 *nam
= (struct sockaddr
*)sin
;
848 in_setpeeraddr(so
, nam
)
850 struct sockaddr
**nam
;
853 register struct sockaddr_in
*sin
;
856 * Do the malloc first in case it blocks.
858 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
861 bzero((caddr_t
)sin
, sizeof (*sin
));
862 sin
->sin_family
= AF_INET
;
863 sin
->sin_len
= sizeof(*sin
);
870 sin
->sin_port
= inp
->inp_fport
;
871 sin
->sin_addr
= inp
->inp_faddr
;
873 *nam
= (struct sockaddr
*)sin
;
878 in_pcbnotifyall(pcbinfo
, faddr
, errno
, notify
)
879 struct inpcbinfo
*pcbinfo
;
880 struct in_addr faddr
;
881 void (*notify
) (struct inpcb
*, int);
885 lck_rw_lock_shared(pcbinfo
->mtx
);
887 LIST_FOREACH(inp
, pcbinfo
->listhead
, inp_list
) {
889 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
892 if (inp
->inp_faddr
.s_addr
!= faddr
.s_addr
||
893 inp
->inp_socket
== NULL
)
895 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
897 socket_lock(inp
->inp_socket
, 1);
898 (*notify
)(inp
, errno
);
899 (void)in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
900 socket_unlock(inp
->inp_socket
, 1);
902 lck_rw_done(pcbinfo
->mtx
);
911 struct ip_moptions
*imo
;
914 for (inp
= head
; inp
!= NULL
; inp
= LIST_NEXT(inp
, inp_list
)) {
915 imo
= inp
->inp_moptions
;
916 if ((inp
->inp_vflag
& INP_IPV4
) &&
919 * Unselect the outgoing interface if it is being
922 if (imo
->imo_multicast_ifp
== ifp
)
923 imo
->imo_multicast_ifp
= NULL
;
926 * Drop multicast group membership if we joined
927 * through the interface being detached.
929 for (i
= 0, gap
= 0; i
< imo
->imo_num_memberships
;
931 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
) {
932 in_delmulti(&imo
->imo_membership
[i
]);
935 imo
->imo_membership
[i
- gap
] =
936 imo
->imo_membership
[i
];
938 imo
->imo_num_memberships
-= gap
;
944 * Check for alternatives when higher level complains
945 * about service problems. For now, invalidate cached
946 * routing information. If the route was created dynamically
947 * (by a redirect), time to try a default gateway again.
953 register struct rtentry
*rt
;
954 struct rt_addrinfo info
;
956 if ((rt
= inp
->inp_route
.ro_rt
)) {
957 lck_mtx_lock(rt_mtx
);
958 bzero((caddr_t
)&info
, sizeof(info
));
959 info
.rti_info
[RTAX_DST
] =
960 (struct sockaddr
*)&inp
->inp_route
.ro_dst
;
961 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
962 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
963 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
964 if (rt
->rt_flags
& RTF_DYNAMIC
)
965 (void) rtrequest_locked(RTM_DELETE
, rt_key(rt
),
966 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
967 (struct rtentry
**)0);
968 inp
->inp_route
.ro_rt
= 0;
970 lck_mtx_unlock(rt_mtx
);
972 * A new route can be allocated
973 * the next time output is attempted.
979 * After a routing change, flush old routing
980 * and allocate a (hopefully) better one.
983 in_rtchange(inp
, errno
)
984 register struct inpcb
*inp
;
987 if (inp
->inp_route
.ro_rt
) {
988 if ((ifa_foraddr(inp
->inp_laddr
.s_addr
)) == 0)
989 return; /* we can't remove the route now. not sure if still ok to use src */
990 rtfree(inp
->inp_route
.ro_rt
);
991 inp
->inp_route
.ro_rt
= 0;
993 * A new route can be allocated the next time
994 * output is attempted.
1000 * Lookup a PCB based on the local address and port.
1003 in_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
1004 struct inpcbinfo
*pcbinfo
;
1005 struct in_addr laddr
;
1009 register struct inpcb
*inp
;
1010 int matchwild
= 3, wildcard
;
1011 u_short lport
= lport_arg
;
1013 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_START
, 0,0,0,0,0);
1016 struct inpcbhead
*head
;
1018 * Look for an unconnected (wildcard foreign addr) PCB that
1019 * matches the local address and port we're looking for.
1021 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
1022 LIST_FOREACH(inp
, head
, inp_hash
) {
1024 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
1027 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
1028 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1029 inp
->inp_lport
== lport
) {
1039 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, 0,0,0,0,0);
1042 struct inpcbporthead
*porthash
;
1043 struct inpcbport
*phd
;
1044 struct inpcb
*match
= NULL
;
1046 * Best fit PCB lookup.
1048 * First see if this local port is in use by looking on the
1051 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1052 pcbinfo
->porthashmask
)];
1053 LIST_FOREACH(phd
, porthash
, phd_hash
) {
1054 if (phd
->phd_port
== lport
)
1059 * Port is in use by one or more PCBs. Look for best
1062 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
1065 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
1068 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
1070 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
1071 if (laddr
.s_addr
== INADDR_ANY
)
1073 else if (inp
->inp_laddr
.s_addr
!= laddr
.s_addr
)
1076 if (laddr
.s_addr
!= INADDR_ANY
)
1079 if (wildcard
< matchwild
) {
1081 matchwild
= wildcard
;
1082 if (matchwild
== 0) {
1088 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, match
,0,0,0,0);
1094 * Lookup PCB in hash list.
1098 struct inpcbinfo
*pcbinfo
,
1099 struct in_addr faddr
,
1101 struct in_addr laddr
,
1106 struct inpcbhead
*head
;
1107 register struct inpcb
*inp
;
1108 u_short fport
= fport_arg
, lport
= lport_arg
;
1111 * We may have found the pcb in the last lookup - check this first.
1114 lck_rw_lock_shared(pcbinfo
->mtx
);
1117 * First look for an exact match.
1119 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
1120 LIST_FOREACH(inp
, head
, inp_hash
) {
1122 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
1125 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
1126 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1127 inp
->inp_fport
== fport
&&
1128 inp
->inp_lport
== lport
) {
1132 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1133 lck_rw_done(pcbinfo
->mtx
);
1136 else { /* it's there but dead, say it isn't found */
1137 lck_rw_done(pcbinfo
->mtx
);
1143 struct inpcb
*local_wild
= NULL
;
1145 struct inpcb
*local_wild_mapped
= NULL
;
1148 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
1149 LIST_FOREACH(inp
, head
, inp_hash
) {
1151 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
1154 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
1155 inp
->inp_lport
== lport
) {
1156 #if defined(NFAITH) && NFAITH > 0
1157 if (ifp
&& ifp
->if_type
== IFT_FAITH
&&
1158 (inp
->inp_flags
& INP_FAITH
) == 0)
1161 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
1162 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1163 lck_rw_done(pcbinfo
->mtx
);
1166 else { /* it's there but dead, say it isn't found */
1167 lck_rw_done(pcbinfo
->mtx
);
1171 else if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1173 if (INP_CHECK_SOCKAF(inp
->inp_socket
,
1175 local_wild_mapped
= inp
;
1177 #endif /* defined(INET6) */
1183 if (local_wild
== NULL
) {
1184 if (local_wild_mapped
!= NULL
) {
1185 if (in_pcb_checkstate(local_wild_mapped
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1186 lck_rw_done(pcbinfo
->mtx
);
1187 return (local_wild_mapped
);
1189 else { /* it's there but dead, say it isn't found */
1190 lck_rw_done(pcbinfo
->mtx
);
1194 lck_rw_done(pcbinfo
->mtx
);
1197 #endif /* defined(INET6) */
1198 if (in_pcb_checkstate(local_wild
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1199 lck_rw_done(pcbinfo
->mtx
);
1200 return (local_wild
);
1202 else { /* it's there but dead, say it isn't found */
1203 lck_rw_done(pcbinfo
->mtx
);
1211 lck_rw_done(pcbinfo
->mtx
);
1216 * Insert PCB onto various hash lists.
1219 in_pcbinshash(inp
, locked
)
1221 int locked
; /* list already locked exclusive */
1223 struct inpcbhead
*pcbhash
;
1224 struct inpcbporthead
*pcbporthash
;
1225 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1226 struct inpcbport
*phd
;
1227 u_int32_t hashkey_faddr
;
1230 if (inp
->inp_vflag
& INP_IPV6
)
1231 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1234 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1236 inp
->hash_element
= INP_PCBHASH(hashkey_faddr
, inp
->inp_lport
, inp
->inp_fport
, pcbinfo
->hashmask
);
1239 if (!lck_rw_try_lock_exclusive(pcbinfo
->mtx
)) {
1240 /*lock inversion issue, mostly with udp multicast packets */
1241 socket_unlock(inp
->inp_socket
, 0);
1242 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1243 socket_lock(inp
->inp_socket
, 0);
1247 pcbhash
= &pcbinfo
->hashbase
[inp
->hash_element
];
1249 pcbporthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(inp
->inp_lport
,
1250 pcbinfo
->porthashmask
)];
1253 * Go through port list and look for a head for this lport.
1255 LIST_FOREACH(phd
, pcbporthash
, phd_hash
) {
1256 if (phd
->phd_port
== inp
->inp_lport
)
1260 * If none exists, malloc one and tack it on.
1263 MALLOC(phd
, struct inpcbport
*, sizeof(struct inpcbport
), M_PCB
, M_WAITOK
);
1266 lck_rw_done(pcbinfo
->mtx
);
1267 return (ENOBUFS
); /* XXX */
1269 phd
->phd_port
= inp
->inp_lport
;
1270 LIST_INIT(&phd
->phd_pcblist
);
1271 LIST_INSERT_HEAD(pcbporthash
, phd
, phd_hash
);
1274 LIST_INSERT_HEAD(&phd
->phd_pcblist
, inp
, inp_portlist
);
1275 LIST_INSERT_HEAD(pcbhash
, inp
, inp_hash
);
1277 lck_rw_done(pcbinfo
->mtx
);
1282 * Move PCB to the proper hash bucket when { faddr, fport } have been
1283 * changed. NOTE: This does not handle the case of the lport changing (the
1284 * hashed port list would have to be updated as well), so the lport must
1285 * not change after in_pcbinshash() has been called.
1291 struct inpcbhead
*head
;
1292 u_int32_t hashkey_faddr
;
1295 if (inp
->inp_vflag
& INP_IPV6
)
1296 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1299 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1300 inp
->hash_element
= INP_PCBHASH(hashkey_faddr
, inp
->inp_lport
,
1301 inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
);
1302 head
= &inp
->inp_pcbinfo
->hashbase
[inp
->hash_element
];
1304 LIST_REMOVE(inp
, inp_hash
);
1305 LIST_INSERT_HEAD(head
, inp
, inp_hash
);
1309 * Remove PCB from various lists.
1311 //###LOCK must be called with list lock held
1316 inp
->inp_gencnt
= ++inp
->inp_pcbinfo
->ipi_gencnt
;
1318 if (inp
->inp_lport
) {
1319 struct inpcbport
*phd
= inp
->inp_phd
;
1321 LIST_REMOVE(inp
, inp_hash
);
1322 LIST_REMOVE(inp
, inp_portlist
);
1323 if (phd
!= NULL
&& (LIST_FIRST(&phd
->phd_pcblist
) == NULL
)) {
1324 LIST_REMOVE(phd
, phd_hash
);
1328 LIST_REMOVE(inp
, inp_list
);
1329 inp
->inp_pcbinfo
->ipi_count
--;
1332 static void in_pcb_detach_port( struct inpcb
*inp
);
1334 in_pcb_grab_port (struct inpcbinfo
*pcbinfo
,
1336 struct in_addr laddr
,
1338 struct in_addr faddr
,
1343 struct inpcb
*inp
, *pcb
;
1344 struct sockaddr_in sin
;
1345 struct proc
*p
= current_proc();
1349 pcbinfo
->nat_dummy_socket
.so_pcb
= 0;
1350 pcbinfo
->nat_dummy_socket
.so_options
= 0;
1352 /* The grabber wants a particular port */
1354 if (faddr
.s_addr
|| fport
) {
1356 * This is either the second half of an active connect, or
1357 * it's from the acceptance of an incoming connection.
1359 if (laddr
.s_addr
== 0) {
1360 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1364 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
, laddr
, *lport
, 0, NULL
);
1366 /* pcb was found, its count was upped. need to decrease it here */
1367 in_pcb_checkstate(inp
, WNT_RELEASE
, 0);
1368 if (!(IN_MULTICAST(ntohl(laddr
.s_addr
)))) {
1369 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1370 return (EADDRINUSE
);
1374 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1376 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1379 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1380 pcb
->inp_vflag
|= INP_IPV4
;
1382 pcb
->inp_lport
= *lport
;
1383 pcb
->inp_laddr
.s_addr
= laddr
.s_addr
;
1385 pcb
->inp_faddr
= faddr
;
1386 pcb
->inp_fport
= fport
;
1388 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1389 in_pcbinshash(pcb
, 1);
1390 lck_rw_done(pcbinfo
->mtx
);
1394 * This is either a bind for a passive socket, or it's the
1395 * first part of bind-connect sequence (not likely since an
1396 * ephemeral port is usually used in this case). Or, it's
1397 * the result of a connection acceptance when the foreign
1398 * address/port cannot be provided (which requires the SO_REUSEADDR
1399 * flag if laddr is not multicast).
1402 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1404 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1407 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1408 pcb
->inp_vflag
|= INP_IPV4
;
1410 pcbinfo
->nat_dummy_socket
.so_options
= options
;
1411 bzero(&sin
, sizeof(struct sockaddr_in
));
1412 sin
.sin_len
= sizeof(struct sockaddr_in
);
1413 sin
.sin_family
= AF_INET
;
1414 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1415 sin
.sin_port
= *lport
;
1417 socket_lock(&pcbinfo
->nat_dummy_socket
, 1);
1418 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1419 (struct sockaddr
*) &sin
, p
);
1421 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1); /*detach first */
1422 in_pcb_detach_port(pcb
); /* will restore dummy pcb */
1425 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1429 /* The grabber wants an ephemeral port */
1431 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1433 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1436 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1437 pcb
->inp_vflag
|= INP_IPV4
;
1439 bzero(&sin
, sizeof(struct sockaddr_in
));
1440 sin
.sin_len
= sizeof(struct sockaddr_in
);
1441 sin
.sin_family
= AF_INET
;
1442 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1445 if (faddr
.s_addr
|| fport
) {
1447 * Not sure if this case will be used - could occur when connect
1448 * is called, skipping the bind.
1451 if (laddr
.s_addr
== 0) {
1452 in_pcb_detach_port(pcb
); /* restores dummy pcb */
1456 socket_lock(&pcbinfo
->nat_dummy_socket
, 1);
1457 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1458 (struct sockaddr
*) &sin
, p
);
1460 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1461 in_pcb_detach_port(pcb
); /* restores dummy pcb */
1465 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1466 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1467 pcb
->inp_laddr
, pcb
->inp_lport
, 0, NULL
);
1469 /* pcb was found, its count was upped. need to decrease it here */
1470 in_pcb_checkstate(inp
, WNT_RELEASE
, 0);
1471 in_pcb_detach_port(pcb
);
1472 return (EADDRINUSE
);
1475 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1476 pcb
->inp_faddr
= faddr
;
1477 pcb
->inp_fport
= fport
;
1479 lck_rw_done(pcbinfo
->mtx
);
1483 * This is a simple bind of an ephemeral port. The local addr
1484 * may or may not be defined.
1487 socket_lock(&pcbinfo
->nat_dummy_socket
, 1);
1488 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1489 (struct sockaddr
*) &sin
, p
);
1491 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1492 in_pcb_detach_port(pcb
);
1495 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1497 *lport
= pcb
->inp_lport
;
1501 pcb
->nat_owner
= owner_id
;
1502 pcb
->nat_cookie
= cookie
;
1503 pcb
->inp_ppcb
= (caddr_t
) pcbinfo
->dummy_cb
;
1504 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
; /* restores dummypcb */
1508 /* 3962035 - in_pcb_letgo_port needs a special case function for detaching */
1513 struct socket
*so
= inp
->inp_socket
;
1514 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1516 if (so
!= &pcbinfo
->nat_dummy_socket
)
1517 panic("in_pcb_detach_port: not a dummy_sock: so=%x, inp=%x\n", so
, inp
);
1518 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
1519 /*### access ipi in in_pcbremlists */
1520 in_pcbremlists(inp
);
1522 inp
->inp_socket
= 0;
1523 inp
->reserved
[0] = so
;
1524 zfree(pcbinfo
->ipi_zone
, inp
);
1525 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
; /* restores dummypcb */
1529 in_pcb_letgo_port(struct inpcbinfo
*pcbinfo
, struct in_addr laddr
, u_short lport
,
1530 struct in_addr faddr
, u_short fport
, u_char owner_id
)
1532 struct inpcbhead
*head
;
1533 register struct inpcb
*inp
;
1537 * First look for an exact match.
1540 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1541 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
1542 for (inp
= head
->lh_first
; inp
!= NULL
; inp
= inp
->inp_hash
.le_next
) {
1543 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
1544 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1545 inp
->inp_fport
== fport
&&
1546 inp
->inp_lport
== lport
&&
1547 inp
->nat_owner
== owner_id
) {
1551 in_pcb_detach_port(inp
);
1552 lck_rw_done(pcbinfo
->mtx
);
1557 lck_rw_done(pcbinfo
->mtx
);
1562 in_pcb_get_owner(struct inpcbinfo
*pcbinfo
,
1563 struct in_addr laddr
, u_short lport
,
1564 struct in_addr faddr
, u_short fport
,
1569 u_char owner_id
= INPCB_NO_OWNER
;
1570 struct inpcbport
*phd
;
1571 struct inpcbporthead
*porthash
;
1574 if (IN_MULTICAST(laddr
.s_addr
)) {
1576 * Walk through PCB's looking for registered
1580 lck_rw_lock_shared(pcbinfo
->mtx
);
1581 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1582 pcbinfo
->porthashmask
)];
1583 for (phd
= porthash
->lh_first
; phd
!= NULL
; phd
= phd
->phd_hash
.le_next
) {
1584 if (phd
->phd_port
== lport
)
1589 lck_rw_done(pcbinfo
->mtx
);
1590 return INPCB_NO_OWNER
;
1593 owner_id
= INPCB_NO_OWNER
;
1594 for (inp
= phd
->phd_pcblist
.lh_first
; inp
!= NULL
;
1595 inp
= inp
->inp_portlist
.le_next
) {
1597 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
1598 if (inp
->nat_owner
== 0)
1599 owner_id
|= INPCB_OWNED_BY_X
;
1601 owner_id
|= inp
->nat_owner
;
1605 lck_rw_done(pcbinfo
->mtx
);
1609 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1610 laddr
, lport
, 1, NULL
);
1612 /* pcb was found, its count was upped. need to decrease it here */
1613 /* if we found it, that pcb is already locked by the caller */
1614 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
)
1615 return(INPCB_NO_OWNER
);
1617 if (inp
->nat_owner
) {
1618 owner_id
= inp
->nat_owner
;
1619 *cookie
= inp
->nat_cookie
;
1622 owner_id
= INPCB_OWNED_BY_X
;
1626 owner_id
= INPCB_NO_OWNER
;
1633 in_pcb_new_share_client(struct inpcbinfo
*pcbinfo
, u_char
*owner_id
)
1639 for (i
=0; i
< INPCB_MAX_IDS
; i
++) {
1640 if ((pcbinfo
->all_owners
& (1 << i
)) == 0) {
1641 pcbinfo
->all_owners
|= (1 << i
);
1642 *owner_id
= (1 << i
);
1651 in_pcb_rem_share_client(struct inpcbinfo
*pcbinfo
, u_char owner_id
)
1656 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1657 if (pcbinfo
->all_owners
& owner_id
) {
1658 pcbinfo
->all_owners
&= ~owner_id
;
1659 for (inp
= pcbinfo
->listhead
->lh_first
; inp
!= NULL
; inp
= inp
->inp_list
.le_next
) {
1660 if (inp
->nat_owner
& owner_id
) {
1661 if (inp
->nat_owner
== owner_id
)
1663 * Deallocate the pcb
1665 in_pcb_detach_port(inp
);
1667 inp
->nat_owner
&= ~owner_id
;
1672 lck_rw_done(pcbinfo
->mtx
);
1676 lck_rw_done(pcbinfo
->mtx
);
1682 void in_pcb_nat_init(struct inpcbinfo
*pcbinfo
, int afamily
,
1683 int pfamily
, int protocol
)
1686 struct proc
*p
= current_proc();
1688 bzero(&pcbinfo
->nat_dummy_socket
, sizeof(struct socket
));
1689 pcbinfo
->nat_dummy_socket
.so_proto
= pffindproto_locked(afamily
, pfamily
, protocol
);
1690 pcbinfo
->all_owners
= 0;
1691 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1693 panic("in_pcb_nat_init: can't alloc fakepcb err=%\n", stat
);
1694 pcbinfo
->nat_dummy_pcb
= pcbinfo
->nat_dummy_socket
.so_pcb
;
1697 /* Mechanism used to defer the memory release of PCBs
1698 * The pcb list will contain the pcb until the ripper can clean it up if
1699 * the following conditions are met: 1) state "DEAD", 2) wantcnt is STOPUSING
1700 * 3) usecount is null
1701 * This function will be called to either mark the pcb as
1704 in_pcb_checkstate(struct inpcb
*pcb
, int mode
, int locked
)
1708 volatile UInt32
*wantcnt
= (volatile UInt32
*)&pcb
->inp_wantcnt
;
1714 case WNT_STOPUSING
: /* try to mark the pcb as ready for recycling */
1716 /* compareswap with STOPUSING, if success we're good, if it's in use, will be marked later */
1719 socket_lock(pcb
->inp_socket
, 1);
1720 pcb
->inp_state
= INPCB_STATE_DEAD
;
1722 if (pcb
->inp_socket
->so_usecount
< 0)
1723 panic("in_pcb_checkstate STOP pcb=%x so=%x usecount is negative\n", pcb
, pcb
->inp_socket
);
1725 socket_unlock(pcb
->inp_socket
, 1);
1727 origwant
= *wantcnt
;
1728 if ((UInt16
) origwant
== 0xffff ) /* should stop using */
1729 return (WNT_STOPUSING
);
1731 if ((UInt16
) origwant
== 0) {/* try to mark it as unsuable now */
1732 OSCompareAndSwap(origwant
, newwant
, (UInt32
*) wantcnt
) ;
1734 return (WNT_STOPUSING
);
1737 case WNT_ACQUIRE
: /* try to increase reference to pcb */
1738 /* if WNT_STOPUSING should bail out */
1740 * if socket state DEAD, try to set count to STOPUSING, return failed
1741 * otherwise increase cnt
1744 origwant
= *wantcnt
;
1745 if ((UInt16
) origwant
== 0xffff ) {/* should stop using */
1746 // printf("in_pcb_checkstate: ACQ PCB was STOPUSING while release. odd pcb=%x\n", pcb);
1747 return (WNT_STOPUSING
);
1749 newwant
= origwant
+ 1;
1750 } while (!OSCompareAndSwap(origwant
, newwant
, (UInt32
*) wantcnt
));
1751 return (WNT_ACQUIRE
);
1754 case WNT_RELEASE
: /* release reference. if result is null and pcb state is DEAD,
1755 set wanted bit to STOPUSING
1759 socket_lock(pcb
->inp_socket
, 1);
1762 origwant
= *wantcnt
;
1763 if ((UInt16
) origwant
== 0x0 )
1764 panic("in_pcb_checkstate pcb=%x release with zero count", pcb
);
1765 if ((UInt16
) origwant
== 0xffff ) {/* should stop using */
1767 printf("in_pcb_checkstate: REL PCB was STOPUSING while release. odd pcb=%x\n", pcb
);
1770 socket_unlock(pcb
->inp_socket
, 1);
1771 return (WNT_STOPUSING
);
1773 newwant
= origwant
- 1;
1774 } while (!OSCompareAndSwap(origwant
, newwant
, (UInt32
*) wantcnt
));
1776 if (pcb
->inp_state
== INPCB_STATE_DEAD
)
1778 if (pcb
->inp_socket
->so_usecount
< 0)
1779 panic("in_pcb_checkstate RELEASE pcb=%x so=%x usecount is negative\n", pcb
, pcb
->inp_socket
);
1782 socket_unlock(pcb
->inp_socket
, 1);
1783 return (WNT_RELEASE
);
1788 panic("in_pcb_checkstate: so=%x not a valid state =%x\n", pcb
->inp_socket
, mode
);
1796 * inpcb_to_compat copies specific bits of an inpcb to a inpcb_compat.
1797 * The inpcb_compat data structure is passed to user space and must
1798 * not change. We intentionally avoid copying pointers. The socket is
1799 * the one exception, though we probably shouldn't copy that either.
1804 struct inpcb_compat
*inp_compat
)
1806 bzero(inp_compat
, sizeof(*inp_compat
));
1807 inp_compat
->inp_fport
= inp
->inp_fport
;
1808 inp_compat
->inp_lport
= inp
->inp_lport
;
1809 inp_compat
->inp_socket
= inp
->inp_socket
;
1810 inp_compat
->nat_owner
= inp
->nat_owner
;
1811 inp_compat
->nat_cookie
= inp
->nat_cookie
;
1812 inp_compat
->inp_gencnt
= inp
->inp_gencnt
;
1813 inp_compat
->inp_flags
= inp
->inp_flags
;
1814 inp_compat
->inp_flow
= inp
->inp_flow
;
1815 inp_compat
->inp_vflag
= inp
->inp_vflag
;
1816 inp_compat
->inp_ip_ttl
= inp
->inp_ip_ttl
;
1817 inp_compat
->inp_ip_p
= inp
->inp_ip_p
;
1818 inp_compat
->inp_dependfaddr
.inp6_foreign
= inp
->inp_dependfaddr
.inp6_foreign
;
1819 inp_compat
->inp_dependladdr
.inp6_local
= inp
->inp_dependladdr
.inp6_local
;
1820 inp_compat
->inp_depend4
.inp4_ip_tos
= inp
->inp_depend4
.inp4_ip_tos
;
1821 inp_compat
->inp_depend6
.inp6_hlim
= inp
->inp_depend6
.inp6_hlim
;
1822 inp_compat
->inp_depend6
.inp6_cksum
= inp
->inp_depend6
.inp6_cksum
;
1823 inp_compat
->inp6_ifindex
= inp
->inp6_ifindex
;
1824 inp_compat
->inp_depend6
.inp6_hops
= inp
->inp_depend6
.inp6_hops
;
1828 prison_xinpcb(struct proc
*p
, struct inpcb
*inp
)
1832 if (ntohl(inp
->inp_laddr
.s_addr
) == p
->p_prison
->pr_ip
)