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