1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 1982, 1986, 1990, 1993
3199767f8SToomas Soome  *	The Regents of the University of California.
4199767f8SToomas Soome  * Copyright (c) 2010-2011 Juniper Networks, Inc.
5199767f8SToomas Soome  * All rights reserved.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * Portions of this software were developed by Robert N. M. Watson under
8199767f8SToomas Soome  * contract to Juniper Networks, Inc.
9199767f8SToomas Soome  *
10199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
11199767f8SToomas Soome  * modification, are permitted provided that the following conditions
12199767f8SToomas Soome  * are met:
13199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
18199767f8SToomas Soome  * 4. Neither the name of the University nor the names of its contributors
19199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
20199767f8SToomas Soome  *    without specific prior written permission.
21199767f8SToomas Soome  *
22199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32199767f8SToomas Soome  * SUCH DAMAGE.
33199767f8SToomas Soome  *
34199767f8SToomas Soome  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
35199767f8SToomas Soome  * $FreeBSD$
36199767f8SToomas Soome  */
37199767f8SToomas Soome 
38199767f8SToomas Soome #ifndef _NETINET_IN_PCB_H_
39199767f8SToomas Soome #define _NETINET_IN_PCB_H_
40199767f8SToomas Soome 
41199767f8SToomas Soome #include <sys/queue.h>
42199767f8SToomas Soome #include <sys/_lock.h>
43199767f8SToomas Soome #include <sys/_mutex.h>
44199767f8SToomas Soome #include <sys/_rwlock.h>
45199767f8SToomas Soome 
46199767f8SToomas Soome #ifdef _KERNEL
47199767f8SToomas Soome #include <sys/lock.h>
48199767f8SToomas Soome #include <sys/rwlock.h>
49199767f8SToomas Soome #include <net/vnet.h>
50199767f8SToomas Soome #include <vm/uma.h>
51199767f8SToomas Soome #endif
52199767f8SToomas Soome 
53199767f8SToomas Soome #define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
54199767f8SToomas Soome #define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
55199767f8SToomas Soome struct inpcbpolicy;
56199767f8SToomas Soome 
57199767f8SToomas Soome /*
58199767f8SToomas Soome  * struct inpcb is the common protocol control block structure used in most
59199767f8SToomas Soome  * IP transport protocols.
60199767f8SToomas Soome  *
61199767f8SToomas Soome  * Pointers to local and foreign host table entries, local and foreign socket
62199767f8SToomas Soome  * numbers, and pointers up (to a socket structure) and down (to a
63199767f8SToomas Soome  * protocol-specific control block) are stored here.
64199767f8SToomas Soome  */
65199767f8SToomas Soome LIST_HEAD(inpcbhead, inpcb);
66199767f8SToomas Soome LIST_HEAD(inpcbporthead, inpcbport);
67199767f8SToomas Soome typedef	u_quad_t	inp_gen_t;
68199767f8SToomas Soome 
69199767f8SToomas Soome /*
70199767f8SToomas Soome  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
71199767f8SToomas Soome  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
72199767f8SToomas Soome  * the following structure.
73199767f8SToomas Soome  */
74199767f8SToomas Soome struct in_addr_4in6 {
75199767f8SToomas Soome 	u_int32_t	ia46_pad32[3];
76199767f8SToomas Soome 	struct	in_addr	ia46_addr4;
77199767f8SToomas Soome };
78199767f8SToomas Soome 
79199767f8SToomas Soome /*
80199767f8SToomas Soome  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
81199767f8SToomas Soome  * some extra padding to accomplish this.
82199767f8SToomas Soome  * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport,
83199767f8SToomas Soome  * lport, faddr to generate hash, so these fields shouldn't be moved.
84199767f8SToomas Soome  */
85199767f8SToomas Soome struct in_endpoints {
86199767f8SToomas Soome 	u_int16_t	ie_fport;		/* foreign port */
87199767f8SToomas Soome 	u_int16_t	ie_lport;		/* local port */
88199767f8SToomas Soome 	/* protocol dependent part, local and foreign addr */
89199767f8SToomas Soome 	union {
90199767f8SToomas Soome 		/* foreign host table entry */
91199767f8SToomas Soome 		struct	in_addr_4in6 ie46_foreign;
92199767f8SToomas Soome 		struct	in6_addr ie6_foreign;
93199767f8SToomas Soome 	} ie_dependfaddr;
94199767f8SToomas Soome 	union {
95199767f8SToomas Soome 		/* local host table entry */
96199767f8SToomas Soome 		struct	in_addr_4in6 ie46_local;
97199767f8SToomas Soome 		struct	in6_addr ie6_local;
98199767f8SToomas Soome 	} ie_dependladdr;
99199767f8SToomas Soome 	u_int32_t	ie6_zoneid;		/* scope zone id */
100199767f8SToomas Soome };
101199767f8SToomas Soome #define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
102199767f8SToomas Soome #define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
103199767f8SToomas Soome #define	ie6_faddr	ie_dependfaddr.ie6_foreign
104199767f8SToomas Soome #define	ie6_laddr	ie_dependladdr.ie6_local
105199767f8SToomas Soome 
106199767f8SToomas Soome /*
107199767f8SToomas Soome  * XXX The defines for inc_* are hacks and should be changed to direct
108199767f8SToomas Soome  * references.
109199767f8SToomas Soome  */
110199767f8SToomas Soome struct in_conninfo {
111199767f8SToomas Soome 	u_int8_t	inc_flags;
112199767f8SToomas Soome 	u_int8_t	inc_len;
113199767f8SToomas Soome 	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
114199767f8SToomas Soome 	/* protocol dependent part */
115199767f8SToomas Soome 	struct	in_endpoints inc_ie;
116199767f8SToomas Soome };
117199767f8SToomas Soome 
118199767f8SToomas Soome /*
119199767f8SToomas Soome  * Flags for inc_flags.
120199767f8SToomas Soome  */
121199767f8SToomas Soome #define	INC_ISIPV6	0x01
122199767f8SToomas Soome 
123199767f8SToomas Soome #define inc_isipv6	inc_flags	/* temp compatability */
124199767f8SToomas Soome #define	inc_fport	inc_ie.ie_fport
125199767f8SToomas Soome #define	inc_lport	inc_ie.ie_lport
126199767f8SToomas Soome #define	inc_faddr	inc_ie.ie_faddr
127199767f8SToomas Soome #define	inc_laddr	inc_ie.ie_laddr
128199767f8SToomas Soome #define	inc6_faddr	inc_ie.ie6_faddr
129199767f8SToomas Soome #define	inc6_laddr	inc_ie.ie6_laddr
130199767f8SToomas Soome #define	inc6_zoneid	inc_ie.ie6_zoneid
131199767f8SToomas Soome 
132199767f8SToomas Soome struct	icmp6_filter;
133199767f8SToomas Soome 
134199767f8SToomas Soome /*-
135199767f8SToomas Soome  * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and
136199767f8SToomas Soome  * IPv6 sockets.  In the case of TCP and UDP, further per-connection state is
137199767f8SToomas Soome  * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
138199767f8SToomas Soome  * are static after creation or protected by a per-inpcb rwlock, inp_lock.  A
139199767f8SToomas Soome  * few fields are protected by multiple locks as indicated in the locking notes
140199767f8SToomas Soome  * below.  For these fields, all of the listed locks must be write-locked for
141199767f8SToomas Soome  * any modifications.  However, these fields can be safely read while any one of
142199767f8SToomas Soome  * the listed locks are read-locked.  This model can permit greater concurrency
143199767f8SToomas Soome  * for read operations.  For example, connections can be looked up while only
144199767f8SToomas Soome  * holding a read lock on the global pcblist lock.  This is important for
145199767f8SToomas Soome  * performance when attempting to find the connection for a packet given its IP
146199767f8SToomas Soome  * and port tuple.
147199767f8SToomas Soome  *
148199767f8SToomas Soome  * One noteworthy exception is that the global pcbinfo lock follows a different
149199767f8SToomas Soome  * set of rules in relation to the inp_list field.  Rather than being
150199767f8SToomas Soome  * write-locked for modifications and read-locked for list iterations, it must
151199767f8SToomas Soome  * be read-locked during modifications and write-locked during list iterations.
152199767f8SToomas Soome  * This ensures that the relatively rare global list iterations safely walk a
153199767f8SToomas Soome  * stable snapshot of connections while allowing more common list modifications
154199767f8SToomas Soome  * to safely grab the pcblist lock just while adding or removing a connection
155199767f8SToomas Soome  * from the global list.
156199767f8SToomas Soome  *
157199767f8SToomas Soome  * Key:
158199767f8SToomas Soome  * (c) - Constant after initialization
159199767f8SToomas Soome  * (g) - Protected by the pcbgroup lock
160199767f8SToomas Soome  * (i) - Protected by the inpcb lock
161199767f8SToomas Soome  * (p) - Protected by the pcbinfo lock for the inpcb
162199767f8SToomas Soome  * (l) - Protected by the pcblist lock for the inpcb
163199767f8SToomas Soome  * (h) - Protected by the pcbhash lock for the inpcb
164199767f8SToomas Soome  * (s) - Protected by another subsystem's locks
165199767f8SToomas Soome  * (x) - Undefined locking
166199767f8SToomas Soome  *
167199767f8SToomas Soome  * A few other notes:
168199767f8SToomas Soome  *
169199767f8SToomas Soome  * When a read lock is held, stability of the field is guaranteed; to write
170199767f8SToomas Soome  * to a field, a write lock must generally be held.
171199767f8SToomas Soome  *
172199767f8SToomas Soome  * netinet/netinet6-layer code should not assume that the inp_socket pointer
173199767f8SToomas Soome  * is safe to dereference without inp_lock being held, even for protocols
174199767f8SToomas Soome  * other than TCP (where the inpcb persists during TIMEWAIT even after the
175199767f8SToomas Soome  * socket has been freed), or there may be close(2)-related races.
176199767f8SToomas Soome  *
177199767f8SToomas Soome  * The inp_vflag field is overloaded, and would otherwise ideally be (c).
178199767f8SToomas Soome  *
179199767f8SToomas Soome  * TODO:  Currently only the TCP stack is leveraging the global pcbinfo lock
180199767f8SToomas Soome  * read-lock usage during modification, this model can be applied to other
181199767f8SToomas Soome  * protocols (especially SCTP).
182199767f8SToomas Soome  */
183199767f8SToomas Soome struct inpcb {
184199767f8SToomas Soome 	LIST_ENTRY(inpcb) inp_hash;	/* (h/i) hash list */
185199767f8SToomas Soome 	LIST_ENTRY(inpcb) inp_pcbgrouphash;	/* (g/i) hash list */
186199767f8SToomas Soome 	LIST_ENTRY(inpcb) inp_list;	/* (p/l) list for all PCBs for proto */
187199767f8SToomas Soome 	                                /* (p[w]) for list iteration */
188199767f8SToomas Soome 	                                /* (p[r]/l) for addition/removal */
189199767f8SToomas Soome 	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
190199767f8SToomas Soome 	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
191199767f8SToomas Soome 	struct	inpcbgroup *inp_pcbgroup; /* (g/i) PCB group list */
192199767f8SToomas Soome 	LIST_ENTRY(inpcb) inp_pcbgroup_wild; /* (g/i/h) group wildcard entry */
193199767f8SToomas Soome 	struct	socket *inp_socket;	/* (i) back pointer to socket */
194199767f8SToomas Soome 	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
195199767f8SToomas Soome 	u_int32_t inp_flow;		/* (i) IPv6 flow information */
196199767f8SToomas Soome 	int	inp_flags;		/* (i) generic IP/datagram flags */
197199767f8SToomas Soome 	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
198199767f8SToomas Soome 	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
199199767f8SToomas Soome 	u_char	inp_ip_ttl;		/* (i) time to live proto */
200199767f8SToomas Soome 	u_char	inp_ip_p;		/* (c) protocol proto */
201199767f8SToomas Soome 	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
202199767f8SToomas Soome 	uint32_t inp_flowid;		/* (x) flow id / queue id */
203199767f8SToomas Soome 	u_int	inp_refcount;		/* (i) refcount */
204199767f8SToomas Soome 	void	*inp_pspare[5];		/* (x) route caching / general use */
205199767f8SToomas Soome 	uint32_t inp_flowtype;		/* (x) M_HASHTYPE value */
206199767f8SToomas Soome 	uint32_t inp_rss_listen_bucket;	/* (x) overridden RSS listen bucket */
207199767f8SToomas Soome 	u_int	inp_ispare[4];		/* (x) route caching / user cookie /
208199767f8SToomas Soome 					 *     general use */
209199767f8SToomas Soome 
210199767f8SToomas Soome 	/* Local and foreign ports, local and foreign addr. */
211199767f8SToomas Soome 	struct	in_conninfo inp_inc;	/* (i) list for PCB's local port */
212199767f8SToomas Soome 
213199767f8SToomas Soome 	/* MAC and IPSEC policy information. */
214199767f8SToomas Soome 	struct	label *inp_label;	/* (i) MAC label */
215199767f8SToomas Soome 	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
216199767f8SToomas Soome 
217199767f8SToomas Soome 	/* Protocol-dependent part; options. */
218199767f8SToomas Soome 	struct {
219199767f8SToomas Soome 		u_char	inp4_ip_tos;		/* (i) type of service proto */
220199767f8SToomas Soome 		struct	mbuf *inp4_options;	/* (i) IP options */
221199767f8SToomas Soome 		struct	ip_moptions *inp4_moptions; /* (i) IP mcast options */
222199767f8SToomas Soome 	} inp_depend4;
223199767f8SToomas Soome 	struct {
224199767f8SToomas Soome 		/* (i) IP options */
225199767f8SToomas Soome 		struct	mbuf *inp6_options;
226199767f8SToomas Soome 		/* (i) IP6 options for outgoing packets */
227199767f8SToomas Soome 		struct	ip6_pktopts *inp6_outputopts;
228199767f8SToomas Soome 		/* (i) IP multicast options */
229199767f8SToomas Soome 		struct	ip6_moptions *inp6_moptions;
230199767f8SToomas Soome 		/* (i) ICMPv6 code type filter */
231199767f8SToomas Soome 		struct	icmp6_filter *inp6_icmp6filt;
232199767f8SToomas Soome 		/* (i) IPV6_CHECKSUM setsockopt */
233199767f8SToomas Soome 		int	inp6_cksum;
234199767f8SToomas Soome 		short	inp6_hops;
235199767f8SToomas Soome 	} inp_depend6;
236199767f8SToomas Soome 	LIST_ENTRY(inpcb) inp_portlist;	/* (i/h) */
237199767f8SToomas Soome 	struct	inpcbport *inp_phd;	/* (i/h) head of this list */
238199767f8SToomas Soome #define inp_zero_size offsetof(struct inpcb, inp_gencnt)
239199767f8SToomas Soome 	inp_gen_t	inp_gencnt;	/* (c) generation count */
240199767f8SToomas Soome 	struct llentry	*inp_lle;	/* cached L2 information */
241199767f8SToomas Soome 	struct rtentry	*inp_rt;	/* cached L3 information */
242199767f8SToomas Soome 	struct rwlock	inp_lock;
243199767f8SToomas Soome };
244199767f8SToomas Soome #define	inp_fport	inp_inc.inc_fport
245199767f8SToomas Soome #define	inp_lport	inp_inc.inc_lport
246199767f8SToomas Soome #define	inp_faddr	inp_inc.inc_faddr
247199767f8SToomas Soome #define	inp_laddr	inp_inc.inc_laddr
248199767f8SToomas Soome #define	inp_ip_tos	inp_depend4.inp4_ip_tos
249199767f8SToomas Soome #define	inp_options	inp_depend4.inp4_options
250199767f8SToomas Soome #define	inp_moptions	inp_depend4.inp4_moptions
251199767f8SToomas Soome 
252199767f8SToomas Soome #define	in6p_faddr	inp_inc.inc6_faddr
253199767f8SToomas Soome #define	in6p_laddr	inp_inc.inc6_laddr
254199767f8SToomas Soome #define	in6p_zoneid	inp_inc.inc6_zoneid
255199767f8SToomas Soome #define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
256199767f8SToomas Soome #define	in6p_flowinfo	inp_flow
257199767f8SToomas Soome #define	in6p_options	inp_depend6.inp6_options
258199767f8SToomas Soome #define	in6p_outputopts	inp_depend6.inp6_outputopts
259199767f8SToomas Soome #define	in6p_moptions	inp_depend6.inp6_moptions
260199767f8SToomas Soome #define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
261199767f8SToomas Soome #define	in6p_cksum	inp_depend6.inp6_cksum
262199767f8SToomas Soome 
263199767f8SToomas Soome #define	inp_vnet	inp_pcbinfo->ipi_vnet
264199767f8SToomas Soome 
265199767f8SToomas Soome /*
266199767f8SToomas Soome  * The range of the generation count, as used in this implementation, is 9e19.
267199767f8SToomas Soome  * We would have to create 300 billion connections per second for this number
268199767f8SToomas Soome  * to roll over in a year.  This seems sufficiently unlikely that we simply
269199767f8SToomas Soome  * don't concern ourselves with that possibility.
270199767f8SToomas Soome  */
271199767f8SToomas Soome 
272199767f8SToomas Soome /*
273199767f8SToomas Soome  * Interface exported to userland by various protocols which use inpcbs.  Hack
274199767f8SToomas Soome  * alert -- only define if struct xsocket is in scope.
275199767f8SToomas Soome  */
276199767f8SToomas Soome #ifdef _SYS_SOCKETVAR_H_
277199767f8SToomas Soome struct	xinpcb {
278199767f8SToomas Soome 	size_t	xi_len;		/* length of this structure */
279199767f8SToomas Soome 	struct	inpcb xi_inp;
280199767f8SToomas Soome 	struct	xsocket xi_socket;
281199767f8SToomas Soome 	u_quad_t	xi_alignment_hack;
282199767f8SToomas Soome };
283199767f8SToomas Soome 
284199767f8SToomas Soome struct	xinpgen {
285199767f8SToomas Soome 	size_t	xig_len;	/* length of this structure */
286199767f8SToomas Soome 	u_int	xig_count;	/* number of PCBs at this time */
287199767f8SToomas Soome 	inp_gen_t xig_gen;	/* generation count at this time */
288199767f8SToomas Soome 	so_gen_t xig_sogen;	/* socket generation count at this time */
289199767f8SToomas Soome };
290199767f8SToomas Soome #endif /* _SYS_SOCKETVAR_H_ */
291199767f8SToomas Soome 
292199767f8SToomas Soome struct inpcbport {
293199767f8SToomas Soome 	LIST_ENTRY(inpcbport) phd_hash;
294199767f8SToomas Soome 	struct inpcbhead phd_pcblist;
295199767f8SToomas Soome 	u_short phd_port;
296199767f8SToomas Soome };
297199767f8SToomas Soome 
298199767f8SToomas Soome /*-
299199767f8SToomas Soome  * Global data structure for each high-level protocol (UDP, TCP, ...) in both
300199767f8SToomas Soome  * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
301199767f8SToomas Soome  *
302199767f8SToomas Soome  * Each pcbinfo is protected by three locks: ipi_lock, ipi_hash_lock and
303199767f8SToomas Soome  * ipi_list_lock:
304199767f8SToomas Soome  *  - ipi_lock covering the global pcb list stability during loop iteration,
305199767f8SToomas Soome  *  - ipi_hash_lock covering the hashed lookup tables,
306199767f8SToomas Soome  *  - ipi_list_lock covering mutable global fields (such as the global
307199767f8SToomas Soome  *    pcb list)
308199767f8SToomas Soome  *
309199767f8SToomas Soome  * The lock order is:
310199767f8SToomas Soome  *
311199767f8SToomas Soome  *    ipi_lock (before)
312199767f8SToomas Soome  *        inpcb locks (before)
313199767f8SToomas Soome  *            ipi_list locks (before)
314199767f8SToomas Soome  *                {ipi_hash_lock, pcbgroup locks}
315199767f8SToomas Soome  *
316199767f8SToomas Soome  * Locking key:
317199767f8SToomas Soome  *
318199767f8SToomas Soome  * (c) Constant or nearly constant after initialisation
319199767f8SToomas Soome  * (g) Locked by ipi_lock
320199767f8SToomas Soome  * (l) Locked by ipi_list_lock
321199767f8SToomas Soome  * (h) Read using either ipi_hash_lock or inpcb lock; write requires both
322199767f8SToomas Soome  * (p) Protected by one or more pcbgroup locks
323199767f8SToomas Soome  * (x) Synchronisation properties poorly defined
324199767f8SToomas Soome  */
325199767f8SToomas Soome struct inpcbinfo {
326199767f8SToomas Soome 	/*
327199767f8SToomas Soome 	 * Global lock protecting full inpcb list traversal
328199767f8SToomas Soome 	 */
329199767f8SToomas Soome 	struct rwlock		 ipi_lock;
330199767f8SToomas Soome 
331199767f8SToomas Soome 	/*
332199767f8SToomas Soome 	 * Global list of inpcbs on the protocol.
333199767f8SToomas Soome 	 */
334199767f8SToomas Soome 	struct inpcbhead	*ipi_listhead;		/* (g/l) */
335199767f8SToomas Soome 	u_int			 ipi_count;		/* (l) */
336199767f8SToomas Soome 
337199767f8SToomas Soome 	/*
338199767f8SToomas Soome 	 * Generation count -- incremented each time a connection is allocated
339199767f8SToomas Soome 	 * or freed.
340199767f8SToomas Soome 	 */
341199767f8SToomas Soome 	u_quad_t		 ipi_gencnt;		/* (l) */
342199767f8SToomas Soome 
343199767f8SToomas Soome 	/*
344199767f8SToomas Soome 	 * Fields associated with port lookup and allocation.
345199767f8SToomas Soome 	 */
346199767f8SToomas Soome 	u_short			 ipi_lastport;		/* (x) */
347199767f8SToomas Soome 	u_short			 ipi_lastlow;		/* (x) */
348199767f8SToomas Soome 	u_short			 ipi_lasthi;		/* (x) */
349199767f8SToomas Soome 
350199767f8SToomas Soome 	/*
351199767f8SToomas Soome 	 * UMA zone from which inpcbs are allocated for this protocol.
352199767f8SToomas Soome 	 */
353199767f8SToomas Soome 	struct	uma_zone	*ipi_zone;		/* (c) */
354199767f8SToomas Soome 
355199767f8SToomas Soome 	/*
356199767f8SToomas Soome 	 * Connection groups associated with this protocol.  These fields are
357199767f8SToomas Soome 	 * constant, but pcbgroup structures themselves are protected by
358199767f8SToomas Soome 	 * per-pcbgroup locks.
359199767f8SToomas Soome 	 */
360199767f8SToomas Soome 	struct inpcbgroup	*ipi_pcbgroups;		/* (c) */
361199767f8SToomas Soome 	u_int			 ipi_npcbgroups;	/* (c) */
362199767f8SToomas Soome 	u_int			 ipi_hashfields;	/* (c) */
363199767f8SToomas Soome 
364199767f8SToomas Soome 	/*
365199767f8SToomas Soome 	 * Global lock protecting non-pcbgroup hash lookup tables.
366199767f8SToomas Soome 	 */
367199767f8SToomas Soome 	struct rwlock		 ipi_hash_lock;
368199767f8SToomas Soome 
369199767f8SToomas Soome 	/*
370199767f8SToomas Soome 	 * Global hash of inpcbs, hashed by local and foreign addresses and
371199767f8SToomas Soome 	 * port numbers.
372199767f8SToomas Soome 	 */
373199767f8SToomas Soome 	struct inpcbhead	*ipi_hashbase;		/* (h) */
374199767f8SToomas Soome 	u_long			 ipi_hashmask;		/* (h) */
375199767f8SToomas Soome 
376199767f8SToomas Soome 	/*
377199767f8SToomas Soome 	 * Global hash of inpcbs, hashed by only local port number.
378199767f8SToomas Soome 	 */
379199767f8SToomas Soome 	struct inpcbporthead	*ipi_porthashbase;	/* (h) */
380199767f8SToomas Soome 	u_long			 ipi_porthashmask;	/* (h) */
381199767f8SToomas Soome 
382199767f8SToomas Soome 	/*
383199767f8SToomas Soome 	 * List of wildcard inpcbs for use with pcbgroups.  In the past, was
384199767f8SToomas Soome 	 * per-pcbgroup but is now global.  All pcbgroup locks must be held
385199767f8SToomas Soome 	 * to modify the list, so any is sufficient to read it.
386199767f8SToomas Soome 	 */
387199767f8SToomas Soome 	struct inpcbhead	*ipi_wildbase;		/* (p) */
388199767f8SToomas Soome 	u_long			 ipi_wildmask;		/* (p) */
389199767f8SToomas Soome 
390199767f8SToomas Soome 	/*
391199767f8SToomas Soome 	 * Pointer to network stack instance
392199767f8SToomas Soome 	 */
393199767f8SToomas Soome 	struct vnet		*ipi_vnet;		/* (c) */
394199767f8SToomas Soome 
395199767f8SToomas Soome 	/*
396199767f8SToomas Soome 	 * general use 2
397199767f8SToomas Soome 	 */
398199767f8SToomas Soome 	void 			*ipi_pspare[2];
399199767f8SToomas Soome 
400199767f8SToomas Soome 	/*
401199767f8SToomas Soome 	 * Global lock protecting global inpcb list, inpcb count, etc.
402199767f8SToomas Soome 	 */
403199767f8SToomas Soome 	struct rwlock		 ipi_list_lock;
404199767f8SToomas Soome };
405199767f8SToomas Soome 
406199767f8SToomas Soome #ifdef _KERNEL
407199767f8SToomas Soome /*
408199767f8SToomas Soome  * Connection groups hold sets of connections that have similar CPU/thread
409199767f8SToomas Soome  * affinity.  Each connection belongs to exactly one connection group.
410199767f8SToomas Soome  */
411199767f8SToomas Soome struct inpcbgroup {
412199767f8SToomas Soome 	/*
413199767f8SToomas Soome 	 * Per-connection group hash of inpcbs, hashed by local and foreign
414199767f8SToomas Soome 	 * addresses and port numbers.
415199767f8SToomas Soome 	 */
416199767f8SToomas Soome 	struct inpcbhead	*ipg_hashbase;		/* (c) */
417199767f8SToomas Soome 	u_long			 ipg_hashmask;		/* (c) */
418199767f8SToomas Soome 
419199767f8SToomas Soome 	/*
420199767f8SToomas Soome 	 * Notional affinity of this pcbgroup.
421199767f8SToomas Soome 	 */
422199767f8SToomas Soome 	u_int			 ipg_cpu;		/* (p) */
423199767f8SToomas Soome 
424199767f8SToomas Soome 	/*
425199767f8SToomas Soome 	 * Per-connection group lock, not to be confused with ipi_lock.
426199767f8SToomas Soome 	 * Protects the hash table hung off the group, but also the global
427199767f8SToomas Soome 	 * wildcard list in inpcbinfo.
428199767f8SToomas Soome 	 */
429199767f8SToomas Soome 	struct mtx		 ipg_lock;
430199767f8SToomas Soome } __aligned(CACHE_LINE_SIZE);
431199767f8SToomas Soome 
432199767f8SToomas Soome #define INP_LOCK_INIT(inp, d, t) \
433199767f8SToomas Soome 	rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE |  RW_DUPOK)
434199767f8SToomas Soome #define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
435199767f8SToomas Soome #define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
436199767f8SToomas Soome #define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
437199767f8SToomas Soome #define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
438199767f8SToomas Soome #define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
439199767f8SToomas Soome #define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
440199767f8SToomas Soome #define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
441199767f8SToomas Soome #define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
442199767f8SToomas Soome #define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
443199767f8SToomas Soome #define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
444199767f8SToomas Soome #define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
445199767f8SToomas Soome #define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
446199767f8SToomas Soome #define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
447199767f8SToomas Soome #define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
448199767f8SToomas Soome 
449199767f8SToomas Soome /*
450199767f8SToomas Soome  * These locking functions are for inpcb consumers outside of sys/netinet,
451199767f8SToomas Soome  * more specifically, they were added for the benefit of TOE drivers. The
452199767f8SToomas Soome  * macros are reserved for use by the stack.
453199767f8SToomas Soome  */
454199767f8SToomas Soome void inp_wlock(struct inpcb *);
455199767f8SToomas Soome void inp_wunlock(struct inpcb *);
456199767f8SToomas Soome void inp_rlock(struct inpcb *);
457199767f8SToomas Soome void inp_runlock(struct inpcb *);
458199767f8SToomas Soome 
459199767f8SToomas Soome #ifdef INVARIANTS
460199767f8SToomas Soome void inp_lock_assert(struct inpcb *);
461199767f8SToomas Soome void inp_unlock_assert(struct inpcb *);
462199767f8SToomas Soome #else
463199767f8SToomas Soome static __inline void
inp_lock_assert(struct inpcb * inp __unused)464199767f8SToomas Soome inp_lock_assert(struct inpcb *inp __unused)
465199767f8SToomas Soome {
466199767f8SToomas Soome }
467199767f8SToomas Soome 
468199767f8SToomas Soome static __inline void
inp_unlock_assert(struct inpcb * inp __unused)469199767f8SToomas Soome inp_unlock_assert(struct inpcb *inp __unused)
470199767f8SToomas Soome {
471199767f8SToomas Soome }
472199767f8SToomas Soome 
473199767f8SToomas Soome #endif
474199767f8SToomas Soome 
475199767f8SToomas Soome void	inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
476199767f8SToomas Soome int 	inp_ip_tos_get(const struct inpcb *inp);
477199767f8SToomas Soome void 	inp_ip_tos_set(struct inpcb *inp, int val);
478199767f8SToomas Soome struct socket *
479199767f8SToomas Soome 	inp_inpcbtosocket(struct inpcb *inp);
480199767f8SToomas Soome struct tcpcb *
481199767f8SToomas Soome 	inp_inpcbtotcpcb(struct inpcb *inp);
482199767f8SToomas Soome void 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
483199767f8SToomas Soome 		uint32_t *faddr, uint16_t *fp);
484199767f8SToomas Soome short	inp_so_options(const struct inpcb *inp);
485199767f8SToomas Soome 
486199767f8SToomas Soome #endif /* _KERNEL */
487199767f8SToomas Soome 
488199767f8SToomas Soome #define INP_INFO_LOCK_INIT(ipi, d) \
489199767f8SToomas Soome 	rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE)
490199767f8SToomas Soome #define INP_INFO_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_lock)
491199767f8SToomas Soome #define INP_INFO_RLOCK(ipi)	rw_rlock(&(ipi)->ipi_lock)
492199767f8SToomas Soome #define INP_INFO_WLOCK(ipi)	rw_wlock(&(ipi)->ipi_lock)
493199767f8SToomas Soome #define INP_INFO_TRY_RLOCK(ipi)	rw_try_rlock(&(ipi)->ipi_lock)
494199767f8SToomas Soome #define INP_INFO_TRY_WLOCK(ipi)	rw_try_wlock(&(ipi)->ipi_lock)
495199767f8SToomas Soome #define INP_INFO_TRY_UPGRADE(ipi)	rw_try_upgrade(&(ipi)->ipi_lock)
496199767f8SToomas Soome #define INP_INFO_WLOCKED(ipi)	rw_wowned(&(ipi)->ipi_lock)
497199767f8SToomas Soome #define INP_INFO_RUNLOCK(ipi)	rw_runlock(&(ipi)->ipi_lock)
498199767f8SToomas Soome #define INP_INFO_WUNLOCK(ipi)	rw_wunlock(&(ipi)->ipi_lock)
499199767f8SToomas Soome #define	INP_INFO_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_LOCKED)
500199767f8SToomas Soome #define INP_INFO_RLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_RLOCKED)
501199767f8SToomas Soome #define INP_INFO_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_WLOCKED)
502199767f8SToomas Soome #define INP_INFO_UNLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED)
503199767f8SToomas Soome 
504199767f8SToomas Soome #define INP_LIST_LOCK_INIT(ipi, d) \
505199767f8SToomas Soome         rw_init_flags(&(ipi)->ipi_list_lock, (d), 0)
506199767f8SToomas Soome #define INP_LIST_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_list_lock)
507199767f8SToomas Soome #define INP_LIST_RLOCK(ipi)     rw_rlock(&(ipi)->ipi_list_lock)
508199767f8SToomas Soome #define INP_LIST_WLOCK(ipi)     rw_wlock(&(ipi)->ipi_list_lock)
509199767f8SToomas Soome #define INP_LIST_TRY_RLOCK(ipi) rw_try_rlock(&(ipi)->ipi_list_lock)
510199767f8SToomas Soome #define INP_LIST_TRY_WLOCK(ipi) rw_try_wlock(&(ipi)->ipi_list_lock)
511199767f8SToomas Soome #define INP_LIST_TRY_UPGRADE(ipi)       rw_try_upgrade(&(ipi)->ipi_list_lock)
512199767f8SToomas Soome #define INP_LIST_RUNLOCK(ipi)   rw_runlock(&(ipi)->ipi_list_lock)
513199767f8SToomas Soome #define INP_LIST_WUNLOCK(ipi)   rw_wunlock(&(ipi)->ipi_list_lock)
514199767f8SToomas Soome #define INP_LIST_LOCK_ASSERT(ipi) \
515199767f8SToomas Soome 	rw_assert(&(ipi)->ipi_list_lock, RA_LOCKED)
516199767f8SToomas Soome #define INP_LIST_RLOCK_ASSERT(ipi) \
517199767f8SToomas Soome 	rw_assert(&(ipi)->ipi_list_lock, RA_RLOCKED)
518199767f8SToomas Soome #define INP_LIST_WLOCK_ASSERT(ipi) \
519199767f8SToomas Soome 	rw_assert(&(ipi)->ipi_list_lock, RA_WLOCKED)
520199767f8SToomas Soome #define INP_LIST_UNLOCK_ASSERT(ipi) \
521199767f8SToomas Soome 	rw_assert(&(ipi)->ipi_list_lock, RA_UNLOCKED)
522199767f8SToomas Soome 
523199767f8SToomas Soome #define	INP_HASH_LOCK_INIT(ipi, d) \
524199767f8SToomas Soome 	rw_init_flags(&(ipi)->ipi_hash_lock, (d), 0)
525199767f8SToomas Soome #define	INP_HASH_LOCK_DESTROY(ipi)	rw_destroy(&(ipi)->ipi_hash_lock)
526199767f8SToomas Soome #define	INP_HASH_RLOCK(ipi)		rw_rlock(&(ipi)->ipi_hash_lock)
527199767f8SToomas Soome #define	INP_HASH_WLOCK(ipi)		rw_wlock(&(ipi)->ipi_hash_lock)
528199767f8SToomas Soome #define	INP_HASH_RUNLOCK(ipi)		rw_runlock(&(ipi)->ipi_hash_lock)
529199767f8SToomas Soome #define	INP_HASH_WUNLOCK(ipi)		rw_wunlock(&(ipi)->ipi_hash_lock)
530199767f8SToomas Soome #define	INP_HASH_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_hash_lock, \
531199767f8SToomas Soome 					    RA_LOCKED)
532199767f8SToomas Soome #define	INP_HASH_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_hash_lock, \
533199767f8SToomas Soome 					    RA_WLOCKED)
534199767f8SToomas Soome 
535199767f8SToomas Soome #define	INP_GROUP_LOCK_INIT(ipg, d)	mtx_init(&(ipg)->ipg_lock, (d), NULL, \
536199767f8SToomas Soome 					    MTX_DEF | MTX_DUPOK)
537199767f8SToomas Soome #define	INP_GROUP_LOCK_DESTROY(ipg)	mtx_destroy(&(ipg)->ipg_lock)
538199767f8SToomas Soome 
539199767f8SToomas Soome #define	INP_GROUP_LOCK(ipg)		mtx_lock(&(ipg)->ipg_lock)
540199767f8SToomas Soome #define	INP_GROUP_LOCK_ASSERT(ipg)	mtx_assert(&(ipg)->ipg_lock, MA_OWNED)
541199767f8SToomas Soome #define	INP_GROUP_UNLOCK(ipg)		mtx_unlock(&(ipg)->ipg_lock)
542199767f8SToomas Soome 
543199767f8SToomas Soome #define INP_PCBHASH(faddr, lport, fport, mask) \
544199767f8SToomas Soome 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
545199767f8SToomas Soome #define INP_PCBPORTHASH(lport, mask) \
546199767f8SToomas Soome 	(ntohs((lport)) & (mask))
547199767f8SToomas Soome #define	INP6_PCBHASHKEY(faddr)	((faddr)->s6_addr32[3])
548199767f8SToomas Soome 
549199767f8SToomas Soome /*
550199767f8SToomas Soome  * Flags for inp_vflags -- historically version flags only
551199767f8SToomas Soome  */
552199767f8SToomas Soome #define	INP_IPV4	0x1
553199767f8SToomas Soome #define	INP_IPV6	0x2
554199767f8SToomas Soome #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
555199767f8SToomas Soome 
556199767f8SToomas Soome /*
557199767f8SToomas Soome  * Flags for inp_flags.
558199767f8SToomas Soome  */
559199767f8SToomas Soome #define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
560199767f8SToomas Soome #define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
561199767f8SToomas Soome #define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
562199767f8SToomas Soome #define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
563199767f8SToomas Soome #define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
564199767f8SToomas Soome #define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
565199767f8SToomas Soome #define	INP_ANONPORT		0x00000040 /* port chosen for user */
566199767f8SToomas Soome #define	INP_RECVIF		0x00000080 /* receive incoming interface */
567199767f8SToomas Soome #define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
568199767f8SToomas Soome 				   	   /* 0x000200 unused: was INP_FAITH */
569199767f8SToomas Soome #define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
570199767f8SToomas Soome #define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
571199767f8SToomas Soome #define	INP_BINDANY		0x00001000 /* allow bind to any address */
572199767f8SToomas Soome #define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
573199767f8SToomas Soome #define	INP_RECVTOS		0x00004000 /* receive incoming IP TOS */
574199767f8SToomas Soome #define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
575199767f8SToomas Soome #define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
576199767f8SToomas Soome #define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
577199767f8SToomas Soome #define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
578199767f8SToomas Soome #define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
579199767f8SToomas Soome #define	IN6P_RTHDR		0x00100000 /* receive routing header */
580199767f8SToomas Soome #define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
581199767f8SToomas Soome #define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
582199767f8SToomas Soome #define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
583199767f8SToomas Soome #define	INP_TIMEWAIT		0x01000000 /* in TIMEWAIT, ppcb is tcptw */
584199767f8SToomas Soome #define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
585199767f8SToomas Soome #define	INP_DROPPED		0x04000000 /* protocol drop flag */
586199767f8SToomas Soome #define	INP_SOCKREF		0x08000000 /* strong socket reference */
587199767f8SToomas Soome #define	INP_RESERVED_0          0x10000000 /* reserved field */
588199767f8SToomas Soome #define	INP_RESERVED_1          0x20000000 /* reserved field */
589199767f8SToomas Soome #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
590199767f8SToomas Soome #define	IN6P_MTU		0x80000000 /* receive path MTU */
591199767f8SToomas Soome 
592199767f8SToomas Soome #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
593199767f8SToomas Soome 				 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
594199767f8SToomas Soome 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
595199767f8SToomas Soome 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
596199767f8SToomas Soome 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
597199767f8SToomas Soome 				 IN6P_MTU)
598199767f8SToomas Soome 
599199767f8SToomas Soome /*
600199767f8SToomas Soome  * Flags for inp_flags2.
601199767f8SToomas Soome  */
602*55fea89dSDan Cross #define	INP_LLE_VALID		0x00000001 /* cached lle is valid */
603199767f8SToomas Soome #define	INP_RT_VALID		0x00000002 /* cached rtentry is valid */
604199767f8SToomas Soome #define	INP_PCBGROUPWILD	0x00000004 /* in pcbgroup wildcard list */
605199767f8SToomas Soome #define	INP_REUSEPORT		0x00000008 /* SO_REUSEPORT option is set */
606199767f8SToomas Soome #define	INP_FREED		0x00000010 /* inp itself is not valid */
607199767f8SToomas Soome #define	INP_REUSEADDR		0x00000020 /* SO_REUSEADDR option is set */
608199767f8SToomas Soome #define	INP_BINDMULTI		0x00000040 /* IP_BINDMULTI option is set */
609199767f8SToomas Soome #define	INP_RSS_BUCKET_SET	0x00000080 /* IP_RSS_LISTEN_BUCKET is set */
610199767f8SToomas Soome #define	INP_RECVFLOWID		0x00000100 /* populate recv datagram with flow info */
611199767f8SToomas Soome #define	INP_RECVRSSBUCKETID	0x00000200 /* populate recv datagram with bucket id */
612199767f8SToomas Soome 
613199767f8SToomas Soome /*
614199767f8SToomas Soome  * Flags passed to in_pcblookup*() functions.
615199767f8SToomas Soome  */
616199767f8SToomas Soome #define	INPLOOKUP_WILDCARD	0x00000001	/* Allow wildcard sockets. */
617199767f8SToomas Soome #define	INPLOOKUP_RLOCKPCB	0x00000002	/* Return inpcb read-locked. */
618199767f8SToomas Soome #define	INPLOOKUP_WLOCKPCB	0x00000004	/* Return inpcb write-locked. */
619199767f8SToomas Soome 
620199767f8SToomas Soome #define	INPLOOKUP_MASK	(INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
621199767f8SToomas Soome 			    INPLOOKUP_WLOCKPCB)
622199767f8SToomas Soome 
623199767f8SToomas Soome #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
624199767f8SToomas Soome #define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
625199767f8SToomas Soome 
626199767f8SToomas Soome #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
627199767f8SToomas Soome 
628199767f8SToomas Soome #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
629199767f8SToomas Soome 
630199767f8SToomas Soome /*
631199767f8SToomas Soome  * Constants for pcbinfo.ipi_hashfields.
632199767f8SToomas Soome  */
633199767f8SToomas Soome #define	IPI_HASHFIELDS_NONE	0
634199767f8SToomas Soome #define	IPI_HASHFIELDS_2TUPLE	1
635199767f8SToomas Soome #define	IPI_HASHFIELDS_4TUPLE	2
636199767f8SToomas Soome 
637199767f8SToomas Soome #ifdef _KERNEL
638199767f8SToomas Soome VNET_DECLARE(int, ipport_reservedhigh);
639199767f8SToomas Soome VNET_DECLARE(int, ipport_reservedlow);
640199767f8SToomas Soome VNET_DECLARE(int, ipport_lowfirstauto);
641199767f8SToomas Soome VNET_DECLARE(int, ipport_lowlastauto);
642199767f8SToomas Soome VNET_DECLARE(int, ipport_firstauto);
643199767f8SToomas Soome VNET_DECLARE(int, ipport_lastauto);
644199767f8SToomas Soome VNET_DECLARE(int, ipport_hifirstauto);
645199767f8SToomas Soome VNET_DECLARE(int, ipport_hilastauto);
646199767f8SToomas Soome VNET_DECLARE(int, ipport_randomized);
647199767f8SToomas Soome VNET_DECLARE(int, ipport_randomcps);
648199767f8SToomas Soome VNET_DECLARE(int, ipport_randomtime);
649199767f8SToomas Soome VNET_DECLARE(int, ipport_stoprandom);
650199767f8SToomas Soome VNET_DECLARE(int, ipport_tcpallocs);
651199767f8SToomas Soome 
652199767f8SToomas Soome #define	V_ipport_reservedhigh	VNET(ipport_reservedhigh)
653199767f8SToomas Soome #define	V_ipport_reservedlow	VNET(ipport_reservedlow)
654199767f8SToomas Soome #define	V_ipport_lowfirstauto	VNET(ipport_lowfirstauto)
655199767f8SToomas Soome #define	V_ipport_lowlastauto	VNET(ipport_lowlastauto)
656199767f8SToomas Soome #define	V_ipport_firstauto	VNET(ipport_firstauto)
657199767f8SToomas Soome #define	V_ipport_lastauto	VNET(ipport_lastauto)
658199767f8SToomas Soome #define	V_ipport_hifirstauto	VNET(ipport_hifirstauto)
659199767f8SToomas Soome #define	V_ipport_hilastauto	VNET(ipport_hilastauto)
660199767f8SToomas Soome #define	V_ipport_randomized	VNET(ipport_randomized)
661199767f8SToomas Soome #define	V_ipport_randomcps	VNET(ipport_randomcps)
662199767f8SToomas Soome #define	V_ipport_randomtime	VNET(ipport_randomtime)
663199767f8SToomas Soome #define	V_ipport_stoprandom	VNET(ipport_stoprandom)
664199767f8SToomas Soome #define	V_ipport_tcpallocs	VNET(ipport_tcpallocs)
665199767f8SToomas Soome 
666199767f8SToomas Soome void	in_pcbinfo_destroy(struct inpcbinfo *);
667199767f8SToomas Soome void	in_pcbinfo_init(struct inpcbinfo *, const char *, struct inpcbhead *,
668199767f8SToomas Soome 	    int, int, char *, uma_init, uma_fini, uint32_t, u_int);
669199767f8SToomas Soome 
670199767f8SToomas Soome int	in_pcbbind_check_bindmulti(const struct inpcb *ni,
671199767f8SToomas Soome 	    const struct inpcb *oi);
672199767f8SToomas Soome 
673199767f8SToomas Soome struct inpcbgroup *
674199767f8SToomas Soome 	in_pcbgroup_byhash(struct inpcbinfo *, u_int, uint32_t);
675199767f8SToomas Soome struct inpcbgroup *
676199767f8SToomas Soome 	in_pcbgroup_byinpcb(struct inpcb *);
677199767f8SToomas Soome struct inpcbgroup *
678199767f8SToomas Soome 	in_pcbgroup_bytuple(struct inpcbinfo *, struct in_addr, u_short,
679199767f8SToomas Soome 	    struct in_addr, u_short);
680199767f8SToomas Soome void	in_pcbgroup_destroy(struct inpcbinfo *);
681199767f8SToomas Soome int	in_pcbgroup_enabled(struct inpcbinfo *);
682199767f8SToomas Soome void	in_pcbgroup_init(struct inpcbinfo *, u_int, int);
683199767f8SToomas Soome void	in_pcbgroup_remove(struct inpcb *);
684199767f8SToomas Soome void	in_pcbgroup_update(struct inpcb *);
685199767f8SToomas Soome void	in_pcbgroup_update_mbuf(struct inpcb *, struct mbuf *);
686199767f8SToomas Soome 
687199767f8SToomas Soome void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
688199767f8SToomas Soome int	in_pcballoc(struct socket *, struct inpcbinfo *);
689199767f8SToomas Soome int	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
690199767f8SToomas Soome int	in_pcb_lport(struct inpcb *, struct in_addr *, u_short *,
691199767f8SToomas Soome 	    struct ucred *, int);
692199767f8SToomas Soome int	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
693199767f8SToomas Soome 	    u_short *, struct ucred *);
694199767f8SToomas Soome int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
695199767f8SToomas Soome int	in_pcbconnect_mbuf(struct inpcb *, struct sockaddr *, struct ucred *,
696199767f8SToomas Soome 	    struct mbuf *);
697199767f8SToomas Soome int	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
698199767f8SToomas Soome 	    u_short *, in_addr_t *, u_short *, struct inpcb **,
699199767f8SToomas Soome 	    struct ucred *);
700199767f8SToomas Soome void	in_pcbdetach(struct inpcb *);
701199767f8SToomas Soome void	in_pcbdisconnect(struct inpcb *);
702199767f8SToomas Soome void	in_pcbdrop(struct inpcb *);
703199767f8SToomas Soome void	in_pcbfree(struct inpcb *);
704199767f8SToomas Soome int	in_pcbinshash(struct inpcb *);
705199767f8SToomas Soome int	in_pcbinshash_nopcbgroup(struct inpcb *);
706199767f8SToomas Soome int	in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
707199767f8SToomas Soome 	    struct ucred *);
708199767f8SToomas Soome struct inpcb *
709199767f8SToomas Soome 	in_pcblookup_local(struct inpcbinfo *,
710199767f8SToomas Soome 	    struct in_addr, u_short, int, struct ucred *);
711199767f8SToomas Soome struct inpcb *
712199767f8SToomas Soome 	in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
713199767f8SToomas Soome 	    struct in_addr, u_int, int, struct ifnet *);
714199767f8SToomas Soome struct inpcb *
715199767f8SToomas Soome 	in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
716199767f8SToomas Soome 	    struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
717199767f8SToomas Soome void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
718199767f8SToomas Soome 	    int, struct inpcb *(*)(struct inpcb *, int));
719199767f8SToomas Soome void	in_pcbref(struct inpcb *);
720199767f8SToomas Soome void	in_pcbrehash(struct inpcb *);
721199767f8SToomas Soome void	in_pcbrehash_mbuf(struct inpcb *, struct mbuf *);
722199767f8SToomas Soome int	in_pcbrele(struct inpcb *);
723199767f8SToomas Soome int	in_pcbrele_rlocked(struct inpcb *);
724199767f8SToomas Soome int	in_pcbrele_wlocked(struct inpcb *);
725199767f8SToomas Soome void	in_pcbsetsolabel(struct socket *so);
726199767f8SToomas Soome int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
727199767f8SToomas Soome int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
728199767f8SToomas Soome struct sockaddr *
729199767f8SToomas Soome 	in_sockaddr(in_port_t port, struct in_addr *addr);
730199767f8SToomas Soome void	in_pcbsosetlabel(struct socket *so);
731199767f8SToomas Soome #endif /* _KERNEL */
732199767f8SToomas Soome 
733199767f8SToomas Soome #endif /* !_NETINET_IN_PCB_H_ */
734