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