]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_pcb.h
xnu-344.26.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 * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.32.2.4 2001/08/13 16:26:17 ume Exp $
56 */
57
58 #ifndef _NETINET_IN_PCB_H_
59 #define _NETINET_IN_PCB_H_
60 #include <sys/appleapiopts.h>
61
62 #include <sys/queue.h>
63
64
65 #include <netinet6/ipsec.h> /* for IPSEC */
66
67 #ifdef __APPLE_API_PRIVATE
68
69 #define in6pcb inpcb /* for KAME src sync over BSD*'s */
70 #define in6p_sp inp_sp /* for KAME src sync over BSD*'s */
71
72 /*
73 * Common structure pcb for internet protocol implementation.
74 * Here are stored pointers to local and foreign host table
75 * entries, local and foreign socket numbers, and pointers
76 * up (to a socket structure) and down (to a protocol-specific)
77 * control block.
78 */
79 LIST_HEAD(inpcbhead, inpcb);
80 LIST_HEAD(inpcbporthead, inpcbport);
81 typedef u_quad_t inp_gen_t;
82
83 /*
84 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
85 * So, AF_INET6 null laddr is also used as AF_INET null laddr,
86 * by utilize following structure. (At last, same as INRIA)
87 */
88 struct in_addr_4in6 {
89 u_int32_t ia46_pad32[3];
90 struct in_addr ia46_addr4;
91 };
92
93 /*
94 * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
95 * of the structure. Therefore, it is important that the members in
96 * that position not contain any information which is required to be
97 * stable.
98 */
99 struct icmp6_filter;
100
101 struct inpcb {
102 LIST_ENTRY(inpcb) inp_hash; /* hash list */
103 struct in_addr inp_faddr; /* foreign host table entry */
104 struct in_addr inp_laddr; /* local host table entry */
105 u_short inp_fport; /* foreign port */
106 u_short inp_lport; /* local port */
107 LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
108 caddr_t inp_ppcb; /* pointer to per-protocol pcb */
109 struct inpcbinfo *inp_pcbinfo; /* PCB list info */
110 struct socket *inp_socket; /* back pointer to socket */
111 u_char nat_owner; /* Used to NAT TCP/UDP traffic */
112 u_long nat_cookie; /* Cookie stored and returned to NAT */
113 LIST_ENTRY(inpcb) inp_portlist; /* list for this PCB's local port */
114 struct inpcbport *inp_phd; /* head of this list */
115 inp_gen_t inp_gencnt; /* generation count of this instance */
116 int inp_flags; /* generic IP/datagram flags */
117 u_int32_t inp_flow;
118
119 u_char inp_vflag;
120 #define INP_IPV4 0x1
121 #define INP_IPV6 0x2
122
123 u_char inp_ip_ttl; /* time to live proto */
124 u_char inp_ip_p; /* protocol proto */
125 /* protocol dependent part */
126 union {
127 /* foreign host table entry */
128 struct in_addr_4in6 inp46_foreign;
129 struct in6_addr inp6_foreign;
130 } inp_dependfaddr;
131 union {
132 /* local host table entry */
133 struct in_addr_4in6 inp46_local;
134 struct in6_addr inp6_local;
135 } inp_dependladdr;
136 union {
137 /* placeholder for routing entry */
138 struct route inp4_route;
139 struct route_in6 inp6_route;
140 } inp_dependroute;
141 struct {
142 /* type of service proto */
143 u_char inp4_ip_tos;
144 /* IP options */
145 struct mbuf *inp4_options;
146 /* IP multicast options */
147 struct ip_moptions *inp4_moptions;
148 } inp_depend4;
149 #define inp_faddr inp_dependfaddr.inp46_foreign.ia46_addr4
150 #define inp_laddr inp_dependladdr.inp46_local.ia46_addr4
151 #define inp_route inp_dependroute.inp4_route
152 #define inp_ip_tos inp_depend4.inp4_ip_tos
153 #define inp_options inp_depend4.inp4_options
154 #define inp_moptions inp_depend4.inp4_moptions
155 struct {
156 /* IP options */
157 struct mbuf *inp6_options;
158 u_int8_t inp6_hlim;
159 u_int8_t unused_uint8_1;
160 ushort unused_uint16_1;
161 /* IP6 options for outgoing packets */
162 struct ip6_pktopts *inp6_outputopts;
163 /* IP multicast options */
164 struct ip6_moptions *inp6_moptions;
165 /* ICMPv6 code type filter */
166 struct icmp6_filter *inp6_icmp6filt;
167 /* IPV6_CHECKSUM setsockopt */
168 int inp6_cksum;
169 u_short inp6_ifindex;
170 short inp6_hops;
171 } inp_depend6;
172 #define in6p_faddr inp_dependfaddr.inp6_foreign
173 #define in6p_laddr inp_dependladdr.inp6_local
174 #define in6p_route inp_dependroute.inp6_route
175 #define in6p_ip6_hlim inp_depend6.inp6_hlim
176 #define in6p_hops inp_depend6.inp6_hops /* default hop limit */
177 #define in6p_ip6_nxt inp_ip_p
178 #define in6p_flowinfo inp_flow
179 #define in6p_vflag inp_vflag
180 #define in6p_options inp_depend6.inp6_options
181 #define in6p_outputopts inp_depend6.inp6_outputopts
182 #define in6p_moptions inp_depend6.inp6_moptions
183 #define in6p_icmp6filt inp_depend6.inp6_icmp6filt
184 #define in6p_cksum inp_depend6.inp6_cksum
185 #define inp6_ifindex inp_depend6.inp6_ifindex
186 #define in6p_flags inp_flags /* for KAME src sync over BSD*'s */
187 #define in6p_socket inp_socket /* for KAME src sync over BSD*'s */
188 #define in6p_lport inp_lport /* for KAME src sync over BSD*'s */
189 #define in6p_fport inp_fport /* for KAME src sync over BSD*'s */
190 #define in6p_ppcb inp_ppcb /* for KAME src sync over BSD*'s */
191
192 int hash_element; /* Array index of pcb's hash list */
193 caddr_t inp_saved_ppcb; /* place to save pointer while cached */
194 struct inpcbpolicy *inp_sp;
195 u_long reserved[3]; /* For future use */
196 };
197 #endif /* __APPLE_API_PRIVATE */
198 /*
199 * The range of the generation count, as used in this implementation,
200 * is 9e19. We would have to create 300 billion connections per
201 * second for this number to roll over in a year. This seems sufficiently
202 * unlikely that we simply don't concern ourselves with that possibility.
203 */
204
205 /*
206 * Interface exported to userland by various protocols which use
207 * inpcbs. Hack alert -- only define if struct xsocket is in scope.
208 */
209 #ifdef _SYS_SOCKETVAR_H_
210 struct xinpcb {
211 size_t xi_len; /* length of this structure */
212 struct inpcb xi_inp;
213 struct xsocket xi_socket;
214 u_quad_t xi_alignment_hack;
215 };
216
217 struct xinpgen {
218 size_t xig_len; /* length of this structure */
219 u_int xig_count; /* number of PCBs at this time */
220 inp_gen_t xig_gen; /* generation count at this time */
221 so_gen_t xig_sogen; /* socket generation count at this time */
222 };
223 #endif /* _SYS_SOCKETVAR_H_ */
224
225 #ifdef __APPLE_API_PRIVATE
226 struct inpcbport {
227 LIST_ENTRY(inpcbport) phd_hash;
228 struct inpcbhead phd_pcblist;
229 u_short phd_port;
230 };
231
232 struct inpcbinfo { /* XXX documentation, prefixes */
233 struct inpcbhead *hashbase;
234 #ifdef __APPLE__
235 u_long hashsize; /* in elements */
236 #endif
237 u_long hashmask;
238 struct inpcbporthead *porthashbase;
239 u_long porthashmask;
240 struct inpcbhead *listhead;
241 u_short lastport;
242 u_short lastlow;
243 u_short lasthi;
244 void *ipi_zone; /* zone to allocate pcbs from */
245 u_int ipi_count; /* number of pcbs in this list */
246 u_quad_t ipi_gencnt; /* current generation count */
247 #ifdef __APPLE__
248 u_char all_owners;
249 struct socket nat_dummy_socket;
250 struct inpcb *last_pcb;
251 caddr_t dummy_cb;
252 #endif
253 };
254
255 #define INP_PCBHASH(faddr, lport, fport, mask) \
256 (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
257 #define INP_PCBPORTHASH(lport, mask) \
258 (ntohs((lport)) & (mask))
259
260 /* flags in inp_flags: */
261 #define INP_RECVOPTS 0x01 /* receive incoming IP options */
262 #define INP_RECVRETOPTS 0x02 /* receive IP options for reply */
263 #define INP_RECVDSTADDR 0x04 /* receive IP dst address */
264 #define INP_HDRINCL 0x08 /* user supplies entire IP header */
265 #define INP_HIGHPORT 0x10 /* user wants "high" port binding */
266 #define INP_LOWPORT 0x20 /* user wants "low" port binding */
267 #define INP_ANONPORT 0x40 /* port chosen for user */
268 #define INP_RECVIF 0x80 /* receive incoming interface */
269 #define INP_MTUDISC 0x100 /* user can do MTU discovery */
270 #ifdef __APPLE__
271 #define INP_STRIPHDR 0x200 /* Strip headers in raw_ip, for OT support */
272 #endif
273 #define INP_FAITH 0x400 /* accept FAITH'ed connections */
274
275 #define IN6P_IPV6_V6ONLY 0x008000 /* restrict AF_INET6 socket for v6 */
276
277 #define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */
278 #define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */
279 #define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */
280 #define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */
281 #define IN6P_RTHDR 0x100000 /* receive routing header */
282 #define IN6P_RTHDRDSTOPTS 0x200000 /* receive dstoptions before rthdr */
283 #define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */
284 #define IN6P_BINDV6ONLY 0x10000000 /* do not grab IPv4 traffic */
285
286 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
287 INP_RECVIF|\
288 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
289 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
290 IN6P_AUTOFLOWLABEL)
291 #define INP_UNMAPPABLEOPTS (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
292 IN6P_AUTOFLOWLABEL)
293
294 /* for KAME src sync over BSD*'s */
295 #define IN6P_HIGHPORT INP_HIGHPORT
296 #define IN6P_LOWPORT INP_LOWPORT
297 #define IN6P_ANONPORT INP_ANONPORT
298 #define IN6P_RECVIF INP_RECVIF
299 #define IN6P_MTUDISC INP_MTUDISC
300 #define IN6P_FAITH INP_FAITH
301 #define IN6P_CONTROLOPTS INP_CONTROLOPTS
302 /*
303 * socket AF version is {newer than,or include}
304 * actual datagram AF version
305 */
306
307 #define INPLOOKUP_WILDCARD 1
308 #ifdef __APPLE__
309 #define INPCB_ALL_OWNERS 0xff
310 #define INPCB_NO_OWNER 0x0
311 #define INPCB_OWNED_BY_X 0x80
312 #define INPCB_MAX_IDS 7
313 #endif
314
315 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
316 #define sotoin6pcb(so) sotoinpcb(so) /* for KAME src sync over BSD*'s */
317
318 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
319
320 #define INP_CHECK_SOCKAF(so, af) (INP_SOCKAF(so) == af)
321
322 #ifdef KERNEL
323 extern int ipport_lowfirstauto;
324 extern int ipport_lowlastauto;
325 extern int ipport_firstauto;
326 extern int ipport_lastauto;
327 extern int ipport_hifirstauto;
328 extern int ipport_hilastauto;
329
330 void in_pcbpurgeif0 __P((struct inpcb *, struct ifnet *));
331 void in_losing __P((struct inpcb *));
332 void in_rtchange __P((struct inpcb *, int));
333 int in_pcballoc __P((struct socket *, struct inpcbinfo *, struct proc *));
334 int in_pcbbind __P((struct inpcb *, struct sockaddr *, struct proc *));
335 int in_pcbconnect __P((struct inpcb *, struct sockaddr *, struct proc *));
336 void in_pcbdetach __P((struct inpcb *));
337 void in_pcbdisconnect __P((struct inpcb *));
338 int in_pcbinshash __P((struct inpcb *));
339 int in_pcbladdr __P((struct inpcb *, struct sockaddr *,
340 struct sockaddr_in **));
341 struct inpcb *
342 in_pcblookup_local __P((struct inpcbinfo *,
343 struct in_addr, u_int, int));
344 struct inpcb *
345 in_pcblookup_hash __P((struct inpcbinfo *,
346 struct in_addr, u_int, struct in_addr, u_int,
347 int, struct ifnet *));
348 void in_pcbnotifyall __P((struct inpcbhead *, struct in_addr,
349 int, void (*)(struct inpcb *, int)));
350 void in_pcbrehash __P((struct inpcb *));
351 int in_setpeeraddr __P((struct socket *so, struct sockaddr **nam));
352 int in_setsockaddr __P((struct socket *so, struct sockaddr **nam));
353
354 #ifdef __APPLE__
355 int
356 in_pcb_grab_port __P((struct inpcbinfo *pcbinfo,
357 u_short options,
358 struct in_addr laddr,
359 u_short *lport,
360 struct in_addr faddr,
361 u_short fport,
362 u_int cookie,
363 u_char owner_id));
364
365 int
366 in_pcb_letgo_port __P((struct inpcbinfo *pcbinfo,
367 struct in_addr laddr,
368 u_short lport,
369 struct in_addr faddr,
370 u_short fport, u_char owner_id));
371
372 u_char
373 in_pcb_get_owner __P((struct inpcbinfo *pcbinfo,
374 struct in_addr laddr,
375 u_short lport,
376 struct in_addr faddr,
377 u_short fport,
378 u_int *cookie));
379
380 void in_pcb_nat_init(struct inpcbinfo *pcbinfo, int afamily, int pfamily,
381 int protocol);
382
383 int
384 in_pcb_new_share_client(struct inpcbinfo *pcbinfo, u_char *owner_id);
385
386 int
387 in_pcb_rem_share_client(struct inpcbinfo *pcbinfo, u_char owner_id);
388 #endif /* __APPLE__ */
389
390 void in_pcbremlists __P((struct inpcb *inp));
391 #ifndef __APPLE__
392 int prison_xinpcb __P((struct proc *p, struct inpcb *inp));
393 #endif
394 #endif /* _KERNEL */
395 #endif /* __APPLE_API_PRIVATE */
396
397 #endif /* !_NETINET_IN_PCB_H_ */