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