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