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

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

CVSweb <webmaster@jp.NetBSD.org>