1 /*	$OpenBSD: in_pcb.h,v 1.49 2003/12/21 15:12:27 markus 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 union inpaddru {
74 	struct in6_addr iau_addr6;
75 	struct {
76 		uint8_t pad[12];
77 		struct in_addr inaddr;	/* easier transition */
78 	} iau_a4u;
79 };
80 
81 /*
82  * Common structure pcb for internet protocol implementation.
83  * Here are stored pointers to local and foreign host table
84  * entries, local and foreign socket numbers, and pointers
85  * up (to a socket structure) and down (to a protocol-specific)
86  * control block.
87  */
88 struct inpcb {
89 	LIST_ENTRY(inpcb) inp_hash;
90 	LIST_ENTRY(inpcb) inp_lhash;		/* extra hash for lport */
91 	CIRCLEQ_ENTRY(inpcb) inp_queue;
92 	struct	  inpcbtable *inp_table;
93 	union	  inpaddru inp_faddru;		/* Foreign address. */
94 	union	  inpaddru inp_laddru;		/* Local address. */
95 #define	inp_faddr	inp_faddru.iau_a4u.inaddr
96 #define	inp_faddr6	inp_faddru.iau_addr6
97 #define	inp_laddr	inp_laddru.iau_a4u.inaddr
98 #define	inp_laddr6	inp_laddru.iau_addr6
99 	u_int16_t inp_fport;		/* foreign port */
100 	u_int16_t inp_lport;		/* local port */
101 	struct	  socket *inp_socket;	/* back pointer to socket */
102 	caddr_t	  inp_ppcb;		/* pointer to per-protocol pcb */
103 	union {				/* Route (notice increased size). */
104 		struct route ru_route;
105 		struct route_in6 ru_route6;
106 	} inp_ru;
107 #define	inp_route	inp_ru.ru_route
108 #define	inp_route6	inp_ru.ru_route6
109 	int	  inp_flags;		/* generic IP/datagram flags */
110 	union {				/* Header prototype. */
111 		struct ip hu_ip;
112 		struct ip6_hdr hu_ipv6;
113 	} inp_hu;
114 #define	inp_ip		inp_hu.hu_ip
115 #define	inp_ipv6	inp_hu.hu_ipv6
116 	struct	  mbuf *inp_options;	/* IP options */
117 	struct ip6_pktopts *inp_outputopts6; /* IP6 options for outgoing packets */
118 	int inp_hops;
119 	union {
120 		struct ip_moptions *mou_mo;    /* IPv4 multicast options */
121 		struct ip6_moptions *mou_mo6; /* IPv6 multicast options */
122 	} inp_mou;
123 #define inp_moptions inp_mou.mou_mo
124 #define inp_moptions6 inp_mou.mou_mo6
125 	u_char	  inp_seclevel[4];
126 #define SL_AUTH           0             /* Authentication level */
127 #define SL_ESP_TRANS      1             /* ESP transport level */
128 #define SL_ESP_NETWORK    2             /* ESP network (encapsulation) level */
129 #define SL_IPCOMP         3             /* Compression level */
130 	u_int8_t  inp_secrequire:4,     /* Condensed State from above */
131 	          inp_secresult:4;	/* Result from Key Management */
132 #define SR_FAILED         1             /* Negotiation failed permanently */
133 #define SR_SUCCESS        2             /* SA successfully established */
134 #define SR_WAIT           3             /* Waiting for SA */
135 	TAILQ_ENTRY(inpcb) inp_tdb_in_next, inp_tdb_out_next;
136 	struct tdb     *inp_tdb_in, *inp_tdb_out;
137 	struct ipsec_policy *inp_ipo;
138 	struct ipsec_ref *inp_ipsec_remotecred;
139 	struct ipsec_ref *inp_ipsec_remoteauth;
140 #define	inp_flowinfo	inp_hu.hu_ipv6.ip6_flow
141 
142 	int	in6p_cksum;
143 #ifndef _KERNEL
144 #define inp_csumoffset	in6p_cksum
145 #endif
146 	struct	icmp6_filter *inp_icmp6filt;
147 };
148 
149 struct inpcbtable {
150 	CIRCLEQ_HEAD(, inpcb) inpt_queue;
151 	LIST_HEAD(inpcbhead, inpcb) *inpt_hashtbl, *inpt_lhashtbl;
152 	u_long	  inpt_hash, inpt_lhash;
153 	u_int16_t inpt_lastport;
154 };
155 
156 /* flags in inp_flags: */
157 #define	INP_RECVOPTS	0x001	/* receive incoming IP options */
158 #define	INP_RECVRETOPTS	0x002	/* receive IP options for reply */
159 #define	INP_RECVDSTADDR	0x004	/* receive IP dst address */
160 
161 #define	INP_RXDSTOPTS	INP_RECVOPTS
162 #define	INP_RXHOPOPTS	INP_RECVRETOPTS
163 #define	INP_RXINFO	INP_RECVDSTADDR
164 #define	INP_RXSRCRT	0x010
165 #define	INP_HOPLIMIT	0x020
166 
167 #define	INP_CONTROLOPTS	(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR| \
168 	    INP_RXSRCRT|INP_HOPLIMIT)
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 
174 /*
175  * These flags' values should be determined by either the transport
176  * protocol at PRU_BIND, PRU_LISTEN, PRU_CONNECT, etc, or by in_pcb*().
177  */
178 #define	INP_IPV6	0x100	/* sotopf(inp->inp_socket) == PF_INET6 */
179 
180 #if 1	/*KAME*/
181 /*
182  * Flags in in6p_flags
183  * We define KAME's original flags in higher 16 bits as much as possible
184  * for compatibility with *bsd*s.
185  * XXX: Should IN6P_HIGHPORT and IN6P_LOWPORT be moved as well?
186  */
187 #define IN6P_RECVOPTS		INP_RECVOPTS	/* recv incoming IP6 options */
188 #define IN6P_RECVRETOPTS	INP_RECVRETOPTS /* recv IP6 options for reply */
189 #define IN6P_RECVDSTADDR	INP_RECVDSTADDR /* recv IP6 dst address */
190 #define IN6P_HIGHPORT		INP_HIGHPORT	/* user wants "high" port */
191 #define IN6P_LOWPORT		INP_LOWPORT	/* user wants "low" port */
192 #define IN6P_PKTINFO		0x010000 /* receive IP6 dst and I/F */
193 #define IN6P_HOPLIMIT		0x020000 /* receive hoplimit */
194 #define IN6P_HOPOPTS		0x040000 /* receive hop-by-hop options */
195 #define IN6P_DSTOPTS		0x080000 /* receive dst options after rthdr */
196 #define IN6P_RTHDR		0x100000 /* receive routing header */
197 #define IN6P_RTHDRDSTOPTS	0x200000 /* receive dstoptions before rthdr */
198 
199 #define IN6P_ANONPORT		0x4000000 /* port chosen for user */
200 #define IN6P_FAITH		0x8000000 /* accept FAITH'ed connections */
201 
202 #define IN6P_MINMTU		0x20000000 /* use minimum MTU */
203 
204 #define IN6P_CONTROLOPTS	(IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
205 				 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
206 				 IN6P_MINMTU)
207 #endif
208 
209 #define	INPLOOKUP_WILDCARD	1
210 #define	INPLOOKUP_SETLOCAL	2
211 #define	INPLOOKUP_IPV6		4
212 
213 #define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
214 
215 /* macros for handling bitmap of ports not to allocate dynamically */
216 #define	DP_MAPBITS	(sizeof(u_int32_t) * NBBY)
217 #define	DP_MAPSIZE	(howmany(IPPORT_RESERVED/2, DP_MAPBITS))
218 #define	DP_SET(m, p)	((m)[((p) - IPPORT_RESERVED/2) / DP_MAPBITS] |= (1 << ((p) % DP_MAPBITS)))
219 #define	DP_CLR(m, p)	((m)[((p) - IPPORT_RESERVED/2) / DP_MAPBITS] &= ~(1 << ((p) % DP_MAPBITS)))
220 #define	DP_ISSET(m, p)	((m)[((p) - IPPORT_RESERVED/2) / DP_MAPBITS] & (1 << ((p) % DP_MAPBITS)))
221 
222 /* default values for baddynamicports [see ip_init()] */
223 #define	DEFBADDYNAMICPORTS_TCP	{ 587, 749, 750, 751, 760, 761, 871, 0 }
224 #define	DEFBADDYNAMICPORTS_UDP	{ 750, 751, 0 }
225 
226 struct baddynamicports {
227 	u_int32_t tcp[DP_MAPSIZE];
228 	u_int32_t udp[DP_MAPSIZE];
229 };
230 
231 #ifdef _KERNEL
232 
233 #define sotopf(so)  (so->so_proto->pr_domain->dom_family)
234 
235 void	 in_losing(struct inpcb *);
236 int	 in_pcballoc(struct socket *, void *);
237 int	 in_pcbbind(void *, struct mbuf *);
238 int	 in_pcbconnect(void *, struct mbuf *);
239 void	 in_pcbdetach(void *);
240 void	 in_pcbdisconnect(void *);
241 struct inpcb *
242 	 in_pcbhashlookup(struct inpcbtable *, struct in_addr,
243 			       u_int, struct in_addr, u_int);
244 struct inpcb *
245 	 in_pcblookup_listen(struct inpcbtable *, struct in_addr, u_int, int);
246 #ifdef INET6
247 struct inpcb *
248 	 in6_pcbhashlookup(struct inpcbtable *, struct in6_addr *,
249 			       u_int, struct in6_addr *, u_int);
250 struct inpcb *
251 	 in6_pcblookup_listen(struct inpcbtable *,
252 			       struct in6_addr *, u_int, int);
253 int	 in6_pcbbind(struct inpcb *, struct mbuf *);
254 int	 in6_pcbconnect(struct inpcb *, struct mbuf *);
255 int	 in6_setsockaddr(struct inpcb *, struct mbuf *);
256 int	 in6_setpeeraddr(struct inpcb *, struct mbuf *);
257 #endif /* INET6 */
258 void	 in_pcbinit(struct inpcbtable *, int);
259 struct inpcb *
260 	 in_pcblookup(struct inpcbtable *, void *, u_int, void *,
261 	    u_int, int);
262 int	 in_pcbnotify(struct inpcbtable *, struct sockaddr *,
263 	    u_int, struct in_addr, u_int, int, void (*)(struct inpcb *, int));
264 void	 in_pcbnotifyall(struct inpcbtable *, struct sockaddr *,
265 	    int, void (*)(struct inpcb *, int));
266 void	 in_pcbrehash(struct inpcb *);
267 void	 in_rtchange(struct inpcb *, int);
268 void	 in_setpeeraddr(struct inpcb *, struct mbuf *);
269 void	 in_setsockaddr(struct inpcb *, struct mbuf *);
270 int	 in_baddynamic(u_int16_t, u_int16_t);
271 extern struct sockaddr_in *in_selectsrc(struct sockaddr_in *,
272 	struct route *, int, struct ip_moptions *, int *);
273 struct rtentry *
274 	in_pcbrtentry(struct inpcb *);
275 
276 /* INET6 stuff */
277 int	in6_pcbnotify(struct inpcbtable *, struct sockaddr *,
278 	u_int, struct sockaddr *, u_int, int, void *,
279 	void (*)(struct inpcb *, int));
280 int	in6_selecthlim(struct inpcb *, struct ifnet *);
281 int	in6_pcbsetport(struct in6_addr *, struct inpcb *, struct proc *);
282 #endif /* _KERNEL */
283 #endif /* _NETINET_IN_PCB_H_ */
284