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