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

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

CVSweb <webmaster@jp.NetBSD.org>