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 if (sb->sb_flags & SB_SENDHEAD) {
1780 sb->sb_sendhead = NULL;
1781 }
1782
1783 sb_empty_assert(sb, __func__);
1784 sbunlock(sb, TRUE); /* keep socket locked */
1785 }
1786
1787 /*
1788 * Drop data from (the front of) a sockbuf.
1789 * use m_freem_list to free the mbuf structures
1790 * under a single lock... this is done by pruning
1791 * the top of the tree from the body by keeping track
1792 * of where we get to in the tree and then zeroing the
1793 * two pertinent pointers m_nextpkt and m_next
1794 * the socket buffer is then updated to point at the new
1795 * top of the tree and the pruned area is released via
1796 * m_freem_list.
1797 */
1798 void
sbdrop(struct sockbuf * sb,int len)1799 sbdrop(struct sockbuf *sb, int len)
1800 {
1801 struct mbuf *m, *free_list, *ml;
1802 struct mbuf *next, *last;
1803
1804 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1805 #if MPTCP
1806 if (m != NULL && len > 0 && !(sb->sb_flags & SB_RECV) &&
1807 ((sb->sb_so->so_flags & SOF_MP_SUBFLOW) ||
1808 (SOCK_CHECK_DOM(sb->sb_so, PF_MULTIPATH) &&
1809 SOCK_CHECK_PROTO(sb->sb_so, IPPROTO_TCP))) &&
1810 !(sb->sb_so->so_flags1 & SOF1_POST_FALLBACK_SYNC)) {
1811 mptcp_preproc_sbdrop(sb->sb_so, m, (unsigned int)len);
1812 }
1813 if (m != NULL && len > 0 && !(sb->sb_flags & SB_RECV) &&
1814 (sb->sb_so->so_flags & SOF_MP_SUBFLOW) &&
1815 (sb->sb_so->so_flags1 & SOF1_POST_FALLBACK_SYNC)) {
1816 mptcp_fallback_sbdrop(sb->sb_so, m, len);
1817 }
1818 #endif /* MPTCP */
1819 KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_START), sb, len, 0, 0, 0);
1820
1821 free_list = last = m;
1822 ml = (struct mbuf *)0;
1823
1824 if (sb->sb_flags & SB_SENDHEAD) {
1825 sb->sb_sendoff -= MIN(len, sb->sb_sendoff);
1826 }
1827
1828 while (len > 0) {
1829 if (m == NULL) {
1830 if (next == NULL) {
1831 /*
1832 * temporarily replacing this panic with printf
1833 * because it occurs occasionally when closing
1834 * a socket when there is no harm in ignoring
1835 * it. This problem will be investigated
1836 * further.
1837 */
1838 /* panic("sbdrop"); */
1839 printf("sbdrop - count not zero\n");
1840 len = 0;
1841 /*
1842 * zero the counts. if we have no mbufs,
1843 * we have no data (PR-2986815)
1844 */
1845 sb->sb_cc = 0;
1846 sb->sb_mbcnt = 0;
1847 break;
1848 }
1849 m = last = next;
1850 next = m->m_nextpkt;
1851 continue;
1852 }
1853 if (m->m_len > len) {
1854 m->m_len -= len;
1855 m->m_data += len;
1856 sb->sb_cc -= len;
1857 /* update the send byte count */
1858 if (sb->sb_flags & SB_SNDBYTE_CNT) {
1859 inp_decr_sndbytes_total(sb->sb_so, len);
1860 }
1861 if (sb->sb_flags & SB_SENDHEAD) {
1862 if (sb->sb_sendhead == m) {
1863 sb->sb_sendhead = NULL;
1864 }
1865 }
1866 if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
1867 m->m_type != MT_OOBDATA) {
1868 sb->sb_ctl -= len;
1869 }
1870 break;
1871 }
1872 len -= m->m_len;
1873 sbfree(sb, m);
1874
1875 ml = m;
1876 m = m->m_next;
1877 }
1878 while (m && m->m_len == 0) {
1879 sbfree(sb, m);
1880
1881 ml = m;
1882 m = m->m_next;
1883 }
1884 if (ml) {
1885 ml->m_next = (struct mbuf *)0;
1886 last->m_nextpkt = (struct mbuf *)0;
1887 m_freem_list(free_list);
1888 }
1889 if (m) {
1890 sb->sb_mb = m;
1891 m->m_nextpkt = next;
1892 } else {
1893 sb->sb_mb = next;
1894 }
1895
1896 /*
1897 * First part is an inline SB_EMPTY_FIXUP(). Second part
1898 * makes sure sb_lastrecord is up-to-date if we dropped
1899 * part of the last record.
1900 */
1901 m = sb->sb_mb;
1902 if (m == NULL) {
1903 sb->sb_mbtail = NULL;
1904 sb->sb_lastrecord = NULL;
1905 } else if (m->m_nextpkt == NULL) {
1906 sb->sb_lastrecord = m;
1907 }
1908
1909 #if CONTENT_FILTER
1910 cfil_sock_buf_update(sb);
1911 #endif /* CONTENT_FILTER */
1912
1913 KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, 0, 0, 0, 0);
1914 }
1915
1916 /*
1917 * Drop a record off the front of a sockbuf
1918 * and move the next record to the front.
1919 */
1920 void
sbdroprecord(struct sockbuf * sb)1921 sbdroprecord(struct sockbuf *sb)
1922 {
1923 struct mbuf *m, *mn;
1924
1925 m = sb->sb_mb;
1926 if (m) {
1927 sb->sb_mb = m->m_nextpkt;
1928 do {
1929 sbfree(sb, m);
1930 MFREE(m, mn);
1931 m = mn;
1932 } while (m);
1933 }
1934 SB_EMPTY_FIXUP(sb);
1935 }
1936
1937 /*
1938 * Create a "control" mbuf containing the specified data
1939 * with the specified type for presentation on a socket buffer.
1940 */
1941 struct mbuf *
sbcreatecontrol(caddr_t p,int size,int type,int level)1942 sbcreatecontrol(caddr_t p, int size, int type, int level)
1943 {
1944 struct cmsghdr *cp;
1945 struct mbuf *m;
1946
1947 if (CMSG_SPACE((u_int)size) > MLEN) {
1948 return (struct mbuf *)NULL;
1949 }
1950 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) {
1951 return (struct mbuf *)NULL;
1952 }
1953 cp = mtod(m, struct cmsghdr *);
1954 VERIFY(IS_P2ALIGNED(cp, sizeof(u_int32_t)));
1955 /* XXX check size? */
1956 (void) memcpy(CMSG_DATA(cp), p, size);
1957 m->m_len = (int32_t)CMSG_SPACE(size);
1958 cp->cmsg_len = CMSG_LEN(size);
1959 cp->cmsg_level = level;
1960 cp->cmsg_type = type;
1961 return m;
1962 }
1963
1964 struct mbuf **
sbcreatecontrol_mbuf(caddr_t p,int size,int type,int level,struct mbuf ** mp)1965 sbcreatecontrol_mbuf(caddr_t p, int size, int type, int level, struct mbuf **mp)
1966 {
1967 struct mbuf *m;
1968 struct cmsghdr *cp;
1969
1970 if (*mp == NULL) {
1971 *mp = sbcreatecontrol(p, size, type, level);
1972 return mp;
1973 }
1974
1975 if (CMSG_SPACE((u_int)size) + (*mp)->m_len > MLEN) {
1976 mp = &(*mp)->m_next;
1977 *mp = sbcreatecontrol(p, size, type, level);
1978 return mp;
1979 }
1980
1981 m = *mp;
1982
1983 cp = (struct cmsghdr *)(void *)(mtod(m, char *) + m->m_len);
1984 /* CMSG_SPACE ensures 32-bit alignment */
1985 VERIFY(IS_P2ALIGNED(cp, sizeof(u_int32_t)));
1986 m->m_len += (int32_t)CMSG_SPACE(size);
1987
1988 /* XXX check size? */
1989 (void) memcpy(CMSG_DATA(cp), p, size);
1990 cp->cmsg_len = CMSG_LEN(size);
1991 cp->cmsg_level = level;
1992 cp->cmsg_type = type;
1993
1994 return mp;
1995 }
1996
1997
1998 /*
1999 * Some routines that return EOPNOTSUPP for entry points that are not
2000 * supported by a protocol. Fill in as needed.
2001 */
2002 int
pru_abort_notsupp(struct socket * so)2003 pru_abort_notsupp(struct socket *so)
2004 {
2005 #pragma unused(so)
2006 return EOPNOTSUPP;
2007 }
2008
2009 int
pru_accept_notsupp(struct socket * so,struct sockaddr ** nam)2010 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
2011 {
2012 #pragma unused(so, nam)
2013 return EOPNOTSUPP;
2014 }
2015
2016 int
pru_attach_notsupp(struct socket * so,int proto,struct proc * p)2017 pru_attach_notsupp(struct socket *so, int proto, struct proc *p)
2018 {
2019 #pragma unused(so, proto, p)
2020 return EOPNOTSUPP;
2021 }
2022
2023 int
pru_bind_notsupp(struct socket * so,struct sockaddr * nam,struct proc * p)2024 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
2025 {
2026 #pragma unused(so, nam, p)
2027 return EOPNOTSUPP;
2028 }
2029
2030 int
pru_connect_notsupp(struct socket * so,struct sockaddr * nam,struct proc * p)2031 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
2032 {
2033 #pragma unused(so, nam, p)
2034 return EOPNOTSUPP;
2035 }
2036
2037 int
pru_connect2_notsupp(struct socket * so1,struct socket * so2)2038 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
2039 {
2040 #pragma unused(so1, so2)
2041 return EOPNOTSUPP;
2042 }
2043
2044 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)2045 pru_connectx_notsupp(struct socket *so, struct sockaddr *src,
2046 struct sockaddr *dst, struct proc *p, uint32_t ifscope,
2047 sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
2048 uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
2049 {
2050 #pragma unused(so, src, dst, p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written)
2051 return EOPNOTSUPP;
2052 }
2053
2054 int
pru_control_notsupp(struct socket * so,u_long cmd,caddr_t data,struct ifnet * ifp,struct proc * p)2055 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
2056 struct ifnet *ifp, struct proc *p)
2057 {
2058 #pragma unused(so, cmd, data, ifp, p)
2059 return EOPNOTSUPP;
2060 }
2061
2062 int
pru_detach_notsupp(struct socket * so)2063 pru_detach_notsupp(struct socket *so)
2064 {
2065 #pragma unused(so)
2066 return EOPNOTSUPP;
2067 }
2068
2069 int
pru_disconnect_notsupp(struct socket * so)2070 pru_disconnect_notsupp(struct socket *so)
2071 {
2072 #pragma unused(so)
2073 return EOPNOTSUPP;
2074 }
2075
2076 int
pru_disconnectx_notsupp(struct socket * so,sae_associd_t aid,sae_connid_t cid)2077 pru_disconnectx_notsupp(struct socket *so, sae_associd_t aid, sae_connid_t cid)
2078 {
2079 #pragma unused(so, aid, cid)
2080 return EOPNOTSUPP;
2081 }
2082
2083 int
pru_listen_notsupp(struct socket * so,struct proc * p)2084 pru_listen_notsupp(struct socket *so, struct proc *p)
2085 {
2086 #pragma unused(so, p)
2087 return EOPNOTSUPP;
2088 }
2089
2090 int
pru_peeraddr_notsupp(struct socket * so,struct sockaddr ** nam)2091 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
2092 {
2093 #pragma unused(so, nam)
2094 return EOPNOTSUPP;
2095 }
2096
2097 int
pru_rcvd_notsupp(struct socket * so,int flags)2098 pru_rcvd_notsupp(struct socket *so, int flags)
2099 {
2100 #pragma unused(so, flags)
2101 return EOPNOTSUPP;
2102 }
2103
2104 int
pru_rcvoob_notsupp(struct socket * so,struct mbuf * m,int flags)2105 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
2106 {
2107 #pragma unused(so, m, flags)
2108 return EOPNOTSUPP;
2109 }
2110
2111 int
pru_send_notsupp(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct proc * p)2112 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
2113 struct sockaddr *addr, struct mbuf *control, struct proc *p)
2114 {
2115 #pragma unused(so, flags, m, addr, control, p)
2116 return EOPNOTSUPP;
2117 }
2118
2119 int
pru_send_list_notsupp(struct socket * so,struct mbuf * m,u_int * pktcnt,int flags)2120 pru_send_list_notsupp(struct socket *so, struct mbuf *m, u_int *pktcnt,
2121 int flags)
2122 {
2123 #pragma unused(so, m, pktcnt, flags)
2124 return EOPNOTSUPP;
2125 }
2126
2127 /*
2128 * This isn't really a ``null'' operation, but it's the default one
2129 * and doesn't do anything destructive.
2130 */
2131 int
pru_sense_null(struct socket * so,void * ub,int isstat64)2132 pru_sense_null(struct socket *so, void *ub, int isstat64)
2133 {
2134 if (isstat64 != 0) {
2135 struct stat64 *sb64;
2136
2137 sb64 = (struct stat64 *)ub;
2138 sb64->st_blksize = so->so_snd.sb_hiwat;
2139 } else {
2140 struct stat *sb;
2141
2142 sb = (struct stat *)ub;
2143 sb->st_blksize = so->so_snd.sb_hiwat;
2144 }
2145
2146 return 0;
2147 }
2148
2149 int
pru_sosend_notsupp(struct socket * so,struct sockaddr * addr,struct uio * uio,struct mbuf * top,struct mbuf * control,int flags)2150 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
2151 struct mbuf *top, struct mbuf *control, int flags)
2152 {
2153 #pragma unused(so, addr, uio, top, control, flags)
2154 return EOPNOTSUPP;
2155 }
2156
2157 int
pru_sosend_list_notsupp(struct socket * so,struct mbuf * m,size_t total_len,u_int * pktcnt,int flags)2158 pru_sosend_list_notsupp(struct socket *so, struct mbuf *m, size_t total_len, u_int *pktcnt, int flags)
2159 {
2160 #pragma unused(so, m, total_len, pktcnt, flags)
2161 return EOPNOTSUPP;
2162 }
2163
2164 int
pru_soreceive_notsupp(struct socket * so,struct sockaddr ** paddr,struct uio * uio,struct mbuf ** mp0,struct mbuf ** controlp,int * flagsp)2165 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
2166 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2167 {
2168 #pragma unused(so, paddr, uio, mp0, controlp, flagsp)
2169 return EOPNOTSUPP;
2170 }
2171
2172 int
pru_shutdown_notsupp(struct socket * so)2173 pru_shutdown_notsupp(struct socket *so)
2174 {
2175 #pragma unused(so)
2176 return EOPNOTSUPP;
2177 }
2178
2179 int
pru_sockaddr_notsupp(struct socket * so,struct sockaddr ** nam)2180 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
2181 {
2182 #pragma unused(so, nam)
2183 return EOPNOTSUPP;
2184 }
2185
2186 int
pru_sopoll_notsupp(struct socket * so,int events,kauth_cred_t cred,void * wql)2187 pru_sopoll_notsupp(struct socket *so, int events, kauth_cred_t cred, void *wql)
2188 {
2189 #pragma unused(so, events, cred, wql)
2190 return EOPNOTSUPP;
2191 }
2192
2193 int
pru_socheckopt_null(struct socket * so,struct sockopt * sopt)2194 pru_socheckopt_null(struct socket *so, struct sockopt *sopt)
2195 {
2196 #pragma unused(so, sopt)
2197 /*
2198 * Allow all options for set/get by default.
2199 */
2200 return 0;
2201 }
2202
2203 static int
pru_preconnect_null(struct socket * so)2204 pru_preconnect_null(struct socket *so)
2205 {
2206 #pragma unused(so)
2207 return 0;
2208 }
2209
2210 static int
pru_defunct_null(struct socket * so)2211 pru_defunct_null(struct socket *so)
2212 {
2213 #pragma unused(so)
2214 return 0;
2215 }
2216
2217
2218 void
pru_sanitize(struct pr_usrreqs * pru)2219 pru_sanitize(struct pr_usrreqs *pru)
2220 {
2221 #define DEFAULT(foo, bar) if ((foo) == NULL) (foo) = (bar)
2222 DEFAULT(pru->pru_abort, pru_abort_notsupp);
2223 DEFAULT(pru->pru_accept, pru_accept_notsupp);
2224 DEFAULT(pru->pru_attach, pru_attach_notsupp);
2225 DEFAULT(pru->pru_bind, pru_bind_notsupp);
2226 DEFAULT(pru->pru_connect, pru_connect_notsupp);
2227 DEFAULT(pru->pru_connect2, pru_connect2_notsupp);
2228 DEFAULT(pru->pru_connectx, pru_connectx_notsupp);
2229 DEFAULT(pru->pru_control, pru_control_notsupp);
2230 DEFAULT(pru->pru_detach, pru_detach_notsupp);
2231 DEFAULT(pru->pru_disconnect, pru_disconnect_notsupp);
2232 DEFAULT(pru->pru_disconnectx, pru_disconnectx_notsupp);
2233 DEFAULT(pru->pru_listen, pru_listen_notsupp);
2234 DEFAULT(pru->pru_peeraddr, pru_peeraddr_notsupp);
2235 DEFAULT(pru->pru_rcvd, pru_rcvd_notsupp);
2236 DEFAULT(pru->pru_rcvoob, pru_rcvoob_notsupp);
2237 DEFAULT(pru->pru_send, pru_send_notsupp);
2238 DEFAULT(pru->pru_send_list, pru_send_list_notsupp);
2239 DEFAULT(pru->pru_sense, pru_sense_null);
2240 DEFAULT(pru->pru_shutdown, pru_shutdown_notsupp);
2241 DEFAULT(pru->pru_sockaddr, pru_sockaddr_notsupp);
2242 DEFAULT(pru->pru_sopoll, pru_sopoll_notsupp);
2243 DEFAULT(pru->pru_soreceive, pru_soreceive_notsupp);
2244 DEFAULT(pru->pru_sosend, pru_sosend_notsupp);
2245 DEFAULT(pru->pru_sosend_list, pru_sosend_list_notsupp);
2246 DEFAULT(pru->pru_socheckopt, pru_socheckopt_null);
2247 DEFAULT(pru->pru_preconnect, pru_preconnect_null);
2248 DEFAULT(pru->pru_defunct, pru_defunct_null);
2249 #undef DEFAULT
2250 }
2251
2252 /*
2253 * The following are macros on BSD and functions on Darwin
2254 */
2255
2256 /*
2257 * Do we need to notify the other side when I/O is possible?
2258 */
2259
2260 int
sb_notify(struct sockbuf * sb)2261 sb_notify(struct sockbuf *sb)
2262 {
2263 return sb->sb_waiters > 0 ||
2264 (sb->sb_flags & (SB_SEL | SB_ASYNC | SB_UPCALL | SB_KNOTE));
2265 }
2266
2267 /*
2268 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
2269 * This is problematical if the fields are unsigned, as the space might
2270 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
2271 * overflow and return 0.
2272 */
2273 int
sbspace(struct sockbuf * sb)2274 sbspace(struct sockbuf *sb)
2275 {
2276 int pending = 0;
2277 int space;
2278
2279 if (sb->sb_flags & SB_KCTL) {
2280 space = (int)(sb->sb_hiwat - sb->sb_cc);
2281 } else {
2282 space = imin((int)(sb->sb_hiwat - sb->sb_cc),
2283 (int)(sb->sb_mbmax - sb->sb_mbcnt));
2284 }
2285 if (sb->sb_preconn_hiwat != 0) {
2286 space = imin((int)(sb->sb_preconn_hiwat - sb->sb_cc), space);
2287 }
2288
2289 if (space < 0) {
2290 space = 0;
2291 }
2292
2293 /* Compensate for data being processed by content filters */
2294 #if CONTENT_FILTER
2295 pending = cfil_sock_data_space(sb);
2296 #endif /* CONTENT_FILTER */
2297 if (pending > space) {
2298 space = 0;
2299 } else {
2300 space -= pending;
2301 }
2302
2303 return space;
2304 }
2305
2306 /* do we have to send all at once on a socket? */
2307 int
sosendallatonce(struct socket * so)2308 sosendallatonce(struct socket *so)
2309 {
2310 return so->so_proto->pr_flags & PR_ATOMIC;
2311 }
2312
2313 /* can we read something from so? */
2314 int
soreadable(struct socket * so)2315 soreadable(struct socket *so)
2316 {
2317 return so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
2318 ((so->so_state & SS_CANTRCVMORE)
2319 #if CONTENT_FILTER
2320 && cfil_sock_data_pending(&so->so_rcv) == 0
2321 #endif /* CONTENT_FILTER */
2322 ) ||
2323 so->so_comp.tqh_first || so->so_error;
2324 }
2325
2326 /* can we write something to so? */
2327
2328 int
sowriteable(struct socket * so)2329 sowriteable(struct socket *so)
2330 {
2331 if ((so->so_state & SS_CANTSENDMORE) ||
2332 so->so_error > 0) {
2333 return 1;
2334 }
2335 if (so_wait_for_if_feedback(so) || !socanwrite(so)) {
2336 return 0;
2337 }
2338 if (so->so_flags1 & SOF1_PRECONNECT_DATA) {
2339 return 1;
2340 }
2341
2342 int64_t data = sbspace(&so->so_snd);
2343 int64_t lowat = so->so_snd.sb_lowat;
2344 /*
2345 * Deal with connected UNIX domain sockets which
2346 * rely on the fact that the sender's socket buffer is
2347 * actually the receiver's socket buffer.
2348 */
2349 if (SOCK_DOM(so) == PF_LOCAL) {
2350 struct unpcb *unp = sotounpcb(so);
2351 if (unp != NULL && unp->unp_conn != NULL &&
2352 unp->unp_conn->unp_socket != NULL) {
2353 struct socket *so2 = unp->unp_conn->unp_socket;
2354 /*
2355 * At this point we know that `so' is locked
2356 * and that `unp_conn` isn't going to change.
2357 * However, we don't lock `so2` because doing so
2358 * may require unlocking `so'
2359 * (see unp_get_locks_in_order()).
2360 *
2361 * Two cases can happen:
2362 *
2363 * 1) we return 1 and tell the application that
2364 * it can write. Meanwhile, another thread
2365 * fills up the socket buffer. This will either
2366 * lead to a blocking send or EWOULDBLOCK
2367 * which the application should deal with.
2368 * 2) we return 0 and tell the application that
2369 * the socket is not writable. Meanwhile,
2370 * another thread depletes the receive socket
2371 * buffer. In this case the application will
2372 * be woken up by sb_notify().
2373 *
2374 * MIN() is required because otherwise sosendcheck()
2375 * may return EWOULDBLOCK since it only considers
2376 * so->so_snd.
2377 */
2378 data = MIN(data, sbspace(&so2->so_rcv));
2379 }
2380 }
2381
2382 if (data >= lowat) {
2383 if (so->so_flags & SOF_NOTSENT_LOWAT) {
2384 if ((SOCK_DOM(so) == PF_INET6 ||
2385 SOCK_DOM(so) == PF_INET) &&
2386 so->so_type == SOCK_STREAM) {
2387 return tcp_notsent_lowat_check(so);
2388 }
2389 #if MPTCP
2390 else if ((SOCK_DOM(so) == PF_MULTIPATH) &&
2391 (SOCK_PROTO(so) == IPPROTO_TCP)) {
2392 return mptcp_notsent_lowat_check(so);
2393 }
2394 #endif
2395 else {
2396 return 1;
2397 }
2398 } else {
2399 return 1;
2400 }
2401 }
2402 return 0;
2403 }
2404
2405 /* adjust counters in sb reflecting allocation of m */
2406
2407 void
sballoc(struct sockbuf * sb,struct mbuf * m)2408 sballoc(struct sockbuf *sb, struct mbuf *m)
2409 {
2410 u_int32_t cnt = 1;
2411 sb->sb_cc += m->m_len;
2412 if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
2413 m->m_type != MT_OOBDATA) {
2414 sb->sb_ctl += m->m_len;
2415 }
2416 sb->sb_mbcnt += _MSIZE;
2417
2418 if (m->m_flags & M_EXT) {
2419 sb->sb_mbcnt += m->m_ext.ext_size;
2420 cnt += (m->m_ext.ext_size + _MSIZE - 1) / _MSIZE;
2421 }
2422 OSAddAtomic(cnt, &total_sbmb_cnt);
2423 VERIFY(total_sbmb_cnt > 0);
2424 if (total_sbmb_cnt > total_sbmb_cnt_peak) {
2425 total_sbmb_cnt_peak = total_sbmb_cnt;
2426 }
2427
2428 /*
2429 * If data is being added to the send socket buffer,
2430 * update the send byte count
2431 */
2432 if (sb->sb_flags & SB_SNDBYTE_CNT) {
2433 inp_incr_sndbytes_total(sb->sb_so, m->m_len);
2434 inp_incr_sndbytes_unsent(sb->sb_so, m->m_len);
2435 }
2436 }
2437
2438 /* adjust counters in sb reflecting freeing of m */
2439 void
sbfree(struct sockbuf * sb,struct mbuf * m)2440 sbfree(struct sockbuf *sb, struct mbuf *m)
2441 {
2442 int cnt = -1;
2443
2444 sb->sb_cc -= m->m_len;
2445 if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
2446 m->m_type != MT_OOBDATA) {
2447 sb->sb_ctl -= m->m_len;
2448 }
2449 sb->sb_mbcnt -= _MSIZE;
2450 if (m->m_flags & M_EXT) {
2451 sb->sb_mbcnt -= m->m_ext.ext_size;
2452 cnt -= (m->m_ext.ext_size + _MSIZE - 1) / _MSIZE;
2453 }
2454 OSAddAtomic(cnt, &total_sbmb_cnt);
2455 VERIFY(total_sbmb_cnt >= 0);
2456 if (total_sbmb_cnt < total_sbmb_cnt_floor) {
2457 total_sbmb_cnt_floor = total_sbmb_cnt;
2458 }
2459
2460 /*
2461 * If data is being removed from the send socket buffer,
2462 * update the send byte count
2463 */
2464 if (sb->sb_flags & SB_SNDBYTE_CNT) {
2465 inp_decr_sndbytes_total(sb->sb_so, m->m_len);
2466 }
2467
2468 if (sb->sb_flags & SB_SENDHEAD) {
2469 if (m == sb->sb_sendhead) {
2470 sb->sb_sendhead = NULL;
2471 }
2472 }
2473 }
2474
2475 /*
2476 * Set lock on sockbuf sb; sleep if lock is already held.
2477 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
2478 * Returns error without lock if sleep is interrupted.
2479 */
2480 int
sblock(struct sockbuf * sb,uint32_t flags)2481 sblock(struct sockbuf *sb, uint32_t flags)
2482 {
2483 boolean_t nointr = ((sb->sb_flags & SB_NOINTR) || (flags & SBL_NOINTR));
2484 void *lr_saved = __builtin_return_address(0);
2485 struct socket *so = sb->sb_so;
2486 void * wchan;
2487 int error = 0;
2488 thread_t tp = current_thread();
2489
2490 VERIFY((flags & SBL_VALID) == flags);
2491
2492 /* so_usecount may be 0 if we get here from sofreelastref() */
2493 if (so == NULL) {
2494 panic("%s: null so, sb=%p sb_flags=0x%x lr=%p",
2495 __func__, sb, sb->sb_flags, lr_saved);
2496 /* NOTREACHED */
2497 } else if (so->so_usecount < 0) {
2498 panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
2499 "lrh= %s\n", __func__, sb, sb->sb_flags, so,
2500 so->so_usecount, lr_saved, solockhistory_nr(so));
2501 /* NOTREACHED */
2502 }
2503
2504 /*
2505 * The content filter thread must hold the sockbuf lock
2506 */
2507 if ((so->so_flags & SOF_CONTENT_FILTER) && sb->sb_cfil_thread == tp) {
2508 /*
2509 * Don't panic if we are defunct because SB_LOCK has
2510 * been cleared by sodefunct()
2511 */
2512 if (!(so->so_flags & SOF_DEFUNCT) && !(sb->sb_flags & SB_LOCK)) {
2513 panic("%s: SB_LOCK not held for %p",
2514 __func__, sb);
2515 }
2516
2517 /* Keep the sockbuf locked */
2518 return 0;
2519 }
2520
2521 if ((sb->sb_flags & SB_LOCK) && !(flags & SBL_WAIT)) {
2522 return EWOULDBLOCK;
2523 }
2524 /*
2525 * We may get here from sorflush(), in which case "sb" may not
2526 * point to the real socket buffer. Use the actual socket buffer
2527 * address from the socket instead.
2528 */
2529 wchan = (sb->sb_flags & SB_RECV) ?
2530 &so->so_rcv.sb_flags : &so->so_snd.sb_flags;
2531
2532 /*
2533 * A content filter thread has exclusive access to the sockbuf
2534 * until it clears the
2535 */
2536 while ((sb->sb_flags & SB_LOCK) ||
2537 ((so->so_flags & SOF_CONTENT_FILTER) &&
2538 sb->sb_cfil_thread != NULL)) {
2539 lck_mtx_t *mutex_held;
2540
2541 /*
2542 * XXX: This code should be moved up above outside of this loop;
2543 * however, we may get here as part of sofreelastref(), and
2544 * at that time pr_getlock() may no longer be able to return
2545 * us the lock. This will be fixed in future.
2546 */
2547 if (so->so_proto->pr_getlock != NULL) {
2548 mutex_held = (*so->so_proto->pr_getlock)(so, PR_F_WILLUNLOCK);
2549 } else {
2550 mutex_held = so->so_proto->pr_domain->dom_mtx;
2551 }
2552
2553 LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
2554
2555 sb->sb_wantlock++;
2556 VERIFY(sb->sb_wantlock != 0);
2557
2558 error = msleep(wchan, mutex_held,
2559 nointr ? PSOCK : PSOCK | PCATCH,
2560 nointr ? "sb_lock_nointr" : "sb_lock", NULL);
2561
2562 VERIFY(sb->sb_wantlock != 0);
2563 sb->sb_wantlock--;
2564
2565 if (error == 0 && (so->so_flags & SOF_DEFUNCT) &&
2566 !(flags & SBL_IGNDEFUNCT)) {
2567 error = EBADF;
2568 SODEFUNCTLOG("%s[%d, %s]: defunct so 0x%llu [%d,%d] "
2569 "(%d)\n", __func__, proc_selfpid(),
2570 proc_best_name(current_proc()),
2571 so->so_gencnt,
2572 SOCK_DOM(so), SOCK_TYPE(so), error);
2573 }
2574
2575 if (error != 0) {
2576 return error;
2577 }
2578 }
2579 sb->sb_flags |= SB_LOCK;
2580 return 0;
2581 }
2582
2583 /*
2584 * Release lock on sockbuf sb
2585 */
2586 void
sbunlock(struct sockbuf * sb,boolean_t keeplocked)2587 sbunlock(struct sockbuf *sb, boolean_t keeplocked)
2588 {
2589 void *lr_saved = __builtin_return_address(0);
2590 struct socket *so = sb->sb_so;
2591 thread_t tp = current_thread();
2592
2593 /* so_usecount may be 0 if we get here from sofreelastref() */
2594 if (so == NULL) {
2595 panic("%s: null so, sb=%p sb_flags=0x%x lr=%p",
2596 __func__, sb, sb->sb_flags, lr_saved);
2597 /* NOTREACHED */
2598 } else if (so->so_usecount < 0) {
2599 panic("%s: sb=%p sb_flags=0x%x sb_so=%p usecount=%d lr=%p "
2600 "lrh= %s\n", __func__, sb, sb->sb_flags, so,
2601 so->so_usecount, lr_saved, solockhistory_nr(so));
2602 /* NOTREACHED */
2603 }
2604
2605 /*
2606 * The content filter thread must hold the sockbuf lock
2607 */
2608 if ((so->so_flags & SOF_CONTENT_FILTER) && sb->sb_cfil_thread == tp) {
2609 /*
2610 * Don't panic if we are defunct because SB_LOCK has
2611 * been cleared by sodefunct()
2612 */
2613 if (!(so->so_flags & SOF_DEFUNCT) &&
2614 !(sb->sb_flags & SB_LOCK) &&
2615 !(so->so_state & SS_DEFUNCT) &&
2616 !(so->so_flags1 & SOF1_DEFUNCTINPROG)) {
2617 panic("%s: SB_LOCK not held for %p",
2618 __func__, sb);
2619 }
2620 /* Keep the sockbuf locked and proceed */
2621 } else {
2622 VERIFY((sb->sb_flags & SB_LOCK) ||
2623 (so->so_state & SS_DEFUNCT) ||
2624 (so->so_flags1 & SOF1_DEFUNCTINPROG));
2625
2626 sb->sb_flags &= ~SB_LOCK;
2627
2628 if (sb->sb_wantlock > 0) {
2629 /*
2630 * We may get here from sorflush(), in which case "sb"
2631 * may not point to the real socket buffer. Use the
2632 * actual socket buffer address from the socket instead.
2633 */
2634 wakeup((sb->sb_flags & SB_RECV) ? &so->so_rcv.sb_flags :
2635 &so->so_snd.sb_flags);
2636 }
2637 }
2638
2639 if (!keeplocked) { /* unlock on exit */
2640 if (so->so_flags & SOF_MP_SUBFLOW || SOCK_DOM(so) == PF_MULTIPATH) {
2641 (*so->so_proto->pr_unlock)(so, 1, lr_saved);
2642 } else {
2643 lck_mtx_t *mutex_held;
2644
2645 if (so->so_proto->pr_getlock != NULL) {
2646 mutex_held = (*so->so_proto->pr_getlock)(so, PR_F_WILLUNLOCK);
2647 } else {
2648 mutex_held = so->so_proto->pr_domain->dom_mtx;
2649 }
2650
2651 LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
2652
2653 VERIFY(so->so_usecount > 0);
2654 so->so_usecount--;
2655 so->unlock_lr[so->next_unlock_lr] = lr_saved;
2656 so->next_unlock_lr = (so->next_unlock_lr + 1) % SO_LCKDBG_MAX;
2657 lck_mtx_unlock(mutex_held);
2658 }
2659 }
2660 }
2661
2662 void
sorwakeup(struct socket * so)2663 sorwakeup(struct socket *so)
2664 {
2665 if (sb_notify(&so->so_rcv)) {
2666 sowakeup(so, &so->so_rcv, NULL);
2667 }
2668 }
2669
2670 void
sowwakeup(struct socket * so)2671 sowwakeup(struct socket *so)
2672 {
2673 if (sb_notify(&so->so_snd)) {
2674 sowakeup(so, &so->so_snd, NULL);
2675 }
2676 }
2677
2678 static void
soevupcall(struct socket * so,uint32_t hint)2679 soevupcall(struct socket *so, uint32_t hint)
2680 {
2681 if (so->so_event != NULL) {
2682 caddr_t so_eventarg = so->so_eventarg;
2683
2684 hint &= so->so_eventmask;
2685 if (hint != 0) {
2686 so->so_event(so, so_eventarg, hint);
2687 }
2688 }
2689 }
2690
2691 void
soevent(struct socket * so,uint32_t hint)2692 soevent(struct socket *so, uint32_t hint)
2693 {
2694 if (net_wake_pkt_debug > 0 && (hint & SO_FILT_HINT_WAKE_PKT)) {
2695 os_log(OS_LOG_DEFAULT, "%s: SO_FILT_HINT_WAKE_PKT so %p",
2696 __func__, so);
2697 }
2698
2699 if (so->so_flags & SOF_KNOTE) {
2700 KNOTE(&so->so_klist, hint);
2701 }
2702
2703 soevupcall(so, hint);
2704
2705 /*
2706 * Don't post an event if this a subflow socket or
2707 * the app has opted out of using cellular interface
2708 */
2709 if ((hint & SO_FILT_HINT_IFDENIED) &&
2710 !(so->so_flags & SOF_MP_SUBFLOW) &&
2711 !(so->so_restrictions & SO_RESTRICT_DENY_CELLULAR) &&
2712 !(so->so_restrictions & SO_RESTRICT_DENY_EXPENSIVE) &&
2713 !(so->so_restrictions & SO_RESTRICT_DENY_CONSTRAINED)) {
2714 soevent_ifdenied(so);
2715 }
2716 }
2717
2718 static void
soevent_ifdenied(struct socket * so)2719 soevent_ifdenied(struct socket *so)
2720 {
2721 struct kev_netpolicy_ifdenied ev_ifdenied;
2722
2723 bzero(&ev_ifdenied, sizeof(ev_ifdenied));
2724 /*
2725 * The event consumer is interested about the effective {upid,pid,uuid}
2726 * info which can be different than the those related to the process
2727 * that recently performed a system call on the socket, i.e. when the
2728 * socket is delegated.
2729 */
2730 if (so->so_flags & SOF_DELEGATED) {
2731 ev_ifdenied.ev_data.eupid = so->e_upid;
2732 ev_ifdenied.ev_data.epid = so->e_pid;
2733 uuid_copy(ev_ifdenied.ev_data.euuid, so->e_uuid);
2734 } else {
2735 ev_ifdenied.ev_data.eupid = so->last_upid;
2736 ev_ifdenied.ev_data.epid = so->last_pid;
2737 uuid_copy(ev_ifdenied.ev_data.euuid, so->last_uuid);
2738 }
2739
2740 if (++so->so_ifdenied_notifies > 1) {
2741 /*
2742 * Allow for at most one kernel event to be generated per
2743 * socket; so_ifdenied_notifies is reset upon changes in
2744 * the UUID policy. See comments in inp_update_policy.
2745 */
2746 if (net_io_policy_log) {
2747 uuid_string_t buf;
2748
2749 uuid_unparse(ev_ifdenied.ev_data.euuid, buf);
2750 log(LOG_DEBUG, "%s[%d]: so 0x%llx [%d,%d] epid %llu "
2751 "euuid %s%s has %d redundant events supressed\n",
2752 __func__, so->last_pid,
2753 (uint64_t)VM_KERNEL_ADDRPERM(so), SOCK_DOM(so),
2754 SOCK_TYPE(so), ev_ifdenied.ev_data.epid, buf,
2755 ((so->so_flags & SOF_DELEGATED) ?
2756 " [delegated]" : ""), so->so_ifdenied_notifies);
2757 }
2758 } else {
2759 if (net_io_policy_log) {
2760 uuid_string_t buf;
2761
2762 uuid_unparse(ev_ifdenied.ev_data.euuid, buf);
2763 log(LOG_DEBUG, "%s[%d]: so 0x%llx [%d,%d] epid %llu "
2764 "euuid %s%s event posted\n", __func__,
2765 so->last_pid, (uint64_t)VM_KERNEL_ADDRPERM(so),
2766 SOCK_DOM(so), SOCK_TYPE(so),
2767 ev_ifdenied.ev_data.epid, buf,
2768 ((so->so_flags & SOF_DELEGATED) ?
2769 " [delegated]" : ""));
2770 }
2771 netpolicy_post_msg(KEV_NETPOLICY_IFDENIED, &ev_ifdenied.ev_data,
2772 sizeof(ev_ifdenied));
2773 }
2774 }
2775
2776 /*
2777 * Make a copy of a sockaddr in a malloced buffer of type SONAME.
2778 */
2779 struct sockaddr *
dup_sockaddr(struct sockaddr * sa,int canwait)2780 dup_sockaddr(struct sockaddr *sa, int canwait)
2781 {
2782 struct sockaddr *sa2;
2783
2784 sa2 = (struct sockaddr *)alloc_sockaddr(sa->sa_len, canwait ? Z_WAITOK : Z_NOWAIT);
2785 if (sa2 != NULL) {
2786 bcopy(sa, sa2, sa->sa_len);
2787 }
2788 return sa2;
2789 }
2790
2791 void *
alloc_sockaddr(size_t size,zalloc_flags_t flags)2792 alloc_sockaddr(size_t size, zalloc_flags_t flags)
2793 {
2794 VERIFY((size) <= UINT8_MAX);
2795
2796 __typed_allocators_ignore_push
2797 struct sockaddr *sa = kheap_alloc(KHEAP_SONAME, size, flags | Z_ZERO);
2798 __typed_allocators_ignore_pop
2799 if (sa != NULL) {
2800 sa->sa_len = (uint8_t)size;
2801 }
2802
2803 return sa;
2804 }
2805
2806 /*
2807 * Create an external-format (``xsocket'') structure using the information
2808 * in the kernel-format socket structure pointed to by so. This is done
2809 * to reduce the spew of irrelevant information over this interface,
2810 * to isolate user code from changes in the kernel structure, and
2811 * potentially to provide information-hiding if we decide that
2812 * some of this information should be hidden from users.
2813 */
2814 void
sotoxsocket(struct socket * so,struct xsocket * xso)2815 sotoxsocket(struct socket *so, struct xsocket *xso)
2816 {
2817 xso->xso_len = sizeof(*xso);
2818 xso->xso_so = (_XSOCKET_PTR(struct socket *))VM_KERNEL_ADDRPERM(so);
2819 xso->so_type = so->so_type;
2820 xso->so_options = (short)(so->so_options & 0xffff);
2821 xso->so_linger = so->so_linger;
2822 xso->so_state = so->so_state;
2823 xso->so_pcb = (_XSOCKET_PTR(caddr_t))VM_KERNEL_ADDRPERM(so->so_pcb);
2824 if (so->so_proto) {
2825 xso->xso_protocol = SOCK_PROTO(so);
2826 xso->xso_family = SOCK_DOM(so);
2827 } else {
2828 xso->xso_protocol = xso->xso_family = 0;
2829 }
2830 xso->so_qlen = so->so_qlen;
2831 xso->so_incqlen = so->so_incqlen;
2832 xso->so_qlimit = so->so_qlimit;
2833 xso->so_timeo = so->so_timeo;
2834 xso->so_error = so->so_error;
2835 xso->so_pgid = so->so_pgid;
2836 xso->so_oobmark = so->so_oobmark;
2837 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
2838 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
2839 xso->so_uid = kauth_cred_getuid(so->so_cred);
2840 }
2841
2842
2843 #if XNU_TARGET_OS_OSX
2844
2845 void
sotoxsocket64(struct socket * so,struct xsocket64 * xso)2846 sotoxsocket64(struct socket *so, struct xsocket64 *xso)
2847 {
2848 xso->xso_len = sizeof(*xso);
2849 xso->xso_so = (u_int64_t)VM_KERNEL_ADDRPERM(so);
2850 xso->so_type = so->so_type;
2851 xso->so_options = (short)(so->so_options & 0xffff);
2852 xso->so_linger = so->so_linger;
2853 xso->so_state = so->so_state;
2854 xso->so_pcb = (u_int64_t)VM_KERNEL_ADDRPERM(so->so_pcb);
2855 if (so->so_proto) {
2856 xso->xso_protocol = SOCK_PROTO(so);
2857 xso->xso_family = SOCK_DOM(so);
2858 } else {
2859 xso->xso_protocol = xso->xso_family = 0;
2860 }
2861 xso->so_qlen = so->so_qlen;
2862 xso->so_incqlen = so->so_incqlen;
2863 xso->so_qlimit = so->so_qlimit;
2864 xso->so_timeo = so->so_timeo;
2865 xso->so_error = so->so_error;
2866 xso->so_pgid = so->so_pgid;
2867 xso->so_oobmark = so->so_oobmark;
2868 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
2869 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
2870 xso->so_uid = kauth_cred_getuid(so->so_cred);
2871 }
2872
2873 #endif /* XNU_TARGET_OS_OSX */
2874
2875 /*
2876 * This does the same for sockbufs. Note that the xsockbuf structure,
2877 * since it is always embedded in a socket, does not include a self
2878 * pointer nor a length. We make this entry point public in case
2879 * some other mechanism needs it.
2880 */
2881 void
sbtoxsockbuf(struct sockbuf * sb,struct xsockbuf * xsb)2882 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
2883 {
2884 xsb->sb_cc = sb->sb_cc;
2885 xsb->sb_hiwat = sb->sb_hiwat;
2886 xsb->sb_mbcnt = sb->sb_mbcnt;
2887 xsb->sb_mbmax = sb->sb_mbmax;
2888 xsb->sb_lowat = sb->sb_lowat;
2889 xsb->sb_flags = (short)sb->sb_flags;
2890 xsb->sb_timeo = (short)
2891 ((sb->sb_timeo.tv_sec * hz) + sb->sb_timeo.tv_usec / tick);
2892 if (xsb->sb_timeo == 0 && sb->sb_timeo.tv_usec != 0) {
2893 xsb->sb_timeo = 1;
2894 }
2895 }
2896
2897 /*
2898 * Based on the policy set by an all knowing decison maker, throttle sockets
2899 * that either have been marked as belonging to "background" process.
2900 */
2901 inline int
soisthrottled(struct socket * so)2902 soisthrottled(struct socket *so)
2903 {
2904 return so->so_flags1 & SOF1_TRAFFIC_MGT_SO_BACKGROUND;
2905 }
2906
2907 inline int
soisprivilegedtraffic(struct socket * so)2908 soisprivilegedtraffic(struct socket *so)
2909 {
2910 return (so->so_flags & SOF_PRIVILEGED_TRAFFIC_CLASS) ? 1 : 0;
2911 }
2912
2913 inline int
soissrcbackground(struct socket * so)2914 soissrcbackground(struct socket *so)
2915 {
2916 return (so->so_flags1 & SOF1_TRAFFIC_MGT_SO_BACKGROUND) ||
2917 IS_SO_TC_BACKGROUND(so->so_traffic_class);
2918 }
2919
2920 inline int
soissrcrealtime(struct socket * so)2921 soissrcrealtime(struct socket *so)
2922 {
2923 return so->so_traffic_class >= SO_TC_AV &&
2924 so->so_traffic_class <= SO_TC_VO;
2925 }
2926
2927 inline int
soissrcbesteffort(struct socket * so)2928 soissrcbesteffort(struct socket *so)
2929 {
2930 return so->so_traffic_class == SO_TC_BE ||
2931 so->so_traffic_class == SO_TC_RD ||
2932 so->so_traffic_class == SO_TC_OAM;
2933 }
2934
2935 void
soclearfastopen(struct socket * so)2936 soclearfastopen(struct socket *so)
2937 {
2938 if (so->so_flags1 & SOF1_PRECONNECT_DATA) {
2939 so->so_flags1 &= ~SOF1_PRECONNECT_DATA;
2940 }
2941
2942 if (so->so_flags1 & SOF1_DATA_IDEMPOTENT) {
2943 so->so_flags1 &= ~SOF1_DATA_IDEMPOTENT;
2944 }
2945 }
2946
2947 void
sonullevent(struct socket * so,void * arg,uint32_t hint)2948 sonullevent(struct socket *so, void *arg, uint32_t hint)
2949 {
2950 #pragma unused(so, arg, hint)
2951 }
2952
2953 /*
2954 * Here is the definition of some of the basic objects in the kern.ipc
2955 * branch of the MIB.
2956 */
2957 SYSCTL_NODE(_kern, KERN_IPC, ipc,
2958 CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY, 0, "IPC");
2959
2960 /* Check that the maximum socket buffer size is within a range */
2961
2962 static int
2963 sysctl_sb_max SYSCTL_HANDLER_ARGS
2964 {
2965 #pragma unused(oidp, arg1, arg2)
2966 u_int32_t new_value;
2967 int changed = 0;
2968 int error = sysctl_io_number(req, sb_max, sizeof(u_int32_t),
2969 &new_value, &changed);
2970 if (!error && changed) {
2971 if (new_value > LOW_SB_MAX && new_value <= high_sb_max &&
2972 SB_MAX_ADJUST(new_value) < UINT32_MAX) {
2973 sb_max = new_value;
2974 sb_max_adj = SB_MAX_ADJUST(sb_max);
2975 } else {
2976 error = ERANGE;
2977 }
2978 }
2979 return error;
2980 }
2981
2982 SYSCTL_PROC(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf,
2983 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
2984 &sb_max, 0, &sysctl_sb_max, "IU", "Maximum socket buffer size");
2985
2986 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor,
2987 CTLFLAG_RW | CTLFLAG_LOCKED, &sb_efficiency, 0, "");
2988
2989 SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters,
2990 CTLFLAG_RD | CTLFLAG_LOCKED, &nmbclusters, 0, "");
2991
2992 SYSCTL_INT(_kern_ipc, OID_AUTO, njcl,
2993 CTLFLAG_RD | CTLFLAG_LOCKED, &njcl, 0, "");
2994
2995 SYSCTL_INT(_kern_ipc, OID_AUTO, njclbytes,
2996 CTLFLAG_RD | CTLFLAG_LOCKED, &njclbytes, 0, "");
2997
2998 SYSCTL_INT(_kern_ipc, KIPC_SOQLIMITCOMPAT, soqlimitcompat,
2999 CTLFLAG_RW | CTLFLAG_LOCKED, &soqlimitcompat, 1,
3000 "Enable socket queue limit compatibility");
3001
3002 /*
3003 * Hack alert -- rdar://33572856
3004 * A loopback test we cannot change was failing because it sets
3005 * SO_SENDTIMEO to 5 seconds and that's also the value
3006 * of the minimum persist timer. Because of the persist timer,
3007 * the connection was not idle for 5 seconds and SO_SNDTIMEO
3008 * was not triggering at 5 seconds causing the test failure.
3009 * As a workaround we check the sysctl soqlencomp the test is already
3010 * setting to set disable auto tuning of the receive buffer.
3011 */
3012
3013 extern u_int32_t tcp_do_autorcvbuf;
3014
3015 static int
3016 sysctl_soqlencomp SYSCTL_HANDLER_ARGS
3017 {
3018 #pragma unused(oidp, arg1, arg2)
3019 u_int32_t new_value;
3020 int changed = 0;
3021 int error = sysctl_io_number(req, soqlencomp, sizeof(u_int32_t),
3022 &new_value, &changed);
3023 if (!error && changed) {
3024 soqlencomp = new_value;
3025 if (new_value != 0) {
3026 tcp_do_autorcvbuf = 0;
3027 tcptv_persmin_val = 6 * TCP_RETRANSHZ;
3028 }
3029 }
3030 return error;
3031 }
3032 SYSCTL_PROC(_kern_ipc, OID_AUTO, soqlencomp,
3033 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
3034 &soqlencomp, 0, &sysctl_soqlencomp, "IU", "");
3035
3036 SYSCTL_INT(_kern_ipc, OID_AUTO, sbmb_cnt, CTLFLAG_RD | CTLFLAG_LOCKED,
3037 &total_sbmb_cnt, 0, "");
3038 SYSCTL_INT(_kern_ipc, OID_AUTO, sbmb_cnt_peak, CTLFLAG_RD | CTLFLAG_LOCKED,
3039 &total_sbmb_cnt_peak, 0, "");
3040 SYSCTL_INT(_kern_ipc, OID_AUTO, sbmb_cnt_floor, CTLFLAG_RD | CTLFLAG_LOCKED,
3041 &total_sbmb_cnt_floor, 0, "");
3042 SYSCTL_QUAD(_kern_ipc, OID_AUTO, sbmb_limreached, CTLFLAG_RD | CTLFLAG_LOCKED,
3043 &sbmb_limreached, "");
3044
3045
3046 SYSCTL_NODE(_kern_ipc, OID_AUTO, io_policy, CTLFLAG_RW, 0, "network IO policy");
3047
3048 SYSCTL_INT(_kern_ipc_io_policy, OID_AUTO, log, CTLFLAG_RW | CTLFLAG_LOCKED,
3049 &net_io_policy_log, 0, "");
3050
3051 #if CONFIG_PROC_UUID_POLICY
3052 SYSCTL_INT(_kern_ipc_io_policy, OID_AUTO, uuid, CTLFLAG_RW | CTLFLAG_LOCKED,
3053 &net_io_policy_uuid, 0, "");
3054 #endif /* CONFIG_PROC_UUID_POLICY */
3055