xref: /freebsd/sys/netinet/in_pcb.h (revision c03c5b1c)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1990, 1993
5  *	The Regents of the University of California.
6  * Copyright (c) 2010-2011 Juniper Networks, Inc.
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Robert N. M. Watson under
10  * contract to Juniper Networks, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
37  * $FreeBSD$
38  */
39 
40 #ifndef _NETINET_IN_PCB_H_
41 #define _NETINET_IN_PCB_H_
42 
43 #include <sys/queue.h>
44 #include <sys/epoch.h>
45 #include <sys/_lock.h>
46 #include <sys/_mutex.h>
47 #include <sys/_rwlock.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/smr.h>
55 #include <sys/sysctl.h>
56 #include <net/vnet.h>
57 #include <vm/uma.h>
58 #endif
59 #include <sys/ck.h>
60 
61 /*
62  * struct inpcb is the common protocol control block structure used in most
63  * IP transport protocols.
64  *
65  * Pointers to local and foreign host table entries, local and foreign socket
66  * numbers, and pointers up (to a socket structure) and down (to a
67  * protocol-specific control block) are stored here.
68  */
69 CK_LIST_HEAD(inpcbhead, inpcb);
70 CK_LIST_HEAD(inpcbporthead, inpcbport);
71 CK_LIST_HEAD(inpcblbgrouphead, inpcblbgroup);
72 typedef	uint64_t	inp_gen_t;
73 
74 /*
75  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
76  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
77  * the following structure.  This requires padding always be zeroed out,
78  * which is done right after inpcb allocation and stays through its lifetime.
79  */
80 struct in_addr_4in6 {
81 	u_int32_t	ia46_pad32[3];
82 	struct	in_addr	ia46_addr4;
83 };
84 
85 union in_dependaddr {
86 	struct in_addr_4in6 id46_addr;
87 	struct in6_addr	id6_addr;
88 };
89 
90 /*
91  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
92  * some extra padding to accomplish this.
93  * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport,
94  * lport, faddr to generate hash, so these fields shouldn't be moved.
95  */
96 struct in_endpoints {
97 	u_int16_t	ie_fport;		/* foreign port */
98 	u_int16_t	ie_lport;		/* local port */
99 	/* protocol dependent part, local and foreign addr */
100 	union in_dependaddr ie_dependfaddr;	/* foreign host table entry */
101 	union in_dependaddr ie_dependladdr;	/* local host table entry */
102 #define	ie_faddr	ie_dependfaddr.id46_addr.ia46_addr4
103 #define	ie_laddr	ie_dependladdr.id46_addr.ia46_addr4
104 #define	ie6_faddr	ie_dependfaddr.id6_addr
105 #define	ie6_laddr	ie_dependladdr.id6_addr
106 	u_int32_t	ie6_zoneid;		/* scope zone id */
107 };
108 
109 /*
110  * XXX The defines for inc_* are hacks and should be changed to direct
111  * references.
112  */
113 struct in_conninfo {
114 	u_int8_t	inc_flags;
115 	u_int8_t	inc_len;
116 	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
117 	/* protocol dependent part */
118 	struct	in_endpoints inc_ie;
119 };
120 
121 /*
122  * Flags for inc_flags.
123  */
124 #define	INC_ISIPV6	0x01
125 #define	INC_IPV6MINMTU	0x02
126 
127 #define	inc_fport	inc_ie.ie_fport
128 #define	inc_lport	inc_ie.ie_lport
129 #define	inc_faddr	inc_ie.ie_faddr
130 #define	inc_laddr	inc_ie.ie_laddr
131 #define	inc6_faddr	inc_ie.ie6_faddr
132 #define	inc6_laddr	inc_ie.ie6_laddr
133 #define	inc6_zoneid	inc_ie.ie6_zoneid
134 
135 #if defined(_KERNEL) || defined(_WANT_INPCB)
136 /*
137  * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and
138  * IPv6 sockets.  In the case of TCP and UDP, further per-connection state is
139  * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
140  * are static after creation or protected by a per-inpcb rwlock, inp_lock.
141  *
142  * A inpcb database is indexed by addresses/ports hash as well as list of
143  * all pcbs that belong to a certain proto. Database lookups or list traversals
144  * are be performed inside SMR section. Once desired PCB is found its own
145  * lock is to be obtained and SMR section exited.
146  *
147  * Key:
148  * (b) - Protected by the hpts lock.
149  * (c) - Constant after initialization
150  * (e) - Protected by the SMR section
151  * (i) - Protected by the inpcb lock
152  * (p) - Protected by the pcbinfo lock for the inpcb
153  * (h) - Protected by the pcbhash lock for the inpcb
154  * (s) - Protected by another subsystem's locks
155  * (x) - Undefined locking
156  *
157  * Notes on the tcp_hpts:
158  *
159  * First Hpts lock order is
160  * 1) INP_WLOCK()
161  * 2) HPTS_LOCK() i.e. hpts->pmtx
162  *
163  * To insert a TCB on the hpts you *must* be holding the INP_WLOCK().
164  * You may check the inp->inp_in_hpts flag without the hpts lock.
165  * The hpts is the only one that will clear this flag holding
166  * only the hpts lock. This means that in your tcp_output()
167  * routine when you test for the inp_in_hpts flag to be 1
168  * it may be transitioning to 0 (by the hpts).
169  * That's ok since that will just mean an extra call to tcp_output
170  * that most likely will find the call you executed
171  * (when the mis-match occured) will have put the TCB back
172  * on the hpts and it will return. If your
173  * call did not add the inp back to the hpts then you will either
174  * over-send or the cwnd will block you from sending more.
175  *
176  * Note you should also be holding the INP_WLOCK() when you
177  * call the remove from the hpts as well. Though usually
178  * you are either doing this from a timer, where you need and have
179  * the INP_WLOCK() or from destroying your TCB where again
180  * you should already have the INP_WLOCK().
181  *
182  * The inp_hpts_cpu, inp_hpts_cpu_set, inp_input_cpu and
183  * inp_input_cpu_set fields are controlled completely by
184  * the hpts. Do not ever set these. The inp_hpts_cpu_set
185  * and inp_input_cpu_set fields indicate if the hpts has
186  * setup the respective cpu field. It is advised if this
187  * field is 0, to enqueue the packet with the appropriate
188  * hpts_immediate() call. If the _set field is 1, then
189  * you may compare the inp_*_cpu field to the curcpu and
190  * may want to again insert onto the hpts if these fields
191  * are not equal (i.e. you are not on the expected CPU).
192  *
193  * A note on inp_hpts_calls and inp_input_calls, these
194  * flags are set when the hpts calls either the output
195  * or do_segment routines respectively. If the routine
196  * being called wants to use this, then it needs to
197  * clear the flag before returning. The hpts will not
198  * clear the flag. The flags can be used to tell if
199  * the hpts is the function calling the respective
200  * routine.
201  *
202  * A few other notes:
203  *
204  * When a read lock is held, stability of the field is guaranteed; to write
205  * to a field, a write lock must generally be held.
206  *
207  * netinet/netinet6-layer code should not assume that the inp_socket pointer
208  * is safe to dereference without inp_lock being held, even for protocols
209  * other than TCP (where the inpcb persists during TIMEWAIT even after the
210  * socket has been freed), or there may be close(2)-related races.
211  *
212  * The inp_vflag field is overloaded, and would otherwise ideally be (c).
213  */
214 struct icmp6_filter;
215 struct inpcbpolicy;
216 struct m_snd_tag;
217 struct inpcb {
218 	/* Cache line #1 (amd64) */
219 	CK_LIST_ENTRY(inpcb) inp_hash;	/* (w:h/r:e)  hash list */
220 	struct rwlock	inp_lock;
221 	/* Cache line #2 (amd64) */
222 #define	inp_start_zero	inp_hpts
223 #define	inp_zero_size	(sizeof(struct inpcb) - \
224 			    offsetof(struct inpcb, inp_start_zero))
225 	TAILQ_ENTRY(inpcb) inp_hpts;	/* pacing out queue next lock(b) */
226 	uint32_t inp_hpts_gencnt;	/* XXXGL */
227 	uint32_t inp_hpts_request;	/* Current hpts request, zero if
228 					 * fits in the pacing window (i&b). */
229 	/*
230 	 * Note the next fields are protected by a
231 	 * different lock (hpts-lock). This means that
232 	 * they must correspond in size to the smallest
233 	 * protectable bit field (uint8_t on x86, and
234 	 * other platfomrs potentially uint32_t?). Also
235 	 * since CPU switches can occur at different times the two
236 	 * fields can *not* be collapsed into a signal bit field.
237 	 */
238 #if defined(__amd64__) || defined(__i386__)
239 	uint8_t inp_in_hpts; /* on output hpts (lock b) */
240 #else
241 	uint32_t inp_in_hpts; /* on output hpts (lock b) */
242 #endif
243 	volatile uint16_t  inp_hpts_cpu; /* Lock (i) */
244 	volatile uint16_t  inp_irq_cpu;	/* Set by LRO in behalf of or the driver */
245 	u_int	inp_refcount;		/* (i) refcount */
246 	int	inp_flags;		/* (i) generic IP/datagram flags */
247 	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
248 	uint8_t inp_hpts_cpu_set :1,  /* on output hpts (i) */
249 			 inp_hpts_calls :1,	/* (i) from output hpts */
250 			 inp_irq_cpu_set :1,	/* (i) from LRO/Driver */
251 			 inp_spare_bits2 : 3;
252 	uint8_t inp_numa_domain;	/* numa domain */
253 	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
254 	struct	socket *inp_socket;	/* (i) back pointer to socket */
255 	int32_t 	 inp_hptsslot;	/* Hpts wheel slot this tcb is Lock(i&b) */
256 	uint32_t         inp_hpts_drop_reas;	/* reason we are dropping the PCB (lock i&b) */
257 	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
258 	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
259 	u_int32_t inp_flow;		/* (i) IPv6 flow information */
260 	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
261 	u_char	inp_ip_ttl;		/* (i) time to live proto */
262 	u_char	inp_ip_p;		/* (c) protocol proto */
263 	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
264 	uint32_t inp_flowid;		/* (x) flow id / queue id */
265 	struct m_snd_tag *inp_snd_tag;	/* (i) send tag for outgoing mbufs */
266 	uint32_t inp_flowtype;		/* (x) M_HASHTYPE value */
267 	uint32_t inp_rss_listen_bucket;	/* (x) overridden RSS listen bucket */
268 
269 	/* Local and foreign ports, local and foreign addr. */
270 	struct	in_conninfo inp_inc;	/* (i) list for PCB's local port */
271 
272 	/* MAC and IPSEC policy information. */
273 	struct	label *inp_label;	/* (i) MAC label */
274 	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
275 
276 	/* Protocol-dependent part; options. */
277 	struct {
278 		u_char	inp_ip_tos;		/* (i) type of service proto */
279 		struct mbuf		*inp_options;	/* (i) IP options */
280 		struct ip_moptions	*inp_moptions;	/* (i) mcast options */
281 	};
282 	struct {
283 		/* (i) IP options */
284 		struct mbuf		*in6p_options;
285 		/* (i) IP6 options for outgoing packets */
286 		struct ip6_pktopts	*in6p_outputopts;
287 		/* (i) IP multicast options */
288 		struct ip6_moptions	*in6p_moptions;
289 		/* (i) ICMPv6 code type filter */
290 		struct icmp6_filter	*in6p_icmp6filt;
291 		/* (i) IPV6_CHECKSUM setsockopt */
292 		int	in6p_cksum;
293 		short	in6p_hops;
294 	};
295 	CK_LIST_ENTRY(inpcb) inp_portlist;	/* (r:e/w:h) port list */
296 	struct	inpcbport *inp_phd;	/* (r:e/w:h) head of this list */
297 	inp_gen_t	inp_gencnt;	/* (c) generation count */
298 	void		*spare_ptr;	/* Spare pointer. */
299 	rt_gen_t	inp_rt_cookie;	/* generation for route entry */
300 	union {				/* cached L3 information */
301 		struct route inp_route;
302 		struct route_in6 inp_route6;
303 	};
304 	CK_LIST_ENTRY(inpcb) inp_list;	/* (r:e/w:p) all PCBs for proto */
305 };
306 #endif	/* _KERNEL */
307 
308 #define	inp_fport	inp_inc.inc_fport
309 #define	inp_lport	inp_inc.inc_lport
310 #define	inp_faddr	inp_inc.inc_faddr
311 #define	inp_laddr	inp_inc.inc_laddr
312 
313 #define	in6p_faddr	inp_inc.inc6_faddr
314 #define	in6p_laddr	inp_inc.inc6_laddr
315 #define	in6p_zoneid	inp_inc.inc6_zoneid
316 
317 #define	inp_vnet	inp_pcbinfo->ipi_vnet
318 
319 /*
320  * The range of the generation count, as used in this implementation, is 9e19.
321  * We would have to create 300 billion connections per second for this number
322  * to roll over in a year.  This seems sufficiently unlikely that we simply
323  * don't concern ourselves with that possibility.
324  */
325 
326 /*
327  * Interface exported to userland by various protocols which use inpcbs.  Hack
328  * alert -- only define if struct xsocket is in scope.
329  * Fields prefixed with "xi_" are unique to this structure, and the rest
330  * match fields in the struct inpcb, to ease coding and porting.
331  *
332  * Legend:
333  * (s) - used by userland utilities in src
334  * (p) - used by utilities in ports
335  * (3) - is known to be used by third party software not in ports
336  * (n) - no known usage
337  */
338 #ifdef _SYS_SOCKETVAR_H_
339 struct xinpcb {
340 	ksize_t		xi_len;			/* length of this structure */
341 	struct xsocket	xi_socket;		/* (s,p) */
342 	struct in_conninfo inp_inc;		/* (s,p) */
343 	uint64_t	inp_gencnt;		/* (s,p) */
344 	kvaddr_t	inp_ppcb;		/* (s) netstat(1) */
345 	int64_t		inp_spare64[4];
346 	uint32_t	inp_flow;		/* (s) */
347 	uint32_t	inp_flowid;		/* (s) */
348 	uint32_t	inp_flowtype;		/* (s) */
349 	int32_t		inp_flags;		/* (s,p) */
350 	int32_t		inp_flags2;		/* (s) */
351 	int32_t		inp_rss_listen_bucket;	/* (n) */
352 	int32_t		in6p_cksum;		/* (n) */
353 	int32_t		inp_spare32[4];
354 	uint16_t	in6p_hops;		/* (n) */
355 	uint8_t		inp_ip_tos;		/* (n) */
356 	int8_t		pad8;
357 	uint8_t		inp_vflag;		/* (s,p) */
358 	uint8_t		inp_ip_ttl;		/* (n) */
359 	uint8_t		inp_ip_p;		/* (n) */
360 	uint8_t		inp_ip_minttl;		/* (n) */
361 	int8_t		inp_spare8[4];
362 } __aligned(8);
363 
364 struct xinpgen {
365 	ksize_t	xig_len;	/* length of this structure */
366 	u_int		xig_count;	/* number of PCBs at this time */
367 	uint32_t	_xig_spare32;
368 	inp_gen_t	xig_gen;	/* generation count at this time */
369 	so_gen_t	xig_sogen;	/* socket generation count this time */
370 	uint64_t	_xig_spare64[4];
371 } __aligned(8);
372 
373 struct sockopt_parameters {
374 	struct in_conninfo sop_inc;
375 	uint64_t sop_id;
376 	int sop_level;
377 	int sop_optname;
378 	char sop_optval[];
379 };
380 
381 #ifdef	_KERNEL
382 int	sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo,
383 	    int (*ctloutput_set)(struct inpcb *, struct sockopt *));
384 void	in_pcbtoxinpcb(const struct inpcb *, struct xinpcb *);
385 #endif
386 #endif /* _SYS_SOCKETVAR_H_ */
387 
388 #ifdef _KERNEL
389 /*
390  * Per-VNET pcb database for each high-level protocol (UDP, TCP, ...) in both
391  * IPv4 and IPv6.
392  *
393  * The pcbs are protected with SMR section and thus all lists in inpcbinfo
394  * are CK-lists.  Locking is required to insert a pcb into database. Two
395  * locks are provided: one for the hash and one for the global list of pcbs,
396  * as well as overall count and generation count.
397  *
398  * Locking key:
399  *
400  * (c) Constant or nearly constant after initialisation
401  * (e) Protected by SMR section
402  * (g) Locked by ipi_lock
403  * (h) Locked by ipi_hash_lock
404  */
405 struct inpcbinfo {
406 	/*
407 	 * Global lock protecting inpcb list modification
408 	 */
409 	struct mtx		 ipi_lock;
410 	struct inpcbhead	 ipi_listhead;		/* (r:e/w:g) */
411 	u_int			 ipi_count;		/* (g) */
412 
413 	/*
414 	 * Generation count -- incremented each time a connection is allocated
415 	 * or freed.
416 	 */
417 	u_quad_t		 ipi_gencnt;		/* (g) */
418 
419 	/*
420 	 * Fields associated with port lookup and allocation.
421 	 */
422 	u_short			 ipi_lastport;		/* (h) */
423 	u_short			 ipi_lastlow;		/* (h) */
424 	u_short			 ipi_lasthi;		/* (h) */
425 
426 	/*
427 	 * UMA zone from which inpcbs are allocated for this protocol.
428 	 */
429 	uma_zone_t		 ipi_zone;		/* (c) */
430 	uma_zone_t		 ipi_portzone;		/* (c) */
431 	smr_t			 ipi_smr;		/* (c) */
432 
433 	/*
434 	 * Global hash of inpcbs, hashed by local and foreign addresses and
435 	 * port numbers.
436 	 */
437 	struct mtx		 ipi_hash_lock;
438 	struct inpcbhead 	*ipi_hashbase;		/* (r:e/w:h) */
439 	u_long			 ipi_hashmask;		/* (c) */
440 
441 	/*
442 	 * Global hash of inpcbs, hashed by only local port number.
443 	 */
444 	struct inpcbporthead	*ipi_porthashbase;	/* (h) */
445 	u_long			 ipi_porthashmask;	/* (h) */
446 
447 	/*
448 	 * Load balance groups used for the SO_REUSEPORT_LB option,
449 	 * hashed by local port.
450 	 */
451 	struct	inpcblbgrouphead *ipi_lbgrouphashbase;	/* (r:e/w:h) */
452 	u_long			 ipi_lbgrouphashmask;	/* (h) */
453 
454 	/*
455 	 * Pointer to network stack instance
456 	 */
457 	struct vnet		*ipi_vnet;		/* (c) */
458 };
459 
460 /*
461  * Global allocation storage for each high-level protocol (UDP, TCP, ...).
462  * Each corresponding per-VNET inpcbinfo points into this one.
463  */
464 struct inpcbstorage {
465 	uma_zone_t	ips_zone;
466 	uma_zone_t	ips_portzone;
467 	uma_init	ips_pcbinit;
468 	const char *	ips_zone_name;
469 	const char *	ips_portzone_name;
470 	const char *	ips_infolock_name;
471 	const char *	ips_hashlock_name;
472 };
473 
474 #define INPCBSTORAGE_DEFINE(prot, lname, zname, iname, hname)		\
475 static int								\
476 prot##_inpcb_init(void *mem, int size __unused, int flags __unused)	\
477 {									\
478 	struct inpcb *inp = mem;					\
479 									\
480 	rw_init_flags(&inp->inp_lock, lname, RW_RECURSE | RW_DUPOK);	\
481 	return (0);							\
482 }									\
483 static struct inpcbstorage prot = {					\
484 	.ips_pcbinit = prot##_inpcb_init,				\
485 	.ips_zone_name = zname,						\
486 	.ips_portzone_name = zname " ports",				\
487 	.ips_infolock_name = iname,					\
488 	.ips_hashlock_name = hname,					\
489 };									\
490 SYSINIT(prot##_inpcbstorage_init, SI_SUB_PROTO_DOMAIN,			\
491     SI_ORDER_SECOND, in_pcbstorage_init, &prot);			\
492 SYSUNINIT(prot##_inpcbstorage_uninit, SI_SUB_PROTO_DOMAIN,		\
493     SI_ORDER_SECOND, in_pcbstorage_destroy, &prot)
494 
495 /*
496  * Load balance groups used for the SO_REUSEPORT_LB socket option. Each group
497  * (or unique address:port combination) can be re-used at most
498  * INPCBLBGROUP_SIZMAX (256) times. The inpcbs are stored in il_inp which
499  * is dynamically resized as processes bind/unbind to that specific group.
500  */
501 struct inpcblbgroup {
502 	CK_LIST_ENTRY(inpcblbgroup) il_list;
503 	struct epoch_context il_epoch_ctx;
504 	uint16_t	il_lport;			/* (c) */
505 	u_char		il_vflag;			/* (c) */
506 	u_int8_t		il_numa_domain;
507 	uint32_t	il_pad2;
508 	union in_dependaddr il_dependladdr;		/* (c) */
509 #define	il_laddr	il_dependladdr.id46_addr.ia46_addr4
510 #define	il6_laddr	il_dependladdr.id6_addr
511 	uint32_t	il_inpsiz; /* max count in il_inp[] (h) */
512 	uint32_t	il_inpcnt; /* cur count in il_inp[] (h) */
513 	struct inpcb	*il_inp[];			/* (h) */
514 };
515 
516 #define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
517 #define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
518 #define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
519 #define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
520 #define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
521 #define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
522 #define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
523 #define INP_UNLOCK(inp)		rw_unlock(&(inp)->inp_lock)
524 #define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
525 #define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
526 #define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
527 #define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
528 #define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
529 #define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
530 #define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
531 
532 /*
533  * These locking functions are for inpcb consumers outside of sys/netinet,
534  * more specifically, they were added for the benefit of TOE drivers. The
535  * macros are reserved for use by the stack.
536  */
537 void inp_wlock(struct inpcb *);
538 void inp_wunlock(struct inpcb *);
539 void inp_rlock(struct inpcb *);
540 void inp_runlock(struct inpcb *);
541 
542 #ifdef INVARIANT_SUPPORT
543 void inp_lock_assert(struct inpcb *);
544 void inp_unlock_assert(struct inpcb *);
545 #else
546 #define	inp_lock_assert(inp)	do {} while (0)
547 #define	inp_unlock_assert(inp)	do {} while (0)
548 #endif
549 
550 void	inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
551 int 	inp_ip_tos_get(const struct inpcb *inp);
552 void 	inp_ip_tos_set(struct inpcb *inp, int val);
553 struct socket *
554 	inp_inpcbtosocket(struct inpcb *inp);
555 struct tcpcb *
556 	inp_inpcbtotcpcb(struct inpcb *inp);
557 void 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
558 		uint32_t *faddr, uint16_t *fp);
559 int	inp_so_options(const struct inpcb *inp);
560 
561 #endif /* _KERNEL */
562 
563 #define INP_INFO_WLOCK(ipi)	mtx_lock(&(ipi)->ipi_lock)
564 #define INP_INFO_WLOCKED(ipi)	mtx_owned(&(ipi)->ipi_lock)
565 #define INP_INFO_WUNLOCK(ipi)	mtx_unlock(&(ipi)->ipi_lock)
566 #define	INP_INFO_LOCK_ASSERT(ipi)	MPASS(SMR_ENTERED((ipi)->ipi_smr) || \
567 					mtx_owned(&(ipi)->ipi_lock))
568 #define INP_INFO_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_lock, MA_OWNED)
569 #define INP_INFO_WUNLOCK_ASSERT(ipi)	\
570 				mtx_assert(&(ipi)->ipi_lock, MA_NOTOWNED)
571 
572 #define	INP_HASH_WLOCK(ipi)		mtx_lock(&(ipi)->ipi_hash_lock)
573 #define	INP_HASH_WUNLOCK(ipi)		mtx_unlock(&(ipi)->ipi_hash_lock)
574 #define	INP_HASH_LOCK_ASSERT(ipi)	MPASS(SMR_ENTERED((ipi)->ipi_smr) || \
575 					mtx_owned(&(ipi)->ipi_hash_lock))
576 #define	INP_HASH_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_hash_lock, \
577 					MA_OWNED)
578 
579 /*
580  * Wildcard matching hash is not just a microoptimisation!  The hash for
581  * wildcard IPv4 and wildcard IPv6 must be the same, otherwise AF_INET6
582  * wildcard bound pcb won't be able to receive AF_INET connections, while:
583  * jenkins_hash(&zeroes, 1, s) != jenkins_hash(&zeroes, 4, s)
584  * See also comment above struct in_addr_4in6.
585  */
586 #define	IN_ADDR_JHASH32(addr)						\
587 	((addr)->s_addr == INADDR_ANY ? V_in_pcbhashseed :		\
588 	    jenkins_hash32((&(addr)->s_addr), 1, V_in_pcbhashseed))
589 #define	IN6_ADDR_JHASH32(addr)						\
590 	(memcmp((addr), &in6addr_any, sizeof(in6addr_any)) == 0 ?	\
591 	    V_in_pcbhashseed :						\
592 	    jenkins_hash32((addr)->__u6_addr.__u6_addr32,		\
593 	    nitems((addr)->__u6_addr.__u6_addr32), V_in_pcbhashseed))
594 
595 #define INP_PCBHASH(faddr, lport, fport, mask)				\
596 	((IN_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) & (mask))
597 #define	INP6_PCBHASH(faddr, lport, fport, mask)				\
598 	((IN6_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) & (mask))
599 
600 #define	INP_PCBHASH_WILD(lport, mask)					\
601 	((V_in_pcbhashseed ^ ntohs(lport)) & (mask))
602 
603 #define	INP_PCBLBGROUP_PKTHASH(faddr, lport, fport)			\
604 	(IN_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport)))
605 #define	INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport)			\
606 	(IN6_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport)))
607 
608 #define INP_PCBPORTHASH(lport, mask)	(ntohs((lport)) & (mask))
609 
610 /*
611  * Flags for inp_vflags -- historically version flags only
612  */
613 #define	INP_IPV4	0x1
614 #define	INP_IPV6	0x2
615 #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
616 
617 /*
618  * Flags for inp_flags.
619  */
620 #define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
621 #define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
622 #define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
623 #define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
624 #define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
625 #define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
626 #define	INP_ANONPORT		0x00000040 /* port chosen for user */
627 #define	INP_RECVIF		0x00000080 /* receive incoming interface */
628 #define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
629 /*	INP_FREED		0x00000200 private to in_pcb.c */
630 #define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
631 #define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
632 #define	INP_BINDANY		0x00001000 /* allow bind to any address */
633 #define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
634 #define	INP_RECVTOS		0x00004000 /* receive incoming IP TOS */
635 #define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
636 #define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
637 #define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
638 #define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
639 #define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
640 #define	IN6P_RTHDR		0x00100000 /* receive routing header */
641 #define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
642 #define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
643 #define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
644 #define	INP_TIMEWAIT		0x01000000 /* in TIMEWAIT, ppcb is tcptw */
645 #define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
646 #define	INP_DROPPED		0x04000000 /* protocol drop flag */
647 #define	INP_SOCKREF		0x08000000 /* strong socket reference */
648 #define	INP_RESERVED_0          0x10000000 /* reserved field */
649 #define	INP_RESERVED_1          0x20000000 /* reserved field */
650 #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
651 #define	IN6P_MTU		0x80000000 /* receive path MTU */
652 
653 #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
654 				 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
655 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
656 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
657 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
658 				 IN6P_MTU)
659 
660 /*
661  * Flags for inp_flags2.
662  */
663 #define	INP_MBUF_L_ACKS		0x00000001 /* We need large mbufs for ack compression */
664 #define	INP_MBUF_ACKCMP		0x00000002 /* TCP mbuf ack compression ok */
665 /*				0x00000004 */
666 #define	INP_REUSEPORT		0x00000008 /* SO_REUSEPORT option is set */
667 /*				0x00000010 */
668 #define	INP_REUSEADDR		0x00000020 /* SO_REUSEADDR option is set */
669 #define	INP_BINDMULTI		0x00000040 /* IP_BINDMULTI option is set */
670 #define	INP_RSS_BUCKET_SET	0x00000080 /* IP_RSS_LISTEN_BUCKET is set */
671 #define	INP_RECVFLOWID		0x00000100 /* populate recv datagram with flow info */
672 #define	INP_RECVRSSBUCKETID	0x00000200 /* populate recv datagram with bucket id */
673 #define	INP_RATE_LIMIT_CHANGED	0x00000400 /* rate limit needs attention */
674 #define	INP_ORIGDSTADDR		0x00000800 /* receive IP dst address/port */
675 #define INP_CANNOT_DO_ECN	0x00001000 /* The stack does not do ECN */
676 #define	INP_REUSEPORT_LB	0x00002000 /* SO_REUSEPORT_LB option is set */
677 #define INP_SUPPORTS_MBUFQ	0x00004000 /* Supports the mbuf queue method of LRO */
678 #define INP_MBUF_QUEUE_READY	0x00008000 /* The transport is pacing, inputs can be queued */
679 #define INP_DONT_SACK_QUEUE	0x00010000 /* If a sack arrives do not wake me */
680 #define INP_2PCP_SET		0x00020000 /* If the Eth PCP should be set explicitly */
681 #define INP_2PCP_BIT0		0x00040000 /* Eth PCP Bit 0 */
682 #define INP_2PCP_BIT1		0x00080000 /* Eth PCP Bit 1 */
683 #define INP_2PCP_BIT2		0x00100000 /* Eth PCP Bit 2 */
684 #define INP_2PCP_BASE	INP_2PCP_BIT0
685 #define INP_2PCP_MASK	(INP_2PCP_BIT0 | INP_2PCP_BIT1 | INP_2PCP_BIT2)
686 #define INP_2PCP_SHIFT		18         /* shift PCP field in/out of inp_flags2 */
687 
688 /*
689  * Flags passed to in_pcblookup*(), inp_smr_lock() and inp_next().
690  */
691 typedef	enum {
692 	INPLOOKUP_WILDCARD = 0x00000001,	/* Allow wildcard sockets. */
693 	INPLOOKUP_RLOCKPCB = 0x00000002,	/* Return inpcb read-locked. */
694 	INPLOOKUP_WLOCKPCB = 0x00000004,	/* Return inpcb write-locked. */
695 } inp_lookup_t;
696 
697 #define	INPLOOKUP_MASK	(INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
698 	    INPLOOKUP_WLOCKPCB)
699 #define	INPLOOKUP_LOCKMASK	(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)
700 
701 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
702 
703 #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
704 
705 #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
706 
707 #ifdef _KERNEL
708 VNET_DECLARE(int, ipport_reservedhigh);
709 VNET_DECLARE(int, ipport_reservedlow);
710 VNET_DECLARE(int, ipport_lowfirstauto);
711 VNET_DECLARE(int, ipport_lowlastauto);
712 VNET_DECLARE(int, ipport_firstauto);
713 VNET_DECLARE(int, ipport_lastauto);
714 VNET_DECLARE(int, ipport_hifirstauto);
715 VNET_DECLARE(int, ipport_hilastauto);
716 VNET_DECLARE(int, ipport_randomized);
717 VNET_DECLARE(int, ipport_randomcps);
718 VNET_DECLARE(int, ipport_randomtime);
719 VNET_DECLARE(int, ipport_stoprandom);
720 VNET_DECLARE(int, ipport_tcpallocs);
721 
722 #define	V_ipport_reservedhigh	VNET(ipport_reservedhigh)
723 #define	V_ipport_reservedlow	VNET(ipport_reservedlow)
724 #define	V_ipport_lowfirstauto	VNET(ipport_lowfirstauto)
725 #define	V_ipport_lowlastauto	VNET(ipport_lowlastauto)
726 #define	V_ipport_firstauto	VNET(ipport_firstauto)
727 #define	V_ipport_lastauto	VNET(ipport_lastauto)
728 #define	V_ipport_hifirstauto	VNET(ipport_hifirstauto)
729 #define	V_ipport_hilastauto	VNET(ipport_hilastauto)
730 #define	V_ipport_randomized	VNET(ipport_randomized)
731 #define	V_ipport_randomcps	VNET(ipport_randomcps)
732 #define	V_ipport_randomtime	VNET(ipport_randomtime)
733 #define	V_ipport_stoprandom	VNET(ipport_stoprandom)
734 #define	V_ipport_tcpallocs	VNET(ipport_tcpallocs)
735 
736 void	in_pcbinfo_init(struct inpcbinfo *, struct inpcbstorage *,
737 	    u_int, u_int);
738 void	in_pcbinfo_destroy(struct inpcbinfo *);
739 void	in_pcbstorage_init(void *);
740 void	in_pcbstorage_destroy(void *);
741 
742 int	in_pcbbind_check_bindmulti(const struct inpcb *ni,
743 	    const struct inpcb *oi);
744 
745 void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
746 int	in_pcballoc(struct socket *, struct inpcbinfo *);
747 int	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
748 int	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
749 	    u_short *, struct ucred *);
750 int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *, bool);
751 int	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
752 	    u_short *, in_addr_t *, u_short *, struct inpcb **,
753 	    struct ucred *);
754 void	in_pcbdetach(struct inpcb *);
755 void	in_pcbdisconnect(struct inpcb *);
756 void	in_pcbdrop(struct inpcb *);
757 void	in_pcbfree(struct inpcb *);
758 int	in_pcbinshash(struct inpcb *);
759 int	in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
760 	    struct ucred *);
761 int	in_pcblbgroup_numa(struct inpcb *, int arg);
762 struct inpcb *
763 	in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
764 	    struct in_addr, u_int, int, struct ifnet *);
765 struct inpcb *
766 	in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
767 	    struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
768 void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
769 	    int, struct inpcb *(*)(struct inpcb *, int));
770 void	in_pcbref(struct inpcb *);
771 void	in_pcbrehash(struct inpcb *);
772 bool	in_pcbrele_rlocked(struct inpcb *);
773 bool	in_pcbrele_wlocked(struct inpcb *);
774 
775 typedef bool inp_match_t(const struct inpcb *, void *);
776 struct inpcb_iterator {
777 	const struct inpcbinfo	*ipi;
778 	struct inpcb		*inp;
779 	inp_match_t		*match;
780 	void			*ctx;
781 	int			hash;
782 #define	INP_ALL_LIST		-1
783 	const inp_lookup_t	lock;
784 };
785 
786 /* Note: sparse initializers guarantee .inp = NULL. */
787 #define	INP_ITERATOR(_ipi, _lock, _match, _ctx)		\
788 	{						\
789 		.ipi = (_ipi),				\
790 		.lock = (_lock),			\
791 		.hash = INP_ALL_LIST,			\
792 		.match = (_match),			\
793 		.ctx = (_ctx),				\
794 	}
795 #define	INP_ALL_ITERATOR(_ipi, _lock)			\
796 	{						\
797 		.ipi = (_ipi),				\
798 		.lock = (_lock),			\
799 		.hash = INP_ALL_LIST,			\
800 	}
801 
802 struct inpcb *inp_next(struct inpcb_iterator *);
803 void	in_losing(struct inpcb *);
804 void	in_pcbsetsolabel(struct socket *so);
805 int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
806 int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
807 struct sockaddr *
808 	in_sockaddr(in_port_t port, struct in_addr *addr);
809 void	in_pcbsosetlabel(struct socket *so);
810 #ifdef RATELIMIT
811 int
812 in_pcboutput_txrtlmt_locked(struct inpcb *, struct ifnet *,
813 	    struct mbuf *, uint32_t);
814 int	in_pcbattach_txrtlmt(struct inpcb *, struct ifnet *, uint32_t, uint32_t,
815 	    uint32_t, struct m_snd_tag **);
816 void	in_pcbdetach_txrtlmt(struct inpcb *);
817 void    in_pcbdetach_tag(struct m_snd_tag *);
818 int	in_pcbmodify_txrtlmt(struct inpcb *, uint32_t);
819 int	in_pcbquery_txrtlmt(struct inpcb *, uint32_t *);
820 int	in_pcbquery_txrlevel(struct inpcb *, uint32_t *);
821 void	in_pcboutput_txrtlmt(struct inpcb *, struct ifnet *, struct mbuf *);
822 void	in_pcboutput_eagain(struct inpcb *);
823 #endif
824 #endif /* _KERNEL */
825 
826 #endif /* !_NETINET_IN_PCB_H_ */
827