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