xref: /openbsd-src/sys/netinet/in_pcb.h (revision c1a45aed656e7d5627c30c92421893a76f370ccb)
1 /*	$OpenBSD: in_pcb.h,v 1.128 2022/03/21 23:37:09 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/refcnt.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet/icmp6.h>
73 #include <netinet/ip_ipsp.h>
74 
75 #include <crypto/siphash.h>
76 
77 /*
78  * Locks used to protect struct members in this file:
79  *	I	immutable after creation
80  *	N	net lock
81  *	t	inpt_mtx		pcb table mutex
82  */
83 
84 struct pf_state_key;
85 
86 union inpaddru {
87 	struct in6_addr iau_addr6;
88 	struct {
89 		uint8_t pad[12];
90 		struct in_addr inaddr;	/* easier transition */
91 	} iau_a4u;
92 };
93 
94 /*
95  * Common structure pcb for internet protocol implementation.
96  * Here are stored pointers to local and foreign host table
97  * entries, local and foreign socket numbers, and pointers
98  * up (to a socket structure) and down (to a protocol-specific)
99  * control block.
100  */
101 struct inpcb {
102 	LIST_ENTRY(inpcb) inp_hash;		/* [t] local and foreign hash */
103 	LIST_ENTRY(inpcb) inp_lhash;		/* [t] local port hash */
104 	TAILQ_ENTRY(inpcb) inp_queue;		/* [t] inet PCB queue */
105 	SIMPLEQ_ENTRY(inpcb) inp_notify;	/* [N] notify or udp append */
106 	struct	  inpcbtable *inp_table;	/* [I] inet queue/hash table */
107 	union	  inpaddru inp_faddru;		/* Foreign address. */
108 	union	  inpaddru inp_laddru;		/* Local address. */
109 #define	inp_faddr	inp_faddru.iau_a4u.inaddr
110 #define	inp_faddr6	inp_faddru.iau_addr6
111 #define	inp_laddr	inp_laddru.iau_a4u.inaddr
112 #define	inp_laddr6	inp_laddru.iau_addr6
113 	u_int16_t inp_fport;		/* foreign port */
114 	u_int16_t inp_lport;		/* local port */
115 	struct	  socket *inp_socket;	/* back pointer to socket */
116 	caddr_t	  inp_ppcb;		/* pointer to per-protocol pcb */
117 	union {				/* Route (notice increased size). */
118 		struct route ru_route;
119 		struct route_in6 ru_route6;
120 	} inp_ru;
121 #define	inp_route	inp_ru.ru_route
122 #define	inp_route6	inp_ru.ru_route6
123 	struct    refcnt inp_refcnt;	/* refcount PCB, delay memory free */
124 	int	  inp_flags;		/* generic IP/datagram flags */
125 	union {				/* Header prototype. */
126 		struct ip hu_ip;
127 		struct ip6_hdr hu_ipv6;
128 	} inp_hu;
129 #define	inp_ip		inp_hu.hu_ip
130 #define	inp_ipv6	inp_hu.hu_ipv6
131 	struct	  mbuf *inp_options;	/* IP options */
132 	struct ip6_pktopts *inp_outputopts6; /* IP6 options for outgoing packets */
133 	int inp_hops;
134 	union {
135 		struct ip_moptions *mou_mo;    /* IPv4 multicast options */
136 		struct ip6_moptions *mou_mo6; /* IPv6 multicast options */
137 	} inp_mou;
138 #define inp_moptions inp_mou.mou_mo
139 #define inp_moptions6 inp_mou.mou_mo6
140 	u_char	  inp_seclevel[4];
141 #define SL_AUTH           0             /* Authentication level */
142 #define SL_ESP_TRANS      1             /* ESP transport level */
143 #define SL_ESP_NETWORK    2             /* ESP network (encapsulation) level */
144 #define SL_IPCOMP         3             /* Compression level */
145 	u_char	inp_ip_minttl;		/* minimum TTL or drop */
146 #define inp_ip6_minhlim inp_ip_minttl	/* minimum Hop Limit or drop */
147 #define	inp_flowinfo	inp_hu.hu_ipv6.ip6_flow
148 
149 	int	inp_cksum6;
150 #ifndef _KERNEL
151 #define inp_csumoffset	inp_cksum6
152 #endif
153 	struct	icmp6_filter *inp_icmp6filt;
154 	struct	pf_state_key *inp_pf_sk;
155 	struct	mbuf *(*inp_upcall)(void *, struct mbuf *,
156 		    struct ip *, struct ip6_hdr *, void *, int);
157 	void	*inp_upcall_arg;
158 	u_int	inp_rtableid;
159 	int	inp_pipex;		/* pipex indication */
160 	uint16_t inp_flowid;
161 };
162 
163 LIST_HEAD(inpcbhead, inpcb);
164 
165 struct inpcbtable {
166 	struct mutex inpt_mtx;			/* protect queue and hash */
167 	TAILQ_HEAD(inpthead, inpcb) inpt_queue;	/* [t] inet PCB queue */
168 	struct	inpcbhead *inpt_hashtbl;	/* [t] local and foreign hash */
169 	struct	inpcbhead *inpt_lhashtbl;	/* [t] local port hash */
170 	SIPHASH_KEY inpt_key, inpt_lkey;	/* [t] secrets for hashes */
171 	u_long	inpt_mask, inpt_lmask;		/* [t] hash masks */
172 	int	inpt_count, inpt_size;		/* [t] queue count, hash size */
173 };
174 
175 /* flags in inp_flags: */
176 #define	INP_RECVOPTS	0x001	/* receive incoming IP options */
177 #define	INP_RECVRETOPTS	0x002	/* receive IP options for reply */
178 #define	INP_RECVDSTADDR	0x004	/* receive IP dst address */
179 
180 #define	INP_RXDSTOPTS	INP_RECVOPTS
181 #define	INP_RXHOPOPTS	INP_RECVRETOPTS
182 #define	INP_RXINFO	INP_RECVDSTADDR
183 #define	INP_RXSRCRT	0x010
184 #define	INP_HOPLIMIT	0x020
185 
186 #define	INP_HDRINCL	0x008	/* user supplies entire IP header */
187 #define	INP_HIGHPORT	0x010	/* user wants "high" port binding */
188 #define	INP_LOWPORT	0x020	/* user wants "low" port binding */
189 #define	INP_RECVIF	0x080	/* receive incoming interface */
190 #define	INP_RECVTTL	0x040	/* receive incoming IP TTL */
191 #define	INP_RECVDSTPORT	0x200	/* receive IP dst addr before rdr */
192 #define	INP_RECVRTABLE	0x400	/* receive routing table */
193 #define	INP_IPSECFLOWINFO 0x800	/* receive IPsec flow info */
194 
195 #define	INP_CONTROLOPTS	(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR| \
196 	    INP_RXSRCRT|INP_HOPLIMIT|INP_RECVIF|INP_RECVTTL|INP_RECVDSTPORT| \
197 	    INP_RECVRTABLE)
198 
199 /*
200  * These flags' values should be determined by either the transport
201  * protocol at PRU_BIND, PRU_LISTEN, PRU_CONNECT, etc, or by in_pcb*().
202  */
203 #define	INP_IPV6	0x100	/* sotopf(inp->inp_socket) == PF_INET6 */
204 
205 /*
206  * Flags in inp_flags for IPV6
207  */
208 #define IN6P_HIGHPORT		INP_HIGHPORT	/* user wants "high" port */
209 #define IN6P_LOWPORT		INP_LOWPORT	/* user wants "low" port */
210 #define IN6P_RECVDSTPORT	INP_RECVDSTPORT	/* receive IP dst addr before rdr */
211 #define IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
212 #define IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
213 #define IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
214 #define IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
215 #define IN6P_RTHDR		0x100000 /* receive routing header */
216 #define IN6P_TCLASS		0x400000 /* receive traffic class value */
217 #define IN6P_AUTOFLOWLABEL	0x800000 /* attach flowlabel automatically */
218 
219 #define IN6P_ANONPORT		0x4000000 /* port chosen for user */
220 #define IN6P_RFC2292		0x40000000 /* used RFC2292 API on the socket */
221 #define IN6P_MTU		0x80000000 /* receive path MTU */
222 
223 #define IN6P_MINMTU		0x20000000 /* use minimum MTU */
224 
225 #define IN6P_CONTROLOPTS	(IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
226 				 IN6P_DSTOPTS|IN6P_RTHDR|\
227 				 IN6P_TCLASS|IN6P_AUTOFLOWLABEL|IN6P_RFC2292|\
228 				 IN6P_MTU|IN6P_RECVDSTPORT)
229 
230 #define	INPLOOKUP_WILDCARD	1
231 #define	INPLOOKUP_SETLOCAL	2
232 #define	INPLOOKUP_IPV6		4
233 
234 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
235 
236 /* macros for handling bitmap of ports not to allocate dynamically */
237 #define	DP_MAPBITS	(sizeof(u_int32_t) * NBBY)
238 #define	DP_MAPSIZE	(howmany(65536, DP_MAPBITS))
239 #define	DP_SET(m, p)	((m)[(p) / DP_MAPBITS] |= (1U << ((p) % DP_MAPBITS)))
240 #define	DP_CLR(m, p)	((m)[(p) / DP_MAPBITS] &= ~(1U << ((p) % DP_MAPBITS)))
241 #define	DP_ISSET(m, p)	((m)[(p) / DP_MAPBITS] & (1U << ((p) % DP_MAPBITS)))
242 
243 /* default values for baddynamicports [see ip_init()] */
244 #define	DEFBADDYNAMICPORTS_TCP	{ \
245 	587, 749, 750, 751, 853, 871, 2049, \
246 	6000, 6001, 6002, 6003, 6004, 6005, 6006, 6007, 6008, 6009, 6010, \
247 	0 }
248 #define	DEFBADDYNAMICPORTS_UDP	{ 623, 664, 749, 750, 751, 2049, \
249 	3784, 3785, 7784, /* BFD/S-BFD ports */ \
250 	 0 }
251 
252 #define DEFROOTONLYPORTS_TCP { \
253 	2049, \
254 	0 }
255 #define DEFROOTONLYPORTS_UDP { \
256 	2049, \
257 	0 }
258 
259 struct baddynamicports {
260 	u_int32_t tcp[DP_MAPSIZE];
261 	u_int32_t udp[DP_MAPSIZE];
262 };
263 
264 #ifdef _KERNEL
265 
266 extern struct inpcbtable rawcbtable, rawin6pcbtable;
267 extern struct baddynamicports baddynamicports;
268 extern struct baddynamicports rootonlyports;
269 extern int in_pcbnotifymiss;
270 
271 #define sotopf(so)  (so->so_proto->pr_domain->dom_family)
272 
273 void	 in_init(void);
274 void	 in_losing(struct inpcb *);
275 int	 in_pcballoc(struct socket *, struct inpcbtable *);
276 int	 in_pcbbind(struct inpcb *, struct mbuf *, struct proc *);
277 int	 in_pcbaddrisavail(struct inpcb *, struct sockaddr_in *, int,
278 	    struct proc *);
279 int	 in_pcbconnect(struct inpcb *, struct mbuf *);
280 void	 in_pcbdetach(struct inpcb *);
281 struct inpcb *
282 	 in_pcbref(struct inpcb *);
283 void	 in_pcbunref(struct inpcb *);
284 void	 in_pcbdisconnect(struct inpcb *);
285 struct inpcb *
286 	 in_pcbhashlookup(struct inpcbtable *, struct in_addr,
287 			       u_int, struct in_addr, u_int, u_int);
288 struct inpcb *
289 	 in_pcblookup_listen(struct inpcbtable *, struct in_addr, u_int,
290 	    struct mbuf *, u_int);
291 #ifdef INET6
292 struct inpcbhead *
293 	 in6_pcbhash(struct inpcbtable *, int, const struct in6_addr *,
294 	    u_short, const struct in6_addr *, u_short);
295 struct inpcb *
296 	 in6_pcbhashlookup(struct inpcbtable *, const struct in6_addr *,
297 			       u_int, const struct in6_addr *, u_int, u_int);
298 struct inpcb *
299 	 in6_pcblookup_listen(struct inpcbtable *, struct in6_addr *, u_int,
300 	    struct mbuf *, u_int);
301 int	 in6_pcbaddrisavail(struct inpcb *, struct sockaddr_in6 *, int,
302 	    struct proc *);
303 int	 in6_pcbconnect(struct inpcb *, struct mbuf *);
304 void	 in6_setsockaddr(struct inpcb *, struct mbuf *);
305 void	 in6_setpeeraddr(struct inpcb *, struct mbuf *);
306 #endif /* INET6 */
307 void	 in_pcbinit(struct inpcbtable *, int);
308 struct inpcb *
309 	 in_pcblookup_local(struct inpcbtable *, void *, u_int, int, u_int);
310 void	 in_pcbnotifyall(struct inpcbtable *, struct sockaddr *,
311 	    u_int, int, void (*)(struct inpcb *, int));
312 void	 in_pcbrehash(struct inpcb *);
313 void	 in_rtchange(struct inpcb *, int);
314 void	 in_setpeeraddr(struct inpcb *, struct mbuf *);
315 void	 in_setsockaddr(struct inpcb *, struct mbuf *);
316 int	 in_baddynamic(u_int16_t, u_int16_t);
317 int	 in_rootonly(u_int16_t, u_int16_t);
318 int	 in_pcbselsrc(struct in_addr **, struct sockaddr_in *, struct inpcb *);
319 struct rtentry *
320 	in_pcbrtentry(struct inpcb *);
321 
322 /* INET6 stuff */
323 void	in6_pcbnotify(struct inpcbtable *, struct sockaddr_in6 *,
324 	u_int, const struct sockaddr_in6 *, u_int, u_int, int, void *,
325 	void (*)(struct inpcb *, int));
326 int	in6_selecthlim(struct inpcb *);
327 int	in_pcbpickport(u_int16_t *, void *, int, struct inpcb *, struct proc *);
328 #endif /* _KERNEL */
329 #endif /* _NETINET_IN_PCB_H_ */
330