2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1990, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93
63 * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.32.2.4 2001/08/13 16:26:17 ume Exp $
66 #ifndef _NETINET_IN_PCB_H_
67 #define _NETINET_IN_PCB_H_
68 #include <sys/appleapiopts.h>
70 #include <sys/types.h>
71 #include <sys/queue.h>
74 #include <kern/locks.h>
76 #endif /* KERNEL_PRIVATE */
78 #include <netinet6/ipsec.h> /* for IPSEC */
82 #define in6pcb inpcb /* for KAME src sync over BSD*'s */
83 #define in6p_sp inp_sp /* for KAME src sync over BSD*'s */
86 * Common structure pcb for internet protocol implementation.
87 * Here are stored pointers to local and foreign host table
88 * entries, local and foreign socket numbers, and pointers
89 * up (to a socket structure) and down (to a protocol-specific)
92 LIST_HEAD(inpcbhead
, inpcb
);
93 LIST_HEAD(inpcbporthead
, inpcbport
);
94 #endif /* KERNEL_PRIVATE */
95 typedef u_quad_t inp_gen_t
;
98 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
99 * So, AF_INET6 null laddr is also used as AF_INET null laddr,
100 * by utilize following structure. (At last, same as INRIA)
102 struct in_addr_4in6
{
103 u_int32_t ia46_pad32
[3];
104 struct in_addr ia46_addr4
;
107 #ifdef KERNEL_PRIVATE
109 * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
110 * of the structure. Therefore, it is important that the members in
111 * that position not contain any information which is required to be
117 LIST_ENTRY(inpcb
) inp_hash
; /* hash list */
118 int inp_wantcnt
; /* pcb wanted count. protected by pcb list lock */
119 int inp_state
; /* state of this pcb, in use, recycled, ready for recycling... */
120 u_short inp_fport
; /* foreign port */
121 u_short inp_lport
; /* local port */
122 LIST_ENTRY(inpcb
) inp_list
; /* list for all PCBs of this proto */
123 caddr_t inp_ppcb
; /* pointer to per-protocol pcb */
124 struct inpcbinfo
*inp_pcbinfo
; /* PCB list info */
125 struct socket
*inp_socket
; /* back pointer to socket */
126 u_char nat_owner
; /* Used to NAT TCP/UDP traffic */
127 u_long nat_cookie
; /* Cookie stored and returned to NAT */
128 LIST_ENTRY(inpcb
) inp_portlist
; /* list for this PCB's local port */
129 struct inpcbport
*inp_phd
; /* head of this list */
130 inp_gen_t inp_gencnt
; /* generation count of this instance */
131 int inp_flags
; /* generic IP/datagram flags */
134 u_char inp_vflag
; /* INP_IPV4 or INP_IPV6 */
136 u_char inp_ip_ttl
; /* time to live proto */
137 u_char inp_ip_p
; /* protocol proto */
138 /* protocol dependent part */
140 /* foreign host table entry */
141 struct in_addr_4in6 inp46_foreign
;
142 struct in6_addr inp6_foreign
;
145 /* local host table entry */
146 struct in_addr_4in6 inp46_local
;
147 struct in6_addr inp6_local
;
150 /* placeholder for routing entry */
151 struct route inp4_route
;
152 struct route_in6 inp6_route
;
155 /* type of service proto */
158 struct mbuf
*inp4_options
;
159 /* IP multicast options */
160 struct ip_moptions
*inp4_moptions
;
164 struct mbuf
*inp6_options
;
166 u_int8_t unused_uint8_1
;
167 ushort unused_uint16_1
;
168 /* IP6 options for outgoing packets */
169 struct ip6_pktopts
*inp6_outputopts
;
170 /* IP multicast options */
171 struct ip6_moptions
*inp6_moptions
;
172 /* ICMPv6 code type filter */
173 struct icmp6_filter
*inp6_icmp6filt
;
174 /* IPV6_CHECKSUM setsockopt */
176 u_short inp6_ifindex
;
180 int hash_element
; /* Array index of pcb's hash list */
181 caddr_t inp_saved_ppcb
; /* place to save pointer while cached */
182 struct inpcbpolicy
*inp_sp
;
183 #ifdef _KERN_LOCKS_H_
184 lck_mtx_t
*inpcb_mtx
; /* inpcb per-socket mutex */
188 u_long reserved
[2]; /* For future use */
191 #endif /* KERNEL_PRIVATE */
194 * The range of the generation count, as used in this implementation,
195 * is 9e19. We would have to create 300 billion connections per
196 * second for this number to roll over in a year. This seems sufficiently
197 * unlikely that we simply don't concern ourselves with that possibility.
201 * Interface exported to userland by various protocols which use
202 * inpcbs. Hack alert -- only define if struct xsocket is in scope.
206 * This is a copy of the inpcb as it shipped in Panther. This structure
207 * is filled out in a copy function. This allows the inpcb to change
208 * without breaking userland tools.
210 * CAUTION: Many fields may not be filled out. Fewer may be filled out
211 * in the future. Code defensively.
213 #ifdef KERNEL_PRIVATE
214 struct inpcb_compat
{
225 #endif /* KERNEL_PRIVATE */
226 LIST_ENTRY(inpcb
) inp_hash
; /* hash list */
227 struct in_addr reserved1
; /* APPLE reserved: inp_faddr defined in protcol indep. part */
228 struct in_addr reserved2
; /* APPLE reserved */
229 u_short inp_fport
; /* foreign port */
230 u_short inp_lport
; /* local port */
231 LIST_ENTRY(inpcb
) inp_list
; /* list for all PCBs of this proto */
232 caddr_t inp_ppcb
; /* pointer to per-protocol pcb */
233 struct inpcbinfo
*inp_pcbinfo
; /* PCB list info */
234 void* inp_socket
; /* back pointer to socket */
235 u_char nat_owner
; /* Used to NAT TCP/UDP traffic */
236 u_long nat_cookie
; /* Cookie stored and returned to NAT */
237 LIST_ENTRY(inpcb
) inp_portlist
; /* list for this PCB's local port */
238 struct inpcbport
*inp_phd
; /* head of this list */
239 inp_gen_t inp_gencnt
; /* generation count of this instance */
240 int inp_flags
; /* generic IP/datagram flags */
245 u_char inp_ip_ttl
; /* time to live proto */
246 u_char inp_ip_p
; /* protocol proto */
247 /* protocol dependent part */
249 /* foreign host table entry */
250 struct in_addr_4in6 inp46_foreign
;
251 struct in6_addr inp6_foreign
;
254 /* local host table entry */
255 struct in_addr_4in6 inp46_local
;
256 struct in6_addr inp6_local
;
259 /* placeholder for routing entry */
260 u_char inp4_route
[20];
261 u_char inp6_route
[32];
264 /* type of service proto */
267 struct mbuf
*inp4_options
;
268 /* IP multicast options */
269 struct ip_moptions
*inp4_moptions
;
274 struct mbuf
*inp6_options
;
276 u_int8_t unused_uint8_1
;
277 ushort unused_uint16_1
;
278 /* IP6 options for outgoing packets */
279 struct ip6_pktopts
*inp6_outputopts
;
280 /* IP multicast options */
281 struct ip6_moptions
*inp6_moptions
;
282 /* ICMPv6 code type filter */
283 struct icmp6_filter
*inp6_icmp6filt
;
284 /* IPV6_CHECKSUM setsockopt */
286 u_short inp6_ifindex
;
290 int hash_element
; /* Array index of pcb's hash list */
291 caddr_t inp_saved_ppcb
; /* place to save pointer while cached */
292 struct inpcbpolicy
*inp_sp
;
293 u_long reserved
[3]; /* For future use */
297 size_t xi_len
; /* length of this structure */
298 #ifdef KERNEL_PRIVATE
299 struct inpcb_compat xi_inp
;
303 struct xsocket xi_socket
;
304 u_quad_t xi_alignment_hack
;
308 size_t xig_len
; /* length of this structure */
309 u_int xig_count
; /* number of PCBs at this time */
310 inp_gen_t xig_gen
; /* generation count at this time */
311 so_gen_t xig_sogen
; /* socket generation count at this time */
315 * These defines are for use with the inpcb.
319 #define inp_faddr inp_dependfaddr.inp46_foreign.ia46_addr4
320 #define inp_laddr inp_dependladdr.inp46_local.ia46_addr4
321 #define inp_route inp_dependroute.inp4_route
322 #define inp_ip_tos inp_depend4.inp4_ip_tos
323 #define inp_options inp_depend4.inp4_options
324 #define inp_moptions inp_depend4.inp4_moptions
325 #define in6p_faddr inp_dependfaddr.inp6_foreign
326 #define in6p_laddr inp_dependladdr.inp6_local
327 #define in6p_route inp_dependroute.inp6_route
328 #define in6p_ip6_hlim inp_depend6.inp6_hlim
329 #define in6p_hops inp_depend6.inp6_hops /* default hop limit */
330 #define in6p_ip6_nxt inp_ip_p
331 #define in6p_flowinfo inp_flow
332 #define in6p_vflag inp_vflag
333 #define in6p_options inp_depend6.inp6_options
334 #define in6p_outputopts inp_depend6.inp6_outputopts
335 #define in6p_moptions inp_depend6.inp6_moptions
336 #define in6p_icmp6filt inp_depend6.inp6_icmp6filt
337 #define in6p_cksum inp_depend6.inp6_cksum
338 #define inp6_ifindex inp_depend6.inp6_ifindex
339 #define in6p_flags inp_flags /* for KAME src sync over BSD*'s */
340 #define in6p_socket inp_socket /* for KAME src sync over BSD*'s */
341 #define in6p_lport inp_lport /* for KAME src sync over BSD*'s */
342 #define in6p_fport inp_fport /* for KAME src sync over BSD*'s */
343 #define in6p_ppcb inp_ppcb /* for KAME src sync over BSD*'s */
344 #define in6p_state inp_state
345 #define in6p_wantcnt inp_wantcnt
347 #ifdef KERNEL_PRIVATE
349 LIST_ENTRY(inpcbport
) phd_hash
;
350 struct inpcbhead phd_pcblist
;
354 struct inpcbinfo
{ /* XXX documentation, prefixes */
355 struct inpcbhead
*hashbase
;
357 u_long hashsize
; /* in elements */
360 struct inpcbporthead
*porthashbase
;
362 struct inpcbhead
*listhead
;
366 void *ipi_zone
; /* zone to allocate pcbs from */
367 u_int ipi_count
; /* number of pcbs in this list */
368 u_quad_t ipi_gencnt
; /* current generation count */
371 struct socket nat_dummy_socket
; /* fake socket for NAT pcb backpointer */
372 struct inpcb
*nat_dummy_pcb
; /* fake pcb for finding NAT mutex */
374 #ifdef _KERN_LOCKS_H_
375 lck_attr_t
*mtx_attr
; /* mutex attributes */
376 lck_grp_t
*mtx_grp
; /* mutex group definition */
377 lck_grp_attr_t
*mtx_grp_attr
; /* mutex group attributes */
378 lck_rw_t
*mtx
; /* global mutex for the pcblist*/
380 void *mtx_attr
; /* mutex attributes */
381 void *mtx_grp
; /* mutex group definition */
382 void *mtx_grp_attr
; /* mutex group attributes */
383 void *mtx
; /* global mutex for the pcblist*/
388 #define INP_PCBHASH(faddr, lport, fport, mask) \
389 (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
390 #define INP_PCBPORTHASH(lport, mask) \
391 (ntohs((lport)) & (mask))
393 #endif /* KERNEL_PRIVATE */
395 /* flags in inp_flags: */
396 #define INP_RECVOPTS 0x01 /* receive incoming IP options */
397 #define INP_RECVRETOPTS 0x02 /* receive IP options for reply */
398 #define INP_RECVDSTADDR 0x04 /* receive IP dst address */
399 #define INP_HDRINCL 0x08 /* user supplies entire IP header */
400 #define INP_HIGHPORT 0x10 /* user wants "high" port binding */
401 #define INP_LOWPORT 0x20 /* user wants "low" port binding */
402 #define INP_ANONPORT 0x40 /* port chosen for user */
403 #define INP_RECVIF 0x80 /* receive incoming interface */
404 #define INP_MTUDISC 0x100 /* user can do MTU discovery */
406 #define INP_STRIPHDR 0x200 /* Strip headers in raw_ip, for OT support */
408 #define INP_FAITH 0x400 /* accept FAITH'ed connections */
409 #define INP_INADDR_ANY 0x800 /* local address wasn't specified */
411 #define INP_RECVTTL 0x1000
413 #define IN6P_IPV6_V6ONLY 0x008000 /* restrict AF_INET6 socket for v6 */
415 #define IN6P_PKTINFO 0x010000 /* receive IP6 dst and I/F */
416 #define IN6P_HOPLIMIT 0x020000 /* receive hoplimit */
417 #define IN6P_HOPOPTS 0x040000 /* receive hop-by-hop options */
418 #define IN6P_DSTOPTS 0x080000 /* receive dst options after rthdr */
419 #define IN6P_RTHDR 0x100000 /* receive routing header */
420 #define IN6P_RTHDRDSTOPTS 0x200000 /* receive dstoptions before rthdr */
421 #define IN6P_AUTOFLOWLABEL 0x800000 /* attach flowlabel automatically */
422 #define IN6P_BINDV6ONLY 0x10000000 /* do not grab IPv4 traffic */
424 #ifdef KERNEL_PRIVATE
425 #define INP_CONTROLOPTS (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|\
427 IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|\
428 IN6P_DSTOPTS|IN6P_RTHDR|IN6P_RTHDRDSTOPTS|\
429 IN6P_AUTOFLOWLABEL|INP_RECVTTL)
430 #define INP_UNMAPPABLEOPTS (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR|\
433 /* for KAME src sync over BSD*'s */
434 #define IN6P_HIGHPORT INP_HIGHPORT
435 #define IN6P_LOWPORT INP_LOWPORT
436 #define IN6P_ANONPORT INP_ANONPORT
437 #define IN6P_RECVIF INP_RECVIF
438 #define IN6P_MTUDISC INP_MTUDISC
439 #define IN6P_FAITH INP_FAITH
440 #define IN6P_CONTROLOPTS INP_CONTROLOPTS
442 * socket AF version is {newer than,or include}
443 * actual datagram AF version
446 #define INPLOOKUP_WILDCARD 1
448 #define INPCB_ALL_OWNERS 0xff
449 #define INPCB_NO_OWNER 0x0
450 #define INPCB_OWNED_BY_X 0x80
451 #define INPCB_MAX_IDS 7
452 #endif /* __APPLE__ */
454 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
455 #define sotoin6pcb(so) sotoinpcb(so) /* for KAME src sync over BSD*'s */
457 #define INP_SOCKAF(so) so->so_proto->pr_domain->dom_family
459 #define INP_CHECK_SOCKAF(so, af) (INP_SOCKAF(so) == af)
462 extern int ipport_lowfirstauto
;
463 extern int ipport_lowlastauto
;
464 extern int ipport_firstauto
;
465 extern int ipport_lastauto
;
466 extern int ipport_hifirstauto
;
467 extern int ipport_hilastauto
;
469 #define INPCB_STATE_INUSE 0x1 /* freshly allocated PCB, it's in use */
470 #define INPCB_STATE_CACHED 0x2 /* this pcb is sitting in a a cache */
471 #define INPCB_STATE_DEAD 0x3 /* should treat as gone, will be garbage collected and freed */
473 #define WNT_STOPUSING 0xffff /* marked as ready to be garbaged collected, should be treated as not found */
474 #define WNT_ACQUIRE 0x1 /* that pcb is being acquired, do not recycle this time */
475 #define WNT_RELEASE 0x2 /* release acquired mode, can be garbage collected when wantcnt is null */
478 void in_pcbpurgeif0(struct inpcb
*, struct ifnet
*);
479 void in_losing(struct inpcb
*);
480 void in_rtchange(struct inpcb
*, int);
481 int in_pcballoc(struct socket
*, struct inpcbinfo
*, struct proc
*);
482 int in_pcbbind(struct inpcb
*, struct sockaddr
*, struct proc
*);
483 int in_pcbconnect(struct inpcb
*, struct sockaddr
*, struct proc
*);
484 void in_pcbdetach(struct inpcb
*);
485 void in_pcbdispose (struct inpcb
*);
486 void in_pcbdisconnect(struct inpcb
*);
487 int in_pcbinshash(struct inpcb
*, int);
488 int in_pcbladdr(struct inpcb
*, struct sockaddr
*, struct sockaddr_in
**);
490 in_pcblookup_local(struct inpcbinfo
*, struct in_addr
, u_int
, int);
492 in_pcblookup_hash(struct inpcbinfo
*,
493 struct in_addr
, u_int
, struct in_addr
, u_int
,
494 int, struct ifnet
*);
495 void in_pcbnotifyall(struct inpcbinfo
*, struct in_addr
,
496 int, void (*)(struct inpcb
*, int));
497 void in_pcbrehash(struct inpcb
*);
498 int in_setpeeraddr(struct socket
*so
, struct sockaddr
**nam
);
499 int in_setsockaddr(struct socket
*so
, struct sockaddr
**nam
);
500 int in_pcb_checkstate(struct inpcb
*pcb
, int mode
, int locked
);
503 in_pcb_grab_port (struct inpcbinfo
*pcbinfo
,
505 struct in_addr laddr
,
507 struct in_addr faddr
,
513 in_pcb_letgo_port(struct inpcbinfo
*pcbinfo
,
514 struct in_addr laddr
,
516 struct in_addr faddr
,
517 u_short fport
, u_char owner_id
);
520 in_pcb_get_owner(struct inpcbinfo
*pcbinfo
,
521 struct in_addr laddr
,
523 struct in_addr faddr
,
527 void in_pcb_nat_init(struct inpcbinfo
*pcbinfo
, int afamily
, int pfamily
,
531 in_pcb_new_share_client(struct inpcbinfo
*pcbinfo
, u_char
*owner_id
);
534 in_pcb_rem_share_client(struct inpcbinfo
*pcbinfo
, u_char owner_id
);
536 void in_pcbremlists(struct inpcb
*inp
);
537 int in_pcb_ckeckstate(struct inpcb
*, int, int);
538 void inpcb_to_compat(struct inpcb
*inp
, struct inpcb_compat
*inp_compat
);
541 #endif /* KERNEL_PRIVATE */
543 #endif /* !_NETINET_IN_PCB_H_ */