]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_pcb.h
xnu-3248.40.184.tar.gz
[apple/xnu.git] / bsd / netinet / in_pcb.h
1 /*
2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1986, 1990, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)in_pcb.h 8.1 (Berkeley) 6/10/93
61 * $FreeBSD: src/sys/netinet/in_pcb.h,v 1.32.2.4 2001/08/13 16:26:17 ume Exp $
62 */
63 /*
64 * NOTICE: This file was modified by SPARTA, Inc. in 2007 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
67 * Version 2.0.
68 */
69
70 #ifndef _NETINET_IN_PCB_H_
71 #define _NETINET_IN_PCB_H_
72 #include <sys/appleapiopts.h>
73
74 #include <sys/types.h>
75 #include <sys/queue.h>
76 #ifdef BSD_KERNEL_PRIVATE
77 #include <sys/bitstring.h>
78 #include <sys/tree.h>
79 #include <kern/locks.h>
80 #include <kern/zalloc.h>
81 #endif /* BSD_KERNEL_PRIVATE */
82
83 #include <netinet6/ipsec.h> /* for IPSEC */
84 #if NECP
85 #include <net/necp.h>
86 #endif
87
88 #if IPSEC
89 #include <netinet6/ipsec.h> /* for IPSEC */
90 #endif
91
92 #ifdef BSD_KERNEL_PRIVATE
93 /*
94 * struct inpcb is the common protocol control block structure used in most
95 * IP transport protocols.
96 *
97 * Pointers to local and foreign host table entries, local and foreign socket
98 * numbers, and pointers up (to a socket structure) and down (to a
99 * protocol-specific control block) are stored here.
100 */
101 LIST_HEAD(inpcbhead, inpcb);
102 LIST_HEAD(inpcbporthead, inpcbport);
103 #endif /* BSD_KERNEL_PRIVATE */
104 typedef u_quad_t inp_gen_t;
105
106 /*
107 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
108 * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
109 * the following structure.
110 */
111 struct in_addr_4in6 {
112 u_int32_t ia46_pad32[3];
113 struct in_addr ia46_addr4;
114 };
115
116 #ifdef BSD_KERNEL_PRIVATE
117 /*
118 * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
119 * of the structure. Therefore, it is important that the members in
120 * that position not contain any information which is required to be
121 * stable.
122 */
123 struct icmp6_filter;
124 #if CONFIG_MACF_NET
125 struct label;
126 #endif
127 struct ifnet;
128
129 struct inp_stat {
130 u_int64_t rxpackets;
131 u_int64_t rxbytes;
132 u_int64_t txpackets;
133 u_int64_t txbytes;
134 };
135
136 /*
137 * struct inpcb captures the network layer state for TCP, UDP and raw IPv6
138 * and IPv6 sockets. In the case of TCP, further per-connection state is
139 * hung off of inp_ppcb most of the time.
140 */
141 struct inpcb {
142 decl_lck_mtx_data(, inpcb_mtx); /* inpcb per-socket mutex */
143 LIST_ENTRY(inpcb) inp_hash; /* hash list */
144 LIST_ENTRY(inpcb) inp_list; /* list for all PCBs of this proto */
145 void *inp_ppcb; /* pointer to per-protocol pcb */
146 struct inpcbinfo *inp_pcbinfo; /* PCB list info */
147 struct socket *inp_socket; /* back pointer to socket */
148 LIST_ENTRY(inpcb) inp_portlist; /* list for this PCB's local port */
149 RB_ENTRY(inpcb) infc_link; /* link for flowhash RB tree */
150 struct inpcbport *inp_phd; /* head of this list */
151 inp_gen_t inp_gencnt; /* generation count of this instance */
152 int inp_hash_element; /* array index of pcb's hash list */
153 int inp_wantcnt; /* wanted count; atomically updated */
154 int inp_state; /* state (INUSE/CACHED/DEAD) */
155 u_short inp_fport; /* foreign port */
156 u_short inp_lport; /* local port */
157 u_int32_t inp_flags; /* generic IP/datagram flags */
158 u_int32_t inp_flags2; /* generic IP/datagram flags #2 */
159 u_int32_t inp_flow; /* IPv6 flow information */
160
161 u_char inp_sndinprog_cnt; /* outstanding send operations */
162 u_char inp_vflag; /* INP_IPV4 or INP_IPV6 */
163
164 u_char inp_ip_ttl; /* time to live proto */
165 u_char inp_ip_p; /* protocol proto */
166
167 struct ifnet *inp_boundifp; /* interface for INP_BOUND_IF */
168 struct ifnet *inp_last_outifp; /* last known outgoing interface */
169 u_int32_t inp_flowhash; /* flow hash */
170
171 /* Protocol-dependent part */
172 union {
173 /* foreign host table entry */
174 struct in_addr_4in6 inp46_foreign;
175 struct in6_addr inp6_foreign;
176 } inp_dependfaddr;
177 union {
178 /* local host table entry */
179 struct in_addr_4in6 inp46_local;
180 struct in6_addr inp6_local;
181 } inp_dependladdr;
182 union {
183 /* placeholder for routing entry */
184 struct route inp4_route;
185 struct route_in6 inp6_route;
186 } inp_dependroute;
187 struct {
188 /* type of service proto */
189 u_char inp4_ip_tos;
190 /* IP options */
191 struct mbuf *inp4_options;
192 /* IP multicast options */
193 struct ip_moptions *inp4_moptions;
194 } inp_depend4;
195 struct {
196 /* IP options */
197 struct mbuf *inp6_options;
198 /* IP6 options for outgoing packets */
199 struct ip6_pktopts *inp6_outputopts;
200 /* IP multicast options */
201 struct ip6_moptions *inp6_moptions;
202 /* ICMPv6 code type filter */
203 struct icmp6_filter *inp6_icmp6filt;
204 /* IPV6_CHECKSUM setsockopt */
205 int inp6_cksum;
206 short inp6_hops;
207 } inp_depend6;
208
209 caddr_t inp_saved_ppcb; /* place to save pointer while cached */
210 #if CONFIG_MACF_NET
211 struct label *inp_label; /* MAC label */
212 #endif
213 #if IPSEC
214 struct inpcbpolicy *inp_sp; /* for IPSec */
215 #endif /* IPSEC */
216 #if NECP
217 struct {
218 char *inp_domain;
219 char *inp_account;
220 } inp_necp_attributes;
221 struct necp_inpcb_result inp_policyresult;
222 #endif
223 u_char *inp_keepalive_data; /* for keepalive offload */
224 u_int8_t inp_keepalive_datalen; /* keepalive data length */
225 u_int8_t inp_keepalive_type; /* type of application */
226 u_int16_t inp_keepalive_interval; /* keepalive interval */
227 uint32_t inp_nstat_refcnt __attribute__((aligned(4)));
228 struct inp_stat *inp_stat;
229 struct inp_stat *inp_cstat; /* cellular data */
230 struct inp_stat *inp_wstat; /* Wi-Fi data */
231 struct inp_stat *inp_Wstat; /* Wired data */
232 u_int8_t inp_stat_store[sizeof (struct inp_stat) + sizeof (u_int64_t)];
233 u_int8_t inp_cstat_store[sizeof (struct inp_stat) + sizeof (u_int64_t)];
234 u_int8_t inp_wstat_store[sizeof (struct inp_stat) + sizeof (u_int64_t)];
235 u_int8_t inp_Wstat_store[sizeof (struct inp_stat) + sizeof (u_int64_t)];
236 };
237
238 #define INP_ADD_STAT(_inp, _cnt_cellular, _cnt_wifi, _cnt_wired, _a, _n)\
239 do { \
240 locked_add_64(&((_inp)->inp_stat->_a), (_n)); \
241 if (_cnt_cellular) \
242 locked_add_64(&((_inp)->inp_cstat->_a), (_n)); \
243 if (_cnt_wifi) \
244 locked_add_64(&((_inp)->inp_wstat->_a), (_n)); \
245 if (_cnt_wired) \
246 locked_add_64(&((_inp)->inp_Wstat->_a), (_n)); \
247 } while (0);
248 #endif /* BSD_KERNEL_PRIVATE */
249
250 /*
251 * Interface exported to userland by various protocols which use
252 * inpcbs. Hack alert -- only define if struct xsocket is in scope.
253 */
254 #pragma pack(4)
255
256 #if defined(__LP64__)
257 struct _inpcb_list_entry {
258 u_int32_t le_next;
259 u_int32_t le_prev;
260 };
261 #define _INPCB_PTR(x) u_int32_t
262 #define _INPCB_LIST_ENTRY(x) struct _inpcb_list_entry
263 #else /* !__LP64__ */
264 #define _INPCB_PTR(x) x
265 #define _INPCB_LIST_ENTRY(x) LIST_ENTRY(x)
266 #endif /* !__LP64__ */
267
268 #ifdef XNU_KERNEL_PRIVATE
269 /*
270 * This is a copy of the inpcb as it shipped in Panther. This structure
271 * is filled out in a copy function. This allows the inpcb to change
272 * without breaking userland tools.
273 *
274 * CAUTION: Many fields may not be filled out. Fewer may be filled out
275 * in the future. Code defensively.
276 */
277 struct inpcb_compat {
278 #else
279 struct inpcbinfo;
280 struct inpcbport;
281 struct mbuf;
282 struct ip6_pktopts;
283 struct ip6_moptions;
284 struct icmp6_filter;
285 struct inpcbpolicy;
286
287 struct inpcb {
288 #endif /* KERNEL_PRIVATE */
289 _INPCB_LIST_ENTRY(inpcb) inp_hash; /* hash list */
290 struct in_addr reserved1; /* reserved */
291 struct in_addr reserved2; /* reserved */
292 u_short inp_fport; /* foreign port */
293 u_short inp_lport; /* local port */
294 _INPCB_LIST_ENTRY(inpcb) inp_list; /* list for all peer PCBs */
295 _INPCB_PTR(caddr_t) inp_ppcb; /* per-protocol pcb */
296 _INPCB_PTR(struct inpcbinfo *) inp_pcbinfo; /* PCB list info */
297 _INPCB_PTR(void *) inp_socket; /* back pointer to socket */
298 u_char nat_owner; /* Used to NAT TCP/UDP traffic */
299 u_int32_t nat_cookie; /* Cookie stored and returned to NAT */
300 _INPCB_LIST_ENTRY(inpcb) inp_portlist; /* this PCB's local port list */
301 _INPCB_PTR(struct inpcbport *) inp_phd; /* head of this list */
302 inp_gen_t inp_gencnt; /* generation count of this instance */
303 int inp_flags; /* generic IP/datagram flags */
304 u_int32_t inp_flow;
305
306 u_char inp_vflag;
307
308 u_char inp_ip_ttl; /* time to live proto */
309 u_char inp_ip_p; /* protocol proto */
310 /* protocol dependent part */
311 union {
312 /* foreign host table entry */
313 struct in_addr_4in6 inp46_foreign;
314 struct in6_addr inp6_foreign;
315 } inp_dependfaddr;
316 union {
317 /* local host table entry */
318 struct in_addr_4in6 inp46_local;
319 struct in6_addr inp6_local;
320 } inp_dependladdr;
321 union {
322 /* placeholder for routing entry */
323 u_char inp4_route[20];
324 u_char inp6_route[32];
325 } inp_dependroute;
326 struct {
327 /* type of service proto */
328 u_char inp4_ip_tos;
329 /* IP options */
330 _INPCB_PTR(struct mbuf *) inp4_options;
331 /* IP multicast options */
332 _INPCB_PTR(struct ip_moptions *) inp4_moptions;
333 } inp_depend4;
334
335 struct {
336 /* IP options */
337 _INPCB_PTR(struct mbuf *) inp6_options;
338 u_int8_t inp6_hlim;
339 u_int8_t unused_uint8_1;
340 ushort unused_uint16_1;
341 /* IP6 options for outgoing packets */
342 _INPCB_PTR(struct ip6_pktopts *) inp6_outputopts;
343 /* IP multicast options */
344 _INPCB_PTR(struct ip6_moptions *) inp6_moptions;
345 /* ICMPv6 code type filter */
346 _INPCB_PTR(struct icmp6_filter *) inp6_icmp6filt;
347 /* IPV6_CHECKSUM setsockopt */
348 int inp6_cksum;
349 u_short inp6_ifindex;
350 short inp6_hops;
351 } inp_depend6;
352
353 int hash_element; /* Array index of pcb's hash list */
354 _INPCB_PTR(caddr_t) inp_saved_ppcb; /* pointer while cached */
355 _INPCB_PTR(struct inpcbpolicy *) inp_sp;
356 u_int32_t reserved[3]; /* reserved */
357 };
358
359 struct xinpcb {
360 u_int32_t xi_len; /* length of this structure */
361 #ifdef XNU_KERNEL_PRIVATE
362 struct inpcb_compat xi_inp;
363 #else
364 struct inpcb xi_inp;
365 #endif
366 struct xsocket xi_socket;
367 u_quad_t xi_alignment_hack;
368 };
369
370 struct inpcb64_list_entry {
371 u_int64_t le_next;
372 u_int64_t le_prev;
373 };
374
375 struct xinpcb64 {
376 u_int64_t xi_len; /* length of this structure */
377 u_int64_t xi_inpp;
378 u_short inp_fport; /* foreign port */
379 u_short inp_lport; /* local port */
380 struct inpcb64_list_entry inp_list; /* list for all PCBs */
381 u_int64_t inp_ppcb; /* ptr to per-protocol PCB */
382 u_int64_t inp_pcbinfo; /* PCB list info */
383 struct inpcb64_list_entry inp_portlist; /* this PCB's local port list */
384 u_int64_t inp_phd; /* head of this list */
385 inp_gen_t inp_gencnt; /* current generation count */
386 int inp_flags; /* generic IP/datagram flags */
387 u_int32_t inp_flow;
388 u_char inp_vflag;
389 u_char inp_ip_ttl; /* time to live */
390 u_char inp_ip_p; /* protocol */
391 union { /* foreign host table entry */
392 struct in_addr_4in6 inp46_foreign;
393 struct in6_addr inp6_foreign;
394 } inp_dependfaddr;
395 union { /* local host table entry */
396 struct in_addr_4in6 inp46_local;
397 struct in6_addr inp6_local;
398 } inp_dependladdr;
399 struct {
400 u_char inp4_ip_tos; /* type of service */
401 } inp_depend4;
402 struct {
403 u_int8_t inp6_hlim;
404 int inp6_cksum;
405 u_short inp6_ifindex;
406 short inp6_hops;
407 } inp_depend6;
408 struct xsocket64 xi_socket;
409 u_quad_t xi_alignment_hack;
410 };
411
412 #ifdef PRIVATE
413 struct xinpcb_list_entry {
414 u_int64_t le_next;
415 u_int64_t le_prev;
416 };
417
418 struct xinpcb_n {
419 u_int32_t xi_len; /* length of this structure */
420 u_int32_t xi_kind; /* XSO_INPCB */
421 u_int64_t xi_inpp;
422 u_short inp_fport; /* foreign port */
423 u_short inp_lport; /* local port */
424 u_int64_t inp_ppcb; /* pointer to per-protocol pcb */
425 inp_gen_t inp_gencnt; /* generation count of this instance */
426 int inp_flags; /* generic IP/datagram flags */
427 u_int32_t inp_flow;
428 u_char inp_vflag;
429 u_char inp_ip_ttl; /* time to live */
430 u_char inp_ip_p; /* protocol */
431 union { /* foreign host table entry */
432 struct in_addr_4in6 inp46_foreign;
433 struct in6_addr inp6_foreign;
434 } inp_dependfaddr;
435 union { /* local host table entry */
436 struct in_addr_4in6 inp46_local;
437 struct in6_addr inp6_local;
438 } inp_dependladdr;
439 struct {
440 u_char inp4_ip_tos; /* type of service */
441 } inp_depend4;
442 struct {
443 u_int8_t inp6_hlim;
444 int inp6_cksum;
445 u_short inp6_ifindex;
446 short inp6_hops;
447 } inp_depend6;
448 u_int32_t inp_flowhash;
449 u_int32_t inp_flags2;
450 };
451 #endif /* PRIVATE */
452
453 struct xinpgen {
454 u_int32_t xig_len; /* length of this structure */
455 u_int xig_count; /* number of PCBs at this time */
456 inp_gen_t xig_gen; /* generation count at this time */
457 so_gen_t xig_sogen; /* current socket generation count */
458 };
459
460 #pragma pack()
461
462 /*
463 * These defines are for use with the inpcb.
464 */
465 #define INP_IPV4 0x1
466 #define INP_IPV6 0x2
467 #define inp_faddr inp_dependfaddr.inp46_foreign.ia46_addr4
468 #define inp_laddr inp_dependladdr.inp46_local.ia46_addr4
469 #define in6p_faddr inp_dependfaddr.inp6_foreign
470 #define in6p_laddr inp_dependladdr.inp6_local
471
472 #ifdef BSD_KERNEL_PRIVATE
473 #define inp_route inp_dependroute.inp4_route
474 #define inp_ip_tos inp_depend4.inp4_ip_tos
475 #define inp_options inp_depend4.inp4_options
476 #define inp_moptions inp_depend4.inp4_moptions
477 #define in6p_route inp_dependroute.inp6_route
478 #define in6p_ip6_hlim inp_depend6.inp6_hlim
479 #define in6p_hops inp_depend6.inp6_hops /* default hop limit */
480 #define in6p_ip6_nxt inp_ip_p
481 #define in6p_vflag inp_vflag
482 #define in6p_options inp_depend6.inp6_options
483 #define in6p_outputopts inp_depend6.inp6_outputopts
484 #define in6p_moptions inp_depend6.inp6_moptions
485 #define in6p_icmp6filt inp_depend6.inp6_icmp6filt
486 #define in6p_cksum inp_depend6.inp6_cksum
487 #define in6p_ifindex inp_depend6.inp6_ifindex
488 #define in6p_flags inp_flags
489 #define in6p_flags2 inp_flags2
490 #define in6p_socket inp_socket
491 #define in6p_lport inp_lport
492 #define in6p_fport inp_fport
493 #define in6p_ppcb inp_ppcb
494 #define in6p_state inp_state
495 #define in6p_wantcnt inp_wantcnt
496 #define in6p_last_outifp inp_last_outifp
497 #define in6pcb inpcb
498 #if IPSEC
499 #define in6p_sp inp_sp
500 #endif /* IPSEC */
501 #define INP_INC_IFNET_STAT(_inp_, _stat_) { \
502 if ((_inp_)->inp_last_outifp != NULL) { \
503 if ((_inp_)->inp_vflag & INP_IPV6) { \
504 (_inp_)->inp_last_outifp->if_ipv6_stat->_stat_++;\
505 } else { \
506 (_inp_)->inp_last_outifp->if_ipv4_stat->_stat_++;\
507 }\
508 }\
509 }
510
511 struct inpcbport {
512 LIST_ENTRY(inpcbport) phd_hash;
513 struct inpcbhead phd_pcblist;
514 u_short phd_port;
515 };
516
517 struct intimercount {
518 u_int32_t intimer_lazy; /* lazy requests for timer scheduling */
519 u_int32_t intimer_fast; /* fast requests, can be coalesced */
520 u_int32_t intimer_nodelay; /* fast requests, never coalesced */
521 };
522
523 typedef void (*inpcb_timer_func_t)(struct inpcbinfo *);
524
525 /*
526 * Global data structure for each high-level protocol (UDP, TCP, ...) in both
527 * IPv4 and IPv6. Holds inpcb lists and information for managing them. Each
528 * pcbinfo is protected by a RW lock: ipi_lock.
529 *
530 * All INPCB pcbinfo entries are linked together via ipi_entry.
531 */
532 struct inpcbinfo {
533 /*
534 * Glue to all PCB infos, as well as garbage collector and
535 * timer callbacks, protected by inpcb_lock. Callout request
536 * counts are atomically updated.
537 */
538 TAILQ_ENTRY(inpcbinfo) ipi_entry;
539 inpcb_timer_func_t ipi_gc;
540 inpcb_timer_func_t ipi_timer;
541 struct intimercount ipi_gc_req;
542 struct intimercount ipi_timer_req;
543
544 /*
545 * Per-protocol lock protecting pcb list, pcb count, etc.
546 */
547 lck_rw_t *ipi_lock;
548
549 /*
550 * List and count of pcbs on the protocol.
551 */
552 struct inpcbhead *ipi_listhead;
553 uint32_t ipi_count;
554
555 /*
556 * Count of pcbs marked with INP2_TIMEWAIT flag.
557 */
558 uint32_t ipi_twcount;
559
560 /*
561 * Generation count -- incremented each time a connection is
562 * allocated or freed.
563 */
564 uint64_t ipi_gencnt;
565
566 /*
567 * Fields associated with port lookup and allocation.
568 */
569 uint16_t ipi_lastport;
570 uint16_t ipi_lastlow;
571 uint16_t ipi_lasthi;
572
573 /*
574 * Zone from which inpcbs are allocated for this protocol.
575 */
576 struct zone *ipi_zone;
577
578 /*
579 * Per-protocol hash of pcbs, hashed by local and foreign
580 * addresses and port numbers.
581 */
582 struct inpcbhead *ipi_hashbase;
583 u_long ipi_hashmask;
584
585 /*
586 * Per-protocol hash of pcbs, hashed by only local port number.
587 */
588 struct inpcbporthead *ipi_porthashbase;
589 u_long ipi_porthashmask;
590
591 /*
592 * Misc.
593 */
594 lck_attr_t *ipi_lock_attr;
595 lck_grp_t *ipi_lock_grp;
596 lck_grp_attr_t *ipi_lock_grp_attr;
597 };
598
599 #define INP_PCBHASH(faddr, lport, fport, mask) \
600 (((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
601 #define INP_PCBPORTHASH(lport, mask) \
602 (ntohs((lport)) & (mask))
603
604 #define INP_IS_FLOW_CONTROLLED(_inp_) \
605 ((_inp_)->inp_flags & INP_FLOW_CONTROLLED)
606 #define INP_IS_FLOW_SUSPENDED(_inp_) \
607 (((_inp_)->inp_flags & INP_FLOW_SUSPENDED) || \
608 ((_inp_)->inp_socket->so_flags & SOF_SUSPENDED))
609 #define INP_WAIT_FOR_IF_FEEDBACK(_inp_) \
610 (((_inp_)->inp_flags & (INP_FLOW_CONTROLLED | INP_FLOW_SUSPENDED)) != 0)
611
612 #define INP_NO_CELLULAR(_inp) \
613 ((_inp)->inp_flags & INP_NO_IFT_CELLULAR)
614 #define INP_NO_EXPENSIVE(_inp) \
615 ((_inp)->inp_flags2 & INP2_NO_IFF_EXPENSIVE)
616 #define INP_AWDL_UNRESTRICTED(_inp) \
617 ((_inp)->inp_flags2 & INP2_AWDL_UNRESTRICTED)
618
619 #endif /* BSD_KERNEL_PRIVATE */
620
621 /*
622 * Flags for inp_flags.
623 *
624 * Some of these are publicly defined for legacy reasons, as they are
625 * (unfortunately) used by certain applications to determine, at compile
626 * time, whether or not the OS supports certain features.
627 */
628 #ifdef BSD_KERNEL_PRIVATE
629 #define INP_RECVOPTS 0x00000001 /* receive incoming IP options */
630 #define INP_RECVRETOPTS 0x00000002 /* receive IP options for reply */
631 #define INP_RECVDSTADDR 0x00000004 /* receive IP dst address */
632 #define INP_HDRINCL 0x00000008 /* user supplies entire IP header */
633 #define INP_HIGHPORT 0x00000010 /* user wants "high" port binding */
634 #define INP_LOWPORT 0x00000020 /* user wants "low" port binding */
635 #endif /* BSD_KERNEL_PRIVATE */
636
637 #define INP_ANONPORT 0x00000040 /* port chosen for user */
638
639 #ifdef BSD_KERNEL_PRIVATE
640 #define INP_RECVIF 0x00000080 /* receive incoming interface */
641 #define INP_MTUDISC 0x00000100 /* unused */
642 #define INP_STRIPHDR 0x00000200 /* strip hdrs in raw_ip (for OT) */
643 #define INP_RECV_ANYIF 0x00000400 /* don't restrict inbound iface */
644 #define INP_INADDR_ANY 0x00000800 /* local address wasn't specified */
645 #define INP_IN6ADDR_ANY INP_INADDR_ANY
646 #define INP_RECVTTL 0x00001000 /* receive incoming IP TTL */
647 #define INP_UDP_NOCKSUM 0x00002000 /* turn off outbound UDP checksum */
648 #define INP_BOUND_IF 0x00004000 /* bind socket to an interface */
649 #endif /* BSD_KERNEL_PRIVATE */
650
651 #define IN6P_IPV6_V6ONLY 0x00008000 /* restrict AF_INET6 socket for v6 */
652
653 #ifdef BSD_KERNEL_PRIVATE
654 #define IN6P_PKTINFO 0x00010000 /* receive IP6 dst and I/F */
655 #define IN6P_HOPLIMIT 0x00020000 /* receive hoplimit */
656 #define IN6P_HOPOPTS 0x00040000 /* receive hop-by-hop options */
657 #define IN6P_DSTOPTS 0x00080000 /* receive dst options after rthdr */
658 #define IN6P_RTHDR 0x00100000 /* receive routing header */
659 #define IN6P_RTHDRDSTOPTS 0x00200000 /* receive dstoptions before rthdr */
660 #define IN6P_TCLASS 0x00400000 /* receive traffic class value */
661 #define IN6P_AUTOFLOWLABEL 0x00800000 /* attach flowlabel automatically */
662 #endif /* BSD_KERNEL_PRIVATE */
663
664 #define IN6P_BINDV6ONLY 0x01000000 /* do not grab IPv4 traffic */
665
666 #ifdef BSD_KERNEL_PRIVATE
667 #define IN6P_RFC2292 0x02000000 /* used RFC2292 API on the socket */
668 #define IN6P_MTU 0x04000000 /* receive path MTU */
669 #define INP_PKTINFO 0x08000000 /* rcv and snd PKTINFO for IPv4 */
670 #define INP_FLOW_SUSPENDED 0x10000000 /* flow suspended */
671 #define INP_NO_IFT_CELLULAR 0x20000000 /* do not use cellular interface */
672 #define INP_FLOW_CONTROLLED 0x40000000 /* flow controlled */
673 #define INP_FC_FEEDBACK 0x80000000 /* got interface flow adv feedback */
674
675 #define INP_CONTROLOPTS \
676 (INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|INP_RECVIF|INP_RECVTTL| \
677 INP_PKTINFO|IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|IN6P_DSTOPTS| \
678 IN6P_RTHDR|IN6P_RTHDRDSTOPTS|IN6P_TCLASS|IN6P_RFC2292|IN6P_MTU)
679
680 #define INP_UNMAPPABLEOPTS \
681 (IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR| IN6P_TCLASS|IN6P_AUTOFLOWLABEL)
682
683 /*
684 * Flags for inp_flags2.
685 *
686 * Overflowed INP flags; use INP2 prefix to avoid misuse.
687 */
688 #define INP2_TIMEWAIT 0x00000001 /* in TIMEWAIT */
689 #define INP2_IN_FCTREE 0x00000002 /* in inp_fc_tree */
690 #define INP2_WANT_APP_POLICY 0x00000004 /* necp app policy check is desired */
691 #define INP2_NO_IFF_EXPENSIVE 0x00000008 /* do not use expensive interface */
692 #define INP2_INHASHLIST 0x00000010 /* pcb is in inp_hash list */
693 #define INP2_AWDL_UNRESTRICTED 0x00000020 /* AWDL restricted mode allowed */
694 #define INP2_KEEPALIVE_OFFLOAD 0x00000040 /* Enable UDP keepalive offload */
695
696 /*
697 * Flags passed to in_pcblookup*() functions.
698 */
699 #define INPLOOKUP_WILDCARD 1
700
701 #define sotoinpcb(so) ((struct inpcb *)(so)->so_pcb)
702 #define sotoin6pcb(so) sotoinpcb(so)
703
704 struct sysctl_req;
705
706 extern int ipport_lowfirstauto;
707 extern int ipport_lowlastauto;
708 extern int ipport_firstauto;
709 extern int ipport_lastauto;
710 extern int ipport_hifirstauto;
711 extern int ipport_hilastauto;
712
713 /* freshly allocated PCB, it's in use */
714 #define INPCB_STATE_INUSE 0x1
715 /* this pcb is sitting in a a cache */
716 #define INPCB_STATE_CACHED 0x2
717 /* should treat as gone, will be garbage collected and freed */
718 #define INPCB_STATE_DEAD 0x3
719
720 /* marked as ready to be garbaged collected, should be treated as not found */
721 #define WNT_STOPUSING 0xffff
722 /* that pcb is being acquired, do not recycle this time */
723 #define WNT_ACQUIRE 0x1
724 /* release acquired mode, can be garbage collected when wantcnt is null */
725 #define WNT_RELEASE 0x2
726
727 extern void in_pcbinit(void);
728 extern void in_pcbinfo_attach(struct inpcbinfo *);
729 extern int in_pcbinfo_detach(struct inpcbinfo *);
730
731 /* type of timer to be scheduled by inpcb_gc_sched and inpcb_timer_sched */
732 enum {
733 INPCB_TIMER_LAZY = 0x1,
734 INPCB_TIMER_FAST,
735 INPCB_TIMER_NODELAY
736 };
737 extern void inpcb_gc_sched(struct inpcbinfo *, u_int32_t type);
738 extern void inpcb_timer_sched(struct inpcbinfo *, u_int32_t type);
739
740 extern void in_losing(struct inpcb *);
741 extern void in_rtchange(struct inpcb *, int);
742 extern int in_pcballoc(struct socket *, struct inpcbinfo *, struct proc *);
743 extern int in_pcbbind(struct inpcb *, struct sockaddr *, struct proc *);
744 extern int in_pcbconnect(struct inpcb *, struct sockaddr *, struct proc *,
745 unsigned int, struct ifnet **);
746 extern void in_pcbdetach(struct inpcb *);
747 extern void in_pcbdispose(struct inpcb *);
748 extern void in_pcbdisconnect(struct inpcb *);
749 extern int in_pcbinshash(struct inpcb *, int);
750 extern int in_pcbladdr(struct inpcb *, struct sockaddr *, struct in_addr *,
751 unsigned int, struct ifnet **);
752 extern struct inpcb *in_pcblookup_local(struct inpcbinfo *, struct in_addr,
753 u_int, int);
754 extern struct inpcb *in_pcblookup_local_and_cleanup(struct inpcbinfo *,
755 struct in_addr, u_int, int);
756 extern struct inpcb *in_pcblookup_hash(struct inpcbinfo *, struct in_addr,
757 u_int, struct in_addr, u_int, int, struct ifnet *);
758 extern int in_pcblookup_hash_exists(struct inpcbinfo *, struct in_addr,
759 u_int, struct in_addr, u_int, int, uid_t *, gid_t *, struct ifnet *);
760 extern void in_pcbnotifyall(struct inpcbinfo *, struct in_addr, int,
761 void (*)(struct inpcb *, int));
762 extern void in_pcbrehash(struct inpcb *);
763 extern int in_getpeeraddr(struct socket *, struct sockaddr **);
764 extern int in_getpeeraddr_s(struct socket *, struct sockaddr_storage *);
765 extern int in_getsockaddr(struct socket *, struct sockaddr **);
766 extern int in_getsockaddr_s(struct socket *, struct sockaddr_storage *);
767 extern int in_pcb_checkstate(struct inpcb *, int, int);
768 extern void in_pcbremlists(struct inpcb *);
769 extern void inpcb_to_compat(struct inpcb *, struct inpcb_compat *);
770 extern void inpcb_to_xinpcb64(struct inpcb *, struct xinpcb64 *);
771
772 extern int get_pcblist_n(short, struct sysctl_req *, struct inpcbinfo *);
773 #define INPCB_GET_PORTS_USED_WILDCARDOK 0x01
774 #define INPCB_GET_PORTS_USED_NOWAKEUPOK 0x02
775 #define INPCB_GET_PORTS_USED_RECVANYIFONLY 0x04
776 #define INPCB_GET_PORTS_USED_EXTBGIDLEONLY 0x08
777 #define INPCB_GET_PORTS_USED_ACTIVEONLY 0x10
778
779 extern void inpcb_get_ports_used(u_int32_t, int, u_int32_t, bitstr_t *,
780 struct inpcbinfo *);
781 #define INPCB_OPPORTUNISTIC_THROTTLEON 0x0001
782 #define INPCB_OPPORTUNISTIC_SETCMD 0x0002
783 extern uint32_t inpcb_count_opportunistic(unsigned int, struct inpcbinfo *,
784 u_int32_t);
785 extern uint32_t inpcb_find_anypcb_byaddr(struct ifaddr *, struct inpcbinfo *);
786 extern void inp_route_copyout(struct inpcb *, struct route *);
787 extern void inp_route_copyin(struct inpcb *, struct route *);
788 extern int inp_bindif(struct inpcb *, unsigned int, struct ifnet **);
789 extern void inp_set_nocellular(struct inpcb *);
790 extern void inp_clear_nocellular(struct inpcb *);
791 extern void inp_set_noexpensive(struct inpcb *);
792 extern void inp_set_awdl_unrestricted(struct inpcb *);
793 extern boolean_t inp_get_awdl_unrestricted(struct inpcb *);
794 extern void inp_clear_awdl_unrestricted(struct inpcb *);
795 #if NECP
796 extern void inp_update_necp_policy(struct inpcb *, struct sockaddr *, struct sockaddr *, u_int);
797 extern void inp_set_want_app_policy(struct inpcb *);
798 extern void inp_clear_want_app_policy(struct inpcb *);
799 #endif /* NECP */
800 extern u_int32_t inp_calc_flowhash(struct inpcb *);
801 extern void inp_reset_fc_state(struct inpcb *);
802 extern int inp_set_fc_state(struct inpcb *, int advcode);
803 extern void inp_fc_unthrottle_tcp(struct inpcb *);
804 extern void inp_flowadv(uint32_t);
805 extern int inp_flush(struct inpcb *, int);
806 extern int inp_findinpcb_procinfo(struct inpcbinfo *, uint32_t, struct so_procinfo *);
807 extern void inp_get_soprocinfo(struct inpcb *, struct so_procinfo *);
808 extern int inp_update_policy(struct inpcb *);
809 extern boolean_t inp_restricted_recv(struct inpcb *, struct ifnet *);
810 extern boolean_t inp_restricted_send(struct inpcb *, struct ifnet *);
811 #endif /* BSD_KERNEL_PRIVATE */
812 #ifdef KERNEL_PRIVATE
813 /* exported for PPP */
814 extern void inp_clear_INP_INADDR_ANY(struct socket *);
815 #endif /* KERNEL_PRIVATE */
816 #endif /* !_NETINET_IN_PCB_H_ */