xref: /titanic_52/usr/src/boot/include/netinet/in_pcb.h (revision 4a5d661a82b942b6538acd26209d959ce98b593a)
1*4a5d661aSToomas Soome /*-
2*4a5d661aSToomas Soome  * Copyright (c) 1982, 1986, 1990, 1993
3*4a5d661aSToomas Soome  *	The Regents of the University of California.
4*4a5d661aSToomas Soome  * Copyright (c) 2010-2011 Juniper Networks, Inc.
5*4a5d661aSToomas Soome  * All rights reserved.
6*4a5d661aSToomas Soome  *
7*4a5d661aSToomas Soome  * Portions of this software were developed by Robert N. M. Watson under
8*4a5d661aSToomas Soome  * contract to Juniper Networks, Inc.
9*4a5d661aSToomas Soome  *
10*4a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
11*4a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
12*4a5d661aSToomas Soome  * are met:
13*4a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
14*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
15*4a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16*4a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17*4a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
18*4a5d661aSToomas Soome  * 4. Neither the name of the University nor the names of its contributors
19*4a5d661aSToomas Soome  *    may be used to endorse or promote products derived from this software
20*4a5d661aSToomas Soome  *    without specific prior written permission.
21*4a5d661aSToomas Soome  *
22*4a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*4a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*4a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*4a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*4a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*4a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*4a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*4a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*4a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*4a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*4a5d661aSToomas Soome  * SUCH DAMAGE.
33*4a5d661aSToomas Soome  *
34*4a5d661aSToomas Soome  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
35*4a5d661aSToomas Soome  * $FreeBSD$
36*4a5d661aSToomas Soome  */
37*4a5d661aSToomas Soome 
38*4a5d661aSToomas Soome #ifndef _NETINET_IN_PCB_H_
39*4a5d661aSToomas Soome #define _NETINET_IN_PCB_H_
40*4a5d661aSToomas Soome 
41*4a5d661aSToomas Soome #include <sys/queue.h>
42*4a5d661aSToomas Soome #include <sys/_lock.h>
43*4a5d661aSToomas Soome #include <sys/_mutex.h>
44*4a5d661aSToomas Soome #include <sys/_rwlock.h>
45*4a5d661aSToomas Soome 
46*4a5d661aSToomas Soome #ifdef _KERNEL
47*4a5d661aSToomas Soome #include <sys/lock.h>
48*4a5d661aSToomas Soome #include <sys/rwlock.h>
49*4a5d661aSToomas Soome #include <net/vnet.h>
50*4a5d661aSToomas Soome #include <vm/uma.h>
51*4a5d661aSToomas Soome #endif
52*4a5d661aSToomas Soome 
53*4a5d661aSToomas Soome #define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
54*4a5d661aSToomas Soome #define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
55*4a5d661aSToomas Soome struct inpcbpolicy;
56*4a5d661aSToomas Soome 
57*4a5d661aSToomas Soome /*
58*4a5d661aSToomas Soome  * struct inpcb is the common protocol control block structure used in most
59*4a5d661aSToomas Soome  * IP transport protocols.
60*4a5d661aSToomas Soome  *
61*4a5d661aSToomas Soome  * Pointers to local and foreign host table entries, local and foreign socket
62*4a5d661aSToomas Soome  * numbers, and pointers up (to a socket structure) and down (to a
63*4a5d661aSToomas Soome  * protocol-specific control block) are stored here.
64*4a5d661aSToomas Soome  */
65*4a5d661aSToomas Soome LIST_HEAD(inpcbhead, inpcb);
66*4a5d661aSToomas Soome LIST_HEAD(inpcbporthead, inpcbport);
67*4a5d661aSToomas Soome typedef	u_quad_t	inp_gen_t;
68*4a5d661aSToomas Soome 
69*4a5d661aSToomas Soome /*
70*4a5d661aSToomas Soome  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
71*4a5d661aSToomas Soome  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
72*4a5d661aSToomas Soome  * the following structure.
73*4a5d661aSToomas Soome  */
74*4a5d661aSToomas Soome struct in_addr_4in6 {
75*4a5d661aSToomas Soome 	u_int32_t	ia46_pad32[3];
76*4a5d661aSToomas Soome 	struct	in_addr	ia46_addr4;
77*4a5d661aSToomas Soome };
78*4a5d661aSToomas Soome 
79*4a5d661aSToomas Soome /*
80*4a5d661aSToomas Soome  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
81*4a5d661aSToomas Soome  * some extra padding to accomplish this.
82*4a5d661aSToomas Soome  * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport,
83*4a5d661aSToomas Soome  * lport, faddr to generate hash, so these fields shouldn't be moved.
84*4a5d661aSToomas Soome  */
85*4a5d661aSToomas Soome struct in_endpoints {
86*4a5d661aSToomas Soome 	u_int16_t	ie_fport;		/* foreign port */
87*4a5d661aSToomas Soome 	u_int16_t	ie_lport;		/* local port */
88*4a5d661aSToomas Soome 	/* protocol dependent part, local and foreign addr */
89*4a5d661aSToomas Soome 	union {
90*4a5d661aSToomas Soome 		/* foreign host table entry */
91*4a5d661aSToomas Soome 		struct	in_addr_4in6 ie46_foreign;
92*4a5d661aSToomas Soome 		struct	in6_addr ie6_foreign;
93*4a5d661aSToomas Soome 	} ie_dependfaddr;
94*4a5d661aSToomas Soome 	union {
95*4a5d661aSToomas Soome 		/* local host table entry */
96*4a5d661aSToomas Soome 		struct	in_addr_4in6 ie46_local;
97*4a5d661aSToomas Soome 		struct	in6_addr ie6_local;
98*4a5d661aSToomas Soome 	} ie_dependladdr;
99*4a5d661aSToomas Soome 	u_int32_t	ie6_zoneid;		/* scope zone id */
100*4a5d661aSToomas Soome };
101*4a5d661aSToomas Soome #define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
102*4a5d661aSToomas Soome #define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
103*4a5d661aSToomas Soome #define	ie6_faddr	ie_dependfaddr.ie6_foreign
104*4a5d661aSToomas Soome #define	ie6_laddr	ie_dependladdr.ie6_local
105*4a5d661aSToomas Soome 
106*4a5d661aSToomas Soome /*
107*4a5d661aSToomas Soome  * XXX The defines for inc_* are hacks and should be changed to direct
108*4a5d661aSToomas Soome  * references.
109*4a5d661aSToomas Soome  */
110*4a5d661aSToomas Soome struct in_conninfo {
111*4a5d661aSToomas Soome 	u_int8_t	inc_flags;
112*4a5d661aSToomas Soome 	u_int8_t	inc_len;
113*4a5d661aSToomas Soome 	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
114*4a5d661aSToomas Soome 	/* protocol dependent part */
115*4a5d661aSToomas Soome 	struct	in_endpoints inc_ie;
116*4a5d661aSToomas Soome };
117*4a5d661aSToomas Soome 
118*4a5d661aSToomas Soome /*
119*4a5d661aSToomas Soome  * Flags for inc_flags.
120*4a5d661aSToomas Soome  */
121*4a5d661aSToomas Soome #define	INC_ISIPV6	0x01
122*4a5d661aSToomas Soome 
123*4a5d661aSToomas Soome #define inc_isipv6	inc_flags	/* temp compatability */
124*4a5d661aSToomas Soome #define	inc_fport	inc_ie.ie_fport
125*4a5d661aSToomas Soome #define	inc_lport	inc_ie.ie_lport
126*4a5d661aSToomas Soome #define	inc_faddr	inc_ie.ie_faddr
127*4a5d661aSToomas Soome #define	inc_laddr	inc_ie.ie_laddr
128*4a5d661aSToomas Soome #define	inc6_faddr	inc_ie.ie6_faddr
129*4a5d661aSToomas Soome #define	inc6_laddr	inc_ie.ie6_laddr
130*4a5d661aSToomas Soome #define	inc6_zoneid	inc_ie.ie6_zoneid
131*4a5d661aSToomas Soome 
132*4a5d661aSToomas Soome struct	icmp6_filter;
133*4a5d661aSToomas Soome 
134*4a5d661aSToomas Soome /*-
135*4a5d661aSToomas Soome  * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and
136*4a5d661aSToomas Soome  * IPv6 sockets.  In the case of TCP and UDP, further per-connection state is
137*4a5d661aSToomas Soome  * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
138*4a5d661aSToomas Soome  * are static after creation or protected by a per-inpcb rwlock, inp_lock.  A
139*4a5d661aSToomas Soome  * few fields are protected by multiple locks as indicated in the locking notes
140*4a5d661aSToomas Soome  * below.  For these fields, all of the listed locks must be write-locked for
141*4a5d661aSToomas Soome  * any modifications.  However, these fields can be safely read while any one of
142*4a5d661aSToomas Soome  * the listed locks are read-locked.  This model can permit greater concurrency
143*4a5d661aSToomas Soome  * for read operations.  For example, connections can be looked up while only
144*4a5d661aSToomas Soome  * holding a read lock on the global pcblist lock.  This is important for
145*4a5d661aSToomas Soome  * performance when attempting to find the connection for a packet given its IP
146*4a5d661aSToomas Soome  * and port tuple.
147*4a5d661aSToomas Soome  *
148*4a5d661aSToomas Soome  * One noteworthy exception is that the global pcbinfo lock follows a different
149*4a5d661aSToomas Soome  * set of rules in relation to the inp_list field.  Rather than being
150*4a5d661aSToomas Soome  * write-locked for modifications and read-locked for list iterations, it must
151*4a5d661aSToomas Soome  * be read-locked during modifications and write-locked during list iterations.
152*4a5d661aSToomas Soome  * This ensures that the relatively rare global list iterations safely walk a
153*4a5d661aSToomas Soome  * stable snapshot of connections while allowing more common list modifications
154*4a5d661aSToomas Soome  * to safely grab the pcblist lock just while adding or removing a connection
155*4a5d661aSToomas Soome  * from the global list.
156*4a5d661aSToomas Soome  *
157*4a5d661aSToomas Soome  * Key:
158*4a5d661aSToomas Soome  * (c) - Constant after initialization
159*4a5d661aSToomas Soome  * (g) - Protected by the pcbgroup lock
160*4a5d661aSToomas Soome  * (i) - Protected by the inpcb lock
161*4a5d661aSToomas Soome  * (p) - Protected by the pcbinfo lock for the inpcb
162*4a5d661aSToomas Soome  * (l) - Protected by the pcblist lock for the inpcb
163*4a5d661aSToomas Soome  * (h) - Protected by the pcbhash lock for the inpcb
164*4a5d661aSToomas Soome  * (s) - Protected by another subsystem's locks
165*4a5d661aSToomas Soome  * (x) - Undefined locking
166*4a5d661aSToomas Soome  *
167*4a5d661aSToomas Soome  * A few other notes:
168*4a5d661aSToomas Soome  *
169*4a5d661aSToomas Soome  * When a read lock is held, stability of the field is guaranteed; to write
170*4a5d661aSToomas Soome  * to a field, a write lock must generally be held.
171*4a5d661aSToomas Soome  *
172*4a5d661aSToomas Soome  * netinet/netinet6-layer code should not assume that the inp_socket pointer
173*4a5d661aSToomas Soome  * is safe to dereference without inp_lock being held, even for protocols
174*4a5d661aSToomas Soome  * other than TCP (where the inpcb persists during TIMEWAIT even after the
175*4a5d661aSToomas Soome  * socket has been freed), or there may be close(2)-related races.
176*4a5d661aSToomas Soome  *
177*4a5d661aSToomas Soome  * The inp_vflag field is overloaded, and would otherwise ideally be (c).
178*4a5d661aSToomas Soome  *
179*4a5d661aSToomas Soome  * TODO:  Currently only the TCP stack is leveraging the global pcbinfo lock
180*4a5d661aSToomas Soome  * read-lock usage during modification, this model can be applied to other
181*4a5d661aSToomas Soome  * protocols (especially SCTP).
182*4a5d661aSToomas Soome  */
183*4a5d661aSToomas Soome struct inpcb {
184*4a5d661aSToomas Soome 	LIST_ENTRY(inpcb) inp_hash;	/* (h/i) hash list */
185*4a5d661aSToomas Soome 	LIST_ENTRY(inpcb) inp_pcbgrouphash;	/* (g/i) hash list */
186*4a5d661aSToomas Soome 	LIST_ENTRY(inpcb) inp_list;	/* (p/l) list for all PCBs for proto */
187*4a5d661aSToomas Soome 	                                /* (p[w]) for list iteration */
188*4a5d661aSToomas Soome 	                                /* (p[r]/l) for addition/removal */
189*4a5d661aSToomas Soome 	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
190*4a5d661aSToomas Soome 	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
191*4a5d661aSToomas Soome 	struct	inpcbgroup *inp_pcbgroup; /* (g/i) PCB group list */
192*4a5d661aSToomas Soome 	LIST_ENTRY(inpcb) inp_pcbgroup_wild; /* (g/i/h) group wildcard entry */
193*4a5d661aSToomas Soome 	struct	socket *inp_socket;	/* (i) back pointer to socket */
194*4a5d661aSToomas Soome 	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
195*4a5d661aSToomas Soome 	u_int32_t inp_flow;		/* (i) IPv6 flow information */
196*4a5d661aSToomas Soome 	int	inp_flags;		/* (i) generic IP/datagram flags */
197*4a5d661aSToomas Soome 	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
198*4a5d661aSToomas Soome 	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
199*4a5d661aSToomas Soome 	u_char	inp_ip_ttl;		/* (i) time to live proto */
200*4a5d661aSToomas Soome 	u_char	inp_ip_p;		/* (c) protocol proto */
201*4a5d661aSToomas Soome 	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
202*4a5d661aSToomas Soome 	uint32_t inp_flowid;		/* (x) flow id / queue id */
203*4a5d661aSToomas Soome 	u_int	inp_refcount;		/* (i) refcount */
204*4a5d661aSToomas Soome 	void	*inp_pspare[5];		/* (x) route caching / general use */
205*4a5d661aSToomas Soome 	uint32_t inp_flowtype;		/* (x) M_HASHTYPE value */
206*4a5d661aSToomas Soome 	uint32_t inp_rss_listen_bucket;	/* (x) overridden RSS listen bucket */
207*4a5d661aSToomas Soome 	u_int	inp_ispare[4];		/* (x) route caching / user cookie /
208*4a5d661aSToomas Soome 					 *     general use */
209*4a5d661aSToomas Soome 
210*4a5d661aSToomas Soome 	/* Local and foreign ports, local and foreign addr. */
211*4a5d661aSToomas Soome 	struct	in_conninfo inp_inc;	/* (i) list for PCB's local port */
212*4a5d661aSToomas Soome 
213*4a5d661aSToomas Soome 	/* MAC and IPSEC policy information. */
214*4a5d661aSToomas Soome 	struct	label *inp_label;	/* (i) MAC label */
215*4a5d661aSToomas Soome 	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
216*4a5d661aSToomas Soome 
217*4a5d661aSToomas Soome 	/* Protocol-dependent part; options. */
218*4a5d661aSToomas Soome 	struct {
219*4a5d661aSToomas Soome 		u_char	inp4_ip_tos;		/* (i) type of service proto */
220*4a5d661aSToomas Soome 		struct	mbuf *inp4_options;	/* (i) IP options */
221*4a5d661aSToomas Soome 		struct	ip_moptions *inp4_moptions; /* (i) IP mcast options */
222*4a5d661aSToomas Soome 	} inp_depend4;
223*4a5d661aSToomas Soome 	struct {
224*4a5d661aSToomas Soome 		/* (i) IP options */
225*4a5d661aSToomas Soome 		struct	mbuf *inp6_options;
226*4a5d661aSToomas Soome 		/* (i) IP6 options for outgoing packets */
227*4a5d661aSToomas Soome 		struct	ip6_pktopts *inp6_outputopts;
228*4a5d661aSToomas Soome 		/* (i) IP multicast options */
229*4a5d661aSToomas Soome 		struct	ip6_moptions *inp6_moptions;
230*4a5d661aSToomas Soome 		/* (i) ICMPv6 code type filter */
231*4a5d661aSToomas Soome 		struct	icmp6_filter *inp6_icmp6filt;
232*4a5d661aSToomas Soome 		/* (i) IPV6_CHECKSUM setsockopt */
233*4a5d661aSToomas Soome 		int	inp6_cksum;
234*4a5d661aSToomas Soome 		short	inp6_hops;
235*4a5d661aSToomas Soome 	} inp_depend6;
236*4a5d661aSToomas Soome 	LIST_ENTRY(inpcb) inp_portlist;	/* (i/h) */
237*4a5d661aSToomas Soome 	struct	inpcbport *inp_phd;	/* (i/h) head of this list */
238*4a5d661aSToomas Soome #define inp_zero_size offsetof(struct inpcb, inp_gencnt)
239*4a5d661aSToomas Soome 	inp_gen_t	inp_gencnt;	/* (c) generation count */
240*4a5d661aSToomas Soome 	struct llentry	*inp_lle;	/* cached L2 information */
241*4a5d661aSToomas Soome 	struct rtentry	*inp_rt;	/* cached L3 information */
242*4a5d661aSToomas Soome 	struct rwlock	inp_lock;
243*4a5d661aSToomas Soome };
244*4a5d661aSToomas Soome #define	inp_fport	inp_inc.inc_fport
245*4a5d661aSToomas Soome #define	inp_lport	inp_inc.inc_lport
246*4a5d661aSToomas Soome #define	inp_faddr	inp_inc.inc_faddr
247*4a5d661aSToomas Soome #define	inp_laddr	inp_inc.inc_laddr
248*4a5d661aSToomas Soome #define	inp_ip_tos	inp_depend4.inp4_ip_tos
249*4a5d661aSToomas Soome #define	inp_options	inp_depend4.inp4_options
250*4a5d661aSToomas Soome #define	inp_moptions	inp_depend4.inp4_moptions
251*4a5d661aSToomas Soome 
252*4a5d661aSToomas Soome #define	in6p_faddr	inp_inc.inc6_faddr
253*4a5d661aSToomas Soome #define	in6p_laddr	inp_inc.inc6_laddr
254*4a5d661aSToomas Soome #define	in6p_zoneid	inp_inc.inc6_zoneid
255*4a5d661aSToomas Soome #define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
256*4a5d661aSToomas Soome #define	in6p_flowinfo	inp_flow
257*4a5d661aSToomas Soome #define	in6p_options	inp_depend6.inp6_options
258*4a5d661aSToomas Soome #define	in6p_outputopts	inp_depend6.inp6_outputopts
259*4a5d661aSToomas Soome #define	in6p_moptions	inp_depend6.inp6_moptions
260*4a5d661aSToomas Soome #define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
261*4a5d661aSToomas Soome #define	in6p_cksum	inp_depend6.inp6_cksum
262*4a5d661aSToomas Soome 
263*4a5d661aSToomas Soome #define	inp_vnet	inp_pcbinfo->ipi_vnet
264*4a5d661aSToomas Soome 
265*4a5d661aSToomas Soome /*
266*4a5d661aSToomas Soome  * The range of the generation count, as used in this implementation, is 9e19.
267*4a5d661aSToomas Soome  * We would have to create 300 billion connections per second for this number
268*4a5d661aSToomas Soome  * to roll over in a year.  This seems sufficiently unlikely that we simply
269*4a5d661aSToomas Soome  * don't concern ourselves with that possibility.
270*4a5d661aSToomas Soome  */
271*4a5d661aSToomas Soome 
272*4a5d661aSToomas Soome /*
273*4a5d661aSToomas Soome  * Interface exported to userland by various protocols which use inpcbs.  Hack
274*4a5d661aSToomas Soome  * alert -- only define if struct xsocket is in scope.
275*4a5d661aSToomas Soome  */
276*4a5d661aSToomas Soome #ifdef _SYS_SOCKETVAR_H_
277*4a5d661aSToomas Soome struct	xinpcb {
278*4a5d661aSToomas Soome 	size_t	xi_len;		/* length of this structure */
279*4a5d661aSToomas Soome 	struct	inpcb xi_inp;
280*4a5d661aSToomas Soome 	struct	xsocket xi_socket;
281*4a5d661aSToomas Soome 	u_quad_t	xi_alignment_hack;
282*4a5d661aSToomas Soome };
283*4a5d661aSToomas Soome 
284*4a5d661aSToomas Soome struct	xinpgen {
285*4a5d661aSToomas Soome 	size_t	xig_len;	/* length of this structure */
286*4a5d661aSToomas Soome 	u_int	xig_count;	/* number of PCBs at this time */
287*4a5d661aSToomas Soome 	inp_gen_t xig_gen;	/* generation count at this time */
288*4a5d661aSToomas Soome 	so_gen_t xig_sogen;	/* socket generation count at this time */
289*4a5d661aSToomas Soome };
290*4a5d661aSToomas Soome #endif /* _SYS_SOCKETVAR_H_ */
291*4a5d661aSToomas Soome 
292*4a5d661aSToomas Soome struct inpcbport {
293*4a5d661aSToomas Soome 	LIST_ENTRY(inpcbport) phd_hash;
294*4a5d661aSToomas Soome 	struct inpcbhead phd_pcblist;
295*4a5d661aSToomas Soome 	u_short phd_port;
296*4a5d661aSToomas Soome };
297*4a5d661aSToomas Soome 
298*4a5d661aSToomas Soome /*-
299*4a5d661aSToomas Soome  * Global data structure for each high-level protocol (UDP, TCP, ...) in both
300*4a5d661aSToomas Soome  * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
301*4a5d661aSToomas Soome  *
302*4a5d661aSToomas Soome  * Each pcbinfo is protected by three locks: ipi_lock, ipi_hash_lock and
303*4a5d661aSToomas Soome  * ipi_list_lock:
304*4a5d661aSToomas Soome  *  - ipi_lock covering the global pcb list stability during loop iteration,
305*4a5d661aSToomas Soome  *  - ipi_hash_lock covering the hashed lookup tables,
306*4a5d661aSToomas Soome  *  - ipi_list_lock covering mutable global fields (such as the global
307*4a5d661aSToomas Soome  *    pcb list)
308*4a5d661aSToomas Soome  *
309*4a5d661aSToomas Soome  * The lock order is:
310*4a5d661aSToomas Soome  *
311*4a5d661aSToomas Soome  *    ipi_lock (before)
312*4a5d661aSToomas Soome  *        inpcb locks (before)
313*4a5d661aSToomas Soome  *            ipi_list locks (before)
314*4a5d661aSToomas Soome  *                {ipi_hash_lock, pcbgroup locks}
315*4a5d661aSToomas Soome  *
316*4a5d661aSToomas Soome  * Locking key:
317*4a5d661aSToomas Soome  *
318*4a5d661aSToomas Soome  * (c) Constant or nearly constant after initialisation
319*4a5d661aSToomas Soome  * (g) Locked by ipi_lock
320*4a5d661aSToomas Soome  * (l) Locked by ipi_list_lock
321*4a5d661aSToomas Soome  * (h) Read using either ipi_hash_lock or inpcb lock; write requires both
322*4a5d661aSToomas Soome  * (p) Protected by one or more pcbgroup locks
323*4a5d661aSToomas Soome  * (x) Synchronisation properties poorly defined
324*4a5d661aSToomas Soome  */
325*4a5d661aSToomas Soome struct inpcbinfo {
326*4a5d661aSToomas Soome 	/*
327*4a5d661aSToomas Soome 	 * Global lock protecting full inpcb list traversal
328*4a5d661aSToomas Soome 	 */
329*4a5d661aSToomas Soome 	struct rwlock		 ipi_lock;
330*4a5d661aSToomas Soome 
331*4a5d661aSToomas Soome 	/*
332*4a5d661aSToomas Soome 	 * Global list of inpcbs on the protocol.
333*4a5d661aSToomas Soome 	 */
334*4a5d661aSToomas Soome 	struct inpcbhead	*ipi_listhead;		/* (g/l) */
335*4a5d661aSToomas Soome 	u_int			 ipi_count;		/* (l) */
336*4a5d661aSToomas Soome 
337*4a5d661aSToomas Soome 	/*
338*4a5d661aSToomas Soome 	 * Generation count -- incremented each time a connection is allocated
339*4a5d661aSToomas Soome 	 * or freed.
340*4a5d661aSToomas Soome 	 */
341*4a5d661aSToomas Soome 	u_quad_t		 ipi_gencnt;		/* (l) */
342*4a5d661aSToomas Soome 
343*4a5d661aSToomas Soome 	/*
344*4a5d661aSToomas Soome 	 * Fields associated with port lookup and allocation.
345*4a5d661aSToomas Soome 	 */
346*4a5d661aSToomas Soome 	u_short			 ipi_lastport;		/* (x) */
347*4a5d661aSToomas Soome 	u_short			 ipi_lastlow;		/* (x) */
348*4a5d661aSToomas Soome 	u_short			 ipi_lasthi;		/* (x) */
349*4a5d661aSToomas Soome 
350*4a5d661aSToomas Soome 	/*
351*4a5d661aSToomas Soome 	 * UMA zone from which inpcbs are allocated for this protocol.
352*4a5d661aSToomas Soome 	 */
353*4a5d661aSToomas Soome 	struct	uma_zone	*ipi_zone;		/* (c) */
354*4a5d661aSToomas Soome 
355*4a5d661aSToomas Soome 	/*
356*4a5d661aSToomas Soome 	 * Connection groups associated with this protocol.  These fields are
357*4a5d661aSToomas Soome 	 * constant, but pcbgroup structures themselves are protected by
358*4a5d661aSToomas Soome 	 * per-pcbgroup locks.
359*4a5d661aSToomas Soome 	 */
360*4a5d661aSToomas Soome 	struct inpcbgroup	*ipi_pcbgroups;		/* (c) */
361*4a5d661aSToomas Soome 	u_int			 ipi_npcbgroups;	/* (c) */
362*4a5d661aSToomas Soome 	u_int			 ipi_hashfields;	/* (c) */
363*4a5d661aSToomas Soome 
364*4a5d661aSToomas Soome 	/*
365*4a5d661aSToomas Soome 	 * Global lock protecting non-pcbgroup hash lookup tables.
366*4a5d661aSToomas Soome 	 */
367*4a5d661aSToomas Soome 	struct rwlock		 ipi_hash_lock;
368*4a5d661aSToomas Soome 
369*4a5d661aSToomas Soome 	/*
370*4a5d661aSToomas Soome 	 * Global hash of inpcbs, hashed by local and foreign addresses and
371*4a5d661aSToomas Soome 	 * port numbers.
372*4a5d661aSToomas Soome 	 */
373*4a5d661aSToomas Soome 	struct inpcbhead	*ipi_hashbase;		/* (h) */
374*4a5d661aSToomas Soome 	u_long			 ipi_hashmask;		/* (h) */
375*4a5d661aSToomas Soome 
376*4a5d661aSToomas Soome 	/*
377*4a5d661aSToomas Soome 	 * Global hash of inpcbs, hashed by only local port number.
378*4a5d661aSToomas Soome 	 */
379*4a5d661aSToomas Soome 	struct inpcbporthead	*ipi_porthashbase;	/* (h) */
380*4a5d661aSToomas Soome 	u_long			 ipi_porthashmask;	/* (h) */
381*4a5d661aSToomas Soome 
382*4a5d661aSToomas Soome 	/*
383*4a5d661aSToomas Soome 	 * List of wildcard inpcbs for use with pcbgroups.  In the past, was
384*4a5d661aSToomas Soome 	 * per-pcbgroup but is now global.  All pcbgroup locks must be held
385*4a5d661aSToomas Soome 	 * to modify the list, so any is sufficient to read it.
386*4a5d661aSToomas Soome 	 */
387*4a5d661aSToomas Soome 	struct inpcbhead	*ipi_wildbase;		/* (p) */
388*4a5d661aSToomas Soome 	u_long			 ipi_wildmask;		/* (p) */
389*4a5d661aSToomas Soome 
390*4a5d661aSToomas Soome 	/*
391*4a5d661aSToomas Soome 	 * Pointer to network stack instance
392*4a5d661aSToomas Soome 	 */
393*4a5d661aSToomas Soome 	struct vnet		*ipi_vnet;		/* (c) */
394*4a5d661aSToomas Soome 
395*4a5d661aSToomas Soome 	/*
396*4a5d661aSToomas Soome 	 * general use 2
397*4a5d661aSToomas Soome 	 */
398*4a5d661aSToomas Soome 	void 			*ipi_pspare[2];
399*4a5d661aSToomas Soome 
400*4a5d661aSToomas Soome 	/*
401*4a5d661aSToomas Soome 	 * Global lock protecting global inpcb list, inpcb count, etc.
402*4a5d661aSToomas Soome 	 */
403*4a5d661aSToomas Soome 	struct rwlock		 ipi_list_lock;
404*4a5d661aSToomas Soome };
405*4a5d661aSToomas Soome 
406*4a5d661aSToomas Soome #ifdef _KERNEL
407*4a5d661aSToomas Soome /*
408*4a5d661aSToomas Soome  * Connection groups hold sets of connections that have similar CPU/thread
409*4a5d661aSToomas Soome  * affinity.  Each connection belongs to exactly one connection group.
410*4a5d661aSToomas Soome  */
411*4a5d661aSToomas Soome struct inpcbgroup {
412*4a5d661aSToomas Soome 	/*
413*4a5d661aSToomas Soome 	 * Per-connection group hash of inpcbs, hashed by local and foreign
414*4a5d661aSToomas Soome 	 * addresses and port numbers.
415*4a5d661aSToomas Soome 	 */
416*4a5d661aSToomas Soome 	struct inpcbhead	*ipg_hashbase;		/* (c) */
417*4a5d661aSToomas Soome 	u_long			 ipg_hashmask;		/* (c) */
418*4a5d661aSToomas Soome 
419*4a5d661aSToomas Soome 	/*
420*4a5d661aSToomas Soome 	 * Notional affinity of this pcbgroup.
421*4a5d661aSToomas Soome 	 */
422*4a5d661aSToomas Soome 	u_int			 ipg_cpu;		/* (p) */
423*4a5d661aSToomas Soome 
424*4a5d661aSToomas Soome 	/*
425*4a5d661aSToomas Soome 	 * Per-connection group lock, not to be confused with ipi_lock.
426*4a5d661aSToomas Soome 	 * Protects the hash table hung off the group, but also the global
427*4a5d661aSToomas Soome 	 * wildcard list in inpcbinfo.
428*4a5d661aSToomas Soome 	 */
429*4a5d661aSToomas Soome 	struct mtx		 ipg_lock;
430*4a5d661aSToomas Soome } __aligned(CACHE_LINE_SIZE);
431*4a5d661aSToomas Soome 
432*4a5d661aSToomas Soome #define INP_LOCK_INIT(inp, d, t) \
433*4a5d661aSToomas Soome 	rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE |  RW_DUPOK)
434*4a5d661aSToomas Soome #define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
435*4a5d661aSToomas Soome #define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
436*4a5d661aSToomas Soome #define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
437*4a5d661aSToomas Soome #define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
438*4a5d661aSToomas Soome #define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
439*4a5d661aSToomas Soome #define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
440*4a5d661aSToomas Soome #define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
441*4a5d661aSToomas Soome #define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
442*4a5d661aSToomas Soome #define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
443*4a5d661aSToomas Soome #define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
444*4a5d661aSToomas Soome #define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
445*4a5d661aSToomas Soome #define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
446*4a5d661aSToomas Soome #define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
447*4a5d661aSToomas Soome #define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
448*4a5d661aSToomas Soome 
449*4a5d661aSToomas Soome /*
450*4a5d661aSToomas Soome  * These locking functions are for inpcb consumers outside of sys/netinet,
451*4a5d661aSToomas Soome  * more specifically, they were added for the benefit of TOE drivers. The
452*4a5d661aSToomas Soome  * macros are reserved for use by the stack.
453*4a5d661aSToomas Soome  */
454*4a5d661aSToomas Soome void inp_wlock(struct inpcb *);
455*4a5d661aSToomas Soome void inp_wunlock(struct inpcb *);
456*4a5d661aSToomas Soome void inp_rlock(struct inpcb *);
457*4a5d661aSToomas Soome void inp_runlock(struct inpcb *);
458*4a5d661aSToomas Soome 
459*4a5d661aSToomas Soome #ifdef INVARIANTS
460*4a5d661aSToomas Soome void inp_lock_assert(struct inpcb *);
461*4a5d661aSToomas Soome void inp_unlock_assert(struct inpcb *);
462*4a5d661aSToomas Soome #else
463*4a5d661aSToomas Soome static __inline void
464*4a5d661aSToomas Soome inp_lock_assert(struct inpcb *inp __unused)
465*4a5d661aSToomas Soome {
466*4a5d661aSToomas Soome }
467*4a5d661aSToomas Soome 
468*4a5d661aSToomas Soome static __inline void
469*4a5d661aSToomas Soome inp_unlock_assert(struct inpcb *inp __unused)
470*4a5d661aSToomas Soome {
471*4a5d661aSToomas Soome }
472*4a5d661aSToomas Soome 
473*4a5d661aSToomas Soome #endif
474*4a5d661aSToomas Soome 
475*4a5d661aSToomas Soome void	inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
476*4a5d661aSToomas Soome int 	inp_ip_tos_get(const struct inpcb *inp);
477*4a5d661aSToomas Soome void 	inp_ip_tos_set(struct inpcb *inp, int val);
478*4a5d661aSToomas Soome struct socket *
479*4a5d661aSToomas Soome 	inp_inpcbtosocket(struct inpcb *inp);
480*4a5d661aSToomas Soome struct tcpcb *
481*4a5d661aSToomas Soome 	inp_inpcbtotcpcb(struct inpcb *inp);
482*4a5d661aSToomas Soome void 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
483*4a5d661aSToomas Soome 		uint32_t *faddr, uint16_t *fp);
484*4a5d661aSToomas Soome short	inp_so_options(const struct inpcb *inp);
485*4a5d661aSToomas Soome 
486*4a5d661aSToomas Soome #endif /* _KERNEL */
487*4a5d661aSToomas Soome 
488*4a5d661aSToomas Soome #define INP_INFO_LOCK_INIT(ipi, d) \
489*4a5d661aSToomas Soome 	rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE)
490*4a5d661aSToomas Soome #define INP_INFO_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_lock)
491*4a5d661aSToomas Soome #define INP_INFO_RLOCK(ipi)	rw_rlock(&(ipi)->ipi_lock)
492*4a5d661aSToomas Soome #define INP_INFO_WLOCK(ipi)	rw_wlock(&(ipi)->ipi_lock)
493*4a5d661aSToomas Soome #define INP_INFO_TRY_RLOCK(ipi)	rw_try_rlock(&(ipi)->ipi_lock)
494*4a5d661aSToomas Soome #define INP_INFO_TRY_WLOCK(ipi)	rw_try_wlock(&(ipi)->ipi_lock)
495*4a5d661aSToomas Soome #define INP_INFO_TRY_UPGRADE(ipi)	rw_try_upgrade(&(ipi)->ipi_lock)
496*4a5d661aSToomas Soome #define INP_INFO_WLOCKED(ipi)	rw_wowned(&(ipi)->ipi_lock)
497*4a5d661aSToomas Soome #define INP_INFO_RUNLOCK(ipi)	rw_runlock(&(ipi)->ipi_lock)
498*4a5d661aSToomas Soome #define INP_INFO_WUNLOCK(ipi)	rw_wunlock(&(ipi)->ipi_lock)
499*4a5d661aSToomas Soome #define	INP_INFO_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_LOCKED)
500*4a5d661aSToomas Soome #define INP_INFO_RLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_RLOCKED)
501*4a5d661aSToomas Soome #define INP_INFO_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_WLOCKED)
502*4a5d661aSToomas Soome #define INP_INFO_UNLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED)
503*4a5d661aSToomas Soome 
504*4a5d661aSToomas Soome #define INP_LIST_LOCK_INIT(ipi, d) \
505*4a5d661aSToomas Soome         rw_init_flags(&(ipi)->ipi_list_lock, (d), 0)
506*4a5d661aSToomas Soome #define INP_LIST_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_list_lock)
507*4a5d661aSToomas Soome #define INP_LIST_RLOCK(ipi)     rw_rlock(&(ipi)->ipi_list_lock)
508*4a5d661aSToomas Soome #define INP_LIST_WLOCK(ipi)     rw_wlock(&(ipi)->ipi_list_lock)
509*4a5d661aSToomas Soome #define INP_LIST_TRY_RLOCK(ipi) rw_try_rlock(&(ipi)->ipi_list_lock)
510*4a5d661aSToomas Soome #define INP_LIST_TRY_WLOCK(ipi) rw_try_wlock(&(ipi)->ipi_list_lock)
511*4a5d661aSToomas Soome #define INP_LIST_TRY_UPGRADE(ipi)       rw_try_upgrade(&(ipi)->ipi_list_lock)
512*4a5d661aSToomas Soome #define INP_LIST_RUNLOCK(ipi)   rw_runlock(&(ipi)->ipi_list_lock)
513*4a5d661aSToomas Soome #define INP_LIST_WUNLOCK(ipi)   rw_wunlock(&(ipi)->ipi_list_lock)
514*4a5d661aSToomas Soome #define INP_LIST_LOCK_ASSERT(ipi) \
515*4a5d661aSToomas Soome 	rw_assert(&(ipi)->ipi_list_lock, RA_LOCKED)
516*4a5d661aSToomas Soome #define INP_LIST_RLOCK_ASSERT(ipi) \
517*4a5d661aSToomas Soome 	rw_assert(&(ipi)->ipi_list_lock, RA_RLOCKED)
518*4a5d661aSToomas Soome #define INP_LIST_WLOCK_ASSERT(ipi) \
519*4a5d661aSToomas Soome 	rw_assert(&(ipi)->ipi_list_lock, RA_WLOCKED)
520*4a5d661aSToomas Soome #define INP_LIST_UNLOCK_ASSERT(ipi) \
521*4a5d661aSToomas Soome 	rw_assert(&(ipi)->ipi_list_lock, RA_UNLOCKED)
522*4a5d661aSToomas Soome 
523*4a5d661aSToomas Soome #define	INP_HASH_LOCK_INIT(ipi, d) \
524*4a5d661aSToomas Soome 	rw_init_flags(&(ipi)->ipi_hash_lock, (d), 0)
525*4a5d661aSToomas Soome #define	INP_HASH_LOCK_DESTROY(ipi)	rw_destroy(&(ipi)->ipi_hash_lock)
526*4a5d661aSToomas Soome #define	INP_HASH_RLOCK(ipi)		rw_rlock(&(ipi)->ipi_hash_lock)
527*4a5d661aSToomas Soome #define	INP_HASH_WLOCK(ipi)		rw_wlock(&(ipi)->ipi_hash_lock)
528*4a5d661aSToomas Soome #define	INP_HASH_RUNLOCK(ipi)		rw_runlock(&(ipi)->ipi_hash_lock)
529*4a5d661aSToomas Soome #define	INP_HASH_WUNLOCK(ipi)		rw_wunlock(&(ipi)->ipi_hash_lock)
530*4a5d661aSToomas Soome #define	INP_HASH_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_hash_lock, \
531*4a5d661aSToomas Soome 					    RA_LOCKED)
532*4a5d661aSToomas Soome #define	INP_HASH_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_hash_lock, \
533*4a5d661aSToomas Soome 					    RA_WLOCKED)
534*4a5d661aSToomas Soome 
535*4a5d661aSToomas Soome #define	INP_GROUP_LOCK_INIT(ipg, d)	mtx_init(&(ipg)->ipg_lock, (d), NULL, \
536*4a5d661aSToomas Soome 					    MTX_DEF | MTX_DUPOK)
537*4a5d661aSToomas Soome #define	INP_GROUP_LOCK_DESTROY(ipg)	mtx_destroy(&(ipg)->ipg_lock)
538*4a5d661aSToomas Soome 
539*4a5d661aSToomas Soome #define	INP_GROUP_LOCK(ipg)		mtx_lock(&(ipg)->ipg_lock)
540*4a5d661aSToomas Soome #define	INP_GROUP_LOCK_ASSERT(ipg)	mtx_assert(&(ipg)->ipg_lock, MA_OWNED)
541*4a5d661aSToomas Soome #define	INP_GROUP_UNLOCK(ipg)		mtx_unlock(&(ipg)->ipg_lock)
542*4a5d661aSToomas Soome 
543*4a5d661aSToomas Soome #define INP_PCBHASH(faddr, lport, fport, mask) \
544*4a5d661aSToomas Soome 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
545*4a5d661aSToomas Soome #define INP_PCBPORTHASH(lport, mask) \
546*4a5d661aSToomas Soome 	(ntohs((lport)) & (mask))
547*4a5d661aSToomas Soome #define	INP6_PCBHASHKEY(faddr)	((faddr)->s6_addr32[3])
548*4a5d661aSToomas Soome 
549*4a5d661aSToomas Soome /*
550*4a5d661aSToomas Soome  * Flags for inp_vflags -- historically version flags only
551*4a5d661aSToomas Soome  */
552*4a5d661aSToomas Soome #define	INP_IPV4	0x1
553*4a5d661aSToomas Soome #define	INP_IPV6	0x2
554*4a5d661aSToomas Soome #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
555*4a5d661aSToomas Soome 
556*4a5d661aSToomas Soome /*
557*4a5d661aSToomas Soome  * Flags for inp_flags.
558*4a5d661aSToomas Soome  */
559*4a5d661aSToomas Soome #define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
560*4a5d661aSToomas Soome #define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
561*4a5d661aSToomas Soome #define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
562*4a5d661aSToomas Soome #define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
563*4a5d661aSToomas Soome #define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
564*4a5d661aSToomas Soome #define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
565*4a5d661aSToomas Soome #define	INP_ANONPORT		0x00000040 /* port chosen for user */
566*4a5d661aSToomas Soome #define	INP_RECVIF		0x00000080 /* receive incoming interface */
567*4a5d661aSToomas Soome #define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
568*4a5d661aSToomas Soome 				   	   /* 0x000200 unused: was INP_FAITH */
569*4a5d661aSToomas Soome #define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
570*4a5d661aSToomas Soome #define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
571*4a5d661aSToomas Soome #define	INP_BINDANY		0x00001000 /* allow bind to any address */
572*4a5d661aSToomas Soome #define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
573*4a5d661aSToomas Soome #define	INP_RECVTOS		0x00004000 /* receive incoming IP TOS */
574*4a5d661aSToomas Soome #define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
575*4a5d661aSToomas Soome #define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
576*4a5d661aSToomas Soome #define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
577*4a5d661aSToomas Soome #define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
578*4a5d661aSToomas Soome #define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
579*4a5d661aSToomas Soome #define	IN6P_RTHDR		0x00100000 /* receive routing header */
580*4a5d661aSToomas Soome #define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
581*4a5d661aSToomas Soome #define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
582*4a5d661aSToomas Soome #define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
583*4a5d661aSToomas Soome #define	INP_TIMEWAIT		0x01000000 /* in TIMEWAIT, ppcb is tcptw */
584*4a5d661aSToomas Soome #define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
585*4a5d661aSToomas Soome #define	INP_DROPPED		0x04000000 /* protocol drop flag */
586*4a5d661aSToomas Soome #define	INP_SOCKREF		0x08000000 /* strong socket reference */
587*4a5d661aSToomas Soome #define	INP_RESERVED_0          0x10000000 /* reserved field */
588*4a5d661aSToomas Soome #define	INP_RESERVED_1          0x20000000 /* reserved field */
589*4a5d661aSToomas Soome #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
590*4a5d661aSToomas Soome #define	IN6P_MTU		0x80000000 /* receive path MTU */
591*4a5d661aSToomas Soome 
592*4a5d661aSToomas Soome #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
593*4a5d661aSToomas Soome 				 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
594*4a5d661aSToomas Soome 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
595*4a5d661aSToomas Soome 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
596*4a5d661aSToomas Soome 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
597*4a5d661aSToomas Soome 				 IN6P_MTU)
598*4a5d661aSToomas Soome 
599*4a5d661aSToomas Soome /*
600*4a5d661aSToomas Soome  * Flags for inp_flags2.
601*4a5d661aSToomas Soome  */
602*4a5d661aSToomas Soome #define	INP_LLE_VALID		0x00000001 /* cached lle is valid */
603*4a5d661aSToomas Soome #define	INP_RT_VALID		0x00000002 /* cached rtentry is valid */
604*4a5d661aSToomas Soome #define	INP_PCBGROUPWILD	0x00000004 /* in pcbgroup wildcard list */
605*4a5d661aSToomas Soome #define	INP_REUSEPORT		0x00000008 /* SO_REUSEPORT option is set */
606*4a5d661aSToomas Soome #define	INP_FREED		0x00000010 /* inp itself is not valid */
607*4a5d661aSToomas Soome #define	INP_REUSEADDR		0x00000020 /* SO_REUSEADDR option is set */
608*4a5d661aSToomas Soome #define	INP_BINDMULTI		0x00000040 /* IP_BINDMULTI option is set */
609*4a5d661aSToomas Soome #define	INP_RSS_BUCKET_SET	0x00000080 /* IP_RSS_LISTEN_BUCKET is set */
610*4a5d661aSToomas Soome #define	INP_RECVFLOWID		0x00000100 /* populate recv datagram with flow info */
611*4a5d661aSToomas Soome #define	INP_RECVRSSBUCKETID	0x00000200 /* populate recv datagram with bucket id */
612*4a5d661aSToomas Soome 
613*4a5d661aSToomas Soome /*
614*4a5d661aSToomas Soome  * Flags passed to in_pcblookup*() functions.
615*4a5d661aSToomas Soome  */
616*4a5d661aSToomas Soome #define	INPLOOKUP_WILDCARD	0x00000001	/* Allow wildcard sockets. */
617*4a5d661aSToomas Soome #define	INPLOOKUP_RLOCKPCB	0x00000002	/* Return inpcb read-locked. */
618*4a5d661aSToomas Soome #define	INPLOOKUP_WLOCKPCB	0x00000004	/* Return inpcb write-locked. */
619*4a5d661aSToomas Soome 
620*4a5d661aSToomas Soome #define	INPLOOKUP_MASK	(INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
621*4a5d661aSToomas Soome 			    INPLOOKUP_WLOCKPCB)
622*4a5d661aSToomas Soome 
623*4a5d661aSToomas Soome #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
624*4a5d661aSToomas Soome #define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
625*4a5d661aSToomas Soome 
626*4a5d661aSToomas Soome #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
627*4a5d661aSToomas Soome 
628*4a5d661aSToomas Soome #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
629*4a5d661aSToomas Soome 
630*4a5d661aSToomas Soome /*
631*4a5d661aSToomas Soome  * Constants for pcbinfo.ipi_hashfields.
632*4a5d661aSToomas Soome  */
633*4a5d661aSToomas Soome #define	IPI_HASHFIELDS_NONE	0
634*4a5d661aSToomas Soome #define	IPI_HASHFIELDS_2TUPLE	1
635*4a5d661aSToomas Soome #define	IPI_HASHFIELDS_4TUPLE	2
636*4a5d661aSToomas Soome 
637*4a5d661aSToomas Soome #ifdef _KERNEL
638*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_reservedhigh);
639*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_reservedlow);
640*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_lowfirstauto);
641*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_lowlastauto);
642*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_firstauto);
643*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_lastauto);
644*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_hifirstauto);
645*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_hilastauto);
646*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_randomized);
647*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_randomcps);
648*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_randomtime);
649*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_stoprandom);
650*4a5d661aSToomas Soome VNET_DECLARE(int, ipport_tcpallocs);
651*4a5d661aSToomas Soome 
652*4a5d661aSToomas Soome #define	V_ipport_reservedhigh	VNET(ipport_reservedhigh)
653*4a5d661aSToomas Soome #define	V_ipport_reservedlow	VNET(ipport_reservedlow)
654*4a5d661aSToomas Soome #define	V_ipport_lowfirstauto	VNET(ipport_lowfirstauto)
655*4a5d661aSToomas Soome #define	V_ipport_lowlastauto	VNET(ipport_lowlastauto)
656*4a5d661aSToomas Soome #define	V_ipport_firstauto	VNET(ipport_firstauto)
657*4a5d661aSToomas Soome #define	V_ipport_lastauto	VNET(ipport_lastauto)
658*4a5d661aSToomas Soome #define	V_ipport_hifirstauto	VNET(ipport_hifirstauto)
659*4a5d661aSToomas Soome #define	V_ipport_hilastauto	VNET(ipport_hilastauto)
660*4a5d661aSToomas Soome #define	V_ipport_randomized	VNET(ipport_randomized)
661*4a5d661aSToomas Soome #define	V_ipport_randomcps	VNET(ipport_randomcps)
662*4a5d661aSToomas Soome #define	V_ipport_randomtime	VNET(ipport_randomtime)
663*4a5d661aSToomas Soome #define	V_ipport_stoprandom	VNET(ipport_stoprandom)
664*4a5d661aSToomas Soome #define	V_ipport_tcpallocs	VNET(ipport_tcpallocs)
665*4a5d661aSToomas Soome 
666*4a5d661aSToomas Soome void	in_pcbinfo_destroy(struct inpcbinfo *);
667*4a5d661aSToomas Soome void	in_pcbinfo_init(struct inpcbinfo *, const char *, struct inpcbhead *,
668*4a5d661aSToomas Soome 	    int, int, char *, uma_init, uma_fini, uint32_t, u_int);
669*4a5d661aSToomas Soome 
670*4a5d661aSToomas Soome int	in_pcbbind_check_bindmulti(const struct inpcb *ni,
671*4a5d661aSToomas Soome 	    const struct inpcb *oi);
672*4a5d661aSToomas Soome 
673*4a5d661aSToomas Soome struct inpcbgroup *
674*4a5d661aSToomas Soome 	in_pcbgroup_byhash(struct inpcbinfo *, u_int, uint32_t);
675*4a5d661aSToomas Soome struct inpcbgroup *
676*4a5d661aSToomas Soome 	in_pcbgroup_byinpcb(struct inpcb *);
677*4a5d661aSToomas Soome struct inpcbgroup *
678*4a5d661aSToomas Soome 	in_pcbgroup_bytuple(struct inpcbinfo *, struct in_addr, u_short,
679*4a5d661aSToomas Soome 	    struct in_addr, u_short);
680*4a5d661aSToomas Soome void	in_pcbgroup_destroy(struct inpcbinfo *);
681*4a5d661aSToomas Soome int	in_pcbgroup_enabled(struct inpcbinfo *);
682*4a5d661aSToomas Soome void	in_pcbgroup_init(struct inpcbinfo *, u_int, int);
683*4a5d661aSToomas Soome void	in_pcbgroup_remove(struct inpcb *);
684*4a5d661aSToomas Soome void	in_pcbgroup_update(struct inpcb *);
685*4a5d661aSToomas Soome void	in_pcbgroup_update_mbuf(struct inpcb *, struct mbuf *);
686*4a5d661aSToomas Soome 
687*4a5d661aSToomas Soome void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
688*4a5d661aSToomas Soome int	in_pcballoc(struct socket *, struct inpcbinfo *);
689*4a5d661aSToomas Soome int	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
690*4a5d661aSToomas Soome int	in_pcb_lport(struct inpcb *, struct in_addr *, u_short *,
691*4a5d661aSToomas Soome 	    struct ucred *, int);
692*4a5d661aSToomas Soome int	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
693*4a5d661aSToomas Soome 	    u_short *, struct ucred *);
694*4a5d661aSToomas Soome int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
695*4a5d661aSToomas Soome int	in_pcbconnect_mbuf(struct inpcb *, struct sockaddr *, struct ucred *,
696*4a5d661aSToomas Soome 	    struct mbuf *);
697*4a5d661aSToomas Soome int	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
698*4a5d661aSToomas Soome 	    u_short *, in_addr_t *, u_short *, struct inpcb **,
699*4a5d661aSToomas Soome 	    struct ucred *);
700*4a5d661aSToomas Soome void	in_pcbdetach(struct inpcb *);
701*4a5d661aSToomas Soome void	in_pcbdisconnect(struct inpcb *);
702*4a5d661aSToomas Soome void	in_pcbdrop(struct inpcb *);
703*4a5d661aSToomas Soome void	in_pcbfree(struct inpcb *);
704*4a5d661aSToomas Soome int	in_pcbinshash(struct inpcb *);
705*4a5d661aSToomas Soome int	in_pcbinshash_nopcbgroup(struct inpcb *);
706*4a5d661aSToomas Soome int	in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
707*4a5d661aSToomas Soome 	    struct ucred *);
708*4a5d661aSToomas Soome struct inpcb *
709*4a5d661aSToomas Soome 	in_pcblookup_local(struct inpcbinfo *,
710*4a5d661aSToomas Soome 	    struct in_addr, u_short, int, struct ucred *);
711*4a5d661aSToomas Soome struct inpcb *
712*4a5d661aSToomas Soome 	in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
713*4a5d661aSToomas Soome 	    struct in_addr, u_int, int, struct ifnet *);
714*4a5d661aSToomas Soome struct inpcb *
715*4a5d661aSToomas Soome 	in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
716*4a5d661aSToomas Soome 	    struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
717*4a5d661aSToomas Soome void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
718*4a5d661aSToomas Soome 	    int, struct inpcb *(*)(struct inpcb *, int));
719*4a5d661aSToomas Soome void	in_pcbref(struct inpcb *);
720*4a5d661aSToomas Soome void	in_pcbrehash(struct inpcb *);
721*4a5d661aSToomas Soome void	in_pcbrehash_mbuf(struct inpcb *, struct mbuf *);
722*4a5d661aSToomas Soome int	in_pcbrele(struct inpcb *);
723*4a5d661aSToomas Soome int	in_pcbrele_rlocked(struct inpcb *);
724*4a5d661aSToomas Soome int	in_pcbrele_wlocked(struct inpcb *);
725*4a5d661aSToomas Soome void	in_pcbsetsolabel(struct socket *so);
726*4a5d661aSToomas Soome int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
727*4a5d661aSToomas Soome int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
728*4a5d661aSToomas Soome struct sockaddr *
729*4a5d661aSToomas Soome 	in_sockaddr(in_port_t port, struct in_addr *addr);
730*4a5d661aSToomas Soome void	in_pcbsosetlabel(struct socket *so);
731*4a5d661aSToomas Soome #endif /* _KERNEL */
732*4a5d661aSToomas Soome 
733*4a5d661aSToomas Soome #endif /* !_NETINET_IN_PCB_H_ */
734