]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in_pcb.h
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / netinet / in_pcb.h
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
9bccf70c 61 * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.32.2.4 2001/08/13 16:26:17 ume Exp $
1c79356b 62 */
2d21ac55
A
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 */
1c79356b
A
69
70#ifndef _NETINET_IN_PCB_H_
0a7de745 71#define _NETINET_IN_PCB_H_
9bccf70c 72#include <sys/appleapiopts.h>
1c79356b 73
91447636 74#include <sys/types.h>
1c79356b 75#include <sys/queue.h>
316670eb 76#ifdef BSD_KERNEL_PRIVATE
39236c6e 77#include <sys/bitstring.h>
316670eb 78#include <sys/tree.h>
91447636 79#include <kern/locks.h>
39236c6e 80#include <kern/zalloc.h>
5ba3f43e 81#include <netinet/in_stat.h>
39236c6e 82#endif /* BSD_KERNEL_PRIVATE */
9bccf70c 83
5ba3f43e 84#if IPSEC
9bccf70c 85#include <netinet6/ipsec.h> /* for IPSEC */
5ba3f43e
A
86#endif /* IPSEC */
87
fe8ab488
A
88#if NECP
89#include <net/necp.h>
90#endif
91
9bccf70c 92
316670eb 93#ifdef BSD_KERNEL_PRIVATE
1c79356b 94/*
39236c6e
A
95 * struct inpcb is the common protocol control block structure used in most
96 * IP transport protocols.
97 *
98 * Pointers to local and foreign host table entries, local and foreign socket
99 * numbers, and pointers up (to a socket structure) and down (to a
100 * protocol-specific control block) are stored here.
1c79356b
A
101 */
102LIST_HEAD(inpcbhead, inpcb);
103LIST_HEAD(inpcbporthead, inpcbport);
316670eb 104#endif /* BSD_KERNEL_PRIVATE */
0a7de745 105typedef u_quad_t inp_gen_t;
39236c6e 106
1c79356b
A
107/*
108 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
6d2010ae
A
109 * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
110 * the following structure.
1c79356b
A
111 */
112struct in_addr_4in6 {
0a7de745
A
113 u_int32_t ia46_pad32[3];
114 struct in_addr ia46_addr4;
1c79356b
A
115};
116
39236c6e 117#ifdef BSD_KERNEL_PRIVATE
1c79356b
A
118/*
119 * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
120 * of the structure. Therefore, it is important that the members in
121 * that position not contain any information which is required to be
122 * stable.
123 */
0a7de745 124struct icmp6_filter;
316670eb
A
125struct ifnet;
126
316670eb 127struct inp_stat {
0a7de745
A
128 u_int64_t rxpackets;
129 u_int64_t rxbytes;
130 u_int64_t txpackets;
131 u_int64_t txbytes;
6d2010ae
A
132};
133
39236c6e
A
134/*
135 * struct inpcb captures the network layer state for TCP, UDP and raw IPv6
136 * and IPv6 sockets. In the case of TCP, further per-connection state is
137 * hung off of inp_ppcb most of the time.
138 */
1c79356b 139struct inpcb {
0a7de745
A
140 decl_lck_mtx_data(, inpcb_mtx); /* inpcb per-socket mutex */
141 LIST_ENTRY(inpcb) inp_hash; /* hash list */
142 LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
143 void *inp_ppcb; /* pointer to per-protocol pcb */
144 struct inpcbinfo *inp_pcbinfo; /* PCB list info */
145 struct socket *inp_socket; /* back pointer to socket */
146 LIST_ENTRY(inpcb) inp_portlist; /* list for this PCB's local port */
147 RB_ENTRY(inpcb) infc_link; /* link for flowhash RB tree */
148 struct inpcbport *inp_phd; /* head of this list */
149 inp_gen_t inp_gencnt; /* generation count of this instance */
150 int inp_hash_element; /* array index of pcb's hash list */
151 int inp_wantcnt; /* wanted count; atomically updated */
152 int inp_state; /* state (INUSE/CACHED/DEAD) */
153 u_short inp_fport; /* foreign port */
154 u_short inp_lport; /* local port */
155 u_int32_t inp_flags; /* generic IP/datagram flags */
156 u_int32_t inp_flags2; /* generic IP/datagram flags #2 */
157 u_int32_t inp_flow; /* IPv6 flow information */
158
159 u_char inp_sndinprog_cnt; /* outstanding send operations */
cb323159 160 uint32_t inp_sndingprog_waiters;/* waiters for outstanding send */
0a7de745
A
161 u_char inp_vflag; /* INP_IPV4 or INP_IPV6 */
162
163 u_char inp_ip_ttl; /* time to live proto */
164 u_char inp_ip_p; /* protocol proto */
165
166 struct ifnet *inp_boundifp; /* interface for INP_BOUND_IF */
167 struct ifnet *inp_last_outifp; /* last known outgoing interface */
168 u_int32_t inp_flowhash; /* flow hash */
39236c6e
A
169
170 /* Protocol-dependent part */
1c79356b
A
171 union {
172 /* foreign host table entry */
39236c6e
A
173 struct in_addr_4in6 inp46_foreign;
174 struct in6_addr inp6_foreign;
1c79356b
A
175 } inp_dependfaddr;
176 union {
177 /* local host table entry */
39236c6e
A
178 struct in_addr_4in6 inp46_local;
179 struct in6_addr inp6_local;
1c79356b
A
180 } inp_dependladdr;
181 union {
182 /* placeholder for routing entry */
39236c6e
A
183 struct route inp4_route;
184 struct route_in6 inp6_route;
1c79356b
A
185 } inp_dependroute;
186 struct {
187 /* type of service proto */
188 u_char inp4_ip_tos;
189 /* IP options */
190 struct mbuf *inp4_options;
191 /* IP multicast options */
192 struct ip_moptions *inp4_moptions;
193 } inp_depend4;
1c79356b
A
194 struct {
195 /* IP options */
196 struct mbuf *inp6_options;
1c79356b 197 /* IP6 options for outgoing packets */
0a7de745 198 struct ip6_pktopts *inp6_outputopts;
1c79356b 199 /* IP multicast options */
0a7de745 200 struct ip6_moptions *inp6_moptions;
1c79356b 201 /* ICMPv6 code type filter */
0a7de745 202 struct icmp6_filter *inp6_icmp6filt;
1c79356b 203 /* IPV6_CHECKSUM setsockopt */
0a7de745
A
204 int inp6_cksum;
205 short inp6_hops;
1c79356b 206 } inp_depend6;
9bccf70c 207
0a7de745 208 caddr_t inp_saved_ppcb; /* place to save pointer while cached */
39236c6e 209#if IPSEC
cb323159 210 struct inpcbpolicy *inp_sp; /* for IPsec */
39236c6e 211#endif /* IPSEC */
fe8ab488
A
212#if NECP
213 struct {
214 char *inp_domain;
215 char *inp_account;
216 } inp_necp_attributes;
217 struct necp_inpcb_result inp_policyresult;
5ba3f43e 218 uuid_t necp_client_uuid;
d9a64523 219 necp_client_flow_cb necp_cb;
fe8ab488 220#endif
0a7de745 221 u_char *inp_keepalive_data; /* for keepalive offload */
3e170ce0 222 u_int8_t inp_keepalive_datalen; /* keepalive data length */
0a7de745 223 u_int8_t inp_keepalive_type; /* type of application */
3e170ce0
A
224 u_int16_t inp_keepalive_interval; /* keepalive interval */
225 uint32_t inp_nstat_refcnt __attribute__((aligned(4)));
0a7de745
A
226 struct inp_stat *inp_stat;
227 struct inp_stat *inp_cstat; /* cellular data */
228 struct inp_stat *inp_wstat; /* Wi-Fi data */
229 struct inp_stat *inp_Wstat; /* Wired data */
230 u_int8_t inp_stat_store[sizeof(struct inp_stat) + sizeof(u_int64_t)];
231 u_int8_t inp_cstat_store[sizeof(struct inp_stat) + sizeof(u_int64_t)];
232 u_int8_t inp_wstat_store[sizeof(struct inp_stat) + sizeof(u_int64_t)];
233 u_int8_t inp_Wstat_store[sizeof(struct inp_stat) + sizeof(u_int64_t)];
5ba3f43e
A
234 activity_bitmap_t inp_nw_activity;
235 u_int64_t inp_start_timestamp;
cb323159
A
236
237 char inp_last_proc_name[MAXCOMLEN + 1];
238 char inp_e_proc_name[MAXCOMLEN + 1];
1c79356b 239};
91447636 240
0a7de745
A
241#define INP_ADD_STAT(_inp, _cnt_cellular, _cnt_wifi, _cnt_wired, _a, _n) \
242do { \
243 locked_add_64(&((_inp)->inp_stat->_a), (_n)); \
244 if (_cnt_cellular) \
245 locked_add_64(&((_inp)->inp_cstat->_a), (_n)); \
246 if (_cnt_wifi) \
247 locked_add_64(&((_inp)->inp_wstat->_a), (_n)); \
248 if (_cnt_wired) \
249 locked_add_64(&((_inp)->inp_Wstat->_a), (_n)); \
39236c6e
A
250} while (0);
251#endif /* BSD_KERNEL_PRIVATE */
1c79356b
A
252
253/*
254 * Interface exported to userland by various protocols which use
255 * inpcbs. Hack alert -- only define if struct xsocket is in scope.
256 */
39236c6e 257#pragma pack(4)
91447636 258
39236c6e
A
259#if defined(__LP64__)
260struct _inpcb_list_entry {
0a7de745
A
261 u_int32_t le_next;
262 u_int32_t le_prev;
39236c6e 263};
0a7de745
A
264#define _INPCB_PTR(x) u_int32_t
265#define _INPCB_LIST_ENTRY(x) struct _inpcb_list_entry
39236c6e 266#else /* !__LP64__ */
0a7de745
A
267#define _INPCB_PTR(x) x
268#define _INPCB_LIST_ENTRY(x) LIST_ENTRY(x)
39236c6e
A
269#endif /* !__LP64__ */
270
271#ifdef XNU_KERNEL_PRIVATE
91447636
A
272/*
273 * This is a copy of the inpcb as it shipped in Panther. This structure
274 * is filled out in a copy function. This allows the inpcb to change
275 * without breaking userland tools.
39236c6e 276 *
91447636
A
277 * CAUTION: Many fields may not be filled out. Fewer may be filled out
278 * in the future. Code defensively.
279 */
91447636
A
280struct inpcb_compat {
281#else
282struct inpcbinfo;
283struct inpcbport;
284struct mbuf;
2d21ac55
A
285struct ip6_pktopts;
286struct ip6_moptions;
287struct icmp6_filter;
91447636
A
288struct inpcbpolicy;
289
290struct inpcb {
291#endif /* KERNEL_PRIVATE */
0a7de745
A
292 _INPCB_LIST_ENTRY(inpcb) inp_hash; /* hash list */
293 struct in_addr reserved1; /* reserved */
294 struct in_addr reserved2; /* reserved */
295 u_short inp_fport; /* foreign port */
296 u_short inp_lport; /* local port */
297 _INPCB_LIST_ENTRY(inpcb) inp_list; /* list for all peer PCBs */
298 _INPCB_PTR(caddr_t) inp_ppcb; /* per-protocol pcb */
299 _INPCB_PTR(struct inpcbinfo *) inp_pcbinfo; /* PCB list info */
300 _INPCB_PTR(void *) inp_socket; /* back pointer to socket */
301 u_char nat_owner; /* Used to NAT TCP/UDP traffic */
302 u_int32_t nat_cookie; /* Cookie stored and returned to NAT */
303 _INPCB_LIST_ENTRY(inpcb) inp_portlist; /* this PCB's local port list */
39236c6e 304 _INPCB_PTR(struct inpcbport *) inp_phd; /* head of this list */
0a7de745
A
305 inp_gen_t inp_gencnt; /* generation count of this instance */
306 int inp_flags; /* generic IP/datagram flags */
91447636
A
307 u_int32_t inp_flow;
308
39236c6e 309 u_char inp_vflag;
91447636 310
0a7de745
A
311 u_char inp_ip_ttl; /* time to live proto */
312 u_char inp_ip_p; /* protocol proto */
91447636
A
313 /* protocol dependent part */
314 union {
315 /* foreign host table entry */
39236c6e
A
316 struct in_addr_4in6 inp46_foreign;
317 struct in6_addr inp6_foreign;
91447636
A
318 } inp_dependfaddr;
319 union {
320 /* local host table entry */
39236c6e
A
321 struct in_addr_4in6 inp46_local;
322 struct in6_addr inp6_local;
91447636
A
323 } inp_dependladdr;
324 union {
325 /* placeholder for routing entry */
39236c6e
A
326 u_char inp4_route[20];
327 u_char inp6_route[32];
91447636
A
328 } inp_dependroute;
329 struct {
330 /* type of service proto */
331 u_char inp4_ip_tos;
332 /* IP options */
2d21ac55 333 _INPCB_PTR(struct mbuf *) inp4_options;
91447636 334 /* IP multicast options */
2d21ac55 335 _INPCB_PTR(struct ip_moptions *) inp4_moptions;
91447636
A
336 } inp_depend4;
337
338 struct {
339 /* IP options */
39236c6e
A
340 _INPCB_PTR(struct mbuf *) inp6_options;
341 u_int8_t inp6_hlim;
342 u_int8_t unused_uint8_1;
343 ushort unused_uint16_1;
91447636 344 /* IP6 options for outgoing packets */
39236c6e 345 _INPCB_PTR(struct ip6_pktopts *) inp6_outputopts;
91447636 346 /* IP multicast options */
39236c6e 347 _INPCB_PTR(struct ip6_moptions *) inp6_moptions;
91447636 348 /* ICMPv6 code type filter */
39236c6e 349 _INPCB_PTR(struct icmp6_filter *) inp6_icmp6filt;
91447636 350 /* IPV6_CHECKSUM setsockopt */
0a7de745
A
351 int inp6_cksum;
352 u_short inp6_ifindex;
353 short inp6_hops;
91447636
A
354 } inp_depend6;
355
0a7de745 356 int hash_element; /* Array index of pcb's hash list */
39236c6e
A
357 _INPCB_PTR(caddr_t) inp_saved_ppcb; /* pointer while cached */
358 _INPCB_PTR(struct inpcbpolicy *) inp_sp;
0a7de745 359 u_int32_t reserved[3]; /* reserved */
91447636
A
360};
361
0a7de745
A
362struct xinpcb {
363 u_int32_t xi_len; /* length of this structure */
39236c6e 364#ifdef XNU_KERNEL_PRIVATE
0a7de745 365 struct inpcb_compat xi_inp;
91447636 366#else
0a7de745 367 struct inpcb xi_inp;
91447636 368#endif
0a7de745
A
369 struct xsocket xi_socket;
370 u_quad_t xi_alignment_hack;
1c79356b
A
371};
372
f427ee49 373#if XNU_TARGET_OS_OSX || !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
b0d623f7 374struct inpcb64_list_entry {
0a7de745
A
375 u_int64_t le_next;
376 u_int64_t le_prev;
b0d623f7
A
377};
378
0a7de745
A
379struct xinpcb64 {
380 u_int64_t xi_len; /* length of this structure */
381 u_int64_t xi_inpp;
382 u_short inp_fport; /* foreign port */
383 u_short inp_lport; /* local port */
39236c6e 384 struct inpcb64_list_entry inp_list; /* list for all PCBs */
0a7de745
A
385 u_int64_t inp_ppcb; /* ptr to per-protocol PCB */
386 u_int64_t inp_pcbinfo; /* PCB list info */
387 struct inpcb64_list_entry inp_portlist; /* this PCB's local port list */
388 u_int64_t inp_phd; /* head of this list */
389 inp_gen_t inp_gencnt; /* current generation count */
390 int inp_flags; /* generic IP/datagram flags */
391 u_int32_t inp_flow;
392 u_char inp_vflag;
393 u_char inp_ip_ttl; /* time to live */
394 u_char inp_ip_p; /* protocol */
395 union { /* foreign host table entry */
396 struct in_addr_4in6 inp46_foreign;
397 struct in6_addr inp6_foreign;
39236c6e 398 } inp_dependfaddr;
0a7de745
A
399 union { /* local host table entry */
400 struct in_addr_4in6 inp46_local;
401 struct in6_addr inp6_local;
39236c6e 402 } inp_dependladdr;
6d2010ae 403 struct {
0a7de745 404 u_char inp4_ip_tos; /* type of service */
39236c6e 405 } inp_depend4;
6d2010ae 406 struct {
39236c6e 407 u_int8_t inp6_hlim;
0a7de745
A
408 int inp6_cksum;
409 u_short inp6_ifindex;
410 short inp6_hops;
39236c6e
A
411 } inp_depend6;
412 struct xsocket64 xi_socket;
0a7de745 413 u_quad_t xi_alignment_hack;
b0d623f7 414};
f427ee49 415#endif /* XNU_TARGET_OS_OSX || !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
b0d623f7 416
6d2010ae 417#ifdef PRIVATE
6d2010ae 418struct xinpcb_list_entry {
0a7de745
A
419 u_int64_t le_next;
420 u_int64_t le_prev;
6d2010ae
A
421};
422
0a7de745
A
423struct xinpcb_n {
424 u_int32_t xi_len; /* length of this structure */
425 u_int32_t xi_kind; /* XSO_INPCB */
426 u_int64_t xi_inpp;
427 u_short inp_fport; /* foreign port */
428 u_short inp_lport; /* local port */
429 u_int64_t inp_ppcb; /* pointer to per-protocol pcb */
430 inp_gen_t inp_gencnt; /* generation count of this instance */
431 int inp_flags; /* generic IP/datagram flags */
432 u_int32_t inp_flow;
433 u_char inp_vflag;
434 u_char inp_ip_ttl; /* time to live */
435 u_char inp_ip_p; /* protocol */
436 union { /* foreign host table entry */
437 struct in_addr_4in6 inp46_foreign;
438 struct in6_addr inp6_foreign;
39236c6e 439 } inp_dependfaddr;
0a7de745
A
440 union { /* local host table entry */
441 struct in_addr_4in6 inp46_local;
442 struct in6_addr inp6_local;
39236c6e 443 } inp_dependladdr;
6d2010ae 444 struct {
0a7de745 445 u_char inp4_ip_tos; /* type of service */
39236c6e 446 } inp_depend4;
6d2010ae 447 struct {
39236c6e 448 u_int8_t inp6_hlim;
0a7de745
A
449 int inp6_cksum;
450 u_short inp6_ifindex;
451 short inp6_hops;
39236c6e 452 } inp_depend6;
0a7de745
A
453 u_int32_t inp_flowhash;
454 u_int32_t inp_flags2;
6d2010ae 455};
6d2010ae
A
456#endif /* PRIVATE */
457
0a7de745
A
458struct xinpgen {
459 u_int32_t xig_len; /* length of this structure */
460 u_int xig_count; /* number of PCBs at this time */
461 inp_gen_t xig_gen; /* generation count at this time */
462 so_gen_t xig_sogen; /* current socket generation count */
1c79356b 463};
1c79356b 464
2d21ac55
A
465#pragma pack()
466
91447636
A
467/*
468 * These defines are for use with the inpcb.
469 */
0a7de745
A
470#define INP_IPV4 0x1
471#define INP_IPV6 0x2
472#define inp_faddr inp_dependfaddr.inp46_foreign.ia46_addr4
473#define inp_laddr inp_dependladdr.inp46_local.ia46_addr4
474#define in6p_faddr inp_dependfaddr.inp6_foreign
475#define in6p_laddr inp_dependladdr.inp6_local
316670eb
A
476
477#ifdef BSD_KERNEL_PRIVATE
0a7de745
A
478#define inp_route inp_dependroute.inp4_route
479#define inp_ip_tos inp_depend4.inp4_ip_tos
480#define inp_options inp_depend4.inp4_options
481#define inp_moptions inp_depend4.inp4_moptions
482#define in6p_route inp_dependroute.inp6_route
483#define in6p_ip6_hlim inp_depend6.inp6_hlim
484#define in6p_hops inp_depend6.inp6_hops /* default hop limit */
485#define in6p_ip6_nxt inp_ip_p
486#define in6p_vflag inp_vflag
487#define in6p_options inp_depend6.inp6_options
488#define in6p_outputopts inp_depend6.inp6_outputopts
489#define in6p_moptions inp_depend6.inp6_moptions
490#define in6p_icmp6filt inp_depend6.inp6_icmp6filt
491#define in6p_cksum inp_depend6.inp6_cksum
492#define in6p_ifindex inp_depend6.inp6_ifindex
493#define in6p_flags inp_flags
494#define in6p_flags2 inp_flags2
495#define in6p_socket inp_socket
496#define in6p_lport inp_lport
497#define in6p_fport inp_fport
498#define in6p_ppcb inp_ppcb
499#define in6p_state inp_state
500#define in6p_wantcnt inp_wantcnt
501#define in6p_last_outifp inp_last_outifp
502#define in6pcb inpcb
39236c6e 503#if IPSEC
0a7de745 504#define in6p_sp inp_sp
39236c6e 505#endif /* IPSEC */
4bd07ac2
A
506#define INP_INC_IFNET_STAT(_inp_, _stat_) { \
507 if ((_inp_)->inp_last_outifp != NULL) { \
0a7de745
A
508 if ((_inp_)->inp_vflag & INP_IPV6) { \
509 (_inp_)->inp_last_outifp->if_ipv6_stat->_stat_++;\
510 } else { \
511 (_inp_)->inp_last_outifp->if_ipv4_stat->_stat_++;\
512 }\
4bd07ac2
A
513 }\
514}
91447636 515
1c79356b
A
516struct inpcbport {
517 LIST_ENTRY(inpcbport) phd_hash;
518 struct inpcbhead phd_pcblist;
519 u_short phd_port;
520};
521
39236c6e 522struct intimercount {
0a7de745 523 u_int32_t intimer_lazy; /* lazy requests for timer scheduling */
39236c6e
A
524 u_int32_t intimer_fast; /* fast requests, can be coalesced */
525 u_int32_t intimer_nodelay; /* fast requests, never coalesced */
1c79356b
A
526};
527
39236c6e
A
528typedef void (*inpcb_timer_func_t)(struct inpcbinfo *);
529
530/*
531 * Global data structure for each high-level protocol (UDP, TCP, ...) in both
532 * IPv4 and IPv6. Holds inpcb lists and information for managing them. Each
533 * pcbinfo is protected by a RW lock: ipi_lock.
534 *
535 * All INPCB pcbinfo entries are linked together via ipi_entry.
536 */
537struct inpcbinfo {
538 /*
539 * Glue to all PCB infos, as well as garbage collector and
540 * timer callbacks, protected by inpcb_lock. Callout request
541 * counts are atomically updated.
542 */
0a7de745
A
543 TAILQ_ENTRY(inpcbinfo) ipi_entry;
544 inpcb_timer_func_t ipi_gc;
545 inpcb_timer_func_t ipi_timer;
546 struct intimercount ipi_gc_req;
547 struct intimercount ipi_timer_req;
39236c6e
A
548
549 /*
550 * Per-protocol lock protecting pcb list, pcb count, etc.
551 */
0a7de745 552 lck_rw_t *ipi_lock;
39236c6e
A
553
554 /*
555 * List and count of pcbs on the protocol.
556 */
0a7de745
A
557 struct inpcbhead *ipi_listhead;
558 uint32_t ipi_count;
39236c6e
A
559
560 /*
561 * Count of pcbs marked with INP2_TIMEWAIT flag.
562 */
0a7de745 563 uint32_t ipi_twcount;
39236c6e
A
564
565 /*
566 * Generation count -- incremented each time a connection is
567 * allocated or freed.
568 */
0a7de745 569 uint64_t ipi_gencnt;
39236c6e
A
570
571 /*
572 * Fields associated with port lookup and allocation.
573 */
0a7de745
A
574 uint16_t ipi_lastport;
575 uint16_t ipi_lastlow;
576 uint16_t ipi_lasthi;
39236c6e
A
577
578 /*
579 * Zone from which inpcbs are allocated for this protocol.
580 */
0a7de745 581 struct zone *ipi_zone;
39236c6e
A
582
583 /*
584 * Per-protocol hash of pcbs, hashed by local and foreign
585 * addresses and port numbers.
586 */
0a7de745
A
587 struct inpcbhead *ipi_hashbase;
588 u_long ipi_hashmask;
39236c6e
A
589
590 /*
591 * Per-protocol hash of pcbs, hashed by only local port number.
592 */
0a7de745
A
593 struct inpcbporthead *ipi_porthashbase;
594 u_long ipi_porthashmask;
39236c6e
A
595
596 /*
597 * Misc.
598 */
0a7de745
A
599 lck_attr_t *ipi_lock_attr;
600 lck_grp_t *ipi_lock_grp;
601 lck_grp_attr_t *ipi_lock_grp_attr;
39037602 602
0a7de745
A
603#define INPCBINFO_UPDATE_MSS 0x1
604#define INPCBINFO_HANDLE_LQM_ABORT 0x2
605 u_int32_t ipi_flags;
39236c6e
A
606};
607
0a7de745 608#define INP_PCBHASH(faddr, lport, fport, mask) \
1c79356b 609 (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
0a7de745 610#define INP_PCBPORTHASH(lport, mask) \
1c79356b
A
611 (ntohs((lport)) & (mask))
612
0a7de745 613#define INP_IS_FLOW_CONTROLLED(_inp_) \
39236c6e 614 ((_inp_)->inp_flags & INP_FLOW_CONTROLLED)
0a7de745
A
615#define INP_IS_FLOW_SUSPENDED(_inp_) \
616 (((_inp_)->inp_flags & INP_FLOW_SUSPENDED) || \
316670eb 617 ((_inp_)->inp_socket->so_flags & SOF_SUSPENDED))
0a7de745 618#define INP_WAIT_FOR_IF_FEEDBACK(_inp_) \
316670eb
A
619 (((_inp_)->inp_flags & (INP_FLOW_CONTROLLED | INP_FLOW_SUSPENDED)) != 0)
620
fe8ab488
A
621#define INP_NO_CELLULAR(_inp) \
622 ((_inp)->inp_flags & INP_NO_IFT_CELLULAR)
623#define INP_NO_EXPENSIVE(_inp) \
624 ((_inp)->inp_flags2 & INP2_NO_IFF_EXPENSIVE)
cb323159
A
625#define INP_NO_CONSTRAINED(_inp) \
626 ((_inp)->inp_flags2 & INP2_NO_IFF_CONSTRAINED)
0a7de745 627#define INP_AWDL_UNRESTRICTED(_inp) \
fe8ab488 628 ((_inp)->inp_flags2 & INP2_AWDL_UNRESTRICTED)
0a7de745 629#define INP_INTCOPROC_ALLOWED(_inp) \
39037602 630 ((_inp)->inp_flags2 & INP2_INTCOPROC_ALLOWED)
fe8ab488 631
316670eb 632#endif /* BSD_KERNEL_PRIVATE */
91447636 633
39236c6e
A
634/*
635 * Flags for inp_flags.
636 *
637 * Some of these are publicly defined for legacy reasons, as they are
638 * (unfortunately) used by certain applications to determine, at compile
639 * time, whether or not the OS supports certain features.
640 */
316670eb 641#ifdef BSD_KERNEL_PRIVATE
0a7de745
A
642#define INP_RECVOPTS 0x00000001 /* receive incoming IP options */
643#define INP_RECVRETOPTS 0x00000002 /* receive IP options for reply */
644#define INP_RECVDSTADDR 0x00000004 /* receive IP dst address */
645#define INP_HDRINCL 0x00000008 /* user supplies entire IP header */
646#define INP_HIGHPORT 0x00000010 /* user wants "high" port binding */
647#define INP_LOWPORT 0x00000020 /* user wants "low" port binding */
316670eb 648#endif /* BSD_KERNEL_PRIVATE */
9bccf70c 649
0a7de745 650#define INP_ANONPORT 0x00000040 /* port chosen for user */
316670eb
A
651
652#ifdef BSD_KERNEL_PRIVATE
0a7de745
A
653#define INP_RECVIF 0x00000080 /* receive incoming interface */
654#define INP_MTUDISC 0x00000100 /* unused */
655#define INP_STRIPHDR 0x00000200 /* strip hdrs in raw_ip (for OT) */
656#define INP_RECV_ANYIF 0x00000400 /* don't restrict inbound iface */
657#define INP_INADDR_ANY 0x00000800 /* local address wasn't specified */
658#define INP_IN6ADDR_ANY INP_INADDR_ANY
659#define INP_RECVTTL 0x00001000 /* receive incoming IP TTL */
660#define INP_UDP_NOCKSUM 0x00002000 /* turn off outbound UDP checksum */
661#define INP_BOUND_IF 0x00004000 /* bind socket to an interface */
316670eb
A
662#endif /* BSD_KERNEL_PRIVATE */
663
0a7de745 664#define IN6P_IPV6_V6ONLY 0x00008000 /* restrict AF_INET6 socket for v6 */
316670eb
A
665
666#ifdef BSD_KERNEL_PRIVATE
0a7de745
A
667#define IN6P_PKTINFO 0x00010000 /* receive IP6 dst and I/F */
668#define IN6P_HOPLIMIT 0x00020000 /* receive hoplimit */
669#define IN6P_HOPOPTS 0x00040000 /* receive hop-by-hop options */
670#define IN6P_DSTOPTS 0x00080000 /* receive dst options after rthdr */
671#define IN6P_RTHDR 0x00100000 /* receive routing header */
672#define IN6P_RTHDRDSTOPTS 0x00200000 /* receive dstoptions before rthdr */
673#define IN6P_TCLASS 0x00400000 /* receive traffic class value */
674#define INP_RECVTOS IN6P_TCLASS /* receive incoming IP TOS */
675#define IN6P_AUTOFLOWLABEL 0x00800000 /* attach flowlabel automatically */
316670eb 676#endif /* BSD_KERNEL_PRIVATE */
1c79356b 677
0a7de745 678#define IN6P_BINDV6ONLY 0x01000000 /* do not grab IPv4 traffic */
1c79356b 679
316670eb 680#ifdef BSD_KERNEL_PRIVATE
0a7de745 681#define IN6P_RFC2292 0x02000000 /* used RFC2292 API on the socket */
f427ee49 682#define IN6P_MTU 0x04000000 /* receive path MTU for IPv6 */
0a7de745
A
683#define INP_PKTINFO 0x08000000 /* rcv and snd PKTINFO for IPv4 */
684#define INP_FLOW_SUSPENDED 0x10000000 /* flow suspended */
685#define INP_NO_IFT_CELLULAR 0x20000000 /* do not use cellular interface */
686#define INP_FLOW_CONTROLLED 0x40000000 /* flow controlled */
687#define INP_FC_FEEDBACK 0x80000000 /* got interface flow adv feedback */
688
689#define INP_CONTROLOPTS \
39236c6e
A
690 (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|INP_RECVIF|INP_RECVTTL| \
691 INP_PKTINFO|IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|IN6P_DSTOPTS| \
692 IN6P_RTHDR|IN6P_RTHDRDSTOPTS|IN6P_TCLASS|IN6P_RFC2292|IN6P_MTU)
693
0a7de745 694#define INP_UNMAPPABLEOPTS \
d9a64523 695 (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|IN6P_AUTOFLOWLABEL)
1c79356b 696
39236c6e
A
697/*
698 * Flags for inp_flags2.
699 *
700 * Overflowed INP flags; use INP2 prefix to avoid misuse.
701 */
0a7de745
A
702#define INP2_TIMEWAIT 0x00000001 /* in TIMEWAIT */
703#define INP2_IN_FCTREE 0x00000002 /* in inp_fc_tree */
704#define INP2_WANT_APP_POLICY 0x00000004 /* necp app policy check is desired */
705#define INP2_NO_IFF_EXPENSIVE 0x00000008 /* do not use expensive interface */
706#define INP2_INHASHLIST 0x00000010 /* pcb is in inp_hash list */
707#define INP2_AWDL_UNRESTRICTED 0x00000020 /* AWDL restricted mode allowed */
708#define INP2_KEEPALIVE_OFFLOAD 0x00000040 /* Enable UDP or TCP keepalive offload */
709#define INP2_INTCOPROC_ALLOWED 0x00000080 /* Allow communication via internal co-processor interfaces */
710#define INP2_CONNECT_IN_PROGRESS 0x00000100 /* A connect call is in progress, so binds are intermediate steps */
711#define INP2_CLAT46_FLOW 0x00000200 /* The flow is going to use CLAT46 path */
cb323159
A
712#define INP2_EXTERNAL_PORT 0x00000400 /* The port is registered externally, for NECP listeners */
713#define INP2_NO_IFF_CONSTRAINED 0x00000800 /* do not use constrained interface */
f427ee49 714#define INP2_DONTFRAG 0x00001000 /* mark the DF bit in the IP header to avoid fragmentation */
1c79356b 715
39236c6e
A
716/*
717 * Flags passed to in_pcblookup*() functions.
718 */
0a7de745 719#define INPLOOKUP_WILDCARD 1
316670eb 720
0a7de745
A
721#define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
722#define sotoin6pcb(so) sotoinpcb(so)
9bccf70c 723
6d2010ae
A
724struct sysctl_req;
725
39236c6e
A
726extern int ipport_lowfirstauto;
727extern int ipport_lowlastauto;
728extern int ipport_firstauto;
729extern int ipport_lastauto;
730extern int ipport_hifirstauto;
731extern int ipport_hilastauto;
f427ee49
A
732extern int allow_udp_port_exhaustion;
733#define UDP_RANDOM_PORT_RESERVE 4096
39236c6e
A
734
735/* freshly allocated PCB, it's in use */
0a7de745 736#define INPCB_STATE_INUSE 0x1
39236c6e 737/* this pcb is sitting in a a cache */
0a7de745 738#define INPCB_STATE_CACHED 0x2
39236c6e 739/* should treat as gone, will be garbage collected and freed */
0a7de745 740#define INPCB_STATE_DEAD 0x3
39236c6e
A
741
742/* marked as ready to be garbaged collected, should be treated as not found */
0a7de745 743#define WNT_STOPUSING 0xffff
39236c6e 744/* that pcb is being acquired, do not recycle this time */
0a7de745 745#define WNT_ACQUIRE 0x1
39236c6e 746/* release acquired mode, can be garbage collected when wantcnt is null */
0a7de745 747#define WNT_RELEASE 0x2
39236c6e
A
748
749extern void in_pcbinit(void);
750extern void in_pcbinfo_attach(struct inpcbinfo *);
751extern int in_pcbinfo_detach(struct inpcbinfo *);
752
753/* type of timer to be scheduled by inpcb_gc_sched and inpcb_timer_sched */
754enum {
755 INPCB_TIMER_LAZY = 0x1,
756 INPCB_TIMER_FAST,
757 INPCB_TIMER_NODELAY
758};
759extern void inpcb_gc_sched(struct inpcbinfo *, u_int32_t type);
760extern void inpcb_timer_sched(struct inpcbinfo *, u_int32_t type);
761
762extern void in_losing(struct inpcb *);
763extern void in_rtchange(struct inpcb *, int);
764extern int in_pcballoc(struct socket *, struct inpcbinfo *, struct proc *);
765extern int in_pcbbind(struct inpcb *, struct sockaddr *, struct proc *);
766extern int in_pcbconnect(struct inpcb *, struct sockaddr *, struct proc *,
767 unsigned int, struct ifnet **);
768extern void in_pcbdetach(struct inpcb *);
769extern void in_pcbdispose(struct inpcb *);
770extern void in_pcbdisconnect(struct inpcb *);
771extern int in_pcbinshash(struct inpcb *, int);
772extern int in_pcbladdr(struct inpcb *, struct sockaddr *, struct in_addr *,
39037602 773 unsigned int, struct ifnet **, int);
b0d623f7 774extern struct inpcb *in_pcblookup_local(struct inpcbinfo *, struct in_addr,
39236c6e 775 u_int, int);
b0d623f7 776extern struct inpcb *in_pcblookup_local_and_cleanup(struct inpcbinfo *,
39236c6e 777 struct in_addr, u_int, int);
b0d623f7 778extern struct inpcb *in_pcblookup_hash(struct inpcbinfo *, struct in_addr,
39236c6e
A
779 u_int, struct in_addr, u_int, int, struct ifnet *);
780extern int in_pcblookup_hash_exists(struct inpcbinfo *, struct in_addr,
781 u_int, struct in_addr, u_int, int, uid_t *, gid_t *, struct ifnet *);
782extern void in_pcbnotifyall(struct inpcbinfo *, struct in_addr, int,
783 void (*)(struct inpcb *, int));
784extern void in_pcbrehash(struct inpcb *);
785extern int in_getpeeraddr(struct socket *, struct sockaddr **);
39236c6e 786extern int in_getsockaddr(struct socket *, struct sockaddr **);
5ba3f43e 787extern int in_getsockaddr_s(struct socket *, struct sockaddr_in *);
39236c6e
A
788extern int in_pcb_checkstate(struct inpcb *, int, int);
789extern void in_pcbremlists(struct inpcb *);
790extern void inpcb_to_compat(struct inpcb *, struct inpcb_compat *);
f427ee49 791#if XNU_TARGET_OS_OSX
39236c6e 792extern void inpcb_to_xinpcb64(struct inpcb *, struct xinpcb64 *);
f427ee49 793#endif /* XNU_TARGET_OS_OSX */
3e170ce0 794
39236c6e 795extern int get_pcblist_n(short, struct sysctl_req *, struct inpcbinfo *);
3e170ce0 796
39236c6e
A
797extern void inpcb_get_ports_used(u_int32_t, int, u_int32_t, bitstr_t *,
798 struct inpcbinfo *);
0a7de745
A
799#define INPCB_OPPORTUNISTIC_THROTTLEON 0x0001
800#define INPCB_OPPORTUNISTIC_SETCMD 0x0002
39236c6e
A
801extern uint32_t inpcb_count_opportunistic(unsigned int, struct inpcbinfo *,
802 u_int32_t);
803extern uint32_t inpcb_find_anypcb_byaddr(struct ifaddr *, struct inpcbinfo *);
804extern void inp_route_copyout(struct inpcb *, struct route *);
805extern void inp_route_copyin(struct inpcb *, struct route *);
806extern int inp_bindif(struct inpcb *, unsigned int, struct ifnet **);
807extern void inp_set_nocellular(struct inpcb *);
808extern void inp_clear_nocellular(struct inpcb *);
fe8ab488 809extern void inp_set_noexpensive(struct inpcb *);
cb323159 810extern void inp_set_noconstrained(struct inpcb *);
fe8ab488
A
811extern void inp_set_awdl_unrestricted(struct inpcb *);
812extern boolean_t inp_get_awdl_unrestricted(struct inpcb *);
813extern void inp_clear_awdl_unrestricted(struct inpcb *);
39037602
A
814extern void inp_set_intcoproc_allowed(struct inpcb *);
815extern boolean_t inp_get_intcoproc_allowed(struct inpcb *);
816extern void inp_clear_intcoproc_allowed(struct inpcb *);
fe8ab488
A
817#if NECP
818extern void inp_update_necp_policy(struct inpcb *, struct sockaddr *, struct sockaddr *, u_int);
819extern void inp_set_want_app_policy(struct inpcb *);
820extern void inp_clear_want_app_policy(struct inpcb *);
821#endif /* NECP */
316670eb 822extern u_int32_t inp_calc_flowhash(struct inpcb *);
39236c6e
A
823extern void inp_reset_fc_state(struct inpcb *);
824extern int inp_set_fc_state(struct inpcb *, int advcode);
825extern void inp_fc_unthrottle_tcp(struct inpcb *);
f427ee49 826extern void inp_fc_throttle_tcp(struct inpcb *inp);
39236c6e
A
827extern void inp_flowadv(uint32_t);
828extern int inp_flush(struct inpcb *, int);
829extern int inp_findinpcb_procinfo(struct inpcbinfo *, uint32_t, struct so_procinfo *);
830extern void inp_get_soprocinfo(struct inpcb *, struct so_procinfo *);
831extern int inp_update_policy(struct inpcb *);
fe8ab488
A
832extern boolean_t inp_restricted_recv(struct inpcb *, struct ifnet *);
833extern boolean_t inp_restricted_send(struct inpcb *, struct ifnet *);
39037602
A
834extern void inp_incr_sndbytes_total(struct socket *, int);
835extern void inp_decr_sndbytes_total(struct socket *, int);
836extern void inp_count_sndbytes(struct inpcb *, u_int32_t);
837extern void inp_incr_sndbytes_unsent(struct socket *, int32_t);
838extern void inp_decr_sndbytes_unsent(struct socket *, int32_t);
839extern int32_t inp_get_sndbytes_allunsent(struct socket *, u_int32_t);
840extern void inp_decr_sndbytes_allunsent(struct socket *, u_int32_t);
5ba3f43e
A
841extern void inp_set_activity_bitmap(struct inpcb *inp);
842extern void inp_get_activity_bitmap(struct inpcb *inp, activity_bitmap_t *b);
cb323159
A
843extern void inp_update_last_owner(struct socket *so, struct proc *p, struct proc *ep);
844extern void inp_copy_last_owner(struct socket *so, struct socket *head);
316670eb 845#endif /* BSD_KERNEL_PRIVATE */
316670eb 846#ifdef KERNEL_PRIVATE
39236c6e
A
847/* exported for PPP */
848extern void inp_clear_INP_INADDR_ANY(struct socket *);
f427ee49
A
849extern int inp_limit_companion_link(struct inpcbinfo *pcbinfo, u_int32_t limit);
850extern int inp_recover_companion_link(struct inpcbinfo *pcbinfo);
91447636 851#endif /* KERNEL_PRIVATE */
1c79356b 852#endif /* !_NETINET_IN_PCB_H_ */