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

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

CVSweb <webmaster@jp.NetBSD.org>