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