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