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

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

CVSweb <webmaster@jp.NetBSD.org>