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