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

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

CVSweb <webmaster@jp.NetBSD.org>