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

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

CVSweb <webmaster@jp.NetBSD.org>