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