]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
316670eb | 2 | * Copyright (c) 1998-2012 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
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. | |
8f6c56a5 | 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 | |
9bccf70c | 62 | * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $ |
1c79356b | 63 | */ |
2d21ac55 A |
64 | /* |
65 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
66 | * support for mandatory and extensible security protections. This notice | |
67 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
68 | * Version 2.0. | |
69 | */ | |
1c79356b A |
70 | |
71 | #include <sys/param.h> | |
72 | #include <sys/systm.h> | |
73 | #include <sys/domain.h> | |
74 | #include <sys/kernel.h> | |
91447636 A |
75 | #include <sys/proc_internal.h> |
76 | #include <sys/kauth.h> | |
1c79356b A |
77 | #include <sys/malloc.h> |
78 | #include <sys/mbuf.h> | |
316670eb | 79 | #include <sys/mcache.h> |
1c79356b A |
80 | #include <sys/protosw.h> |
81 | #include <sys/stat.h> | |
82 | #include <sys/socket.h> | |
83 | #include <sys/socketvar.h> | |
84 | #include <sys/signalvar.h> | |
85 | #include <sys/sysctl.h> | |
86 | #include <sys/ev.h> | |
91447636 A |
87 | #include <kern/locks.h> |
88 | #include <net/route.h> | |
89 | #include <netinet/in.h> | |
90 | #include <netinet/in_pcb.h> | |
fa4905b1 | 91 | #include <sys/kdebug.h> |
2d21ac55 A |
92 | #include <libkern/OSAtomic.h> |
93 | ||
94 | #if CONFIG_MACF | |
95 | #include <security/mac_framework.h> | |
96 | #endif | |
97 | ||
316670eb A |
98 | #include <mach/vm_param.h> |
99 | ||
2d21ac55 A |
100 | /* TODO: this should be in a header file somewhere */ |
101 | extern void postevent(struct socket *, struct sockbuf *, int); | |
fa4905b1 A |
102 | |
103 | #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4) | |
104 | #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5) | |
105 | ||
2d21ac55 A |
106 | static inline void sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *); |
107 | static struct socket *sonewconn_internal(struct socket *, int); | |
108 | static int sbappendaddr_internal(struct sockbuf *, struct sockaddr *, | |
109 | struct mbuf *, struct mbuf *); | |
110 | static int sbappendcontrol_internal(struct sockbuf *, struct mbuf *, | |
111 | struct mbuf *); | |
fa4905b1 | 112 | |
1c79356b A |
113 | /* |
114 | * Primitive routines for operating on sockets and socket buffers | |
115 | */ | |
2d21ac55 A |
116 | static int soqlimitcompat = 1; |
117 | static int soqlencomp = 0; | |
1c79356b | 118 | |
b0d623f7 A |
119 | /* Based on the number of mbuf clusters configured, high_sb_max and sb_max can get |
120 | * scaled up or down to suit that memory configuration. high_sb_max is a higher | |
121 | * limit on sb_max that is checked when sb_max gets set through sysctl. | |
122 | */ | |
123 | ||
124 | u_int32_t sb_max = SB_MAX; /* XXX should be static */ | |
125 | u_int32_t high_sb_max = SB_MAX; | |
1c79356b | 126 | |
b0d623f7 | 127 | static u_int32_t sb_efficiency = 8; /* parameter for sbreserve() */ |
316670eb A |
128 | __private_extern__ int32_t total_sbmb_cnt = 0; |
129 | ||
130 | /* Control whether to throttle sockets eligible to be throttled */ | |
131 | __private_extern__ u_int32_t net_io_policy_throttled = 0; | |
132 | static int sysctl_io_policy_throttled SYSCTL_HANDLER_ARGS; | |
1c79356b | 133 | |
1c79356b A |
134 | /* |
135 | * Procedures to manipulate state flags of socket | |
136 | * and do appropriate wakeups. Normal sequence from the | |
137 | * active (originating) side is that soisconnecting() is | |
138 | * called during processing of connect() call, | |
139 | * resulting in an eventual call to soisconnected() if/when the | |
140 | * connection is established. When the connection is torn down | |
9bccf70c | 141 | * soisdisconnecting() is called during processing of disconnect() call, |
1c79356b A |
142 | * and soisdisconnected() is called when the connection to the peer |
143 | * is totally severed. The semantics of these routines are such that | |
144 | * connectionless protocols can call soisconnected() and soisdisconnected() | |
145 | * only, bypassing the in-progress calls when setting up a ``connection'' | |
146 | * takes no time. | |
147 | * | |
148 | * From the passive side, a socket is created with | |
e3027f41 A |
149 | * two queues of sockets: so_incomp for connections in progress |
150 | * and so_comp for connections already made and awaiting user acceptance. | |
9bccf70c | 151 | * As a protocol is preparing incoming connections, it creates a socket |
e3027f41 | 152 | * structure queued on so_incomp by calling sonewconn(). When the connection |
1c79356b | 153 | * is established, soisconnected() is called, and transfers the |
e3027f41 | 154 | * socket structure to so_comp, making it available to accept(). |
1c79356b | 155 | * |
9bccf70c | 156 | * If a socket is closed with sockets on either |
e3027f41 | 157 | * so_incomp or so_comp, these sockets are dropped. |
9bccf70c | 158 | * |
1c79356b A |
159 | * If higher level protocols are implemented in |
160 | * the kernel, the wakeups done here will sometimes | |
161 | * cause software-interrupt process scheduling. | |
162 | */ | |
1c79356b | 163 | void |
2d21ac55 | 164 | soisconnecting(struct socket *so) |
1c79356b A |
165 | { |
166 | ||
167 | so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); | |
168 | so->so_state |= SS_ISCONNECTING; | |
2d21ac55 | 169 | |
91447636 | 170 | sflt_notify(so, sock_evt_connecting, NULL); |
1c79356b A |
171 | } |
172 | ||
173 | void | |
2d21ac55 | 174 | soisconnected(struct socket *so) |
9bccf70c A |
175 | { |
176 | struct socket *head = so->so_head; | |
1c79356b A |
177 | |
178 | so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); | |
179 | so->so_state |= SS_ISCONNECTED; | |
2d21ac55 | 180 | |
91447636 | 181 | sflt_notify(so, sock_evt_connected, NULL); |
2d21ac55 | 182 | |
1c79356b | 183 | if (head && (so->so_state & SS_INCOMP)) { |
ff6e181a A |
184 | so->so_state &= ~SS_INCOMP; |
185 | so->so_state |= SS_COMP; | |
186 | if (head->so_proto->pr_getlock != NULL) { | |
187 | socket_unlock(so, 0); | |
91447636 | 188 | socket_lock(head, 1); |
ff6e181a | 189 | } |
91447636 | 190 | postevent(head, 0, EV_RCONN); |
1c79356b A |
191 | TAILQ_REMOVE(&head->so_incomp, so, so_list); |
192 | head->so_incqlen--; | |
1c79356b | 193 | TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); |
1c79356b | 194 | sorwakeup(head); |
91447636 | 195 | wakeup_one((caddr_t)&head->so_timeo); |
ff6e181a | 196 | if (head->so_proto->pr_getlock != NULL) { |
91447636 | 197 | socket_unlock(head, 1); |
ff6e181a A |
198 | socket_lock(so, 0); |
199 | } | |
1c79356b | 200 | } else { |
91447636 | 201 | postevent(so, 0, EV_WCONN); |
1c79356b A |
202 | wakeup((caddr_t)&so->so_timeo); |
203 | sorwakeup(so); | |
204 | sowwakeup(so); | |
316670eb | 205 | soevent(so, SO_FILT_HINT_LOCKED); |
1c79356b A |
206 | } |
207 | } | |
208 | ||
209 | void | |
2d21ac55 | 210 | soisdisconnecting(struct socket *so) |
9bccf70c | 211 | { |
1c79356b A |
212 | so->so_state &= ~SS_ISCONNECTING; |
213 | so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); | |
316670eb | 214 | soevent(so, SO_FILT_HINT_LOCKED); |
91447636 | 215 | sflt_notify(so, sock_evt_disconnecting, NULL); |
1c79356b A |
216 | wakeup((caddr_t)&so->so_timeo); |
217 | sowwakeup(so); | |
218 | sorwakeup(so); | |
219 | } | |
220 | ||
221 | void | |
2d21ac55 | 222 | soisdisconnected(struct socket *so) |
9bccf70c | 223 | { |
1c79356b | 224 | so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); |
9bccf70c | 225 | so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); |
316670eb | 226 | soevent(so, SO_FILT_HINT_LOCKED); |
91447636 | 227 | sflt_notify(so, sock_evt_disconnected, NULL); |
1c79356b A |
228 | wakeup((caddr_t)&so->so_timeo); |
229 | sowwakeup(so); | |
230 | sorwakeup(so); | |
231 | } | |
232 | ||
6d2010ae A |
233 | /* This function will issue a wakeup like soisdisconnected but it will not |
234 | * notify the socket filters. This will avoid unlocking the socket | |
235 | * in the midst of closing it. | |
236 | */ | |
237 | void | |
238 | sodisconnectwakeup(struct socket *so) | |
239 | { | |
240 | so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); | |
241 | so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); | |
316670eb | 242 | soevent(so, SO_FILT_HINT_LOCKED); |
6d2010ae A |
243 | wakeup((caddr_t)&so->so_timeo); |
244 | sowwakeup(so); | |
245 | sorwakeup(so); | |
246 | } | |
247 | ||
1c79356b A |
248 | /* |
249 | * When an attempt at a new connection is noted on a socket | |
250 | * which accepts connections, sonewconn is called. If the | |
251 | * connection is possible (subject to space constraints, etc.) | |
252 | * then we allocate a new structure, propoerly linked into the | |
253 | * data structure of the original socket, and return this. | |
254 | * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. | |
255 | */ | |
91447636 | 256 | static struct socket * |
2d21ac55 | 257 | sonewconn_internal(struct socket *head, int connstatus) |
9bccf70c | 258 | { |
2d21ac55 A |
259 | int so_qlen, error = 0; |
260 | struct socket *so; | |
91447636 A |
261 | lck_mtx_t *mutex_held; |
262 | ||
2d21ac55 | 263 | if (head->so_proto->pr_getlock != NULL) |
91447636 | 264 | mutex_held = (*head->so_proto->pr_getlock)(head, 0); |
2d21ac55 | 265 | else |
91447636 A |
266 | mutex_held = head->so_proto->pr_domain->dom_mtx; |
267 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
1c79356b | 268 | |
2d21ac55 A |
269 | if (!soqlencomp) { |
270 | /* | |
271 | * This is the default case; so_qlen represents the | |
272 | * sum of both incomplete and completed queues. | |
273 | */ | |
274 | so_qlen = head->so_qlen; | |
275 | } else { | |
276 | /* | |
277 | * When kern.ipc.soqlencomp is set to 1, so_qlen | |
278 | * represents only the completed queue. Since we | |
279 | * cannot let the incomplete queue goes unbounded | |
280 | * (in case of SYN flood), we cap the incomplete | |
281 | * queue length to at most somaxconn, and use that | |
282 | * as so_qlen so that we fail immediately below. | |
283 | */ | |
284 | so_qlen = head->so_qlen - head->so_incqlen; | |
285 | if (head->so_incqlen > somaxconn) | |
286 | so_qlen = somaxconn; | |
287 | } | |
288 | ||
289 | if (so_qlen >= | |
290 | (soqlimitcompat ? head->so_qlimit : (3 * head->so_qlimit / 2))) | |
1c79356b | 291 | return ((struct socket *)0); |
b0d623f7 | 292 | so = soalloc(1, head->so_proto->pr_domain->dom_family, |
2d21ac55 | 293 | head->so_type); |
1c79356b A |
294 | if (so == NULL) |
295 | return ((struct socket *)0); | |
9bccf70c A |
296 | /* check if head was closed during the soalloc */ |
297 | if (head->so_proto == NULL) { | |
2d21ac55 A |
298 | sodealloc(so); |
299 | return ((struct socket *)0); | |
1c79356b A |
300 | } |
301 | ||
1c79356b A |
302 | so->so_type = head->so_type; |
303 | so->so_options = head->so_options &~ SO_ACCEPTCONN; | |
304 | so->so_linger = head->so_linger; | |
305 | so->so_state = head->so_state | SS_NOFDREF; | |
306 | so->so_proto = head->so_proto; | |
307 | so->so_timeo = head->so_timeo; | |
308 | so->so_pgid = head->so_pgid; | |
316670eb A |
309 | kauth_cred_ref(head->so_cred); |
310 | so->so_cred = head->so_cred; | |
311 | so->last_pid = head->last_pid; | |
312 | so->last_upid = head->last_upid; | |
b0d623f7 A |
313 | /* inherit socket options stored in so_flags */ |
314 | so->so_flags = head->so_flags & (SOF_NOSIGPIPE | | |
315 | SOF_NOADDRAVAIL | | |
316 | SOF_REUSESHAREUID | | |
317 | SOF_NOTIFYCONFLICT | | |
318 | SOF_BINDRANDOMPORT | | |
6d2010ae | 319 | SOF_NPX_SETOPTSHUT | |
316670eb A |
320 | SOF_NODEFUNCT | |
321 | SOF_PRIVILEGED_TRAFFIC_CLASS| | |
322 | SOF_NOTSENT_LOWAT | | |
323 | SOF_USELRO); | |
91447636 | 324 | so->so_usecount = 1; |
0c530ab8 A |
325 | so->next_lock_lr = 0; |
326 | so->next_unlock_lr = 0; | |
1c79356b | 327 | |
13fec989 A |
328 | #ifdef __APPLE__ |
329 | so->so_rcv.sb_flags |= SB_RECV; /* XXX */ | |
330 | so->so_rcv.sb_so = so->so_snd.sb_so = so; | |
331 | TAILQ_INIT(&so->so_evlist); | |
332 | #endif | |
333 | ||
2d21ac55 A |
334 | #if CONFIG_MACF_SOCKET |
335 | mac_socket_label_associate_accept(head, so); | |
336 | #endif | |
337 | ||
d1ecb069 | 338 | /* inherit traffic management properties of listener */ |
6d2010ae | 339 | so->so_traffic_mgt_flags = head->so_traffic_mgt_flags & (TRAFFIC_MGT_SO_BACKGROUND); |
d1ecb069 | 340 | so->so_background_thread = head->so_background_thread; |
d41d1dae | 341 | so->so_traffic_class = head->so_traffic_class; |
d1ecb069 | 342 | |
91447636 | 343 | if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) { |
9bccf70c A |
344 | sodealloc(so); |
345 | return ((struct socket *)0); | |
346 | } | |
316670eb A |
347 | so->so_rcv.sb_flags |= (head->so_rcv.sb_flags & SB_USRSIZE); |
348 | so->so_snd.sb_flags |= (head->so_snd.sb_flags & SB_USRSIZE); | |
9bccf70c | 349 | |
91447636 | 350 | /* |
2d21ac55 A |
351 | * Must be done with head unlocked to avoid deadlock |
352 | * for protocol with per socket mutexes. | |
91447636 | 353 | */ |
37839358 A |
354 | if (head->so_proto->pr_unlock) |
355 | socket_unlock(head, 0); | |
2d21ac55 A |
356 | if (((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL) != 0) || |
357 | error) { | |
1c79356b | 358 | sodealloc(so); |
37839358 A |
359 | if (head->so_proto->pr_unlock) |
360 | socket_lock(head, 0); | |
1c79356b A |
361 | return ((struct socket *)0); |
362 | } | |
6d2010ae | 363 | if (head->so_proto->pr_unlock) { |
37839358 | 364 | socket_lock(head, 0); |
6d2010ae A |
365 | /* Radar 7385998 Recheck that the head is still accepting |
366 | * to avoid race condition when head is getting closed. | |
367 | */ | |
368 | if ((head->so_options & SO_ACCEPTCONN) == 0) { | |
369 | so->so_state &= ~SS_NOFDREF; | |
370 | soclose(so); | |
371 | return ((struct socket *)0); | |
372 | } | |
373 | } | |
374 | ||
9bccf70c | 375 | #ifdef __APPLE__ |
1c79356b | 376 | so->so_proto->pr_domain->dom_refs++; |
9bccf70c | 377 | #endif |
6d2010ae A |
378 | /* Insert in head appropriate lists */ |
379 | so->so_head = head; | |
380 | ||
381 | /* Since this socket is going to be inserted into the incomp | |
382 | * queue, it can be picked up by another thread in | |
383 | * tcp_dropdropablreq to get dropped before it is setup.. | |
384 | * To prevent this race, set in-progress flag which can be | |
385 | * cleared later | |
386 | */ | |
387 | so->so_flags |= SOF_INCOMP_INPROGRESS; | |
1c79356b A |
388 | |
389 | if (connstatus) { | |
390 | TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); | |
391 | so->so_state |= SS_COMP; | |
392 | } else { | |
393 | TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); | |
394 | so->so_state |= SS_INCOMP; | |
395 | head->so_incqlen++; | |
396 | } | |
397 | head->so_qlen++; | |
91447636 | 398 | |
13fec989 | 399 | #ifdef __APPLE__ |
0c530ab8 A |
400 | /* Attach socket filters for this protocol */ |
401 | sflt_initsock(so); | |
9bccf70c | 402 | #endif |
2d21ac55 | 403 | |
91447636 A |
404 | if (connstatus) { |
405 | so->so_state |= connstatus; | |
406 | sorwakeup(head); | |
407 | wakeup((caddr_t)&head->so_timeo); | |
408 | } | |
1c79356b A |
409 | return (so); |
410 | } | |
411 | ||
91447636 A |
412 | |
413 | struct socket * | |
2d21ac55 | 414 | sonewconn(struct socket *head, int connstatus, const struct sockaddr *from) |
91447636 | 415 | { |
6d2010ae | 416 | int error = sflt_connectin(head, from); |
91447636 | 417 | if (error) { |
2d21ac55 | 418 | return (NULL); |
91447636 | 419 | } |
2d21ac55 A |
420 | |
421 | return (sonewconn_internal(head, connstatus)); | |
91447636 A |
422 | } |
423 | ||
1c79356b A |
424 | /* |
425 | * Socantsendmore indicates that no more data will be sent on the | |
426 | * socket; it would normally be applied to a socket when the user | |
427 | * informs the system that no more data is to be sent, by the protocol | |
428 | * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data | |
429 | * will be received, and will normally be applied to the socket by a | |
430 | * protocol when it detects that the peer will send no more data. | |
431 | * Data queued for reading in the socket may yet be read. | |
432 | */ | |
433 | ||
434 | void | |
2d21ac55 | 435 | socantsendmore(struct socket *so) |
9bccf70c | 436 | { |
1c79356b | 437 | so->so_state |= SS_CANTSENDMORE; |
316670eb | 438 | soevent(so, SO_FILT_HINT_LOCKED); |
91447636 | 439 | sflt_notify(so, sock_evt_cantsendmore, NULL); |
1c79356b A |
440 | sowwakeup(so); |
441 | } | |
442 | ||
443 | void | |
2d21ac55 | 444 | socantrcvmore(struct socket *so) |
9bccf70c | 445 | { |
1c79356b | 446 | so->so_state |= SS_CANTRCVMORE; |
316670eb | 447 | soevent(so, SO_FILT_HINT_LOCKED); |
91447636 | 448 | sflt_notify(so, sock_evt_cantrecvmore, NULL); |
1c79356b A |
449 | sorwakeup(so); |
450 | } | |
451 | ||
452 | /* | |
453 | * Wait for data to arrive at/drain from a socket buffer. | |
2d21ac55 A |
454 | * |
455 | * Returns: 0 Success | |
456 | * EBADF | |
457 | * msleep:EINTR | |
1c79356b A |
458 | */ |
459 | int | |
2d21ac55 | 460 | sbwait(struct sockbuf *sb) |
1c79356b | 461 | { |
b0d623f7 A |
462 | int error = 0; |
463 | uintptr_t lr_saved; | |
91447636 A |
464 | struct socket *so = sb->sb_so; |
465 | lck_mtx_t *mutex_held; | |
466 | struct timespec ts; | |
467 | ||
b0d623f7 | 468 | lr_saved = (uintptr_t) __builtin_return_address(0); |
2d21ac55 A |
469 | |
470 | if (so->so_proto->pr_getlock != NULL) | |
91447636 | 471 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
2d21ac55 | 472 | else |
91447636 | 473 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
6d2010ae | 474 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); |
1c79356b A |
475 | |
476 | sb->sb_flags |= SB_WAIT; | |
91447636 A |
477 | |
478 | if (so->so_usecount < 1) | |
2d21ac55 | 479 | panic("sbwait: so=%p refcount=%d\n", so, so->so_usecount); |
91447636 A |
480 | ts.tv_sec = sb->sb_timeo.tv_sec; |
481 | ts.tv_nsec = sb->sb_timeo.tv_usec * 1000; | |
482 | error = msleep((caddr_t)&sb->sb_cc, mutex_held, | |
2d21ac55 | 483 | (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", &ts); |
91447636 A |
484 | |
485 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
486 | ||
487 | if (so->so_usecount < 1) | |
2d21ac55 | 488 | panic("sbwait: so=%p refcount=%d\n", so, so->so_usecount); |
91447636 | 489 | |
6d2010ae | 490 | if ((so->so_state & SS_DRAINING) || (so->so_flags & SOF_DEFUNCT)) { |
91447636 | 491 | error = EBADF; |
6d2010ae A |
492 | if (so->so_flags & SOF_DEFUNCT) { |
493 | SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] (%d)\n", | |
494 | __func__, proc_selfpid(), so, INP_SOCKAF(so), | |
495 | INP_SOCKTYPE(so), error)); | |
496 | } | |
91447636 A |
497 | } |
498 | ||
499 | return (error); | |
1c79356b A |
500 | } |
501 | ||
502 | /* | |
503 | * Lock a sockbuf already known to be locked; | |
504 | * return any error returned from sleep (EINTR). | |
2d21ac55 A |
505 | * |
506 | * Returns: 0 Success | |
507 | * EINTR | |
1c79356b A |
508 | */ |
509 | int | |
2d21ac55 | 510 | sb_lock(struct sockbuf *sb) |
1c79356b | 511 | { |
91447636 | 512 | struct socket *so = sb->sb_so; |
2d21ac55 | 513 | lck_mtx_t *mutex_held; |
0c530ab8 | 514 | int error = 0; |
2d21ac55 | 515 | |
91447636 | 516 | if (so == NULL) |
2d21ac55 | 517 | panic("sb_lock: null so back pointer sb=%p\n", sb); |
1c79356b A |
518 | |
519 | while (sb->sb_flags & SB_LOCK) { | |
520 | sb->sb_flags |= SB_WANT; | |
6d2010ae | 521 | |
2d21ac55 | 522 | if (so->so_proto->pr_getlock != NULL) |
91447636 A |
523 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
524 | else | |
525 | mutex_held = so->so_proto->pr_domain->dom_mtx; | |
6d2010ae A |
526 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); |
527 | ||
91447636 | 528 | if (so->so_usecount < 1) |
2d21ac55 A |
529 | panic("sb_lock: so=%p refcount=%d\n", so, |
530 | so->so_usecount); | |
0c530ab8 | 531 | |
91447636 | 532 | error = msleep((caddr_t)&sb->sb_flags, mutex_held, |
2d21ac55 A |
533 | (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, |
534 | "sb_lock", 0); | |
91447636 | 535 | if (so->so_usecount < 1) |
2d21ac55 A |
536 | panic("sb_lock: 2 so=%p refcount=%d\n", so, |
537 | so->so_usecount); | |
6d2010ae A |
538 | |
539 | if (error == 0 && (so->so_flags & SOF_DEFUNCT)) { | |
540 | error = EBADF; | |
541 | SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] (%d)\n", | |
542 | __func__, proc_selfpid(), so, INP_SOCKAF(so), | |
543 | INP_SOCKTYPE(so), error)); | |
544 | } | |
545 | ||
2d21ac55 | 546 | if (error) |
1c79356b A |
547 | return (error); |
548 | } | |
549 | sb->sb_flags |= SB_LOCK; | |
550 | return (0); | |
551 | } | |
552 | ||
6d2010ae A |
553 | void |
554 | sbwakeup(struct sockbuf *sb) | |
555 | { | |
556 | if (sb->sb_flags & SB_WAIT) { | |
557 | sb->sb_flags &= ~SB_WAIT; | |
558 | wakeup((caddr_t)&sb->sb_cc); | |
559 | } | |
560 | } | |
561 | ||
1c79356b A |
562 | /* |
563 | * Wakeup processes waiting on a socket buffer. | |
564 | * Do asynchronous notification via SIGIO | |
565 | * if the socket has the SS_ASYNC flag set. | |
566 | */ | |
567 | void | |
2d21ac55 | 568 | sowakeup(struct socket *so, struct sockbuf *sb) |
1c79356b | 569 | { |
6d2010ae A |
570 | if (so->so_flags & SOF_DEFUNCT) { |
571 | SODEFUNCTLOG(("%s[%d]: defunct so %p [%d,%d] si 0x%x, " | |
572 | "fl 0x%x [%s]\n", __func__, proc_selfpid(), so, | |
573 | INP_SOCKAF(so), INP_SOCKTYPE(so), | |
574 | (uint32_t)sb->sb_sel.si_flags, (uint16_t)sb->sb_flags, | |
575 | (sb->sb_flags & SB_RECV) ? "rcv" : "snd")); | |
576 | } | |
577 | ||
0b4e3aa0 | 578 | sb->sb_flags &= ~SB_SEL; |
1c79356b | 579 | selwakeup(&sb->sb_sel); |
6d2010ae | 580 | sbwakeup(sb); |
1c79356b A |
581 | if (so->so_state & SS_ASYNC) { |
582 | if (so->so_pgid < 0) | |
583 | gsignal(-so->so_pgid, SIGIO); | |
2d21ac55 A |
584 | else if (so->so_pgid > 0) |
585 | proc_signal(so->so_pgid, SIGIO); | |
1c79356b | 586 | } |
91447636 A |
587 | if (sb->sb_flags & SB_KNOTE) { |
588 | KNOTE(&sb->sb_sel.si_note, SO_FILT_HINT_LOCKED); | |
589 | } | |
590 | if (sb->sb_flags & SB_UPCALL) { | |
2d21ac55 A |
591 | void (*so_upcall)(struct socket *, caddr_t, int); |
592 | caddr_t so_upcallarg; | |
593 | ||
594 | so_upcall = so->so_upcall; | |
595 | so_upcallarg = so->so_upcallarg; | |
596 | /* Let close know that we're about to do an upcall */ | |
316670eb | 597 | so->so_upcallusecount++; |
2d21ac55 | 598 | |
91447636 | 599 | socket_unlock(so, 0); |
2d21ac55 | 600 | (*so_upcall)(so, so_upcallarg, M_DONTWAIT); |
91447636 | 601 | socket_lock(so, 0); |
2d21ac55 | 602 | |
316670eb | 603 | so->so_upcallusecount--; |
2d21ac55 | 604 | /* Tell close that it's safe to proceed */ |
316670eb | 605 | if (so->so_flags & SOF_CLOSEWAIT && so->so_upcallusecount == 0) |
2d21ac55 | 606 | wakeup((caddr_t)&so->so_upcall); |
91447636 | 607 | } |
1c79356b A |
608 | } |
609 | ||
610 | /* | |
611 | * Socket buffer (struct sockbuf) utility routines. | |
612 | * | |
613 | * Each socket contains two socket buffers: one for sending data and | |
614 | * one for receiving data. Each buffer contains a queue of mbufs, | |
615 | * information about the number of mbufs and amount of data in the | |
616 | * queue, and other fields allowing select() statements and notification | |
617 | * on data availability to be implemented. | |
618 | * | |
619 | * Data stored in a socket buffer is maintained as a list of records. | |
620 | * Each record is a list of mbufs chained together with the m_next | |
621 | * field. Records are chained together with the m_nextpkt field. The upper | |
622 | * level routine soreceive() expects the following conventions to be | |
623 | * observed when placing information in the receive buffer: | |
624 | * | |
625 | * 1. If the protocol requires each message be preceded by the sender's | |
626 | * name, then a record containing that name must be present before | |
627 | * any associated data (mbuf's must be of type MT_SONAME). | |
628 | * 2. If the protocol supports the exchange of ``access rights'' (really | |
629 | * just additional data associated with the message), and there are | |
630 | * ``rights'' to be received, then a record containing this data | |
631 | * should be present (mbuf's must be of type MT_RIGHTS). | |
632 | * 3. If a name or rights record exists, then it must be followed by | |
633 | * a data record, perhaps of zero length. | |
634 | * | |
635 | * Before using a new socket structure it is first necessary to reserve | |
636 | * buffer space to the socket, by calling sbreserve(). This should commit | |
637 | * some of the available buffer space in the system buffer pool for the | |
638 | * socket (currently, it does nothing but enforce limits). The space | |
639 | * should be released by calling sbrelease() when the socket is destroyed. | |
640 | */ | |
641 | ||
2d21ac55 A |
642 | /* |
643 | * Returns: 0 Success | |
644 | * ENOBUFS | |
645 | */ | |
1c79356b | 646 | int |
b0d623f7 | 647 | soreserve(struct socket *so, u_int32_t sndcc, u_int32_t rcvcc) |
1c79356b | 648 | { |
1c79356b A |
649 | |
650 | if (sbreserve(&so->so_snd, sndcc) == 0) | |
651 | goto bad; | |
316670eb A |
652 | else |
653 | so->so_snd.sb_idealsize = sndcc; | |
654 | ||
1c79356b A |
655 | if (sbreserve(&so->so_rcv, rcvcc) == 0) |
656 | goto bad2; | |
316670eb A |
657 | else |
658 | so->so_rcv.sb_idealsize = rcvcc; | |
659 | ||
1c79356b A |
660 | if (so->so_rcv.sb_lowat == 0) |
661 | so->so_rcv.sb_lowat = 1; | |
662 | if (so->so_snd.sb_lowat == 0) | |
663 | so->so_snd.sb_lowat = MCLBYTES; | |
664 | if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) | |
665 | so->so_snd.sb_lowat = so->so_snd.sb_hiwat; | |
666 | return (0); | |
667 | bad2: | |
9bccf70c | 668 | #ifdef __APPLE__ |
0b4e3aa0 | 669 | selthreadclear(&so->so_snd.sb_sel); |
9bccf70c | 670 | #endif |
1c79356b A |
671 | sbrelease(&so->so_snd); |
672 | bad: | |
673 | return (ENOBUFS); | |
674 | } | |
675 | ||
676 | /* | |
677 | * Allot mbufs to a sockbuf. | |
678 | * Attempt to scale mbmax so that mbcnt doesn't become limiting | |
679 | * if buffering efficiency is near the normal case. | |
680 | */ | |
681 | int | |
b0d623f7 | 682 | sbreserve(struct sockbuf *sb, u_int32_t cc) |
1c79356b A |
683 | { |
684 | if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES)) | |
685 | return (0); | |
686 | sb->sb_hiwat = cc; | |
687 | sb->sb_mbmax = min(cc * sb_efficiency, sb_max); | |
688 | if (sb->sb_lowat > sb->sb_hiwat) | |
689 | sb->sb_lowat = sb->sb_hiwat; | |
690 | return (1); | |
691 | } | |
692 | ||
693 | /* | |
694 | * Free mbufs held by a socket, and reserved mbuf space. | |
695 | */ | |
2d21ac55 | 696 | /* WARNING needs to do selthreadclear() before calling this */ |
1c79356b | 697 | void |
2d21ac55 | 698 | sbrelease(struct sockbuf *sb) |
1c79356b | 699 | { |
1c79356b | 700 | sbflush(sb); |
9bccf70c A |
701 | sb->sb_hiwat = 0; |
702 | sb->sb_mbmax = 0; | |
1c79356b A |
703 | } |
704 | ||
705 | /* | |
706 | * Routines to add and remove | |
707 | * data from an mbuf queue. | |
708 | * | |
709 | * The routines sbappend() or sbappendrecord() are normally called to | |
710 | * append new mbufs to a socket buffer, after checking that adequate | |
711 | * space is available, comparing the function sbspace() with the amount | |
712 | * of data to be added. sbappendrecord() differs from sbappend() in | |
713 | * that data supplied is treated as the beginning of a new record. | |
714 | * To place a sender's address, optional access rights, and data in a | |
715 | * socket receive buffer, sbappendaddr() should be used. To place | |
716 | * access rights and data in a socket receive buffer, sbappendrights() | |
717 | * should be used. In either case, the new data begins a new record. | |
718 | * Note that unlike sbappend() and sbappendrecord(), these routines check | |
719 | * for the caller that there will be enough space to store the data. | |
720 | * Each fails if there is not enough space, or if it cannot find mbufs | |
721 | * to store additional information in. | |
722 | * | |
723 | * Reliable protocols may use the socket send buffer to hold data | |
724 | * awaiting acknowledgement. Data is normally copied from a socket | |
725 | * send buffer in a protocol with m_copy for output to a peer, | |
726 | * and then removing the data from the socket buffer with sbdrop() | |
727 | * or sbdroprecord() when the data is acknowledged by the peer. | |
728 | */ | |
729 | ||
730 | /* | |
731 | * Append mbuf chain m to the last record in the | |
732 | * socket buffer sb. The additional space associated | |
733 | * the mbuf chain is recorded in sb. Empty mbufs are | |
734 | * discarded and mbufs are compacted where possible. | |
735 | */ | |
91447636 | 736 | int |
2d21ac55 | 737 | sbappend(struct sockbuf *sb, struct mbuf *m) |
9bccf70c | 738 | { |
2d21ac55 | 739 | struct socket *so = sb->sb_so; |
1c79356b | 740 | |
2d21ac55 A |
741 | if (m == NULL || (sb->sb_flags & SB_DROP)) { |
742 | if (m != NULL) | |
743 | m_freem(m); | |
744 | return (0); | |
745 | } | |
fa4905b1 | 746 | |
2d21ac55 | 747 | SBLASTRECORDCHK(sb, "sbappend 1"); |
fa4905b1 | 748 | |
2d21ac55 A |
749 | if (sb->sb_lastrecord != NULL && (sb->sb_mbtail->m_flags & M_EOR)) |
750 | return (sbappendrecord(sb, m)); | |
751 | ||
752 | if (sb->sb_flags & SB_RECV) { | |
6d2010ae | 753 | int error = sflt_data_in(so, NULL, &m, NULL, 0); |
2d21ac55 A |
754 | SBLASTRECORDCHK(sb, "sbappend 2"); |
755 | if (error != 0) { | |
756 | if (error != EJUSTRETURN) | |
757 | m_freem(m); | |
758 | return (0); | |
91447636 | 759 | } |
91447636 A |
760 | } |
761 | ||
2d21ac55 A |
762 | /* If this is the first record, it's also the last record */ |
763 | if (sb->sb_lastrecord == NULL) | |
764 | sb->sb_lastrecord = m; | |
fa4905b1 | 765 | |
2d21ac55 A |
766 | sbcompress(sb, m, sb->sb_mbtail); |
767 | SBLASTRECORDCHK(sb, "sbappend 3"); | |
768 | return (1); | |
769 | } | |
770 | ||
771 | /* | |
772 | * Similar to sbappend, except that this is optimized for stream sockets. | |
773 | */ | |
774 | int | |
775 | sbappendstream(struct sockbuf *sb, struct mbuf *m) | |
776 | { | |
777 | struct socket *so = sb->sb_so; | |
778 | ||
779 | if (m->m_nextpkt != NULL || (sb->sb_mb != sb->sb_lastrecord)) | |
780 | panic("sbappendstream: nexpkt %p || mb %p != lastrecord %p\n", | |
781 | m->m_nextpkt, sb->sb_mb, sb->sb_lastrecord); | |
782 | ||
783 | SBLASTMBUFCHK(sb, __func__); | |
784 | ||
785 | if (m == NULL || (sb->sb_flags & SB_DROP)) { | |
786 | if (m != NULL) | |
787 | m_freem(m); | |
788 | return (0); | |
789 | } | |
790 | ||
791 | if (sb->sb_flags & SB_RECV) { | |
6d2010ae | 792 | int error = sflt_data_in(so, NULL, &m, NULL, 0); |
2d21ac55 A |
793 | SBLASTRECORDCHK(sb, "sbappendstream 1"); |
794 | if (error != 0) { | |
795 | if (error != EJUSTRETURN) | |
796 | m_freem(m); | |
797 | return (0); | |
798 | } | |
799 | } | |
800 | ||
801 | sbcompress(sb, m, sb->sb_mbtail); | |
802 | sb->sb_lastrecord = sb->sb_mb; | |
803 | SBLASTRECORDCHK(sb, "sbappendstream 2"); | |
804 | return (1); | |
1c79356b A |
805 | } |
806 | ||
807 | #ifdef SOCKBUF_DEBUG | |
808 | void | |
2d21ac55 | 809 | sbcheck(struct sockbuf *sb) |
1c79356b | 810 | { |
2d21ac55 A |
811 | struct mbuf *m; |
812 | struct mbuf *n = 0; | |
b0d623f7 | 813 | u_int32_t len = 0, mbcnt = 0; |
91447636 A |
814 | lck_mtx_t *mutex_held; |
815 | ||
2d21ac55 | 816 | if (sb->sb_so->so_proto->pr_getlock != NULL) |
91447636 | 817 | mutex_held = (*sb->sb_so->so_proto->pr_getlock)(sb->sb_so, 0); |
2d21ac55 | 818 | else |
91447636 A |
819 | mutex_held = sb->sb_so->so_proto->pr_domain->dom_mtx; |
820 | ||
821 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
822 | ||
823 | if (sbchecking == 0) | |
824 | return; | |
1c79356b A |
825 | |
826 | for (m = sb->sb_mb; m; m = n) { | |
2d21ac55 A |
827 | n = m->m_nextpkt; |
828 | for (; m; m = m->m_next) { | |
829 | len += m->m_len; | |
830 | mbcnt += MSIZE; | |
831 | /* XXX pretty sure this is bogus */ | |
832 | if (m->m_flags & M_EXT) | |
833 | mbcnt += m->m_ext.ext_size; | |
834 | } | |
835 | } | |
836 | if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { | |
837 | panic("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc, | |
838 | mbcnt, sb->sb_mbcnt); | |
1c79356b A |
839 | } |
840 | } | |
841 | #endif | |
842 | ||
2d21ac55 A |
843 | void |
844 | sblastrecordchk(struct sockbuf *sb, const char *where) | |
845 | { | |
846 | struct mbuf *m = sb->sb_mb; | |
847 | ||
848 | while (m && m->m_nextpkt) | |
849 | m = m->m_nextpkt; | |
850 | ||
851 | if (m != sb->sb_lastrecord) { | |
852 | printf("sblastrecordchk: mb %p lastrecord %p last %p\n", | |
853 | sb->sb_mb, sb->sb_lastrecord, m); | |
854 | printf("packet chain:\n"); | |
855 | for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) | |
856 | printf("\t%p\n", m); | |
857 | panic("sblastrecordchk from %s", where); | |
858 | } | |
859 | } | |
860 | ||
861 | void | |
862 | sblastmbufchk(struct sockbuf *sb, const char *where) | |
863 | { | |
864 | struct mbuf *m = sb->sb_mb; | |
865 | struct mbuf *n; | |
866 | ||
867 | while (m && m->m_nextpkt) | |
868 | m = m->m_nextpkt; | |
869 | ||
870 | while (m && m->m_next) | |
871 | m = m->m_next; | |
872 | ||
873 | if (m != sb->sb_mbtail) { | |
874 | printf("sblastmbufchk: mb %p mbtail %p last %p\n", | |
875 | sb->sb_mb, sb->sb_mbtail, m); | |
876 | printf("packet tree:\n"); | |
877 | for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { | |
878 | printf("\t"); | |
879 | for (n = m; n != NULL; n = n->m_next) | |
880 | printf("%p ", n); | |
881 | printf("\n"); | |
882 | } | |
883 | panic("sblastmbufchk from %s", where); | |
884 | } | |
885 | } | |
886 | ||
1c79356b | 887 | /* |
2d21ac55 | 888 | * Similar to sbappend, except the mbuf chain begins a new record. |
1c79356b | 889 | */ |
91447636 | 890 | int |
2d21ac55 | 891 | sbappendrecord(struct sockbuf *sb, struct mbuf *m0) |
1c79356b | 892 | { |
2d21ac55 A |
893 | struct mbuf *m; |
894 | int space = 0; | |
9bccf70c | 895 | |
2d21ac55 A |
896 | if (m0 == NULL || (sb->sb_flags & SB_DROP)) { |
897 | if (m0 != NULL) | |
898 | m_freem(m0); | |
899 | return (0); | |
900 | } | |
901 | ||
902 | for (m = m0; m != NULL; m = m->m_next) | |
903 | space += m->m_len; | |
904 | ||
905 | if (space > sbspace(sb) && !(sb->sb_flags & SB_UNIX)) { | |
906 | m_freem(m0); | |
907 | return (0); | |
908 | } | |
909 | ||
910 | if (sb->sb_flags & SB_RECV) { | |
911 | int error = sflt_data_in(sb->sb_so, NULL, &m0, NULL, | |
6d2010ae | 912 | sock_data_filt_flag_record); |
91447636 | 913 | if (error != 0) { |
2d21ac55 | 914 | SBLASTRECORDCHK(sb, "sbappendrecord 1"); |
91447636 A |
915 | if (error != EJUSTRETURN) |
916 | m_freem(m0); | |
2d21ac55 | 917 | return (0); |
1c79356b | 918 | } |
1c79356b | 919 | } |
2d21ac55 | 920 | |
1c79356b | 921 | /* |
1c79356b A |
922 | * Note this permits zero length records. |
923 | */ | |
924 | sballoc(sb, m0); | |
2d21ac55 A |
925 | SBLASTRECORDCHK(sb, "sbappendrecord 2"); |
926 | if (sb->sb_lastrecord != NULL) { | |
927 | sb->sb_lastrecord->m_nextpkt = m0; | |
928 | } else { | |
1c79356b | 929 | sb->sb_mb = m0; |
2d21ac55 A |
930 | } |
931 | sb->sb_lastrecord = m0; | |
4a3eedf9 | 932 | sb->sb_mbtail = m0; |
2d21ac55 | 933 | |
1c79356b A |
934 | m = m0->m_next; |
935 | m0->m_next = 0; | |
936 | if (m && (m0->m_flags & M_EOR)) { | |
937 | m0->m_flags &= ~M_EOR; | |
938 | m->m_flags |= M_EOR; | |
939 | } | |
2d21ac55 A |
940 | sbcompress(sb, m, m0); |
941 | SBLASTRECORDCHK(sb, "sbappendrecord 3"); | |
942 | return (1); | |
1c79356b A |
943 | } |
944 | ||
945 | /* | |
946 | * As above except that OOB data | |
947 | * is inserted at the beginning of the sockbuf, | |
948 | * but after any other OOB data. | |
949 | */ | |
91447636 | 950 | int |
2d21ac55 | 951 | sbinsertoob(struct sockbuf *sb, struct mbuf *m0) |
1c79356b | 952 | { |
91447636 A |
953 | struct mbuf *m; |
954 | struct mbuf **mp; | |
1c79356b A |
955 | |
956 | if (m0 == 0) | |
2d21ac55 A |
957 | return (0); |
958 | ||
959 | SBLASTRECORDCHK(sb, "sbinsertoob 1"); | |
960 | ||
91447636 A |
961 | if ((sb->sb_flags & SB_RECV) != 0) { |
962 | int error = sflt_data_in(sb->sb_so, NULL, &m0, NULL, | |
6d2010ae | 963 | sock_data_filt_flag_oob); |
2d21ac55 A |
964 | |
965 | SBLASTRECORDCHK(sb, "sbinsertoob 2"); | |
91447636 A |
966 | if (error) { |
967 | if (error != EJUSTRETURN) { | |
968 | m_freem(m0); | |
969 | } | |
2d21ac55 | 970 | return (0); |
1c79356b | 971 | } |
1c79356b | 972 | } |
2d21ac55 A |
973 | |
974 | for (mp = &sb->sb_mb; *mp; mp = &((*mp)->m_nextpkt)) { | |
975 | m = *mp; | |
976 | again: | |
1c79356b A |
977 | switch (m->m_type) { |
978 | ||
979 | case MT_OOBDATA: | |
980 | continue; /* WANT next train */ | |
981 | ||
982 | case MT_CONTROL: | |
983 | m = m->m_next; | |
984 | if (m) | |
985 | goto again; /* inspect THIS train further */ | |
986 | } | |
987 | break; | |
988 | } | |
989 | /* | |
990 | * Put the first mbuf on the queue. | |
991 | * Note this permits zero length records. | |
992 | */ | |
993 | sballoc(sb, m0); | |
994 | m0->m_nextpkt = *mp; | |
2d21ac55 A |
995 | if (*mp == NULL) { |
996 | /* m0 is actually the new tail */ | |
997 | sb->sb_lastrecord = m0; | |
998 | } | |
1c79356b A |
999 | *mp = m0; |
1000 | m = m0->m_next; | |
1001 | m0->m_next = 0; | |
1002 | if (m && (m0->m_flags & M_EOR)) { | |
1003 | m0->m_flags &= ~M_EOR; | |
1004 | m->m_flags |= M_EOR; | |
1005 | } | |
2d21ac55 A |
1006 | sbcompress(sb, m, m0); |
1007 | SBLASTRECORDCHK(sb, "sbinsertoob 3"); | |
1008 | return (1); | |
1c79356b A |
1009 | } |
1010 | ||
1011 | /* | |
1012 | * Append address and data, and optionally, control (ancillary) data | |
1013 | * to the receive queue of a socket. If present, | |
1014 | * m0 must include a packet header with total length. | |
1015 | * Returns 0 if no space in sockbuf or insufficient mbufs. | |
2d21ac55 A |
1016 | * |
1017 | * Returns: 0 No space/out of mbufs | |
1018 | * 1 Success | |
1c79356b | 1019 | */ |
91447636 | 1020 | static int |
2d21ac55 A |
1021 | sbappendaddr_internal(struct sockbuf *sb, struct sockaddr *asa, |
1022 | struct mbuf *m0, struct mbuf *control) | |
1c79356b | 1023 | { |
2d21ac55 | 1024 | struct mbuf *m, *n, *nlast; |
1c79356b | 1025 | int space = asa->sa_len; |
1c79356b A |
1026 | |
1027 | if (m0 && (m0->m_flags & M_PKTHDR) == 0) | |
1028 | panic("sbappendaddr"); | |
1029 | ||
1c79356b A |
1030 | if (m0) |
1031 | space += m0->m_pkthdr.len; | |
1032 | for (n = control; n; n = n->m_next) { | |
1033 | space += n->m_len; | |
1034 | if (n->m_next == 0) /* keep pointer to last control buf */ | |
1035 | break; | |
1036 | } | |
1037 | if (space > sbspace(sb)) | |
1038 | return (0); | |
1039 | if (asa->sa_len > MLEN) | |
1040 | return (0); | |
1041 | MGET(m, M_DONTWAIT, MT_SONAME); | |
1042 | if (m == 0) | |
1043 | return (0); | |
1044 | m->m_len = asa->sa_len; | |
1045 | bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); | |
1046 | if (n) | |
1047 | n->m_next = m0; /* concatenate data to control */ | |
1048 | else | |
1049 | control = m0; | |
1050 | m->m_next = control; | |
2d21ac55 A |
1051 | |
1052 | SBLASTRECORDCHK(sb, "sbappendadddr 1"); | |
1053 | ||
1054 | for (n = m; n->m_next != NULL; n = n->m_next) | |
1c79356b | 1055 | sballoc(sb, n); |
2d21ac55 A |
1056 | sballoc(sb, n); |
1057 | nlast = n; | |
1058 | ||
1059 | if (sb->sb_lastrecord != NULL) { | |
1060 | sb->sb_lastrecord->m_nextpkt = m; | |
1061 | } else { | |
1c79356b | 1062 | sb->sb_mb = m; |
2d21ac55 A |
1063 | } |
1064 | sb->sb_lastrecord = m; | |
1065 | sb->sb_mbtail = nlast; | |
1066 | ||
1067 | SBLASTMBUFCHK(sb, __func__); | |
1068 | SBLASTRECORDCHK(sb, "sbappendadddr 2"); | |
1069 | ||
1070 | postevent(0, sb, EV_RWBYTES); | |
1c79356b A |
1071 | return (1); |
1072 | } | |
1073 | ||
2d21ac55 A |
1074 | /* |
1075 | * Returns: 0 Error: No space/out of mbufs/etc. | |
1076 | * 1 Success | |
1077 | * | |
1078 | * Imputed: (*error_out) errno for error | |
1079 | * ENOBUFS | |
1080 | * sflt_data_in:??? [whatever a filter author chooses] | |
1081 | */ | |
1c79356b | 1082 | int |
2d21ac55 A |
1083 | sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0, |
1084 | struct mbuf *control, int *error_out) | |
91447636 A |
1085 | { |
1086 | int result = 0; | |
2d21ac55 A |
1087 | boolean_t sb_unix = (sb->sb_flags & SB_UNIX); |
1088 | ||
1089 | if (error_out) | |
1090 | *error_out = 0; | |
1091 | ||
91447636 A |
1092 | if (m0 && (m0->m_flags & M_PKTHDR) == 0) |
1093 | panic("sbappendaddrorfree"); | |
2d21ac55 A |
1094 | |
1095 | if (sb->sb_flags & SB_DROP) { | |
1096 | if (m0 != NULL) | |
1097 | m_freem(m0); | |
1098 | if (control != NULL && !sb_unix) | |
1099 | m_freem(control); | |
1100 | if (error_out != NULL) | |
1101 | *error_out = EINVAL; | |
1102 | return (0); | |
1103 | } | |
1104 | ||
91447636 A |
1105 | /* Call socket data in filters */ |
1106 | if ((sb->sb_flags & SB_RECV) != 0) { | |
1107 | int error; | |
6d2010ae | 1108 | error = sflt_data_in(sb->sb_so, asa, &m0, &control, 0); |
2d21ac55 | 1109 | SBLASTRECORDCHK(sb, __func__); |
91447636 A |
1110 | if (error) { |
1111 | if (error != EJUSTRETURN) { | |
2d21ac55 A |
1112 | if (m0) |
1113 | m_freem(m0); | |
1114 | if (control != NULL && !sb_unix) | |
1115 | m_freem(control); | |
1116 | if (error_out) | |
1117 | *error_out = error; | |
91447636 | 1118 | } |
2d21ac55 | 1119 | return (0); |
91447636 A |
1120 | } |
1121 | } | |
2d21ac55 | 1122 | |
91447636 A |
1123 | result = sbappendaddr_internal(sb, asa, m0, control); |
1124 | if (result == 0) { | |
2d21ac55 A |
1125 | if (m0) |
1126 | m_freem(m0); | |
1127 | if (control != NULL && !sb_unix) | |
1128 | m_freem(control); | |
1129 | if (error_out) | |
1130 | *error_out = ENOBUFS; | |
91447636 | 1131 | } |
2d21ac55 A |
1132 | |
1133 | return (result); | |
91447636 A |
1134 | } |
1135 | ||
1136 | static int | |
2d21ac55 A |
1137 | sbappendcontrol_internal(struct sockbuf *sb, struct mbuf *m0, |
1138 | struct mbuf *control) | |
1c79356b | 1139 | { |
2d21ac55 | 1140 | struct mbuf *m, *mlast, *n; |
1c79356b | 1141 | int space = 0; |
1c79356b A |
1142 | |
1143 | if (control == 0) | |
1144 | panic("sbappendcontrol"); | |
1145 | ||
1c79356b A |
1146 | for (m = control; ; m = m->m_next) { |
1147 | space += m->m_len; | |
1148 | if (m->m_next == 0) | |
1149 | break; | |
1150 | } | |
1151 | n = m; /* save pointer to last control buffer */ | |
1152 | for (m = m0; m; m = m->m_next) | |
1153 | space += m->m_len; | |
2d21ac55 | 1154 | if (space > sbspace(sb) && !(sb->sb_flags & SB_UNIX)) |
1c79356b A |
1155 | return (0); |
1156 | n->m_next = m0; /* concatenate data to control */ | |
2d21ac55 A |
1157 | |
1158 | SBLASTRECORDCHK(sb, "sbappendcontrol 1"); | |
1159 | ||
1160 | for (m = control; m->m_next != NULL; m = m->m_next) | |
1c79356b | 1161 | sballoc(sb, m); |
2d21ac55 A |
1162 | sballoc(sb, m); |
1163 | mlast = m; | |
1164 | ||
1165 | if (sb->sb_lastrecord != NULL) { | |
1166 | sb->sb_lastrecord->m_nextpkt = control; | |
1167 | } else { | |
1c79356b | 1168 | sb->sb_mb = control; |
2d21ac55 A |
1169 | } |
1170 | sb->sb_lastrecord = control; | |
1171 | sb->sb_mbtail = mlast; | |
1172 | ||
1173 | SBLASTMBUFCHK(sb, __func__); | |
1174 | SBLASTRECORDCHK(sb, "sbappendcontrol 2"); | |
1175 | ||
1176 | postevent(0, sb, EV_RWBYTES); | |
1c79356b A |
1177 | return (1); |
1178 | } | |
1179 | ||
91447636 | 1180 | int |
2d21ac55 A |
1181 | sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control, |
1182 | int *error_out) | |
91447636 A |
1183 | { |
1184 | int result = 0; | |
2d21ac55 A |
1185 | boolean_t sb_unix = (sb->sb_flags & SB_UNIX); |
1186 | ||
1187 | if (error_out) | |
1188 | *error_out = 0; | |
1189 | ||
1190 | if (sb->sb_flags & SB_DROP) { | |
1191 | if (m0 != NULL) | |
1192 | m_freem(m0); | |
1193 | if (control != NULL && !sb_unix) | |
1194 | m_freem(control); | |
1195 | if (error_out != NULL) | |
1196 | *error_out = EINVAL; | |
1197 | return (0); | |
1198 | } | |
1199 | ||
91447636 A |
1200 | if (sb->sb_flags & SB_RECV) { |
1201 | int error; | |
2d21ac55 | 1202 | |
6d2010ae | 1203 | error = sflt_data_in(sb->sb_so, NULL, &m0, &control, 0); |
2d21ac55 | 1204 | SBLASTRECORDCHK(sb, __func__); |
91447636 A |
1205 | if (error) { |
1206 | if (error != EJUSTRETURN) { | |
2d21ac55 A |
1207 | if (m0) |
1208 | m_freem(m0); | |
1209 | if (control != NULL && !sb_unix) | |
1210 | m_freem(control); | |
1211 | if (error_out) | |
1212 | *error_out = error; | |
91447636 | 1213 | } |
2d21ac55 | 1214 | return (0); |
91447636 A |
1215 | } |
1216 | } | |
2d21ac55 | 1217 | |
91447636 A |
1218 | result = sbappendcontrol_internal(sb, m0, control); |
1219 | if (result == 0) { | |
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 = ENOBUFS; | |
91447636 | 1226 | } |
2d21ac55 A |
1227 | |
1228 | return (result); | |
91447636 A |
1229 | } |
1230 | ||
1c79356b A |
1231 | /* |
1232 | * Compress mbuf chain m into the socket | |
1233 | * buffer sb following mbuf n. If n | |
1234 | * is null, the buffer is presumed empty. | |
1235 | */ | |
2d21ac55 A |
1236 | static inline void |
1237 | sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n) | |
1238 | { | |
1239 | int eor = 0; | |
1240 | struct mbuf *o; | |
1241 | ||
1242 | if (m == NULL) { | |
1243 | /* There is nothing to compress; just update the tail */ | |
1244 | for (; n->m_next != NULL; n = n->m_next) | |
1245 | ; | |
1246 | sb->sb_mbtail = n; | |
1247 | goto done; | |
1248 | } | |
1c79356b A |
1249 | |
1250 | while (m) { | |
1251 | eor |= m->m_flags & M_EOR; | |
2d21ac55 A |
1252 | if (m->m_len == 0 && (eor == 0 || |
1253 | (((o = m->m_next) || (o = n)) && o->m_type == m->m_type))) { | |
1254 | if (sb->sb_lastrecord == m) | |
1255 | sb->sb_lastrecord = m->m_next; | |
1c79356b A |
1256 | m = m_free(m); |
1257 | continue; | |
1258 | } | |
9bccf70c A |
1259 | if (n && (n->m_flags & M_EOR) == 0 && |
1260 | #ifndef __APPLE__ | |
1261 | M_WRITABLE(n) && | |
1262 | #endif | |
1263 | m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ | |
1264 | m->m_len <= M_TRAILINGSPACE(n) && | |
1c79356b A |
1265 | n->m_type == m->m_type) { |
1266 | bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, | |
1267 | (unsigned)m->m_len); | |
1268 | n->m_len += m->m_len; | |
1269 | sb->sb_cc += m->m_len; | |
2d21ac55 A |
1270 | if (m->m_type != MT_DATA && m->m_type != MT_HEADER && |
1271 | m->m_type != MT_OOBDATA) | |
1272 | /* XXX: Probably don't need.*/ | |
1273 | sb->sb_ctl += m->m_len; | |
1c79356b A |
1274 | m = m_free(m); |
1275 | continue; | |
1276 | } | |
1277 | if (n) | |
1278 | n->m_next = m; | |
1279 | else | |
1280 | sb->sb_mb = m; | |
2d21ac55 | 1281 | sb->sb_mbtail = m; |
1c79356b A |
1282 | sballoc(sb, m); |
1283 | n = m; | |
1284 | m->m_flags &= ~M_EOR; | |
1285 | m = m->m_next; | |
1286 | n->m_next = 0; | |
1287 | } | |
1288 | if (eor) { | |
1289 | if (n) | |
1290 | n->m_flags |= eor; | |
1291 | else | |
1292 | printf("semi-panic: sbcompress\n"); | |
1293 | } | |
2d21ac55 A |
1294 | done: |
1295 | SBLASTMBUFCHK(sb, __func__); | |
1296 | postevent(0, sb, EV_RWBYTES); | |
1297 | } | |
1298 | ||
1299 | void | |
1300 | sb_empty_assert(struct sockbuf *sb, const char *where) | |
1301 | { | |
1302 | if (!(sb->sb_cc == 0 && sb->sb_mb == NULL && sb->sb_mbcnt == 0 && | |
1303 | sb->sb_mbtail == NULL && sb->sb_lastrecord == NULL)) { | |
b0d623f7 | 1304 | panic("%s: sb %p so %p cc %d mbcnt %d mb %p mbtail %p " |
2d21ac55 A |
1305 | "lastrecord %p\n", where, sb, sb->sb_so, sb->sb_cc, |
1306 | sb->sb_mbcnt, sb->sb_mb, sb->sb_mbtail, sb->sb_lastrecord); | |
1307 | /* NOTREACHED */ | |
1308 | } | |
1c79356b A |
1309 | } |
1310 | ||
1311 | /* | |
1312 | * Free all mbufs in a sockbuf. | |
1313 | * Check that all resources are reclaimed. | |
1314 | */ | |
1315 | void | |
2d21ac55 | 1316 | sbflush(struct sockbuf *sb) |
1c79356b | 1317 | { |
91447636 | 1318 | if (sb->sb_so == NULL) |
2d21ac55 A |
1319 | panic("sbflush sb->sb_so already null sb=%p\n", sb); |
1320 | (void) sblock(sb, M_WAIT); | |
9bccf70c A |
1321 | while (sb->sb_mbcnt) { |
1322 | /* | |
1323 | * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: | |
1324 | * we would loop forever. Panic instead. | |
1325 | */ | |
1326 | if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) | |
1327 | break; | |
1c79356b | 1328 | sbdrop(sb, (int)sb->sb_cc); |
9bccf70c | 1329 | } |
2d21ac55 | 1330 | sb_empty_assert(sb, __func__); |
1c79356b | 1331 | postevent(0, sb, EV_RWBYTES); |
91447636 A |
1332 | sbunlock(sb, 1); /* keep socket locked */ |
1333 | ||
1c79356b A |
1334 | } |
1335 | ||
1336 | /* | |
1337 | * Drop data from (the front of) a sockbuf. | |
9bccf70c A |
1338 | * use m_freem_list to free the mbuf structures |
1339 | * under a single lock... this is done by pruning | |
1340 | * the top of the tree from the body by keeping track | |
1341 | * of where we get to in the tree and then zeroing the | |
1342 | * two pertinent pointers m_nextpkt and m_next | |
1343 | * the socket buffer is then updated to point at the new | |
1344 | * top of the tree and the pruned area is released via | |
1345 | * m_freem_list. | |
1c79356b A |
1346 | */ |
1347 | void | |
2d21ac55 | 1348 | sbdrop(struct sockbuf *sb, int len) |
1c79356b | 1349 | { |
2d21ac55 | 1350 | struct mbuf *m, *free_list, *ml; |
fa4905b1 | 1351 | struct mbuf *next, *last; |
1c79356b | 1352 | |
fa4905b1 A |
1353 | KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_START), sb, len, 0, 0, 0); |
1354 | ||
1c79356b | 1355 | next = (m = sb->sb_mb) ? m->m_nextpkt : 0; |
fa4905b1 A |
1356 | free_list = last = m; |
1357 | ml = (struct mbuf *)0; | |
1358 | ||
1c79356b A |
1359 | while (len > 0) { |
1360 | if (m == 0) { | |
2d21ac55 A |
1361 | if (next == 0) { |
1362 | /* | |
1363 | * temporarily replacing this panic with printf | |
1364 | * because it occurs occasionally when closing | |
1365 | * a socket when there is no harm in ignoring | |
1366 | * it. This problem will be investigated | |
1367 | * further. | |
1368 | */ | |
1369 | /* panic("sbdrop"); */ | |
1370 | printf("sbdrop - count not zero\n"); | |
1371 | len = 0; | |
1372 | /* | |
1373 | * zero the counts. if we have no mbufs, | |
1374 | * we have no data (PR-2986815) | |
1375 | */ | |
1376 | sb->sb_cc = 0; | |
1377 | sb->sb_mbcnt = 0; | |
1378 | break; | |
1379 | } | |
1380 | m = last = next; | |
1381 | next = m->m_nextpkt; | |
1382 | continue; | |
1c79356b A |
1383 | } |
1384 | if (m->m_len > len) { | |
1385 | m->m_len -= len; | |
1386 | m->m_data += len; | |
1387 | sb->sb_cc -= len; | |
2d21ac55 A |
1388 | if (m->m_type != MT_DATA && m->m_type != MT_HEADER && |
1389 | m->m_type != MT_OOBDATA) | |
1390 | sb->sb_ctl -= len; | |
1c79356b A |
1391 | break; |
1392 | } | |
1393 | len -= m->m_len; | |
1394 | sbfree(sb, m); | |
fa4905b1 A |
1395 | |
1396 | ml = m; | |
1397 | m = m->m_next; | |
1c79356b A |
1398 | } |
1399 | while (m && m->m_len == 0) { | |
1400 | sbfree(sb, m); | |
fa4905b1 A |
1401 | |
1402 | ml = m; | |
1403 | m = m->m_next; | |
1404 | } | |
1405 | if (ml) { | |
2d21ac55 | 1406 | ml->m_next = (struct mbuf *)0; |
fa4905b1 | 1407 | last->m_nextpkt = (struct mbuf *)0; |
2d21ac55 | 1408 | m_freem_list(free_list); |
1c79356b A |
1409 | } |
1410 | if (m) { | |
1411 | sb->sb_mb = m; | |
1412 | m->m_nextpkt = next; | |
2d21ac55 | 1413 | } else { |
1c79356b | 1414 | sb->sb_mb = next; |
2d21ac55 A |
1415 | } |
1416 | ||
1417 | /* | |
1418 | * First part is an inline SB_EMPTY_FIXUP(). Second part | |
1419 | * makes sure sb_lastrecord is up-to-date if we dropped | |
1420 | * part of the last record. | |
1421 | */ | |
1422 | m = sb->sb_mb; | |
1423 | if (m == NULL) { | |
1424 | sb->sb_mbtail = NULL; | |
1425 | sb->sb_lastrecord = NULL; | |
1426 | } else if (m->m_nextpkt == NULL) { | |
1427 | sb->sb_lastrecord = m; | |
1428 | } | |
fa4905b1 | 1429 | |
1c79356b | 1430 | postevent(0, sb, EV_RWBYTES); |
fa4905b1 A |
1431 | |
1432 | KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, 0, 0, 0, 0); | |
1c79356b A |
1433 | } |
1434 | ||
1435 | /* | |
1436 | * Drop a record off the front of a sockbuf | |
1437 | * and move the next record to the front. | |
1438 | */ | |
1439 | void | |
2d21ac55 | 1440 | sbdroprecord(struct sockbuf *sb) |
1c79356b | 1441 | { |
2d21ac55 | 1442 | struct mbuf *m, *mn; |
1c79356b A |
1443 | |
1444 | m = sb->sb_mb; | |
1445 | if (m) { | |
1446 | sb->sb_mb = m->m_nextpkt; | |
1447 | do { | |
1448 | sbfree(sb, m); | |
1449 | MFREE(m, mn); | |
9bccf70c A |
1450 | m = mn; |
1451 | } while (m); | |
1c79356b | 1452 | } |
2d21ac55 | 1453 | SB_EMPTY_FIXUP(sb); |
1c79356b A |
1454 | postevent(0, sb, EV_RWBYTES); |
1455 | } | |
1456 | ||
1457 | /* | |
1458 | * Create a "control" mbuf containing the specified data | |
1459 | * with the specified type for presentation on a socket buffer. | |
1460 | */ | |
1461 | struct mbuf * | |
2d21ac55 | 1462 | sbcreatecontrol(caddr_t p, int size, int type, int level) |
1c79356b | 1463 | { |
2d21ac55 | 1464 | struct cmsghdr *cp; |
1c79356b A |
1465 | struct mbuf *m; |
1466 | ||
9bccf70c | 1467 | if (CMSG_SPACE((u_int)size) > MLEN) |
2d21ac55 | 1468 | return ((struct mbuf *)NULL); |
1c79356b | 1469 | if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) |
2d21ac55 | 1470 | return ((struct mbuf *)NULL); |
1c79356b | 1471 | cp = mtod(m, struct cmsghdr *); |
316670eb | 1472 | VERIFY(IS_P2ALIGNED(cp, sizeof (u_int32_t))); |
1c79356b | 1473 | /* XXX check size? */ |
2d21ac55 | 1474 | (void) memcpy(CMSG_DATA(cp), p, size); |
9bccf70c A |
1475 | m->m_len = CMSG_SPACE(size); |
1476 | cp->cmsg_len = CMSG_LEN(size); | |
1c79356b A |
1477 | cp->cmsg_level = level; |
1478 | cp->cmsg_type = type; | |
1479 | return (m); | |
1480 | } | |
1481 | ||
6d2010ae A |
1482 | struct mbuf** |
1483 | sbcreatecontrol_mbuf(caddr_t p, int size, int type, int level, struct mbuf** mp) | |
1484 | { | |
1485 | struct mbuf* m; | |
1486 | struct cmsghdr *cp; | |
1487 | ||
1488 | if (*mp == NULL){ | |
1489 | *mp = sbcreatecontrol(p, size, type, level); | |
1490 | return mp; | |
1491 | } | |
316670eb | 1492 | |
6d2010ae A |
1493 | if (CMSG_SPACE((u_int)size) + (*mp)->m_len > MLEN){ |
1494 | mp = &(*mp)->m_next; | |
1495 | *mp = sbcreatecontrol(p, size, type, level); | |
1496 | return mp; | |
1497 | } | |
316670eb | 1498 | |
6d2010ae | 1499 | m = *mp; |
316670eb A |
1500 | |
1501 | cp = (struct cmsghdr *)(void *)(mtod(m, char *) + m->m_len); | |
1502 | /* CMSG_SPACE ensures 32-bit alignment */ | |
1503 | VERIFY(IS_P2ALIGNED(cp, sizeof (u_int32_t))); | |
6d2010ae | 1504 | m->m_len += CMSG_SPACE(size); |
316670eb | 1505 | |
6d2010ae A |
1506 | /* XXX check size? */ |
1507 | (void) memcpy(CMSG_DATA(cp), p, size); | |
1508 | cp->cmsg_len = CMSG_LEN(size); | |
1509 | cp->cmsg_level = level; | |
1510 | cp->cmsg_type = type; | |
316670eb | 1511 | |
6d2010ae A |
1512 | return mp; |
1513 | } | |
1514 | ||
1515 | ||
1c79356b A |
1516 | /* |
1517 | * Some routines that return EOPNOTSUPP for entry points that are not | |
1518 | * supported by a protocol. Fill in as needed. | |
1519 | */ | |
1520 | int | |
2d21ac55 | 1521 | pru_abort_notsupp(__unused struct socket *so) |
1c79356b | 1522 | { |
2d21ac55 | 1523 | return (EOPNOTSUPP); |
1c79356b A |
1524 | } |
1525 | ||
1c79356b | 1526 | int |
2d21ac55 | 1527 | pru_accept_notsupp(__unused struct socket *so, __unused struct sockaddr **nam) |
1c79356b | 1528 | { |
2d21ac55 | 1529 | return (EOPNOTSUPP); |
1c79356b A |
1530 | } |
1531 | ||
1532 | int | |
2d21ac55 A |
1533 | pru_attach_notsupp(__unused struct socket *so, __unused int proto, |
1534 | __unused struct proc *p) | |
1c79356b | 1535 | { |
2d21ac55 | 1536 | return (EOPNOTSUPP); |
1c79356b A |
1537 | } |
1538 | ||
1539 | int | |
2d21ac55 A |
1540 | pru_bind_notsupp(__unused struct socket *so, __unused struct sockaddr *nam, |
1541 | __unused struct proc *p) | |
1c79356b | 1542 | { |
2d21ac55 | 1543 | return (EOPNOTSUPP); |
1c79356b A |
1544 | } |
1545 | ||
1546 | int | |
2d21ac55 A |
1547 | pru_connect_notsupp(__unused struct socket *so, __unused struct sockaddr *nam, |
1548 | __unused struct proc *p) | |
1c79356b | 1549 | { |
2d21ac55 | 1550 | return (EOPNOTSUPP); |
1c79356b A |
1551 | } |
1552 | ||
1553 | int | |
2d21ac55 | 1554 | pru_connect2_notsupp(__unused struct socket *so1, __unused struct socket *so2) |
1c79356b | 1555 | { |
2d21ac55 | 1556 | return (EOPNOTSUPP); |
1c79356b A |
1557 | } |
1558 | ||
1559 | int | |
b0d623f7 | 1560 | pru_control_notsupp(__unused struct socket *so, __unused u_long cmd, |
2d21ac55 | 1561 | __unused caddr_t data, __unused struct ifnet *ifp, __unused struct proc *p) |
1c79356b | 1562 | { |
2d21ac55 | 1563 | return (EOPNOTSUPP); |
1c79356b A |
1564 | } |
1565 | ||
1566 | int | |
2d21ac55 | 1567 | pru_detach_notsupp(__unused struct socket *so) |
1c79356b | 1568 | { |
2d21ac55 | 1569 | return (EOPNOTSUPP); |
1c79356b A |
1570 | } |
1571 | ||
1572 | int | |
2d21ac55 | 1573 | pru_disconnect_notsupp(__unused struct socket *so) |
1c79356b | 1574 | { |
2d21ac55 | 1575 | return (EOPNOTSUPP); |
1c79356b A |
1576 | } |
1577 | ||
1578 | int | |
2d21ac55 | 1579 | pru_listen_notsupp(__unused struct socket *so, __unused struct proc *p) |
1c79356b | 1580 | { |
2d21ac55 | 1581 | return (EOPNOTSUPP); |
1c79356b A |
1582 | } |
1583 | ||
1584 | int | |
2d21ac55 | 1585 | pru_peeraddr_notsupp(__unused struct socket *so, __unused struct sockaddr **nam) |
1c79356b | 1586 | { |
2d21ac55 | 1587 | return (EOPNOTSUPP); |
1c79356b A |
1588 | } |
1589 | ||
1590 | int | |
2d21ac55 | 1591 | pru_rcvd_notsupp(__unused struct socket *so, __unused int flags) |
1c79356b | 1592 | { |
2d21ac55 | 1593 | return (EOPNOTSUPP); |
1c79356b A |
1594 | } |
1595 | ||
1596 | int | |
2d21ac55 A |
1597 | pru_rcvoob_notsupp(__unused struct socket *so, __unused struct mbuf *m, |
1598 | __unused int flags) | |
1c79356b | 1599 | { |
2d21ac55 | 1600 | return (EOPNOTSUPP); |
1c79356b A |
1601 | } |
1602 | ||
1603 | int | |
2d21ac55 A |
1604 | pru_send_notsupp(__unused struct socket *so, __unused int flags, |
1605 | __unused struct mbuf *m, __unused struct sockaddr *addr, | |
1606 | __unused struct mbuf *control, __unused struct proc *p) | |
1c79356b A |
1607 | |
1608 | { | |
2d21ac55 | 1609 | return (EOPNOTSUPP); |
1c79356b A |
1610 | } |
1611 | ||
1612 | ||
1613 | /* | |
1614 | * This isn't really a ``null'' operation, but it's the default one | |
1615 | * and doesn't do anything destructive. | |
1616 | */ | |
1617 | int | |
2d21ac55 | 1618 | pru_sense_null(struct socket *so, void *ub, int isstat64) |
1c79356b | 1619 | { |
2d21ac55 A |
1620 | if (isstat64 != 0) { |
1621 | struct stat64 *sb64; | |
1c79356b | 1622 | |
2d21ac55 A |
1623 | sb64 = (struct stat64 *)ub; |
1624 | sb64->st_blksize = so->so_snd.sb_hiwat; | |
1625 | } else { | |
1626 | struct stat *sb; | |
1c79356b | 1627 | |
2d21ac55 A |
1628 | sb = (struct stat *)ub; |
1629 | sb->st_blksize = so->so_snd.sb_hiwat; | |
1630 | } | |
1c79356b | 1631 | |
2d21ac55 | 1632 | return (0); |
1c79356b A |
1633 | } |
1634 | ||
1c79356b A |
1635 | |
1636 | int | |
2d21ac55 A |
1637 | pru_sosend_notsupp(__unused struct socket *so, __unused struct sockaddr *addr, |
1638 | __unused struct uio *uio, __unused struct mbuf *top, | |
1639 | __unused struct mbuf *control, __unused int flags) | |
1c79356b | 1640 | |
1c79356b | 1641 | { |
2d21ac55 | 1642 | return (EOPNOTSUPP); |
1c79356b A |
1643 | } |
1644 | ||
1645 | int | |
2d21ac55 A |
1646 | pru_soreceive_notsupp(__unused struct socket *so, |
1647 | __unused struct sockaddr **paddr, | |
1648 | __unused struct uio *uio, __unused struct mbuf **mp0, | |
1649 | __unused struct mbuf **controlp, __unused int *flagsp) | |
1c79356b | 1650 | { |
2d21ac55 | 1651 | return (EOPNOTSUPP); |
1c79356b A |
1652 | } |
1653 | ||
2d21ac55 A |
1654 | int |
1655 | pru_shutdown_notsupp(__unused struct socket *so) | |
1c79356b | 1656 | { |
2d21ac55 | 1657 | return (EOPNOTSUPP); |
1c79356b A |
1658 | } |
1659 | ||
2d21ac55 A |
1660 | int |
1661 | pru_sockaddr_notsupp(__unused struct socket *so, __unused struct sockaddr **nam) | |
1c79356b | 1662 | { |
2d21ac55 | 1663 | return (EOPNOTSUPP); |
1c79356b A |
1664 | } |
1665 | ||
91447636 A |
1666 | int |
1667 | pru_sopoll_notsupp(__unused struct socket *so, __unused int events, | |
2d21ac55 | 1668 | __unused kauth_cred_t cred, __unused void *wql) |
1c79356b | 1669 | { |
2d21ac55 | 1670 | return (EOPNOTSUPP); |
1c79356b A |
1671 | } |
1672 | ||
1673 | ||
9bccf70c A |
1674 | #ifdef __APPLE__ |
1675 | /* | |
1676 | * The following are macros on BSD and functions on Darwin | |
1677 | */ | |
1c79356b | 1678 | |
0b4e3aa0 A |
1679 | /* |
1680 | * Do we need to notify the other side when I/O is possible? | |
1681 | */ | |
1682 | ||
2d21ac55 | 1683 | int |
0b4e3aa0 A |
1684 | sb_notify(struct sockbuf *sb) |
1685 | { | |
2d21ac55 A |
1686 | return ((sb->sb_flags & |
1687 | (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL|SB_KNOTE)) != 0); | |
0b4e3aa0 A |
1688 | } |
1689 | ||
1690 | /* | |
1691 | * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? | |
1692 | * This is problematical if the fields are unsigned, as the space might | |
1693 | * still be negative (cc > hiwat or mbcnt > mbmax). Should detect | |
b0d623f7 | 1694 | * overflow and return 0. |
0b4e3aa0 | 1695 | */ |
b0d623f7 | 1696 | int |
0b4e3aa0 A |
1697 | sbspace(struct sockbuf *sb) |
1698 | { | |
b0d623f7 A |
1699 | int space = |
1700 | imin((int)(sb->sb_hiwat - sb->sb_cc), | |
1701 | (int)(sb->sb_mbmax - sb->sb_mbcnt)); | |
1702 | if (space < 0) | |
1703 | space = 0; | |
1704 | ||
1705 | return space; | |
0b4e3aa0 A |
1706 | } |
1707 | ||
1708 | /* do we have to send all at once on a socket? */ | |
1709 | int | |
1710 | sosendallatonce(struct socket *so) | |
1711 | { | |
2d21ac55 | 1712 | return (so->so_proto->pr_flags & PR_ATOMIC); |
0b4e3aa0 A |
1713 | } |
1714 | ||
1715 | /* can we read something from so? */ | |
1716 | int | |
1717 | soreadable(struct socket *so) | |
1718 | { | |
2d21ac55 A |
1719 | return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || |
1720 | (so->so_state & SS_CANTRCVMORE) || | |
1721 | so->so_comp.tqh_first || so->so_error); | |
0b4e3aa0 A |
1722 | } |
1723 | ||
1724 | /* can we write something to so? */ | |
1725 | ||
1726 | int | |
1727 | sowriteable(struct socket *so) | |
1728 | { | |
316670eb A |
1729 | return ((!so_wait_for_if_feedback(so) && |
1730 | sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && | |
1731 | ((so->so_state & SS_ISCONNECTED) || | |
1732 | (so->so_proto->pr_flags & PR_CONNREQUIRED) == 0)) || | |
2d21ac55 A |
1733 | (so->so_state & SS_CANTSENDMORE) || |
1734 | so->so_error); | |
0b4e3aa0 A |
1735 | } |
1736 | ||
1737 | /* adjust counters in sb reflecting allocation of m */ | |
1738 | ||
1739 | void | |
1740 | sballoc(struct sockbuf *sb, struct mbuf *m) | |
1741 | { | |
316670eb | 1742 | u_int32_t cnt = 1; |
0b4e3aa0 | 1743 | sb->sb_cc += m->m_len; |
2d21ac55 A |
1744 | if (m->m_type != MT_DATA && m->m_type != MT_HEADER && |
1745 | m->m_type != MT_OOBDATA) | |
1746 | sb->sb_ctl += m->m_len; | |
0b4e3aa0 | 1747 | sb->sb_mbcnt += MSIZE; |
2d21ac55 A |
1748 | |
1749 | if (m->m_flags & M_EXT) { | |
0b4e3aa0 | 1750 | sb->sb_mbcnt += m->m_ext.ext_size; |
316670eb | 1751 | cnt += (m->m_ext.ext_size >> MSIZESHIFT) ; |
2d21ac55 | 1752 | } |
316670eb A |
1753 | OSAddAtomic(cnt, &total_sbmb_cnt); |
1754 | VERIFY(total_sbmb_cnt > 0); | |
0b4e3aa0 A |
1755 | } |
1756 | ||
1757 | /* adjust counters in sb reflecting freeing of m */ | |
1758 | void | |
1759 | sbfree(struct sockbuf *sb, struct mbuf *m) | |
1760 | { | |
2d21ac55 | 1761 | int cnt = -1; |
316670eb | 1762 | |
2d21ac55 A |
1763 | sb->sb_cc -= m->m_len; |
1764 | if (m->m_type != MT_DATA && m->m_type != MT_HEADER && | |
1765 | m->m_type != MT_OOBDATA) | |
1766 | sb->sb_ctl -= m->m_len; | |
0b4e3aa0 | 1767 | sb->sb_mbcnt -= MSIZE; |
2d21ac55 | 1768 | if (m->m_flags & M_EXT) { |
0b4e3aa0 | 1769 | sb->sb_mbcnt -= m->m_ext.ext_size; |
316670eb | 1770 | cnt -= (m->m_ext.ext_size >> MSIZESHIFT) ; |
2d21ac55 | 1771 | } |
316670eb A |
1772 | OSAddAtomic(cnt, &total_sbmb_cnt); |
1773 | VERIFY(total_sbmb_cnt >= 0); | |
0b4e3aa0 A |
1774 | } |
1775 | ||
1776 | /* | |
1777 | * Set lock on sockbuf sb; sleep if lock is already held. | |
1778 | * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. | |
1779 | * Returns error without lock if sleep is interrupted. | |
2d21ac55 A |
1780 | * |
1781 | * Returns: 0 Success | |
1782 | * EWOULDBLOCK | |
1783 | * sb_lock:EINTR | |
0b4e3aa0 A |
1784 | */ |
1785 | int | |
1786 | sblock(struct sockbuf *sb, int wf) | |
1787 | { | |
6601e61a A |
1788 | int error = 0; |
1789 | ||
1790 | if (sb->sb_flags & SB_LOCK) | |
1791 | error = (wf == M_WAIT) ? sb_lock(sb) : EWOULDBLOCK; | |
1792 | else | |
1793 | sb->sb_flags |= SB_LOCK; | |
1794 | ||
1795 | return (error); | |
0b4e3aa0 A |
1796 | } |
1797 | ||
1798 | /* release lock on sockbuf sb */ | |
1799 | void | |
91447636 | 1800 | sbunlock(struct sockbuf *sb, int keeplocked) |
0b4e3aa0 | 1801 | { |
91447636 | 1802 | struct socket *so = sb->sb_so; |
b0d623f7 | 1803 | void *lr_saved; |
91447636 A |
1804 | lck_mtx_t *mutex_held; |
1805 | ||
b0d623f7 | 1806 | lr_saved = __builtin_return_address(0); |
91447636 | 1807 | |
2d21ac55 | 1808 | sb->sb_flags &= ~SB_LOCK; |
91447636 | 1809 | |
2d21ac55 A |
1810 | if (sb->sb_flags & SB_WANT) { |
1811 | sb->sb_flags &= ~SB_WANT; | |
b0d623f7 A |
1812 | if (so->so_usecount < 0) { |
1813 | panic("sbunlock: b4 wakeup so=%p ref=%d lr=%p " | |
1814 | "sb_flags=%x lrh= %s\n", sb->sb_so, so->so_usecount, | |
1815 | lr_saved, sb->sb_flags, solockhistory_nr(so)); | |
1816 | /* NOTREACHED */ | |
1817 | } | |
2d21ac55 A |
1818 | wakeup((caddr_t)&(sb)->sb_flags); |
1819 | } | |
91447636 | 1820 | if (keeplocked == 0) { /* unlock on exit */ |
b0d623f7 | 1821 | if (so->so_proto->pr_getlock != NULL) |
0c530ab8 | 1822 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
b0d623f7 | 1823 | else |
0c530ab8 | 1824 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
b0d623f7 | 1825 | |
0c530ab8 A |
1826 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); |
1827 | ||
91447636 A |
1828 | so->so_usecount--; |
1829 | if (so->so_usecount < 0) | |
b0d623f7 A |
1830 | panic("sbunlock: unlock on exit so=%p ref=%d lr=%p " |
1831 | "sb_flags=%x lrh= %s\n", so, so->so_usecount, lr_saved, | |
1832 | sb->sb_flags, solockhistory_nr(so)); | |
1833 | so->unlock_lr[so->next_unlock_lr] = lr_saved; | |
0c530ab8 | 1834 | so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX; |
91447636 A |
1835 | lck_mtx_unlock(mutex_held); |
1836 | } | |
0b4e3aa0 A |
1837 | } |
1838 | ||
1839 | void | |
2d21ac55 | 1840 | sorwakeup(struct socket *so) |
0b4e3aa0 | 1841 | { |
2d21ac55 A |
1842 | if (sb_notify(&so->so_rcv)) |
1843 | sowakeup(so, &so->so_rcv); | |
0b4e3aa0 A |
1844 | } |
1845 | ||
1846 | void | |
2d21ac55 | 1847 | sowwakeup(struct socket *so) |
0b4e3aa0 | 1848 | { |
2d21ac55 A |
1849 | if (sb_notify(&so->so_snd)) |
1850 | sowakeup(so, &so->so_snd); | |
0b4e3aa0 | 1851 | } |
316670eb A |
1852 | |
1853 | void | |
1854 | soevent(struct socket *so, long hint) | |
1855 | { | |
1856 | if (so->so_flags & SOF_KNOTE) | |
1857 | KNOTE(&so->so_klist, hint); | |
1858 | } | |
1859 | ||
2d21ac55 | 1860 | #endif /* __APPLE__ */ |
0b4e3aa0 | 1861 | |
1c79356b A |
1862 | /* |
1863 | * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. | |
1864 | */ | |
1865 | struct sockaddr * | |
2d21ac55 | 1866 | dup_sockaddr(struct sockaddr *sa, int canwait) |
1c79356b A |
1867 | { |
1868 | struct sockaddr *sa2; | |
1869 | ||
2d21ac55 A |
1870 | MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, |
1871 | canwait ? M_WAITOK : M_NOWAIT); | |
1c79356b A |
1872 | if (sa2) |
1873 | bcopy(sa, sa2, sa->sa_len); | |
2d21ac55 | 1874 | return (sa2); |
1c79356b A |
1875 | } |
1876 | ||
1877 | /* | |
1878 | * Create an external-format (``xsocket'') structure using the information | |
1879 | * in the kernel-format socket structure pointed to by so. This is done | |
1880 | * to reduce the spew of irrelevant information over this interface, | |
1881 | * to isolate user code from changes in the kernel structure, and | |
1882 | * potentially to provide information-hiding if we decide that | |
1883 | * some of this information should be hidden from users. | |
1884 | */ | |
1885 | void | |
1886 | sotoxsocket(struct socket *so, struct xsocket *xso) | |
1887 | { | |
2d21ac55 | 1888 | xso->xso_len = sizeof (*xso); |
316670eb | 1889 | xso->xso_so = (_XSOCKET_PTR(struct socket *))VM_KERNEL_ADDRPERM(so); |
1c79356b | 1890 | xso->so_type = so->so_type; |
316670eb | 1891 | xso->so_options = (short)(so->so_options & 0xffff); |
1c79356b A |
1892 | xso->so_linger = so->so_linger; |
1893 | xso->so_state = so->so_state; | |
316670eb | 1894 | xso->so_pcb = (_XSOCKET_PTR(caddr_t))VM_KERNEL_ADDRPERM(so->so_pcb); |
91447636 A |
1895 | if (so->so_proto) { |
1896 | xso->xso_protocol = so->so_proto->pr_protocol; | |
1897 | xso->xso_family = so->so_proto->pr_domain->dom_family; | |
2d21ac55 | 1898 | } else { |
91447636 | 1899 | xso->xso_protocol = xso->xso_family = 0; |
2d21ac55 | 1900 | } |
1c79356b A |
1901 | xso->so_qlen = so->so_qlen; |
1902 | xso->so_incqlen = so->so_incqlen; | |
1903 | xso->so_qlimit = so->so_qlimit; | |
1904 | xso->so_timeo = so->so_timeo; | |
1905 | xso->so_error = so->so_error; | |
1906 | xso->so_pgid = so->so_pgid; | |
1907 | xso->so_oobmark = so->so_oobmark; | |
1908 | sbtoxsockbuf(&so->so_snd, &xso->so_snd); | |
1909 | sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); | |
316670eb | 1910 | xso->so_uid = kauth_cred_getuid(so->so_cred); |
1c79356b A |
1911 | } |
1912 | ||
b0d623f7 A |
1913 | |
1914 | #if !CONFIG_EMBEDDED | |
1915 | ||
1916 | void | |
1917 | sotoxsocket64(struct socket *so, struct xsocket64 *xso) | |
1918 | { | |
1919 | xso->xso_len = sizeof (*xso); | |
316670eb | 1920 | xso->xso_so = (u_int64_t)VM_KERNEL_ADDRPERM(so); |
b0d623f7 | 1921 | xso->so_type = so->so_type; |
316670eb | 1922 | xso->so_options = (short)(so->so_options & 0xffff); |
b0d623f7 A |
1923 | xso->so_linger = so->so_linger; |
1924 | xso->so_state = so->so_state; | |
316670eb | 1925 | xso->so_pcb = (u_int64_t)VM_KERNEL_ADDRPERM(so->so_pcb); |
b0d623f7 A |
1926 | if (so->so_proto) { |
1927 | xso->xso_protocol = so->so_proto->pr_protocol; | |
1928 | xso->xso_family = so->so_proto->pr_domain->dom_family; | |
1929 | } else { | |
1930 | xso->xso_protocol = xso->xso_family = 0; | |
1931 | } | |
1932 | xso->so_qlen = so->so_qlen; | |
1933 | xso->so_incqlen = so->so_incqlen; | |
1934 | xso->so_qlimit = so->so_qlimit; | |
1935 | xso->so_timeo = so->so_timeo; | |
1936 | xso->so_error = so->so_error; | |
1937 | xso->so_pgid = so->so_pgid; | |
1938 | xso->so_oobmark = so->so_oobmark; | |
1939 | sbtoxsockbuf(&so->so_snd, &xso->so_snd); | |
1940 | sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); | |
316670eb | 1941 | xso->so_uid = kauth_cred_getuid(so->so_cred); |
b0d623f7 A |
1942 | } |
1943 | ||
1944 | #endif /* !CONFIG_EMBEDDED */ | |
1945 | ||
1c79356b A |
1946 | /* |
1947 | * This does the same for sockbufs. Note that the xsockbuf structure, | |
1948 | * since it is always embedded in a socket, does not include a self | |
1949 | * pointer nor a length. We make this entry point public in case | |
1950 | * some other mechanism needs it. | |
1951 | */ | |
1952 | void | |
1953 | sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) | |
1954 | { | |
1955 | xsb->sb_cc = sb->sb_cc; | |
1956 | xsb->sb_hiwat = sb->sb_hiwat; | |
1957 | xsb->sb_mbcnt = sb->sb_mbcnt; | |
1958 | xsb->sb_mbmax = sb->sb_mbmax; | |
1959 | xsb->sb_lowat = sb->sb_lowat; | |
1960 | xsb->sb_flags = sb->sb_flags; | |
b0d623f7 | 1961 | xsb->sb_timeo = (short) |
2d21ac55 | 1962 | (sb->sb_timeo.tv_sec * hz) + sb->sb_timeo.tv_usec / tick; |
91447636 A |
1963 | if (xsb->sb_timeo == 0 && sb->sb_timeo.tv_usec != 0) |
1964 | xsb->sb_timeo = 1; | |
1c79356b A |
1965 | } |
1966 | ||
316670eb A |
1967 | /* |
1968 | * Based on the policy set by an all knowing decison maker, throttle sockets | |
1969 | * that either have been marked as belonging to "background" process. | |
1970 | */ | |
d1ecb069 | 1971 | int |
316670eb | 1972 | soisthrottled(struct socket *so) |
d1ecb069 | 1973 | { |
316670eb A |
1974 | /* |
1975 | * On non-embedded, we rely on implicit throttling by the application, | |
1976 | * as we're missing the system-wide "decision maker". | |
1977 | */ | |
1978 | return ( | |
1979 | #if CONFIG_EMBEDDED | |
1980 | net_io_policy_throttled && | |
1981 | #endif /* CONFIG_EMBEDDED */ | |
1982 | (so->so_traffic_mgt_flags & TRAFFIC_MGT_SO_BACKGROUND)); | |
d1ecb069 A |
1983 | } |
1984 | ||
316670eb A |
1985 | int |
1986 | soisprivilegedtraffic(struct socket *so) | |
1987 | { | |
1988 | return (so->so_flags & SOF_PRIVILEGED_TRAFFIC_CLASS); | |
1989 | } | |
d41d1dae | 1990 | |
1c79356b A |
1991 | /* |
1992 | * Here is the definition of some of the basic objects in the kern.ipc | |
1993 | * branch of the MIB. | |
1994 | */ | |
6d2010ae | 1995 | SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW|CTLFLAG_LOCKED|CTLFLAG_ANYBODY, 0, "IPC"); |
1c79356b | 1996 | |
b0d623f7 A |
1997 | /* Check that the maximum socket buffer size is within a range */ |
1998 | ||
1999 | static int | |
2000 | sysctl_sb_max(__unused struct sysctl_oid *oidp, __unused void *arg1, | |
2001 | __unused int arg2, struct sysctl_req *req) | |
2002 | { | |
2003 | u_int32_t new_value; | |
2004 | int changed = 0; | |
2005 | int error = sysctl_io_number(req, sb_max, sizeof(u_int32_t), &new_value, | |
2006 | &changed); | |
2007 | if (!error && changed) { | |
2008 | if (new_value > LOW_SB_MAX && | |
2009 | new_value <= high_sb_max ) { | |
2010 | sb_max = new_value; | |
2011 | } else { | |
2012 | error = ERANGE; | |
2013 | } | |
2014 | } | |
2015 | return error; | |
2016 | } | |
2017 | ||
316670eb A |
2018 | static int |
2019 | sysctl_io_policy_throttled SYSCTL_HANDLER_ARGS | |
2020 | { | |
2021 | #pragma unused(arg1, arg2) | |
2022 | int i, err; | |
2023 | ||
2024 | i = net_io_policy_throttled; | |
2025 | ||
2026 | err = sysctl_handle_int(oidp, &i, 0, req); | |
2027 | if (err != 0 || req->newptr == USER_ADDR_NULL) | |
2028 | return (err); | |
2029 | ||
2030 | if (i != net_io_policy_throttled) | |
2031 | SOTHROTTLELOG(("throttle: network IO policy throttling is " | |
2032 | "now %s\n", i ? "ON" : "OFF")); | |
2033 | ||
2034 | net_io_policy_throttled = i; | |
2035 | ||
2036 | return (err); | |
2037 | } | |
2038 | ||
6d2010ae | 2039 | SYSCTL_PROC(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, |
b0d623f7 | 2040 | &sb_max, 0, &sysctl_sb_max, "IU", "Maximum socket buffer size"); |
1c79356b | 2041 | |
6d2010ae | 2042 | SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD | CTLFLAG_LOCKED, |
9bccf70c | 2043 | &maxsockets, 0, "Maximum number of sockets avaliable"); |
6d2010ae | 2044 | SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW | CTLFLAG_LOCKED, |
2d21ac55 | 2045 | &sb_efficiency, 0, ""); |
6d2010ae | 2046 | SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD | CTLFLAG_LOCKED, |
2d21ac55 | 2047 | &nmbclusters, 0, ""); |
6d2010ae A |
2048 | SYSCTL_INT(_kern_ipc, OID_AUTO, njcl, CTLFLAG_RD | CTLFLAG_LOCKED, &njcl, 0, ""); |
2049 | SYSCTL_INT(_kern_ipc, OID_AUTO, njclbytes, CTLFLAG_RD | CTLFLAG_LOCKED, &njclbytes, 0, ""); | |
2050 | SYSCTL_INT(_kern_ipc, KIPC_SOQLIMITCOMPAT, soqlimitcompat, CTLFLAG_RW | CTLFLAG_LOCKED, | |
2d21ac55 | 2051 | &soqlimitcompat, 1, "Enable socket queue limit compatibility"); |
6d2010ae | 2052 | SYSCTL_INT(_kern_ipc, OID_AUTO, soqlencomp, CTLFLAG_RW | CTLFLAG_LOCKED, |
2d21ac55 | 2053 | &soqlencomp, 0, "Listen backlog represents only complete queue"); |
316670eb A |
2054 | |
2055 | SYSCTL_NODE(_kern_ipc, OID_AUTO, io_policy, CTLFLAG_RW, 0, "network IO policy"); | |
2056 | ||
2057 | SYSCTL_PROC(_kern_ipc_io_policy, OID_AUTO, throttled, | |
2058 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &net_io_policy_throttled, 0, | |
2059 | sysctl_io_policy_throttled, "I", ""); |