xref: /freebsd/sys/netinet/in_pcb.h (revision eddfbb763ded6b5f6777335142be9a0edab628bb)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1990, 1993
3a5654bb2SRobert Watson  *	The Regents of the University of California.
4a5654bb2SRobert Watson  * All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
7df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
8df8bae1dSRodney W. Grimes  * are met:
9df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
10df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
11df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
13df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
14df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
15df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
16df8bae1dSRodney W. Grimes  *    without specific prior written permission.
17df8bae1dSRodney W. Grimes  *
18df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
29df8bae1dSRodney W. Grimes  *
30df8bae1dSRodney W. Grimes  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
31c3aac50fSPeter Wemm  * $FreeBSD$
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34707f139eSPaul Richards #ifndef _NETINET_IN_PCB_H_
35707f139eSPaul Richards #define _NETINET_IN_PCB_H_
36707f139eSPaul Richards 
37f1844d4cSBruce Evans #include <sys/queue.h>
3840545cf5SBruce Evans #include <sys/_lock.h>
3940545cf5SBruce Evans #include <sys/_mutex.h>
408501a69cSRobert Watson #include <sys/_rwlock.h>
41f1844d4cSBruce Evans 
428501a69cSRobert Watson #ifdef _KERNEL
438501a69cSRobert Watson #include <sys/rwlock.h>
44eddfbb76SRobert Watson #include <net/vnet.h>
458501a69cSRobert Watson #endif
468501a69cSRobert Watson 
4782cd038dSYoshinobu Inoue #define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
4882cd038dSYoshinobu Inoue #define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
49b9234fafSSam Leffler struct inpcbpolicy;
5082cd038dSYoshinobu Inoue 
51df8bae1dSRodney W. Grimes /*
52fc18af96SRobert Watson  * struct inpcb is the common protocol control block structure used in most
53fc18af96SRobert Watson  * IP transport protocols.
54712fc218SRobert Watson  *
55712fc218SRobert Watson  * Pointers to local and foreign host table entries, local and foreign socket
56712fc218SRobert Watson  * numbers, and pointers up (to a socket structure) and down (to a
57712fc218SRobert Watson  * protocol-specific control block) are stored here.
58df8bae1dSRodney W. Grimes  */
59e3975643SJake Burkholder LIST_HEAD(inpcbhead, inpcb);
60e3975643SJake Burkholder LIST_HEAD(inpcbporthead, inpcbport);
6198271db4SGarrett Wollman typedef	u_quad_t	inp_gen_t;
6215bd2b43SDavid Greenman 
633d4d47f3SGarrett Wollman /*
6482cd038dSYoshinobu Inoue  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
65712fc218SRobert Watson  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
66712fc218SRobert Watson  * the following structure.
6782cd038dSYoshinobu Inoue  */
6882cd038dSYoshinobu Inoue struct in_addr_4in6 {
6982cd038dSYoshinobu Inoue 	u_int32_t	ia46_pad32[3];
7082cd038dSYoshinobu Inoue 	struct	in_addr	ia46_addr4;
7182cd038dSYoshinobu Inoue };
7282cd038dSYoshinobu Inoue 
7382cd038dSYoshinobu Inoue /*
74712fc218SRobert Watson  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
75712fc218SRobert Watson  * some extra padding to accomplish this.
76be2ac88cSJonathan Lemon  */
77be2ac88cSJonathan Lemon struct in_endpoints {
78be2ac88cSJonathan Lemon 	u_int16_t	ie_fport;		/* foreign port */
79be2ac88cSJonathan Lemon 	u_int16_t	ie_lport;		/* local port */
80be2ac88cSJonathan Lemon 	/* protocol dependent part, local and foreign addr */
81be2ac88cSJonathan Lemon 	union {
82be2ac88cSJonathan Lemon 		/* foreign host table entry */
83be2ac88cSJonathan Lemon 		struct	in_addr_4in6 ie46_foreign;
84be2ac88cSJonathan Lemon 		struct	in6_addr ie6_foreign;
85be2ac88cSJonathan Lemon 	} ie_dependfaddr;
86be2ac88cSJonathan Lemon 	union {
87be2ac88cSJonathan Lemon 		/* local host table entry */
88be2ac88cSJonathan Lemon 		struct	in_addr_4in6 ie46_local;
89be2ac88cSJonathan Lemon 		struct	in6_addr ie6_local;
90be2ac88cSJonathan Lemon 	} ie_dependladdr;
91a5654bb2SRobert Watson };
92be2ac88cSJonathan Lemon #define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
93be2ac88cSJonathan Lemon #define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
94be2ac88cSJonathan Lemon #define	ie6_faddr	ie_dependfaddr.ie6_foreign
95be2ac88cSJonathan Lemon #define	ie6_laddr	ie_dependladdr.ie6_local
96be2ac88cSJonathan Lemon 
97be2ac88cSJonathan Lemon /*
98712fc218SRobert Watson  * XXX The defines for inc_* are hacks and should be changed to direct
99712fc218SRobert Watson  * references.
100be2ac88cSJonathan Lemon  */
101be2ac88cSJonathan Lemon struct in_conninfo {
102be2ac88cSJonathan Lemon 	u_int8_t	inc_flags;
103be2ac88cSJonathan Lemon 	u_int8_t	inc_len;
1048b07e49aSJulian Elischer 	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
10597d8d152SAndre Oppermann 	/* protocol dependent part */
106be2ac88cSJonathan Lemon 	struct	in_endpoints inc_ie;
107be2ac88cSJonathan Lemon };
108dcdb4371SBjoern A. Zeeb 
109dcdb4371SBjoern A. Zeeb /*
110dcdb4371SBjoern A. Zeeb  * Flags for inc_flags.
111dcdb4371SBjoern A. Zeeb  */
112dcdb4371SBjoern A. Zeeb #define	INC_ISIPV6	0x01
113dcdb4371SBjoern A. Zeeb 
114be2ac88cSJonathan Lemon #define inc_isipv6	inc_flags	/* temp compatability */
115be2ac88cSJonathan Lemon #define	inc_fport	inc_ie.ie_fport
116be2ac88cSJonathan Lemon #define	inc_lport	inc_ie.ie_lport
117be2ac88cSJonathan Lemon #define	inc_faddr	inc_ie.ie_faddr
118be2ac88cSJonathan Lemon #define	inc_laddr	inc_ie.ie_laddr
119be2ac88cSJonathan Lemon #define	inc6_faddr	inc_ie.ie6_faddr
120be2ac88cSJonathan Lemon #define	inc6_laddr	inc_ie.ie6_laddr
121be2ac88cSJonathan Lemon 
12282cd038dSYoshinobu Inoue struct	icmp6_filter;
12382cd038dSYoshinobu Inoue 
1247b709f8aSRobert Watson /*-
1257b709f8aSRobert Watson  * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4
1267b709f8aSRobert Watson  * and IPv6 sockets.  In the case of TCP, further per-connection state is
1277b709f8aSRobert Watson  * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
1287b709f8aSRobert Watson  * are static after creation or protected by a per-inpcb rwlock, inp_lock.  A
1297b709f8aSRobert Watson  * few fields also require the global pcbinfo lock for the inpcb to be held,
1307b709f8aSRobert Watson  * when modified, such as the global connection lists and hashes, as well as
1317b709f8aSRobert Watson  * binding information (which affects which hash a connection is on).  This
1327b709f8aSRobert Watson  * model means that connections can be looked up without holding the
1337b709f8aSRobert Watson  * per-connection lock, which is important for performance when attempting to
1347b709f8aSRobert Watson  * find the connection for a packet given its IP and port tuple.  Writing to
1357b709f8aSRobert Watson  * these fields that write locks be held on both the inpcb and global locks.
1367b709f8aSRobert Watson  *
1377b709f8aSRobert Watson  * Key:
1387b709f8aSRobert Watson  * (c) - Constant after initialization
1397b709f8aSRobert Watson  * (i) - Protected by the inpcb lock
1407b709f8aSRobert Watson  * (p) - Protected by the pcbinfo lock for the inpcb
1417b709f8aSRobert Watson  * (s) - Protected by another subsystem's locks
1427b709f8aSRobert Watson  * (x) - Undefined locking
1437b709f8aSRobert Watson  *
1447b709f8aSRobert Watson  * A few other notes:
1457b709f8aSRobert Watson  *
1467b709f8aSRobert Watson  * When a read lock is held, stability of the field is guaranteed; to write
1477b709f8aSRobert Watson  * to a field, a write lock must generally be held.
1487b709f8aSRobert Watson  *
1497b709f8aSRobert Watson  * netinet/netinet6-layer code should not assume that the inp_socket pointer
1507b709f8aSRobert Watson  * is safe to dereference without inp_lock being held, even for protocols
1517b709f8aSRobert Watson  * other than TCP (where the inpcb persists during TIMEWAIT even after the
1527b709f8aSRobert Watson  * socket has been freed), or there may be close(2)-related races.
1537b709f8aSRobert Watson  *
1547b709f8aSRobert Watson  * The inp_vflag field is overloaded, and would otherwise ideally be (c).
1557b709f8aSRobert Watson  */
156df8bae1dSRodney W. Grimes struct inpcb {
1577b709f8aSRobert Watson 	LIST_ENTRY(inpcb) inp_hash;	/* (i/p) hash list */
1587b709f8aSRobert Watson 	LIST_ENTRY(inpcb) inp_list;	/* (i/p) list for all PCBs for proto */
1597b709f8aSRobert Watson 	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
1607b709f8aSRobert Watson 	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
1617b709f8aSRobert Watson 	struct	socket *inp_socket;	/* (i) back pointer to socket */
16286d02c5cSBjoern A. Zeeb 	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
1637b709f8aSRobert Watson 	u_int32_t inp_flow;		/* (i) IPv6 flow information */
1647b709f8aSRobert Watson 	int	inp_flags;		/* (i) generic IP/datagram flags */
165773b573aSKip Macy 	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
1667b709f8aSRobert Watson 	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
1677b709f8aSRobert Watson 	u_char	inp_ip_ttl;		/* (i) time to live proto */
1687b709f8aSRobert Watson 	u_char	inp_ip_p;		/* (c) protocol proto */
1697b709f8aSRobert Watson 	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
17080cb9f21SKip Macy 	uint32_t inp_flowid;		/* (x) flow id / queue id */
17128696211SRobert Watson 	u_int	inp_refcount;		/* (i) refcount */
172ab25fa35SKip Macy 	void	*inp_pspare[2];		/* (x) rtentry / general use */
1732de2af32SKip Macy 
1742de2af32SKip Macy 	/* Local and foreign ports, local and foreign addr. */
175a5654bb2SRobert Watson 	struct	in_conninfo inp_inc;	/* (i/p) list for PCB's local port */
1762de2af32SKip Macy 
177a5654bb2SRobert Watson 	/* MAC and IPSEC policy information. */
1787b709f8aSRobert Watson 	struct	label *inp_label;	/* (i) MAC label */
1797b709f8aSRobert Watson 	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
18082cd038dSYoshinobu Inoue 
181712fc218SRobert Watson 	/* Protocol-dependent part; options. */
18282cd038dSYoshinobu Inoue 	struct {
1837b709f8aSRobert Watson 		u_char	inp4_ip_tos;		/* (i) type of service proto */
1847b709f8aSRobert Watson 		struct	mbuf *inp4_options;	/* (i) IP options */
185a5654bb2SRobert Watson 		struct	ip_moptions *inp4_moptions; /* (i) IP mcast options */
18682cd038dSYoshinobu Inoue 	} inp_depend4;
18782cd038dSYoshinobu Inoue 	struct {
1887b709f8aSRobert Watson 		/* (i) IP options */
18982cd038dSYoshinobu Inoue 		struct	mbuf *inp6_options;
1907b709f8aSRobert Watson 		/* (i) IP6 options for outgoing packets */
19182cd038dSYoshinobu Inoue 		struct	ip6_pktopts *inp6_outputopts;
1927b709f8aSRobert Watson 		/* (i) IP multicast options */
19382cd038dSYoshinobu Inoue 		struct	ip6_moptions *inp6_moptions;
1947b709f8aSRobert Watson 		/* (i) ICMPv6 code type filter */
19582cd038dSYoshinobu Inoue 		struct	icmp6_filter *inp6_icmp6filt;
1967b709f8aSRobert Watson 		/* (i) IPV6_CHECKSUM setsockopt */
19782cd038dSYoshinobu Inoue 		int	inp6_cksum;
19882cd038dSYoshinobu Inoue 		short	inp6_hops;
19982cd038dSYoshinobu Inoue 	} inp_depend6;
2007b709f8aSRobert Watson 	LIST_ENTRY(inpcb) inp_portlist;	/* (i/p) */
2017b709f8aSRobert Watson 	struct	inpcbport *inp_phd;	/* (i/p) head of this list */
202d915b280SStephan Uphoff #define inp_zero_size offsetof(struct inpcb, inp_gencnt)
203a5654bb2SRobert Watson 	inp_gen_t	inp_gencnt;	/* (c) generation count */
204114f15c6SKip Macy 	struct llentry	*inp_lle;	/* cached L2 information */
205114f15c6SKip Macy 	struct rtentry	*inp_rt;	/* cached L3 information */
2068501a69cSRobert Watson 	struct rwlock	inp_lock;
207a5654bb2SRobert Watson };
208a5654bb2SRobert Watson #define	inp_fport	inp_inc.inc_fport
209a5654bb2SRobert Watson #define	inp_lport	inp_inc.inc_lport
210a5654bb2SRobert Watson #define	inp_faddr	inp_inc.inc_faddr
211a5654bb2SRobert Watson #define	inp_laddr	inp_inc.inc_laddr
212a5654bb2SRobert Watson #define	inp_ip_tos	inp_depend4.inp4_ip_tos
213a5654bb2SRobert Watson #define	inp_options	inp_depend4.inp4_options
214a5654bb2SRobert Watson #define	inp_moptions	inp_depend4.inp4_moptions
215f76fcf6dSJeffrey Hsu 
216be2ac88cSJonathan Lemon #define	in6p_faddr	inp_inc.inc6_faddr
217be2ac88cSJonathan Lemon #define	in6p_laddr	inp_inc.inc6_laddr
21882cd038dSYoshinobu Inoue #define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
21982cd038dSYoshinobu Inoue #define	in6p_flowinfo	inp_flow
22082cd038dSYoshinobu Inoue #define	in6p_options	inp_depend6.inp6_options
22182cd038dSYoshinobu Inoue #define	in6p_outputopts	inp_depend6.inp6_outputopts
22282cd038dSYoshinobu Inoue #define	in6p_moptions	inp_depend6.inp6_moptions
22382cd038dSYoshinobu Inoue #define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
22482cd038dSYoshinobu Inoue #define	in6p_cksum	inp_depend6.inp6_cksum
225a5654bb2SRobert Watson 
226f6dfe47aSMarko Zec #define	inp_vnet	inp_pcbinfo->ipi_vnet
227f6dfe47aSMarko Zec 
2283d4d47f3SGarrett Wollman /*
229712fc218SRobert Watson  * The range of the generation count, as used in this implementation, is 9e19.
230712fc218SRobert Watson  * We would have to create 300 billion connections per second for this number
231712fc218SRobert Watson  * to roll over in a year.  This seems sufficiently unlikely that we simply
232712fc218SRobert Watson  * don't concern ourselves with that possibility.
2333d4d47f3SGarrett Wollman  */
234c3229e05SDavid Greenman 
23598271db4SGarrett Wollman /*
236712fc218SRobert Watson  * Interface exported to userland by various protocols which use inpcbs.  Hack
237712fc218SRobert Watson  * alert -- only define if struct xsocket is in scope.
23898271db4SGarrett Wollman  */
23998271db4SGarrett Wollman #ifdef _SYS_SOCKETVAR_H_
24098271db4SGarrett Wollman struct	xinpcb {
24198271db4SGarrett Wollman 	size_t	xi_len;		/* length of this structure */
24298271db4SGarrett Wollman 	struct	inpcb xi_inp;
24398271db4SGarrett Wollman 	struct	xsocket xi_socket;
24498271db4SGarrett Wollman 	u_quad_t	xi_alignment_hack;
24598271db4SGarrett Wollman };
24698271db4SGarrett Wollman 
24798271db4SGarrett Wollman struct	xinpgen {
24898271db4SGarrett Wollman 	size_t	xig_len;	/* length of this structure */
24998271db4SGarrett Wollman 	u_int	xig_count;	/* number of PCBs at this time */
25098271db4SGarrett Wollman 	inp_gen_t xig_gen;	/* generation count at this time */
25198271db4SGarrett Wollman 	so_gen_t xig_sogen;	/* socket generation count at this time */
25298271db4SGarrett Wollman };
25398271db4SGarrett Wollman #endif /* _SYS_SOCKETVAR_H_ */
25498271db4SGarrett Wollman 
255c3229e05SDavid Greenman struct inpcbport {
256e3975643SJake Burkholder 	LIST_ENTRY(inpcbport) phd_hash;
257c3229e05SDavid Greenman 	struct inpcbhead phd_pcblist;
258c3229e05SDavid Greenman 	u_short phd_port;
259df8bae1dSRodney W. Grimes };
260df8bae1dSRodney W. Grimes 
261712fc218SRobert Watson /*
262712fc218SRobert Watson  * Global data structure for each high-level protocol (UDP, TCP, ...) in both
263712fc218SRobert Watson  * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
264712fc218SRobert Watson  */
265712fc218SRobert Watson struct inpcbinfo {
266712fc218SRobert Watson 	/*
267712fc218SRobert Watson 	 * Global list of inpcbs on the protocol.
268712fc218SRobert Watson 	 */
269712fc218SRobert Watson 	struct inpcbhead	*ipi_listhead;
270712fc218SRobert Watson 	u_int			 ipi_count;
271712fc218SRobert Watson 
272712fc218SRobert Watson 	/*
273712fc218SRobert Watson 	 * Global hash of inpcbs, hashed by local and foreign addresses and
274712fc218SRobert Watson 	 * port numbers.
275712fc218SRobert Watson 	 */
276712fc218SRobert Watson 	struct inpcbhead	*ipi_hashbase;
277712fc218SRobert Watson 	u_long			 ipi_hashmask;
278712fc218SRobert Watson 
279712fc218SRobert Watson 	/*
280712fc218SRobert Watson 	 * Global hash of inpcbs, hashed by only local port number.
281712fc218SRobert Watson 	 */
282712fc218SRobert Watson 	struct inpcbporthead	*ipi_porthashbase;
283712fc218SRobert Watson 	u_long			 ipi_porthashmask;
284712fc218SRobert Watson 
285712fc218SRobert Watson 	/*
286712fc218SRobert Watson 	 * Fields associated with port lookup and allocation.
287712fc218SRobert Watson 	 */
288712fc218SRobert Watson 	u_short			 ipi_lastport;
289712fc218SRobert Watson 	u_short			 ipi_lastlow;
290712fc218SRobert Watson 	u_short			 ipi_lasthi;
291712fc218SRobert Watson 
292712fc218SRobert Watson 	/*
293712fc218SRobert Watson 	 * UMA zone from which inpcbs are allocated for this protocol.
294712fc218SRobert Watson 	 */
295712fc218SRobert Watson 	struct	uma_zone	*ipi_zone;
296712fc218SRobert Watson 
297712fc218SRobert Watson 	/*
298712fc218SRobert Watson 	 * Generation count--incremented each time a connection is allocated
299712fc218SRobert Watson 	 * or freed.
300712fc218SRobert Watson 	 */
301712fc218SRobert Watson 	u_quad_t		 ipi_gencnt;
3028501a69cSRobert Watson 	struct rwlock		 ipi_lock;
3032de2af32SKip Macy 
3042de2af32SKip Macy 	/*
305f6dfe47aSMarko Zec 	 * Pointer to network stack instance
306f6dfe47aSMarko Zec 	 */
307f6dfe47aSMarko Zec 	struct vnet		*ipi_vnet;
308f6dfe47aSMarko Zec 
309f6dfe47aSMarko Zec 	/*
310f6dfe47aSMarko Zec 	 * general use 2
3112de2af32SKip Macy 	 */
3122de2af32SKip Macy 	void 			*ipi_pspare[2];
31315bd2b43SDavid Greenman };
31415bd2b43SDavid Greenman 
3155bd311a5SSam Leffler #define INP_LOCK_INIT(inp, d, t) \
3168501a69cSRobert Watson 	rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE |  RW_DUPOK)
3178501a69cSRobert Watson #define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
3188501a69cSRobert Watson #define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
3198501a69cSRobert Watson #define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
32043cc0bc1SRobert Watson #define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
32143cc0bc1SRobert Watson #define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
3228501a69cSRobert Watson #define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
3238501a69cSRobert Watson #define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
32482c33e73SKip Macy #define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
32582c33e73SKip Macy #define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
32682c33e73SKip Macy #define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
3278501a69cSRobert Watson #define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
3288501a69cSRobert Watson #define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
3298501a69cSRobert Watson #define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
3308501a69cSRobert Watson #define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
331f76fcf6dSJeffrey Hsu 
332e79dd20dSKip Macy #ifdef _KERNEL
3333d585327SKip Macy /*
3343d585327SKip Macy  * These locking functions are for inpcb consumers outside of sys/netinet,
3353d585327SKip Macy  * more specifically, they were added for the benefit of TOE drivers. The
3363d585327SKip Macy  * macros are reserved for use by the stack.
3373d585327SKip Macy  */
3383d585327SKip Macy void inp_wlock(struct inpcb *);
3393d585327SKip Macy void inp_wunlock(struct inpcb *);
3403d585327SKip Macy void inp_rlock(struct inpcb *);
3413d585327SKip Macy void inp_runlock(struct inpcb *);
3423d585327SKip Macy 
3433d585327SKip Macy #ifdef INVARIANTS
344e79dd20dSKip Macy void inp_lock_assert(struct inpcb *);
345e79dd20dSKip Macy void inp_unlock_assert(struct inpcb *);
3463d585327SKip Macy #else
3473d585327SKip Macy static __inline void
348e79dd20dSKip Macy inp_lock_assert(struct inpcb *inp __unused)
3493d585327SKip Macy {
3503d585327SKip Macy }
3513d585327SKip Macy 
3523d585327SKip Macy static __inline void
353e79dd20dSKip Macy inp_unlock_assert(struct inpcb *inp __unused)
3543d585327SKip Macy {
3553d585327SKip Macy }
356e79dd20dSKip Macy 
3573d585327SKip Macy #endif
3589378e437SKip Macy 
3599378e437SKip Macy void	inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
3609378e437SKip Macy int 	inp_ip_tos_get(const struct inpcb *inp);
3619378e437SKip Macy void 	inp_ip_tos_set(struct inpcb *inp, int val);
3629d29c635SKip Macy struct socket *
3639d29c635SKip Macy 	inp_inpcbtosocket(struct inpcb *inp);
3649d29c635SKip Macy struct tcpcb *
3659d29c635SKip Macy 	inp_inpcbtotcpcb(struct inpcb *inp);
366df9cf830STai-hwa Liang void 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
3679d29c635SKip Macy 		uint32_t *faddr, uint16_t *fp);
3689378e437SKip Macy 
369e79dd20dSKip Macy #endif /* _KERNEL */
3703d585327SKip Macy 
371f76fcf6dSJeffrey Hsu #define INP_INFO_LOCK_INIT(ipi, d) \
3728501a69cSRobert Watson 	rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE)
3738501a69cSRobert Watson #define INP_INFO_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_lock)
3748501a69cSRobert Watson #define INP_INFO_RLOCK(ipi)	rw_rlock(&(ipi)->ipi_lock)
3758501a69cSRobert Watson #define INP_INFO_WLOCK(ipi)	rw_wlock(&(ipi)->ipi_lock)
37643cc0bc1SRobert Watson #define INP_INFO_TRY_RLOCK(ipi)	rw_try_rlock(&(ipi)->ipi_lock)
37743cc0bc1SRobert Watson #define INP_INFO_TRY_WLOCK(ipi)	rw_try_wlock(&(ipi)->ipi_lock)
3788501a69cSRobert Watson #define INP_INFO_RUNLOCK(ipi)	rw_runlock(&(ipi)->ipi_lock)
3798501a69cSRobert Watson #define INP_INFO_WUNLOCK(ipi)	rw_wunlock(&(ipi)->ipi_lock)
3808501a69cSRobert Watson #define	INP_INFO_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_LOCKED)
3818501a69cSRobert Watson #define INP_INFO_RLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_RLOCKED)
3828501a69cSRobert Watson #define INP_INFO_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_WLOCKED)
3838501a69cSRobert Watson #define INP_INFO_UNLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED)
384f76fcf6dSJeffrey Hsu 
385ddd79a97SDavid Greenman #define INP_PCBHASH(faddr, lport, fport, mask) \
386c3229e05SDavid Greenman 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
387c3229e05SDavid Greenman #define INP_PCBPORTHASH(lport, mask) \
388c3229e05SDavid Greenman 	(ntohs((lport)) & (mask))
389ddd79a97SDavid Greenman 
390a5654bb2SRobert Watson /*
391773b573aSKip Macy  * Flags for inp_vflags -- historically version flags only
392a5654bb2SRobert Watson  */
393a5654bb2SRobert Watson #define	INP_IPV4	0x1
394a5654bb2SRobert Watson #define	INP_IPV6	0x2
395a5654bb2SRobert Watson #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
396a5654bb2SRobert Watson 
397a5654bb2SRobert Watson /*
398773b573aSKip Macy  * Flags for inp_flags.
399a5654bb2SRobert Watson  */
400ad71fe3cSRobert Watson #define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
401ad71fe3cSRobert Watson #define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
402ad71fe3cSRobert Watson #define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
403ad71fe3cSRobert Watson #define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
404ad71fe3cSRobert Watson #define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
405ad71fe3cSRobert Watson #define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
406ad71fe3cSRobert Watson #define	INP_ANONPORT		0x00000040 /* port chosen for user */
407ad71fe3cSRobert Watson #define	INP_RECVIF		0x00000080 /* receive incoming interface */
408ad71fe3cSRobert Watson #define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
409ad71fe3cSRobert Watson #define	INP_FAITH		0x00000200 /* accept FAITH'ed connections */
410ad71fe3cSRobert Watson #define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
411ad71fe3cSRobert Watson #define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
412f44270e7SPawel Jakub Dawidek #define	INP_BINDANY		0x00001000 /* allow bind to any address */
413ad71fe3cSRobert Watson #define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
414ad71fe3cSRobert Watson #define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
415ad71fe3cSRobert Watson #define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
416ad71fe3cSRobert Watson #define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
417ad71fe3cSRobert Watson #define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
418ad71fe3cSRobert Watson #define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
419ad71fe3cSRobert Watson #define	IN6P_RTHDR		0x00100000 /* receive routing header */
420ad71fe3cSRobert Watson #define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
421ad71fe3cSRobert Watson #define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
422ad71fe3cSRobert Watson #define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
423ad71fe3cSRobert Watson #define	INP_TIMEWAIT		0x01000000 /* in TIMEWAIT, ppcb is tcptw */
424ad71fe3cSRobert Watson #define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
425ad71fe3cSRobert Watson #define	INP_DROPPED		0x04000000 /* protocol drop flag */
426ad71fe3cSRobert Watson #define	INP_SOCKREF		0x08000000 /* strong socket reference */
42780cb9f21SKip Macy #define	INP_SW_FLOWID           0x10000000 /* software generated flow id */
42880cb9f21SKip Macy #define	INP_HW_FLOWID           0x20000000 /* hardware generated flow id */
429f95d4633SHajimu UMEMOTO #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
430f95d4633SHajimu UMEMOTO #define	IN6P_MTU		0x80000000 /* receive path MTU */
43133841545SHajimu UMEMOTO 
43282c23ebaSBill Fenner #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
4334957466bSMatthew N. Dodd 				 INP_RECVIF|INP_RECVTTL|\
43433841545SHajimu UMEMOTO 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
43533841545SHajimu UMEMOTO 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
436f95d4633SHajimu UMEMOTO 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
437f95d4633SHajimu UMEMOTO 				 IN6P_MTU)
43882cd038dSYoshinobu Inoue 
439773b573aSKip Macy /*
440773b573aSKip Macy  * Flags for inp_flags2.
441773b573aSKip Macy  */
4428b12a7c2SKip Macy #define	INP_LLE_VALID		0x00000001 /* cached lle is valid */
4438b12a7c2SKip Macy #define	INP_RT_VALID		0x00000002 /* cached rtentry is valid */
444773b573aSKip Macy 
445df8bae1dSRodney W. Grimes #define	INPLOOKUP_WILDCARD	1
446df8bae1dSRodney W. Grimes #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
44782cd038dSYoshinobu Inoue #define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
44882cd038dSYoshinobu Inoue 
44982cd038dSYoshinobu Inoue #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
45082cd038dSYoshinobu Inoue 
45182cd038dSYoshinobu Inoue #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
452df8bae1dSRodney W. Grimes 
453664a31e4SPeter Wemm #ifdef _KERNEL
454eddfbb76SRobert Watson VNET_DECLARE(int, ipport_reservedhigh);
455eddfbb76SRobert Watson VNET_DECLARE(int, ipport_reservedlow);
456eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lowfirstauto);
457eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lowlastauto);
458eddfbb76SRobert Watson VNET_DECLARE(int, ipport_firstauto);
459eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lastauto);
460eddfbb76SRobert Watson VNET_DECLARE(int, ipport_hifirstauto);
461eddfbb76SRobert Watson VNET_DECLARE(int, ipport_hilastauto);
462eddfbb76SRobert Watson VNET_DECLARE(int, ipport_randomized);
463eddfbb76SRobert Watson VNET_DECLARE(int, ipport_randomcps);
464eddfbb76SRobert Watson VNET_DECLARE(int, ipport_randomtime);
465eddfbb76SRobert Watson VNET_DECLARE(int, ipport_stoprandom);
466eddfbb76SRobert Watson VNET_DECLARE(int, ipport_tcpallocs);
467eddfbb76SRobert Watson 
468eddfbb76SRobert Watson #define	V_ipport_reservedhigh	VNET_GET(ipport_reservedhigh)
469eddfbb76SRobert Watson #define	V_ipport_reservedlow	VNET_GET(ipport_reservedlow)
470eddfbb76SRobert Watson #define	V_ipport_lowfirstauto	VNET_GET(ipport_lowfirstauto)
471eddfbb76SRobert Watson #define	V_ipport_lowlastauto	VNET_GET(ipport_lowlastauto)
472eddfbb76SRobert Watson #define	V_ipport_firstauto	VNET_GET(ipport_firstauto)
473eddfbb76SRobert Watson #define	V_ipport_lastauto	VNET_GET(ipport_lastauto)
474eddfbb76SRobert Watson #define	V_ipport_hifirstauto	VNET_GET(ipport_hifirstauto)
475eddfbb76SRobert Watson #define	V_ipport_hilastauto	VNET_GET(ipport_hilastauto)
476eddfbb76SRobert Watson #define	V_ipport_randomized	VNET_GET(ipport_randomized)
477eddfbb76SRobert Watson #define	V_ipport_randomcps	VNET_GET(ipport_randomcps)
478eddfbb76SRobert Watson #define	V_ipport_randomtime	VNET_GET(ipport_randomtime)
479eddfbb76SRobert Watson #define	V_ipport_stoprandom	VNET_GET(ipport_stoprandom)
480eddfbb76SRobert Watson #define	V_ipport_tcpallocs	VNET_GET(ipport_tcpallocs)
481eddfbb76SRobert Watson 
4825f311da2SMike Silbersack extern struct callout ipport_tick_callout;
48382cd038dSYoshinobu Inoue 
484f76fcf6dSJeffrey Hsu void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
485d915b280SStephan Uphoff int	in_pcballoc(struct socket *, struct inpcbinfo *);
486b0330ed9SPawel Jakub Dawidek int	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
4874b932371SIan Dowse int	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
488b0330ed9SPawel Jakub Dawidek 	    u_short *, struct ucred *);
489b0330ed9SPawel Jakub Dawidek int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
4905200e00eSIan Dowse int	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
4915200e00eSIan Dowse 	    u_short *, in_addr_t *, u_short *, struct inpcb **,
492b0330ed9SPawel Jakub Dawidek 	    struct ucred *);
4934d77a549SAlfred Perlstein void	in_pcbdetach(struct inpcb *);
4944d77a549SAlfred Perlstein void	in_pcbdisconnect(struct inpcb *);
49510702a28SRobert Watson void	in_pcbdrop(struct inpcb *);
4964c7c478dSRobert Watson void	in_pcbfree(struct inpcb *);
4974d77a549SAlfred Perlstein int	in_pcbinshash(struct inpcb *);
498df8bae1dSRodney W. Grimes struct inpcb *
4994d77a549SAlfred Perlstein 	in_pcblookup_local(struct inpcbinfo *,
500078b7042SBjoern A. Zeeb 	    struct in_addr, u_short, int, struct ucred *);
50115bd2b43SDavid Greenman struct inpcb *
502c1cd65baSBruce Evans 	in_pcblookup_hash(struct inpcbinfo *, struct in_addr, u_int,
503c1cd65baSBruce Evans 	    struct in_addr, u_int, int, struct ifnet *);
504f76fcf6dSJeffrey Hsu void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
5053ce144eaSJeffrey Hsu 	    int, struct inpcb *(*)(struct inpcb *, int));
50628696211SRobert Watson void	in_pcbref(struct inpcb *);
5074d77a549SAlfred Perlstein void	in_pcbrehash(struct inpcb *);
50828696211SRobert Watson int	in_pcbrele(struct inpcb *);
509a557af22SRobert Watson void	in_pcbsetsolabel(struct socket *so);
51054d642bbSRobert Watson int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
51154d642bbSRobert Watson int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
51226ef6ac4SDon Lewis struct sockaddr *
51326ef6ac4SDon Lewis 	in_sockaddr(in_port_t port, struct in_addr *addr);
514a557af22SRobert Watson void	in_pcbsosetlabel(struct socket *so);
5155f311da2SMike Silbersack void	ipport_tick(void *xtp);
516664a31e4SPeter Wemm #endif /* _KERNEL */
5173d4d47f3SGarrett Wollman 
5188781d8e9SBruce Evans #endif /* !_NETINET_IN_PCB_H_ */
519