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