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

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

CVSweb <webmaster@jp.NetBSD.org>