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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
30  * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.100.2.1 2007/12/07 05:46:08 kmacy Exp $
31  */
32 
33 #ifndef _USER_INPCB_H_
34 #define _USER_INPCB_H_
35 
36 #include <user_route.h> /* was <net/route.h> */
37 
38 struct inpcbpolicy;
39 
40 /*
41  * Struct inpcb is the ommon structure pcb for the Internet Protocol
42  * implementation.
43  *
44  * Pointers to local and foreign host table entries, local and foreign socket
45  * numbers, and pointers up (to a socket structure) and down (to a
46  * protocol-specific control block) are stored here.
47  */
48 LIST_HEAD(inpcbhead, inpcb);
49 LIST_HEAD(inpcbporthead, inpcbport);
50 
51 /*
52  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
53  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
54  * the following structure.
55  */
56 struct in_addr_4in6 {
57 	uint32_t	ia46_pad32[3];
58 	struct	in_addr	ia46_addr4;
59 };
60 
61 /*
62  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
63  * some extra padding to accomplish this.
64  */
65 struct in_endpoints {
66 	uint16_t	ie_fport;		/* foreign port */
67 	uint16_t	ie_lport;		/* local port */
68 	/* protocol dependent part, local and foreign addr */
69 	union {
70 		/* foreign host table entry */
71 		struct	in_addr_4in6 ie46_foreign;
72 		struct	in6_addr ie6_foreign;
73 	} ie_dependfaddr;
74 	union {
75 		/* local host table entry */
76 		struct	in_addr_4in6 ie46_local;
77 		struct	in6_addr ie6_local;
78 	} ie_dependladdr;
79 #define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
80 #define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
81 #define	ie6_faddr	ie_dependfaddr.ie6_foreign
82 #define	ie6_laddr	ie_dependladdr.ie6_local
83 };
84 
85 /*
86  * XXX The defines for inc_* are hacks and should be changed to direct
87  * references.
88  */
89 struct in_conninfo {
90 	uint8_t	inc_flags;
91 	uint8_t	inc_len;
92 	uint16_t	inc_pad;	/* XXX alignment for in_endpoints */
93 	/* protocol dependent part */
94 	struct	in_endpoints inc_ie;
95 };
96 #define inc_isipv6	inc_flags	/* temp compatibility */
97 #define	inc_fport	inc_ie.ie_fport
98 #define	inc_lport	inc_ie.ie_lport
99 #define	inc_faddr	inc_ie.ie_faddr
100 #define	inc_laddr	inc_ie.ie_laddr
101 #define	inc6_faddr	inc_ie.ie6_faddr
102 #define	inc6_laddr	inc_ie.ie6_laddr
103 
104 struct	icmp6_filter;
105 
106 struct inpcb {
107 	LIST_ENTRY(inpcb) inp_hash;	/* hash list */
108 	LIST_ENTRY(inpcb) inp_list;	/* list for all PCBs of this proto */
109 	void	*inp_ppcb;		/* pointer to per-protocol pcb */
110 	struct	inpcbinfo *inp_pcbinfo;	/* PCB list info */
111 	struct	socket *inp_socket;	/* back pointer to socket */
112 
113 	uint32_t	inp_flow;
114 	int	inp_flags;		/* generic IP/datagram flags */
115 
116 	u_char	inp_vflag;		/* IP version flag (v4/v6) */
117 #define	INP_IPV4	0x1
118 #define	INP_IPV6	0x2
119 #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
120 #define	INP_TIMEWAIT	0x8		/* .. probably doesn't go here */
121 #define	INP_ONESBCAST	0x10		/* send all-ones broadcast */
122 #define	INP_DROPPED	0x20		/* protocol drop flag */
123 #define	INP_SOCKREF	0x40		/* strong socket reference */
124 #define INP_CONN        0x80
125 	u_char	inp_ip_ttl;		/* time to live proto */
126 	u_char	inp_ip_p;		/* protocol proto */
127 	u_char	inp_ip_minttl;		/* minimum TTL or drop */
128 	uint32_t inp_ispare1;		/* connection id / queue id */
129 	void	*inp_pspare[2];		/* rtentry / general use */
130 
131 	/* Local and foreign ports, local and foreign addr. */
132 	struct	in_conninfo inp_inc;
133 
134 					/* list for this PCB's local port */
135 	struct	label *inp_label;	/* MAC label */
136 	struct	inpcbpolicy *inp_sp;    /* for IPSEC */
137 
138 	/* Protocol-dependent part; options. */
139 	struct {
140 		u_char	inp4_ip_tos;		/* type of service proto */
141 		struct	mbuf *inp4_options;	/* IP options */
142 		struct	ip_moptions *inp4_moptions; /* IP multicast options */
143 	} inp_depend4;
144 #define	inp_fport	inp_inc.inc_fport
145 #define	inp_lport	inp_inc.inc_lport
146 #define	inp_faddr	inp_inc.inc_faddr
147 #define	inp_laddr	inp_inc.inc_laddr
148 #define	inp_ip_tos	inp_depend4.inp4_ip_tos
149 #define	inp_options	inp_depend4.inp4_options
150 #define	inp_moptions	inp_depend4.inp4_moptions
151 	struct {
152 		/* IP options */
153 		struct	mbuf *inp6_options;
154 		/* IP6 options for outgoing packets */
155 		struct	ip6_pktopts *inp6_outputopts;
156 		/* IP multicast options */
157 #if 0
158 		struct	ip6_moptions *inp6_moptions;
159 #endif
160 		/* ICMPv6 code type filter */
161 		struct	icmp6_filter *inp6_icmp6filt;
162 		/* IPV6_CHECKSUM setsockopt */
163 		int	inp6_cksum;
164 		short	inp6_hops;
165 	} inp_depend6;
166 	LIST_ENTRY(inpcb) inp_portlist;
167 	struct	inpcbport *inp_phd;	/* head of this list */
168 #define inp_zero_size offsetof(struct inpcb, inp_gencnt)
169 	struct mtx	inp_mtx;
170 
171 #define	in6p_faddr	inp_inc.inc6_faddr
172 #define	in6p_laddr	inp_inc.inc6_laddr
173 #define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
174 #define	in6p_ip6_nxt	inp_ip_p
175 #define	in6p_flowinfo	inp_flow
176 #define	in6p_vflag	inp_vflag
177 #define	in6p_options	inp_depend6.inp6_options
178 #define	in6p_outputopts	inp_depend6.inp6_outputopts
179 #if 0
180 #define	in6p_moptions	inp_depend6.inp6_moptions
181 #endif
182 #define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
183 #define	in6p_cksum	inp_depend6.inp6_cksum
184 #define	in6p_flags	inp_flags  /* for KAME src sync over BSD*'s */
185 #define	in6p_socket	inp_socket  /* for KAME src sync over BSD*'s */
186 #define	in6p_lport	inp_lport  /* for KAME src sync over BSD*'s */
187 #define	in6p_fport	inp_fport  /* for KAME src sync over BSD*'s */
188 #define	in6p_ppcb	inp_ppcb  /* for KAME src sync over BSD*'s */
189 };
190 /*
191  * The range of the generation count, as used in this implementation, is 9e19.
192  * We would have to create 300 billion connections per second for this number
193  * to roll over in a year.  This seems sufficiently unlikely that we simply
194  * don't concern ourselves with that possibility.
195  */
196 
197 struct inpcbport {
198 	LIST_ENTRY(inpcbport) phd_hash;
199 	struct inpcbhead phd_pcblist;
200 	u_short phd_port;
201 };
202 
203 /*
204  * Global data structure for each high-level protocol (UDP, TCP, ...) in both
205  * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
206  */
207 struct inpcbinfo {
208 	/*
209 	 * Global list of inpcbs on the protocol.
210 	 */
211 	struct inpcbhead	*ipi_listhead;
212 	u_int			 ipi_count;
213 
214 	/*
215 	 * Global hash of inpcbs, hashed by local and foreign addresses and
216 	 * port numbers.
217 	 */
218 	struct inpcbhead	*ipi_hashbase;
219 	u_long			 ipi_hashmask;
220 
221 	/*
222 	 * Global hash of inpcbs, hashed by only local port number.
223 	 */
224 	struct inpcbporthead	*ipi_porthashbase;
225 	u_long			 ipi_porthashmask;
226 
227 	/*
228 	 * Fields associated with port lookup and allocation.
229 	 */
230 	u_short			 ipi_lastport;
231 	u_short			 ipi_lastlow;
232 	u_short			 ipi_lasthi;
233 
234 	/*
235 	 * UMA zone from which inpcbs are allocated for this protocol.
236 	 */
237 	struct	uma_zone	*ipi_zone;
238 
239 	/*
240 	 * Generation count--incremented each time a connection is allocated
241 	 * or freed.
242 	 */
243 	struct mtx		 ipi_mtx;
244 
245 	/*
246 	 * vimage 1
247 	 * general use 1
248 	 */
249 	void 			*ipi_pspare[2];
250 };
251 
252 #define INP_LOCK_INIT(inp, d, t) \
253 	mtx_init(&(inp)->inp_mtx, (d), (t), MTX_DEF | MTX_RECURSE | MTX_DUPOK)
254 #define INP_LOCK_DESTROY(inp)	mtx_destroy(&(inp)->inp_mtx)
255 #define INP_LOCK(inp)		mtx_lock(&(inp)->inp_mtx)
256 #define INP_UNLOCK(inp)		mtx_unlock(&(inp)->inp_mtx)
257 #define INP_LOCK_ASSERT(inp)	mtx_assert(&(inp)->inp_mtx, MA_OWNED)
258 #define	INP_UNLOCK_ASSERT(inp)	mtx_assert(&(inp)->inp_mtx, MA_NOTOWNED)
259 
260 #define INP_INFO_LOCK_INIT(ipi, d) \
261 	mtx_init(&(ipi)->ipi_mtx, (d), NULL, MTX_DEF | MTX_RECURSE)
262 #define INP_INFO_LOCK_DESTROY(ipi)  mtx_destroy(&(ipi)->ipi_mtx)
263 #define INP_INFO_RLOCK(ipi)	mtx_lock(&(ipi)->ipi_mtx)
264 #define INP_INFO_WLOCK(ipi)	mtx_lock(&(ipi)->ipi_mtx)
265 #define INP_INFO_RUNLOCK(ipi)	mtx_unlock(&(ipi)->ipi_mtx)
266 #define INP_INFO_WUNLOCK(ipi)	mtx_unlock(&(ipi)->ipi_mtx)
267 #define INP_INFO_RLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_mtx, MA_OWNED)
268 #define INP_INFO_WLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_mtx, MA_OWNED)
269 #define INP_INFO_UNLOCK_ASSERT(ipi)	mtx_assert(&(ipi)->ipi_mtx, MA_NOTOWNED)
270 
271 #define INP_PCBHASH(faddr, lport, fport, mask) \
272 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
273 #define INP_PCBPORTHASH(lport, mask) \
274 	(ntohs((lport)) & (mask))
275 
276 /* flags in inp_flags: */
277 #define	INP_RECVOPTS		0x01	/* receive incoming IP options */
278 #define	INP_RECVRETOPTS		0x02	/* receive IP options for reply */
279 #define	INP_RECVDSTADDR		0x04	/* receive IP dst address */
280 #define	INP_HDRINCL		0x08	/* user supplies entire IP header */
281 #define	INP_HIGHPORT		0x10	/* user wants "high" port binding */
282 #define	INP_LOWPORT		0x20	/* user wants "low" port binding */
283 #define	INP_ANONPORT		0x40	/* port chosen for user */
284 #define	INP_RECVIF		0x80	/* receive incoming interface */
285 #define	INP_MTUDISC		0x100	/* user can do MTU discovery */
286 #define	INP_FAITH		0x200	/* accept FAITH'ed connections */
287 #define	INP_RECVTTL		0x400	/* receive incoming IP TTL */
288 #define	INP_DONTFRAG		0x800	/* don't fragment packet */
289 
290 #define IN6P_IPV6_V6ONLY	0x008000 /* restrict AF_INET6 socket for v6 */
291 
292 #define	IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
293 #define	IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
294 #define	IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
295 #define	IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
296 #define	IN6P_RTHDR		0x100000 /* receive routing header */
297 #define	IN6P_RTHDRDSTOPTS	0x200000 /* receive dstoptions before rthdr */
298 #define	IN6P_TCLASS		0x400000 /* receive traffic class value */
299 #define	IN6P_AUTOFLOWLABEL	0x800000 /* attach flowlabel automatically */
300 #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
301 #define	IN6P_MTU		0x80000000 /* receive path MTU */
302 
303 #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
304 				 INP_RECVIF|INP_RECVTTL|\
305 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
306 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
307 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
308 				 IN6P_MTU)
309 #define	INP_UNMAPPABLEOPTS	(IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
310 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL)
311 
312  /* for KAME src sync over BSD*'s */
313 #define	IN6P_HIGHPORT		INP_HIGHPORT
314 #define	IN6P_LOWPORT		INP_LOWPORT
315 #define	IN6P_ANONPORT		INP_ANONPORT
316 #define	IN6P_RECVIF		INP_RECVIF
317 #define	IN6P_MTUDISC		INP_MTUDISC
318 #define	IN6P_FAITH		INP_FAITH
319 #define	IN6P_CONTROLOPTS INP_CONTROLOPTS
320 	/*
321 	 * socket AF version is {newer than,or include}
322 	 * actual datagram AF version
323 	 */
324 
325 #define	INPLOOKUP_WILDCARD	1
326 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
327 
328 #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
329 
330 #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
331 
332 extern int	ipport_reservedhigh;
333 extern int	ipport_reservedlow;
334 extern int	ipport_lowfirstauto;
335 extern int	ipport_lowlastauto;
336 extern int	ipport_firstauto;
337 extern int	ipport_lastauto;
338 extern int	ipport_hifirstauto;
339 extern int	ipport_hilastauto;
340 extern struct callout ipport_tick_callout;
341 
342 void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
343 int	in_pcballoc(struct socket *, struct inpcbinfo *);
344 int	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
345 int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
346 void	in_pcbdetach(struct inpcb *);
347 void	in_pcbdisconnect(struct inpcb *);
348 void	in_pcbdrop(struct inpcb *);
349 void	in_pcbfree(struct inpcb *);
350 int	in_pcbinshash(struct inpcb *);
351 struct inpcb *
352 	in_pcblookup_local(struct inpcbinfo *,
353 	    struct in_addr, u_int, int);
354 struct inpcb *
355 	in_pcblookup_hash(struct inpcbinfo *, struct in_addr, u_int,
356 	    struct in_addr, u_int, int, struct ifnet *);
357 void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
358 	    int, struct inpcb *(*)(struct inpcb *, int));
359 void	in_pcbrehash(struct inpcb *);
360 void	in_pcbsetsolabel(struct socket *so);
361 int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
362 int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
363 void	in_pcbsosetlabel(struct socket *so);
364 void	in_pcbremlists(struct inpcb *inp);
365 void	ipport_tick(void *xtp);
366 
367 /*
368  * Debugging routines compiled in when DDB is present.
369  */
370 void	db_print_inpcb(struct inpcb *inp, const char *name, int indent);
371 
372 
373 #endif /* !_NETINET_IN_PCB_H_ */
374