xref: /openbsd-src/sys/netinet/in_pcb.h (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: in_pcb.h,v 1.38 2001/07/05 08:31:47 jjbg 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. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)in_pcb.h	8.1 (Berkeley) 6/10/93
66  */
67 
68 #ifndef _NETINET_IN_PCB_H_
69 #define _NETINET_IN_PCB_H_
70 
71 #include <sys/queue.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet/icmp6.h>
75 #include <netinet/ip_ipsp.h>
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 	CIRCLEQ_ENTRY(inpcb) inp_queue;
95 	struct	  inpcbtable *inp_table;
96 	union	  inpaddru inp_faddru;		/* Foreign address. */
97 	union	  inpaddru inp_laddru;		/* Local address. */
98 #define	inp_faddr	inp_faddru.iau_a4u.inaddr
99 #define	inp_faddr6	inp_faddru.iau_addr6
100 #define	inp_laddr	inp_laddru.iau_a4u.inaddr
101 #define	inp_laddr6	inp_laddru.iau_addr6
102 	u_int16_t inp_fport;		/* foreign port */
103 	u_int16_t inp_lport;		/* local port */
104 	struct	  socket *inp_socket;	/* back pointer to socket */
105 	caddr_t	  inp_ppcb;		/* pointer to per-protocol pcb */
106 	union {				/* Route (notice increased size). */
107 		struct route ru_route;
108 		struct route_in6 ru_route6;
109 	} inp_ru;
110 #define	inp_route	inp_ru.ru_route
111 #define	inp_route6	inp_ru.ru_route6
112 	int	  inp_flags;		/* generic IP/datagram flags */
113 	union {				/* Header prototype. */
114 		struct ip hu_ip;
115 		struct ip6_hdr hu_ipv6;
116 	} inp_hu;
117 #define	inp_ip		inp_hu.hu_ip
118 #define	inp_ipv6	inp_hu.hu_ipv6
119 	struct	  mbuf *inp_options;	/* IP options */
120 	struct ip6_pktopts *inp_outputopts6; /* IP6 options for outgoing packets */
121 	int inp_hops;
122 	union {
123 		struct ip_moptions *mou_mo;    /* IPv4 multicast options */
124 		struct ip6_moptions *mou_mo6; /* IPv6 multicast options */
125 	} inp_mou;
126 #define inp_moptions inp_mou.mou_mo
127 #define inp_moptions6 inp_mou.mou_mo6
128 	u_char	  inp_seclevel[4];
129 #define SL_AUTH           0             /* Authentication level */
130 #define SL_ESP_TRANS      1             /* ESP transport level */
131 #define SL_ESP_NETWORK    2             /* ESP network (encapsulation) level */
132 #define SL_IPCOMP         3             /* Compression level */
133 	u_int8_t  inp_secrequire:4,     /* Condensed State from above */
134 	          inp_secresult:4;	/* Result from Key Management */
135 #define SR_FAILED         1             /* Negotiation failed permanently */
136 #define SR_SUCCESS        2             /* SA successfully established */
137 #define SR_WAIT           3             /* Waiting for SA */
138 	TAILQ_ENTRY(inpcb) inp_tdb_in_next, inp_tdb_out_next;
139 	struct tdb     *inp_tdb_in, *inp_tdb_out;
140 	struct ipsec_ref *inp_ipsec_localid;
141 	struct ipsec_ref *inp_ipsec_remoteid;
142 	struct ipsec_ref *inp_ipsec_localcred;
143 	struct ipsec_ref *inp_ipsec_remotecred;
144 	struct ipsec_ref *inp_ipsec_localauth;
145 	struct ipsec_ref *inp_ipsec_remoteauth;
146 #define	inp_flowinfo	inp_hu.hu_ipv6.ip6_flow
147 
148 	int	in6p_cksum;
149 #ifndef _KERNEL
150 #define inp_csumoffset	in6p_cksum
151 #endif
152 	struct	icmp6_filter *inp_icmp6filt;
153 };
154 
155 struct inpcbtable {
156 	CIRCLEQ_HEAD(, inpcb) inpt_queue;
157 	LIST_HEAD(inpcbhead, inpcb) *inpt_hashtbl;
158 	u_long	  inpt_hash;
159 	u_int16_t inpt_lastport;
160 };
161 
162 /* flags in inp_flags: */
163 #define	INP_RECVOPTS	0x001	/* receive incoming IP options */
164 #define	INP_RECVRETOPTS	0x002	/* receive IP options for reply */
165 #define	INP_RECVDSTADDR	0x004	/* receive IP dst address */
166 
167 #define	INP_RXDSTOPTS	INP_RECVOPTS
168 #define	INP_RXHOPOPTS	INP_RECVRETOPTS
169 #define	INP_RXINFO	INP_RECVDSTADDR
170 #define	INP_RXSRCRT	0x010
171 #define	INP_HOPLIMIT	0x020
172 
173 #define	INP_CONTROLOPTS	(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR| \
174 	    INP_RXSRCRT|INP_HOPLIMIT)
175 
176 #define	INP_HDRINCL	0x008	/* user supplies entire IP header */
177 #define	INP_HIGHPORT	0x010	/* user wants "high" port binding */
178 #define	INP_LOWPORT	0x020	/* user wants "low" port binding */
179 
180 /*
181  * These flags' values should be determined by either the transport
182  * protocol at PRU_BIND, PRU_LISTEN, PRU_CONNECT, etc, or by in_pcb*().
183  */
184 #define	INP_IPV6	0x100	/* sotopf(inp->inp_socket) == PF_INET6 */
185 
186 #if 1	/*KAME*/
187 /*
188  * Flags in in6p_flags
189  * We define KAME's original flags in higher 16 bits as much as possible
190  * for compatibility with *bsd*s.
191  * XXX: Should IN6P_HIGHPORT and IN6P_LOWPORT be moved as well?
192  */
193 #define IN6P_RECVOPTS		INP_RECVOPTS	/* recv incoming IP6 options */
194 #define IN6P_RECVRETOPTS	INP_RECVRETOPTS /* recv IP6 options for reply */
195 #define IN6P_RECVDSTADDR	INP_RECVDSTADDR /* recv IP6 dst address */
196 #define IN6P_HIGHPORT		INP_HIGHPORT	/* user wants "high" port */
197 #define IN6P_LOWPORT		INP_LOWPORT	/* user wants "low" port */
198 #define IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
199 #define IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
200 #define IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
201 #define IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
202 #define IN6P_RTHDR		0x100000 /* receive routing header */
203 #define IN6P_RTHDRDSTOPTS	0x200000 /* receive dstoptions before rthdr */
204 
205 #define IN6P_ANONPORT		0x4000000 /* port chosen for user */
206 #define IN6P_FAITH		0x8000000 /* accept FAITH'ed connections */
207 
208 #define IN6P_CONTROLOPTS	(IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
209 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS)
210 #endif
211 
212 #define	INPLOOKUP_WILDCARD	1
213 #define	INPLOOKUP_SETLOCAL	2
214 #define	INPLOOKUP_IPV6		4
215 
216 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
217 
218 /* macros for handling bitmap of ports not to allocate dynamically */
219 #define	DP_MAPBITS	(sizeof(u_int32_t) * NBBY)
220 #define	DP_MAPSIZE	(howmany(IPPORT_RESERVED/2, DP_MAPBITS))
221 #define	DP_SET(m, p)	((m)[((p) - IPPORT_RESERVED/2) / DP_MAPBITS] |= (1 << ((p) % DP_MAPBITS)))
222 #define	DP_CLR(m, p)	((m)[((p) - IPPORT_RESERVED/2) / DP_MAPBITS] &= ~(1 << ((p) % DP_MAPBITS)))
223 #define	DP_ISSET(m, p)	((m)[((p) - IPPORT_RESERVED/2) / DP_MAPBITS] & (1 << ((p) % DP_MAPBITS)))
224 
225 /* default values for baddynamicports [see ip_init()] */
226 #define	DEFBADDYNAMICPORTS_TCP	{ 587, 749, 750, 751, 760, 761, 871, 0 }
227 #define	DEFBADDYNAMICPORTS_UDP	{ 750, 751, 0 }
228 
229 struct baddynamicports {
230 	u_int32_t tcp[DP_MAPSIZE];
231 	u_int32_t udp[DP_MAPSIZE];
232 };
233 
234 #ifdef _KERNEL
235 
236 #define sotopf(so)  (so->so_proto->pr_domain->dom_family)
237 
238 void	 in_losing __P((struct inpcb *));
239 int	 in_pcballoc __P((struct socket *, void *));
240 int	 in_pcbbind __P((void *, struct mbuf *));
241 int	 in_pcbconnect __P((void *, struct mbuf *));
242 void	 in_pcbdetach __P((void *));
243 void	 in_pcbdisconnect __P((void *));
244 struct inpcb *
245 	 in_pcbhashlookup __P((struct inpcbtable *, struct in_addr,
246 			       u_int, struct in_addr, u_int));
247 #ifdef INET6
248 struct inpcb *
249 	 in6_pcbhashlookup __P((struct inpcbtable *, struct in6_addr *,
250 			       u_int, struct in6_addr *, u_int));
251 int	 in6_pcbbind __P((struct inpcb *, struct mbuf *));
252 int	 in6_pcbconnect __P((struct inpcb *, struct mbuf *));
253 int	 in6_setsockaddr __P((struct inpcb *, struct mbuf *));
254 int	 in6_setpeeraddr __P((struct inpcb *, struct mbuf *));
255 #endif /* INET6 */
256 void	 in_pcbinit __P((struct inpcbtable *, int));
257 struct inpcb *
258 	 in_pcblookup __P((struct inpcbtable *, void *, u_int, void *,
259 	    u_int, int));
260 void	 in_pcbnotify __P((struct inpcbtable *, struct sockaddr *,
261 	    u_int, struct in_addr, u_int, int, void (*)(struct inpcb *, int)));
262 void	 in_pcbnotifyall __P((struct inpcbtable *, struct sockaddr *,
263 	    int, void (*)(struct inpcb *, int)));
264 void	 in_pcbrehash __P((struct inpcb *));
265 void	 in_rtchange __P((struct inpcb *, int));
266 void	 in_setpeeraddr __P((struct inpcb *, struct mbuf *));
267 void	 in_setsockaddr __P((struct inpcb *, struct mbuf *));
268 int	 in_baddynamic __P((u_int16_t, u_int16_t));
269 extern struct sockaddr_in *in_selectsrc __P((struct sockaddr_in *,
270 	struct route *, int, struct ip_moptions *, int *));
271 struct rtentry *
272 	in_pcbrtentry __P((struct inpcb *));
273 
274 /* INET6 stuff */
275 int	in6_pcbnotify __P((struct inpcbtable *, struct sockaddr *,
276 			   u_int, struct sockaddr *, u_int, int, void *,
277 			   void (*)(struct inpcb *, int)));
278 struct 	in6_addr *in6_selectsrc __P((struct sockaddr_in6 *,
279 				     struct ip6_pktopts *,
280 				     struct ip6_moptions *,
281 				     struct route_in6 *,
282 				     struct in6_addr *, int *));
283 int	in6_selecthlim __P((struct inpcb *, struct ifnet *));
284 int	in6_pcbsetport __P((struct in6_addr *, struct inpcb *));
285 #endif /* _KERNEL */
286 #endif /* _NETINET_IN_PCB_H_ */
287