xref: /freebsd/sys/netinet/in_pcb.h (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * Copyright (c) 1982, 1986, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
34  * $FreeBSD$
35  */
36 
37 #ifndef _NETINET_IN_PCB_H_
38 #define _NETINET_IN_PCB_H_
39 
40 #include <sys/queue.h>
41 
42 
43 #include <netinet6/ipsec.h> /* for IPSEC */
44 
45 #define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
46 #define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
47 
48 /*
49  * Common structure pcb for internet protocol implementation.
50  * Here are stored pointers to local and foreign host table
51  * entries, local and foreign socket numbers, and pointers
52  * up (to a socket structure) and down (to a protocol-specific)
53  * control block.
54  */
55 LIST_HEAD(inpcbhead, inpcb);
56 LIST_HEAD(inpcbporthead, inpcbport);
57 typedef	u_quad_t	inp_gen_t;
58 
59 /*
60  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
61  * So, AF_INET6 null laddr is also used as AF_INET null laddr,
62  * by utilize following structure. (At last, same as INRIA)
63  */
64 struct in_addr_4in6 {
65 	u_int32_t	ia46_pad32[3];
66 	struct	in_addr	ia46_addr4;
67 };
68 
69 /*
70  * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
71  * of the structure.  Therefore, it is important that the members in
72  * that position not contain any information which is required to be
73  * stable.
74  */
75 struct	icmp6_filter;
76 
77 struct inpcb {
78 	LIST_ENTRY(inpcb) inp_hash; /* hash list */
79 	u_short	inp_fport;		/* foreign port */
80 	u_short	inp_lport;		/* local port */
81 	LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
82 	u_int32_t	inp_flow;
83 
84 	/* protocol dependent part, local and foreign addr */
85 	union {
86 		/* foreign host table entry */
87 		struct	in_addr_4in6 inp46_foreign;
88 		struct	in6_addr inp6_foreign;
89 	} inp_dependfaddr;
90 	union {
91 		/* local host table entry */
92 		struct	in_addr_4in6 inp46_local;
93 		struct	in6_addr inp6_local;
94 	} inp_dependladdr;
95 
96 	caddr_t	inp_ppcb;		/* pointer to per-protocol pcb */
97 	struct	inpcbinfo *inp_pcbinfo;	/* PCB list info */
98 	struct	socket *inp_socket;	/* back pointer to socket */
99 					/* list for this PCB's local port */
100 	int	inp_flags;		/* generic IP/datagram flags */
101 
102 	/* protocol dependent part; cached route */
103 	union {
104 		/* placeholder for routing entry */
105 		struct	route inp4_route;
106 		struct	route_in6 inp6_route;
107 	} inp_dependroute;
108 
109 	struct	inpcbpolicy *inp_sp; /* for IPSEC */
110 	u_char	inp_vflag;
111 #define	INP_IPV4	0x1
112 #define	INP_IPV6	0x2
113 	u_char	inp_ip_ttl;		/* time to live proto */
114 	u_char	inp_ip_p;		/* protocol proto */
115 
116 	/* protocol dependent part; options */
117 	struct {
118 		u_char	inp4_ip_tos;		/* type of service proto */
119 		struct	mbuf *inp4_options;	/* IP options */
120 		struct	ip_moptions *inp4_moptions; /* IP multicast options */
121 	} inp_depend4;
122 #define	inp_faddr	inp_dependfaddr.inp46_foreign.ia46_addr4
123 #define	inp_laddr	inp_dependladdr.inp46_local.ia46_addr4
124 #define	inp_route	inp_dependroute.inp4_route
125 #define	inp_ip_tos	inp_depend4.inp4_ip_tos
126 #define	inp_options	inp_depend4.inp4_options
127 #define	inp_moptions	inp_depend4.inp4_moptions
128 	struct {
129 		/* IP options */
130 		struct	mbuf *inp6_options;
131 		/* IP6 options for outgoing packets */
132 		struct	ip6_pktopts *inp6_outputopts;
133 		/* IP multicast options */
134 		struct	ip6_moptions *inp6_moptions;
135 		/* ICMPv6 code type filter */
136 		struct	icmp6_filter *inp6_icmp6filt;
137 		/* IPV6_CHECKSUM setsockopt */
138 		int	inp6_cksum;
139 		u_short	inp6_ifindex;
140 		short	inp6_hops;
141 		u_int8_t	inp6_hlim;
142 	} inp_depend6;
143 	LIST_ENTRY(inpcb) inp_portlist;
144 	struct	inpcbport *inp_phd;	/* head of this list */
145 	inp_gen_t	inp_gencnt;	/* generation count of this instance */
146 #define	in6p_faddr	inp_dependfaddr.inp6_foreign
147 #define	in6p_laddr	inp_dependladdr.inp6_local
148 #define	in6p_route	inp_dependroute.inp6_route
149 #define	in6p_ip6_hlim	inp_depend6.inp6_hlim
150 #define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
151 #define	in6p_ip6_nxt	inp_ip_p
152 #define	in6p_flowinfo	inp_flow
153 #define	in6p_vflag	inp_vflag
154 #define	in6p_options	inp_depend6.inp6_options
155 #define	in6p_outputopts	inp_depend6.inp6_outputopts
156 #define	in6p_moptions	inp_depend6.inp6_moptions
157 #define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
158 #define	in6p_cksum	inp_depend6.inp6_cksum
159 #define	inp6_ifindex	inp_depend6.inp6_ifindex
160 #define	in6p_flags	inp_flags  /* for KAME src sync over BSD*'s */
161 #define	in6p_socket	inp_socket  /* for KAME src sync over BSD*'s */
162 #define	in6p_lport	inp_lport  /* for KAME src sync over BSD*'s */
163 #define	in6p_fport	inp_fport  /* for KAME src sync over BSD*'s */
164 #define	in6p_ppcb	inp_ppcb  /* for KAME src sync over BSD*'s */
165 };
166 /*
167  * The range of the generation count, as used in this implementation,
168  * is 9e19.  We would have to create 300 billion connections per
169  * second for this number to roll over in a year.  This seems sufficiently
170  * unlikely that we simply don't concern ourselves with that possibility.
171  */
172 
173 /*
174  * Interface exported to userland by various protocols which use
175  * inpcbs.  Hack alert -- only define if struct xsocket is in scope.
176  */
177 #ifdef _SYS_SOCKETVAR_H_
178 struct	xinpcb {
179 	size_t	xi_len;		/* length of this structure */
180 	struct	inpcb xi_inp;
181 	struct	xsocket xi_socket;
182 	u_quad_t	xi_alignment_hack;
183 };
184 
185 struct	xinpgen {
186 	size_t	xig_len;	/* length of this structure */
187 	u_int	xig_count;	/* number of PCBs at this time */
188 	inp_gen_t xig_gen;	/* generation count at this time */
189 	so_gen_t xig_sogen;	/* socket generation count at this time */
190 };
191 #endif /* _SYS_SOCKETVAR_H_ */
192 
193 struct inpcbport {
194 	LIST_ENTRY(inpcbport) phd_hash;
195 	struct inpcbhead phd_pcblist;
196 	u_short phd_port;
197 };
198 
199 struct inpcbinfo {		/* XXX documentation, prefixes */
200 	struct	inpcbhead *hashbase;
201 	u_long	hashmask;
202 	struct	inpcbporthead *porthashbase;
203 	u_long	porthashmask;
204 	struct	inpcbhead *listhead;
205 	u_short	lastport;
206 	u_short	lastlow;
207 	u_short	lasthi;
208 	struct	vm_zone *ipi_zone; /* zone to allocate pcbs from */
209 	u_int	ipi_count;	/* number of pcbs in this list */
210 	u_quad_t ipi_gencnt;	/* current generation count */
211 };
212 
213 #define INP_PCBHASH(faddr, lport, fport, mask) \
214 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
215 #define INP_PCBPORTHASH(lport, mask) \
216 	(ntohs((lport)) & (mask))
217 
218 /* flags in inp_flags: */
219 #define	INP_RECVOPTS		0x01	/* receive incoming IP options */
220 #define	INP_RECVRETOPTS		0x02	/* receive IP options for reply */
221 #define	INP_RECVDSTADDR		0x04	/* receive IP dst address */
222 #define	INP_HDRINCL		0x08	/* user supplies entire IP header */
223 #define	INP_HIGHPORT		0x10	/* user wants "high" port binding */
224 #define	INP_LOWPORT		0x20	/* user wants "low" port binding */
225 #define	INP_ANONPORT		0x40	/* port chosen for user */
226 #define	INP_RECVIF		0x80	/* receive incoming interface */
227 #define	INP_MTUDISC		0x100	/* user can do MTU discovery */
228 #define	INP_FAITH		0x200	/* accept FAITH'ed connections */
229 
230 #define IN6P_IPV6_V6ONLY	0x008000 /* restrict AF_INET6 socket for v6 */
231 
232 #define	IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
233 #define	IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
234 #define	IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
235 #define	IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
236 #define	IN6P_RTHDR		0x100000 /* receive routing header */
237 #define	IN6P_RTHDRDSTOPTS	0x200000 /* receive dstoptions before rthdr */
238 #define IN6P_AUTOFLOWLABEL	0x800000 /* attach flowlabel automatically */
239 #define	IN6P_BINDV6ONLY		0x10000000 /* do not grab IPv4 traffic */
240 
241 #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
242 					INP_RECVIF|\
243 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
244 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
245 				 IN6P_AUTOFLOWLABEL)
246 #define	INP_UNMAPPABLEOPTS	(IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
247 				 IN6P_AUTOFLOWLABEL)
248 
249  /* for KAME src sync over BSD*'s */
250 #define	IN6P_HIGHPORT		INP_HIGHPORT
251 #define	IN6P_LOWPORT		INP_LOWPORT
252 #define	IN6P_ANONPORT		INP_ANONPORT
253 #define	IN6P_RECVIF		INP_RECVIF
254 #define	IN6P_MTUDISC		INP_MTUDISC
255 #define	IN6P_FAITH		INP_FAITH
256 #define	IN6P_CONTROLOPTS INP_CONTROLOPTS
257 	/*
258 	 * socket AF version is {newer than,or include}
259 	 * actual datagram AF version
260 	 */
261 
262 #define	INPLOOKUP_WILDCARD	1
263 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
264 #define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
265 
266 #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
267 
268 #define	INP_CHECK_SOCKAF(so, af) 	(INP_SOCKAF(so) == af)
269 
270 #ifdef _KERNEL
271 extern int	ipport_lowfirstauto;
272 extern int	ipport_lowlastauto;
273 extern int	ipport_firstauto;
274 extern int	ipport_lastauto;
275 extern int	ipport_hifirstauto;
276 extern int	ipport_hilastauto;
277 
278 void	in_losing __P((struct inpcb *));
279 void	in_rtchange __P((struct inpcb *, int));
280 int	in_pcballoc __P((struct socket *, struct inpcbinfo *, struct proc *));
281 int	in_pcbbind __P((struct inpcb *, struct sockaddr *, struct proc *));
282 int	in_pcbconnect __P((struct inpcb *, struct sockaddr *, struct proc *));
283 void	in_pcbdetach __P((struct inpcb *));
284 void	in_pcbdisconnect __P((struct inpcb *));
285 int	in_pcbinshash __P((struct inpcb *));
286 int	in_pcbladdr __P((struct inpcb *, struct sockaddr *,
287 	    struct sockaddr_in **));
288 struct inpcb *
289 	in_pcblookup_local __P((struct inpcbinfo *,
290 	    struct in_addr, u_int, int));
291 struct inpcb *
292 	in_pcblookup_hash __P((struct inpcbinfo *,
293 			       struct in_addr, u_int, struct in_addr, u_int,
294 			       int, struct ifnet *));
295 void	in_pcbnotifyall __P((struct inpcbhead *, struct in_addr,
296 	    int, void (*)(struct inpcb *, int)));
297 void	in_pcbrehash __P((struct inpcb *));
298 int	in_setpeeraddr __P((struct socket *so, struct sockaddr **nam));
299 int	in_setsockaddr __P((struct socket *so, struct sockaddr **nam));
300 void	in_pcbremlists __P((struct inpcb *inp));
301 int	prison_xinpcb __P((struct proc *p, struct inpcb *inp));
302 #endif /* _KERNEL */
303 
304 #endif /* !_NETINET_IN_PCB_H_ */
305