xref: /freebsd/sys/netinet/in_pcb.h (revision 3ae4b0e7e8e6d8e4d5a451db1c327cc1f7206b89)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1990, 1993
3a5654bb2SRobert Watson  *	The Regents of the University of California.
482a5be49SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
5a5654bb2SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
782a5be49SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
882a5be49SRobert Watson  * contract to Juniper Networks, Inc.
982a5be49SRobert Watson  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
35c3aac50fSPeter Wemm  * $FreeBSD$
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
38707f139eSPaul Richards #ifndef _NETINET_IN_PCB_H_
39707f139eSPaul Richards #define _NETINET_IN_PCB_H_
40707f139eSPaul Richards 
41f1844d4cSBruce Evans #include <sys/queue.h>
4240545cf5SBruce Evans #include <sys/_lock.h>
4340545cf5SBruce Evans #include <sys/_mutex.h>
448501a69cSRobert Watson #include <sys/_rwlock.h>
4584cc0778SGeorge V. Neville-Neil #include <net/route.h>
46f1844d4cSBruce Evans 
478501a69cSRobert Watson #ifdef _KERNEL
481417604eSBjoern A. Zeeb #include <sys/lock.h>
498501a69cSRobert Watson #include <sys/rwlock.h>
50eddfbb76SRobert Watson #include <net/vnet.h>
519bcd427bSRobert Watson #include <vm/uma.h>
528501a69cSRobert Watson #endif
538501a69cSRobert Watson 
5482cd038dSYoshinobu Inoue #define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
5582cd038dSYoshinobu Inoue #define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
5682cd038dSYoshinobu Inoue 
57df8bae1dSRodney W. Grimes /*
58fc18af96SRobert Watson  * struct inpcb is the common protocol control block structure used in most
59fc18af96SRobert Watson  * IP transport protocols.
60712fc218SRobert Watson  *
61712fc218SRobert Watson  * Pointers to local and foreign host table entries, local and foreign socket
62712fc218SRobert Watson  * numbers, and pointers up (to a socket structure) and down (to a
63712fc218SRobert Watson  * protocol-specific control block) are stored here.
64df8bae1dSRodney W. Grimes  */
65e3975643SJake Burkholder LIST_HEAD(inpcbhead, inpcb);
66e3975643SJake Burkholder LIST_HEAD(inpcbporthead, inpcbport);
67cc65eb4eSGleb Smirnoff typedef	uint64_t	inp_gen_t;
6815bd2b43SDavid Greenman 
693d4d47f3SGarrett Wollman /*
7082cd038dSYoshinobu Inoue  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
71712fc218SRobert Watson  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
72712fc218SRobert Watson  * the following structure.
7382cd038dSYoshinobu Inoue  */
7482cd038dSYoshinobu Inoue struct in_addr_4in6 {
7582cd038dSYoshinobu Inoue 	u_int32_t	ia46_pad32[3];
7682cd038dSYoshinobu Inoue 	struct	in_addr	ia46_addr4;
7782cd038dSYoshinobu Inoue };
7882cd038dSYoshinobu Inoue 
7982cd038dSYoshinobu Inoue /*
80712fc218SRobert Watson  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
81712fc218SRobert Watson  * some extra padding to accomplish this.
82388909a1SGleb Smirnoff  * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport,
83388909a1SGleb Smirnoff  * lport, faddr to generate hash, so these fields shouldn't be moved.
84be2ac88cSJonathan Lemon  */
85be2ac88cSJonathan Lemon struct in_endpoints {
86be2ac88cSJonathan Lemon 	u_int16_t	ie_fport;		/* foreign port */
87be2ac88cSJonathan Lemon 	u_int16_t	ie_lport;		/* local port */
88be2ac88cSJonathan Lemon 	/* protocol dependent part, local and foreign addr */
89be2ac88cSJonathan Lemon 	union {
90be2ac88cSJonathan Lemon 		/* foreign host table entry */
91be2ac88cSJonathan Lemon 		struct	in_addr_4in6 ie46_foreign;
92be2ac88cSJonathan Lemon 		struct	in6_addr ie6_foreign;
93be2ac88cSJonathan Lemon 	} ie_dependfaddr;
94be2ac88cSJonathan Lemon 	union {
95be2ac88cSJonathan Lemon 		/* local host table entry */
96be2ac88cSJonathan Lemon 		struct	in_addr_4in6 ie46_local;
97be2ac88cSJonathan Lemon 		struct	in6_addr ie6_local;
98be2ac88cSJonathan Lemon 	} ie_dependladdr;
99028bdf28SAndrey V. Elsukov 	u_int32_t	ie6_zoneid;		/* scope zone id */
100a5654bb2SRobert Watson };
101be2ac88cSJonathan Lemon #define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
102be2ac88cSJonathan Lemon #define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
103be2ac88cSJonathan Lemon #define	ie6_faddr	ie_dependfaddr.ie6_foreign
104be2ac88cSJonathan Lemon #define	ie6_laddr	ie_dependladdr.ie6_local
105be2ac88cSJonathan Lemon 
106be2ac88cSJonathan Lemon /*
107712fc218SRobert Watson  * XXX The defines for inc_* are hacks and should be changed to direct
108712fc218SRobert Watson  * references.
109be2ac88cSJonathan Lemon  */
110be2ac88cSJonathan Lemon struct in_conninfo {
111be2ac88cSJonathan Lemon 	u_int8_t	inc_flags;
112be2ac88cSJonathan Lemon 	u_int8_t	inc_len;
1138b07e49aSJulian Elischer 	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
11497d8d152SAndre Oppermann 	/* protocol dependent part */
115be2ac88cSJonathan Lemon 	struct	in_endpoints inc_ie;
116be2ac88cSJonathan Lemon };
117dcdb4371SBjoern A. Zeeb 
118dcdb4371SBjoern A. Zeeb /*
119dcdb4371SBjoern A. Zeeb  * Flags for inc_flags.
120dcdb4371SBjoern A. Zeeb  */
121dcdb4371SBjoern A. Zeeb #define	INC_ISIPV6	0x01
122dcdb4371SBjoern A. Zeeb 
123a4641f4eSPedro F. Giffuni #define	inc_isipv6	inc_flags	/* temp compatibility */
124be2ac88cSJonathan Lemon #define	inc_fport	inc_ie.ie_fport
125be2ac88cSJonathan Lemon #define	inc_lport	inc_ie.ie_lport
126be2ac88cSJonathan Lemon #define	inc_faddr	inc_ie.ie_faddr
127be2ac88cSJonathan Lemon #define	inc_laddr	inc_ie.ie_laddr
128be2ac88cSJonathan Lemon #define	inc6_faddr	inc_ie.ie6_faddr
129be2ac88cSJonathan Lemon #define	inc6_laddr	inc_ie.ie6_laddr
130028bdf28SAndrey V. Elsukov #define	inc6_zoneid	inc_ie.ie6_zoneid
131be2ac88cSJonathan Lemon 
132cc65eb4eSGleb Smirnoff #if defined(_KERNEL) || defined(_WANT_INPCB)
133cc65eb4eSGleb Smirnoff /*
134ff9b006dSJulien Charbon  * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and
135ff9b006dSJulien Charbon  * IPv6 sockets.  In the case of TCP and UDP, further per-connection state is
1367b709f8aSRobert Watson  * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
1377b709f8aSRobert Watson  * are static after creation or protected by a per-inpcb rwlock, inp_lock.  A
138ff9b006dSJulien Charbon  * few fields are protected by multiple locks as indicated in the locking notes
139ff9b006dSJulien Charbon  * below.  For these fields, all of the listed locks must be write-locked for
140ff9b006dSJulien Charbon  * any modifications.  However, these fields can be safely read while any one of
141ff9b006dSJulien Charbon  * the listed locks are read-locked.  This model can permit greater concurrency
142ff9b006dSJulien Charbon  * for read operations.  For example, connections can be looked up while only
143ff9b006dSJulien Charbon  * holding a read lock on the global pcblist lock.  This is important for
144ff9b006dSJulien Charbon  * performance when attempting to find the connection for a packet given its IP
145ff9b006dSJulien Charbon  * and port tuple.
146ff9b006dSJulien Charbon  *
147ff9b006dSJulien Charbon  * One noteworthy exception is that the global pcbinfo lock follows a different
148ff9b006dSJulien Charbon  * set of rules in relation to the inp_list field.  Rather than being
149ff9b006dSJulien Charbon  * write-locked for modifications and read-locked for list iterations, it must
150ff9b006dSJulien Charbon  * be read-locked during modifications and write-locked during list iterations.
151ff9b006dSJulien Charbon  * This ensures that the relatively rare global list iterations safely walk a
152ff9b006dSJulien Charbon  * stable snapshot of connections while allowing more common list modifications
153ff9b006dSJulien Charbon  * to safely grab the pcblist lock just while adding or removing a connection
154ff9b006dSJulien Charbon  * from the global list.
1557b709f8aSRobert Watson  *
1567b709f8aSRobert Watson  * Key:
1577b709f8aSRobert Watson  * (c) - Constant after initialization
15852cd27cbSRobert Watson  * (g) - Protected by the pcbgroup lock
1597b709f8aSRobert Watson  * (i) - Protected by the inpcb lock
1607b709f8aSRobert Watson  * (p) - Protected by the pcbinfo lock for the inpcb
161ff9b006dSJulien Charbon  * (l) - Protected by the pcblist lock for the inpcb
162ff9b006dSJulien Charbon  * (h) - Protected by the pcbhash lock for the inpcb
1637b709f8aSRobert Watson  * (s) - Protected by another subsystem's locks
1647b709f8aSRobert Watson  * (x) - Undefined locking
1657b709f8aSRobert Watson  *
1667b709f8aSRobert Watson  * A few other notes:
1677b709f8aSRobert Watson  *
1687b709f8aSRobert Watson  * When a read lock is held, stability of the field is guaranteed; to write
1697b709f8aSRobert Watson  * to a field, a write lock must generally be held.
1707b709f8aSRobert Watson  *
1717b709f8aSRobert Watson  * netinet/netinet6-layer code should not assume that the inp_socket pointer
1727b709f8aSRobert Watson  * is safe to dereference without inp_lock being held, even for protocols
1737b709f8aSRobert Watson  * other than TCP (where the inpcb persists during TIMEWAIT even after the
1747b709f8aSRobert Watson  * socket has been freed), or there may be close(2)-related races.
1757b709f8aSRobert Watson  *
1767b709f8aSRobert Watson  * The inp_vflag field is overloaded, and would otherwise ideally be (c).
177ff9b006dSJulien Charbon  *
178ff9b006dSJulien Charbon  * TODO:  Currently only the TCP stack is leveraging the global pcbinfo lock
179ff9b006dSJulien Charbon  * read-lock usage during modification, this model can be applied to other
180ff9b006dSJulien Charbon  * protocols (especially SCTP).
1817b709f8aSRobert Watson  */
182cc65eb4eSGleb Smirnoff struct icmp6_filter;
183cc65eb4eSGleb Smirnoff struct inpcbpolicy;
184f3e7afe2SHans Petter Selasky struct m_snd_tag;
185df8bae1dSRodney W. Grimes struct inpcb {
186ff9b006dSJulien Charbon 	LIST_ENTRY(inpcb) inp_hash;	/* (h/i) hash list */
18752cd27cbSRobert Watson 	LIST_ENTRY(inpcb) inp_pcbgrouphash;	/* (g/i) hash list */
188ff9b006dSJulien Charbon 	LIST_ENTRY(inpcb) inp_list;	/* (p/l) list for all PCBs for proto */
189ff9b006dSJulien Charbon 	                                /* (p[w]) for list iteration */
190ff9b006dSJulien Charbon 	                                /* (p[r]/l) for addition/removal */
1917b709f8aSRobert Watson 	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
1927b709f8aSRobert Watson 	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
19352cd27cbSRobert Watson 	struct	inpcbgroup *inp_pcbgroup; /* (g/i) PCB group list */
194ff9b006dSJulien Charbon 	LIST_ENTRY(inpcb) inp_pcbgroup_wild; /* (g/i/h) group wildcard entry */
1957b709f8aSRobert Watson 	struct	socket *inp_socket;	/* (i) back pointer to socket */
19686d02c5cSBjoern A. Zeeb 	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
1977b709f8aSRobert Watson 	u_int32_t inp_flow;		/* (i) IPv6 flow information */
1987b709f8aSRobert Watson 	int	inp_flags;		/* (i) generic IP/datagram flags */
199773b573aSKip Macy 	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
2007b709f8aSRobert Watson 	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
2017b709f8aSRobert Watson 	u_char	inp_ip_ttl;		/* (i) time to live proto */
2027b709f8aSRobert Watson 	u_char	inp_ip_p;		/* (c) protocol proto */
2037b709f8aSRobert Watson 	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
20480cb9f21SKip Macy 	uint32_t inp_flowid;		/* (x) flow id / queue id */
20528696211SRobert Watson 	u_int	inp_refcount;		/* (i) refcount */
206f3e7afe2SHans Petter Selasky 	struct m_snd_tag *inp_snd_tag;	/* (i) send tag for outgoing mbufs */
20734e3dcedSAdrian Chadd 	uint32_t inp_flowtype;		/* (x) M_HASHTYPE value */
2080a100a6fSAdrian Chadd 	uint32_t inp_rss_listen_bucket;	/* (x) overridden RSS listen bucket */
2092de2af32SKip Macy 
2102de2af32SKip Macy 	/* Local and foreign ports, local and foreign addr. */
211ff9b006dSJulien Charbon 	struct	in_conninfo inp_inc;	/* (i) list for PCB's local port */
2122de2af32SKip Macy 
213a5654bb2SRobert Watson 	/* MAC and IPSEC policy information. */
2147b709f8aSRobert Watson 	struct	label *inp_label;	/* (i) MAC label */
2157b709f8aSRobert Watson 	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
21682cd038dSYoshinobu Inoue 
217712fc218SRobert Watson 	/* Protocol-dependent part; options. */
21882cd038dSYoshinobu Inoue 	struct {
219cc65eb4eSGleb Smirnoff 		u_char	inp_ip_tos;		/* (i) type of service proto */
220cc65eb4eSGleb Smirnoff 		struct mbuf		*inp_options;	/* (i) IP options */
221cc65eb4eSGleb Smirnoff 		struct ip_moptions	*inp_moptions;	/* (i) mcast options */
222cc65eb4eSGleb Smirnoff 	};
22382cd038dSYoshinobu Inoue 	struct {
2247b709f8aSRobert Watson 		/* (i) IP options */
225cc65eb4eSGleb Smirnoff 		struct mbuf		*in6p_options;
2267b709f8aSRobert Watson 		/* (i) IP6 options for outgoing packets */
227cc65eb4eSGleb Smirnoff 		struct ip6_pktopts	*in6p_outputopts;
2287b709f8aSRobert Watson 		/* (i) IP multicast options */
229cc65eb4eSGleb Smirnoff 		struct ip6_moptions	*in6p_moptions;
2307b709f8aSRobert Watson 		/* (i) ICMPv6 code type filter */
231cc65eb4eSGleb Smirnoff 		struct icmp6_filter	*in6p_icmp6filt;
2327b709f8aSRobert Watson 		/* (i) IPV6_CHECKSUM setsockopt */
233cc65eb4eSGleb Smirnoff 		int	in6p_cksum;
234cc65eb4eSGleb Smirnoff 		short	in6p_hops;
235cc65eb4eSGleb Smirnoff 	};
236ff9b006dSJulien Charbon 	LIST_ENTRY(inpcb) inp_portlist;	/* (i/h) */
237ff9b006dSJulien Charbon 	struct	inpcbport *inp_phd;	/* (i/h) head of this list */
238d915b280SStephan Uphoff #define inp_zero_size offsetof(struct inpcb, inp_gencnt)
239a5654bb2SRobert Watson 	inp_gen_t	inp_gencnt;	/* (c) generation count */
240114f15c6SKip Macy 	struct llentry	*inp_lle;	/* cached L2 information */
2418501a69cSRobert Watson 	struct rwlock	inp_lock;
24284cc0778SGeorge V. Neville-Neil 	rt_gen_t	inp_rt_cookie;	/* generation for route entry */
24384cc0778SGeorge V. Neville-Neil 	union {				/* cached L3 information */
24484cc0778SGeorge V. Neville-Neil 		struct route inpu_route;
24584cc0778SGeorge V. Neville-Neil 		struct route_in6 inpu_route6;
24684cc0778SGeorge V. Neville-Neil 	} inp_rtu;
24784cc0778SGeorge V. Neville-Neil #define inp_route inp_rtu.inpu_route
24884cc0778SGeorge V. Neville-Neil #define inp_route6 inp_rtu.inpu_route6
249a5654bb2SRobert Watson };
250cc65eb4eSGleb Smirnoff #endif	/* _KERNEL */
251cc65eb4eSGleb Smirnoff 
252a5654bb2SRobert Watson #define	inp_fport	inp_inc.inc_fport
253a5654bb2SRobert Watson #define	inp_lport	inp_inc.inc_lport
254a5654bb2SRobert Watson #define	inp_faddr	inp_inc.inc_faddr
255a5654bb2SRobert Watson #define	inp_laddr	inp_inc.inc_laddr
256f76fcf6dSJeffrey Hsu 
257be2ac88cSJonathan Lemon #define	in6p_faddr	inp_inc.inc6_faddr
258be2ac88cSJonathan Lemon #define	in6p_laddr	inp_inc.inc6_laddr
259028bdf28SAndrey V. Elsukov #define	in6p_zoneid	inp_inc.inc6_zoneid
26082cd038dSYoshinobu Inoue #define	in6p_flowinfo	inp_flow
261a5654bb2SRobert Watson 
262f6dfe47aSMarko Zec #define	inp_vnet	inp_pcbinfo->ipi_vnet
263f6dfe47aSMarko Zec 
2643d4d47f3SGarrett Wollman /*
265712fc218SRobert Watson  * The range of the generation count, as used in this implementation, is 9e19.
266712fc218SRobert Watson  * We would have to create 300 billion connections per second for this number
267712fc218SRobert Watson  * to roll over in a year.  This seems sufficiently unlikely that we simply
268712fc218SRobert Watson  * don't concern ourselves with that possibility.
2693d4d47f3SGarrett Wollman  */
270c3229e05SDavid Greenman 
27198271db4SGarrett Wollman /*
272712fc218SRobert Watson  * Interface exported to userland by various protocols which use inpcbs.  Hack
273712fc218SRobert Watson  * alert -- only define if struct xsocket is in scope.
274cc65eb4eSGleb Smirnoff  * Fields prefixed with "xi_" are unique to this structure, and the rest
275cc65eb4eSGleb Smirnoff  * match fields in the struct inpcb, to ease coding and porting.
276cc65eb4eSGleb Smirnoff  *
277cc65eb4eSGleb Smirnoff  * Legend:
278cc65eb4eSGleb Smirnoff  * (s) - used by userland utilities in src
279cc65eb4eSGleb Smirnoff  * (p) - used by utilities in ports
280cc65eb4eSGleb Smirnoff  * (3) - is known to be used by third party software not in ports
281cc65eb4eSGleb Smirnoff  * (n) - no known usage
28298271db4SGarrett Wollman  */
28398271db4SGarrett Wollman #ifdef _SYS_SOCKETVAR_H_
28498271db4SGarrett Wollman struct xinpcb {
28598271db4SGarrett Wollman 	size_t		xi_len;		/* length of this structure */
286cc65eb4eSGleb Smirnoff 	struct xsocket	xi_socket;		/* (s,p) */
287cc65eb4eSGleb Smirnoff 	struct in_conninfo inp_inc;		/* (s,p) */
288cc65eb4eSGleb Smirnoff 	uint64_t	inp_gencnt;		/* (s,p) */
289cc65eb4eSGleb Smirnoff 	union {
290cc65eb4eSGleb Smirnoff 		void	*inp_ppcb;		/* (s) netstat(1) */
291cc65eb4eSGleb Smirnoff 		int64_t	ph_ppcb;
29298271db4SGarrett Wollman 	};
293cc65eb4eSGleb Smirnoff 	int64_t		inp_spare64[4];
294cc65eb4eSGleb Smirnoff 	uint32_t	inp_flow;		/* (s) */
295cc65eb4eSGleb Smirnoff 	uint32_t	inp_flowid;		/* (s) */
296cc65eb4eSGleb Smirnoff 	uint32_t	inp_flowtype;		/* (s) */
297cc65eb4eSGleb Smirnoff 	int32_t		inp_flags;		/* (s,p) */
298cc65eb4eSGleb Smirnoff 	int32_t		inp_flags2;		/* (s) */
299cc65eb4eSGleb Smirnoff 	int32_t		inp_rss_listen_bucket;	/* (n) */
300cc65eb4eSGleb Smirnoff 	int32_t		in6p_cksum;		/* (n) */
301cc65eb4eSGleb Smirnoff 	int32_t		inp_spare32[4];
302cc65eb4eSGleb Smirnoff 	uint16_t	in6p_hops;		/* (n) */
303cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_tos;		/* (n) */
304cc65eb4eSGleb Smirnoff 	int8_t		pad8;
305cc65eb4eSGleb Smirnoff 	uint8_t		inp_vflag;		/* (s,p) */
306cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_ttl;		/* (n) */
307cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_p;		/* (n) */
308cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_minttl;		/* (n) */
309cc65eb4eSGleb Smirnoff 	int8_t		inp_spare8[4];
310cc65eb4eSGleb Smirnoff } __aligned(8);
31198271db4SGarrett Wollman 
31298271db4SGarrett Wollman struct xinpgen {
31398271db4SGarrett Wollman 	size_t		xig_len;	/* length of this structure */
31498271db4SGarrett Wollman 	u_int		xig_count;	/* number of PCBs at this time */
31598271db4SGarrett Wollman 	inp_gen_t	xig_gen;	/* generation count at this time */
316cc65eb4eSGleb Smirnoff 	so_gen_t	xig_sogen;	/* socket generation count this time */
317*3ae4b0e7SGleb Smirnoff } __aligned(8);
318cc65eb4eSGleb Smirnoff #ifdef	_KERNEL
319cc65eb4eSGleb Smirnoff void	in_pcbtoxinpcb(const struct inpcb *, struct xinpcb *);
320cc65eb4eSGleb Smirnoff #endif
32198271db4SGarrett Wollman #endif /* _SYS_SOCKETVAR_H_ */
32298271db4SGarrett Wollman 
323c3229e05SDavid Greenman struct inpcbport {
324e3975643SJake Burkholder 	LIST_ENTRY(inpcbport) phd_hash;
325c3229e05SDavid Greenman 	struct inpcbhead phd_pcblist;
326c3229e05SDavid Greenman 	u_short phd_port;
327df8bae1dSRodney W. Grimes };
328df8bae1dSRodney W. Grimes 
32982a5be49SRobert Watson /*-
330712fc218SRobert Watson  * Global data structure for each high-level protocol (UDP, TCP, ...) in both
331712fc218SRobert Watson  * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
33282a5be49SRobert Watson  *
333ff9b006dSJulien Charbon  * Each pcbinfo is protected by three locks: ipi_lock, ipi_hash_lock and
334ff9b006dSJulien Charbon  * ipi_list_lock:
335ff9b006dSJulien Charbon  *  - ipi_lock covering the global pcb list stability during loop iteration,
336ff9b006dSJulien Charbon  *  - ipi_hash_lock covering the hashed lookup tables,
337ff9b006dSJulien Charbon  *  - ipi_list_lock covering mutable global fields (such as the global
338ff9b006dSJulien Charbon  *    pcb list)
33982a5be49SRobert Watson  *
340ff9b006dSJulien Charbon  * The lock order is:
341ff9b006dSJulien Charbon  *
342ff9b006dSJulien Charbon  *    ipi_lock (before)
343ff9b006dSJulien Charbon  *        inpcb locks (before)
344ff9b006dSJulien Charbon  *            ipi_list locks (before)
345ff9b006dSJulien Charbon  *                {ipi_hash_lock, pcbgroup locks}
34682a5be49SRobert Watson  *
34782a5be49SRobert Watson  * Locking key:
34882a5be49SRobert Watson  *
34982a5be49SRobert Watson  * (c) Constant or nearly constant after initialisation
35082a5be49SRobert Watson  * (g) Locked by ipi_lock
351ff9b006dSJulien Charbon  * (l) Locked by ipi_list_lock
35252cd27cbSRobert Watson  * (h) Read using either ipi_hash_lock or inpcb lock; write requires both
35352cd27cbSRobert Watson  * (p) Protected by one or more pcbgroup locks
35482a5be49SRobert Watson  * (x) Synchronisation properties poorly defined
355712fc218SRobert Watson  */
356712fc218SRobert Watson struct inpcbinfo {
357712fc218SRobert Watson 	/*
358ff9b006dSJulien Charbon 	 * Global lock protecting full inpcb list traversal
35982a5be49SRobert Watson 	 */
36082a5be49SRobert Watson 	struct rwlock		 ipi_lock;
36182a5be49SRobert Watson 
36282a5be49SRobert Watson 	/*
363712fc218SRobert Watson 	 * Global list of inpcbs on the protocol.
364712fc218SRobert Watson 	 */
365ff9b006dSJulien Charbon 	struct inpcbhead	*ipi_listhead;		/* (g/l) */
366ff9b006dSJulien Charbon 	u_int			 ipi_count;		/* (l) */
367712fc218SRobert Watson 
368712fc218SRobert Watson 	/*
369712fc218SRobert Watson 	 * Generation count -- incremented each time a connection is allocated
370712fc218SRobert Watson 	 * or freed.
371712fc218SRobert Watson 	 */
372ff9b006dSJulien Charbon 	u_quad_t		 ipi_gencnt;		/* (l) */
37382a5be49SRobert Watson 
37482a5be49SRobert Watson 	/*
37582a5be49SRobert Watson 	 * Fields associated with port lookup and allocation.
37682a5be49SRobert Watson 	 */
37782a5be49SRobert Watson 	u_short			 ipi_lastport;		/* (x) */
37882a5be49SRobert Watson 	u_short			 ipi_lastlow;		/* (x) */
37982a5be49SRobert Watson 	u_short			 ipi_lasthi;		/* (x) */
38082a5be49SRobert Watson 
38182a5be49SRobert Watson 	/*
38282a5be49SRobert Watson 	 * UMA zone from which inpcbs are allocated for this protocol.
38382a5be49SRobert Watson 	 */
38482a5be49SRobert Watson 	struct	uma_zone	*ipi_zone;		/* (c) */
38582a5be49SRobert Watson 
38682a5be49SRobert Watson 	/*
38752cd27cbSRobert Watson 	 * Connection groups associated with this protocol.  These fields are
38852cd27cbSRobert Watson 	 * constant, but pcbgroup structures themselves are protected by
38952cd27cbSRobert Watson 	 * per-pcbgroup locks.
39052cd27cbSRobert Watson 	 */
39152cd27cbSRobert Watson 	struct inpcbgroup	*ipi_pcbgroups;		/* (c) */
39252cd27cbSRobert Watson 	u_int			 ipi_npcbgroups;	/* (c) */
39352cd27cbSRobert Watson 	u_int			 ipi_hashfields;	/* (c) */
39452cd27cbSRobert Watson 
39552cd27cbSRobert Watson 	/*
39652cd27cbSRobert Watson 	 * Global lock protecting non-pcbgroup hash lookup tables.
397fa046d87SRobert Watson 	 */
398f89d4c3aSAndre Oppermann 	struct rwlock		 ipi_hash_lock;
399fa046d87SRobert Watson 
400fa046d87SRobert Watson 	/*
40182a5be49SRobert Watson 	 * Global hash of inpcbs, hashed by local and foreign addresses and
40282a5be49SRobert Watson 	 * port numbers.
40382a5be49SRobert Watson 	 */
404fa046d87SRobert Watson 	struct inpcbhead	*ipi_hashbase;		/* (h) */
405fa046d87SRobert Watson 	u_long			 ipi_hashmask;		/* (h) */
40682a5be49SRobert Watson 
40782a5be49SRobert Watson 	/*
40882a5be49SRobert Watson 	 * Global hash of inpcbs, hashed by only local port number.
40982a5be49SRobert Watson 	 */
410fa046d87SRobert Watson 	struct inpcbporthead	*ipi_porthashbase;	/* (h) */
411fa046d87SRobert Watson 	u_long			 ipi_porthashmask;	/* (h) */
4122de2af32SKip Macy 
4132de2af32SKip Macy 	/*
41452cd27cbSRobert Watson 	 * List of wildcard inpcbs for use with pcbgroups.  In the past, was
41552cd27cbSRobert Watson 	 * per-pcbgroup but is now global.  All pcbgroup locks must be held
41652cd27cbSRobert Watson 	 * to modify the list, so any is sufficient to read it.
41752cd27cbSRobert Watson 	 */
41852cd27cbSRobert Watson 	struct inpcbhead	*ipi_wildbase;		/* (p) */
41952cd27cbSRobert Watson 	u_long			 ipi_wildmask;		/* (p) */
42052cd27cbSRobert Watson 
42152cd27cbSRobert Watson 	/*
422f6dfe47aSMarko Zec 	 * Pointer to network stack instance
423f6dfe47aSMarko Zec 	 */
42482a5be49SRobert Watson 	struct vnet		*ipi_vnet;		/* (c) */
425f6dfe47aSMarko Zec 
426f6dfe47aSMarko Zec 	/*
427f6dfe47aSMarko Zec 	 * general use 2
4282de2af32SKip Macy 	 */
4292de2af32SKip Macy 	void 			*ipi_pspare[2];
430ff9b006dSJulien Charbon 
431ff9b006dSJulien Charbon 	/*
432ff9b006dSJulien Charbon 	 * Global lock protecting global inpcb list, inpcb count, etc.
433ff9b006dSJulien Charbon 	 */
434ff9b006dSJulien Charbon 	struct rwlock		 ipi_list_lock;
43515bd2b43SDavid Greenman };
43615bd2b43SDavid Greenman 
43775410f02SRobert Millan #ifdef _KERNEL
43852cd27cbSRobert Watson /*
43952cd27cbSRobert Watson  * Connection groups hold sets of connections that have similar CPU/thread
44052cd27cbSRobert Watson  * affinity.  Each connection belongs to exactly one connection group.
44152cd27cbSRobert Watson  */
44252cd27cbSRobert Watson struct inpcbgroup {
44352cd27cbSRobert Watson 	/*
44452cd27cbSRobert Watson 	 * Per-connection group hash of inpcbs, hashed by local and foreign
44552cd27cbSRobert Watson 	 * addresses and port numbers.
44652cd27cbSRobert Watson 	 */
44752cd27cbSRobert Watson 	struct inpcbhead	*ipg_hashbase;		/* (c) */
44852cd27cbSRobert Watson 	u_long			 ipg_hashmask;		/* (c) */
44952cd27cbSRobert Watson 
45052cd27cbSRobert Watson 	/*
45152cd27cbSRobert Watson 	 * Notional affinity of this pcbgroup.
45252cd27cbSRobert Watson 	 */
45352cd27cbSRobert Watson 	u_int			 ipg_cpu;		/* (p) */
45452cd27cbSRobert Watson 
45552cd27cbSRobert Watson 	/*
45652cd27cbSRobert Watson 	 * Per-connection group lock, not to be confused with ipi_lock.
45752cd27cbSRobert Watson 	 * Protects the hash table hung off the group, but also the global
45852cd27cbSRobert Watson 	 * wildcard list in inpcbinfo.
45952cd27cbSRobert Watson 	 */
46052cd27cbSRobert Watson 	struct mtx		 ipg_lock;
46152cd27cbSRobert Watson } __aligned(CACHE_LINE_SIZE);
46252cd27cbSRobert Watson 
4635bd311a5SSam Leffler #define INP_LOCK_INIT(inp, d, t) \
4648501a69cSRobert Watson 	rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE |  RW_DUPOK)
4658501a69cSRobert Watson #define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
4668501a69cSRobert Watson #define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
4678501a69cSRobert Watson #define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
46843cc0bc1SRobert Watson #define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
46943cc0bc1SRobert Watson #define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
4708501a69cSRobert Watson #define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
4718501a69cSRobert Watson #define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
47282c33e73SKip Macy #define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
47382c33e73SKip Macy #define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
47482c33e73SKip Macy #define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
4758501a69cSRobert Watson #define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
4768501a69cSRobert Watson #define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
4778501a69cSRobert Watson #define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
4788501a69cSRobert Watson #define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
479f76fcf6dSJeffrey Hsu 
4803d585327SKip Macy /*
4813d585327SKip Macy  * These locking functions are for inpcb consumers outside of sys/netinet,
4823d585327SKip Macy  * more specifically, they were added for the benefit of TOE drivers. The
4833d585327SKip Macy  * macros are reserved for use by the stack.
4843d585327SKip Macy  */
4853d585327SKip Macy void inp_wlock(struct inpcb *);
4863d585327SKip Macy void inp_wunlock(struct inpcb *);
4873d585327SKip Macy void inp_rlock(struct inpcb *);
4883d585327SKip Macy void inp_runlock(struct inpcb *);
4893d585327SKip Macy 
490c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
491e79dd20dSKip Macy void inp_lock_assert(struct inpcb *);
492e79dd20dSKip Macy void inp_unlock_assert(struct inpcb *);
4933d585327SKip Macy #else
494c75e2666SGleb Smirnoff #define	inp_lock_assert(inp)	do {} while (0)
495c75e2666SGleb Smirnoff #define	inp_unlock_assert(inp)	do {} while (0)
4963d585327SKip Macy #endif
4979378e437SKip Macy 
4989378e437SKip Macy void	inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
4999378e437SKip Macy int 	inp_ip_tos_get(const struct inpcb *inp);
5009378e437SKip Macy void 	inp_ip_tos_set(struct inpcb *inp, int val);
5019d29c635SKip Macy struct socket *
5029d29c635SKip Macy 	inp_inpcbtosocket(struct inpcb *inp);
5039d29c635SKip Macy struct tcpcb *
5049d29c635SKip Macy 	inp_inpcbtotcpcb(struct inpcb *inp);
505df9cf830STai-hwa Liang void 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
5069d29c635SKip Macy 		uint32_t *faddr, uint16_t *fp);
507efdf104bSMikolaj Golub short	inp_so_options(const struct inpcb *inp);
5089378e437SKip Macy 
509e79dd20dSKip Macy #endif /* _KERNEL */
5103d585327SKip Macy 
511f76fcf6dSJeffrey Hsu #define INP_INFO_LOCK_INIT(ipi, d) \
5128501a69cSRobert Watson 	rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE)
5138501a69cSRobert Watson #define INP_INFO_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_lock)
5148501a69cSRobert Watson #define INP_INFO_RLOCK(ipi)	rw_rlock(&(ipi)->ipi_lock)
5158501a69cSRobert Watson #define INP_INFO_WLOCK(ipi)	rw_wlock(&(ipi)->ipi_lock)
51643cc0bc1SRobert Watson #define INP_INFO_TRY_RLOCK(ipi)	rw_try_rlock(&(ipi)->ipi_lock)
51743cc0bc1SRobert Watson #define INP_INFO_TRY_WLOCK(ipi)	rw_try_wlock(&(ipi)->ipi_lock)
51866f80e90SRobert Watson #define INP_INFO_TRY_UPGRADE(ipi)	rw_try_upgrade(&(ipi)->ipi_lock)
519079672cbSJulien Charbon #define INP_INFO_WLOCKED(ipi)	rw_wowned(&(ipi)->ipi_lock)
5208501a69cSRobert Watson #define INP_INFO_RUNLOCK(ipi)	rw_runlock(&(ipi)->ipi_lock)
5218501a69cSRobert Watson #define INP_INFO_WUNLOCK(ipi)	rw_wunlock(&(ipi)->ipi_lock)
5228501a69cSRobert Watson #define	INP_INFO_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_LOCKED)
5238501a69cSRobert Watson #define INP_INFO_RLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_RLOCKED)
5248501a69cSRobert Watson #define INP_INFO_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_WLOCKED)
5258501a69cSRobert Watson #define INP_INFO_UNLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED)
526f76fcf6dSJeffrey Hsu 
527ff9b006dSJulien Charbon #define INP_LIST_LOCK_INIT(ipi, d) \
528ff9b006dSJulien Charbon         rw_init_flags(&(ipi)->ipi_list_lock, (d), 0)
529ff9b006dSJulien Charbon #define INP_LIST_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_list_lock)
530ff9b006dSJulien Charbon #define INP_LIST_RLOCK(ipi)     rw_rlock(&(ipi)->ipi_list_lock)
531ff9b006dSJulien Charbon #define INP_LIST_WLOCK(ipi)     rw_wlock(&(ipi)->ipi_list_lock)
532ff9b006dSJulien Charbon #define INP_LIST_TRY_RLOCK(ipi) rw_try_rlock(&(ipi)->ipi_list_lock)
533ff9b006dSJulien Charbon #define INP_LIST_TRY_WLOCK(ipi) rw_try_wlock(&(ipi)->ipi_list_lock)
534ff9b006dSJulien Charbon #define INP_LIST_TRY_UPGRADE(ipi)       rw_try_upgrade(&(ipi)->ipi_list_lock)
535ff9b006dSJulien Charbon #define INP_LIST_RUNLOCK(ipi)   rw_runlock(&(ipi)->ipi_list_lock)
536ff9b006dSJulien Charbon #define INP_LIST_WUNLOCK(ipi)   rw_wunlock(&(ipi)->ipi_list_lock)
537ff9b006dSJulien Charbon #define INP_LIST_LOCK_ASSERT(ipi) \
538ff9b006dSJulien Charbon 	rw_assert(&(ipi)->ipi_list_lock, RA_LOCKED)
539ff9b006dSJulien Charbon #define INP_LIST_RLOCK_ASSERT(ipi) \
540ff9b006dSJulien Charbon 	rw_assert(&(ipi)->ipi_list_lock, RA_RLOCKED)
541ff9b006dSJulien Charbon #define INP_LIST_WLOCK_ASSERT(ipi) \
542ff9b006dSJulien Charbon 	rw_assert(&(ipi)->ipi_list_lock, RA_WLOCKED)
543ff9b006dSJulien Charbon #define INP_LIST_UNLOCK_ASSERT(ipi) \
544ff9b006dSJulien Charbon 	rw_assert(&(ipi)->ipi_list_lock, RA_UNLOCKED)
545ff9b006dSJulien Charbon 
546fa046d87SRobert Watson #define	INP_HASH_LOCK_INIT(ipi, d) \
547fa046d87SRobert Watson 	rw_init_flags(&(ipi)->ipi_hash_lock, (d), 0)
548fa046d87SRobert Watson #define	INP_HASH_LOCK_DESTROY(ipi)	rw_destroy(&(ipi)->ipi_hash_lock)
549fa046d87SRobert Watson #define	INP_HASH_RLOCK(ipi)		rw_rlock(&(ipi)->ipi_hash_lock)
550fa046d87SRobert Watson #define	INP_HASH_WLOCK(ipi)		rw_wlock(&(ipi)->ipi_hash_lock)
551fa046d87SRobert Watson #define	INP_HASH_RUNLOCK(ipi)		rw_runlock(&(ipi)->ipi_hash_lock)
552fa046d87SRobert Watson #define	INP_HASH_WUNLOCK(ipi)		rw_wunlock(&(ipi)->ipi_hash_lock)
553fa046d87SRobert Watson #define	INP_HASH_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_hash_lock, \
554fa046d87SRobert Watson 					    RA_LOCKED)
555fa046d87SRobert Watson #define	INP_HASH_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_hash_lock, \
556fa046d87SRobert Watson 					    RA_WLOCKED)
557fa046d87SRobert Watson 
55852cd27cbSRobert Watson #define	INP_GROUP_LOCK_INIT(ipg, d)	mtx_init(&(ipg)->ipg_lock, (d), NULL, \
55952cd27cbSRobert Watson 					    MTX_DEF | MTX_DUPOK)
56052cd27cbSRobert Watson #define	INP_GROUP_LOCK_DESTROY(ipg)	mtx_destroy(&(ipg)->ipg_lock)
56152cd27cbSRobert Watson 
56252cd27cbSRobert Watson #define	INP_GROUP_LOCK(ipg)		mtx_lock(&(ipg)->ipg_lock)
56352cd27cbSRobert Watson #define	INP_GROUP_LOCK_ASSERT(ipg)	mtx_assert(&(ipg)->ipg_lock, MA_OWNED)
56452cd27cbSRobert Watson #define	INP_GROUP_UNLOCK(ipg)		mtx_unlock(&(ipg)->ipg_lock)
56552cd27cbSRobert Watson 
566ddd79a97SDavid Greenman #define INP_PCBHASH(faddr, lport, fport, mask) \
567c3229e05SDavid Greenman 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
568c3229e05SDavid Greenman #define INP_PCBPORTHASH(lport, mask) \
569c3229e05SDavid Greenman 	(ntohs((lport)) & (mask))
5701b44e5ffSAndrey V. Elsukov #define	INP6_PCBHASHKEY(faddr)	((faddr)->s6_addr32[3])
571ddd79a97SDavid Greenman 
572a5654bb2SRobert Watson /*
573773b573aSKip Macy  * Flags for inp_vflags -- historically version flags only
574a5654bb2SRobert Watson  */
575a5654bb2SRobert Watson #define	INP_IPV4	0x1
576a5654bb2SRobert Watson #define	INP_IPV6	0x2
577a5654bb2SRobert Watson #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
578a5654bb2SRobert Watson 
579a5654bb2SRobert Watson /*
580773b573aSKip Macy  * Flags for inp_flags.
581a5654bb2SRobert Watson  */
582ad71fe3cSRobert Watson #define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
583ad71fe3cSRobert Watson #define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
584ad71fe3cSRobert Watson #define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
585ad71fe3cSRobert Watson #define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
586ad71fe3cSRobert Watson #define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
587ad71fe3cSRobert Watson #define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
588ad71fe3cSRobert Watson #define	INP_ANONPORT		0x00000040 /* port chosen for user */
589ad71fe3cSRobert Watson #define	INP_RECVIF		0x00000080 /* receive incoming interface */
590ad71fe3cSRobert Watson #define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
591603eaf79SAlexander V. Chernikov 				   	   /* 0x000200 unused: was INP_FAITH */
592ad71fe3cSRobert Watson #define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
593ad71fe3cSRobert Watson #define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
594f44270e7SPawel Jakub Dawidek #define	INP_BINDANY		0x00001000 /* allow bind to any address */
595ad71fe3cSRobert Watson #define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
5963cca425bSMichael Tuexen #define	INP_RECVTOS		0x00004000 /* receive incoming IP TOS */
597ad71fe3cSRobert Watson #define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
598ad71fe3cSRobert Watson #define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
599ad71fe3cSRobert Watson #define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
600ad71fe3cSRobert Watson #define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
601ad71fe3cSRobert Watson #define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
602ad71fe3cSRobert Watson #define	IN6P_RTHDR		0x00100000 /* receive routing header */
603ad71fe3cSRobert Watson #define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
604ad71fe3cSRobert Watson #define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
605ad71fe3cSRobert Watson #define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
606ad71fe3cSRobert Watson #define	INP_TIMEWAIT		0x01000000 /* in TIMEWAIT, ppcb is tcptw */
607ad71fe3cSRobert Watson #define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
608ad71fe3cSRobert Watson #define	INP_DROPPED		0x04000000 /* protocol drop flag */
609ad71fe3cSRobert Watson #define	INP_SOCKREF		0x08000000 /* strong socket reference */
610c2529042SHans Petter Selasky #define	INP_RESERVED_0          0x10000000 /* reserved field */
611c2529042SHans Petter Selasky #define	INP_RESERVED_1          0x20000000 /* reserved field */
612f95d4633SHajimu UMEMOTO #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
613f95d4633SHajimu UMEMOTO #define	IN6P_MTU		0x80000000 /* receive path MTU */
61433841545SHajimu UMEMOTO 
61582c23ebaSBill Fenner #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
6163cca425bSMichael Tuexen 				 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
61733841545SHajimu UMEMOTO 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
61833841545SHajimu UMEMOTO 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
619f95d4633SHajimu UMEMOTO 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
620f95d4633SHajimu UMEMOTO 				 IN6P_MTU)
62182cd038dSYoshinobu Inoue 
622773b573aSKip Macy /*
623773b573aSKip Macy  * Flags for inp_flags2.
624773b573aSKip Macy  */
6258b12a7c2SKip Macy #define	INP_LLE_VALID		0x00000001 /* cached lle is valid */
6268b12a7c2SKip Macy #define	INP_RT_VALID		0x00000002 /* cached rtentry is valid */
62752cd27cbSRobert Watson #define	INP_PCBGROUPWILD	0x00000004 /* in pcbgroup wildcard list */
628fc06cd42SMikolaj Golub #define	INP_REUSEPORT		0x00000008 /* SO_REUSEPORT option is set */
629df4e91d3SGleb Smirnoff #define	INP_FREED		0x00000010 /* inp itself is not valid */
630efdf104bSMikolaj Golub #define	INP_REUSEADDR		0x00000020 /* SO_REUSEADDR option is set */
6310a100a6fSAdrian Chadd #define	INP_BINDMULTI		0x00000040 /* IP_BINDMULTI option is set */
6320a100a6fSAdrian Chadd #define	INP_RSS_BUCKET_SET	0x00000080 /* IP_RSS_LISTEN_BUCKET is set */
6339d3ddf43SAdrian Chadd #define	INP_RECVFLOWID		0x00000100 /* populate recv datagram with flow info */
6349d3ddf43SAdrian Chadd #define	INP_RECVRSSBUCKETID	0x00000200 /* populate recv datagram with bucket id */
635f3e7afe2SHans Petter Selasky #define	INP_RATE_LIMIT_CHANGED	0x00000400 /* rate limit needs attention */
636dce33a45SErmal Luçi #define	INP_ORIGDSTADDR		0x00000800 /* receive IP dst address/port */
637773b573aSKip Macy 
638fa046d87SRobert Watson /*
639fa046d87SRobert Watson  * Flags passed to in_pcblookup*() functions.
640fa046d87SRobert Watson  */
641fa046d87SRobert Watson #define	INPLOOKUP_WILDCARD	0x00000001	/* Allow wildcard sockets. */
642fa046d87SRobert Watson #define	INPLOOKUP_RLOCKPCB	0x00000002	/* Return inpcb read-locked. */
643fa046d87SRobert Watson #define	INPLOOKUP_WLOCKPCB	0x00000004	/* Return inpcb write-locked. */
644fa046d87SRobert Watson 
645fa046d87SRobert Watson #define	INPLOOKUP_MASK	(INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
646fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB)
647fa046d87SRobert Watson 
648df8bae1dSRodney W. Grimes #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
64982cd038dSYoshinobu Inoue #define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
65082cd038dSYoshinobu Inoue 
65182cd038dSYoshinobu Inoue #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
65282cd038dSYoshinobu Inoue 
65382cd038dSYoshinobu Inoue #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
654df8bae1dSRodney W. Grimes 
65552cd27cbSRobert Watson /*
65652cd27cbSRobert Watson  * Constants for pcbinfo.ipi_hashfields.
65752cd27cbSRobert Watson  */
65852cd27cbSRobert Watson #define	IPI_HASHFIELDS_NONE	0
65952cd27cbSRobert Watson #define	IPI_HASHFIELDS_2TUPLE	1
66052cd27cbSRobert Watson #define	IPI_HASHFIELDS_4TUPLE	2
66152cd27cbSRobert Watson 
662664a31e4SPeter Wemm #ifdef _KERNEL
663eddfbb76SRobert Watson VNET_DECLARE(int, ipport_reservedhigh);
664eddfbb76SRobert Watson VNET_DECLARE(int, ipport_reservedlow);
665eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lowfirstauto);
666eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lowlastauto);
667eddfbb76SRobert Watson VNET_DECLARE(int, ipport_firstauto);
668eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lastauto);
669eddfbb76SRobert Watson VNET_DECLARE(int, ipport_hifirstauto);
670eddfbb76SRobert Watson VNET_DECLARE(int, ipport_hilastauto);
671eddfbb76SRobert Watson VNET_DECLARE(int, ipport_randomized);
672eddfbb76SRobert Watson VNET_DECLARE(int, ipport_randomcps);
673eddfbb76SRobert Watson VNET_DECLARE(int, ipport_randomtime);
674eddfbb76SRobert Watson VNET_DECLARE(int, ipport_stoprandom);
675eddfbb76SRobert Watson VNET_DECLARE(int, ipport_tcpallocs);
676eddfbb76SRobert Watson 
6771e77c105SRobert Watson #define	V_ipport_reservedhigh	VNET(ipport_reservedhigh)
6781e77c105SRobert Watson #define	V_ipport_reservedlow	VNET(ipport_reservedlow)
6791e77c105SRobert Watson #define	V_ipport_lowfirstauto	VNET(ipport_lowfirstauto)
6801e77c105SRobert Watson #define	V_ipport_lowlastauto	VNET(ipport_lowlastauto)
6811e77c105SRobert Watson #define	V_ipport_firstauto	VNET(ipport_firstauto)
6821e77c105SRobert Watson #define	V_ipport_lastauto	VNET(ipport_lastauto)
6831e77c105SRobert Watson #define	V_ipport_hifirstauto	VNET(ipport_hifirstauto)
6841e77c105SRobert Watson #define	V_ipport_hilastauto	VNET(ipport_hilastauto)
6851e77c105SRobert Watson #define	V_ipport_randomized	VNET(ipport_randomized)
6861e77c105SRobert Watson #define	V_ipport_randomcps	VNET(ipport_randomcps)
6871e77c105SRobert Watson #define	V_ipport_randomtime	VNET(ipport_randomtime)
6881e77c105SRobert Watson #define	V_ipport_stoprandom	VNET(ipport_stoprandom)
6891e77c105SRobert Watson #define	V_ipport_tcpallocs	VNET(ipport_tcpallocs)
690eddfbb76SRobert Watson 
6919bcd427bSRobert Watson void	in_pcbinfo_destroy(struct inpcbinfo *);
6929bcd427bSRobert Watson void	in_pcbinfo_init(struct inpcbinfo *, const char *, struct inpcbhead *,
69352cd27cbSRobert Watson 	    int, int, char *, uma_init, uma_fini, uint32_t, u_int);
69452cd27cbSRobert Watson 
695d5bb8bd3SAdrian Chadd int	in_pcbbind_check_bindmulti(const struct inpcb *ni,
696d5bb8bd3SAdrian Chadd 	    const struct inpcb *oi);
697d5bb8bd3SAdrian Chadd 
69852cd27cbSRobert Watson struct inpcbgroup *
69952cd27cbSRobert Watson 	in_pcbgroup_byhash(struct inpcbinfo *, u_int, uint32_t);
70052cd27cbSRobert Watson struct inpcbgroup *
70152cd27cbSRobert Watson 	in_pcbgroup_byinpcb(struct inpcb *);
70252cd27cbSRobert Watson struct inpcbgroup *
70352cd27cbSRobert Watson 	in_pcbgroup_bytuple(struct inpcbinfo *, struct in_addr, u_short,
70452cd27cbSRobert Watson 	    struct in_addr, u_short);
70552cd27cbSRobert Watson void	in_pcbgroup_destroy(struct inpcbinfo *);
70652cd27cbSRobert Watson int	in_pcbgroup_enabled(struct inpcbinfo *);
70752cd27cbSRobert Watson void	in_pcbgroup_init(struct inpcbinfo *, u_int, int);
70852cd27cbSRobert Watson void	in_pcbgroup_remove(struct inpcb *);
70952cd27cbSRobert Watson void	in_pcbgroup_update(struct inpcb *);
71052cd27cbSRobert Watson void	in_pcbgroup_update_mbuf(struct inpcb *, struct mbuf *);
7119bcd427bSRobert Watson 
712f76fcf6dSJeffrey Hsu void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
713d915b280SStephan Uphoff int	in_pcballoc(struct socket *, struct inpcbinfo *);
714b0330ed9SPawel Jakub Dawidek int	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
7154616026fSErmal Luçi int	in_pcb_lport(struct inpcb *, struct in_addr *, u_short *,
7164616026fSErmal Luçi 	    struct ucred *, int);
7174b932371SIan Dowse int	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
718b0330ed9SPawel Jakub Dawidek 	    u_short *, struct ucred *);
719b0330ed9SPawel Jakub Dawidek int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
720d3c1f003SRobert Watson int	in_pcbconnect_mbuf(struct inpcb *, struct sockaddr *, struct ucred *,
721d3c1f003SRobert Watson 	    struct mbuf *);
7225200e00eSIan Dowse int	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
7235200e00eSIan Dowse 	    u_short *, in_addr_t *, u_short *, struct inpcb **,
724b0330ed9SPawel Jakub Dawidek 	    struct ucred *);
7254d77a549SAlfred Perlstein void	in_pcbdetach(struct inpcb *);
7264d77a549SAlfred Perlstein void	in_pcbdisconnect(struct inpcb *);
72710702a28SRobert Watson void	in_pcbdrop(struct inpcb *);
7284c7c478dSRobert Watson void	in_pcbfree(struct inpcb *);
7294d77a549SAlfred Perlstein int	in_pcbinshash(struct inpcb *);
73052cd27cbSRobert Watson int	in_pcbinshash_nopcbgroup(struct inpcb *);
731ae190832SSteven Hartland int	in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
732ae190832SSteven Hartland 	    struct ucred *);
733df8bae1dSRodney W. Grimes struct inpcb *
7344d77a549SAlfred Perlstein 	in_pcblookup_local(struct inpcbinfo *,
735078b7042SBjoern A. Zeeb 	    struct in_addr, u_short, int, struct ucred *);
73615bd2b43SDavid Greenman struct inpcb *
737fa046d87SRobert Watson 	in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
738c1cd65baSBruce Evans 	    struct in_addr, u_int, int, struct ifnet *);
739d3c1f003SRobert Watson struct inpcb *
740d3c1f003SRobert Watson 	in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
741d3c1f003SRobert Watson 	    struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
742f76fcf6dSJeffrey Hsu void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
7433ce144eaSJeffrey Hsu 	    int, struct inpcb *(*)(struct inpcb *, int));
74428696211SRobert Watson void	in_pcbref(struct inpcb *);
7454d77a549SAlfred Perlstein void	in_pcbrehash(struct inpcb *);
746d3c1f003SRobert Watson void	in_pcbrehash_mbuf(struct inpcb *, struct mbuf *);
74728696211SRobert Watson int	in_pcbrele(struct inpcb *);
74879bdc6e5SRobert Watson int	in_pcbrele_rlocked(struct inpcb *);
74979bdc6e5SRobert Watson int	in_pcbrele_wlocked(struct inpcb *);
75084cc0778SGeorge V. Neville-Neil void	in_losing(struct inpcb *);
751a557af22SRobert Watson void	in_pcbsetsolabel(struct socket *so);
75254d642bbSRobert Watson int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
75354d642bbSRobert Watson int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
75426ef6ac4SDon Lewis struct sockaddr *
75526ef6ac4SDon Lewis 	in_sockaddr(in_port_t port, struct in_addr *addr);
756a557af22SRobert Watson void	in_pcbsosetlabel(struct socket *so);
757f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
758f3e7afe2SHans Petter Selasky int	in_pcbattach_txrtlmt(struct inpcb *, struct ifnet *, uint32_t, uint32_t, uint32_t);
759f3e7afe2SHans Petter Selasky void	in_pcbdetach_txrtlmt(struct inpcb *);
760f3e7afe2SHans Petter Selasky int	in_pcbmodify_txrtlmt(struct inpcb *, uint32_t);
761f3e7afe2SHans Petter Selasky int	in_pcbquery_txrtlmt(struct inpcb *, uint32_t *);
762f3e7afe2SHans Petter Selasky void	in_pcboutput_txrtlmt(struct inpcb *, struct ifnet *, struct mbuf *);
763f3e7afe2SHans Petter Selasky void	in_pcboutput_eagain(struct inpcb *);
764f3e7afe2SHans Petter Selasky #endif
765664a31e4SPeter Wemm #endif /* _KERNEL */
7663d4d47f3SGarrett Wollman 
7678781d8e9SBruce Evans #endif /* !_NETINET_IN_PCB_H_ */
768