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