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