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