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

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

CVSweb <webmaster@jp.NetBSD.org>