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