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