[BACK]Return to uipc_socket.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/uipc_socket.c, Revision 1.170

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

CVSweb <webmaster@jp.NetBSD.org>