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