[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.179

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

CVSweb <webmaster@jp.NetBSD.org>