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