2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1991, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
55 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
62 #include <sys/domain.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
72 #include <libkern/OSAtomic.h>
74 #include <machine/limits.h>
77 #include <kern/zalloc.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
96 #include <netinet6/ipsec.h>
97 #include <netkey/key.h>
100 #include <sys/kdebug.h>
103 extern int ipsec_bypass
;
104 extern lck_mtx_t
*sadb_mutex
;
107 extern u_long route_generation
;
109 #define DBG_FNC_PCB_LOOKUP NETDBG_CODE(DBG_NETTCP, (6 << 8))
110 #define DBG_FNC_PCB_HLOOKUP NETDBG_CODE(DBG_NETTCP, ((6 << 8) | 1))
112 struct in_addr zeroin_addr
;
115 * These configure the range of local port addresses assigned to
116 * "unspecified" outgoing connections/packets/whatever.
118 int ipport_lowfirstauto
= IPPORT_RESERVED
- 1; /* 1023 */
119 int ipport_lowlastauto
= IPPORT_RESERVEDSTART
; /* 600 */
121 int ipport_firstauto
= IPPORT_RESERVED
; /* 1024 */
122 int ipport_lastauto
= IPPORT_USERRESERVED
; /* 5000 */
124 int ipport_firstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
125 int ipport_lastauto
= IPPORT_HILASTAUTO
; /* 65535 */
127 int ipport_hifirstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
128 int ipport_hilastauto
= IPPORT_HILASTAUTO
; /* 65535 */
130 #define RANGECHK(var, min, max) \
131 if ((var) < (min)) { (var) = (min); } \
132 else if ((var) > (max)) { (var) = (max); }
135 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
137 int error
= sysctl_handle_int(oidp
,
138 oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
140 RANGECHK(ipport_lowfirstauto
, 1, IPPORT_RESERVED
- 1);
141 RANGECHK(ipport_lowlastauto
, 1, IPPORT_RESERVED
- 1);
142 RANGECHK(ipport_firstauto
, IPPORT_RESERVED
, USHRT_MAX
);
143 RANGECHK(ipport_lastauto
, IPPORT_RESERVED
, USHRT_MAX
);
144 RANGECHK(ipport_hifirstauto
, IPPORT_RESERVED
, USHRT_MAX
);
145 RANGECHK(ipport_hilastauto
, IPPORT_RESERVED
, USHRT_MAX
);
152 SYSCTL_NODE(_net_inet_ip
, IPPROTO_IP
, portrange
, CTLFLAG_RW
, 0, "IP Ports");
154 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowfirst
, CTLTYPE_INT
|CTLFLAG_RW
,
155 &ipport_lowfirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
156 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowlast
, CTLTYPE_INT
|CTLFLAG_RW
,
157 &ipport_lowlastauto
, 0, &sysctl_net_ipport_check
, "I", "");
158 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, first
, CTLTYPE_INT
|CTLFLAG_RW
,
159 &ipport_firstauto
, 0, &sysctl_net_ipport_check
, "I", "");
160 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, last
, CTLTYPE_INT
|CTLFLAG_RW
,
161 &ipport_lastauto
, 0, &sysctl_net_ipport_check
, "I", "");
162 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hifirst
, CTLTYPE_INT
|CTLFLAG_RW
,
163 &ipport_hifirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
164 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hilast
, CTLTYPE_INT
|CTLFLAG_RW
,
165 &ipport_hilastauto
, 0, &sysctl_net_ipport_check
, "I", "");
168 * in_pcb.c: manage the Protocol Control Blocks.
170 * NOTE: It is assumed that most of these functions will be called at
171 * splnet(). XXX - There are, unfortunately, a few exceptions to this
172 * rule that should be fixed.
176 * Allocate a PCB and associate it with the socket.
179 in_pcballoc(so
, pcbinfo
, p
)
181 struct inpcbinfo
*pcbinfo
;
184 register struct inpcb
*inp
;
192 if (so
->cached_in_sock_layer
== 0) {
194 printf("PCBALLOC calling zalloc for socket %x\n", so
);
196 inp
= (struct inpcb
*) zalloc(pcbinfo
->ipi_zone
);
199 bzero((caddr_t
)inp
, sizeof(*inp
));
203 printf("PCBALLOC reusing PCB for socket %x\n", so
);
205 inp
= (struct inpcb
*) so
->so_saved_pcb
;
206 temp
= inp
->inp_saved_ppcb
;
207 bzero((caddr_t
) inp
, sizeof(*inp
));
208 inp
->inp_saved_ppcb
= temp
;
211 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
212 inp
->inp_pcbinfo
= pcbinfo
;
213 inp
->inp_socket
= so
;
214 so
->so_pcb
= (caddr_t
)inp
;
216 if (so
->so_proto
->pr_flags
& PR_PCBLOCK
) {
217 inp
->inpcb_mtx
= lck_mtx_alloc_init(pcbinfo
->mtx_grp
, pcbinfo
->mtx_attr
);
218 if (inp
->inpcb_mtx
== NULL
) {
219 printf("in_pcballoc: can't alloc mutex! so=%x\n", so
);
226 if (ipsec_bypass
== 0) {
227 lck_mtx_lock(sadb_mutex
);
228 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
229 lck_mtx_unlock(sadb_mutex
);
231 zfree(pcbinfo
->ipi_zone
, inp
);
238 if (INP_SOCKAF(so
) == AF_INET6
&& !ip6_mapped_addr_on
)
239 inp
->inp_flags
|= IN6P_IPV6_V6ONLY
;
243 if (ip6_auto_flowlabel
)
244 inp
->inp_flags
|= IN6P_AUTOFLOWLABEL
;
246 lck_rw_lock_exclusive(pcbinfo
->mtx
);
247 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
248 LIST_INSERT_HEAD(pcbinfo
->listhead
, inp
, inp_list
);
249 pcbinfo
->ipi_count
++;
250 lck_rw_done(pcbinfo
->mtx
);
255 in_pcbbind(inp
, nam
, p
)
256 register struct inpcb
*inp
;
257 struct sockaddr
*nam
;
260 register struct socket
*so
= inp
->inp_socket
;
261 unsigned short *lastport
;
262 struct sockaddr_in
*sin
;
263 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
265 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
268 if (TAILQ_EMPTY(&in_ifaddrhead
)) /* XXX broken! */
269 return (EADDRNOTAVAIL
);
270 if (inp
->inp_lport
|| inp
->inp_laddr
.s_addr
!= INADDR_ANY
)
272 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
274 socket_unlock(so
, 0); /* keep reference on socket */
275 lck_rw_lock_exclusive(pcbinfo
->mtx
);
277 sin
= (struct sockaddr_in
*)nam
;
278 if (nam
->sa_len
!= sizeof (*sin
)) {
279 lck_rw_done(pcbinfo
->mtx
);
285 * We should check the family, but old programs
286 * incorrectly fail to initialize it.
288 if (sin
->sin_family
!= AF_INET
) {
289 lck_rw_done(pcbinfo
->mtx
);
291 return (EAFNOSUPPORT
);
294 lport
= sin
->sin_port
;
295 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
297 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
298 * allow complete duplication of binding if
299 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
300 * and a multicast address is bound on both
301 * new and duplicated sockets.
303 if (so
->so_options
& SO_REUSEADDR
)
304 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
305 } else if (sin
->sin_addr
.s_addr
!= INADDR_ANY
) {
307 sin
->sin_port
= 0; /* yech... */
308 if ((ifa
= ifa_ifwithaddr((struct sockaddr
*)sin
)) == 0) {
309 lck_rw_done(pcbinfo
->mtx
);
311 return (EADDRNOTAVAIL
);
321 if (ntohs(lport
) < IPPORT_RESERVED
&& p
&&
323 lck_rw_done(pcbinfo
->mtx
);
328 !IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
329 t
= in_pcblookup_local(inp
->inp_pcbinfo
,
330 sin
->sin_addr
, lport
, INPLOOKUP_WILDCARD
);
332 (ntohl(sin
->sin_addr
.s_addr
) != INADDR_ANY
||
333 ntohl(t
->inp_laddr
.s_addr
) != INADDR_ANY
||
334 (t
->inp_socket
->so_options
&
335 SO_REUSEPORT
) == 0) &&
336 (so
->so_uid
!= t
->inp_socket
->so_uid
)) {
338 if (ntohl(sin
->sin_addr
.s_addr
) !=
340 ntohl(t
->inp_laddr
.s_addr
) !=
343 INP_SOCKAF(t
->inp_socket
)) {
344 #endif /* defined(INET6) */
345 lck_rw_done(pcbinfo
->mtx
);
351 t
= in_pcblookup_local(pcbinfo
, sin
->sin_addr
,
354 (reuseport
& t
->inp_socket
->so_options
) == 0) {
356 if (ip6_mapped_addr_on
== 0 ||
357 ntohl(sin
->sin_addr
.s_addr
) !=
359 ntohl(t
->inp_laddr
.s_addr
) !=
362 INP_SOCKAF(t
->inp_socket
)) {
363 #endif /* defined(INET6) */
364 lck_rw_done(pcbinfo
->mtx
);
370 inp
->inp_laddr
= sin
->sin_addr
;
376 inp
->inp_flags
|= INP_ANONPORT
;
378 if (inp
->inp_flags
& INP_HIGHPORT
) {
379 first
= ipport_hifirstauto
; /* sysctl */
380 last
= ipport_hilastauto
;
381 lastport
= &pcbinfo
->lasthi
;
382 } else if (inp
->inp_flags
& INP_LOWPORT
) {
383 if (p
&& (error
= proc_suser(p
))) {
384 lck_rw_done(pcbinfo
->mtx
);
388 first
= ipport_lowfirstauto
; /* 1023 */
389 last
= ipport_lowlastauto
; /* 600 */
390 lastport
= &pcbinfo
->lastlow
;
392 first
= ipport_firstauto
; /* sysctl */
393 last
= ipport_lastauto
;
394 lastport
= &pcbinfo
->lastport
;
397 * Simple check to ensure all ports are not used up causing
400 * We split the two cases (up and down) so that the direction
401 * is not being tested on each round of the loop.
407 count
= first
- last
;
410 if (count
-- < 0) { /* completely used? */
411 lck_rw_done(pcbinfo
->mtx
);
413 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
414 return (EADDRNOTAVAIL
);
417 if (*lastport
> first
|| *lastport
< last
)
419 lport
= htons(*lastport
);
420 } while (in_pcblookup_local(pcbinfo
,
421 inp
->inp_laddr
, lport
, wild
));
426 count
= last
- first
;
429 if (count
-- < 0) { /* completely used? */
430 lck_rw_done(pcbinfo
->mtx
);
432 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
433 return (EADDRNOTAVAIL
);
436 if (*lastport
< first
|| *lastport
> last
)
438 lport
= htons(*lastport
);
439 } while (in_pcblookup_local(pcbinfo
,
440 inp
->inp_laddr
, lport
, wild
));
444 inp
->inp_lport
= lport
;
445 if (in_pcbinshash(inp
, 1) != 0) {
446 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
448 lck_rw_done(pcbinfo
->mtx
);
451 lck_rw_done(pcbinfo
->mtx
);
456 * Transform old in_pcbconnect() into an inner subroutine for new
457 * in_pcbconnect(): Do some validity-checking on the remote
458 * address (in mbuf 'nam') and then determine local host address
459 * (i.e., which interface) to use to access that remote host.
461 * This preserves definition of in_pcbconnect(), while supporting a
462 * slightly different version for T/TCP. (This is more than
463 * a bit of a kludge, but cleaning up the internal interfaces would
464 * have forced minor changes in every protocol).
468 in_pcbladdr(inp
, nam
, plocal_sin
)
469 register struct inpcb
*inp
;
470 struct sockaddr
*nam
;
471 struct sockaddr_in
**plocal_sin
;
473 struct in_ifaddr
*ia
;
474 register struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
476 if (nam
->sa_len
!= sizeof (*sin
))
478 if (sin
->sin_family
!= AF_INET
)
479 return (EAFNOSUPPORT
);
480 if (sin
->sin_port
== 0)
481 return (EADDRNOTAVAIL
);
482 lck_mtx_lock(rt_mtx
);
483 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
485 * If the destination address is INADDR_ANY,
486 * use the primary local address.
487 * If the supplied address is INADDR_BROADCAST,
488 * and the primary interface supports broadcast,
489 * choose the broadcast address for that interface.
491 #define satosin(sa) ((struct sockaddr_in *)(sa))
492 #define sintosa(sin) ((struct sockaddr *)(sin))
493 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
494 if (sin
->sin_addr
.s_addr
== INADDR_ANY
)
495 sin
->sin_addr
= IA_SIN(TAILQ_FIRST(&in_ifaddrhead
))->sin_addr
;
496 else if (sin
->sin_addr
.s_addr
== (u_long
)INADDR_BROADCAST
&&
497 (TAILQ_FIRST(&in_ifaddrhead
)->ia_ifp
->if_flags
& IFF_BROADCAST
))
498 sin
->sin_addr
= satosin(&TAILQ_FIRST(&in_ifaddrhead
)->ia_broadaddr
)->sin_addr
;
500 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
501 register struct route
*ro
;
503 ia
= (struct in_ifaddr
*)0;
505 * If route is known or can be allocated now,
506 * our src addr is taken from the i/f, else punt.
507 * Note that we should check the address family of the cached
508 * destination, in case of sharing the cache with IPv6.
510 ro
= &inp
->inp_route
;
512 (ro
->ro_dst
.sa_family
!= AF_INET
||
513 satosin(&ro
->ro_dst
)->sin_addr
.s_addr
!=
514 sin
->sin_addr
.s_addr
||
515 inp
->inp_socket
->so_options
& SO_DONTROUTE
||
516 ro
->ro_rt
->generation_id
!= route_generation
)) {
517 rtfree_locked(ro
->ro_rt
);
518 ro
->ro_rt
= (struct rtentry
*)0;
520 if ((inp
->inp_socket
->so_options
& SO_DONTROUTE
) == 0 && /*XXX*/
521 (ro
->ro_rt
== (struct rtentry
*)0 ||
522 ro
->ro_rt
->rt_ifp
== 0)) {
523 /* No route yet, so try to acquire one */
524 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in
));
525 ro
->ro_dst
.sa_family
= AF_INET
;
526 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
527 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
529 rtalloc_ign_locked(ro
, 0UL);
532 * If we found a route, use the address
533 * corresponding to the outgoing interface
534 * unless it is the loopback (in case a route
535 * to our address on another net goes to loopback).
537 if (ro
->ro_rt
&& !(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
538 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
543 u_short fport
= sin
->sin_port
;
546 ia
= ifatoia(ifa_ifwithdstaddr(sintosa(sin
)));
548 ia
= ifatoia(ifa_ifwithnet(sintosa(sin
)));
550 sin
->sin_port
= fport
;
552 ia
= TAILQ_FIRST(&in_ifaddrhead
);
557 lck_mtx_unlock(rt_mtx
);
558 return (EADDRNOTAVAIL
);
562 * If the destination address is multicast and an outgoing
563 * interface has been set as a multicast option, use the
564 * address of that interface as our source address.
566 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)) &&
567 inp
->inp_moptions
!= NULL
) {
568 struct ip_moptions
*imo
;
571 imo
= inp
->inp_moptions
;
572 if (imo
->imo_multicast_ifp
!= NULL
&& (ia
== NULL
||
573 ia
->ia_ifp
!= imo
->imo_multicast_ifp
)) {
574 ifp
= imo
->imo_multicast_ifp
;
576 ifafree(&ia
->ia_ifa
);
577 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
578 if (ia
->ia_ifp
== ifp
)
581 lck_mtx_unlock(rt_mtx
);
582 return (EADDRNOTAVAIL
);
588 * Don't do pcblookup call here; return interface in plocal_sin
589 * and exit to caller, that will do the lookup.
591 *plocal_sin
= &ia
->ia_addr
;
592 ifafree(&ia
->ia_ifa
);
594 lck_mtx_unlock(rt_mtx
);
600 * Connect from a socket to a specified address.
601 * Both address and port must be specified in argument sin.
602 * If don't have a local address for this socket yet,
606 in_pcbconnect(inp
, nam
, p
)
607 register struct inpcb
*inp
;
608 struct sockaddr
*nam
;
611 struct sockaddr_in
*ifaddr
;
612 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
617 * Call inner routine, to assign local interface address.
619 if ((error
= in_pcbladdr(inp
, nam
, &ifaddr
)) != 0)
622 socket_unlock(inp
->inp_socket
, 0);
623 pcb
= in_pcblookup_hash(inp
->inp_pcbinfo
, sin
->sin_addr
, sin
->sin_port
,
624 inp
->inp_laddr
.s_addr
? inp
->inp_laddr
: ifaddr
->sin_addr
,
625 inp
->inp_lport
, 0, NULL
);
626 socket_lock(inp
->inp_socket
, 0);
628 in_pcb_checkstate(pcb
, WNT_RELEASE
, 0);
631 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
632 if (inp
->inp_lport
== 0) {
633 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
637 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
638 /*lock inversion issue, mostly with udp multicast packets */
639 socket_unlock(inp
->inp_socket
, 0);
640 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
641 socket_lock(inp
->inp_socket
, 0);
643 inp
->inp_laddr
= ifaddr
->sin_addr
;
644 inp
->inp_flags
|= INP_INADDR_ANY
;
647 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
648 /*lock inversion issue, mostly with udp multicast packets */
649 socket_unlock(inp
->inp_socket
, 0);
650 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
651 socket_lock(inp
->inp_socket
, 0);
654 inp
->inp_faddr
= sin
->sin_addr
;
655 inp
->inp_fport
= sin
->sin_port
;
657 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
662 in_pcbdisconnect(inp
)
666 inp
->inp_faddr
.s_addr
= INADDR_ANY
;
669 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
670 /*lock inversion issue, mostly with udp multicast packets */
671 socket_unlock(inp
->inp_socket
, 0);
672 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
673 socket_lock(inp
->inp_socket
, 0);
677 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
679 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
687 struct socket
*so
= inp
->inp_socket
;
688 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
690 if (so
->so_pcb
== 0) { /* we've been called twice */
691 panic("in_pcbdetach: inp=%x so=%x proto=%x so_pcb is null!\n",
692 inp
, so
, so
->so_proto
->pr_protocol
);
696 if (ipsec_bypass
== 0) {
697 lck_mtx_lock(sadb_mutex
);
698 ipsec4_delete_pcbpolicy(inp
);
699 lck_mtx_unlock(sadb_mutex
);
703 /* mark socket state as dead */
704 if (in_pcb_checkstate(inp
, WNT_STOPUSING
, 1) != WNT_STOPUSING
)
705 panic("in_pcbdetach so=%x prot=%x couldn't set to STOPUSING\n", so
, so
->so_proto
->pr_protocol
);
708 if (so
->cached_in_sock_layer
)
709 printf("in_pcbdetach for cached socket %x flags=%x\n", so
, so
->so_flags
);
711 printf("in_pcbdetach for allocated socket %x flags=%x\n", so
, so
->so_flags
);
713 if ((so
->so_flags
& SOF_PCBCLEARING
) == 0) {
715 if (inp
->inp_options
)
716 (void)m_free(inp
->inp_options
);
719 * route deletion requires reference count to be <= zero
721 lck_mtx_lock(rt_mtx
);
722 if ((rt
->rt_flags
& RTF_DELCLONE
) &&
723 (rt
->rt_flags
& RTF_WASCLONED
) &&
724 (rt
->rt_refcnt
<= 1)) {
726 rt
->rt_flags
&= ~RTF_UP
;
727 rtrequest_locked(RTM_DELETE
, rt_key(rt
),
728 rt
->rt_gateway
, rt_mask(rt
),
729 rt
->rt_flags
, (struct rtentry
**)0);
733 inp
->inp_route
.ro_rt
= 0;
735 lck_mtx_unlock(rt_mtx
);
737 ip_freemoptions(inp
->inp_moptions
);
738 inp
->inp_moptions
= NULL
;
739 sofreelastref(so
, 0);
740 inp
->inp_state
= INPCB_STATE_DEAD
;
741 so
->so_flags
|= SOF_PCBCLEARING
; /* makes sure we're not called twice from so_close */
750 struct socket
*so
= inp
->inp_socket
;
751 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
754 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
755 printf("in_pcbdispose: not dead yet? so=%x\n", so
);
759 if (so
&& so
->so_usecount
!= 0)
760 panic("in_pcbdispose: use count=%x so=%x\n", so
->so_usecount
, so
);
763 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
764 /*### access ipi in in_pcbremlists */
768 if (so
->so_proto
->pr_flags
& PR_PCBLOCK
) {
769 sofreelastref(so
, 0);
770 if (so
->so_rcv
.sb_cc
|| so
->so_snd
.sb_cc
) {
772 printf("in_pcbdispose sb not cleaned up so=%x rc_cci=%x snd_cc=%x\n",
773 so
, so
->so_rcv
.sb_cc
, so
->so_snd
.sb_cc
);
775 sbrelease(&so
->so_rcv
);
776 sbrelease(&so
->so_snd
);
778 if (so
->so_head
!= NULL
)
779 panic("in_pcbdispose, so=%x head still exist\n", so
);
780 lck_mtx_unlock(inp
->inpcb_mtx
);
781 lck_mtx_free(inp
->inpcb_mtx
, ipi
->mtx_grp
);
783 so
->so_flags
|= SOF_PCBCLEARING
; /* makes sure we're not called twice from so_close */
784 so
->so_saved_pcb
= (caddr_t
) inp
;
787 inp
->reserved
[0] = so
;
788 if (so
->cached_in_sock_layer
== 0) {
789 zfree(ipi
->ipi_zone
, inp
);
795 printf("in_pcbdispose: no socket for inp=%x\n", inp
);
800 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
801 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
802 * in struct pr_usrreqs, so that protocols can just reference then directly
803 * without the need for a wrapper function. The socket must have a valid
804 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
805 * except through a kernel programming error, so it is acceptable to panic
806 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
807 * because there actually /is/ a programming error somewhere... XXX)
810 in_setsockaddr(so
, nam
)
812 struct sockaddr
**nam
;
814 register struct inpcb
*inp
;
815 register struct sockaddr_in
*sin
;
818 * Do the malloc first in case it blocks.
820 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
823 bzero(sin
, sizeof *sin
);
824 sin
->sin_family
= AF_INET
;
825 sin
->sin_len
= sizeof(*sin
);
832 sin
->sin_port
= inp
->inp_lport
;
833 sin
->sin_addr
= inp
->inp_laddr
;
835 *nam
= (struct sockaddr
*)sin
;
840 in_setpeeraddr(so
, nam
)
842 struct sockaddr
**nam
;
845 register struct sockaddr_in
*sin
;
848 * Do the malloc first in case it blocks.
850 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
853 bzero((caddr_t
)sin
, sizeof (*sin
));
854 sin
->sin_family
= AF_INET
;
855 sin
->sin_len
= sizeof(*sin
);
862 sin
->sin_port
= inp
->inp_fport
;
863 sin
->sin_addr
= inp
->inp_faddr
;
865 *nam
= (struct sockaddr
*)sin
;
870 in_pcbnotifyall(pcbinfo
, faddr
, errno
, notify
)
871 struct inpcbinfo
*pcbinfo
;
872 struct in_addr faddr
;
873 void (*notify
) (struct inpcb
*, int);
877 lck_rw_lock_shared(pcbinfo
->mtx
);
879 LIST_FOREACH(inp
, pcbinfo
->listhead
, inp_list
) {
881 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
884 if (inp
->inp_faddr
.s_addr
!= faddr
.s_addr
||
885 inp
->inp_socket
== NULL
)
887 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
889 socket_lock(inp
->inp_socket
, 1);
890 (*notify
)(inp
, errno
);
891 (void)in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
892 socket_unlock(inp
->inp_socket
, 1);
894 lck_rw_done(pcbinfo
->mtx
);
903 struct ip_moptions
*imo
;
906 for (inp
= head
; inp
!= NULL
; inp
= LIST_NEXT(inp
, inp_list
)) {
907 imo
= inp
->inp_moptions
;
908 if ((inp
->inp_vflag
& INP_IPV4
) &&
911 * Unselect the outgoing interface if it is being
914 if (imo
->imo_multicast_ifp
== ifp
)
915 imo
->imo_multicast_ifp
= NULL
;
918 * Drop multicast group membership if we joined
919 * through the interface being detached.
921 for (i
= 0, gap
= 0; i
< imo
->imo_num_memberships
;
923 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
) {
924 in_delmulti(&imo
->imo_membership
[i
]);
927 imo
->imo_membership
[i
- gap
] =
928 imo
->imo_membership
[i
];
930 imo
->imo_num_memberships
-= gap
;
936 * Check for alternatives when higher level complains
937 * about service problems. For now, invalidate cached
938 * routing information. If the route was created dynamically
939 * (by a redirect), time to try a default gateway again.
945 register struct rtentry
*rt
;
946 struct rt_addrinfo info
;
948 if ((rt
= inp
->inp_route
.ro_rt
)) {
949 lck_mtx_lock(rt_mtx
);
950 bzero((caddr_t
)&info
, sizeof(info
));
951 info
.rti_info
[RTAX_DST
] =
952 (struct sockaddr
*)&inp
->inp_route
.ro_dst
;
953 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
954 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
955 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
956 if (rt
->rt_flags
& RTF_DYNAMIC
)
957 (void) rtrequest_locked(RTM_DELETE
, rt_key(rt
),
958 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
959 (struct rtentry
**)0);
960 inp
->inp_route
.ro_rt
= 0;
962 lck_mtx_unlock(rt_mtx
);
964 * A new route can be allocated
965 * the next time output is attempted.
971 * After a routing change, flush old routing
972 * and allocate a (hopefully) better one.
975 in_rtchange(inp
, errno
)
976 register struct inpcb
*inp
;
979 if (inp
->inp_route
.ro_rt
) {
980 if ((ifa_foraddr(inp
->inp_laddr
.s_addr
)) == 0)
981 return; /* we can't remove the route now. not sure if still ok to use src */
982 rtfree(inp
->inp_route
.ro_rt
);
983 inp
->inp_route
.ro_rt
= 0;
985 * A new route can be allocated the next time
986 * output is attempted.
992 * Lookup a PCB based on the local address and port.
995 in_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
996 struct inpcbinfo
*pcbinfo
;
997 struct in_addr laddr
;
1001 register struct inpcb
*inp
;
1002 int matchwild
= 3, wildcard
;
1003 u_short lport
= lport_arg
;
1005 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_START
, 0,0,0,0,0);
1008 struct inpcbhead
*head
;
1010 * Look for an unconnected (wildcard foreign addr) PCB that
1011 * matches the local address and port we're looking for.
1013 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
1014 LIST_FOREACH(inp
, head
, inp_hash
) {
1016 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
1019 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
1020 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1021 inp
->inp_lport
== lport
) {
1031 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, 0,0,0,0,0);
1034 struct inpcbporthead
*porthash
;
1035 struct inpcbport
*phd
;
1036 struct inpcb
*match
= NULL
;
1038 * Best fit PCB lookup.
1040 * First see if this local port is in use by looking on the
1043 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1044 pcbinfo
->porthashmask
)];
1045 LIST_FOREACH(phd
, porthash
, phd_hash
) {
1046 if (phd
->phd_port
== lport
)
1051 * Port is in use by one or more PCBs. Look for best
1054 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
1057 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
1060 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
1062 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
1063 if (laddr
.s_addr
== INADDR_ANY
)
1065 else if (inp
->inp_laddr
.s_addr
!= laddr
.s_addr
)
1068 if (laddr
.s_addr
!= INADDR_ANY
)
1071 if (wildcard
< matchwild
) {
1073 matchwild
= wildcard
;
1074 if (matchwild
== 0) {
1080 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, match
,0,0,0,0);
1086 * Lookup PCB in hash list.
1090 struct inpcbinfo
*pcbinfo
,
1091 struct in_addr faddr
,
1093 struct in_addr laddr
,
1098 struct inpcbhead
*head
;
1099 register struct inpcb
*inp
;
1100 u_short fport
= fport_arg
, lport
= lport_arg
;
1103 * We may have found the pcb in the last lookup - check this first.
1106 lck_rw_lock_shared(pcbinfo
->mtx
);
1109 * First look for an exact match.
1111 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
1112 LIST_FOREACH(inp
, head
, inp_hash
) {
1114 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
1117 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
1118 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1119 inp
->inp_fport
== fport
&&
1120 inp
->inp_lport
== lport
) {
1124 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1125 lck_rw_done(pcbinfo
->mtx
);
1128 else { /* it's there but dead, say it isn't found */
1129 lck_rw_done(pcbinfo
->mtx
);
1135 struct inpcb
*local_wild
= NULL
;
1137 struct inpcb
*local_wild_mapped
= NULL
;
1140 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
1141 LIST_FOREACH(inp
, head
, inp_hash
) {
1143 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
1146 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
1147 inp
->inp_lport
== lport
) {
1148 #if defined(NFAITH) && NFAITH > 0
1149 if (ifp
&& ifp
->if_type
== IFT_FAITH
&&
1150 (inp
->inp_flags
& INP_FAITH
) == 0)
1153 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
1154 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1155 lck_rw_done(pcbinfo
->mtx
);
1158 else { /* it's there but dead, say it isn't found */
1159 lck_rw_done(pcbinfo
->mtx
);
1163 else if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1165 if (INP_CHECK_SOCKAF(inp
->inp_socket
,
1167 local_wild_mapped
= inp
;
1169 #endif /* defined(INET6) */
1175 if (local_wild
== NULL
) {
1176 if (local_wild_mapped
!= NULL
) {
1177 if (in_pcb_checkstate(local_wild_mapped
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1178 lck_rw_done(pcbinfo
->mtx
);
1179 return (local_wild_mapped
);
1181 else { /* it's there but dead, say it isn't found */
1182 lck_rw_done(pcbinfo
->mtx
);
1186 lck_rw_done(pcbinfo
->mtx
);
1189 #endif /* defined(INET6) */
1190 if (in_pcb_checkstate(local_wild
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
1191 lck_rw_done(pcbinfo
->mtx
);
1192 return (local_wild
);
1194 else { /* it's there but dead, say it isn't found */
1195 lck_rw_done(pcbinfo
->mtx
);
1203 lck_rw_done(pcbinfo
->mtx
);
1208 * Insert PCB onto various hash lists.
1211 in_pcbinshash(inp
, locked
)
1213 int locked
; /* list already locked exclusive */
1215 struct inpcbhead
*pcbhash
;
1216 struct inpcbporthead
*pcbporthash
;
1217 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1218 struct inpcbport
*phd
;
1219 u_int32_t hashkey_faddr
;
1222 if (inp
->inp_vflag
& INP_IPV6
)
1223 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1226 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1228 inp
->hash_element
= INP_PCBHASH(hashkey_faddr
, inp
->inp_lport
, inp
->inp_fport
, pcbinfo
->hashmask
);
1231 if (!lck_rw_try_lock_exclusive(pcbinfo
->mtx
)) {
1232 /*lock inversion issue, mostly with udp multicast packets */
1233 socket_unlock(inp
->inp_socket
, 0);
1234 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1235 socket_lock(inp
->inp_socket
, 0);
1239 pcbhash
= &pcbinfo
->hashbase
[inp
->hash_element
];
1241 pcbporthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(inp
->inp_lport
,
1242 pcbinfo
->porthashmask
)];
1245 * Go through port list and look for a head for this lport.
1247 LIST_FOREACH(phd
, pcbporthash
, phd_hash
) {
1248 if (phd
->phd_port
== inp
->inp_lport
)
1252 * If none exists, malloc one and tack it on.
1255 MALLOC(phd
, struct inpcbport
*, sizeof(struct inpcbport
), M_PCB
, M_WAITOK
);
1258 lck_rw_done(pcbinfo
->mtx
);
1259 return (ENOBUFS
); /* XXX */
1261 phd
->phd_port
= inp
->inp_lport
;
1262 LIST_INIT(&phd
->phd_pcblist
);
1263 LIST_INSERT_HEAD(pcbporthash
, phd
, phd_hash
);
1266 LIST_INSERT_HEAD(&phd
->phd_pcblist
, inp
, inp_portlist
);
1267 LIST_INSERT_HEAD(pcbhash
, inp
, inp_hash
);
1269 lck_rw_done(pcbinfo
->mtx
);
1274 * Move PCB to the proper hash bucket when { faddr, fport } have been
1275 * changed. NOTE: This does not handle the case of the lport changing (the
1276 * hashed port list would have to be updated as well), so the lport must
1277 * not change after in_pcbinshash() has been called.
1283 struct inpcbhead
*head
;
1284 u_int32_t hashkey_faddr
;
1287 if (inp
->inp_vflag
& INP_IPV6
)
1288 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1291 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1292 inp
->hash_element
= INP_PCBHASH(hashkey_faddr
, inp
->inp_lport
,
1293 inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
);
1294 head
= &inp
->inp_pcbinfo
->hashbase
[inp
->hash_element
];
1296 LIST_REMOVE(inp
, inp_hash
);
1297 LIST_INSERT_HEAD(head
, inp
, inp_hash
);
1301 * Remove PCB from various lists.
1303 //###LOCK must be called with list lock held
1308 inp
->inp_gencnt
= ++inp
->inp_pcbinfo
->ipi_gencnt
;
1310 if (inp
->inp_lport
) {
1311 struct inpcbport
*phd
= inp
->inp_phd
;
1313 LIST_REMOVE(inp
, inp_hash
);
1314 LIST_REMOVE(inp
, inp_portlist
);
1315 if (phd
!= NULL
&& (LIST_FIRST(&phd
->phd_pcblist
) == NULL
)) {
1316 LIST_REMOVE(phd
, phd_hash
);
1320 LIST_REMOVE(inp
, inp_list
);
1321 inp
->inp_pcbinfo
->ipi_count
--;
1324 static void in_pcb_detach_port( struct inpcb
*inp
);
1326 in_pcb_grab_port (struct inpcbinfo
*pcbinfo
,
1328 struct in_addr laddr
,
1330 struct in_addr faddr
,
1335 struct inpcb
*inp
, *pcb
;
1336 struct sockaddr_in sin
;
1337 struct proc
*p
= current_proc();
1341 pcbinfo
->nat_dummy_socket
.so_pcb
= 0;
1342 pcbinfo
->nat_dummy_socket
.so_options
= 0;
1344 /* The grabber wants a particular port */
1346 if (faddr
.s_addr
|| fport
) {
1348 * This is either the second half of an active connect, or
1349 * it's from the acceptance of an incoming connection.
1351 if (laddr
.s_addr
== 0) {
1352 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1356 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
, laddr
, *lport
, 0, NULL
);
1358 /* pcb was found, its count was upped. need to decrease it here */
1359 in_pcb_checkstate(inp
, WNT_RELEASE
, 0);
1360 if (!(IN_MULTICAST(ntohl(laddr
.s_addr
)))) {
1361 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1362 return (EADDRINUSE
);
1366 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1368 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1371 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1372 pcb
->inp_vflag
|= INP_IPV4
;
1374 pcb
->inp_lport
= *lport
;
1375 pcb
->inp_laddr
.s_addr
= laddr
.s_addr
;
1377 pcb
->inp_faddr
= faddr
;
1378 pcb
->inp_fport
= fport
;
1380 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1381 in_pcbinshash(pcb
, 1);
1382 lck_rw_done(pcbinfo
->mtx
);
1386 * This is either a bind for a passive socket, or it's the
1387 * first part of bind-connect sequence (not likely since an
1388 * ephemeral port is usually used in this case). Or, it's
1389 * the result of a connection acceptance when the foreign
1390 * address/port cannot be provided (which requires the SO_REUSEADDR
1391 * flag if laddr is not multicast).
1394 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1396 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1399 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1400 pcb
->inp_vflag
|= INP_IPV4
;
1402 pcbinfo
->nat_dummy_socket
.so_options
= options
;
1403 bzero(&sin
, sizeof(struct sockaddr_in
));
1404 sin
.sin_len
= sizeof(struct sockaddr_in
);
1405 sin
.sin_family
= AF_INET
;
1406 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1407 sin
.sin_port
= *lport
;
1409 socket_lock(&pcbinfo
->nat_dummy_socket
, 1);
1410 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1411 (struct sockaddr
*) &sin
, p
);
1413 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1); /*detach first */
1414 in_pcb_detach_port(pcb
); /* will restore dummy pcb */
1417 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1421 /* The grabber wants an ephemeral port */
1423 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1425 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
;
1428 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1429 pcb
->inp_vflag
|= INP_IPV4
;
1431 bzero(&sin
, sizeof(struct sockaddr_in
));
1432 sin
.sin_len
= sizeof(struct sockaddr_in
);
1433 sin
.sin_family
= AF_INET
;
1434 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1437 if (faddr
.s_addr
|| fport
) {
1439 * Not sure if this case will be used - could occur when connect
1440 * is called, skipping the bind.
1443 if (laddr
.s_addr
== 0) {
1444 in_pcb_detach_port(pcb
); /* restores dummy pcb */
1448 socket_lock(&pcbinfo
->nat_dummy_socket
, 1);
1449 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1450 (struct sockaddr
*) &sin
, p
);
1452 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1453 in_pcb_detach_port(pcb
); /* restores dummy pcb */
1457 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1458 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1459 pcb
->inp_laddr
, pcb
->inp_lport
, 0, NULL
);
1461 /* pcb was found, its count was upped. need to decrease it here */
1462 in_pcb_checkstate(inp
, WNT_RELEASE
, 0);
1463 in_pcb_detach_port(pcb
);
1464 return (EADDRINUSE
);
1467 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1468 pcb
->inp_faddr
= faddr
;
1469 pcb
->inp_fport
= fport
;
1471 lck_rw_done(pcbinfo
->mtx
);
1475 * This is a simple bind of an ephemeral port. The local addr
1476 * may or may not be defined.
1479 socket_lock(&pcbinfo
->nat_dummy_socket
, 1);
1480 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1481 (struct sockaddr
*) &sin
, p
);
1483 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1484 in_pcb_detach_port(pcb
);
1487 socket_unlock(&pcbinfo
->nat_dummy_socket
, 1);
1489 *lport
= pcb
->inp_lport
;
1493 pcb
->nat_owner
= owner_id
;
1494 pcb
->nat_cookie
= cookie
;
1495 pcb
->inp_ppcb
= (caddr_t
) pcbinfo
->dummy_cb
;
1496 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
; /* restores dummypcb */
1500 /* 3962035 - in_pcb_letgo_port needs a special case function for detaching */
1505 struct socket
*so
= inp
->inp_socket
;
1506 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1508 if (so
!= &pcbinfo
->nat_dummy_socket
)
1509 panic("in_pcb_detach_port: not a dummy_sock: so=%x, inp=%x\n", so
, inp
);
1510 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
1511 /*### access ipi in in_pcbremlists */
1512 in_pcbremlists(inp
);
1514 inp
->inp_socket
= 0;
1515 inp
->reserved
[0] = so
;
1516 zfree(pcbinfo
->ipi_zone
, inp
);
1517 pcbinfo
->nat_dummy_socket
.so_pcb
= (caddr_t
)pcbinfo
->nat_dummy_pcb
; /* restores dummypcb */
1521 in_pcb_letgo_port(struct inpcbinfo
*pcbinfo
, struct in_addr laddr
, u_short lport
,
1522 struct in_addr faddr
, u_short fport
, u_char owner_id
)
1524 struct inpcbhead
*head
;
1525 register struct inpcb
*inp
;
1529 * First look for an exact match.
1532 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1533 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
1534 for (inp
= head
->lh_first
; inp
!= NULL
; inp
= inp
->inp_hash
.le_next
) {
1535 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
1536 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1537 inp
->inp_fport
== fport
&&
1538 inp
->inp_lport
== lport
&&
1539 inp
->nat_owner
== owner_id
) {
1543 in_pcb_detach_port(inp
);
1544 lck_rw_done(pcbinfo
->mtx
);
1549 lck_rw_done(pcbinfo
->mtx
);
1554 in_pcb_get_owner(struct inpcbinfo
*pcbinfo
,
1555 struct in_addr laddr
, u_short lport
,
1556 struct in_addr faddr
, u_short fport
,
1561 u_char owner_id
= INPCB_NO_OWNER
;
1562 struct inpcbport
*phd
;
1563 struct inpcbporthead
*porthash
;
1566 if (IN_MULTICAST(laddr
.s_addr
)) {
1568 * Walk through PCB's looking for registered
1572 lck_rw_lock_shared(pcbinfo
->mtx
);
1573 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1574 pcbinfo
->porthashmask
)];
1575 for (phd
= porthash
->lh_first
; phd
!= NULL
; phd
= phd
->phd_hash
.le_next
) {
1576 if (phd
->phd_port
== lport
)
1581 lck_rw_done(pcbinfo
->mtx
);
1582 return INPCB_NO_OWNER
;
1585 owner_id
= INPCB_NO_OWNER
;
1586 for (inp
= phd
->phd_pcblist
.lh_first
; inp
!= NULL
;
1587 inp
= inp
->inp_portlist
.le_next
) {
1589 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
1590 if (inp
->nat_owner
== 0)
1591 owner_id
|= INPCB_OWNED_BY_X
;
1593 owner_id
|= inp
->nat_owner
;
1597 lck_rw_done(pcbinfo
->mtx
);
1601 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1602 laddr
, lport
, 1, NULL
);
1604 /* pcb was found, its count was upped. need to decrease it here */
1605 /* if we found it, that pcb is already locked by the caller */
1606 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
)
1607 return(INPCB_NO_OWNER
);
1609 if (inp
->nat_owner
) {
1610 owner_id
= inp
->nat_owner
;
1611 *cookie
= inp
->nat_cookie
;
1614 owner_id
= INPCB_OWNED_BY_X
;
1618 owner_id
= INPCB_NO_OWNER
;
1625 in_pcb_new_share_client(struct inpcbinfo
*pcbinfo
, u_char
*owner_id
)
1631 for (i
=0; i
< INPCB_MAX_IDS
; i
++) {
1632 if ((pcbinfo
->all_owners
& (1 << i
)) == 0) {
1633 pcbinfo
->all_owners
|= (1 << i
);
1634 *owner_id
= (1 << i
);
1643 in_pcb_rem_share_client(struct inpcbinfo
*pcbinfo
, u_char owner_id
)
1648 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1649 if (pcbinfo
->all_owners
& owner_id
) {
1650 pcbinfo
->all_owners
&= ~owner_id
;
1651 for (inp
= pcbinfo
->listhead
->lh_first
; inp
!= NULL
; inp
= inp
->inp_list
.le_next
) {
1652 if (inp
->nat_owner
& owner_id
) {
1653 if (inp
->nat_owner
== owner_id
)
1655 * Deallocate the pcb
1657 in_pcb_detach_port(inp
);
1659 inp
->nat_owner
&= ~owner_id
;
1664 lck_rw_done(pcbinfo
->mtx
);
1668 lck_rw_done(pcbinfo
->mtx
);
1674 void in_pcb_nat_init(struct inpcbinfo
*pcbinfo
, int afamily
,
1675 int pfamily
, int protocol
)
1678 struct proc
*p
= current_proc();
1680 bzero(&pcbinfo
->nat_dummy_socket
, sizeof(struct socket
));
1681 pcbinfo
->nat_dummy_socket
.so_proto
= pffindproto_locked(afamily
, pfamily
, protocol
);
1682 pcbinfo
->all_owners
= 0;
1683 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1685 panic("in_pcb_nat_init: can't alloc fakepcb err=%\n", stat
);
1686 pcbinfo
->nat_dummy_pcb
= pcbinfo
->nat_dummy_socket
.so_pcb
;
1689 /* Mechanism used to defer the memory release of PCBs
1690 * The pcb list will contain the pcb until the ripper can clean it up if
1691 * the following conditions are met: 1) state "DEAD", 2) wantcnt is STOPUSING
1692 * 3) usecount is null
1693 * This function will be called to either mark the pcb as
1696 in_pcb_checkstate(struct inpcb
*pcb
, int mode
, int locked
)
1700 volatile UInt32
*wantcnt
= (volatile UInt32
*)&pcb
->inp_wantcnt
;
1706 case WNT_STOPUSING
: /* try to mark the pcb as ready for recycling */
1708 /* compareswap with STOPUSING, if success we're good, if it's in use, will be marked later */
1711 socket_lock(pcb
->inp_socket
, 1);
1712 pcb
->inp_state
= INPCB_STATE_DEAD
;
1714 if (pcb
->inp_socket
->so_usecount
< 0)
1715 panic("in_pcb_checkstate STOP pcb=%x so=%x usecount is negative\n", pcb
, pcb
->inp_socket
);
1717 socket_unlock(pcb
->inp_socket
, 1);
1719 origwant
= *wantcnt
;
1720 if ((UInt16
) origwant
== 0xffff ) /* should stop using */
1721 return (WNT_STOPUSING
);
1723 if ((UInt16
) origwant
== 0) {/* try to mark it as unsuable now */
1724 OSCompareAndSwap(origwant
, newwant
, (UInt32
*) wantcnt
) ;
1726 return (WNT_STOPUSING
);
1729 case WNT_ACQUIRE
: /* try to increase reference to pcb */
1730 /* if WNT_STOPUSING should bail out */
1732 * if socket state DEAD, try to set count to STOPUSING, return failed
1733 * otherwise increase cnt
1736 origwant
= *wantcnt
;
1737 if ((UInt16
) origwant
== 0xffff ) {/* should stop using */
1738 // printf("in_pcb_checkstate: ACQ PCB was STOPUSING while release. odd pcb=%x\n", pcb);
1739 return (WNT_STOPUSING
);
1741 newwant
= origwant
+ 1;
1742 } while (!OSCompareAndSwap(origwant
, newwant
, (UInt32
*) wantcnt
));
1743 return (WNT_ACQUIRE
);
1746 case WNT_RELEASE
: /* release reference. if result is null and pcb state is DEAD,
1747 set wanted bit to STOPUSING
1751 socket_lock(pcb
->inp_socket
, 1);
1754 origwant
= *wantcnt
;
1755 if ((UInt16
) origwant
== 0x0 )
1756 panic("in_pcb_checkstate pcb=%x release with zero count", pcb
);
1757 if ((UInt16
) origwant
== 0xffff ) {/* should stop using */
1759 printf("in_pcb_checkstate: REL PCB was STOPUSING while release. odd pcb=%x\n", pcb
);
1762 socket_unlock(pcb
->inp_socket
, 1);
1763 return (WNT_STOPUSING
);
1765 newwant
= origwant
- 1;
1766 } while (!OSCompareAndSwap(origwant
, newwant
, (UInt32
*) wantcnt
));
1768 if (pcb
->inp_state
== INPCB_STATE_DEAD
)
1770 if (pcb
->inp_socket
->so_usecount
< 0)
1771 panic("in_pcb_checkstate RELEASE pcb=%x so=%x usecount is negative\n", pcb
, pcb
->inp_socket
);
1774 socket_unlock(pcb
->inp_socket
, 1);
1775 return (WNT_RELEASE
);
1780 panic("in_pcb_checkstate: so=%x not a valid state =%x\n", pcb
->inp_socket
, mode
);
1788 * inpcb_to_compat copies specific bits of an inpcb to a inpcb_compat.
1789 * The inpcb_compat data structure is passed to user space and must
1790 * not change. We intentionally avoid copying pointers. The socket is
1791 * the one exception, though we probably shouldn't copy that either.
1796 struct inpcb_compat
*inp_compat
)
1798 bzero(inp_compat
, sizeof(*inp_compat
));
1799 inp_compat
->inp_fport
= inp
->inp_fport
;
1800 inp_compat
->inp_lport
= inp
->inp_lport
;
1801 inp_compat
->inp_socket
= inp
->inp_socket
;
1802 inp_compat
->nat_owner
= inp
->nat_owner
;
1803 inp_compat
->nat_cookie
= inp
->nat_cookie
;
1804 inp_compat
->inp_gencnt
= inp
->inp_gencnt
;
1805 inp_compat
->inp_flags
= inp
->inp_flags
;
1806 inp_compat
->inp_flow
= inp
->inp_flow
;
1807 inp_compat
->inp_vflag
= inp
->inp_vflag
;
1808 inp_compat
->inp_ip_ttl
= inp
->inp_ip_ttl
;
1809 inp_compat
->inp_ip_p
= inp
->inp_ip_p
;
1810 inp_compat
->inp_dependfaddr
.inp6_foreign
= inp
->inp_dependfaddr
.inp6_foreign
;
1811 inp_compat
->inp_dependladdr
.inp6_local
= inp
->inp_dependladdr
.inp6_local
;
1812 inp_compat
->inp_depend4
.inp4_ip_tos
= inp
->inp_depend4
.inp4_ip_tos
;
1813 inp_compat
->inp_depend6
.inp6_hlim
= inp
->inp_depend6
.inp6_hlim
;
1814 inp_compat
->inp_depend6
.inp6_cksum
= inp
->inp_depend6
.inp6_cksum
;
1815 inp_compat
->inp6_ifindex
= inp
->inp6_ifindex
;
1816 inp_compat
->inp_depend6
.inp6_hops
= inp
->inp_depend6
.inp6_hops
;
1820 prison_xinpcb(struct proc
*p
, struct inpcb
*inp
)
1824 if (ntohl(inp
->inp_laddr
.s_addr
) == p
->p_prison
->pr_ip
)