in_pcb.h revision 102981
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1990, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
3450477Speter * $FreeBSD: head/sys/netinet/in_pcb.h 102981 2002-09-05 19:48:52Z bde $
351541Srgrimes */
361541Srgrimes
372169Spaul#ifndef _NETINET_IN_PCB_H_
382169Spaul#define _NETINET_IN_PCB_H_
392169Spaul
4012644Sbde#include <sys/queue.h>
41102981Sbde#include <sys/_lock.h>
42102981Sbde#include <sys/_mutex.h>
4312644Sbde
4486991Srwatson#include <net/route.h>
4553541Sshin#include <netinet6/ipsec.h> /* for IPSEC */
4653541Sshin
4753541Sshin#define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
4853541Sshin#define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
4953541Sshin
501541Srgrimes/*
511541Srgrimes * Common structure pcb for internet protocol implementation.
521541Srgrimes * Here are stored pointers to local and foreign host table
531541Srgrimes * entries, local and foreign socket numbers, and pointers
541541Srgrimes * up (to a socket structure) and down (to a protocol-specific)
551541Srgrimes * control block.
561541Srgrimes */
5760938SjakeLIST_HEAD(inpcbhead, inpcb);
5860938SjakeLIST_HEAD(inpcbporthead, inpcbport);
5936079Swollmantypedef	u_quad_t	inp_gen_t;
607684Sdg
6134881Swollman/*
6253541Sshin * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
6353541Sshin * So, AF_INET6 null laddr is also used as AF_INET null laddr,
6453541Sshin * by utilize following structure. (At last, same as INRIA)
6553541Sshin */
6653541Sshinstruct in_addr_4in6 {
6753541Sshin	u_int32_t	ia46_pad32[3];
6853541Sshin	struct	in_addr	ia46_addr4;
6953541Sshin};
7053541Sshin
7153541Sshin/*
7286764Sjlemon * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.
7386764Sjlemon * in_conninfo has some extra padding to accomplish this.
7486764Sjlemon */
7586764Sjlemonstruct in_endpoints {
7686764Sjlemon	u_int16_t	ie_fport;		/* foreign port */
7786764Sjlemon	u_int16_t	ie_lport;		/* local port */
7886764Sjlemon	/* protocol dependent part, local and foreign addr */
7986764Sjlemon	union {
8086764Sjlemon		/* foreign host table entry */
8186764Sjlemon		struct	in_addr_4in6 ie46_foreign;
8286764Sjlemon		struct	in6_addr ie6_foreign;
8386764Sjlemon	} ie_dependfaddr;
8486764Sjlemon	union {
8586764Sjlemon		/* local host table entry */
8686764Sjlemon		struct	in_addr_4in6 ie46_local;
8786764Sjlemon		struct	in6_addr ie6_local;
8886764Sjlemon	} ie_dependladdr;
8986764Sjlemon#define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
9086764Sjlemon#define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
9186764Sjlemon#define	ie6_faddr	ie_dependfaddr.ie6_foreign
9286764Sjlemon#define	ie6_laddr	ie_dependladdr.ie6_local
9386764Sjlemon};
9486764Sjlemon
9586764Sjlemon/*
9686764Sjlemon * XXX
9786764Sjlemon * At some point struct route should possibly change to:
9886764Sjlemon *   struct rtentry *rt
9986764Sjlemon *   struct in_endpoints *ie;
10086764Sjlemon */
10186764Sjlemonstruct in_conninfo {
10286764Sjlemon	u_int8_t	inc_flags;
10386764Sjlemon	u_int8_t	inc_len;
10486764Sjlemon	u_int16_t	inc_pad;	/* XXX alignment for in_endpoints */
10586764Sjlemon	/* protocol dependent part; cached route */
10686764Sjlemon	struct	in_endpoints inc_ie;
10786764Sjlemon	union {
10886764Sjlemon		/* placeholder for routing entry */
10986764Sjlemon		struct	route inc4_route;
11086764Sjlemon		struct	route_in6 inc6_route;
11186764Sjlemon	} inc_dependroute;
11286764Sjlemon};
11386764Sjlemon#define inc_isipv6	inc_flags	/* temp compatability */
11486764Sjlemon#define	inc_fport	inc_ie.ie_fport
11586764Sjlemon#define	inc_lport	inc_ie.ie_lport
11686764Sjlemon#define	inc_faddr	inc_ie.ie_faddr
11786764Sjlemon#define	inc_laddr	inc_ie.ie_laddr
11886764Sjlemon#define	inc_route	inc_dependroute.inc4_route
11986764Sjlemon#define	inc6_faddr	inc_ie.ie6_faddr
12086764Sjlemon#define	inc6_laddr	inc_ie.ie6_laddr
12186764Sjlemon#define	inc6_route	inc_dependroute.inc6_route
12286764Sjlemon
12353541Sshinstruct	icmp6_filter;
12453541Sshin
1251541Srgrimesstruct inpcb {
12660938Sjake	LIST_ENTRY(inpcb) inp_hash; /* hash list */
12760938Sjake	LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
12853541Sshin	u_int32_t	inp_flow;
12953541Sshin
13086764Sjlemon	/* local and foreign ports, local and foreign addr */
13186764Sjlemon	struct	in_conninfo inp_inc;
13253541Sshin
13324570Sdg	caddr_t	inp_ppcb;		/* pointer to per-protocol pcb */
13432821Sdg	struct	inpcbinfo *inp_pcbinfo;	/* PCB list info */
1351541Srgrimes	struct	socket *inp_socket;	/* back pointer to socket */
13653541Sshin					/* list for this PCB's local port */
1371541Srgrimes	int	inp_flags;		/* generic IP/datagram flags */
13853541Sshin
13953541Sshin	struct	inpcbpolicy *inp_sp; /* for IPSEC */
14091236Salfred	u_char	inp_vflag;		/* IP version flag (v4/v6) */
14153541Sshin#define	INP_IPV4	0x1
14253541Sshin#define	INP_IPV6	0x2
14324570Sdg	u_char	inp_ip_ttl;		/* time to live proto */
14424570Sdg	u_char	inp_ip_p;		/* protocol proto */
14553541Sshin
14653541Sshin	/* protocol dependent part; options */
14753541Sshin	struct {
14853541Sshin		u_char	inp4_ip_tos;		/* type of service proto */
14953541Sshin		struct	mbuf *inp4_options;	/* IP options */
15053541Sshin		struct	ip_moptions *inp4_moptions; /* IP multicast options */
15153541Sshin	} inp_depend4;
15286764Sjlemon#define inp_fport	inp_inc.inc_fport
15386764Sjlemon#define inp_lport	inp_inc.inc_lport
15486764Sjlemon#define	inp_faddr	inp_inc.inc_faddr
15586764Sjlemon#define	inp_laddr	inp_inc.inc_laddr
15686764Sjlemon#define	inp_route	inp_inc.inc_route
15753541Sshin#define	inp_ip_tos	inp_depend4.inp4_ip_tos
15853541Sshin#define	inp_options	inp_depend4.inp4_options
15953541Sshin#define	inp_moptions	inp_depend4.inp4_moptions
16053541Sshin	struct {
16153541Sshin		/* IP options */
16253541Sshin		struct	mbuf *inp6_options;
16353541Sshin		/* IP6 options for outgoing packets */
16453541Sshin		struct	ip6_pktopts *inp6_outputopts;
16553541Sshin		/* IP multicast options */
16653541Sshin		struct	ip6_moptions *inp6_moptions;
16753541Sshin		/* ICMPv6 code type filter */
16853541Sshin		struct	icmp6_filter *inp6_icmp6filt;
16953541Sshin		/* IPV6_CHECKSUM setsockopt */
17053541Sshin		int	inp6_cksum;
17153541Sshin		u_short	inp6_ifindex;
17253541Sshin		short	inp6_hops;
17353541Sshin		u_int8_t	inp6_hlim;
17453541Sshin	} inp_depend6;
17560938Sjake	LIST_ENTRY(inpcb) inp_portlist;
17634923Sbde	struct	inpcbport *inp_phd;	/* head of this list */
17753541Sshin	inp_gen_t	inp_gencnt;	/* generation count of this instance */
17898102Shsu	struct mtx	inp_mtx;
17998102Shsu
18086764Sjlemon#define	in6p_faddr	inp_inc.inc6_faddr
18186764Sjlemon#define	in6p_laddr	inp_inc.inc6_laddr
18286764Sjlemon#define	in6p_route	inp_inc.inc6_route
18353541Sshin#define	in6p_ip6_hlim	inp_depend6.inp6_hlim
18453541Sshin#define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
18553541Sshin#define	in6p_ip6_nxt	inp_ip_p
18653541Sshin#define	in6p_flowinfo	inp_flow
18753541Sshin#define	in6p_vflag	inp_vflag
18853541Sshin#define	in6p_options	inp_depend6.inp6_options
18953541Sshin#define	in6p_outputopts	inp_depend6.inp6_outputopts
19053541Sshin#define	in6p_moptions	inp_depend6.inp6_moptions
19153541Sshin#define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
19253541Sshin#define	in6p_cksum	inp_depend6.inp6_cksum
19353541Sshin#define	inp6_ifindex	inp_depend6.inp6_ifindex
19453541Sshin#define	in6p_flags	inp_flags  /* for KAME src sync over BSD*'s */
19553541Sshin#define	in6p_socket	inp_socket  /* for KAME src sync over BSD*'s */
19653541Sshin#define	in6p_lport	inp_lport  /* for KAME src sync over BSD*'s */
19753541Sshin#define	in6p_fport	inp_fport  /* for KAME src sync over BSD*'s */
19853541Sshin#define	in6p_ppcb	inp_ppcb  /* for KAME src sync over BSD*'s */
1991541Srgrimes};
20034881Swollman/*
20134881Swollman * The range of the generation count, as used in this implementation,
20234881Swollman * is 9e19.  We would have to create 300 billion connections per
20334881Swollman * second for this number to roll over in a year.  This seems sufficiently
20434881Swollman * unlikely that we simply don't concern ourselves with that possibility.
20534881Swollman */
2061541Srgrimes
20736079Swollman/*
20836079Swollman * Interface exported to userland by various protocols which use
20936079Swollman * inpcbs.  Hack alert -- only define if struct xsocket is in scope.
21036079Swollman */
21136079Swollman#ifdef _SYS_SOCKETVAR_H_
21236079Swollmanstruct	xinpcb {
21336079Swollman	size_t	xi_len;		/* length of this structure */
21436079Swollman	struct	inpcb xi_inp;
21536079Swollman	struct	xsocket xi_socket;
21636079Swollman	u_quad_t	xi_alignment_hack;
21736079Swollman};
21836079Swollman
21936079Swollmanstruct	xinpgen {
22036079Swollman	size_t	xig_len;	/* length of this structure */
22136079Swollman	u_int	xig_count;	/* number of PCBs at this time */
22236079Swollman	inp_gen_t xig_gen;	/* generation count at this time */
22336079Swollman	so_gen_t xig_sogen;	/* socket generation count at this time */
22436079Swollman};
22536079Swollman#endif /* _SYS_SOCKETVAR_H_ */
22636079Swollman
22732821Sdgstruct inpcbport {
22860938Sjake	LIST_ENTRY(inpcbport) phd_hash;
22932821Sdg	struct inpcbhead phd_pcblist;
23032821Sdg	u_short phd_port;
23132821Sdg};
23232821Sdg
23334881Swollmanstruct inpcbinfo {		/* XXX documentation, prefixes */
23434881Swollman	struct	inpcbhead *hashbase;
23534881Swollman	u_long	hashmask;
23634881Swollman	struct	inpcbporthead *porthashbase;
23734881Swollman	u_long	porthashmask;
23834881Swollman	struct	inpcbhead *listhead;
23934881Swollman	u_short	lastport;
24034881Swollman	u_short	lastlow;
24134881Swollman	u_short	lasthi;
242102981Sbde	struct	uma_zone *ipi_zone; /* zone to allocate pcbs from */
24334881Swollman	u_int	ipi_count;	/* number of pcbs in this list */
24434881Swollman	u_quad_t ipi_gencnt;	/* current generation count */
24598102Shsu	struct	mtx ipi_mtx;
2467684Sdg};
2477684Sdg
24898102Shsu#define INP_LOCK_INIT(inp, d) \
24998102Shsu	mtx_init(&(inp)->inp_mtx, (d), NULL, MTX_DEF | MTX_RECURSE)
25098102Shsu#define INP_LOCK_DESTROY(inp)	mtx_destroy(&(inp)->inp_mtx)
25198102Shsu#define INP_LOCK(inp)		mtx_lock(&(inp)->inp_mtx)
25298102Shsu#define INP_UNLOCK(inp)		mtx_unlock(&(inp)->inp_mtx)
25398102Shsu
25498102Shsu#define INP_INFO_LOCK_INIT(ipi, d) \
25598102Shsu	mtx_init(&(ipi)->ipi_mtx, (d), NULL, MTX_DEF | MTX_RECURSE)
25698102Shsu#define INP_INFO_RLOCK(ipi)	mtx_lock(&(ipi)->ipi_mtx)
25798102Shsu#define INP_INFO_WLOCK(ipi)	mtx_lock(&(ipi)->ipi_mtx)
25898102Shsu#define INP_INFO_RUNLOCK(ipi)	mtx_unlock(&(ipi)->ipi_mtx)
25998102Shsu#define INP_INFO_WUNLOCK(ipi)	mtx_unlock(&(ipi)->ipi_mtx)
26098102Shsu
26123324Sdg#define INP_PCBHASH(faddr, lport, fport, mask) \
26232821Sdg	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
26332821Sdg#define INP_PCBPORTHASH(lport, mask) \
26432821Sdg	(ntohs((lport)) & (mask))
26523324Sdg
2661541Srgrimes/* flags in inp_flags: */
2671541Srgrimes#define	INP_RECVOPTS		0x01	/* receive incoming IP options */
2681541Srgrimes#define	INP_RECVRETOPTS		0x02	/* receive IP options for reply */
2691541Srgrimes#define	INP_RECVDSTADDR		0x04	/* receive IP dst address */
2701541Srgrimes#define	INP_HDRINCL		0x08	/* user supplies entire IP header */
27114195Speter#define	INP_HIGHPORT		0x10	/* user wants "high" port binding */
27214195Speter#define	INP_LOWPORT		0x20	/* user wants "low" port binding */
27317795Sphk#define	INP_ANONPORT		0x40	/* port chosen for user */
27419622Sfenner#define	INP_RECVIF		0x80	/* receive incoming interface */
27528270Swollman#define	INP_MTUDISC		0x100	/* user can do MTU discovery */
27653541Sshin#define	INP_FAITH		0x200	/* accept FAITH'ed connections */
27778064Sume
27878064Sume#define IN6P_IPV6_V6ONLY	0x008000 /* restrict AF_INET6 socket for v6 */
27978064Sume
28078064Sume#define	IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
28178064Sume#define	IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
28278064Sume#define	IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
28378064Sume#define	IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
28478064Sume#define	IN6P_RTHDR		0x100000 /* receive routing header */
28578064Sume#define	IN6P_RTHDRDSTOPTS	0x200000 /* receive dstoptions before rthdr */
28678064Sume#define IN6P_AUTOFLOWLABEL	0x800000 /* attach flowlabel automatically */
28778064Sume
28819622Sfenner#define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
28953541Sshin					INP_RECVIF|\
29078064Sume				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
29178064Sume				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
29278064Sume				 IN6P_AUTOFLOWLABEL)
29378064Sume#define	INP_UNMAPPABLEOPTS	(IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
29478064Sume				 IN6P_AUTOFLOWLABEL)
2951541Srgrimes
29653541Sshin /* for KAME src sync over BSD*'s */
29753541Sshin#define	IN6P_HIGHPORT		INP_HIGHPORT
29853541Sshin#define	IN6P_LOWPORT		INP_LOWPORT
29953541Sshin#define	IN6P_ANONPORT		INP_ANONPORT
30053541Sshin#define	IN6P_RECVIF		INP_RECVIF
30153541Sshin#define	IN6P_MTUDISC		INP_MTUDISC
30253541Sshin#define	IN6P_FAITH		INP_FAITH
30353541Sshin#define	IN6P_CONTROLOPTS INP_CONTROLOPTS
30453541Sshin	/*
30553541Sshin	 * socket AF version is {newer than,or include}
30653541Sshin	 * actual datagram AF version
30753541Sshin	 */
30853541Sshin
3091541Srgrimes#define	INPLOOKUP_WILDCARD	1
3101541Srgrimes#define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
31153541Sshin#define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
3121541Srgrimes
31353541Sshin#define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
31453541Sshin
31553541Sshin#define	INP_CHECK_SOCKAF(so, af) 	(INP_SOCKAF(so) == af)
31653541Sshin
31755205Speter#ifdef _KERNEL
31853541Sshinextern int	ipport_lowfirstauto;
31953541Sshinextern int	ipport_lowlastauto;
32053541Sshinextern int	ipport_firstauto;
32153541Sshinextern int	ipport_lastauto;
32253541Sshinextern int	ipport_hifirstauto;
32353541Sshinextern int	ipport_hilastauto;
32453541Sshin
32598102Shsuvoid	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
32692723Salfredvoid	in_losing(struct inpcb *);
32798211Shsustruct inpcb *
32898211Shsu	in_rtchange(struct inpcb *, int);
32992723Salfredint	in_pcballoc(struct socket *, struct inpcbinfo *, struct thread *);
33092723Salfredint	in_pcbbind(struct inpcb *, struct sockaddr *, struct thread *);
33192723Salfredint	in_pcbconnect(struct inpcb *, struct sockaddr *, struct thread *);
33292723Salfredvoid	in_pcbdetach(struct inpcb *);
33392723Salfredvoid	in_pcbdisconnect(struct inpcb *);
33492723Salfredint	in_pcbinshash(struct inpcb *);
33592723Salfredint	in_pcbladdr(struct inpcb *, struct sockaddr *,
33692723Salfred	    struct sockaddr_in **);
3371541Srgrimesstruct inpcb *
33892723Salfred	in_pcblookup_local(struct inpcbinfo *,
33992723Salfred	    struct in_addr, u_int, int);
3407684Sdgstruct inpcb *
34193085Sbde	in_pcblookup_hash(struct inpcbinfo *, struct in_addr, u_int,
34293085Sbde	    struct in_addr, u_int, int, struct ifnet *);
34398102Shsuvoid	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
34498211Shsu	    int, struct inpcb *(*)(struct inpcb *, int));
34592723Salfredvoid	in_pcbrehash(struct inpcb *);
34698102Shsuint	in_setpeeraddr(struct socket *so, struct sockaddr **nam, struct inpcbinfo *pcbinfo);
34798102Shsuint	in_setsockaddr(struct socket *so, struct sockaddr **nam, struct inpcbinfo *pcbinfo);;
348102218Struckmanstruct sockaddr *
349102218Struckman	in_sockaddr(in_port_t port, struct in_addr *addr);
35092723Salfredvoid	in_pcbremlists(struct inpcb *inp);
35194304Sjhbint	prison_xinpcb(struct thread *td, struct inpcb *inp);
35255205Speter#endif /* _KERNEL */
35334881Swollman
35434923Sbde#endif /* !_NETINET_IN_PCB_H_ */
355