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