xref: /openbsd-src/sys/netinet/in_pcb.h (revision 8550894424f8a4aa4aafb6cd57229dd6ed7cd9dd)
1 /*	$OpenBSD: in_pcb.h,v 1.135 2022/10/03 16:43:52 bluhm Exp $	*/
2 /*	$NetBSD: in_pcb.h,v 1.14 1996/02/13 23:42:00 christos Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1990, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
62  */
63 
64 #ifndef _NETINET_IN_PCB_H_
65 #define _NETINET_IN_PCB_H_
66 
67 #include <sys/queue.h>
68 #include <sys/mutex.h>
69 #include <sys/rwlock.h>
70 #include <sys/refcnt.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet/icmp6.h>
74 #include <netinet/ip_ipsp.h>
75 
76 #include <crypto/siphash.h>
77 
78 /*
79  * Locks used to protect struct members in this file:
80  *	I	immutable after creation
81  *	N	net lock
82  *	t	inpt_mtx		pcb table mutex
83  *	y	inpt_notify		pcb table rwlock for notify
84  *	p	inpcb_mtx		pcb mutex
85  */
86 
87 struct pf_state_key;
88 
89 union inpaddru {
90 	struct in6_addr iau_addr6;
91 	struct {
92 		uint8_t pad[12];
93 		struct in_addr inaddr;	/* easier transition */
94 	} iau_a4u;
95 };
96 
97 /*
98  * Common structure pcb for internet protocol implementation.
99  * Here are stored pointers to local and foreign host table
100  * entries, local and foreign socket numbers, and pointers
101  * up (to a socket structure) and down (to a protocol-specific)
102  * control block.
103  */
104 struct inpcb {
105 	LIST_ENTRY(inpcb) inp_hash;		/* [t] local and foreign hash */
106 	LIST_ENTRY(inpcb) inp_lhash;		/* [t] local port hash */
107 	TAILQ_ENTRY(inpcb) inp_queue;		/* [t] inet PCB queue */
108 	SIMPLEQ_ENTRY(inpcb) inp_notify;	/* [y] notify or udp append */
109 	struct	  inpcbtable *inp_table;	/* [I] inet queue/hash table */
110 	union	  inpaddru inp_faddru;		/* Foreign address. */
111 	union	  inpaddru inp_laddru;		/* Local address. */
112 #define	inp_faddr	inp_faddru.iau_a4u.inaddr
113 #define	inp_faddr6	inp_faddru.iau_addr6
114 #define	inp_laddr	inp_laddru.iau_a4u.inaddr
115 #define	inp_laddr6	inp_laddru.iau_addr6
116 	u_int16_t inp_fport;		/* foreign port */
117 	u_int16_t inp_lport;		/* local port */
118 	struct	  socket *inp_socket;	/* back pointer to socket */
119 	caddr_t	  inp_ppcb;		/* pointer to per-protocol pcb */
120 	union {				/* Route (notice increased size). */
121 		struct route ru_route;
122 		struct route_in6 ru_route6;
123 	} inp_ru;
124 #define	inp_route	inp_ru.ru_route
125 #define	inp_route6	inp_ru.ru_route6
126 	struct    refcnt inp_refcnt;	/* refcount PCB, delay memory free */
127 	struct	  mutex inp_mtx;	/* protect PCB and socket members */
128 	int	  inp_flags;		/* generic IP/datagram flags */
129 	union {				/* Header prototype. */
130 		struct ip hu_ip;
131 		struct ip6_hdr hu_ipv6;
132 	} inp_hu;
133 #define	inp_ip		inp_hu.hu_ip
134 #define	inp_ipv6	inp_hu.hu_ipv6
135 	struct	  mbuf *inp_options;	/* IP options */
136 	struct ip6_pktopts *inp_outputopts6; /* IP6 options for outgoing packets */
137 	int inp_hops;
138 	union {
139 		struct ip_moptions *mou_mo;    /* IPv4 multicast options */
140 		struct ip6_moptions *mou_mo6; /* IPv6 multicast options */
141 	} inp_mou;
142 #define inp_moptions inp_mou.mou_mo
143 #define inp_moptions6 inp_mou.mou_mo6
144 	u_char	  inp_seclevel[4];
145 #define SL_AUTH           0             /* Authentication level */
146 #define SL_ESP_TRANS      1             /* ESP transport level */
147 #define SL_ESP_NETWORK    2             /* ESP network (encapsulation) level */
148 #define SL_IPCOMP         3             /* Compression level */
149 	u_char	inp_ip_minttl;		/* minimum TTL or drop */
150 #define inp_ip6_minhlim inp_ip_minttl	/* minimum Hop Limit or drop */
151 #define	inp_flowinfo	inp_hu.hu_ipv6.ip6_flow
152 
153 	int	inp_cksum6;
154 #ifndef _KERNEL
155 #define inp_csumoffset	inp_cksum6
156 #endif
157 	struct	icmp6_filter *inp_icmp6filt;
158 	struct	pf_state_key *inp_pf_sk;
159 	struct	mbuf *(*inp_upcall)(void *, struct mbuf *,
160 		    struct ip *, struct ip6_hdr *, void *, int);
161 	void	*inp_upcall_arg;
162 	u_int	inp_rtableid;
163 	int	inp_pipex;		/* pipex indication */
164 	uint16_t inp_flowid;
165 };
166 
167 LIST_HEAD(inpcbhead, inpcb);
168 
169 struct inpcbtable {
170 	struct mutex inpt_mtx;			/* protect queue and hash */
171 	struct rwlock inpt_notify;		/* protect inp_notify list */
172 	TAILQ_HEAD(inpthead, inpcb) inpt_queue;	/* [t] inet PCB queue */
173 	struct	inpcbhead *inpt_hashtbl;	/* [t] local and foreign hash */
174 	struct	inpcbhead *inpt_lhashtbl;	/* [t] local port hash */
175 	SIPHASH_KEY inpt_key, inpt_lkey;	/* [t] secrets for hashes */
176 	u_long	inpt_mask, inpt_lmask;		/* [t] hash masks */
177 	int	inpt_count, inpt_size;		/* [t] queue count, hash size */
178 };
179 
180 /* flags in inp_flags: */
181 #define	INP_RECVOPTS	0x001	/* receive incoming IP options */
182 #define	INP_RECVRETOPTS	0x002	/* receive IP options for reply */
183 #define	INP_RECVDSTADDR	0x004	/* receive IP dst address */
184 
185 #define	INP_RXDSTOPTS	INP_RECVOPTS
186 #define	INP_RXHOPOPTS	INP_RECVRETOPTS
187 #define	INP_RXINFO	INP_RECVDSTADDR
188 #define	INP_RXSRCRT	0x010
189 #define	INP_HOPLIMIT	0x020
190 
191 #define	INP_HDRINCL	0x008	/* user supplies entire IP header */
192 #define	INP_HIGHPORT	0x010	/* user wants "high" port binding */
193 #define	INP_LOWPORT	0x020	/* user wants "low" port binding */
194 #define	INP_RECVIF	0x080	/* receive incoming interface */
195 #define	INP_RECVTTL	0x040	/* receive incoming IP TTL */
196 #define	INP_RECVDSTPORT	0x200	/* receive IP dst addr before rdr */
197 #define	INP_RECVRTABLE	0x400	/* receive routing table */
198 #define	INP_IPSECFLOWINFO 0x800	/* receive IPsec flow info */
199 
200 #define	INP_CONTROLOPTS	(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR| \
201 	    INP_RXSRCRT|INP_HOPLIMIT|INP_RECVIF|INP_RECVTTL|INP_RECVDSTPORT| \
202 	    INP_RECVRTABLE)
203 
204 /*
205  * These flags' values should be determined by either the transport
206  * protocol at PRU_BIND, PRU_LISTEN, PRU_CONNECT, etc, or by in_pcb*().
207  */
208 #define	INP_IPV6	0x100	/* sotopf(inp->inp_socket) == PF_INET6 */
209 
210 /*
211  * Flags in inp_flags for IPV6
212  */
213 #define IN6P_HIGHPORT		INP_HIGHPORT	/* user wants "high" port */
214 #define IN6P_LOWPORT		INP_LOWPORT	/* user wants "low" port */
215 #define IN6P_RECVDSTPORT	INP_RECVDSTPORT	/* receive IP dst addr before rdr */
216 #define IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
217 #define IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
218 #define IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
219 #define IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
220 #define IN6P_RTHDR		0x100000 /* receive routing header */
221 #define IN6P_TCLASS		0x400000 /* receive traffic class value */
222 #define IN6P_AUTOFLOWLABEL	0x800000 /* attach flowlabel automatically */
223 
224 #define IN6P_ANONPORT		0x4000000 /* port chosen for user */
225 #define IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
226 #define IN6P_MTU		0x80000000 /* receive path MTU */
227 
228 #define IN6P_MINMTU		0x20000000 /* use minimum MTU */
229 
230 #define IN6P_CONTROLOPTS	(IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
231 				 IN6P_DSTOPTS|IN6P_RTHDR|\
232 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
233 				 IN6P_MTU|IN6P_RECVDSTPORT)
234 
235 #define	INPLOOKUP_WILDCARD	1
236 #define	INPLOOKUP_SETLOCAL	2
237 #define	INPLOOKUP_IPV6		4
238 
239 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
240 
241 /* macros for handling bitmap of ports not to allocate dynamically */
242 #define	DP_MAPBITS	(sizeof(u_int32_t) * NBBY)
243 #define	DP_MAPSIZE	(howmany(65536, DP_MAPBITS))
244 #define	DP_SET(m, p)	((m)[(p) / DP_MAPBITS] |= (1U << ((p) % DP_MAPBITS)))
245 #define	DP_CLR(m, p)	((m)[(p) / DP_MAPBITS] &= ~(1U << ((p) % DP_MAPBITS)))
246 #define	DP_ISSET(m, p)	((m)[(p) / DP_MAPBITS] & (1U << ((p) % DP_MAPBITS)))
247 
248 /* default values for baddynamicports [see ip_init()] */
249 #define	DEFBADDYNAMICPORTS_TCP	{ \
250 	587, 749, 750, 751, 853, 871, 2049, \
251 	6000, 6001, 6002, 6003, 6004, 6005, 6006, 6007, 6008, 6009, 6010, \
252 	0 }
253 #define	DEFBADDYNAMICPORTS_UDP	{ 623, 664, 749, 750, 751, 2049, \
254 	3784, 3785, 7784, /* BFD/S-BFD ports */ \
255 	 0 }
256 
257 #define DEFROOTONLYPORTS_TCP { \
258 	2049, \
259 	0 }
260 #define DEFROOTONLYPORTS_UDP { \
261 	2049, \
262 	0 }
263 
264 struct baddynamicports {
265 	u_int32_t tcp[DP_MAPSIZE];
266 	u_int32_t udp[DP_MAPSIZE];
267 };
268 
269 #ifdef _KERNEL
270 
271 extern struct inpcbtable rawcbtable, rawin6pcbtable;
272 extern struct baddynamicports baddynamicports;
273 extern struct baddynamicports rootonlyports;
274 extern int in_pcbnotifymiss;
275 
276 #define sotopf(so)  (so->so_proto->pr_domain->dom_family)
277 
278 void	 in_init(void);
279 void	 in_losing(struct inpcb *);
280 int	 in_pcballoc(struct socket *, struct inpcbtable *, int);
281 int	 in_pcbbind(struct inpcb *, struct mbuf *, struct proc *);
282 int	 in_pcbaddrisavail(struct inpcb *, struct sockaddr_in *, int,
283 	    struct proc *);
284 int	 in_pcbconnect(struct inpcb *, struct mbuf *);
285 void	 in_pcbdetach(struct inpcb *);
286 struct inpcb *
287 	 in_pcbref(struct inpcb *);
288 void	 in_pcbunref(struct inpcb *);
289 void	 in_pcbdisconnect(struct inpcb *);
290 struct inpcb *
291 	 in_pcblookup(struct inpcbtable *, struct in_addr,
292 			       u_int, struct in_addr, u_int, u_int);
293 struct inpcb *
294 	 in_pcblookup_listen(struct inpcbtable *, struct in_addr, u_int,
295 	    struct mbuf *, u_int);
296 #ifdef INET6
297 struct inpcbhead *
298 	 in6_pcbhash(struct inpcbtable *, u_int, const struct in6_addr *,
299 	    u_short, const struct in6_addr *, u_short);
300 struct inpcb *
301 	 in6_pcblookup(struct inpcbtable *, const struct in6_addr *,
302 			       u_int, const struct in6_addr *, u_int, u_int);
303 struct inpcb *
304 	 in6_pcblookup_listen(struct inpcbtable *, struct in6_addr *, u_int,
305 	    struct mbuf *, u_int);
306 int	 in6_pcbaddrisavail(struct inpcb *, struct sockaddr_in6 *, int,
307 	    struct proc *);
308 int	 in6_pcbconnect(struct inpcb *, struct mbuf *);
309 void	 in6_setsockaddr(struct inpcb *, struct mbuf *);
310 void	 in6_setpeeraddr(struct inpcb *, struct mbuf *);
311 int	 in6_sockaddr(struct socket *, struct mbuf *);
312 int	 in6_peeraddr(struct socket *, struct mbuf *);
313 #endif /* INET6 */
314 void	 in_pcbinit(struct inpcbtable *, int);
315 struct inpcb *
316 	 in_pcblookup_local(struct inpcbtable *, void *, u_int, int, u_int);
317 void	 in_pcbnotifyall(struct inpcbtable *, struct sockaddr *,
318 	    u_int, int, void (*)(struct inpcb *, int));
319 void	 in_pcbrehash(struct inpcb *);
320 void	 in_rtchange(struct inpcb *, int);
321 void	 in_setpeeraddr(struct inpcb *, struct mbuf *);
322 void	 in_setsockaddr(struct inpcb *, struct mbuf *);
323 int	 in_sockaddr(struct socket *, struct mbuf *);
324 int	 in_peeraddr(struct socket *, struct mbuf *);
325 int	 in_baddynamic(u_int16_t, u_int16_t);
326 int	 in_rootonly(u_int16_t, u_int16_t);
327 int	 in_pcbselsrc(struct in_addr *, struct sockaddr_in *, struct inpcb *);
328 struct rtentry *
329 	in_pcbrtentry(struct inpcb *);
330 
331 /* INET6 stuff */
332 void	in6_pcbnotify(struct inpcbtable *, struct sockaddr_in6 *,
333 	u_int, const struct sockaddr_in6 *, u_int, u_int, int, void *,
334 	void (*)(struct inpcb *, int));
335 int	in6_selecthlim(struct inpcb *);
336 int	in_pcbpickport(u_int16_t *, void *, int, struct inpcb *, struct proc *);
337 #endif /* _KERNEL */
338 #endif /* _NETINET_IN_PCB_H_ */
339