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