xref: /freebsd/sys/netinet/in_pcb.h (revision 42c159fe388a3765f69860c84183700af37aca8a)
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 #include <net/route.h>
43 #include <netinet6/ipsec.h> /* for IPSEC */
44 #include <vm/uma.h>
45 
46 #define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
47 #define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
48 
49 /*
50  * Common structure pcb for internet protocol implementation.
51  * Here are stored pointers to local and foreign host table
52  * entries, local and foreign socket numbers, and pointers
53  * up (to a socket structure) and down (to a protocol-specific)
54  * control block.
55  */
56 LIST_HEAD(inpcbhead, inpcb);
57 LIST_HEAD(inpcbporthead, inpcbport);
58 typedef	u_quad_t	inp_gen_t;
59 
60 /*
61  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
62  * So, AF_INET6 null laddr is also used as AF_INET null laddr,
63  * by utilize following structure. (At last, same as INRIA)
64  */
65 struct in_addr_4in6 {
66 	u_int32_t	ia46_pad32[3];
67 	struct	in_addr	ia46_addr4;
68 };
69 
70 /*
71  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.
72  * in_conninfo has some extra padding to accomplish this.
73  */
74 struct in_endpoints {
75 	u_int16_t	ie_fport;		/* foreign port */
76 	u_int16_t	ie_lport;		/* local port */
77 	/* protocol dependent part, local and foreign addr */
78 	union {
79 		/* foreign host table entry */
80 		struct	in_addr_4in6 ie46_foreign;
81 		struct	in6_addr ie6_foreign;
82 	} ie_dependfaddr;
83 	union {
84 		/* local host table entry */
85 		struct	in_addr_4in6 ie46_local;
86 		struct	in6_addr ie6_local;
87 	} ie_dependladdr;
88 #define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
89 #define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
90 #define	ie6_faddr	ie_dependfaddr.ie6_foreign
91 #define	ie6_laddr	ie_dependladdr.ie6_local
92 };
93 
94 /*
95  * XXX
96  * At some point struct route should possibly change to:
97  *   struct rtentry *rt
98  *   struct in_endpoints *ie;
99  */
100 struct in_conninfo {
101 	u_int8_t	inc_flags;
102 	u_int8_t	inc_len;
103 	u_int16_t	inc_pad;	/* XXX alignment for in_endpoints */
104 	/* protocol dependent part; cached route */
105 	struct	in_endpoints inc_ie;
106 	union {
107 		/* placeholder for routing entry */
108 		struct	route inc4_route;
109 		struct	route_in6 inc6_route;
110 	} inc_dependroute;
111 };
112 #define inc_isipv6	inc_flags	/* temp compatability */
113 #define	inc_fport	inc_ie.ie_fport
114 #define	inc_lport	inc_ie.ie_lport
115 #define	inc_faddr	inc_ie.ie_faddr
116 #define	inc_laddr	inc_ie.ie_laddr
117 #define	inc_route	inc_dependroute.inc4_route
118 #define	inc6_faddr	inc_ie.ie6_faddr
119 #define	inc6_laddr	inc_ie.ie6_laddr
120 #define	inc6_route	inc_dependroute.inc6_route
121 
122 struct	icmp6_filter;
123 
124 struct inpcb {
125 	LIST_ENTRY(inpcb) inp_hash; /* hash list */
126 	LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
127 	u_int32_t	inp_flow;
128 
129 	/* local and foreign ports, local and foreign addr */
130 	struct	in_conninfo inp_inc;
131 
132 	caddr_t	inp_ppcb;		/* pointer to per-protocol pcb */
133 	struct	inpcbinfo *inp_pcbinfo;	/* PCB list info */
134 	struct	socket *inp_socket;	/* back pointer to socket */
135 					/* list for this PCB's local port */
136 	int	inp_flags;		/* generic IP/datagram flags */
137 
138 	struct	inpcbpolicy *inp_sp; /* for IPSEC */
139 	u_char	inp_vflag;		/* IP version flag (v4/v6) */
140 #define	INP_IPV4	0x1
141 #define	INP_IPV6	0x2
142 	u_char	inp_ip_ttl;		/* time to live proto */
143 	u_char	inp_ip_p;		/* protocol proto */
144 
145 	/* protocol dependent part; options */
146 	struct {
147 		u_char	inp4_ip_tos;		/* type of service proto */
148 		struct	mbuf *inp4_options;	/* IP options */
149 		struct	ip_moptions *inp4_moptions; /* IP multicast options */
150 	} inp_depend4;
151 #define inp_fport	inp_inc.inc_fport
152 #define inp_lport	inp_inc.inc_lport
153 #define	inp_faddr	inp_inc.inc_faddr
154 #define	inp_laddr	inp_inc.inc_laddr
155 #define	inp_route	inp_inc.inc_route
156 #define	inp_ip_tos	inp_depend4.inp4_ip_tos
157 #define	inp_options	inp_depend4.inp4_options
158 #define	inp_moptions	inp_depend4.inp4_moptions
159 	struct {
160 		/* IP options */
161 		struct	mbuf *inp6_options;
162 		/* IP6 options for outgoing packets */
163 		struct	ip6_pktopts *inp6_outputopts;
164 		/* IP multicast options */
165 		struct	ip6_moptions *inp6_moptions;
166 		/* ICMPv6 code type filter */
167 		struct	icmp6_filter *inp6_icmp6filt;
168 		/* IPV6_CHECKSUM setsockopt */
169 		int	inp6_cksum;
170 		u_short	inp6_ifindex;
171 		short	inp6_hops;
172 		u_int8_t	inp6_hlim;
173 	} inp_depend6;
174 	LIST_ENTRY(inpcb) inp_portlist;
175 	struct	inpcbport *inp_phd;	/* head of this list */
176 	inp_gen_t	inp_gencnt;	/* generation count of this instance */
177 #define	in6p_faddr	inp_inc.inc6_faddr
178 #define	in6p_laddr	inp_inc.inc6_laddr
179 #define	in6p_route	inp_inc.inc6_route
180 #define	in6p_ip6_hlim	inp_depend6.inp6_hlim
181 #define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
182 #define	in6p_ip6_nxt	inp_ip_p
183 #define	in6p_flowinfo	inp_flow
184 #define	in6p_vflag	inp_vflag
185 #define	in6p_options	inp_depend6.inp6_options
186 #define	in6p_outputopts	inp_depend6.inp6_outputopts
187 #define	in6p_moptions	inp_depend6.inp6_moptions
188 #define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
189 #define	in6p_cksum	inp_depend6.inp6_cksum
190 #define	inp6_ifindex	inp_depend6.inp6_ifindex
191 #define	in6p_flags	inp_flags  /* for KAME src sync over BSD*'s */
192 #define	in6p_socket	inp_socket  /* for KAME src sync over BSD*'s */
193 #define	in6p_lport	inp_lport  /* for KAME src sync over BSD*'s */
194 #define	in6p_fport	inp_fport  /* for KAME src sync over BSD*'s */
195 #define	in6p_ppcb	inp_ppcb  /* for KAME src sync over BSD*'s */
196 };
197 /*
198  * The range of the generation count, as used in this implementation,
199  * is 9e19.  We would have to create 300 billion connections per
200  * second for this number to roll over in a year.  This seems sufficiently
201  * unlikely that we simply don't concern ourselves with that possibility.
202  */
203 
204 /*
205  * Interface exported to userland by various protocols which use
206  * inpcbs.  Hack alert -- only define if struct xsocket is in scope.
207  */
208 #ifdef _SYS_SOCKETVAR_H_
209 struct	xinpcb {
210 	size_t	xi_len;		/* length of this structure */
211 	struct	inpcb xi_inp;
212 	struct	xsocket xi_socket;
213 	u_quad_t	xi_alignment_hack;
214 };
215 
216 struct	xinpgen {
217 	size_t	xig_len;	/* length of this structure */
218 	u_int	xig_count;	/* number of PCBs at this time */
219 	inp_gen_t xig_gen;	/* generation count at this time */
220 	so_gen_t xig_sogen;	/* socket generation count at this time */
221 };
222 #endif /* _SYS_SOCKETVAR_H_ */
223 
224 struct inpcbport {
225 	LIST_ENTRY(inpcbport) phd_hash;
226 	struct inpcbhead phd_pcblist;
227 	u_short phd_port;
228 };
229 
230 struct inpcbinfo {		/* XXX documentation, prefixes */
231 	struct	inpcbhead *hashbase;
232 	u_long	hashmask;
233 	struct	inpcbporthead *porthashbase;
234 	u_long	porthashmask;
235 	struct	inpcbhead *listhead;
236 	u_short	lastport;
237 	u_short	lastlow;
238 	u_short	lasthi;
239 	uma_zone_t ipi_zone; /* zone to allocate pcbs from */
240 	u_int	ipi_count;	/* number of pcbs in this list */
241 	u_quad_t ipi_gencnt;	/* current generation count */
242 };
243 
244 #define INP_PCBHASH(faddr, lport, fport, mask) \
245 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
246 #define INP_PCBPORTHASH(lport, mask) \
247 	(ntohs((lport)) & (mask))
248 
249 /* flags in inp_flags: */
250 #define	INP_RECVOPTS		0x01	/* receive incoming IP options */
251 #define	INP_RECVRETOPTS		0x02	/* receive IP options for reply */
252 #define	INP_RECVDSTADDR		0x04	/* receive IP dst address */
253 #define	INP_HDRINCL		0x08	/* user supplies entire IP header */
254 #define	INP_HIGHPORT		0x10	/* user wants "high" port binding */
255 #define	INP_LOWPORT		0x20	/* user wants "low" port binding */
256 #define	INP_ANONPORT		0x40	/* port chosen for user */
257 #define	INP_RECVIF		0x80	/* receive incoming interface */
258 #define	INP_MTUDISC		0x100	/* user can do MTU discovery */
259 #define	INP_FAITH		0x200	/* accept FAITH'ed connections */
260 
261 #define IN6P_IPV6_V6ONLY	0x008000 /* restrict AF_INET6 socket for v6 */
262 
263 #define	IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
264 #define	IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
265 #define	IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
266 #define	IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
267 #define	IN6P_RTHDR		0x100000 /* receive routing header */
268 #define	IN6P_RTHDRDSTOPTS	0x200000 /* receive dstoptions before rthdr */
269 #define IN6P_AUTOFLOWLABEL	0x800000 /* attach flowlabel automatically */
270 #define	IN6P_BINDV6ONLY		0x10000000 /* do not grab IPv4 traffic */
271 
272 #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
273 					INP_RECVIF|\
274 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
275 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
276 				 IN6P_AUTOFLOWLABEL)
277 #define	INP_UNMAPPABLEOPTS	(IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
278 				 IN6P_AUTOFLOWLABEL)
279 
280  /* for KAME src sync over BSD*'s */
281 #define	IN6P_HIGHPORT		INP_HIGHPORT
282 #define	IN6P_LOWPORT		INP_LOWPORT
283 #define	IN6P_ANONPORT		INP_ANONPORT
284 #define	IN6P_RECVIF		INP_RECVIF
285 #define	IN6P_MTUDISC		INP_MTUDISC
286 #define	IN6P_FAITH		INP_FAITH
287 #define	IN6P_CONTROLOPTS INP_CONTROLOPTS
288 	/*
289 	 * socket AF version is {newer than,or include}
290 	 * actual datagram AF version
291 	 */
292 
293 #define	INPLOOKUP_WILDCARD	1
294 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
295 #define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
296 
297 #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
298 
299 #define	INP_CHECK_SOCKAF(so, af) 	(INP_SOCKAF(so) == af)
300 
301 #ifdef _KERNEL
302 extern int	ipport_lowfirstauto;
303 extern int	ipport_lowlastauto;
304 extern int	ipport_firstauto;
305 extern int	ipport_lastauto;
306 extern int	ipport_hifirstauto;
307 extern int	ipport_hilastauto;
308 
309 void	in_pcbpurgeif0(struct inpcb *, struct ifnet *);
310 void	in_losing(struct inpcb *);
311 void	in_rtchange(struct inpcb *, int);
312 int	in_pcballoc(struct socket *, struct inpcbinfo *, struct thread *);
313 int	in_pcbbind(struct inpcb *, struct sockaddr *, struct thread *);
314 int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct thread *);
315 void	in_pcbdetach(struct inpcb *);
316 void	in_pcbdisconnect(struct inpcb *);
317 int	in_pcbinshash(struct inpcb *);
318 int	in_pcbladdr(struct inpcb *, struct sockaddr *,
319 	    struct sockaddr_in **);
320 struct inpcb *
321 	in_pcblookup_local(struct inpcbinfo *,
322 	    struct in_addr, u_int, int);
323 struct inpcb *
324 	in_pcblookup_hash(struct inpcbinfo *, struct in_addr, u_int,
325 	    struct in_addr, u_int, int, struct ifnet *);
326 void	in_pcbnotifyall(struct inpcbhead *, struct in_addr,
327 	    int, void (*)(struct inpcb *, int));
328 void	in_pcbrehash(struct inpcb *);
329 int	in_setpeeraddr(struct socket *so, struct sockaddr **nam);
330 int	in_setsockaddr(struct socket *so, struct sockaddr **nam);
331 void	in_pcbremlists(struct inpcb *inp);
332 int	prison_xinpcb(struct proc *p, struct inpcb *inp);
333 #endif /* _KERNEL */
334 
335 #endif /* !_NETINET_IN_PCB_H_ */
336