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