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