xref: /xnu-10002.1.13/bsd/kern/uipc_socket2.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Copyright (c) 1998-2020 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions  *
6*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions  *
15*1031c584SApple OSS Distributions  * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions  *
18*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions  * limitations under the License.
25*1031c584SApple OSS Distributions  *
26*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions  */
28*1031c584SApple OSS Distributions /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29*1031c584SApple OSS Distributions /*
30*1031c584SApple OSS Distributions  * Copyright (c) 1982, 1986, 1988, 1990, 1993
31*1031c584SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
32*1031c584SApple OSS Distributions  *
33*1031c584SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
34*1031c584SApple OSS Distributions  * modification, are permitted provided that the following conditions
35*1031c584SApple OSS Distributions  * are met:
36*1031c584SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
37*1031c584SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
38*1031c584SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
39*1031c584SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
40*1031c584SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
41*1031c584SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
42*1031c584SApple OSS Distributions  *    must display the following acknowledgement:
43*1031c584SApple OSS Distributions  *	This product includes software developed by the University of
44*1031c584SApple OSS Distributions  *	California, Berkeley and its contributors.
45*1031c584SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
46*1031c584SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
47*1031c584SApple OSS Distributions  *    without specific prior written permission.
48*1031c584SApple OSS Distributions  *
49*1031c584SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50*1031c584SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51*1031c584SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52*1031c584SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53*1031c584SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54*1031c584SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55*1031c584SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56*1031c584SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57*1031c584SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58*1031c584SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59*1031c584SApple OSS Distributions  * SUCH DAMAGE.
60*1031c584SApple OSS Distributions  *
61*1031c584SApple OSS Distributions  *	@(#)uipc_socket2.c	8.1 (Berkeley) 6/10/93
62*1031c584SApple OSS Distributions  */
63*1031c584SApple OSS Distributions /*
64*1031c584SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65*1031c584SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
66*1031c584SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
67*1031c584SApple OSS Distributions  * Version 2.0.
68*1031c584SApple OSS Distributions  */
69*1031c584SApple OSS Distributions 
70*1031c584SApple OSS Distributions #include <sys/param.h>
71*1031c584SApple OSS Distributions #include <sys/systm.h>
72*1031c584SApple OSS Distributions #include <sys/domain.h>
73*1031c584SApple OSS Distributions #include <sys/kernel.h>
74*1031c584SApple OSS Distributions #include <sys/proc_internal.h>
75*1031c584SApple OSS Distributions #include <sys/kauth.h>
76*1031c584SApple OSS Distributions #include <sys/malloc.h>
77*1031c584SApple OSS Distributions #include <sys/mbuf.h>
78*1031c584SApple OSS Distributions #include <sys/mcache.h>
79*1031c584SApple OSS Distributions #include <sys/protosw.h>
80*1031c584SApple OSS Distributions #include <sys/stat.h>
81*1031c584SApple OSS Distributions #include <sys/socket.h>
82*1031c584SApple OSS Distributions #include <sys/socketvar.h>
83*1031c584SApple OSS Distributions #include <sys/signalvar.h>
84*1031c584SApple OSS Distributions #include <sys/sysctl.h>
85*1031c584SApple OSS Distributions #include <sys/syslog.h>
86*1031c584SApple OSS Distributions #include <sys/unpcb.h>
87*1031c584SApple OSS Distributions #include <sys/ev.h>
88*1031c584SApple OSS Distributions #include <kern/locks.h>
89*1031c584SApple OSS Distributions #include <net/route.h>
90*1031c584SApple OSS Distributions #include <net/content_filter.h>
91*1031c584SApple OSS Distributions #include <netinet/in.h>
92*1031c584SApple OSS Distributions #include <netinet/in_pcb.h>
93*1031c584SApple OSS Distributions #include <netinet/tcp_var.h>
94*1031c584SApple OSS Distributions #include <sys/kdebug.h>
95*1031c584SApple OSS Distributions #include <libkern/OSAtomic.h>
96*1031c584SApple OSS Distributions 
97*1031c584SApple OSS Distributions #if CONFIG_MACF
98*1031c584SApple OSS Distributions #include <security/mac_framework.h>
99*1031c584SApple OSS Distributions #endif
100*1031c584SApple OSS Distributions 
101*1031c584SApple OSS Distributions #include <mach/vm_param.h>
102*1031c584SApple OSS Distributions 
103*1031c584SApple OSS Distributions #if MPTCP
104*1031c584SApple OSS Distributions #include <netinet/mptcp_var.h>
105*1031c584SApple OSS Distributions #endif
106*1031c584SApple OSS Distributions 
107*1031c584SApple OSS Distributions extern uint32_t net_wake_pkt_debug;
108*1031c584SApple OSS Distributions 
109*1031c584SApple OSS Distributions #define DBG_FNC_SBDROP          NETDBG_CODE(DBG_NETSOCK, 4)
110*1031c584SApple OSS Distributions #define DBG_FNC_SBAPPEND        NETDBG_CODE(DBG_NETSOCK, 5)
111*1031c584SApple OSS Distributions 
112*1031c584SApple OSS Distributions SYSCTL_DECL(_kern_ipc);
113*1031c584SApple OSS Distributions 
114*1031c584SApple OSS Distributions __private_extern__ u_int32_t net_io_policy_throttle_best_effort = 0;
115*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, OID_AUTO, throttle_best_effort,
116*1031c584SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &net_io_policy_throttle_best_effort, 0, "");
117*1031c584SApple OSS Distributions 
118*1031c584SApple OSS Distributions static inline void sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *);
119*1031c584SApple OSS Distributions static struct socket *sonewconn_internal(struct socket *, int);
120*1031c584SApple OSS Distributions static int sbappendcontrol_internal(struct sockbuf *, struct mbuf *,
121*1031c584SApple OSS Distributions     struct mbuf *);
122*1031c584SApple OSS Distributions static void soevent_ifdenied(struct socket *);
123*1031c584SApple OSS Distributions 
124*1031c584SApple OSS Distributions static int sbappendrecord_common(struct sockbuf *sb, struct mbuf *m0, boolean_t nodrop);
125*1031c584SApple OSS Distributions static int sbappend_common(struct sockbuf *sb, struct mbuf *m, boolean_t nodrop);
126*1031c584SApple OSS Distributions 
127*1031c584SApple OSS Distributions /*
128*1031c584SApple OSS Distributions  * Primitive routines for operating on sockets and socket buffers
129*1031c584SApple OSS Distributions  */
130*1031c584SApple OSS Distributions static int soqlimitcompat = 1;
131*1031c584SApple OSS Distributions static int soqlencomp = 0;
132*1031c584SApple OSS Distributions 
133*1031c584SApple OSS Distributions /*
134*1031c584SApple OSS Distributions  * Based on the number of mbuf clusters configured, high_sb_max and sb_max can
135*1031c584SApple OSS Distributions  * get scaled up or down to suit that memory configuration. high_sb_max is a
136*1031c584SApple OSS Distributions  * higher limit on sb_max that is checked when sb_max gets set through sysctl.
137*1031c584SApple OSS Distributions  */
138*1031c584SApple OSS Distributions u_int32_t       sb_max = SB_MAX;
139*1031c584SApple OSS Distributions uint64_t        sb_max_adj = SB_MAX_ADJUST(SB_MAX);
140*1031c584SApple OSS Distributions u_int32_t       high_sb_max = SB_MAX;
141*1031c584SApple OSS Distributions 
142*1031c584SApple OSS Distributions static  u_int32_t sb_efficiency = 8;    /* parameter for sbreserve() */
143*1031c584SApple OSS Distributions int32_t total_sbmb_cnt __attribute__((aligned(8))) = 0;
144*1031c584SApple OSS Distributions int32_t total_sbmb_cnt_floor __attribute__((aligned(8))) = 0;
145*1031c584SApple OSS Distributions int32_t total_sbmb_cnt_peak __attribute__((aligned(8))) = 0;
146*1031c584SApple OSS Distributions int64_t sbmb_limreached __attribute__((aligned(8))) = 0;
147*1031c584SApple OSS Distributions 
148*1031c584SApple OSS Distributions u_int32_t net_io_policy_log = 0;        /* log socket policy changes */
149*1031c584SApple OSS Distributions #if CONFIG_PROC_UUID_POLICY
150*1031c584SApple OSS Distributions u_int32_t net_io_policy_uuid = 1;       /* enable UUID socket policy */
151*1031c584SApple OSS Distributions #endif /* CONFIG_PROC_UUID_POLICY */
152*1031c584SApple OSS Distributions 
153*1031c584SApple OSS Distributions /*
154*1031c584SApple OSS Distributions  * Procedures to manipulate state flags of socket
155*1031c584SApple OSS Distributions  * and do appropriate wakeups.  Normal sequence from the
156*1031c584SApple OSS Distributions  * active (originating) side is that soisconnecting() is
157*1031c584SApple OSS Distributions  * called during processing of connect() call,
158*1031c584SApple OSS Distributions  * resulting in an eventual call to soisconnected() if/when the
159*1031c584SApple OSS Distributions  * connection is established.  When the connection is torn down
160*1031c584SApple OSS Distributions  * soisdisconnecting() is called during processing of disconnect() call,
161*1031c584SApple OSS Distributions  * and soisdisconnected() is called when the connection to the peer
162*1031c584SApple OSS Distributions  * is totally severed.  The semantics of these routines are such that
163*1031c584SApple OSS Distributions  * connectionless protocols can call soisconnected() and soisdisconnected()
164*1031c584SApple OSS Distributions  * only, bypassing the in-progress calls when setting up a ``connection''
165*1031c584SApple OSS Distributions  * takes no time.
166*1031c584SApple OSS Distributions  *
167*1031c584SApple OSS Distributions  * From the passive side, a socket is created with
168*1031c584SApple OSS Distributions  * two queues of sockets: so_incomp for connections in progress
169*1031c584SApple OSS Distributions  * and so_comp for connections already made and awaiting user acceptance.
170*1031c584SApple OSS Distributions  * As a protocol is preparing incoming connections, it creates a socket
171*1031c584SApple OSS Distributions  * structure queued on so_incomp by calling sonewconn().  When the connection
172*1031c584SApple OSS Distributions  * is established, soisconnected() is called, and transfers the
173*1031c584SApple OSS Distributions  * socket structure to so_comp, making it available to accept().
174*1031c584SApple OSS Distributions  *
175*1031c584SApple OSS Distributions  * If a socket is closed with sockets on either
176*1031c584SApple OSS Distributions  * so_incomp or so_comp, these sockets are dropped.
177*1031c584SApple OSS Distributions  *
178*1031c584SApple OSS Distributions  * If higher level protocols are implemented in
179*1031c584SApple OSS Distributions  * the kernel, the wakeups done here will sometimes
180*1031c584SApple OSS Distributions  * cause software-interrupt process scheduling.
181*1031c584SApple OSS Distributions  */
182*1031c584SApple OSS Distributions void
soisconnecting(struct socket * so)183*1031c584SApple OSS Distributions soisconnecting(struct socket *so)
184*1031c584SApple OSS Distributions {
185*1031c584SApple OSS Distributions 	so->so_state &= ~(SS_ISCONNECTED | SS_ISDISCONNECTING);
186*1031c584SApple OSS Distributions 	so->so_state |= SS_ISCONNECTING;
187*1031c584SApple OSS Distributions 
188*1031c584SApple OSS Distributions 	sflt_notify(so, sock_evt_connecting, NULL);
189*1031c584SApple OSS Distributions }
190*1031c584SApple OSS Distributions 
191*1031c584SApple OSS Distributions void
soisconnected(struct socket * so)192*1031c584SApple OSS Distributions soisconnected(struct socket *so)
193*1031c584SApple OSS Distributions {
194*1031c584SApple OSS Distributions 	/*
195*1031c584SApple OSS Distributions 	 * If socket is subject to filter and is pending initial verdict,
196*1031c584SApple OSS Distributions 	 * delay marking socket as connected and do not present the connected
197*1031c584SApple OSS Distributions 	 * socket to user just yet.
198*1031c584SApple OSS Distributions 	 */
199*1031c584SApple OSS Distributions 	if (cfil_sock_connected_pending_verdict(so)) {
200*1031c584SApple OSS Distributions 		return;
201*1031c584SApple OSS Distributions 	}
202*1031c584SApple OSS Distributions 
203*1031c584SApple OSS Distributions 	so->so_state &= ~(SS_ISCONNECTING | SS_ISDISCONNECTING | SS_ISCONFIRMING);
204*1031c584SApple OSS Distributions 	so->so_state |= SS_ISCONNECTED;
205*1031c584SApple OSS Distributions 
206*1031c584SApple OSS Distributions 	soreserve_preconnect(so, 0);
207*1031c584SApple OSS Distributions 
208*1031c584SApple OSS Distributions 	sflt_notify(so, sock_evt_connected, NULL);
209*1031c584SApple OSS Distributions 
210*1031c584SApple OSS Distributions 	if (so->so_head != NULL && (so->so_state & SS_INCOMP)) {
211*1031c584SApple OSS Distributions 		struct socket *head = so->so_head;
212*1031c584SApple OSS Distributions 		int locked = 0;
213*1031c584SApple OSS Distributions 
214*1031c584SApple OSS Distributions 		/*
215*1031c584SApple OSS Distributions 		 * Enforce lock order when the protocol has per socket locks
216*1031c584SApple OSS Distributions 		 */
217*1031c584SApple OSS Distributions 		if (head->so_proto->pr_getlock != NULL) {
218*1031c584SApple OSS Distributions 			socket_lock(head, 1);
219*1031c584SApple OSS Distributions 			so_acquire_accept_list(head, so);
220*1031c584SApple OSS Distributions 			locked = 1;
221*1031c584SApple OSS Distributions 		}
222*1031c584SApple OSS Distributions 		if (so->so_head == head && (so->so_state & SS_INCOMP)) {
223*1031c584SApple OSS Distributions 			so->so_state &= ~SS_INCOMP;
224*1031c584SApple OSS Distributions 			so->so_state |= SS_COMP;
225*1031c584SApple OSS Distributions 			TAILQ_REMOVE(&head->so_incomp, so, so_list);
226*1031c584SApple OSS Distributions 			TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
227*1031c584SApple OSS Distributions 			head->so_incqlen--;
228*1031c584SApple OSS Distributions 
229*1031c584SApple OSS Distributions 			/*
230*1031c584SApple OSS Distributions 			 * We have to release the accept list in
231*1031c584SApple OSS Distributions 			 * case a socket callback calls sock_accept()
232*1031c584SApple OSS Distributions 			 */
233*1031c584SApple OSS Distributions 			if (locked != 0) {
234*1031c584SApple OSS Distributions 				so_release_accept_list(head);
235*1031c584SApple OSS Distributions 				socket_unlock(so, 0);
236*1031c584SApple OSS Distributions 			}
237*1031c584SApple OSS Distributions 			sorwakeup(head);
238*1031c584SApple OSS Distributions 			wakeup_one((caddr_t)&head->so_timeo);
239*1031c584SApple OSS Distributions 
240*1031c584SApple OSS Distributions 			if (locked != 0) {
241*1031c584SApple OSS Distributions 				socket_unlock(head, 1);
242*1031c584SApple OSS Distributions 				socket_lock(so, 0);
243*1031c584SApple OSS Distributions 			}
244*1031c584SApple OSS Distributions 		} else if (locked != 0) {
245*1031c584SApple OSS Distributions 			so_release_accept_list(head);
246*1031c584SApple OSS Distributions 			socket_unlock(head, 1);
247*1031c584SApple OSS Distributions 		}
248*1031c584SApple OSS Distributions 	} else {
249*1031c584SApple OSS Distributions 		wakeup((caddr_t)&so->so_timeo);
250*1031c584SApple OSS Distributions 		sorwakeup(so);
251*1031c584SApple OSS Distributions 		sowwakeup(so);
252*1031c584SApple OSS Distributions 		soevent(so, SO_FILT_HINT_LOCKED | SO_FILT_HINT_CONNECTED |
253*1031c584SApple OSS Distributions 		    SO_FILT_HINT_CONNINFO_UPDATED);
254*1031c584SApple OSS Distributions 	}
255*1031c584SApple OSS Distributions }
256*1031c584SApple OSS Distributions 
257*1031c584SApple OSS Distributions boolean_t
socanwrite(struct socket * so)258*1031c584SApple OSS Distributions socanwrite(struct socket *so)
259*1031c584SApple OSS Distributions {
260*1031c584SApple OSS Distributions 	return (so->so_state & SS_ISCONNECTED) ||
261*1031c584SApple OSS Distributions 	       !(so->so_proto->pr_flags & PR_CONNREQUIRED) ||
262*1031c584SApple OSS Distributions 	       (so->so_flags1 & SOF1_PRECONNECT_DATA);
263*1031c584SApple OSS Distributions }
264*1031c584SApple OSS Distributions 
265*1031c584SApple OSS Distributions void
soisdisconnecting(struct socket * so)266*1031c584SApple OSS Distributions soisdisconnecting(struct socket *so)
267*1031c584SApple OSS Distributions {
268*1031c584SApple OSS Distributions 	so->so_state &= ~SS_ISCONNECTING;
269*1031c584SApple OSS Distributions 	so->so_state |= (SS_ISDISCONNECTING | SS_CANTRCVMORE | SS_CANTSENDMORE);
270*1031c584SApple OSS Distributions 	soevent(so, SO_FILT_HINT_LOCKED);
271*1031c584SApple OSS Distributions 	sflt_notify(so, sock_evt_disconnecting, NULL);
272*1031c584SApple OSS Distributions 	wakeup((caddr_t)&so->so_timeo);
273*1031c584SApple OSS Distributions 	sowwakeup(so);
274*1031c584SApple OSS Distributions 	sorwakeup(so);
275*1031c584SApple OSS Distributions }
276*1031c584SApple OSS Distributions 
277*1031c584SApple OSS Distributions void
soisdisconnected(struct socket * so)278*1031c584SApple OSS Distributions soisdisconnected(struct socket *so)
279*1031c584SApple OSS Distributions {
280*1031c584SApple OSS Distributions 	so->so_state &= ~(SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING);
281*1031c584SApple OSS Distributions 	so->so_state |= (SS_CANTRCVMORE | SS_CANTSENDMORE | SS_ISDISCONNECTED);
282*1031c584SApple OSS Distributions 	soevent(so, SO_FILT_HINT_LOCKED | SO_FILT_HINT_DISCONNECTED |
283*1031c584SApple OSS Distributions 	    SO_FILT_HINT_CONNINFO_UPDATED);
284*1031c584SApple OSS Distributions 	sflt_notify(so, sock_evt_disconnected, NULL);
285*1031c584SApple OSS Distributions 	wakeup((caddr_t)&so->so_timeo);
286*1031c584SApple OSS Distributions 	sowwakeup(so);
287*1031c584SApple OSS Distributions 	sorwakeup(so);
288*1031c584SApple OSS Distributions 
289*1031c584SApple OSS Distributions #if CONTENT_FILTER
290*1031c584SApple OSS Distributions 	/* Notify content filters as soon as we cannot send/receive data */
291*1031c584SApple OSS Distributions 	cfil_sock_notify_shutdown(so, SHUT_RDWR);
292*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
293*1031c584SApple OSS Distributions }
294*1031c584SApple OSS Distributions 
295*1031c584SApple OSS Distributions /*
296*1031c584SApple OSS Distributions  * This function will issue a wakeup like soisdisconnected but it will not
297*1031c584SApple OSS Distributions  * notify the socket filters. This will avoid unlocking the socket
298*1031c584SApple OSS Distributions  * in the midst of closing it.
299*1031c584SApple OSS Distributions  */
300*1031c584SApple OSS Distributions void
sodisconnectwakeup(struct socket * so)301*1031c584SApple OSS Distributions sodisconnectwakeup(struct socket *so)
302*1031c584SApple OSS Distributions {
303*1031c584SApple OSS Distributions 	so->so_state &= ~(SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING);
304*1031c584SApple OSS Distributions 	so->so_state |= (SS_CANTRCVMORE | SS_CANTSENDMORE | SS_ISDISCONNECTED);
305*1031c584SApple OSS Distributions 	soevent(so, SO_FILT_HINT_LOCKED | SO_FILT_HINT_DISCONNECTED |
306*1031c584SApple OSS Distributions 	    SO_FILT_HINT_CONNINFO_UPDATED);
307*1031c584SApple OSS Distributions 	wakeup((caddr_t)&so->so_timeo);
308*1031c584SApple OSS Distributions 	sowwakeup(so);
309*1031c584SApple OSS Distributions 	sorwakeup(so);
310*1031c584SApple OSS Distributions 
311*1031c584SApple OSS Distributions #if CONTENT_FILTER
312*1031c584SApple OSS Distributions 	/* Notify content filters as soon as we cannot send/receive data */
313*1031c584SApple OSS Distributions 	cfil_sock_notify_shutdown(so, SHUT_RDWR);
314*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
315*1031c584SApple OSS Distributions }
316*1031c584SApple OSS Distributions 
317*1031c584SApple OSS Distributions /*
318*1031c584SApple OSS Distributions  * When an attempt at a new connection is noted on a socket
319*1031c584SApple OSS Distributions  * which accepts connections, sonewconn is called.  If the
320*1031c584SApple OSS Distributions  * connection is possible (subject to space constraints, etc.)
321*1031c584SApple OSS Distributions  * then we allocate a new structure, propoerly linked into the
322*1031c584SApple OSS Distributions  * data structure of the original socket, and return this.
323*1031c584SApple OSS Distributions  * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
324*1031c584SApple OSS Distributions  */
325*1031c584SApple OSS Distributions static struct socket *
sonewconn_internal(struct socket * head,int connstatus)326*1031c584SApple OSS Distributions sonewconn_internal(struct socket *head, int connstatus)
327*1031c584SApple OSS Distributions {
328*1031c584SApple OSS Distributions 	int so_qlen, error = 0;
329*1031c584SApple OSS Distributions 	struct socket *so;
330*1031c584SApple OSS Distributions 	lck_mtx_t *mutex_held;
331*1031c584SApple OSS Distributions 
332*1031c584SApple OSS Distributions 	if (head->so_proto->pr_getlock != NULL) {
333*1031c584SApple OSS Distributions 		mutex_held = (*head->so_proto->pr_getlock)(head, 0);
334*1031c584SApple OSS Distributions 	} else {
335*1031c584SApple OSS Distributions 		mutex_held = head->so_proto->pr_domain->dom_mtx;
336*1031c584SApple OSS Distributions 	}
337*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
338*1031c584SApple OSS Distributions 
339*1031c584SApple OSS Distributions 	if (!soqlencomp) {
340*1031c584SApple OSS Distributions 		/*
341*1031c584SApple OSS Distributions 		 * This is the default case; so_qlen represents the
342*1031c584SApple OSS Distributions 		 * sum of both incomplete and completed queues.
343*1031c584SApple OSS Distributions 		 */
344*1031c584SApple OSS Distributions 		so_qlen = head->so_qlen;
345*1031c584SApple OSS Distributions 	} else {
346*1031c584SApple OSS Distributions 		/*
347*1031c584SApple OSS Distributions 		 * When kern.ipc.soqlencomp is set to 1, so_qlen
348*1031c584SApple OSS Distributions 		 * represents only the completed queue.  Since we
349*1031c584SApple OSS Distributions 		 * cannot let the incomplete queue goes unbounded
350*1031c584SApple OSS Distributions 		 * (in case of SYN flood), we cap the incomplete
351*1031c584SApple OSS Distributions 		 * queue length to at most somaxconn, and use that
352*1031c584SApple OSS Distributions 		 * as so_qlen so that we fail immediately below.
353*1031c584SApple OSS Distributions 		 */
354*1031c584SApple OSS Distributions 		so_qlen = head->so_qlen - head->so_incqlen;
355*1031c584SApple OSS Distributions 		if (head->so_incqlen > somaxconn) {
356*1031c584SApple OSS Distributions 			so_qlen = somaxconn;
357*1031c584SApple OSS Distributions 		}
358*1031c584SApple OSS Distributions 	}
359*1031c584SApple OSS Distributions 
360*1031c584SApple OSS Distributions 	if (so_qlen >=
361*1031c584SApple OSS Distributions 	    (soqlimitcompat ? head->so_qlimit : (3 * head->so_qlimit / 2))) {
362*1031c584SApple OSS Distributions 		return (struct socket *)0;
363*1031c584SApple OSS Distributions 	}
364*1031c584SApple OSS Distributions 	so = soalloc(1, SOCK_DOM(head), head->so_type);
365*1031c584SApple OSS Distributions 	if (so == NULL) {
366*1031c584SApple OSS Distributions 		return (struct socket *)0;
367*1031c584SApple OSS Distributions 	}
368*1031c584SApple OSS Distributions 	/* check if head was closed during the soalloc */
369*1031c584SApple OSS Distributions 	if (head->so_proto == NULL) {
370*1031c584SApple OSS Distributions 		sodealloc(so);
371*1031c584SApple OSS Distributions 		return (struct socket *)0;
372*1031c584SApple OSS Distributions 	}
373*1031c584SApple OSS Distributions 
374*1031c584SApple OSS Distributions 	so->so_type = head->so_type;
375*1031c584SApple OSS Distributions 	so->so_options = head->so_options & ~SO_ACCEPTCONN;
376*1031c584SApple OSS Distributions 	so->so_linger = head->so_linger;
377*1031c584SApple OSS Distributions 	so->so_state = head->so_state | SS_NOFDREF;
378*1031c584SApple OSS Distributions 	so->so_proto = head->so_proto;
379*1031c584SApple OSS Distributions 	so->so_timeo = head->so_timeo;
380*1031c584SApple OSS Distributions 	so->so_pgid  = head->so_pgid;
381*1031c584SApple OSS Distributions 	kauth_cred_ref(head->so_cred);
382*1031c584SApple OSS Distributions 	so->so_cred = head->so_cred;
383*1031c584SApple OSS Distributions 	so->so_persona_id = head->so_persona_id;
384*1031c584SApple OSS Distributions 	so->last_pid = head->last_pid;
385*1031c584SApple OSS Distributions 	so->last_upid = head->last_upid;
386*1031c584SApple OSS Distributions 	memcpy(so->last_uuid, head->last_uuid, sizeof(so->last_uuid));
387*1031c584SApple OSS Distributions 	if (head->so_flags & SOF_DELEGATED) {
388*1031c584SApple OSS Distributions 		so->e_pid = head->e_pid;
389*1031c584SApple OSS Distributions 		so->e_upid = head->e_upid;
390*1031c584SApple OSS Distributions 		memcpy(so->e_uuid, head->e_uuid, sizeof(so->e_uuid));
391*1031c584SApple OSS Distributions 	}
392*1031c584SApple OSS Distributions 	/* inherit socket options stored in so_flags */
393*1031c584SApple OSS Distributions 	so->so_flags = head->so_flags &
394*1031c584SApple OSS Distributions 	    (SOF_NOSIGPIPE | SOF_NOADDRAVAIL | SOF_REUSESHAREUID |
395*1031c584SApple OSS Distributions 	    SOF_NOTIFYCONFLICT | SOF_BINDRANDOMPORT | SOF_NPX_SETOPTSHUT |
396*1031c584SApple OSS Distributions 	    SOF_NODEFUNCT | SOF_PRIVILEGED_TRAFFIC_CLASS | SOF_NOTSENT_LOWAT |
397*1031c584SApple OSS Distributions 	    SOF_DELEGATED);
398*1031c584SApple OSS Distributions 	so->so_flags1 |= SOF1_INBOUND;
399*1031c584SApple OSS Distributions 	so->so_usecount = 1;
400*1031c584SApple OSS Distributions 	so->next_lock_lr = 0;
401*1031c584SApple OSS Distributions 	so->next_unlock_lr = 0;
402*1031c584SApple OSS Distributions 
403*1031c584SApple OSS Distributions 	so->so_rcv.sb_flags |= SB_RECV; /* XXX */
404*1031c584SApple OSS Distributions 	so->so_rcv.sb_so = so->so_snd.sb_so = so;
405*1031c584SApple OSS Distributions 
406*1031c584SApple OSS Distributions 	/* inherit traffic management properties of listener */
407*1031c584SApple OSS Distributions 	so->so_flags1 |=
408*1031c584SApple OSS Distributions 	    head->so_flags1 & (SOF1_TRAFFIC_MGT_SO_BACKGROUND | SOF1_TC_NET_SERV_TYPE |
409*1031c584SApple OSS Distributions 	    SOF1_QOSMARKING_ALLOWED | SOF1_QOSMARKING_POLICY_OVERRIDE);
410*1031c584SApple OSS Distributions 	so->so_background_thread = head->so_background_thread;
411*1031c584SApple OSS Distributions 	so->so_traffic_class = head->so_traffic_class;
412*1031c584SApple OSS Distributions 	so->so_netsvctype = head->so_netsvctype;
413*1031c584SApple OSS Distributions 
414*1031c584SApple OSS Distributions 	if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
415*1031c584SApple OSS Distributions 		sodealloc(so);
416*1031c584SApple OSS Distributions 		return (struct socket *)0;
417*1031c584SApple OSS Distributions 	}
418*1031c584SApple OSS Distributions 	so->so_rcv.sb_flags |= (head->so_rcv.sb_flags & SB_USRSIZE);
419*1031c584SApple OSS Distributions 	so->so_snd.sb_flags |= (head->so_snd.sb_flags & SB_USRSIZE);
420*1031c584SApple OSS Distributions 
421*1031c584SApple OSS Distributions 	/*
422*1031c584SApple OSS Distributions 	 * Must be done with head unlocked to avoid deadlock
423*1031c584SApple OSS Distributions 	 * for protocol with per socket mutexes.
424*1031c584SApple OSS Distributions 	 */
425*1031c584SApple OSS Distributions 	if (head->so_proto->pr_unlock) {
426*1031c584SApple OSS Distributions 		socket_unlock(head, 0);
427*1031c584SApple OSS Distributions 	}
428*1031c584SApple OSS Distributions 	if (((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL) != 0) ||
429*1031c584SApple OSS Distributions 	    error) {
430*1031c584SApple OSS Distributions 		sodealloc(so);
431*1031c584SApple OSS Distributions 		if (head->so_proto->pr_unlock) {
432*1031c584SApple OSS Distributions 			socket_lock(head, 0);
433*1031c584SApple OSS Distributions 		}
434*1031c584SApple OSS Distributions 		return (struct socket *)0;
435*1031c584SApple OSS Distributions 	}
436*1031c584SApple OSS Distributions 	if (head->so_proto->pr_unlock) {
437*1031c584SApple OSS Distributions 		socket_lock(head, 0);
438*1031c584SApple OSS Distributions 		/*
439*1031c584SApple OSS Distributions 		 * Radar 7385998 Recheck that the head is still accepting
440*1031c584SApple OSS Distributions 		 * to avoid race condition when head is getting closed.
441*1031c584SApple OSS Distributions 		 */
442*1031c584SApple OSS Distributions 		if ((head->so_options & SO_ACCEPTCONN) == 0) {
443*1031c584SApple OSS Distributions 			so->so_state &= ~SS_NOFDREF;
444*1031c584SApple OSS Distributions 			soclose(so);
445*1031c584SApple OSS Distributions 			return (struct socket *)0;
446*1031c584SApple OSS Distributions 		}
447*1031c584SApple OSS Distributions 	}
448*1031c584SApple OSS Distributions 
449*1031c584SApple OSS Distributions 	if (so->so_proto->pr_copy_last_owner != NULL) {
450*1031c584SApple OSS Distributions 		(*so->so_proto->pr_copy_last_owner)(so, head);
451*1031c584SApple OSS Distributions 	}
452*1031c584SApple OSS Distributions 	os_atomic_inc(&so->so_proto->pr_domain->dom_refs, relaxed);
453*1031c584SApple OSS Distributions 
454*1031c584SApple OSS Distributions 	/* Insert in head appropriate lists */
455*1031c584SApple OSS Distributions 	so_acquire_accept_list(head, NULL);
456*1031c584SApple OSS Distributions 
457*1031c584SApple OSS Distributions 	so->so_head = head;
458*1031c584SApple OSS Distributions 
459*1031c584SApple OSS Distributions 	/*
460*1031c584SApple OSS Distributions 	 * Since this socket is going to be inserted into the incomp
461*1031c584SApple OSS Distributions 	 * queue, it can be picked up by another thread in
462*1031c584SApple OSS Distributions 	 * tcp_dropdropablreq to get dropped before it is setup..
463*1031c584SApple OSS Distributions 	 * To prevent this race, set in-progress flag which can be
464*1031c584SApple OSS Distributions 	 * cleared later
465*1031c584SApple OSS Distributions 	 */
466*1031c584SApple OSS Distributions 	so->so_flags |= SOF_INCOMP_INPROGRESS;
467*1031c584SApple OSS Distributions 
468*1031c584SApple OSS Distributions 	if (connstatus) {
469*1031c584SApple OSS Distributions 		TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
470*1031c584SApple OSS Distributions 		so->so_state |= SS_COMP;
471*1031c584SApple OSS Distributions 	} else {
472*1031c584SApple OSS Distributions 		TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
473*1031c584SApple OSS Distributions 		so->so_state |= SS_INCOMP;
474*1031c584SApple OSS Distributions 		head->so_incqlen++;
475*1031c584SApple OSS Distributions 	}
476*1031c584SApple OSS Distributions 	head->so_qlen++;
477*1031c584SApple OSS Distributions 
478*1031c584SApple OSS Distributions 	so_release_accept_list(head);
479*1031c584SApple OSS Distributions 
480*1031c584SApple OSS Distributions 	/* Attach socket filters for this protocol */
481*1031c584SApple OSS Distributions 	sflt_initsock(so);
482*1031c584SApple OSS Distributions 
483*1031c584SApple OSS Distributions 	if (connstatus) {
484*1031c584SApple OSS Distributions 		so->so_state |= (short)connstatus;
485*1031c584SApple OSS Distributions 		sorwakeup(head);
486*1031c584SApple OSS Distributions 		wakeup((caddr_t)&head->so_timeo);
487*1031c584SApple OSS Distributions 	}
488*1031c584SApple OSS Distributions 	return so;
489*1031c584SApple OSS Distributions }
490*1031c584SApple OSS Distributions 
491*1031c584SApple OSS Distributions 
492*1031c584SApple OSS Distributions struct socket *
sonewconn(struct socket * head,int connstatus,const struct sockaddr * from)493*1031c584SApple OSS Distributions sonewconn(struct socket *head, int connstatus, const struct sockaddr *from)
494*1031c584SApple OSS Distributions {
495*1031c584SApple OSS Distributions 	int error = sflt_connectin(head, from);
496*1031c584SApple OSS Distributions 	if (error) {
497*1031c584SApple OSS Distributions 		return NULL;
498*1031c584SApple OSS Distributions 	}
499*1031c584SApple OSS Distributions 
500*1031c584SApple OSS Distributions 	return sonewconn_internal(head, connstatus);
501*1031c584SApple OSS Distributions }
502*1031c584SApple OSS Distributions 
503*1031c584SApple OSS Distributions /*
504*1031c584SApple OSS Distributions  * Socantsendmore indicates that no more data will be sent on the
505*1031c584SApple OSS Distributions  * socket; it would normally be applied to a socket when the user
506*1031c584SApple OSS Distributions  * informs the system that no more data is to be sent, by the protocol
507*1031c584SApple OSS Distributions  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
508*1031c584SApple OSS Distributions  * will be received, and will normally be applied to the socket by a
509*1031c584SApple OSS Distributions  * protocol when it detects that the peer will send no more data.
510*1031c584SApple OSS Distributions  * Data queued for reading in the socket may yet be read.
511*1031c584SApple OSS Distributions  */
512*1031c584SApple OSS Distributions 
513*1031c584SApple OSS Distributions void
socantsendmore(struct socket * so)514*1031c584SApple OSS Distributions socantsendmore(struct socket *so)
515*1031c584SApple OSS Distributions {
516*1031c584SApple OSS Distributions 	so->so_state |= SS_CANTSENDMORE;
517*1031c584SApple OSS Distributions 	soevent(so, SO_FILT_HINT_LOCKED | SO_FILT_HINT_CANTSENDMORE);
518*1031c584SApple OSS Distributions 	sflt_notify(so, sock_evt_cantsendmore, NULL);
519*1031c584SApple OSS Distributions 	sowwakeup(so);
520*1031c584SApple OSS Distributions }
521*1031c584SApple OSS Distributions 
522*1031c584SApple OSS Distributions void
socantrcvmore(struct socket * so)523*1031c584SApple OSS Distributions socantrcvmore(struct socket *so)
524*1031c584SApple OSS Distributions {
525*1031c584SApple OSS Distributions 	so->so_state |= SS_CANTRCVMORE;
526*1031c584SApple OSS Distributions 	soevent(so, SO_FILT_HINT_LOCKED | SO_FILT_HINT_CANTRCVMORE);
527*1031c584SApple OSS Distributions 	sflt_notify(so, sock_evt_cantrecvmore, NULL);
528*1031c584SApple OSS Distributions 	sorwakeup(so);
529*1031c584SApple OSS Distributions }
530*1031c584SApple OSS Distributions 
531*1031c584SApple OSS Distributions /*
532*1031c584SApple OSS Distributions  * Wait for data to arrive at/drain from a socket buffer.
533*1031c584SApple OSS Distributions  */
534*1031c584SApple OSS Distributions int
sbwait(struct sockbuf * sb)535*1031c584SApple OSS Distributions sbwait(struct sockbuf *sb)
536*1031c584SApple OSS Distributions {
537*1031c584SApple OSS Distributions 	boolean_t nointr = (sb->sb_flags & SB_NOINTR);
538*1031c584SApple OSS Distributions 	void *lr_saved = __builtin_return_address(0);
539*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
540*1031c584SApple OSS Distributions 	lck_mtx_t *mutex_held;
541*1031c584SApple OSS Distributions 	struct timespec ts;
542*1031c584SApple OSS Distributions 	int error = 0;
543*1031c584SApple OSS Distributions 
544*1031c584SApple OSS Distributions 	if (so == NULL) {
545*1031c584SApple OSS Distributions 		panic("%s: null so, sb=%p sb_flags=0x%x lr=%p",
546*1031c584SApple OSS Distributions 		    __func__, sb, sb->sb_flags, lr_saved);
547*1031c584SApple OSS Distributions 		/* NOTREACHED */
548*1031c584SApple OSS Distributions 	} else if (so->so_usecount < 1) {
549*1031c584SApple OSS Distributions 		panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
550*1031c584SApple OSS Distributions 		    "lrh= %s\n", __func__, sb, sb->sb_flags, so,
551*1031c584SApple OSS Distributions 		    so->so_usecount, lr_saved, solockhistory_nr(so));
552*1031c584SApple OSS Distributions 		/* NOTREACHED */
553*1031c584SApple OSS Distributions 	}
554*1031c584SApple OSS Distributions 
555*1031c584SApple OSS Distributions 	if ((so->so_state & SS_DRAINING) || (so->so_flags & SOF_DEFUNCT)) {
556*1031c584SApple OSS Distributions 		error = EBADF;
557*1031c584SApple OSS Distributions 		if (so->so_flags & SOF_DEFUNCT) {
558*1031c584SApple OSS Distributions 			SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llu [%d,%d] "
559*1031c584SApple OSS Distributions 			    "(%d)\n", __func__, proc_selfpid(),
560*1031c584SApple OSS Distributions 			    proc_best_name(current_proc()),
561*1031c584SApple OSS Distributions 			    so->so_gencnt,
562*1031c584SApple OSS Distributions 			    SOCK_DOM(so), SOCK_TYPE(so), error);
563*1031c584SApple OSS Distributions 		}
564*1031c584SApple OSS Distributions 		return error;
565*1031c584SApple OSS Distributions 	}
566*1031c584SApple OSS Distributions 
567*1031c584SApple OSS Distributions 	if (so->so_proto->pr_getlock != NULL) {
568*1031c584SApple OSS Distributions 		mutex_held = (*so->so_proto->pr_getlock)(so, PR_F_WILLUNLOCK);
569*1031c584SApple OSS Distributions 	} else {
570*1031c584SApple OSS Distributions 		mutex_held = so->so_proto->pr_domain->dom_mtx;
571*1031c584SApple OSS Distributions 	}
572*1031c584SApple OSS Distributions 
573*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
574*1031c584SApple OSS Distributions 
575*1031c584SApple OSS Distributions 	ts.tv_sec = sb->sb_timeo.tv_sec;
576*1031c584SApple OSS Distributions 	ts.tv_nsec = sb->sb_timeo.tv_usec * 1000;
577*1031c584SApple OSS Distributions 
578*1031c584SApple OSS Distributions 	sb->sb_waiters++;
579*1031c584SApple OSS Distributions 	VERIFY(sb->sb_waiters != 0);
580*1031c584SApple OSS Distributions 
581*1031c584SApple OSS Distributions 	error = msleep((caddr_t)&sb->sb_cc, mutex_held,
582*1031c584SApple OSS Distributions 	    nointr ? PSOCK : PSOCK | PCATCH,
583*1031c584SApple OSS Distributions 	    nointr ? "sbwait_nointr" : "sbwait", &ts);
584*1031c584SApple OSS Distributions 
585*1031c584SApple OSS Distributions 	VERIFY(sb->sb_waiters != 0);
586*1031c584SApple OSS Distributions 	sb->sb_waiters--;
587*1031c584SApple OSS Distributions 
588*1031c584SApple OSS Distributions 	if (so->so_usecount < 1) {
589*1031c584SApple OSS Distributions 		panic("%s: 2 sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
590*1031c584SApple OSS Distributions 		    "lrh= %s\n", __func__, sb, sb->sb_flags, so,
591*1031c584SApple OSS Distributions 		    so->so_usecount, lr_saved, solockhistory_nr(so));
592*1031c584SApple OSS Distributions 		/* NOTREACHED */
593*1031c584SApple OSS Distributions 	}
594*1031c584SApple OSS Distributions 
595*1031c584SApple OSS Distributions 	if ((so->so_state & SS_DRAINING) || (so->so_flags & SOF_DEFUNCT)) {
596*1031c584SApple OSS Distributions 		error = EBADF;
597*1031c584SApple OSS Distributions 		if (so->so_flags & SOF_DEFUNCT) {
598*1031c584SApple OSS Distributions 			SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llu [%d,%d] "
599*1031c584SApple OSS Distributions 			    "(%d)\n", __func__, proc_selfpid(),
600*1031c584SApple OSS Distributions 			    proc_best_name(current_proc()),
601*1031c584SApple OSS Distributions 			    so->so_gencnt,
602*1031c584SApple OSS Distributions 			    SOCK_DOM(so), SOCK_TYPE(so), error);
603*1031c584SApple OSS Distributions 		}
604*1031c584SApple OSS Distributions 	}
605*1031c584SApple OSS Distributions 
606*1031c584SApple OSS Distributions 	return error;
607*1031c584SApple OSS Distributions }
608*1031c584SApple OSS Distributions 
609*1031c584SApple OSS Distributions void
sbwakeup(struct sockbuf * sb)610*1031c584SApple OSS Distributions sbwakeup(struct sockbuf *sb)
611*1031c584SApple OSS Distributions {
612*1031c584SApple OSS Distributions 	if (sb->sb_waiters > 0) {
613*1031c584SApple OSS Distributions 		wakeup((caddr_t)&sb->sb_cc);
614*1031c584SApple OSS Distributions 	}
615*1031c584SApple OSS Distributions }
616*1031c584SApple OSS Distributions 
617*1031c584SApple OSS Distributions /*
618*1031c584SApple OSS Distributions  * Wakeup processes waiting on a socket buffer.
619*1031c584SApple OSS Distributions  * Do asynchronous notification via SIGIO
620*1031c584SApple OSS Distributions  * if the socket has the SS_ASYNC flag set.
621*1031c584SApple OSS Distributions  */
622*1031c584SApple OSS Distributions void
sowakeup(struct socket * so,struct sockbuf * sb,struct socket * so2)623*1031c584SApple OSS Distributions sowakeup(struct socket *so, struct sockbuf *sb, struct socket *so2)
624*1031c584SApple OSS Distributions {
625*1031c584SApple OSS Distributions 	if (so->so_flags & SOF_DEFUNCT) {
626*1031c584SApple OSS Distributions 		SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llu [%d,%d] si 0x%x, "
627*1031c584SApple OSS Distributions 		    "fl 0x%x [%s]\n", __func__, proc_selfpid(),
628*1031c584SApple OSS Distributions 		    proc_best_name(current_proc()),
629*1031c584SApple OSS Distributions 		    so->so_gencnt, SOCK_DOM(so),
630*1031c584SApple OSS Distributions 		    SOCK_TYPE(so), (uint32_t)sb->sb_sel.si_flags, sb->sb_flags,
631*1031c584SApple OSS Distributions 		    (sb->sb_flags & SB_RECV) ? "rcv" : "snd");
632*1031c584SApple OSS Distributions 	}
633*1031c584SApple OSS Distributions 
634*1031c584SApple OSS Distributions 	sb->sb_flags &= ~SB_SEL;
635*1031c584SApple OSS Distributions 	selwakeup(&sb->sb_sel);
636*1031c584SApple OSS Distributions 	sbwakeup(sb);
637*1031c584SApple OSS Distributions 	if (so->so_state & SS_ASYNC) {
638*1031c584SApple OSS Distributions 		if (so->so_pgid < 0) {
639*1031c584SApple OSS Distributions 			gsignal(-so->so_pgid, SIGIO);
640*1031c584SApple OSS Distributions 		} else if (so->so_pgid > 0) {
641*1031c584SApple OSS Distributions 			proc_signal(so->so_pgid, SIGIO);
642*1031c584SApple OSS Distributions 		}
643*1031c584SApple OSS Distributions 	}
644*1031c584SApple OSS Distributions 	if (sb->sb_flags & SB_KNOTE) {
645*1031c584SApple OSS Distributions 		KNOTE(&sb->sb_sel.si_note, SO_FILT_HINT_LOCKED);
646*1031c584SApple OSS Distributions 	}
647*1031c584SApple OSS Distributions 	if (sb->sb_flags & SB_UPCALL) {
648*1031c584SApple OSS Distributions 		void (*sb_upcall)(struct socket *, void *, int);
649*1031c584SApple OSS Distributions 		caddr_t sb_upcallarg;
650*1031c584SApple OSS Distributions 		int lock = !(sb->sb_flags & SB_UPCALL_LOCK);
651*1031c584SApple OSS Distributions 
652*1031c584SApple OSS Distributions 		sb_upcall = sb->sb_upcall;
653*1031c584SApple OSS Distributions 		sb_upcallarg = sb->sb_upcallarg;
654*1031c584SApple OSS Distributions 		/* Let close know that we're about to do an upcall */
655*1031c584SApple OSS Distributions 		so->so_upcallusecount++;
656*1031c584SApple OSS Distributions 
657*1031c584SApple OSS Distributions 		if (lock) {
658*1031c584SApple OSS Distributions 			if (so2) {
659*1031c584SApple OSS Distributions 				struct unpcb *unp = sotounpcb(so2);
660*1031c584SApple OSS Distributions 				unp->unp_flags |= UNP_DONTDISCONNECT;
661*1031c584SApple OSS Distributions 				unp->rw_thrcount++;
662*1031c584SApple OSS Distributions 
663*1031c584SApple OSS Distributions 				socket_unlock(so2, 0);
664*1031c584SApple OSS Distributions 			}
665*1031c584SApple OSS Distributions 			socket_unlock(so, 0);
666*1031c584SApple OSS Distributions 		}
667*1031c584SApple OSS Distributions 		(*sb_upcall)(so, sb_upcallarg, M_DONTWAIT);
668*1031c584SApple OSS Distributions 		if (lock) {
669*1031c584SApple OSS Distributions 			if (so2 && so > so2) {
670*1031c584SApple OSS Distributions 				struct unpcb *unp;
671*1031c584SApple OSS Distributions 				socket_lock(so2, 0);
672*1031c584SApple OSS Distributions 
673*1031c584SApple OSS Distributions 				unp = sotounpcb(so2);
674*1031c584SApple OSS Distributions 				unp->rw_thrcount--;
675*1031c584SApple OSS Distributions 				if (unp->rw_thrcount == 0) {
676*1031c584SApple OSS Distributions 					unp->unp_flags &= ~UNP_DONTDISCONNECT;
677*1031c584SApple OSS Distributions 					wakeup(unp);
678*1031c584SApple OSS Distributions 				}
679*1031c584SApple OSS Distributions 			}
680*1031c584SApple OSS Distributions 
681*1031c584SApple OSS Distributions 			socket_lock(so, 0);
682*1031c584SApple OSS Distributions 
683*1031c584SApple OSS Distributions 			if (so2 && so < so2) {
684*1031c584SApple OSS Distributions 				struct unpcb *unp;
685*1031c584SApple OSS Distributions 				socket_lock(so2, 0);
686*1031c584SApple OSS Distributions 
687*1031c584SApple OSS Distributions 				unp = sotounpcb(so2);
688*1031c584SApple OSS Distributions 				unp->rw_thrcount--;
689*1031c584SApple OSS Distributions 				if (unp->rw_thrcount == 0) {
690*1031c584SApple OSS Distributions 					unp->unp_flags &= ~UNP_DONTDISCONNECT;
691*1031c584SApple OSS Distributions 					wakeup(unp);
692*1031c584SApple OSS Distributions 				}
693*1031c584SApple OSS Distributions 			}
694*1031c584SApple OSS Distributions 		}
695*1031c584SApple OSS Distributions 
696*1031c584SApple OSS Distributions 		so->so_upcallusecount--;
697*1031c584SApple OSS Distributions 		/* Tell close that it's safe to proceed */
698*1031c584SApple OSS Distributions 		if ((so->so_flags & SOF_CLOSEWAIT) &&
699*1031c584SApple OSS Distributions 		    so->so_upcallusecount == 0) {
700*1031c584SApple OSS Distributions 			wakeup((caddr_t)&so->so_upcallusecount);
701*1031c584SApple OSS Distributions 		}
702*1031c584SApple OSS Distributions 	}
703*1031c584SApple OSS Distributions #if CONTENT_FILTER
704*1031c584SApple OSS Distributions 	/*
705*1031c584SApple OSS Distributions 	 * Trap disconnection events for content filters
706*1031c584SApple OSS Distributions 	 */
707*1031c584SApple OSS Distributions 	if ((so->so_flags & SOF_CONTENT_FILTER) != 0) {
708*1031c584SApple OSS Distributions 		if ((sb->sb_flags & SB_RECV)) {
709*1031c584SApple OSS Distributions 			if (so->so_state & (SS_CANTRCVMORE)) {
710*1031c584SApple OSS Distributions 				cfil_sock_notify_shutdown(so, SHUT_RD);
711*1031c584SApple OSS Distributions 			}
712*1031c584SApple OSS Distributions 		} else {
713*1031c584SApple OSS Distributions 			if (so->so_state & (SS_CANTSENDMORE)) {
714*1031c584SApple OSS Distributions 				cfil_sock_notify_shutdown(so, SHUT_WR);
715*1031c584SApple OSS Distributions 			}
716*1031c584SApple OSS Distributions 		}
717*1031c584SApple OSS Distributions 	}
718*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
719*1031c584SApple OSS Distributions }
720*1031c584SApple OSS Distributions 
721*1031c584SApple OSS Distributions /*
722*1031c584SApple OSS Distributions  * Socket buffer (struct sockbuf) utility routines.
723*1031c584SApple OSS Distributions  *
724*1031c584SApple OSS Distributions  * Each socket contains two socket buffers: one for sending data and
725*1031c584SApple OSS Distributions  * one for receiving data.  Each buffer contains a queue of mbufs,
726*1031c584SApple OSS Distributions  * information about the number of mbufs and amount of data in the
727*1031c584SApple OSS Distributions  * queue, and other fields allowing select() statements and notification
728*1031c584SApple OSS Distributions  * on data availability to be implemented.
729*1031c584SApple OSS Distributions  *
730*1031c584SApple OSS Distributions  * Data stored in a socket buffer is maintained as a list of records.
731*1031c584SApple OSS Distributions  * Each record is a list of mbufs chained together with the m_next
732*1031c584SApple OSS Distributions  * field.  Records are chained together with the m_nextpkt field. The upper
733*1031c584SApple OSS Distributions  * level routine soreceive() expects the following conventions to be
734*1031c584SApple OSS Distributions  * observed when placing information in the receive buffer:
735*1031c584SApple OSS Distributions  *
736*1031c584SApple OSS Distributions  * 1. If the protocol requires each message be preceded by the sender's
737*1031c584SApple OSS Distributions  *    name, then a record containing that name must be present before
738*1031c584SApple OSS Distributions  *    any associated data (mbuf's must be of type MT_SONAME).
739*1031c584SApple OSS Distributions  * 2. If the protocol supports the exchange of ``access rights'' (really
740*1031c584SApple OSS Distributions  *    just additional data associated with the message), and there are
741*1031c584SApple OSS Distributions  *    ``rights'' to be received, then a record containing this data
742*1031c584SApple OSS Distributions  *    should be present (mbuf's must be of type MT_RIGHTS).
743*1031c584SApple OSS Distributions  * 3. If a name or rights record exists, then it must be followed by
744*1031c584SApple OSS Distributions  *    a data record, perhaps of zero length.
745*1031c584SApple OSS Distributions  *
746*1031c584SApple OSS Distributions  * Before using a new socket structure it is first necessary to reserve
747*1031c584SApple OSS Distributions  * buffer space to the socket, by calling sbreserve().  This should commit
748*1031c584SApple OSS Distributions  * some of the available buffer space in the system buffer pool for the
749*1031c584SApple OSS Distributions  * socket (currently, it does nothing but enforce limits).  The space
750*1031c584SApple OSS Distributions  * should be released by calling sbrelease() when the socket is destroyed.
751*1031c584SApple OSS Distributions  */
752*1031c584SApple OSS Distributions 
753*1031c584SApple OSS Distributions /*
754*1031c584SApple OSS Distributions  * Returns:	0			Success
755*1031c584SApple OSS Distributions  *		ENOBUFS
756*1031c584SApple OSS Distributions  */
757*1031c584SApple OSS Distributions int
soreserve(struct socket * so,uint32_t sndcc,uint32_t rcvcc)758*1031c584SApple OSS Distributions soreserve(struct socket *so, uint32_t sndcc, uint32_t rcvcc)
759*1031c584SApple OSS Distributions {
760*1031c584SApple OSS Distributions 	/*
761*1031c584SApple OSS Distributions 	 * We do not want to fail the creation of a socket
762*1031c584SApple OSS Distributions 	 * when kern.ipc.maxsockbuf is less than the
763*1031c584SApple OSS Distributions 	 * default socket buffer socket size of the protocol
764*1031c584SApple OSS Distributions 	 * so force the buffer sizes to be at most the
765*1031c584SApple OSS Distributions 	 * limit enforced by sbreserve()
766*1031c584SApple OSS Distributions 	 */
767*1031c584SApple OSS Distributions 	if (sndcc > sb_max_adj) {
768*1031c584SApple OSS Distributions 		sndcc = (uint32_t)sb_max_adj;
769*1031c584SApple OSS Distributions 	}
770*1031c584SApple OSS Distributions 	if (rcvcc > sb_max_adj) {
771*1031c584SApple OSS Distributions 		rcvcc = (uint32_t)sb_max_adj;
772*1031c584SApple OSS Distributions 	}
773*1031c584SApple OSS Distributions 	if (sbreserve(&so->so_snd, sndcc) == 0) {
774*1031c584SApple OSS Distributions 		goto bad;
775*1031c584SApple OSS Distributions 	} else {
776*1031c584SApple OSS Distributions 		so->so_snd.sb_idealsize = sndcc;
777*1031c584SApple OSS Distributions 	}
778*1031c584SApple OSS Distributions 
779*1031c584SApple OSS Distributions 	if (sbreserve(&so->so_rcv, rcvcc) == 0) {
780*1031c584SApple OSS Distributions 		goto bad2;
781*1031c584SApple OSS Distributions 	} else {
782*1031c584SApple OSS Distributions 		so->so_rcv.sb_idealsize = rcvcc;
783*1031c584SApple OSS Distributions 	}
784*1031c584SApple OSS Distributions 
785*1031c584SApple OSS Distributions 	if (so->so_rcv.sb_lowat == 0) {
786*1031c584SApple OSS Distributions 		so->so_rcv.sb_lowat = 1;
787*1031c584SApple OSS Distributions 	}
788*1031c584SApple OSS Distributions 	if (so->so_snd.sb_lowat == 0) {
789*1031c584SApple OSS Distributions 		so->so_snd.sb_lowat = MCLBYTES;
790*1031c584SApple OSS Distributions 	}
791*1031c584SApple OSS Distributions 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) {
792*1031c584SApple OSS Distributions 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
793*1031c584SApple OSS Distributions 	}
794*1031c584SApple OSS Distributions 	return 0;
795*1031c584SApple OSS Distributions bad2:
796*1031c584SApple OSS Distributions 	so->so_snd.sb_flags &= ~SB_SEL;
797*1031c584SApple OSS Distributions 	selthreadclear(&so->so_snd.sb_sel);
798*1031c584SApple OSS Distributions 	sbrelease(&so->so_snd);
799*1031c584SApple OSS Distributions bad:
800*1031c584SApple OSS Distributions 	return ENOBUFS;
801*1031c584SApple OSS Distributions }
802*1031c584SApple OSS Distributions 
803*1031c584SApple OSS Distributions void
soreserve_preconnect(struct socket * so,unsigned int pre_cc)804*1031c584SApple OSS Distributions soreserve_preconnect(struct socket *so, unsigned int pre_cc)
805*1031c584SApple OSS Distributions {
806*1031c584SApple OSS Distributions 	/* As of now, same bytes for both preconnect read and write */
807*1031c584SApple OSS Distributions 	so->so_snd.sb_preconn_hiwat = pre_cc;
808*1031c584SApple OSS Distributions 	so->so_rcv.sb_preconn_hiwat = pre_cc;
809*1031c584SApple OSS Distributions }
810*1031c584SApple OSS Distributions 
811*1031c584SApple OSS Distributions /*
812*1031c584SApple OSS Distributions  * Allot mbufs to a sockbuf.
813*1031c584SApple OSS Distributions  * Attempt to scale mbmax so that mbcnt doesn't become limiting
814*1031c584SApple OSS Distributions  * if buffering efficiency is near the normal case.
815*1031c584SApple OSS Distributions  */
816*1031c584SApple OSS Distributions int
sbreserve(struct sockbuf * sb,u_int32_t cc)817*1031c584SApple OSS Distributions sbreserve(struct sockbuf *sb, u_int32_t cc)
818*1031c584SApple OSS Distributions {
819*1031c584SApple OSS Distributions 	if ((u_quad_t)cc > (u_quad_t)sb_max_adj ||
820*1031c584SApple OSS Distributions 	    (cc > sb->sb_hiwat && (sb->sb_flags & SB_LIMITED))) {
821*1031c584SApple OSS Distributions 		return 0;
822*1031c584SApple OSS Distributions 	}
823*1031c584SApple OSS Distributions 	sb->sb_hiwat = cc;
824*1031c584SApple OSS Distributions 	sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
825*1031c584SApple OSS Distributions 	if (sb->sb_lowat > sb->sb_hiwat) {
826*1031c584SApple OSS Distributions 		sb->sb_lowat = sb->sb_hiwat;
827*1031c584SApple OSS Distributions 	}
828*1031c584SApple OSS Distributions 	return 1;
829*1031c584SApple OSS Distributions }
830*1031c584SApple OSS Distributions 
831*1031c584SApple OSS Distributions /*
832*1031c584SApple OSS Distributions  * Free mbufs held by a socket, and reserved mbuf space.
833*1031c584SApple OSS Distributions  */
834*1031c584SApple OSS Distributions /*  WARNING needs to do selthreadclear() before calling this */
835*1031c584SApple OSS Distributions void
sbrelease(struct sockbuf * sb)836*1031c584SApple OSS Distributions sbrelease(struct sockbuf *sb)
837*1031c584SApple OSS Distributions {
838*1031c584SApple OSS Distributions 	sbflush(sb);
839*1031c584SApple OSS Distributions 	sb->sb_hiwat = 0;
840*1031c584SApple OSS Distributions 	sb->sb_mbmax = 0;
841*1031c584SApple OSS Distributions }
842*1031c584SApple OSS Distributions 
843*1031c584SApple OSS Distributions /*
844*1031c584SApple OSS Distributions  * Routines to add and remove
845*1031c584SApple OSS Distributions  * data from an mbuf queue.
846*1031c584SApple OSS Distributions  *
847*1031c584SApple OSS Distributions  * The routines sbappend() or sbappendrecord() are normally called to
848*1031c584SApple OSS Distributions  * append new mbufs to a socket buffer, after checking that adequate
849*1031c584SApple OSS Distributions  * space is available, comparing the function sbspace() with the amount
850*1031c584SApple OSS Distributions  * of data to be added.  sbappendrecord() differs from sbappend() in
851*1031c584SApple OSS Distributions  * that data supplied is treated as the beginning of a new record.
852*1031c584SApple OSS Distributions  * To place a sender's address, optional access rights, and data in a
853*1031c584SApple OSS Distributions  * socket receive buffer, sbappendaddr() should be used.  To place
854*1031c584SApple OSS Distributions  * access rights and data in a socket receive buffer, sbappendrights()
855*1031c584SApple OSS Distributions  * should be used.  In either case, the new data begins a new record.
856*1031c584SApple OSS Distributions  * Note that unlike sbappend() and sbappendrecord(), these routines check
857*1031c584SApple OSS Distributions  * for the caller that there will be enough space to store the data.
858*1031c584SApple OSS Distributions  * Each fails if there is not enough space, or if it cannot find mbufs
859*1031c584SApple OSS Distributions  * to store additional information in.
860*1031c584SApple OSS Distributions  *
861*1031c584SApple OSS Distributions  * Reliable protocols may use the socket send buffer to hold data
862*1031c584SApple OSS Distributions  * awaiting acknowledgement.  Data is normally copied from a socket
863*1031c584SApple OSS Distributions  * send buffer in a protocol with m_copy for output to a peer,
864*1031c584SApple OSS Distributions  * and then removing the data from the socket buffer with sbdrop()
865*1031c584SApple OSS Distributions  * or sbdroprecord() when the data is acknowledged by the peer.
866*1031c584SApple OSS Distributions  */
867*1031c584SApple OSS Distributions 
868*1031c584SApple OSS Distributions /*
869*1031c584SApple OSS Distributions  * Append mbuf chain m to the last record in the
870*1031c584SApple OSS Distributions  * socket buffer sb.  The additional space associated
871*1031c584SApple OSS Distributions  * the mbuf chain is recorded in sb.  Empty mbufs are
872*1031c584SApple OSS Distributions  * discarded and mbufs are compacted where possible.
873*1031c584SApple OSS Distributions  */
874*1031c584SApple OSS Distributions static int
sbappend_common(struct sockbuf * sb,struct mbuf * m,boolean_t nodrop)875*1031c584SApple OSS Distributions sbappend_common(struct sockbuf *sb, struct mbuf *m, boolean_t nodrop)
876*1031c584SApple OSS Distributions {
877*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
878*1031c584SApple OSS Distributions 	struct soflow_hash_entry *dgram_flow_entry = NULL;
879*1031c584SApple OSS Distributions 
880*1031c584SApple OSS Distributions 	if (m == NULL || (sb->sb_flags & SB_DROP)) {
881*1031c584SApple OSS Distributions 		if (m != NULL && !nodrop) {
882*1031c584SApple OSS Distributions 			m_freem(m);
883*1031c584SApple OSS Distributions 		}
884*1031c584SApple OSS Distributions 		return 0;
885*1031c584SApple OSS Distributions 	}
886*1031c584SApple OSS Distributions 
887*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappend 1");
888*1031c584SApple OSS Distributions 
889*1031c584SApple OSS Distributions 	if (sb->sb_lastrecord != NULL && (sb->sb_mbtail->m_flags & M_EOR)) {
890*1031c584SApple OSS Distributions 		return sbappendrecord_common(sb, m, nodrop);
891*1031c584SApple OSS Distributions 	}
892*1031c584SApple OSS Distributions 
893*1031c584SApple OSS Distributions 	if (SOCK_DOM(sb->sb_so) == PF_INET || SOCK_DOM(sb->sb_so) == PF_INET6) {
894*1031c584SApple OSS Distributions 		ASSERT(nodrop == FALSE);
895*1031c584SApple OSS Distributions 
896*1031c584SApple OSS Distributions 		if (NEED_DGRAM_FLOW_TRACKING(so)) {
897*1031c584SApple OSS Distributions 			dgram_flow_entry = soflow_get_flow(so, NULL, NULL, NULL, m != NULL ? m_length(m) : 0, false, (m != NULL && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0);
898*1031c584SApple OSS Distributions 		}
899*1031c584SApple OSS Distributions 
900*1031c584SApple OSS Distributions 		if (sb->sb_flags & SB_RECV && !(m && m->m_flags & M_SKIPCFIL)) {
901*1031c584SApple OSS Distributions 			int error = sflt_data_in(so, NULL, &m, NULL, 0);
902*1031c584SApple OSS Distributions 			SBLASTRECORDCHK(sb, "sbappend 2");
903*1031c584SApple OSS Distributions 
904*1031c584SApple OSS Distributions #if CONTENT_FILTER
905*1031c584SApple OSS Distributions 			if (error == 0) {
906*1031c584SApple OSS Distributions 				error = cfil_sock_data_in(so, NULL, m, NULL, 0, dgram_flow_entry);
907*1031c584SApple OSS Distributions 			}
908*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
909*1031c584SApple OSS Distributions 
910*1031c584SApple OSS Distributions 			if (error != 0) {
911*1031c584SApple OSS Distributions 				if (error != EJUSTRETURN) {
912*1031c584SApple OSS Distributions 					m_freem(m);
913*1031c584SApple OSS Distributions 				}
914*1031c584SApple OSS Distributions 				if (dgram_flow_entry != NULL) {
915*1031c584SApple OSS Distributions 					soflow_free_flow(dgram_flow_entry);
916*1031c584SApple OSS Distributions 				}
917*1031c584SApple OSS Distributions 				return 0;
918*1031c584SApple OSS Distributions 			}
919*1031c584SApple OSS Distributions 		} else if (m) {
920*1031c584SApple OSS Distributions 			m->m_flags &= ~M_SKIPCFIL;
921*1031c584SApple OSS Distributions 		}
922*1031c584SApple OSS Distributions 
923*1031c584SApple OSS Distributions 		if (dgram_flow_entry != NULL) {
924*1031c584SApple OSS Distributions 			soflow_free_flow(dgram_flow_entry);
925*1031c584SApple OSS Distributions 		}
926*1031c584SApple OSS Distributions 	}
927*1031c584SApple OSS Distributions 
928*1031c584SApple OSS Distributions 	/* If this is the first record, it's also the last record */
929*1031c584SApple OSS Distributions 	if (sb->sb_lastrecord == NULL) {
930*1031c584SApple OSS Distributions 		sb->sb_lastrecord = m;
931*1031c584SApple OSS Distributions 	}
932*1031c584SApple OSS Distributions 
933*1031c584SApple OSS Distributions 	sbcompress(sb, m, sb->sb_mbtail);
934*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappend 3");
935*1031c584SApple OSS Distributions 	return 1;
936*1031c584SApple OSS Distributions }
937*1031c584SApple OSS Distributions 
938*1031c584SApple OSS Distributions int
sbappend(struct sockbuf * sb,struct mbuf * m)939*1031c584SApple OSS Distributions sbappend(struct sockbuf *sb, struct mbuf *m)
940*1031c584SApple OSS Distributions {
941*1031c584SApple OSS Distributions 	return sbappend_common(sb, m, FALSE);
942*1031c584SApple OSS Distributions }
943*1031c584SApple OSS Distributions 
944*1031c584SApple OSS Distributions int
sbappend_nodrop(struct sockbuf * sb,struct mbuf * m)945*1031c584SApple OSS Distributions sbappend_nodrop(struct sockbuf *sb, struct mbuf *m)
946*1031c584SApple OSS Distributions {
947*1031c584SApple OSS Distributions 	return sbappend_common(sb, m, TRUE);
948*1031c584SApple OSS Distributions }
949*1031c584SApple OSS Distributions 
950*1031c584SApple OSS Distributions /*
951*1031c584SApple OSS Distributions  * Similar to sbappend, except that this is optimized for stream sockets.
952*1031c584SApple OSS Distributions  */
953*1031c584SApple OSS Distributions int
sbappendstream(struct sockbuf * sb,struct mbuf * m)954*1031c584SApple OSS Distributions sbappendstream(struct sockbuf *sb, struct mbuf *m)
955*1031c584SApple OSS Distributions {
956*1031c584SApple OSS Distributions 	struct soflow_hash_entry *dgram_flow_entry = NULL;
957*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
958*1031c584SApple OSS Distributions 
959*1031c584SApple OSS Distributions 	if (m == NULL || (sb->sb_flags & SB_DROP)) {
960*1031c584SApple OSS Distributions 		if (m != NULL) {
961*1031c584SApple OSS Distributions 			m_freem(m);
962*1031c584SApple OSS Distributions 		}
963*1031c584SApple OSS Distributions 		return 0;
964*1031c584SApple OSS Distributions 	}
965*1031c584SApple OSS Distributions 
966*1031c584SApple OSS Distributions 	if (m->m_nextpkt != NULL || (sb->sb_mb != sb->sb_lastrecord)) {
967*1031c584SApple OSS Distributions 		panic("sbappendstream: nexpkt %p || mb %p != lastrecord %p",
968*1031c584SApple OSS Distributions 		    m->m_nextpkt, sb->sb_mb, sb->sb_lastrecord);
969*1031c584SApple OSS Distributions 		/* NOTREACHED */
970*1031c584SApple OSS Distributions 	}
971*1031c584SApple OSS Distributions 
972*1031c584SApple OSS Distributions 	SBLASTMBUFCHK(sb, __func__);
973*1031c584SApple OSS Distributions 
974*1031c584SApple OSS Distributions 	if (SOCK_DOM(sb->sb_so) == PF_INET || SOCK_DOM(sb->sb_so) == PF_INET6) {
975*1031c584SApple OSS Distributions 		if (NEED_DGRAM_FLOW_TRACKING(so)) {
976*1031c584SApple OSS Distributions 			dgram_flow_entry = soflow_get_flow(so, NULL, NULL, NULL, m != NULL ? m_length(m) : 0, false, (m != NULL && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0);
977*1031c584SApple OSS Distributions 		}
978*1031c584SApple OSS Distributions 
979*1031c584SApple OSS Distributions 		if (sb->sb_flags & SB_RECV && !(m && m->m_flags & M_SKIPCFIL)) {
980*1031c584SApple OSS Distributions 			int error = sflt_data_in(so, NULL, &m, NULL, 0);
981*1031c584SApple OSS Distributions 			SBLASTRECORDCHK(sb, "sbappendstream 1");
982*1031c584SApple OSS Distributions 
983*1031c584SApple OSS Distributions #if CONTENT_FILTER
984*1031c584SApple OSS Distributions 			if (error == 0) {
985*1031c584SApple OSS Distributions 				error = cfil_sock_data_in(so, NULL, m, NULL, 0, dgram_flow_entry);
986*1031c584SApple OSS Distributions 			}
987*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
988*1031c584SApple OSS Distributions 
989*1031c584SApple OSS Distributions 			if (error != 0) {
990*1031c584SApple OSS Distributions 				if (error != EJUSTRETURN) {
991*1031c584SApple OSS Distributions 					m_freem(m);
992*1031c584SApple OSS Distributions 				}
993*1031c584SApple OSS Distributions 				if (dgram_flow_entry != NULL) {
994*1031c584SApple OSS Distributions 					soflow_free_flow(dgram_flow_entry);
995*1031c584SApple OSS Distributions 				}
996*1031c584SApple OSS Distributions 				return 0;
997*1031c584SApple OSS Distributions 			}
998*1031c584SApple OSS Distributions 		} else if (m) {
999*1031c584SApple OSS Distributions 			m->m_flags &= ~M_SKIPCFIL;
1000*1031c584SApple OSS Distributions 		}
1001*1031c584SApple OSS Distributions 
1002*1031c584SApple OSS Distributions 		if (dgram_flow_entry != NULL) {
1003*1031c584SApple OSS Distributions 			soflow_free_flow(dgram_flow_entry);
1004*1031c584SApple OSS Distributions 		}
1005*1031c584SApple OSS Distributions 	}
1006*1031c584SApple OSS Distributions 
1007*1031c584SApple OSS Distributions 	sbcompress(sb, m, sb->sb_mbtail);
1008*1031c584SApple OSS Distributions 	sb->sb_lastrecord = sb->sb_mb;
1009*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappendstream 2");
1010*1031c584SApple OSS Distributions 	return 1;
1011*1031c584SApple OSS Distributions }
1012*1031c584SApple OSS Distributions 
1013*1031c584SApple OSS Distributions #ifdef SOCKBUF_DEBUG
1014*1031c584SApple OSS Distributions void
sbcheck(struct sockbuf * sb)1015*1031c584SApple OSS Distributions sbcheck(struct sockbuf *sb)
1016*1031c584SApple OSS Distributions {
1017*1031c584SApple OSS Distributions 	struct mbuf *m;
1018*1031c584SApple OSS Distributions 	struct mbuf *n = 0;
1019*1031c584SApple OSS Distributions 	u_int32_t len = 0, mbcnt = 0;
1020*1031c584SApple OSS Distributions 	lck_mtx_t *mutex_held;
1021*1031c584SApple OSS Distributions 
1022*1031c584SApple OSS Distributions 	if (sb->sb_so->so_proto->pr_getlock != NULL) {
1023*1031c584SApple OSS Distributions 		mutex_held = (*sb->sb_so->so_proto->pr_getlock)(sb->sb_so, 0);
1024*1031c584SApple OSS Distributions 	} else {
1025*1031c584SApple OSS Distributions 		mutex_held = sb->sb_so->so_proto->pr_domain->dom_mtx;
1026*1031c584SApple OSS Distributions 	}
1027*1031c584SApple OSS Distributions 
1028*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
1029*1031c584SApple OSS Distributions 
1030*1031c584SApple OSS Distributions 	if (sbchecking == 0) {
1031*1031c584SApple OSS Distributions 		return;
1032*1031c584SApple OSS Distributions 	}
1033*1031c584SApple OSS Distributions 
1034*1031c584SApple OSS Distributions 	for (m = sb->sb_mb; m; m = n) {
1035*1031c584SApple OSS Distributions 		n = m->m_nextpkt;
1036*1031c584SApple OSS Distributions 		for (; m; m = m->m_next) {
1037*1031c584SApple OSS Distributions 			len += m->m_len;
1038*1031c584SApple OSS Distributions 			mbcnt += _MSIZE;
1039*1031c584SApple OSS Distributions 			/* XXX pretty sure this is bogus */
1040*1031c584SApple OSS Distributions 			if (m->m_flags & M_EXT) {
1041*1031c584SApple OSS Distributions 				mbcnt += m->m_ext.ext_size;
1042*1031c584SApple OSS Distributions 			}
1043*1031c584SApple OSS Distributions 		}
1044*1031c584SApple OSS Distributions 	}
1045*1031c584SApple OSS Distributions 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
1046*1031c584SApple OSS Distributions 		panic("cc %ld != %ld || mbcnt %ld != %ld", len, sb->sb_cc,
1047*1031c584SApple OSS Distributions 		    mbcnt, sb->sb_mbcnt);
1048*1031c584SApple OSS Distributions 	}
1049*1031c584SApple OSS Distributions }
1050*1031c584SApple OSS Distributions #endif
1051*1031c584SApple OSS Distributions 
1052*1031c584SApple OSS Distributions void
sblastrecordchk(struct sockbuf * sb,const char * where)1053*1031c584SApple OSS Distributions sblastrecordchk(struct sockbuf *sb, const char *where)
1054*1031c584SApple OSS Distributions {
1055*1031c584SApple OSS Distributions 	struct mbuf *m = sb->sb_mb;
1056*1031c584SApple OSS Distributions 
1057*1031c584SApple OSS Distributions 	while (m && m->m_nextpkt) {
1058*1031c584SApple OSS Distributions 		m = m->m_nextpkt;
1059*1031c584SApple OSS Distributions 	}
1060*1031c584SApple OSS Distributions 
1061*1031c584SApple OSS Distributions 	if (m != sb->sb_lastrecord) {
1062*1031c584SApple OSS Distributions 		printf("sblastrecordchk: mb 0x%llx lastrecord 0x%llx "
1063*1031c584SApple OSS Distributions 		    "last 0x%llx\n",
1064*1031c584SApple OSS Distributions 		    (uint64_t)VM_KERNEL_ADDRPERM(sb->sb_mb),
1065*1031c584SApple OSS Distributions 		    (uint64_t)VM_KERNEL_ADDRPERM(sb->sb_lastrecord),
1066*1031c584SApple OSS Distributions 		    (uint64_t)VM_KERNEL_ADDRPERM(m));
1067*1031c584SApple OSS Distributions 		printf("packet chain:\n");
1068*1031c584SApple OSS Distributions 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
1069*1031c584SApple OSS Distributions 			printf("\t0x%llx\n", (uint64_t)VM_KERNEL_ADDRPERM(m));
1070*1031c584SApple OSS Distributions 		}
1071*1031c584SApple OSS Distributions 		panic("sblastrecordchk from %s", where);
1072*1031c584SApple OSS Distributions 	}
1073*1031c584SApple OSS Distributions }
1074*1031c584SApple OSS Distributions 
1075*1031c584SApple OSS Distributions void
sblastmbufchk(struct sockbuf * sb,const char * where)1076*1031c584SApple OSS Distributions sblastmbufchk(struct sockbuf *sb, const char *where)
1077*1031c584SApple OSS Distributions {
1078*1031c584SApple OSS Distributions 	struct mbuf *m = sb->sb_mb;
1079*1031c584SApple OSS Distributions 	struct mbuf *n;
1080*1031c584SApple OSS Distributions 
1081*1031c584SApple OSS Distributions 	while (m && m->m_nextpkt) {
1082*1031c584SApple OSS Distributions 		m = m->m_nextpkt;
1083*1031c584SApple OSS Distributions 	}
1084*1031c584SApple OSS Distributions 
1085*1031c584SApple OSS Distributions 	while (m && m->m_next) {
1086*1031c584SApple OSS Distributions 		m = m->m_next;
1087*1031c584SApple OSS Distributions 	}
1088*1031c584SApple OSS Distributions 
1089*1031c584SApple OSS Distributions 	if (m != sb->sb_mbtail) {
1090*1031c584SApple OSS Distributions 		printf("sblastmbufchk: mb 0x%llx mbtail 0x%llx last 0x%llx\n",
1091*1031c584SApple OSS Distributions 		    (uint64_t)VM_KERNEL_ADDRPERM(sb->sb_mb),
1092*1031c584SApple OSS Distributions 		    (uint64_t)VM_KERNEL_ADDRPERM(sb->sb_mbtail),
1093*1031c584SApple OSS Distributions 		    (uint64_t)VM_KERNEL_ADDRPERM(m));
1094*1031c584SApple OSS Distributions 		printf("packet tree:\n");
1095*1031c584SApple OSS Distributions 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
1096*1031c584SApple OSS Distributions 			printf("\t");
1097*1031c584SApple OSS Distributions 			for (n = m; n != NULL; n = n->m_next) {
1098*1031c584SApple OSS Distributions 				printf("0x%llx ",
1099*1031c584SApple OSS Distributions 				    (uint64_t)VM_KERNEL_ADDRPERM(n));
1100*1031c584SApple OSS Distributions 			}
1101*1031c584SApple OSS Distributions 			printf("\n");
1102*1031c584SApple OSS Distributions 		}
1103*1031c584SApple OSS Distributions 		panic("sblastmbufchk from %s", where);
1104*1031c584SApple OSS Distributions 	}
1105*1031c584SApple OSS Distributions }
1106*1031c584SApple OSS Distributions 
1107*1031c584SApple OSS Distributions /*
1108*1031c584SApple OSS Distributions  * Similar to sbappend, except the mbuf chain begins a new record.
1109*1031c584SApple OSS Distributions  */
1110*1031c584SApple OSS Distributions static int
sbappendrecord_common(struct sockbuf * sb,struct mbuf * m0,boolean_t nodrop)1111*1031c584SApple OSS Distributions sbappendrecord_common(struct sockbuf *sb, struct mbuf *m0, boolean_t nodrop)
1112*1031c584SApple OSS Distributions {
1113*1031c584SApple OSS Distributions 	struct soflow_hash_entry *dgram_flow_entry = NULL;
1114*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
1115*1031c584SApple OSS Distributions 	struct mbuf *m;
1116*1031c584SApple OSS Distributions 	int space = 0;
1117*1031c584SApple OSS Distributions 
1118*1031c584SApple OSS Distributions 	if (m0 == NULL || (sb->sb_flags & SB_DROP)) {
1119*1031c584SApple OSS Distributions 		if (m0 != NULL && nodrop == FALSE) {
1120*1031c584SApple OSS Distributions 			m_freem(m0);
1121*1031c584SApple OSS Distributions 		}
1122*1031c584SApple OSS Distributions 		return 0;
1123*1031c584SApple OSS Distributions 	}
1124*1031c584SApple OSS Distributions 
1125*1031c584SApple OSS Distributions 	for (m = m0; m != NULL; m = m->m_next) {
1126*1031c584SApple OSS Distributions 		space += m->m_len;
1127*1031c584SApple OSS Distributions 	}
1128*1031c584SApple OSS Distributions 
1129*1031c584SApple OSS Distributions 	if (space > sbspace(sb) && !(sb->sb_flags & SB_UNIX)) {
1130*1031c584SApple OSS Distributions 		if (nodrop == FALSE) {
1131*1031c584SApple OSS Distributions 			m_freem(m0);
1132*1031c584SApple OSS Distributions 		}
1133*1031c584SApple OSS Distributions 		return 0;
1134*1031c584SApple OSS Distributions 	}
1135*1031c584SApple OSS Distributions 
1136*1031c584SApple OSS Distributions 	if (SOCK_DOM(sb->sb_so) == PF_INET || SOCK_DOM(sb->sb_so) == PF_INET6) {
1137*1031c584SApple OSS Distributions 		ASSERT(nodrop == FALSE);
1138*1031c584SApple OSS Distributions 
1139*1031c584SApple OSS Distributions 		if (NEED_DGRAM_FLOW_TRACKING(so)) {
1140*1031c584SApple OSS Distributions 			dgram_flow_entry = soflow_get_flow(so, NULL, NULL, NULL, m0 != NULL ? m_length(m0) : 0, false, (m0 != NULL && m0->m_pkthdr.rcvif) ? m0->m_pkthdr.rcvif->if_index : 0);
1141*1031c584SApple OSS Distributions 		}
1142*1031c584SApple OSS Distributions 
1143*1031c584SApple OSS Distributions 		if (sb->sb_flags & SB_RECV && !(m0 && m0->m_flags & M_SKIPCFIL)) {
1144*1031c584SApple OSS Distributions 			int error = sflt_data_in(sb->sb_so, NULL, &m0, NULL,
1145*1031c584SApple OSS Distributions 			    sock_data_filt_flag_record);
1146*1031c584SApple OSS Distributions 
1147*1031c584SApple OSS Distributions #if CONTENT_FILTER
1148*1031c584SApple OSS Distributions 			if (error == 0) {
1149*1031c584SApple OSS Distributions 				error = cfil_sock_data_in(sb->sb_so, NULL, m0, NULL, 0, dgram_flow_entry);
1150*1031c584SApple OSS Distributions 			}
1151*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
1152*1031c584SApple OSS Distributions 
1153*1031c584SApple OSS Distributions 			if (error != 0) {
1154*1031c584SApple OSS Distributions 				SBLASTRECORDCHK(sb, "sbappendrecord 1");
1155*1031c584SApple OSS Distributions 				if (error != EJUSTRETURN) {
1156*1031c584SApple OSS Distributions 					m_freem(m0);
1157*1031c584SApple OSS Distributions 				}
1158*1031c584SApple OSS Distributions 				if (dgram_flow_entry != NULL) {
1159*1031c584SApple OSS Distributions 					soflow_free_flow(dgram_flow_entry);
1160*1031c584SApple OSS Distributions 				}
1161*1031c584SApple OSS Distributions 				return 0;
1162*1031c584SApple OSS Distributions 			}
1163*1031c584SApple OSS Distributions 		} else if (m0) {
1164*1031c584SApple OSS Distributions 			m0->m_flags &= ~M_SKIPCFIL;
1165*1031c584SApple OSS Distributions 		}
1166*1031c584SApple OSS Distributions 
1167*1031c584SApple OSS Distributions 		if (dgram_flow_entry != NULL) {
1168*1031c584SApple OSS Distributions 			soflow_free_flow(dgram_flow_entry);
1169*1031c584SApple OSS Distributions 		}
1170*1031c584SApple OSS Distributions 	}
1171*1031c584SApple OSS Distributions 
1172*1031c584SApple OSS Distributions 	/*
1173*1031c584SApple OSS Distributions 	 * Note this permits zero length records.
1174*1031c584SApple OSS Distributions 	 */
1175*1031c584SApple OSS Distributions 	sballoc(sb, m0);
1176*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappendrecord 2");
1177*1031c584SApple OSS Distributions 	if (sb->sb_lastrecord != NULL) {
1178*1031c584SApple OSS Distributions 		sb->sb_lastrecord->m_nextpkt = m0;
1179*1031c584SApple OSS Distributions 	} else {
1180*1031c584SApple OSS Distributions 		sb->sb_mb = m0;
1181*1031c584SApple OSS Distributions 	}
1182*1031c584SApple OSS Distributions 	sb->sb_lastrecord = m0;
1183*1031c584SApple OSS Distributions 	sb->sb_mbtail = m0;
1184*1031c584SApple OSS Distributions 
1185*1031c584SApple OSS Distributions 	m = m0->m_next;
1186*1031c584SApple OSS Distributions 	m0->m_next = 0;
1187*1031c584SApple OSS Distributions 	if (m && (m0->m_flags & M_EOR)) {
1188*1031c584SApple OSS Distributions 		m0->m_flags &= ~M_EOR;
1189*1031c584SApple OSS Distributions 		m->m_flags |= M_EOR;
1190*1031c584SApple OSS Distributions 	}
1191*1031c584SApple OSS Distributions 	sbcompress(sb, m, m0);
1192*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappendrecord 3");
1193*1031c584SApple OSS Distributions 	return 1;
1194*1031c584SApple OSS Distributions }
1195*1031c584SApple OSS Distributions 
1196*1031c584SApple OSS Distributions int
sbappendrecord(struct sockbuf * sb,struct mbuf * m0)1197*1031c584SApple OSS Distributions sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
1198*1031c584SApple OSS Distributions {
1199*1031c584SApple OSS Distributions 	return sbappendrecord_common(sb, m0, FALSE);
1200*1031c584SApple OSS Distributions }
1201*1031c584SApple OSS Distributions 
1202*1031c584SApple OSS Distributions int
sbappendrecord_nodrop(struct sockbuf * sb,struct mbuf * m0)1203*1031c584SApple OSS Distributions sbappendrecord_nodrop(struct sockbuf *sb, struct mbuf *m0)
1204*1031c584SApple OSS Distributions {
1205*1031c584SApple OSS Distributions 	return sbappendrecord_common(sb, m0, TRUE);
1206*1031c584SApple OSS Distributions }
1207*1031c584SApple OSS Distributions 
1208*1031c584SApple OSS Distributions /*
1209*1031c584SApple OSS Distributions  * Concatenate address (optional), control (optional) and data into one
1210*1031c584SApple OSS Distributions  * single mbuf chain.  If sockbuf *sb is passed in, space check will be
1211*1031c584SApple OSS Distributions  * performed.
1212*1031c584SApple OSS Distributions  *
1213*1031c584SApple OSS Distributions  * Returns:	mbuf chain pointer if succeeded, NULL if failed
1214*1031c584SApple OSS Distributions  */
1215*1031c584SApple OSS Distributions struct mbuf *
sbconcat_mbufs(struct sockbuf * sb,struct sockaddr * asa,struct mbuf * m0,struct mbuf * control)1216*1031c584SApple OSS Distributions sbconcat_mbufs(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0, struct mbuf *control)
1217*1031c584SApple OSS Distributions {
1218*1031c584SApple OSS Distributions 	struct mbuf *m = NULL, *n = NULL;
1219*1031c584SApple OSS Distributions 	int space = 0;
1220*1031c584SApple OSS Distributions 
1221*1031c584SApple OSS Distributions 	if (m0 && (m0->m_flags & M_PKTHDR) == 0) {
1222*1031c584SApple OSS Distributions 		panic("sbconcat_mbufs");
1223*1031c584SApple OSS Distributions 	}
1224*1031c584SApple OSS Distributions 
1225*1031c584SApple OSS Distributions 	if (m0) {
1226*1031c584SApple OSS Distributions 		space += m0->m_pkthdr.len;
1227*1031c584SApple OSS Distributions 	}
1228*1031c584SApple OSS Distributions 	for (n = control; n; n = n->m_next) {
1229*1031c584SApple OSS Distributions 		space += n->m_len;
1230*1031c584SApple OSS Distributions 		if (n->m_next == 0) {   /* keep pointer to last control buf */
1231*1031c584SApple OSS Distributions 			break;
1232*1031c584SApple OSS Distributions 		}
1233*1031c584SApple OSS Distributions 	}
1234*1031c584SApple OSS Distributions 
1235*1031c584SApple OSS Distributions 	if (asa != NULL) {
1236*1031c584SApple OSS Distributions 		_CASSERT(sizeof(asa->sa_len) == sizeof(__uint8_t));
1237*1031c584SApple OSS Distributions #if _MSIZE <= UINT8_MAX
1238*1031c584SApple OSS Distributions 		if (asa->sa_len > MLEN) {
1239*1031c584SApple OSS Distributions 			return NULL;
1240*1031c584SApple OSS Distributions 		}
1241*1031c584SApple OSS Distributions #endif
1242*1031c584SApple OSS Distributions 		_CASSERT(sizeof(asa->sa_len) == sizeof(__uint8_t));
1243*1031c584SApple OSS Distributions 		space += asa->sa_len;
1244*1031c584SApple OSS Distributions 	}
1245*1031c584SApple OSS Distributions 
1246*1031c584SApple OSS Distributions 	if (sb != NULL && space > sbspace(sb)) {
1247*1031c584SApple OSS Distributions 		return NULL;
1248*1031c584SApple OSS Distributions 	}
1249*1031c584SApple OSS Distributions 
1250*1031c584SApple OSS Distributions 	if (n) {
1251*1031c584SApple OSS Distributions 		n->m_next = m0;         /* concatenate data to control */
1252*1031c584SApple OSS Distributions 	} else {
1253*1031c584SApple OSS Distributions 		control = m0;
1254*1031c584SApple OSS Distributions 	}
1255*1031c584SApple OSS Distributions 
1256*1031c584SApple OSS Distributions 	if (asa != NULL) {
1257*1031c584SApple OSS Distributions 		MGET(m, M_DONTWAIT, MT_SONAME);
1258*1031c584SApple OSS Distributions 		if (m == 0) {
1259*1031c584SApple OSS Distributions 			if (n) {
1260*1031c584SApple OSS Distributions 				/* unchain control and data if necessary */
1261*1031c584SApple OSS Distributions 				n->m_next = NULL;
1262*1031c584SApple OSS Distributions 			}
1263*1031c584SApple OSS Distributions 			return NULL;
1264*1031c584SApple OSS Distributions 		}
1265*1031c584SApple OSS Distributions 		m->m_len = asa->sa_len;
1266*1031c584SApple OSS Distributions 		bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
1267*1031c584SApple OSS Distributions 
1268*1031c584SApple OSS Distributions 		m->m_next = control;
1269*1031c584SApple OSS Distributions 	} else {
1270*1031c584SApple OSS Distributions 		m = control;
1271*1031c584SApple OSS Distributions 	}
1272*1031c584SApple OSS Distributions 
1273*1031c584SApple OSS Distributions 	return m;
1274*1031c584SApple OSS Distributions }
1275*1031c584SApple OSS Distributions 
1276*1031c584SApple OSS Distributions /*
1277*1031c584SApple OSS Distributions  * Queue mbuf chain to the receive queue of a socket.
1278*1031c584SApple OSS Distributions  * Parameter space is the total len of the mbuf chain.
1279*1031c584SApple OSS Distributions  * If passed in, sockbuf space will be checked.
1280*1031c584SApple OSS Distributions  *
1281*1031c584SApple OSS Distributions  * Returns:	0		Invalid mbuf chain
1282*1031c584SApple OSS Distributions  *			1		Success
1283*1031c584SApple OSS Distributions  */
1284*1031c584SApple OSS Distributions int
sbappendchain(struct sockbuf * sb,struct mbuf * m,int space)1285*1031c584SApple OSS Distributions sbappendchain(struct sockbuf *sb, struct mbuf *m, int space)
1286*1031c584SApple OSS Distributions {
1287*1031c584SApple OSS Distributions 	struct mbuf *n, *nlast;
1288*1031c584SApple OSS Distributions 
1289*1031c584SApple OSS Distributions 	if (m == NULL) {
1290*1031c584SApple OSS Distributions 		return 0;
1291*1031c584SApple OSS Distributions 	}
1292*1031c584SApple OSS Distributions 
1293*1031c584SApple OSS Distributions 	if (space != 0 && space > sbspace(sb)) {
1294*1031c584SApple OSS Distributions 		return 0;
1295*1031c584SApple OSS Distributions 	}
1296*1031c584SApple OSS Distributions 
1297*1031c584SApple OSS Distributions 	for (n = m; n->m_next != NULL; n = n->m_next) {
1298*1031c584SApple OSS Distributions 		sballoc(sb, n);
1299*1031c584SApple OSS Distributions 	}
1300*1031c584SApple OSS Distributions 	sballoc(sb, n);
1301*1031c584SApple OSS Distributions 	nlast = n;
1302*1031c584SApple OSS Distributions 
1303*1031c584SApple OSS Distributions 	if (sb->sb_lastrecord != NULL) {
1304*1031c584SApple OSS Distributions 		sb->sb_lastrecord->m_nextpkt = m;
1305*1031c584SApple OSS Distributions 	} else {
1306*1031c584SApple OSS Distributions 		sb->sb_mb = m;
1307*1031c584SApple OSS Distributions 	}
1308*1031c584SApple OSS Distributions 	sb->sb_lastrecord = m;
1309*1031c584SApple OSS Distributions 	sb->sb_mbtail = nlast;
1310*1031c584SApple OSS Distributions 
1311*1031c584SApple OSS Distributions 	SBLASTMBUFCHK(sb, __func__);
1312*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappendadddr 2");
1313*1031c584SApple OSS Distributions 	return 1;
1314*1031c584SApple OSS Distributions }
1315*1031c584SApple OSS Distributions 
1316*1031c584SApple OSS Distributions /*
1317*1031c584SApple OSS Distributions  * Returns:	0			Error: No space/out of mbufs/etc.
1318*1031c584SApple OSS Distributions  *		1			Success
1319*1031c584SApple OSS Distributions  *
1320*1031c584SApple OSS Distributions  * Imputed:	(*error_out)		errno for error
1321*1031c584SApple OSS Distributions  *		ENOBUFS
1322*1031c584SApple OSS Distributions  *	sflt_data_in:???		[whatever a filter author chooses]
1323*1031c584SApple OSS Distributions  */
1324*1031c584SApple OSS Distributions int
sbappendaddr(struct sockbuf * sb,struct sockaddr * asa,struct mbuf * m0,struct mbuf * control,int * error_out)1325*1031c584SApple OSS Distributions sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0,
1326*1031c584SApple OSS Distributions     struct mbuf *control, int *error_out)
1327*1031c584SApple OSS Distributions {
1328*1031c584SApple OSS Distributions 	int result = 0;
1329*1031c584SApple OSS Distributions 	boolean_t sb_unix = (sb->sb_flags & SB_UNIX);
1330*1031c584SApple OSS Distributions 	struct mbuf *mbuf_chain = NULL;
1331*1031c584SApple OSS Distributions 	struct soflow_hash_entry *dgram_flow_entry = NULL;
1332*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
1333*1031c584SApple OSS Distributions 
1334*1031c584SApple OSS Distributions 	if (error_out) {
1335*1031c584SApple OSS Distributions 		*error_out = 0;
1336*1031c584SApple OSS Distributions 	}
1337*1031c584SApple OSS Distributions 
1338*1031c584SApple OSS Distributions 	if (m0 && (m0->m_flags & M_PKTHDR) == 0) {
1339*1031c584SApple OSS Distributions 		panic("sbappendaddrorfree");
1340*1031c584SApple OSS Distributions 	}
1341*1031c584SApple OSS Distributions 
1342*1031c584SApple OSS Distributions 	if (sb->sb_flags & SB_DROP) {
1343*1031c584SApple OSS Distributions 		if (m0 != NULL) {
1344*1031c584SApple OSS Distributions 			m_freem(m0);
1345*1031c584SApple OSS Distributions 		}
1346*1031c584SApple OSS Distributions 		if (control != NULL && !sb_unix) {
1347*1031c584SApple OSS Distributions 			m_freem(control);
1348*1031c584SApple OSS Distributions 		}
1349*1031c584SApple OSS Distributions 		if (error_out != NULL) {
1350*1031c584SApple OSS Distributions 			*error_out = EINVAL;
1351*1031c584SApple OSS Distributions 		}
1352*1031c584SApple OSS Distributions 		return 0;
1353*1031c584SApple OSS Distributions 	}
1354*1031c584SApple OSS Distributions 
1355*1031c584SApple OSS Distributions 	if (SOCK_DOM(sb->sb_so) == PF_INET || SOCK_DOM(sb->sb_so) == PF_INET6) {
1356*1031c584SApple OSS Distributions 		/* Call socket data in filters */
1357*1031c584SApple OSS Distributions 
1358*1031c584SApple OSS Distributions 		if (NEED_DGRAM_FLOW_TRACKING(so)) {
1359*1031c584SApple OSS Distributions 			dgram_flow_entry = soflow_get_flow(so, NULL, asa, control, m0 != NULL ? m_length(m0) : 0, false, (m0 != NULL && m0->m_pkthdr.rcvif) ? m0->m_pkthdr.rcvif->if_index : 0);
1360*1031c584SApple OSS Distributions 		}
1361*1031c584SApple OSS Distributions 
1362*1031c584SApple OSS Distributions 		if (sb->sb_flags & SB_RECV && !(m0 && m0->m_flags & M_SKIPCFIL)) {
1363*1031c584SApple OSS Distributions 			int error;
1364*1031c584SApple OSS Distributions 			error = sflt_data_in(sb->sb_so, asa, &m0, &control, 0);
1365*1031c584SApple OSS Distributions 			SBLASTRECORDCHK(sb, __func__);
1366*1031c584SApple OSS Distributions 
1367*1031c584SApple OSS Distributions #if CONTENT_FILTER
1368*1031c584SApple OSS Distributions 			if (error == 0) {
1369*1031c584SApple OSS Distributions 				error = cfil_sock_data_in(sb->sb_so, asa, m0, control,
1370*1031c584SApple OSS Distributions 				    0, dgram_flow_entry);
1371*1031c584SApple OSS Distributions 			}
1372*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
1373*1031c584SApple OSS Distributions 
1374*1031c584SApple OSS Distributions 			if (error) {
1375*1031c584SApple OSS Distributions 				if (error != EJUSTRETURN) {
1376*1031c584SApple OSS Distributions 					if (m0) {
1377*1031c584SApple OSS Distributions 						m_freem(m0);
1378*1031c584SApple OSS Distributions 					}
1379*1031c584SApple OSS Distributions 					if (control != NULL && !sb_unix) {
1380*1031c584SApple OSS Distributions 						m_freem(control);
1381*1031c584SApple OSS Distributions 					}
1382*1031c584SApple OSS Distributions 					if (error_out) {
1383*1031c584SApple OSS Distributions 						*error_out = error;
1384*1031c584SApple OSS Distributions 					}
1385*1031c584SApple OSS Distributions 				}
1386*1031c584SApple OSS Distributions 				if (dgram_flow_entry != NULL) {
1387*1031c584SApple OSS Distributions 					soflow_free_flow(dgram_flow_entry);
1388*1031c584SApple OSS Distributions 				}
1389*1031c584SApple OSS Distributions 				return 0;
1390*1031c584SApple OSS Distributions 			}
1391*1031c584SApple OSS Distributions 		} else if (m0) {
1392*1031c584SApple OSS Distributions 			m0->m_flags &= ~M_SKIPCFIL;
1393*1031c584SApple OSS Distributions 		}
1394*1031c584SApple OSS Distributions 
1395*1031c584SApple OSS Distributions 		if (dgram_flow_entry != NULL) {
1396*1031c584SApple OSS Distributions 			soflow_free_flow(dgram_flow_entry);
1397*1031c584SApple OSS Distributions 		}
1398*1031c584SApple OSS Distributions 	}
1399*1031c584SApple OSS Distributions 
1400*1031c584SApple OSS Distributions 	mbuf_chain = sbconcat_mbufs(sb, asa, m0, control);
1401*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappendadddr 1");
1402*1031c584SApple OSS Distributions 	result = sbappendchain(sb, mbuf_chain, 0);
1403*1031c584SApple OSS Distributions 	if (result == 0) {
1404*1031c584SApple OSS Distributions 		if (m0) {
1405*1031c584SApple OSS Distributions 			m_freem(m0);
1406*1031c584SApple OSS Distributions 		}
1407*1031c584SApple OSS Distributions 		if (control != NULL && !sb_unix) {
1408*1031c584SApple OSS Distributions 			m_freem(control);
1409*1031c584SApple OSS Distributions 		}
1410*1031c584SApple OSS Distributions 		if (error_out) {
1411*1031c584SApple OSS Distributions 			*error_out = ENOBUFS;
1412*1031c584SApple OSS Distributions 		}
1413*1031c584SApple OSS Distributions 	}
1414*1031c584SApple OSS Distributions 
1415*1031c584SApple OSS Distributions 	return result;
1416*1031c584SApple OSS Distributions }
1417*1031c584SApple OSS Distributions 
1418*1031c584SApple OSS Distributions inline boolean_t
is_cmsg_valid(struct mbuf * control,struct cmsghdr * cmsg)1419*1031c584SApple OSS Distributions is_cmsg_valid(struct mbuf *control, struct cmsghdr *cmsg)
1420*1031c584SApple OSS Distributions {
1421*1031c584SApple OSS Distributions 	if (cmsg == NULL) {
1422*1031c584SApple OSS Distributions 		return FALSE;
1423*1031c584SApple OSS Distributions 	}
1424*1031c584SApple OSS Distributions 
1425*1031c584SApple OSS Distributions 	if (cmsg->cmsg_len < sizeof(struct cmsghdr)) {
1426*1031c584SApple OSS Distributions 		return FALSE;
1427*1031c584SApple OSS Distributions 	}
1428*1031c584SApple OSS Distributions 
1429*1031c584SApple OSS Distributions 	if ((uint8_t *)control->m_data >= (uint8_t *)cmsg + cmsg->cmsg_len) {
1430*1031c584SApple OSS Distributions 		return FALSE;
1431*1031c584SApple OSS Distributions 	}
1432*1031c584SApple OSS Distributions 
1433*1031c584SApple OSS Distributions 	if ((uint8_t *)control->m_data + control->m_len <
1434*1031c584SApple OSS Distributions 	    (uint8_t *)cmsg + cmsg->cmsg_len) {
1435*1031c584SApple OSS Distributions 		return FALSE;
1436*1031c584SApple OSS Distributions 	}
1437*1031c584SApple OSS Distributions 
1438*1031c584SApple OSS Distributions 	return TRUE;
1439*1031c584SApple OSS Distributions }
1440*1031c584SApple OSS Distributions 
1441*1031c584SApple OSS Distributions static int
sbappendcontrol_internal(struct sockbuf * sb,struct mbuf * m0,struct mbuf * control)1442*1031c584SApple OSS Distributions sbappendcontrol_internal(struct sockbuf *sb, struct mbuf *m0,
1443*1031c584SApple OSS Distributions     struct mbuf *control)
1444*1031c584SApple OSS Distributions {
1445*1031c584SApple OSS Distributions 	struct mbuf *m, *mlast, *n;
1446*1031c584SApple OSS Distributions 	int space = 0;
1447*1031c584SApple OSS Distributions 
1448*1031c584SApple OSS Distributions 	if (control == 0) {
1449*1031c584SApple OSS Distributions 		panic("sbappendcontrol");
1450*1031c584SApple OSS Distributions 	}
1451*1031c584SApple OSS Distributions 
1452*1031c584SApple OSS Distributions 	for (m = control;; m = m->m_next) {
1453*1031c584SApple OSS Distributions 		space += m->m_len;
1454*1031c584SApple OSS Distributions 		if (m->m_next == 0) {
1455*1031c584SApple OSS Distributions 			break;
1456*1031c584SApple OSS Distributions 		}
1457*1031c584SApple OSS Distributions 	}
1458*1031c584SApple OSS Distributions 	n = m;                  /* save pointer to last control buffer */
1459*1031c584SApple OSS Distributions 	for (m = m0; m; m = m->m_next) {
1460*1031c584SApple OSS Distributions 		space += m->m_len;
1461*1031c584SApple OSS Distributions 	}
1462*1031c584SApple OSS Distributions 	if (space > sbspace(sb) && !(sb->sb_flags & SB_UNIX)) {
1463*1031c584SApple OSS Distributions 		return 0;
1464*1031c584SApple OSS Distributions 	}
1465*1031c584SApple OSS Distributions 	n->m_next = m0;                 /* concatenate data to control */
1466*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappendcontrol 1");
1467*1031c584SApple OSS Distributions 
1468*1031c584SApple OSS Distributions 	for (m = control; m->m_next != NULL; m = m->m_next) {
1469*1031c584SApple OSS Distributions 		sballoc(sb, m);
1470*1031c584SApple OSS Distributions 	}
1471*1031c584SApple OSS Distributions 	sballoc(sb, m);
1472*1031c584SApple OSS Distributions 	mlast = m;
1473*1031c584SApple OSS Distributions 
1474*1031c584SApple OSS Distributions 	if (sb->sb_lastrecord != NULL) {
1475*1031c584SApple OSS Distributions 		sb->sb_lastrecord->m_nextpkt = control;
1476*1031c584SApple OSS Distributions 	} else {
1477*1031c584SApple OSS Distributions 		sb->sb_mb = control;
1478*1031c584SApple OSS Distributions 	}
1479*1031c584SApple OSS Distributions 	sb->sb_lastrecord = control;
1480*1031c584SApple OSS Distributions 	sb->sb_mbtail = mlast;
1481*1031c584SApple OSS Distributions 
1482*1031c584SApple OSS Distributions 	SBLASTMBUFCHK(sb, __func__);
1483*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, "sbappendcontrol 2");
1484*1031c584SApple OSS Distributions 	return 1;
1485*1031c584SApple OSS Distributions }
1486*1031c584SApple OSS Distributions 
1487*1031c584SApple OSS Distributions int
sbappendcontrol(struct sockbuf * sb,struct mbuf * m0,struct mbuf * control,int * error_out)1488*1031c584SApple OSS Distributions sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control,
1489*1031c584SApple OSS Distributions     int *error_out)
1490*1031c584SApple OSS Distributions {
1491*1031c584SApple OSS Distributions 	struct soflow_hash_entry *dgram_flow_entry = NULL;
1492*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
1493*1031c584SApple OSS Distributions 	int result = 0;
1494*1031c584SApple OSS Distributions 	boolean_t sb_unix = (sb->sb_flags & SB_UNIX);
1495*1031c584SApple OSS Distributions 
1496*1031c584SApple OSS Distributions 	if (error_out) {
1497*1031c584SApple OSS Distributions 		*error_out = 0;
1498*1031c584SApple OSS Distributions 	}
1499*1031c584SApple OSS Distributions 
1500*1031c584SApple OSS Distributions 	if (sb->sb_flags & SB_DROP) {
1501*1031c584SApple OSS Distributions 		if (m0 != NULL) {
1502*1031c584SApple OSS Distributions 			m_freem(m0);
1503*1031c584SApple OSS Distributions 		}
1504*1031c584SApple OSS Distributions 		if (control != NULL && !sb_unix) {
1505*1031c584SApple OSS Distributions 			m_freem(control);
1506*1031c584SApple OSS Distributions 		}
1507*1031c584SApple OSS Distributions 		if (error_out != NULL) {
1508*1031c584SApple OSS Distributions 			*error_out = EINVAL;
1509*1031c584SApple OSS Distributions 		}
1510*1031c584SApple OSS Distributions 		return 0;
1511*1031c584SApple OSS Distributions 	}
1512*1031c584SApple OSS Distributions 
1513*1031c584SApple OSS Distributions 	if (SOCK_DOM(sb->sb_so) == PF_INET || SOCK_DOM(sb->sb_so) == PF_INET6) {
1514*1031c584SApple OSS Distributions 		if (NEED_DGRAM_FLOW_TRACKING(so)) {
1515*1031c584SApple OSS Distributions 			dgram_flow_entry = soflow_get_flow(so, NULL, NULL, control, m0 != NULL ? m_length(m0) : 0, false, (m0 != NULL && m0->m_pkthdr.rcvif) ? m0->m_pkthdr.rcvif->if_index : 0);
1516*1031c584SApple OSS Distributions 		}
1517*1031c584SApple OSS Distributions 
1518*1031c584SApple OSS Distributions 		if (sb->sb_flags & SB_RECV && !(m0 && m0->m_flags & M_SKIPCFIL)) {
1519*1031c584SApple OSS Distributions 			int error;
1520*1031c584SApple OSS Distributions 
1521*1031c584SApple OSS Distributions 			error = sflt_data_in(sb->sb_so, NULL, &m0, &control, 0);
1522*1031c584SApple OSS Distributions 			SBLASTRECORDCHK(sb, __func__);
1523*1031c584SApple OSS Distributions 
1524*1031c584SApple OSS Distributions #if CONTENT_FILTER
1525*1031c584SApple OSS Distributions 			if (error == 0) {
1526*1031c584SApple OSS Distributions 				error = cfil_sock_data_in(sb->sb_so, NULL, m0, control,
1527*1031c584SApple OSS Distributions 				    0, dgram_flow_entry);
1528*1031c584SApple OSS Distributions 			}
1529*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
1530*1031c584SApple OSS Distributions 
1531*1031c584SApple OSS Distributions 			if (error) {
1532*1031c584SApple OSS Distributions 				if (error != EJUSTRETURN) {
1533*1031c584SApple OSS Distributions 					if (m0) {
1534*1031c584SApple OSS Distributions 						m_freem(m0);
1535*1031c584SApple OSS Distributions 					}
1536*1031c584SApple OSS Distributions 					if (control != NULL && !sb_unix) {
1537*1031c584SApple OSS Distributions 						m_freem(control);
1538*1031c584SApple OSS Distributions 					}
1539*1031c584SApple OSS Distributions 					if (error_out) {
1540*1031c584SApple OSS Distributions 						*error_out = error;
1541*1031c584SApple OSS Distributions 					}
1542*1031c584SApple OSS Distributions 				}
1543*1031c584SApple OSS Distributions 				if (dgram_flow_entry != NULL) {
1544*1031c584SApple OSS Distributions 					soflow_free_flow(dgram_flow_entry);
1545*1031c584SApple OSS Distributions 				}
1546*1031c584SApple OSS Distributions 				return 0;
1547*1031c584SApple OSS Distributions 			}
1548*1031c584SApple OSS Distributions 		} else if (m0) {
1549*1031c584SApple OSS Distributions 			m0->m_flags &= ~M_SKIPCFIL;
1550*1031c584SApple OSS Distributions 		}
1551*1031c584SApple OSS Distributions 
1552*1031c584SApple OSS Distributions 		if (dgram_flow_entry != NULL) {
1553*1031c584SApple OSS Distributions 			soflow_free_flow(dgram_flow_entry);
1554*1031c584SApple OSS Distributions 		}
1555*1031c584SApple OSS Distributions 	}
1556*1031c584SApple OSS Distributions 
1557*1031c584SApple OSS Distributions 	result = sbappendcontrol_internal(sb, m0, control);
1558*1031c584SApple OSS Distributions 	if (result == 0) {
1559*1031c584SApple OSS Distributions 		if (m0) {
1560*1031c584SApple OSS Distributions 			m_freem(m0);
1561*1031c584SApple OSS Distributions 		}
1562*1031c584SApple OSS Distributions 		if (control != NULL && !sb_unix) {
1563*1031c584SApple OSS Distributions 			m_freem(control);
1564*1031c584SApple OSS Distributions 		}
1565*1031c584SApple OSS Distributions 		if (error_out) {
1566*1031c584SApple OSS Distributions 			*error_out = ENOBUFS;
1567*1031c584SApple OSS Distributions 		}
1568*1031c584SApple OSS Distributions 	}
1569*1031c584SApple OSS Distributions 
1570*1031c584SApple OSS Distributions 	return result;
1571*1031c584SApple OSS Distributions }
1572*1031c584SApple OSS Distributions 
1573*1031c584SApple OSS Distributions /*
1574*1031c584SApple OSS Distributions  * TCP streams have Multipath TCP support or are regular TCP sockets.
1575*1031c584SApple OSS Distributions  */
1576*1031c584SApple OSS Distributions int
sbappendstream_rcvdemux(struct socket * so,struct mbuf * m)1577*1031c584SApple OSS Distributions sbappendstream_rcvdemux(struct socket *so, struct mbuf *m)
1578*1031c584SApple OSS Distributions {
1579*1031c584SApple OSS Distributions 	int ret = 0;
1580*1031c584SApple OSS Distributions 
1581*1031c584SApple OSS Distributions 	if ((m != NULL) &&
1582*1031c584SApple OSS Distributions 	    m_pktlen(m) <= 0 &&
1583*1031c584SApple OSS Distributions 	    !((so->so_flags & SOF_MP_SUBFLOW) &&
1584*1031c584SApple OSS Distributions 	    (m->m_flags & M_PKTHDR) &&
1585*1031c584SApple OSS Distributions 	    (m->m_pkthdr.pkt_flags & PKTF_MPTCP_DFIN))) {
1586*1031c584SApple OSS Distributions 		m_freem(m);
1587*1031c584SApple OSS Distributions 		return ret;
1588*1031c584SApple OSS Distributions 	}
1589*1031c584SApple OSS Distributions 
1590*1031c584SApple OSS Distributions #if MPTCP
1591*1031c584SApple OSS Distributions 	if (so->so_flags & SOF_MP_SUBFLOW) {
1592*1031c584SApple OSS Distributions 		return sbappendmptcpstream_rcv(&so->so_rcv, m);
1593*1031c584SApple OSS Distributions 	} else
1594*1031c584SApple OSS Distributions #endif /* MPTCP */
1595*1031c584SApple OSS Distributions 	{
1596*1031c584SApple OSS Distributions 		return sbappendstream(&so->so_rcv, m);
1597*1031c584SApple OSS Distributions 	}
1598*1031c584SApple OSS Distributions }
1599*1031c584SApple OSS Distributions 
1600*1031c584SApple OSS Distributions #if MPTCP
1601*1031c584SApple OSS Distributions int
sbappendmptcpstream_rcv(struct sockbuf * sb,struct mbuf * m)1602*1031c584SApple OSS Distributions sbappendmptcpstream_rcv(struct sockbuf *sb, struct mbuf *m)
1603*1031c584SApple OSS Distributions {
1604*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
1605*1031c584SApple OSS Distributions 
1606*1031c584SApple OSS Distributions 	VERIFY(m == NULL || (m->m_flags & M_PKTHDR));
1607*1031c584SApple OSS Distributions 	/* SB_NOCOMPRESS must be set prevent loss of M_PKTHDR data */
1608*1031c584SApple OSS Distributions 	VERIFY((sb->sb_flags & (SB_RECV | SB_NOCOMPRESS)) ==
1609*1031c584SApple OSS Distributions 	    (SB_RECV | SB_NOCOMPRESS));
1610*1031c584SApple OSS Distributions 
1611*1031c584SApple OSS Distributions 	if (m == NULL || m_pktlen(m) == 0 || (sb->sb_flags & SB_DROP) ||
1612*1031c584SApple OSS Distributions 	    (so->so_state & SS_CANTRCVMORE)) {
1613*1031c584SApple OSS Distributions 		if (m && (m->m_flags & M_PKTHDR) &&
1614*1031c584SApple OSS Distributions 		    m_pktlen(m) == 0 &&
1615*1031c584SApple OSS Distributions 		    (m->m_pkthdr.pkt_flags & PKTF_MPTCP_DFIN)) {
1616*1031c584SApple OSS Distributions 			mptcp_input(tptomptp(sototcpcb(so))->mpt_mpte, m);
1617*1031c584SApple OSS Distributions 			return 1;
1618*1031c584SApple OSS Distributions 		} else if (m != NULL) {
1619*1031c584SApple OSS Distributions 			m_freem(m);
1620*1031c584SApple OSS Distributions 		}
1621*1031c584SApple OSS Distributions 		return 0;
1622*1031c584SApple OSS Distributions 	}
1623*1031c584SApple OSS Distributions 	/* the socket is not closed, so SOF_MP_SUBFLOW must be set */
1624*1031c584SApple OSS Distributions 	VERIFY(so->so_flags & SOF_MP_SUBFLOW);
1625*1031c584SApple OSS Distributions 
1626*1031c584SApple OSS Distributions 	if (m->m_nextpkt != NULL || (sb->sb_mb != sb->sb_lastrecord)) {
1627*1031c584SApple OSS Distributions 		panic("%s: nexpkt %p || mb %p != lastrecord %p", __func__,
1628*1031c584SApple OSS Distributions 		    m->m_nextpkt, sb->sb_mb, sb->sb_lastrecord);
1629*1031c584SApple OSS Distributions 		/* NOTREACHED */
1630*1031c584SApple OSS Distributions 	}
1631*1031c584SApple OSS Distributions 
1632*1031c584SApple OSS Distributions 	SBLASTMBUFCHK(sb, __func__);
1633*1031c584SApple OSS Distributions 
1634*1031c584SApple OSS Distributions 	/* No filter support (SB_RECV) on mptcp subflow sockets */
1635*1031c584SApple OSS Distributions 
1636*1031c584SApple OSS Distributions 	sbcompress(sb, m, sb->sb_mbtail);
1637*1031c584SApple OSS Distributions 	sb->sb_lastrecord = sb->sb_mb;
1638*1031c584SApple OSS Distributions 	SBLASTRECORDCHK(sb, __func__);
1639*1031c584SApple OSS Distributions 	return 1;
1640*1031c584SApple OSS Distributions }
1641*1031c584SApple OSS Distributions #endif /* MPTCP */
1642*1031c584SApple OSS Distributions 
1643*1031c584SApple OSS Distributions /*
1644*1031c584SApple OSS Distributions  * Compress mbuf chain m into the socket
1645*1031c584SApple OSS Distributions  * buffer sb following mbuf n.  If n
1646*1031c584SApple OSS Distributions  * is null, the buffer is presumed empty.
1647*1031c584SApple OSS Distributions  */
1648*1031c584SApple OSS Distributions static inline void
sbcompress(struct sockbuf * sb,struct mbuf * m,struct mbuf * n)1649*1031c584SApple OSS Distributions sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
1650*1031c584SApple OSS Distributions {
1651*1031c584SApple OSS Distributions 	int eor = 0, compress = (!(sb->sb_flags & SB_NOCOMPRESS));
1652*1031c584SApple OSS Distributions 	struct mbuf *o;
1653*1031c584SApple OSS Distributions 
1654*1031c584SApple OSS Distributions 	if (m == NULL) {
1655*1031c584SApple OSS Distributions 		/* There is nothing to compress; just update the tail */
1656*1031c584SApple OSS Distributions 		for (; n->m_next != NULL; n = n->m_next) {
1657*1031c584SApple OSS Distributions 			;
1658*1031c584SApple OSS Distributions 		}
1659*1031c584SApple OSS Distributions 		sb->sb_mbtail = n;
1660*1031c584SApple OSS Distributions 		goto done;
1661*1031c584SApple OSS Distributions 	}
1662*1031c584SApple OSS Distributions 
1663*1031c584SApple OSS Distributions 	while (m != NULL) {
1664*1031c584SApple OSS Distributions 		eor |= m->m_flags & M_EOR;
1665*1031c584SApple OSS Distributions 		if (compress && m->m_len == 0 && (eor == 0 ||
1666*1031c584SApple OSS Distributions 		    (((o = m->m_next) || (o = n)) && o->m_type == m->m_type))) {
1667*1031c584SApple OSS Distributions 			if (sb->sb_lastrecord == m) {
1668*1031c584SApple OSS Distributions 				sb->sb_lastrecord = m->m_next;
1669*1031c584SApple OSS Distributions 			}
1670*1031c584SApple OSS Distributions 			m = m_free(m);
1671*1031c584SApple OSS Distributions 			continue;
1672*1031c584SApple OSS Distributions 		}
1673*1031c584SApple OSS Distributions 		if (compress && n != NULL && (n->m_flags & M_EOR) == 0 &&
1674*1031c584SApple OSS Distributions #ifndef __APPLE__
1675*1031c584SApple OSS Distributions 		    M_WRITABLE(n) &&
1676*1031c584SApple OSS Distributions #endif
1677*1031c584SApple OSS Distributions 		    m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
1678*1031c584SApple OSS Distributions 		    m->m_len <= M_TRAILINGSPACE(n) &&
1679*1031c584SApple OSS Distributions 		    n->m_type == m->m_type) {
1680*1031c584SApple OSS Distributions 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
1681*1031c584SApple OSS Distributions 			    (unsigned)m->m_len);
1682*1031c584SApple OSS Distributions 			n->m_len += m->m_len;
1683*1031c584SApple OSS Distributions 			sb->sb_cc += m->m_len;
1684*1031c584SApple OSS Distributions 			if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
1685*1031c584SApple OSS Distributions 			    m->m_type != MT_OOBDATA) {
1686*1031c584SApple OSS Distributions 				/* XXX: Probably don't need */
1687*1031c584SApple OSS Distributions 				sb->sb_ctl += m->m_len;
1688*1031c584SApple OSS Distributions 			}
1689*1031c584SApple OSS Distributions 
1690*1031c584SApple OSS Distributions 			/* update send byte count */
1691*1031c584SApple OSS Distributions 			if (sb->sb_flags & SB_SNDBYTE_CNT) {
1692*1031c584SApple OSS Distributions 				inp_incr_sndbytes_total(sb->sb_so,
1693*1031c584SApple OSS Distributions 				    m->m_len);
1694*1031c584SApple OSS Distributions 				inp_incr_sndbytes_unsent(sb->sb_so,
1695*1031c584SApple OSS Distributions 				    m->m_len);
1696*1031c584SApple OSS Distributions 			}
1697*1031c584SApple OSS Distributions 			m = m_free(m);
1698*1031c584SApple OSS Distributions 			continue;
1699*1031c584SApple OSS Distributions 		}
1700*1031c584SApple OSS Distributions 		if (n != NULL) {
1701*1031c584SApple OSS Distributions 			n->m_next = m;
1702*1031c584SApple OSS Distributions 		} else {
1703*1031c584SApple OSS Distributions 			sb->sb_mb = m;
1704*1031c584SApple OSS Distributions 		}
1705*1031c584SApple OSS Distributions 		sb->sb_mbtail = m;
1706*1031c584SApple OSS Distributions 		sballoc(sb, m);
1707*1031c584SApple OSS Distributions 		n = m;
1708*1031c584SApple OSS Distributions 		m->m_flags &= ~M_EOR;
1709*1031c584SApple OSS Distributions 		m = m->m_next;
1710*1031c584SApple OSS Distributions 		n->m_next = NULL;
1711*1031c584SApple OSS Distributions 	}
1712*1031c584SApple OSS Distributions 	if (eor != 0) {
1713*1031c584SApple OSS Distributions 		if (n != NULL) {
1714*1031c584SApple OSS Distributions 			n->m_flags |= M_EOR;
1715*1031c584SApple OSS Distributions 		} else {
1716*1031c584SApple OSS Distributions 			printf("semi-panic: sbcompress\n");
1717*1031c584SApple OSS Distributions 		}
1718*1031c584SApple OSS Distributions 	}
1719*1031c584SApple OSS Distributions done:
1720*1031c584SApple OSS Distributions 	SBLASTMBUFCHK(sb, __func__);
1721*1031c584SApple OSS Distributions }
1722*1031c584SApple OSS Distributions 
1723*1031c584SApple OSS Distributions void
sb_empty_assert(struct sockbuf * sb,const char * where)1724*1031c584SApple OSS Distributions sb_empty_assert(struct sockbuf *sb, const char *where)
1725*1031c584SApple OSS Distributions {
1726*1031c584SApple OSS Distributions 	if (!(sb->sb_cc == 0 && sb->sb_mb == NULL && sb->sb_mbcnt == 0 &&
1727*1031c584SApple OSS Distributions 	    sb->sb_mbtail == NULL && sb->sb_lastrecord == NULL)) {
1728*1031c584SApple OSS Distributions 		panic("%s: sb %p so %p cc %d mbcnt %d mb %p mbtail %p "
1729*1031c584SApple OSS Distributions 		    "lastrecord %p\n", where, sb, sb->sb_so, sb->sb_cc,
1730*1031c584SApple OSS Distributions 		    sb->sb_mbcnt, sb->sb_mb, sb->sb_mbtail,
1731*1031c584SApple OSS Distributions 		    sb->sb_lastrecord);
1732*1031c584SApple OSS Distributions 		/* NOTREACHED */
1733*1031c584SApple OSS Distributions 	}
1734*1031c584SApple OSS Distributions }
1735*1031c584SApple OSS Distributions 
1736*1031c584SApple OSS Distributions /*
1737*1031c584SApple OSS Distributions  * Free all mbufs in a sockbuf.
1738*1031c584SApple OSS Distributions  * Check that all resources are reclaimed.
1739*1031c584SApple OSS Distributions  */
1740*1031c584SApple OSS Distributions void
sbflush(struct sockbuf * sb)1741*1031c584SApple OSS Distributions sbflush(struct sockbuf *sb)
1742*1031c584SApple OSS Distributions {
1743*1031c584SApple OSS Distributions 	void *lr_saved = __builtin_return_address(0);
1744*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
1745*1031c584SApple OSS Distributions 
1746*1031c584SApple OSS Distributions 	/* so_usecount may be 0 if we get here from sofreelastref() */
1747*1031c584SApple OSS Distributions 	if (so == NULL) {
1748*1031c584SApple OSS Distributions 		panic("%s: null so, sb=%p sb_flags=0x%x lr=%p",
1749*1031c584SApple OSS Distributions 		    __func__, sb, sb->sb_flags, lr_saved);
1750*1031c584SApple OSS Distributions 		/* NOTREACHED */
1751*1031c584SApple OSS Distributions 	} else if (so->so_usecount < 0) {
1752*1031c584SApple OSS Distributions 		panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
1753*1031c584SApple OSS Distributions 		    "lrh= %s\n", __func__, sb, sb->sb_flags, so,
1754*1031c584SApple OSS Distributions 		    so->so_usecount, lr_saved, solockhistory_nr(so));
1755*1031c584SApple OSS Distributions 		/* NOTREACHED */
1756*1031c584SApple OSS Distributions 	}
1757*1031c584SApple OSS Distributions 
1758*1031c584SApple OSS Distributions 	/*
1759*1031c584SApple OSS Distributions 	 * Obtain lock on the socket buffer (SB_LOCK).  This is required
1760*1031c584SApple OSS Distributions 	 * to prevent the socket buffer from being unexpectedly altered
1761*1031c584SApple OSS Distributions 	 * while it is used by another thread in socket send/receive.
1762*1031c584SApple OSS Distributions 	 *
1763*1031c584SApple OSS Distributions 	 * sblock() must not fail here, hence the assertion.
1764*1031c584SApple OSS Distributions 	 */
1765*1031c584SApple OSS Distributions 	(void) sblock(sb, SBL_WAIT | SBL_NOINTR | SBL_IGNDEFUNCT);
1766*1031c584SApple OSS Distributions 	VERIFY(sb->sb_flags & SB_LOCK);
1767*1031c584SApple OSS Distributions 
1768*1031c584SApple OSS Distributions 	while (sb->sb_mbcnt > 0) {
1769*1031c584SApple OSS Distributions 		/*
1770*1031c584SApple OSS Distributions 		 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
1771*1031c584SApple OSS Distributions 		 * we would loop forever. Panic instead.
1772*1031c584SApple OSS Distributions 		 */
1773*1031c584SApple OSS Distributions 		if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) {
1774*1031c584SApple OSS Distributions 			break;
1775*1031c584SApple OSS Distributions 		}
1776*1031c584SApple OSS Distributions 		sbdrop(sb, (int)sb->sb_cc);
1777*1031c584SApple OSS Distributions 	}
1778*1031c584SApple OSS Distributions 
1779*1031c584SApple OSS Distributions 	sb_empty_assert(sb, __func__);
1780*1031c584SApple OSS Distributions 	sbunlock(sb, TRUE);     /* keep socket locked */
1781*1031c584SApple OSS Distributions }
1782*1031c584SApple OSS Distributions 
1783*1031c584SApple OSS Distributions /*
1784*1031c584SApple OSS Distributions  * Drop data from (the front of) a sockbuf.
1785*1031c584SApple OSS Distributions  * use m_freem_list to free the mbuf structures
1786*1031c584SApple OSS Distributions  * under a single lock... this is done by pruning
1787*1031c584SApple OSS Distributions  * the top of the tree from the body by keeping track
1788*1031c584SApple OSS Distributions  * of where we get to in the tree and then zeroing the
1789*1031c584SApple OSS Distributions  * two pertinent pointers m_nextpkt and m_next
1790*1031c584SApple OSS Distributions  * the socket buffer is then updated to point at the new
1791*1031c584SApple OSS Distributions  * top of the tree and the pruned area is released via
1792*1031c584SApple OSS Distributions  * m_freem_list.
1793*1031c584SApple OSS Distributions  */
1794*1031c584SApple OSS Distributions void
sbdrop(struct sockbuf * sb,int len)1795*1031c584SApple OSS Distributions sbdrop(struct sockbuf *sb, int len)
1796*1031c584SApple OSS Distributions {
1797*1031c584SApple OSS Distributions 	struct mbuf *m, *free_list, *ml;
1798*1031c584SApple OSS Distributions 	struct mbuf *next, *last;
1799*1031c584SApple OSS Distributions 
1800*1031c584SApple OSS Distributions 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1801*1031c584SApple OSS Distributions #if MPTCP
1802*1031c584SApple OSS Distributions 	if (m != NULL && len > 0 && !(sb->sb_flags & SB_RECV) &&
1803*1031c584SApple OSS Distributions 	    ((sb->sb_so->so_flags & SOF_MP_SUBFLOW) ||
1804*1031c584SApple OSS Distributions 	    (SOCK_CHECK_DOM(sb->sb_so, PF_MULTIPATH) &&
1805*1031c584SApple OSS Distributions 	    SOCK_CHECK_PROTO(sb->sb_so, IPPROTO_TCP))) &&
1806*1031c584SApple OSS Distributions 	    !(sb->sb_so->so_flags1 & SOF1_POST_FALLBACK_SYNC)) {
1807*1031c584SApple OSS Distributions 		mptcp_preproc_sbdrop(sb->sb_so, m, (unsigned int)len);
1808*1031c584SApple OSS Distributions 	}
1809*1031c584SApple OSS Distributions 	if (m != NULL && len > 0 && !(sb->sb_flags & SB_RECV) &&
1810*1031c584SApple OSS Distributions 	    (sb->sb_so->so_flags & SOF_MP_SUBFLOW) &&
1811*1031c584SApple OSS Distributions 	    (sb->sb_so->so_flags1 & SOF1_POST_FALLBACK_SYNC)) {
1812*1031c584SApple OSS Distributions 		mptcp_fallback_sbdrop(sb->sb_so, m, len);
1813*1031c584SApple OSS Distributions 	}
1814*1031c584SApple OSS Distributions #endif /* MPTCP */
1815*1031c584SApple OSS Distributions 	KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_START), sb, len, 0, 0, 0);
1816*1031c584SApple OSS Distributions 
1817*1031c584SApple OSS Distributions 	free_list = last = m;
1818*1031c584SApple OSS Distributions 	ml = (struct mbuf *)0;
1819*1031c584SApple OSS Distributions 
1820*1031c584SApple OSS Distributions 	while (len > 0) {
1821*1031c584SApple OSS Distributions 		if (m == NULL) {
1822*1031c584SApple OSS Distributions 			if (next == NULL) {
1823*1031c584SApple OSS Distributions 				/*
1824*1031c584SApple OSS Distributions 				 * temporarily replacing this panic with printf
1825*1031c584SApple OSS Distributions 				 * because it occurs occasionally when closing
1826*1031c584SApple OSS Distributions 				 * a socket when there is no harm in ignoring
1827*1031c584SApple OSS Distributions 				 * it. This problem will be investigated
1828*1031c584SApple OSS Distributions 				 * further.
1829*1031c584SApple OSS Distributions 				 */
1830*1031c584SApple OSS Distributions 				/* panic("sbdrop"); */
1831*1031c584SApple OSS Distributions 				printf("sbdrop - count not zero\n");
1832*1031c584SApple OSS Distributions 				len = 0;
1833*1031c584SApple OSS Distributions 				/*
1834*1031c584SApple OSS Distributions 				 * zero the counts. if we have no mbufs,
1835*1031c584SApple OSS Distributions 				 * we have no data (PR-2986815)
1836*1031c584SApple OSS Distributions 				 */
1837*1031c584SApple OSS Distributions 				sb->sb_cc = 0;
1838*1031c584SApple OSS Distributions 				sb->sb_mbcnt = 0;
1839*1031c584SApple OSS Distributions 				break;
1840*1031c584SApple OSS Distributions 			}
1841*1031c584SApple OSS Distributions 			m = last = next;
1842*1031c584SApple OSS Distributions 			next = m->m_nextpkt;
1843*1031c584SApple OSS Distributions 			continue;
1844*1031c584SApple OSS Distributions 		}
1845*1031c584SApple OSS Distributions 		if (m->m_len > len) {
1846*1031c584SApple OSS Distributions 			m->m_len -= len;
1847*1031c584SApple OSS Distributions 			m->m_data += len;
1848*1031c584SApple OSS Distributions 			sb->sb_cc -= len;
1849*1031c584SApple OSS Distributions 			/* update the send byte count */
1850*1031c584SApple OSS Distributions 			if (sb->sb_flags & SB_SNDBYTE_CNT) {
1851*1031c584SApple OSS Distributions 				inp_decr_sndbytes_total(sb->sb_so, len);
1852*1031c584SApple OSS Distributions 			}
1853*1031c584SApple OSS Distributions 			if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
1854*1031c584SApple OSS Distributions 			    m->m_type != MT_OOBDATA) {
1855*1031c584SApple OSS Distributions 				sb->sb_ctl -= len;
1856*1031c584SApple OSS Distributions 			}
1857*1031c584SApple OSS Distributions 			break;
1858*1031c584SApple OSS Distributions 		}
1859*1031c584SApple OSS Distributions 		len -= m->m_len;
1860*1031c584SApple OSS Distributions 		sbfree(sb, m);
1861*1031c584SApple OSS Distributions 
1862*1031c584SApple OSS Distributions 		ml = m;
1863*1031c584SApple OSS Distributions 		m = m->m_next;
1864*1031c584SApple OSS Distributions 	}
1865*1031c584SApple OSS Distributions 	while (m && m->m_len == 0) {
1866*1031c584SApple OSS Distributions 		sbfree(sb, m);
1867*1031c584SApple OSS Distributions 
1868*1031c584SApple OSS Distributions 		ml = m;
1869*1031c584SApple OSS Distributions 		m = m->m_next;
1870*1031c584SApple OSS Distributions 	}
1871*1031c584SApple OSS Distributions 	if (ml) {
1872*1031c584SApple OSS Distributions 		ml->m_next = (struct mbuf *)0;
1873*1031c584SApple OSS Distributions 		last->m_nextpkt = (struct mbuf *)0;
1874*1031c584SApple OSS Distributions 		m_freem_list(free_list);
1875*1031c584SApple OSS Distributions 	}
1876*1031c584SApple OSS Distributions 	if (m) {
1877*1031c584SApple OSS Distributions 		sb->sb_mb = m;
1878*1031c584SApple OSS Distributions 		m->m_nextpkt = next;
1879*1031c584SApple OSS Distributions 	} else {
1880*1031c584SApple OSS Distributions 		sb->sb_mb = next;
1881*1031c584SApple OSS Distributions 	}
1882*1031c584SApple OSS Distributions 
1883*1031c584SApple OSS Distributions 	/*
1884*1031c584SApple OSS Distributions 	 * First part is an inline SB_EMPTY_FIXUP().  Second part
1885*1031c584SApple OSS Distributions 	 * makes sure sb_lastrecord is up-to-date if we dropped
1886*1031c584SApple OSS Distributions 	 * part of the last record.
1887*1031c584SApple OSS Distributions 	 */
1888*1031c584SApple OSS Distributions 	m = sb->sb_mb;
1889*1031c584SApple OSS Distributions 	if (m == NULL) {
1890*1031c584SApple OSS Distributions 		sb->sb_mbtail = NULL;
1891*1031c584SApple OSS Distributions 		sb->sb_lastrecord = NULL;
1892*1031c584SApple OSS Distributions 	} else if (m->m_nextpkt == NULL) {
1893*1031c584SApple OSS Distributions 		sb->sb_lastrecord = m;
1894*1031c584SApple OSS Distributions 	}
1895*1031c584SApple OSS Distributions 
1896*1031c584SApple OSS Distributions #if CONTENT_FILTER
1897*1031c584SApple OSS Distributions 	cfil_sock_buf_update(sb);
1898*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
1899*1031c584SApple OSS Distributions 
1900*1031c584SApple OSS Distributions 	KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, 0, 0, 0, 0);
1901*1031c584SApple OSS Distributions }
1902*1031c584SApple OSS Distributions 
1903*1031c584SApple OSS Distributions /*
1904*1031c584SApple OSS Distributions  * Drop a record off the front of a sockbuf
1905*1031c584SApple OSS Distributions  * and move the next record to the front.
1906*1031c584SApple OSS Distributions  */
1907*1031c584SApple OSS Distributions void
sbdroprecord(struct sockbuf * sb)1908*1031c584SApple OSS Distributions sbdroprecord(struct sockbuf *sb)
1909*1031c584SApple OSS Distributions {
1910*1031c584SApple OSS Distributions 	struct mbuf *m, *mn;
1911*1031c584SApple OSS Distributions 
1912*1031c584SApple OSS Distributions 	m = sb->sb_mb;
1913*1031c584SApple OSS Distributions 	if (m) {
1914*1031c584SApple OSS Distributions 		sb->sb_mb = m->m_nextpkt;
1915*1031c584SApple OSS Distributions 		do {
1916*1031c584SApple OSS Distributions 			sbfree(sb, m);
1917*1031c584SApple OSS Distributions 			MFREE(m, mn);
1918*1031c584SApple OSS Distributions 			m = mn;
1919*1031c584SApple OSS Distributions 		} while (m);
1920*1031c584SApple OSS Distributions 	}
1921*1031c584SApple OSS Distributions 	SB_EMPTY_FIXUP(sb);
1922*1031c584SApple OSS Distributions }
1923*1031c584SApple OSS Distributions 
1924*1031c584SApple OSS Distributions /*
1925*1031c584SApple OSS Distributions  * Create a "control" mbuf containing the specified data
1926*1031c584SApple OSS Distributions  * with the specified type for presentation on a socket buffer.
1927*1031c584SApple OSS Distributions  */
1928*1031c584SApple OSS Distributions struct mbuf *
sbcreatecontrol(caddr_t p,int size,int type,int level)1929*1031c584SApple OSS Distributions sbcreatecontrol(caddr_t p, int size, int type, int level)
1930*1031c584SApple OSS Distributions {
1931*1031c584SApple OSS Distributions 	struct cmsghdr *cp;
1932*1031c584SApple OSS Distributions 	struct mbuf *m;
1933*1031c584SApple OSS Distributions 
1934*1031c584SApple OSS Distributions 	if (CMSG_SPACE((u_int)size) > MLEN) {
1935*1031c584SApple OSS Distributions 		return (struct mbuf *)NULL;
1936*1031c584SApple OSS Distributions 	}
1937*1031c584SApple OSS Distributions 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) {
1938*1031c584SApple OSS Distributions 		return (struct mbuf *)NULL;
1939*1031c584SApple OSS Distributions 	}
1940*1031c584SApple OSS Distributions 	cp = mtod(m, struct cmsghdr *);
1941*1031c584SApple OSS Distributions 	VERIFY(IS_P2ALIGNED(cp, sizeof(u_int32_t)));
1942*1031c584SApple OSS Distributions 	/* XXX check size? */
1943*1031c584SApple OSS Distributions 	(void) memcpy(CMSG_DATA(cp), p, size);
1944*1031c584SApple OSS Distributions 	m->m_len = (int32_t)CMSG_SPACE(size);
1945*1031c584SApple OSS Distributions 	cp->cmsg_len = CMSG_LEN(size);
1946*1031c584SApple OSS Distributions 	cp->cmsg_level = level;
1947*1031c584SApple OSS Distributions 	cp->cmsg_type = type;
1948*1031c584SApple OSS Distributions 	return m;
1949*1031c584SApple OSS Distributions }
1950*1031c584SApple OSS Distributions 
1951*1031c584SApple OSS Distributions struct mbuf **
sbcreatecontrol_mbuf(caddr_t p,int size,int type,int level,struct mbuf ** mp)1952*1031c584SApple OSS Distributions sbcreatecontrol_mbuf(caddr_t p, int size, int type, int level, struct mbuf **mp)
1953*1031c584SApple OSS Distributions {
1954*1031c584SApple OSS Distributions 	struct mbuf *m;
1955*1031c584SApple OSS Distributions 	struct cmsghdr *cp;
1956*1031c584SApple OSS Distributions 
1957*1031c584SApple OSS Distributions 	if (*mp == NULL) {
1958*1031c584SApple OSS Distributions 		*mp = sbcreatecontrol(p, size, type, level);
1959*1031c584SApple OSS Distributions 		return mp;
1960*1031c584SApple OSS Distributions 	}
1961*1031c584SApple OSS Distributions 
1962*1031c584SApple OSS Distributions 	if (CMSG_SPACE((u_int)size) + (*mp)->m_len > MLEN) {
1963*1031c584SApple OSS Distributions 		mp = &(*mp)->m_next;
1964*1031c584SApple OSS Distributions 		*mp = sbcreatecontrol(p, size, type, level);
1965*1031c584SApple OSS Distributions 		return mp;
1966*1031c584SApple OSS Distributions 	}
1967*1031c584SApple OSS Distributions 
1968*1031c584SApple OSS Distributions 	m = *mp;
1969*1031c584SApple OSS Distributions 
1970*1031c584SApple OSS Distributions 	cp = (struct cmsghdr *)(void *)(mtod(m, char *) + m->m_len);
1971*1031c584SApple OSS Distributions 	/* CMSG_SPACE ensures 32-bit alignment */
1972*1031c584SApple OSS Distributions 	VERIFY(IS_P2ALIGNED(cp, sizeof(u_int32_t)));
1973*1031c584SApple OSS Distributions 	m->m_len += (int32_t)CMSG_SPACE(size);
1974*1031c584SApple OSS Distributions 
1975*1031c584SApple OSS Distributions 	/* XXX check size? */
1976*1031c584SApple OSS Distributions 	(void) memcpy(CMSG_DATA(cp), p, size);
1977*1031c584SApple OSS Distributions 	cp->cmsg_len = CMSG_LEN(size);
1978*1031c584SApple OSS Distributions 	cp->cmsg_level = level;
1979*1031c584SApple OSS Distributions 	cp->cmsg_type = type;
1980*1031c584SApple OSS Distributions 
1981*1031c584SApple OSS Distributions 	return mp;
1982*1031c584SApple OSS Distributions }
1983*1031c584SApple OSS Distributions 
1984*1031c584SApple OSS Distributions 
1985*1031c584SApple OSS Distributions /*
1986*1031c584SApple OSS Distributions  * Some routines that return EOPNOTSUPP for entry points that are not
1987*1031c584SApple OSS Distributions  * supported by a protocol.  Fill in as needed.
1988*1031c584SApple OSS Distributions  */
1989*1031c584SApple OSS Distributions int
pru_abort_notsupp(struct socket * so)1990*1031c584SApple OSS Distributions pru_abort_notsupp(struct socket *so)
1991*1031c584SApple OSS Distributions {
1992*1031c584SApple OSS Distributions #pragma unused(so)
1993*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
1994*1031c584SApple OSS Distributions }
1995*1031c584SApple OSS Distributions 
1996*1031c584SApple OSS Distributions int
pru_accept_notsupp(struct socket * so,struct sockaddr ** nam)1997*1031c584SApple OSS Distributions pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
1998*1031c584SApple OSS Distributions {
1999*1031c584SApple OSS Distributions #pragma unused(so, nam)
2000*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2001*1031c584SApple OSS Distributions }
2002*1031c584SApple OSS Distributions 
2003*1031c584SApple OSS Distributions int
pru_attach_notsupp(struct socket * so,int proto,struct proc * p)2004*1031c584SApple OSS Distributions pru_attach_notsupp(struct socket *so, int proto, struct proc *p)
2005*1031c584SApple OSS Distributions {
2006*1031c584SApple OSS Distributions #pragma unused(so, proto, p)
2007*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2008*1031c584SApple OSS Distributions }
2009*1031c584SApple OSS Distributions 
2010*1031c584SApple OSS Distributions int
pru_bind_notsupp(struct socket * so,struct sockaddr * nam,struct proc * p)2011*1031c584SApple OSS Distributions pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
2012*1031c584SApple OSS Distributions {
2013*1031c584SApple OSS Distributions #pragma unused(so, nam, p)
2014*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2015*1031c584SApple OSS Distributions }
2016*1031c584SApple OSS Distributions 
2017*1031c584SApple OSS Distributions int
pru_connect_notsupp(struct socket * so,struct sockaddr * nam,struct proc * p)2018*1031c584SApple OSS Distributions pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
2019*1031c584SApple OSS Distributions {
2020*1031c584SApple OSS Distributions #pragma unused(so, nam, p)
2021*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2022*1031c584SApple OSS Distributions }
2023*1031c584SApple OSS Distributions 
2024*1031c584SApple OSS Distributions int
pru_connect2_notsupp(struct socket * so1,struct socket * so2)2025*1031c584SApple OSS Distributions pru_connect2_notsupp(struct socket *so1, struct socket *so2)
2026*1031c584SApple OSS Distributions {
2027*1031c584SApple OSS Distributions #pragma unused(so1, so2)
2028*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2029*1031c584SApple OSS Distributions }
2030*1031c584SApple OSS Distributions 
2031*1031c584SApple OSS Distributions int
pru_connectx_notsupp(struct socket * so,struct sockaddr * src,struct sockaddr * dst,struct proc * p,uint32_t ifscope,sae_associd_t aid,sae_connid_t * pcid,uint32_t flags,void * arg,uint32_t arglen,struct uio * uio,user_ssize_t * bytes_written)2032*1031c584SApple OSS Distributions pru_connectx_notsupp(struct socket *so, struct sockaddr *src,
2033*1031c584SApple OSS Distributions     struct sockaddr *dst, struct proc *p, uint32_t ifscope,
2034*1031c584SApple OSS Distributions     sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
2035*1031c584SApple OSS Distributions     uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
2036*1031c584SApple OSS Distributions {
2037*1031c584SApple OSS Distributions #pragma unused(so, src, dst, p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written)
2038*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2039*1031c584SApple OSS Distributions }
2040*1031c584SApple OSS Distributions 
2041*1031c584SApple OSS Distributions int
pru_control_notsupp(struct socket * so,u_long cmd,caddr_t data,struct ifnet * ifp,struct proc * p)2042*1031c584SApple OSS Distributions pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
2043*1031c584SApple OSS Distributions     struct ifnet *ifp, struct proc *p)
2044*1031c584SApple OSS Distributions {
2045*1031c584SApple OSS Distributions #pragma unused(so, cmd, data, ifp, p)
2046*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2047*1031c584SApple OSS Distributions }
2048*1031c584SApple OSS Distributions 
2049*1031c584SApple OSS Distributions int
pru_detach_notsupp(struct socket * so)2050*1031c584SApple OSS Distributions pru_detach_notsupp(struct socket *so)
2051*1031c584SApple OSS Distributions {
2052*1031c584SApple OSS Distributions #pragma unused(so)
2053*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2054*1031c584SApple OSS Distributions }
2055*1031c584SApple OSS Distributions 
2056*1031c584SApple OSS Distributions int
pru_disconnect_notsupp(struct socket * so)2057*1031c584SApple OSS Distributions pru_disconnect_notsupp(struct socket *so)
2058*1031c584SApple OSS Distributions {
2059*1031c584SApple OSS Distributions #pragma unused(so)
2060*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2061*1031c584SApple OSS Distributions }
2062*1031c584SApple OSS Distributions 
2063*1031c584SApple OSS Distributions int
pru_disconnectx_notsupp(struct socket * so,sae_associd_t aid,sae_connid_t cid)2064*1031c584SApple OSS Distributions pru_disconnectx_notsupp(struct socket *so, sae_associd_t aid, sae_connid_t cid)
2065*1031c584SApple OSS Distributions {
2066*1031c584SApple OSS Distributions #pragma unused(so, aid, cid)
2067*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2068*1031c584SApple OSS Distributions }
2069*1031c584SApple OSS Distributions 
2070*1031c584SApple OSS Distributions int
pru_listen_notsupp(struct socket * so,struct proc * p)2071*1031c584SApple OSS Distributions pru_listen_notsupp(struct socket *so, struct proc *p)
2072*1031c584SApple OSS Distributions {
2073*1031c584SApple OSS Distributions #pragma unused(so, p)
2074*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2075*1031c584SApple OSS Distributions }
2076*1031c584SApple OSS Distributions 
2077*1031c584SApple OSS Distributions int
pru_peeraddr_notsupp(struct socket * so,struct sockaddr ** nam)2078*1031c584SApple OSS Distributions pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
2079*1031c584SApple OSS Distributions {
2080*1031c584SApple OSS Distributions #pragma unused(so, nam)
2081*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2082*1031c584SApple OSS Distributions }
2083*1031c584SApple OSS Distributions 
2084*1031c584SApple OSS Distributions int
pru_rcvd_notsupp(struct socket * so,int flags)2085*1031c584SApple OSS Distributions pru_rcvd_notsupp(struct socket *so, int flags)
2086*1031c584SApple OSS Distributions {
2087*1031c584SApple OSS Distributions #pragma unused(so, flags)
2088*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2089*1031c584SApple OSS Distributions }
2090*1031c584SApple OSS Distributions 
2091*1031c584SApple OSS Distributions int
pru_rcvoob_notsupp(struct socket * so,struct mbuf * m,int flags)2092*1031c584SApple OSS Distributions pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
2093*1031c584SApple OSS Distributions {
2094*1031c584SApple OSS Distributions #pragma unused(so, m, flags)
2095*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2096*1031c584SApple OSS Distributions }
2097*1031c584SApple OSS Distributions 
2098*1031c584SApple OSS Distributions int
pru_send_notsupp(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct proc * p)2099*1031c584SApple OSS Distributions pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
2100*1031c584SApple OSS Distributions     struct sockaddr *addr, struct mbuf *control, struct proc *p)
2101*1031c584SApple OSS Distributions {
2102*1031c584SApple OSS Distributions #pragma unused(so, flags, m, addr, control, p)
2103*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2104*1031c584SApple OSS Distributions }
2105*1031c584SApple OSS Distributions 
2106*1031c584SApple OSS Distributions int
pru_send_list_notsupp(struct socket * so,struct mbuf * m,u_int * pktcnt,int flags)2107*1031c584SApple OSS Distributions pru_send_list_notsupp(struct socket *so, struct mbuf *m, u_int *pktcnt,
2108*1031c584SApple OSS Distributions     int flags)
2109*1031c584SApple OSS Distributions {
2110*1031c584SApple OSS Distributions #pragma unused(so, m, pktcnt, flags)
2111*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2112*1031c584SApple OSS Distributions }
2113*1031c584SApple OSS Distributions 
2114*1031c584SApple OSS Distributions /*
2115*1031c584SApple OSS Distributions  * This isn't really a ``null'' operation, but it's the default one
2116*1031c584SApple OSS Distributions  * and doesn't do anything destructive.
2117*1031c584SApple OSS Distributions  */
2118*1031c584SApple OSS Distributions int
pru_sense_null(struct socket * so,void * ub,int isstat64)2119*1031c584SApple OSS Distributions pru_sense_null(struct socket *so, void *ub, int isstat64)
2120*1031c584SApple OSS Distributions {
2121*1031c584SApple OSS Distributions 	if (isstat64 != 0) {
2122*1031c584SApple OSS Distributions 		struct stat64 *sb64;
2123*1031c584SApple OSS Distributions 
2124*1031c584SApple OSS Distributions 		sb64 = (struct stat64 *)ub;
2125*1031c584SApple OSS Distributions 		sb64->st_blksize = so->so_snd.sb_hiwat;
2126*1031c584SApple OSS Distributions 	} else {
2127*1031c584SApple OSS Distributions 		struct stat *sb;
2128*1031c584SApple OSS Distributions 
2129*1031c584SApple OSS Distributions 		sb = (struct stat *)ub;
2130*1031c584SApple OSS Distributions 		sb->st_blksize = so->so_snd.sb_hiwat;
2131*1031c584SApple OSS Distributions 	}
2132*1031c584SApple OSS Distributions 
2133*1031c584SApple OSS Distributions 	return 0;
2134*1031c584SApple OSS Distributions }
2135*1031c584SApple OSS Distributions 
2136*1031c584SApple OSS Distributions int
pru_sosend_notsupp(struct socket * so,struct sockaddr * addr,struct uio * uio,struct mbuf * top,struct mbuf * control,int flags)2137*1031c584SApple OSS Distributions pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
2138*1031c584SApple OSS Distributions     struct mbuf *top, struct mbuf *control, int flags)
2139*1031c584SApple OSS Distributions {
2140*1031c584SApple OSS Distributions #pragma unused(so, addr, uio, top, control, flags)
2141*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2142*1031c584SApple OSS Distributions }
2143*1031c584SApple OSS Distributions 
2144*1031c584SApple OSS Distributions int
pru_sosend_list_notsupp(struct socket * so,struct mbuf * m,size_t total_len,u_int * pktcnt,int flags)2145*1031c584SApple OSS Distributions pru_sosend_list_notsupp(struct socket *so, struct mbuf *m, size_t total_len, u_int *pktcnt, int flags)
2146*1031c584SApple OSS Distributions {
2147*1031c584SApple OSS Distributions #pragma unused(so, m, total_len, pktcnt, flags)
2148*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2149*1031c584SApple OSS Distributions }
2150*1031c584SApple OSS Distributions 
2151*1031c584SApple OSS Distributions int
pru_soreceive_notsupp(struct socket * so,struct sockaddr ** paddr,struct uio * uio,struct mbuf ** mp0,struct mbuf ** controlp,int * flagsp)2152*1031c584SApple OSS Distributions pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
2153*1031c584SApple OSS Distributions     struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2154*1031c584SApple OSS Distributions {
2155*1031c584SApple OSS Distributions #pragma unused(so, paddr, uio, mp0, controlp, flagsp)
2156*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2157*1031c584SApple OSS Distributions }
2158*1031c584SApple OSS Distributions 
2159*1031c584SApple OSS Distributions int
pru_shutdown_notsupp(struct socket * so)2160*1031c584SApple OSS Distributions pru_shutdown_notsupp(struct socket *so)
2161*1031c584SApple OSS Distributions {
2162*1031c584SApple OSS Distributions #pragma unused(so)
2163*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2164*1031c584SApple OSS Distributions }
2165*1031c584SApple OSS Distributions 
2166*1031c584SApple OSS Distributions int
pru_sockaddr_notsupp(struct socket * so,struct sockaddr ** nam)2167*1031c584SApple OSS Distributions pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
2168*1031c584SApple OSS Distributions {
2169*1031c584SApple OSS Distributions #pragma unused(so, nam)
2170*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2171*1031c584SApple OSS Distributions }
2172*1031c584SApple OSS Distributions 
2173*1031c584SApple OSS Distributions int
pru_sopoll_notsupp(struct socket * so,int events,kauth_cred_t cred,void * wql)2174*1031c584SApple OSS Distributions pru_sopoll_notsupp(struct socket *so, int events, kauth_cred_t cred, void *wql)
2175*1031c584SApple OSS Distributions {
2176*1031c584SApple OSS Distributions #pragma unused(so, events, cred, wql)
2177*1031c584SApple OSS Distributions 	return EOPNOTSUPP;
2178*1031c584SApple OSS Distributions }
2179*1031c584SApple OSS Distributions 
2180*1031c584SApple OSS Distributions int
pru_socheckopt_null(struct socket * so,struct sockopt * sopt)2181*1031c584SApple OSS Distributions pru_socheckopt_null(struct socket *so, struct sockopt *sopt)
2182*1031c584SApple OSS Distributions {
2183*1031c584SApple OSS Distributions #pragma unused(so, sopt)
2184*1031c584SApple OSS Distributions 	/*
2185*1031c584SApple OSS Distributions 	 * Allow all options for set/get by default.
2186*1031c584SApple OSS Distributions 	 */
2187*1031c584SApple OSS Distributions 	return 0;
2188*1031c584SApple OSS Distributions }
2189*1031c584SApple OSS Distributions 
2190*1031c584SApple OSS Distributions static int
pru_preconnect_null(struct socket * so)2191*1031c584SApple OSS Distributions pru_preconnect_null(struct socket *so)
2192*1031c584SApple OSS Distributions {
2193*1031c584SApple OSS Distributions #pragma unused(so)
2194*1031c584SApple OSS Distributions 	return 0;
2195*1031c584SApple OSS Distributions }
2196*1031c584SApple OSS Distributions 
2197*1031c584SApple OSS Distributions static int
pru_defunct_null(struct socket * so)2198*1031c584SApple OSS Distributions pru_defunct_null(struct socket *so)
2199*1031c584SApple OSS Distributions {
2200*1031c584SApple OSS Distributions #pragma unused(so)
2201*1031c584SApple OSS Distributions 	return 0;
2202*1031c584SApple OSS Distributions }
2203*1031c584SApple OSS Distributions 
2204*1031c584SApple OSS Distributions 
2205*1031c584SApple OSS Distributions void
pru_sanitize(struct pr_usrreqs * pru)2206*1031c584SApple OSS Distributions pru_sanitize(struct pr_usrreqs *pru)
2207*1031c584SApple OSS Distributions {
2208*1031c584SApple OSS Distributions #define DEFAULT(foo, bar)       if ((foo) == NULL) (foo) = (bar)
2209*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_abort, pru_abort_notsupp);
2210*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_accept, pru_accept_notsupp);
2211*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_attach, pru_attach_notsupp);
2212*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_bind, pru_bind_notsupp);
2213*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_connect, pru_connect_notsupp);
2214*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_connect2, pru_connect2_notsupp);
2215*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_connectx, pru_connectx_notsupp);
2216*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_control, pru_control_notsupp);
2217*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_detach, pru_detach_notsupp);
2218*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_disconnect, pru_disconnect_notsupp);
2219*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_disconnectx, pru_disconnectx_notsupp);
2220*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_listen, pru_listen_notsupp);
2221*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_peeraddr, pru_peeraddr_notsupp);
2222*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_rcvd, pru_rcvd_notsupp);
2223*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_rcvoob, pru_rcvoob_notsupp);
2224*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_send, pru_send_notsupp);
2225*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_send_list, pru_send_list_notsupp);
2226*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_sense, pru_sense_null);
2227*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_shutdown, pru_shutdown_notsupp);
2228*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_sockaddr, pru_sockaddr_notsupp);
2229*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_sopoll, pru_sopoll_notsupp);
2230*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_soreceive, pru_soreceive_notsupp);
2231*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_sosend, pru_sosend_notsupp);
2232*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_sosend_list, pru_sosend_list_notsupp);
2233*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_socheckopt, pru_socheckopt_null);
2234*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_preconnect, pru_preconnect_null);
2235*1031c584SApple OSS Distributions 	DEFAULT(pru->pru_defunct, pru_defunct_null);
2236*1031c584SApple OSS Distributions #undef DEFAULT
2237*1031c584SApple OSS Distributions }
2238*1031c584SApple OSS Distributions 
2239*1031c584SApple OSS Distributions /*
2240*1031c584SApple OSS Distributions  * The following are macros on BSD and functions on Darwin
2241*1031c584SApple OSS Distributions  */
2242*1031c584SApple OSS Distributions 
2243*1031c584SApple OSS Distributions /*
2244*1031c584SApple OSS Distributions  * Do we need to notify the other side when I/O is possible?
2245*1031c584SApple OSS Distributions  */
2246*1031c584SApple OSS Distributions 
2247*1031c584SApple OSS Distributions int
sb_notify(struct sockbuf * sb)2248*1031c584SApple OSS Distributions sb_notify(struct sockbuf *sb)
2249*1031c584SApple OSS Distributions {
2250*1031c584SApple OSS Distributions 	return sb->sb_waiters > 0 ||
2251*1031c584SApple OSS Distributions 	       (sb->sb_flags & (SB_SEL | SB_ASYNC | SB_UPCALL | SB_KNOTE));
2252*1031c584SApple OSS Distributions }
2253*1031c584SApple OSS Distributions 
2254*1031c584SApple OSS Distributions /*
2255*1031c584SApple OSS Distributions  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
2256*1031c584SApple OSS Distributions  * This is problematical if the fields are unsigned, as the space might
2257*1031c584SApple OSS Distributions  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
2258*1031c584SApple OSS Distributions  * overflow and return 0.
2259*1031c584SApple OSS Distributions  */
2260*1031c584SApple OSS Distributions int
sbspace(struct sockbuf * sb)2261*1031c584SApple OSS Distributions sbspace(struct sockbuf *sb)
2262*1031c584SApple OSS Distributions {
2263*1031c584SApple OSS Distributions 	int pending = 0;
2264*1031c584SApple OSS Distributions 	int space;
2265*1031c584SApple OSS Distributions 
2266*1031c584SApple OSS Distributions 	if (sb->sb_flags & SB_KCTL) {
2267*1031c584SApple OSS Distributions 		space = (int)(sb->sb_hiwat - sb->sb_cc);
2268*1031c584SApple OSS Distributions 	} else {
2269*1031c584SApple OSS Distributions 		space = imin((int)(sb->sb_hiwat - sb->sb_cc),
2270*1031c584SApple OSS Distributions 		    (int)(sb->sb_mbmax - sb->sb_mbcnt));
2271*1031c584SApple OSS Distributions 	}
2272*1031c584SApple OSS Distributions 	if (sb->sb_preconn_hiwat != 0) {
2273*1031c584SApple OSS Distributions 		space = imin((int)(sb->sb_preconn_hiwat - sb->sb_cc), space);
2274*1031c584SApple OSS Distributions 	}
2275*1031c584SApple OSS Distributions 
2276*1031c584SApple OSS Distributions 	if (space < 0) {
2277*1031c584SApple OSS Distributions 		space = 0;
2278*1031c584SApple OSS Distributions 	}
2279*1031c584SApple OSS Distributions 
2280*1031c584SApple OSS Distributions 	/* Compensate for data being processed by content filters */
2281*1031c584SApple OSS Distributions #if CONTENT_FILTER
2282*1031c584SApple OSS Distributions 	pending = cfil_sock_data_space(sb);
2283*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
2284*1031c584SApple OSS Distributions 	if (pending > space) {
2285*1031c584SApple OSS Distributions 		space = 0;
2286*1031c584SApple OSS Distributions 	} else {
2287*1031c584SApple OSS Distributions 		space -= pending;
2288*1031c584SApple OSS Distributions 	}
2289*1031c584SApple OSS Distributions 
2290*1031c584SApple OSS Distributions 	return space;
2291*1031c584SApple OSS Distributions }
2292*1031c584SApple OSS Distributions 
2293*1031c584SApple OSS Distributions /* do we have to send all at once on a socket? */
2294*1031c584SApple OSS Distributions int
sosendallatonce(struct socket * so)2295*1031c584SApple OSS Distributions sosendallatonce(struct socket *so)
2296*1031c584SApple OSS Distributions {
2297*1031c584SApple OSS Distributions 	return so->so_proto->pr_flags & PR_ATOMIC;
2298*1031c584SApple OSS Distributions }
2299*1031c584SApple OSS Distributions 
2300*1031c584SApple OSS Distributions /* can we read something from so? */
2301*1031c584SApple OSS Distributions int
soreadable(struct socket * so)2302*1031c584SApple OSS Distributions soreadable(struct socket *so)
2303*1031c584SApple OSS Distributions {
2304*1031c584SApple OSS Distributions 	return so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
2305*1031c584SApple OSS Distributions 	       ((so->so_state & SS_CANTRCVMORE)
2306*1031c584SApple OSS Distributions #if CONTENT_FILTER
2307*1031c584SApple OSS Distributions 	       && cfil_sock_data_pending(&so->so_rcv) == 0
2308*1031c584SApple OSS Distributions #endif /* CONTENT_FILTER */
2309*1031c584SApple OSS Distributions 	       ) ||
2310*1031c584SApple OSS Distributions 	       so->so_comp.tqh_first || so->so_error;
2311*1031c584SApple OSS Distributions }
2312*1031c584SApple OSS Distributions 
2313*1031c584SApple OSS Distributions /* can we write something to so? */
2314*1031c584SApple OSS Distributions 
2315*1031c584SApple OSS Distributions int
sowriteable(struct socket * so)2316*1031c584SApple OSS Distributions sowriteable(struct socket *so)
2317*1031c584SApple OSS Distributions {
2318*1031c584SApple OSS Distributions 	if ((so->so_state & SS_CANTSENDMORE) ||
2319*1031c584SApple OSS Distributions 	    so->so_error > 0) {
2320*1031c584SApple OSS Distributions 		return 1;
2321*1031c584SApple OSS Distributions 	}
2322*1031c584SApple OSS Distributions 	if (so_wait_for_if_feedback(so) || !socanwrite(so)) {
2323*1031c584SApple OSS Distributions 		return 0;
2324*1031c584SApple OSS Distributions 	}
2325*1031c584SApple OSS Distributions 	if (so->so_flags1 & SOF1_PRECONNECT_DATA) {
2326*1031c584SApple OSS Distributions 		return 1;
2327*1031c584SApple OSS Distributions 	}
2328*1031c584SApple OSS Distributions 
2329*1031c584SApple OSS Distributions 	int64_t data = sbspace(&so->so_snd);
2330*1031c584SApple OSS Distributions 	int64_t lowat = so->so_snd.sb_lowat;
2331*1031c584SApple OSS Distributions 	/*
2332*1031c584SApple OSS Distributions 	 * Deal with connected UNIX domain sockets which
2333*1031c584SApple OSS Distributions 	 * rely on the fact that the sender's socket buffer is
2334*1031c584SApple OSS Distributions 	 * actually the receiver's socket buffer.
2335*1031c584SApple OSS Distributions 	 */
2336*1031c584SApple OSS Distributions 	if (SOCK_DOM(so) == PF_LOCAL) {
2337*1031c584SApple OSS Distributions 		struct unpcb *unp = sotounpcb(so);
2338*1031c584SApple OSS Distributions 		if (unp != NULL && unp->unp_conn != NULL &&
2339*1031c584SApple OSS Distributions 		    unp->unp_conn->unp_socket != NULL) {
2340*1031c584SApple OSS Distributions 			struct socket *so2 = unp->unp_conn->unp_socket;
2341*1031c584SApple OSS Distributions 			/*
2342*1031c584SApple OSS Distributions 			 * At this point we know that `so' is locked
2343*1031c584SApple OSS Distributions 			 * and that `unp_conn` isn't going to change.
2344*1031c584SApple OSS Distributions 			 * However, we don't lock `so2` because doing so
2345*1031c584SApple OSS Distributions 			 * may require unlocking `so'
2346*1031c584SApple OSS Distributions 			 * (see unp_get_locks_in_order()).
2347*1031c584SApple OSS Distributions 			 *
2348*1031c584SApple OSS Distributions 			 * Two cases can happen:
2349*1031c584SApple OSS Distributions 			 *
2350*1031c584SApple OSS Distributions 			 * 1) we return 1 and tell the application that
2351*1031c584SApple OSS Distributions 			 *    it can write.  Meanwhile, another thread
2352*1031c584SApple OSS Distributions 			 *    fills up the socket buffer.  This will either
2353*1031c584SApple OSS Distributions 			 *    lead to a blocking send or EWOULDBLOCK
2354*1031c584SApple OSS Distributions 			 *    which the application should deal with.
2355*1031c584SApple OSS Distributions 			 * 2) we return 0 and tell the application that
2356*1031c584SApple OSS Distributions 			 *    the socket is not writable.  Meanwhile,
2357*1031c584SApple OSS Distributions 			 *    another thread depletes the receive socket
2358*1031c584SApple OSS Distributions 			 *    buffer. In this case the application will
2359*1031c584SApple OSS Distributions 			 *    be woken up by sb_notify().
2360*1031c584SApple OSS Distributions 			 *
2361*1031c584SApple OSS Distributions 			 * MIN() is required because otherwise sosendcheck()
2362*1031c584SApple OSS Distributions 			 * may return EWOULDBLOCK since it only considers
2363*1031c584SApple OSS Distributions 			 * so->so_snd.
2364*1031c584SApple OSS Distributions 			 */
2365*1031c584SApple OSS Distributions 			data = MIN(data, sbspace(&so2->so_rcv));
2366*1031c584SApple OSS Distributions 		}
2367*1031c584SApple OSS Distributions 	}
2368*1031c584SApple OSS Distributions 
2369*1031c584SApple OSS Distributions 	if (data >= lowat) {
2370*1031c584SApple OSS Distributions 		if (so->so_flags & SOF_NOTSENT_LOWAT) {
2371*1031c584SApple OSS Distributions 			if ((SOCK_DOM(so) == PF_INET6 ||
2372*1031c584SApple OSS Distributions 			    SOCK_DOM(so) == PF_INET) &&
2373*1031c584SApple OSS Distributions 			    so->so_type == SOCK_STREAM) {
2374*1031c584SApple OSS Distributions 				return tcp_notsent_lowat_check(so);
2375*1031c584SApple OSS Distributions 			}
2376*1031c584SApple OSS Distributions #if MPTCP
2377*1031c584SApple OSS Distributions 			else if ((SOCK_DOM(so) == PF_MULTIPATH) &&
2378*1031c584SApple OSS Distributions 			    (SOCK_PROTO(so) == IPPROTO_TCP)) {
2379*1031c584SApple OSS Distributions 				return mptcp_notsent_lowat_check(so);
2380*1031c584SApple OSS Distributions 			}
2381*1031c584SApple OSS Distributions #endif
2382*1031c584SApple OSS Distributions 			else {
2383*1031c584SApple OSS Distributions 				return 1;
2384*1031c584SApple OSS Distributions 			}
2385*1031c584SApple OSS Distributions 		} else {
2386*1031c584SApple OSS Distributions 			return 1;
2387*1031c584SApple OSS Distributions 		}
2388*1031c584SApple OSS Distributions 	}
2389*1031c584SApple OSS Distributions 	return 0;
2390*1031c584SApple OSS Distributions }
2391*1031c584SApple OSS Distributions 
2392*1031c584SApple OSS Distributions /* adjust counters in sb reflecting allocation of m */
2393*1031c584SApple OSS Distributions 
2394*1031c584SApple OSS Distributions void
sballoc(struct sockbuf * sb,struct mbuf * m)2395*1031c584SApple OSS Distributions sballoc(struct sockbuf *sb, struct mbuf *m)
2396*1031c584SApple OSS Distributions {
2397*1031c584SApple OSS Distributions 	u_int32_t cnt = 1;
2398*1031c584SApple OSS Distributions 	sb->sb_cc += m->m_len;
2399*1031c584SApple OSS Distributions 	if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
2400*1031c584SApple OSS Distributions 	    m->m_type != MT_OOBDATA) {
2401*1031c584SApple OSS Distributions 		sb->sb_ctl += m->m_len;
2402*1031c584SApple OSS Distributions 	}
2403*1031c584SApple OSS Distributions 	sb->sb_mbcnt += _MSIZE;
2404*1031c584SApple OSS Distributions 
2405*1031c584SApple OSS Distributions 	if (m->m_flags & M_EXT) {
2406*1031c584SApple OSS Distributions 		sb->sb_mbcnt += m->m_ext.ext_size;
2407*1031c584SApple OSS Distributions 		cnt += (m->m_ext.ext_size + _MSIZE - 1) / _MSIZE;
2408*1031c584SApple OSS Distributions 	}
2409*1031c584SApple OSS Distributions 	OSAddAtomic(cnt, &total_sbmb_cnt);
2410*1031c584SApple OSS Distributions 	VERIFY(total_sbmb_cnt > 0);
2411*1031c584SApple OSS Distributions 	if (total_sbmb_cnt > total_sbmb_cnt_peak) {
2412*1031c584SApple OSS Distributions 		total_sbmb_cnt_peak = total_sbmb_cnt;
2413*1031c584SApple OSS Distributions 	}
2414*1031c584SApple OSS Distributions 
2415*1031c584SApple OSS Distributions 	/*
2416*1031c584SApple OSS Distributions 	 * If data is being added to the send socket buffer,
2417*1031c584SApple OSS Distributions 	 * update the send byte count
2418*1031c584SApple OSS Distributions 	 */
2419*1031c584SApple OSS Distributions 	if (sb->sb_flags & SB_SNDBYTE_CNT) {
2420*1031c584SApple OSS Distributions 		inp_incr_sndbytes_total(sb->sb_so, m->m_len);
2421*1031c584SApple OSS Distributions 		inp_incr_sndbytes_unsent(sb->sb_so, m->m_len);
2422*1031c584SApple OSS Distributions 	}
2423*1031c584SApple OSS Distributions }
2424*1031c584SApple OSS Distributions 
2425*1031c584SApple OSS Distributions /* adjust counters in sb reflecting freeing of m */
2426*1031c584SApple OSS Distributions void
sbfree(struct sockbuf * sb,struct mbuf * m)2427*1031c584SApple OSS Distributions sbfree(struct sockbuf *sb, struct mbuf *m)
2428*1031c584SApple OSS Distributions {
2429*1031c584SApple OSS Distributions 	int cnt = -1;
2430*1031c584SApple OSS Distributions 
2431*1031c584SApple OSS Distributions 	sb->sb_cc -= m->m_len;
2432*1031c584SApple OSS Distributions 	if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
2433*1031c584SApple OSS Distributions 	    m->m_type != MT_OOBDATA) {
2434*1031c584SApple OSS Distributions 		sb->sb_ctl -= m->m_len;
2435*1031c584SApple OSS Distributions 	}
2436*1031c584SApple OSS Distributions 	sb->sb_mbcnt -= _MSIZE;
2437*1031c584SApple OSS Distributions 	if (m->m_flags & M_EXT) {
2438*1031c584SApple OSS Distributions 		sb->sb_mbcnt -= m->m_ext.ext_size;
2439*1031c584SApple OSS Distributions 		cnt -= (m->m_ext.ext_size + _MSIZE - 1) / _MSIZE;
2440*1031c584SApple OSS Distributions 	}
2441*1031c584SApple OSS Distributions 	OSAddAtomic(cnt, &total_sbmb_cnt);
2442*1031c584SApple OSS Distributions 	VERIFY(total_sbmb_cnt >= 0);
2443*1031c584SApple OSS Distributions 	if (total_sbmb_cnt < total_sbmb_cnt_floor) {
2444*1031c584SApple OSS Distributions 		total_sbmb_cnt_floor = total_sbmb_cnt;
2445*1031c584SApple OSS Distributions 	}
2446*1031c584SApple OSS Distributions 
2447*1031c584SApple OSS Distributions 	/*
2448*1031c584SApple OSS Distributions 	 * If data is being removed from the send socket buffer,
2449*1031c584SApple OSS Distributions 	 * update the send byte count
2450*1031c584SApple OSS Distributions 	 */
2451*1031c584SApple OSS Distributions 	if (sb->sb_flags & SB_SNDBYTE_CNT) {
2452*1031c584SApple OSS Distributions 		inp_decr_sndbytes_total(sb->sb_so, m->m_len);
2453*1031c584SApple OSS Distributions 	}
2454*1031c584SApple OSS Distributions }
2455*1031c584SApple OSS Distributions 
2456*1031c584SApple OSS Distributions /*
2457*1031c584SApple OSS Distributions  * Set lock on sockbuf sb; sleep if lock is already held.
2458*1031c584SApple OSS Distributions  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
2459*1031c584SApple OSS Distributions  * Returns error without lock if sleep is interrupted.
2460*1031c584SApple OSS Distributions  */
2461*1031c584SApple OSS Distributions int
sblock(struct sockbuf * sb,uint32_t flags)2462*1031c584SApple OSS Distributions sblock(struct sockbuf *sb, uint32_t flags)
2463*1031c584SApple OSS Distributions {
2464*1031c584SApple OSS Distributions 	boolean_t nointr = ((sb->sb_flags & SB_NOINTR) || (flags & SBL_NOINTR));
2465*1031c584SApple OSS Distributions 	void *lr_saved = __builtin_return_address(0);
2466*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
2467*1031c584SApple OSS Distributions 	void * wchan;
2468*1031c584SApple OSS Distributions 	int error = 0;
2469*1031c584SApple OSS Distributions 	thread_t tp = current_thread();
2470*1031c584SApple OSS Distributions 
2471*1031c584SApple OSS Distributions 	VERIFY((flags & SBL_VALID) == flags);
2472*1031c584SApple OSS Distributions 
2473*1031c584SApple OSS Distributions 	/* so_usecount may be 0 if we get here from sofreelastref() */
2474*1031c584SApple OSS Distributions 	if (so == NULL) {
2475*1031c584SApple OSS Distributions 		panic("%s: null so, sb=%p sb_flags=0x%x lr=%p",
2476*1031c584SApple OSS Distributions 		    __func__, sb, sb->sb_flags, lr_saved);
2477*1031c584SApple OSS Distributions 		/* NOTREACHED */
2478*1031c584SApple OSS Distributions 	} else if (so->so_usecount < 0) {
2479*1031c584SApple OSS Distributions 		panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
2480*1031c584SApple OSS Distributions 		    "lrh= %s\n", __func__, sb, sb->sb_flags, so,
2481*1031c584SApple OSS Distributions 		    so->so_usecount, lr_saved, solockhistory_nr(so));
2482*1031c584SApple OSS Distributions 		/* NOTREACHED */
2483*1031c584SApple OSS Distributions 	}
2484*1031c584SApple OSS Distributions 
2485*1031c584SApple OSS Distributions 	/*
2486*1031c584SApple OSS Distributions 	 * The content filter thread must hold the sockbuf lock
2487*1031c584SApple OSS Distributions 	 */
2488*1031c584SApple OSS Distributions 	if ((so->so_flags & SOF_CONTENT_FILTER) && sb->sb_cfil_thread == tp) {
2489*1031c584SApple OSS Distributions 		/*
2490*1031c584SApple OSS Distributions 		 * Don't panic if we are defunct because SB_LOCK has
2491*1031c584SApple OSS Distributions 		 * been cleared by sodefunct()
2492*1031c584SApple OSS Distributions 		 */
2493*1031c584SApple OSS Distributions 		if (!(so->so_flags & SOF_DEFUNCT) && !(sb->sb_flags & SB_LOCK)) {
2494*1031c584SApple OSS Distributions 			panic("%s: SB_LOCK not held for %p",
2495*1031c584SApple OSS Distributions 			    __func__, sb);
2496*1031c584SApple OSS Distributions 		}
2497*1031c584SApple OSS Distributions 
2498*1031c584SApple OSS Distributions 		/* Keep the sockbuf locked */
2499*1031c584SApple OSS Distributions 		return 0;
2500*1031c584SApple OSS Distributions 	}
2501*1031c584SApple OSS Distributions 
2502*1031c584SApple OSS Distributions 	if ((sb->sb_flags & SB_LOCK) && !(flags & SBL_WAIT)) {
2503*1031c584SApple OSS Distributions 		return EWOULDBLOCK;
2504*1031c584SApple OSS Distributions 	}
2505*1031c584SApple OSS Distributions 	/*
2506*1031c584SApple OSS Distributions 	 * We may get here from sorflush(), in which case "sb" may not
2507*1031c584SApple OSS Distributions 	 * point to the real socket buffer.  Use the actual socket buffer
2508*1031c584SApple OSS Distributions 	 * address from the socket instead.
2509*1031c584SApple OSS Distributions 	 */
2510*1031c584SApple OSS Distributions 	wchan = (sb->sb_flags & SB_RECV) ?
2511*1031c584SApple OSS Distributions 	    &so->so_rcv.sb_flags : &so->so_snd.sb_flags;
2512*1031c584SApple OSS Distributions 
2513*1031c584SApple OSS Distributions 	/*
2514*1031c584SApple OSS Distributions 	 * A content filter thread has exclusive access to the sockbuf
2515*1031c584SApple OSS Distributions 	 * until it clears the
2516*1031c584SApple OSS Distributions 	 */
2517*1031c584SApple OSS Distributions 	while ((sb->sb_flags & SB_LOCK) ||
2518*1031c584SApple OSS Distributions 	    ((so->so_flags & SOF_CONTENT_FILTER) &&
2519*1031c584SApple OSS Distributions 	    sb->sb_cfil_thread != NULL)) {
2520*1031c584SApple OSS Distributions 		lck_mtx_t *mutex_held;
2521*1031c584SApple OSS Distributions 
2522*1031c584SApple OSS Distributions 		/*
2523*1031c584SApple OSS Distributions 		 * XXX: This code should be moved up above outside of this loop;
2524*1031c584SApple OSS Distributions 		 * however, we may get here as part of sofreelastref(), and
2525*1031c584SApple OSS Distributions 		 * at that time pr_getlock() may no longer be able to return
2526*1031c584SApple OSS Distributions 		 * us the lock.  This will be fixed in future.
2527*1031c584SApple OSS Distributions 		 */
2528*1031c584SApple OSS Distributions 		if (so->so_proto->pr_getlock != NULL) {
2529*1031c584SApple OSS Distributions 			mutex_held = (*so->so_proto->pr_getlock)(so, PR_F_WILLUNLOCK);
2530*1031c584SApple OSS Distributions 		} else {
2531*1031c584SApple OSS Distributions 			mutex_held = so->so_proto->pr_domain->dom_mtx;
2532*1031c584SApple OSS Distributions 		}
2533*1031c584SApple OSS Distributions 
2534*1031c584SApple OSS Distributions 		LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
2535*1031c584SApple OSS Distributions 
2536*1031c584SApple OSS Distributions 		sb->sb_wantlock++;
2537*1031c584SApple OSS Distributions 		VERIFY(sb->sb_wantlock != 0);
2538*1031c584SApple OSS Distributions 
2539*1031c584SApple OSS Distributions 		error = msleep(wchan, mutex_held,
2540*1031c584SApple OSS Distributions 		    nointr ? PSOCK : PSOCK | PCATCH,
2541*1031c584SApple OSS Distributions 		    nointr ? "sb_lock_nointr" : "sb_lock", NULL);
2542*1031c584SApple OSS Distributions 
2543*1031c584SApple OSS Distributions 		VERIFY(sb->sb_wantlock != 0);
2544*1031c584SApple OSS Distributions 		sb->sb_wantlock--;
2545*1031c584SApple OSS Distributions 
2546*1031c584SApple OSS Distributions 		if (error == 0 && (so->so_flags & SOF_DEFUNCT) &&
2547*1031c584SApple OSS Distributions 		    !(flags & SBL_IGNDEFUNCT)) {
2548*1031c584SApple OSS Distributions 			error = EBADF;
2549*1031c584SApple OSS Distributions 			SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llu [%d,%d] "
2550*1031c584SApple OSS Distributions 			    "(%d)\n", __func__, proc_selfpid(),
2551*1031c584SApple OSS Distributions 			    proc_best_name(current_proc()),
2552*1031c584SApple OSS Distributions 			    so->so_gencnt,
2553*1031c584SApple OSS Distributions 			    SOCK_DOM(so), SOCK_TYPE(so), error);
2554*1031c584SApple OSS Distributions 		}
2555*1031c584SApple OSS Distributions 
2556*1031c584SApple OSS Distributions 		if (error != 0) {
2557*1031c584SApple OSS Distributions 			return error;
2558*1031c584SApple OSS Distributions 		}
2559*1031c584SApple OSS Distributions 	}
2560*1031c584SApple OSS Distributions 	sb->sb_flags |= SB_LOCK;
2561*1031c584SApple OSS Distributions 	return 0;
2562*1031c584SApple OSS Distributions }
2563*1031c584SApple OSS Distributions 
2564*1031c584SApple OSS Distributions /*
2565*1031c584SApple OSS Distributions  * Release lock on sockbuf sb
2566*1031c584SApple OSS Distributions  */
2567*1031c584SApple OSS Distributions void
sbunlock(struct sockbuf * sb,boolean_t keeplocked)2568*1031c584SApple OSS Distributions sbunlock(struct sockbuf *sb, boolean_t keeplocked)
2569*1031c584SApple OSS Distributions {
2570*1031c584SApple OSS Distributions 	void *lr_saved = __builtin_return_address(0);
2571*1031c584SApple OSS Distributions 	struct socket *so = sb->sb_so;
2572*1031c584SApple OSS Distributions 	thread_t tp = current_thread();
2573*1031c584SApple OSS Distributions 
2574*1031c584SApple OSS Distributions 	/* so_usecount may be 0 if we get here from sofreelastref() */
2575*1031c584SApple OSS Distributions 	if (so == NULL) {
2576*1031c584SApple OSS Distributions 		panic("%s: null so, sb=%p sb_flags=0x%x lr=%p",
2577*1031c584SApple OSS Distributions 		    __func__, sb, sb->sb_flags, lr_saved);
2578*1031c584SApple OSS Distributions 		/* NOTREACHED */
2579*1031c584SApple OSS Distributions 	} else if (so->so_usecount < 0) {
2580*1031c584SApple OSS Distributions 		panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
2581*1031c584SApple OSS Distributions 		    "lrh= %s\n", __func__, sb, sb->sb_flags, so,
2582*1031c584SApple OSS Distributions 		    so->so_usecount, lr_saved, solockhistory_nr(so));
2583*1031c584SApple OSS Distributions 		/* NOTREACHED */
2584*1031c584SApple OSS Distributions 	}
2585*1031c584SApple OSS Distributions 
2586*1031c584SApple OSS Distributions 	/*
2587*1031c584SApple OSS Distributions 	 * The content filter thread must hold the sockbuf lock
2588*1031c584SApple OSS Distributions 	 */
2589*1031c584SApple OSS Distributions 	if ((so->so_flags & SOF_CONTENT_FILTER) && sb->sb_cfil_thread == tp) {
2590*1031c584SApple OSS Distributions 		/*
2591*1031c584SApple OSS Distributions 		 * Don't panic if we are defunct because SB_LOCK has
2592*1031c584SApple OSS Distributions 		 * been cleared by sodefunct()
2593*1031c584SApple OSS Distributions 		 */
2594*1031c584SApple OSS Distributions 		if (!(so->so_flags & SOF_DEFUNCT) &&
2595*1031c584SApple OSS Distributions 		    !(sb->sb_flags & SB_LOCK) &&
2596*1031c584SApple OSS Distributions 		    !(so->so_state & SS_DEFUNCT) &&
2597*1031c584SApple OSS Distributions 		    !(so->so_flags1 & SOF1_DEFUNCTINPROG)) {
2598*1031c584SApple OSS Distributions 			panic("%s: SB_LOCK not held for %p",
2599*1031c584SApple OSS Distributions 			    __func__, sb);
2600*1031c584SApple OSS Distributions 		}
2601*1031c584SApple OSS Distributions 		/* Keep the sockbuf locked and proceed */
2602*1031c584SApple OSS Distributions 	} else {
2603*1031c584SApple OSS Distributions 		VERIFY((sb->sb_flags & SB_LOCK) ||
2604*1031c584SApple OSS Distributions 		    (so->so_state & SS_DEFUNCT) ||
2605*1031c584SApple OSS Distributions 		    (so->so_flags1 & SOF1_DEFUNCTINPROG));
2606*1031c584SApple OSS Distributions 
2607*1031c584SApple OSS Distributions 		sb->sb_flags &= ~SB_LOCK;
2608*1031c584SApple OSS Distributions 
2609*1031c584SApple OSS Distributions 		if (sb->sb_wantlock > 0) {
2610*1031c584SApple OSS Distributions 			/*
2611*1031c584SApple OSS Distributions 			 * We may get here from sorflush(), in which case "sb"
2612*1031c584SApple OSS Distributions 			 * may not point to the real socket buffer.  Use the
2613*1031c584SApple OSS Distributions 			 * actual socket buffer address from the socket instead.
2614*1031c584SApple OSS Distributions 			 */
2615*1031c584SApple OSS Distributions 			wakeup((sb->sb_flags & SB_RECV) ? &so->so_rcv.sb_flags :
2616*1031c584SApple OSS Distributions 			    &so->so_snd.sb_flags);
2617*1031c584SApple OSS Distributions 		}
2618*1031c584SApple OSS Distributions 	}
2619*1031c584SApple OSS Distributions 
2620*1031c584SApple OSS Distributions 	if (!keeplocked) {      /* unlock on exit */
2621*1031c584SApple OSS Distributions 		if (so->so_flags & SOF_MP_SUBFLOW || SOCK_DOM(so) == PF_MULTIPATH) {
2622*1031c584SApple OSS Distributions 			(*so->so_proto->pr_unlock)(so, 1, lr_saved);
2623*1031c584SApple OSS Distributions 		} else {
2624*1031c584SApple OSS Distributions 			lck_mtx_t *mutex_held;
2625*1031c584SApple OSS Distributions 
2626*1031c584SApple OSS Distributions 			if (so->so_proto->pr_getlock != NULL) {
2627*1031c584SApple OSS Distributions 				mutex_held = (*so->so_proto->pr_getlock)(so, PR_F_WILLUNLOCK);
2628*1031c584SApple OSS Distributions 			} else {
2629*1031c584SApple OSS Distributions 				mutex_held = so->so_proto->pr_domain->dom_mtx;
2630*1031c584SApple OSS Distributions 			}
2631*1031c584SApple OSS Distributions 
2632*1031c584SApple OSS Distributions 			LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
2633*1031c584SApple OSS Distributions 
2634*1031c584SApple OSS Distributions 			VERIFY(so->so_usecount > 0);
2635*1031c584SApple OSS Distributions 			so->so_usecount--;
2636*1031c584SApple OSS Distributions 			so->unlock_lr[so->next_unlock_lr] = lr_saved;
2637*1031c584SApple OSS Distributions 			so->next_unlock_lr = (so->next_unlock_lr + 1) % SO_LCKDBG_MAX;
2638*1031c584SApple OSS Distributions 			lck_mtx_unlock(mutex_held);
2639*1031c584SApple OSS Distributions 		}
2640*1031c584SApple OSS Distributions 	}
2641*1031c584SApple OSS Distributions }
2642*1031c584SApple OSS Distributions 
2643*1031c584SApple OSS Distributions void
sorwakeup(struct socket * so)2644*1031c584SApple OSS Distributions sorwakeup(struct socket *so)
2645*1031c584SApple OSS Distributions {
2646*1031c584SApple OSS Distributions 	if (sb_notify(&so->so_rcv)) {
2647*1031c584SApple OSS Distributions 		sowakeup(so, &so->so_rcv, NULL);
2648*1031c584SApple OSS Distributions 	}
2649*1031c584SApple OSS Distributions }
2650*1031c584SApple OSS Distributions 
2651*1031c584SApple OSS Distributions void
sowwakeup(struct socket * so)2652*1031c584SApple OSS Distributions sowwakeup(struct socket *so)
2653*1031c584SApple OSS Distributions {
2654*1031c584SApple OSS Distributions 	if (sb_notify(&so->so_snd)) {
2655*1031c584SApple OSS Distributions 		sowakeup(so, &so->so_snd, NULL);
2656*1031c584SApple OSS Distributions 	}
2657*1031c584SApple OSS Distributions }
2658*1031c584SApple OSS Distributions 
2659*1031c584SApple OSS Distributions static void
soevupcall(struct socket * so,uint32_t hint)2660*1031c584SApple OSS Distributions soevupcall(struct socket *so, uint32_t hint)
2661*1031c584SApple OSS Distributions {
2662*1031c584SApple OSS Distributions 	if (so->so_event != NULL) {
2663*1031c584SApple OSS Distributions 		caddr_t so_eventarg = so->so_eventarg;
2664*1031c584SApple OSS Distributions 
2665*1031c584SApple OSS Distributions 		hint &= so->so_eventmask;
2666*1031c584SApple OSS Distributions 		if (hint != 0) {
2667*1031c584SApple OSS Distributions 			so->so_event(so, so_eventarg, hint);
2668*1031c584SApple OSS Distributions 		}
2669*1031c584SApple OSS Distributions 	}
2670*1031c584SApple OSS Distributions }
2671*1031c584SApple OSS Distributions 
2672*1031c584SApple OSS Distributions void
soevent(struct socket * so,uint32_t hint)2673*1031c584SApple OSS Distributions soevent(struct socket *so, uint32_t hint)
2674*1031c584SApple OSS Distributions {
2675*1031c584SApple OSS Distributions 	if (net_wake_pkt_debug > 0 && (hint & SO_FILT_HINT_WAKE_PKT)) {
2676*1031c584SApple OSS Distributions 		os_log(OS_LOG_DEFAULT, "%s: SO_FILT_HINT_WAKE_PKT so %p",
2677*1031c584SApple OSS Distributions 		    __func__, so);
2678*1031c584SApple OSS Distributions 	}
2679*1031c584SApple OSS Distributions 
2680*1031c584SApple OSS Distributions 	if (so->so_flags & SOF_KNOTE) {
2681*1031c584SApple OSS Distributions 		KNOTE(&so->so_klist, hint);
2682*1031c584SApple OSS Distributions 	}
2683*1031c584SApple OSS Distributions 
2684*1031c584SApple OSS Distributions 	soevupcall(so, hint);
2685*1031c584SApple OSS Distributions 
2686*1031c584SApple OSS Distributions 	/*
2687*1031c584SApple OSS Distributions 	 * Don't post an event if this a subflow socket or
2688*1031c584SApple OSS Distributions 	 * the app has opted out of using cellular interface
2689*1031c584SApple OSS Distributions 	 */
2690*1031c584SApple OSS Distributions 	if ((hint & SO_FILT_HINT_IFDENIED) &&
2691*1031c584SApple OSS Distributions 	    !(so->so_flags & SOF_MP_SUBFLOW) &&
2692*1031c584SApple OSS Distributions 	    !(so->so_restrictions & SO_RESTRICT_DENY_CELLULAR) &&
2693*1031c584SApple OSS Distributions 	    !(so->so_restrictions & SO_RESTRICT_DENY_EXPENSIVE) &&
2694*1031c584SApple OSS Distributions 	    !(so->so_restrictions & SO_RESTRICT_DENY_CONSTRAINED)) {
2695*1031c584SApple OSS Distributions 		soevent_ifdenied(so);
2696*1031c584SApple OSS Distributions 	}
2697*1031c584SApple OSS Distributions }
2698*1031c584SApple OSS Distributions 
2699*1031c584SApple OSS Distributions static void
soevent_ifdenied(struct socket * so)2700*1031c584SApple OSS Distributions soevent_ifdenied(struct socket *so)
2701*1031c584SApple OSS Distributions {
2702*1031c584SApple OSS Distributions 	struct kev_netpolicy_ifdenied ev_ifdenied;
2703*1031c584SApple OSS Distributions 
2704*1031c584SApple OSS Distributions 	bzero(&ev_ifdenied, sizeof(ev_ifdenied));
2705*1031c584SApple OSS Distributions 	/*
2706*1031c584SApple OSS Distributions 	 * The event consumer is interested about the effective {upid,pid,uuid}
2707*1031c584SApple OSS Distributions 	 * info which can be different than the those related to the process
2708*1031c584SApple OSS Distributions 	 * that recently performed a system call on the socket, i.e. when the
2709*1031c584SApple OSS Distributions 	 * socket is delegated.
2710*1031c584SApple OSS Distributions 	 */
2711*1031c584SApple OSS Distributions 	if (so->so_flags & SOF_DELEGATED) {
2712*1031c584SApple OSS Distributions 		ev_ifdenied.ev_data.eupid = so->e_upid;
2713*1031c584SApple OSS Distributions 		ev_ifdenied.ev_data.epid = so->e_pid;
2714*1031c584SApple OSS Distributions 		uuid_copy(ev_ifdenied.ev_data.euuid, so->e_uuid);
2715*1031c584SApple OSS Distributions 	} else {
2716*1031c584SApple OSS Distributions 		ev_ifdenied.ev_data.eupid = so->last_upid;
2717*1031c584SApple OSS Distributions 		ev_ifdenied.ev_data.epid = so->last_pid;
2718*1031c584SApple OSS Distributions 		uuid_copy(ev_ifdenied.ev_data.euuid, so->last_uuid);
2719*1031c584SApple OSS Distributions 	}
2720*1031c584SApple OSS Distributions 
2721*1031c584SApple OSS Distributions 	if (++so->so_ifdenied_notifies > 1) {
2722*1031c584SApple OSS Distributions 		/*
2723*1031c584SApple OSS Distributions 		 * Allow for at most one kernel event to be generated per
2724*1031c584SApple OSS Distributions 		 * socket; so_ifdenied_notifies is reset upon changes in
2725*1031c584SApple OSS Distributions 		 * the UUID policy.  See comments in inp_update_policy.
2726*1031c584SApple OSS Distributions 		 */
2727*1031c584SApple OSS Distributions 		if (net_io_policy_log) {
2728*1031c584SApple OSS Distributions 			uuid_string_t buf;
2729*1031c584SApple OSS Distributions 
2730*1031c584SApple OSS Distributions 			uuid_unparse(ev_ifdenied.ev_data.euuid, buf);
2731*1031c584SApple OSS Distributions 			log(LOG_DEBUG, "%s[%d]: so 0x%llx [%d,%d] epid %llu "
2732*1031c584SApple OSS Distributions 			    "euuid %s%s has %d redundant events supressed\n",
2733*1031c584SApple OSS Distributions 			    __func__, so->last_pid,
2734*1031c584SApple OSS Distributions 			    (uint64_t)VM_KERNEL_ADDRPERM(so), SOCK_DOM(so),
2735*1031c584SApple OSS Distributions 			    SOCK_TYPE(so), ev_ifdenied.ev_data.epid, buf,
2736*1031c584SApple OSS Distributions 			    ((so->so_flags & SOF_DELEGATED) ?
2737*1031c584SApple OSS Distributions 			    " [delegated]" : ""), so->so_ifdenied_notifies);
2738*1031c584SApple OSS Distributions 		}
2739*1031c584SApple OSS Distributions 	} else {
2740*1031c584SApple OSS Distributions 		if (net_io_policy_log) {
2741*1031c584SApple OSS Distributions 			uuid_string_t buf;
2742*1031c584SApple OSS Distributions 
2743*1031c584SApple OSS Distributions 			uuid_unparse(ev_ifdenied.ev_data.euuid, buf);
2744*1031c584SApple OSS Distributions 			log(LOG_DEBUG, "%s[%d]: so 0x%llx [%d,%d] epid %llu "
2745*1031c584SApple OSS Distributions 			    "euuid %s%s event posted\n", __func__,
2746*1031c584SApple OSS Distributions 			    so->last_pid, (uint64_t)VM_KERNEL_ADDRPERM(so),
2747*1031c584SApple OSS Distributions 			    SOCK_DOM(so), SOCK_TYPE(so),
2748*1031c584SApple OSS Distributions 			    ev_ifdenied.ev_data.epid, buf,
2749*1031c584SApple OSS Distributions 			    ((so->so_flags & SOF_DELEGATED) ?
2750*1031c584SApple OSS Distributions 			    " [delegated]" : ""));
2751*1031c584SApple OSS Distributions 		}
2752*1031c584SApple OSS Distributions 		netpolicy_post_msg(KEV_NETPOLICY_IFDENIED, &ev_ifdenied.ev_data,
2753*1031c584SApple OSS Distributions 		    sizeof(ev_ifdenied));
2754*1031c584SApple OSS Distributions 	}
2755*1031c584SApple OSS Distributions }
2756*1031c584SApple OSS Distributions 
2757*1031c584SApple OSS Distributions /*
2758*1031c584SApple OSS Distributions  * Make a copy of a sockaddr in a malloced buffer of type SONAME.
2759*1031c584SApple OSS Distributions  */
2760*1031c584SApple OSS Distributions struct sockaddr *
dup_sockaddr(struct sockaddr * sa,int canwait)2761*1031c584SApple OSS Distributions dup_sockaddr(struct sockaddr *sa, int canwait)
2762*1031c584SApple OSS Distributions {
2763*1031c584SApple OSS Distributions 	struct sockaddr *sa2;
2764*1031c584SApple OSS Distributions 
2765*1031c584SApple OSS Distributions 	sa2 = (struct sockaddr *)alloc_sockaddr(sa->sa_len, canwait ? Z_WAITOK : Z_NOWAIT);
2766*1031c584SApple OSS Distributions 	if (sa2 != NULL) {
2767*1031c584SApple OSS Distributions 		bcopy(sa, sa2, sa->sa_len);
2768*1031c584SApple OSS Distributions 	}
2769*1031c584SApple OSS Distributions 	return sa2;
2770*1031c584SApple OSS Distributions }
2771*1031c584SApple OSS Distributions 
2772*1031c584SApple OSS Distributions void *
alloc_sockaddr(size_t size,zalloc_flags_t flags)2773*1031c584SApple OSS Distributions alloc_sockaddr(size_t size, zalloc_flags_t flags)
2774*1031c584SApple OSS Distributions {
2775*1031c584SApple OSS Distributions 	VERIFY((size) <= UINT8_MAX);
2776*1031c584SApple OSS Distributions 
2777*1031c584SApple OSS Distributions 	__typed_allocators_ignore_push
2778*1031c584SApple OSS Distributions 	struct sockaddr *sa = kheap_alloc(KHEAP_SONAME, size, flags | Z_ZERO);
2779*1031c584SApple OSS Distributions 	__typed_allocators_ignore_pop
2780*1031c584SApple OSS Distributions 	if (sa != NULL) {
2781*1031c584SApple OSS Distributions 		sa->sa_len = (uint8_t)size;
2782*1031c584SApple OSS Distributions 	}
2783*1031c584SApple OSS Distributions 
2784*1031c584SApple OSS Distributions 	return sa;
2785*1031c584SApple OSS Distributions }
2786*1031c584SApple OSS Distributions 
2787*1031c584SApple OSS Distributions /*
2788*1031c584SApple OSS Distributions  * Create an external-format (``xsocket'') structure using the information
2789*1031c584SApple OSS Distributions  * in the kernel-format socket structure pointed to by so.  This is done
2790*1031c584SApple OSS Distributions  * to reduce the spew of irrelevant information over this interface,
2791*1031c584SApple OSS Distributions  * to isolate user code from changes in the kernel structure, and
2792*1031c584SApple OSS Distributions  * potentially to provide information-hiding if we decide that
2793*1031c584SApple OSS Distributions  * some of this information should be hidden from users.
2794*1031c584SApple OSS Distributions  */
2795*1031c584SApple OSS Distributions void
sotoxsocket(struct socket * so,struct xsocket * xso)2796*1031c584SApple OSS Distributions sotoxsocket(struct socket *so, struct xsocket *xso)
2797*1031c584SApple OSS Distributions {
2798*1031c584SApple OSS Distributions 	xso->xso_len = sizeof(*xso);
2799*1031c584SApple OSS Distributions 	xso->xso_so = (_XSOCKET_PTR(struct socket *))VM_KERNEL_ADDRPERM(so);
2800*1031c584SApple OSS Distributions 	xso->so_type = so->so_type;
2801*1031c584SApple OSS Distributions 	xso->so_options = (short)(so->so_options & 0xffff);
2802*1031c584SApple OSS Distributions 	xso->so_linger = so->so_linger;
2803*1031c584SApple OSS Distributions 	xso->so_state = so->so_state;
2804*1031c584SApple OSS Distributions 	xso->so_pcb = (_XSOCKET_PTR(caddr_t))VM_KERNEL_ADDRPERM(so->so_pcb);
2805*1031c584SApple OSS Distributions 	if (so->so_proto) {
2806*1031c584SApple OSS Distributions 		xso->xso_protocol = SOCK_PROTO(so);
2807*1031c584SApple OSS Distributions 		xso->xso_family = SOCK_DOM(so);
2808*1031c584SApple OSS Distributions 	} else {
2809*1031c584SApple OSS Distributions 		xso->xso_protocol = xso->xso_family = 0;
2810*1031c584SApple OSS Distributions 	}
2811*1031c584SApple OSS Distributions 	xso->so_qlen = so->so_qlen;
2812*1031c584SApple OSS Distributions 	xso->so_incqlen = so->so_incqlen;
2813*1031c584SApple OSS Distributions 	xso->so_qlimit = so->so_qlimit;
2814*1031c584SApple OSS Distributions 	xso->so_timeo = so->so_timeo;
2815*1031c584SApple OSS Distributions 	xso->so_error = so->so_error;
2816*1031c584SApple OSS Distributions 	xso->so_pgid = so->so_pgid;
2817*1031c584SApple OSS Distributions 	xso->so_oobmark = so->so_oobmark;
2818*1031c584SApple OSS Distributions 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
2819*1031c584SApple OSS Distributions 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
2820*1031c584SApple OSS Distributions 	xso->so_uid = kauth_cred_getuid(so->so_cred);
2821*1031c584SApple OSS Distributions }
2822*1031c584SApple OSS Distributions 
2823*1031c584SApple OSS Distributions 
2824*1031c584SApple OSS Distributions #if XNU_TARGET_OS_OSX
2825*1031c584SApple OSS Distributions 
2826*1031c584SApple OSS Distributions void
sotoxsocket64(struct socket * so,struct xsocket64 * xso)2827*1031c584SApple OSS Distributions sotoxsocket64(struct socket *so, struct xsocket64 *xso)
2828*1031c584SApple OSS Distributions {
2829*1031c584SApple OSS Distributions 	xso->xso_len = sizeof(*xso);
2830*1031c584SApple OSS Distributions 	xso->xso_so = (u_int64_t)VM_KERNEL_ADDRPERM(so);
2831*1031c584SApple OSS Distributions 	xso->so_type = so->so_type;
2832*1031c584SApple OSS Distributions 	xso->so_options = (short)(so->so_options & 0xffff);
2833*1031c584SApple OSS Distributions 	xso->so_linger = so->so_linger;
2834*1031c584SApple OSS Distributions 	xso->so_state = so->so_state;
2835*1031c584SApple OSS Distributions 	xso->so_pcb = (u_int64_t)VM_KERNEL_ADDRPERM(so->so_pcb);
2836*1031c584SApple OSS Distributions 	if (so->so_proto) {
2837*1031c584SApple OSS Distributions 		xso->xso_protocol = SOCK_PROTO(so);
2838*1031c584SApple OSS Distributions 		xso->xso_family = SOCK_DOM(so);
2839*1031c584SApple OSS Distributions 	} else {
2840*1031c584SApple OSS Distributions 		xso->xso_protocol = xso->xso_family = 0;
2841*1031c584SApple OSS Distributions 	}
2842*1031c584SApple OSS Distributions 	xso->so_qlen = so->so_qlen;
2843*1031c584SApple OSS Distributions 	xso->so_incqlen = so->so_incqlen;
2844*1031c584SApple OSS Distributions 	xso->so_qlimit = so->so_qlimit;
2845*1031c584SApple OSS Distributions 	xso->so_timeo = so->so_timeo;
2846*1031c584SApple OSS Distributions 	xso->so_error = so->so_error;
2847*1031c584SApple OSS Distributions 	xso->so_pgid = so->so_pgid;
2848*1031c584SApple OSS Distributions 	xso->so_oobmark = so->so_oobmark;
2849*1031c584SApple OSS Distributions 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
2850*1031c584SApple OSS Distributions 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
2851*1031c584SApple OSS Distributions 	xso->so_uid = kauth_cred_getuid(so->so_cred);
2852*1031c584SApple OSS Distributions }
2853*1031c584SApple OSS Distributions 
2854*1031c584SApple OSS Distributions #endif /* XNU_TARGET_OS_OSX */
2855*1031c584SApple OSS Distributions 
2856*1031c584SApple OSS Distributions /*
2857*1031c584SApple OSS Distributions  * This does the same for sockbufs.  Note that the xsockbuf structure,
2858*1031c584SApple OSS Distributions  * since it is always embedded in a socket, does not include a self
2859*1031c584SApple OSS Distributions  * pointer nor a length.  We make this entry point public in case
2860*1031c584SApple OSS Distributions  * some other mechanism needs it.
2861*1031c584SApple OSS Distributions  */
2862*1031c584SApple OSS Distributions void
sbtoxsockbuf(struct sockbuf * sb,struct xsockbuf * xsb)2863*1031c584SApple OSS Distributions sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
2864*1031c584SApple OSS Distributions {
2865*1031c584SApple OSS Distributions 	xsb->sb_cc = sb->sb_cc;
2866*1031c584SApple OSS Distributions 	xsb->sb_hiwat = sb->sb_hiwat;
2867*1031c584SApple OSS Distributions 	xsb->sb_mbcnt = sb->sb_mbcnt;
2868*1031c584SApple OSS Distributions 	xsb->sb_mbmax = sb->sb_mbmax;
2869*1031c584SApple OSS Distributions 	xsb->sb_lowat = sb->sb_lowat;
2870*1031c584SApple OSS Distributions 	xsb->sb_flags = (short)sb->sb_flags;
2871*1031c584SApple OSS Distributions 	xsb->sb_timeo = (short)
2872*1031c584SApple OSS Distributions 	    ((sb->sb_timeo.tv_sec * hz) + sb->sb_timeo.tv_usec / tick);
2873*1031c584SApple OSS Distributions 	if (xsb->sb_timeo == 0 && sb->sb_timeo.tv_usec != 0) {
2874*1031c584SApple OSS Distributions 		xsb->sb_timeo = 1;
2875*1031c584SApple OSS Distributions 	}
2876*1031c584SApple OSS Distributions }
2877*1031c584SApple OSS Distributions 
2878*1031c584SApple OSS Distributions /*
2879*1031c584SApple OSS Distributions  * Based on the policy set by an all knowing decison maker, throttle sockets
2880*1031c584SApple OSS Distributions  * that either have been marked as belonging to "background" process.
2881*1031c584SApple OSS Distributions  */
2882*1031c584SApple OSS Distributions inline int
soisthrottled(struct socket * so)2883*1031c584SApple OSS Distributions soisthrottled(struct socket *so)
2884*1031c584SApple OSS Distributions {
2885*1031c584SApple OSS Distributions 	return so->so_flags1 & SOF1_TRAFFIC_MGT_SO_BACKGROUND;
2886*1031c584SApple OSS Distributions }
2887*1031c584SApple OSS Distributions 
2888*1031c584SApple OSS Distributions inline int
soisprivilegedtraffic(struct socket * so)2889*1031c584SApple OSS Distributions soisprivilegedtraffic(struct socket *so)
2890*1031c584SApple OSS Distributions {
2891*1031c584SApple OSS Distributions 	return (so->so_flags & SOF_PRIVILEGED_TRAFFIC_CLASS) ? 1 : 0;
2892*1031c584SApple OSS Distributions }
2893*1031c584SApple OSS Distributions 
2894*1031c584SApple OSS Distributions inline int
soissrcbackground(struct socket * so)2895*1031c584SApple OSS Distributions soissrcbackground(struct socket *so)
2896*1031c584SApple OSS Distributions {
2897*1031c584SApple OSS Distributions 	return (so->so_flags1 & SOF1_TRAFFIC_MGT_SO_BACKGROUND) ||
2898*1031c584SApple OSS Distributions 	       IS_SO_TC_BACKGROUND(so->so_traffic_class);
2899*1031c584SApple OSS Distributions }
2900*1031c584SApple OSS Distributions 
2901*1031c584SApple OSS Distributions inline int
soissrcrealtime(struct socket * so)2902*1031c584SApple OSS Distributions soissrcrealtime(struct socket *so)
2903*1031c584SApple OSS Distributions {
2904*1031c584SApple OSS Distributions 	return so->so_traffic_class >= SO_TC_AV &&
2905*1031c584SApple OSS Distributions 	       so->so_traffic_class <= SO_TC_VO;
2906*1031c584SApple OSS Distributions }
2907*1031c584SApple OSS Distributions 
2908*1031c584SApple OSS Distributions inline int
soissrcbesteffort(struct socket * so)2909*1031c584SApple OSS Distributions soissrcbesteffort(struct socket *so)
2910*1031c584SApple OSS Distributions {
2911*1031c584SApple OSS Distributions 	return so->so_traffic_class == SO_TC_BE ||
2912*1031c584SApple OSS Distributions 	       so->so_traffic_class == SO_TC_RD ||
2913*1031c584SApple OSS Distributions 	       so->so_traffic_class == SO_TC_OAM;
2914*1031c584SApple OSS Distributions }
2915*1031c584SApple OSS Distributions 
2916*1031c584SApple OSS Distributions void
soclearfastopen(struct socket * so)2917*1031c584SApple OSS Distributions soclearfastopen(struct socket *so)
2918*1031c584SApple OSS Distributions {
2919*1031c584SApple OSS Distributions 	if (so->so_flags1 & SOF1_PRECONNECT_DATA) {
2920*1031c584SApple OSS Distributions 		so->so_flags1 &= ~SOF1_PRECONNECT_DATA;
2921*1031c584SApple OSS Distributions 	}
2922*1031c584SApple OSS Distributions 
2923*1031c584SApple OSS Distributions 	if (so->so_flags1 & SOF1_DATA_IDEMPOTENT) {
2924*1031c584SApple OSS Distributions 		so->so_flags1 &= ~SOF1_DATA_IDEMPOTENT;
2925*1031c584SApple OSS Distributions 	}
2926*1031c584SApple OSS Distributions }
2927*1031c584SApple OSS Distributions 
2928*1031c584SApple OSS Distributions void
sonullevent(struct socket * so,void * arg,uint32_t hint)2929*1031c584SApple OSS Distributions sonullevent(struct socket *so, void *arg, uint32_t hint)
2930*1031c584SApple OSS Distributions {
2931*1031c584SApple OSS Distributions #pragma unused(so, arg, hint)
2932*1031c584SApple OSS Distributions }
2933*1031c584SApple OSS Distributions 
2934*1031c584SApple OSS Distributions /*
2935*1031c584SApple OSS Distributions  * Here is the definition of some of the basic objects in the kern.ipc
2936*1031c584SApple OSS Distributions  * branch of the MIB.
2937*1031c584SApple OSS Distributions  */
2938*1031c584SApple OSS Distributions SYSCTL_NODE(_kern, KERN_IPC, ipc,
2939*1031c584SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY, 0, "IPC");
2940*1031c584SApple OSS Distributions 
2941*1031c584SApple OSS Distributions /* Check that the maximum socket buffer size is within a range */
2942*1031c584SApple OSS Distributions 
2943*1031c584SApple OSS Distributions static int
2944*1031c584SApple OSS Distributions sysctl_sb_max SYSCTL_HANDLER_ARGS
2945*1031c584SApple OSS Distributions {
2946*1031c584SApple OSS Distributions #pragma unused(oidp, arg1, arg2)
2947*1031c584SApple OSS Distributions 	u_int32_t new_value;
2948*1031c584SApple OSS Distributions 	int changed = 0;
2949*1031c584SApple OSS Distributions 	int error = sysctl_io_number(req, sb_max, sizeof(u_int32_t),
2950*1031c584SApple OSS Distributions 	    &new_value, &changed);
2951*1031c584SApple OSS Distributions 	if (!error && changed) {
2952*1031c584SApple OSS Distributions 		if (new_value > LOW_SB_MAX && new_value <= high_sb_max &&
2953*1031c584SApple OSS Distributions 		    SB_MAX_ADJUST(new_value) < UINT32_MAX) {
2954*1031c584SApple OSS Distributions 			sb_max = new_value;
2955*1031c584SApple OSS Distributions 			sb_max_adj = SB_MAX_ADJUST(sb_max);
2956*1031c584SApple OSS Distributions 		} else {
2957*1031c584SApple OSS Distributions 			error = ERANGE;
2958*1031c584SApple OSS Distributions 		}
2959*1031c584SApple OSS Distributions 	}
2960*1031c584SApple OSS Distributions 	return error;
2961*1031c584SApple OSS Distributions }
2962*1031c584SApple OSS Distributions 
2963*1031c584SApple OSS Distributions SYSCTL_PROC(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf,
2964*1031c584SApple OSS Distributions     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2965*1031c584SApple OSS Distributions     &sb_max, 0, &sysctl_sb_max, "IU", "Maximum socket buffer size");
2966*1031c584SApple OSS Distributions 
2967*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor,
2968*1031c584SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &sb_efficiency, 0, "");
2969*1031c584SApple OSS Distributions 
2970*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters,
2971*1031c584SApple OSS Distributions     CTLFLAG_RD | CTLFLAG_LOCKED, &nmbclusters, 0, "");
2972*1031c584SApple OSS Distributions 
2973*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, OID_AUTO, njcl,
2974*1031c584SApple OSS Distributions     CTLFLAG_RD | CTLFLAG_LOCKED, &njcl, 0, "");
2975*1031c584SApple OSS Distributions 
2976*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, OID_AUTO, njclbytes,
2977*1031c584SApple OSS Distributions     CTLFLAG_RD | CTLFLAG_LOCKED, &njclbytes, 0, "");
2978*1031c584SApple OSS Distributions 
2979*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, KIPC_SOQLIMITCOMPAT, soqlimitcompat,
2980*1031c584SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &soqlimitcompat, 1,
2981*1031c584SApple OSS Distributions     "Enable socket queue limit compatibility");
2982*1031c584SApple OSS Distributions 
2983*1031c584SApple OSS Distributions /*
2984*1031c584SApple OSS Distributions  * Hack alert -- rdar://33572856
2985*1031c584SApple OSS Distributions  * A loopback test we cannot change was failing because it sets
2986*1031c584SApple OSS Distributions  * SO_SENDTIMEO to 5 seconds and that's also the value
2987*1031c584SApple OSS Distributions  * of the minimum persist timer. Because of the persist timer,
2988*1031c584SApple OSS Distributions  * the connection was not idle for 5 seconds and SO_SNDTIMEO
2989*1031c584SApple OSS Distributions  * was not triggering at 5 seconds causing the test failure.
2990*1031c584SApple OSS Distributions  * As a workaround we check the sysctl soqlencomp the test is already
2991*1031c584SApple OSS Distributions  * setting to set disable auto tuning of the receive buffer.
2992*1031c584SApple OSS Distributions  */
2993*1031c584SApple OSS Distributions 
2994*1031c584SApple OSS Distributions extern u_int32_t tcp_do_autorcvbuf;
2995*1031c584SApple OSS Distributions 
2996*1031c584SApple OSS Distributions static int
2997*1031c584SApple OSS Distributions sysctl_soqlencomp SYSCTL_HANDLER_ARGS
2998*1031c584SApple OSS Distributions {
2999*1031c584SApple OSS Distributions #pragma unused(oidp, arg1, arg2)
3000*1031c584SApple OSS Distributions 	u_int32_t new_value;
3001*1031c584SApple OSS Distributions 	int changed = 0;
3002*1031c584SApple OSS Distributions 	int error = sysctl_io_number(req, soqlencomp, sizeof(u_int32_t),
3003*1031c584SApple OSS Distributions 	    &new_value, &changed);
3004*1031c584SApple OSS Distributions 	if (!error && changed) {
3005*1031c584SApple OSS Distributions 		soqlencomp = new_value;
3006*1031c584SApple OSS Distributions 		if (new_value != 0) {
3007*1031c584SApple OSS Distributions 			tcp_do_autorcvbuf = 0;
3008*1031c584SApple OSS Distributions 			tcptv_persmin_val = 6 * TCP_RETRANSHZ;
3009*1031c584SApple OSS Distributions 		}
3010*1031c584SApple OSS Distributions 	}
3011*1031c584SApple OSS Distributions 	return error;
3012*1031c584SApple OSS Distributions }
3013*1031c584SApple OSS Distributions SYSCTL_PROC(_kern_ipc, OID_AUTO, soqlencomp,
3014*1031c584SApple OSS Distributions     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
3015*1031c584SApple OSS Distributions     &soqlencomp, 0, &sysctl_soqlencomp, "IU", "");
3016*1031c584SApple OSS Distributions 
3017*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, OID_AUTO, sbmb_cnt, CTLFLAG_RD | CTLFLAG_LOCKED,
3018*1031c584SApple OSS Distributions     &total_sbmb_cnt, 0, "");
3019*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, OID_AUTO, sbmb_cnt_peak, CTLFLAG_RD | CTLFLAG_LOCKED,
3020*1031c584SApple OSS Distributions     &total_sbmb_cnt_peak, 0, "");
3021*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc, OID_AUTO, sbmb_cnt_floor, CTLFLAG_RD | CTLFLAG_LOCKED,
3022*1031c584SApple OSS Distributions     &total_sbmb_cnt_floor, 0, "");
3023*1031c584SApple OSS Distributions SYSCTL_QUAD(_kern_ipc, OID_AUTO, sbmb_limreached, CTLFLAG_RD | CTLFLAG_LOCKED,
3024*1031c584SApple OSS Distributions     &sbmb_limreached, "");
3025*1031c584SApple OSS Distributions 
3026*1031c584SApple OSS Distributions 
3027*1031c584SApple OSS Distributions SYSCTL_NODE(_kern_ipc, OID_AUTO, io_policy, CTLFLAG_RW, 0, "network IO policy");
3028*1031c584SApple OSS Distributions 
3029*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc_io_policy, OID_AUTO, log, CTLFLAG_RW | CTLFLAG_LOCKED,
3030*1031c584SApple OSS Distributions     &net_io_policy_log, 0, "");
3031*1031c584SApple OSS Distributions 
3032*1031c584SApple OSS Distributions #if CONFIG_PROC_UUID_POLICY
3033*1031c584SApple OSS Distributions SYSCTL_INT(_kern_ipc_io_policy, OID_AUTO, uuid, CTLFLAG_RW | CTLFLAG_LOCKED,
3034*1031c584SApple OSS Distributions     &net_io_policy_uuid, 0, "");
3035*1031c584SApple OSS Distributions #endif /* CONFIG_PROC_UUID_POLICY */
3036