in_pcb.h revision 195699
1139823Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1990, 1993
3185791Srwatson *	The Regents of the University of California.
4185791Srwatson * All rights reserved.
51541Srgrimes *
61541Srgrimes * Redistribution and use in source and binary forms, with or without
71541Srgrimes * modification, are permitted provided that the following conditions
81541Srgrimes * are met:
91541Srgrimes * 1. Redistributions of source code must retain the above copyright
101541Srgrimes *    notice, this list of conditions and the following disclaimer.
111541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer in the
131541Srgrimes *    documentation and/or other materials provided with the distribution.
141541Srgrimes * 4. Neither the name of the University nor the names of its contributors
151541Srgrimes *    may be used to endorse or promote products derived from this software
161541Srgrimes *    without specific prior written permission.
171541Srgrimes *
181541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281541Srgrimes * SUCH DAMAGE.
291541Srgrimes *
301541Srgrimes *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
3150477Speter * $FreeBSD: head/sys/netinet/in_pcb.h 195699 2009-07-14 22:48:30Z rwatson $
321541Srgrimes */
331541Srgrimes
342169Spaul#ifndef _NETINET_IN_PCB_H_
352169Spaul#define _NETINET_IN_PCB_H_
362169Spaul
3712644Sbde#include <sys/queue.h>
38102981Sbde#include <sys/_lock.h>
39102981Sbde#include <sys/_mutex.h>
40178285Srwatson#include <sys/_rwlock.h>
4112644Sbde
42178285Srwatson#ifdef _KERNEL
43178285Srwatson#include <sys/rwlock.h>
44195699Srwatson#include <net/vnet.h>
45178285Srwatson#endif
46178285Srwatson
4753541Sshin#define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
4853541Sshin#define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
49105199Ssamstruct inpcbpolicy;
5053541Sshin
511541Srgrimes/*
52183460Srwatson * struct inpcb is the common protocol control block structure used in most
53183460Srwatson * IP transport protocols.
54169154Srwatson *
55169154Srwatson * Pointers to local and foreign host table entries, local and foreign socket
56169154Srwatson * numbers, and pointers up (to a socket structure) and down (to a
57169154Srwatson * protocol-specific control block) are stored here.
581541Srgrimes */
5960938SjakeLIST_HEAD(inpcbhead, inpcb);
6060938SjakeLIST_HEAD(inpcbporthead, inpcbport);
6136079Swollmantypedef	u_quad_t	inp_gen_t;
627684Sdg
6334881Swollman/*
6453541Sshin * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
65169154Srwatson * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
66169154Srwatson * the following structure.
6753541Sshin */
6853541Sshinstruct in_addr_4in6 {
6953541Sshin	u_int32_t	ia46_pad32[3];
7053541Sshin	struct	in_addr	ia46_addr4;
7153541Sshin};
7253541Sshin
7353541Sshin/*
74169154Srwatson * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
75169154Srwatson * some extra padding to accomplish this.
7686764Sjlemon */
7786764Sjlemonstruct in_endpoints {
7886764Sjlemon	u_int16_t	ie_fport;		/* foreign port */
7986764Sjlemon	u_int16_t	ie_lport;		/* local port */
8086764Sjlemon	/* protocol dependent part, local and foreign addr */
8186764Sjlemon	union {
8286764Sjlemon		/* foreign host table entry */
8386764Sjlemon		struct	in_addr_4in6 ie46_foreign;
8486764Sjlemon		struct	in6_addr ie6_foreign;
8586764Sjlemon	} ie_dependfaddr;
8686764Sjlemon	union {
8786764Sjlemon		/* local host table entry */
8886764Sjlemon		struct	in_addr_4in6 ie46_local;
8986764Sjlemon		struct	in6_addr ie6_local;
9086764Sjlemon	} ie_dependladdr;
91185791Srwatson};
9286764Sjlemon#define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
9386764Sjlemon#define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
9486764Sjlemon#define	ie6_faddr	ie_dependfaddr.ie6_foreign
9586764Sjlemon#define	ie6_laddr	ie_dependladdr.ie6_local
9686764Sjlemon
9786764Sjlemon/*
98169154Srwatson * XXX The defines for inc_* are hacks and should be changed to direct
99169154Srwatson * references.
10086764Sjlemon */
10186764Sjlemonstruct in_conninfo {
10286764Sjlemon	u_int8_t	inc_flags;
10386764Sjlemon	u_int8_t	inc_len;
104178888Sjulian	u_int16_t	inc_fibnum;	/* XXX was pad, 16 bits is plenty */
105122922Sandre	/* protocol dependent part */
10686764Sjlemon	struct	in_endpoints inc_ie;
10786764Sjlemon};
108186222Sbz
109186222Sbz/*
110186222Sbz * Flags for inc_flags.
111186222Sbz */
112186222Sbz#define	INC_ISIPV6	0x01
113186222Sbz
11486764Sjlemon#define inc_isipv6	inc_flags	/* temp compatability */
11586764Sjlemon#define	inc_fport	inc_ie.ie_fport
11686764Sjlemon#define	inc_lport	inc_ie.ie_lport
11786764Sjlemon#define	inc_faddr	inc_ie.ie_faddr
11886764Sjlemon#define	inc_laddr	inc_ie.ie_laddr
11986764Sjlemon#define	inc6_faddr	inc_ie.ie6_faddr
12086764Sjlemon#define	inc6_laddr	inc_ie.ie6_laddr
12186764Sjlemon
12253541Sshinstruct	icmp6_filter;
12353541Sshin
124180368Srwatson/*-
125180368Srwatson * struct inpcb captures the network layer state for TCP, UDP, and raw IPv4
126180368Srwatson * and IPv6 sockets.  In the case of TCP, further per-connection state is
127180368Srwatson * hung off of inp_ppcb most of the time.  Almost all fields of struct inpcb
128180368Srwatson * are static after creation or protected by a per-inpcb rwlock, inp_lock.  A
129180368Srwatson * few fields also require the global pcbinfo lock for the inpcb to be held,
130180368Srwatson * when modified, such as the global connection lists and hashes, as well as
131180368Srwatson * binding information (which affects which hash a connection is on).  This
132180368Srwatson * model means that connections can be looked up without holding the
133180368Srwatson * per-connection lock, which is important for performance when attempting to
134180368Srwatson * find the connection for a packet given its IP and port tuple.  Writing to
135180368Srwatson * these fields that write locks be held on both the inpcb and global locks.
136180368Srwatson *
137180368Srwatson * Key:
138180368Srwatson * (c) - Constant after initialization
139180368Srwatson * (i) - Protected by the inpcb lock
140180368Srwatson * (p) - Protected by the pcbinfo lock for the inpcb
141180368Srwatson * (s) - Protected by another subsystem's locks
142180368Srwatson * (x) - Undefined locking
143180368Srwatson *
144180368Srwatson * A few other notes:
145180368Srwatson *
146180368Srwatson * When a read lock is held, stability of the field is guaranteed; to write
147180368Srwatson * to a field, a write lock must generally be held.
148180368Srwatson *
149180368Srwatson * netinet/netinet6-layer code should not assume that the inp_socket pointer
150180368Srwatson * is safe to dereference without inp_lock being held, even for protocols
151180368Srwatson * other than TCP (where the inpcb persists during TIMEWAIT even after the
152180368Srwatson * socket has been freed), or there may be close(2)-related races.
153180368Srwatson *
154180368Srwatson * The inp_vflag field is overloaded, and would otherwise ideally be (c).
155180368Srwatson */
1561541Srgrimesstruct inpcb {
157180368Srwatson	LIST_ENTRY(inpcb) inp_hash;	/* (i/p) hash list */
158180368Srwatson	LIST_ENTRY(inpcb) inp_list;	/* (i/p) list for all PCBs for proto */
159180368Srwatson	void	*inp_ppcb;		/* (i) pointer to per-protocol pcb */
160180368Srwatson	struct	inpcbinfo *inp_pcbinfo;	/* (c) PCB list info */
161185791Srwatson	struct	socket *inp_socket;	/* (i) back pointer to socket */
162183606Sbz	struct	ucred	*inp_cred;	/* (c) cache of socket cred */
163185791Srwatson	u_int32_t inp_flow;		/* (i) IPv6 flow information */
164180368Srwatson	int	inp_flags;		/* (i) generic IP/datagram flags */
165191126Skmacy	int	inp_flags2;		/* (i) generic IP/datagram flags #2*/
166180368Srwatson	u_char	inp_vflag;		/* (i) IP version flag (v4/v6) */
167180368Srwatson	u_char	inp_ip_ttl;		/* (i) time to live proto */
168180368Srwatson	u_char	inp_ip_p;		/* (c) protocol proto */
169180368Srwatson	u_char	inp_ip_minttl;		/* (i) minimum TTL or drop */
170190880Skmacy	uint32_t inp_flowid;		/* (x) flow id / queue id */
171185773Srwatson	u_int	inp_refcount;		/* (i) refcount */
172191160Skmacy	void	*inp_pspare[2];		/* (x) rtentry / general use */
17353541Sshin
174174388Skmacy	/* Local and foreign ports, local and foreign addr. */
175185791Srwatson	struct	in_conninfo inp_inc;	/* (i/p) list for PCB's local port */
176174388Skmacy
177185791Srwatson	/* MAC and IPSEC policy information. */
178180368Srwatson	struct	label *inp_label;	/* (i) MAC label */
179180368Srwatson	struct	inpcbpolicy *inp_sp;    /* (s) for IPSEC */
180174388Skmacy
181169154Srwatson	/* Protocol-dependent part; options. */
18253541Sshin	struct {
183180368Srwatson		u_char	inp4_ip_tos;		/* (i) type of service proto */
184180368Srwatson		struct	mbuf *inp4_options;	/* (i) IP options */
185185791Srwatson		struct	ip_moptions *inp4_moptions; /* (i) IP mcast options */
18653541Sshin	} inp_depend4;
18753541Sshin	struct {
188180368Srwatson		/* (i) IP options */
18953541Sshin		struct	mbuf *inp6_options;
190180368Srwatson		/* (i) IP6 options for outgoing packets */
19153541Sshin		struct	ip6_pktopts *inp6_outputopts;
192180368Srwatson		/* (i) IP multicast options */
19353541Sshin		struct	ip6_moptions *inp6_moptions;
194180368Srwatson		/* (i) ICMPv6 code type filter */
19553541Sshin		struct	icmp6_filter *inp6_icmp6filt;
196180368Srwatson		/* (i) IPV6_CHECKSUM setsockopt */
19753541Sshin		int	inp6_cksum;
19853541Sshin		short	inp6_hops;
19953541Sshin	} inp_depend6;
200180368Srwatson	LIST_ENTRY(inpcb) inp_portlist;	/* (i/p) */
201180368Srwatson	struct	inpcbport *inp_phd;	/* (i/p) head of this list */
202160491Sups#define inp_zero_size offsetof(struct inpcb, inp_gencnt)
203185791Srwatson	inp_gen_t	inp_gencnt;	/* (c) generation count */
204191158Skmacy	struct llentry	*inp_lle;	/* cached L2 information */
205191158Skmacy	struct rtentry	*inp_rt;	/* cached L3 information */
206178285Srwatson	struct rwlock	inp_lock;
207185791Srwatson};
208185791Srwatson#define	inp_fport	inp_inc.inc_fport
209185791Srwatson#define	inp_lport	inp_inc.inc_lport
210185791Srwatson#define	inp_faddr	inp_inc.inc_faddr
211185791Srwatson#define	inp_laddr	inp_inc.inc_laddr
212185791Srwatson#define	inp_ip_tos	inp_depend4.inp4_ip_tos
213185791Srwatson#define	inp_options	inp_depend4.inp4_options
214185791Srwatson#define	inp_moptions	inp_depend4.inp4_moptions
21598102Shsu
21686764Sjlemon#define	in6p_faddr	inp_inc.inc6_faddr
21786764Sjlemon#define	in6p_laddr	inp_inc.inc6_laddr
21853541Sshin#define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
21953541Sshin#define	in6p_flowinfo	inp_flow
22053541Sshin#define	in6p_options	inp_depend6.inp6_options
22153541Sshin#define	in6p_outputopts	inp_depend6.inp6_outputopts
22253541Sshin#define	in6p_moptions	inp_depend6.inp6_moptions
22353541Sshin#define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
22453541Sshin#define	in6p_cksum	inp_depend6.inp6_cksum
225185791Srwatson
226191688Szec#define	inp_vnet	inp_pcbinfo->ipi_vnet
227191688Szec
22834881Swollman/*
229169154Srwatson * The range of the generation count, as used in this implementation, is 9e19.
230169154Srwatson * We would have to create 300 billion connections per second for this number
231169154Srwatson * to roll over in a year.  This seems sufficiently unlikely that we simply
232169154Srwatson * don't concern ourselves with that possibility.
23334881Swollman */
2341541Srgrimes
23536079Swollman/*
236169154Srwatson * Interface exported to userland by various protocols which use inpcbs.  Hack
237169154Srwatson * alert -- only define if struct xsocket is in scope.
23836079Swollman */
23936079Swollman#ifdef _SYS_SOCKETVAR_H_
24036079Swollmanstruct	xinpcb {
24136079Swollman	size_t	xi_len;		/* length of this structure */
24236079Swollman	struct	inpcb xi_inp;
24336079Swollman	struct	xsocket xi_socket;
24436079Swollman	u_quad_t	xi_alignment_hack;
24536079Swollman};
24636079Swollman
24736079Swollmanstruct	xinpgen {
24836079Swollman	size_t	xig_len;	/* length of this structure */
24936079Swollman	u_int	xig_count;	/* number of PCBs at this time */
25036079Swollman	inp_gen_t xig_gen;	/* generation count at this time */
25136079Swollman	so_gen_t xig_sogen;	/* socket generation count at this time */
25236079Swollman};
25336079Swollman#endif /* _SYS_SOCKETVAR_H_ */
25436079Swollman
25532821Sdgstruct inpcbport {
25660938Sjake	LIST_ENTRY(inpcbport) phd_hash;
25732821Sdg	struct inpcbhead phd_pcblist;
25832821Sdg	u_short phd_port;
25932821Sdg};
26032821Sdg
261169154Srwatson/*
262169154Srwatson * Global data structure for each high-level protocol (UDP, TCP, ...) in both
263169154Srwatson * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
264169154Srwatson */
265169154Srwatsonstruct inpcbinfo {
266169154Srwatson	/*
267169154Srwatson	 * Global list of inpcbs on the protocol.
268169154Srwatson	 */
269169154Srwatson	struct inpcbhead	*ipi_listhead;
270169154Srwatson	u_int			 ipi_count;
271169154Srwatson
272169154Srwatson	/*
273169154Srwatson	 * Global hash of inpcbs, hashed by local and foreign addresses and
274169154Srwatson	 * port numbers.
275169154Srwatson	 */
276169154Srwatson	struct inpcbhead	*ipi_hashbase;
277169154Srwatson	u_long			 ipi_hashmask;
278169154Srwatson
279169154Srwatson	/*
280169154Srwatson	 * Global hash of inpcbs, hashed by only local port number.
281169154Srwatson	 */
282169154Srwatson	struct inpcbporthead	*ipi_porthashbase;
283169154Srwatson	u_long			 ipi_porthashmask;
284169154Srwatson
285169154Srwatson	/*
286169154Srwatson	 * Fields associated with port lookup and allocation.
287169154Srwatson	 */
288169154Srwatson	u_short			 ipi_lastport;
289169154Srwatson	u_short			 ipi_lastlow;
290169154Srwatson	u_short			 ipi_lasthi;
291169154Srwatson
292169154Srwatson	/*
293169154Srwatson	 * UMA zone from which inpcbs are allocated for this protocol.
294169154Srwatson	 */
295169154Srwatson	struct	uma_zone	*ipi_zone;
296169154Srwatson
297169154Srwatson	/*
298169154Srwatson	 * Generation count--incremented each time a connection is allocated
299169154Srwatson	 * or freed.
300169154Srwatson	 */
301169154Srwatson	u_quad_t		 ipi_gencnt;
302178285Srwatson	struct rwlock		 ipi_lock;
303174388Skmacy
304174388Skmacy	/*
305191688Szec	 * Pointer to network stack instance
306174388Skmacy	 */
307191688Szec	struct vnet		*ipi_vnet;
308191688Szec
309191688Szec	/*
310191688Szec	 * general use 2
311191688Szec	 */
312181365Srwatson	void 			*ipi_pspare[2];
3137684Sdg};
3147684Sdg
315122991Ssam#define INP_LOCK_INIT(inp, d, t) \
316178285Srwatson	rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE |  RW_DUPOK)
317178285Srwatson#define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
318178285Srwatson#define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
319178285Srwatson#define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
320180536Srwatson#define INP_TRY_RLOCK(inp)	rw_try_rlock(&(inp)->inp_lock)
321180536Srwatson#define INP_TRY_WLOCK(inp)	rw_try_wlock(&(inp)->inp_lock)
322178285Srwatson#define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
323178285Srwatson#define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
324191125Skmacy#define	INP_TRY_UPGRADE(inp)	rw_try_upgrade(&(inp)->inp_lock)
325191125Skmacy#define	INP_DOWNGRADE(inp)	rw_downgrade(&(inp)->inp_lock)
326191125Skmacy#define	INP_WLOCKED(inp)	rw_wowned(&(inp)->inp_lock)
327191125Skmacy#define	INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
328178285Srwatson#define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
329178285Srwatson#define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
330178285Srwatson#define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
33198102Shsu
332177575Skmacy#ifdef _KERNEL
333177530Skmacy/*
334181365Srwatson * These locking functions are for inpcb consumers outside of sys/netinet,
335177530Skmacy * more specifically, they were added for the benefit of TOE drivers. The
336177530Skmacy * macros are reserved for use by the stack.
337177530Skmacy */
338177530Skmacyvoid inp_wlock(struct inpcb *);
339177530Skmacyvoid inp_wunlock(struct inpcb *);
340177530Skmacyvoid inp_rlock(struct inpcb *);
341177530Skmacyvoid inp_runlock(struct inpcb *);
342177530Skmacy
343177530Skmacy#ifdef INVARIANTS
344177575Skmacyvoid inp_lock_assert(struct inpcb *);
345177575Skmacyvoid inp_unlock_assert(struct inpcb *);
346177530Skmacy#else
347177530Skmacystatic __inline void
348177575Skmacyinp_lock_assert(struct inpcb *inp __unused)
349177530Skmacy{
350177530Skmacy}
351177530Skmacy
352177530Skmacystatic __inline void
353177575Skmacyinp_unlock_assert(struct inpcb *inp __unused)
354177530Skmacy{
355177530Skmacy}
356177575Skmacy
357177530Skmacy#endif
358180640Skmacy
359180678Skmacyvoid	inp_apply_all(void (*func)(struct inpcb *, void *), void *arg);
360180678Skmacyint 	inp_ip_tos_get(const struct inpcb *inp);
361180678Skmacyvoid 	inp_ip_tos_set(struct inpcb *inp, int val);
362180678Skmacystruct socket *
363180678Skmacy	inp_inpcbtosocket(struct inpcb *inp);
364180678Skmacystruct tcpcb *
365180678Skmacy	inp_inpcbtotcpcb(struct inpcb *inp);
366181365Srwatsonvoid 	inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
367180678Skmacy		uint32_t *faddr, uint16_t *fp);
368180640Skmacy
369177575Skmacy#endif /* _KERNEL */
370177530Skmacy
37198102Shsu#define INP_INFO_LOCK_INIT(ipi, d) \
372178285Srwatson	rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE)
373178285Srwatson#define INP_INFO_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_lock)
374178285Srwatson#define INP_INFO_RLOCK(ipi)	rw_rlock(&(ipi)->ipi_lock)
375178285Srwatson#define INP_INFO_WLOCK(ipi)	rw_wlock(&(ipi)->ipi_lock)
376180536Srwatson#define INP_INFO_TRY_RLOCK(ipi)	rw_try_rlock(&(ipi)->ipi_lock)
377180536Srwatson#define INP_INFO_TRY_WLOCK(ipi)	rw_try_wlock(&(ipi)->ipi_lock)
378178285Srwatson#define INP_INFO_RUNLOCK(ipi)	rw_runlock(&(ipi)->ipi_lock)
379178285Srwatson#define INP_INFO_WUNLOCK(ipi)	rw_wunlock(&(ipi)->ipi_lock)
380178285Srwatson#define	INP_INFO_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_LOCKED)
381178285Srwatson#define INP_INFO_RLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_RLOCKED)
382178285Srwatson#define INP_INFO_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_WLOCKED)
383178285Srwatson#define INP_INFO_UNLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED)
38498102Shsu
38523324Sdg#define INP_PCBHASH(faddr, lport, fport, mask) \
38632821Sdg	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
38732821Sdg#define INP_PCBPORTHASH(lport, mask) \
38832821Sdg	(ntohs((lport)) & (mask))
38923324Sdg
390185791Srwatson/*
391191126Skmacy * Flags for inp_vflags -- historically version flags only
392185791Srwatson */
393185791Srwatson#define	INP_IPV4	0x1
394185791Srwatson#define	INP_IPV6	0x2
395185791Srwatson#define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
396185791Srwatson
397185791Srwatson/*
398191126Skmacy * Flags for inp_flags.
399185791Srwatson */
400189848Srwatson#define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
401189848Srwatson#define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
402189848Srwatson#define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
403189848Srwatson#define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
404189848Srwatson#define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
405189848Srwatson#define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
406189848Srwatson#define	INP_ANONPORT		0x00000040 /* port chosen for user */
407189848Srwatson#define	INP_RECVIF		0x00000080 /* receive incoming interface */
408189848Srwatson#define	INP_MTUDISC		0x00000100 /* user can do MTU discovery */
409189848Srwatson#define	INP_FAITH		0x00000200 /* accept FAITH'ed connections */
410189848Srwatson#define	INP_RECVTTL		0x00000400 /* receive incoming IP TTL */
411189848Srwatson#define	INP_DONTFRAG		0x00000800 /* don't fragment packet */
412193217Spjd#define	INP_BINDANY		0x00001000 /* allow bind to any address */
413189848Srwatson#define	INP_INHASHLIST		0x00002000 /* in_pcbinshash() has been called */
414189848Srwatson#define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
415189848Srwatson#define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
416189848Srwatson#define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
417189848Srwatson#define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
418189848Srwatson#define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
419189848Srwatson#define	IN6P_RTHDR		0x00100000 /* receive routing header */
420189848Srwatson#define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
421189848Srwatson#define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
422189848Srwatson#define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
423189848Srwatson#define	INP_TIMEWAIT		0x01000000 /* in TIMEWAIT, ppcb is tcptw */
424189848Srwatson#define	INP_ONESBCAST		0x02000000 /* send all-ones broadcast */
425189848Srwatson#define	INP_DROPPED		0x04000000 /* protocol drop flag */
426189848Srwatson#define	INP_SOCKREF		0x08000000 /* strong socket reference */
427190880Skmacy#define	INP_SW_FLOWID           0x10000000 /* software generated flow id */
428190880Skmacy#define	INP_HW_FLOWID           0x20000000 /* hardware generated flow id */
429121477Sume#define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
430121477Sume#define	IN6P_MTU		0x80000000 /* receive path MTU */
43178064Sume
43219622Sfenner#define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
433114258Smdodd				 INP_RECVIF|INP_RECVTTL|\
43478064Sume				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
43578064Sume				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
436121472Sume				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
437121472Sume				 IN6P_MTU)
4381541Srgrimes
439191126Skmacy/*
440191126Skmacy * Flags for inp_flags2.
441191126Skmacy */
442191129Skmacy#define	INP_LLE_VALID		0x00000001 /* cached lle is valid */
443191129Skmacy#define	INP_RT_VALID		0x00000002 /* cached rtentry is valid */
444191126Skmacy
4451541Srgrimes#define	INPLOOKUP_WILDCARD	1
4461541Srgrimes#define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
44753541Sshin#define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
4481541Srgrimes
44953541Sshin#define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
45053541Sshin
451133874Srwatson#define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
45253541Sshin
45355205Speter#ifdef _KERNEL
454195699SrwatsonVNET_DECLARE(int, ipport_reservedhigh);
455195699SrwatsonVNET_DECLARE(int, ipport_reservedlow);
456195699SrwatsonVNET_DECLARE(int, ipport_lowfirstauto);
457195699SrwatsonVNET_DECLARE(int, ipport_lowlastauto);
458195699SrwatsonVNET_DECLARE(int, ipport_firstauto);
459195699SrwatsonVNET_DECLARE(int, ipport_lastauto);
460195699SrwatsonVNET_DECLARE(int, ipport_hifirstauto);
461195699SrwatsonVNET_DECLARE(int, ipport_hilastauto);
462195699SrwatsonVNET_DECLARE(int, ipport_randomized);
463195699SrwatsonVNET_DECLARE(int, ipport_randomcps);
464195699SrwatsonVNET_DECLARE(int, ipport_randomtime);
465195699SrwatsonVNET_DECLARE(int, ipport_stoprandom);
466195699SrwatsonVNET_DECLARE(int, ipport_tcpallocs);
467195699Srwatson
468195699Srwatson#define	V_ipport_reservedhigh	VNET_GET(ipport_reservedhigh)
469195699Srwatson#define	V_ipport_reservedlow	VNET_GET(ipport_reservedlow)
470195699Srwatson#define	V_ipport_lowfirstauto	VNET_GET(ipport_lowfirstauto)
471195699Srwatson#define	V_ipport_lowlastauto	VNET_GET(ipport_lowlastauto)
472195699Srwatson#define	V_ipport_firstauto	VNET_GET(ipport_firstauto)
473195699Srwatson#define	V_ipport_lastauto	VNET_GET(ipport_lastauto)
474195699Srwatson#define	V_ipport_hifirstauto	VNET_GET(ipport_hifirstauto)
475195699Srwatson#define	V_ipport_hilastauto	VNET_GET(ipport_hilastauto)
476195699Srwatson#define	V_ipport_randomized	VNET_GET(ipport_randomized)
477195699Srwatson#define	V_ipport_randomcps	VNET_GET(ipport_randomcps)
478195699Srwatson#define	V_ipport_randomtime	VNET_GET(ipport_randomtime)
479195699Srwatson#define	V_ipport_stoprandom	VNET_GET(ipport_stoprandom)
480195699Srwatson#define	V_ipport_tcpallocs	VNET_GET(ipport_tcpallocs)
481195699Srwatson
482139558Ssilbyextern struct callout ipport_tick_callout;
48353541Sshin
48498102Shsuvoid	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
485160491Supsint	in_pcballoc(struct socket *, struct inpcbinfo *);
486127505Spjdint	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
487105565Siedowseint	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
488127505Spjd	    u_short *, struct ucred *);
489127505Spjdint	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
490105629Siedowseint	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
491105629Siedowse	    u_short *, in_addr_t *, u_short *, struct inpcb **,
492127505Spjd	    struct ucred *);
49392723Salfredvoid	in_pcbdetach(struct inpcb *);
49492723Salfredvoid	in_pcbdisconnect(struct inpcb *);
495158009Srwatsonvoid	in_pcbdrop(struct inpcb *);
496157373Srwatsonvoid	in_pcbfree(struct inpcb *);
49792723Salfredint	in_pcbinshash(struct inpcb *);
4981541Srgrimesstruct inpcb *
49992723Salfred	in_pcblookup_local(struct inpcbinfo *,
500180427Sbz	    struct in_addr, u_short, int, struct ucred *);
5017684Sdgstruct inpcb *
50293085Sbde	in_pcblookup_hash(struct inpcbinfo *, struct in_addr, u_int,
50393085Sbde	    struct in_addr, u_int, int, struct ifnet *);
50498102Shsuvoid	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
50598211Shsu	    int, struct inpcb *(*)(struct inpcb *, int));
506185773Srwatsonvoid	in_pcbref(struct inpcb *);
50792723Salfredvoid	in_pcbrehash(struct inpcb *);
508185773Srwatsonint	in_pcbrele(struct inpcb *);
509122875Srwatsonvoid	in_pcbsetsolabel(struct socket *so);
510169462Srwatsonint	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
511169462Srwatsonint	in_getsockaddr(struct socket *so, struct sockaddr **nam);
512102218Struckmanstruct sockaddr *
513102218Struckman	in_sockaddr(in_port_t port, struct in_addr *addr);
514122875Srwatsonvoid	in_pcbsosetlabel(struct socket *so);
515139558Ssilbyvoid	ipport_tick(void *xtp);
51655205Speter#endif /* _KERNEL */
51734881Swollman
51834923Sbde#endif /* !_NETINET_IN_PCB_H_ */
519