xref: /freebsd/sys/netinet/in_pcb.h (revision db612abe8df3355d1eb23bb3b50fdd97bc21e979)
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$
31  */
32 
33 #ifndef _NETINET_IN_PCB_H_
34 #define _NETINET_IN_PCB_H_
35 
36 #include <sys/queue.h>
37 #include <sys/_lock.h>
38 #include <sys/_mutex.h>
39 #include <sys/_rwlock.h>
40 
41 #include <net/route.h>
42 
43 #ifdef _KERNEL
44 #include <sys/rwlock.h>
45 #endif
46 
47 #define	in6pcb		inpcb	/* for KAME src sync over BSD*'s */
48 #define	in6p_sp		inp_sp	/* for KAME src sync over BSD*'s */
49 struct inpcbpolicy;
50 
51 /*
52  * Struct inpcb is the ommon structure pcb for the Internet Protocol
53  * implementation.
54  *
55  * Pointers to local and foreign host table entries, local and foreign socket
56  * numbers, and pointers up (to a socket structure) and down (to a
57  * protocol-specific control block) are stored here.
58  */
59 LIST_HEAD(inpcbhead, inpcb);
60 LIST_HEAD(inpcbporthead, inpcbport);
61 typedef	u_quad_t	inp_gen_t;
62 
63 /*
64  * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
65  * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
66  * the following structure.
67  */
68 struct in_addr_4in6 {
69 	u_int32_t	ia46_pad32[3];
70 	struct	in_addr	ia46_addr4;
71 };
72 
73 /*
74  * NOTE: ipv6 addrs should be 64-bit aligned, per RFC 2553.  in_conninfo has
75  * some extra padding to accomplish this.
76  */
77 struct in_endpoints {
78 	u_int16_t	ie_fport;		/* foreign port */
79 	u_int16_t	ie_lport;		/* local port */
80 	/* protocol dependent part, local and foreign addr */
81 	union {
82 		/* foreign host table entry */
83 		struct	in_addr_4in6 ie46_foreign;
84 		struct	in6_addr ie6_foreign;
85 	} ie_dependfaddr;
86 	union {
87 		/* local host table entry */
88 		struct	in_addr_4in6 ie46_local;
89 		struct	in6_addr ie6_local;
90 	} ie_dependladdr;
91 #define	ie_faddr	ie_dependfaddr.ie46_foreign.ia46_addr4
92 #define	ie_laddr	ie_dependladdr.ie46_local.ia46_addr4
93 #define	ie6_faddr	ie_dependfaddr.ie6_foreign
94 #define	ie6_laddr	ie_dependladdr.ie6_local
95 };
96 
97 /*
98  * XXX The defines for inc_* are hacks and should be changed to direct
99  * references.
100  */
101 struct in_conninfo {
102 	u_int8_t	inc_flags;
103 	u_int8_t	inc_len;
104 	u_int16_t	inc_pad;	/* XXX alignment for in_endpoints */
105 	/* protocol dependent part */
106 	struct	in_endpoints inc_ie;
107 };
108 #define inc_isipv6	inc_flags	/* temp compatability */
109 #define	inc_fport	inc_ie.ie_fport
110 #define	inc_lport	inc_ie.ie_lport
111 #define	inc_faddr	inc_ie.ie_faddr
112 #define	inc_laddr	inc_ie.ie_laddr
113 #define	inc6_faddr	inc_ie.ie6_faddr
114 #define	inc6_laddr	inc_ie.ie6_laddr
115 
116 struct	icmp6_filter;
117 
118 struct inpcb {
119 	LIST_ENTRY(inpcb) inp_hash;	/* hash list */
120 	LIST_ENTRY(inpcb) inp_list;	/* list for all PCBs of this proto */
121 	void	*inp_ppcb;		/* pointer to per-protocol pcb */
122 	struct	inpcbinfo *inp_pcbinfo;	/* PCB list info */
123 	struct	socket *inp_socket;	/* back pointer to socket */
124 
125 	u_int32_t	inp_flow;
126 	int	inp_flags;		/* generic IP/datagram flags */
127 
128 	u_char	inp_vflag;		/* IP version flag (v4/v6) */
129 #define	INP_IPV4	0x1
130 #define	INP_IPV6	0x2
131 #define	INP_IPV6PROTO	0x4		/* opened under IPv6 protocol */
132 #define	INP_TIMEWAIT	0x8		/* .. probably doesn't go here */
133 #define	INP_ONESBCAST	0x10		/* send all-ones broadcast */
134 #define	INP_DROPPED	0x20		/* protocol drop flag */
135 #define	INP_SOCKREF	0x40		/* strong socket reference */
136 	u_char	inp_ip_ttl;		/* time to live proto */
137 	u_char	inp_ip_p;		/* protocol proto */
138 	u_char	inp_ip_minttl;		/* minimum TTL or drop */
139 	uint32_t inp_ispare1;		/* connection id / queue id */
140 	void	*inp_pspare[2];		/* rtentry / general use */
141 
142 	/* Local and foreign ports, local and foreign addr. */
143 	struct	in_conninfo inp_inc;
144 
145 					/* list for this PCB's local port */
146 	struct	label *inp_label;	/* MAC label */
147 	struct	inpcbpolicy *inp_sp;    /* for IPSEC */
148 
149 	/* Protocol-dependent part; options. */
150 	struct {
151 		u_char	inp4_ip_tos;		/* type of service proto */
152 		struct	mbuf *inp4_options;	/* IP options */
153 		struct	ip_moptions *inp4_moptions; /* IP multicast options */
154 	} inp_depend4;
155 #define	inp_fport	inp_inc.inc_fport
156 #define	inp_lport	inp_inc.inc_lport
157 #define	inp_faddr	inp_inc.inc_faddr
158 #define	inp_laddr	inp_inc.inc_laddr
159 #define	inp_ip_tos	inp_depend4.inp4_ip_tos
160 #define	inp_options	inp_depend4.inp4_options
161 #define	inp_moptions	inp_depend4.inp4_moptions
162 	struct {
163 		/* IP options */
164 		struct	mbuf *inp6_options;
165 		/* IP6 options for outgoing packets */
166 		struct	ip6_pktopts *inp6_outputopts;
167 		/* IP multicast options */
168 		struct	ip6_moptions *inp6_moptions;
169 		/* ICMPv6 code type filter */
170 		struct	icmp6_filter *inp6_icmp6filt;
171 		/* IPV6_CHECKSUM setsockopt */
172 		int	inp6_cksum;
173 		short	inp6_hops;
174 	} inp_depend6;
175 	LIST_ENTRY(inpcb) inp_portlist;
176 	struct	inpcbport *inp_phd;	/* head of this list */
177 #define inp_zero_size offsetof(struct inpcb, inp_gencnt)
178 	inp_gen_t	inp_gencnt;	/* generation count of this instance */
179 	struct rwlock	inp_lock;
180 
181 #define	in6p_faddr	inp_inc.inc6_faddr
182 #define	in6p_laddr	inp_inc.inc6_laddr
183 #define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
184 #define	in6p_ip6_nxt	inp_ip_p
185 #define	in6p_flowinfo	inp_flow
186 #define	in6p_vflag	inp_vflag
187 #define	in6p_options	inp_depend6.inp6_options
188 #define	in6p_outputopts	inp_depend6.inp6_outputopts
189 #define	in6p_moptions	inp_depend6.inp6_moptions
190 #define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
191 #define	in6p_cksum	inp_depend6.inp6_cksum
192 #define	in6p_flags	inp_flags  /* for KAME src sync over BSD*'s */
193 #define	in6p_socket	inp_socket  /* for KAME src sync over BSD*'s */
194 #define	in6p_lport	inp_lport  /* for KAME src sync over BSD*'s */
195 #define	in6p_fport	inp_fport  /* for KAME src sync over BSD*'s */
196 #define	in6p_ppcb	inp_ppcb  /* for KAME src sync over BSD*'s */
197 };
198 /*
199  * The range of the generation count, as used in this implementation, is 9e19.
200  * We would have to create 300 billion connections per second for this number
201  * to roll over in a year.  This seems sufficiently unlikely that we simply
202  * don't concern ourselves with that possibility.
203  */
204 
205 /*
206  * Interface exported to userland by various protocols which use inpcbs.  Hack
207  * alert -- only define if struct xsocket is in scope.
208  */
209 #ifdef _SYS_SOCKETVAR_H_
210 struct	xinpcb {
211 	size_t	xi_len;		/* length of this structure */
212 	struct	inpcb xi_inp;
213 	struct	xsocket xi_socket;
214 	u_quad_t	xi_alignment_hack;
215 };
216 
217 struct	xinpgen {
218 	size_t	xig_len;	/* length of this structure */
219 	u_int	xig_count;	/* number of PCBs at this time */
220 	inp_gen_t xig_gen;	/* generation count at this time */
221 	so_gen_t xig_sogen;	/* socket generation count at this time */
222 };
223 #endif /* _SYS_SOCKETVAR_H_ */
224 
225 struct inpcbport {
226 	LIST_ENTRY(inpcbport) phd_hash;
227 	struct inpcbhead phd_pcblist;
228 	u_short phd_port;
229 };
230 
231 /*
232  * Global data structure for each high-level protocol (UDP, TCP, ...) in both
233  * IPv4 and IPv6.  Holds inpcb lists and information for managing them.
234  */
235 struct inpcbinfo {
236 	/*
237 	 * Global list of inpcbs on the protocol.
238 	 */
239 	struct inpcbhead	*ipi_listhead;
240 	u_int			 ipi_count;
241 
242 	/*
243 	 * Global hash of inpcbs, hashed by local and foreign addresses and
244 	 * port numbers.
245 	 */
246 	struct inpcbhead	*ipi_hashbase;
247 	u_long			 ipi_hashmask;
248 
249 	/*
250 	 * Global hash of inpcbs, hashed by only local port number.
251 	 */
252 	struct inpcbporthead	*ipi_porthashbase;
253 	u_long			 ipi_porthashmask;
254 
255 	/*
256 	 * Fields associated with port lookup and allocation.
257 	 */
258 	u_short			 ipi_lastport;
259 	u_short			 ipi_lastlow;
260 	u_short			 ipi_lasthi;
261 
262 	/*
263 	 * UMA zone from which inpcbs are allocated for this protocol.
264 	 */
265 	struct	uma_zone	*ipi_zone;
266 
267 	/*
268 	 * Generation count--incremented each time a connection is allocated
269 	 * or freed.
270 	 */
271 	u_quad_t		 ipi_gencnt;
272 	struct rwlock		 ipi_lock;
273 
274 	/*
275 	 * vimage 1
276 	 * general use 1
277 	 */
278 	void 			*ipi_pspare[2];
279 };
280 
281 #define INP_LOCK_INIT(inp, d, t) \
282 	rw_init_flags(&(inp)->inp_lock, (t), RW_RECURSE |  RW_DUPOK)
283 #define INP_LOCK_DESTROY(inp)	rw_destroy(&(inp)->inp_lock)
284 #define INP_RLOCK(inp)		rw_rlock(&(inp)->inp_lock)
285 #define INP_WLOCK(inp)		rw_wlock(&(inp)->inp_lock)
286 #define INP_RUNLOCK(inp)	rw_runlock(&(inp)->inp_lock)
287 #define INP_WUNLOCK(inp)	rw_wunlock(&(inp)->inp_lock)
288 #define INP_LOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_LOCKED)
289 #define	INP_RLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_RLOCKED)
290 #define	INP_WLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_WLOCKED)
291 #define	INP_UNLOCK_ASSERT(inp)	rw_assert(&(inp)->inp_lock, RA_UNLOCKED)
292 
293 #ifdef _KERNEL
294 /*
295  * These locking functions are for inpcb consumers outside of sys/netinet,
296  * more specifically, they were added for the benefit of TOE drivers. The
297  * macros are reserved for use by the stack.
298  */
299 void inp_wlock(struct inpcb *);
300 void inp_wunlock(struct inpcb *);
301 void inp_rlock(struct inpcb *);
302 void inp_runlock(struct inpcb *);
303 
304 #ifdef INVARIANTS
305 void inp_lock_assert(struct inpcb *);
306 void inp_unlock_assert(struct inpcb *);
307 #else
308 static __inline void
309 inp_lock_assert(struct inpcb *inp __unused)
310 {
311 }
312 
313 static __inline void
314 inp_unlock_assert(struct inpcb *inp __unused)
315 {
316 }
317 
318 #endif
319 #endif /* _KERNEL */
320 
321 
322 #define INP_INFO_LOCK_INIT(ipi, d) \
323 	rw_init_flags(&(ipi)->ipi_lock, (d), RW_RECURSE)
324 #define INP_INFO_LOCK_DESTROY(ipi)  rw_destroy(&(ipi)->ipi_lock)
325 #define INP_INFO_RLOCK(ipi)	rw_rlock(&(ipi)->ipi_lock)
326 #define INP_INFO_WLOCK(ipi)	rw_wlock(&(ipi)->ipi_lock)
327 #define INP_INFO_RUNLOCK(ipi)	rw_runlock(&(ipi)->ipi_lock)
328 #define INP_INFO_WUNLOCK(ipi)	rw_wunlock(&(ipi)->ipi_lock)
329 #define	INP_INFO_LOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_LOCKED)
330 #define INP_INFO_RLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_RLOCKED)
331 #define INP_INFO_WLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_WLOCKED)
332 #define INP_INFO_UNLOCK_ASSERT(ipi)	rw_assert(&(ipi)->ipi_lock, RA_UNLOCKED)
333 
334 #define INP_PCBHASH(faddr, lport, fport, mask) \
335 	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
336 #define INP_PCBPORTHASH(lport, mask) \
337 	(ntohs((lport)) & (mask))
338 
339 /* flags in inp_flags: */
340 #define	INP_RECVOPTS		0x01	/* receive incoming IP options */
341 #define	INP_RECVRETOPTS		0x02	/* receive IP options for reply */
342 #define	INP_RECVDSTADDR		0x04	/* receive IP dst address */
343 #define	INP_HDRINCL		0x08	/* user supplies entire IP header */
344 #define	INP_HIGHPORT		0x10	/* user wants "high" port binding */
345 #define	INP_LOWPORT		0x20	/* user wants "low" port binding */
346 #define	INP_ANONPORT		0x40	/* port chosen for user */
347 #define	INP_RECVIF		0x80	/* receive incoming interface */
348 #define	INP_MTUDISC		0x100	/* user can do MTU discovery */
349 #define	INP_FAITH		0x200	/* accept FAITH'ed connections */
350 #define	INP_RECVTTL		0x400	/* receive incoming IP TTL */
351 #define	INP_DONTFRAG		0x800	/* don't fragment packet */
352 
353 #define IN6P_IPV6_V6ONLY	0x008000 /* restrict AF_INET6 socket for v6 */
354 
355 #define	IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
356 #define	IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
357 #define	IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
358 #define	IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
359 #define	IN6P_RTHDR		0x100000 /* receive routing header */
360 #define	IN6P_RTHDRDSTOPTS	0x200000 /* receive dstoptions before rthdr */
361 #define	IN6P_TCLASS		0x400000 /* receive traffic class value */
362 #define	IN6P_AUTOFLOWLABEL	0x800000 /* attach flowlabel automatically */
363 #define	IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
364 #define	IN6P_MTU		0x80000000 /* receive path MTU */
365 
366 #define	INP_CONTROLOPTS		(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
367 				 INP_RECVIF|INP_RECVTTL|\
368 				 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
369 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
370 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
371 				 IN6P_MTU)
372 #define	INP_UNMAPPABLEOPTS	(IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
373 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL)
374 
375  /* for KAME src sync over BSD*'s */
376 #define	IN6P_HIGHPORT		INP_HIGHPORT
377 #define	IN6P_LOWPORT		INP_LOWPORT
378 #define	IN6P_ANONPORT		INP_ANONPORT
379 #define	IN6P_RECVIF		INP_RECVIF
380 #define	IN6P_MTUDISC		INP_MTUDISC
381 #define	IN6P_FAITH		INP_FAITH
382 #define	IN6P_CONTROLOPTS INP_CONTROLOPTS
383 	/*
384 	 * socket AF version is {newer than,or include}
385 	 * actual datagram AF version
386 	 */
387 
388 #define	INPLOOKUP_WILDCARD	1
389 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
390 #define	sotoin6pcb(so)	sotoinpcb(so) /* for KAME src sync over BSD*'s */
391 
392 #define	INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
393 
394 #define	INP_CHECK_SOCKAF(so, af)	(INP_SOCKAF(so) == af)
395 
396 #ifdef _KERNEL
397 extern int	ipport_reservedhigh;
398 extern int	ipport_reservedlow;
399 extern int	ipport_lowfirstauto;
400 extern int	ipport_lowlastauto;
401 extern int	ipport_firstauto;
402 extern int	ipport_lastauto;
403 extern int	ipport_hifirstauto;
404 extern int	ipport_hilastauto;
405 extern struct callout ipport_tick_callout;
406 
407 void	in_pcbpurgeif0(struct inpcbinfo *, struct ifnet *);
408 int	in_pcballoc(struct socket *, struct inpcbinfo *);
409 int	in_pcbbind(struct inpcb *, struct sockaddr *, struct ucred *);
410 int	in_pcbbind_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
411 	    u_short *, struct ucred *);
412 int	in_pcbconnect(struct inpcb *, struct sockaddr *, struct ucred *);
413 int	in_pcbconnect_setup(struct inpcb *, struct sockaddr *, in_addr_t *,
414 	    u_short *, in_addr_t *, u_short *, struct inpcb **,
415 	    struct ucred *);
416 void	in_pcbdetach(struct inpcb *);
417 void	in_pcbdisconnect(struct inpcb *);
418 void	in_pcbdrop(struct inpcb *);
419 void	in_pcbfree(struct inpcb *);
420 int	in_pcbinshash(struct inpcb *);
421 struct inpcb *
422 	in_pcblookup_local(struct inpcbinfo *,
423 	    struct in_addr, u_int, int);
424 struct inpcb *
425 	in_pcblookup_hash(struct inpcbinfo *, struct in_addr, u_int,
426 	    struct in_addr, u_int, int, struct ifnet *);
427 void	in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr,
428 	    int, struct inpcb *(*)(struct inpcb *, int));
429 void	in_pcbrehash(struct inpcb *);
430 void	in_pcbsetsolabel(struct socket *so);
431 int	in_getpeeraddr(struct socket *so, struct sockaddr **nam);
432 int	in_getsockaddr(struct socket *so, struct sockaddr **nam);
433 struct sockaddr *
434 	in_sockaddr(in_port_t port, struct in_addr *addr);
435 void	in_pcbsosetlabel(struct socket *so);
436 void	in_pcbremlists(struct inpcb *inp);
437 void	ipport_tick(void *xtp);
438 
439 /*
440  * Debugging routines compiled in when DDB is present.
441  */
442 void	db_print_inpcb(struct inpcb *inp, const char *name, int indent);
443 
444 #endif /* _KERNEL */
445 
446 #endif /* !_NETINET_IN_PCB_H_ */
447