1/*
2 * Copyright (c) 2000-2013 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
85#ifdef BSD_KERNEL_PRIVATE
86/*
87 * struct inpcb is the common protocol control block structure used in most
88 * IP transport protocols.
89 *
90 * Pointers to local and foreign host table entries, local and foreign socket
91 * numbers, and pointers up (to a socket structure) and down (to a
92 * protocol-specific control block) are stored here.
93 */
94LIST_HEAD(inpcbhead, inpcb);
95LIST_HEAD(inpcbporthead, inpcbport);
96#endif /* BSD_KERNEL_PRIVATE */
97typedef	u_quad_t	inp_gen_t;
98
99/*
100 * PCB with AF_INET6 null bind'ed laddr can receive AF_INET input packet.
101 * So, AF_INET6 null laddr is also used as AF_INET null laddr, by utilizing
102 * the following structure.
103 */
104struct in_addr_4in6 {
105	u_int32_t	ia46_pad32[3];
106	struct	in_addr	ia46_addr4;
107};
108
109#ifdef BSD_KERNEL_PRIVATE
110/*
111 * NB: the zone allocator is type-stable EXCEPT FOR THE FIRST TWO LONGS
112 * of the structure.  Therefore, it is important that the members in
113 * that position not contain any information which is required to be
114 * stable.
115 */
116struct	icmp6_filter;
117#if CONFIG_MACF_NET
118struct	label;
119#endif
120struct ifnet;
121
122struct inp_stat {
123	u_int64_t	rxpackets;
124	u_int64_t	rxbytes;
125	u_int64_t	txpackets;
126	u_int64_t	txbytes;
127};
128
129/*
130 * struct inpcb captures the network layer state for TCP, UDP and raw IPv6
131 * and IPv6 sockets.  In the case of TCP, further per-connection state is
132 * hung off of inp_ppcb most of the time.
133 */
134struct inpcb {
135	decl_lck_mtx_data(, inpcb_mtx);	/* inpcb per-socket mutex */
136	LIST_ENTRY(inpcb) inp_hash;	/* hash list */
137	LIST_ENTRY(inpcb) inp_list;	/* list for all PCBs of this proto */
138	void	*inp_ppcb;		/* pointer to per-protocol pcb */
139	struct inpcbinfo *inp_pcbinfo;	/* PCB list info */
140	struct socket *inp_socket;	/* back pointer to socket */
141	LIST_ENTRY(inpcb) inp_portlist;	/* list for this PCB's local port */
142	RB_ENTRY(inpcb) infc_link;	/* link for flowhash RB tree */
143	struct inpcbport *inp_phd;	/* head of this list */
144	inp_gen_t inp_gencnt;		/* generation count of this instance */
145	int	inp_hash_element;	/* array index of pcb's hash list */
146	int	inp_wantcnt;		/* wanted count; atomically updated */
147	int	inp_state;		/* state (INUSE/CACHED/DEAD) */
148	u_short	inp_fport;		/* foreign port */
149	u_short	inp_lport;		/* local port */
150	u_int32_t inp_flags;		/* generic IP/datagram flags */
151	u_int32_t inp_flags2;		/* generic IP/datagram flags #2 */
152	u_int32_t inp_flow;		/* IPv6 flow information */
153
154	u_char	inp_sndinprog_cnt;	/* outstanding send operations */
155	u_char	inp_vflag;		/* INP_IPV4 or INP_IPV6 */
156
157	u_char inp_ip_ttl;		/* time to live proto */
158	u_char inp_ip_p;		/* protocol proto */
159
160	struct ifnet *inp_boundifp;	/* interface for INP_BOUND_IF */
161	struct ifnet *inp_last_outifp;	/* last known outgoing interface */
162	u_int32_t inp_flowhash;		/* flow hash */
163
164	/* Protocol-dependent part */
165	union {
166		/* foreign host table entry */
167		struct in_addr_4in6 inp46_foreign;
168		struct in6_addr inp6_foreign;
169	} inp_dependfaddr;
170	union {
171		/* local host table entry */
172		struct in_addr_4in6 inp46_local;
173		struct in6_addr inp6_local;
174	} inp_dependladdr;
175	union {
176		/* placeholder for routing entry */
177		struct route inp4_route;
178		struct route_in6 inp6_route;
179	} inp_dependroute;
180	struct {
181		/* type of service proto */
182		u_char inp4_ip_tos;
183		/* IP options */
184		struct mbuf *inp4_options;
185		/* IP multicast options */
186		struct ip_moptions *inp4_moptions;
187	} inp_depend4;
188	struct {
189		/* IP options */
190		struct mbuf *inp6_options;
191		/* IP6 options for outgoing packets */
192		struct	ip6_pktopts *inp6_outputopts;
193		/* IP multicast options */
194		struct	ip6_moptions *inp6_moptions;
195		/* ICMPv6 code type filter */
196		struct	icmp6_filter *inp6_icmp6filt;
197		/* IPV6_CHECKSUM setsockopt */
198		int	inp6_cksum;
199		short	inp6_hops;
200	} inp_depend6;
201
202	caddr_t inp_saved_ppcb;		/* place to save pointer while cached */
203#if CONFIG_MACF_NET
204	struct label *inp_label;	/* MAC label */
205#endif
206#if IPSEC
207	struct inpcbpolicy *inp_sp;	/* for IPSec */
208#endif /* IPSEC */
209	struct inp_stat	*inp_stat;
210	struct inp_stat	*inp_cstat;	/* cellular data */
211	struct inp_stat	*inp_wstat;	/* Wi-Fi data */
212	u_int8_t inp_stat_store[sizeof (struct inp_stat) + sizeof (u_int64_t)];
213	u_int8_t inp_cstat_store[sizeof (struct inp_stat) + sizeof (u_int64_t)];
214	u_int8_t inp_wstat_store[sizeof (struct inp_stat) + sizeof (u_int64_t)];
215};
216
217#define	INP_ADD_STAT(_inp, _cnt_cellular, _cnt_wifi, _a, _n) do {	\
218	locked_add_64(&((_inp)->inp_stat->_a), (_n));			\
219	if (_cnt_cellular)						\
220		locked_add_64(&((_inp)->inp_cstat->_a), (_n));		\
221	if (_cnt_wifi)							\
222		locked_add_64(&((_inp)->inp_wstat->_a), (_n));		\
223} while (0);
224#endif /* BSD_KERNEL_PRIVATE */
225
226/*
227 * Interface exported to userland by various protocols which use
228 * inpcbs.  Hack alert -- only define if struct xsocket is in scope.
229 */
230#pragma pack(4)
231
232#if defined(__LP64__)
233struct _inpcb_list_entry {
234    u_int32_t	le_next;
235    u_int32_t	le_prev;
236};
237#define	_INPCB_PTR(x)		u_int32_t
238#define	_INPCB_LIST_ENTRY(x)	struct _inpcb_list_entry
239#else /* !__LP64__ */
240#define	_INPCB_PTR(x)		x
241#define	_INPCB_LIST_ENTRY(x)	LIST_ENTRY(x)
242#endif /* !__LP64__ */
243
244#ifdef XNU_KERNEL_PRIVATE
245/*
246 * This is a copy of the inpcb as it shipped in Panther. This structure
247 * is filled out in a copy function. This allows the inpcb to change
248 * without breaking userland tools.
249 *
250 * CAUTION: Many fields may not be filled out. Fewer may be filled out
251 * in the future. Code defensively.
252 */
253struct inpcb_compat {
254#else
255struct inpcbinfo;
256struct inpcbport;
257struct mbuf;
258struct ip6_pktopts;
259struct ip6_moptions;
260struct icmp6_filter;
261struct inpcbpolicy;
262
263struct inpcb {
264#endif /* KERNEL_PRIVATE */
265	_INPCB_LIST_ENTRY(inpcb) inp_hash;	/* hash list */
266	struct in_addr reserved1;		/* reserved */
267	struct in_addr reserved2;		/* reserved */
268	u_short	inp_fport;			/* foreign port */
269	u_short	inp_lport;			/* local port */
270	_INPCB_LIST_ENTRY(inpcb) inp_list;	/* list for all peer PCBs */
271	_INPCB_PTR(caddr_t) inp_ppcb;		/* per-protocol pcb */
272	_INPCB_PTR(struct inpcbinfo *) inp_pcbinfo;	/* PCB list info */
273	_INPCB_PTR(void *) inp_socket;	/* back pointer to socket */
274	u_char nat_owner;		/* Used to NAT TCP/UDP traffic */
275	u_int32_t nat_cookie;		/* Cookie stored and returned to NAT */
276	_INPCB_LIST_ENTRY(inpcb) inp_portlist;	/* this PCB's local port list */
277	_INPCB_PTR(struct inpcbport *) inp_phd; /* head of this list */
278	inp_gen_t inp_gencnt;		/* generation count of this instance */
279	int inp_flags;			/* generic IP/datagram flags */
280	u_int32_t inp_flow;
281
282	u_char inp_vflag;
283
284	u_char inp_ip_ttl;		/* time to live proto */
285	u_char inp_ip_p;		/* protocol proto */
286	/* protocol dependent part */
287	union {
288		/* foreign host table entry */
289		struct in_addr_4in6 inp46_foreign;
290		struct in6_addr inp6_foreign;
291	} inp_dependfaddr;
292	union {
293		/* local host table entry */
294		struct in_addr_4in6 inp46_local;
295		struct in6_addr inp6_local;
296	} inp_dependladdr;
297	union {
298		/* placeholder for routing entry */
299		u_char inp4_route[20];
300		u_char inp6_route[32];
301	} inp_dependroute;
302	struct {
303		/* type of service proto */
304		u_char inp4_ip_tos;
305		/* IP options */
306		_INPCB_PTR(struct mbuf *) inp4_options;
307		/* IP multicast options */
308		_INPCB_PTR(struct ip_moptions *) inp4_moptions;
309	} inp_depend4;
310
311	struct {
312		/* IP options */
313		_INPCB_PTR(struct mbuf *) inp6_options;
314		u_int8_t inp6_hlim;
315		u_int8_t unused_uint8_1;
316		ushort unused_uint16_1;
317		/* IP6 options for outgoing packets */
318		_INPCB_PTR(struct ip6_pktopts *) inp6_outputopts;
319		/* IP multicast options */
320		_INPCB_PTR(struct ip6_moptions *) inp6_moptions;
321		/* ICMPv6 code type filter */
322		_INPCB_PTR(struct icmp6_filter *) inp6_icmp6filt;
323		/* IPV6_CHECKSUM setsockopt */
324		int	inp6_cksum;
325		u_short	inp6_ifindex;
326		short	inp6_hops;
327	} inp_depend6;
328
329	int hash_element;		/* Array index of pcb's hash list */
330	_INPCB_PTR(caddr_t) inp_saved_ppcb; /* pointer while cached */
331	_INPCB_PTR(struct inpcbpolicy *) inp_sp;
332	u_int32_t	reserved[3];	/* reserved */
333};
334
335struct	xinpcb {
336	u_int32_t	xi_len;		/* length of this structure */
337#ifdef XNU_KERNEL_PRIVATE
338	struct	inpcb_compat xi_inp;
339#else
340	struct	inpcb xi_inp;
341#endif
342	struct	xsocket xi_socket;
343	u_quad_t	xi_alignment_hack;
344};
345
346struct inpcb64_list_entry {
347    u_int64_t   le_next;
348    u_int64_t   le_prev;
349};
350
351struct	xinpcb64 {
352	u_int64_t	xi_len;		/* length of this structure */
353	u_int64_t	xi_inpp;
354	u_short		inp_fport;	/* foreign port */
355	u_short		inp_lport;	/* local port */
356	struct inpcb64_list_entry inp_list; /* list for all PCBs */
357	u_int64_t	inp_ppcb;	/* ptr to per-protocol PCB */
358	u_int64_t	inp_pcbinfo;	/* PCB list info */
359	struct inpcb64_list_entry inp_portlist;	/* this PCB's local port list */
360	u_int64_t	inp_phd;	/* head of this list */
361	inp_gen_t	inp_gencnt;	/* current generation count */
362	int		inp_flags;	/* generic IP/datagram flags */
363	u_int32_t	inp_flow;
364	u_char		inp_vflag;
365	u_char		inp_ip_ttl;	/* time to live */
366	u_char		inp_ip_p;	/* protocol */
367	union {				/* foreign host table entry */
368		struct  in_addr_4in6	inp46_foreign;
369		struct  in6_addr	inp6_foreign;
370	} inp_dependfaddr;
371	union {				/* local host table entry */
372		struct  in_addr_4in6	inp46_local;
373		struct  in6_addr	inp6_local;
374	} inp_dependladdr;
375	struct {
376		u_char	inp4_ip_tos;	/* type of service */
377	} inp_depend4;
378	struct {
379		u_int8_t inp6_hlim;
380		int	inp6_cksum;
381		u_short	inp6_ifindex;
382		short	inp6_hops;
383	} inp_depend6;
384	struct  xsocket64 xi_socket;
385	u_quad_t	xi_alignment_hack;
386};
387
388#ifdef PRIVATE
389struct xinpcb_list_entry {
390    u_int64_t   le_next;
391    u_int64_t   le_prev;
392};
393
394struct	xinpcb_n {
395	u_int32_t	xi_len;		/* length of this structure */
396	u_int32_t	xi_kind;	/* XSO_INPCB */
397	u_int64_t	xi_inpp;
398	u_short		inp_fport;	/* foreign port */
399	u_short		inp_lport;	/* local port */
400	u_int64_t	inp_ppcb;	/* pointer to per-protocol pcb */
401	inp_gen_t	inp_gencnt;	/* generation count of this instance */
402	int		inp_flags;	/* generic IP/datagram flags */
403	u_int32_t	inp_flow;
404	u_char		inp_vflag;
405	u_char		inp_ip_ttl;	/* time to live */
406	u_char		inp_ip_p;	/* protocol */
407	union {				/* foreign host table entry */
408		struct in_addr_4in6	inp46_foreign;
409		struct in6_addr		inp6_foreign;
410	} inp_dependfaddr;
411	union {				/* local host table entry */
412		struct in_addr_4in6	inp46_local;
413		struct in6_addr		inp6_local;
414	} inp_dependladdr;
415	struct {
416		u_char	inp4_ip_tos;	/* type of service */
417	} inp_depend4;
418	struct {
419		u_int8_t inp6_hlim;
420		int	inp6_cksum;
421		u_short	inp6_ifindex;
422		short	inp6_hops;
423	} inp_depend6;
424	u_int32_t		inp_flowhash;
425};
426#endif /* PRIVATE */
427
428struct	xinpgen {
429	u_int32_t	xig_len;	/* length of this structure */
430	u_int		xig_count;	/* number of PCBs at this time */
431	inp_gen_t	xig_gen;	/* generation count at this time */
432	so_gen_t	xig_sogen;	/* current socket generation count */
433};
434
435#pragma pack()
436
437/*
438 * These defines are for use with the inpcb.
439 */
440#define	INP_IPV4	0x1
441#define	INP_IPV6	0x2
442#define	inp_faddr	inp_dependfaddr.inp46_foreign.ia46_addr4
443#define	inp_laddr	inp_dependladdr.inp46_local.ia46_addr4
444#define	in6p_faddr	inp_dependfaddr.inp6_foreign
445#define	in6p_laddr	inp_dependladdr.inp6_local
446
447#ifdef BSD_KERNEL_PRIVATE
448#define	inp_route	inp_dependroute.inp4_route
449#define	inp_ip_tos	inp_depend4.inp4_ip_tos
450#define	inp_options	inp_depend4.inp4_options
451#define	inp_moptions	inp_depend4.inp4_moptions
452#define	in6p_route	inp_dependroute.inp6_route
453#define	in6p_ip6_hlim	inp_depend6.inp6_hlim
454#define	in6p_hops	inp_depend6.inp6_hops	/* default hop limit */
455#define	in6p_ip6_nxt	inp_ip_p
456#define	in6p_vflag	inp_vflag
457#define	in6p_options	inp_depend6.inp6_options
458#define	in6p_outputopts	inp_depend6.inp6_outputopts
459#define	in6p_moptions	inp_depend6.inp6_moptions
460#define	in6p_icmp6filt	inp_depend6.inp6_icmp6filt
461#define	in6p_cksum	inp_depend6.inp6_cksum
462#define	in6p_ifindex	inp_depend6.inp6_ifindex
463#define	in6p_flags	inp_flags
464#define	in6p_flags2	inp_flags2
465#define	in6p_socket	inp_socket
466#define	in6p_lport	inp_lport
467#define	in6p_fport	inp_fport
468#define	in6p_ppcb	inp_ppcb
469#define	in6p_state	inp_state
470#define	in6p_wantcnt	inp_wantcnt
471#define	in6p_last_outifp inp_last_outifp
472#define	in6pcb		inpcb
473#if IPSEC
474#define	in6p_sp		inp_sp
475#endif /* IPSEC */
476
477struct inpcbport {
478	LIST_ENTRY(inpcbport) phd_hash;
479	struct inpcbhead phd_pcblist;
480	u_short phd_port;
481};
482
483struct intimercount {
484	u_int32_t intimer_lazy;	/* lazy requests for timer scheduling */
485	u_int32_t intimer_fast; /* fast requests, can be coalesced */
486	u_int32_t intimer_nodelay; /* fast requests, never coalesced */
487};
488
489typedef void (*inpcb_timer_func_t)(struct inpcbinfo *);
490
491/*
492 * Global data structure for each high-level protocol (UDP, TCP, ...) in both
493 * IPv4 and IPv6.  Holds inpcb lists and information for managing them.  Each
494 * pcbinfo is protected by a RW lock: ipi_lock.
495 *
496 * All INPCB pcbinfo entries are linked together via ipi_entry.
497 */
498struct inpcbinfo {
499	/*
500	 * Glue to all PCB infos, as well as garbage collector and
501	 * timer callbacks, protected by inpcb_lock.  Callout request
502	 * counts are atomically updated.
503	 */
504	TAILQ_ENTRY(inpcbinfo)	ipi_entry;
505	inpcb_timer_func_t	ipi_gc;
506	inpcb_timer_func_t	ipi_timer;
507	struct intimercount	ipi_gc_req;
508	struct intimercount	ipi_timer_req;
509
510	/*
511	 * Per-protocol lock protecting pcb list, pcb count, etc.
512	 */
513	lck_rw_t		*ipi_lock;
514
515	/*
516	 * List and count of pcbs on the protocol.
517	 */
518	struct inpcbhead	*ipi_listhead;
519	uint32_t		ipi_count;
520
521	/*
522	 * Count of pcbs marked with INP2_TIMEWAIT flag.
523	 */
524	uint32_t		ipi_twcount;
525
526	/*
527	 * Generation count -- incremented each time a connection is
528	 * allocated or freed.
529	 */
530	uint64_t		ipi_gencnt;
531
532	/*
533	 * Fields associated with port lookup and allocation.
534	 */
535	uint16_t		ipi_lastport;
536	uint16_t		ipi_lastlow;
537	uint16_t		ipi_lasthi;
538
539	/*
540	 * Zone from which inpcbs are allocated for this protocol.
541	 */
542	struct zone		*ipi_zone;
543
544	/*
545	 * Per-protocol hash of pcbs, hashed by local and foreign
546	 * addresses and port numbers.
547	 */
548	struct inpcbhead	*ipi_hashbase;
549	u_long			ipi_hashmask;
550
551	/*
552	 * Per-protocol hash of pcbs, hashed by only local port number.
553	 */
554	struct inpcbporthead	*ipi_porthashbase;
555	u_long			ipi_porthashmask;
556
557	/*
558	 * Misc.
559	 */
560	lck_attr_t		*ipi_lock_attr;
561	lck_grp_t		*ipi_lock_grp;
562	lck_grp_attr_t		*ipi_lock_grp_attr;
563};
564
565#define	INP_PCBHASH(faddr, lport, fport, mask) \
566	(((faddr) ^ ((faddr) >> 16) ^ ntohs((lport) ^ (fport))) & (mask))
567#define	INP_PCBPORTHASH(lport, mask) \
568	(ntohs((lport)) & (mask))
569
570#define	INP_IS_FLOW_CONTROLLED(_inp_) \
571	((_inp_)->inp_flags & INP_FLOW_CONTROLLED)
572#define	INP_IS_FLOW_SUSPENDED(_inp_) \
573	(((_inp_)->inp_flags & INP_FLOW_SUSPENDED) ||	\
574	((_inp_)->inp_socket->so_flags & SOF_SUSPENDED))
575#define	INP_WAIT_FOR_IF_FEEDBACK(_inp_) \
576	(((_inp_)->inp_flags & (INP_FLOW_CONTROLLED | INP_FLOW_SUSPENDED)) != 0)
577
578#endif /* BSD_KERNEL_PRIVATE */
579
580/*
581 * Flags for inp_flags.
582 *
583 * Some of these are publicly defined for legacy reasons, as they are
584 * (unfortunately) used by certain applications to determine, at compile
585 * time, whether or not the OS supports certain features.
586 */
587#ifdef BSD_KERNEL_PRIVATE
588#define	INP_RECVOPTS		0x00000001 /* receive incoming IP options */
589#define	INP_RECVRETOPTS		0x00000002 /* receive IP options for reply */
590#define	INP_RECVDSTADDR		0x00000004 /* receive IP dst address */
591#define	INP_HDRINCL		0x00000008 /* user supplies entire IP header */
592#define	INP_HIGHPORT		0x00000010 /* user wants "high" port binding */
593#define	INP_LOWPORT		0x00000020 /* user wants "low" port binding */
594#endif /* BSD_KERNEL_PRIVATE */
595
596#define	INP_ANONPORT		0x00000040 /* port chosen for user */
597
598#ifdef BSD_KERNEL_PRIVATE
599#define	INP_RECVIF		0x00000080 /* receive incoming interface */
600#define	INP_MTUDISC		0x00000100 /* unused */
601#define	INP_STRIPHDR		0x00000200 /* strip hdrs in raw_ip (for OT) */
602#define	INP_RECV_ANYIF		0x00000400 /* don't restrict inbound iface */
603#define	INP_INADDR_ANY		0x00000800 /* local address wasn't specified */
604#define	INP_IN6ADDR_ANY		INP_INADDR_ANY
605#define	INP_RECVTTL		0x00001000 /* receive incoming IP TTL */
606#define	INP_UDP_NOCKSUM		0x00002000 /* turn off outbound UDP checksum */
607#define	INP_BOUND_IF		0x00004000 /* bind socket to an interface */
608#endif /* BSD_KERNEL_PRIVATE */
609
610#define	IN6P_IPV6_V6ONLY	0x00008000 /* restrict AF_INET6 socket for v6 */
611
612#ifdef BSD_KERNEL_PRIVATE
613#define	IN6P_PKTINFO		0x00010000 /* receive IP6 dst and I/F */
614#define	IN6P_HOPLIMIT		0x00020000 /* receive hoplimit */
615#define	IN6P_HOPOPTS		0x00040000 /* receive hop-by-hop options */
616#define	IN6P_DSTOPTS		0x00080000 /* receive dst options after rthdr */
617#define	IN6P_RTHDR		0x00100000 /* receive routing header */
618#define	IN6P_RTHDRDSTOPTS	0x00200000 /* receive dstoptions before rthdr */
619#define	IN6P_TCLASS		0x00400000 /* receive traffic class value */
620#define	IN6P_AUTOFLOWLABEL	0x00800000 /* attach flowlabel automatically */
621#endif /* BSD_KERNEL_PRIVATE */
622
623#define	IN6P_BINDV6ONLY		0x01000000 /* do not grab IPv4 traffic */
624
625#ifdef BSD_KERNEL_PRIVATE
626#define	IN6P_RFC2292		0x02000000 /* used RFC2292 API on the socket */
627#define	IN6P_MTU		0x04000000 /* receive path MTU */
628#define	INP_PKTINFO		0x08000000 /* rcv and snd PKTINFO for IPv4 */
629#define	INP_FLOW_SUSPENDED	0x10000000 /* flow suspended */
630#define	INP_NO_IFT_CELLULAR	0x20000000 /* do not use cellular interface */
631#define	INP_FLOW_CONTROLLED	0x40000000 /* flow controlled */
632#define	INP_FC_FEEDBACK		0x80000000 /* got interface flow adv feedback */
633
634#define	INP_CONTROLOPTS	\
635	(INP_RECVOPTS|INP_RECVRETOPTS|INP_RECVDSTADDR|INP_RECVIF|INP_RECVTTL| \
636	INP_PKTINFO|IN6P_PKTINFO|IN6P_HOPLIMIT|IN6P_HOPOPTS|IN6P_DSTOPTS| \
637	IN6P_RTHDR|IN6P_RTHDRDSTOPTS|IN6P_TCLASS|IN6P_RFC2292|IN6P_MTU)
638
639#define	INP_UNMAPPABLEOPTS \
640	(IN6P_HOPOPTS|IN6P_DSTOPTS|IN6P_RTHDR| IN6P_TCLASS|IN6P_AUTOFLOWLABEL)
641
642/*
643 * Flags for inp_flags2.
644 *
645 * Overflowed INP flags; use INP2 prefix to avoid misuse.
646 */
647#define	INP2_TIMEWAIT		0x00000001 /* in TIMEWAIT */
648#define	INP2_IN_FCTREE		0x00000002 /* in inp_fc_tree */
649#define	INP2_WANT_FLOW_DIVERT	0x00000004 /* flow divert is desired */
650
651/*
652 * Flags passed to in_pcblookup*() functions.
653 */
654#define	INPLOOKUP_WILDCARD	1
655
656#define	sotoinpcb(so)	((struct inpcb *)(so)->so_pcb)
657#define	sotoin6pcb(so)	sotoinpcb(so)
658
659struct sysctl_req;
660
661extern int ipport_lowfirstauto;
662extern int ipport_lowlastauto;
663extern int ipport_firstauto;
664extern int ipport_lastauto;
665extern int ipport_hifirstauto;
666extern int ipport_hilastauto;
667
668/* freshly allocated PCB, it's in use */
669#define	INPCB_STATE_INUSE	0x1
670/* this pcb is sitting in a a cache */
671#define	INPCB_STATE_CACHED	0x2
672/* should treat as gone, will be garbage collected and freed */
673#define	INPCB_STATE_DEAD	0x3
674
675/* marked as ready to be garbaged collected, should be treated as not found */
676#define	WNT_STOPUSING		0xffff
677/* that pcb is being acquired, do not recycle this time */
678#define	WNT_ACQUIRE		0x1
679/* release acquired mode, can be garbage collected when wantcnt is null */
680#define	WNT_RELEASE		0x2
681
682extern void in_pcbinit(void);
683extern void in_pcbinfo_attach(struct inpcbinfo *);
684extern int in_pcbinfo_detach(struct inpcbinfo *);
685
686/* type of timer to be scheduled by inpcb_gc_sched and inpcb_timer_sched */
687enum {
688	INPCB_TIMER_LAZY = 0x1,
689	INPCB_TIMER_FAST,
690	INPCB_TIMER_NODELAY
691};
692extern void inpcb_gc_sched(struct inpcbinfo *, u_int32_t type);
693extern void inpcb_timer_sched(struct inpcbinfo *, u_int32_t type);
694
695extern void in_losing(struct inpcb *);
696extern void in_rtchange(struct inpcb *, int);
697extern int in_pcballoc(struct socket *, struct inpcbinfo *, struct proc *);
698extern int in_pcbbind(struct inpcb *, struct sockaddr *, struct proc *);
699extern int in_pcbconnect(struct inpcb *, struct sockaddr *, struct proc *,
700    unsigned int, struct ifnet **);
701extern void in_pcbdetach(struct inpcb *);
702extern void in_pcbdispose(struct inpcb *);
703extern void in_pcbdisconnect(struct inpcb *);
704extern int in_pcbinshash(struct inpcb *, int);
705extern int in_pcbladdr(struct inpcb *, struct sockaddr *, struct in_addr *,
706    unsigned int, struct ifnet **);
707extern struct inpcb *in_pcblookup_local(struct inpcbinfo *, struct in_addr,
708    u_int, int);
709extern struct inpcb *in_pcblookup_local_and_cleanup(struct inpcbinfo *,
710    struct in_addr, u_int, int);
711extern struct inpcb *in_pcblookup_hash(struct inpcbinfo *, struct in_addr,
712    u_int, struct in_addr, u_int, int, struct ifnet *);
713extern int in_pcblookup_hash_exists(struct inpcbinfo *, struct in_addr,
714    u_int, struct in_addr, u_int, int, uid_t *, gid_t *, struct ifnet *);
715extern void in_pcbnotifyall(struct inpcbinfo *, struct in_addr, int,
716    void (*)(struct inpcb *, int));
717extern void in_pcbrehash(struct inpcb *);
718extern int in_getpeeraddr(struct socket *, struct sockaddr **);
719extern int in_getpeeraddr_s(struct socket *, struct sockaddr_storage *);
720extern int in_getsockaddr(struct socket *, struct sockaddr **);
721extern int in_getsockaddr_s(struct socket *, struct sockaddr_storage *);
722extern int in_pcb_checkstate(struct inpcb *, int, int);
723extern void in_pcbremlists(struct inpcb *);
724extern void inpcb_to_compat(struct inpcb *, struct inpcb_compat *);
725extern void inpcb_to_xinpcb64(struct inpcb *, struct xinpcb64 *);
726extern int get_pcblist_n(short, struct sysctl_req *, struct inpcbinfo *);
727extern void inpcb_get_ports_used(u_int32_t, int, u_int32_t, bitstr_t *,
728    struct inpcbinfo *);
729#define	INPCB_OPPORTUNISTIC_THROTTLEON	0x0001
730#define	INPCB_OPPORTUNISTIC_SETCMD	0x0002
731extern uint32_t inpcb_count_opportunistic(unsigned int, struct inpcbinfo *,
732    u_int32_t);
733extern uint32_t inpcb_find_anypcb_byaddr(struct ifaddr *, struct inpcbinfo *);
734extern void inp_route_copyout(struct inpcb *, struct route *);
735extern void inp_route_copyin(struct inpcb *, struct route *);
736extern int inp_bindif(struct inpcb *, unsigned int, struct ifnet **);
737extern void inp_set_nocellular(struct inpcb *);
738extern void inp_clear_nocellular(struct inpcb *);
739#if FLOW_DIVERT
740extern void inp_set_flow_divert(struct inpcb *);
741extern void inp_clear_flow_divert(struct inpcb *);
742#endif /* FLOW_DIVERT */
743extern u_int32_t inp_calc_flowhash(struct inpcb *);
744extern void inp_reset_fc_state(struct inpcb *);
745extern int inp_set_fc_state(struct inpcb *, int advcode);
746extern void inp_fc_unthrottle_tcp(struct inpcb *);
747extern void inp_flowadv(uint32_t);
748extern int inp_flush(struct inpcb *, int);
749extern int inp_findinpcb_procinfo(struct inpcbinfo *, uint32_t, struct so_procinfo *);
750extern void inp_get_soprocinfo(struct inpcb *, struct so_procinfo *);
751extern int inp_update_policy(struct inpcb *);
752extern boolean_t inp_restricted(struct inpcb *, struct ifnet *);
753#endif /* BSD_KERNEL_PRIVATE */
754#ifdef KERNEL_PRIVATE
755/* exported for PPP */
756extern void inp_clear_INP_INADDR_ANY(struct socket *);
757#endif /* KERNEL_PRIVATE */
758#endif /* !_NETINET_IN_PCB_H_ */
759