]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_socket2.c
xnu-1699.24.8.tar.gz
[apple/xnu.git] / bsd / kern / uipc_socket2.c
1 /*
2 * Copyright (c) 1998-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
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
62 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $
63 */
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 */
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/domain.h>
74 #include <sys/kernel.h>
75 #include <sys/proc_internal.h>
76 #include <sys/kauth.h>
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>
86 #include <kern/locks.h>
87 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_pcb.h>
90 #include <sys/kdebug.h>
91 #include <libkern/OSAtomic.h>
92
93 #if CONFIG_MACF
94 #include <security/mac_framework.h>
95 #endif
96
97 /* TODO: this should be in a header file somewhere */
98 extern void postevent(struct socket *, struct sockbuf *, int);
99
100 #define DBG_FNC_SBDROP NETDBG_CODE(DBG_NETSOCK, 4)
101 #define DBG_FNC_SBAPPEND NETDBG_CODE(DBG_NETSOCK, 5)
102
103 static inline void sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *);
104 static struct socket *sonewconn_internal(struct socket *, int);
105 static int sbappendaddr_internal(struct sockbuf *, struct sockaddr *,
106 struct mbuf *, struct mbuf *);
107 static int sbappendcontrol_internal(struct sockbuf *, struct mbuf *,
108 struct mbuf *);
109
110 /*
111 * Primitive routines for operating on sockets and socket buffers
112 */
113 static int soqlimitcompat = 1;
114 static int soqlencomp = 0;
115
116 /* Based on the number of mbuf clusters configured, high_sb_max and sb_max can get
117 * scaled up or down to suit that memory configuration. high_sb_max is a higher
118 * limit on sb_max that is checked when sb_max gets set through sysctl.
119 */
120
121 u_int32_t sb_max = SB_MAX; /* XXX should be static */
122 u_int32_t high_sb_max = SB_MAX;
123
124 static u_int32_t sb_efficiency = 8; /* parameter for sbreserve() */
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;
128
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
136 * soisdisconnecting() is called during processing of disconnect() call,
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
144 * two queues of sockets: so_incomp for connections in progress
145 * and so_comp for connections already made and awaiting user acceptance.
146 * As a protocol is preparing incoming connections, it creates a socket
147 * structure queued on so_incomp by calling sonewconn(). When the connection
148 * is established, soisconnected() is called, and transfers the
149 * socket structure to so_comp, making it available to accept().
150 *
151 * If a socket is closed with sockets on either
152 * so_incomp or so_comp, these sockets are dropped.
153 *
154 * If higher level protocols are implemented in
155 * the kernel, the wakeups done here will sometimes
156 * cause software-interrupt process scheduling.
157 */
158 void
159 soisconnecting(struct socket *so)
160 {
161
162 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
163 so->so_state |= SS_ISCONNECTING;
164
165 sflt_notify(so, sock_evt_connecting, NULL);
166 }
167
168 void
169 soisconnected(struct socket *so)
170 {
171 struct socket *head = so->so_head;
172
173 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
174 so->so_state |= SS_ISCONNECTED;
175
176 sflt_notify(so, sock_evt_connected, NULL);
177
178 if (head && (so->so_state & SS_INCOMP)) {
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);
183 socket_lock(head, 1);
184 }
185 postevent(head, 0, EV_RCONN);
186 TAILQ_REMOVE(&head->so_incomp, so, so_list);
187 head->so_incqlen--;
188 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
189 sorwakeup(head);
190 wakeup_one((caddr_t)&head->so_timeo);
191 if (head->so_proto->pr_getlock != NULL) {
192 socket_unlock(head, 1);
193 socket_lock(so, 0);
194 }
195 } else {
196 postevent(so, 0, EV_WCONN);
197 wakeup((caddr_t)&so->so_timeo);
198 sorwakeup(so);
199 sowwakeup(so);
200 }
201 }
202
203 void
204 soisdisconnecting(struct socket *so)
205 {
206 so->so_state &= ~SS_ISCONNECTING;
207 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
208 sflt_notify(so, sock_evt_disconnecting, NULL);
209 wakeup((caddr_t)&so->so_timeo);
210 sowwakeup(so);
211 sorwakeup(so);
212 }
213
214 void
215 soisdisconnected(struct socket *so)
216 {
217 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
218 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
219 sflt_notify(so, sock_evt_disconnected, NULL);
220 wakeup((caddr_t)&so->so_timeo);
221 sowwakeup(so);
222 sorwakeup(so);
223 }
224
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 */
229 void
230 sodisconnectwakeup(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
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 */
247 static struct socket *
248 sonewconn_internal(struct socket *head, int connstatus)
249 {
250 int so_qlen, error = 0;
251 struct socket *so;
252 lck_mtx_t *mutex_held;
253
254 if (head->so_proto->pr_getlock != NULL)
255 mutex_held = (*head->so_proto->pr_getlock)(head, 0);
256 else
257 mutex_held = head->so_proto->pr_domain->dom_mtx;
258 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
259
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)))
282 return ((struct socket *)0);
283 so = soalloc(1, head->so_proto->pr_domain->dom_family,
284 head->so_type);
285 if (so == NULL)
286 return ((struct socket *)0);
287 /* check if head was closed during the soalloc */
288 if (head->so_proto == NULL) {
289 sodealloc(so);
290 return ((struct socket *)0);
291 }
292
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;
301 so->so_gid = head->so_gid;
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 |
308 SOF_NPX_SETOPTSHUT |
309 SOF_NODEFUNCT);
310 so->so_usecount = 1;
311 so->next_lock_lr = 0;
312 so->next_unlock_lr = 0;
313
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
320 #if CONFIG_MACF_SOCKET
321 mac_socket_label_associate_accept(head, so);
322 #endif
323
324 /* inherit traffic management properties of listener */
325 so->so_traffic_mgt_flags = head->so_traffic_mgt_flags & (TRAFFIC_MGT_SO_BACKGROUND);
326 so->so_background_thread = head->so_background_thread;
327 so->so_traffic_class = head->so_traffic_class;
328
329 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
330 sodealloc(so);
331 return ((struct socket *)0);
332 }
333
334 /*
335 * Must be done with head unlocked to avoid deadlock
336 * for protocol with per socket mutexes.
337 */
338 if (head->so_proto->pr_unlock)
339 socket_unlock(head, 0);
340 if (((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL) != 0) ||
341 error) {
342 sodealloc(so);
343 if (head->so_proto->pr_unlock)
344 socket_lock(head, 0);
345 return ((struct socket *)0);
346 }
347 if (head->so_proto->pr_unlock) {
348 socket_lock(head, 0);
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
359 #ifdef __APPLE__
360 so->so_proto->pr_domain->dom_refs++;
361 #endif
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;
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++;
382
383 #ifdef __APPLE__
384 /* Attach socket filters for this protocol */
385 sflt_initsock(so);
386 #endif
387
388 if (connstatus) {
389 so->so_state |= connstatus;
390 sorwakeup(head);
391 wakeup((caddr_t)&head->so_timeo);
392 }
393 return (so);
394 }
395
396
397 struct socket *
398 sonewconn(struct socket *head, int connstatus, const struct sockaddr *from)
399 {
400 int error = sflt_connectin(head, from);
401 if (error) {
402 return (NULL);
403 }
404
405 return (sonewconn_internal(head, connstatus));
406 }
407
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
418 void
419 socantsendmore(struct socket *so)
420 {
421 so->so_state |= SS_CANTSENDMORE;
422 sflt_notify(so, sock_evt_cantsendmore, NULL);
423 sowwakeup(so);
424 }
425
426 void
427 socantrcvmore(struct socket *so)
428 {
429 so->so_state |= SS_CANTRCVMORE;
430 sflt_notify(so, sock_evt_cantrecvmore, NULL);
431 sorwakeup(so);
432 }
433
434 /*
435 * Wait for data to arrive at/drain from a socket buffer.
436 *
437 * Returns: 0 Success
438 * EBADF
439 * msleep:EINTR
440 */
441 int
442 sbwait(struct sockbuf *sb)
443 {
444 int error = 0;
445 uintptr_t lr_saved;
446 struct socket *so = sb->sb_so;
447 lck_mtx_t *mutex_held;
448 struct timespec ts;
449
450 lr_saved = (uintptr_t) __builtin_return_address(0);
451
452 if (so->so_proto->pr_getlock != NULL)
453 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
454 else
455 mutex_held = so->so_proto->pr_domain->dom_mtx;
456 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
457
458 sb->sb_flags |= SB_WAIT;
459
460 if (so->so_usecount < 1)
461 panic("sbwait: so=%p refcount=%d\n", so, so->so_usecount);
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,
465 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", &ts);
466
467 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
468
469 if (so->so_usecount < 1)
470 panic("sbwait: so=%p refcount=%d\n", so, so->so_usecount);
471
472 if ((so->so_state & SS_DRAINING) || (so->so_flags & SOF_DEFUNCT)) {
473 error = EBADF;
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 }
479 }
480
481 return (error);
482 }
483
484 /*
485 * Lock a sockbuf already known to be locked;
486 * return any error returned from sleep (EINTR).
487 *
488 * Returns: 0 Success
489 * EINTR
490 */
491 int
492 sb_lock(struct sockbuf *sb)
493 {
494 struct socket *so = sb->sb_so;
495 lck_mtx_t *mutex_held;
496 int error = 0;
497
498 if (so == NULL)
499 panic("sb_lock: null so back pointer sb=%p\n", sb);
500
501 while (sb->sb_flags & SB_LOCK) {
502 sb->sb_flags |= SB_WANT;
503
504 if (so->so_proto->pr_getlock != NULL)
505 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
506 else
507 mutex_held = so->so_proto->pr_domain->dom_mtx;
508 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
509
510 if (so->so_usecount < 1)
511 panic("sb_lock: so=%p refcount=%d\n", so,
512 so->so_usecount);
513
514 error = msleep((caddr_t)&sb->sb_flags, mutex_held,
515 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH,
516 "sb_lock", 0);
517 if (so->so_usecount < 1)
518 panic("sb_lock: 2 so=%p refcount=%d\n", so,
519 so->so_usecount);
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
528 if (error)
529 return (error);
530 }
531 sb->sb_flags |= SB_LOCK;
532 return (0);
533 }
534
535 void
536 sbwakeup(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
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 */
549 void
550 sowakeup(struct socket *so, struct sockbuf *sb)
551 {
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
560 sb->sb_flags &= ~SB_SEL;
561 selwakeup(&sb->sb_sel);
562 sbwakeup(sb);
563 if (so->so_state & SS_ASYNC) {
564 if (so->so_pgid < 0)
565 gsignal(-so->so_pgid, SIGIO);
566 else if (so->so_pgid > 0)
567 proc_signal(so->so_pgid, SIGIO);
568 }
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) {
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
581 socket_unlock(so, 0);
582 (*so_upcall)(so, so_upcallarg, M_DONTWAIT);
583 socket_lock(so, 0);
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);
589 }
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
624 /*
625 * Returns: 0 Success
626 * ENOBUFS
627 */
628 int
629 soreserve(struct socket *so, u_int32_t sndcc, u_int32_t rcvcc)
630 {
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);
643 bad2:
644 #ifdef __APPLE__
645 selthreadclear(&so->so_snd.sb_sel);
646 #endif
647 sbrelease(&so->so_snd);
648 bad:
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 */
657 int
658 sbreserve(struct sockbuf *sb, u_int32_t cc)
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 */
672 /* WARNING needs to do selthreadclear() before calling this */
673 void
674 sbrelease(struct sockbuf *sb)
675 {
676 sbflush(sb);
677 sb->sb_hiwat = 0;
678 sb->sb_mbmax = 0;
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 */
712 int
713 sbappend(struct sockbuf *sb, struct mbuf *m)
714 {
715 struct socket *so = sb->sb_so;
716
717 if (m == NULL || (sb->sb_flags & SB_DROP)) {
718 if (m != NULL)
719 m_freem(m);
720 return (0);
721 }
722
723 SBLASTRECORDCHK(sb, "sbappend 1");
724
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) {
729 int error = sflt_data_in(so, NULL, &m, NULL, 0);
730 SBLASTRECORDCHK(sb, "sbappend 2");
731 if (error != 0) {
732 if (error != EJUSTRETURN)
733 m_freem(m);
734 return (0);
735 }
736 }
737
738 /* If this is the first record, it's also the last record */
739 if (sb->sb_lastrecord == NULL)
740 sb->sb_lastrecord = m;
741
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 */
750 int
751 sbappendstream(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) {
768 int error = sflt_data_in(so, NULL, &m, NULL, 0);
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);
781 }
782
783 #ifdef SOCKBUF_DEBUG
784 void
785 sbcheck(struct sockbuf *sb)
786 {
787 struct mbuf *m;
788 struct mbuf *n = 0;
789 u_int32_t len = 0, mbcnt = 0;
790 lck_mtx_t *mutex_held;
791
792 if (sb->sb_so->so_proto->pr_getlock != NULL)
793 mutex_held = (*sb->sb_so->so_proto->pr_getlock)(sb->sb_so, 0);
794 else
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;
801
802 for (m = sb->sb_mb; m; m = n) {
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);
815 }
816 }
817 #endif
818
819 void
820 sblastrecordchk(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
837 void
838 sblastmbufchk(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
863 /*
864 * Similar to sbappend, except the mbuf chain begins a new record.
865 */
866 int
867 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
868 {
869 struct mbuf *m;
870 int space = 0;
871
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,
888 sock_data_filt_flag_record);
889 if (error != 0) {
890 SBLASTRECORDCHK(sb, "sbappendrecord 1");
891 if (error != EJUSTRETURN)
892 m_freem(m0);
893 return (0);
894 }
895 }
896
897 /*
898 * Note this permits zero length records.
899 */
900 sballoc(sb, m0);
901 SBLASTRECORDCHK(sb, "sbappendrecord 2");
902 if (sb->sb_lastrecord != NULL) {
903 sb->sb_lastrecord->m_nextpkt = m0;
904 } else {
905 sb->sb_mb = m0;
906 }
907 sb->sb_lastrecord = m0;
908 sb->sb_mbtail = m0;
909
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 }
916 sbcompress(sb, m, m0);
917 SBLASTRECORDCHK(sb, "sbappendrecord 3");
918 return (1);
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 */
926 int
927 sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
928 {
929 struct mbuf *m;
930 struct mbuf **mp;
931
932 if (m0 == 0)
933 return (0);
934
935 SBLASTRECORDCHK(sb, "sbinsertoob 1");
936
937 if ((sb->sb_flags & SB_RECV) != 0) {
938 int error = sflt_data_in(sb->sb_so, NULL, &m0, NULL,
939 sock_data_filt_flag_oob);
940
941 SBLASTRECORDCHK(sb, "sbinsertoob 2");
942 if (error) {
943 if (error != EJUSTRETURN) {
944 m_freem(m0);
945 }
946 return (0);
947 }
948 }
949
950 for (mp = &sb->sb_mb; *mp; mp = &((*mp)->m_nextpkt)) {
951 m = *mp;
952 again:
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;
971 if (*mp == NULL) {
972 /* m0 is actually the new tail */
973 sb->sb_lastrecord = m0;
974 }
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 }
982 sbcompress(sb, m, m0);
983 SBLASTRECORDCHK(sb, "sbinsertoob 3");
984 return (1);
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.
992 *
993 * Returns: 0 No space/out of mbufs
994 * 1 Success
995 */
996 static int
997 sbappendaddr_internal(struct sockbuf *sb, struct sockaddr *asa,
998 struct mbuf *m0, struct mbuf *control)
999 {
1000 struct mbuf *m, *n, *nlast;
1001 int space = asa->sa_len;
1002
1003 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
1004 panic("sbappendaddr");
1005
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;
1027
1028 SBLASTRECORDCHK(sb, "sbappendadddr 1");
1029
1030 for (n = m; n->m_next != NULL; n = n->m_next)
1031 sballoc(sb, n);
1032 sballoc(sb, n);
1033 nlast = n;
1034
1035 if (sb->sb_lastrecord != NULL) {
1036 sb->sb_lastrecord->m_nextpkt = m;
1037 } else {
1038 sb->sb_mb = m;
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);
1047 return (1);
1048 }
1049
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 */
1058 int
1059 sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0,
1060 struct mbuf *control, int *error_out)
1061 {
1062 int result = 0;
1063 boolean_t sb_unix = (sb->sb_flags & SB_UNIX);
1064
1065 if (error_out)
1066 *error_out = 0;
1067
1068 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
1069 panic("sbappendaddrorfree");
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
1081 /* Call socket data in filters */
1082 if ((sb->sb_flags & SB_RECV) != 0) {
1083 int error;
1084 error = sflt_data_in(sb->sb_so, asa, &m0, &control, 0);
1085 SBLASTRECORDCHK(sb, __func__);
1086 if (error) {
1087 if (error != EJUSTRETURN) {
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;
1094 }
1095 return (0);
1096 }
1097 }
1098
1099 result = sbappendaddr_internal(sb, asa, m0, control);
1100 if (result == 0) {
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;
1107 }
1108
1109 return (result);
1110 }
1111
1112 static int
1113 sbappendcontrol_internal(struct sockbuf *sb, struct mbuf *m0,
1114 struct mbuf *control)
1115 {
1116 struct mbuf *m, *mlast, *n;
1117 int space = 0;
1118
1119 if (control == 0)
1120 panic("sbappendcontrol");
1121
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;
1130 if (space > sbspace(sb) && !(sb->sb_flags & SB_UNIX))
1131 return (0);
1132 n->m_next = m0; /* concatenate data to control */
1133
1134 SBLASTRECORDCHK(sb, "sbappendcontrol 1");
1135
1136 for (m = control; m->m_next != NULL; m = m->m_next)
1137 sballoc(sb, m);
1138 sballoc(sb, m);
1139 mlast = m;
1140
1141 if (sb->sb_lastrecord != NULL) {
1142 sb->sb_lastrecord->m_nextpkt = control;
1143 } else {
1144 sb->sb_mb = control;
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);
1153 return (1);
1154 }
1155
1156 int
1157 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control,
1158 int *error_out)
1159 {
1160 int result = 0;
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
1176 if (sb->sb_flags & SB_RECV) {
1177 int error;
1178
1179 error = sflt_data_in(sb->sb_so, NULL, &m0, &control, 0);
1180 SBLASTRECORDCHK(sb, __func__);
1181 if (error) {
1182 if (error != EJUSTRETURN) {
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;
1189 }
1190 return (0);
1191 }
1192 }
1193
1194 result = sbappendcontrol_internal(sb, m0, control);
1195 if (result == 0) {
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;
1202 }
1203
1204 return (result);
1205 }
1206
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 */
1212 static inline void
1213 sbcompress(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 }
1225
1226 while (m) {
1227 eor |= m->m_flags & M_EOR;
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;
1232 m = m_free(m);
1233 continue;
1234 }
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) &&
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;
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;
1250 m = m_free(m);
1251 continue;
1252 }
1253 if (n)
1254 n->m_next = m;
1255 else
1256 sb->sb_mb = m;
1257 sb->sb_mbtail = m;
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 }
1270 done:
1271 SBLASTMBUFCHK(sb, __func__);
1272 postevent(0, sb, EV_RWBYTES);
1273 }
1274
1275 void
1276 sb_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)) {
1280 panic("%s: sb %p so %p cc %d mbcnt %d mb %p mbtail %p "
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 }
1285 }
1286
1287 /*
1288 * Free all mbufs in a sockbuf.
1289 * Check that all resources are reclaimed.
1290 */
1291 void
1292 sbflush(struct sockbuf *sb)
1293 {
1294 if (sb->sb_so == NULL)
1295 panic("sbflush sb->sb_so already null sb=%p\n", sb);
1296 (void) sblock(sb, M_WAIT);
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;
1304 sbdrop(sb, (int)sb->sb_cc);
1305 }
1306 sb_empty_assert(sb, __func__);
1307 postevent(0, sb, EV_RWBYTES);
1308 sbunlock(sb, 1); /* keep socket locked */
1309
1310 }
1311
1312 /*
1313 * Drop data from (the front of) a sockbuf.
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.
1322 */
1323 void
1324 sbdrop(struct sockbuf *sb, int len)
1325 {
1326 struct mbuf *m, *free_list, *ml;
1327 struct mbuf *next, *last;
1328
1329 KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_START), sb, len, 0, 0, 0);
1330
1331 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
1332 free_list = last = m;
1333 ml = (struct mbuf *)0;
1334
1335 while (len > 0) {
1336 if (m == 0) {
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;
1359 }
1360 if (m->m_len > len) {
1361 m->m_len -= len;
1362 m->m_data += len;
1363 sb->sb_cc -= len;
1364 if (m->m_type != MT_DATA && m->m_type != MT_HEADER &&
1365 m->m_type != MT_OOBDATA)
1366 sb->sb_ctl -= len;
1367 break;
1368 }
1369 len -= m->m_len;
1370 sbfree(sb, m);
1371
1372 ml = m;
1373 m = m->m_next;
1374 }
1375 while (m && m->m_len == 0) {
1376 sbfree(sb, m);
1377
1378 ml = m;
1379 m = m->m_next;
1380 }
1381 if (ml) {
1382 ml->m_next = (struct mbuf *)0;
1383 last->m_nextpkt = (struct mbuf *)0;
1384 m_freem_list(free_list);
1385 }
1386 if (m) {
1387 sb->sb_mb = m;
1388 m->m_nextpkt = next;
1389 } else {
1390 sb->sb_mb = next;
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 }
1405
1406 postevent(0, sb, EV_RWBYTES);
1407
1408 KERNEL_DEBUG((DBG_FNC_SBDROP | DBG_FUNC_END), sb, 0, 0, 0, 0);
1409 }
1410
1411 /*
1412 * Drop a record off the front of a sockbuf
1413 * and move the next record to the front.
1414 */
1415 void
1416 sbdroprecord(struct sockbuf *sb)
1417 {
1418 struct mbuf *m, *mn;
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);
1426 m = mn;
1427 } while (m);
1428 }
1429 SB_EMPTY_FIXUP(sb);
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 */
1437 struct mbuf *
1438 sbcreatecontrol(caddr_t p, int size, int type, int level)
1439 {
1440 struct cmsghdr *cp;
1441 struct mbuf *m;
1442
1443 if (CMSG_SPACE((u_int)size) > MLEN)
1444 return ((struct mbuf *)NULL);
1445 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
1446 return ((struct mbuf *)NULL);
1447 cp = mtod(m, struct cmsghdr *);
1448 /* XXX check size? */
1449 (void) memcpy(CMSG_DATA(cp), p, size);
1450 m->m_len = CMSG_SPACE(size);
1451 cp->cmsg_len = CMSG_LEN(size);
1452 cp->cmsg_level = level;
1453 cp->cmsg_type = type;
1454 return (m);
1455 }
1456
1457 struct mbuf**
1458 sbcreatecontrol_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
1489 /*
1490 * Some routines that return EOPNOTSUPP for entry points that are not
1491 * supported by a protocol. Fill in as needed.
1492 */
1493 int
1494 pru_abort_notsupp(__unused struct socket *so)
1495 {
1496 return (EOPNOTSUPP);
1497 }
1498
1499 int
1500 pru_accept_notsupp(__unused struct socket *so, __unused struct sockaddr **nam)
1501 {
1502 return (EOPNOTSUPP);
1503 }
1504
1505 int
1506 pru_attach_notsupp(__unused struct socket *so, __unused int proto,
1507 __unused struct proc *p)
1508 {
1509 return (EOPNOTSUPP);
1510 }
1511
1512 int
1513 pru_bind_notsupp(__unused struct socket *so, __unused struct sockaddr *nam,
1514 __unused struct proc *p)
1515 {
1516 return (EOPNOTSUPP);
1517 }
1518
1519 int
1520 pru_connect_notsupp(__unused struct socket *so, __unused struct sockaddr *nam,
1521 __unused struct proc *p)
1522 {
1523 return (EOPNOTSUPP);
1524 }
1525
1526 int
1527 pru_connect2_notsupp(__unused struct socket *so1, __unused struct socket *so2)
1528 {
1529 return (EOPNOTSUPP);
1530 }
1531
1532 int
1533 pru_control_notsupp(__unused struct socket *so, __unused u_long cmd,
1534 __unused caddr_t data, __unused struct ifnet *ifp, __unused struct proc *p)
1535 {
1536 return (EOPNOTSUPP);
1537 }
1538
1539 int
1540 pru_detach_notsupp(__unused struct socket *so)
1541 {
1542 return (EOPNOTSUPP);
1543 }
1544
1545 int
1546 pru_disconnect_notsupp(__unused struct socket *so)
1547 {
1548 return (EOPNOTSUPP);
1549 }
1550
1551 int
1552 pru_listen_notsupp(__unused struct socket *so, __unused struct proc *p)
1553 {
1554 return (EOPNOTSUPP);
1555 }
1556
1557 int
1558 pru_peeraddr_notsupp(__unused struct socket *so, __unused struct sockaddr **nam)
1559 {
1560 return (EOPNOTSUPP);
1561 }
1562
1563 int
1564 pru_rcvd_notsupp(__unused struct socket *so, __unused int flags)
1565 {
1566 return (EOPNOTSUPP);
1567 }
1568
1569 int
1570 pru_rcvoob_notsupp(__unused struct socket *so, __unused struct mbuf *m,
1571 __unused int flags)
1572 {
1573 return (EOPNOTSUPP);
1574 }
1575
1576 int
1577 pru_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)
1580
1581 {
1582 return (EOPNOTSUPP);
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 */
1590 int
1591 pru_sense_null(struct socket *so, void *ub, int isstat64)
1592 {
1593 if (isstat64 != 0) {
1594 struct stat64 *sb64;
1595
1596 sb64 = (struct stat64 *)ub;
1597 sb64->st_blksize = so->so_snd.sb_hiwat;
1598 } else {
1599 struct stat *sb;
1600
1601 sb = (struct stat *)ub;
1602 sb->st_blksize = so->so_snd.sb_hiwat;
1603 }
1604
1605 return (0);
1606 }
1607
1608
1609 int
1610 pru_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)
1613
1614 {
1615 return (EOPNOTSUPP);
1616 }
1617
1618 int
1619 pru_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)
1623 {
1624 return (EOPNOTSUPP);
1625 }
1626
1627 int
1628 pru_shutdown_notsupp(__unused struct socket *so)
1629 {
1630 return (EOPNOTSUPP);
1631 }
1632
1633 int
1634 pru_sockaddr_notsupp(__unused struct socket *so, __unused struct sockaddr **nam)
1635 {
1636 return (EOPNOTSUPP);
1637 }
1638
1639 int
1640 pru_sopoll_notsupp(__unused struct socket *so, __unused int events,
1641 __unused kauth_cred_t cred, __unused void *wql)
1642 {
1643 return (EOPNOTSUPP);
1644 }
1645
1646
1647 #ifdef __APPLE__
1648 /*
1649 * The following are macros on BSD and functions on Darwin
1650 */
1651
1652 /*
1653 * Do we need to notify the other side when I/O is possible?
1654 */
1655
1656 int
1657 sb_notify(struct sockbuf *sb)
1658 {
1659 return ((sb->sb_flags &
1660 (SB_WAIT|SB_SEL|SB_ASYNC|SB_UPCALL|SB_KNOTE)) != 0);
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
1667 * overflow and return 0.
1668 */
1669 int
1670 sbspace(struct sockbuf *sb)
1671 {
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;
1679 }
1680
1681 /* do we have to send all at once on a socket? */
1682 int
1683 sosendallatonce(struct socket *so)
1684 {
1685 return (so->so_proto->pr_flags & PR_ATOMIC);
1686 }
1687
1688 /* can we read something from so? */
1689 int
1690 soreadable(struct socket *so)
1691 {
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);
1695 }
1696
1697 /* can we write something to so? */
1698
1699 int
1700 sowriteable(struct socket *so)
1701 {
1702 return ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat &&
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);
1707 }
1708
1709 /* adjust counters in sb reflecting allocation of m */
1710
1711 void
1712 sballoc(struct sockbuf *sb, struct mbuf *m)
1713 {
1714 int cnt = 1;
1715 sb->sb_cc += m->m_len;
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;
1719 sb->sb_mbcnt += MSIZE;
1720
1721 if (m->m_flags & M_EXT) {
1722 sb->sb_mbcnt += m->m_ext.ext_size;
1723 cnt += m->m_ext.ext_size / MSIZE ;
1724 }
1725 OSAddAtomic(cnt, &total_mb_cnt);
1726 }
1727
1728 /* adjust counters in sb reflecting freeing of m */
1729 void
1730 sbfree(struct sockbuf *sb, struct mbuf *m)
1731 {
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;
1737 sb->sb_mbcnt -= MSIZE;
1738 if (m->m_flags & M_EXT) {
1739 sb->sb_mbcnt -= m->m_ext.ext_size;
1740 cnt -= m->m_ext.ext_size / MSIZE ;
1741 }
1742 OSAddAtomic(cnt, &total_mb_cnt);
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.
1749 *
1750 * Returns: 0 Success
1751 * EWOULDBLOCK
1752 * sb_lock:EINTR
1753 */
1754 int
1755 sblock(struct sockbuf *sb, int wf)
1756 {
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);
1765 }
1766
1767 /* release lock on sockbuf sb */
1768 void
1769 sbunlock(struct sockbuf *sb, int keeplocked)
1770 {
1771 struct socket *so = sb->sb_so;
1772 void *lr_saved;
1773 lck_mtx_t *mutex_held;
1774
1775 lr_saved = __builtin_return_address(0);
1776
1777 sb->sb_flags &= ~SB_LOCK;
1778
1779 if (sb->sb_flags & SB_WANT) {
1780 sb->sb_flags &= ~SB_WANT;
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 }
1787 wakeup((caddr_t)&(sb)->sb_flags);
1788 }
1789 if (keeplocked == 0) { /* unlock on exit */
1790 if (so->so_proto->pr_getlock != NULL)
1791 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
1792 else
1793 mutex_held = so->so_proto->pr_domain->dom_mtx;
1794
1795 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
1796
1797 so->so_usecount--;
1798 if (so->so_usecount < 0)
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;
1803 so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
1804 lck_mtx_unlock(mutex_held);
1805 }
1806 }
1807
1808 void
1809 sorwakeup(struct socket *so)
1810 {
1811 if (sb_notify(&so->so_rcv))
1812 sowakeup(so, &so->so_rcv);
1813 }
1814
1815 void
1816 sowwakeup(struct socket *so)
1817 {
1818 if (sb_notify(&so->so_snd))
1819 sowakeup(so, &so->so_snd);
1820 }
1821 #endif /* __APPLE__ */
1822
1823 /*
1824 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
1825 */
1826 struct sockaddr *
1827 dup_sockaddr(struct sockaddr *sa, int canwait)
1828 {
1829 struct sockaddr *sa2;
1830
1831 MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
1832 canwait ? M_WAITOK : M_NOWAIT);
1833 if (sa2)
1834 bcopy(sa, sa2, sa->sa_len);
1835 return (sa2);
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 */
1846 void
1847 sotoxsocket(struct socket *so, struct xsocket *xso)
1848 {
1849 xso->xso_len = sizeof (*xso);
1850 xso->xso_so = (_XSOCKET_PTR(struct socket *))(uintptr_t)so;
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;
1855 xso->so_pcb = (_XSOCKET_PTR(caddr_t))(uintptr_t)so->so_pcb;
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;
1859 } else {
1860 xso->xso_protocol = xso->xso_family = 0;
1861 }
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
1874
1875 #if !CONFIG_EMBEDDED
1876
1877 void
1878 sotoxsocket64(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
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 */
1913 void
1914 sbtoxsockbuf(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;
1922 xsb->sb_timeo = (short)
1923 (sb->sb_timeo.tv_sec * hz) + sb->sb_timeo.tv_usec / tick;
1924 if (xsb->sb_timeo == 0 && sb->sb_timeo.tv_usec != 0)
1925 xsb->sb_timeo = 1;
1926 }
1927
1928 int
1929 soisbackground(struct socket *so)
1930 {
1931 return (so->so_traffic_mgt_flags & TRAFFIC_MGT_SO_BACKGROUND);
1932 }
1933
1934
1935 /*
1936 * Here is the definition of some of the basic objects in the kern.ipc
1937 * branch of the MIB.
1938 */
1939 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW|CTLFLAG_LOCKED|CTLFLAG_ANYBODY, 0, "IPC");
1940
1941 /* Check that the maximum socket buffer size is within a range */
1942
1943 static int
1944 sysctl_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
1962 SYSCTL_PROC(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
1963 &sb_max, 0, &sysctl_sb_max, "IU", "Maximum socket buffer size");
1964
1965 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD | CTLFLAG_LOCKED,
1966 &maxsockets, 0, "Maximum number of sockets avaliable");
1967 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW | CTLFLAG_LOCKED,
1968 &sb_efficiency, 0, "");
1969 SYSCTL_INT(_kern_ipc, OID_AUTO, sbspace_factor, CTLFLAG_RW | CTLFLAG_LOCKED,
1970 &sbspace_factor, 0, "Ratio of mbuf/cluster use for socket layers");
1971 SYSCTL_INT(_kern_ipc, KIPC_NMBCLUSTERS, nmbclusters, CTLFLAG_RD | CTLFLAG_LOCKED,
1972 &nmbclusters, 0, "");
1973 SYSCTL_INT(_kern_ipc, OID_AUTO, njcl, CTLFLAG_RD | CTLFLAG_LOCKED, &njcl, 0, "");
1974 SYSCTL_INT(_kern_ipc, OID_AUTO, njclbytes, CTLFLAG_RD | CTLFLAG_LOCKED, &njclbytes, 0, "");
1975 SYSCTL_INT(_kern_ipc, KIPC_SOQLIMITCOMPAT, soqlimitcompat, CTLFLAG_RW | CTLFLAG_LOCKED,
1976 &soqlimitcompat, 1, "Enable socket queue limit compatibility");
1977 SYSCTL_INT(_kern_ipc, OID_AUTO, soqlencomp, CTLFLAG_RW | CTLFLAG_LOCKED,
1978 &soqlencomp, 0, "Listen backlog represents only complete queue");