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