xref: /freebsd/sys/netinet/in_pcb.h (revision 0b4539ee542f7011160c07bf52af7f16739f4a92)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1990, 1993
5a5654bb2SRobert Watson  *	The Regents of the University of California.
682a5be49SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
7a5654bb2SRobert Watson  * All rights reserved.
8df8bae1dSRodney W. Grimes  *
982a5be49SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
1082a5be49SRobert Watson  * contract to Juniper Networks, Inc.
1182a5be49SRobert Watson  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37707f139eSPaul Richards #ifndef _NETINET_IN_PCB_H_
38707f139eSPaul Richards #define _NETINET_IN_PCB_H_
39707f139eSPaul Richards 
40f1844d4cSBruce Evans #include <sys/queue.h>
41ddece765SMatt Macy #include <sys/epoch.h>
4240545cf5SBruce Evans #include <sys/_lock.h>
4340545cf5SBruce Evans #include <sys/_mutex.h>
448501a69cSRobert Watson #include <sys/_rwlock.h>
45fdb987beSMark Johnston #include <sys/_smr.h>
4684cc0778SGeorge V. Neville-Neil #include <net/route.h>
47f1844d4cSBruce Evans 
488501a69cSRobert Watson #ifdef _KERNEL
491417604eSBjoern A. Zeeb #include <sys/lock.h>
50db0ac6deSCy Schubert #include <sys/proc.h>
518501a69cSRobert Watson #include <sys/rwlock.h>
52a35bdd44SMichael Tuexen #include <sys/sysctl.h>
53eddfbb76SRobert Watson #include <net/vnet.h>
549bcd427bSRobert Watson #include <vm/uma.h>
558501a69cSRobert Watson #endif
56b872626dSMatt Macy #include <sys/ck.h>
578501a69cSRobert Watson 
58df8bae1dSRodney W. Grimes /*
59fc18af96SRobert Watson  * struct inpcb is the common protocol control block structure used in most
60fc18af96SRobert Watson  * IP transport protocols.
61712fc218SRobert Watson  *
62712fc218SRobert Watson  * Pointers to local and foreign host table entries, local and foreign socket
63712fc218SRobert Watson  * numbers, and pointers up (to a socket structure) and down (to a
64712fc218SRobert Watson  * protocol-specific control block) are stored here.
65df8bae1dSRodney W. Grimes  */
66b872626dSMatt Macy CK_LIST_HEAD(inpcbhead, inpcb);
67b872626dSMatt Macy CK_LIST_HEAD(inpcbporthead, inpcbport);
6854af3d0dSMark Johnston CK_LIST_HEAD(inpcblbgrouphead, inpcblbgroup);
69cc65eb4eSGleb Smirnoff typedef	uint64_t	inp_gen_t;
7015bd2b43SDavid Greenman 
713d4d47f3SGarrett Wollman /*
7282cd038dSYoshinobu Inoue  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
73712fc218SRobert Watson  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
74a0577692SGleb Smirnoff  * the following structure.  This requires padding always be zeroed out,
75a0577692SGleb Smirnoff  * which is done right after inpcb allocation and stays through its lifetime.
7682cd038dSYoshinobu Inoue  */
7782cd038dSYoshinobu Inoue struct in_addr_4in6 {
7882cd038dSYoshinobu Inoue 	u_int32_t	ia46_pad32[3];
7982cd038dSYoshinobu Inoue 	struct	in_addr	ia46_addr4;
8082cd038dSYoshinobu Inoue };
8182cd038dSYoshinobu Inoue 
821a43cff9SSean Bruno union in_dependaddr {
831a43cff9SSean Bruno 	struct in_addr_4in6 id46_addr;
841a43cff9SSean Bruno 	struct in6_addr	id6_addr;
851a43cff9SSean Bruno };
861a43cff9SSean Bruno 
8782cd038dSYoshinobu Inoue /*
88712fc218SRobert Watson  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
89712fc218SRobert Watson  * some extra padding to accomplish this.
90388909a1SGleb Smirnoff  * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport,
91388909a1SGleb Smirnoff  * lport, faddr to generate hash, so these fields shouldn't be moved.
92be2ac88cSJonathan Lemon  */
93be2ac88cSJonathan Lemon struct in_endpoints {
94be2ac88cSJonathan Lemon 	u_int16_t	ie_fport;		/* foreign port */
95be2ac88cSJonathan Lemon 	u_int16_t	ie_lport;		/* local port */
96be2ac88cSJonathan Lemon 	/* protocol dependent part, local and foreign addr */
971a43cff9SSean Bruno 	union in_dependaddr ie_dependfaddr;	/* foreign host table entry */
981a43cff9SSean Bruno 	union in_dependaddr ie_dependladdr;	/* local host table entry */
991a43cff9SSean Bruno #define	ie_faddr	ie_dependfaddr.id46_addr.ia46_addr4
1001a43cff9SSean Bruno #define	ie_laddr	ie_dependladdr.id46_addr.ia46_addr4
1011a43cff9SSean Bruno #define	ie6_faddr	ie_dependfaddr.id6_addr
1021a43cff9SSean Bruno #define	ie6_laddr	ie_dependladdr.id6_addr
103028bdf28SAndrey V. Elsukov 	u_int32_t	ie6_zoneid;		/* scope zone id */
104a5654bb2SRobert Watson };
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
1225dff1c38SMichael Tuexen #define	INC_IPV6MINMTU	0x02
123dcdb4371SBjoern A. Zeeb 
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
1361a8d1764SGleb Smirnoff  * located in a larger protocol specific structure that embeds inpcb in it.
1371a8d1764SGleb Smirnoff  * Almost all fields of struct inpcb are static after creation or protected by
1381a8d1764SGleb Smirnoff  * a per-inpcb rwlock, inp_lock.
139ff9b006dSJulien Charbon  *
140db0ac6deSCy Schubert  * A inpcb database is indexed by addresses/ports hash as well as list of
141db0ac6deSCy Schubert  * all pcbs that belong to a certain proto. Database lookups or list traversals
142db0ac6deSCy Schubert  * are be performed inside SMR section. Once desired PCB is found its own
143db0ac6deSCy Schubert  * lock is to be obtained and SMR section exited.
1447b709f8aSRobert Watson  *
1457b709f8aSRobert Watson  * Key:
1467b709f8aSRobert Watson  * (c) - Constant after initialization
147db0ac6deSCy Schubert  * (e) - Protected by the SMR section
1487b709f8aSRobert Watson  * (i) - Protected by the inpcb lock
1497b709f8aSRobert Watson  * (p) - Protected by the pcbinfo lock for the inpcb
150ff9b006dSJulien Charbon  * (h) - Protected by the pcbhash lock for the inpcb
1517b709f8aSRobert Watson  * (s) - Protected by another subsystem's locks
1527b709f8aSRobert Watson  * (x) - Undefined locking
1537b709f8aSRobert Watson  *
1547b709f8aSRobert Watson  * A few other notes:
1557b709f8aSRobert Watson  *
1567b709f8aSRobert Watson  * When a read lock is held, stability of the field is guaranteed; to write
1577b709f8aSRobert Watson  * to a field, a write lock must generally be held.
1587b709f8aSRobert Watson  *
1597b709f8aSRobert Watson  * netinet/netinet6-layer code should not assume that the inp_socket pointer
16053af6903SGleb Smirnoff  * is safe to dereference without inp_lock being held, there may be
16153af6903SGleb Smirnoff  * close(2)-related races.
1627b709f8aSRobert Watson  *
1637b709f8aSRobert Watson  * The inp_vflag field is overloaded, and would otherwise ideally be (c).
1647b709f8aSRobert Watson  */
165cc65eb4eSGleb Smirnoff struct icmp6_filter;
166cc65eb4eSGleb Smirnoff struct inpcbpolicy;
167f3e7afe2SHans Petter Selasky struct m_snd_tag;
168df8bae1dSRodney W. Grimes struct inpcb {
1696a6cefacSGleb Smirnoff 	/* Cache line #1 (amd64) */
170fdb987beSMark Johnston 	CK_LIST_ENTRY(inpcb) inp_hash_exact;	/* hash table linkage */
171fdb987beSMark Johnston 	CK_LIST_ENTRY(inpcb) inp_hash_wild;	/* hash table linkage */
1726a6cefacSGleb Smirnoff 	struct rwlock	inp_lock;
1736a6cefacSGleb Smirnoff 	/* Cache line #2 (amd64) */
174c2a69e84SGleb Smirnoff #define	inp_start_zero	inp_refcount
1756a6cefacSGleb Smirnoff #define	inp_zero_size	(sizeof(struct inpcb) - \
1766a6cefacSGleb Smirnoff 			    offsetof(struct inpcb, inp_start_zero))
1776a6cefacSGleb Smirnoff 	u_int	inp_refcount;		/* (i) refcount */
1786a6cefacSGleb Smirnoff 	int	inp_flags;		/* (i) generic IP/datagram flags */
1796a6cefacSGleb Smirnoff 	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
18050575ce1SAndrew Gallatin 	uint8_t inp_numa_domain;	/* numa domain */
1816a6cefacSGleb Smirnoff 	struct	socket *inp_socket;	/* (i) back pointer to socket */
1827b709f8aSRobert Watson 	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
18386d02c5cSBjoern A. Zeeb 	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
1847b709f8aSRobert Watson 	u_int32_t inp_flow;		/* (i) IPv6 flow information */
1857b709f8aSRobert Watson 	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
1867b709f8aSRobert Watson 	u_char	inp_ip_ttl;		/* (i) time to live proto */
1877b709f8aSRobert Watson 	u_char	inp_ip_p;		/* (c) protocol proto */
1887b709f8aSRobert Watson 	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
18980cb9f21SKip Macy 	uint32_t inp_flowid;		/* (x) flow id / queue id */
190fdb987beSMark Johnston 	smr_seq_t inp_smr;		/* (i) sequence number at disconnect */
191f3e7afe2SHans Petter Selasky 	struct m_snd_tag *inp_snd_tag;	/* (i) send tag for outgoing mbufs */
19234e3dcedSAdrian Chadd 	uint32_t inp_flowtype;		/* (x) M_HASHTYPE value */
1932de2af32SKip Macy 
1942de2af32SKip Macy 	/* Local and foreign ports, local and foreign addr. */
195fdb987beSMark Johnston 	struct	in_conninfo inp_inc;	/* (i,h) list for PCB's local port */
1962de2af32SKip Macy 
197a5654bb2SRobert Watson 	/* MAC and IPSEC policy information. */
1987b709f8aSRobert Watson 	struct	label *inp_label;	/* (i) MAC label */
1997b709f8aSRobert Watson 	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
20082cd038dSYoshinobu Inoue 
201712fc218SRobert Watson 	/* Protocol-dependent part; options. */
20282cd038dSYoshinobu Inoue 	struct {
203cc65eb4eSGleb Smirnoff 		u_char	inp_ip_tos;		/* (i) type of service proto */
204cc65eb4eSGleb Smirnoff 		struct mbuf		*inp_options;	/* (i) IP options */
205cc65eb4eSGleb Smirnoff 		struct ip_moptions	*inp_moptions;	/* (i) mcast options */
206cc65eb4eSGleb Smirnoff 	};
20782cd038dSYoshinobu Inoue 	struct {
2087b709f8aSRobert Watson 		/* (i) IP options */
209cc65eb4eSGleb Smirnoff 		struct mbuf		*in6p_options;
2107b709f8aSRobert Watson 		/* (i) IP6 options for outgoing packets */
211cc65eb4eSGleb Smirnoff 		struct ip6_pktopts	*in6p_outputopts;
2127b709f8aSRobert Watson 		/* (i) IP multicast options */
213cc65eb4eSGleb Smirnoff 		struct ip6_moptions	*in6p_moptions;
2147b709f8aSRobert Watson 		/* (i) ICMPv6 code type filter */
215cc65eb4eSGleb Smirnoff 		struct icmp6_filter	*in6p_icmp6filt;
2167b709f8aSRobert Watson 		/* (i) IPV6_CHECKSUM setsockopt */
217cc65eb4eSGleb Smirnoff 		int	in6p_cksum;
218cc65eb4eSGleb Smirnoff 		short	in6p_hops;
219cc65eb4eSGleb Smirnoff 	};
220db0ac6deSCy Schubert 	CK_LIST_ENTRY(inpcb) inp_portlist;	/* (r:e/w:h) port list */
221db0ac6deSCy Schubert 	struct	inpcbport *inp_phd;	/* (r:e/w:h) head of this list */
222a5654bb2SRobert Watson 	inp_gen_t	inp_gencnt;	/* (c) generation count */
223113c4fadSBjoern A. Zeeb 	void		*spare_ptr;	/* Spare pointer. */
22484cc0778SGeorge V. Neville-Neil 	rt_gen_t	inp_rt_cookie;	/* generation for route entry */
22584cc0778SGeorge V. Neville-Neil 	union {				/* cached L3 information */
2266a6cefacSGleb Smirnoff 		struct route inp_route;
2276a6cefacSGleb Smirnoff 		struct route_in6 inp_route6;
2286a6cefacSGleb Smirnoff 	};
229db0ac6deSCy Schubert 	CK_LIST_ENTRY(inpcb) inp_list;	/* (r:e/w:p) all PCBs for proto */
230a5654bb2SRobert Watson };
231cc65eb4eSGleb Smirnoff #endif	/* _KERNEL */
232cc65eb4eSGleb Smirnoff 
233a5654bb2SRobert Watson #define	inp_fport	inp_inc.inc_fport
234a5654bb2SRobert Watson #define	inp_lport	inp_inc.inc_lport
235a5654bb2SRobert Watson #define	inp_faddr	inp_inc.inc_faddr
236a5654bb2SRobert Watson #define	inp_laddr	inp_inc.inc_laddr
237f76fcf6dSJeffrey Hsu 
238be2ac88cSJonathan Lemon #define	in6p_faddr	inp_inc.inc6_faddr
239be2ac88cSJonathan Lemon #define	in6p_laddr	inp_inc.inc6_laddr
240028bdf28SAndrey V. Elsukov #define	in6p_zoneid	inp_inc.inc6_zoneid
241a5654bb2SRobert Watson 
242f6dfe47aSMarko Zec #define	inp_vnet	inp_pcbinfo->ipi_vnet
243f6dfe47aSMarko Zec 
2443d4d47f3SGarrett Wollman /*
245712fc218SRobert Watson  * The range of the generation count, as used in this implementation, is 9e19.
246712fc218SRobert Watson  * We would have to create 300 billion connections per second for this number
247712fc218SRobert Watson  * to roll over in a year.  This seems sufficiently unlikely that we simply
248712fc218SRobert Watson  * don't concern ourselves with that possibility.
2493d4d47f3SGarrett Wollman  */
250c3229e05SDavid Greenman 
25198271db4SGarrett Wollman /*
252712fc218SRobert Watson  * Interface exported to userland by various protocols which use inpcbs.  Hack
253712fc218SRobert Watson  * alert -- only define if struct xsocket is in scope.
254cc65eb4eSGleb Smirnoff  * Fields prefixed with "xi_" are unique to this structure, and the rest
255cc65eb4eSGleb Smirnoff  * match fields in the struct inpcb, to ease coding and porting.
256cc65eb4eSGleb Smirnoff  *
257cc65eb4eSGleb Smirnoff  * Legend:
258cc65eb4eSGleb Smirnoff  * (s) - used by userland utilities in src
259cc65eb4eSGleb Smirnoff  * (p) - used by utilities in ports
260cc65eb4eSGleb Smirnoff  * (3) - is known to be used by third party software not in ports
261cc65eb4eSGleb Smirnoff  * (n) - no known usage
26298271db4SGarrett Wollman  */
26398271db4SGarrett Wollman #ifdef _SYS_SOCKETVAR_H_
26498271db4SGarrett Wollman struct xinpcb {
265f38b68aeSBrooks Davis 	ksize_t		xi_len;			/* length of this structure */
266cc65eb4eSGleb Smirnoff 	struct xsocket	xi_socket;		/* (s,p) */
267cc65eb4eSGleb Smirnoff 	struct in_conninfo inp_inc;		/* (s,p) */
268cc65eb4eSGleb Smirnoff 	uint64_t	inp_gencnt;		/* (s,p) */
2691a8d1764SGleb Smirnoff 	int64_t		inp_spare64[5];
270cc65eb4eSGleb Smirnoff 	uint32_t	inp_flow;		/* (s) */
271cc65eb4eSGleb Smirnoff 	uint32_t	inp_flowid;		/* (s) */
272cc65eb4eSGleb Smirnoff 	uint32_t	inp_flowtype;		/* (s) */
273cc65eb4eSGleb Smirnoff 	int32_t		inp_flags;		/* (s,p) */
274cc65eb4eSGleb Smirnoff 	int32_t		inp_flags2;		/* (s) */
275317fa516SMark Johnston 	uint32_t	inp_unused;
276cc65eb4eSGleb Smirnoff 	int32_t		in6p_cksum;		/* (n) */
277cc65eb4eSGleb Smirnoff 	int32_t		inp_spare32[4];
278cc65eb4eSGleb Smirnoff 	uint16_t	in6p_hops;		/* (n) */
279cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_tos;		/* (n) */
280cc65eb4eSGleb Smirnoff 	int8_t		pad8;
281cc65eb4eSGleb Smirnoff 	uint8_t		inp_vflag;		/* (s,p) */
282cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_ttl;		/* (n) */
283cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_p;		/* (n) */
284cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_minttl;		/* (n) */
285cc65eb4eSGleb Smirnoff 	int8_t		inp_spare8[4];
286cc65eb4eSGleb Smirnoff } __aligned(8);
28798271db4SGarrett Wollman 
28898271db4SGarrett Wollman struct xinpgen {
289f38b68aeSBrooks Davis 	ksize_t	xig_len;	/* length of this structure */
29098271db4SGarrett Wollman 	u_int		xig_count;	/* number of PCBs at this time */
291f38b68aeSBrooks Davis 	uint32_t	_xig_spare32;
29298271db4SGarrett Wollman 	inp_gen_t	xig_gen;	/* generation count at this time */
293cc65eb4eSGleb Smirnoff 	so_gen_t	xig_sogen;	/* socket generation count this time */
294f38b68aeSBrooks Davis 	uint64_t	_xig_spare64[4];
2953ae4b0e7SGleb Smirnoff } __aligned(8);
296a35bdd44SMichael Tuexen 
297a35bdd44SMichael Tuexen struct sockopt_parameters {
298a35bdd44SMichael Tuexen 	struct in_conninfo sop_inc;
299a35bdd44SMichael Tuexen 	uint64_t sop_id;
300a35bdd44SMichael Tuexen 	int sop_level;
301a35bdd44SMichael Tuexen 	int sop_optname;
302a35bdd44SMichael Tuexen 	char sop_optval[];
303a35bdd44SMichael Tuexen };
304a35bdd44SMichael Tuexen 
305cc65eb4eSGleb Smirnoff #ifdef	_KERNEL
306a35bdd44SMichael Tuexen int	sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo,
307a35bdd44SMichael Tuexen 	    int (*ctloutput_set)(struct inpcb *, struct sockopt *));
308cc65eb4eSGleb Smirnoff void	in_pcbtoxinpcb(const struct inpcb *, struct xinpcb *);
309cc65eb4eSGleb Smirnoff #endif
31098271db4SGarrett Wollman #endif /* _SYS_SOCKETVAR_H_ */
31198271db4SGarrett Wollman 
312db0ac6deSCy Schubert #ifdef _KERNEL
313db0ac6deSCy Schubert /*
314fec8a8c7SGleb Smirnoff  * Per-VNET pcb database for each high-level protocol (UDP, TCP, ...) in both
315fec8a8c7SGleb Smirnoff  * IPv4 and IPv6.
31682a5be49SRobert Watson  *
317db0ac6deSCy Schubert  * The pcbs are protected with SMR section and thus all lists in inpcbinfo
318db0ac6deSCy Schubert  * are CK-lists.  Locking is required to insert a pcb into database. Two
319db0ac6deSCy Schubert  * locks are provided: one for the hash and one for the global list of pcbs,
320db0ac6deSCy Schubert  * as well as overall count and generation count.
32182a5be49SRobert Watson  *
32282a5be49SRobert Watson  * Locking key:
32382a5be49SRobert Watson  *
32482a5be49SRobert Watson  * (c) Constant or nearly constant after initialisation
325db0ac6deSCy Schubert  * (e) Protected by SMR section
32682a5be49SRobert Watson  * (g) Locked by ipi_lock
327db0ac6deSCy Schubert  * (h) Locked by ipi_hash_lock
328712fc218SRobert Watson  */
329712fc218SRobert Watson struct inpcbinfo {
330712fc218SRobert Watson 	/*
3319e58ff6fSMatt Macy 	 * Global lock protecting inpcb list modification
33282a5be49SRobert Watson 	 */
3339e58ff6fSMatt Macy 	struct mtx		 ipi_lock;
334db0ac6deSCy Schubert 	struct inpcbhead	 ipi_listhead;		/* (r:e/w:g) */
335db0ac6deSCy Schubert 	u_int			 ipi_count;		/* (g) */
336712fc218SRobert Watson 
337712fc218SRobert Watson 	/*
338712fc218SRobert Watson 	 * Generation count -- incremented each time a connection is allocated
339712fc218SRobert Watson 	 * or freed.
340712fc218SRobert Watson 	 */
341db0ac6deSCy Schubert 	u_quad_t		 ipi_gencnt;		/* (g) */
34282a5be49SRobert Watson 
34382a5be49SRobert Watson 	/*
34482a5be49SRobert Watson 	 * Fields associated with port lookup and allocation.
34582a5be49SRobert Watson 	 */
346db0ac6deSCy Schubert 	u_short			 ipi_lastport;		/* (h) */
347db0ac6deSCy Schubert 	u_short			 ipi_lastlow;		/* (h) */
348db0ac6deSCy Schubert 	u_short			 ipi_lasthi;		/* (h) */
34982a5be49SRobert Watson 
35082a5be49SRobert Watson 	/*
35182a5be49SRobert Watson 	 * UMA zone from which inpcbs are allocated for this protocol.
35282a5be49SRobert Watson 	 */
353db0ac6deSCy Schubert 	uma_zone_t		 ipi_zone;		/* (c) */
354db0ac6deSCy Schubert 	uma_zone_t		 ipi_portzone;		/* (c) */
355db0ac6deSCy Schubert 	smr_t			 ipi_smr;		/* (c) */
356fa046d87SRobert Watson 
357fa046d87SRobert Watson 	/*
35882a5be49SRobert Watson 	 * Global hash of inpcbs, hashed by local and foreign addresses and
359fdb987beSMark Johnston 	 * port numbers.  The "exact" hash holds PCBs connected to a foreign
360fdb987beSMark Johnston 	 * address, and "wild" holds the rest.
36182a5be49SRobert Watson 	 */
362db0ac6deSCy Schubert 	struct mtx		 ipi_hash_lock;
363fdb987beSMark Johnston 	struct inpcbhead 	*ipi_hash_exact;	/* (r:e/w:h) */
364fdb987beSMark Johnston 	struct inpcbhead 	*ipi_hash_wild;		/* (r:e/w:h) */
365db0ac6deSCy Schubert 	u_long			 ipi_hashmask;		/* (c) */
36682a5be49SRobert Watson 
36782a5be49SRobert Watson 	/*
36882a5be49SRobert Watson 	 * Global hash of inpcbs, hashed by only local port number.
36982a5be49SRobert Watson 	 */
370fa046d87SRobert Watson 	struct inpcbporthead	*ipi_porthashbase;	/* (h) */
371fa046d87SRobert Watson 	u_long			 ipi_porthashmask;	/* (h) */
3722de2af32SKip Macy 
3732de2af32SKip Macy 	/*
3741a43cff9SSean Bruno 	 * Load balance groups used for the SO_REUSEPORT_LB option,
3751a43cff9SSean Bruno 	 * hashed by local port.
3761a43cff9SSean Bruno 	 */
377db0ac6deSCy Schubert 	struct	inpcblbgrouphead *ipi_lbgrouphashbase;	/* (r:e/w:h) */
3781a43cff9SSean Bruno 	u_long			 ipi_lbgrouphashmask;	/* (h) */
3791a43cff9SSean Bruno 
3801a43cff9SSean Bruno 	/*
381f6dfe47aSMarko Zec 	 * Pointer to network stack instance
382f6dfe47aSMarko Zec 	 */
38382a5be49SRobert Watson 	struct vnet		*ipi_vnet;		/* (c) */
38415bd2b43SDavid Greenman };
38515bd2b43SDavid Greenman 
38652cd27cbSRobert Watson /*
387fec8a8c7SGleb Smirnoff  * Global allocation storage for each high-level protocol (UDP, TCP, ...).
388fec8a8c7SGleb Smirnoff  * Each corresponding per-VNET inpcbinfo points into this one.
389fec8a8c7SGleb Smirnoff  */
390fec8a8c7SGleb Smirnoff struct inpcbstorage {
391fec8a8c7SGleb Smirnoff 	uma_zone_t	ips_zone;
392fec8a8c7SGleb Smirnoff 	uma_zone_t	ips_portzone;
393fec8a8c7SGleb Smirnoff 	uma_init	ips_pcbinit;
3940aa120d5SGleb Smirnoff 	size_t		ips_size;
395fec8a8c7SGleb Smirnoff 	const char *	ips_zone_name;
396fec8a8c7SGleb Smirnoff 	const char *	ips_portzone_name;
397fec8a8c7SGleb Smirnoff 	const char *	ips_infolock_name;
398fec8a8c7SGleb Smirnoff 	const char *	ips_hashlock_name;
399fec8a8c7SGleb Smirnoff };
400fec8a8c7SGleb Smirnoff 
4010aa120d5SGleb Smirnoff #define INPCBSTORAGE_DEFINE(prot, ppcb, lname, zname, iname, hname)	\
402fec8a8c7SGleb Smirnoff static int								\
403fec8a8c7SGleb Smirnoff prot##_inpcb_init(void *mem, int size __unused, int flags __unused)	\
404fec8a8c7SGleb Smirnoff {									\
405fec8a8c7SGleb Smirnoff 	struct inpcb *inp = mem;					\
406fec8a8c7SGleb Smirnoff 									\
407fec8a8c7SGleb Smirnoff 	rw_init_flags(&inp->inp_lock, lname, RW_RECURSE | RW_DUPOK);	\
408fec8a8c7SGleb Smirnoff 	return (0);							\
409fec8a8c7SGleb Smirnoff }									\
410fec8a8c7SGleb Smirnoff static struct inpcbstorage prot = {					\
4110aa120d5SGleb Smirnoff 	.ips_size = sizeof(struct ppcb),				\
412fec8a8c7SGleb Smirnoff 	.ips_pcbinit = prot##_inpcb_init,				\
413fec8a8c7SGleb Smirnoff 	.ips_zone_name = zname,						\
414fec8a8c7SGleb Smirnoff 	.ips_portzone_name = zname " ports",				\
415fec8a8c7SGleb Smirnoff 	.ips_infolock_name = iname,					\
416fec8a8c7SGleb Smirnoff 	.ips_hashlock_name = hname,					\
417fec8a8c7SGleb Smirnoff };									\
418fec8a8c7SGleb Smirnoff SYSINIT(prot##_inpcbstorage_init, SI_SUB_PROTO_DOMAIN,			\
419fec8a8c7SGleb Smirnoff     SI_ORDER_SECOND, in_pcbstorage_init, &prot);			\
420fec8a8c7SGleb Smirnoff SYSUNINIT(prot##_inpcbstorage_uninit, SI_SUB_PROTO_DOMAIN,		\
421fec8a8c7SGleb Smirnoff     SI_ORDER_SECOND, in_pcbstorage_destroy, &prot)
422fec8a8c7SGleb Smirnoff 
423fec8a8c7SGleb Smirnoff /*
4241a43cff9SSean Bruno  * Load balance groups used for the SO_REUSEPORT_LB socket option. Each group
4251a43cff9SSean Bruno  * (or unique address:port combination) can be re-used at most
4261a43cff9SSean Bruno  * INPCBLBGROUP_SIZMAX (256) times. The inpcbs are stored in il_inp which
4271a43cff9SSean Bruno  * is dynamically resized as processes bind/unbind to that specific group.
4281a43cff9SSean Bruno  */
4291a43cff9SSean Bruno struct inpcblbgroup {
43054af3d0dSMark Johnston 	CK_LIST_ENTRY(inpcblbgroup) il_list;
43154af3d0dSMark Johnston 	struct epoch_context il_epoch_ctx;
432d93ec8cbSMark Johnston 	struct ucred	*il_cred;
4331a43cff9SSean Bruno 	uint16_t	il_lport;			/* (c) */
4341a43cff9SSean Bruno 	u_char		il_vflag;			/* (c) */
435d93ec8cbSMark Johnston 	uint8_t		il_numa_domain;
4361a43cff9SSean Bruno 	uint32_t	il_pad2;
4371a43cff9SSean Bruno 	union in_dependaddr il_dependladdr;		/* (c) */
4381a43cff9SSean Bruno #define	il_laddr	il_dependladdr.id46_addr.ia46_addr4
4391a43cff9SSean Bruno #define	il6_laddr	il_dependladdr.id6_addr
4401a43cff9SSean Bruno 	uint32_t	il_inpsiz; /* max count in il_inp[] (h) */
4411a43cff9SSean Bruno 	uint32_t	il_inpcnt; /* cur count in il_inp[] (h) */
4421a43cff9SSean Bruno 	struct inpcb	*il_inp[];			/* (h) */
4431a43cff9SSean Bruno };
4441a43cff9SSean Bruno 
4458501a69cSRobert Watson #define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
4468501a69cSRobert Watson #define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
4478501a69cSRobert Watson #define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
44843cc0bc1SRobert Watson #define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
44943cc0bc1SRobert Watson #define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
4508501a69cSRobert Watson #define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
4518501a69cSRobert Watson #define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
4525a126433SGleb Smirnoff #define INP_UNLOCK(inp)		rw_unlock(&(inp)->inp_lock)
45382c33e73SKip Macy #define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
45482c33e73SKip Macy #define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
45582c33e73SKip Macy #define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
4568501a69cSRobert Watson #define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
4578501a69cSRobert Watson #define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
4588501a69cSRobert Watson #define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
4598501a69cSRobert Watson #define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
460f76fcf6dSJeffrey Hsu 
4613d585327SKip Macy /*
4623d585327SKip Macy  * These locking functions are for inpcb consumers outside of sys/netinet,
4633d585327SKip Macy  * more specifically, they were added for the benefit of TOE drivers. The
4643d585327SKip Macy  * macros are reserved for use by the stack.
4653d585327SKip Macy  */
4663d585327SKip Macy void inp_wlock(struct inpcb *);
4673d585327SKip Macy void inp_wunlock(struct inpcb *);
4683d585327SKip Macy void inp_rlock(struct inpcb *);
4693d585327SKip Macy void inp_runlock(struct inpcb *);
4703d585327SKip Macy 
471c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
472e79dd20dSKip Macy void inp_lock_assert(struct inpcb *);
473e79dd20dSKip Macy void inp_unlock_assert(struct inpcb *);
4743d585327SKip Macy #else
475c75e2666SGleb Smirnoff #define	inp_lock_assert(inp)	do {} while (0)
476c75e2666SGleb Smirnoff #define	inp_unlock_assert(inp)	do {} while (0)
4773d585327SKip Macy #endif
4789378e437SKip Macy 
47924cf7a8dSGleb Smirnoff void	inp_apply_all(struct inpcbinfo *, void (*func)(struct inpcb *, void *),
48024cf7a8dSGleb Smirnoff 	    void *arg);
4819d29c635SKip Macy struct socket *
4829d29c635SKip Macy 	inp_inpcbtosocket(struct inpcb *inp);
483df9cf830STai-hwa Liang void 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
4849d29c635SKip Macy 		uint32_t *faddr, uint16_t *fp);
4859378e437SKip Macy 
486e79dd20dSKip Macy #endif /* _KERNEL */
4873d585327SKip Macy 
4889e58ff6fSMatt Macy #define INP_INFO_WLOCK(ipi)	mtx_lock(&(ipi)->ipi_lock)
4899e58ff6fSMatt Macy #define INP_INFO_WLOCKED(ipi)	mtx_owned(&(ipi)->ipi_lock)
4909e58ff6fSMatt Macy #define INP_INFO_WUNLOCK(ipi)	mtx_unlock(&(ipi)->ipi_lock)
491db0ac6deSCy Schubert #define	INP_INFO_LOCK_ASSERT(ipi)	MPASS(SMR_ENTERED((ipi)->ipi_smr) || \
492db0ac6deSCy Schubert 					mtx_owned(&(ipi)->ipi_lock))
4939e58ff6fSMatt Macy #define INP_INFO_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_lock, MA_OWNED)
494384a5c3cSAndrey V. Elsukov #define INP_INFO_WUNLOCK_ASSERT(ipi)	\
495384a5c3cSAndrey V. Elsukov 				mtx_assert(&(ipi)->ipi_lock, MA_NOTOWNED)
496f76fcf6dSJeffrey Hsu 
4979e58ff6fSMatt Macy #define	INP_HASH_WLOCK(ipi)		mtx_lock(&(ipi)->ipi_hash_lock)
4989e58ff6fSMatt Macy #define	INP_HASH_WUNLOCK(ipi)		mtx_unlock(&(ipi)->ipi_hash_lock)
499db0ac6deSCy Schubert #define	INP_HASH_LOCK_ASSERT(ipi)	MPASS(SMR_ENTERED((ipi)->ipi_smr) || \
500db0ac6deSCy Schubert 					mtx_owned(&(ipi)->ipi_hash_lock))
501db0ac6deSCy Schubert #define	INP_HASH_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_hash_lock, \
502db0ac6deSCy Schubert 					MA_OWNED)
50352cd27cbSRobert Watson 
504a0577692SGleb Smirnoff /*
505a0577692SGleb Smirnoff  * Wildcard matching hash is not just a microoptimisation!  The hash for
506a0577692SGleb Smirnoff  * wildcard IPv4 and wildcard IPv6 must be the same, otherwise AF_INET6
507a0577692SGleb Smirnoff  * wildcard bound pcb won't be able to receive AF_INET connections, while:
508a0577692SGleb Smirnoff  * jenkins_hash(&zeroes, 1, s) != jenkins_hash(&zeroes, 4, s)
509a0577692SGleb Smirnoff  * See also comment above struct in_addr_4in6.
510a0577692SGleb Smirnoff  */
511a0577692SGleb Smirnoff #define	IN_ADDR_JHASH32(addr)						\
512a0577692SGleb Smirnoff 	((addr)->s_addr == INADDR_ANY ? V_in_pcbhashseed :		\
513a0577692SGleb Smirnoff 	    jenkins_hash32((&(addr)->s_addr), 1, V_in_pcbhashseed))
514a0577692SGleb Smirnoff #define	IN6_ADDR_JHASH32(addr)						\
515a0577692SGleb Smirnoff 	(memcmp((addr), &in6addr_any, sizeof(in6addr_any)) == 0 ?	\
516a0577692SGleb Smirnoff 	    V_in_pcbhashseed :						\
517a0577692SGleb Smirnoff 	    jenkins_hash32((addr)->__u6_addr.__u6_addr32,		\
518a0577692SGleb Smirnoff 	    nitems((addr)->__u6_addr.__u6_addr32), V_in_pcbhashseed))
519a0577692SGleb Smirnoff 
520ddd79a97SDavid Greenman #define INP_PCBHASH(faddr, lport, fport, mask)				\
521a0577692SGleb Smirnoff 	((IN_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) & (mask))
522a0577692SGleb Smirnoff #define	INP6_PCBHASH(faddr, lport, fport, mask)				\
523a0577692SGleb Smirnoff 	((IN6_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) & (mask))
524a0577692SGleb Smirnoff 
525a0577692SGleb Smirnoff #define	INP_PCBHASH_WILD(lport, mask)					\
526a0577692SGleb Smirnoff 	((V_in_pcbhashseed ^ ntohs(lport)) & (mask))
527a0577692SGleb Smirnoff 
5281a43cff9SSean Bruno #define	INP_PCBLBGROUP_PKTHASH(faddr, lport, fport)			\
529a0577692SGleb Smirnoff 	(IN_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport)))
530a0577692SGleb Smirnoff #define	INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport)			\
531a0577692SGleb Smirnoff 	(IN6_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport)))
532a0577692SGleb Smirnoff 
533a0577692SGleb Smirnoff #define INP_PCBPORTHASH(lport, mask)	(ntohs((lport)) & (mask))
534ddd79a97SDavid Greenman 
535a5654bb2SRobert Watson /*
536773b573aSKip Macy  * Flags for inp_vflags -- historically version flags only
537a5654bb2SRobert Watson  */
538a5654bb2SRobert Watson #define	INP_IPV4	0x1
539a5654bb2SRobert Watson #define	INP_IPV6	0x2
540a5654bb2SRobert Watson #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
541a5654bb2SRobert Watson 
542a5654bb2SRobert Watson /*
543773b573aSKip Macy  * Flags for inp_flags.
544a5654bb2SRobert Watson  */
545ad71fe3cSRobert Watson #define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
546ad71fe3cSRobert Watson #define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
547ad71fe3cSRobert Watson #define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
548ad71fe3cSRobert Watson #define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
549ad71fe3cSRobert Watson #define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
550ad71fe3cSRobert Watson #define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
55109d3671bSGleb Smirnoff #define	INP_ANONPORT		0x00000040 /* read by netstat(1) */
552ad71fe3cSRobert Watson #define	INP_RECVIF		0x00000080 /* receive incoming interface */
553ad71fe3cSRobert Watson #define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
554db0ac6deSCy Schubert /*	INP_FREED		0x00000200 private to in_pcb.c */
555ad71fe3cSRobert Watson #define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
556ad71fe3cSRobert Watson #define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
557f44270e7SPawel Jakub Dawidek #define	INP_BINDANY		0x00001000 /* allow bind to any address */
558ad71fe3cSRobert Watson #define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
5593cca425bSMichael Tuexen #define	INP_RECVTOS		0x00004000 /* receive incoming IP TOS */
560ad71fe3cSRobert Watson #define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
561ad71fe3cSRobert Watson #define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
562ad71fe3cSRobert Watson #define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
563ad71fe3cSRobert Watson #define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
564ad71fe3cSRobert Watson #define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
565ad71fe3cSRobert Watson #define	IN6P_RTHDR		0x00100000 /* receive routing header */
566ad71fe3cSRobert Watson #define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
567ad71fe3cSRobert Watson #define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
568ad71fe3cSRobert Watson #define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
569a43e7a96SGleb Smirnoff /*	INP_INLBGROUP		0x01000000 private to in_pcb.c */
570ad71fe3cSRobert Watson #define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
571ad71fe3cSRobert Watson #define	INP_DROPPED		0x04000000 /* protocol drop flag */
572ad71fe3cSRobert Watson #define	INP_SOCKREF		0x08000000 /* strong socket reference */
573c2529042SHans Petter Selasky #define	INP_RESERVED_0          0x10000000 /* reserved field */
574c2529042SHans Petter Selasky #define	INP_RESERVED_1          0x20000000 /* reserved field */
575f95d4633SHajimu UMEMOTO #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
576f95d4633SHajimu UMEMOTO #define	IN6P_MTU		0x80000000 /* receive path MTU */
57733841545SHajimu UMEMOTO 
57882c23ebaSBill Fenner #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
5793cca425bSMichael Tuexen 				 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
58033841545SHajimu UMEMOTO 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
58133841545SHajimu UMEMOTO 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
582f95d4633SHajimu UMEMOTO 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
583f95d4633SHajimu UMEMOTO 				 IN6P_MTU)
58482cd038dSYoshinobu Inoue 
585773b573aSKip Macy /*
586773b573aSKip Macy  * Flags for inp_flags2.
587773b573aSKip Macy  */
588c3c20de3SGleb Smirnoff /*				0x00000001 */
589c3c20de3SGleb Smirnoff /*				0x00000002 */
590db0ac6deSCy Schubert /*				0x00000004 */
591e3ba0d6aSGleb Smirnoff /*				0x00000008 */
592db0ac6deSCy Schubert /*				0x00000010 */
593e3ba0d6aSGleb Smirnoff /*				0x00000020 */
594317fa516SMark Johnston /*				0x00000040 */
595317fa516SMark Johnston /*				0x00000080 */
5969d3ddf43SAdrian Chadd #define	INP_RECVFLOWID		0x00000100 /* populate recv datagram with flow info */
5979d3ddf43SAdrian Chadd #define	INP_RECVRSSBUCKETID	0x00000200 /* populate recv datagram with bucket id */
598f3e7afe2SHans Petter Selasky #define	INP_RATE_LIMIT_CHANGED	0x00000400 /* rate limit needs attention */
599dce33a45SErmal Luçi #define	INP_ORIGDSTADDR		0x00000800 /* receive IP dst address/port */
600c3c20de3SGleb Smirnoff /*				0x00001000 */
601e3ba0d6aSGleb Smirnoff /*				0x00002000 */
602c3c20de3SGleb Smirnoff /*				0x00004000 */
603c3c20de3SGleb Smirnoff /*				0x00008000 */
604c3c20de3SGleb Smirnoff /*				0x00010000 */
605868aabb4SRichard Scheffenegger #define INP_2PCP_SET		0x00020000 /* If the Eth PCP should be set explicitly */
606868aabb4SRichard Scheffenegger #define INP_2PCP_BIT0		0x00040000 /* Eth PCP Bit 0 */
607868aabb4SRichard Scheffenegger #define INP_2PCP_BIT1		0x00080000 /* Eth PCP Bit 1 */
608868aabb4SRichard Scheffenegger #define INP_2PCP_BIT2		0x00100000 /* Eth PCP Bit 2 */
609868aabb4SRichard Scheffenegger #define INP_2PCP_BASE	INP_2PCP_BIT0
610868aabb4SRichard Scheffenegger #define INP_2PCP_MASK	(INP_2PCP_BIT0 | INP_2PCP_BIT1 | INP_2PCP_BIT2)
611868aabb4SRichard Scheffenegger #define INP_2PCP_SHIFT		18         /* shift PCP field in/out of inp_flags2 */
612db0ac6deSCy Schubert 
613fa046d87SRobert Watson /*
614db0ac6deSCy Schubert  * Flags passed to in_pcblookup*(), inp_smr_lock() and inp_next().
615fa046d87SRobert Watson  */
616db0ac6deSCy Schubert typedef	enum {
617db0ac6deSCy Schubert 	INPLOOKUP_WILDCARD = 0x00000001,	/* Allow wildcard sockets. */
618db0ac6deSCy Schubert 	INPLOOKUP_RLOCKPCB = 0x00000002,	/* Return inpcb read-locked. */
619db0ac6deSCy Schubert 	INPLOOKUP_WLOCKPCB = 0x00000004,	/* Return inpcb write-locked. */
620db0ac6deSCy Schubert } inp_lookup_t;
621fa046d87SRobert Watson 
622fa046d87SRobert Watson #define	INPLOOKUP_MASK	(INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
6231db08fbeSGleb Smirnoff 	    INPLOOKUP_WLOCKPCB)
624db0ac6deSCy Schubert #define	INPLOOKUP_LOCKMASK	(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)
625fa046d87SRobert Watson 
626df8bae1dSRodney W. Grimes #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
62782cd038dSYoshinobu Inoue 
62882cd038dSYoshinobu Inoue #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
62982cd038dSYoshinobu Inoue 
63082cd038dSYoshinobu Inoue #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
631df8bae1dSRodney W. Grimes 
632664a31e4SPeter Wemm #ifdef _KERNEL
633eddfbb76SRobert Watson VNET_DECLARE(int, ipport_reservedhigh);
634eddfbb76SRobert Watson VNET_DECLARE(int, ipport_reservedlow);
635eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lowfirstauto);
636eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lowlastauto);
637eddfbb76SRobert Watson VNET_DECLARE(int, ipport_firstauto);
638eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lastauto);
639eddfbb76SRobert Watson VNET_DECLARE(int, ipport_hifirstauto);
640eddfbb76SRobert Watson VNET_DECLARE(int, ipport_hilastauto);
641eddfbb76SRobert Watson VNET_DECLARE(int, ipport_randomized);
642eddfbb76SRobert Watson 
6431e77c105SRobert Watson #define	V_ipport_reservedhigh	VNET(ipport_reservedhigh)
6441e77c105SRobert Watson #define	V_ipport_reservedlow	VNET(ipport_reservedlow)
6451e77c105SRobert Watson #define	V_ipport_lowfirstauto	VNET(ipport_lowfirstauto)
6461e77c105SRobert Watson #define	V_ipport_lowlastauto	VNET(ipport_lowlastauto)
6471e77c105SRobert Watson #define	V_ipport_firstauto	VNET(ipport_firstauto)
6481e77c105SRobert Watson #define	V_ipport_lastauto	VNET(ipport_lastauto)
6491e77c105SRobert Watson #define	V_ipport_hifirstauto	VNET(ipport_hifirstauto)
6501e77c105SRobert Watson #define	V_ipport_hilastauto	VNET(ipport_hilastauto)
6511e77c105SRobert Watson #define	V_ipport_randomized	VNET(ipport_randomized)
652eddfbb76SRobert Watson 
653fec8a8c7SGleb Smirnoff void	in_pcbinfo_init(struct inpcbinfo *, struct inpcbstorage *,
654fec8a8c7SGleb Smirnoff 	    u_int, u_int);
6559bcd427bSRobert Watson void	in_pcbinfo_destroy(struct inpcbinfo *);
656fec8a8c7SGleb Smirnoff void	in_pcbstorage_init(void *);
657fec8a8c7SGleb Smirnoff void	in_pcbstorage_destroy(void *);
65852cd27cbSRobert Watson 
659f76fcf6dSJeffrey Hsu void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
660d915b280SStephan Uphoff int	in_pcballoc(struct socket *, struct inpcbinfo *);
66196871af0SGleb Smirnoff int	in_pcbbind(struct inpcb *, struct sockaddr_in *, struct ucred *);
66296871af0SGleb Smirnoff int	in_pcbbind_setup(struct inpcb *, struct sockaddr_in *, in_addr_t *,
663b0330ed9SPawel Jakub Dawidek 	    u_short *, struct ucred *);
664*0b4539eeSGleb Smirnoff int	in_pcbconnect(struct inpcb *, struct sockaddr_in *, struct ucred *);
665a9d22cceSGleb Smirnoff int	in_pcbconnect_setup(struct inpcb *, struct sockaddr_in *, in_addr_t *,
6669e46ff4dSGleb Smirnoff 	    u_short *, in_addr_t *, u_short *, struct ucred *);
6674d77a549SAlfred Perlstein void	in_pcbdisconnect(struct inpcb *);
66810702a28SRobert Watson void	in_pcbdrop(struct inpcb *);
6694c7c478dSRobert Watson void	in_pcbfree(struct inpcb *);
6704d77a549SAlfred Perlstein int	in_pcbinshash(struct inpcb *);
671ae190832SSteven Hartland int	in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
672ae190832SSteven Hartland 	    struct ucred *);
673a034518aSAndrew Gallatin int	in_pcblbgroup_numa(struct inpcb *, int arg);
674df8bae1dSRodney W. Grimes struct inpcb *
675fa046d87SRobert Watson 	in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
676c1cd65baSBruce Evans 	    struct in_addr, u_int, int, struct ifnet *);
677d3c1f003SRobert Watson struct inpcb *
678d3c1f003SRobert Watson 	in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
679d3c1f003SRobert Watson 	    struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
68028696211SRobert Watson void	in_pcbref(struct inpcb *);
6814d77a549SAlfred Perlstein void	in_pcbrehash(struct inpcb *);
682fdb987beSMark Johnston void	in_pcbremhash_locked(struct inpcb *);
6837b92493aSMark Johnston bool	in_pcbrele(struct inpcb *, inp_lookup_t);
684db0ac6deSCy Schubert bool	in_pcbrele_rlocked(struct inpcb *);
685db0ac6deSCy Schubert bool	in_pcbrele_wlocked(struct inpcb *);
686db0ac6deSCy Schubert 
687db0ac6deSCy Schubert typedef bool inp_match_t(const struct inpcb *, void *);
688db0ac6deSCy Schubert struct inpcb_iterator {
689db0ac6deSCy Schubert 	const struct inpcbinfo	*ipi;
690db0ac6deSCy Schubert 	struct inpcb		*inp;
691db0ac6deSCy Schubert 	inp_match_t		*match;
692db0ac6deSCy Schubert 	void			*ctx;
693db0ac6deSCy Schubert 	int			hash;
694db0ac6deSCy Schubert #define	INP_ALL_LIST		-1
695db0ac6deSCy Schubert 	const inp_lookup_t	lock;
696db0ac6deSCy Schubert };
697db0ac6deSCy Schubert 
698db0ac6deSCy Schubert /* Note: sparse initializers guarantee .inp = NULL. */
699db0ac6deSCy Schubert #define	INP_ITERATOR(_ipi, _lock, _match, _ctx)		\
700db0ac6deSCy Schubert 	{						\
701db0ac6deSCy Schubert 		.ipi = (_ipi),				\
702db0ac6deSCy Schubert 		.lock = (_lock),			\
703db0ac6deSCy Schubert 		.hash = INP_ALL_LIST,			\
704db0ac6deSCy Schubert 		.match = (_match),			\
705db0ac6deSCy Schubert 		.ctx = (_ctx),				\
706db0ac6deSCy Schubert 	}
707db0ac6deSCy Schubert #define	INP_ALL_ITERATOR(_ipi, _lock)			\
708db0ac6deSCy Schubert 	{						\
709db0ac6deSCy Schubert 		.ipi = (_ipi),				\
710db0ac6deSCy Schubert 		.lock = (_lock),			\
711db0ac6deSCy Schubert 		.hash = INP_ALL_LIST,			\
712db0ac6deSCy Schubert 	}
713db0ac6deSCy Schubert 
714db0ac6deSCy Schubert struct inpcb *inp_next(struct inpcb_iterator *);
71584cc0778SGeorge V. Neville-Neil void	in_losing(struct inpcb *);
716a557af22SRobert Watson void	in_pcbsetsolabel(struct socket *so);
7170fac350cSGleb Smirnoff int	in_getpeeraddr(struct socket *, struct sockaddr *sa);
7180fac350cSGleb Smirnoff int	in_getsockaddr(struct socket *, struct sockaddr *sa);
719a557af22SRobert Watson void	in_pcbsosetlabel(struct socket *so);
720f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
72120abea66SRandall Stewart int
72220abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *, struct ifnet *,
72320abea66SRandall Stewart 	    struct mbuf *, uint32_t);
72420abea66SRandall Stewart int	in_pcbattach_txrtlmt(struct inpcb *, struct ifnet *, uint32_t, uint32_t,
72520abea66SRandall Stewart 	    uint32_t, struct m_snd_tag **);
726f3e7afe2SHans Petter Selasky void	in_pcbdetach_txrtlmt(struct inpcb *);
72798d7a8d9SJohn Baldwin void    in_pcbdetach_tag(struct m_snd_tag *);
728f3e7afe2SHans Petter Selasky int	in_pcbmodify_txrtlmt(struct inpcb *, uint32_t);
729f3e7afe2SHans Petter Selasky int	in_pcbquery_txrtlmt(struct inpcb *, uint32_t *);
73095ed5015SHans Petter Selasky int	in_pcbquery_txrlevel(struct inpcb *, uint32_t *);
731f3e7afe2SHans Petter Selasky void	in_pcboutput_txrtlmt(struct inpcb *, struct ifnet *, struct mbuf *);
732f3e7afe2SHans Petter Selasky void	in_pcboutput_eagain(struct inpcb *);
733f3e7afe2SHans Petter Selasky #endif
734664a31e4SPeter Wemm #endif /* _KERNEL */
7353d4d47f3SGarrett Wollman 
7368781d8e9SBruce Evans #endif /* !_NETINET_IN_PCB_H_ */
737