2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1982, 1986, 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93
56 * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.32.2.4 2001/08/13 16:26:17 ume Exp $
59 #ifndef _NETINET_IN_PCB_H_
60 #define _NETINET_IN_PCB_H_
61 #include <sys/appleapiopts.h>
63 #include <sys/types.h>
64 #include <sys/queue.h>
67 #include <kern/locks.h>
69 #endif /* KERNEL_PRIVATE */
71 #include <netinet6/ipsec.h> /* for IPSEC */
75 #define in6pcb inpcb /* for KAME src sync over BSD*'s */
76 #define in6p_sp inp_sp /* for KAME src sync over BSD*'s */
79 * Common structure pcb for internet protocol implementation.
80 * Here are stored pointers to local and foreign host table
81 * entries, local and foreign socket numbers, and pointers
82 * up (to a socket structure) and down (to a protocol-specific)
85 LIST_HEAD(inpcbhead
, inpcb
);
86 LIST_HEAD(inpcbporthead
, inpcbport
);
87 #endif /* KERNEL_PRIVATE */
88 typedef u_quad_t inp_gen_t
;
91 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
92 * So, AF_INET6 null laddr is also used as AF_INET null laddr,
93 * by utilize following structure. (At last, same as INRIA)
96 u_int32_t ia46_pad32
[3];
97 struct in_addr ia46_addr4
;
100 #ifdef KERNEL_PRIVATE
102 * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
103 * of the structure. Therefore, it is important that the members in
104 * that position not contain any information which is required to be
110 LIST_ENTRY(inpcb
) inp_hash
; /* hash list */
111 int inp_wantcnt
; /* pcb wanted count. protected by pcb list lock */
112 int inp_state
; /* state of this pcb, in use, recycled, ready for recycling... */
113 u_short inp_fport
; /* foreign port */
114 u_short inp_lport
; /* local port */
115 LIST_ENTRY(inpcb
) inp_list
; /* list for all PCBs of this proto */
116 caddr_t inp_ppcb
; /* pointer to per-protocol pcb */
117 struct inpcbinfo
*inp_pcbinfo
; /* PCB list info */
118 struct socket
*inp_socket
; /* back pointer to socket */
119 u_char nat_owner
; /* Used to NAT TCP/UDP traffic */
120 u_long nat_cookie
; /* Cookie stored and returned to NAT */
121 LIST_ENTRY(inpcb
) inp_portlist
; /* list for this PCB's local port */
122 struct inpcbport
*inp_phd
; /* head of this list */
123 inp_gen_t inp_gencnt
; /* generation count of this instance */
124 int inp_flags
; /* generic IP/datagram flags */
127 u_char inp_vflag
; /* INP_IPV4 or INP_IPV6 */
129 u_char inp_ip_ttl
; /* time to live proto */
130 u_char inp_ip_p
; /* protocol proto */
131 /* protocol dependent part */
133 /* foreign host table entry */
134 struct in_addr_4in6 inp46_foreign
;
135 struct in6_addr inp6_foreign
;
138 /* local host table entry */
139 struct in_addr_4in6 inp46_local
;
140 struct in6_addr inp6_local
;
143 /* placeholder for routing entry */
144 struct route inp4_route
;
145 struct route_in6 inp6_route
;
148 /* type of service proto */
151 struct mbuf
*inp4_options
;
152 /* IP multicast options */
153 struct ip_moptions
*inp4_moptions
;
157 struct mbuf
*inp6_options
;
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 */
169 u_short inp6_ifindex
;
173 int hash_element
; /* Array index of pcb's hash list */
174 caddr_t inp_saved_ppcb
; /* place to save pointer while cached */
175 struct inpcbpolicy
*inp_sp
;
176 #ifdef _KERN_LOCKS_H_
177 lck_mtx_t
*inpcb_mtx
; /* inpcb per-socket mutex */
181 u_long reserved
[2]; /* For future use */
184 #endif /* KERNEL_PRIVATE */
187 * The range of the generation count, as used in this implementation,
188 * is 9e19. We would have to create 300 billion connections per
189 * second for this number to roll over in a year. This seems sufficiently
190 * unlikely that we simply don't concern ourselves with that possibility.
194 * Interface exported to userland by various protocols which use
195 * inpcbs. Hack alert -- only define if struct xsocket is in scope.
199 * This is a copy of the inpcb as it shipped in Panther. This structure
200 * is filled out in a copy function. This allows the inpcb to change
201 * without breaking userland tools.
203 * CAUTION: Many fields may not be filled out. Fewer may be filled out
204 * in the future. Code defensively.
206 #ifdef KERNEL_PRIVATE
207 struct inpcb_compat
{
218 #endif /* KERNEL_PRIVATE */
219 LIST_ENTRY(inpcb
) inp_hash
; /* hash list */
220 struct in_addr reserved1
; /* APPLE reserved: inp_faddr defined in protcol indep. part */
221 struct in_addr reserved2
; /* APPLE reserved */
222 u_short inp_fport
; /* foreign port */
223 u_short inp_lport
; /* local port */
224 LIST_ENTRY(inpcb
) inp_list
; /* list for all PCBs of this proto */
225 caddr_t inp_ppcb
; /* pointer to per-protocol pcb */
226 struct inpcbinfo
*inp_pcbinfo
; /* PCB list info */
227 void* inp_socket
; /* back pointer to socket */
228 u_char nat_owner
; /* Used to NAT TCP/UDP traffic */
229 u_long nat_cookie
; /* Cookie stored and returned to NAT */
230 LIST_ENTRY(inpcb
) inp_portlist
; /* list for this PCB's local port */
231 struct inpcbport
*inp_phd
; /* head of this list */
232 inp_gen_t inp_gencnt
; /* generation count of this instance */
233 int inp_flags
; /* generic IP/datagram flags */
238 u_char inp_ip_ttl
; /* time to live proto */
239 u_char inp_ip_p
; /* protocol proto */
240 /* protocol dependent part */
242 /* foreign host table entry */
243 struct in_addr_4in6 inp46_foreign
;
244 struct in6_addr inp6_foreign
;
247 /* local host table entry */
248 struct in_addr_4in6 inp46_local
;
249 struct in6_addr inp6_local
;
252 /* placeholder for routing entry */
253 u_char inp4_route
[20];
254 u_char inp6_route
[32];
257 /* type of service proto */
260 struct mbuf
*inp4_options
;
261 /* IP multicast options */
262 struct ip_moptions
*inp4_moptions
;
267 struct mbuf
*inp6_options
;
269 u_int8_t unused_uint8_1
;
270 ushort unused_uint16_1
;
271 /* IP6 options for outgoing packets */
272 struct ip6_pktopts
*inp6_outputopts
;
273 /* IP multicast options */
274 struct ip6_moptions
*inp6_moptions
;
275 /* ICMPv6 code type filter */
276 struct icmp6_filter
*inp6_icmp6filt
;
277 /* IPV6_CHECKSUM setsockopt */
279 u_short inp6_ifindex
;
283 int hash_element
; /* Array index of pcb's hash list */
284 caddr_t inp_saved_ppcb
; /* place to save pointer while cached */
285 struct inpcbpolicy
*inp_sp
;
286 u_long reserved
[3]; /* For future use */
290 size_t xi_len
; /* length of this structure */
291 #ifdef KERNEL_PRIVATE
292 struct inpcb_compat xi_inp
;
296 struct xsocket xi_socket
;
297 u_quad_t xi_alignment_hack
;
301 size_t xig_len
; /* length of this structure */
302 u_int xig_count
; /* number of PCBs at this time */
303 inp_gen_t xig_gen
; /* generation count at this time */
304 so_gen_t xig_sogen
; /* socket generation count at this time */
308 * These defines are for use with the inpcb.
312 #define inp_faddr inp_dependfaddr.inp46_foreign.ia46_addr4
313 #define inp_laddr inp_dependladdr.inp46_local.ia46_addr4
314 #define inp_route inp_dependroute.inp4_route
315 #define inp_ip_tos inp_depend4.inp4_ip_tos
316 #define inp_options inp_depend4.inp4_options
317 #define inp_moptions inp_depend4.inp4_moptions
318 #define in6p_faddr inp_dependfaddr.inp6_foreign
319 #define in6p_laddr inp_dependladdr.inp6_local
320 #define in6p_route inp_dependroute.inp6_route
321 #define in6p_ip6_hlim inp_depend6.inp6_hlim
322 #define in6p_hops inp_depend6.inp6_hops /* default hop limit */
323 #define in6p_ip6_nxt inp_ip_p
324 #define in6p_flowinfo inp_flow
325 #define in6p_vflag inp_vflag
326 #define in6p_options inp_depend6.inp6_options
327 #define in6p_outputopts inp_depend6.inp6_outputopts
328 #define in6p_moptions inp_depend6.inp6_moptions
329 #define in6p_icmp6filt inp_depend6.inp6_icmp6filt
330 #define in6p_cksum inp_depend6.inp6_cksum
331 #define inp6_ifindex inp_depend6.inp6_ifindex
332 #define in6p_flags inp_flags /* for KAME src sync over BSD*'s */
333 #define in6p_socket inp_socket /* for KAME src sync over BSD*'s */
334 #define in6p_lport inp_lport /* for KAME src sync over BSD*'s */
335 #define in6p_fport inp_fport /* for KAME src sync over BSD*'s */
336 #define in6p_ppcb inp_ppcb /* for KAME src sync over BSD*'s */
337 #define in6p_state inp_state
338 #define in6p_wantcnt inp_wantcnt
340 #ifdef KERNEL_PRIVATE
342 LIST_ENTRY(inpcbport
) phd_hash
;
343 struct inpcbhead phd_pcblist
;
347 struct inpcbinfo
{ /* XXX documentation, prefixes */
348 struct inpcbhead
*hashbase
;
350 u_long hashsize
; /* in elements */
353 struct inpcbporthead
*porthashbase
;
355 struct inpcbhead
*listhead
;
359 void *ipi_zone
; /* zone to allocate pcbs from */
360 u_int ipi_count
; /* number of pcbs in this list */
361 u_quad_t ipi_gencnt
; /* current generation count */
364 struct socket nat_dummy_socket
; /* fake socket for NAT pcb backpointer */
365 struct inpcb
*nat_dummy_pcb
; /* fake pcb for finding NAT mutex */
367 #ifdef _KERN_LOCKS_H_
368 lck_attr_t
*mtx_attr
; /* mutex attributes */
369 lck_grp_t
*mtx_grp
; /* mutex group definition */
370 lck_grp_attr_t
*mtx_grp_attr
; /* mutex group attributes */
371 lck_rw_t
*mtx
; /* global mutex for the pcblist*/
373 void *mtx_attr
; /* mutex attributes */
374 void *mtx_grp
; /* mutex group definition */
375 void *mtx_grp_attr
; /* mutex group attributes */
376 void *mtx
; /* global mutex for the pcblist*/
381 #define INP_PCBHASH(faddr, lport, fport, mask) \
382 (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
383 #define INP_PCBPORTHASH(lport, mask) \
384 (ntohs((lport)) & (mask))
386 #endif /* KERNEL_PRIVATE */
388 /* flags in inp_flags: */
389 #define INP_RECVOPTS 0x01 /* receive incoming IP options */
390 #define INP_RECVRETOPTS 0x02 /* receive IP options for reply */
391 #define INP_RECVDSTADDR 0x04 /* receive IP dst address */
392 #define INP_HDRINCL 0x08 /* user supplies entire IP header */
393 #define INP_HIGHPORT 0x10 /* user wants "high" port binding */
394 #define INP_LOWPORT 0x20 /* user wants "low" port binding */
395 #define INP_ANONPORT 0x40 /* port chosen for user */
396 #define INP_RECVIF 0x80 /* receive incoming interface */
397 #define INP_MTUDISC 0x100 /* user can do MTU discovery */
399 #define INP_STRIPHDR 0x200 /* Strip headers in raw_ip, for OT support */
401 #define INP_FAITH 0x400 /* accept FAITH'ed connections */
402 #define INP_INADDR_ANY 0x800 /* local address wasn't specified */
404 #define INP_RECVTTL 0x1000
406 #define IN6P_IPV6_V6ONLY 0x008000 /* restrict AF_INET6 socket for v6 */
408 #define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */
409 #define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */
410 #define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */
411 #define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */
412 #define IN6P_RTHDR 0x100000 /* receive routing header */
413 #define IN6P_RTHDRDSTOPTS 0x200000 /* receive dstoptions before rthdr */
414 #define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */
415 #define IN6P_BINDV6ONLY 0x10000000 /* do not grab IPv4 traffic */
417 #ifdef KERNEL_PRIVATE
418 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
420 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
421 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
422 IN6P_AUTOFLOWLABEL|INP_RECVTTL)
423 #define INP_UNMAPPABLEOPTS (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
426 /* for KAME src sync over BSD*'s */
427 #define IN6P_HIGHPORT INP_HIGHPORT
428 #define IN6P_LOWPORT INP_LOWPORT
429 #define IN6P_ANONPORT INP_ANONPORT
430 #define IN6P_RECVIF INP_RECVIF
431 #define IN6P_MTUDISC INP_MTUDISC
432 #define IN6P_FAITH INP_FAITH
433 #define IN6P_CONTROLOPTS INP_CONTROLOPTS
435 * socket AF version is {newer than,or include}
436 * actual datagram AF version
439 #define INPLOOKUP_WILDCARD 1
441 #define INPCB_ALL_OWNERS 0xff
442 #define INPCB_NO_OWNER 0x0
443 #define INPCB_OWNED_BY_X 0x80
444 #define INPCB_MAX_IDS 7
445 #endif /* __APPLE__ */
447 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
448 #define sotoin6pcb(so) sotoinpcb(so) /* for KAME src sync over BSD*'s */
450 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
452 #define INP_CHECK_SOCKAF(so, af) (INP_SOCKAF(so) == af)
455 extern int ipport_lowfirstauto
;
456 extern int ipport_lowlastauto
;
457 extern int ipport_firstauto
;
458 extern int ipport_lastauto
;
459 extern int ipport_hifirstauto
;
460 extern int ipport_hilastauto
;
462 #define INPCB_STATE_INUSE 0x1 /* freshly allocated PCB, it's in use */
463 #define INPCB_STATE_CACHED 0x2 /* this pcb is sitting in a a cache */
464 #define INPCB_STATE_DEAD 0x3 /* should treat as gone, will be garbage collected and freed */
466 #define WNT_STOPUSING 0xffff /* marked as ready to be garbaged collected, should be treated as not found */
467 #define WNT_ACQUIRE 0x1 /* that pcb is being acquired, do not recycle this time */
468 #define WNT_RELEASE 0x2 /* release acquired mode, can be garbage collected when wantcnt is null */
471 void in_pcbpurgeif0(struct inpcb
*, struct ifnet
*);
472 void in_losing(struct inpcb
*);
473 void in_rtchange(struct inpcb
*, int);
474 int in_pcballoc(struct socket
*, struct inpcbinfo
*, struct proc
*);
475 int in_pcbbind(struct inpcb
*, struct sockaddr
*, struct proc
*);
476 int in_pcbconnect(struct inpcb
*, struct sockaddr
*, struct proc
*);
477 void in_pcbdetach(struct inpcb
*);
478 void in_pcbdispose (struct inpcb
*);
479 void in_pcbdisconnect(struct inpcb
*);
480 int in_pcbinshash(struct inpcb
*, int);
481 int in_pcbladdr(struct inpcb
*, struct sockaddr
*, struct sockaddr_in
**);
483 in_pcblookup_local(struct inpcbinfo
*, struct in_addr
, u_int
, int);
485 in_pcblookup_hash(struct inpcbinfo
*,
486 struct in_addr
, u_int
, struct in_addr
, u_int
,
487 int, struct ifnet
*);
488 void in_pcbnotifyall(struct inpcbinfo
*, struct in_addr
,
489 int, void (*)(struct inpcb
*, int));
490 void in_pcbrehash(struct inpcb
*);
491 int in_setpeeraddr(struct socket
*so
, struct sockaddr
**nam
);
492 int in_setsockaddr(struct socket
*so
, struct sockaddr
**nam
);
493 int in_pcb_checkstate(struct inpcb
*pcb
, int mode
, int locked
);
496 in_pcb_grab_port (struct inpcbinfo
*pcbinfo
,
498 struct in_addr laddr
,
500 struct in_addr faddr
,
506 in_pcb_letgo_port(struct inpcbinfo
*pcbinfo
,
507 struct in_addr laddr
,
509 struct in_addr faddr
,
510 u_short fport
, u_char owner_id
);
513 in_pcb_get_owner(struct inpcbinfo
*pcbinfo
,
514 struct in_addr laddr
,
516 struct in_addr faddr
,
520 void in_pcb_nat_init(struct inpcbinfo
*pcbinfo
, int afamily
, int pfamily
,
524 in_pcb_new_share_client(struct inpcbinfo
*pcbinfo
, u_char
*owner_id
);
527 in_pcb_rem_share_client(struct inpcbinfo
*pcbinfo
, u_char owner_id
);
529 void in_pcbremlists(struct inpcb
*inp
);
530 int in_pcb_ckeckstate(struct inpcb
*, int, int);
531 void inpcb_to_compat(struct inpcb
*inp
, struct inpcb_compat
*inp_compat
);
534 #endif /* KERNEL_PRIVATE */
536 #endif /* !_NETINET_IN_PCB_H_ */