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

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

CVSweb <webmaster@jp.NetBSD.org>