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

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

CVSweb <webmaster@jp.NetBSD.org>