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

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

CVSweb <webmaster@jp.NetBSD.org>