]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
e5568f75 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */ | |
23 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
24 | /* | |
25 | * Copyright (c) 1982, 1986, 1988, 1990, 1993 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that the following conditions | |
30 | * are met: | |
31 | * 1. Redistributions of source code must retain the above copyright | |
32 | * notice, this list of conditions and the following disclaimer. | |
33 | * 2. Redistributions in binary form must reproduce the above copyright | |
34 | * notice, this list of conditions and the following disclaimer in the | |
35 | * documentation and/or other materials provided with the distribution. | |
36 | * 3. All advertising materials mentioning features or use of this software | |
37 | * must display the following acknowledgement: | |
38 | * This product includes software developed by the University of | |
39 | * California, Berkeley and its contributors. | |
40 | * 4. Neither the name of the University nor the names of its contributors | |
41 | * may be used to endorse or promote products derived from this software | |
42 | * without specific prior written permission. | |
43 | * | |
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
54 | * SUCH DAMAGE. | |
55 | * | |
56 | * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 | |
9bccf70c | 57 | * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $ |
1c79356b A |
58 | */ |
59 | ||
60 | #include <sys/param.h> | |
61 | #include <sys/systm.h> | |
62 | #include <sys/domain.h> | |
63 | #include <sys/kernel.h> | |
91447636 A |
64 | #include <sys/proc_internal.h> |
65 | #include <sys/kauth.h> | |
1c79356b A |
66 | #include <sys/malloc.h> |
67 | #include <sys/mbuf.h> | |
68 | #include <sys/protosw.h> | |
69 | #include <sys/stat.h> | |
70 | #include <sys/socket.h> | |
71 | #include <sys/socketvar.h> | |
72 | #include <sys/signalvar.h> | |
73 | #include <sys/sysctl.h> | |
74 | #include <sys/ev.h> | |
91447636 A |
75 | #include <kern/locks.h> |
76 | #include <net/route.h> | |
77 | #include <netinet/in.h> | |
78 | #include <netinet/in_pcb.h> | |
fa4905b1 A |
79 | #include <sys/kdebug.h> |
80 | ||
81 | #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4) | |
82 | #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5) | |
83 | ||
84 | ||
1c79356b A |
85 | /* |
86 | * Primitive routines for operating on sockets and socket buffers | |
87 | */ | |
88 | ||
89 | u_long sb_max = SB_MAX; /* XXX should be static */ | |
90 | ||
91 | static u_long sb_efficiency = 8; /* parameter for sbreserve() */ | |
92 | ||
1c79356b A |
93 | /* |
94 | * Procedures to manipulate state flags of socket | |
95 | * and do appropriate wakeups. Normal sequence from the | |
96 | * active (originating) side is that soisconnecting() is | |
97 | * called during processing of connect() call, | |
98 | * resulting in an eventual call to soisconnected() if/when the | |
99 | * connection is established. When the connection is torn down | |
9bccf70c | 100 | * soisdisconnecting() is called during processing of disconnect() call, |
1c79356b A |
101 | * and soisdisconnected() is called when the connection to the peer |
102 | * is totally severed. The semantics of these routines are such that | |
103 | * connectionless protocols can call soisconnected() and soisdisconnected() | |
104 | * only, bypassing the in-progress calls when setting up a ``connection'' | |
105 | * takes no time. | |
106 | * | |
107 | * From the passive side, a socket is created with | |
e3027f41 A |
108 | * two queues of sockets: so_incomp for connections in progress |
109 | * and so_comp for connections already made and awaiting user acceptance. | |
9bccf70c | 110 | * As a protocol is preparing incoming connections, it creates a socket |
e3027f41 | 111 | * structure queued on so_incomp by calling sonewconn(). When the connection |
1c79356b | 112 | * is established, soisconnected() is called, and transfers the |
e3027f41 | 113 | * socket structure to so_comp, making it available to accept(). |
1c79356b | 114 | * |
9bccf70c | 115 | * If a socket is closed with sockets on either |
e3027f41 | 116 | * so_incomp or so_comp, these sockets are dropped. |
9bccf70c | 117 | * |
1c79356b A |
118 | * If higher level protocols are implemented in |
119 | * the kernel, the wakeups done here will sometimes | |
120 | * cause software-interrupt process scheduling. | |
121 | */ | |
1c79356b A |
122 | void |
123 | soisconnecting(so) | |
124 | register struct socket *so; | |
125 | { | |
126 | ||
127 | so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); | |
128 | so->so_state |= SS_ISCONNECTING; | |
91447636 A |
129 | |
130 | sflt_notify(so, sock_evt_connecting, NULL); | |
1c79356b A |
131 | } |
132 | ||
133 | void | |
134 | soisconnected(so) | |
9bccf70c A |
135 | struct socket *so; |
136 | { | |
137 | struct socket *head = so->so_head; | |
1c79356b A |
138 | |
139 | so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); | |
140 | so->so_state |= SS_ISCONNECTED; | |
91447636 A |
141 | |
142 | sflt_notify(so, sock_evt_connected, NULL); | |
143 | ||
1c79356b | 144 | if (head && (so->so_state & SS_INCOMP)) { |
91447636 A |
145 | if (head->so_proto->pr_getlock != NULL) |
146 | socket_lock(head, 1); | |
147 | postevent(head, 0, EV_RCONN); | |
1c79356b A |
148 | TAILQ_REMOVE(&head->so_incomp, so, so_list); |
149 | head->so_incqlen--; | |
150 | so->so_state &= ~SS_INCOMP; | |
151 | TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); | |
152 | so->so_state |= SS_COMP; | |
153 | sorwakeup(head); | |
91447636 A |
154 | wakeup_one((caddr_t)&head->so_timeo); |
155 | if (head->so_proto->pr_getlock != NULL) | |
156 | socket_unlock(head, 1); | |
1c79356b | 157 | } else { |
91447636 | 158 | postevent(so, 0, EV_WCONN); |
1c79356b A |
159 | wakeup((caddr_t)&so->so_timeo); |
160 | sorwakeup(so); | |
161 | sowwakeup(so); | |
162 | } | |
163 | } | |
164 | ||
165 | void | |
166 | soisdisconnecting(so) | |
167 | register struct socket *so; | |
9bccf70c | 168 | { |
1c79356b A |
169 | so->so_state &= ~SS_ISCONNECTING; |
170 | so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); | |
91447636 | 171 | sflt_notify(so, sock_evt_disconnecting, NULL); |
1c79356b A |
172 | wakeup((caddr_t)&so->so_timeo); |
173 | sowwakeup(so); | |
174 | sorwakeup(so); | |
175 | } | |
176 | ||
177 | void | |
178 | soisdisconnected(so) | |
179 | register struct socket *so; | |
9bccf70c | 180 | { |
1c79356b | 181 | so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); |
9bccf70c | 182 | so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); |
91447636 | 183 | sflt_notify(so, sock_evt_disconnected, NULL); |
1c79356b A |
184 | wakeup((caddr_t)&so->so_timeo); |
185 | sowwakeup(so); | |
186 | sorwakeup(so); | |
187 | } | |
188 | ||
189 | /* | |
190 | * Return a random connection that hasn't been serviced yet and | |
191 | * is eligible for discard. There is a one in qlen chance that | |
192 | * we will return a null, saying that there are no dropable | |
193 | * requests. In this case, the protocol specific code should drop | |
194 | * the new request. This insures fairness. | |
195 | * | |
196 | * This may be used in conjunction with protocol specific queue | |
197 | * congestion routines. | |
198 | */ | |
199 | struct socket * | |
200 | sodropablereq(head) | |
201 | register struct socket *head; | |
202 | { | |
91447636 | 203 | struct socket *so, *sonext = NULL; |
1c79356b A |
204 | unsigned int i, j, qlen; |
205 | static int rnd; | |
206 | static struct timeval old_runtime; | |
207 | static unsigned int cur_cnt, old_cnt; | |
208 | struct timeval tv; | |
209 | ||
210 | microtime(&tv); | |
211 | if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) { | |
212 | old_runtime = tv; | |
213 | old_cnt = cur_cnt / i; | |
214 | cur_cnt = 0; | |
215 | } | |
216 | ||
217 | so = TAILQ_FIRST(&head->so_incomp); | |
218 | if (!so) | |
91447636 | 219 | return (NULL); |
1c79356b A |
220 | |
221 | qlen = head->so_incqlen; | |
222 | if (++cur_cnt > qlen || old_cnt > qlen) { | |
223 | rnd = (314159 * rnd + 66329) & 0xffff; | |
224 | j = ((qlen + 1) * rnd) >> 16; | |
91447636 A |
225 | //###LD To clean up |
226 | while (j-- && so) { | |
227 | // if (in_pcb_checkstate(so->so_pcb, WNT_ACQUIRE, 0) != WNT_STOPUSING) { | |
228 | socket_lock(so, 1); | |
229 | sonext = TAILQ_NEXT(so, so_list); | |
230 | // in_pcb_check_state(so->so_pcb, WNT_RELEASE, 0); | |
231 | socket_unlock(so, 1); | |
232 | so = sonext; | |
233 | } | |
1c79356b A |
234 | } |
235 | ||
91447636 A |
236 | // if (in_pcb_checkstate(so->so_pcb, WNT_ACQUIRE, 0) == WNT_STOPUSING) |
237 | // return (NULL); | |
238 | // else | |
239 | return (so); | |
1c79356b A |
240 | } |
241 | ||
242 | /* | |
243 | * When an attempt at a new connection is noted on a socket | |
244 | * which accepts connections, sonewconn is called. If the | |
245 | * connection is possible (subject to space constraints, etc.) | |
246 | * then we allocate a new structure, propoerly linked into the | |
247 | * data structure of the original socket, and return this. | |
248 | * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. | |
249 | */ | |
91447636 A |
250 | static struct socket * |
251 | sonewconn_internal(head, connstatus) | |
1c79356b A |
252 | register struct socket *head; |
253 | int connstatus; | |
9bccf70c A |
254 | { |
255 | int error = 0; | |
1c79356b | 256 | register struct socket *so; |
91447636 A |
257 | lck_mtx_t *mutex_held; |
258 | ||
259 | if (head->so_proto->pr_getlock != NULL) | |
260 | mutex_held = (*head->so_proto->pr_getlock)(head, 0); | |
261 | else | |
262 | mutex_held = head->so_proto->pr_domain->dom_mtx; | |
263 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
1c79356b A |
264 | |
265 | if (head->so_qlen > 3 * head->so_qlimit / 2) | |
266 | return ((struct socket *)0); | |
0b4e3aa0 | 267 | so = soalloc(1, head->so_proto->pr_domain->dom_family, head->so_type); |
1c79356b A |
268 | if (so == NULL) |
269 | return ((struct socket *)0); | |
9bccf70c A |
270 | /* check if head was closed during the soalloc */ |
271 | if (head->so_proto == NULL) { | |
272 | sodealloc(so); | |
273 | return ((struct socket *)0); | |
1c79356b A |
274 | } |
275 | ||
276 | so->so_head = head; | |
277 | so->so_type = head->so_type; | |
278 | so->so_options = head->so_options &~ SO_ACCEPTCONN; | |
279 | so->so_linger = head->so_linger; | |
280 | so->so_state = head->so_state | SS_NOFDREF; | |
281 | so->so_proto = head->so_proto; | |
282 | so->so_timeo = head->so_timeo; | |
283 | so->so_pgid = head->so_pgid; | |
284 | so->so_uid = head->so_uid; | |
91447636 | 285 | so->so_usecount = 1; |
1c79356b | 286 | |
91447636 A |
287 | if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) { |
288 | sflt_termsock(so); | |
9bccf70c A |
289 | sodealloc(so); |
290 | return ((struct socket *)0); | |
291 | } | |
292 | ||
91447636 A |
293 | /* |
294 | * Must be done with head unlocked to avoid deadlock with pcb list | |
295 | */ | |
296 | socket_unlock(head, 0); | |
297 | if (((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL) != 0) || error) { | |
298 | sflt_termsock(so); | |
1c79356b | 299 | sodealloc(so); |
91447636 | 300 | socket_lock(head, 0); |
1c79356b A |
301 | return ((struct socket *)0); |
302 | } | |
91447636 | 303 | socket_lock(head, 0); |
9bccf70c | 304 | #ifdef __APPLE__ |
1c79356b | 305 | so->so_proto->pr_domain->dom_refs++; |
9bccf70c | 306 | #endif |
1c79356b A |
307 | |
308 | if (connstatus) { | |
309 | TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); | |
310 | so->so_state |= SS_COMP; | |
311 | } else { | |
312 | TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); | |
313 | so->so_state |= SS_INCOMP; | |
314 | head->so_incqlen++; | |
315 | } | |
316 | head->so_qlen++; | |
9bccf70c | 317 | #ifdef __APPLE__ |
1c79356b A |
318 | so->so_rcv.sb_so = so->so_snd.sb_so = so; |
319 | TAILQ_INIT(&so->so_evlist); | |
91447636 A |
320 | |
321 | /* Attach socket filters for this protocol */ | |
322 | sflt_initsock(so); | |
9bccf70c | 323 | #endif |
91447636 A |
324 | if (connstatus) { |
325 | so->so_state |= connstatus; | |
326 | sorwakeup(head); | |
327 | wakeup((caddr_t)&head->so_timeo); | |
328 | } | |
1c79356b A |
329 | return (so); |
330 | } | |
331 | ||
91447636 A |
332 | |
333 | struct socket * | |
334 | sonewconn( | |
335 | struct socket *head, | |
336 | int connstatus, | |
337 | const struct sockaddr *from) | |
338 | { | |
339 | int error = 0; | |
340 | struct socket_filter_entry *filter; | |
341 | int filtered = 0; | |
342 | ||
343 | error = 0; | |
344 | for (filter = head->so_filt; filter && (error == 0); | |
345 | filter = filter->sfe_next_onsocket) { | |
346 | if (filter->sfe_filter->sf_filter.sf_connect_in) { | |
347 | if (filtered == 0) { | |
348 | filtered = 1; | |
349 | sflt_use(head); | |
350 | socket_unlock(head, 0); | |
351 | } | |
352 | error = filter->sfe_filter->sf_filter.sf_connect_in( | |
353 | filter->sfe_cookie, head, from); | |
354 | } | |
355 | } | |
356 | if (filtered != 0) { | |
357 | socket_lock(head, 0); | |
358 | sflt_unuse(head); | |
359 | } | |
360 | ||
361 | if (error) { | |
362 | return NULL; | |
363 | } | |
364 | ||
365 | return sonewconn_internal(head, connstatus); | |
366 | } | |
367 | ||
1c79356b A |
368 | /* |
369 | * Socantsendmore indicates that no more data will be sent on the | |
370 | * socket; it would normally be applied to a socket when the user | |
371 | * informs the system that no more data is to be sent, by the protocol | |
372 | * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data | |
373 | * will be received, and will normally be applied to the socket by a | |
374 | * protocol when it detects that the peer will send no more data. | |
375 | * Data queued for reading in the socket may yet be read. | |
376 | */ | |
377 | ||
378 | void | |
379 | socantsendmore(so) | |
380 | struct socket *so; | |
9bccf70c | 381 | { |
1c79356b | 382 | so->so_state |= SS_CANTSENDMORE; |
91447636 | 383 | sflt_notify(so, sock_evt_cantsendmore, NULL); |
1c79356b A |
384 | sowwakeup(so); |
385 | } | |
386 | ||
387 | void | |
388 | socantrcvmore(so) | |
389 | struct socket *so; | |
9bccf70c | 390 | { |
1c79356b | 391 | so->so_state |= SS_CANTRCVMORE; |
91447636 | 392 | sflt_notify(so, sock_evt_cantrecvmore, NULL); |
1c79356b A |
393 | sorwakeup(so); |
394 | } | |
395 | ||
396 | /* | |
397 | * Wait for data to arrive at/drain from a socket buffer. | |
398 | */ | |
399 | int | |
400 | sbwait(sb) | |
401 | struct sockbuf *sb; | |
402 | { | |
91447636 A |
403 | int error = 0, lr, lr_saved; |
404 | struct socket *so = sb->sb_so; | |
405 | lck_mtx_t *mutex_held; | |
406 | struct timespec ts; | |
407 | ||
408 | #ifdef __ppc__ | |
409 | __asm__ volatile("mflr %0" : "=r" (lr)); | |
410 | lr_saved = lr; | |
411 | #endif | |
412 | ||
413 | ||
414 | if (so->so_proto->pr_getlock != NULL) | |
415 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); | |
416 | else | |
417 | mutex_held = so->so_proto->pr_domain->dom_mtx; | |
1c79356b A |
418 | |
419 | sb->sb_flags |= SB_WAIT; | |
91447636 A |
420 | |
421 | if (so->so_usecount < 1) | |
422 | panic("sbwait: so=%x refcount=%d\n", so, so->so_usecount); | |
423 | ts.tv_sec = sb->sb_timeo.tv_sec; | |
424 | ts.tv_nsec = sb->sb_timeo.tv_usec * 1000; | |
425 | error = msleep((caddr_t)&sb->sb_cc, mutex_held, | |
426 | (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", | |
427 | &ts); | |
428 | ||
429 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
430 | ||
431 | if (so->so_usecount < 1) | |
432 | panic("sbwait: so=%x refcount=%d\n", so, so->so_usecount); | |
433 | ||
434 | if ((so->so_state & SS_DRAINING)) { | |
435 | error = EBADF; | |
436 | } | |
437 | ||
438 | return (error); | |
1c79356b A |
439 | } |
440 | ||
441 | /* | |
442 | * Lock a sockbuf already known to be locked; | |
443 | * return any error returned from sleep (EINTR). | |
444 | */ | |
445 | int | |
446 | sb_lock(sb) | |
447 | register struct sockbuf *sb; | |
448 | { | |
91447636 A |
449 | struct socket *so = sb->sb_so; |
450 | lck_mtx_t * mutex_held; | |
451 | int error = 0, lr, lr_saved; | |
452 | ||
453 | #ifdef __ppc__ | |
454 | __asm__ volatile("mflr %0" : "=r" (lr)); | |
455 | lr_saved = lr; | |
456 | #endif | |
457 | ||
458 | if (so == NULL) | |
459 | panic("sb_lock: null so back pointer sb=%x\n", sb); | |
1c79356b A |
460 | |
461 | while (sb->sb_flags & SB_LOCK) { | |
462 | sb->sb_flags |= SB_WANT; | |
91447636 A |
463 | if (so->so_proto->pr_getlock != NULL) |
464 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); | |
465 | else | |
466 | mutex_held = so->so_proto->pr_domain->dom_mtx; | |
467 | if (so->so_usecount < 1) | |
468 | panic("sb_lock: so=%x refcount=%d\n", so, so->so_usecount); | |
469 | error = msleep((caddr_t)&sb->sb_flags, mutex_held, | |
470 | (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sblock", 0); | |
471 | if (so->so_usecount < 1) | |
472 | panic("sb_lock: 2 so=%x refcount=%d\n", so, so->so_usecount); | |
473 | if (error) | |
1c79356b A |
474 | return (error); |
475 | } | |
476 | sb->sb_flags |= SB_LOCK; | |
477 | return (0); | |
478 | } | |
479 | ||
480 | /* | |
481 | * Wakeup processes waiting on a socket buffer. | |
482 | * Do asynchronous notification via SIGIO | |
483 | * if the socket has the SS_ASYNC flag set. | |
484 | */ | |
485 | void | |
486 | sowakeup(so, sb) | |
487 | register struct socket *so; | |
488 | register struct sockbuf *sb; | |
489 | { | |
490 | struct proc *p = current_proc(); | |
0b4e3aa0 | 491 | sb->sb_flags &= ~SB_SEL; |
1c79356b | 492 | selwakeup(&sb->sb_sel); |
1c79356b A |
493 | if (sb->sb_flags & SB_WAIT) { |
494 | sb->sb_flags &= ~SB_WAIT; | |
495 | wakeup((caddr_t)&sb->sb_cc); | |
496 | } | |
497 | if (so->so_state & SS_ASYNC) { | |
498 | if (so->so_pgid < 0) | |
499 | gsignal(-so->so_pgid, SIGIO); | |
500 | else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) | |
501 | psignal(p, SIGIO); | |
502 | } | |
91447636 A |
503 | if (sb->sb_flags & SB_KNOTE) { |
504 | KNOTE(&sb->sb_sel.si_note, SO_FILT_HINT_LOCKED); | |
505 | } | |
506 | if (sb->sb_flags & SB_UPCALL) { | |
507 | socket_unlock(so, 0); | |
1c79356b | 508 | (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT); |
91447636 A |
509 | socket_lock(so, 0); |
510 | } | |
1c79356b A |
511 | } |
512 | ||
513 | /* | |
514 | * Socket buffer (struct sockbuf) utility routines. | |
515 | * | |
516 | * Each socket contains two socket buffers: one for sending data and | |
517 | * one for receiving data. Each buffer contains a queue of mbufs, | |
518 | * information about the number of mbufs and amount of data in the | |
519 | * queue, and other fields allowing select() statements and notification | |
520 | * on data availability to be implemented. | |
521 | * | |
522 | * Data stored in a socket buffer is maintained as a list of records. | |
523 | * Each record is a list of mbufs chained together with the m_next | |
524 | * field. Records are chained together with the m_nextpkt field. The upper | |
525 | * level routine soreceive() expects the following conventions to be | |
526 | * observed when placing information in the receive buffer: | |
527 | * | |
528 | * 1. If the protocol requires each message be preceded by the sender's | |
529 | * name, then a record containing that name must be present before | |
530 | * any associated data (mbuf's must be of type MT_SONAME). | |
531 | * 2. If the protocol supports the exchange of ``access rights'' (really | |
532 | * just additional data associated with the message), and there are | |
533 | * ``rights'' to be received, then a record containing this data | |
534 | * should be present (mbuf's must be of type MT_RIGHTS). | |
535 | * 3. If a name or rights record exists, then it must be followed by | |
536 | * a data record, perhaps of zero length. | |
537 | * | |
538 | * Before using a new socket structure it is first necessary to reserve | |
539 | * buffer space to the socket, by calling sbreserve(). This should commit | |
540 | * some of the available buffer space in the system buffer pool for the | |
541 | * socket (currently, it does nothing but enforce limits). The space | |
542 | * should be released by calling sbrelease() when the socket is destroyed. | |
543 | */ | |
544 | ||
545 | int | |
546 | soreserve(so, sndcc, rcvcc) | |
547 | register struct socket *so; | |
548 | u_long sndcc, rcvcc; | |
549 | { | |
1c79356b A |
550 | |
551 | if (sbreserve(&so->so_snd, sndcc) == 0) | |
552 | goto bad; | |
553 | if (sbreserve(&so->so_rcv, rcvcc) == 0) | |
554 | goto bad2; | |
555 | if (so->so_rcv.sb_lowat == 0) | |
556 | so->so_rcv.sb_lowat = 1; | |
557 | if (so->so_snd.sb_lowat == 0) | |
558 | so->so_snd.sb_lowat = MCLBYTES; | |
559 | if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) | |
560 | so->so_snd.sb_lowat = so->so_snd.sb_hiwat; | |
561 | return (0); | |
562 | bad2: | |
9bccf70c | 563 | #ifdef __APPLE__ |
0b4e3aa0 | 564 | selthreadclear(&so->so_snd.sb_sel); |
9bccf70c | 565 | #endif |
1c79356b A |
566 | sbrelease(&so->so_snd); |
567 | bad: | |
568 | return (ENOBUFS); | |
569 | } | |
570 | ||
571 | /* | |
572 | * Allot mbufs to a sockbuf. | |
573 | * Attempt to scale mbmax so that mbcnt doesn't become limiting | |
574 | * if buffering efficiency is near the normal case. | |
575 | */ | |
576 | int | |
577 | sbreserve(sb, cc) | |
578 | struct sockbuf *sb; | |
579 | u_long cc; | |
580 | { | |
581 | if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES)) | |
582 | return (0); | |
583 | sb->sb_hiwat = cc; | |
584 | sb->sb_mbmax = min(cc * sb_efficiency, sb_max); | |
585 | if (sb->sb_lowat > sb->sb_hiwat) | |
586 | sb->sb_lowat = sb->sb_hiwat; | |
587 | return (1); | |
588 | } | |
589 | ||
590 | /* | |
591 | * Free mbufs held by a socket, and reserved mbuf space. | |
592 | */ | |
0b4e3aa0 | 593 | /* WARNING needs to do selthreadclear() before calling this */ |
1c79356b A |
594 | void |
595 | sbrelease(sb) | |
596 | struct sockbuf *sb; | |
597 | { | |
598 | ||
599 | sbflush(sb); | |
9bccf70c A |
600 | sb->sb_hiwat = 0; |
601 | sb->sb_mbmax = 0; | |
602 | ||
1c79356b A |
603 | } |
604 | ||
605 | /* | |
606 | * Routines to add and remove | |
607 | * data from an mbuf queue. | |
608 | * | |
609 | * The routines sbappend() or sbappendrecord() are normally called to | |
610 | * append new mbufs to a socket buffer, after checking that adequate | |
611 | * space is available, comparing the function sbspace() with the amount | |
612 | * of data to be added. sbappendrecord() differs from sbappend() in | |
613 | * that data supplied is treated as the beginning of a new record. | |
614 | * To place a sender's address, optional access rights, and data in a | |
615 | * socket receive buffer, sbappendaddr() should be used. To place | |
616 | * access rights and data in a socket receive buffer, sbappendrights() | |
617 | * should be used. In either case, the new data begins a new record. | |
618 | * Note that unlike sbappend() and sbappendrecord(), these routines check | |
619 | * for the caller that there will be enough space to store the data. | |
620 | * Each fails if there is not enough space, or if it cannot find mbufs | |
621 | * to store additional information in. | |
622 | * | |
623 | * Reliable protocols may use the socket send buffer to hold data | |
624 | * awaiting acknowledgement. Data is normally copied from a socket | |
625 | * send buffer in a protocol with m_copy for output to a peer, | |
626 | * and then removing the data from the socket buffer with sbdrop() | |
627 | * or sbdroprecord() when the data is acknowledged by the peer. | |
628 | */ | |
629 | ||
630 | /* | |
631 | * Append mbuf chain m to the last record in the | |
632 | * socket buffer sb. The additional space associated | |
633 | * the mbuf chain is recorded in sb. Empty mbufs are | |
634 | * discarded and mbufs are compacted where possible. | |
635 | */ | |
91447636 | 636 | int |
1c79356b A |
637 | sbappend(sb, m) |
638 | struct sockbuf *sb; | |
639 | struct mbuf *m; | |
9bccf70c | 640 | { |
91447636 A |
641 | register struct mbuf *n, *sb_first; |
642 | int result = 0; | |
643 | int error = 0; | |
1c79356b | 644 | |
fa4905b1 A |
645 | |
646 | KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_START), sb, m->m_len, 0, 0, 0); | |
647 | ||
1c79356b | 648 | if (m == 0) |
91447636 A |
649 | return 0; |
650 | sb_first = n = sb->sb_mb; | |
9bccf70c | 651 | if (n) { |
1c79356b A |
652 | while (n->m_nextpkt) |
653 | n = n->m_nextpkt; | |
654 | do { | |
655 | if (n->m_flags & M_EOR) { | |
91447636 | 656 | result = sbappendrecord(sb, m); /* XXXXXX!!!! */ |
55e303ae | 657 | KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_END), sb, sb->sb_cc, 0, 0, 0); |
91447636 | 658 | return result; |
1c79356b A |
659 | } |
660 | } while (n->m_next && (n = n->m_next)); | |
661 | } | |
91447636 A |
662 | |
663 | if ((sb->sb_flags & SB_RECV) != 0) { | |
664 | error = sflt_data_in(sb->sb_so, NULL, &m, NULL, 0); | |
665 | if (error) { | |
666 | /* no data was appended, caller should not call sowakeup */ | |
667 | return 0; | |
668 | } | |
669 | } | |
670 | ||
671 | /* 3962537 - sflt_data_in may drop the lock, need to validate state again */ | |
672 | if (sb_first != sb->sb_mb) { | |
673 | n = sb->sb_mb; | |
674 | if (n) { | |
675 | while (n->m_nextpkt) | |
676 | n = n->m_nextpkt; | |
677 | } | |
678 | } | |
679 | ||
680 | result = sbcompress(sb, m, n); | |
fa4905b1 A |
681 | |
682 | KERNEL_DEBUG((DBG_FNC_SBAPPEND | DBG_FUNC_END), sb, sb->sb_cc, 0, 0, 0); | |
91447636 A |
683 | |
684 | return result; | |
1c79356b A |
685 | } |
686 | ||
687 | #ifdef SOCKBUF_DEBUG | |
688 | void | |
689 | sbcheck(sb) | |
690 | register struct sockbuf *sb; | |
691 | { | |
692 | register struct mbuf *m; | |
693 | register struct mbuf *n = 0; | |
694 | register u_long len = 0, mbcnt = 0; | |
91447636 A |
695 | lck_mtx_t *mutex_held; |
696 | ||
697 | if (sb->sb_so->so_proto->pr_getlock != NULL) | |
698 | mutex_held = (*sb->sb_so->so_proto->pr_getlock)(sb->sb_so, 0); | |
699 | else | |
700 | mutex_held = sb->sb_so->so_proto->pr_domain->dom_mtx; | |
701 | ||
702 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
703 | ||
704 | if (sbchecking == 0) | |
705 | return; | |
1c79356b A |
706 | |
707 | for (m = sb->sb_mb; m; m = n) { | |
708 | n = m->m_nextpkt; | |
709 | for (; m; m = m->m_next) { | |
9bccf70c A |
710 | len += m->m_len; |
711 | mbcnt += MSIZE; | |
712 | if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */ | |
713 | mbcnt += m->m_ext.ext_size; | |
714 | } | |
1c79356b | 715 | } |
9bccf70c | 716 | if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { |
91447636 | 717 | panic("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc, |
9bccf70c | 718 | mbcnt, sb->sb_mbcnt); |
9bccf70c | 719 | } |
1c79356b A |
720 | } |
721 | #endif | |
722 | ||
723 | /* | |
724 | * As above, except the mbuf chain | |
725 | * begins a new record. | |
726 | */ | |
91447636 | 727 | int |
1c79356b A |
728 | sbappendrecord(sb, m0) |
729 | register struct sockbuf *sb; | |
730 | register struct mbuf *m0; | |
731 | { | |
732 | register struct mbuf *m; | |
91447636 | 733 | int result = 0; |
9bccf70c | 734 | |
1c79356b | 735 | if (m0 == 0) |
91447636 A |
736 | return 0; |
737 | ||
738 | if ((sb->sb_flags & SB_RECV) != 0) { | |
739 | int error = sflt_data_in(sb->sb_so, NULL, &m0, NULL, sock_data_filt_flag_record); | |
740 | if (error != 0) { | |
741 | if (error != EJUSTRETURN) | |
742 | m_freem(m0); | |
743 | return 0; | |
1c79356b | 744 | } |
1c79356b A |
745 | } |
746 | ||
747 | m = sb->sb_mb; | |
748 | if (m) | |
749 | while (m->m_nextpkt) | |
750 | m = m->m_nextpkt; | |
751 | /* | |
752 | * Put the first mbuf on the queue. | |
753 | * Note this permits zero length records. | |
754 | */ | |
755 | sballoc(sb, m0); | |
756 | if (m) | |
757 | m->m_nextpkt = m0; | |
758 | else | |
759 | sb->sb_mb = m0; | |
760 | m = m0->m_next; | |
761 | m0->m_next = 0; | |
762 | if (m && (m0->m_flags & M_EOR)) { | |
763 | m0->m_flags &= ~M_EOR; | |
764 | m->m_flags |= M_EOR; | |
765 | } | |
91447636 | 766 | return sbcompress(sb, m, m0); |
1c79356b A |
767 | } |
768 | ||
769 | /* | |
770 | * As above except that OOB data | |
771 | * is inserted at the beginning of the sockbuf, | |
772 | * but after any other OOB data. | |
773 | */ | |
91447636 | 774 | int |
1c79356b | 775 | sbinsertoob(sb, m0) |
91447636 A |
776 | struct sockbuf *sb; |
777 | struct mbuf *m0; | |
1c79356b | 778 | { |
91447636 A |
779 | struct mbuf *m; |
780 | struct mbuf **mp; | |
1c79356b A |
781 | |
782 | if (m0 == 0) | |
91447636 A |
783 | return 0; |
784 | ||
785 | if ((sb->sb_flags & SB_RECV) != 0) { | |
786 | int error = sflt_data_in(sb->sb_so, NULL, &m0, NULL, | |
787 | sock_data_filt_flag_oob); | |
788 | ||
789 | if (error) { | |
790 | if (error != EJUSTRETURN) { | |
791 | m_freem(m0); | |
792 | } | |
793 | return 0; | |
1c79356b | 794 | } |
1c79356b A |
795 | } |
796 | ||
797 | for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) { | |
798 | m = *mp; | |
799 | again: | |
800 | switch (m->m_type) { | |
801 | ||
802 | case MT_OOBDATA: | |
803 | continue; /* WANT next train */ | |
804 | ||
805 | case MT_CONTROL: | |
806 | m = m->m_next; | |
807 | if (m) | |
808 | goto again; /* inspect THIS train further */ | |
809 | } | |
810 | break; | |
811 | } | |
812 | /* | |
813 | * Put the first mbuf on the queue. | |
814 | * Note this permits zero length records. | |
815 | */ | |
816 | sballoc(sb, m0); | |
817 | m0->m_nextpkt = *mp; | |
818 | *mp = m0; | |
819 | m = m0->m_next; | |
820 | m0->m_next = 0; | |
821 | if (m && (m0->m_flags & M_EOR)) { | |
822 | m0->m_flags &= ~M_EOR; | |
823 | m->m_flags |= M_EOR; | |
824 | } | |
91447636 | 825 | return sbcompress(sb, m, m0); |
1c79356b A |
826 | } |
827 | ||
828 | /* | |
829 | * Append address and data, and optionally, control (ancillary) data | |
830 | * to the receive queue of a socket. If present, | |
831 | * m0 must include a packet header with total length. | |
832 | * Returns 0 if no space in sockbuf or insufficient mbufs. | |
833 | */ | |
91447636 A |
834 | static int |
835 | sbappendaddr_internal(sb, asa, m0, control) | |
1c79356b A |
836 | register struct sockbuf *sb; |
837 | struct sockaddr *asa; | |
838 | struct mbuf *m0, *control; | |
839 | { | |
840 | register struct mbuf *m, *n; | |
841 | int space = asa->sa_len; | |
1c79356b A |
842 | |
843 | if (m0 && (m0->m_flags & M_PKTHDR) == 0) | |
844 | panic("sbappendaddr"); | |
845 | ||
1c79356b A |
846 | if (m0) |
847 | space += m0->m_pkthdr.len; | |
848 | for (n = control; n; n = n->m_next) { | |
849 | space += n->m_len; | |
850 | if (n->m_next == 0) /* keep pointer to last control buf */ | |
851 | break; | |
852 | } | |
853 | if (space > sbspace(sb)) | |
854 | return (0); | |
855 | if (asa->sa_len > MLEN) | |
856 | return (0); | |
857 | MGET(m, M_DONTWAIT, MT_SONAME); | |
858 | if (m == 0) | |
859 | return (0); | |
860 | m->m_len = asa->sa_len; | |
861 | bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); | |
862 | if (n) | |
863 | n->m_next = m0; /* concatenate data to control */ | |
864 | else | |
865 | control = m0; | |
866 | m->m_next = control; | |
867 | for (n = m; n; n = n->m_next) | |
868 | sballoc(sb, n); | |
869 | n = sb->sb_mb; | |
870 | if (n) { | |
871 | while (n->m_nextpkt) | |
872 | n = n->m_nextpkt; | |
873 | n->m_nextpkt = m; | |
874 | } else | |
875 | sb->sb_mb = m; | |
876 | postevent(0,sb,EV_RWBYTES); | |
877 | return (1); | |
878 | } | |
879 | ||
880 | int | |
91447636 A |
881 | sbappendaddr( |
882 | struct sockbuf* sb, | |
883 | struct sockaddr* asa, | |
884 | struct mbuf *m0, | |
885 | struct mbuf *control, | |
886 | int *error_out) | |
887 | { | |
888 | int result = 0; | |
889 | ||
890 | if (error_out) *error_out = 0; | |
891 | ||
892 | if (m0 && (m0->m_flags & M_PKTHDR) == 0) | |
893 | panic("sbappendaddrorfree"); | |
894 | ||
895 | /* Call socket data in filters */ | |
896 | if ((sb->sb_flags & SB_RECV) != 0) { | |
897 | int error; | |
898 | error = sflt_data_in(sb->sb_so, asa, &m0, &control, 0); | |
899 | if (error) { | |
900 | if (error != EJUSTRETURN) { | |
901 | if (m0) m_freem(m0); | |
902 | if (control) m_freem(control); | |
903 | if (error_out) *error_out = error; | |
904 | } | |
905 | return 0; | |
906 | } | |
907 | } | |
908 | ||
909 | result = sbappendaddr_internal(sb, asa, m0, control); | |
910 | if (result == 0) { | |
911 | if (m0) m_freem(m0); | |
912 | if (control) m_freem(control); | |
913 | if (error_out) *error_out = ENOBUFS; | |
914 | } | |
915 | ||
916 | return result; | |
917 | } | |
918 | ||
919 | static int | |
920 | sbappendcontrol_internal(sb, m0, control) | |
1c79356b A |
921 | struct sockbuf *sb; |
922 | struct mbuf *control, *m0; | |
923 | { | |
924 | register struct mbuf *m, *n; | |
925 | int space = 0; | |
1c79356b A |
926 | |
927 | if (control == 0) | |
928 | panic("sbappendcontrol"); | |
929 | ||
1c79356b A |
930 | for (m = control; ; m = m->m_next) { |
931 | space += m->m_len; | |
932 | if (m->m_next == 0) | |
933 | break; | |
934 | } | |
935 | n = m; /* save pointer to last control buffer */ | |
936 | for (m = m0; m; m = m->m_next) | |
937 | space += m->m_len; | |
938 | if (space > sbspace(sb)) | |
939 | return (0); | |
940 | n->m_next = m0; /* concatenate data to control */ | |
941 | for (m = control; m; m = m->m_next) | |
942 | sballoc(sb, m); | |
943 | n = sb->sb_mb; | |
944 | if (n) { | |
945 | while (n->m_nextpkt) | |
946 | n = n->m_nextpkt; | |
947 | n->m_nextpkt = control; | |
948 | } else | |
949 | sb->sb_mb = control; | |
950 | postevent(0,sb,EV_RWBYTES); | |
951 | return (1); | |
952 | } | |
953 | ||
91447636 A |
954 | int |
955 | sbappendcontrol( | |
956 | struct sockbuf *sb, | |
957 | struct mbuf *m0, | |
958 | struct mbuf *control, | |
959 | int *error_out) | |
960 | { | |
961 | int result = 0; | |
962 | ||
963 | if (error_out) *error_out = 0; | |
964 | ||
965 | if (sb->sb_flags & SB_RECV) { | |
966 | int error; | |
967 | error = sflt_data_in(sb->sb_so, NULL, &m0, &control, 0); | |
968 | if (error) { | |
969 | if (error != EJUSTRETURN) { | |
970 | if (m0) m_freem(m0); | |
971 | if (control) m_freem(control); | |
972 | if (error_out) *error_out = error; | |
973 | } | |
974 | return 0; | |
975 | } | |
976 | } | |
977 | ||
978 | result = sbappendcontrol_internal(sb, m0, control); | |
979 | if (result == 0) { | |
980 | if (m0) m_freem(m0); | |
981 | if (control) m_freem(control); | |
982 | if (error_out) *error_out = ENOBUFS; | |
983 | } | |
984 | ||
985 | return result; | |
986 | } | |
987 | ||
1c79356b A |
988 | /* |
989 | * Compress mbuf chain m into the socket | |
990 | * buffer sb following mbuf n. If n | |
991 | * is null, the buffer is presumed empty. | |
992 | */ | |
91447636 | 993 | static int |
1c79356b A |
994 | sbcompress(sb, m, n) |
995 | register struct sockbuf *sb; | |
996 | register struct mbuf *m, *n; | |
997 | { | |
998 | register int eor = 0; | |
999 | register struct mbuf *o; | |
1000 | ||
1001 | while (m) { | |
1002 | eor |= m->m_flags & M_EOR; | |
1003 | if (m->m_len == 0 && | |
1004 | (eor == 0 || | |
1005 | (((o = m->m_next) || (o = n)) && | |
1006 | o->m_type == m->m_type))) { | |
1007 | m = m_free(m); | |
1008 | continue; | |
1009 | } | |
9bccf70c A |
1010 | if (n && (n->m_flags & M_EOR) == 0 && |
1011 | #ifndef __APPLE__ | |
1012 | M_WRITABLE(n) && | |
1013 | #endif | |
1014 | m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */ | |
1015 | m->m_len <= M_TRAILINGSPACE(n) && | |
1c79356b A |
1016 | n->m_type == m->m_type) { |
1017 | bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, | |
1018 | (unsigned)m->m_len); | |
1019 | n->m_len += m->m_len; | |
1020 | sb->sb_cc += m->m_len; | |
1021 | m = m_free(m); | |
1022 | continue; | |
1023 | } | |
1024 | if (n) | |
1025 | n->m_next = m; | |
1026 | else | |
1027 | sb->sb_mb = m; | |
1028 | sballoc(sb, m); | |
1029 | n = m; | |
1030 | m->m_flags &= ~M_EOR; | |
1031 | m = m->m_next; | |
1032 | n->m_next = 0; | |
1033 | } | |
1034 | if (eor) { | |
1035 | if (n) | |
1036 | n->m_flags |= eor; | |
1037 | else | |
1038 | printf("semi-panic: sbcompress\n"); | |
1039 | } | |
1040 | postevent(0,sb, EV_RWBYTES); | |
91447636 | 1041 | return 1; |
1c79356b A |
1042 | } |
1043 | ||
1044 | /* | |
1045 | * Free all mbufs in a sockbuf. | |
1046 | * Check that all resources are reclaimed. | |
1047 | */ | |
1048 | void | |
1049 | sbflush(sb) | |
1050 | register struct sockbuf *sb; | |
1051 | { | |
91447636 A |
1052 | if (sb->sb_so == NULL) |
1053 | panic ("sbflush sb->sb_so already null sb=%x\n", sb); | |
55e303ae | 1054 | (void)sblock(sb, M_WAIT); |
9bccf70c A |
1055 | while (sb->sb_mbcnt) { |
1056 | /* | |
1057 | * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty: | |
1058 | * we would loop forever. Panic instead. | |
1059 | */ | |
1060 | if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len)) | |
1061 | break; | |
1c79356b | 1062 | sbdrop(sb, (int)sb->sb_cc); |
9bccf70c | 1063 | } |
91447636 A |
1064 | if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt || sb->sb_so == NULL) |
1065 | panic("sbflush: cc %ld || mb %p || mbcnt %ld sb_so=%x", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt, sb->sb_so); | |
55e303ae | 1066 | |
1c79356b | 1067 | postevent(0, sb, EV_RWBYTES); |
91447636 A |
1068 | sbunlock(sb, 1); /* keep socket locked */ |
1069 | ||
1c79356b A |
1070 | } |
1071 | ||
1072 | /* | |
1073 | * Drop data from (the front of) a sockbuf. | |
9bccf70c A |
1074 | * use m_freem_list to free the mbuf structures |
1075 | * under a single lock... this is done by pruning | |
1076 | * the top of the tree from the body by keeping track | |
1077 | * of where we get to in the tree and then zeroing the | |
1078 | * two pertinent pointers m_nextpkt and m_next | |
1079 | * the socket buffer is then updated to point at the new | |
1080 | * top of the tree and the pruned area is released via | |
1081 | * m_freem_list. | |
1c79356b A |
1082 | */ |
1083 | void | |
1084 | sbdrop(sb, len) | |
1085 | register struct sockbuf *sb; | |
1086 | register int len; | |
1087 | { | |
fa4905b1 A |
1088 | register struct mbuf *m, *free_list, *ml; |
1089 | struct mbuf *next, *last; | |
1c79356b | 1090 | |
fa4905b1 A |
1091 | KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_START), sb, len, 0, 0, 0); |
1092 | ||
1c79356b | 1093 | next = (m = sb->sb_mb) ? m->m_nextpkt : 0; |
fa4905b1 A |
1094 | free_list = last = m; |
1095 | ml = (struct mbuf *)0; | |
1096 | ||
1c79356b A |
1097 | while (len > 0) { |
1098 | if (m == 0) { | |
9bccf70c A |
1099 | if (next == 0) { |
1100 | /* temporarily replacing this panic with printf because | |
1101 | * it occurs occasionally when closing a socket when there | |
1102 | * is no harm in ignoring it. This problem will be investigated | |
1103 | * further. | |
1104 | */ | |
1105 | /* panic("sbdrop"); */ | |
1106 | printf("sbdrop - count not zero\n"); | |
1107 | len = 0; | |
1108 | /* zero the counts. if we have no mbufs, we have no data (PR-2986815) */ | |
1109 | sb->sb_cc = 0; | |
1110 | sb->sb_mbcnt = 0; | |
1111 | break; | |
1112 | } | |
1113 | m = last = next; | |
1114 | next = m->m_nextpkt; | |
1115 | continue; | |
1c79356b A |
1116 | } |
1117 | if (m->m_len > len) { | |
1118 | m->m_len -= len; | |
1119 | m->m_data += len; | |
1120 | sb->sb_cc -= len; | |
1121 | break; | |
1122 | } | |
1123 | len -= m->m_len; | |
1124 | sbfree(sb, m); | |
fa4905b1 A |
1125 | |
1126 | ml = m; | |
1127 | m = m->m_next; | |
1c79356b A |
1128 | } |
1129 | while (m && m->m_len == 0) { | |
1130 | sbfree(sb, m); | |
fa4905b1 A |
1131 | |
1132 | ml = m; | |
1133 | m = m->m_next; | |
1134 | } | |
1135 | if (ml) { | |
1136 | ml->m_next = (struct mbuf *)0; | |
1137 | last->m_nextpkt = (struct mbuf *)0; | |
1138 | m_freem_list(free_list); | |
1c79356b A |
1139 | } |
1140 | if (m) { | |
1141 | sb->sb_mb = m; | |
1142 | m->m_nextpkt = next; | |
1143 | } else | |
1144 | sb->sb_mb = next; | |
fa4905b1 | 1145 | |
1c79356b | 1146 | postevent(0, sb, EV_RWBYTES); |
fa4905b1 A |
1147 | |
1148 | KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, 0, 0, 0, 0); | |
1c79356b A |
1149 | } |
1150 | ||
1151 | /* | |
1152 | * Drop a record off the front of a sockbuf | |
1153 | * and move the next record to the front. | |
1154 | */ | |
1155 | void | |
1156 | sbdroprecord(sb) | |
1157 | register struct sockbuf *sb; | |
1158 | { | |
1159 | register struct mbuf *m, *mn; | |
1c79356b A |
1160 | |
1161 | m = sb->sb_mb; | |
1162 | if (m) { | |
1163 | sb->sb_mb = m->m_nextpkt; | |
1164 | do { | |
1165 | sbfree(sb, m); | |
1166 | MFREE(m, mn); | |
9bccf70c A |
1167 | m = mn; |
1168 | } while (m); | |
1c79356b A |
1169 | } |
1170 | postevent(0, sb, EV_RWBYTES); | |
1171 | } | |
1172 | ||
1173 | /* | |
1174 | * Create a "control" mbuf containing the specified data | |
1175 | * with the specified type for presentation on a socket buffer. | |
1176 | */ | |
1177 | struct mbuf * | |
1178 | sbcreatecontrol(p, size, type, level) | |
1179 | caddr_t p; | |
1180 | register int size; | |
1181 | int type, level; | |
1182 | { | |
1183 | register struct cmsghdr *cp; | |
1184 | struct mbuf *m; | |
1185 | ||
9bccf70c A |
1186 | if (CMSG_SPACE((u_int)size) > MLEN) |
1187 | return ((struct mbuf *) NULL); | |
1c79356b A |
1188 | if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) |
1189 | return ((struct mbuf *) NULL); | |
1190 | cp = mtod(m, struct cmsghdr *); | |
1191 | /* XXX check size? */ | |
1192 | (void)memcpy(CMSG_DATA(cp), p, size); | |
9bccf70c A |
1193 | m->m_len = CMSG_SPACE(size); |
1194 | cp->cmsg_len = CMSG_LEN(size); | |
1c79356b A |
1195 | cp->cmsg_level = level; |
1196 | cp->cmsg_type = type; | |
1197 | return (m); | |
1198 | } | |
1199 | ||
1200 | /* | |
1201 | * Some routines that return EOPNOTSUPP for entry points that are not | |
1202 | * supported by a protocol. Fill in as needed. | |
1203 | */ | |
1204 | int | |
1205 | pru_abort_notsupp(struct socket *so) | |
1206 | { | |
1207 | return EOPNOTSUPP; | |
1208 | } | |
1209 | ||
1210 | ||
1211 | int | |
1212 | pru_accept_notsupp(struct socket *so, struct sockaddr **nam) | |
1213 | { | |
1214 | return EOPNOTSUPP; | |
1215 | } | |
1216 | ||
1217 | int | |
1218 | pru_attach_notsupp(struct socket *so, int proto, struct proc *p) | |
1219 | { | |
1220 | return EOPNOTSUPP; | |
1221 | } | |
1222 | ||
1223 | int | |
1224 | pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p) | |
1225 | { | |
1226 | return EOPNOTSUPP; | |
1227 | } | |
1228 | ||
1229 | int | |
1230 | pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p) | |
1231 | { | |
1232 | return EOPNOTSUPP; | |
1233 | } | |
1234 | ||
1235 | int | |
1236 | pru_connect2_notsupp(struct socket *so1, struct socket *so2) | |
1237 | { | |
1238 | return EOPNOTSUPP; | |
1239 | } | |
1240 | ||
1241 | int | |
1242 | pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, | |
1243 | struct ifnet *ifp, struct proc *p) | |
1244 | { | |
1245 | return EOPNOTSUPP; | |
1246 | } | |
1247 | ||
1248 | int | |
1249 | pru_detach_notsupp(struct socket *so) | |
1250 | { | |
1251 | return EOPNOTSUPP; | |
1252 | } | |
1253 | ||
1254 | int | |
1255 | pru_disconnect_notsupp(struct socket *so) | |
1256 | { | |
1257 | return EOPNOTSUPP; | |
1258 | } | |
1259 | ||
1260 | int | |
1261 | pru_listen_notsupp(struct socket *so, struct proc *p) | |
1262 | { | |
1263 | return EOPNOTSUPP; | |
1264 | } | |
1265 | ||
1266 | int | |
1267 | pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam) | |
1268 | { | |
1269 | return EOPNOTSUPP; | |
1270 | } | |
1271 | ||
1272 | int | |
1273 | pru_rcvd_notsupp(struct socket *so, int flags) | |
1274 | { | |
1275 | return EOPNOTSUPP; | |
1276 | } | |
1277 | ||
1278 | int | |
1279 | pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) | |
1280 | { | |
1281 | return EOPNOTSUPP; | |
1282 | } | |
1283 | ||
1284 | int | |
1285 | pru_send_notsupp(struct socket *so, int flags, struct mbuf *m, | |
1286 | struct sockaddr *addr, struct mbuf *control, | |
1287 | struct proc *p) | |
1288 | ||
1289 | { | |
1290 | return EOPNOTSUPP; | |
1291 | } | |
1292 | ||
1293 | ||
1294 | /* | |
1295 | * This isn't really a ``null'' operation, but it's the default one | |
1296 | * and doesn't do anything destructive. | |
1297 | */ | |
1298 | int | |
1299 | pru_sense_null(struct socket *so, struct stat *sb) | |
1300 | { | |
1301 | sb->st_blksize = so->so_snd.sb_hiwat; | |
1302 | return 0; | |
1303 | } | |
1304 | ||
1305 | ||
1306 | int pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, | |
1307 | struct uio *uio, struct mbuf *top, | |
1308 | struct mbuf *control, int flags) | |
1309 | ||
1310 | { | |
1311 | return EOPNOTSUPP; | |
1312 | } | |
1313 | ||
1314 | int pru_soreceive_notsupp(struct socket *so, | |
1315 | struct sockaddr **paddr, | |
1316 | struct uio *uio, struct mbuf **mp0, | |
1317 | struct mbuf **controlp, int *flagsp) | |
1318 | { | |
1319 | return EOPNOTSUPP; | |
1320 | } | |
1321 | ||
1322 | int | |
1323 | ||
1324 | pru_shutdown_notsupp(struct socket *so) | |
1325 | { | |
1326 | return EOPNOTSUPP; | |
1327 | } | |
1328 | ||
1329 | int | |
1330 | pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam) | |
1331 | { | |
1332 | return EOPNOTSUPP; | |
1333 | } | |
1334 | ||
1335 | int pru_sosend(struct socket *so, struct sockaddr *addr, | |
1336 | struct uio *uio, struct mbuf *top, | |
1337 | struct mbuf *control, int flags) | |
1338 | { | |
1339 | return EOPNOTSUPP; | |
1340 | } | |
1341 | ||
1342 | int pru_soreceive(struct socket *so, | |
1343 | struct sockaddr **paddr, | |
1344 | struct uio *uio, struct mbuf **mp0, | |
1345 | struct mbuf **controlp, int *flagsp) | |
1346 | { | |
1347 | return EOPNOTSUPP; | |
1348 | } | |
1349 | ||
1350 | ||
91447636 A |
1351 | int |
1352 | pru_sopoll_notsupp(__unused struct socket *so, __unused int events, | |
1353 | __unused kauth_cred_t cred, __unused void *wql) | |
1c79356b A |
1354 | { |
1355 | return EOPNOTSUPP; | |
1356 | } | |
1357 | ||
1358 | ||
9bccf70c A |
1359 | #ifdef __APPLE__ |
1360 | /* | |
1361 | * The following are macros on BSD and functions on Darwin | |
1362 | */ | |
1c79356b | 1363 | |
0b4e3aa0 A |
1364 | /* |
1365 | * Do we need to notify the other side when I/O is possible? | |
1366 | */ | |
1367 | ||
1368 | int | |
1369 | sb_notify(struct sockbuf *sb) | |
1370 | { | |
55e303ae | 1371 | return ((sb->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL|SB_KNOTE)) != 0); |
0b4e3aa0 A |
1372 | } |
1373 | ||
1374 | /* | |
1375 | * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? | |
1376 | * This is problematical if the fields are unsigned, as the space might | |
1377 | * still be negative (cc > hiwat or mbcnt > mbmax). Should detect | |
1378 | * overflow and return 0. Should use "lmin" but it doesn't exist now. | |
1379 | */ | |
1380 | long | |
1381 | sbspace(struct sockbuf *sb) | |
1382 | { | |
1383 | return ((long) imin((int)(sb->sb_hiwat - sb->sb_cc), | |
1384 | (int)(sb->sb_mbmax - sb->sb_mbcnt))); | |
1385 | } | |
1386 | ||
1387 | /* do we have to send all at once on a socket? */ | |
1388 | int | |
1389 | sosendallatonce(struct socket *so) | |
1390 | { | |
1391 | return (so->so_proto->pr_flags & PR_ATOMIC); | |
1392 | } | |
1393 | ||
1394 | /* can we read something from so? */ | |
1395 | int | |
1396 | soreadable(struct socket *so) | |
1397 | { | |
1398 | return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || | |
1399 | (so->so_state & SS_CANTRCVMORE) || | |
1400 | so->so_comp.tqh_first || so->so_error); | |
1401 | } | |
1402 | ||
1403 | /* can we write something to so? */ | |
1404 | ||
1405 | int | |
1406 | sowriteable(struct socket *so) | |
1407 | { | |
1408 | return ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && | |
1409 | ((so->so_state&SS_ISCONNECTED) || | |
1410 | (so->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || | |
1411 | (so->so_state & SS_CANTSENDMORE) || | |
1412 | so->so_error); | |
1413 | } | |
1414 | ||
1415 | /* adjust counters in sb reflecting allocation of m */ | |
1416 | ||
1417 | void | |
1418 | sballoc(struct sockbuf *sb, struct mbuf *m) | |
1419 | { | |
1420 | sb->sb_cc += m->m_len; | |
1421 | sb->sb_mbcnt += MSIZE; | |
1422 | if (m->m_flags & M_EXT) | |
1423 | sb->sb_mbcnt += m->m_ext.ext_size; | |
1424 | } | |
1425 | ||
1426 | /* adjust counters in sb reflecting freeing of m */ | |
1427 | void | |
1428 | sbfree(struct sockbuf *sb, struct mbuf *m) | |
1429 | { | |
1430 | sb->sb_cc -= m->m_len; | |
1431 | sb->sb_mbcnt -= MSIZE; | |
1432 | if (m->m_flags & M_EXT) | |
1433 | sb->sb_mbcnt -= m->m_ext.ext_size; | |
1434 | } | |
1435 | ||
1436 | /* | |
1437 | * Set lock on sockbuf sb; sleep if lock is already held. | |
1438 | * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. | |
1439 | * Returns error without lock if sleep is interrupted. | |
1440 | */ | |
1441 | int | |
1442 | sblock(struct sockbuf *sb, int wf) | |
1443 | { | |
1444 | return(sb->sb_flags & SB_LOCK ? | |
1445 | ((wf == M_WAIT) ? sb_lock(sb) : EWOULDBLOCK) : | |
1446 | (sb->sb_flags |= SB_LOCK), 0); | |
1447 | } | |
1448 | ||
1449 | /* release lock on sockbuf sb */ | |
1450 | void | |
91447636 | 1451 | sbunlock(struct sockbuf *sb, int keeplocked) |
0b4e3aa0 | 1452 | { |
91447636 A |
1453 | struct socket *so = sb->sb_so; |
1454 | int lr, lr_saved; | |
1455 | lck_mtx_t *mutex_held; | |
1456 | ||
1457 | #ifdef __ppc__ | |
1458 | __asm__ volatile("mflr %0" : "=r" (lr)); | |
1459 | lr_saved = lr; | |
1460 | #endif | |
0b4e3aa0 | 1461 | sb->sb_flags &= ~SB_LOCK; |
91447636 A |
1462 | |
1463 | if (so->so_proto->pr_getlock != NULL) | |
1464 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); | |
1465 | else | |
1466 | mutex_held = so->so_proto->pr_domain->dom_mtx; | |
1467 | ||
1468 | if (keeplocked == 0) | |
1469 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
1470 | ||
0b4e3aa0 A |
1471 | if (sb->sb_flags & SB_WANT) { |
1472 | sb->sb_flags &= ~SB_WANT; | |
91447636 A |
1473 | if (so->so_usecount < 0) |
1474 | panic("sbunlock: b4 wakeup so=%x ref=%d lr=%x sb_flags=%x\n", sb->sb_so, so->so_usecount, lr_saved, sb->sb_flags); | |
1475 | ||
0b4e3aa0 A |
1476 | wakeup((caddr_t)&(sb)->sb_flags); |
1477 | } | |
91447636 A |
1478 | if (keeplocked == 0) { /* unlock on exit */ |
1479 | so->so_usecount--; | |
1480 | if (so->so_usecount < 0) | |
1481 | panic("sbunlock: unlock on exit so=%x lr=%x sb_flags=%x\n", so, so->so_usecount,lr_saved, sb->sb_flags); | |
1482 | so->reserved4= lr_saved; | |
1483 | lck_mtx_unlock(mutex_held); | |
1484 | } | |
0b4e3aa0 A |
1485 | } |
1486 | ||
1487 | void | |
1488 | sorwakeup(struct socket * so) | |
1489 | { | |
1490 | if (sb_notify(&so->so_rcv)) | |
1491 | sowakeup(so, &so->so_rcv); | |
1492 | } | |
1493 | ||
1494 | void | |
1495 | sowwakeup(struct socket * so) | |
1496 | { | |
1497 | if (sb_notify(&so->so_snd)) | |
1498 | sowakeup(so, &so->so_snd); | |
1499 | } | |
9bccf70c | 1500 | #endif __APPLE__ |
0b4e3aa0 | 1501 | |
1c79356b A |
1502 | /* |
1503 | * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. | |
1504 | */ | |
1505 | struct sockaddr * | |
1506 | dup_sockaddr(sa, canwait) | |
1507 | struct sockaddr *sa; | |
1508 | int canwait; | |
1509 | { | |
1510 | struct sockaddr *sa2; | |
1511 | ||
1512 | MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME, | |
1513 | canwait ? M_WAITOK : M_NOWAIT); | |
1514 | if (sa2) | |
1515 | bcopy(sa, sa2, sa->sa_len); | |
1516 | return sa2; | |
1517 | } | |
1518 | ||
1519 | /* | |
1520 | * Create an external-format (``xsocket'') structure using the information | |
1521 | * in the kernel-format socket structure pointed to by so. This is done | |
1522 | * to reduce the spew of irrelevant information over this interface, | |
1523 | * to isolate user code from changes in the kernel structure, and | |
1524 | * potentially to provide information-hiding if we decide that | |
1525 | * some of this information should be hidden from users. | |
1526 | */ | |
1527 | void | |
1528 | sotoxsocket(struct socket *so, struct xsocket *xso) | |
1529 | { | |
1530 | xso->xso_len = sizeof *xso; | |
1531 | xso->xso_so = so; | |
1532 | xso->so_type = so->so_type; | |
1533 | xso->so_options = so->so_options; | |
1534 | xso->so_linger = so->so_linger; | |
1535 | xso->so_state = so->so_state; | |
1536 | xso->so_pcb = so->so_pcb; | |
91447636 A |
1537 | if (so->so_proto) { |
1538 | xso->xso_protocol = so->so_proto->pr_protocol; | |
1539 | xso->xso_family = so->so_proto->pr_domain->dom_family; | |
1540 | } | |
1541 | else | |
1542 | xso->xso_protocol = xso->xso_family = 0; | |
1c79356b A |
1543 | xso->so_qlen = so->so_qlen; |
1544 | xso->so_incqlen = so->so_incqlen; | |
1545 | xso->so_qlimit = so->so_qlimit; | |
1546 | xso->so_timeo = so->so_timeo; | |
1547 | xso->so_error = so->so_error; | |
1548 | xso->so_pgid = so->so_pgid; | |
1549 | xso->so_oobmark = so->so_oobmark; | |
1550 | sbtoxsockbuf(&so->so_snd, &xso->so_snd); | |
1551 | sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); | |
1552 | xso->so_uid = so->so_uid; | |
1553 | } | |
1554 | ||
1555 | /* | |
1556 | * This does the same for sockbufs. Note that the xsockbuf structure, | |
1557 | * since it is always embedded in a socket, does not include a self | |
1558 | * pointer nor a length. We make this entry point public in case | |
1559 | * some other mechanism needs it. | |
1560 | */ | |
1561 | void | |
1562 | sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb) | |
1563 | { | |
1564 | xsb->sb_cc = sb->sb_cc; | |
1565 | xsb->sb_hiwat = sb->sb_hiwat; | |
1566 | xsb->sb_mbcnt = sb->sb_mbcnt; | |
1567 | xsb->sb_mbmax = sb->sb_mbmax; | |
1568 | xsb->sb_lowat = sb->sb_lowat; | |
1569 | xsb->sb_flags = sb->sb_flags; | |
91447636 A |
1570 | xsb->sb_timeo = (u_long)(sb->sb_timeo.tv_sec * hz) + sb->sb_timeo.tv_usec / tick; |
1571 | if (xsb->sb_timeo == 0 && sb->sb_timeo.tv_usec != 0) | |
1572 | xsb->sb_timeo = 1; | |
1c79356b A |
1573 | } |
1574 | ||
1575 | /* | |
1576 | * Here is the definition of some of the basic objects in the kern.ipc | |
1577 | * branch of the MIB. | |
1578 | */ | |
1c79356b A |
1579 | SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); |
1580 | ||
1581 | /* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */ | |
1582 | static int dummy; | |
1583 | SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); | |
1584 | ||
9bccf70c A |
1585 | SYSCTL_INT(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, |
1586 | &sb_max, 0, "Maximum socket buffer size"); | |
1587 | SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, | |
1588 | &maxsockets, 0, "Maximum number of sockets avaliable"); | |
1c79356b A |
1589 | SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, |
1590 | &sb_efficiency, 0, ""); | |
1591 | SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD, &nmbclusters, 0, ""); | |
1592 |