]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_pcb.h
xnu-124.13.tar.gz
[apple/xnu.git] / bsd / netinet / in_pcb.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1982, 1986, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93
55 */
56
57 #ifndef _NETINET_IN_PCB_H_
58 #define _NETINET_IN_PCB_H_
59
60 #include <sys/queue.h>
61 #if IPSEC
62 #include <netinet6/ipsec.h>
63 #endif
64
65 #define in6pcb inpcb /* for KAME src sync over BSD*'s */
66 #define in6p_sp inp_sp /* for KAME src sync over BSD*'s */
67
68 /*
69 * Common structure pcb for internet protocol implementation.
70 * Here are stored pointers to local and foreign host table
71 * entries, local and foreign socket numbers, and pointers
72 * up (to a socket structure) and down (to a protocol-specific)
73 * control block.
74 */
75 LIST_HEAD(inpcbhead, inpcb);
76 LIST_HEAD(inpcbporthead, inpcbport);
77 typedef u_quad_t inp_gen_t;
78
79 /*
80 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
81 * So, AF_INET6 null laddr is also used as AF_INET null laddr,
82 * by utilize following structure. (At last, same as INRIA)
83 */
84 struct in_addr_4in6 {
85 u_int32_t ia46_pad32[3];
86 struct in_addr ia46_addr4;
87 };
88
89 /*
90 * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
91 * of the structure. Therefore, it is important that the members in
92 * that position not contain any information which is required to be
93 * stable.
94 */
95 struct icmp6_filter;
96
97 struct inpcb {
98 LIST_ENTRY(inpcb) inp_hash; /* hash list */
99 struct in_addr inp_faddr; /* foreign host table entry */
100 struct in_addr inp_laddr; /* local host table entry */
101 u_short inp_fport; /* foreign port */
102 u_short inp_lport; /* local port */
103 LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
104 caddr_t inp_ppcb; /* pointer to per-protocol pcb */
105 struct inpcbinfo *inp_pcbinfo; /* PCB list info */
106 struct socket *inp_socket; /* back pointer to socket */
107 u_char nat_owner; /* Used to NAT TCP/UDP traffic */
108 u_long nat_cookie; /* Cookie stored and returned to NAT */
109 LIST_ENTRY(inpcb) inp_portlist; /* list for this PCB's local port */
110 struct inpcbport *inp_phd; /* head of this list */
111 inp_gen_t inp_gencnt; /* generation count of this instance */
112 int inp_flags; /* generic IP/datagram flags */
113 u_int32_t inp_flow;
114
115 u_char inp_vflag;
116 #define INP_IPV4 0x1
117 #define INP_IPV6 0x2
118
119 u_char inp_ip_ttl; /* time to live proto */
120 u_char inp_ip_p; /* protocol proto */
121 /* protocol dependent part */
122 union {
123 /* foreign host table entry */
124 struct in_addr_4in6 inp46_foreign;
125 struct in6_addr inp6_foreign;
126 } inp_dependfaddr;
127 union {
128 /* local host table entry */
129 struct in_addr_4in6 inp46_local;
130 struct in6_addr inp6_local;
131 } inp_dependladdr;
132 union {
133 /* placeholder for routing entry */
134 struct route inp4_route;
135 struct route_in6 inp6_route;
136 } inp_dependroute;
137 struct {
138 /* type of service proto */
139 u_char inp4_ip_tos;
140 /* IP options */
141 struct mbuf *inp4_options;
142 /* IP multicast options */
143 struct ip_moptions *inp4_moptions;
144 } inp_depend4;
145 #define inp_faddr inp_dependfaddr.inp46_foreign.ia46_addr4
146 #define inp_laddr inp_dependladdr.inp46_local.ia46_addr4
147 #define inp_route inp_dependroute.inp4_route
148 #define inp_ip_tos inp_depend4.inp4_ip_tos
149 #define inp_options inp_depend4.inp4_options
150 #define inp_moptions inp_depend4.inp4_moptions
151 struct {
152 /* IP options */
153 struct mbuf *inp6_options;
154 /* IP6 options for incoming packets */
155 struct ip6_recvpktopts *inp6_inputopts;
156 /* IP6 options for outgoing packets */
157 struct ip6_pktopts *inp6_outputopts;
158 /* IP multicast options */
159 struct ip6_moptions *inp6_moptions;
160 /* ICMPv6 code type filter */
161 struct icmp6_filter *inp6_icmp6filt;
162 /* IPV6_CHECKSUM setsockopt */
163 int inp6_cksum;
164 u_short inp6_ifindex;
165 short inp6_hops;
166 } inp_depend6;
167 #define in6p_faddr inp_dependfaddr.inp6_foreign
168 #define in6p_laddr inp_dependladdr.inp6_local
169 #define in6p_route inp_dependroute.inp6_route
170 #define in6p_hops inp_depend6.inp6_hops /* default hop limit */
171 #define in6p_ip6_nxt inp_ip_p
172 #define in6p_flowinfo inp_flow
173 #define in6p_vflag inp_vflag
174 #define in6p_options inp_depend6.inp6_options
175 #define in6p_inputopts inp_depend6.inp6_inputopts
176 #define in6p_outputopts inp_depend6.inp6_outputopts
177 #define in6p_moptions inp_depend6.inp6_moptions
178 #define in6p_icmp6filt inp_depend6.inp6_icmp6filt
179 #define in6p_cksum inp_depend6.inp6_cksum
180 #define inp6_ifindex inp_depend6.inp6_ifindex
181 #define in6p_flags inp_flags /* for KAME src sync over BSD*'s */
182 #define in6p_socket inp_socket /* for KAME src sync over BSD*'s */
183 #define in6p_lport inp_lport /* for KAME src sync over BSD*'s */
184 #define in6p_fport inp_fport /* for KAME src sync over BSD*'s */
185 #define in6p_ppcb inp_ppcb /* for KAME src sync over BSD*'s */
186 #if IPSEC
187 struct inpcbpolicy *inp_sp;
188 #endif
189 int hash_element; /* Array index of pcb's hash list */
190 caddr_t inp_saved_ppcb; /* place to save pointer while cached */
191 };
192 /*
193 * The range of the generation count, as used in this implementation,
194 * is 9e19. We would have to create 300 billion connections per
195 * second for this number to roll over in a year. This seems sufficiently
196 * unlikely that we simply don't concern ourselves with that possibility.
197 */
198
199 /*
200 * Interface exported to userland by various protocols which use
201 * inpcbs. Hack alert -- only define if struct xsocket is in scope.
202 */
203 #ifdef _SYS_SOCKETVAR_H_
204 struct xinpcb {
205 size_t xi_len; /* length of this structure */
206 struct inpcb xi_inp;
207 struct xsocket xi_socket;
208 u_quad_t xi_alignment_hack;
209 };
210
211 struct xinpgen {
212 size_t xig_len; /* length of this structure */
213 u_int xig_count; /* number of PCBs at this time */
214 inp_gen_t xig_gen; /* generation count at this time */
215 so_gen_t xig_sogen; /* socket generation count at this time */
216 };
217 #endif /* _SYS_SOCKETVAR_H_ */
218
219 struct inpcbport {
220 LIST_ENTRY(inpcbport) phd_hash;
221 struct inpcbhead phd_pcblist;
222 u_short phd_port;
223 };
224
225 struct inpcbinfo { /* XXX documentation, prefixes */
226 struct inpcbhead *hashbase;
227 u_long hashsize; /* in elements */
228 u_long hashmask;
229 struct inpcbporthead *porthashbase;
230 u_long porthashmask;
231 struct inpcbhead *listhead;
232 u_short lastport;
233 u_short lastlow;
234 u_short lasthi;
235 void *ipi_zone; /* zone to allocate pcbs from */
236 u_int ipi_count; /* number of pcbs in this list */
237 u_quad_t ipi_gencnt; /* current generation count */
238 u_char all_owners;
239 struct socket nat_dummy_socket;
240 struct inpcb *last_pcb;
241 caddr_t dummy_cb;
242 };
243
244 #define INP_PCBHASH(faddr, lport, fport, mask) \
245 (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
246 #define INP_PCBPORTHASH(lport, mask) \
247 (ntohs((lport)) & (mask))
248
249 /* flags in inp_flags: */
250 #define INP_RECVOPTS 0x01 /* receive incoming IP options */
251 #define INP_RECVRETOPTS 0x02 /* receive IP options for reply */
252 #define INP_RECVDSTADDR 0x04 /* receive IP dst address */
253 #define INP_HDRINCL 0x08 /* user supplies entire IP header */
254 #define INP_HIGHPORT 0x10 /* user wants "high" port binding */
255 #define INP_LOWPORT 0x20 /* user wants "low" port binding */
256 #define INP_ANONPORT 0x40 /* port chosen for user */
257 #define INP_RECVIF 0x80 /* receive incoming interface */
258 #define INP_MTUDISC 0x100 /* user can do MTU discovery */
259 #define INP_STRIPHDR 0x200 /* drop receive of raw IP header */
260 #define INP_FAITH 0x400 /* accept FAITH'ed connections */
261 #define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */
262 #define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */
263 #define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */
264 #define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */
265 #define IN6P_RTHDR 0x100000 /* receive routing header */
266 #define IN6P_RTHDRDSTOPTS 0x200000 /* receive dstoptions before rthdr */
267 #define IN6P_BINDV6ONLY 0x10000000 /* do not grab IPv4 traffic */
268 #define IN6P_MINMTU 0x20000000 /* use minimum MTU */
269
270 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
271 INP_RECVIF|\
272 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
273 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS)
274 #define INP_UNMAPPABLEOPTS (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR)
275
276 /* for KAME src sync over BSD*'s */
277 #define IN6P_HIGHPORT INP_HIGHPORT
278 #define IN6P_LOWPORT INP_LOWPORT
279 #define IN6P_ANONPORT INP_ANONPORT
280 #define IN6P_RECVIF INP_RECVIF
281 #define IN6P_MTUDISC INP_MTUDISC
282 #define IN6P_FAITH INP_FAITH
283 #define IN6P_CONTROLOPTS INP_CONTROLOPTS
284 /*
285 * socket AF version is {newer than,or include}
286 * actual datagram AF version
287 */
288
289 #define INPLOOKUP_WILDCARD 1
290 #define INPCB_ALL_OWNERS 0xff
291 #define INPCB_NO_OWNER 0x0
292 #define INPCB_OWNED_BY_X 0x80
293 #define INPCB_MAX_IDS 7
294
295 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
296 #define sotoin6pcb(so) sotoinpcb(so) /* for KAME src sync over BSD*'s */
297
298 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
299
300 #define INP_CHECK_SOCKAF(so, af) \
301 (INP_SOCKAF(so) == af)
302
303 #ifdef KERNEL
304 extern int ipport_lowfirstauto;
305 extern int ipport_lowlastauto;
306 extern int ipport_firstauto;
307 extern int ipport_lastauto;
308 extern int ipport_hifirstauto;
309 extern int ipport_hilastauto;
310
311 void in_losing __P((struct inpcb *));
312 int in_pcballoc __P((struct socket *, struct inpcbinfo *, struct proc *));
313 int in_pcbbind __P((struct inpcb *, struct sockaddr *, struct proc *));
314 int in_pcbconnect __P((struct inpcb *, struct sockaddr *, struct proc *));
315 void in_pcbdetach __P((struct inpcb *));
316 void in_pcbdisconnect __P((struct inpcb *));
317 int in_pcbinshash __P((struct inpcb *));
318 int in_pcbladdr __P((struct inpcb *, struct sockaddr *,
319 struct sockaddr_in **));
320 struct inpcb *
321 in_pcblookup_local __P((struct inpcbinfo *,
322 struct in_addr, u_int, int));
323 struct inpcb *
324 in_pcblookup_hash __P((struct inpcbinfo *,
325 struct in_addr, u_int, struct in_addr, u_int, int, struct ifnet *));
326 void in_pcbnotify __P((struct inpcbhead *, struct sockaddr *,
327 u_int, struct in_addr, u_int, int, void (*)(struct inpcb *, int)));
328 void in_pcbrehash __P((struct inpcb *));
329 int in_setpeeraddr __P((struct socket *so, struct sockaddr **nam));
330 int in_setsockaddr __P((struct socket *so, struct sockaddr **nam));
331
332 int
333 in_pcb_grab_port __P((struct inpcbinfo *pcbinfo,
334 u_short options,
335 struct in_addr laddr,
336 u_short *lport,
337 struct in_addr faddr,
338 u_short fport,
339 u_int cookie,
340 u_char owner_id));
341
342 int
343 in_pcb_letgo_port __P((struct inpcbinfo *pcbinfo,
344 struct in_addr laddr,
345 u_short lport,
346 struct in_addr faddr,
347 u_short fport, u_char owner_id));
348
349 u_char
350 in_pcb_get_owner __P((struct inpcbinfo *pcbinfo,
351 struct in_addr laddr,
352 u_short lport,
353 struct in_addr faddr,
354 u_short fport,
355 u_int *cookie));
356
357 void in_pcb_nat_init(struct inpcbinfo *pcbinfo, int afamily, int pfamily,
358 int protocol);
359
360 int
361 in_pcb_new_share_client(struct inpcbinfo *pcbinfo, u_char *owner_id);
362
363 int
364 in_pcb_rem_share_client(struct inpcbinfo *pcbinfo, u_char owner_id);
365
366 void in_pcbremlists __P((struct inpcb *inp));
367 #if INET6
368 int in6_selecthlim __P((struct inpcb *, struct ifnet *));
369 #endif
370
371 #endif /* KERNEL */
372
373 #endif /* !_NETINET_IN_PCB_H_ */