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