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