1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1990, 1993 5 * The Regents of the University of California. 6 * Copyright (c) 2010-2011 Juniper Networks, Inc. 7 * All rights reserved. 8 * 9 * Portions of this software were developed by Robert N. M. Watson under 10 * contract to Juniper Networks, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93 37 * $FreeBSD$ 38 */ 39 40 #ifndef _NETINET_IN_PCB_H_ 41 #define _NETINET_IN_PCB_H_ 42 43 #include <sys/queue.h> 44 #include <sys/_lock.h> 45 #include <sys/_mutex.h> 46 #include <sys/_rwlock.h> 47 #include <net/route.h> 48 49 #ifdef _KERNEL 50 #include <sys/lock.h> 51 #include <sys/rwlock.h> 52 #include <net/vnet.h> 53 #include <vm/uma.h> 54 #endif 55 56 #define in6pcb inpcb /* for KAME src sync over BSD*'s */ 57 #define in6p_sp inp_sp /* for KAME src sync over BSD*'s */ 58 59 /* 60 * struct inpcb is the common protocol control block structure used in most 61 * IP transport protocols. 62 * 63 * Pointers to local and foreign host table entries, local and foreign socket 64 * numbers, and pointers up (to a socket structure) and down (to a 65 * protocol-specific control block) are stored here. 66 */ 67 LIST_HEAD(inpcbhead, inpcb); 68 LIST_HEAD(inpcbporthead, inpcbport); 69 typedef uint64_t inp_gen_t; 70 71 /* 72 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet. 73 * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing 74 * the following structure. 75 */ 76 struct in_addr_4in6 { 77 u_int32_t ia46_pad32[3]; 78 struct in_addr ia46_addr4; 79 }; 80 81 /* 82 * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553. in_conninfo has 83 * some extra padding to accomplish this. 84 * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport, 85 * lport, faddr to generate hash, so these fields shouldn't be moved. 86 */ 87 struct in_endpoints { 88 u_int16_t ie_fport; /* foreign port */ 89 u_int16_t ie_lport; /* local port */ 90 /* protocol dependent part, local and foreign addr */ 91 union { 92 /* foreign host table entry */ 93 struct in_addr_4in6 ie46_foreign; 94 struct in6_addr ie6_foreign; 95 } ie_dependfaddr; 96 union { 97 /* local host table entry */ 98 struct in_addr_4in6 ie46_local; 99 struct in6_addr ie6_local; 100 } ie_dependladdr; 101 u_int32_t ie6_zoneid; /* scope zone id */ 102 }; 103 #define ie_faddr ie_dependfaddr.ie46_foreign.ia46_addr4 104 #define ie_laddr ie_dependladdr.ie46_local.ia46_addr4 105 #define ie6_faddr ie_dependfaddr.ie6_foreign 106 #define ie6_laddr ie_dependladdr.ie6_local 107 108 /* 109 * XXX The defines for inc_* are hacks and should be changed to direct 110 * references. 111 */ 112 struct in_conninfo { 113 u_int8_t inc_flags; 114 u_int8_t inc_len; 115 u_int16_t inc_fibnum; /* XXX was pad, 16 bits is plenty */ 116 /* protocol dependent part */ 117 struct in_endpoints inc_ie; 118 }; 119 120 /* 121 * Flags for inc_flags. 122 */ 123 #define INC_ISIPV6 0x01 124 125 #define inc_isipv6 inc_flags /* temp compatibility */ 126 #define inc_fport inc_ie.ie_fport 127 #define inc_lport inc_ie.ie_lport 128 #define inc_faddr inc_ie.ie_faddr 129 #define inc_laddr inc_ie.ie_laddr 130 #define inc6_faddr inc_ie.ie6_faddr 131 #define inc6_laddr inc_ie.ie6_laddr 132 #define inc6_zoneid inc_ie.ie6_zoneid 133 134 #if defined(_KERNEL) || defined(_WANT_INPCB) 135 /* 136 * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and 137 * IPv6 sockets. In the case of TCP and UDP, further per-connection state is 138 * hung off of inp_ppcb most of the time. Almost all fields of struct inpcb 139 * are static after creation or protected by a per-inpcb rwlock, inp_lock. A 140 * few fields are protected by multiple locks as indicated in the locking notes 141 * below. For these fields, all of the listed locks must be write-locked for 142 * any modifications. However, these fields can be safely read while any one of 143 * the listed locks are read-locked. This model can permit greater concurrency 144 * for read operations. For example, connections can be looked up while only 145 * holding a read lock on the global pcblist lock. This is important for 146 * performance when attempting to find the connection for a packet given its IP 147 * and port tuple. 148 * 149 * One noteworthy exception is that the global pcbinfo lock follows a different 150 * set of rules in relation to the inp_list field. Rather than being 151 * write-locked for modifications and read-locked for list iterations, it must 152 * be read-locked during modifications and write-locked during list iterations. 153 * This ensures that the relatively rare global list iterations safely walk a 154 * stable snapshot of connections while allowing more common list modifications 155 * to safely grab the pcblist lock just while adding or removing a connection 156 * from the global list. 157 * 158 * Key: 159 * (c) - Constant after initialization 160 * (g) - Protected by the pcbgroup lock 161 * (i) - Protected by the inpcb lock 162 * (p) - Protected by the pcbinfo lock for the inpcb 163 * (l) - Protected by the pcblist lock for the inpcb 164 * (h) - Protected by the pcbhash lock for the inpcb 165 * (s) - Protected by another subsystem's locks 166 * (x) - Undefined locking 167 * 168 * A few other notes: 169 * 170 * When a read lock is held, stability of the field is guaranteed; to write 171 * to a field, a write lock must generally be held. 172 * 173 * netinet/netinet6-layer code should not assume that the inp_socket pointer 174 * is safe to dereference without inp_lock being held, even for protocols 175 * other than TCP (where the inpcb persists during TIMEWAIT even after the 176 * socket has been freed), or there may be close(2)-related races. 177 * 178 * The inp_vflag field is overloaded, and would otherwise ideally be (c). 179 * 180 * TODO: Currently only the TCP stack is leveraging the global pcbinfo lock 181 * read-lock usage during modification, this model can be applied to other 182 * protocols (especially SCTP). 183 */ 184 struct icmp6_filter; 185 struct inpcbpolicy; 186 struct m_snd_tag; 187 struct inpcb { 188 /* Cache line #1 (amd64) */ 189 LIST_ENTRY(inpcb) inp_hash; /* (h/i) hash list */ 190 LIST_ENTRY(inpcb) inp_pcbgrouphash; /* (g/i) hash list */ 191 struct rwlock inp_lock; 192 /* Cache line #2 (amd64) */ 193 #define inp_start_zero inp_refcount 194 #define inp_zero_size (sizeof(struct inpcb) - \ 195 offsetof(struct inpcb, inp_start_zero)) 196 u_int inp_refcount; /* (i) refcount */ 197 int inp_flags; /* (i) generic IP/datagram flags */ 198 int inp_flags2; /* (i) generic IP/datagram flags #2*/ 199 void *inp_ppcb; /* (i) pointer to per-protocol pcb */ 200 struct socket *inp_socket; /* (i) back pointer to socket */ 201 struct inpcbinfo *inp_pcbinfo; /* (c) PCB list info */ 202 struct inpcbgroup *inp_pcbgroup; /* (g/i) PCB group list */ 203 LIST_ENTRY(inpcb) inp_pcbgroup_wild; /* (g/i/h) group wildcard entry */ 204 struct ucred *inp_cred; /* (c) cache of socket cred */ 205 u_int32_t inp_flow; /* (i) IPv6 flow information */ 206 u_char inp_vflag; /* (i) IP version flag (v4/v6) */ 207 u_char inp_ip_ttl; /* (i) time to live proto */ 208 u_char inp_ip_p; /* (c) protocol proto */ 209 u_char inp_ip_minttl; /* (i) minimum TTL or drop */ 210 uint32_t inp_flowid; /* (x) flow id / queue id */ 211 struct m_snd_tag *inp_snd_tag; /* (i) send tag for outgoing mbufs */ 212 uint32_t inp_flowtype; /* (x) M_HASHTYPE value */ 213 uint32_t inp_rss_listen_bucket; /* (x) overridden RSS listen bucket */ 214 215 /* Local and foreign ports, local and foreign addr. */ 216 struct in_conninfo inp_inc; /* (i) list for PCB's local port */ 217 218 /* MAC and IPSEC policy information. */ 219 struct label *inp_label; /* (i) MAC label */ 220 struct inpcbpolicy *inp_sp; /* (s) for IPSEC */ 221 222 /* Protocol-dependent part; options. */ 223 struct { 224 u_char inp_ip_tos; /* (i) type of service proto */ 225 struct mbuf *inp_options; /* (i) IP options */ 226 struct ip_moptions *inp_moptions; /* (i) mcast options */ 227 }; 228 struct { 229 /* (i) IP options */ 230 struct mbuf *in6p_options; 231 /* (i) IP6 options for outgoing packets */ 232 struct ip6_pktopts *in6p_outputopts; 233 /* (i) IP multicast options */ 234 struct ip6_moptions *in6p_moptions; 235 /* (i) ICMPv6 code type filter */ 236 struct icmp6_filter *in6p_icmp6filt; 237 /* (i) IPV6_CHECKSUM setsockopt */ 238 int in6p_cksum; 239 short in6p_hops; 240 }; 241 LIST_ENTRY(inpcb) inp_portlist; /* (i/h) */ 242 struct inpcbport *inp_phd; /* (i/h) head of this list */ 243 inp_gen_t inp_gencnt; /* (c) generation count */ 244 struct llentry *inp_lle; /* cached L2 information */ 245 rt_gen_t inp_rt_cookie; /* generation for route entry */ 246 union { /* cached L3 information */ 247 struct route inp_route; 248 struct route_in6 inp_route6; 249 }; 250 LIST_ENTRY(inpcb) inp_list; /* (p/l) list for all PCBs for proto */ 251 /* (p[w]) for list iteration */ 252 /* (p[r]/l) for addition/removal */ 253 }; 254 #endif /* _KERNEL */ 255 256 #define inp_fport inp_inc.inc_fport 257 #define inp_lport inp_inc.inc_lport 258 #define inp_faddr inp_inc.inc_faddr 259 #define inp_laddr inp_inc.inc_laddr 260 261 #define in6p_faddr inp_inc.inc6_faddr 262 #define in6p_laddr inp_inc.inc6_laddr 263 #define in6p_zoneid inp_inc.inc6_zoneid 264 #define in6p_flowinfo inp_flow 265 266 #define inp_vnet inp_pcbinfo->ipi_vnet 267 268 /* 269 * The range of the generation count, as used in this implementation, is 9e19. 270 * We would have to create 300 billion connections per second for this number 271 * to roll over in a year. This seems sufficiently unlikely that we simply 272 * don't concern ourselves with that possibility. 273 */ 274 275 /* 276 * Interface exported to userland by various protocols which use inpcbs. Hack 277 * alert -- only define if struct xsocket is in scope. 278 * Fields prefixed with "xi_" are unique to this structure, and the rest 279 * match fields in the struct inpcb, to ease coding and porting. 280 * 281 * Legend: 282 * (s) - used by userland utilities in src 283 * (p) - used by utilities in ports 284 * (3) - is known to be used by third party software not in ports 285 * (n) - no known usage 286 */ 287 #ifdef _SYS_SOCKETVAR_H_ 288 struct xinpcb { 289 size_t xi_len; /* length of this structure */ 290 struct xsocket xi_socket; /* (s,p) */ 291 struct in_conninfo inp_inc; /* (s,p) */ 292 uint64_t inp_gencnt; /* (s,p) */ 293 union { 294 void *inp_ppcb; /* (s) netstat(1) */ 295 int64_t ph_ppcb; 296 }; 297 int64_t inp_spare64[4]; 298 uint32_t inp_flow; /* (s) */ 299 uint32_t inp_flowid; /* (s) */ 300 uint32_t inp_flowtype; /* (s) */ 301 int32_t inp_flags; /* (s,p) */ 302 int32_t inp_flags2; /* (s) */ 303 int32_t inp_rss_listen_bucket; /* (n) */ 304 int32_t in6p_cksum; /* (n) */ 305 int32_t inp_spare32[4]; 306 uint16_t in6p_hops; /* (n) */ 307 uint8_t inp_ip_tos; /* (n) */ 308 int8_t pad8; 309 uint8_t inp_vflag; /* (s,p) */ 310 uint8_t inp_ip_ttl; /* (n) */ 311 uint8_t inp_ip_p; /* (n) */ 312 uint8_t inp_ip_minttl; /* (n) */ 313 int8_t inp_spare8[4]; 314 } __aligned(8); 315 316 struct xinpgen { 317 size_t xig_len; /* length of this structure */ 318 u_int xig_count; /* number of PCBs at this time */ 319 inp_gen_t xig_gen; /* generation count at this time */ 320 so_gen_t xig_sogen; /* socket generation count this time */ 321 } __aligned(8); 322 #ifdef _KERNEL 323 void in_pcbtoxinpcb(const struct inpcb *, struct xinpcb *); 324 #endif 325 #endif /* _SYS_SOCKETVAR_H_ */ 326 327 struct inpcbport { 328 LIST_ENTRY(inpcbport) phd_hash; 329 struct inpcbhead phd_pcblist; 330 u_short phd_port; 331 }; 332 333 /*- 334 * Global data structure for each high-level protocol (UDP, TCP, ...) in both 335 * IPv4 and IPv6. Holds inpcb lists and information for managing them. 336 * 337 * Each pcbinfo is protected by three locks: ipi_lock, ipi_hash_lock and 338 * ipi_list_lock: 339 * - ipi_lock covering the global pcb list stability during loop iteration, 340 * - ipi_hash_lock covering the hashed lookup tables, 341 * - ipi_list_lock covering mutable global fields (such as the global 342 * pcb list) 343 * 344 * The lock order is: 345 * 346 * ipi_lock (before) 347 * inpcb locks (before) 348 * ipi_list locks (before) 349 * {ipi_hash_lock, pcbgroup locks} 350 * 351 * Locking key: 352 * 353 * (c) Constant or nearly constant after initialisation 354 * (g) Locked by ipi_lock 355 * (l) Locked by ipi_list_lock 356 * (h) Read using either ipi_hash_lock or inpcb lock; write requires both 357 * (p) Protected by one or more pcbgroup locks 358 * (x) Synchronisation properties poorly defined 359 */ 360 struct inpcbinfo { 361 /* 362 * Global lock protecting full inpcb list traversal 363 */ 364 struct rwlock ipi_lock; 365 366 /* 367 * Global list of inpcbs on the protocol. 368 */ 369 struct inpcbhead *ipi_listhead; /* (g/l) */ 370 u_int ipi_count; /* (l) */ 371 372 /* 373 * Generation count -- incremented each time a connection is allocated 374 * or freed. 375 */ 376 u_quad_t ipi_gencnt; /* (l) */ 377 378 /* 379 * Fields associated with port lookup and allocation. 380 */ 381 u_short ipi_lastport; /* (x) */ 382 u_short ipi_lastlow; /* (x) */ 383 u_short ipi_lasthi; /* (x) */ 384 385 /* 386 * UMA zone from which inpcbs are allocated for this protocol. 387 */ 388 struct uma_zone *ipi_zone; /* (c) */ 389 390 /* 391 * Connection groups associated with this protocol. These fields are 392 * constant, but pcbgroup structures themselves are protected by 393 * per-pcbgroup locks. 394 */ 395 struct inpcbgroup *ipi_pcbgroups; /* (c) */ 396 u_int ipi_npcbgroups; /* (c) */ 397 u_int ipi_hashfields; /* (c) */ 398 399 /* 400 * Global lock protecting non-pcbgroup hash lookup tables. 401 */ 402 struct rwlock ipi_hash_lock; 403 404 /* 405 * Global hash of inpcbs, hashed by local and foreign addresses and 406 * port numbers. 407 */ 408 struct inpcbhead *ipi_hashbase; /* (h) */ 409 u_long ipi_hashmask; /* (h) */ 410 411 /* 412 * Global hash of inpcbs, hashed by only local port number. 413 */ 414 struct inpcbporthead *ipi_porthashbase; /* (h) */ 415 u_long ipi_porthashmask; /* (h) */ 416 417 /* 418 * List of wildcard inpcbs for use with pcbgroups. In the past, was 419 * per-pcbgroup but is now global. All pcbgroup locks must be held 420 * to modify the list, so any is sufficient to read it. 421 */ 422 struct inpcbhead *ipi_wildbase; /* (p) */ 423 u_long ipi_wildmask; /* (p) */ 424 425 /* 426 * Pointer to network stack instance 427 */ 428 struct vnet *ipi_vnet; /* (c) */ 429 430 /* 431 * general use 2 432 */ 433 void *ipi_pspare[2]; 434 435 /* 436 * Global lock protecting global inpcb list, inpcb count, etc. 437 */ 438 struct rwlock ipi_list_lock; 439 }; 440 441 #ifdef _KERNEL 442 /* 443 * Connection groups hold sets of connections that have similar CPU/thread 444 * affinity. Each connection belongs to exactly one connection group. 445 */ 446 struct inpcbgroup { 447 /* 448 * Per-connection group hash of inpcbs, hashed by local and foreign 449 * addresses and port numbers. 450 */ 451 struct inpcbhead *ipg_hashbase; /* (c) */ 452 u_long ipg_hashmask; /* (c) */ 453 454 /* 455 * Notional affinity of this pcbgroup. 456 */ 457 u_int ipg_cpu; /* (p) */ 458 459 /* 460 * Per-connection group lock, not to be confused with ipi_lock. 461 * Protects the hash table hung off the group, but also the global 462 * wildcard list in inpcbinfo. 463 */ 464 struct mtx ipg_lock; 465 } __aligned(CACHE_LINE_SIZE); 466 467 #define INP_LOCK_INIT(inp, d, t) \ 468 rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE | RW_DUPOK) 469 #define INP_LOCK_DESTROY(inp) rw_destroy(&(inp)->inp_lock) 470 #define INP_RLOCK(inp) rw_rlock(&(inp)->inp_lock) 471 #define INP_WLOCK(inp) rw_wlock(&(inp)->inp_lock) 472 #define INP_TRY_RLOCK(inp) rw_try_rlock(&(inp)->inp_lock) 473 #define INP_TRY_WLOCK(inp) rw_try_wlock(&(inp)->inp_lock) 474 #define INP_RUNLOCK(inp) rw_runlock(&(inp)->inp_lock) 475 #define INP_WUNLOCK(inp) rw_wunlock(&(inp)->inp_lock) 476 #define INP_TRY_UPGRADE(inp) rw_try_upgrade(&(inp)->inp_lock) 477 #define INP_DOWNGRADE(inp) rw_downgrade(&(inp)->inp_lock) 478 #define INP_WLOCKED(inp) rw_wowned(&(inp)->inp_lock) 479 #define INP_LOCK_ASSERT(inp) rw_assert(&(inp)->inp_lock, RA_LOCKED) 480 #define INP_RLOCK_ASSERT(inp) rw_assert(&(inp)->inp_lock, RA_RLOCKED) 481 #define INP_WLOCK_ASSERT(inp) rw_assert(&(inp)->inp_lock, RA_WLOCKED) 482 #define INP_UNLOCK_ASSERT(inp) rw_assert(&(inp)->inp_lock, RA_UNLOCKED) 483 484 /* 485 * These locking functions are for inpcb consumers outside of sys/netinet, 486 * more specifically, they were added for the benefit of TOE drivers. The 487 * macros are reserved for use by the stack. 488 */ 489 void inp_wlock(struct inpcb *); 490 void inp_wunlock(struct inpcb *); 491 void inp_rlock(struct inpcb *); 492 void inp_runlock(struct inpcb *); 493 494 #ifdef INVARIANT_SUPPORT 495 void inp_lock_assert(struct inpcb *); 496 void inp_unlock_assert(struct inpcb *); 497 #else 498 #define inp_lock_assert(inp) do {} while (0) 499 #define inp_unlock_assert(inp) do {} while (0) 500 #endif 501 502 void inp_apply_all(void (*func)(struct inpcb *, void *), void *arg); 503 int inp_ip_tos_get(const struct inpcb *inp); 504 void inp_ip_tos_set(struct inpcb *inp, int val); 505 struct socket * 506 inp_inpcbtosocket(struct inpcb *inp); 507 struct tcpcb * 508 inp_inpcbtotcpcb(struct inpcb *inp); 509 void inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 510 uint32_t *faddr, uint16_t *fp); 511 short inp_so_options(const struct inpcb *inp); 512 513 #endif /* _KERNEL */ 514 515 #define INP_INFO_LOCK_INIT(ipi, d) \ 516 rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE) 517 #define INP_INFO_LOCK_DESTROY(ipi) rw_destroy(&(ipi)->ipi_lock) 518 #define INP_INFO_RLOCK(ipi) rw_rlock(&(ipi)->ipi_lock) 519 #define INP_INFO_WLOCK(ipi) rw_wlock(&(ipi)->ipi_lock) 520 #define INP_INFO_TRY_RLOCK(ipi) rw_try_rlock(&(ipi)->ipi_lock) 521 #define INP_INFO_TRY_WLOCK(ipi) rw_try_wlock(&(ipi)->ipi_lock) 522 #define INP_INFO_TRY_UPGRADE(ipi) rw_try_upgrade(&(ipi)->ipi_lock) 523 #define INP_INFO_WLOCKED(ipi) rw_wowned(&(ipi)->ipi_lock) 524 #define INP_INFO_RUNLOCK(ipi) rw_runlock(&(ipi)->ipi_lock) 525 #define INP_INFO_WUNLOCK(ipi) rw_wunlock(&(ipi)->ipi_lock) 526 #define INP_INFO_LOCK_ASSERT(ipi) rw_assert(&(ipi)->ipi_lock, RA_LOCKED) 527 #define INP_INFO_RLOCK_ASSERT(ipi) rw_assert(&(ipi)->ipi_lock, RA_RLOCKED) 528 #define INP_INFO_WLOCK_ASSERT(ipi) rw_assert(&(ipi)->ipi_lock, RA_WLOCKED) 529 #define INP_INFO_UNLOCK_ASSERT(ipi) rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED) 530 531 #define INP_LIST_LOCK_INIT(ipi, d) \ 532 rw_init_flags(&(ipi)->ipi_list_lock, (d), 0) 533 #define INP_LIST_LOCK_DESTROY(ipi) rw_destroy(&(ipi)->ipi_list_lock) 534 #define INP_LIST_RLOCK(ipi) rw_rlock(&(ipi)->ipi_list_lock) 535 #define INP_LIST_WLOCK(ipi) rw_wlock(&(ipi)->ipi_list_lock) 536 #define INP_LIST_TRY_RLOCK(ipi) rw_try_rlock(&(ipi)->ipi_list_lock) 537 #define INP_LIST_TRY_WLOCK(ipi) rw_try_wlock(&(ipi)->ipi_list_lock) 538 #define INP_LIST_TRY_UPGRADE(ipi) rw_try_upgrade(&(ipi)->ipi_list_lock) 539 #define INP_LIST_RUNLOCK(ipi) rw_runlock(&(ipi)->ipi_list_lock) 540 #define INP_LIST_WUNLOCK(ipi) rw_wunlock(&(ipi)->ipi_list_lock) 541 #define INP_LIST_LOCK_ASSERT(ipi) \ 542 rw_assert(&(ipi)->ipi_list_lock, RA_LOCKED) 543 #define INP_LIST_RLOCK_ASSERT(ipi) \ 544 rw_assert(&(ipi)->ipi_list_lock, RA_RLOCKED) 545 #define INP_LIST_WLOCK_ASSERT(ipi) \ 546 rw_assert(&(ipi)->ipi_list_lock, RA_WLOCKED) 547 #define INP_LIST_UNLOCK_ASSERT(ipi) \ 548 rw_assert(&(ipi)->ipi_list_lock, RA_UNLOCKED) 549 550 #define INP_HASH_LOCK_INIT(ipi, d) \ 551 rw_init_flags(&(ipi)->ipi_hash_lock, (d), 0) 552 #define INP_HASH_LOCK_DESTROY(ipi) rw_destroy(&(ipi)->ipi_hash_lock) 553 #define INP_HASH_RLOCK(ipi) rw_rlock(&(ipi)->ipi_hash_lock) 554 #define INP_HASH_WLOCK(ipi) rw_wlock(&(ipi)->ipi_hash_lock) 555 #define INP_HASH_RUNLOCK(ipi) rw_runlock(&(ipi)->ipi_hash_lock) 556 #define INP_HASH_WUNLOCK(ipi) rw_wunlock(&(ipi)->ipi_hash_lock) 557 #define INP_HASH_LOCK_ASSERT(ipi) rw_assert(&(ipi)->ipi_hash_lock, \ 558 RA_LOCKED) 559 #define INP_HASH_WLOCK_ASSERT(ipi) rw_assert(&(ipi)->ipi_hash_lock, \ 560 RA_WLOCKED) 561 562 #define INP_GROUP_LOCK_INIT(ipg, d) mtx_init(&(ipg)->ipg_lock, (d), NULL, \ 563 MTX_DEF | MTX_DUPOK) 564 #define INP_GROUP_LOCK_DESTROY(ipg) mtx_destroy(&(ipg)->ipg_lock) 565 566 #define INP_GROUP_LOCK(ipg) mtx_lock(&(ipg)->ipg_lock) 567 #define INP_GROUP_LOCK_ASSERT(ipg) mtx_assert(&(ipg)->ipg_lock, MA_OWNED) 568 #define INP_GROUP_UNLOCK(ipg) mtx_unlock(&(ipg)->ipg_lock) 569 570 #define INP_PCBHASH(faddr, lport, fport, mask) \ 571 (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask)) 572 #define INP_PCBPORTHASH(lport, mask) \ 573 (ntohs((lport)) & (mask)) 574 #define INP6_PCBHASHKEY(faddr) ((faddr)->s6_addr32[3]) 575 576 /* 577 * Flags for inp_vflags -- historically version flags only 578 */ 579 #define INP_IPV4 0x1 580 #define INP_IPV6 0x2 581 #define INP_IPV6PROTO 0x4 /* opened under IPv6 protocol */ 582 583 /* 584 * Flags for inp_flags. 585 */ 586 #define INP_RECVOPTS 0x00000001 /* receive incoming IP options */ 587 #define INP_RECVRETOPTS 0x00000002 /* receive IP options for reply */ 588 #define INP_RECVDSTADDR 0x00000004 /* receive IP dst address */ 589 #define INP_HDRINCL 0x00000008 /* user supplies entire IP header */ 590 #define INP_HIGHPORT 0x00000010 /* user wants "high" port binding */ 591 #define INP_LOWPORT 0x00000020 /* user wants "low" port binding */ 592 #define INP_ANONPORT 0x00000040 /* port chosen for user */ 593 #define INP_RECVIF 0x00000080 /* receive incoming interface */ 594 #define INP_MTUDISC 0x00000100 /* user can do MTU discovery */ 595 /* 0x000200 unused: was INP_FAITH */ 596 #define INP_RECVTTL 0x00000400 /* receive incoming IP TTL */ 597 #define INP_DONTFRAG 0x00000800 /* don't fragment packet */ 598 #define INP_BINDANY 0x00001000 /* allow bind to any address */ 599 #define INP_INHASHLIST 0x00002000 /* in_pcbinshash() has been called */ 600 #define INP_RECVTOS 0x00004000 /* receive incoming IP TOS */ 601 #define IN6P_IPV6_V6ONLY 0x00008000 /* restrict AF_INET6 socket for v6 */ 602 #define IN6P_PKTINFO 0x00010000 /* receive IP6 dst and I/F */ 603 #define IN6P_HOPLIMIT 0x00020000 /* receive hoplimit */ 604 #define IN6P_HOPOPTS 0x00040000 /* receive hop-by-hop options */ 605 #define IN6P_DSTOPTS 0x00080000 /* receive dst options after rthdr */ 606 #define IN6P_RTHDR 0x00100000 /* receive routing header */ 607 #define IN6P_RTHDRDSTOPTS 0x00200000 /* receive dstoptions before rthdr */ 608 #define IN6P_TCLASS 0x00400000 /* receive traffic class value */ 609 #define IN6P_AUTOFLOWLABEL 0x00800000 /* attach flowlabel automatically */ 610 #define INP_TIMEWAIT 0x01000000 /* in TIMEWAIT, ppcb is tcptw */ 611 #define INP_ONESBCAST 0x02000000 /* send all-ones broadcast */ 612 #define INP_DROPPED 0x04000000 /* protocol drop flag */ 613 #define INP_SOCKREF 0x08000000 /* strong socket reference */ 614 #define INP_RESERVED_0 0x10000000 /* reserved field */ 615 #define INP_RESERVED_1 0x20000000 /* reserved field */ 616 #define IN6P_RFC2292 0x40000000 /* used RFC2292 API on the socket */ 617 #define IN6P_MTU 0x80000000 /* receive path MTU */ 618 619 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\ 620 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\ 621 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\ 622 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\ 623 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\ 624 IN6P_MTU) 625 626 /* 627 * Flags for inp_flags2. 628 */ 629 #define INP_LLE_VALID 0x00000001 /* cached lle is valid */ 630 #define INP_RT_VALID 0x00000002 /* cached rtentry is valid */ 631 #define INP_PCBGROUPWILD 0x00000004 /* in pcbgroup wildcard list */ 632 #define INP_REUSEPORT 0x00000008 /* SO_REUSEPORT option is set */ 633 #define INP_FREED 0x00000010 /* inp itself is not valid */ 634 #define INP_REUSEADDR 0x00000020 /* SO_REUSEADDR option is set */ 635 #define INP_BINDMULTI 0x00000040 /* IP_BINDMULTI option is set */ 636 #define INP_RSS_BUCKET_SET 0x00000080 /* IP_RSS_LISTEN_BUCKET is set */ 637 #define INP_RECVFLOWID 0x00000100 /* populate recv datagram with flow info */ 638 #define INP_RECVRSSBUCKETID 0x00000200 /* populate recv datagram with bucket id */ 639 #define INP_RATE_LIMIT_CHANGED 0x00000400 /* rate limit needs attention */ 640 #define INP_ORIGDSTADDR 0x00000800 /* receive IP dst address/port */ 641 642 /* 643 * Flags passed to in_pcblookup*() functions. 644 */ 645 #define INPLOOKUP_WILDCARD 0x00000001 /* Allow wildcard sockets. */ 646 #define INPLOOKUP_RLOCKPCB 0x00000002 /* Return inpcb read-locked. */ 647 #define INPLOOKUP_WLOCKPCB 0x00000004 /* Return inpcb write-locked. */ 648 649 #define INPLOOKUP_MASK (INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \ 650 INPLOOKUP_WLOCKPCB) 651 652 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb) 653 #define sotoin6pcb(so) sotoinpcb(so) /* for KAME src sync over BSD*'s */ 654 655 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family 656 657 #define INP_CHECK_SOCKAF(so, af) (INP_SOCKAF(so) == af) 658 659 /* 660 * Constants for pcbinfo.ipi_hashfields. 661 */ 662 #define IPI_HASHFIELDS_NONE 0 663 #define IPI_HASHFIELDS_2TUPLE 1 664 #define IPI_HASHFIELDS_4TUPLE 2 665 666 #ifdef _KERNEL 667 VNET_DECLARE(int, ipport_reservedhigh); 668 VNET_DECLARE(int, ipport_reservedlow); 669 VNET_DECLARE(int, ipport_lowfirstauto); 670 VNET_DECLARE(int, ipport_lowlastauto); 671 VNET_DECLARE(int, ipport_firstauto); 672 VNET_DECLARE(int, ipport_lastauto); 673 VNET_DECLARE(int, ipport_hifirstauto); 674 VNET_DECLARE(int, ipport_hilastauto); 675 VNET_DECLARE(int, ipport_randomized); 676 VNET_DECLARE(int, ipport_randomcps); 677 VNET_DECLARE(int, ipport_randomtime); 678 VNET_DECLARE(int, ipport_stoprandom); 679 VNET_DECLARE(int, ipport_tcpallocs); 680 681 #define V_ipport_reservedhigh VNET(ipport_reservedhigh) 682 #define V_ipport_reservedlow VNET(ipport_reservedlow) 683 #define V_ipport_lowfirstauto VNET(ipport_lowfirstauto) 684 #define V_ipport_lowlastauto VNET(ipport_lowlastauto) 685 #define V_ipport_firstauto VNET(ipport_firstauto) 686 #define V_ipport_lastauto VNET(ipport_lastauto) 687 #define V_ipport_hifirstauto VNET(ipport_hifirstauto) 688 #define V_ipport_hilastauto VNET(ipport_hilastauto) 689 #define V_ipport_randomized VNET(ipport_randomized) 690 #define V_ipport_randomcps VNET(ipport_randomcps) 691 #define V_ipport_randomtime VNET(ipport_randomtime) 692 #define V_ipport_stoprandom VNET(ipport_stoprandom) 693 #define V_ipport_tcpallocs VNET(ipport_tcpallocs) 694 695 void in_pcbinfo_destroy(struct inpcbinfo *); 696 void in_pcbinfo_init(struct inpcbinfo *, const char *, struct inpcbhead *, 697 int, int, char *, uma_init, u_int); 698 699 int in_pcbbind_check_bindmulti(const struct inpcb *ni, 700 const struct inpcb *oi); 701 702 struct inpcbgroup * 703 in_pcbgroup_byhash(struct inpcbinfo *, u_int, uint32_t); 704 struct inpcbgroup * 705 in_pcbgroup_byinpcb(struct inpcb *); 706 struct inpcbgroup * 707 in_pcbgroup_bytuple(struct inpcbinfo *, struct in_addr, u_short, 708 struct in_addr, u_short); 709 void in_pcbgroup_destroy(struct inpcbinfo *); 710 int in_pcbgroup_enabled(struct inpcbinfo *); 711 void in_pcbgroup_init(struct inpcbinfo *, u_int, int); 712 void in_pcbgroup_remove(struct inpcb *); 713 void in_pcbgroup_update(struct inpcb *); 714 void in_pcbgroup_update_mbuf(struct inpcb *, struct mbuf *); 715 716 void in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *); 717 int in_pcballoc(struct socket *, struct inpcbinfo *); 718 int in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *); 719 int in_pcb_lport(struct inpcb *, struct in_addr *, u_short *, 720 struct ucred *, int); 721 int in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *, 722 u_short *, struct ucred *); 723 int in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *); 724 int in_pcbconnect_mbuf(struct inpcb *, struct sockaddr *, struct ucred *, 725 struct mbuf *); 726 int in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *, 727 u_short *, in_addr_t *, u_short *, struct inpcb **, 728 struct ucred *); 729 void in_pcbdetach(struct inpcb *); 730 void in_pcbdisconnect(struct inpcb *); 731 void in_pcbdrop(struct inpcb *); 732 void in_pcbfree(struct inpcb *); 733 int in_pcbinshash(struct inpcb *); 734 int in_pcbinshash_nopcbgroup(struct inpcb *); 735 int in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *, 736 struct ucred *); 737 struct inpcb * 738 in_pcblookup_local(struct inpcbinfo *, 739 struct in_addr, u_short, int, struct ucred *); 740 struct inpcb * 741 in_pcblookup(struct inpcbinfo *, struct in_addr, u_int, 742 struct in_addr, u_int, int, struct ifnet *); 743 struct inpcb * 744 in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int, 745 struct in_addr, u_int, int, struct ifnet *, struct mbuf *); 746 void in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr, 747 int, struct inpcb *(*)(struct inpcb *, int)); 748 void in_pcbref(struct inpcb *); 749 void in_pcbrehash(struct inpcb *); 750 void in_pcbrehash_mbuf(struct inpcb *, struct mbuf *); 751 int in_pcbrele(struct inpcb *); 752 int in_pcbrele_rlocked(struct inpcb *); 753 int in_pcbrele_wlocked(struct inpcb *); 754 void in_losing(struct inpcb *); 755 void in_pcbsetsolabel(struct socket *so); 756 int in_getpeeraddr(struct socket *so, struct sockaddr **nam); 757 int in_getsockaddr(struct socket *so, struct sockaddr **nam); 758 struct sockaddr * 759 in_sockaddr(in_port_t port, struct in_addr *addr); 760 void in_pcbsosetlabel(struct socket *so); 761 #ifdef RATELIMIT 762 int in_pcbattach_txrtlmt(struct inpcb *, struct ifnet *, uint32_t, uint32_t, uint32_t); 763 void in_pcbdetach_txrtlmt(struct inpcb *); 764 int in_pcbmodify_txrtlmt(struct inpcb *, uint32_t); 765 int in_pcbquery_txrtlmt(struct inpcb *, uint32_t *); 766 int in_pcbquery_txrlevel(struct inpcb *, uint32_t *); 767 void in_pcboutput_txrtlmt(struct inpcb *, struct ifnet *, struct mbuf *); 768 void in_pcboutput_eagain(struct inpcb *); 769 #endif 770 #endif /* _KERNEL */ 771 772 #endif /* !_NETINET_IN_PCB_H_ */ 773