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