1 /* $NetBSD: uipc_socket.c,v 1.170 2008/08/04 03:55:47 tls Exp $ */ 2 3 /*- 4 * Copyright (c) 2002, 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of Wasabi Systems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 2004 The FreeBSD Foundation 34 * Copyright (c) 2004 Robert Watson 35 * Copyright (c) 1982, 1986, 1988, 1990, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 * 62 * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95 63 */ 64 65 #include <sys/cdefs.h> 66 __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.170 2008/08/04 03:55:47 tls Exp $"); 67 68 #include "opt_inet.h" 69 #include "opt_sock_counters.h" 70 #include "opt_sosend_loan.h" 71 #include "opt_mbuftrace.h" 72 #include "opt_somaxkva.h" 73 #include "opt_multiprocessor.h" /* XXX */ 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/proc.h> 78 #include <sys/file.h> 79 #include <sys/filedesc.h> 80 #include <sys/malloc.h> 81 #include <sys/mbuf.h> 82 #include <sys/domain.h> 83 #include <sys/kernel.h> 84 #include <sys/protosw.h> 85 #include <sys/socket.h> 86 #include <sys/socketvar.h> 87 #include <sys/signalvar.h> 88 #include <sys/resourcevar.h> 89 #include <sys/event.h> 90 #include <sys/poll.h> 91 #include <sys/kauth.h> 92 #include <sys/mutex.h> 93 #include <sys/condvar.h> 94 95 #include <uvm/uvm.h> 96 97 MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); 98 MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 99 100 extern const struct fileops socketops; 101 102 extern int somaxconn; /* patchable (XXX sysctl) */ 103 int somaxconn = SOMAXCONN; 104 kmutex_t *softnet_lock; 105 106 #ifdef SOSEND_COUNTERS 107 #include <sys/device.h> 108 109 static struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 110 NULL, "sosend", "loan big"); 111 static struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 112 NULL, "sosend", "copy big"); 113 static struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 114 NULL, "sosend", "copy small"); 115 static struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 116 NULL, "sosend", "kva limit"); 117 118 #define SOSEND_COUNTER_INCR(ev) (ev)->ev_count++ 119 120 EVCNT_ATTACH_STATIC(sosend_loan_big); 121 EVCNT_ATTACH_STATIC(sosend_copy_big); 122 EVCNT_ATTACH_STATIC(sosend_copy_small); 123 EVCNT_ATTACH_STATIC(sosend_kvalimit); 124 #else 125 126 #define SOSEND_COUNTER_INCR(ev) /* nothing */ 127 128 #endif /* SOSEND_COUNTERS */ 129 130 static struct callback_entry sokva_reclaimerentry; 131 132 #if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR) 133 int sock_loan_thresh = -1; 134 #else 135 int sock_loan_thresh = 4096; 136 #endif 137 138 static kmutex_t so_pendfree_lock; 139 static struct mbuf *so_pendfree; 140 141 #ifndef SOMAXKVA 142 #define SOMAXKVA (16 * 1024 * 1024) 143 #endif 144 int somaxkva = SOMAXKVA; 145 static int socurkva; 146 static kcondvar_t socurkva_cv; 147 148 #define SOCK_LOAN_CHUNK 65536 149 150 static size_t sodopendfree(void); 151 static size_t sodopendfreel(void); 152 153 static vsize_t 154 sokvareserve(struct socket *so, vsize_t len) 155 { 156 int error; 157 158 mutex_enter(&so_pendfree_lock); 159 while (socurkva + len > somaxkva) { 160 size_t freed; 161 162 /* 163 * try to do pendfree. 164 */ 165 166 freed = sodopendfreel(); 167 168 /* 169 * if some kva was freed, try again. 170 */ 171 172 if (freed) 173 continue; 174 175 SOSEND_COUNTER_INCR(&sosend_kvalimit); 176 error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock); 177 if (error) { 178 len = 0; 179 break; 180 } 181 } 182 socurkva += len; 183 mutex_exit(&so_pendfree_lock); 184 return len; 185 } 186 187 static void 188 sokvaunreserve(vsize_t len) 189 { 190 191 mutex_enter(&so_pendfree_lock); 192 socurkva -= len; 193 cv_broadcast(&socurkva_cv); 194 mutex_exit(&so_pendfree_lock); 195 } 196 197 /* 198 * sokvaalloc: allocate kva for loan. 199 */ 200 201 vaddr_t 202 sokvaalloc(vsize_t len, struct socket *so) 203 { 204 vaddr_t lva; 205 206 /* 207 * reserve kva. 208 */ 209 210 if (sokvareserve(so, len) == 0) 211 return 0; 212 213 /* 214 * allocate kva. 215 */ 216 217 lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA); 218 if (lva == 0) { 219 sokvaunreserve(len); 220 return (0); 221 } 222 223 return lva; 224 } 225 226 /* 227 * sokvafree: free kva for loan. 228 */ 229 230 void 231 sokvafree(vaddr_t sva, vsize_t len) 232 { 233 234 /* 235 * free kva. 236 */ 237 238 uvm_km_free(kernel_map, sva, len, UVM_KMF_VAONLY); 239 240 /* 241 * unreserve kva. 242 */ 243 244 sokvaunreserve(len); 245 } 246 247 static void 248 sodoloanfree(struct vm_page **pgs, void *buf, size_t size) 249 { 250 vaddr_t sva, eva; 251 vsize_t len; 252 int npgs; 253 254 KASSERT(pgs != NULL); 255 256 eva = round_page((vaddr_t) buf + size); 257 sva = trunc_page((vaddr_t) buf); 258 len = eva - sva; 259 npgs = len >> PAGE_SHIFT; 260 261 pmap_kremove(sva, len); 262 pmap_update(pmap_kernel()); 263 uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE); 264 sokvafree(sva, len); 265 } 266 267 static size_t 268 sodopendfree(void) 269 { 270 size_t rv; 271 272 if (__predict_true(so_pendfree == NULL)) 273 return 0; 274 275 mutex_enter(&so_pendfree_lock); 276 rv = sodopendfreel(); 277 mutex_exit(&so_pendfree_lock); 278 279 return rv; 280 } 281 282 /* 283 * sodopendfreel: free mbufs on "pendfree" list. 284 * unlock and relock so_pendfree_lock when freeing mbufs. 285 * 286 * => called with so_pendfree_lock held. 287 */ 288 289 static size_t 290 sodopendfreel(void) 291 { 292 struct mbuf *m, *next; 293 size_t rv = 0; 294 295 KASSERT(mutex_owned(&so_pendfree_lock)); 296 297 while (so_pendfree != NULL) { 298 m = so_pendfree; 299 so_pendfree = NULL; 300 mutex_exit(&so_pendfree_lock); 301 302 for (; m != NULL; m = next) { 303 next = m->m_next; 304 KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0); 305 KASSERT(m->m_ext.ext_refcnt == 0); 306 307 rv += m->m_ext.ext_size; 308 sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf, 309 m->m_ext.ext_size); 310 pool_cache_put(mb_cache, m); 311 } 312 313 mutex_enter(&so_pendfree_lock); 314 } 315 316 return (rv); 317 } 318 319 void 320 soloanfree(struct mbuf *m, void *buf, size_t size, void *arg) 321 { 322 323 KASSERT(m != NULL); 324 325 /* 326 * postpone freeing mbuf. 327 * 328 * we can't do it in interrupt context 329 * because we need to put kva back to kernel_map. 330 */ 331 332 mutex_enter(&so_pendfree_lock); 333 m->m_next = so_pendfree; 334 so_pendfree = m; 335 cv_broadcast(&socurkva_cv); 336 mutex_exit(&so_pendfree_lock); 337 } 338 339 static long 340 sosend_loan(struct socket *so, struct uio *uio, struct mbuf *m, long space) 341 { 342 struct iovec *iov = uio->uio_iov; 343 vaddr_t sva, eva; 344 vsize_t len; 345 vaddr_t lva; 346 int npgs, error; 347 vaddr_t va; 348 int i; 349 350 if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) 351 return (0); 352 353 if (iov->iov_len < (size_t) space) 354 space = iov->iov_len; 355 if (space > SOCK_LOAN_CHUNK) 356 space = SOCK_LOAN_CHUNK; 357 358 eva = round_page((vaddr_t) iov->iov_base + space); 359 sva = trunc_page((vaddr_t) iov->iov_base); 360 len = eva - sva; 361 npgs = len >> PAGE_SHIFT; 362 363 KASSERT(npgs <= M_EXT_MAXPAGES); 364 365 lva = sokvaalloc(len, so); 366 if (lva == 0) 367 return 0; 368 369 error = uvm_loan(&uio->uio_vmspace->vm_map, sva, len, 370 m->m_ext.ext_pgs, UVM_LOAN_TOPAGE); 371 if (error) { 372 sokvafree(lva, len); 373 return (0); 374 } 375 376 for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE) 377 pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]), 378 VM_PROT_READ); 379 pmap_update(pmap_kernel()); 380 381 lva += (vaddr_t) iov->iov_base & PAGE_MASK; 382 383 MEXTADD(m, (void *) lva, space, M_MBUF, soloanfree, so); 384 m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP; 385 386 uio->uio_resid -= space; 387 /* uio_offset not updated, not set/used for write(2) */ 388 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + space; 389 uio->uio_iov->iov_len -= space; 390 if (uio->uio_iov->iov_len == 0) { 391 uio->uio_iov++; 392 uio->uio_iovcnt--; 393 } 394 395 return (space); 396 } 397 398 static int 399 sokva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg) 400 { 401 402 KASSERT(ce == &sokva_reclaimerentry); 403 KASSERT(obj == NULL); 404 405 sodopendfree(); 406 if (!vm_map_starved_p(kernel_map)) { 407 return CALLBACK_CHAIN_ABORT; 408 } 409 return CALLBACK_CHAIN_CONTINUE; 410 } 411 412 struct mbuf * 413 getsombuf(struct socket *so, int type) 414 { 415 struct mbuf *m; 416 417 m = m_get(M_WAIT, type); 418 MCLAIM(m, so->so_mowner); 419 return m; 420 } 421 422 struct mbuf * 423 m_intopt(struct socket *so, int val) 424 { 425 struct mbuf *m; 426 427 m = getsombuf(so, MT_SOOPTS); 428 m->m_len = sizeof(int); 429 *mtod(m, int *) = val; 430 return m; 431 } 432 433 void 434 soinit(void) 435 { 436 437 mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM); 438 softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 439 cv_init(&socurkva_cv, "sokva"); 440 soinit2(); 441 442 /* Set the initial adjusted socket buffer size. */ 443 if (sb_max_set(sb_max)) 444 panic("bad initial sb_max value: %lu", sb_max); 445 446 callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback, 447 &sokva_reclaimerentry, NULL, sokva_reclaim_callback); 448 } 449 450 /* 451 * Socket operation routines. 452 * These routines are called by the routines in 453 * sys_socket.c or from a system process, and 454 * implement the semantics of socket operations by 455 * switching out to the protocol specific routines. 456 */ 457 /*ARGSUSED*/ 458 int 459 socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l, 460 struct socket *lockso) 461 { 462 const struct protosw *prp; 463 struct socket *so; 464 uid_t uid; 465 int error; 466 kmutex_t *lock; 467 468 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, 469 KAUTH_REQ_NETWORK_SOCKET_OPEN, KAUTH_ARG(dom), KAUTH_ARG(type), 470 KAUTH_ARG(proto)); 471 if (error != 0) 472 return error; 473 474 if (proto) 475 prp = pffindproto(dom, proto, type); 476 else 477 prp = pffindtype(dom, type); 478 if (prp == NULL) { 479 /* no support for domain */ 480 if (pffinddomain(dom) == 0) 481 return EAFNOSUPPORT; 482 /* no support for socket type */ 483 if (proto == 0 && type != 0) 484 return EPROTOTYPE; 485 return EPROTONOSUPPORT; 486 } 487 if (prp->pr_usrreq == NULL) 488 return EPROTONOSUPPORT; 489 if (prp->pr_type != type) 490 return EPROTOTYPE; 491 492 so = soget(true); 493 so->so_type = type; 494 so->so_proto = prp; 495 so->so_send = sosend; 496 so->so_receive = soreceive; 497 #ifdef MBUFTRACE 498 so->so_rcv.sb_mowner = &prp->pr_domain->dom_mowner; 499 so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner; 500 so->so_mowner = &prp->pr_domain->dom_mowner; 501 #endif 502 uid = kauth_cred_geteuid(l->l_cred); 503 so->so_uidinfo = uid_find(uid); 504 so->so_egid = kauth_cred_getegid(l->l_cred); 505 so->so_cpid = l->l_proc->p_pid; 506 if (lockso != NULL) { 507 /* Caller wants us to share a lock. */ 508 lock = lockso->so_lock; 509 so->so_lock = lock; 510 mutex_obj_hold(lock); 511 mutex_enter(lock); 512 } else { 513 /* Lock assigned and taken during PRU_ATTACH. */ 514 } 515 error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL, 516 (struct mbuf *)(long)proto, NULL, l); 517 KASSERT(solocked(so)); 518 if (error != 0) { 519 so->so_state |= SS_NOFDREF; 520 sofree(so); 521 return error; 522 } 523 sounlock(so); 524 *aso = so; 525 return 0; 526 } 527 528 /* On success, write file descriptor to fdout and return zero. On 529 * failure, return non-zero; *fdout will be undefined. 530 */ 531 int 532 fsocreate(int domain, struct socket **sop, int type, int protocol, 533 struct lwp *l, int *fdout) 534 { 535 struct socket *so; 536 struct file *fp; 537 int fd, error; 538 539 if ((error = fd_allocfile(&fp, &fd)) != 0) 540 return (error); 541 fp->f_flag = FREAD|FWRITE; 542 fp->f_type = DTYPE_SOCKET; 543 fp->f_ops = &socketops; 544 error = socreate(domain, &so, type, protocol, l, NULL); 545 if (error != 0) { 546 fd_abort(curproc, fp, fd); 547 } else { 548 if (sop != NULL) 549 *sop = so; 550 fp->f_data = so; 551 fd_affix(curproc, fp, fd); 552 *fdout = fd; 553 } 554 return error; 555 } 556 557 int 558 sobind(struct socket *so, struct mbuf *nam, struct lwp *l) 559 { 560 int error; 561 562 solock(so); 563 error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l); 564 sounlock(so); 565 return error; 566 } 567 568 int 569 solisten(struct socket *so, int backlog, struct lwp *l) 570 { 571 int error; 572 573 solock(so); 574 if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | 575 SS_ISDISCONNECTING)) != 0) { 576 sounlock(so); 577 return (EOPNOTSUPP); 578 } 579 error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, 580 NULL, NULL, l); 581 if (error != 0) { 582 sounlock(so); 583 return error; 584 } 585 if (TAILQ_EMPTY(&so->so_q)) 586 so->so_options |= SO_ACCEPTCONN; 587 if (backlog < 0) 588 backlog = 0; 589 so->so_qlimit = min(backlog, somaxconn); 590 sounlock(so); 591 return 0; 592 } 593 594 void 595 sofree(struct socket *so) 596 { 597 u_int refs; 598 599 KASSERT(solocked(so)); 600 601 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) { 602 sounlock(so); 603 return; 604 } 605 if (so->so_head) { 606 /* 607 * We must not decommission a socket that's on the accept(2) 608 * queue. If we do, then accept(2) may hang after select(2) 609 * indicated that the listening socket was ready. 610 */ 611 if (!soqremque(so, 0)) { 612 sounlock(so); 613 return; 614 } 615 } 616 if (so->so_rcv.sb_hiwat) 617 (void)chgsbsize(so->so_uidinfo, &so->so_rcv.sb_hiwat, 0, 618 RLIM_INFINITY); 619 if (so->so_snd.sb_hiwat) 620 (void)chgsbsize(so->so_uidinfo, &so->so_snd.sb_hiwat, 0, 621 RLIM_INFINITY); 622 sbrelease(&so->so_snd, so); 623 KASSERT(!cv_has_waiters(&so->so_cv)); 624 KASSERT(!cv_has_waiters(&so->so_rcv.sb_cv)); 625 KASSERT(!cv_has_waiters(&so->so_snd.sb_cv)); 626 sorflush(so); 627 refs = so->so_aborting; /* XXX */ 628 #ifdef INET 629 /* remove acccept filter if one is present. */ 630 if (so->so_accf != NULL) 631 do_setopt_accept_filter(so, NULL); 632 #endif 633 sounlock(so); 634 if (refs == 0) /* XXX */ 635 soput(so); 636 } 637 638 /* 639 * Close a socket on last file table reference removal. 640 * Initiate disconnect if connected. 641 * Free socket when disconnect complete. 642 */ 643 int 644 soclose(struct socket *so) 645 { 646 struct socket *so2; 647 int error; 648 int error2; 649 650 error = 0; 651 solock(so); 652 if (so->so_options & SO_ACCEPTCONN) { 653 do { 654 while ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) { 655 KASSERT(solocked2(so, so2)); 656 (void) soqremque(so2, 0); 657 /* soabort drops the lock. */ 658 (void) soabort(so2); 659 solock(so); 660 } 661 while ((so2 = TAILQ_FIRST(&so->so_q)) != 0) { 662 KASSERT(solocked2(so, so2)); 663 (void) soqremque(so2, 1); 664 /* soabort drops the lock. */ 665 (void) soabort(so2); 666 solock(so); 667 } 668 } while (!TAILQ_EMPTY(&so->so_q0)); 669 } 670 if (so->so_pcb == 0) 671 goto discard; 672 if (so->so_state & SS_ISCONNECTED) { 673 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 674 error = sodisconnect(so); 675 if (error) 676 goto drop; 677 } 678 if (so->so_options & SO_LINGER) { 679 if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio) 680 goto drop; 681 while (so->so_state & SS_ISCONNECTED) { 682 error = sowait(so, so->so_linger * hz); 683 if (error) 684 break; 685 } 686 } 687 } 688 drop: 689 if (so->so_pcb) { 690 error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 691 NULL, NULL, NULL, NULL); 692 if (error == 0) 693 error = error2; 694 } 695 discard: 696 if (so->so_state & SS_NOFDREF) 697 panic("soclose: NOFDREF"); 698 so->so_state |= SS_NOFDREF; 699 sofree(so); 700 return (error); 701 } 702 703 /* 704 * Must be called with the socket locked.. Will return with it unlocked. 705 */ 706 int 707 soabort(struct socket *so) 708 { 709 u_int refs; 710 int error; 711 712 KASSERT(solocked(so)); 713 KASSERT(so->so_head == NULL); 714 715 so->so_aborting++; /* XXX */ 716 error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, 717 NULL, NULL, NULL); 718 refs = --so->so_aborting; /* XXX */ 719 if (error || (refs == 0)) { 720 sofree(so); 721 } else { 722 sounlock(so); 723 } 724 return error; 725 } 726 727 int 728 soaccept(struct socket *so, struct mbuf *nam) 729 { 730 int error; 731 732 KASSERT(solocked(so)); 733 734 error = 0; 735 if ((so->so_state & SS_NOFDREF) == 0) 736 panic("soaccept: !NOFDREF"); 737 so->so_state &= ~SS_NOFDREF; 738 if ((so->so_state & SS_ISDISCONNECTED) == 0 || 739 (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) 740 error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 741 NULL, nam, NULL, NULL); 742 else 743 error = ECONNABORTED; 744 745 return (error); 746 } 747 748 int 749 soconnect(struct socket *so, struct mbuf *nam, struct lwp *l) 750 { 751 int error; 752 753 KASSERT(solocked(so)); 754 755 if (so->so_options & SO_ACCEPTCONN) 756 return (EOPNOTSUPP); 757 /* 758 * If protocol is connection-based, can only connect once. 759 * Otherwise, if connected, try to disconnect first. 760 * This allows user to disconnect by connecting to, e.g., 761 * a null address. 762 */ 763 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 764 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 765 (error = sodisconnect(so)))) 766 error = EISCONN; 767 else 768 error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 769 NULL, nam, NULL, l); 770 return (error); 771 } 772 773 int 774 soconnect2(struct socket *so1, struct socket *so2) 775 { 776 int error; 777 778 KASSERT(solocked2(so1, so2)); 779 780 error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, 781 NULL, (struct mbuf *)so2, NULL, NULL); 782 return (error); 783 } 784 785 int 786 sodisconnect(struct socket *so) 787 { 788 int error; 789 790 KASSERT(solocked(so)); 791 792 if ((so->so_state & SS_ISCONNECTED) == 0) { 793 error = ENOTCONN; 794 } else if (so->so_state & SS_ISDISCONNECTING) { 795 error = EALREADY; 796 } else { 797 error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 798 NULL, NULL, NULL, NULL); 799 } 800 sodopendfree(); 801 return (error); 802 } 803 804 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 805 /* 806 * Send on a socket. 807 * If send must go all at once and message is larger than 808 * send buffering, then hard error. 809 * Lock against other senders. 810 * If must go all at once and not enough room now, then 811 * inform user that this would block and do nothing. 812 * Otherwise, if nonblocking, send as much as possible. 813 * The data to be sent is described by "uio" if nonzero, 814 * otherwise by the mbuf chain "top" (which must be null 815 * if uio is not). Data provided in mbuf chain must be small 816 * enough to send all at once. 817 * 818 * Returns nonzero on error, timeout or signal; callers 819 * must check for short counts if EINTR/ERESTART are returned. 820 * Data and control buffers are freed on return. 821 */ 822 int 823 sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top, 824 struct mbuf *control, int flags, struct lwp *l) 825 { 826 struct mbuf **mp, *m; 827 struct proc *p; 828 long space, len, resid, clen, mlen; 829 int error, s, dontroute, atomic; 830 831 p = l->l_proc; 832 sodopendfree(); 833 clen = 0; 834 835 /* 836 * solock() provides atomicity of access. splsoftnet() prevents 837 * protocol processing soft interrupts from interrupting us and 838 * blocking (expensive). 839 */ 840 s = splsoftnet(); 841 solock(so); 842 atomic = sosendallatonce(so) || top; 843 if (uio) 844 resid = uio->uio_resid; 845 else 846 resid = top->m_pkthdr.len; 847 /* 848 * In theory resid should be unsigned. 849 * However, space must be signed, as it might be less than 0 850 * if we over-committed, and we must use a signed comparison 851 * of space and resid. On the other hand, a negative resid 852 * causes us to loop sending 0-length segments to the protocol. 853 */ 854 if (resid < 0) { 855 error = EINVAL; 856 goto out; 857 } 858 dontroute = 859 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 860 (so->so_proto->pr_flags & PR_ATOMIC); 861 l->l_ru.ru_msgsnd++; 862 if (control) 863 clen = control->m_len; 864 restart: 865 if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0) 866 goto out; 867 do { 868 if (so->so_state & SS_CANTSENDMORE) { 869 error = EPIPE; 870 goto release; 871 } 872 if (so->so_error) { 873 error = so->so_error; 874 so->so_error = 0; 875 goto release; 876 } 877 if ((so->so_state & SS_ISCONNECTED) == 0) { 878 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 879 if ((so->so_state & SS_ISCONFIRMING) == 0 && 880 !(resid == 0 && clen != 0)) { 881 error = ENOTCONN; 882 goto release; 883 } 884 } else if (addr == 0) { 885 error = EDESTADDRREQ; 886 goto release; 887 } 888 } 889 space = sbspace(&so->so_snd); 890 if (flags & MSG_OOB) 891 space += 1024; 892 if ((atomic && resid > so->so_snd.sb_hiwat) || 893 clen > so->so_snd.sb_hiwat) { 894 error = EMSGSIZE; 895 goto release; 896 } 897 if (space < resid + clen && 898 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 899 if (so->so_nbio) { 900 error = EWOULDBLOCK; 901 goto release; 902 } 903 sbunlock(&so->so_snd); 904 error = sbwait(&so->so_snd); 905 if (error) 906 goto out; 907 goto restart; 908 } 909 mp = ⊤ 910 space -= clen; 911 do { 912 if (uio == NULL) { 913 /* 914 * Data is prepackaged in "top". 915 */ 916 resid = 0; 917 if (flags & MSG_EOR) 918 top->m_flags |= M_EOR; 919 } else do { 920 sounlock(so); 921 splx(s); 922 if (top == NULL) { 923 m = m_gethdr(M_WAIT, MT_DATA); 924 mlen = MHLEN; 925 m->m_pkthdr.len = 0; 926 m->m_pkthdr.rcvif = NULL; 927 } else { 928 m = m_get(M_WAIT, MT_DATA); 929 mlen = MLEN; 930 } 931 MCLAIM(m, so->so_snd.sb_mowner); 932 if (sock_loan_thresh >= 0 && 933 uio->uio_iov->iov_len >= sock_loan_thresh && 934 space >= sock_loan_thresh && 935 (len = sosend_loan(so, uio, m, 936 space)) != 0) { 937 SOSEND_COUNTER_INCR(&sosend_loan_big); 938 space -= len; 939 goto have_data; 940 } 941 if (resid >= MINCLSIZE && space >= MCLBYTES) { 942 SOSEND_COUNTER_INCR(&sosend_copy_big); 943 m_clget(m, M_WAIT); 944 if ((m->m_flags & M_EXT) == 0) 945 goto nopages; 946 mlen = MCLBYTES; 947 if (atomic && top == 0) { 948 len = lmin(MCLBYTES - max_hdr, 949 resid); 950 m->m_data += max_hdr; 951 } else 952 len = lmin(MCLBYTES, resid); 953 space -= len; 954 } else { 955 nopages: 956 SOSEND_COUNTER_INCR(&sosend_copy_small); 957 len = lmin(lmin(mlen, resid), space); 958 space -= len; 959 /* 960 * For datagram protocols, leave room 961 * for protocol headers in first mbuf. 962 */ 963 if (atomic && top == 0 && len < mlen) 964 MH_ALIGN(m, len); 965 } 966 error = uiomove(mtod(m, void *), (int)len, uio); 967 have_data: 968 resid = uio->uio_resid; 969 m->m_len = len; 970 *mp = m; 971 top->m_pkthdr.len += len; 972 s = splsoftnet(); 973 solock(so); 974 if (error != 0) 975 goto release; 976 mp = &m->m_next; 977 if (resid <= 0) { 978 if (flags & MSG_EOR) 979 top->m_flags |= M_EOR; 980 break; 981 } 982 } while (space > 0 && atomic); 983 984 if (so->so_state & SS_CANTSENDMORE) { 985 error = EPIPE; 986 goto release; 987 } 988 if (dontroute) 989 so->so_options |= SO_DONTROUTE; 990 if (resid > 0) 991 so->so_state |= SS_MORETOCOME; 992 error = (*so->so_proto->pr_usrreq)(so, 993 (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, 994 top, addr, control, curlwp); 995 if (dontroute) 996 so->so_options &= ~SO_DONTROUTE; 997 if (resid > 0) 998 so->so_state &= ~SS_MORETOCOME; 999 clen = 0; 1000 control = NULL; 1001 top = NULL; 1002 mp = ⊤ 1003 if (error != 0) 1004 goto release; 1005 } while (resid && space > 0); 1006 } while (resid); 1007 1008 release: 1009 sbunlock(&so->so_snd); 1010 out: 1011 sounlock(so); 1012 splx(s); 1013 if (top) 1014 m_freem(top); 1015 if (control) 1016 m_freem(control); 1017 return (error); 1018 } 1019 1020 /* 1021 * Following replacement or removal of the first mbuf on the first 1022 * mbuf chain of a socket buffer, push necessary state changes back 1023 * into the socket buffer so that other consumers see the values 1024 * consistently. 'nextrecord' is the callers locally stored value of 1025 * the original value of sb->sb_mb->m_nextpkt which must be restored 1026 * when the lead mbuf changes. NOTE: 'nextrecord' may be NULL. 1027 */ 1028 static void 1029 sbsync(struct sockbuf *sb, struct mbuf *nextrecord) 1030 { 1031 1032 KASSERT(solocked(sb->sb_so)); 1033 1034 /* 1035 * First, update for the new value of nextrecord. If necessary, 1036 * make it the first record. 1037 */ 1038 if (sb->sb_mb != NULL) 1039 sb->sb_mb->m_nextpkt = nextrecord; 1040 else 1041 sb->sb_mb = nextrecord; 1042 1043 /* 1044 * Now update any dependent socket buffer fields to reflect 1045 * the new state. This is an inline of SB_EMPTY_FIXUP, with 1046 * the addition of a second clause that takes care of the 1047 * case where sb_mb has been updated, but remains the last 1048 * record. 1049 */ 1050 if (sb->sb_mb == NULL) { 1051 sb->sb_mbtail = NULL; 1052 sb->sb_lastrecord = NULL; 1053 } else if (sb->sb_mb->m_nextpkt == NULL) 1054 sb->sb_lastrecord = sb->sb_mb; 1055 } 1056 1057 /* 1058 * Implement receive operations on a socket. 1059 * We depend on the way that records are added to the sockbuf 1060 * by sbappend*. In particular, each record (mbufs linked through m_next) 1061 * must begin with an address if the protocol so specifies, 1062 * followed by an optional mbuf or mbufs containing ancillary data, 1063 * and then zero or more mbufs of data. 1064 * In order to avoid blocking network interrupts for the entire time here, 1065 * we splx() while doing the actual copy to user space. 1066 * Although the sockbuf is locked, new data may still be appended, 1067 * and thus we must maintain consistency of the sockbuf during that time. 1068 * 1069 * The caller may receive the data as a single mbuf chain by supplying 1070 * an mbuf **mp0 for use in returning the chain. The uio is then used 1071 * only for the count in uio_resid. 1072 */ 1073 int 1074 soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, 1075 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 1076 { 1077 struct lwp *l = curlwp; 1078 struct mbuf *m, **mp, *mt; 1079 int atomic, flags, len, error, s, offset, moff, type, orig_resid; 1080 const struct protosw *pr; 1081 struct mbuf *nextrecord; 1082 int mbuf_removed = 0; 1083 const struct domain *dom; 1084 1085 pr = so->so_proto; 1086 atomic = pr->pr_flags & PR_ATOMIC; 1087 dom = pr->pr_domain; 1088 mp = mp0; 1089 type = 0; 1090 orig_resid = uio->uio_resid; 1091 1092 if (paddr != NULL) 1093 *paddr = NULL; 1094 if (controlp != NULL) 1095 *controlp = NULL; 1096 if (flagsp != NULL) 1097 flags = *flagsp &~ MSG_EOR; 1098 else 1099 flags = 0; 1100 1101 if ((flags & MSG_DONTWAIT) == 0) 1102 sodopendfree(); 1103 1104 if (flags & MSG_OOB) { 1105 m = m_get(M_WAIT, MT_DATA); 1106 solock(so); 1107 error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, 1108 (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l); 1109 sounlock(so); 1110 if (error) 1111 goto bad; 1112 do { 1113 error = uiomove(mtod(m, void *), 1114 (int) min(uio->uio_resid, m->m_len), uio); 1115 m = m_free(m); 1116 } while (uio->uio_resid > 0 && error == 0 && m); 1117 bad: 1118 if (m != NULL) 1119 m_freem(m); 1120 return error; 1121 } 1122 if (mp != NULL) 1123 *mp = NULL; 1124 1125 /* 1126 * solock() provides atomicity of access. splsoftnet() prevents 1127 * protocol processing soft interrupts from interrupting us and 1128 * blocking (expensive). 1129 */ 1130 s = splsoftnet(); 1131 solock(so); 1132 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) 1133 (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l); 1134 1135 restart: 1136 if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) { 1137 sounlock(so); 1138 splx(s); 1139 return error; 1140 } 1141 1142 m = so->so_rcv.sb_mb; 1143 /* 1144 * If we have less data than requested, block awaiting more 1145 * (subject to any timeout) if: 1146 * 1. the current count is less than the low water mark, 1147 * 2. MSG_WAITALL is set, and it is possible to do the entire 1148 * receive operation at once if we block (resid <= hiwat), or 1149 * 3. MSG_DONTWAIT is not set. 1150 * If MSG_WAITALL is set but resid is larger than the receive buffer, 1151 * we have to do the receive in sections, and thus risk returning 1152 * a short count if a timeout or signal occurs after we start. 1153 */ 1154 if (m == NULL || 1155 ((flags & MSG_DONTWAIT) == 0 && 1156 so->so_rcv.sb_cc < uio->uio_resid && 1157 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 1158 ((flags & MSG_WAITALL) && 1159 uio->uio_resid <= so->so_rcv.sb_hiwat)) && 1160 m->m_nextpkt == NULL && !atomic)) { 1161 #ifdef DIAGNOSTIC 1162 if (m == NULL && so->so_rcv.sb_cc) 1163 panic("receive 1"); 1164 #endif 1165 if (so->so_error) { 1166 if (m != NULL) 1167 goto dontblock; 1168 error = so->so_error; 1169 if ((flags & MSG_PEEK) == 0) 1170 so->so_error = 0; 1171 goto release; 1172 } 1173 if (so->so_state & SS_CANTRCVMORE) { 1174 if (m != NULL) 1175 goto dontblock; 1176 else 1177 goto release; 1178 } 1179 for (; m != NULL; m = m->m_next) 1180 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 1181 m = so->so_rcv.sb_mb; 1182 goto dontblock; 1183 } 1184 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 1185 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 1186 error = ENOTCONN; 1187 goto release; 1188 } 1189 if (uio->uio_resid == 0) 1190 goto release; 1191 if (so->so_nbio || (flags & MSG_DONTWAIT)) { 1192 error = EWOULDBLOCK; 1193 goto release; 1194 } 1195 SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); 1196 SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); 1197 sbunlock(&so->so_rcv); 1198 error = sbwait(&so->so_rcv); 1199 if (error != 0) { 1200 sounlock(so); 1201 splx(s); 1202 return error; 1203 } 1204 goto restart; 1205 } 1206 dontblock: 1207 /* 1208 * On entry here, m points to the first record of the socket buffer. 1209 * From this point onward, we maintain 'nextrecord' as a cache of the 1210 * pointer to the next record in the socket buffer. We must keep the 1211 * various socket buffer pointers and local stack versions of the 1212 * pointers in sync, pushing out modifications before dropping the 1213 * socket lock, and re-reading them when picking it up. 1214 * 1215 * Otherwise, we will race with the network stack appending new data 1216 * or records onto the socket buffer by using inconsistent/stale 1217 * versions of the field, possibly resulting in socket buffer 1218 * corruption. 1219 * 1220 * By holding the high-level sblock(), we prevent simultaneous 1221 * readers from pulling off the front of the socket buffer. 1222 */ 1223 if (l != NULL) 1224 l->l_ru.ru_msgrcv++; 1225 KASSERT(m == so->so_rcv.sb_mb); 1226 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); 1227 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1"); 1228 nextrecord = m->m_nextpkt; 1229 if (pr->pr_flags & PR_ADDR) { 1230 #ifdef DIAGNOSTIC 1231 if (m->m_type != MT_SONAME) 1232 panic("receive 1a"); 1233 #endif 1234 orig_resid = 0; 1235 if (flags & MSG_PEEK) { 1236 if (paddr) 1237 *paddr = m_copy(m, 0, m->m_len); 1238 m = m->m_next; 1239 } else { 1240 sbfree(&so->so_rcv, m); 1241 mbuf_removed = 1; 1242 if (paddr != NULL) { 1243 *paddr = m; 1244 so->so_rcv.sb_mb = m->m_next; 1245 m->m_next = NULL; 1246 m = so->so_rcv.sb_mb; 1247 } else { 1248 MFREE(m, so->so_rcv.sb_mb); 1249 m = so->so_rcv.sb_mb; 1250 } 1251 sbsync(&so->so_rcv, nextrecord); 1252 } 1253 } 1254 1255 /* 1256 * Process one or more MT_CONTROL mbufs present before any data mbufs 1257 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we 1258 * just copy the data; if !MSG_PEEK, we call into the protocol to 1259 * perform externalization (or freeing if controlp == NULL). 1260 */ 1261 if (__predict_false(m != NULL && m->m_type == MT_CONTROL)) { 1262 struct mbuf *cm = NULL, *cmn; 1263 struct mbuf **cme = &cm; 1264 1265 do { 1266 if (flags & MSG_PEEK) { 1267 if (controlp != NULL) { 1268 *controlp = m_copy(m, 0, m->m_len); 1269 controlp = &(*controlp)->m_next; 1270 } 1271 m = m->m_next; 1272 } else { 1273 sbfree(&so->so_rcv, m); 1274 so->so_rcv.sb_mb = m->m_next; 1275 m->m_next = NULL; 1276 *cme = m; 1277 cme = &(*cme)->m_next; 1278 m = so->so_rcv.sb_mb; 1279 } 1280 } while (m != NULL && m->m_type == MT_CONTROL); 1281 if ((flags & MSG_PEEK) == 0) 1282 sbsync(&so->so_rcv, nextrecord); 1283 for (; cm != NULL; cm = cmn) { 1284 cmn = cm->m_next; 1285 cm->m_next = NULL; 1286 type = mtod(cm, struct cmsghdr *)->cmsg_type; 1287 if (controlp != NULL) { 1288 if (dom->dom_externalize != NULL && 1289 type == SCM_RIGHTS) { 1290 sounlock(so); 1291 splx(s); 1292 error = (*dom->dom_externalize)(cm, l); 1293 s = splsoftnet(); 1294 solock(so); 1295 } 1296 *controlp = cm; 1297 while (*controlp != NULL) 1298 controlp = &(*controlp)->m_next; 1299 } else { 1300 /* 1301 * Dispose of any SCM_RIGHTS message that went 1302 * through the read path rather than recv. 1303 */ 1304 if (dom->dom_dispose != NULL && 1305 type == SCM_RIGHTS) { 1306 sounlock(so); 1307 (*dom->dom_dispose)(cm); 1308 solock(so); 1309 } 1310 m_freem(cm); 1311 } 1312 } 1313 if (m != NULL) 1314 nextrecord = so->so_rcv.sb_mb->m_nextpkt; 1315 else 1316 nextrecord = so->so_rcv.sb_mb; 1317 orig_resid = 0; 1318 } 1319 1320 /* If m is non-NULL, we have some data to read. */ 1321 if (__predict_true(m != NULL)) { 1322 type = m->m_type; 1323 if (type == MT_OOBDATA) 1324 flags |= MSG_OOB; 1325 } 1326 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2"); 1327 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2"); 1328 1329 moff = 0; 1330 offset = 0; 1331 while (m != NULL && uio->uio_resid > 0 && error == 0) { 1332 if (m->m_type == MT_OOBDATA) { 1333 if (type != MT_OOBDATA) 1334 break; 1335 } else if (type == MT_OOBDATA) 1336 break; 1337 #ifdef DIAGNOSTIC 1338 else if (m->m_type != MT_DATA && m->m_type != MT_HEADER) 1339 panic("receive 3"); 1340 #endif 1341 so->so_state &= ~SS_RCVATMARK; 1342 len = uio->uio_resid; 1343 if (so->so_oobmark && len > so->so_oobmark - offset) 1344 len = so->so_oobmark - offset; 1345 if (len > m->m_len - moff) 1346 len = m->m_len - moff; 1347 /* 1348 * If mp is set, just pass back the mbufs. 1349 * Otherwise copy them out via the uio, then free. 1350 * Sockbuf must be consistent here (points to current mbuf, 1351 * it points to next record) when we drop priority; 1352 * we must note any additions to the sockbuf when we 1353 * block interrupts again. 1354 */ 1355 if (mp == NULL) { 1356 SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); 1357 SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); 1358 sounlock(so); 1359 splx(s); 1360 error = uiomove(mtod(m, char *) + moff, (int)len, uio); 1361 s = splsoftnet(); 1362 solock(so); 1363 if (error != 0) { 1364 /* 1365 * If any part of the record has been removed 1366 * (such as the MT_SONAME mbuf, which will 1367 * happen when PR_ADDR, and thus also 1368 * PR_ATOMIC, is set), then drop the entire 1369 * record to maintain the atomicity of the 1370 * receive operation. 1371 * 1372 * This avoids a later panic("receive 1a") 1373 * when compiled with DIAGNOSTIC. 1374 */ 1375 if (m && mbuf_removed && atomic) 1376 (void) sbdroprecord(&so->so_rcv); 1377 1378 goto release; 1379 } 1380 } else 1381 uio->uio_resid -= len; 1382 if (len == m->m_len - moff) { 1383 if (m->m_flags & M_EOR) 1384 flags |= MSG_EOR; 1385 if (flags & MSG_PEEK) { 1386 m = m->m_next; 1387 moff = 0; 1388 } else { 1389 nextrecord = m->m_nextpkt; 1390 sbfree(&so->so_rcv, m); 1391 if (mp) { 1392 *mp = m; 1393 mp = &m->m_next; 1394 so->so_rcv.sb_mb = m = m->m_next; 1395 *mp = NULL; 1396 } else { 1397 MFREE(m, so->so_rcv.sb_mb); 1398 m = so->so_rcv.sb_mb; 1399 } 1400 /* 1401 * If m != NULL, we also know that 1402 * so->so_rcv.sb_mb != NULL. 1403 */ 1404 KASSERT(so->so_rcv.sb_mb == m); 1405 if (m) { 1406 m->m_nextpkt = nextrecord; 1407 if (nextrecord == NULL) 1408 so->so_rcv.sb_lastrecord = m; 1409 } else { 1410 so->so_rcv.sb_mb = nextrecord; 1411 SB_EMPTY_FIXUP(&so->so_rcv); 1412 } 1413 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3"); 1414 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3"); 1415 } 1416 } else if (flags & MSG_PEEK) 1417 moff += len; 1418 else { 1419 if (mp != NULL) { 1420 mt = m_copym(m, 0, len, M_NOWAIT); 1421 if (__predict_false(mt == NULL)) { 1422 sounlock(so); 1423 mt = m_copym(m, 0, len, M_WAIT); 1424 solock(so); 1425 } 1426 *mp = mt; 1427 } 1428 m->m_data += len; 1429 m->m_len -= len; 1430 so->so_rcv.sb_cc -= len; 1431 } 1432 if (so->so_oobmark) { 1433 if ((flags & MSG_PEEK) == 0) { 1434 so->so_oobmark -= len; 1435 if (so->so_oobmark == 0) { 1436 so->so_state |= SS_RCVATMARK; 1437 break; 1438 } 1439 } else { 1440 offset += len; 1441 if (offset == so->so_oobmark) 1442 break; 1443 } 1444 } 1445 if (flags & MSG_EOR) 1446 break; 1447 /* 1448 * If the MSG_WAITALL flag is set (for non-atomic socket), 1449 * we must not quit until "uio->uio_resid == 0" or an error 1450 * termination. If a signal/timeout occurs, return 1451 * with a short count but without error. 1452 * Keep sockbuf locked against other readers. 1453 */ 1454 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && 1455 !sosendallatonce(so) && !nextrecord) { 1456 if (so->so_error || so->so_state & SS_CANTRCVMORE) 1457 break; 1458 /* 1459 * If we are peeking and the socket receive buffer is 1460 * full, stop since we can't get more data to peek at. 1461 */ 1462 if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0) 1463 break; 1464 /* 1465 * If we've drained the socket buffer, tell the 1466 * protocol in case it needs to do something to 1467 * get it filled again. 1468 */ 1469 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) 1470 (*pr->pr_usrreq)(so, PRU_RCVD, 1471 NULL, (struct mbuf *)(long)flags, NULL, l); 1472 SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); 1473 SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); 1474 error = sbwait(&so->so_rcv); 1475 if (error != 0) { 1476 sbunlock(&so->so_rcv); 1477 sounlock(so); 1478 splx(s); 1479 return 0; 1480 } 1481 if ((m = so->so_rcv.sb_mb) != NULL) 1482 nextrecord = m->m_nextpkt; 1483 } 1484 } 1485 1486 if (m && atomic) { 1487 flags |= MSG_TRUNC; 1488 if ((flags & MSG_PEEK) == 0) 1489 (void) sbdroprecord(&so->so_rcv); 1490 } 1491 if ((flags & MSG_PEEK) == 0) { 1492 if (m == NULL) { 1493 /* 1494 * First part is an inline SB_EMPTY_FIXUP(). Second 1495 * part makes sure sb_lastrecord is up-to-date if 1496 * there is still data in the socket buffer. 1497 */ 1498 so->so_rcv.sb_mb = nextrecord; 1499 if (so->so_rcv.sb_mb == NULL) { 1500 so->so_rcv.sb_mbtail = NULL; 1501 so->so_rcv.sb_lastrecord = NULL; 1502 } else if (nextrecord->m_nextpkt == NULL) 1503 so->so_rcv.sb_lastrecord = nextrecord; 1504 } 1505 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); 1506 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); 1507 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1508 (*pr->pr_usrreq)(so, PRU_RCVD, NULL, 1509 (struct mbuf *)(long)flags, NULL, l); 1510 } 1511 if (orig_resid == uio->uio_resid && orig_resid && 1512 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 1513 sbunlock(&so->so_rcv); 1514 goto restart; 1515 } 1516 1517 if (flagsp != NULL) 1518 *flagsp |= flags; 1519 release: 1520 sbunlock(&so->so_rcv); 1521 sounlock(so); 1522 splx(s); 1523 return error; 1524 } 1525 1526 int 1527 soshutdown(struct socket *so, int how) 1528 { 1529 const struct protosw *pr; 1530 int error; 1531 1532 KASSERT(solocked(so)); 1533 1534 pr = so->so_proto; 1535 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1536 return (EINVAL); 1537 1538 if (how == SHUT_RD || how == SHUT_RDWR) { 1539 sorflush(so); 1540 error = 0; 1541 } 1542 if (how == SHUT_WR || how == SHUT_RDWR) 1543 error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, 1544 NULL, NULL, NULL); 1545 1546 return error; 1547 } 1548 1549 void 1550 sorflush(struct socket *so) 1551 { 1552 struct sockbuf *sb, asb; 1553 const struct protosw *pr; 1554 1555 KASSERT(solocked(so)); 1556 1557 sb = &so->so_rcv; 1558 pr = so->so_proto; 1559 socantrcvmore(so); 1560 sb->sb_flags |= SB_NOINTR; 1561 (void )sblock(sb, M_WAITOK); 1562 sbunlock(sb); 1563 asb = *sb; 1564 /* 1565 * Clear most of the sockbuf structure, but leave some of the 1566 * fields valid. 1567 */ 1568 memset(&sb->sb_startzero, 0, 1569 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 1570 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) { 1571 sounlock(so); 1572 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 1573 solock(so); 1574 } 1575 sbrelease(&asb, so); 1576 } 1577 1578 static int 1579 sosetopt1(struct socket *so, int level, int optname, struct mbuf *m) 1580 { 1581 #ifdef INET 1582 int error, optval, val; 1583 #else 1584 int optval, val; 1585 #endif 1586 struct linger *l; 1587 struct sockbuf *sb; 1588 struct timeval *tv; 1589 1590 switch (optname) { 1591 1592 #ifdef INET 1593 case SO_ACCEPTFILTER: 1594 error = do_setopt_accept_filter(so, m); 1595 if (error) 1596 return error; 1597 break; 1598 #endif 1599 1600 case SO_LINGER: 1601 if (m == NULL || m->m_len != sizeof(struct linger)) 1602 return EINVAL; 1603 l = mtod(m, struct linger *); 1604 if (l->l_linger < 0 || l->l_linger > USHRT_MAX || 1605 l->l_linger > (INT_MAX / hz)) 1606 return EDOM; 1607 so->so_linger = l->l_linger; 1608 if (l->l_onoff) 1609 so->so_options |= SO_LINGER; 1610 else 1611 so->so_options &= ~SO_LINGER; 1612 break; 1613 1614 case SO_DEBUG: 1615 case SO_KEEPALIVE: 1616 case SO_DONTROUTE: 1617 case SO_USELOOPBACK: 1618 case SO_BROADCAST: 1619 case SO_REUSEADDR: 1620 case SO_REUSEPORT: 1621 case SO_OOBINLINE: 1622 case SO_TIMESTAMP: 1623 if (m == NULL || m->m_len < sizeof(int)) 1624 return EINVAL; 1625 if (*mtod(m, int *)) 1626 so->so_options |= optname; 1627 else 1628 so->so_options &= ~optname; 1629 break; 1630 1631 case SO_SNDBUF: 1632 case SO_RCVBUF: 1633 case SO_SNDLOWAT: 1634 case SO_RCVLOWAT: 1635 if (m == NULL || m->m_len < sizeof(int)) 1636 return EINVAL; 1637 1638 /* 1639 * Values < 1 make no sense for any of these 1640 * options, so disallow them. 1641 */ 1642 optval = *mtod(m, int *); 1643 if (optval < 1) 1644 return EINVAL; 1645 1646 switch (optname) { 1647 1648 case SO_SNDBUF: 1649 case SO_RCVBUF: 1650 sb = (optname == SO_SNDBUF) ? 1651 &so->so_snd : &so->so_rcv; 1652 if (sbreserve(sb, (u_long)optval, so) == 0) 1653 return ENOBUFS; 1654 sb->sb_flags &= ~SB_AUTOSIZE; 1655 break; 1656 1657 /* 1658 * Make sure the low-water is never greater than 1659 * the high-water. 1660 */ 1661 case SO_SNDLOWAT: 1662 so->so_snd.sb_lowat = 1663 (optval > so->so_snd.sb_hiwat) ? 1664 so->so_snd.sb_hiwat : optval; 1665 break; 1666 case SO_RCVLOWAT: 1667 so->so_rcv.sb_lowat = 1668 (optval > so->so_rcv.sb_hiwat) ? 1669 so->so_rcv.sb_hiwat : optval; 1670 break; 1671 } 1672 break; 1673 1674 case SO_SNDTIMEO: 1675 case SO_RCVTIMEO: 1676 if (m == NULL || m->m_len < sizeof(*tv)) 1677 return EINVAL; 1678 tv = mtod(m, struct timeval *); 1679 if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) 1680 return EDOM; 1681 val = tv->tv_sec * hz + tv->tv_usec / tick; 1682 if (val == 0 && tv->tv_usec != 0) 1683 val = 1; 1684 1685 switch (optname) { 1686 1687 case SO_SNDTIMEO: 1688 so->so_snd.sb_timeo = val; 1689 break; 1690 case SO_RCVTIMEO: 1691 so->so_rcv.sb_timeo = val; 1692 break; 1693 } 1694 break; 1695 1696 default: 1697 return ENOPROTOOPT; 1698 } 1699 return 0; 1700 } 1701 1702 int 1703 sosetopt(struct socket *so, int level, int optname, struct mbuf *m) 1704 { 1705 int error, prerr; 1706 1707 solock(so); 1708 if (level == SOL_SOCKET) 1709 error = sosetopt1(so, level, optname, m); 1710 else 1711 error = ENOPROTOOPT; 1712 1713 if ((error == 0 || error == ENOPROTOOPT) && 1714 so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) { 1715 /* give the protocol stack a shot */ 1716 prerr = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, level, 1717 optname, &m); 1718 if (prerr == 0) 1719 error = 0; 1720 else if (prerr != ENOPROTOOPT) 1721 error = prerr; 1722 } else if (m != NULL) 1723 (void)m_free(m); 1724 sounlock(so); 1725 return error; 1726 } 1727 1728 int 1729 sogetopt(struct socket *so, int level, int optname, struct mbuf **mp) 1730 { 1731 struct mbuf *m; 1732 int error; 1733 1734 solock(so); 1735 if (level != SOL_SOCKET) { 1736 if (so->so_proto && so->so_proto->pr_ctloutput) { 1737 error = ((*so->so_proto->pr_ctloutput) 1738 (PRCO_GETOPT, so, level, optname, mp)); 1739 } else 1740 error = (ENOPROTOOPT); 1741 } else { 1742 m = m_get(M_WAIT, MT_SOOPTS); 1743 m->m_len = sizeof(int); 1744 1745 switch (optname) { 1746 1747 #ifdef INET 1748 case SO_ACCEPTFILTER: 1749 error = do_getopt_accept_filter(so, m); 1750 break; 1751 #endif 1752 1753 case SO_LINGER: 1754 m->m_len = sizeof(struct linger); 1755 mtod(m, struct linger *)->l_onoff = 1756 (so->so_options & SO_LINGER) ? 1 : 0; 1757 mtod(m, struct linger *)->l_linger = so->so_linger; 1758 break; 1759 1760 case SO_USELOOPBACK: 1761 case SO_DONTROUTE: 1762 case SO_DEBUG: 1763 case SO_KEEPALIVE: 1764 case SO_REUSEADDR: 1765 case SO_REUSEPORT: 1766 case SO_BROADCAST: 1767 case SO_OOBINLINE: 1768 case SO_TIMESTAMP: 1769 *mtod(m, int *) = (so->so_options & optname) ? 1 : 0; 1770 break; 1771 1772 case SO_TYPE: 1773 *mtod(m, int *) = so->so_type; 1774 break; 1775 1776 case SO_ERROR: 1777 *mtod(m, int *) = so->so_error; 1778 so->so_error = 0; 1779 break; 1780 1781 case SO_SNDBUF: 1782 *mtod(m, int *) = so->so_snd.sb_hiwat; 1783 break; 1784 1785 case SO_RCVBUF: 1786 *mtod(m, int *) = so->so_rcv.sb_hiwat; 1787 break; 1788 1789 case SO_SNDLOWAT: 1790 *mtod(m, int *) = so->so_snd.sb_lowat; 1791 break; 1792 1793 case SO_RCVLOWAT: 1794 *mtod(m, int *) = so->so_rcv.sb_lowat; 1795 break; 1796 1797 case SO_SNDTIMEO: 1798 case SO_RCVTIMEO: 1799 { 1800 int val = (optname == SO_SNDTIMEO ? 1801 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 1802 1803 m->m_len = sizeof(struct timeval); 1804 mtod(m, struct timeval *)->tv_sec = val / hz; 1805 mtod(m, struct timeval *)->tv_usec = 1806 (val % hz) * tick; 1807 break; 1808 } 1809 1810 case SO_OVERFLOWED: 1811 *mtod(m, int *) = so->so_rcv.sb_overflowed; 1812 break; 1813 1814 default: 1815 sounlock(so); 1816 (void)m_free(m); 1817 return (ENOPROTOOPT); 1818 } 1819 *mp = m; 1820 error = 0; 1821 } 1822 1823 sounlock(so); 1824 return (error); 1825 } 1826 1827 void 1828 sohasoutofband(struct socket *so) 1829 { 1830 1831 fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); 1832 selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0); 1833 } 1834 1835 static void 1836 filt_sordetach(struct knote *kn) 1837 { 1838 struct socket *so; 1839 1840 so = ((file_t *)kn->kn_obj)->f_data; 1841 solock(so); 1842 SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext); 1843 if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist)) 1844 so->so_rcv.sb_flags &= ~SB_KNOTE; 1845 sounlock(so); 1846 } 1847 1848 /*ARGSUSED*/ 1849 static int 1850 filt_soread(struct knote *kn, long hint) 1851 { 1852 struct socket *so; 1853 int rv; 1854 1855 so = ((file_t *)kn->kn_obj)->f_data; 1856 if (hint != NOTE_SUBMIT) 1857 solock(so); 1858 kn->kn_data = so->so_rcv.sb_cc; 1859 if (so->so_state & SS_CANTRCVMORE) { 1860 kn->kn_flags |= EV_EOF; 1861 kn->kn_fflags = so->so_error; 1862 rv = 1; 1863 } else if (so->so_error) /* temporary udp error */ 1864 rv = 1; 1865 else if (kn->kn_sfflags & NOTE_LOWAT) 1866 rv = (kn->kn_data >= kn->kn_sdata); 1867 else 1868 rv = (kn->kn_data >= so->so_rcv.sb_lowat); 1869 if (hint != NOTE_SUBMIT) 1870 sounlock(so); 1871 return rv; 1872 } 1873 1874 static void 1875 filt_sowdetach(struct knote *kn) 1876 { 1877 struct socket *so; 1878 1879 so = ((file_t *)kn->kn_obj)->f_data; 1880 solock(so); 1881 SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext); 1882 if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist)) 1883 so->so_snd.sb_flags &= ~SB_KNOTE; 1884 sounlock(so); 1885 } 1886 1887 /*ARGSUSED*/ 1888 static int 1889 filt_sowrite(struct knote *kn, long hint) 1890 { 1891 struct socket *so; 1892 int rv; 1893 1894 so = ((file_t *)kn->kn_obj)->f_data; 1895 if (hint != NOTE_SUBMIT) 1896 solock(so); 1897 kn->kn_data = sbspace(&so->so_snd); 1898 if (so->so_state & SS_CANTSENDMORE) { 1899 kn->kn_flags |= EV_EOF; 1900 kn->kn_fflags = so->so_error; 1901 rv = 1; 1902 } else if (so->so_error) /* temporary udp error */ 1903 rv = 1; 1904 else if (((so->so_state & SS_ISCONNECTED) == 0) && 1905 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 1906 rv = 0; 1907 else if (kn->kn_sfflags & NOTE_LOWAT) 1908 rv = (kn->kn_data >= kn->kn_sdata); 1909 else 1910 rv = (kn->kn_data >= so->so_snd.sb_lowat); 1911 if (hint != NOTE_SUBMIT) 1912 sounlock(so); 1913 return rv; 1914 } 1915 1916 /*ARGSUSED*/ 1917 static int 1918 filt_solisten(struct knote *kn, long hint) 1919 { 1920 struct socket *so; 1921 int rv; 1922 1923 so = ((file_t *)kn->kn_obj)->f_data; 1924 1925 /* 1926 * Set kn_data to number of incoming connections, not 1927 * counting partial (incomplete) connections. 1928 */ 1929 if (hint != NOTE_SUBMIT) 1930 solock(so); 1931 kn->kn_data = so->so_qlen; 1932 rv = (kn->kn_data > 0); 1933 if (hint != NOTE_SUBMIT) 1934 sounlock(so); 1935 return rv; 1936 } 1937 1938 static const struct filterops solisten_filtops = 1939 { 1, NULL, filt_sordetach, filt_solisten }; 1940 static const struct filterops soread_filtops = 1941 { 1, NULL, filt_sordetach, filt_soread }; 1942 static const struct filterops sowrite_filtops = 1943 { 1, NULL, filt_sowdetach, filt_sowrite }; 1944 1945 int 1946 soo_kqfilter(struct file *fp, struct knote *kn) 1947 { 1948 struct socket *so; 1949 struct sockbuf *sb; 1950 1951 so = ((file_t *)kn->kn_obj)->f_data; 1952 solock(so); 1953 switch (kn->kn_filter) { 1954 case EVFILT_READ: 1955 if (so->so_options & SO_ACCEPTCONN) 1956 kn->kn_fop = &solisten_filtops; 1957 else 1958 kn->kn_fop = &soread_filtops; 1959 sb = &so->so_rcv; 1960 break; 1961 case EVFILT_WRITE: 1962 kn->kn_fop = &sowrite_filtops; 1963 sb = &so->so_snd; 1964 break; 1965 default: 1966 sounlock(so); 1967 return (EINVAL); 1968 } 1969 SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext); 1970 sb->sb_flags |= SB_KNOTE; 1971 sounlock(so); 1972 return (0); 1973 } 1974 1975 static int 1976 sodopoll(struct socket *so, int events) 1977 { 1978 int revents; 1979 1980 revents = 0; 1981 1982 if (events & (POLLIN | POLLRDNORM)) 1983 if (soreadable(so)) 1984 revents |= events & (POLLIN | POLLRDNORM); 1985 1986 if (events & (POLLOUT | POLLWRNORM)) 1987 if (sowritable(so)) 1988 revents |= events & (POLLOUT | POLLWRNORM); 1989 1990 if (events & (POLLPRI | POLLRDBAND)) 1991 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) 1992 revents |= events & (POLLPRI | POLLRDBAND); 1993 1994 return revents; 1995 } 1996 1997 int 1998 sopoll(struct socket *so, int events) 1999 { 2000 int revents = 0; 2001 2002 #ifndef DIAGNOSTIC 2003 /* 2004 * Do a quick, unlocked check in expectation that the socket 2005 * will be ready for I/O. Don't do this check if DIAGNOSTIC, 2006 * as the solocked() assertions will fail. 2007 */ 2008 if ((revents = sodopoll(so, events)) != 0) 2009 return revents; 2010 #endif 2011 2012 solock(so); 2013 if ((revents = sodopoll(so, events)) == 0) { 2014 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { 2015 selrecord(curlwp, &so->so_rcv.sb_sel); 2016 so->so_rcv.sb_flags |= SB_NOTIFY; 2017 } 2018 2019 if (events & (POLLOUT | POLLWRNORM)) { 2020 selrecord(curlwp, &so->so_snd.sb_sel); 2021 so->so_snd.sb_flags |= SB_NOTIFY; 2022 } 2023 } 2024 sounlock(so); 2025 2026 return revents; 2027 } 2028 2029 2030 #include <sys/sysctl.h> 2031 2032 static int sysctl_kern_somaxkva(SYSCTLFN_PROTO); 2033 2034 /* 2035 * sysctl helper routine for kern.somaxkva. ensures that the given 2036 * value is not too small. 2037 * (XXX should we maybe make sure it's not too large as well?) 2038 */ 2039 static int 2040 sysctl_kern_somaxkva(SYSCTLFN_ARGS) 2041 { 2042 int error, new_somaxkva; 2043 struct sysctlnode node; 2044 2045 new_somaxkva = somaxkva; 2046 node = *rnode; 2047 node.sysctl_data = &new_somaxkva; 2048 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2049 if (error || newp == NULL) 2050 return (error); 2051 2052 if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */ 2053 return (EINVAL); 2054 2055 mutex_enter(&so_pendfree_lock); 2056 somaxkva = new_somaxkva; 2057 cv_broadcast(&socurkva_cv); 2058 mutex_exit(&so_pendfree_lock); 2059 2060 return (error); 2061 } 2062 2063 SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup") 2064 { 2065 2066 sysctl_createv(clog, 0, NULL, NULL, 2067 CTLFLAG_PERMANENT, 2068 CTLTYPE_NODE, "kern", NULL, 2069 NULL, 0, NULL, 0, 2070 CTL_KERN, CTL_EOL); 2071 2072 sysctl_createv(clog, 0, NULL, NULL, 2073 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2074 CTLTYPE_INT, "somaxkva", 2075 SYSCTL_DESCR("Maximum amount of kernel memory to be " 2076 "used for socket buffers"), 2077 sysctl_kern_somaxkva, 0, NULL, 0, 2078 CTL_KERN, KERN_SOMAXKVA, CTL_EOL); 2079 } 2080