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 37 #ifndef _NETINET_IN_PCB_H_ 38 #define _NETINET_IN_PCB_H_ 39 40 /* 41 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet. 42 * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing 43 * the following structure. This requires padding always be zeroed out, 44 * which is done right after inpcb allocation and stays through its lifetime. 45 */ 46 struct in_addr_4in6 { 47 uint32_t ia46_pad32[3]; 48 struct in_addr ia46_addr4; 49 }; 50 51 union in_dependaddr { 52 struct in_addr_4in6 id46_addr; 53 struct in6_addr id6_addr; 54 }; 55 56 /* 57 * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553. in_conninfo has 58 * some extra padding to accomplish this. 59 * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport, 60 * lport, faddr to generate hash, so these fields shouldn't be moved. 61 */ 62 struct in_endpoints { 63 uint16_t ie_fport; /* foreign port */ 64 uint16_t ie_lport; /* local port */ 65 /* protocol dependent part, local and foreign addr */ 66 union in_dependaddr ie_dependfaddr; /* foreign host table entry */ 67 union in_dependaddr ie_dependladdr; /* local host table entry */ 68 #define ie_faddr ie_dependfaddr.id46_addr.ia46_addr4 69 #define ie_laddr ie_dependladdr.id46_addr.ia46_addr4 70 #define ie6_faddr ie_dependfaddr.id6_addr 71 #define ie6_laddr ie_dependladdr.id6_addr 72 uint32_t ie6_zoneid; /* scope zone id */ 73 }; 74 75 /* 76 * XXX The defines for inc_* are hacks and should be changed to direct 77 * references. 78 */ 79 struct in_conninfo { 80 uint8_t inc_flags; 81 uint8_t inc_len; 82 uint16_t inc_fibnum; /* XXX was pad, 16 bits is plenty */ 83 /* protocol dependent part */ 84 struct in_endpoints inc_ie; 85 }; 86 87 /* 88 * Flags for inc_flags. 89 */ 90 #define INC_ISIPV6 0x01 91 #define INC_IPV6MINMTU 0x02 92 93 #define inc_fport inc_ie.ie_fport 94 #define inc_lport inc_ie.ie_lport 95 #define inc_faddr inc_ie.ie_faddr 96 #define inc_laddr inc_ie.ie_laddr 97 #define inc6_faddr inc_ie.ie6_faddr 98 #define inc6_laddr inc_ie.ie6_laddr 99 #define inc6_zoneid inc_ie.ie6_zoneid 100 101 #define inp_fport inp_inc.inc_fport 102 #define inp_lport inp_inc.inc_lport 103 #define inp_faddr inp_inc.inc_faddr 104 #define inp_laddr inp_inc.inc_laddr 105 106 #define in6p_faddr inp_inc.inc6_faddr 107 #define in6p_laddr inp_inc.inc6_laddr 108 #define in6p_zoneid inp_inc.inc6_zoneid 109 110 #ifdef _SYS_SOCKETVAR_H_ /* XXX: requires xsocket to be known */ 111 /* 112 * Interface exported to userland by various protocols which use inpcbs. Hack 113 * alert -- only define if struct xsocket is in scope. 114 * Fields prefixed with "xi_" are unique to this structure, and the rest 115 * match fields in the struct inpcb, to ease coding and porting. 116 * 117 * Legend: 118 * (s) - used by userland utilities in src 119 * (p) - used by utilities in ports 120 * (3) - is known to be used by third party software not in ports 121 * (n) - no known usage 122 */ 123 typedef uint64_t inp_gen_t; /* compat */ 124 struct xinpcb { 125 ksize_t xi_len; /* length of this structure */ 126 struct xsocket xi_socket; /* (s,p) */ 127 struct in_conninfo inp_inc; /* (s,p) */ 128 uint64_t inp_gencnt; /* (s,p) */ 129 int64_t inp_spare64[5]; 130 uint32_t inp_flow; /* (s) */ 131 uint32_t inp_flowid; /* (s) */ 132 uint32_t inp_flowtype; /* (s) */ 133 int32_t inp_flags; /* (s,p) */ 134 int32_t inp_flags2; /* (s) */ 135 uint32_t inp_unused; 136 int32_t in6p_cksum; /* (n) */ 137 int32_t inp_spare32[4]; 138 uint16_t in6p_hops; /* (n) */ 139 uint8_t inp_ip_tos; /* (n) */ 140 int8_t pad8; 141 uint8_t inp_vflag; /* (s,p) */ 142 uint8_t inp_ip_ttl; /* (n) */ 143 uint8_t inp_ip_p; /* (n) */ 144 uint8_t inp_ip_minttl; /* (n) */ 145 int8_t inp_spare8[4]; 146 } __aligned(8); 147 148 struct xinpgen { 149 ksize_t xig_len; /* length of this structure */ 150 u_int xig_count; /* number of PCBs at this time */ 151 uint32_t _xig_spare32; 152 uint64_t xig_gen; /* generation count at this time */ 153 so_gen_t xig_sogen; /* socket generation count this time */ 154 uint64_t _xig_spare64[4]; 155 } __aligned(8); 156 #endif /* _SYS_SOCKETVAR_H_ */ 157 158 /* 159 * Flags for inp_vflags -- historically version flags only 160 */ 161 #define INP_IPV4 0x1 162 #define INP_IPV6 0x2 163 #define INP_IPV6PROTO 0x4 /* opened under IPv6 protocol */ 164 165 /* inp_vflags description for use with printf(9) %b identifier. */ 166 #define INP_VFLAGS_BITS "\20\1INP_IPV4\2INP_IPV6\3INP_IPV6PROTO" 167 168 /* 169 * Flags for inp_flags. 170 */ 171 #define INP_RECVOPTS 0x00000001 /* receive incoming IP options */ 172 #define INP_RECVRETOPTS 0x00000002 /* receive IP options for reply */ 173 #define INP_RECVDSTADDR 0x00000004 /* receive IP dst address */ 174 #define INP_HDRINCL 0x00000008 /* user supplies entire IP header */ 175 #define INP_HIGHPORT 0x00000010 /* user wants "high" port binding */ 176 #define INP_LOWPORT 0x00000020 /* user wants "low" port binding */ 177 #define INP_ANONPORT 0x00000040 /* read by netstat(1) */ 178 #define INP_RECVIF 0x00000080 /* receive incoming interface */ 179 #define INP_MTUDISC 0x00000100 /* user can do MTU discovery */ 180 /* INP_FREED 0x00000200 private to in_pcb.c */ 181 #define INP_RECVTTL 0x00000400 /* receive incoming IP TTL */ 182 #define INP_DONTFRAG 0x00000800 /* don't fragment packet */ 183 #define INP_BINDANY 0x00001000 /* allow bind to any address */ 184 /* available 0x00002000 */ 185 #define INP_RECVTOS 0x00004000 /* receive incoming IP TOS */ 186 #define IN6P_IPV6_V6ONLY 0x00008000 /* restrict AF_INET6 socket for v6 */ 187 #define IN6P_PKTINFO 0x00010000 /* receive IP6 dst and I/F */ 188 #define IN6P_HOPLIMIT 0x00020000 /* receive hoplimit */ 189 #define IN6P_HOPOPTS 0x00040000 /* receive hop-by-hop options */ 190 #define IN6P_DSTOPTS 0x00080000 /* receive dst options after rthdr */ 191 #define IN6P_RTHDR 0x00100000 /* receive routing header */ 192 #define IN6P_RTHDRDSTOPTS 0x00200000 /* receive dstoptions before rthdr */ 193 #define IN6P_TCLASS 0x00400000 /* receive traffic class value */ 194 #define IN6P_AUTOFLOWLABEL 0x00800000 /* attach flowlabel automatically */ 195 /* INP_INLBGROUP 0x01000000 private to in_pcb.c */ 196 #define INP_ONESBCAST 0x02000000 /* send all-ones broadcast */ 197 /* INP_UNCONNECTED 0x04000000 private to in_pcb.c/in6_pcb.c */ 198 #define INP_SOCKREF 0x08000000 /* strong socket reference */ 199 #define INP_RESERVED_0 0x10000000 /* reserved field */ 200 #define INP_BOUNDFIB 0x20000000 /* Bound to a specific FIB. */ 201 #define IN6P_RFC2292 0x40000000 /* used RFC2292 API on the socket */ 202 #define IN6P_MTU 0x80000000 /* receive path MTU */ 203 204 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\ 205 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\ 206 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\ 207 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\ 208 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\ 209 IN6P_MTU) 210 211 /* inp_flags description for use with printf(9) %b identifier. */ 212 #define INP_FLAGS_BITS "\20" \ 213 "\1INP_RECVOPTS\2INP_RECVRETOPTS\3INP_RECVDSTADDR\4INP_HDRINCL" \ 214 "\5INP_HIGHPORT\6INP_LOWPORT\7INP_ANONPORT\10INP_RECVIF" \ 215 "\11INP_MTUDISC\12INP_FREED\13INP_RECVTTL\14INP_DONTFRAG" \ 216 "\15INP_BINDANY\17INP_RECVTOS\20IN6P_IPV6_V6ONLY" \ 217 "\21IN6P_PKTINFO\22IN6P_HOPLIMIT\23IN6P_HOPOPTS\24IN6P_DSTOPTS" \ 218 "\25IN6P_RTHDR\26IN6P_RTHDRDSTOPTS\27IN6P_TCLASS\30IN6P_AUTOFLOWLABEL" \ 219 "\31INP_INLBGROUP\32INP_ONESBCAST\33INP_UNCONNECTED\34INP_SOCKREF" \ 220 "\35INP_RESERVED_0\36INP_BOUNDFIB\37IN6P_RFC2292\40IN6P_MTU" 221 222 /* 223 * Flags for inp_flags2. 224 */ 225 /* 0x00000001 */ 226 /* 0x00000002 */ 227 /* 0x00000004 */ 228 /* 0x00000008 */ 229 /* 0x00000010 */ 230 /* 0x00000020 */ 231 /* 0x00000040 */ 232 /* 0x00000080 */ 233 #define INP_RECVFLOWID 0x00000100 /* populate recv datagram with flow info */ 234 #define INP_RECVRSSBUCKETID 0x00000200 /* populate recv datagram with bucket id */ 235 #define INP_RATE_LIMIT_CHANGED 0x00000400 /* rate limit needs attention */ 236 #define INP_ORIGDSTADDR 0x00000800 /* receive IP dst address/port */ 237 /* 0x00001000 */ 238 /* 0x00002000 */ 239 /* 0x00004000 */ 240 /* 0x00008000 */ 241 /* 0x00010000 */ 242 #define INP_2PCP_SET 0x00020000 /* If the Eth PCP should be set explicitly */ 243 #define INP_2PCP_BIT0 0x00040000 /* Eth PCP Bit 0 */ 244 #define INP_2PCP_BIT1 0x00080000 /* Eth PCP Bit 1 */ 245 #define INP_2PCP_BIT2 0x00100000 /* Eth PCP Bit 2 */ 246 #define INP_2PCP_BASE INP_2PCP_BIT0 247 #define INP_2PCP_MASK (INP_2PCP_BIT0 | INP_2PCP_BIT1 | INP_2PCP_BIT2) 248 #define INP_2PCP_SHIFT 18 /* shift PCP field in/out of inp_flags2 */ 249 250 /* inp_flags2 description for use with printf(9) %b identifier. */ 251 #define INP_FLAGS2_BITS "\20" \ 252 "\11INP_RECVFLOWID\12INP_RECVRSSBUCKETID" \ 253 "\13INP_RATE_LIMIT_CHANGED\14INP_ORIGDSTADDR" \ 254 "\22INP_2PCP_SET\23INP_2PCP_BIT0\24INP_2PCP_BIT1" \ 255 "\25INP_2PCP_BIT2" 256 257 struct sockopt_parameters { 258 struct in_conninfo sop_inc; 259 uint64_t sop_id; 260 int sop_level; 261 int sop_optname; 262 char sop_optval[]; 263 }; 264 265 #ifdef _SYS_KTLS_H_ 266 struct xktls_session { 267 uint32_t tsz; /* total sz of elm, next elm is at this+tsz */ 268 uint32_t fsz; /* size of the struct up to keys */ 269 uint64_t inp_gencnt; 270 kvaddr_t so_pcb; 271 struct in_conninfo coninf; 272 u_short rx_vlan_id; 273 struct xktls_session_onedir rcv; 274 struct xktls_session_onedir snd; 275 /* 276 * Next are 277 * - keydata for rcv, first cipher of length rcv.cipher_key_len, then 278 * authentication of length rcv.auth_key_len; 279 * - driver data (string) of length rcv.drv_st_len, if the rcv session is 280 * offloaded to ifnet rcv.ifnet; 281 * - keydata for snd, first cipher of length snd.cipher_key_len, then 282 * authentication of length snd.auth_key_len; 283 * - driver data (string) of length snd.drv_st_len, if the snd session is 284 * offloaded to ifnet snd.ifnet; 285 */ 286 }; 287 #endif /* _SYS_KTLS_H_ */ 288 289 #ifdef _KERNEL 290 /* 291 * No user visible declarations below. 292 */ 293 #include <sys/queue.h> 294 #include <sys/epoch.h> 295 #include <sys/lock.h> 296 #include <sys/mutex.h> 297 #include <sys/rwlock.h> 298 #include <sys/_smr.h> 299 #include <net/route.h> 300 #include <sys/proc.h> 301 #include <sys/sysctl.h> 302 #include <vm/uma.h> 303 #include <sys/ck.h> 304 305 /* 306 * struct inpcb is the common protocol control block structure used in most 307 * IP transport protocols. 308 * 309 * Pointers to local and foreign host table entries, local and foreign socket 310 * numbers, and pointers up (to a socket structure) and down (to a 311 * protocol-specific control block) are stored here. 312 */ 313 CK_LIST_HEAD(inpcbhead, inpcb); 314 CK_LIST_HEAD(inpcblbgrouphead, inpcblbgroup); 315 316 /* 317 * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and 318 * IPv6 sockets. In the case of TCP and UDP, further per-connection state is 319 * located in a larger protocol specific structure that embeds inpcb in it. 320 * Almost all fields of struct inpcb are static after creation or protected by 321 * a per-inpcb rwlock, inp_lock. 322 * 323 * A inpcb database consists of two hash tables: one for connected pcbs and the 324 * other for wildcard-bound pcbs. The newborn pcbs reside on the unconnected 325 * list. Although a pcb can be on either of these three lists, we can't share 326 * the linkage pointers because unlocked readers might be iterating over them. 327 * The only thing that can be unionized is the load-balance table and exact 328 * hash, as a pcb can never participate in both tables through its entire life 329 * time. Database lookups or list traversals are to be performed inside SMR 330 * section. Once desired PCB is found its own lock is to be obtained and SMR 331 * section exited. 332 * 333 * Key: 334 * (c) - Constant after initialization 335 * (e) - Protected by the SMR section 336 * (i) - Protected by the inpcb lock 337 * (p) - Protected by the pcbinfo lock for the inpcb 338 * (h) - Protected by the pcbhash lock for the inpcb 339 * (s) - Protected by another subsystem's locks 340 * (x) - Undefined locking 341 * 342 * A few other notes: 343 * 344 * When a read lock is held, stability of the field is guaranteed; to write 345 * to a field, a write lock must generally be held. 346 * 347 * netinet/netinet6-layer code should not assume that the inp_socket pointer 348 * is safe to dereference without inp_lock being held, there may be 349 * close(2)-related races. 350 * 351 * The inp_vflag field is overloaded, and would otherwise ideally be (c). 352 */ 353 struct icmp6_filter; 354 struct inpcbpolicy; 355 struct m_snd_tag; 356 struct inpcb { 357 union { 358 CK_LIST_ENTRY(inpcb) inp_hash_exact; 359 LIST_ENTRY(inpcb) inp_lbgroup_list; 360 }; 361 CK_LIST_ENTRY(inpcb) inp_hash_wild; 362 CK_LIST_ENTRY(inpcb) inp_unconn_list; 363 struct rwlock inp_lock; 364 #define inp_start_zero inp_refcount 365 #define inp_zero_size (sizeof(struct inpcb) - \ 366 offsetof(struct inpcb, inp_start_zero)) 367 u_int inp_refcount; /* (i) refcount */ 368 int inp_flags; /* (i) generic IP/datagram flags */ 369 int inp_flags2; /* (i) generic IP/datagram flags #2*/ 370 uint8_t inp_numa_domain; /* numa domain */ 371 struct socket *inp_socket; /* (i) back pointer to socket */ 372 struct inpcbinfo *inp_pcbinfo; /* (c) PCB list info */ 373 struct ucred *inp_cred; /* (c) cache of socket cred */ 374 u_int32_t inp_flow; /* (i) IPv6 flow information */ 375 u_char inp_vflag; /* (i) IP version flag (v4/v6) */ 376 u_char inp_ip_ttl; /* (i) time to live proto */ 377 u_char inp_ip_p; /* (c) protocol proto */ 378 u_char inp_ip_minttl; /* (i) minimum TTL or drop */ 379 uint32_t inp_flowid; /* (x) flow id / queue id */ 380 smr_seq_t inp_smr; /* (i) sequence number at disconnect */ 381 struct m_snd_tag *inp_snd_tag; /* (i) send tag for outgoing mbufs */ 382 uint32_t inp_flowtype; /* (x) M_HASHTYPE value */ 383 384 /* Local and foreign ports, local and foreign addr. */ 385 struct in_conninfo inp_inc; /* (i,h) list for PCB's local port */ 386 387 /* MAC and IPSEC policy information. */ 388 struct label *inp_label; /* (i) MAC label */ 389 struct inpcbpolicy *inp_sp; /* (s) for IPSEC */ 390 391 /* Protocol-dependent part; options. */ 392 struct { 393 u_char inp_ip_tos; /* (i) type of service proto */ 394 struct mbuf *inp_options; /* (i) IP options */ 395 struct ip_moptions *inp_moptions; /* (i) mcast options */ 396 }; 397 struct { 398 /* (i) IP options */ 399 struct mbuf *in6p_options; 400 /* (i) IP6 options for outgoing packets */ 401 struct ip6_pktopts *in6p_outputopts; 402 /* (i) IP multicast options */ 403 struct ip6_moptions *in6p_moptions; 404 /* (i) ICMPv6 code type filter */ 405 struct icmp6_filter *in6p_icmp6filt; 406 /* (i) IPV6_CHECKSUM setsockopt */ 407 int in6p_cksum; 408 short in6p_hops; 409 }; 410 CK_LIST_ENTRY(inpcb) inp_portlist; /* (r:e/w:h) port list */ 411 uint64_t inp_gencnt; /* (c) generation count */ 412 void *spare_ptr; /* Spare pointer. */ 413 rt_gen_t inp_rt_cookie; /* generation for route entry */ 414 union { /* cached L3 information */ 415 struct route inp_route; 416 struct route_in6 inp_route6; 417 }; 418 }; 419 420 /* 421 * Per-VNET pcb database for each high-level protocol (UDP, TCP, ...) in both 422 * IPv4 and IPv6. 423 * 424 * The pcbs are protected with SMR section and thus all lists in inpcbinfo 425 * are CK-lists. Locking is required to insert a pcb into database. 426 * 427 * Locking key: 428 * 429 * (c) Constant or nearly constant after initialisation 430 * (e) Protected by SMR section 431 * (h) Locked by ipi_hash_lock 432 */ 433 struct inpcbinfo { 434 u_int ipi_count; /* (h) */ 435 436 /* 437 * Generation count -- incremented each time a connection is allocated 438 * or freed. 439 */ 440 uint64_t ipi_gencnt; /* (h) */ 441 442 /* 443 * Fields associated with port lookup and allocation. 444 */ 445 u_short ipi_lastport; /* (h) */ 446 u_short ipi_lastlow; /* (h) */ 447 u_short ipi_lasthi; /* (h) */ 448 449 /* 450 * UMA zone from which inpcbs are allocated for this protocol. 451 */ 452 uma_zone_t ipi_zone; /* (c) */ 453 uma_zone_t ipi_portzone; /* (c) */ 454 smr_t ipi_smr; /* (c) */ 455 456 /* 457 * Global hash of inpcbs, hashed by local and foreign addresses and 458 * port numbers. The "exact" hash holds PCBs connected to a foreign 459 * address, and "wild" holds the rest. 460 */ 461 struct mtx ipi_hash_lock; 462 struct inpcbhead ipi_list_unconn; /* (r:e/w:h) */ 463 struct inpcbhead *ipi_hash_exact; /* (r:e/w:h) */ 464 struct inpcbhead *ipi_hash_wild; /* (r:e/w:h) */ 465 u_long ipi_hashmask; /* (c) */ 466 u_long ipi_porthashmask; /* (c) */ 467 u_long ipi_lbgrouphashmask; /* (c) */ 468 469 /* 470 * Global hash of inpcbs, hashed by only local port number. 471 */ 472 struct inpcbhead *ipi_porthashbase; /* (h) */ 473 474 /* 475 * Load balance groups used for the SO_REUSEPORT_LB option, 476 * hashed by local port. 477 */ 478 struct inpcblbgrouphead *ipi_lbgrouphashbase; /* (r:e/w:h) */ 479 }; 480 481 /* 482 * Global allocation storage for each high-level protocol (UDP, TCP, ...). 483 * Each corresponding per-VNET inpcbinfo points into this one. 484 */ 485 struct inpcbstorage { 486 uma_zone_t ips_zone; 487 uma_init ips_pcbinit; 488 size_t ips_size; 489 const char * ips_zone_name; 490 const char * ips_hashlock_name; 491 }; 492 493 #define INPCBSTORAGE_DEFINE(prot, ppcb, lname, zname, hname) \ 494 static int \ 495 prot##_inpcb_init(void *mem, int size __unused, int flags __unused) \ 496 { \ 497 struct inpcb *inp = mem; \ 498 \ 499 rw_init_flags(&inp->inp_lock, lname, RW_RECURSE | RW_DUPOK); \ 500 return (0); \ 501 } \ 502 static struct inpcbstorage prot = { \ 503 .ips_size = sizeof(struct ppcb), \ 504 .ips_pcbinit = prot##_inpcb_init, \ 505 .ips_zone_name = zname, \ 506 .ips_hashlock_name = hname, \ 507 }; \ 508 SYSINIT(prot##_inpcbstorage_init, SI_SUB_PROTO_DOMAIN, \ 509 SI_ORDER_SECOND, in_pcbstorage_init, &prot); \ 510 SYSUNINIT(prot##_inpcbstorage_uninit, SI_SUB_PROTO_DOMAIN, \ 511 SI_ORDER_SECOND, in_pcbstorage_destroy, &prot) 512 513 #define INP_LOCK_DESTROY(inp) rw_destroy(&(inp)->inp_lock) 514 #define INP_RLOCK(inp) rw_rlock(&(inp)->inp_lock) 515 #define INP_WLOCK(inp) rw_wlock(&(inp)->inp_lock) 516 #define INP_TRY_RLOCK(inp) rw_try_rlock(&(inp)->inp_lock) 517 #define INP_TRY_WLOCK(inp) rw_try_wlock(&(inp)->inp_lock) 518 #define INP_RUNLOCK(inp) rw_runlock(&(inp)->inp_lock) 519 #define INP_WUNLOCK(inp) rw_wunlock(&(inp)->inp_lock) 520 #define INP_UNLOCK(inp) rw_unlock(&(inp)->inp_lock) 521 #define INP_TRY_UPGRADE(inp) rw_try_upgrade(&(inp)->inp_lock) 522 #define INP_DOWNGRADE(inp) rw_downgrade(&(inp)->inp_lock) 523 #define INP_WLOCKED(inp) rw_wowned(&(inp)->inp_lock) 524 #define INP_LOCK_ASSERT(inp) rw_assert(&(inp)->inp_lock, RA_LOCKED) 525 #define INP_RLOCK_ASSERT(inp) rw_assert(&(inp)->inp_lock, RA_RLOCKED) 526 #define INP_WLOCK_ASSERT(inp) rw_assert(&(inp)->inp_lock, RA_WLOCKED) 527 #define INP_UNLOCK_ASSERT(inp) rw_assert(&(inp)->inp_lock, RA_UNLOCKED) 528 529 /* 530 * These locking functions are for inpcb consumers outside of sys/netinet, 531 * more specifically, they were added for the benefit of TOE drivers. The 532 * macros are reserved for use by the stack. 533 */ 534 void inp_wlock(struct inpcb *); 535 void inp_wunlock(struct inpcb *); 536 void inp_rlock(struct inpcb *); 537 void inp_runlock(struct inpcb *); 538 539 #ifdef INVARIANT_SUPPORT 540 void inp_lock_assert(struct inpcb *); 541 void inp_unlock_assert(struct inpcb *); 542 #else 543 #define inp_lock_assert(inp) do {} while (0) 544 #define inp_unlock_assert(inp) do {} while (0) 545 #endif 546 547 void inp_apply_all(struct inpcbinfo *, void (*func)(struct inpcb *, void *), 548 void *arg); 549 struct socket * 550 inp_inpcbtosocket(struct inpcb *inp); 551 void inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp, 552 uint32_t *faddr, uint16_t *fp); 553 554 #define INP_HASH_WLOCK(ipi) mtx_lock(&(ipi)->ipi_hash_lock) 555 #define INP_HASH_WUNLOCK(ipi) mtx_unlock(&(ipi)->ipi_hash_lock) 556 #define INP_HASH_LOCK_ASSERT(ipi) MPASS(SMR_ENTERED((ipi)->ipi_smr) || \ 557 mtx_owned(&(ipi)->ipi_hash_lock)) 558 #define INP_HASH_WLOCK_ASSERT(ipi) mtx_assert(&(ipi)->ipi_hash_lock, \ 559 MA_OWNED) 560 561 VNET_DECLARE(uint32_t, in_pcbhashseed); 562 #define V_in_pcbhashseed VNET(in_pcbhashseed) 563 564 /* 565 * Wildcard matching hash is not just a microoptimisation! The hash for 566 * wildcard IPv4 and wildcard IPv6 must be the same, otherwise AF_INET6 567 * wildcard bound pcb won't be able to receive AF_INET connections, while: 568 * jenkins_hash(&zeroes, 1, s) != jenkins_hash(&zeroes, 4, s) 569 * See also comment above struct in_addr_4in6. 570 */ 571 #define IN_ADDR_JHASH32(addr) \ 572 ((addr)->s_addr == INADDR_ANY ? V_in_pcbhashseed : \ 573 jenkins_hash32((&(addr)->s_addr), 1, V_in_pcbhashseed)) 574 #define IN6_ADDR_JHASH32(addr) \ 575 (memcmp((addr), &in6addr_any, sizeof(in6addr_any)) == 0 ? \ 576 V_in_pcbhashseed : \ 577 jenkins_hash32((addr)->__u6_addr.__u6_addr32, \ 578 nitems((addr)->__u6_addr.__u6_addr32), V_in_pcbhashseed)) 579 580 #define INP_PCBHASH(faddr, lport, fport, mask) \ 581 ((IN_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) & (mask)) 582 #define INP6_PCBHASH(faddr, lport, fport, mask) \ 583 ((IN6_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) & (mask)) 584 585 #define INP_PCBHASH_WILD(lport, mask) \ 586 ((V_in_pcbhashseed ^ ntohs(lport)) & (mask)) 587 588 #define INP_PCBLBGROUP_PKTHASH(faddr, lport, fport) \ 589 (IN_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) 590 #define INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) \ 591 (IN6_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) 592 593 #define INP_PCBPORTHASH(lport, mask) (ntohs((lport)) & (mask)) 594 595 #if defined(INET) && defined(INET6) 596 #define RIPCB_HASH(inp) (((inp)->inp_vflag & INP_IPV6) ? \ 597 IN6_ADDR_JHASH32(&(inp)->in6p_faddr) : \ 598 IN_ADDR_JHASH32(&(inp)->inp_faddr)) 599 #elif defined(INET6) 600 #define RIPCB_HASH(inp) \ 601 IN6_ADDR_JHASH32(&(inp)->in6p_faddr) 602 #else 603 #define RIPCB_HASH(inp) \ 604 IN_ADDR_JHASH32(&(inp)->inp_faddr) 605 #endif 606 607 /* 608 * Flags passed to in_pcblookup*(), inp_smr_lock() and inp_next(). 609 */ 610 typedef enum { 611 INPLOOKUP_WILDCARD = 0x00000001, /* Allow wildcard sockets. */ 612 INPLOOKUP_RLOCKPCB = 0x00000002, /* Return inpcb read-locked. */ 613 INPLOOKUP_WLOCKPCB = 0x00000004, /* Return inpcb write-locked. */ 614 INPLOOKUP_FIB = 0x00000008, /* inp must be from same FIB. */ 615 } inp_lookup_t; 616 617 #define INPLOOKUP_MASK (INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \ 618 INPLOOKUP_WLOCKPCB | INPLOOKUP_FIB) 619 #define INPLOOKUP_LOCKMASK (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB) 620 621 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb) 622 623 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family 624 625 #define INP_CHECK_SOCKAF(so, af) (INP_SOCKAF(so) == af) 626 627 VNET_DECLARE(int, ipport_reservedhigh); 628 VNET_DECLARE(int, ipport_reservedlow); 629 VNET_DECLARE(int, ipport_lowfirstauto); 630 VNET_DECLARE(int, ipport_lowlastauto); 631 VNET_DECLARE(int, ipport_firstauto); 632 VNET_DECLARE(int, ipport_lastauto); 633 VNET_DECLARE(int, ipport_hifirstauto); 634 VNET_DECLARE(int, ipport_hilastauto); 635 VNET_DECLARE(int, ipport_randomized); 636 637 #define V_ipport_reservedhigh VNET(ipport_reservedhigh) 638 #define V_ipport_reservedlow VNET(ipport_reservedlow) 639 #define V_ipport_lowfirstauto VNET(ipport_lowfirstauto) 640 #define V_ipport_lowlastauto VNET(ipport_lowlastauto) 641 #define V_ipport_firstauto VNET(ipport_firstauto) 642 #define V_ipport_lastauto VNET(ipport_lastauto) 643 #define V_ipport_hifirstauto VNET(ipport_hifirstauto) 644 #define V_ipport_hilastauto VNET(ipport_hilastauto) 645 #define V_ipport_randomized VNET(ipport_randomized) 646 647 void in_pcbinfo_init(struct inpcbinfo *, struct inpcbstorage *, 648 u_int, u_int, u_int); 649 void in_pcbinfo_destroy(struct inpcbinfo *); 650 void in_pcbstorage_init(void *); 651 void in_pcbstorage_destroy(void *); 652 653 void in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *); 654 int in_pcballoc(struct socket *, struct inpcbinfo *); 655 #define INPBIND_FIB 0x0001 /* bind to the PCB's FIB only */ 656 int in_pcbbind(struct inpcb *, struct sockaddr_in *, int, struct ucred *); 657 int in_pcbbind_setup(struct inpcb *, struct sockaddr_in *, in_addr_t *, 658 u_short *, int, struct ucred *); 659 int in_pcbconnect(struct inpcb *, struct sockaddr_in *, struct ucred *); 660 void in_pcbdisconnect(struct inpcb *); 661 void in_pcbfree(struct inpcb *); 662 int in_pcbladdr(const struct inpcb *, struct in_addr *, struct in_addr *, 663 struct ucred *); 664 int in_pcblbgroup_numa(struct inpcb *, int arg); 665 void in_pcblisten(struct inpcb *); 666 struct inpcb * 667 in_pcblookup(struct inpcbinfo *, struct in_addr, u_int, 668 struct in_addr, u_int, int, struct ifnet *); 669 struct inpcb * 670 in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int, 671 struct in_addr, u_int, int, struct ifnet *, struct mbuf *); 672 void in_pcbref(struct inpcb *); 673 bool in_pcbrele(struct inpcb *, inp_lookup_t); 674 bool in_pcbrele_rlocked(struct inpcb *); 675 bool in_pcbrele_wlocked(struct inpcb *); 676 bool in_pcbrele_rlock(struct inpcb *inp); 677 void ripcb_connect(struct inpcb *); 678 void ripcb_disconnect(struct inpcb *); 679 680 #ifdef _SYS_SOCKETVAR_H_ 681 void in_pcbtoxinpcb(const struct inpcb *, struct xinpcb *); 682 int sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo, 683 int (*ctloutput_set)(struct inpcb *, struct sockopt *)); 684 #endif 685 686 /* 687 * struct inpcb_iterator is located on the stack of a function that uses 688 * inp_next(). The caller shall initialize the const members before first 689 * invocation of inp_next(). After that, until the iterator finishes the 690 * caller is supposed to only read 'inp' until it reads NULL. Some members 691 * have constness commented out for convenience of callers, that may reuse 692 * the iterator after it finishes. 693 * (c) - caller 694 * (n) - inp_next() 695 */ 696 typedef bool inp_match_t(const struct inpcb *, void *); 697 struct inpcb_iterator { 698 const struct inpcbinfo *ipi; 699 struct inpcb *inp; /* c:r, n:rw */ 700 /* const */ inp_match_t *match; 701 /* const */ void *ctx; 702 int hash; /* n:rw */ 703 /* const */ int mode; 704 #define INP_ALL_LIST -1 705 #define INP_UNCONN_LIST -2 706 const inp_lookup_t lock; 707 }; 708 709 /* Note: sparse initializers guarantee .inp = NULL. */ 710 #define INP_ITERATOR(_ipi, _lock, _match, _ctx) \ 711 { \ 712 .ipi = (_ipi), \ 713 .lock = (_lock), \ 714 .mode = INP_ALL_LIST, \ 715 .match = (_match), \ 716 .ctx = (_ctx), \ 717 } 718 #define INP_ALL_ITERATOR(_ipi, _lock) \ 719 { \ 720 .ipi = (_ipi), \ 721 .lock = (_lock), \ 722 .mode = INP_ALL_LIST, \ 723 } 724 725 struct inpcb *inp_next(struct inpcb_iterator *); 726 void in_losing(struct inpcb *); 727 void in_pcbsetsolabel(struct socket *so); 728 int in_getpeeraddr(struct socket *, struct sockaddr *sa); 729 int in_getsockaddr(struct socket *, struct sockaddr *sa); 730 void in_pcbsosetlabel(struct socket *so); 731 #ifdef RATELIMIT 732 int 733 in_pcboutput_txrtlmt_locked(struct inpcb *, struct ifnet *, 734 struct mbuf *, uint32_t); 735 int in_pcbattach_txrtlmt(struct inpcb *, struct ifnet *, uint32_t, uint32_t, 736 uint32_t, struct m_snd_tag **); 737 void in_pcbdetach_txrtlmt(struct inpcb *); 738 void in_pcbdetach_tag(struct m_snd_tag *); 739 int in_pcbmodify_txrtlmt(struct inpcb *, uint32_t); 740 int in_pcbquery_txrtlmt(struct inpcb *, uint32_t *); 741 int in_pcbquery_txrlevel(struct inpcb *, uint32_t *); 742 void in_pcboutput_txrtlmt(struct inpcb *, struct ifnet *, struct mbuf *); 743 void in_pcboutput_eagain(struct inpcb *); 744 #endif 745 #ifdef DDB 746 void db_print_inpcb(struct inpcb *, const char *, int); 747 #endif 748 #endif /* _KERNEL */ 749 750 #endif /* !_NETINET_IN_PCB_H_ */ 751