1139823Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1990, 1993
3185791Srwatson *	The Regents of the University of California.
4222213Srwatson * Copyright (c) 2010-2011 Juniper Networks, Inc.
5185791Srwatson * All rights reserved.
61541Srgrimes *
7222213Srwatson * Portions of this software were developed by Robert N. M. Watson under
8222213Srwatson * contract to Juniper Networks, Inc.
9222213Srwatson *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
341541Srgrimes *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
3550477Speter * $FreeBSD$
361541Srgrimes */
371541Srgrimes
382169Spaul#ifndef _NETINET_IN_PCB_H_
392169Spaul#define _NETINET_IN_PCB_H_
402169Spaul
4112644Sbde#include <sys/queue.h>
42102981Sbde#include <sys/_lock.h>
43102981Sbde#include <sys/_mutex.h>
44178285Srwatson#include <sys/_rwlock.h>
4512644Sbde
46178285Srwatson#ifdef _KERNEL
47222787Sbz#include <sys/lock.h>
48178285Srwatson#include <sys/rwlock.h>
49195699Srwatson#include <net/vnet.h>
50205157Srwatson#include <vm/uma.h>
51178285Srwatson#endif
52178285Srwatson
5353541Sshin#define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
5453541Sshin#define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
55105199Ssamstruct inpcbpolicy;
5653541Sshin
571541Srgrimes/*
58183460Srwatson * struct inpcb is the common protocol control block structure used in most
59183460Srwatson * IP transport protocols.
60169154Srwatson *
61169154Srwatson * Pointers to local and foreign host table entries, local and foreign socket
62169154Srwatson * numbers, and pointers up (to a socket structure) and down (to a
63169154Srwatson * protocol-specific control block) are stored here.
641541Srgrimes */
6560938SjakeLIST_HEAD(inpcbhead, inpcb);
6660938SjakeLIST_HEAD(inpcbporthead, inpcbport);
6736079Swollmantypedef	u_quad_t	inp_gen_t;
687684Sdg
6934881Swollman/*
7053541Sshin * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
71169154Srwatson * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
72169154Srwatson * the following structure.
7353541Sshin */
7453541Sshinstruct in_addr_4in6 {
7553541Sshin	u_int32_t	ia46_pad32[3];
7653541Sshin	struct	in_addr	ia46_addr4;
7753541Sshin};
7853541Sshin
7953541Sshin/*
80169154Srwatson * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
81169154Srwatson * some extra padding to accomplish this.
8286764Sjlemon */
8386764Sjlemonstruct in_endpoints {
8486764Sjlemon	u_int16_t	ie_fport;		/* foreign port */
8586764Sjlemon	u_int16_t	ie_lport;		/* local port */
8686764Sjlemon	/* protocol dependent part, local and foreign addr */
8786764Sjlemon	union {
8886764Sjlemon		/* foreign host table entry */
8986764Sjlemon		struct	in_addr_4in6 ie46_foreign;
9086764Sjlemon		struct	in6_addr ie6_foreign;
9186764Sjlemon	} ie_dependfaddr;
9286764Sjlemon	union {
9386764Sjlemon		/* local host table entry */
9486764Sjlemon		struct	in_addr_4in6 ie46_local;
9586764Sjlemon		struct	in6_addr ie6_local;
9686764Sjlemon	} ie_dependladdr;
97185791Srwatson};
9886764Sjlemon#define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
9986764Sjlemon#define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
10086764Sjlemon#define	ie6_faddr	ie_dependfaddr.ie6_foreign
10186764Sjlemon#define	ie6_laddr	ie_dependladdr.ie6_local
10286764Sjlemon
10386764Sjlemon/*
104169154Srwatson * XXX The defines for inc_* are hacks and should be changed to direct
105169154Srwatson * references.
10686764Sjlemon */
10786764Sjlemonstruct in_conninfo {
10886764Sjlemon	u_int8_t	inc_flags;
10986764Sjlemon	u_int8_t	inc_len;
110178888Sjulian	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
111122922Sandre	/* protocol dependent part */
11286764Sjlemon	struct	in_endpoints inc_ie;
11386764Sjlemon};
114186222Sbz
115186222Sbz/*
116186222Sbz * Flags for inc_flags.
117186222Sbz */
118186222Sbz#define	INC_ISIPV6	0x01
119186222Sbz
12086764Sjlemon#define inc_isipv6	inc_flags	/* temp compatability */
12186764Sjlemon#define	inc_fport	inc_ie.ie_fport
12286764Sjlemon#define	inc_lport	inc_ie.ie_lport
12386764Sjlemon#define	inc_faddr	inc_ie.ie_faddr
12486764Sjlemon#define	inc_laddr	inc_ie.ie_laddr
12586764Sjlemon#define	inc6_faddr	inc_ie.ie6_faddr
12686764Sjlemon#define	inc6_laddr	inc_ie.ie6_laddr
12786764Sjlemon
12853541Sshinstruct	icmp6_filter;
12953541Sshin
130180368Srwatson/*-
131180368Srwatson * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4
132180368Srwatson * and IPv6 sockets.  In the case of TCP, further per-connection state is
133180368Srwatson * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
134180368Srwatson * are static after creation or protected by a per-inpcb rwlock, inp_lock.  A
135180368Srwatson * few fields also require the global pcbinfo lock for the inpcb to be held,
136180368Srwatson * when modified, such as the global connection lists and hashes, as well as
137180368Srwatson * binding information (which affects which hash a connection is on).  This
138180368Srwatson * model means that connections can be looked up without holding the
139180368Srwatson * per-connection lock, which is important for performance when attempting to
140180368Srwatson * find the connection for a packet given its IP and port tuple.  Writing to
141180368Srwatson * these fields that write locks be held on both the inpcb and global locks.
142180368Srwatson *
143180368Srwatson * Key:
144180368Srwatson * (c) - Constant after initialization
145222748Srwatson * (g) - Protected by the pcbgroup lock
146180368Srwatson * (i) - Protected by the inpcb lock
147180368Srwatson * (p) - Protected by the pcbinfo lock for the inpcb
148180368Srwatson * (s) - Protected by another subsystem's locks
149180368Srwatson * (x) - Undefined locking
150180368Srwatson *
151180368Srwatson * A few other notes:
152180368Srwatson *
153180368Srwatson * When a read lock is held, stability of the field is guaranteed; to write
154180368Srwatson * to a field, a write lock must generally be held.
155180368Srwatson *
156180368Srwatson * netinet/netinet6-layer code should not assume that the inp_socket pointer
157180368Srwatson * is safe to dereference without inp_lock being held, even for protocols
158180368Srwatson * other than TCP (where the inpcb persists during TIMEWAIT even after the
159180368Srwatson * socket has been freed), or there may be close(2)-related races.
160180368Srwatson *
161180368Srwatson * The inp_vflag field is overloaded, and would otherwise ideally be (c).
162180368Srwatson */
1631541Srgrimesstruct inpcb {
164180368Srwatson	LIST_ENTRY(inpcb) inp_hash;	/* (i/p) hash list */
165222748Srwatson	LIST_ENTRY(inpcb) inp_pcbgrouphash;	/* (g/i) hash list */
166180368Srwatson	LIST_ENTRY(inpcb) inp_list;	/* (i/p) list for all PCBs for proto */
167180368Srwatson	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
168180368Srwatson	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
169222748Srwatson	struct	inpcbgroup *inp_pcbgroup; /* (g/i) PCB group list */
170222748Srwatson	LIST_ENTRY(inpcb) inp_pcbgroup_wild; /* (g/i/p) group wildcard entry */
171185791Srwatson	struct	socket *inp_socket;	/* (i) back pointer to socket */
172183606Sbz	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
173185791Srwatson	u_int32_t inp_flow;		/* (i) IPv6 flow information */
174180368Srwatson	int	inp_flags;		/* (i) generic IP/datagram flags */
175191126Skmacy	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
176180368Srwatson	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
177180368Srwatson	u_char	inp_ip_ttl;		/* (i) time to live proto */
178180368Srwatson	u_char	inp_ip_p;		/* (c) protocol proto */
179180368Srwatson	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
180190880Skmacy	uint32_t inp_flowid;		/* (x) flow id / queue id */
181185773Srwatson	u_int	inp_refcount;		/* (i) refcount */
182224151Sbz	void	*inp_pspare[5];		/* (x) route caching / general use */
183224151Sbz	u_int	inp_ispare[6];		/* (x) route caching / user cookie /
184224151Sbz					 *     general use */
18553541Sshin
186174388Skmacy	/* Local and foreign ports, local and foreign addr. */
187185791Srwatson	struct	in_conninfo inp_inc;	/* (i/p) list for PCB's local port */
188174388Skmacy
189185791Srwatson	/* MAC and IPSEC policy information. */
190180368Srwatson	struct	label *inp_label;	/* (i) MAC label */
191180368Srwatson	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
192174388Skmacy
193169154Srwatson	/* Protocol-dependent part; options. */
19453541Sshin	struct {
195180368Srwatson		u_char	inp4_ip_tos;		/* (i) type of service proto */
196180368Srwatson		struct	mbuf *inp4_options;	/* (i) IP options */
197185791Srwatson		struct	ip_moptions *inp4_moptions; /* (i) IP mcast options */
19853541Sshin	} inp_depend4;
19953541Sshin	struct {
200180368Srwatson		/* (i) IP options */
20153541Sshin		struct	mbuf *inp6_options;
202180368Srwatson		/* (i) IP6 options for outgoing packets */
20353541Sshin		struct	ip6_pktopts *inp6_outputopts;
204180368Srwatson		/* (i) IP multicast options */
20553541Sshin		struct	ip6_moptions *inp6_moptions;
206180368Srwatson		/* (i) ICMPv6 code type filter */
20753541Sshin		struct	icmp6_filter *inp6_icmp6filt;
208180368Srwatson		/* (i) IPV6_CHECKSUM setsockopt */
20953541Sshin		int	inp6_cksum;
21053541Sshin		short	inp6_hops;
21153541Sshin	} inp_depend6;
212180368Srwatson	LIST_ENTRY(inpcb) inp_portlist;	/* (i/p) */
213180368Srwatson	struct	inpcbport *inp_phd;	/* (i/p) head of this list */
214160491Sups#define inp_zero_size offsetof(struct inpcb, inp_gencnt)
215185791Srwatson	inp_gen_t	inp_gencnt;	/* (c) generation count */
216191158Skmacy	struct llentry	*inp_lle;	/* cached L2 information */
217191158Skmacy	struct rtentry	*inp_rt;	/* cached L3 information */
218178285Srwatson	struct rwlock	inp_lock;
219185791Srwatson};
220185791Srwatson#define	inp_fport	inp_inc.inc_fport
221185791Srwatson#define	inp_lport	inp_inc.inc_lport
222185791Srwatson#define	inp_faddr	inp_inc.inc_faddr
223185791Srwatson#define	inp_laddr	inp_inc.inc_laddr
224185791Srwatson#define	inp_ip_tos	inp_depend4.inp4_ip_tos
225185791Srwatson#define	inp_options	inp_depend4.inp4_options
226185791Srwatson#define	inp_moptions	inp_depend4.inp4_moptions
22798102Shsu
22886764Sjlemon#define	in6p_faddr	inp_inc.inc6_faddr
22986764Sjlemon#define	in6p_laddr	inp_inc.inc6_laddr
23053541Sshin#define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
23153541Sshin#define	in6p_flowinfo	inp_flow
23253541Sshin#define	in6p_options	inp_depend6.inp6_options
23353541Sshin#define	in6p_outputopts	inp_depend6.inp6_outputopts
23453541Sshin#define	in6p_moptions	inp_depend6.inp6_moptions
23553541Sshin#define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
23653541Sshin#define	in6p_cksum	inp_depend6.inp6_cksum
237185791Srwatson
238191688Szec#define	inp_vnet	inp_pcbinfo->ipi_vnet
239191688Szec
24034881Swollman/*
241169154Srwatson * The range of the generation count, as used in this implementation, is 9e19.
242169154Srwatson * We would have to create 300 billion connections per second for this number
243169154Srwatson * to roll over in a year.  This seems sufficiently unlikely that we simply
244169154Srwatson * don't concern ourselves with that possibility.
24534881Swollman */
2461541Srgrimes
24736079Swollman/*
248169154Srwatson * Interface exported to userland by various protocols which use inpcbs.  Hack
249169154Srwatson * alert -- only define if struct xsocket is in scope.
25036079Swollman */
25136079Swollman#ifdef _SYS_SOCKETVAR_H_
25236079Swollmanstruct	xinpcb {
25336079Swollman	size_t	xi_len;		/* length of this structure */
25436079Swollman	struct	inpcb xi_inp;
25536079Swollman	struct	xsocket xi_socket;
25636079Swollman	u_quad_t	xi_alignment_hack;
25736079Swollman};
25836079Swollman
25936079Swollmanstruct	xinpgen {
26036079Swollman	size_t	xig_len;	/* length of this structure */
26136079Swollman	u_int	xig_count;	/* number of PCBs at this time */
26236079Swollman	inp_gen_t xig_gen;	/* generation count at this time */
26336079Swollman	so_gen_t xig_sogen;	/* socket generation count at this time */
26436079Swollman};
26536079Swollman#endif /* _SYS_SOCKETVAR_H_ */
26636079Swollman
26732821Sdgstruct inpcbport {
26860938Sjake	LIST_ENTRY(inpcbport) phd_hash;
26932821Sdg	struct inpcbhead phd_pcblist;
27032821Sdg	u_short phd_port;
27132821Sdg};
27232821Sdg
273222213Srwatson/*-
274169154Srwatson * Global data structure for each high-level protocol (UDP, TCP, ...) in both
275169154Srwatson * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
276222213Srwatson *
277222488Srwatson * Each pcbinfo is protected by two locks: ipi_lock and ipi_hash_lock,
278222488Srwatson * the former covering mutable global fields (such as the global pcb list),
279222488Srwatson * and the latter covering the hashed lookup tables.  The lock order is:
280222213Srwatson *
281222748Srwatson *    ipi_lock (before) inpcb locks (before) {ipi_hash_lock, pcbgroup locks}
282222213Srwatson *
283222213Srwatson * Locking key:
284222213Srwatson *
285222213Srwatson * (c) Constant or nearly constant after initialisation
286222213Srwatson * (g) Locked by ipi_lock
287222748Srwatson * (h) Read using either ipi_hash_lock or inpcb lock; write requires both
288222748Srwatson * (p) Protected by one or more pcbgroup locks
289222213Srwatson * (x) Synchronisation properties poorly defined
290169154Srwatson */
291169154Srwatsonstruct inpcbinfo {
292169154Srwatson	/*
293222488Srwatson	 * Global lock protecting global inpcb list, inpcb count, etc.
294169154Srwatson	 */
295222213Srwatson	struct rwlock		 ipi_lock;
296169154Srwatson
297169154Srwatson	/*
298222213Srwatson	 * Global list of inpcbs on the protocol.
299169154Srwatson	 */
300222213Srwatson	struct inpcbhead	*ipi_listhead;		/* (g) */
301222213Srwatson	u_int			 ipi_count;		/* (g) */
302169154Srwatson
303169154Srwatson	/*
304222213Srwatson	 * Generation count -- incremented each time a connection is allocated
305222213Srwatson	 * or freed.
306169154Srwatson	 */
307222213Srwatson	u_quad_t		 ipi_gencnt;		/* (g) */
308169154Srwatson
309169154Srwatson	/*
310169154Srwatson	 * Fields associated with port lookup and allocation.
311169154Srwatson	 */
312222213Srwatson	u_short			 ipi_lastport;		/* (x) */
313222213Srwatson	u_short			 ipi_lastlow;		/* (x) */
314222213Srwatson	u_short			 ipi_lasthi;		/* (x) */
315169154Srwatson
316169154Srwatson	/*
317169154Srwatson	 * UMA zone from which inpcbs are allocated for this protocol.
318169154Srwatson	 */
319222213Srwatson	struct	uma_zone	*ipi_zone;		/* (c) */
320169154Srwatson
321169154Srwatson	/*
322222748Srwatson	 * Connection groups associated with this protocol.  These fields are
323222748Srwatson	 * constant, but pcbgroup structures themselves are protected by
324222748Srwatson	 * per-pcbgroup locks.
325222488Srwatson	 */
326222748Srwatson	struct inpcbgroup	*ipi_pcbgroups;		/* (c) */
327222748Srwatson	u_int			 ipi_npcbgroups;	/* (c) */
328222748Srwatson	u_int			 ipi_hashfields;	/* (c) */
329222748Srwatson
330222748Srwatson	/*
331222748Srwatson	 * Global lock protecting non-pcbgroup hash lookup tables.
332222748Srwatson	 */
333250300Sandre	struct rwlock		 ipi_hash_lock;
334222488Srwatson
335222488Srwatson	/*
336222213Srwatson	 * Global hash of inpcbs, hashed by local and foreign addresses and
337222213Srwatson	 * port numbers.
338169154Srwatson	 */
339222488Srwatson	struct inpcbhead	*ipi_hashbase;		/* (h) */
340222488Srwatson	u_long			 ipi_hashmask;		/* (h) */
341174388Skmacy
342174388Skmacy	/*
343222213Srwatson	 * Global hash of inpcbs, hashed by only local port number.
344222213Srwatson	 */
345222488Srwatson	struct inpcbporthead	*ipi_porthashbase;	/* (h) */
346222488Srwatson	u_long			 ipi_porthashmask;	/* (h) */
347222213Srwatson
348222213Srwatson	/*
349222748Srwatson	 * List of wildcard inpcbs for use with pcbgroups.  In the past, was
350222748Srwatson	 * per-pcbgroup but is now global.  All pcbgroup locks must be held
351222748Srwatson	 * to modify the list, so any is sufficient to read it.
352222748Srwatson	 */
353222748Srwatson	struct inpcbhead	*ipi_wildbase;		/* (p) */
354222748Srwatson	u_long			 ipi_wildmask;		/* (p) */
355222748Srwatson
356222748Srwatson	/*
357191688Szec	 * Pointer to network stack instance
358174388Skmacy	 */
359222213Srwatson	struct vnet		*ipi_vnet;		/* (c) */
360191688Szec
361191688Szec	/*
362191688Szec	 * general use 2
363191688Szec	 */
364181365Srwatson	void 			*ipi_pspare[2];
3657684Sdg};
3667684Sdg
367233096Srmh#ifdef _KERNEL
368222748Srwatson/*
369222748Srwatson * Connection groups hold sets of connections that have similar CPU/thread
370222748Srwatson * affinity.  Each connection belongs to exactly one connection group.
371222748Srwatson */
372222748Srwatsonstruct inpcbgroup {
373222748Srwatson	/*
374222748Srwatson	 * Per-connection group hash of inpcbs, hashed by local and foreign
375222748Srwatson	 * addresses and port numbers.
376222748Srwatson	 */
377222748Srwatson	struct inpcbhead	*ipg_hashbase;		/* (c) */
378222748Srwatson	u_long			 ipg_hashmask;		/* (c) */
379222748Srwatson
380222748Srwatson	/*
381222748Srwatson	 * Notional affinity of this pcbgroup.
382222748Srwatson	 */
383222748Srwatson	u_int			 ipg_cpu;		/* (p) */
384222748Srwatson
385222748Srwatson	/*
386222748Srwatson	 * Per-connection group lock, not to be confused with ipi_lock.
387222748Srwatson	 * Protects the hash table hung off the group, but also the global
388222748Srwatson	 * wildcard list in inpcbinfo.
389222748Srwatson	 */
390222748Srwatson	struct mtx		 ipg_lock;
391222748Srwatson} __aligned(CACHE_LINE_SIZE);
392222748Srwatson
393122991Ssam#define INP_LOCK_INIT(inp, d, t) \
394178285Srwatson	rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE |  RW_DUPOK)
395178285Srwatson#define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
396178285Srwatson#define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
397178285Srwatson#define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
398180536Srwatson#define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
399180536Srwatson#define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
400178285Srwatson#define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
401178285Srwatson#define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
402191125Skmacy#define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
403191125Skmacy#define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
404191125Skmacy#define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
405191125Skmacy#define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
406178285Srwatson#define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
407178285Srwatson#define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
408178285Srwatson#define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
40998102Shsu
410177530Skmacy/*
411181365Srwatson * These locking functions are for inpcb consumers outside of sys/netinet,
412177530Skmacy * more specifically, they were added for the benefit of TOE drivers. The
413177530Skmacy * macros are reserved for use by the stack.
414177530Skmacy */
415177530Skmacyvoid inp_wlock(struct inpcb *);
416177530Skmacyvoid inp_wunlock(struct inpcb *);
417177530Skmacyvoid inp_rlock(struct inpcb *);
418177530Skmacyvoid inp_runlock(struct inpcb *);
419177530Skmacy
420177530Skmacy#ifdef INVARIANTS
421177575Skmacyvoid inp_lock_assert(struct inpcb *);
422177575Skmacyvoid inp_unlock_assert(struct inpcb *);
423177530Skmacy#else
424177530Skmacystatic __inline void
425177575Skmacyinp_lock_assert(struct inpcb *inp __unused)
426177530Skmacy{
427177530Skmacy}
428177530Skmacy
429177530Skmacystatic __inline void
430177575Skmacyinp_unlock_assert(struct inpcb *inp __unused)
431177530Skmacy{
432177530Skmacy}
433177575Skmacy
434177530Skmacy#endif
435180640Skmacy
436180678Skmacyvoid	inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
437180678Skmacyint 	inp_ip_tos_get(const struct inpcb *inp);
438180678Skmacyvoid 	inp_ip_tos_set(struct inpcb *inp, int val);
439180678Skmacystruct socket *
440180678Skmacy	inp_inpcbtosocket(struct inpcb *inp);
441180678Skmacystruct tcpcb *
442180678Skmacy	inp_inpcbtotcpcb(struct inpcb *inp);
443181365Srwatsonvoid 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
444180678Skmacy		uint32_t *faddr, uint16_t *fp);
445252710Strocinyshort	inp_so_options(const struct inpcb *inp);
446180640Skmacy
447177575Skmacy#endif /* _KERNEL */
448177530Skmacy
44998102Shsu#define INP_INFO_LOCK_INIT(ipi, d) \
450178285Srwatson	rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE)
451178285Srwatson#define INP_INFO_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_lock)
452178285Srwatson#define INP_INFO_RLOCK(ipi)	rw_rlock(&(ipi)->ipi_lock)
453178285Srwatson#define INP_INFO_WLOCK(ipi)	rw_wlock(&(ipi)->ipi_lock)
454180536Srwatson#define INP_INFO_TRY_RLOCK(ipi)	rw_try_rlock(&(ipi)->ipi_lock)
455180536Srwatson#define INP_INFO_TRY_WLOCK(ipi)	rw_try_wlock(&(ipi)->ipi_lock)
456204806Srwatson#define INP_INFO_TRY_UPGRADE(ipi)	rw_try_upgrade(&(ipi)->ipi_lock)
457178285Srwatson#define INP_INFO_RUNLOCK(ipi)	rw_runlock(&(ipi)->ipi_lock)
458178285Srwatson#define INP_INFO_WUNLOCK(ipi)	rw_wunlock(&(ipi)->ipi_lock)
459178285Srwatson#define	INP_INFO_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_LOCKED)
460178285Srwatson#define INP_INFO_RLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_RLOCKED)
461178285Srwatson#define INP_INFO_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_WLOCKED)
462178285Srwatson#define INP_INFO_UNLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED)
46398102Shsu
464222488Srwatson#define	INP_HASH_LOCK_INIT(ipi, d) \
465222488Srwatson	rw_init_flags(&(ipi)->ipi_hash_lock, (d), 0)
466222488Srwatson#define	INP_HASH_LOCK_DESTROY(ipi)	rw_destroy(&(ipi)->ipi_hash_lock)
467222488Srwatson#define	INP_HASH_RLOCK(ipi)		rw_rlock(&(ipi)->ipi_hash_lock)
468222488Srwatson#define	INP_HASH_WLOCK(ipi)		rw_wlock(&(ipi)->ipi_hash_lock)
469222488Srwatson#define	INP_HASH_RUNLOCK(ipi)		rw_runlock(&(ipi)->ipi_hash_lock)
470222488Srwatson#define	INP_HASH_WUNLOCK(ipi)		rw_wunlock(&(ipi)->ipi_hash_lock)
471222488Srwatson#define	INP_HASH_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_hash_lock, \
472222488Srwatson					    RA_LOCKED)
473222488Srwatson#define	INP_HASH_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_hash_lock, \
474222488Srwatson					    RA_WLOCKED)
475222488Srwatson
476222748Srwatson#define	INP_GROUP_LOCK_INIT(ipg, d)	mtx_init(&(ipg)->ipg_lock, (d), NULL, \
477222748Srwatson					    MTX_DEF | MTX_DUPOK)
478222748Srwatson#define	INP_GROUP_LOCK_DESTROY(ipg)	mtx_destroy(&(ipg)->ipg_lock)
479222748Srwatson
480222748Srwatson#define	INP_GROUP_LOCK(ipg)		mtx_lock(&(ipg)->ipg_lock)
481222748Srwatson#define	INP_GROUP_LOCK_ASSERT(ipg)	mtx_assert(&(ipg)->ipg_lock, MA_OWNED)
482222748Srwatson#define	INP_GROUP_UNLOCK(ipg)		mtx_unlock(&(ipg)->ipg_lock)
483222748Srwatson
48423324Sdg#define INP_PCBHASH(faddr, lport, fport, mask) \
48532821Sdg	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
48632821Sdg#define INP_PCBPORTHASH(lport, mask) \
48732821Sdg	(ntohs((lport)) & (mask))
48823324Sdg
489185791Srwatson/*
490191126Skmacy * Flags for inp_vflags -- historically version flags only
491185791Srwatson */
492185791Srwatson#define	INP_IPV4	0x1
493185791Srwatson#define	INP_IPV6	0x2
494185791Srwatson#define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
495185791Srwatson
496185791Srwatson/*
497191126Skmacy * Flags for inp_flags.
498185791Srwatson */
499189848Srwatson#define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
500189848Srwatson#define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
501189848Srwatson#define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
502189848Srwatson#define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
503189848Srwatson#define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
504189848Srwatson#define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
505189848Srwatson#define	INP_ANONPORT		0x00000040 /* port chosen for user */
506189848Srwatson#define	INP_RECVIF		0x00000080 /* receive incoming interface */
507189848Srwatson#define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
508189848Srwatson#define	INP_FAITH		0x00000200 /* accept FAITH'ed connections */
509189848Srwatson#define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
510189848Srwatson#define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
511193217Spjd#define	INP_BINDANY		0x00001000 /* allow bind to any address */
512189848Srwatson#define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
513236959Stuexen#define	INP_RECVTOS		0x00004000 /* receive incoming IP TOS */
514189848Srwatson#define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
515189848Srwatson#define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
516189848Srwatson#define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
517189848Srwatson#define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
518189848Srwatson#define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
519189848Srwatson#define	IN6P_RTHDR		0x00100000 /* receive routing header */
520189848Srwatson#define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
521189848Srwatson#define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
522189848Srwatson#define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
523189848Srwatson#define	INP_TIMEWAIT		0x01000000 /* in TIMEWAIT, ppcb is tcptw */
524189848Srwatson#define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
525189848Srwatson#define	INP_DROPPED		0x04000000 /* protocol drop flag */
526189848Srwatson#define	INP_SOCKREF		0x08000000 /* strong socket reference */
527190880Skmacy#define	INP_SW_FLOWID           0x10000000 /* software generated flow id */
528190880Skmacy#define	INP_HW_FLOWID           0x20000000 /* hardware generated flow id */
529121477Sume#define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
530121477Sume#define	IN6P_MTU		0x80000000 /* receive path MTU */
53178064Sume
53219622Sfenner#define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
533236959Stuexen				 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
53478064Sume				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
53578064Sume				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
536121472Sume				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
537121472Sume				 IN6P_MTU)
5381541Srgrimes
539191126Skmacy/*
540191126Skmacy * Flags for inp_flags2.
541191126Skmacy */
542191129Skmacy#define	INP_LLE_VALID		0x00000001 /* cached lle is valid */
543191129Skmacy#define	INP_RT_VALID		0x00000002 /* cached rtentry is valid */
544222748Srwatson#define	INP_PCBGROUPWILD	0x00000004 /* in pcbgroup wildcard list */
545227207Strociny#define	INP_REUSEPORT		0x00000008 /* SO_REUSEPORT option is set */
546241129Sglebius#define	INP_FREED		0x00000010 /* inp itself is not valid */
547252710Strociny#define	INP_REUSEADDR		0x00000020 /* SO_REUSEADDR option is set */
548191126Skmacy
549222488Srwatson/*
550222488Srwatson * Flags passed to in_pcblookup*() functions.
551222488Srwatson */
552222488Srwatson#define	INPLOOKUP_WILDCARD	0x00000001	/* Allow wildcard sockets. */
553222488Srwatson#define	INPLOOKUP_RLOCKPCB	0x00000002	/* Return inpcb read-locked. */
554222488Srwatson#define	INPLOOKUP_WLOCKPCB	0x00000004	/* Return inpcb write-locked. */
555222488Srwatson
556222488Srwatson#define	INPLOOKUP_MASK	(INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
557222488Srwatson			    INPLOOKUP_WLOCKPCB)
558222488Srwatson
5591541Srgrimes#define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
56053541Sshin#define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
5611541Srgrimes
56253541Sshin#define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
56353541Sshin
564133874Srwatson#define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
56553541Sshin
566222748Srwatson/*
567222748Srwatson * Constants for pcbinfo.ipi_hashfields.
568222748Srwatson */
569222748Srwatson#define	IPI_HASHFIELDS_NONE	0
570222748Srwatson#define	IPI_HASHFIELDS_2TUPLE	1
571222748Srwatson#define	IPI_HASHFIELDS_4TUPLE	2
572222748Srwatson
57355205Speter#ifdef _KERNEL
574195699SrwatsonVNET_DECLARE(int, ipport_reservedhigh);
575195699SrwatsonVNET_DECLARE(int, ipport_reservedlow);
576195699SrwatsonVNET_DECLARE(int, ipport_lowfirstauto);
577195699SrwatsonVNET_DECLARE(int, ipport_lowlastauto);
578195699SrwatsonVNET_DECLARE(int, ipport_firstauto);
579195699SrwatsonVNET_DECLARE(int, ipport_lastauto);
580195699SrwatsonVNET_DECLARE(int, ipport_hifirstauto);
581195699SrwatsonVNET_DECLARE(int, ipport_hilastauto);
582195699SrwatsonVNET_DECLARE(int, ipport_randomized);
583195699SrwatsonVNET_DECLARE(int, ipport_randomcps);
584195699SrwatsonVNET_DECLARE(int, ipport_randomtime);
585195699SrwatsonVNET_DECLARE(int, ipport_stoprandom);
586195699SrwatsonVNET_DECLARE(int, ipport_tcpallocs);
587195699Srwatson
588195727Srwatson#define	V_ipport_reservedhigh	VNET(ipport_reservedhigh)
589195727Srwatson#define	V_ipport_reservedlow	VNET(ipport_reservedlow)
590195727Srwatson#define	V_ipport_lowfirstauto	VNET(ipport_lowfirstauto)
591195727Srwatson#define	V_ipport_lowlastauto	VNET(ipport_lowlastauto)
592195727Srwatson#define	V_ipport_firstauto	VNET(ipport_firstauto)
593195727Srwatson#define	V_ipport_lastauto	VNET(ipport_lastauto)
594195727Srwatson#define	V_ipport_hifirstauto	VNET(ipport_hifirstauto)
595195727Srwatson#define	V_ipport_hilastauto	VNET(ipport_hilastauto)
596195727Srwatson#define	V_ipport_randomized	VNET(ipport_randomized)
597195727Srwatson#define	V_ipport_randomcps	VNET(ipport_randomcps)
598195727Srwatson#define	V_ipport_randomtime	VNET(ipport_randomtime)
599195727Srwatson#define	V_ipport_stoprandom	VNET(ipport_stoprandom)
600195727Srwatson#define	V_ipport_tcpallocs	VNET(ipport_tcpallocs)
601195699Srwatson
602205157Srwatsonvoid	in_pcbinfo_destroy(struct inpcbinfo *);
603205157Srwatsonvoid	in_pcbinfo_init(struct inpcbinfo *, const char *, struct inpcbhead *,
604222748Srwatson	    int, int, char *, uma_init, uma_fini, uint32_t, u_int);
605205157Srwatson
606222748Srwatsonstruct inpcbgroup *
607222748Srwatson	in_pcbgroup_byhash(struct inpcbinfo *, u_int, uint32_t);
608222748Srwatsonstruct inpcbgroup *
609222748Srwatson	in_pcbgroup_byinpcb(struct inpcb *);
610222748Srwatsonstruct inpcbgroup *
611222748Srwatson	in_pcbgroup_bytuple(struct inpcbinfo *, struct in_addr, u_short,
612222748Srwatson	    struct in_addr, u_short);
613222748Srwatsonvoid	in_pcbgroup_destroy(struct inpcbinfo *);
614222748Srwatsonint	in_pcbgroup_enabled(struct inpcbinfo *);
615222748Srwatsonvoid	in_pcbgroup_init(struct inpcbinfo *, u_int, int);
616222748Srwatsonvoid	in_pcbgroup_remove(struct inpcb *);
617222748Srwatsonvoid	in_pcbgroup_update(struct inpcb *);
618222748Srwatsonvoid	in_pcbgroup_update_mbuf(struct inpcb *, struct mbuf *);
619222748Srwatson
62098102Shsuvoid	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
621160491Supsint	in_pcballoc(struct socket *, struct inpcbinfo *);
622127505Spjdint	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
623219579Sbzint	in_pcb_lport(struct inpcb *, struct in_addr *, u_short *,
624219579Sbz	    struct ucred *, int);
625105565Siedowseint	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
626127505Spjd	    u_short *, struct ucred *);
627127505Spjdint	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
628222691Srwatsonint	in_pcbconnect_mbuf(struct inpcb *, struct sockaddr *, struct ucred *,
629222691Srwatson	    struct mbuf *);
630105629Siedowseint	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
631105629Siedowse	    u_short *, in_addr_t *, u_short *, struct inpcb **,
632127505Spjd	    struct ucred *);
63392723Salfredvoid	in_pcbdetach(struct inpcb *);
63492723Salfredvoid	in_pcbdisconnect(struct inpcb *);
635158009Srwatsonvoid	in_pcbdrop(struct inpcb *);
636157373Srwatsonvoid	in_pcbfree(struct inpcb *);
63792723Salfredint	in_pcbinshash(struct inpcb *);
638222748Srwatsonint	in_pcbinshash_nopcbgroup(struct inpcb *);
639266718Ssmhint	in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
640266718Ssmh	    struct ucred *);
6411541Srgrimesstruct inpcb *
64292723Salfred	in_pcblookup_local(struct inpcbinfo *,
643180427Sbz	    struct in_addr, u_short, int, struct ucred *);
6447684Sdgstruct inpcb *
645222488Srwatson	in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
64693085Sbde	    struct in_addr, u_int, int, struct ifnet *);
647222691Srwatsonstruct inpcb *
648222691Srwatson	in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
649222691Srwatson	    struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
65098102Shsuvoid	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
65198211Shsu	    int, struct inpcb *(*)(struct inpcb *, int));
652185773Srwatsonvoid	in_pcbref(struct inpcb *);
65392723Salfredvoid	in_pcbrehash(struct inpcb *);
654222691Srwatsonvoid	in_pcbrehash_mbuf(struct inpcb *, struct mbuf *);
655185773Srwatsonint	in_pcbrele(struct inpcb *);
656222217Srwatsonint	in_pcbrele_rlocked(struct inpcb *);
657222217Srwatsonint	in_pcbrele_wlocked(struct inpcb *);
658122875Srwatsonvoid	in_pcbsetsolabel(struct socket *so);
659169462Srwatsonint	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
660169462Srwatsonint	in_getsockaddr(struct socket *so, struct sockaddr **nam);
661102218Struckmanstruct sockaddr *
662102218Struckman	in_sockaddr(in_port_t port, struct in_addr *addr);
663122875Srwatsonvoid	in_pcbsosetlabel(struct socket *so);
66455205Speter#endif /* _KERNEL */
66534881Swollman
66634923Sbde#endif /* !_NETINET_IN_PCB_H_ */
667