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

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

CVSweb <webmaster@jp.NetBSD.org>