xref: /xnu-12377.81.4/bsd/netinet/in_pcb.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2000-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the 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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * Copyright (c) 1982, 1986, 1990, 1993
30  *	The Regents of the University of California.  All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
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. All advertising materials mentioning features or use of this software
41  *    must display the following acknowledgement:
42  *	This product includes software developed by the University of
43  *	California, Berkeley and its contributors.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
61  * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.32.2.4 2001/08/13 16:26:17 ume Exp $
62  */
63 /*
64  * NOTICE: This file was modified by SPARTA, Inc. in 2007 to introduce
65  * support for mandatory and extensible security protections.  This notice
66  * is included in support of clause 2.2 (b) of the Apple Public License,
67  * Version 2.0.
68  */
69 
70 #ifndef _NETINET_IN_PCB_H_
71 #define _NETINET_IN_PCB_H_
72 #include <sys/appleapiopts.h>
73 
74 #include <netinet/in.h>
75 #include <sys/socketvar.h>
76 #include <sys/types.h>
77 #include <sys/queue.h>
78 #ifdef BSD_KERNEL_PRIVATE
79 #include <sys/bitstring.h>
80 #include <sys/tree.h>
81 #include <kern/locks.h>
82 #include <kern/uipc_domain.h>
83 #include <kern/zalloc.h>
84 #include <netinet/in_stat.h>
85 #include <net/if_ports_used.h>
86 #endif /* BSD_KERNEL_PRIVATE */
87 #if !KERNEL
88 #include <TargetConditionals.h>
89 #endif
90 
91 #if IPSEC
92 #include <netinet6/ipsec.h> /* for IPSEC */
93 #endif /* IPSEC */
94 
95 #if NECP
96 #include <net/necp.h>
97 #endif
98 
99 #if SKYWALK
100 #include <skywalk/namespace/netns.h>
101 #endif /* SKYWALK */
102 
103 #ifdef BSD_KERNEL_PRIVATE
104 /*
105  * struct inpcb is the common protocol control block structure used in most
106  * IP transport protocols.
107  *
108  * Pointers to local and foreign host table entries, local and foreign socket
109  * numbers, and pointers up (to a socket structure) and down (to a
110  * protocol-specific control block) are stored here.
111  */
112 LIST_HEAD(inpcbhead, inpcb);
113 LIST_HEAD(inpcbporthead, inpcbport);
114 #endif /* BSD_KERNEL_PRIVATE */
115 typedef u_quad_t        inp_gen_t;
116 
117 /*
118  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
119  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
120  * the following structure.
121  */
122 struct in_addr_4in6 {
123 	u_int32_t       ia46_pad32[3];
124 	struct  in_addr ia46_addr4;
125 };
126 
127 #ifdef BSD_KERNEL_PRIVATE
128 /*
129  * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
130  * of the structure.  Therefore, it is important that the members in
131  * that position not contain any information which is required to be
132  * stable.
133  */
134 struct  icmp6_filter;
135 struct ifnet;
136 
137 typedef enum {
138 	stats_functional_type_untracked         = 0,    /* Deliberately ignored for detailed stats, e.g. loopback */
139 	stats_functional_type_cell              = 1,
140 	stats_functional_type_wifi_infra        = 2,
141 	stats_functional_type_wifi_non_infra    = 3,
142 	stats_functional_type_wired             = 4,
143 	stats_functional_type_bluetooth         = 5,
144 	stats_functional_type_unclassified      = 6,    /* Catch-all, appearance may need further investigation */
145 } stats_functional_type;
146 
147 struct inp_necp_attributes {
148 	char *inp_domain __null_terminated;
149 	char *inp_account __null_terminated;
150 	char *inp_domain_owner __null_terminated;
151 	char *inp_tracker_domain __null_terminated;
152 	char *inp_domain_context __null_terminated;
153 };
154 
155 /*
156  * struct inpcb captures the network layer state for TCP, UDP and raw IPv6
157  * and IPv6 sockets.  In the case of TCP, further per-connection state is
158  * hung off of inp_ppcb most of the time.
159  */
160 struct inpcb {
161 	decl_lck_mtx_data(, inpcb_mtx); /* inpcb per-socket mutex */
162 	LIST_ENTRY(inpcb) inp_hash;     /* hash list */
163 	LIST_ENTRY(inpcb) inp_list;     /* list for all PCBs of this proto */
164 	void    *inp_ppcb;              /* pointer to per-protocol pcb */
165 	struct inpcbinfo *inp_pcbinfo;  /* PCB list info */
166 	struct socket *inp_socket;      /* back pointer to socket */
167 	LIST_ENTRY(inpcb) inp_portlist; /* list for this PCB's local port */
168 	RB_ENTRY(inpcb) infc_link;      /* link for flowhash RB tree */
169 	struct inpcbport *inp_phd;      /* head of this list */
170 	inp_gen_t inp_gencnt;           /* generation count of this instance */
171 	int     inp_hash_element;       /* array index of pcb's hash list */
172 	int     inp_wantcnt;            /* wanted count; atomically updated */
173 	int     inp_state;              /* state (INUSE/CACHED/DEAD) */
174 	u_short inp_fport;              /* foreign port */
175 	u_short inp_lport;              /* local port */
176 	uint32_t inp_flags;            /* generic IP/datagram flags */
177 	uint32_t inp_flags2;           /* generic IP/datagram flags #2 */
178 	uint32_t inp_log_flags;
179 	uint32_t inp_flow;             /* IPv6 flow information */
180 	uint32_t inp_lifscope;          /* IPv6 scope ID of the local address */
181 	uint32_t inp_fifscope;          /* IPv6 scope ID of the foreign address */
182 
183 	uint32_t inp_sndingprog_waiters;/* waiters for outstanding send */
184 	u_char  inp_sndinprog_cnt;      /* outstanding send operations */
185 	u_char  inp_vflag;              /* INP_IPV4 or INP_IPV6 */
186 
187 	u_char inp_ip_ttl;              /* time to live proto */
188 	u_char inp_ip_p;                /* protocol proto */
189 
190 	struct ifnet *inp_boundifp;     /* interface for INP_BOUND_IF */
191 	struct ifnet *inp_last_outifp;  /* last known outgoing interface */
192 	uint32_t inp_flowhash;         /* flow hash */
193 
194 	/* Protocol-dependent part */
195 	union {
196 		/* foreign host table entry */
197 		struct in_addr_4in6 inp46_foreign;
198 		struct in6_addr inp6_foreign;
199 	} inp_dependfaddr;
200 	union {
201 		/* local host table entry */
202 		struct in_addr_4in6 inp46_local;
203 		struct in6_addr inp6_local;
204 	} inp_dependladdr;
205 	union {
206 		/* placeholder for routing entry */
207 		struct route inp4_route;
208 		struct route_in6 inp6_route;
209 	} inp_dependroute;
210 	struct {
211 		/* type of service proto */
212 		u_char inp4_ip_tos;
213 		/* IP options */
214 		struct mbuf *inp4_options;
215 		/* IP multicast options */
216 		struct ip_moptions *inp4_moptions;
217 	} inp_depend4;
218 	struct {
219 		/* IP options */
220 		struct mbuf *inp6_options;
221 		/* IP6 options for outgoing packets */
222 		struct  ip6_pktopts *inp6_outputopts;
223 		/* IP multicast options */
224 		struct  ip6_moptions *inp6_moptions;
225 		/* ICMPv6 code type filter */
226 		struct  icmp6_filter *inp6_icmp6filt;
227 		/* IPV6_CHECKSUM setsockopt */
228 		int     inp6_cksum;
229 		short   inp6_hops;
230 	} inp_depend6;
231 
232 	uint64_t       inp_fadv_total_time;
233 	uint64_t       inp_fadv_start_time;
234 	uint64_t       inp_fadv_cnt;
235 
236 #if IPSEC
237 	struct inpcbpolicy *inp_sp;     /* for IPsec */
238 #endif /* IPSEC */
239 #if NECP
240 	struct inp_necp_attributes inp_necp_attributes;
241 	struct necp_inpcb_result inp_policyresult;
242 	uuid_t necp_client_uuid;
243 
244 	uint32_t inp_bind_in_progress_waiters;
245 	thread_t inp_bind_in_progress_last_waiter_thread;
246 
247 	thread_t inp_bind_in_progress_thread;
248 
249 	necp_client_flow_cb necp_cb;
250 	size_t inp_resolver_signature_length;
251 	uint8_t *inp_resolver_signature __sized_by(inp_resolver_signature_length);
252 #endif
253 #if SKYWALK
254 	netns_token inp_netns_token;    /* shared namespace state */
255 	/* optional IPv4 wildcard namespace reservation for an IPv6 socket */
256 	netns_token inp_wildcard_netns_token;
257 #endif /* SKYWALK */
258 	u_char *__sized_by(inp_keepalive_datalen) inp_keepalive_data;     /* for keepalive offload */
259 	uint8_t inp_keepalive_datalen; /* keepalive data length */
260 	uint8_t inp_keepalive_type;    /* type of application */
261 	uint16_t inp_keepalive_interval; /* keepalive interval */
262 	struct  nstat_sock_locus *inp_nstat_locus  __attribute__((aligned(sizeof(u_int64_t))));
263 	struct media_stats  inp_mstat __attribute__((aligned(8)));    /* All counts, total/cell/wifi etc */
264 	uint64_t inp_start_timestamp;
265 	uint64_t inp_connect_timestamp;
266 
267 	char inp_last_proc_name[MAXCOMLEN + 1];
268 	char inp_e_proc_name[MAXCOMLEN + 1];
269 
270 	uint64_t inp_max_pacing_rate; /* Per-connection maximumg pacing rate to be enforced (Bytes/second) */
271 };
272 
273 #define IFNET_COUNT_TYPE(_ifp)                                              \
274 	IFNET_IS_LOOPBACK(_ifp) ?  stats_functional_type_untracked:             \
275 	IFNET_IS_CELLULAR(_ifp) ?  stats_functional_type_cell:                  \
276 	IFNET_IS_WIFI(_ifp) ?                                                   \
277 	        IFNET_IS_WIFI_INFRA(_ifp) ? stats_functional_type_wifi_infra:   \
278 	                                    stats_functional_type_wifi_non_infra:    \
279 	IFNET_IS_WIRED(_ifp) ?     stats_functional_type_wired:                 \
280 	IFNET_IS_COMPANION_LINK_BLUETOOTH(_ifp)? stats_functional_type_bluetooth: stats_functional_type_unclassified;
281 
282 #define INP_ADD_RXSTAT(_inp, _stats_functional_type, _p, _b)          \
283 do {                                                                    \
284 	locked_add_64(&((_inp)->inp_mstat.ms_total.ts_rxpackets), (_p)); \
285 	locked_add_64(&((_inp)->inp_mstat.ms_total.ts_rxbytes), (_b));   \
286 	in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_total.ts_bitmap), net_uptime());  \
287 	switch(_stats_functional_type) {                                    \
288 	        case stats_functional_type_cell:                            \
289 	            locked_add_64(&((_inp)->inp_mstat.ms_cellular.ts_rxpackets), (_p)); \
290 	            locked_add_64(&((_inp)->inp_mstat.ms_cellular.ts_rxbytes), (_b));  \
291 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_cellular.ts_bitmap), net_uptime());  \
292 	            break;                                                  \
293 	        case stats_functional_type_wifi_infra:                       \
294 	            locked_add_64(&((_inp)->inp_mstat.ms_wifi_infra.ts_rxpackets), (_p)); \
295 	            locked_add_64(&((_inp)->inp_mstat.ms_wifi_infra.ts_rxbytes), (_b));  \
296 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_wifi_infra.ts_bitmap), net_uptime());  \
297 	            break;                                                  \
298 	        case stats_functional_type_wifi_non_infra:                       \
299 	            locked_add_64(&((_inp)->inp_mstat.ms_wifi_non_infra.ts_rxpackets), (_p)); \
300 	            locked_add_64(&((_inp)->inp_mstat.ms_wifi_non_infra.ts_rxbytes), (_b));  \
301 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_wifi_non_infra.ts_bitmap), net_uptime());  \
302 	            break;                                                  \
303 	        case stats_functional_type_wired:                           \
304 	            locked_add_64(&((_inp)->inp_mstat.ms_wired.ts_rxpackets), (_p)); \
305 	            locked_add_64(&((_inp)->inp_mstat.ms_wired.ts_rxbytes), (_b));  \
306 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_wired.ts_bitmap), net_uptime());  \
307 	            break;                                                  \
308 	        case stats_functional_type_bluetooth:                       \
309 	            locked_add_64(&((_inp)->inp_mstat.ms_bluetooth.ts_rxpackets), (_p)); \
310 	            locked_add_64(&((_inp)->inp_mstat.ms_bluetooth.ts_rxbytes), (_b));  \
311 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_bluetooth.ts_bitmap), net_uptime());  \
312 	            break;                                                  \
313 	        case stats_functional_type_unclassified:                    \
314 	            locked_add_64(&((_inp)->inp_mstat.ms_alternate.ts_rxpackets), (_p)); \
315 	            locked_add_64(&((_inp)->inp_mstat.ms_alternate.ts_rxbytes), (_b));  \
316 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_alternate.ts_bitmap), net_uptime());  \
317 	            break;                                                  \
318 	        default:                                                    \
319 	            break;                                                  \
320 	};                                                              \
321 } while (0);
322 
323 #define INP_ADD_TXSTAT(_inp, _stats_functional_type, _p, _b)          \
324 do {                                                                    \
325 	locked_add_64(&((_inp)->inp_mstat.ms_total.ts_txpackets), (_p)); \
326 	locked_add_64(&((_inp)->inp_mstat.ms_total.ts_txbytes), (_b));   \
327 	in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_total.ts_bitmap), net_uptime());  \
328 	switch(_stats_functional_type) {                                    \
329 	        case stats_functional_type_cell:                            \
330 	            locked_add_64(&((_inp)->inp_mstat.ms_cellular.ts_txpackets), (_p)); \
331 	            locked_add_64(&((_inp)->inp_mstat.ms_cellular.ts_txbytes), (_b));  \
332 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_cellular.ts_bitmap), net_uptime());  \
333 	            break;                                                  \
334 	        case stats_functional_type_wifi_infra:                       \
335 	            locked_add_64(&((_inp)->inp_mstat.ms_wifi_infra.ts_txpackets), (_p)); \
336 	            locked_add_64(&((_inp)->inp_mstat.ms_wifi_infra.ts_txbytes), (_b));  \
337 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_wifi_infra.ts_bitmap), net_uptime());  \
338 	            break;                                                  \
339 	        case stats_functional_type_wifi_non_infra:                       \
340 	            locked_add_64(&((_inp)->inp_mstat.ms_wifi_non_infra.ts_txpackets), (_p)); \
341 	            locked_add_64(&((_inp)->inp_mstat.ms_wifi_non_infra.ts_txbytes), (_b));  \
342 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_wifi_non_infra.ts_bitmap), net_uptime());  \
343 	            break;                                                  \
344 	        case stats_functional_type_wired:                           \
345 	            locked_add_64(&((_inp)->inp_mstat.ms_wired.ts_txpackets), (_p)); \
346 	            locked_add_64(&((_inp)->inp_mstat.ms_wired.ts_txbytes), (_b));  \
347 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_wired.ts_bitmap), net_uptime());  \
348 	            break;                                                  \
349 	        case stats_functional_type_bluetooth:                       \
350 	            locked_add_64(&((_inp)->inp_mstat.ms_bluetooth.ts_txpackets), (_p)); \
351 	            locked_add_64(&((_inp)->inp_mstat.ms_bluetooth.ts_txbytes), (_b));  \
352 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_bluetooth.ts_bitmap), net_uptime());  \
353 	            break;                                                  \
354 	        case stats_functional_type_unclassified:                    \
355 	            locked_add_64(&((_inp)->inp_mstat.ms_alternate.ts_txpackets), (_p)); \
356 	            locked_add_64(&((_inp)->inp_mstat.ms_alternate.ts_txbytes), (_b));  \
357 	            in_stat_set_activity_bitmap(&((_inp)->inp_mstat.ms_alternate.ts_bitmap), net_uptime());  \
358 	            break;                                                  \
359 	        default:                                                    \
360 	            break;                                                  \
361 	};                                                              \
362 } while (0);
363 
364 
365 #endif /* BSD_KERNEL_PRIVATE */
366 
367 /*
368  * Interface exported to userland by various protocols which use
369  * inpcbs.  Hack alert -- only define if struct xsocket is in scope.
370  */
371 #pragma pack(4)
372 
373 #if defined(__LP64__)
374 struct _inpcb_list_entry {
375 	u_int32_t   le_next;
376 	u_int32_t   le_prev;
377 };
378 #define _INPCB_PTR(x)           u_int32_t
379 #define _INPCB_LIST_ENTRY(x)    struct _inpcb_list_entry
380 #else /* !__LP64__ */
381 #define _INPCB_PTR(x)           x
382 #define _INPCB_LIST_ENTRY(x)    LIST_ENTRY(x)
383 #endif /* !__LP64__ */
384 
385 #ifdef XNU_KERNEL_PRIVATE
386 /*
387  * This is a copy of the inpcb as it shipped in Panther. This structure
388  * is filled out in a copy function. This allows the inpcb to change
389  * without breaking userland tools.
390  *
391  * CAUTION: Many fields may not be filled out. Fewer may be filled out
392  * in the future. Code defensively.
393  */
394 struct inpcb_compat {
395 #else
396 struct inpcbinfo;
397 struct inpcbport;
398 struct mbuf;
399 struct ip6_pktopts;
400 struct ip6_moptions;
401 struct icmp6_filter;
402 struct inpcbpolicy;
403 
404 struct inpcb {
405 #endif /* KERNEL_PRIVATE */
406 	_INPCB_LIST_ENTRY(inpcb) inp_hash;      /* hash list */
407 	struct in_addr reserved1;               /* reserved */
408 	struct in_addr reserved2;               /* reserved */
409 	u_short inp_fport;                      /* foreign port */
410 	u_short inp_lport;                      /* local port */
411 	_INPCB_LIST_ENTRY(inpcb) inp_list;      /* list for all peer PCBs */
412 	_INPCB_PTR(caddr_t) inp_ppcb;           /* per-protocol pcb */
413 	_INPCB_PTR(struct inpcbinfo *) inp_pcbinfo;     /* PCB list info */
414 	_INPCB_PTR(void *) inp_socket;  /* back pointer to socket */
415 	u_char nat_owner;               /* Used to NAT TCP/UDP traffic */
416 	u_int32_t nat_cookie;           /* Cookie stored and returned to NAT */
417 	_INPCB_LIST_ENTRY(inpcb) inp_portlist;  /* this PCB's local port list */
418 	_INPCB_PTR(struct inpcbport *) inp_phd; /* head of this list */
419 	inp_gen_t inp_gencnt;           /* generation count of this instance */
420 	int inp_flags;                  /* generic IP/datagram flags */
421 	u_int32_t inp_flow;
422 
423 	u_char inp_vflag;
424 
425 	u_char inp_ip_ttl;              /* time to live proto */
426 	u_char inp_ip_p;                /* protocol proto */
427 	/* protocol dependent part */
428 	union {
429 		/* foreign host table entry */
430 		struct in_addr_4in6 inp46_foreign;
431 		struct in6_addr inp6_foreign;
432 	} inp_dependfaddr;
433 	union {
434 		/* local host table entry */
435 		struct in_addr_4in6 inp46_local;
436 		struct in6_addr inp6_local;
437 	} inp_dependladdr;
438 	union {
439 		/* placeholder for routing entry */
440 		u_char inp4_route[20];
441 		u_char inp6_route[32];
442 	} inp_dependroute;
443 	struct {
444 		/* type of service proto */
445 		u_char inp4_ip_tos;
446 		/* IP options */
447 		_INPCB_PTR(struct mbuf *) inp4_options;
448 		/* IP multicast options */
449 		_INPCB_PTR(struct ip_moptions *) inp4_moptions;
450 	} inp_depend4;
451 
452 	struct {
453 		/* IP options */
454 		_INPCB_PTR(struct mbuf *) inp6_options;
455 		u_int8_t inp6_hlim;
456 		u_int8_t unused_uint8_1;
457 		ushort unused_uint16_1;
458 		/* IP6 options for outgoing packets */
459 		_INPCB_PTR(struct ip6_pktopts *) inp6_outputopts;
460 		/* IP multicast options */
461 		_INPCB_PTR(struct ip6_moptions *) inp6_moptions;
462 		/* ICMPv6 code type filter */
463 		_INPCB_PTR(struct icmp6_filter *) inp6_icmp6filt;
464 		/* IPV6_CHECKSUM setsockopt */
465 		int     inp6_cksum;
466 		u_short inp6_ifindex;
467 		short   inp6_hops;
468 	} inp_depend6;
469 
470 	int hash_element;               /* Array index of pcb's hash list */
471 	_INPCB_PTR(caddr_t) inp_saved_ppcb; /* pointer while cached */
472 	_INPCB_PTR(struct inpcbpolicy *) inp_sp;
473 	u_int32_t       reserved[3];    /* reserved */
474 };
475 
476 struct  xinpcb {
477 	u_int32_t       xi_len;         /* length of this structure */
478 #ifdef XNU_KERNEL_PRIVATE
479 	struct  inpcb_compat xi_inp;
480 #else
481 	struct  inpcb xi_inp;
482 #endif
483 	struct  xsocket xi_socket;
484 	u_quad_t        xi_alignment_hack;
485 };
486 
487 #if XNU_TARGET_OS_OSX || KERNEL || !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
488 struct inpcb64_list_entry {
489 	u_int64_t   le_next;
490 	u_int64_t   le_prev;
491 };
492 
493 struct  xinpcb64 {
494 	u_int64_t       xi_len;         /* length of this structure */
495 	u_int64_t       xi_inpp;
496 	u_short         inp_fport;      /* foreign port */
497 	u_short         inp_lport;      /* local port */
498 	struct inpcb64_list_entry inp_list; /* list for all PCBs */
499 	u_int64_t       inp_ppcb;       /* ptr to per-protocol PCB */
500 	u_int64_t       inp_pcbinfo;    /* PCB list info */
501 	struct inpcb64_list_entry inp_portlist; /* this PCB's local port list */
502 	u_int64_t       inp_phd;        /* head of this list */
503 	inp_gen_t       inp_gencnt;     /* current generation count */
504 	int             inp_flags;      /* generic IP/datagram flags */
505 	u_int32_t       inp_flow;
506 	u_char          inp_vflag;
507 	u_char          inp_ip_ttl;     /* time to live */
508 	u_char          inp_ip_p;       /* protocol */
509 	union {                         /* foreign host table entry */
510 		struct  in_addr_4in6    inp46_foreign;
511 		struct  in6_addr        inp6_foreign;
512 	} inp_dependfaddr;
513 	union {                         /* local host table entry */
514 		struct  in_addr_4in6    inp46_local;
515 		struct  in6_addr        inp6_local;
516 	} inp_dependladdr;
517 	struct {
518 		u_char  inp4_ip_tos;    /* type of service */
519 	} inp_depend4;
520 	struct {
521 		u_int8_t inp6_hlim;
522 		int     inp6_cksum;
523 		u_short inp6_ifindex;
524 		short   inp6_hops;
525 	} inp_depend6;
526 	struct  xsocket64 xi_socket;
527 	u_quad_t        xi_alignment_hack;
528 };
529 #endif /* XNU_TARGET_OS_OSX || KERNEL || !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
530 
531 #ifdef PRIVATE
532 struct xinpcb_list_entry {
533 	u_int64_t   le_next;
534 	u_int64_t   le_prev;
535 };
536 
537 struct  xinpcb_n {
538 	u_int32_t       xi_len;         /* length of this structure */
539 	u_int32_t       xi_kind;        /* XSO_INPCB */
540 	u_int64_t       xi_inpp;
541 	u_short         inp_fport;      /* foreign port */
542 	u_short         inp_lport;      /* local port */
543 	u_int64_t       inp_ppcb;       /* pointer to per-protocol pcb */
544 	inp_gen_t       inp_gencnt;     /* generation count of this instance */
545 	int             inp_flags;      /* generic IP/datagram flags */
546 	u_int32_t       inp_flow;
547 	u_char          inp_vflag;
548 	u_char          inp_ip_ttl;     /* time to live */
549 	u_char          inp_ip_p;       /* protocol */
550 	union {                         /* foreign host table entry */
551 		struct in_addr_4in6     inp46_foreign;
552 		struct in6_addr         inp6_foreign;
553 	} inp_dependfaddr;
554 	union {                         /* local host table entry */
555 		struct in_addr_4in6     inp46_local;
556 		struct in6_addr         inp6_local;
557 	} inp_dependladdr;
558 	struct {
559 		u_char  inp4_ip_tos;    /* type of service */
560 	} inp_depend4;
561 	struct {
562 		u_int8_t inp6_hlim;
563 		int     inp6_cksum;
564 		u_short inp6_ifindex;
565 		short   inp6_hops;
566 	} inp_depend6;
567 	u_int32_t               inp_flowhash;
568 	u_int32_t       inp_flags2;
569 };
570 #endif /* PRIVATE */
571 
572 struct  xinpgen {
573 	u_int32_t       xig_len;        /* length of this structure */
574 	u_int           xig_count;      /* number of PCBs at this time */
575 	inp_gen_t       xig_gen;        /* generation count at this time */
576 	so_gen_t        xig_sogen;      /* current socket generation count */
577 };
578 
579 #pragma pack()
580 
581 /*
582  * These defines are for use with the inpcb.
583  */
584 #define INP_IPV4        0x1
585 #define INP_IPV6        0x2
586 #define INP_V4MAPPEDV6  0x4
587 #define inp_faddr       inp_dependfaddr.inp46_foreign.ia46_addr4
588 #define inp_laddr       inp_dependladdr.inp46_local.ia46_addr4
589 #define in6p_faddr      inp_dependfaddr.inp6_foreign
590 #define in6p_laddr      inp_dependladdr.inp6_local
591 
592 #ifdef BSD_KERNEL_PRIVATE
593 #define inp_route       inp_dependroute.inp4_route
594 #define inp_ip_tos      inp_depend4.inp4_ip_tos
595 #define inp_options     inp_depend4.inp4_options
596 #define inp_moptions    inp_depend4.inp4_moptions
597 #define in6p_route      inp_dependroute.inp6_route
598 #define in6p_ip6_hlim   inp_depend6.inp6_hlim
599 #define in6p_hops       inp_depend6.inp6_hops   /* default hop limit */
600 #define in6p_ip6_nxt    inp_ip_p
601 #define in6p_vflag      inp_vflag
602 #define in6p_options    inp_depend6.inp6_options
603 #define in6p_outputopts inp_depend6.inp6_outputopts
604 #define in6p_moptions   inp_depend6.inp6_moptions
605 #define in6p_icmp6filt  inp_depend6.inp6_icmp6filt
606 #define in6p_cksum      inp_depend6.inp6_cksum
607 #define in6p_ifindex    inp_depend6.inp6_ifindex
608 #define in6p_flags      inp_flags
609 #define in6p_flags2     inp_flags2
610 #define in6p_socket     inp_socket
611 #define in6p_lport      inp_lport
612 #define in6p_fport      inp_fport
613 #define in6p_ppcb       inp_ppcb
614 #define in6p_state      inp_state
615 #define in6p_wantcnt    inp_wantcnt
616 #define in6p_last_outifp inp_last_outifp
617 #define in6pcb          inpcb
618 #if IPSEC
619 #define in6p_sp         inp_sp
620 #endif /* IPSEC */
621 #define INP_INC_IFNET_STAT(_inp_, _stat_) { \
622 	if ((_inp_)->inp_last_outifp != NULL) { \
623 	        if ((_inp_)->inp_vflag & INP_IPV6) { \
624 	                (_inp_)->inp_last_outifp->if_ipv6_stat->_stat_++;\
625 	        } else { \
626 	                (_inp_)->inp_last_outifp->if_ipv4_stat->_stat_++;\
627 	        }\
628 	}\
629 }
630 
631 struct inpcbport {
632 	LIST_ENTRY(inpcbport) phd_hash;
633 	struct inpcbhead phd_pcblist;
634 	u_short phd_port;
635 };
636 
637 struct intimercount {
638 	u_int32_t intimer_lazy; /* lazy requests for timer scheduling */
639 	u_int32_t intimer_fast; /* fast requests, can be coalesced */
640 	u_int32_t intimer_nodelay; /* fast requests, never coalesced */
641 };
642 
643 typedef void (*inpcb_timer_func_t)(struct inpcbinfo *);
644 
645 /*
646  * Global data structure for each high-level protocol (UDP, TCP, ...) in both
647  * IPv4 and IPv6.  Holds inpcb lists and information for managing them.  Each
648  * pcbinfo is protected by a RW lock: ipi_lock.
649  *
650  * All INPCB pcbinfo entries are linked together via ipi_entry.
651  */
652 struct inpcbinfo {
653 	/*
654 	 * Glue to all PCB infos, as well as garbage collector and
655 	 * timer callbacks, protected by inpcb_lock.  Callout request
656 	 * counts are atomically updated.
657 	 */
658 	TAILQ_ENTRY(inpcbinfo)  ipi_entry;
659 	inpcb_timer_func_t      ipi_gc;
660 	inpcb_timer_func_t      ipi_timer;
661 	struct intimercount     ipi_gc_req;
662 	struct intimercount     ipi_timer_req;
663 
664 	/*
665 	 * Per-protocol lock protecting pcb list, pcb count, etc.
666 	 */
667 	lck_rw_t                ipi_lock;
668 
669 	/*
670 	 * List and count of pcbs on the protocol.
671 	 */
672 	struct inpcbhead        *ipi_listhead;
673 	uint32_t                ipi_count;
674 
675 	/*
676 	 * Count of pcbs marked with INP2_TIMEWAIT flag.
677 	 */
678 	uint32_t                ipi_twcount;
679 
680 	/*
681 	 * Generation count -- incremented each time a connection is
682 	 * allocated or freed.
683 	 */
684 	uint64_t                ipi_gencnt;
685 
686 	/*
687 	 * Fields associated with port lookup and allocation.
688 	 */
689 	uint16_t                ipi_lastport;
690 	uint16_t                ipi_lastlow;
691 	uint16_t                ipi_lasthi;
692 
693 	/*
694 	 * Zone from which inpcbs are allocated for this protocol.
695 	 */
696 #if BSD_KERNEL_PRIVATE
697 	kalloc_type_view_t       ipi_zone;
698 #else
699 	struct zone             *ipi_zone;
700 #endif
701 
702 	/*
703 	 * Per-protocol hash of pcbs, hashed by local and foreign
704 	 * addresses and port numbers.
705 	 */
706 	struct inpcbhead        *__counted_by(ipi_hashbase_count) ipi_hashbase;
707 	size_t                  ipi_hashbase_count;
708 	u_long                  ipi_hashmask;
709 
710 	/*
711 	 * Per-protocol hash of pcbs, hashed by only local port number.
712 	 */
713 	struct inpcbporthead    *__counted_by(ipi_porthashbase_count) ipi_porthashbase;
714 	size_t                  ipi_porthashbase_count;
715 	u_long                  ipi_porthashmask;
716 
717 	/*
718 	 * Misc.
719 	 */
720 	lck_attr_t              ipi_lock_attr;
721 	lck_grp_t               *ipi_lock_grp;
722 
723 #define INPCBINFO_UPDATE_MSS    0x1
724 #define INPCBINFO_HANDLE_LQM_ABORT      0x2
725 	u_int32_t               ipi_flags;
726 };
727 
728 #define INP_PCBHASH(faddr, lport, fport, mask) \
729 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
730 #define INP_PCBPORTHASH(lport, mask) \
731 	(ntohs((lport)) & (mask))
732 
733 /*
734  * 4-tuple for an IPv4/IPv6 endpoint
735  */
736 union in_dependaddr {
737 	struct in_addr_4in6     id46_addr;
738 	struct in6_addr         id6_addr;
739 };
740 
741 struct in_endpoints {
742 	in_port_t               ie_fport;
743 	in_port_t               ie_lport;
744 	union in_dependaddr     ie_dependfaddr;
745 	union in_dependaddr     ie_dependladdr;
746 };
747 #define ie_faddr        ie_dependfaddr.id46_addr.ia46_addr4
748 #define ie_laddr        ie_dependladdr.id46_addr.ia46_addr4
749 #define ie6_faddr       ie_dependfaddr.id6_addr
750 #define ie6_laddr       ie_dependladdr.id6_addr
751 
752 /*
753  * The following macro need to return a bool value
754  */
755 #define INP_IS_FLOW_CONTROLLED(_inp_) \
756 	(((_inp_)->inp_flags & INP_FLOW_CONTROLLED) ? true : false)
757 #define INP_IS_FLOW_SUSPENDED(_inp_) \
758 	((((_inp_)->inp_flags & INP_FLOW_SUSPENDED) ||   \
759 	((_inp_)->inp_socket->so_flags & SOF_SUSPENDED)) ? true : false)
760 #define INP_WAIT_FOR_IF_FEEDBACK(_inp_) \
761 	(((_inp_)->inp_flags & (INP_FLOW_CONTROLLED | INP_FLOW_SUSPENDED)) != 0)
762 
763 #define INP_NO_CELLULAR(_inp) \
764 	(((_inp)->inp_flags & INP_NO_IFT_CELLULAR) ? true : false)
765 #define INP_NO_EXPENSIVE(_inp) \
766 	(((_inp)->inp_flags2 & INP2_NO_IFF_EXPENSIVE) ? true : false)
767 #define INP_NO_CONSTRAINED(_inp) \
768 	(((_inp)->inp_flags2 & INP2_NO_IFF_CONSTRAINED) ? true : false)
769 #define INP_AWDL_UNRESTRICTED(_inp) \
770 	(((_inp)->inp_flags2 & INP2_AWDL_UNRESTRICTED) ? true : false)
771 #define INP_INTCOPROC_ALLOWED(_inp) \
772 	(((_inp)->inp_flags2 & INP2_INTCOPROC_ALLOWED) ? true : false)
773 /* A process that can access the INTCOPROC interface can also access the MANAGEMENT interface */
774 #define INP_MANAGEMENT_ALLOWED(_inp) \
775 	(((_inp)->inp_flags2 & (INP2_MANAGEMENT_ALLOWED | INP2_INTCOPROC_ALLOWED)) ? true : false)
776 #define INP_ULTRA_CONSTRAINED_ALLOWED(_inp) \
777     (((_inp)->inp_flags2 & INP2_ULTRA_CONSTRAINED_ALLOWED) ? true : false)
778 
779 #endif /* BSD_KERNEL_PRIVATE */
780 
781 /*
782  * Flags for inp_flags.
783  *
784  * Some of these are publicly defined for legacy reasons, as they are
785  * (unfortunately) used by certain applications to determine, at compile
786  * time, whether or not the OS supports certain features.
787  */
788 #ifdef BSD_KERNEL_PRIVATE
789 #define INP_RECVOPTS            0x00000001 /* receive incoming IP options */
790 #define INP_RECVRETOPTS         0x00000002 /* receive IP options for reply */
791 #define INP_RECVDSTADDR         0x00000004 /* receive IP dst address */
792 #define INP_HDRINCL             0x00000008 /* user supplies entire IP header */
793 #define INP_HIGHPORT            0x00000010 /* user wants "high" port binding */
794 #define INP_LOWPORT             0x00000020 /* user wants "low" port binding */
795 #endif /* BSD_KERNEL_PRIVATE */
796 
797 #define INP_ANONPORT            0x00000040 /* port chosen for user */
798 
799 #ifdef BSD_KERNEL_PRIVATE
800 #define INP_RECVIF              0x00000080 /* receive incoming interface */
801 #define INP_MTUDISC             0x00000100 /* unused */
802 #define INP_STRIPHDR            0x00000200 /* strip hdrs in raw_ip (for OT) */
803 #define INP_RECV_ANYIF          0x00000400 /* don't restrict inbound iface */
804 #define INP_INADDR_ANY          0x00000800 /* local address wasn't specified */
805 #define INP_IN6ADDR_ANY         INP_INADDR_ANY
806 #define INP_RECVTTL             0x00001000 /* receive incoming IP TTL */
807 #define INP_UDP_NOCKSUM         0x00002000 /* turn off outbound UDP checksum */
808 #define INP_BOUND_IF            0x00004000 /* bind socket to an interface */
809 #endif /* BSD_KERNEL_PRIVATE */
810 
811 #define IN6P_IPV6_V6ONLY        0x00008000 /* restrict AF_INET6 socket for v6 */
812 
813 #ifdef BSD_KERNEL_PRIVATE
814 #define IN6P_PKTINFO            0x00010000 /* receive IP6 dst and I/F */
815 #define IN6P_HOPLIMIT           0x00020000 /* receive hoplimit */
816 #define IN6P_HOPOPTS            0x00040000 /* receive hop-by-hop options */
817 #define IN6P_DSTOPTS            0x00080000 /* receive dst options after rthdr */
818 #define IN6P_RTHDR              0x00100000 /* receive routing header */
819 #define IN6P_RTHDRDSTOPTS       0x00200000 /* receive dstoptions before rthdr */
820 #define IN6P_TCLASS             0x00400000 /* receive traffic class value */
821 #define INP_RECVTOS             IN6P_TCLASS     /* receive incoming IP TOS */
822 #define IN6P_AUTOFLOWLABEL      0x00800000 /* attach flowlabel automatically */
823 #endif /* BSD_KERNEL_PRIVATE */
824 
825 #define IN6P_BINDV6ONLY         0x01000000 /* do not grab IPv4 traffic */
826 
827 #ifdef BSD_KERNEL_PRIVATE
828 #define IN6P_RFC2292            0x02000000 /* used RFC2292 API on the socket */
829 #define IN6P_MTU                0x04000000 /* receive path MTU for IPv6 */
830 #define INP_PKTINFO             0x08000000 /* rcv and snd PKTINFO for IPv4 */
831 #define INP_FLOW_SUSPENDED      0x10000000 /* flow suspended */
832 #define INP_NO_IFT_CELLULAR     0x20000000 /* do not use cellular interface */
833 #define INP_FLOW_CONTROLLED     0x40000000 /* flow controlled */
834 #define INP_FC_FEEDBACK         0x80000000 /* got interface flow adv feedback */
835 
836 #define INP_CONTROLOPTS \
837 	(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|INP_RECVIF|INP_RECVTTL| \
838 	INP_PKTINFO|IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|IN6P_DSTOPTS| \
839 	IN6P_RTHDR|IN6P_RTHDRDSTOPTS|IN6P_TCLASS|IN6P_RFC2292|IN6P_MTU)
840 
841 #define INP_UNMAPPABLEOPTS \
842 	(IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|IN6P_AUTOFLOWLABEL)
843 
844 /*
845  * Flags for inp_flags2.
846  *
847  * Overflowed INP flags; use INP2 prefix to avoid misuse.
848  */
849 #define INP2_TIMEWAIT                   0x00000001 /* in TIMEWAIT */
850 #define INP2_IN_FCTREE                  0x00000002 /* in inp_fc_tree */
851 #define INP2_WANT_APP_POLICY            0x00000004 /* necp app policy check is desired */
852 #define INP2_NO_IFF_EXPENSIVE           0x00000008 /* do not use expensive interface */
853 #define INP2_INHASHLIST                 0x00000010 /* pcb is in inp_hash list */
854 #define INP2_AWDL_UNRESTRICTED          0x00000020 /* AWDL restricted mode allowed */
855 #define INP2_KEEPALIVE_OFFLOAD          0x00000040 /* Enable UDP or TCP keepalive offload */
856 #define INP2_INTCOPROC_ALLOWED          0x00000080 /* Allow communication via internal co-processor interfaces */
857 #define INP2_CONNECT_IN_PROGRESS        0x00000100 /* A connect call is in progress, so binds are intermediate steps */
858 #define INP2_CLAT46_FLOW                0x00000200 /* The flow is going to use CLAT46 path */
859 #define INP2_EXTERNAL_PORT              0x00000400 /* The port is registered externally, for NECP listeners */
860 #define INP2_NO_IFF_CONSTRAINED         0x00000800 /* do not use constrained interface */
861 #define INP2_DONTFRAG                   0x00001000 /* mark the DF bit in the IP header to avoid fragmentation */
862 #define INP2_SCOPED_BY_NECP             0x00002000 /* NECP scoped the pcb */
863 #define INP2_LOGGING_ENABLED            0x00004000 /* logging enabled for the socket */
864 #define INP2_LOGGED_SUMMARY             0x00008000 /* logged: the final summary */
865 #define INP2_MANAGEMENT_ALLOWED         0x00010000 /* Allow communication over a management interface */
866 #define INP2_MANAGEMENT_CHECKED         0x00020000 /* Checked entitlements for a management interface */
867 #define INP2_BIND_IN_PROGRESS           0x00040000 /* A bind call is in progress */
868 #define INP2_LAST_ROUTE_LOCAL           0x00080000 /* Last used route was local */
869 #define INP2_ULTRA_CONSTRAINED_ALLOWED  0x00100000 /* Allow communication over ultra-constrained interfaces */
870 #define INP2_ULTRA_CONSTRAINED_CHECKED  0x00200000 /* Checked entitlements for ultra-constrained interfaces */
871 #define INP2_RECV_LINK_ADDR_TYPE        0x00400000 /* receive the type of the link level address */
872 #define INP2_CONNECTION_IDLE            0x00800000 /* Connection is idle */
873 
874 /*
875  * Flags passed to in_pcblookup*() functions.
876  */
877 #define INPLOOKUP_WILDCARD      1
878 
879 #define sotoinpcb(so)   ((struct inpcb *)(so)->so_pcb)
880 #define sotoin6pcb(so)  sotoinpcb(so)
881 
882 struct sysctl_req;
883 
884 extern int ipport_lowfirstauto;
885 extern int ipport_lowlastauto;
886 extern int ipport_firstauto;
887 extern int ipport_lastauto;
888 extern int ipport_hifirstauto;
889 extern int ipport_hilastauto;
890 extern int allow_udp_port_exhaustion;
891 #define UDP_RANDOM_PORT_RESERVE   4096
892 
893 /* freshly allocated PCB, it's in use */
894 #define INPCB_STATE_INUSE       0x1
895 /* this pcb is sitting in a a cache */
896 #define INPCB_STATE_CACHED      0x2
897 /* should treat as gone, will be garbage collected and freed */
898 #define INPCB_STATE_DEAD        0x3
899 
900 /* marked as ready to be garbaged collected, should be treated as not found */
901 #define WNT_STOPUSING           0xffff
902 /* that pcb is being acquired, do not recycle this time */
903 #define WNT_ACQUIRE             0x1
904 /* release acquired mode, can be garbage collected when wantcnt is null */
905 #define WNT_RELEASE             0x2
906 
907 extern void in_pcbinit(void);
908 extern void in_pcbinfo_attach(struct inpcbinfo *);
909 extern int in_pcbinfo_detach(struct inpcbinfo *);
910 
911 /* type of timer to be scheduled by inpcb_gc_sched and inpcb_timer_sched */
912 enum {
913 	INPCB_TIMER_LAZY = 0x1,
914 	INPCB_TIMER_FAST,
915 	INPCB_TIMER_NODELAY
916 };
917 extern void inpcb_gc_sched(struct inpcbinfo *, u_int32_t type);
918 extern void inpcb_timer_sched(struct inpcbinfo *, u_int32_t type);
919 
920 extern void in_losing(struct inpcb *);
921 extern void in_rtchange(struct inpcb *, int);
922 extern int in_pcballoc(struct socket *, struct inpcbinfo *, struct proc *);
923 extern int in_pcbbind(struct inpcb *, struct sockaddr *, struct sockaddr *, struct proc *);
924 extern int in_pcbconnect(struct inpcb *, struct sockaddr *, struct proc *,
925     unsigned int, struct ifnet **);
926 extern void in_pcbdetach(struct inpcb *);
927 extern void in_pcbdispose(struct inpcb *);
928 extern void in_pcbdisconnect(struct inpcb *);
929 extern int in_pcbinshash(struct inpcb *, struct sockaddr *, int);
930 extern int in_pcbladdr(struct inpcb *, struct sockaddr *, struct in_addr *,
931     unsigned int, struct ifnet **, int);
932 extern struct inpcb *in_pcblookup_local(struct inpcbinfo *, struct in_addr,
933     u_int, int);
934 extern struct inpcb *in_pcblookup_local_and_cleanup(struct inpcbinfo *,
935     struct in_addr, u_int, int);
936 extern struct inpcb *in_pcblookup_hash(struct inpcbinfo *, struct in_addr,
937     u_int, struct in_addr, u_int, int, struct ifnet *);
938 extern struct inpcb *in_pcblookup_hash_try(struct inpcbinfo *pcbinfo,
939     struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
940     u_int lport_arg, int wildcard, struct ifnet *ifp);
941 extern int in_pcblookup_hash_exists(struct inpcbinfo *, struct in_addr,
942     u_int, struct in_addr, u_int, int, uid_t *, gid_t *, struct ifnet *);
943 extern void in_pcbnotifyall(struct inpcbinfo *, struct in_addr, int,
944     void (*)(struct inpcb *, int));
945 extern void in_pcbrehash(struct inpcb *);
946 extern int in_getpeeraddr(struct socket *, struct sockaddr **);
947 extern int in_getsockaddr(struct socket *, struct sockaddr **);
948 extern int in_getsockaddr_s(struct socket *, struct sockaddr_in *);
949 extern int in_pcb_checkstate(struct inpcb *, int, int);
950 extern void in_pcbremlists(struct inpcb *);
951 extern void inpcb_to_compat(struct inpcb *, struct inpcb_compat *);
952 #if XNU_TARGET_OS_OSX
953 extern void inpcb_to_xinpcb64(struct inpcb *, struct xinpcb64 *);
954 #endif /* XNU_TARGET_OS_OSX */
955 
956 extern int get_pcblist_n(short, struct sysctl_req *, struct inpcbinfo *);
957 
958 extern void inpcb_get_ports_used(ifnet_t, int, u_int32_t,
959     bitstr_t *__counted_by(bitstr_size(IP_PORTRANGE_SIZE)), struct inpcbinfo *);
960 #define INPCB_OPPORTUNISTIC_THROTTLEON  0x0001
961 #define INPCB_OPPORTUNISTIC_SETCMD      0x0002
962 extern uint32_t inpcb_count_opportunistic(unsigned int, struct inpcbinfo *,
963     u_int32_t);
964 extern uint32_t inpcb_find_anypcb_byaddr(struct ifaddr *, struct inpcbinfo *);
965 extern void inp_route_copyout(struct inpcb *, struct route *);
966 extern void inp_route_copyin(struct inpcb *, struct route *);
967 extern int inp_bindif(struct inpcb *, unsigned int, struct ifnet **);
968 extern int inp_bindtodevice(struct inpcb *, const char *);
969 extern void inp_set_nocellular(struct inpcb *);
970 extern void inp_clear_nocellular(struct inpcb *);
971 extern void inp_set_noexpensive(struct inpcb *);
972 extern void inp_set_noconstrained(struct inpcb *);
973 extern void inp_set_awdl_unrestricted(struct inpcb *);
974 extern boolean_t inp_get_awdl_unrestricted(struct inpcb *);
975 extern void inp_clear_awdl_unrestricted(struct inpcb *);
976 extern void inp_set_intcoproc_allowed(struct inpcb *);
977 extern boolean_t inp_get_intcoproc_allowed(struct inpcb *);
978 extern void inp_clear_intcoproc_allowed(struct inpcb *);
979 extern void inp_set_management_allowed(struct inpcb *);
980 extern boolean_t inp_get_management_allowed(struct inpcb *);
981 extern void inp_clear_management_allowed(struct inpcb *);
982 extern void inp_set_ultra_constrained_allowed(struct inpcb *);
983 #if NECP
984 extern void inp_update_necp_policy(struct inpcb *, struct sockaddr *, struct sockaddr *, u_int);
985 extern void inp_set_want_app_policy(struct inpcb *);
986 extern void inp_clear_want_app_policy(struct inpcb *);
987 #endif /* NECP */
988 extern u_int32_t inp_calc_flowhash(struct inpcb *);
989 extern void inp_reset_fc_state(struct inpcb *);
990 extern int inp_set_fc_state(struct inpcb *, int advcode);
991 extern void inp_fc_unthrottle_tcp(struct inpcb *);
992 extern void inp_fc_throttle_tcp(struct inpcb *inp);
993 extern void inp_flowadv(uint32_t);
994 extern int inp_flush(struct inpcb *, int);
995 extern int inp_findinpcb_procinfo(struct inpcbinfo *, uint32_t, struct so_procinfo *);
996 extern void inp_get_soprocinfo(struct inpcb *, struct so_procinfo *);
997 extern int inp_update_policy(struct inpcb *);
998 extern boolean_t inp_restricted_recv(struct inpcb *, struct ifnet *);
999 extern boolean_t inp_restricted_send(struct inpcb *, struct ifnet *);
1000 extern void inp_incr_sndbytes_total(struct socket *, int);
1001 extern void inp_decr_sndbytes_total(struct socket *, int);
1002 extern void inp_count_sndbytes(struct inpcb *, u_int32_t);
1003 extern void inp_incr_sndbytes_unsent(struct socket *, int32_t);
1004 extern void inp_decr_sndbytes_unsent(struct socket *, int32_t);
1005 extern int32_t inp_get_sndbytes_allunsent(struct socket *, u_int32_t);
1006 extern void inp_decr_sndbytes_allunsent(struct socket *, u_int32_t);
1007 extern void inp_get_activity_bitmap(struct inpcb *inp, activity_bitmap_t *b);
1008 extern void inp_update_last_owner(struct socket *so, struct proc *p, struct proc *ep);
1009 extern void inp_copy_last_owner(struct socket *so, struct socket *head);
1010 extern void inp_enter_bind_in_progress(struct socket *so);
1011 extern void inp_exit_bind_in_progress(struct socket *so);
1012 #if SKYWALK
1013 extern void inp_update_netns_flags(struct socket *so);
1014 #endif /* SKYWALK */
1015 #endif /* BSD_KERNEL_PRIVATE */
1016 #ifdef KERNEL_PRIVATE
1017 /* exported for PPP */
1018 extern void inp_clear_INP_INADDR_ANY(struct socket *);
1019 extern int inp_limit_companion_link(struct inpcbinfo *pcbinfo, u_int32_t limit);
1020 extern int inp_recover_companion_link(struct inpcbinfo *pcbinfo);
1021 extern void in_management_interface_check(void);
1022 extern void in_pcb_check_management_entitled(struct inpcb *inp);
1023 extern void in_pcb_check_ultra_constrained_entitled(struct inpcb *inp);
1024 extern char *inp_snprintf_tuple(struct inpcb *, char *__sized_by(buflen) buf, size_t buflen);
1025 extern int in_pcbsetport(struct in_addr, struct sockaddr *, struct inpcb *, struct proc *, int);
1026 #endif /* KERNEL_PRIVATE */
1027 #endif /* !_NETINET_IN_PCB_H_ */
1028