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