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