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