xref: /freebsd/sys/netinet/in_pcb.h (revision 317fa5169d35c508a05c520a764630252adebea8)
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  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
37c3aac50fSPeter Wemm  * $FreeBSD$
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
40707f139eSPaul Richards #ifndef _NETINET_IN_PCB_H_
41707f139eSPaul Richards #define _NETINET_IN_PCB_H_
42707f139eSPaul Richards 
43f1844d4cSBruce Evans #include <sys/queue.h>
44ddece765SMatt Macy #include <sys/epoch.h>
4540545cf5SBruce Evans #include <sys/_lock.h>
4640545cf5SBruce Evans #include <sys/_mutex.h>
478501a69cSRobert Watson #include <sys/_rwlock.h>
4884cc0778SGeorge V. Neville-Neil #include <net/route.h>
49f1844d4cSBruce Evans 
508501a69cSRobert Watson #ifdef _KERNEL
511417604eSBjoern A. Zeeb #include <sys/lock.h>
52db0ac6deSCy Schubert #include <sys/proc.h>
538501a69cSRobert Watson #include <sys/rwlock.h>
54db0ac6deSCy Schubert #include <sys/smr.h>
55a35bdd44SMichael Tuexen #include <sys/sysctl.h>
56eddfbb76SRobert Watson #include <net/vnet.h>
579bcd427bSRobert Watson #include <vm/uma.h>
588501a69cSRobert Watson #endif
59b872626dSMatt Macy #include <sys/ck.h>
608501a69cSRobert Watson 
61df8bae1dSRodney W. Grimes /*
62fc18af96SRobert Watson  * struct inpcb is the common protocol control block structure used in most
63fc18af96SRobert Watson  * IP transport protocols.
64712fc218SRobert Watson  *
65712fc218SRobert Watson  * Pointers to local and foreign host table entries, local and foreign socket
66712fc218SRobert Watson  * numbers, and pointers up (to a socket structure) and down (to a
67712fc218SRobert Watson  * protocol-specific control block) are stored here.
68df8bae1dSRodney W. Grimes  */
69b872626dSMatt Macy CK_LIST_HEAD(inpcbhead, inpcb);
70b872626dSMatt Macy CK_LIST_HEAD(inpcbporthead, inpcbport);
7154af3d0dSMark Johnston CK_LIST_HEAD(inpcblbgrouphead, inpcblbgroup);
72cc65eb4eSGleb Smirnoff typedef	uint64_t	inp_gen_t;
7315bd2b43SDavid Greenman 
743d4d47f3SGarrett Wollman /*
7582cd038dSYoshinobu Inoue  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
76712fc218SRobert Watson  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
77a0577692SGleb Smirnoff  * the following structure.  This requires padding always be zeroed out,
78a0577692SGleb Smirnoff  * which is done right after inpcb allocation and stays through its lifetime.
7982cd038dSYoshinobu Inoue  */
8082cd038dSYoshinobu Inoue struct in_addr_4in6 {
8182cd038dSYoshinobu Inoue 	u_int32_t	ia46_pad32[3];
8282cd038dSYoshinobu Inoue 	struct	in_addr	ia46_addr4;
8382cd038dSYoshinobu Inoue };
8482cd038dSYoshinobu Inoue 
851a43cff9SSean Bruno union in_dependaddr {
861a43cff9SSean Bruno 	struct in_addr_4in6 id46_addr;
871a43cff9SSean Bruno 	struct in6_addr	id6_addr;
881a43cff9SSean Bruno };
891a43cff9SSean Bruno 
9082cd038dSYoshinobu Inoue /*
91712fc218SRobert Watson  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
92712fc218SRobert Watson  * some extra padding to accomplish this.
93388909a1SGleb Smirnoff  * NOTE 2: tcp_syncache.c uses first 5 32-bit words, which identify fport,
94388909a1SGleb Smirnoff  * lport, faddr to generate hash, so these fields shouldn't be moved.
95be2ac88cSJonathan Lemon  */
96be2ac88cSJonathan Lemon struct in_endpoints {
97be2ac88cSJonathan Lemon 	u_int16_t	ie_fport;		/* foreign port */
98be2ac88cSJonathan Lemon 	u_int16_t	ie_lport;		/* local port */
99be2ac88cSJonathan Lemon 	/* protocol dependent part, local and foreign addr */
1001a43cff9SSean Bruno 	union in_dependaddr ie_dependfaddr;	/* foreign host table entry */
1011a43cff9SSean Bruno 	union in_dependaddr ie_dependladdr;	/* local host table entry */
1021a43cff9SSean Bruno #define	ie_faddr	ie_dependfaddr.id46_addr.ia46_addr4
1031a43cff9SSean Bruno #define	ie_laddr	ie_dependladdr.id46_addr.ia46_addr4
1041a43cff9SSean Bruno #define	ie6_faddr	ie_dependfaddr.id6_addr
1051a43cff9SSean Bruno #define	ie6_laddr	ie_dependladdr.id6_addr
106028bdf28SAndrey V. Elsukov 	u_int32_t	ie6_zoneid;		/* scope zone id */
107a5654bb2SRobert Watson };
108be2ac88cSJonathan Lemon 
109be2ac88cSJonathan Lemon /*
110712fc218SRobert Watson  * XXX The defines for inc_* are hacks and should be changed to direct
111712fc218SRobert Watson  * references.
112be2ac88cSJonathan Lemon  */
113be2ac88cSJonathan Lemon struct in_conninfo {
114be2ac88cSJonathan Lemon 	u_int8_t	inc_flags;
115be2ac88cSJonathan Lemon 	u_int8_t	inc_len;
1168b07e49aSJulian Elischer 	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
11797d8d152SAndre Oppermann 	/* protocol dependent part */
118be2ac88cSJonathan Lemon 	struct	in_endpoints inc_ie;
119be2ac88cSJonathan Lemon };
120dcdb4371SBjoern A. Zeeb 
121dcdb4371SBjoern A. Zeeb /*
122dcdb4371SBjoern A. Zeeb  * Flags for inc_flags.
123dcdb4371SBjoern A. Zeeb  */
124dcdb4371SBjoern A. Zeeb #define	INC_ISIPV6	0x01
1255dff1c38SMichael Tuexen #define	INC_IPV6MINMTU	0x02
126dcdb4371SBjoern A. Zeeb 
127be2ac88cSJonathan Lemon #define	inc_fport	inc_ie.ie_fport
128be2ac88cSJonathan Lemon #define	inc_lport	inc_ie.ie_lport
129be2ac88cSJonathan Lemon #define	inc_faddr	inc_ie.ie_faddr
130be2ac88cSJonathan Lemon #define	inc_laddr	inc_ie.ie_laddr
131be2ac88cSJonathan Lemon #define	inc6_faddr	inc_ie.ie6_faddr
132be2ac88cSJonathan Lemon #define	inc6_laddr	inc_ie.ie6_laddr
133028bdf28SAndrey V. Elsukov #define	inc6_zoneid	inc_ie.ie6_zoneid
134be2ac88cSJonathan Lemon 
135cc65eb4eSGleb Smirnoff #if defined(_KERNEL) || defined(_WANT_INPCB)
136cc65eb4eSGleb Smirnoff /*
137ff9b006dSJulien Charbon  * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4 and
138ff9b006dSJulien Charbon  * IPv6 sockets.  In the case of TCP and UDP, further per-connection state is
1397b709f8aSRobert Watson  * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
140db0ac6deSCy Schubert  * are static after creation or protected by a per-inpcb rwlock, inp_lock.
141ff9b006dSJulien Charbon  *
142db0ac6deSCy Schubert  * A inpcb database is indexed by addresses/ports hash as well as list of
143db0ac6deSCy Schubert  * all pcbs that belong to a certain proto. Database lookups or list traversals
144db0ac6deSCy Schubert  * are be performed inside SMR section. Once desired PCB is found its own
145db0ac6deSCy Schubert  * lock is to be obtained and SMR section exited.
1467b709f8aSRobert Watson  *
1477b709f8aSRobert Watson  * Key:
1483ee9c3c4SRandall Stewart  * (b) - Protected by the hpts lock.
1497b709f8aSRobert Watson  * (c) - Constant after initialization
150db0ac6deSCy Schubert  * (e) - Protected by the SMR section
1517b709f8aSRobert Watson  * (i) - Protected by the inpcb lock
1527b709f8aSRobert Watson  * (p) - Protected by the pcbinfo lock for the inpcb
153ff9b006dSJulien Charbon  * (h) - Protected by the pcbhash lock for the inpcb
1547b709f8aSRobert Watson  * (s) - Protected by another subsystem's locks
1557b709f8aSRobert Watson  * (x) - Undefined locking
1567b709f8aSRobert Watson  *
1573ee9c3c4SRandall Stewart  * Notes on the tcp_hpts:
1583ee9c3c4SRandall Stewart  *
1593ee9c3c4SRandall Stewart  * First Hpts lock order is
1603ee9c3c4SRandall Stewart  * 1) INP_WLOCK()
1613ee9c3c4SRandall Stewart  * 2) HPTS_LOCK() i.e. hpts->pmtx
1623ee9c3c4SRandall Stewart  *
1633ee9c3c4SRandall Stewart  * To insert a TCB on the hpts you *must* be holding the INP_WLOCK().
1643ee9c3c4SRandall Stewart  * You may check the inp->inp_in_hpts flag without the hpts lock.
1653ee9c3c4SRandall Stewart  * The hpts is the only one that will clear this flag holding
1663ee9c3c4SRandall Stewart  * only the hpts lock. This means that in your tcp_output()
1673ee9c3c4SRandall Stewart  * routine when you test for the inp_in_hpts flag to be 1
1683ee9c3c4SRandall Stewart  * it may be transitioning to 0 (by the hpts).
1693ee9c3c4SRandall Stewart  * That's ok since that will just mean an extra call to tcp_output
1703ee9c3c4SRandall Stewart  * that most likely will find the call you executed
171893f36b7SGordon Bergling  * (when the mis-match occurred) will have put the TCB back
1723ee9c3c4SRandall Stewart  * on the hpts and it will return. If your
1733ee9c3c4SRandall Stewart  * call did not add the inp back to the hpts then you will either
1743ee9c3c4SRandall Stewart  * over-send or the cwnd will block you from sending more.
1753ee9c3c4SRandall Stewart  *
1763ee9c3c4SRandall Stewart  * Note you should also be holding the INP_WLOCK() when you
1773ee9c3c4SRandall Stewart  * call the remove from the hpts as well. Though usually
1783ee9c3c4SRandall Stewart  * you are either doing this from a timer, where you need and have
1793ee9c3c4SRandall Stewart  * the INP_WLOCK() or from destroying your TCB where again
1803ee9c3c4SRandall Stewart  * you should already have the INP_WLOCK().
1813ee9c3c4SRandall Stewart  *
1823ee9c3c4SRandall Stewart  * The inp_hpts_cpu, inp_hpts_cpu_set, inp_input_cpu and
1833ee9c3c4SRandall Stewart  * inp_input_cpu_set fields are controlled completely by
1843ee9c3c4SRandall Stewart  * the hpts. Do not ever set these. The inp_hpts_cpu_set
1853ee9c3c4SRandall Stewart  * and inp_input_cpu_set fields indicate if the hpts has
1863ee9c3c4SRandall Stewart  * setup the respective cpu field. It is advised if this
1873ee9c3c4SRandall Stewart  * field is 0, to enqueue the packet with the appropriate
1883ee9c3c4SRandall Stewart  * hpts_immediate() call. If the _set field is 1, then
1893ee9c3c4SRandall Stewart  * you may compare the inp_*_cpu field to the curcpu and
1903ee9c3c4SRandall Stewart  * may want to again insert onto the hpts if these fields
1913ee9c3c4SRandall Stewart  * are not equal (i.e. you are not on the expected CPU).
1923ee9c3c4SRandall Stewart  *
1933ee9c3c4SRandall Stewart  * A note on inp_hpts_calls and inp_input_calls, these
1943ee9c3c4SRandall Stewart  * flags are set when the hpts calls either the output
1953ee9c3c4SRandall Stewart  * or do_segment routines respectively. If the routine
1963ee9c3c4SRandall Stewart  * being called wants to use this, then it needs to
1973ee9c3c4SRandall Stewart  * clear the flag before returning. The hpts will not
1983ee9c3c4SRandall Stewart  * clear the flag. The flags can be used to tell if
1993ee9c3c4SRandall Stewart  * the hpts is the function calling the respective
2003ee9c3c4SRandall Stewart  * routine.
2013ee9c3c4SRandall Stewart  *
2027b709f8aSRobert Watson  * A few other notes:
2037b709f8aSRobert Watson  *
2047b709f8aSRobert Watson  * When a read lock is held, stability of the field is guaranteed; to write
2057b709f8aSRobert Watson  * to a field, a write lock must generally be held.
2067b709f8aSRobert Watson  *
2077b709f8aSRobert Watson  * netinet/netinet6-layer code should not assume that the inp_socket pointer
20853af6903SGleb Smirnoff  * is safe to dereference without inp_lock being held, there may be
20953af6903SGleb Smirnoff  * close(2)-related races.
2107b709f8aSRobert Watson  *
2117b709f8aSRobert Watson  * The inp_vflag field is overloaded, and would otherwise ideally be (c).
2127b709f8aSRobert Watson  */
213cc65eb4eSGleb Smirnoff struct icmp6_filter;
214cc65eb4eSGleb Smirnoff struct inpcbpolicy;
215f3e7afe2SHans Petter Selasky struct m_snd_tag;
216df8bae1dSRodney W. Grimes struct inpcb {
2176a6cefacSGleb Smirnoff 	/* Cache line #1 (amd64) */
218db0ac6deSCy Schubert 	CK_LIST_ENTRY(inpcb) inp_hash;	/* (w:h/r:e)  hash list */
2196a6cefacSGleb Smirnoff 	struct rwlock	inp_lock;
2206a6cefacSGleb Smirnoff 	/* Cache line #2 (amd64) */
2213ee9c3c4SRandall Stewart #define	inp_start_zero	inp_hpts
2226a6cefacSGleb Smirnoff #define	inp_zero_size	(sizeof(struct inpcb) - \
2236a6cefacSGleb Smirnoff 			    offsetof(struct inpcb, inp_start_zero))
2243ee9c3c4SRandall Stewart 	TAILQ_ENTRY(inpcb) inp_hpts;	/* pacing out queue next lock(b) */
225db0ac6deSCy Schubert 	uint32_t inp_hpts_gencnt;	/* XXXGL */
2263ee9c3c4SRandall Stewart 	uint32_t inp_hpts_request;	/* Current hpts request, zero if
2273ee9c3c4SRandall Stewart 					 * fits in the pacing window (i&b). */
2283ee9c3c4SRandall Stewart 	/*
2293ee9c3c4SRandall Stewart 	 * Note the next fields are protected by a
2303ee9c3c4SRandall Stewart 	 * different lock (hpts-lock). This means that
2313ee9c3c4SRandall Stewart 	 * they must correspond in size to the smallest
2323ee9c3c4SRandall Stewart 	 * protectable bit field (uint8_t on x86, and
2333ee9c3c4SRandall Stewart 	 * other platfomrs potentially uint32_t?). Also
2343ee9c3c4SRandall Stewart 	 * since CPU switches can occur at different times the two
2353ee9c3c4SRandall Stewart 	 * fields can *not* be collapsed into a signal bit field.
2363ee9c3c4SRandall Stewart 	 */
2373ee9c3c4SRandall Stewart #if defined(__amd64__) || defined(__i386__)
238db0ac6deSCy Schubert 	uint8_t inp_in_hpts; /* on output hpts (lock b) */
2393ee9c3c4SRandall Stewart #else
240db0ac6deSCy Schubert 	uint32_t inp_in_hpts; /* on output hpts (lock b) */
2413ee9c3c4SRandall Stewart #endif
2423ee9c3c4SRandall Stewart 	volatile uint16_t  inp_hpts_cpu; /* Lock (i) */
243d7955cc0SRandall Stewart 	volatile uint16_t  inp_irq_cpu;	/* Set by LRO in behalf of or the driver */
2446a6cefacSGleb Smirnoff 	u_int	inp_refcount;		/* (i) refcount */
2456a6cefacSGleb Smirnoff 	int	inp_flags;		/* (i) generic IP/datagram flags */
2466a6cefacSGleb Smirnoff 	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
247db0ac6deSCy Schubert 	uint8_t inp_hpts_cpu_set :1,  /* on output hpts (i) */
2483ee9c3c4SRandall Stewart 			 inp_hpts_calls :1,	/* (i) from output hpts */
249d7955cc0SRandall Stewart 			 inp_irq_cpu_set :1,	/* (i) from LRO/Driver */
250d7955cc0SRandall Stewart 			 inp_spare_bits2 : 3;
25150575ce1SAndrew Gallatin 	uint8_t inp_numa_domain;	/* numa domain */
2527b709f8aSRobert Watson 	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
2536a6cefacSGleb Smirnoff 	struct	socket *inp_socket;	/* (i) back pointer to socket */
254db0ac6deSCy Schubert 	int32_t 	 inp_hptsslot;	/* Hpts wheel slot this tcb is Lock(i&b) */
2553ee9c3c4SRandall Stewart 	uint32_t         inp_hpts_drop_reas;	/* reason we are dropping the PCB (lock i&b) */
2567b709f8aSRobert Watson 	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
25786d02c5cSBjoern A. Zeeb 	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
2587b709f8aSRobert Watson 	u_int32_t inp_flow;		/* (i) IPv6 flow information */
2597b709f8aSRobert Watson 	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
2607b709f8aSRobert Watson 	u_char	inp_ip_ttl;		/* (i) time to live proto */
2617b709f8aSRobert Watson 	u_char	inp_ip_p;		/* (c) protocol proto */
2627b709f8aSRobert Watson 	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
26380cb9f21SKip Macy 	uint32_t inp_flowid;		/* (x) flow id / queue id */
264f3e7afe2SHans Petter Selasky 	struct m_snd_tag *inp_snd_tag;	/* (i) send tag for outgoing mbufs */
26534e3dcedSAdrian Chadd 	uint32_t inp_flowtype;		/* (x) M_HASHTYPE value */
2662de2af32SKip Macy 
2672de2af32SKip Macy 	/* Local and foreign ports, local and foreign addr. */
268ff9b006dSJulien Charbon 	struct	in_conninfo inp_inc;	/* (i) list for PCB's local port */
2692de2af32SKip Macy 
270a5654bb2SRobert Watson 	/* MAC and IPSEC policy information. */
2717b709f8aSRobert Watson 	struct	label *inp_label;	/* (i) MAC label */
2727b709f8aSRobert Watson 	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
27382cd038dSYoshinobu Inoue 
274712fc218SRobert Watson 	/* Protocol-dependent part; options. */
27582cd038dSYoshinobu Inoue 	struct {
276cc65eb4eSGleb Smirnoff 		u_char	inp_ip_tos;		/* (i) type of service proto */
277cc65eb4eSGleb Smirnoff 		struct mbuf		*inp_options;	/* (i) IP options */
278cc65eb4eSGleb Smirnoff 		struct ip_moptions	*inp_moptions;	/* (i) mcast options */
279cc65eb4eSGleb Smirnoff 	};
28082cd038dSYoshinobu Inoue 	struct {
2817b709f8aSRobert Watson 		/* (i) IP options */
282cc65eb4eSGleb Smirnoff 		struct mbuf		*in6p_options;
2837b709f8aSRobert Watson 		/* (i) IP6 options for outgoing packets */
284cc65eb4eSGleb Smirnoff 		struct ip6_pktopts	*in6p_outputopts;
2857b709f8aSRobert Watson 		/* (i) IP multicast options */
286cc65eb4eSGleb Smirnoff 		struct ip6_moptions	*in6p_moptions;
2877b709f8aSRobert Watson 		/* (i) ICMPv6 code type filter */
288cc65eb4eSGleb Smirnoff 		struct icmp6_filter	*in6p_icmp6filt;
2897b709f8aSRobert Watson 		/* (i) IPV6_CHECKSUM setsockopt */
290cc65eb4eSGleb Smirnoff 		int	in6p_cksum;
291cc65eb4eSGleb Smirnoff 		short	in6p_hops;
292cc65eb4eSGleb Smirnoff 	};
293db0ac6deSCy Schubert 	CK_LIST_ENTRY(inpcb) inp_portlist;	/* (r:e/w:h) port list */
294db0ac6deSCy Schubert 	struct	inpcbport *inp_phd;	/* (r:e/w:h) head of this list */
295a5654bb2SRobert Watson 	inp_gen_t	inp_gencnt;	/* (c) generation count */
296113c4fadSBjoern A. Zeeb 	void		*spare_ptr;	/* Spare pointer. */
29784cc0778SGeorge V. Neville-Neil 	rt_gen_t	inp_rt_cookie;	/* generation for route entry */
29884cc0778SGeorge V. Neville-Neil 	union {				/* cached L3 information */
2996a6cefacSGleb Smirnoff 		struct route inp_route;
3006a6cefacSGleb Smirnoff 		struct route_in6 inp_route6;
3016a6cefacSGleb Smirnoff 	};
302db0ac6deSCy Schubert 	CK_LIST_ENTRY(inpcb) inp_list;	/* (r:e/w:p) all PCBs for proto */
303a5654bb2SRobert Watson };
304cc65eb4eSGleb Smirnoff #endif	/* _KERNEL */
305cc65eb4eSGleb Smirnoff 
306a5654bb2SRobert Watson #define	inp_fport	inp_inc.inc_fport
307a5654bb2SRobert Watson #define	inp_lport	inp_inc.inc_lport
308a5654bb2SRobert Watson #define	inp_faddr	inp_inc.inc_faddr
309a5654bb2SRobert Watson #define	inp_laddr	inp_inc.inc_laddr
310f76fcf6dSJeffrey Hsu 
311be2ac88cSJonathan Lemon #define	in6p_faddr	inp_inc.inc6_faddr
312be2ac88cSJonathan Lemon #define	in6p_laddr	inp_inc.inc6_laddr
313028bdf28SAndrey V. Elsukov #define	in6p_zoneid	inp_inc.inc6_zoneid
314a5654bb2SRobert Watson 
315f6dfe47aSMarko Zec #define	inp_vnet	inp_pcbinfo->ipi_vnet
316f6dfe47aSMarko Zec 
3173d4d47f3SGarrett Wollman /*
318712fc218SRobert Watson  * The range of the generation count, as used in this implementation, is 9e19.
319712fc218SRobert Watson  * We would have to create 300 billion connections per second for this number
320712fc218SRobert Watson  * to roll over in a year.  This seems sufficiently unlikely that we simply
321712fc218SRobert Watson  * don't concern ourselves with that possibility.
3223d4d47f3SGarrett Wollman  */
323c3229e05SDavid Greenman 
32498271db4SGarrett Wollman /*
325712fc218SRobert Watson  * Interface exported to userland by various protocols which use inpcbs.  Hack
326712fc218SRobert Watson  * alert -- only define if struct xsocket is in scope.
327cc65eb4eSGleb Smirnoff  * Fields prefixed with "xi_" are unique to this structure, and the rest
328cc65eb4eSGleb Smirnoff  * match fields in the struct inpcb, to ease coding and porting.
329cc65eb4eSGleb Smirnoff  *
330cc65eb4eSGleb Smirnoff  * Legend:
331cc65eb4eSGleb Smirnoff  * (s) - used by userland utilities in src
332cc65eb4eSGleb Smirnoff  * (p) - used by utilities in ports
333cc65eb4eSGleb Smirnoff  * (3) - is known to be used by third party software not in ports
334cc65eb4eSGleb Smirnoff  * (n) - no known usage
33598271db4SGarrett Wollman  */
33698271db4SGarrett Wollman #ifdef _SYS_SOCKETVAR_H_
33798271db4SGarrett Wollman struct xinpcb {
338f38b68aeSBrooks Davis 	ksize_t		xi_len;			/* length of this structure */
339cc65eb4eSGleb Smirnoff 	struct xsocket	xi_socket;		/* (s,p) */
340cc65eb4eSGleb Smirnoff 	struct in_conninfo inp_inc;		/* (s,p) */
341cc65eb4eSGleb Smirnoff 	uint64_t	inp_gencnt;		/* (s,p) */
342f38b68aeSBrooks Davis 	kvaddr_t	inp_ppcb;		/* (s) netstat(1) */
343cc65eb4eSGleb Smirnoff 	int64_t		inp_spare64[4];
344cc65eb4eSGleb Smirnoff 	uint32_t	inp_flow;		/* (s) */
345cc65eb4eSGleb Smirnoff 	uint32_t	inp_flowid;		/* (s) */
346cc65eb4eSGleb Smirnoff 	uint32_t	inp_flowtype;		/* (s) */
347cc65eb4eSGleb Smirnoff 	int32_t		inp_flags;		/* (s,p) */
348cc65eb4eSGleb Smirnoff 	int32_t		inp_flags2;		/* (s) */
349*317fa516SMark Johnston 	uint32_t	inp_unused;
350cc65eb4eSGleb Smirnoff 	int32_t		in6p_cksum;		/* (n) */
351cc65eb4eSGleb Smirnoff 	int32_t		inp_spare32[4];
352cc65eb4eSGleb Smirnoff 	uint16_t	in6p_hops;		/* (n) */
353cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_tos;		/* (n) */
354cc65eb4eSGleb Smirnoff 	int8_t		pad8;
355cc65eb4eSGleb Smirnoff 	uint8_t		inp_vflag;		/* (s,p) */
356cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_ttl;		/* (n) */
357cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_p;		/* (n) */
358cc65eb4eSGleb Smirnoff 	uint8_t		inp_ip_minttl;		/* (n) */
359cc65eb4eSGleb Smirnoff 	int8_t		inp_spare8[4];
360cc65eb4eSGleb Smirnoff } __aligned(8);
36198271db4SGarrett Wollman 
36298271db4SGarrett Wollman struct xinpgen {
363f38b68aeSBrooks Davis 	ksize_t	xig_len;	/* length of this structure */
36498271db4SGarrett Wollman 	u_int		xig_count;	/* number of PCBs at this time */
365f38b68aeSBrooks Davis 	uint32_t	_xig_spare32;
36698271db4SGarrett Wollman 	inp_gen_t	xig_gen;	/* generation count at this time */
367cc65eb4eSGleb Smirnoff 	so_gen_t	xig_sogen;	/* socket generation count this time */
368f38b68aeSBrooks Davis 	uint64_t	_xig_spare64[4];
3693ae4b0e7SGleb Smirnoff } __aligned(8);
370a35bdd44SMichael Tuexen 
371a35bdd44SMichael Tuexen struct sockopt_parameters {
372a35bdd44SMichael Tuexen 	struct in_conninfo sop_inc;
373a35bdd44SMichael Tuexen 	uint64_t sop_id;
374a35bdd44SMichael Tuexen 	int sop_level;
375a35bdd44SMichael Tuexen 	int sop_optname;
376a35bdd44SMichael Tuexen 	char sop_optval[];
377a35bdd44SMichael Tuexen };
378a35bdd44SMichael Tuexen 
379cc65eb4eSGleb Smirnoff #ifdef	_KERNEL
380a35bdd44SMichael Tuexen int	sysctl_setsockopt(SYSCTL_HANDLER_ARGS, struct inpcbinfo *pcbinfo,
381a35bdd44SMichael Tuexen 	    int (*ctloutput_set)(struct inpcb *, struct sockopt *));
382cc65eb4eSGleb Smirnoff void	in_pcbtoxinpcb(const struct inpcb *, struct xinpcb *);
383cc65eb4eSGleb Smirnoff #endif
38498271db4SGarrett Wollman #endif /* _SYS_SOCKETVAR_H_ */
38598271db4SGarrett Wollman 
386db0ac6deSCy Schubert #ifdef _KERNEL
387db0ac6deSCy Schubert /*
388fec8a8c7SGleb Smirnoff  * Per-VNET pcb database for each high-level protocol (UDP, TCP, ...) in both
389fec8a8c7SGleb Smirnoff  * IPv4 and IPv6.
39082a5be49SRobert Watson  *
391db0ac6deSCy Schubert  * The pcbs are protected with SMR section and thus all lists in inpcbinfo
392db0ac6deSCy Schubert  * are CK-lists.  Locking is required to insert a pcb into database. Two
393db0ac6deSCy Schubert  * locks are provided: one for the hash and one for the global list of pcbs,
394db0ac6deSCy Schubert  * as well as overall count and generation count.
39582a5be49SRobert Watson  *
39682a5be49SRobert Watson  * Locking key:
39782a5be49SRobert Watson  *
39882a5be49SRobert Watson  * (c) Constant or nearly constant after initialisation
399db0ac6deSCy Schubert  * (e) Protected by SMR section
40082a5be49SRobert Watson  * (g) Locked by ipi_lock
401db0ac6deSCy Schubert  * (h) Locked by ipi_hash_lock
402712fc218SRobert Watson  */
403712fc218SRobert Watson struct inpcbinfo {
404712fc218SRobert Watson 	/*
4059e58ff6fSMatt Macy 	 * Global lock protecting inpcb list modification
40682a5be49SRobert Watson 	 */
4079e58ff6fSMatt Macy 	struct mtx		 ipi_lock;
408db0ac6deSCy Schubert 	struct inpcbhead	 ipi_listhead;		/* (r:e/w:g) */
409db0ac6deSCy Schubert 	u_int			 ipi_count;		/* (g) */
410712fc218SRobert Watson 
411712fc218SRobert Watson 	/*
412712fc218SRobert Watson 	 * Generation count -- incremented each time a connection is allocated
413712fc218SRobert Watson 	 * or freed.
414712fc218SRobert Watson 	 */
415db0ac6deSCy Schubert 	u_quad_t		 ipi_gencnt;		/* (g) */
41682a5be49SRobert Watson 
41782a5be49SRobert Watson 	/*
41882a5be49SRobert Watson 	 * Fields associated with port lookup and allocation.
41982a5be49SRobert Watson 	 */
420db0ac6deSCy Schubert 	u_short			 ipi_lastport;		/* (h) */
421db0ac6deSCy Schubert 	u_short			 ipi_lastlow;		/* (h) */
422db0ac6deSCy Schubert 	u_short			 ipi_lasthi;		/* (h) */
42382a5be49SRobert Watson 
42482a5be49SRobert Watson 	/*
42582a5be49SRobert Watson 	 * UMA zone from which inpcbs are allocated for this protocol.
42682a5be49SRobert Watson 	 */
427db0ac6deSCy Schubert 	uma_zone_t		 ipi_zone;		/* (c) */
428db0ac6deSCy Schubert 	uma_zone_t		 ipi_portzone;		/* (c) */
429db0ac6deSCy Schubert 	smr_t			 ipi_smr;		/* (c) */
430fa046d87SRobert Watson 
431fa046d87SRobert Watson 	/*
43282a5be49SRobert Watson 	 * Global hash of inpcbs, hashed by local and foreign addresses and
43382a5be49SRobert Watson 	 * port numbers.
43482a5be49SRobert Watson 	 */
435db0ac6deSCy Schubert 	struct mtx		 ipi_hash_lock;
436db0ac6deSCy Schubert 	struct inpcbhead 	*ipi_hashbase;		/* (r:e/w:h) */
437db0ac6deSCy Schubert 	u_long			 ipi_hashmask;		/* (c) */
43882a5be49SRobert Watson 
43982a5be49SRobert Watson 	/*
44082a5be49SRobert Watson 	 * Global hash of inpcbs, hashed by only local port number.
44182a5be49SRobert Watson 	 */
442fa046d87SRobert Watson 	struct inpcbporthead	*ipi_porthashbase;	/* (h) */
443fa046d87SRobert Watson 	u_long			 ipi_porthashmask;	/* (h) */
4442de2af32SKip Macy 
4452de2af32SKip Macy 	/*
4461a43cff9SSean Bruno 	 * Load balance groups used for the SO_REUSEPORT_LB option,
4471a43cff9SSean Bruno 	 * hashed by local port.
4481a43cff9SSean Bruno 	 */
449db0ac6deSCy Schubert 	struct	inpcblbgrouphead *ipi_lbgrouphashbase;	/* (r:e/w:h) */
4501a43cff9SSean Bruno 	u_long			 ipi_lbgrouphashmask;	/* (h) */
4511a43cff9SSean Bruno 
4521a43cff9SSean Bruno 	/*
453f6dfe47aSMarko Zec 	 * Pointer to network stack instance
454f6dfe47aSMarko Zec 	 */
45582a5be49SRobert Watson 	struct vnet		*ipi_vnet;		/* (c) */
45615bd2b43SDavid Greenman };
45715bd2b43SDavid Greenman 
45852cd27cbSRobert Watson /*
459fec8a8c7SGleb Smirnoff  * Global allocation storage for each high-level protocol (UDP, TCP, ...).
460fec8a8c7SGleb Smirnoff  * Each corresponding per-VNET inpcbinfo points into this one.
461fec8a8c7SGleb Smirnoff  */
462fec8a8c7SGleb Smirnoff struct inpcbstorage {
463fec8a8c7SGleb Smirnoff 	uma_zone_t	ips_zone;
464fec8a8c7SGleb Smirnoff 	uma_zone_t	ips_portzone;
465fec8a8c7SGleb Smirnoff 	uma_init	ips_pcbinit;
4660aa120d5SGleb Smirnoff 	size_t		ips_size;
467fec8a8c7SGleb Smirnoff 	const char *	ips_zone_name;
468fec8a8c7SGleb Smirnoff 	const char *	ips_portzone_name;
469fec8a8c7SGleb Smirnoff 	const char *	ips_infolock_name;
470fec8a8c7SGleb Smirnoff 	const char *	ips_hashlock_name;
471fec8a8c7SGleb Smirnoff };
472fec8a8c7SGleb Smirnoff 
4730aa120d5SGleb Smirnoff #define INPCBSTORAGE_DEFINE(prot, ppcb, lname, zname, iname, hname)	\
474fec8a8c7SGleb Smirnoff static int								\
475fec8a8c7SGleb Smirnoff prot##_inpcb_init(void *mem, int size __unused, int flags __unused)	\
476fec8a8c7SGleb Smirnoff {									\
477fec8a8c7SGleb Smirnoff 	struct inpcb *inp = mem;					\
478fec8a8c7SGleb Smirnoff 									\
479fec8a8c7SGleb Smirnoff 	rw_init_flags(&inp->inp_lock, lname, RW_RECURSE | RW_DUPOK);	\
480fec8a8c7SGleb Smirnoff 	return (0);							\
481fec8a8c7SGleb Smirnoff }									\
482fec8a8c7SGleb Smirnoff static struct inpcbstorage prot = {					\
4830aa120d5SGleb Smirnoff 	.ips_size = sizeof(struct ppcb),				\
484fec8a8c7SGleb Smirnoff 	.ips_pcbinit = prot##_inpcb_init,				\
485fec8a8c7SGleb Smirnoff 	.ips_zone_name = zname,						\
486fec8a8c7SGleb Smirnoff 	.ips_portzone_name = zname " ports",				\
487fec8a8c7SGleb Smirnoff 	.ips_infolock_name = iname,					\
488fec8a8c7SGleb Smirnoff 	.ips_hashlock_name = hname,					\
489fec8a8c7SGleb Smirnoff };									\
490fec8a8c7SGleb Smirnoff SYSINIT(prot##_inpcbstorage_init, SI_SUB_PROTO_DOMAIN,			\
491fec8a8c7SGleb Smirnoff     SI_ORDER_SECOND, in_pcbstorage_init, &prot);			\
492fec8a8c7SGleb Smirnoff SYSUNINIT(prot##_inpcbstorage_uninit, SI_SUB_PROTO_DOMAIN,		\
493fec8a8c7SGleb Smirnoff     SI_ORDER_SECOND, in_pcbstorage_destroy, &prot)
494fec8a8c7SGleb Smirnoff 
495fec8a8c7SGleb Smirnoff /*
4961a43cff9SSean Bruno  * Load balance groups used for the SO_REUSEPORT_LB socket option. Each group
4971a43cff9SSean Bruno  * (or unique address:port combination) can be re-used at most
4981a43cff9SSean Bruno  * INPCBLBGROUP_SIZMAX (256) times. The inpcbs are stored in il_inp which
4991a43cff9SSean Bruno  * is dynamically resized as processes bind/unbind to that specific group.
5001a43cff9SSean Bruno  */
5011a43cff9SSean Bruno struct inpcblbgroup {
50254af3d0dSMark Johnston 	CK_LIST_ENTRY(inpcblbgroup) il_list;
50354af3d0dSMark Johnston 	struct epoch_context il_epoch_ctx;
504d93ec8cbSMark Johnston 	struct ucred	*il_cred;
5051a43cff9SSean Bruno 	uint16_t	il_lport;			/* (c) */
5061a43cff9SSean Bruno 	u_char		il_vflag;			/* (c) */
507d93ec8cbSMark Johnston 	uint8_t		il_numa_domain;
5081a43cff9SSean Bruno 	uint32_t	il_pad2;
5091a43cff9SSean Bruno 	union in_dependaddr il_dependladdr;		/* (c) */
5101a43cff9SSean Bruno #define	il_laddr	il_dependladdr.id46_addr.ia46_addr4
5111a43cff9SSean Bruno #define	il6_laddr	il_dependladdr.id6_addr
5121a43cff9SSean Bruno 	uint32_t	il_inpsiz; /* max count in il_inp[] (h) */
5131a43cff9SSean Bruno 	uint32_t	il_inpcnt; /* cur count in il_inp[] (h) */
5141a43cff9SSean Bruno 	struct inpcb	*il_inp[];			/* (h) */
5151a43cff9SSean Bruno };
5161a43cff9SSean Bruno 
5178501a69cSRobert Watson #define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
5188501a69cSRobert Watson #define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
5198501a69cSRobert Watson #define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
52043cc0bc1SRobert Watson #define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
52143cc0bc1SRobert Watson #define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
5228501a69cSRobert Watson #define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
5238501a69cSRobert Watson #define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
5245a126433SGleb Smirnoff #define INP_UNLOCK(inp)		rw_unlock(&(inp)->inp_lock)
52582c33e73SKip Macy #define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
52682c33e73SKip Macy #define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
52782c33e73SKip Macy #define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
5288501a69cSRobert Watson #define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
5298501a69cSRobert Watson #define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
5308501a69cSRobert Watson #define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
5318501a69cSRobert Watson #define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
532f76fcf6dSJeffrey Hsu 
5333d585327SKip Macy /*
5343d585327SKip Macy  * These locking functions are for inpcb consumers outside of sys/netinet,
5353d585327SKip Macy  * more specifically, they were added for the benefit of TOE drivers. The
5363d585327SKip Macy  * macros are reserved for use by the stack.
5373d585327SKip Macy  */
5383d585327SKip Macy void inp_wlock(struct inpcb *);
5393d585327SKip Macy void inp_wunlock(struct inpcb *);
5403d585327SKip Macy void inp_rlock(struct inpcb *);
5413d585327SKip Macy void inp_runlock(struct inpcb *);
5423d585327SKip Macy 
543c75e2666SGleb Smirnoff #ifdef INVARIANT_SUPPORT
544e79dd20dSKip Macy void inp_lock_assert(struct inpcb *);
545e79dd20dSKip Macy void inp_unlock_assert(struct inpcb *);
5463d585327SKip Macy #else
547c75e2666SGleb Smirnoff #define	inp_lock_assert(inp)	do {} while (0)
548c75e2666SGleb Smirnoff #define	inp_unlock_assert(inp)	do {} while (0)
5493d585327SKip Macy #endif
5509378e437SKip Macy 
55124cf7a8dSGleb Smirnoff void	inp_apply_all(struct inpcbinfo *, void (*func)(struct inpcb *, void *),
55224cf7a8dSGleb Smirnoff 	    void *arg);
5539378e437SKip Macy int 	inp_ip_tos_get(const struct inpcb *inp);
5549378e437SKip Macy void 	inp_ip_tos_set(struct inpcb *inp, int val);
5559d29c635SKip Macy struct socket *
5569d29c635SKip Macy 	inp_inpcbtosocket(struct inpcb *inp);
5579d29c635SKip Macy struct tcpcb *
5589d29c635SKip Macy 	inp_inpcbtotcpcb(struct inpcb *inp);
559df9cf830STai-hwa Liang void 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
5609d29c635SKip Macy 		uint32_t *faddr, uint16_t *fp);
5611a43cff9SSean Bruno int	inp_so_options(const struct inpcb *inp);
5629378e437SKip Macy 
563e79dd20dSKip Macy #endif /* _KERNEL */
5643d585327SKip Macy 
5659e58ff6fSMatt Macy #define INP_INFO_WLOCK(ipi)	mtx_lock(&(ipi)->ipi_lock)
5669e58ff6fSMatt Macy #define INP_INFO_WLOCKED(ipi)	mtx_owned(&(ipi)->ipi_lock)
5679e58ff6fSMatt Macy #define INP_INFO_WUNLOCK(ipi)	mtx_unlock(&(ipi)->ipi_lock)
568db0ac6deSCy Schubert #define	INP_INFO_LOCK_ASSERT(ipi)	MPASS(SMR_ENTERED((ipi)->ipi_smr) || \
569db0ac6deSCy Schubert 					mtx_owned(&(ipi)->ipi_lock))
5709e58ff6fSMatt Macy #define INP_INFO_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_lock, MA_OWNED)
571384a5c3cSAndrey V. Elsukov #define INP_INFO_WUNLOCK_ASSERT(ipi)	\
572384a5c3cSAndrey V. Elsukov 				mtx_assert(&(ipi)->ipi_lock, MA_NOTOWNED)
573f76fcf6dSJeffrey Hsu 
5749e58ff6fSMatt Macy #define	INP_HASH_WLOCK(ipi)		mtx_lock(&(ipi)->ipi_hash_lock)
5759e58ff6fSMatt Macy #define	INP_HASH_WUNLOCK(ipi)		mtx_unlock(&(ipi)->ipi_hash_lock)
576db0ac6deSCy Schubert #define	INP_HASH_LOCK_ASSERT(ipi)	MPASS(SMR_ENTERED((ipi)->ipi_smr) || \
577db0ac6deSCy Schubert 					mtx_owned(&(ipi)->ipi_hash_lock))
578db0ac6deSCy Schubert #define	INP_HASH_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_hash_lock, \
579db0ac6deSCy Schubert 					MA_OWNED)
58052cd27cbSRobert Watson 
581a0577692SGleb Smirnoff /*
582a0577692SGleb Smirnoff  * Wildcard matching hash is not just a microoptimisation!  The hash for
583a0577692SGleb Smirnoff  * wildcard IPv4 and wildcard IPv6 must be the same, otherwise AF_INET6
584a0577692SGleb Smirnoff  * wildcard bound pcb won't be able to receive AF_INET connections, while:
585a0577692SGleb Smirnoff  * jenkins_hash(&zeroes, 1, s) != jenkins_hash(&zeroes, 4, s)
586a0577692SGleb Smirnoff  * See also comment above struct in_addr_4in6.
587a0577692SGleb Smirnoff  */
588a0577692SGleb Smirnoff #define	IN_ADDR_JHASH32(addr)						\
589a0577692SGleb Smirnoff 	((addr)->s_addr == INADDR_ANY ? V_in_pcbhashseed :		\
590a0577692SGleb Smirnoff 	    jenkins_hash32((&(addr)->s_addr), 1, V_in_pcbhashseed))
591a0577692SGleb Smirnoff #define	IN6_ADDR_JHASH32(addr)						\
592a0577692SGleb Smirnoff 	(memcmp((addr), &in6addr_any, sizeof(in6addr_any)) == 0 ?	\
593a0577692SGleb Smirnoff 	    V_in_pcbhashseed :						\
594a0577692SGleb Smirnoff 	    jenkins_hash32((addr)->__u6_addr.__u6_addr32,		\
595a0577692SGleb Smirnoff 	    nitems((addr)->__u6_addr.__u6_addr32), V_in_pcbhashseed))
596a0577692SGleb Smirnoff 
597ddd79a97SDavid Greenman #define INP_PCBHASH(faddr, lport, fport, mask)				\
598a0577692SGleb Smirnoff 	((IN_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) & (mask))
599a0577692SGleb Smirnoff #define	INP6_PCBHASH(faddr, lport, fport, mask)				\
600a0577692SGleb Smirnoff 	((IN6_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport))) & (mask))
601a0577692SGleb Smirnoff 
602a0577692SGleb Smirnoff #define	INP_PCBHASH_WILD(lport, mask)					\
603a0577692SGleb Smirnoff 	((V_in_pcbhashseed ^ ntohs(lport)) & (mask))
604a0577692SGleb Smirnoff 
6051a43cff9SSean Bruno #define	INP_PCBLBGROUP_PKTHASH(faddr, lport, fport)			\
606a0577692SGleb Smirnoff 	(IN_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport)))
607a0577692SGleb Smirnoff #define	INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport)			\
608a0577692SGleb Smirnoff 	(IN6_ADDR_JHASH32(faddr) ^ ntohs((lport) ^ (fport)))
609a0577692SGleb Smirnoff 
610a0577692SGleb Smirnoff #define INP_PCBPORTHASH(lport, mask)	(ntohs((lport)) & (mask))
611ddd79a97SDavid Greenman 
612a5654bb2SRobert Watson /*
613773b573aSKip Macy  * Flags for inp_vflags -- historically version flags only
614a5654bb2SRobert Watson  */
615a5654bb2SRobert Watson #define	INP_IPV4	0x1
616a5654bb2SRobert Watson #define	INP_IPV6	0x2
617a5654bb2SRobert Watson #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
618a5654bb2SRobert Watson 
619a5654bb2SRobert Watson /*
620773b573aSKip Macy  * Flags for inp_flags.
621a5654bb2SRobert Watson  */
622ad71fe3cSRobert Watson #define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
623ad71fe3cSRobert Watson #define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
624ad71fe3cSRobert Watson #define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
625ad71fe3cSRobert Watson #define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
626ad71fe3cSRobert Watson #define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
627ad71fe3cSRobert Watson #define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
62809d3671bSGleb Smirnoff #define	INP_ANONPORT		0x00000040 /* read by netstat(1) */
629ad71fe3cSRobert Watson #define	INP_RECVIF		0x00000080 /* receive incoming interface */
630ad71fe3cSRobert Watson #define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
631db0ac6deSCy Schubert /*	INP_FREED		0x00000200 private to in_pcb.c */
632ad71fe3cSRobert Watson #define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
633ad71fe3cSRobert Watson #define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
634f44270e7SPawel Jakub Dawidek #define	INP_BINDANY		0x00001000 /* allow bind to any address */
635ad71fe3cSRobert Watson #define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
6363cca425bSMichael Tuexen #define	INP_RECVTOS		0x00004000 /* receive incoming IP TOS */
637ad71fe3cSRobert Watson #define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
638ad71fe3cSRobert Watson #define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
639ad71fe3cSRobert Watson #define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
640ad71fe3cSRobert Watson #define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
641ad71fe3cSRobert Watson #define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
642ad71fe3cSRobert Watson #define	IN6P_RTHDR		0x00100000 /* receive routing header */
643ad71fe3cSRobert Watson #define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
644ad71fe3cSRobert Watson #define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
645ad71fe3cSRobert Watson #define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
64653af6903SGleb Smirnoff /* was	INP_TIMEWAIT		0x01000000 */
647ad71fe3cSRobert Watson #define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
648ad71fe3cSRobert Watson #define	INP_DROPPED		0x04000000 /* protocol drop flag */
649ad71fe3cSRobert Watson #define	INP_SOCKREF		0x08000000 /* strong socket reference */
650c2529042SHans Petter Selasky #define	INP_RESERVED_0          0x10000000 /* reserved field */
651c2529042SHans Petter Selasky #define	INP_RESERVED_1          0x20000000 /* reserved field */
652f95d4633SHajimu UMEMOTO #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
653f95d4633SHajimu UMEMOTO #define	IN6P_MTU		0x80000000 /* receive path MTU */
65433841545SHajimu UMEMOTO 
65582c23ebaSBill Fenner #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
6563cca425bSMichael Tuexen 				 INP_RECVIF|INP_RECVTTL|INP_RECVTOS|\
65733841545SHajimu UMEMOTO 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
65833841545SHajimu UMEMOTO 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
659f95d4633SHajimu UMEMOTO 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
660f95d4633SHajimu UMEMOTO 				 IN6P_MTU)
66182cd038dSYoshinobu Inoue 
662773b573aSKip Macy /*
663773b573aSKip Macy  * Flags for inp_flags2.
664773b573aSKip Macy  */
66569a34e8dSRandall Stewart #define	INP_MBUF_L_ACKS		0x00000001 /* We need large mbufs for ack compression */
66669a34e8dSRandall Stewart #define	INP_MBUF_ACKCMP		0x00000002 /* TCP mbuf ack compression ok */
667db0ac6deSCy Schubert /*				0x00000004 */
668fc06cd42SMikolaj Golub #define	INP_REUSEPORT		0x00000008 /* SO_REUSEPORT option is set */
669db0ac6deSCy Schubert /*				0x00000010 */
670efdf104bSMikolaj Golub #define	INP_REUSEADDR		0x00000020 /* SO_REUSEADDR option is set */
671*317fa516SMark Johnston /*				0x00000040 */
672*317fa516SMark Johnston /*				0x00000080 */
6739d3ddf43SAdrian Chadd #define	INP_RECVFLOWID		0x00000100 /* populate recv datagram with flow info */
6749d3ddf43SAdrian Chadd #define	INP_RECVRSSBUCKETID	0x00000200 /* populate recv datagram with bucket id */
675f3e7afe2SHans Petter Selasky #define	INP_RATE_LIMIT_CHANGED	0x00000400 /* rate limit needs attention */
676dce33a45SErmal Luçi #define	INP_ORIGDSTADDR		0x00000800 /* receive IP dst address/port */
6773ee9c3c4SRandall Stewart #define INP_CANNOT_DO_ECN	0x00001000 /* The stack does not do ECN */
6781a43cff9SSean Bruno #define	INP_REUSEPORT_LB	0x00002000 /* SO_REUSEPORT_LB option is set */
6793b0b41e6SRandall Stewart #define INP_SUPPORTS_MBUFQ	0x00004000 /* Supports the mbuf queue method of LRO */
6803b0b41e6SRandall Stewart #define INP_MBUF_QUEUE_READY	0x00008000 /* The transport is pacing, inputs can be queued */
6813b0b41e6SRandall Stewart #define INP_DONT_SACK_QUEUE	0x00010000 /* If a sack arrives do not wake me */
682868aabb4SRichard Scheffenegger #define INP_2PCP_SET		0x00020000 /* If the Eth PCP should be set explicitly */
683868aabb4SRichard Scheffenegger #define INP_2PCP_BIT0		0x00040000 /* Eth PCP Bit 0 */
684868aabb4SRichard Scheffenegger #define INP_2PCP_BIT1		0x00080000 /* Eth PCP Bit 1 */
685868aabb4SRichard Scheffenegger #define INP_2PCP_BIT2		0x00100000 /* Eth PCP Bit 2 */
686868aabb4SRichard Scheffenegger #define INP_2PCP_BASE	INP_2PCP_BIT0
687868aabb4SRichard Scheffenegger #define INP_2PCP_MASK	(INP_2PCP_BIT0 | INP_2PCP_BIT1 | INP_2PCP_BIT2)
688868aabb4SRichard Scheffenegger #define INP_2PCP_SHIFT		18         /* shift PCP field in/out of inp_flags2 */
689db0ac6deSCy Schubert 
690fa046d87SRobert Watson /*
691db0ac6deSCy Schubert  * Flags passed to in_pcblookup*(), inp_smr_lock() and inp_next().
692fa046d87SRobert Watson  */
693db0ac6deSCy Schubert typedef	enum {
694db0ac6deSCy Schubert 	INPLOOKUP_WILDCARD = 0x00000001,	/* Allow wildcard sockets. */
695db0ac6deSCy Schubert 	INPLOOKUP_RLOCKPCB = 0x00000002,	/* Return inpcb read-locked. */
696db0ac6deSCy Schubert 	INPLOOKUP_WLOCKPCB = 0x00000004,	/* Return inpcb write-locked. */
697db0ac6deSCy Schubert } inp_lookup_t;
698fa046d87SRobert Watson 
699fa046d87SRobert Watson #define	INPLOOKUP_MASK	(INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB | \
7001db08fbeSGleb Smirnoff 	    INPLOOKUP_WLOCKPCB)
701db0ac6deSCy Schubert #define	INPLOOKUP_LOCKMASK	(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)
702fa046d87SRobert Watson 
703df8bae1dSRodney W. Grimes #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
70482cd038dSYoshinobu Inoue 
70582cd038dSYoshinobu Inoue #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
70682cd038dSYoshinobu Inoue 
70782cd038dSYoshinobu Inoue #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
708df8bae1dSRodney W. Grimes 
709664a31e4SPeter Wemm #ifdef _KERNEL
710eddfbb76SRobert Watson VNET_DECLARE(int, ipport_reservedhigh);
711eddfbb76SRobert Watson VNET_DECLARE(int, ipport_reservedlow);
712eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lowfirstauto);
713eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lowlastauto);
714eddfbb76SRobert Watson VNET_DECLARE(int, ipport_firstauto);
715eddfbb76SRobert Watson VNET_DECLARE(int, ipport_lastauto);
716eddfbb76SRobert Watson VNET_DECLARE(int, ipport_hifirstauto);
717eddfbb76SRobert Watson VNET_DECLARE(int, ipport_hilastauto);
718eddfbb76SRobert Watson VNET_DECLARE(int, ipport_randomized);
719eddfbb76SRobert Watson 
7201e77c105SRobert Watson #define	V_ipport_reservedhigh	VNET(ipport_reservedhigh)
7211e77c105SRobert Watson #define	V_ipport_reservedlow	VNET(ipport_reservedlow)
7221e77c105SRobert Watson #define	V_ipport_lowfirstauto	VNET(ipport_lowfirstauto)
7231e77c105SRobert Watson #define	V_ipport_lowlastauto	VNET(ipport_lowlastauto)
7241e77c105SRobert Watson #define	V_ipport_firstauto	VNET(ipport_firstauto)
7251e77c105SRobert Watson #define	V_ipport_lastauto	VNET(ipport_lastauto)
7261e77c105SRobert Watson #define	V_ipport_hifirstauto	VNET(ipport_hifirstauto)
7271e77c105SRobert Watson #define	V_ipport_hilastauto	VNET(ipport_hilastauto)
7281e77c105SRobert Watson #define	V_ipport_randomized	VNET(ipport_randomized)
729eddfbb76SRobert Watson 
730fec8a8c7SGleb Smirnoff void	in_pcbinfo_init(struct inpcbinfo *, struct inpcbstorage *,
731fec8a8c7SGleb Smirnoff 	    u_int, u_int);
7329bcd427bSRobert Watson void	in_pcbinfo_destroy(struct inpcbinfo *);
733fec8a8c7SGleb Smirnoff void	in_pcbstorage_init(void *);
734fec8a8c7SGleb Smirnoff void	in_pcbstorage_destroy(void *);
73552cd27cbSRobert Watson 
736f76fcf6dSJeffrey Hsu void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
737d915b280SStephan Uphoff int	in_pcballoc(struct socket *, struct inpcbinfo *);
73896871af0SGleb Smirnoff int	in_pcbbind(struct inpcb *, struct sockaddr_in *, struct ucred *);
73996871af0SGleb Smirnoff int	in_pcbbind_setup(struct inpcb *, struct sockaddr_in *, in_addr_t *,
740b0330ed9SPawel Jakub Dawidek 	    u_short *, struct ucred *);
741a9d22cceSGleb Smirnoff int	in_pcbconnect(struct inpcb *, struct sockaddr_in *, struct ucred *,
742a9d22cceSGleb Smirnoff 	    bool);
743a9d22cceSGleb Smirnoff int	in_pcbconnect_setup(struct inpcb *, struct sockaddr_in *, in_addr_t *,
7449e46ff4dSGleb Smirnoff 	    u_short *, in_addr_t *, u_short *, struct ucred *);
7454d77a549SAlfred Perlstein void	in_pcbdetach(struct inpcb *);
7464d77a549SAlfred Perlstein void	in_pcbdisconnect(struct inpcb *);
74710702a28SRobert Watson void	in_pcbdrop(struct inpcb *);
7484c7c478dSRobert Watson void	in_pcbfree(struct inpcb *);
7494d77a549SAlfred Perlstein int	in_pcbinshash(struct inpcb *);
750ae190832SSteven Hartland int	in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *,
751ae190832SSteven Hartland 	    struct ucred *);
752a034518aSAndrew Gallatin int	in_pcblbgroup_numa(struct inpcb *, int arg);
753df8bae1dSRodney W. Grimes struct inpcb *
754fa046d87SRobert Watson 	in_pcblookup(struct inpcbinfo *, struct in_addr, u_int,
755c1cd65baSBruce Evans 	    struct in_addr, u_int, int, struct ifnet *);
756d3c1f003SRobert Watson struct inpcb *
757d3c1f003SRobert Watson 	in_pcblookup_mbuf(struct inpcbinfo *, struct in_addr, u_int,
758d3c1f003SRobert Watson 	    struct in_addr, u_int, int, struct ifnet *, struct mbuf *);
759f76fcf6dSJeffrey Hsu void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
7603ce144eaSJeffrey Hsu 	    int, struct inpcb *(*)(struct inpcb *, int));
76128696211SRobert Watson void	in_pcbref(struct inpcb *);
7624d77a549SAlfred Perlstein void	in_pcbrehash(struct inpcb *);
763db0ac6deSCy Schubert bool	in_pcbrele_rlocked(struct inpcb *);
764db0ac6deSCy Schubert bool	in_pcbrele_wlocked(struct inpcb *);
765db0ac6deSCy Schubert 
766db0ac6deSCy Schubert typedef bool inp_match_t(const struct inpcb *, void *);
767db0ac6deSCy Schubert struct inpcb_iterator {
768db0ac6deSCy Schubert 	const struct inpcbinfo	*ipi;
769db0ac6deSCy Schubert 	struct inpcb		*inp;
770db0ac6deSCy Schubert 	inp_match_t		*match;
771db0ac6deSCy Schubert 	void			*ctx;
772db0ac6deSCy Schubert 	int			hash;
773db0ac6deSCy Schubert #define	INP_ALL_LIST		-1
774db0ac6deSCy Schubert 	const inp_lookup_t	lock;
775db0ac6deSCy Schubert };
776db0ac6deSCy Schubert 
777db0ac6deSCy Schubert /* Note: sparse initializers guarantee .inp = NULL. */
778db0ac6deSCy Schubert #define	INP_ITERATOR(_ipi, _lock, _match, _ctx)		\
779db0ac6deSCy Schubert 	{						\
780db0ac6deSCy Schubert 		.ipi = (_ipi),				\
781db0ac6deSCy Schubert 		.lock = (_lock),			\
782db0ac6deSCy Schubert 		.hash = INP_ALL_LIST,			\
783db0ac6deSCy Schubert 		.match = (_match),			\
784db0ac6deSCy Schubert 		.ctx = (_ctx),				\
785db0ac6deSCy Schubert 	}
786db0ac6deSCy Schubert #define	INP_ALL_ITERATOR(_ipi, _lock)			\
787db0ac6deSCy Schubert 	{						\
788db0ac6deSCy Schubert 		.ipi = (_ipi),				\
789db0ac6deSCy Schubert 		.lock = (_lock),			\
790db0ac6deSCy Schubert 		.hash = INP_ALL_LIST,			\
791db0ac6deSCy Schubert 	}
792db0ac6deSCy Schubert 
793db0ac6deSCy Schubert struct inpcb *inp_next(struct inpcb_iterator *);
79484cc0778SGeorge V. Neville-Neil void	in_losing(struct inpcb *);
795a557af22SRobert Watson void	in_pcbsetsolabel(struct socket *so);
79654d642bbSRobert Watson int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
79754d642bbSRobert Watson int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
79826ef6ac4SDon Lewis struct sockaddr *
79926ef6ac4SDon Lewis 	in_sockaddr(in_port_t port, struct in_addr *addr);
800a557af22SRobert Watson void	in_pcbsosetlabel(struct socket *so);
801f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
80220abea66SRandall Stewart int
80320abea66SRandall Stewart in_pcboutput_txrtlmt_locked(struct inpcb *, struct ifnet *,
80420abea66SRandall Stewart 	    struct mbuf *, uint32_t);
80520abea66SRandall Stewart int	in_pcbattach_txrtlmt(struct inpcb *, struct ifnet *, uint32_t, uint32_t,
80620abea66SRandall Stewart 	    uint32_t, struct m_snd_tag **);
807f3e7afe2SHans Petter Selasky void	in_pcbdetach_txrtlmt(struct inpcb *);
80898d7a8d9SJohn Baldwin void    in_pcbdetach_tag(struct m_snd_tag *);
809f3e7afe2SHans Petter Selasky int	in_pcbmodify_txrtlmt(struct inpcb *, uint32_t);
810f3e7afe2SHans Petter Selasky int	in_pcbquery_txrtlmt(struct inpcb *, uint32_t *);
81195ed5015SHans Petter Selasky int	in_pcbquery_txrlevel(struct inpcb *, uint32_t *);
812f3e7afe2SHans Petter Selasky void	in_pcboutput_txrtlmt(struct inpcb *, struct ifnet *, struct mbuf *);
813f3e7afe2SHans Petter Selasky void	in_pcboutput_eagain(struct inpcb *);
814f3e7afe2SHans Petter Selasky #endif
815664a31e4SPeter Wemm #endif /* _KERNEL */
8163d4d47f3SGarrett Wollman 
8178781d8e9SBruce Evans #endif /* !_NETINET_IN_PCB_H_ */
818