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