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

1.137   ! ad          1: /*     $NetBSD: uipc_socket.c,v 1.136 2007/03/12 21:33:08 ad Exp $     */
1.64      thorpej     2:
                      3: /*-
1.137   ! ad          4:  * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
1.64      thorpej     5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Jason R. Thorpe of Wasabi Systems, Inc.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the NetBSD
                     21:  *     Foundation, Inc. and its contributors.
                     22:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     23:  *    contributors may be used to endorse or promote products derived
                     24:  *    from this software without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     27:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
                     37:  */
1.16      cgd        38:
1.1       cgd        39: /*
1.15      mycroft    40:  * Copyright (c) 1982, 1986, 1988, 1990, 1993
                     41:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        42:  *
                     43:  * Redistribution and use in source and binary forms, with or without
                     44:  * modification, are permitted provided that the following conditions
                     45:  * are met:
                     46:  * 1. Redistributions of source code must retain the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer.
                     48:  * 2. Redistributions in binary form must reproduce the above copyright
                     49:  *    notice, this list of conditions and the following disclaimer in the
                     50:  *    documentation and/or other materials provided with the distribution.
1.85      agc        51:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        52:  *    may be used to endorse or promote products derived from this software
                     53:  *    without specific prior written permission.
                     54:  *
                     55:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     56:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     57:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     58:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     59:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     60:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     61:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     62:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     63:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     64:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     65:  * SUCH DAMAGE.
                     66:  *
1.32      fvdl       67:  *     @(#)uipc_socket.c       8.6 (Berkeley) 5/2/95
1.1       cgd        68:  */
1.59      lukem      69:
                     70: #include <sys/cdefs.h>
1.137   ! ad         71: __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.136 2007/03/12 21:33:08 ad Exp $");
1.64      thorpej    72:
                     73: #include "opt_sock_counters.h"
                     74: #include "opt_sosend_loan.h"
1.81      martin     75: #include "opt_mbuftrace.h"
1.84      ragge      76: #include "opt_somaxkva.h"
1.1       cgd        77:
1.9       mycroft    78: #include <sys/param.h>
                     79: #include <sys/systm.h>
                     80: #include <sys/proc.h>
                     81: #include <sys/file.h>
                     82: #include <sys/malloc.h>
                     83: #include <sys/mbuf.h>
                     84: #include <sys/domain.h>
                     85: #include <sys/kernel.h>
                     86: #include <sys/protosw.h>
                     87: #include <sys/socket.h>
                     88: #include <sys/socketvar.h>
1.21      christos   89: #include <sys/signalvar.h>
1.9       mycroft    90: #include <sys/resourcevar.h>
1.37      thorpej    91: #include <sys/pool.h>
1.72      jdolecek   92: #include <sys/event.h>
1.89      christos   93: #include <sys/poll.h>
1.118     elad       94: #include <sys/kauth.h>
1.136     ad         95: #include <sys/mutex.h>
                     96: #include <sys/condvar.h>
1.37      thorpej    97:
1.64      thorpej    98: #include <uvm/uvm.h>
                     99:
1.135     ad        100: POOL_INIT(socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL,
                    101:     IPL_SOFTNET);
1.77      thorpej   102:
                    103: MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options");
                    104: MALLOC_DEFINE(M_SONAME, "soname", "socket name");
1.37      thorpej   105:
1.54      lukem     106: extern int     somaxconn;                      /* patchable (XXX sysctl) */
                    107: int            somaxconn = SOMAXCONN;
1.49      jonathan  108:
1.64      thorpej   109: #ifdef SOSEND_COUNTERS
                    110: #include <sys/device.h>
                    111:
1.113     thorpej   112: static struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64      thorpej   113:     NULL, "sosend", "loan big");
1.113     thorpej   114: static struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64      thorpej   115:     NULL, "sosend", "copy big");
1.113     thorpej   116: static struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64      thorpej   117:     NULL, "sosend", "copy small");
1.113     thorpej   118: static struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
1.64      thorpej   119:     NULL, "sosend", "kva limit");
                    120:
                    121: #define        SOSEND_COUNTER_INCR(ev)         (ev)->ev_count++
                    122:
1.101     matt      123: EVCNT_ATTACH_STATIC(sosend_loan_big);
                    124: EVCNT_ATTACH_STATIC(sosend_copy_big);
                    125: EVCNT_ATTACH_STATIC(sosend_copy_small);
                    126: EVCNT_ATTACH_STATIC(sosend_kvalimit);
1.64      thorpej   127: #else
                    128:
                    129: #define        SOSEND_COUNTER_INCR(ev)         /* nothing */
                    130:
                    131: #endif /* SOSEND_COUNTERS */
                    132:
1.119     yamt      133: static struct callback_entry sokva_reclaimerentry;
1.1       cgd       134:
1.71      thorpej   135: #ifdef SOSEND_NO_LOAN
1.121     yamt      136: int sock_loan_thresh = -1;
1.71      thorpej   137: #else
1.121     yamt      138: int sock_loan_thresh = 4096;
1.65      thorpej   139: #endif
1.64      thorpej   140:
1.136     ad        141: static kmutex_t so_pendfree_lock;
1.113     thorpej   142: static struct mbuf *so_pendfree;
1.64      thorpej   143:
1.84      ragge     144: #ifndef SOMAXKVA
                    145: #define        SOMAXKVA (16 * 1024 * 1024)
                    146: #endif
                    147: int somaxkva = SOMAXKVA;
1.113     thorpej   148: static int socurkva;
1.136     ad        149: static kcondvar_t socurkva_cv;
1.64      thorpej   150:
                    151: #define        SOCK_LOAN_CHUNK         65536
                    152:
1.117     yamt      153: static size_t sodopendfree(void);
                    154: static size_t sodopendfreel(void);
1.93      yamt      155:
1.113     thorpej   156: static vsize_t
1.129     yamt      157: sokvareserve(struct socket *so, vsize_t len)
1.80      yamt      158: {
1.98      christos  159:        int error;
1.80      yamt      160:
1.136     ad        161:        mutex_enter(&so_pendfree_lock);
1.80      yamt      162:        while (socurkva + len > somaxkva) {
1.93      yamt      163:                size_t freed;
                    164:
                    165:                /*
                    166:                 * try to do pendfree.
                    167:                 */
                    168:
1.117     yamt      169:                freed = sodopendfreel();
1.93      yamt      170:
                    171:                /*
                    172:                 * if some kva was freed, try again.
                    173:                 */
                    174:
                    175:                if (freed)
1.80      yamt      176:                        continue;
1.93      yamt      177:
1.80      yamt      178:                SOSEND_COUNTER_INCR(&sosend_kvalimit);
1.136     ad        179:                error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock);
1.98      christos  180:                if (error) {
                    181:                        len = 0;
                    182:                        break;
                    183:                }
1.80      yamt      184:        }
1.93      yamt      185:        socurkva += len;
1.136     ad        186:        mutex_exit(&so_pendfree_lock);
1.98      christos  187:        return len;
1.95      yamt      188: }
                    189:
1.113     thorpej   190: static void
1.95      yamt      191: sokvaunreserve(vsize_t len)
                    192: {
                    193:
1.136     ad        194:        mutex_enter(&so_pendfree_lock);
1.95      yamt      195:        socurkva -= len;
1.136     ad        196:        cv_broadcast(&socurkva_cv);
                    197:        mutex_exit(&so_pendfree_lock);
1.95      yamt      198: }
                    199:
                    200: /*
                    201:  * sokvaalloc: allocate kva for loan.
                    202:  */
                    203:
                    204: vaddr_t
                    205: sokvaalloc(vsize_t len, struct socket *so)
                    206: {
                    207:        vaddr_t lva;
                    208:
                    209:        /*
                    210:         * reserve kva.
                    211:         */
                    212:
1.98      christos  213:        if (sokvareserve(so, len) == 0)
                    214:                return 0;
1.93      yamt      215:
                    216:        /*
                    217:         * allocate kva.
                    218:         */
1.80      yamt      219:
1.109     yamt      220:        lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
1.95      yamt      221:        if (lva == 0) {
                    222:                sokvaunreserve(len);
1.80      yamt      223:                return (0);
1.95      yamt      224:        }
1.80      yamt      225:
                    226:        return lva;
                    227: }
                    228:
1.93      yamt      229: /*
                    230:  * sokvafree: free kva for loan.
                    231:  */
                    232:
1.80      yamt      233: void
                    234: sokvafree(vaddr_t sva, vsize_t len)
                    235: {
1.93      yamt      236:
                    237:        /*
                    238:         * free kva.
                    239:         */
1.80      yamt      240:
1.109     yamt      241:        uvm_km_free(kernel_map, sva, len, UVM_KMF_VAONLY);
1.93      yamt      242:
                    243:        /*
                    244:         * unreserve kva.
                    245:         */
                    246:
1.95      yamt      247:        sokvaunreserve(len);
1.80      yamt      248: }
                    249:
1.64      thorpej   250: static void
1.134     christos  251: sodoloanfree(struct vm_page **pgs, void *buf, size_t size)
1.64      thorpej   252: {
                    253:        vaddr_t va, sva, eva;
                    254:        vsize_t len;
                    255:        paddr_t pa;
                    256:        int i, npgs;
                    257:
                    258:        eva = round_page((vaddr_t) buf + size);
                    259:        sva = trunc_page((vaddr_t) buf);
                    260:        len = eva - sva;
                    261:        npgs = len >> PAGE_SHIFT;
                    262:
1.79      thorpej   263:        if (__predict_false(pgs == NULL)) {
                    264:                pgs = alloca(npgs * sizeof(*pgs));
1.64      thorpej   265:
1.79      thorpej   266:                for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) {
1.133     thorpej   267:                        if (pmap_extract(pmap_kernel(), va, &pa) == false)
1.79      thorpej   268:                                panic("sodoloanfree: va 0x%lx not mapped", va);
                    269:                        pgs[i] = PHYS_TO_VM_PAGE(pa);
                    270:                }
1.64      thorpej   271:        }
                    272:
                    273:        pmap_kremove(sva, len);
                    274:        pmap_update(pmap_kernel());
                    275:        uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE);
1.80      yamt      276:        sokvafree(sva, len);
1.64      thorpej   277: }
                    278:
                    279: static size_t
1.117     yamt      280: sodopendfree()
1.64      thorpej   281: {
1.93      yamt      282:        size_t rv;
1.64      thorpej   283:
1.136     ad        284:        mutex_enter(&so_pendfree_lock);
1.117     yamt      285:        rv = sodopendfreel();
1.136     ad        286:        mutex_exit(&so_pendfree_lock);
1.93      yamt      287:
                    288:        return rv;
                    289: }
                    290:
                    291: /*
                    292:  * sodopendfreel: free mbufs on "pendfree" list.
1.136     ad        293:  * unlock and relock so_pendfree_lock when freeing mbufs.
1.93      yamt      294:  *
1.136     ad        295:  * => called with so_pendfree_lock held.
1.93      yamt      296:  */
                    297:
                    298: static size_t
1.117     yamt      299: sodopendfreel()
1.93      yamt      300: {
1.137   ! ad        301:        struct mbuf *m, *next;
1.93      yamt      302:        size_t rv = 0;
1.137   ! ad        303:        int s;
1.93      yamt      304:
1.136     ad        305:        KASSERT(mutex_owned(&so_pendfree_lock));
1.64      thorpej   306:
1.137   ! ad        307:        while (so_pendfree != NULL) {
1.64      thorpej   308:                m = so_pendfree;
1.93      yamt      309:                so_pendfree = NULL;
1.136     ad        310:                mutex_exit(&so_pendfree_lock);
1.93      yamt      311:
                    312:                for (; m != NULL; m = next) {
                    313:                        next = m->m_next;
                    314:
                    315:                        rv += m->m_ext.ext_size;
                    316:                        sodoloanfree((m->m_flags & M_EXT_PAGES) ?
                    317:                            m->m_ext.ext_pgs : NULL, m->m_ext.ext_buf,
                    318:                            m->m_ext.ext_size);
1.137   ! ad        319:                        s = splvm();
1.93      yamt      320:                        pool_cache_put(&mbpool_cache, m);
1.137   ! ad        321:                        splx(s);
1.93      yamt      322:                }
1.64      thorpej   323:
1.136     ad        324:                mutex_enter(&so_pendfree_lock);
1.64      thorpej   325:        }
                    326:
                    327:        return (rv);
                    328: }
                    329:
1.80      yamt      330: void
1.134     christos  331: soloanfree(struct mbuf *m, void *buf, size_t size, void *arg)
1.64      thorpej   332: {
                    333:
                    334:        if (m == NULL) {
1.93      yamt      335:
                    336:                /*
                    337:                 * called from MEXTREMOVE.
                    338:                 */
                    339:
1.79      thorpej   340:                sodoloanfree(NULL, buf, size);
1.64      thorpej   341:                return;
                    342:        }
                    343:
1.93      yamt      344:        /*
                    345:         * postpone freeing mbuf.
                    346:         *
                    347:         * we can't do it in interrupt context
                    348:         * because we need to put kva back to kernel_map.
                    349:         */
                    350:
1.136     ad        351:        mutex_enter(&so_pendfree_lock);
1.92      yamt      352:        m->m_next = so_pendfree;
                    353:        so_pendfree = m;
1.136     ad        354:        cv_broadcast(&socurkva_cv);
                    355:        mutex_exit(&so_pendfree_lock);
1.64      thorpej   356: }
                    357:
                    358: static long
                    359: sosend_loan(struct socket *so, struct uio *uio, struct mbuf *m, long space)
                    360: {
                    361:        struct iovec *iov = uio->uio_iov;
                    362:        vaddr_t sva, eva;
                    363:        vsize_t len;
                    364:        vaddr_t lva, va;
1.80      yamt      365:        int npgs, i, error;
1.64      thorpej   366:
1.116     yamt      367:        if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace))
1.64      thorpej   368:                return (0);
                    369:
                    370:        if (iov->iov_len < (size_t) space)
                    371:                space = iov->iov_len;
                    372:        if (space > SOCK_LOAN_CHUNK)
                    373:                space = SOCK_LOAN_CHUNK;
                    374:
                    375:        eva = round_page((vaddr_t) iov->iov_base + space);
                    376:        sva = trunc_page((vaddr_t) iov->iov_base);
                    377:        len = eva - sva;
                    378:        npgs = len >> PAGE_SHIFT;
                    379:
1.79      thorpej   380:        /* XXX KDASSERT */
                    381:        KASSERT(npgs <= M_EXT_MAXPAGES);
                    382:
1.80      yamt      383:        lva = sokvaalloc(len, so);
1.64      thorpej   384:        if (lva == 0)
1.80      yamt      385:                return 0;
1.64      thorpej   386:
1.116     yamt      387:        error = uvm_loan(&uio->uio_vmspace->vm_map, sva, len,
1.79      thorpej   388:            m->m_ext.ext_pgs, UVM_LOAN_TOPAGE);
1.64      thorpej   389:        if (error) {
1.80      yamt      390:                sokvafree(lva, len);
1.64      thorpej   391:                return (0);
                    392:        }
                    393:
                    394:        for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE)
1.79      thorpej   395:                pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]),
                    396:                    VM_PROT_READ);
1.64      thorpej   397:        pmap_update(pmap_kernel());
                    398:
                    399:        lva += (vaddr_t) iov->iov_base & PAGE_MASK;
                    400:
1.134     christos  401:        MEXTADD(m, (void *) lva, space, M_MBUF, soloanfree, so);
1.79      thorpej   402:        m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP;
1.64      thorpej   403:
                    404:        uio->uio_resid -= space;
                    405:        /* uio_offset not updated, not set/used for write(2) */
1.134     christos  406:        uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + space;
1.64      thorpej   407:        uio->uio_iov->iov_len -= space;
                    408:        if (uio->uio_iov->iov_len == 0) {
                    409:                uio->uio_iov++;
                    410:                uio->uio_iovcnt--;
                    411:        }
                    412:
                    413:        return (space);
                    414: }
                    415:
1.119     yamt      416: static int
1.129     yamt      417: sokva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
1.119     yamt      418: {
                    419:
                    420:        KASSERT(ce == &sokva_reclaimerentry);
                    421:        KASSERT(obj == NULL);
                    422:
                    423:        sodopendfree();
                    424:        if (!vm_map_starved_p(kernel_map)) {
                    425:                return CALLBACK_CHAIN_ABORT;
                    426:        }
                    427:        return CALLBACK_CHAIN_CONTINUE;
                    428: }
                    429:
                    430: void
                    431: soinit(void)
                    432: {
                    433:
1.136     ad        434:        mutex_init(&so_pendfree_lock, MUTEX_DRIVER, IPL_VM);
                    435:        cv_init(&socurkva_cv, "sokva");
                    436:
1.119     yamt      437:        /* Set the initial adjusted socket buffer size. */
                    438:        if (sb_max_set(sb_max))
                    439:                panic("bad initial sb_max value: %lu", sb_max);
                    440:
                    441:        callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback,
                    442:            &sokva_reclaimerentry, NULL, sokva_reclaim_callback);
                    443: }
                    444:
1.1       cgd       445: /*
                    446:  * Socket operation routines.
                    447:  * These routines are called by the routines in
                    448:  * sys_socket.c or from a system process, and
                    449:  * implement the semantics of socket operations by
                    450:  * switching out to the protocol specific routines.
                    451:  */
                    452: /*ARGSUSED*/
1.3       andrew    453: int
1.114     christos  454: socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l)
1.1       cgd       455: {
1.99      matt      456:        const struct protosw    *prp;
1.54      lukem     457:        struct socket   *so;
1.115     yamt      458:        uid_t           uid;
1.54      lukem     459:        int             error, s;
1.1       cgd       460:
1.132     elad      461:        error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET,
                    462:            KAUTH_REQ_NETWORK_SOCKET_OPEN, KAUTH_ARG(dom), KAUTH_ARG(type),
                    463:            KAUTH_ARG(proto));
                    464:        if (error)
                    465:                return (error);
1.127     elad      466:
1.1       cgd       467:        if (proto)
                    468:                prp = pffindproto(dom, proto, type);
                    469:        else
                    470:                prp = pffindtype(dom, type);
1.120     ginsbach  471:        if (prp == 0) {
                    472:                /* no support for domain */
                    473:                if (pffinddomain(dom) == 0)
                    474:                        return (EAFNOSUPPORT);
                    475:                /* no support for socket type */
                    476:                if (proto == 0 && type != 0)
                    477:                        return (EPROTOTYPE);
                    478:                return (EPROTONOSUPPORT);
                    479:        }
                    480:        if (prp->pr_usrreq == 0)
1.1       cgd       481:                return (EPROTONOSUPPORT);
                    482:        if (prp->pr_type != type)
                    483:                return (EPROTOTYPE);
1.39      matt      484:        s = splsoftnet();
1.37      thorpej   485:        so = pool_get(&socket_pool, PR_WAITOK);
1.134     christos  486:        memset((void *)so, 0, sizeof(*so));
1.31      thorpej   487:        TAILQ_INIT(&so->so_q0);
                    488:        TAILQ_INIT(&so->so_q);
1.1       cgd       489:        so->so_type = type;
                    490:        so->so_proto = prp;
1.33      matt      491:        so->so_send = sosend;
                    492:        so->so_receive = soreceive;
1.78      matt      493: #ifdef MBUFTRACE
                    494:        so->so_rcv.sb_mowner = &prp->pr_domain->dom_mowner;
                    495:        so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner;
                    496:        so->so_mowner = &prp->pr_domain->dom_mowner;
                    497: #endif
1.115     yamt      498:        if (l != NULL) {
1.122     ad        499:                uid = kauth_cred_geteuid(l->l_cred);
1.115     yamt      500:        } else {
                    501:                uid = 0;
                    502:        }
                    503:        so->so_uidinfo = uid_find(uid);
1.22      mycroft   504:        error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
1.114     christos  505:            (struct mbuf *)(long)proto, (struct mbuf *)0, l);
1.1       cgd       506:        if (error) {
                    507:                so->so_state |= SS_NOFDREF;
                    508:                sofree(so);
1.39      matt      509:                splx(s);
1.1       cgd       510:                return (error);
                    511:        }
1.39      matt      512:        splx(s);
1.1       cgd       513:        *aso = so;
                    514:        return (0);
                    515: }
                    516:
1.3       andrew    517: int
1.114     christos  518: sobind(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1       cgd       519: {
1.54      lukem     520:        int     s, error;
1.1       cgd       521:
1.54      lukem     522:        s = splsoftnet();
1.22      mycroft   523:        error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0,
1.114     christos  524:            nam, (struct mbuf *)0, l);
1.1       cgd       525:        splx(s);
                    526:        return (error);
                    527: }
                    528:
1.3       andrew    529: int
1.54      lukem     530: solisten(struct socket *so, int backlog)
1.1       cgd       531: {
1.54      lukem     532:        int     s, error;
1.1       cgd       533:
1.54      lukem     534:        s = splsoftnet();
1.22      mycroft   535:        error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0,
1.114     christos  536:            (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1       cgd       537:        if (error) {
                    538:                splx(s);
                    539:                return (error);
                    540:        }
1.63      matt      541:        if (TAILQ_EMPTY(&so->so_q))
1.1       cgd       542:                so->so_options |= SO_ACCEPTCONN;
                    543:        if (backlog < 0)
                    544:                backlog = 0;
1.49      jonathan  545:        so->so_qlimit = min(backlog, somaxconn);
1.1       cgd       546:        splx(s);
                    547:        return (0);
                    548: }
                    549:
1.21      christos  550: void
1.54      lukem     551: sofree(struct socket *so)
1.1       cgd       552: {
                    553:
1.43      mycroft   554:        if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
1.1       cgd       555:                return;
1.43      mycroft   556:        if (so->so_head) {
                    557:                /*
                    558:                 * We must not decommission a socket that's on the accept(2)
                    559:                 * queue.  If we do, then accept(2) may hang after select(2)
                    560:                 * indicated that the listening socket was ready.
                    561:                 */
                    562:                if (!soqremque(so, 0))
                    563:                        return;
                    564:        }
1.98      christos  565:        if (so->so_rcv.sb_hiwat)
1.110     christos  566:                (void)chgsbsize(so->so_uidinfo, &so->so_rcv.sb_hiwat, 0,
1.98      christos  567:                    RLIM_INFINITY);
                    568:        if (so->so_snd.sb_hiwat)
1.110     christos  569:                (void)chgsbsize(so->so_uidinfo, &so->so_snd.sb_hiwat, 0,
1.98      christos  570:                    RLIM_INFINITY);
                    571:        sbrelease(&so->so_snd, so);
1.1       cgd       572:        sorflush(so);
1.37      thorpej   573:        pool_put(&socket_pool, so);
1.1       cgd       574: }
                    575:
                    576: /*
                    577:  * Close a socket on last file table reference removal.
                    578:  * Initiate disconnect if connected.
                    579:  * Free socket when disconnect complete.
                    580:  */
1.3       andrew    581: int
1.54      lukem     582: soclose(struct socket *so)
1.1       cgd       583: {
1.54      lukem     584:        struct socket   *so2;
                    585:        int             s, error;
1.1       cgd       586:
1.54      lukem     587:        error = 0;
                    588:        s = splsoftnet();               /* conservative */
1.1       cgd       589:        if (so->so_options & SO_ACCEPTCONN) {
1.63      matt      590:                while ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) {
1.42      mycroft   591:                        (void) soqremque(so2, 0);
1.41      mycroft   592:                        (void) soabort(so2);
                    593:                }
1.63      matt      594:                while ((so2 = TAILQ_FIRST(&so->so_q)) != 0) {
1.42      mycroft   595:                        (void) soqremque(so2, 1);
1.41      mycroft   596:                        (void) soabort(so2);
                    597:                }
1.1       cgd       598:        }
                    599:        if (so->so_pcb == 0)
                    600:                goto discard;
                    601:        if (so->so_state & SS_ISCONNECTED) {
                    602:                if ((so->so_state & SS_ISDISCONNECTING) == 0) {
                    603:                        error = sodisconnect(so);
                    604:                        if (error)
                    605:                                goto drop;
                    606:                }
                    607:                if (so->so_options & SO_LINGER) {
                    608:                        if ((so->so_state & SS_ISDISCONNECTING) &&
                    609:                            (so->so_state & SS_NBIO))
                    610:                                goto drop;
1.21      christos  611:                        while (so->so_state & SS_ISCONNECTED) {
1.134     christos  612:                                error = tsleep((void *)&so->so_timeo,
1.21      christos  613:                                               PSOCK | PCATCH, netcls,
1.30      thorpej   614:                                               so->so_linger * hz);
1.21      christos  615:                                if (error)
1.1       cgd       616:                                        break;
1.21      christos  617:                        }
1.1       cgd       618:                }
                    619:        }
1.54      lukem     620:  drop:
1.1       cgd       621:        if (so->so_pcb) {
1.22      mycroft   622:                int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
                    623:                    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
1.114     christos  624:                    (struct lwp *)0);
1.1       cgd       625:                if (error == 0)
                    626:                        error = error2;
                    627:        }
1.54      lukem     628:  discard:
1.1       cgd       629:        if (so->so_state & SS_NOFDREF)
                    630:                panic("soclose: NOFDREF");
                    631:        so->so_state |= SS_NOFDREF;
                    632:        sofree(so);
                    633:        splx(s);
                    634:        return (error);
                    635: }
                    636:
                    637: /*
1.20      mycroft   638:  * Must be called at splsoftnet...
1.1       cgd       639:  */
1.3       andrew    640: int
1.54      lukem     641: soabort(struct socket *so)
1.1       cgd       642: {
                    643:
1.22      mycroft   644:        return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0,
1.114     christos  645:            (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1       cgd       646: }
                    647:
1.3       andrew    648: int
1.54      lukem     649: soaccept(struct socket *so, struct mbuf *nam)
1.1       cgd       650: {
1.54      lukem     651:        int     s, error;
1.1       cgd       652:
1.54      lukem     653:        error = 0;
                    654:        s = splsoftnet();
1.1       cgd       655:        if ((so->so_state & SS_NOFDREF) == 0)
                    656:                panic("soaccept: !NOFDREF");
                    657:        so->so_state &= ~SS_NOFDREF;
1.55      thorpej   658:        if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
                    659:            (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
1.41      mycroft   660:                error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
1.114     christos  661:                    (struct mbuf *)0, nam, (struct mbuf *)0, (struct lwp *)0);
1.41      mycroft   662:        else
1.53      itojun    663:                error = ECONNABORTED;
1.52      itojun    664:
1.1       cgd       665:        splx(s);
                    666:        return (error);
                    667: }
                    668:
1.3       andrew    669: int
1.114     christos  670: soconnect(struct socket *so, struct mbuf *nam, struct lwp *l)
1.1       cgd       671: {
1.54      lukem     672:        int             s, error;
1.1       cgd       673:
                    674:        if (so->so_options & SO_ACCEPTCONN)
                    675:                return (EOPNOTSUPP);
1.20      mycroft   676:        s = splsoftnet();
1.1       cgd       677:        /*
                    678:         * If protocol is connection-based, can only connect once.
                    679:         * Otherwise, if connected, try to disconnect first.
                    680:         * This allows user to disconnect by connecting to, e.g.,
                    681:         * a null address.
                    682:         */
                    683:        if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
                    684:            ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
                    685:            (error = sodisconnect(so))))
                    686:                error = EISCONN;
                    687:        else
                    688:                error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
1.114     christos  689:                    (struct mbuf *)0, nam, (struct mbuf *)0, l);
1.1       cgd       690:        splx(s);
                    691:        return (error);
                    692: }
                    693:
1.3       andrew    694: int
1.54      lukem     695: soconnect2(struct socket *so1, struct socket *so2)
1.1       cgd       696: {
1.54      lukem     697:        int     s, error;
1.1       cgd       698:
1.54      lukem     699:        s = splsoftnet();
1.22      mycroft   700:        error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
                    701:            (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0,
1.114     christos  702:            (struct lwp *)0);
1.1       cgd       703:        splx(s);
                    704:        return (error);
                    705: }
                    706:
1.3       andrew    707: int
1.54      lukem     708: sodisconnect(struct socket *so)
1.1       cgd       709: {
1.54      lukem     710:        int     s, error;
1.1       cgd       711:
1.54      lukem     712:        s = splsoftnet();
1.1       cgd       713:        if ((so->so_state & SS_ISCONNECTED) == 0) {
                    714:                error = ENOTCONN;
                    715:                goto bad;
                    716:        }
                    717:        if (so->so_state & SS_ISDISCONNECTING) {
                    718:                error = EALREADY;
                    719:                goto bad;
                    720:        }
1.22      mycroft   721:        error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
                    722:            (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
1.114     christos  723:            (struct lwp *)0);
1.54      lukem     724:  bad:
1.1       cgd       725:        splx(s);
1.117     yamt      726:        sodopendfree();
1.1       cgd       727:        return (error);
                    728: }
                    729:
1.15      mycroft   730: #define        SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
1.1       cgd       731: /*
                    732:  * Send on a socket.
                    733:  * If send must go all at once and message is larger than
                    734:  * send buffering, then hard error.
                    735:  * Lock against other senders.
                    736:  * If must go all at once and not enough room now, then
                    737:  * inform user that this would block and do nothing.
                    738:  * Otherwise, if nonblocking, send as much as possible.
                    739:  * The data to be sent is described by "uio" if nonzero,
                    740:  * otherwise by the mbuf chain "top" (which must be null
                    741:  * if uio is not).  Data provided in mbuf chain must be small
                    742:  * enough to send all at once.
                    743:  *
                    744:  * Returns nonzero on error, timeout or signal; callers
                    745:  * must check for short counts if EINTR/ERESTART are returned.
                    746:  * Data and control buffers are freed on return.
                    747:  */
1.3       andrew    748: int
1.54      lukem     749: sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
1.114     christos  750:        struct mbuf *control, int flags, struct lwp *l)
1.1       cgd       751: {
1.54      lukem     752:        struct mbuf     **mp, *m;
1.114     christos  753:        struct proc     *p;
1.58      jdolecek  754:        long            space, len, resid, clen, mlen;
                    755:        int             error, s, dontroute, atomic;
1.54      lukem     756:
1.114     christos  757:        p = l->l_proc;
1.117     yamt      758:        sodopendfree();
1.64      thorpej   759:
1.54      lukem     760:        clen = 0;
                    761:        atomic = sosendallatonce(so) || top;
1.1       cgd       762:        if (uio)
                    763:                resid = uio->uio_resid;
                    764:        else
                    765:                resid = top->m_pkthdr.len;
1.7       cgd       766:        /*
                    767:         * In theory resid should be unsigned.
                    768:         * However, space must be signed, as it might be less than 0
                    769:         * if we over-committed, and we must use a signed comparison
                    770:         * of space and resid.  On the other hand, a negative resid
                    771:         * causes us to loop sending 0-length segments to the protocol.
                    772:         */
1.29      mycroft   773:        if (resid < 0) {
                    774:                error = EINVAL;
                    775:                goto out;
                    776:        }
1.1       cgd       777:        dontroute =
                    778:            (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
                    779:            (so->so_proto->pr_flags & PR_ATOMIC);
1.102     jonathan  780:        if (p)
                    781:                p->p_stats->p_ru.ru_msgsnd++;
1.1       cgd       782:        if (control)
                    783:                clen = control->m_len;
                    784: #define        snderr(errno)   { error = errno; splx(s); goto release; }
                    785:
1.54      lukem     786:  restart:
1.21      christos  787:        if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
1.1       cgd       788:                goto out;
                    789:        do {
1.20      mycroft   790:                s = splsoftnet();
1.1       cgd       791:                if (so->so_state & SS_CANTSENDMORE)
                    792:                        snderr(EPIPE);
1.48      thorpej   793:                if (so->so_error) {
                    794:                        error = so->so_error;
                    795:                        so->so_error = 0;
                    796:                        splx(s);
                    797:                        goto release;
                    798:                }
1.1       cgd       799:                if ((so->so_state & SS_ISCONNECTED) == 0) {
                    800:                        if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
                    801:                                if ((so->so_state & SS_ISCONFIRMING) == 0 &&
                    802:                                    !(resid == 0 && clen != 0))
                    803:                                        snderr(ENOTCONN);
                    804:                        } else if (addr == 0)
                    805:                                snderr(EDESTADDRREQ);
                    806:                }
                    807:                space = sbspace(&so->so_snd);
                    808:                if (flags & MSG_OOB)
                    809:                        space += 1024;
1.21      christos  810:                if ((atomic && resid > so->so_snd.sb_hiwat) ||
1.11      mycroft   811:                    clen > so->so_snd.sb_hiwat)
                    812:                        snderr(EMSGSIZE);
1.96      mycroft   813:                if (space < resid + clen &&
1.1       cgd       814:                    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
                    815:                        if (so->so_state & SS_NBIO)
                    816:                                snderr(EWOULDBLOCK);
                    817:                        sbunlock(&so->so_snd);
                    818:                        error = sbwait(&so->so_snd);
                    819:                        splx(s);
                    820:                        if (error)
                    821:                                goto out;
                    822:                        goto restart;
                    823:                }
                    824:                splx(s);
                    825:                mp = &top;
                    826:                space -= clen;
                    827:                do {
1.45      tv        828:                        if (uio == NULL) {
                    829:                                /*
                    830:                                 * Data is prepackaged in "top".
                    831:                                 */
                    832:                                resid = 0;
                    833:                                if (flags & MSG_EOR)
                    834:                                        top->m_flags |= M_EOR;
                    835:                        } else do {
                    836:                                if (top == 0) {
1.78      matt      837:                                        m = m_gethdr(M_WAIT, MT_DATA);
1.45      tv        838:                                        mlen = MHLEN;
                    839:                                        m->m_pkthdr.len = 0;
                    840:                                        m->m_pkthdr.rcvif = (struct ifnet *)0;
                    841:                                } else {
1.78      matt      842:                                        m = m_get(M_WAIT, MT_DATA);
1.45      tv        843:                                        mlen = MLEN;
                    844:                                }
1.78      matt      845:                                MCLAIM(m, so->so_snd.sb_mowner);
1.121     yamt      846:                                if (sock_loan_thresh >= 0 &&
                    847:                                    uio->uio_iov->iov_len >= sock_loan_thresh &&
                    848:                                    space >= sock_loan_thresh &&
1.64      thorpej   849:                                    (len = sosend_loan(so, uio, m,
                    850:                                                       space)) != 0) {
                    851:                                        SOSEND_COUNTER_INCR(&sosend_loan_big);
                    852:                                        space -= len;
                    853:                                        goto have_data;
                    854:                                }
1.45      tv        855:                                if (resid >= MINCLSIZE && space >= MCLBYTES) {
1.64      thorpej   856:                                        SOSEND_COUNTER_INCR(&sosend_copy_big);
1.78      matt      857:                                        m_clget(m, M_WAIT);
1.45      tv        858:                                        if ((m->m_flags & M_EXT) == 0)
                    859:                                                goto nopages;
                    860:                                        mlen = MCLBYTES;
                    861:                                        if (atomic && top == 0) {
1.58      jdolecek  862:                                                len = lmin(MCLBYTES - max_hdr,
1.54      lukem     863:                                                    resid);
1.45      tv        864:                                                m->m_data += max_hdr;
                    865:                                        } else
1.58      jdolecek  866:                                                len = lmin(MCLBYTES, resid);
1.45      tv        867:                                        space -= len;
                    868:                                } else {
1.64      thorpej   869:  nopages:
                    870:                                        SOSEND_COUNTER_INCR(&sosend_copy_small);
1.58      jdolecek  871:                                        len = lmin(lmin(mlen, resid), space);
1.45      tv        872:                                        space -= len;
                    873:                                        /*
                    874:                                         * For datagram protocols, leave room
                    875:                                         * for protocol headers in first mbuf.
                    876:                                         */
                    877:                                        if (atomic && top == 0 && len < mlen)
                    878:                                                MH_ALIGN(m, len);
                    879:                                }
1.134     christos  880:                                error = uiomove(mtod(m, void *), (int)len,
1.54      lukem     881:                                    uio);
1.64      thorpej   882:  have_data:
1.45      tv        883:                                resid = uio->uio_resid;
                    884:                                m->m_len = len;
                    885:                                *mp = m;
                    886:                                top->m_pkthdr.len += len;
                    887:                                if (error)
                    888:                                        goto release;
                    889:                                mp = &m->m_next;
                    890:                                if (resid <= 0) {
                    891:                                        if (flags & MSG_EOR)
                    892:                                                top->m_flags |= M_EOR;
                    893:                                        break;
                    894:                                }
                    895:                        } while (space > 0 && atomic);
1.108     perry     896:
1.46      sommerfe  897:                        s = splsoftnet();
                    898:
                    899:                        if (so->so_state & SS_CANTSENDMORE)
                    900:                                snderr(EPIPE);
1.45      tv        901:
                    902:                        if (dontroute)
                    903:                                so->so_options |= SO_DONTROUTE;
                    904:                        if (resid > 0)
                    905:                                so->so_state |= SS_MORETOCOME;
1.46      sommerfe  906:                        error = (*so->so_proto->pr_usrreq)(so,
                    907:                            (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
1.114     christos  908:                            top, addr, control, curlwp);        /* XXX */
1.45      tv        909:                        if (dontroute)
                    910:                                so->so_options &= ~SO_DONTROUTE;
                    911:                        if (resid > 0)
                    912:                                so->so_state &= ~SS_MORETOCOME;
1.46      sommerfe  913:                        splx(s);
                    914:
1.45      tv        915:                        clen = 0;
                    916:                        control = 0;
                    917:                        top = 0;
                    918:                        mp = &top;
1.1       cgd       919:                        if (error)
                    920:                                goto release;
                    921:                } while (resid && space > 0);
                    922:        } while (resid);
                    923:
1.54      lukem     924:  release:
1.1       cgd       925:        sbunlock(&so->so_snd);
1.54      lukem     926:  out:
1.1       cgd       927:        if (top)
                    928:                m_freem(top);
                    929:        if (control)
                    930:                m_freem(control);
                    931:        return (error);
                    932: }
                    933:
                    934: /*
                    935:  * Implement receive operations on a socket.
                    936:  * We depend on the way that records are added to the sockbuf
                    937:  * by sbappend*.  In particular, each record (mbufs linked through m_next)
                    938:  * must begin with an address if the protocol so specifies,
                    939:  * followed by an optional mbuf or mbufs containing ancillary data,
                    940:  * and then zero or more mbufs of data.
                    941:  * In order to avoid blocking network interrupts for the entire time here,
                    942:  * we splx() while doing the actual copy to user space.
                    943:  * Although the sockbuf is locked, new data may still be appended,
                    944:  * and thus we must maintain consistency of the sockbuf during that time.
                    945:  *
                    946:  * The caller may receive the data as a single mbuf chain by supplying
                    947:  * an mbuf **mp0 for use in returning the chain.  The uio is then used
                    948:  * only for the count in uio_resid.
                    949:  */
1.3       andrew    950: int
1.54      lukem     951: soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
                    952:        struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1.1       cgd       953: {
1.116     yamt      954:        struct lwp *l = curlwp;
1.54      lukem     955:        struct mbuf     *m, **mp;
                    956:        int             flags, len, error, s, offset, moff, type, orig_resid;
1.99      matt      957:        const struct protosw    *pr;
1.54      lukem     958:        struct mbuf     *nextrecord;
1.67      he        959:        int             mbuf_removed = 0;
1.64      thorpej   960:
1.54      lukem     961:        pr = so->so_proto;
1.1       cgd       962:        mp = mp0;
1.54      lukem     963:        type = 0;
                    964:        orig_resid = uio->uio_resid;
1.102     jonathan  965:
1.1       cgd       966:        if (paddr)
                    967:                *paddr = 0;
                    968:        if (controlp)
                    969:                *controlp = 0;
                    970:        if (flagsp)
                    971:                flags = *flagsp &~ MSG_EOR;
                    972:        else
                    973:                flags = 0;
1.66      enami     974:
                    975:        if ((flags & MSG_DONTWAIT) == 0)
1.117     yamt      976:                sodopendfree();
1.66      enami     977:
1.1       cgd       978:        if (flags & MSG_OOB) {
                    979:                m = m_get(M_WAIT, MT_DATA);
1.17      cgd       980:                error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.102     jonathan  981:                    (struct mbuf *)(long)(flags & MSG_PEEK),
1.114     christos  982:                    (struct mbuf *)0, l);
1.1       cgd       983:                if (error)
                    984:                        goto bad;
                    985:                do {
1.134     christos  986:                        error = uiomove(mtod(m, void *),
1.1       cgd       987:                            (int) min(uio->uio_resid, m->m_len), uio);
                    988:                        m = m_free(m);
                    989:                } while (uio->uio_resid && error == 0 && m);
1.54      lukem     990:  bad:
1.1       cgd       991:                if (m)
                    992:                        m_freem(m);
                    993:                return (error);
                    994:        }
                    995:        if (mp)
                    996:                *mp = (struct mbuf *)0;
                    997:        if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.22      mycroft   998:                (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
1.114     christos  999:                    (struct mbuf *)0, (struct mbuf *)0, l);
1.1       cgd      1000:
1.54      lukem    1001:  restart:
1.21      christos 1002:        if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
1.1       cgd      1003:                return (error);
1.20      mycroft  1004:        s = splsoftnet();
1.1       cgd      1005:
                   1006:        m = so->so_rcv.sb_mb;
                   1007:        /*
                   1008:         * If we have less data than requested, block awaiting more
                   1009:         * (subject to any timeout) if:
1.15      mycroft  1010:         *   1. the current count is less than the low water mark,
1.1       cgd      1011:         *   2. MSG_WAITALL is set, and it is possible to do the entire
1.15      mycroft  1012:         *      receive operation at once if we block (resid <= hiwat), or
                   1013:         *   3. MSG_DONTWAIT is not set.
1.1       cgd      1014:         * If MSG_WAITALL is set but resid is larger than the receive buffer,
                   1015:         * we have to do the receive in sections, and thus risk returning
                   1016:         * a short count if a timeout or signal occurs after we start.
                   1017:         */
1.21      christos 1018:        if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
1.15      mycroft  1019:            so->so_rcv.sb_cc < uio->uio_resid) &&
1.1       cgd      1020:            (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
                   1021:            ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.21      christos 1022:            m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1.1       cgd      1023: #ifdef DIAGNOSTIC
                   1024:                if (m == 0 && so->so_rcv.sb_cc)
                   1025:                        panic("receive 1");
                   1026: #endif
                   1027:                if (so->so_error) {
                   1028:                        if (m)
1.15      mycroft  1029:                                goto dontblock;
1.1       cgd      1030:                        error = so->so_error;
                   1031:                        if ((flags & MSG_PEEK) == 0)
                   1032:                                so->so_error = 0;
                   1033:                        goto release;
                   1034:                }
                   1035:                if (so->so_state & SS_CANTRCVMORE) {
                   1036:                        if (m)
1.15      mycroft  1037:                                goto dontblock;
1.1       cgd      1038:                        else
                   1039:                                goto release;
                   1040:                }
                   1041:                for (; m; m = m->m_next)
                   1042:                        if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
                   1043:                                m = so->so_rcv.sb_mb;
                   1044:                                goto dontblock;
                   1045:                        }
                   1046:                if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
                   1047:                    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
                   1048:                        error = ENOTCONN;
                   1049:                        goto release;
                   1050:                }
                   1051:                if (uio->uio_resid == 0)
                   1052:                        goto release;
1.15      mycroft  1053:                if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
1.1       cgd      1054:                        error = EWOULDBLOCK;
                   1055:                        goto release;
                   1056:                }
1.69      thorpej  1057:                SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
                   1058:                SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
1.1       cgd      1059:                sbunlock(&so->so_rcv);
                   1060:                error = sbwait(&so->so_rcv);
                   1061:                splx(s);
                   1062:                if (error)
                   1063:                        return (error);
                   1064:                goto restart;
                   1065:        }
1.54      lukem    1066:  dontblock:
1.69      thorpej  1067:        /*
                   1068:         * On entry here, m points to the first record of the socket buffer.
                   1069:         * While we process the initial mbufs containing address and control
                   1070:         * info, we save a copy of m->m_nextpkt into nextrecord.
                   1071:         */
1.114     christos 1072:        if (l)
                   1073:                l->l_proc->p_stats->p_ru.ru_msgrcv++;
1.69      thorpej  1074:        KASSERT(m == so->so_rcv.sb_mb);
                   1075:        SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
                   1076:        SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
1.1       cgd      1077:        nextrecord = m->m_nextpkt;
                   1078:        if (pr->pr_flags & PR_ADDR) {
                   1079: #ifdef DIAGNOSTIC
                   1080:                if (m->m_type != MT_SONAME)
                   1081:                        panic("receive 1a");
                   1082: #endif
1.3       andrew   1083:                orig_resid = 0;
1.1       cgd      1084:                if (flags & MSG_PEEK) {
                   1085:                        if (paddr)
                   1086:                                *paddr = m_copy(m, 0, m->m_len);
                   1087:                        m = m->m_next;
                   1088:                } else {
                   1089:                        sbfree(&so->so_rcv, m);
1.67      he       1090:                        mbuf_removed = 1;
1.1       cgd      1091:                        if (paddr) {
                   1092:                                *paddr = m;
                   1093:                                so->so_rcv.sb_mb = m->m_next;
                   1094:                                m->m_next = 0;
                   1095:                                m = so->so_rcv.sb_mb;
                   1096:                        } else {
                   1097:                                MFREE(m, so->so_rcv.sb_mb);
                   1098:                                m = so->so_rcv.sb_mb;
                   1099:                        }
                   1100:                }
                   1101:        }
                   1102:        while (m && m->m_type == MT_CONTROL && error == 0) {
                   1103:                if (flags & MSG_PEEK) {
                   1104:                        if (controlp)
                   1105:                                *controlp = m_copy(m, 0, m->m_len);
                   1106:                        m = m->m_next;
                   1107:                } else {
                   1108:                        sbfree(&so->so_rcv, m);
1.67      he       1109:                        mbuf_removed = 1;
1.1       cgd      1110:                        if (controlp) {
1.102     jonathan 1111:                                struct domain *dom = pr->pr_domain;
1.114     christos 1112:                                if (dom->dom_externalize && l &&
1.1       cgd      1113:                                    mtod(m, struct cmsghdr *)->cmsg_type ==
                   1114:                                    SCM_RIGHTS)
1.114     christos 1115:                                        error = (*dom->dom_externalize)(m, l);
1.1       cgd      1116:                                *controlp = m;
                   1117:                                so->so_rcv.sb_mb = m->m_next;
                   1118:                                m->m_next = 0;
                   1119:                                m = so->so_rcv.sb_mb;
                   1120:                        } else {
1.106     itojun   1121:                                /*
                   1122:                                 * Dispose of any SCM_RIGHTS message that went
                   1123:                                 * through the read path rather than recv.
                   1124:                                 */
                   1125:                                if (pr->pr_domain->dom_dispose &&
                   1126:                                    mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
                   1127:                                        (*pr->pr_domain->dom_dispose)(m);
1.1       cgd      1128:                                MFREE(m, so->so_rcv.sb_mb);
                   1129:                                m = so->so_rcv.sb_mb;
                   1130:                        }
                   1131:                }
1.3       andrew   1132:                if (controlp) {
                   1133:                        orig_resid = 0;
1.1       cgd      1134:                        controlp = &(*controlp)->m_next;
1.3       andrew   1135:                }
1.1       cgd      1136:        }
1.69      thorpej  1137:
                   1138:        /*
                   1139:         * If m is non-NULL, we have some data to read.  From now on,
                   1140:         * make sure to keep sb_lastrecord consistent when working on
                   1141:         * the last packet on the chain (nextrecord == NULL) and we
                   1142:         * change m->m_nextpkt.
                   1143:         */
1.1       cgd      1144:        if (m) {
1.69      thorpej  1145:                if ((flags & MSG_PEEK) == 0) {
1.1       cgd      1146:                        m->m_nextpkt = nextrecord;
1.69      thorpej  1147:                        /*
                   1148:                         * If nextrecord == NULL (this is a single chain),
                   1149:                         * then sb_lastrecord may not be valid here if m
                   1150:                         * was changed earlier.
                   1151:                         */
                   1152:                        if (nextrecord == NULL) {
                   1153:                                KASSERT(so->so_rcv.sb_mb == m);
                   1154:                                so->so_rcv.sb_lastrecord = m;
                   1155:                        }
                   1156:                }
1.1       cgd      1157:                type = m->m_type;
                   1158:                if (type == MT_OOBDATA)
                   1159:                        flags |= MSG_OOB;
1.69      thorpej  1160:        } else {
                   1161:                if ((flags & MSG_PEEK) == 0) {
                   1162:                        KASSERT(so->so_rcv.sb_mb == m);
                   1163:                        so->so_rcv.sb_mb = nextrecord;
1.70      thorpej  1164:                        SB_EMPTY_FIXUP(&so->so_rcv);
1.69      thorpej  1165:                }
1.1       cgd      1166:        }
1.69      thorpej  1167:        SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
                   1168:        SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
                   1169:
1.1       cgd      1170:        moff = 0;
                   1171:        offset = 0;
                   1172:        while (m && uio->uio_resid > 0 && error == 0) {
                   1173:                if (m->m_type == MT_OOBDATA) {
                   1174:                        if (type != MT_OOBDATA)
                   1175:                                break;
                   1176:                } else if (type == MT_OOBDATA)
                   1177:                        break;
                   1178: #ifdef DIAGNOSTIC
                   1179:                else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
                   1180:                        panic("receive 3");
                   1181: #endif
                   1182:                so->so_state &= ~SS_RCVATMARK;
                   1183:                len = uio->uio_resid;
                   1184:                if (so->so_oobmark && len > so->so_oobmark - offset)
                   1185:                        len = so->so_oobmark - offset;
                   1186:                if (len > m->m_len - moff)
                   1187:                        len = m->m_len - moff;
                   1188:                /*
                   1189:                 * If mp is set, just pass back the mbufs.
                   1190:                 * Otherwise copy them out via the uio, then free.
                   1191:                 * Sockbuf must be consistent here (points to current mbuf,
                   1192:                 * it points to next record) when we drop priority;
                   1193:                 * we must note any additions to the sockbuf when we
                   1194:                 * block interrupts again.
                   1195:                 */
                   1196:                if (mp == 0) {
1.69      thorpej  1197:                        SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
                   1198:                        SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
1.1       cgd      1199:                        splx(s);
1.134     christos 1200:                        error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1.20      mycroft  1201:                        s = splsoftnet();
1.67      he       1202:                        if (error) {
                   1203:                                /*
                   1204:                                 * If any part of the record has been removed
                   1205:                                 * (such as the MT_SONAME mbuf, which will
                   1206:                                 * happen when PR_ADDR, and thus also
                   1207:                                 * PR_ATOMIC, is set), then drop the entire
                   1208:                                 * record to maintain the atomicity of the
                   1209:                                 * receive operation.
                   1210:                                 *
                   1211:                                 * This avoids a later panic("receive 1a")
                   1212:                                 * when compiled with DIAGNOSTIC.
                   1213:                                 */
                   1214:                                if (m && mbuf_removed
                   1215:                                    && (pr->pr_flags & PR_ATOMIC))
                   1216:                                        (void) sbdroprecord(&so->so_rcv);
                   1217:
1.57      jdolecek 1218:                                goto release;
1.67      he       1219:                        }
1.1       cgd      1220:                } else
                   1221:                        uio->uio_resid -= len;
                   1222:                if (len == m->m_len - moff) {
                   1223:                        if (m->m_flags & M_EOR)
                   1224:                                flags |= MSG_EOR;
                   1225:                        if (flags & MSG_PEEK) {
                   1226:                                m = m->m_next;
                   1227:                                moff = 0;
                   1228:                        } else {
                   1229:                                nextrecord = m->m_nextpkt;
                   1230:                                sbfree(&so->so_rcv, m);
                   1231:                                if (mp) {
                   1232:                                        *mp = m;
                   1233:                                        mp = &m->m_next;
                   1234:                                        so->so_rcv.sb_mb = m = m->m_next;
                   1235:                                        *mp = (struct mbuf *)0;
                   1236:                                } else {
                   1237:                                        MFREE(m, so->so_rcv.sb_mb);
                   1238:                                        m = so->so_rcv.sb_mb;
                   1239:                                }
1.69      thorpej  1240:                                /*
                   1241:                                 * If m != NULL, we also know that
                   1242:                                 * so->so_rcv.sb_mb != NULL.
                   1243:                                 */
                   1244:                                KASSERT(so->so_rcv.sb_mb == m);
                   1245:                                if (m) {
1.1       cgd      1246:                                        m->m_nextpkt = nextrecord;
1.69      thorpej  1247:                                        if (nextrecord == NULL)
                   1248:                                                so->so_rcv.sb_lastrecord = m;
                   1249:                                } else {
                   1250:                                        so->so_rcv.sb_mb = nextrecord;
1.70      thorpej  1251:                                        SB_EMPTY_FIXUP(&so->so_rcv);
1.69      thorpej  1252:                                }
                   1253:                                SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
                   1254:                                SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
1.1       cgd      1255:                        }
                   1256:                } else {
                   1257:                        if (flags & MSG_PEEK)
                   1258:                                moff += len;
                   1259:                        else {
                   1260:                                if (mp)
                   1261:                                        *mp = m_copym(m, 0, len, M_WAIT);
                   1262:                                m->m_data += len;
                   1263:                                m->m_len -= len;
                   1264:                                so->so_rcv.sb_cc -= len;
                   1265:                        }
                   1266:                }
                   1267:                if (so->so_oobmark) {
                   1268:                        if ((flags & MSG_PEEK) == 0) {
                   1269:                                so->so_oobmark -= len;
                   1270:                                if (so->so_oobmark == 0) {
                   1271:                                        so->so_state |= SS_RCVATMARK;
                   1272:                                        break;
                   1273:                                }
1.7       cgd      1274:                        } else {
1.1       cgd      1275:                                offset += len;
1.7       cgd      1276:                                if (offset == so->so_oobmark)
                   1277:                                        break;
                   1278:                        }
1.1       cgd      1279:                }
                   1280:                if (flags & MSG_EOR)
                   1281:                        break;
                   1282:                /*
                   1283:                 * If the MSG_WAITALL flag is set (for non-atomic socket),
                   1284:                 * we must not quit until "uio->uio_resid == 0" or an error
                   1285:                 * termination.  If a signal/timeout occurs, return
                   1286:                 * with a short count but without error.
                   1287:                 * Keep sockbuf locked against other readers.
                   1288:                 */
                   1289:                while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
1.3       andrew   1290:                    !sosendallatonce(so) && !nextrecord) {
1.1       cgd      1291:                        if (so->so_error || so->so_state & SS_CANTRCVMORE)
                   1292:                                break;
1.68      matt     1293:                        /*
                   1294:                         * If we are peeking and the socket receive buffer is
                   1295:                         * full, stop since we can't get more data to peek at.
                   1296:                         */
                   1297:                        if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0)
                   1298:                                break;
                   1299:                        /*
                   1300:                         * If we've drained the socket buffer, tell the
                   1301:                         * protocol in case it needs to do something to
                   1302:                         * get it filled again.
                   1303:                         */
                   1304:                        if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
                   1305:                                (*pr->pr_usrreq)(so, PRU_RCVD,
                   1306:                                    (struct mbuf *)0,
                   1307:                                    (struct mbuf *)(long)flags,
1.114     christos 1308:                                    (struct mbuf *)0, l);
1.69      thorpej  1309:                        SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
                   1310:                        SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
1.1       cgd      1311:                        error = sbwait(&so->so_rcv);
                   1312:                        if (error) {
                   1313:                                sbunlock(&so->so_rcv);
                   1314:                                splx(s);
                   1315:                                return (0);
                   1316:                        }
1.21      christos 1317:                        if ((m = so->so_rcv.sb_mb) != NULL)
1.1       cgd      1318:                                nextrecord = m->m_nextpkt;
                   1319:                }
                   1320:        }
1.3       andrew   1321:
                   1322:        if (m && pr->pr_flags & PR_ATOMIC) {
                   1323:                flags |= MSG_TRUNC;
                   1324:                if ((flags & MSG_PEEK) == 0)
                   1325:                        (void) sbdroprecord(&so->so_rcv);
                   1326:        }
1.1       cgd      1327:        if ((flags & MSG_PEEK) == 0) {
1.69      thorpej  1328:                if (m == 0) {
                   1329:                        /*
1.70      thorpej  1330:                         * First part is an inline SB_EMPTY_FIXUP().  Second
1.69      thorpej  1331:                         * part makes sure sb_lastrecord is up-to-date if
                   1332:                         * there is still data in the socket buffer.
                   1333:                         */
1.1       cgd      1334:                        so->so_rcv.sb_mb = nextrecord;
1.69      thorpej  1335:                        if (so->so_rcv.sb_mb == NULL) {
                   1336:                                so->so_rcv.sb_mbtail = NULL;
                   1337:                                so->so_rcv.sb_lastrecord = NULL;
                   1338:                        } else if (nextrecord->m_nextpkt == NULL)
                   1339:                                so->so_rcv.sb_lastrecord = nextrecord;
                   1340:                }
                   1341:                SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
                   1342:                SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
1.1       cgd      1343:                if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.22      mycroft  1344:                        (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
1.114     christos 1345:                            (struct mbuf *)(long)flags, (struct mbuf *)0, l);
1.1       cgd      1346:        }
1.3       andrew   1347:        if (orig_resid == uio->uio_resid && orig_resid &&
                   1348:            (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
                   1349:                sbunlock(&so->so_rcv);
                   1350:                splx(s);
                   1351:                goto restart;
                   1352:        }
1.108     perry    1353:
1.1       cgd      1354:        if (flagsp)
                   1355:                *flagsp |= flags;
1.54      lukem    1356:  release:
1.1       cgd      1357:        sbunlock(&so->so_rcv);
                   1358:        splx(s);
                   1359:        return (error);
                   1360: }
                   1361:
1.14      mycroft  1362: int
1.54      lukem    1363: soshutdown(struct socket *so, int how)
1.1       cgd      1364: {
1.99      matt     1365:        const struct protosw    *pr;
1.34      kleink   1366:
1.54      lukem    1367:        pr = so->so_proto;
1.34      kleink   1368:        if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
                   1369:                return (EINVAL);
1.1       cgd      1370:
1.34      kleink   1371:        if (how == SHUT_RD || how == SHUT_RDWR)
1.1       cgd      1372:                sorflush(so);
1.34      kleink   1373:        if (how == SHUT_WR || how == SHUT_RDWR)
1.22      mycroft  1374:                return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
1.114     christos 1375:                    (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1.1       cgd      1376:        return (0);
                   1377: }
                   1378:
1.14      mycroft  1379: void
1.54      lukem    1380: sorflush(struct socket *so)
1.1       cgd      1381: {
1.54      lukem    1382:        struct sockbuf  *sb, asb;
1.99      matt     1383:        const struct protosw    *pr;
1.54      lukem    1384:        int             s;
1.1       cgd      1385:
1.54      lukem    1386:        sb = &so->so_rcv;
                   1387:        pr = so->so_proto;
1.1       cgd      1388:        sb->sb_flags |= SB_NOINTR;
1.15      mycroft  1389:        (void) sblock(sb, M_WAITOK);
1.56      thorpej  1390:        s = splnet();
1.1       cgd      1391:        socantrcvmore(so);
                   1392:        sbunlock(sb);
                   1393:        asb = *sb;
1.86      wrstuden 1394:        /*
                   1395:         * Clear most of the sockbuf structure, but leave some of the
                   1396:         * fields valid.
                   1397:         */
                   1398:        memset(&sb->sb_startzero, 0,
                   1399:            sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
1.1       cgd      1400:        splx(s);
                   1401:        if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
                   1402:                (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1.98      christos 1403:        sbrelease(&asb, so);
1.1       cgd      1404: }
                   1405:
1.14      mycroft  1406: int
1.54      lukem    1407: sosetopt(struct socket *so, int level, int optname, struct mbuf *m0)
1.1       cgd      1408: {
1.54      lukem    1409:        int             error;
                   1410:        struct mbuf     *m;
1.130     christos 1411:        struct linger   *l;
1.1       cgd      1412:
1.54      lukem    1413:        error = 0;
                   1414:        m = m0;
1.1       cgd      1415:        if (level != SOL_SOCKET) {
                   1416:                if (so->so_proto && so->so_proto->pr_ctloutput)
                   1417:                        return ((*so->so_proto->pr_ctloutput)
                   1418:                                  (PRCO_SETOPT, so, level, optname, &m0));
                   1419:                error = ENOPROTOOPT;
                   1420:        } else {
                   1421:                switch (optname) {
                   1422:
                   1423:                case SO_LINGER:
1.36      perry    1424:                        if (m == NULL || m->m_len != sizeof(struct linger)) {
1.1       cgd      1425:                                error = EINVAL;
                   1426:                                goto bad;
                   1427:                        }
1.130     christos 1428:                        l = mtod(m, struct linger *);
1.131     christos 1429:                        if (l->l_linger < 0 || l->l_linger > USHRT_MAX ||
                   1430:                            l->l_linger > (INT_MAX / hz)) {
1.112     nathanw  1431:                                error = EDOM;
                   1432:                                goto bad;
                   1433:                        }
1.130     christos 1434:                        so->so_linger = l->l_linger;
                   1435:                        if (l->l_onoff)
                   1436:                                so->so_options |= SO_LINGER;
                   1437:                        else
                   1438:                                so->so_options &= ~SO_LINGER;
                   1439:                        break;
1.1       cgd      1440:
                   1441:                case SO_DEBUG:
                   1442:                case SO_KEEPALIVE:
                   1443:                case SO_DONTROUTE:
                   1444:                case SO_USELOOPBACK:
                   1445:                case SO_BROADCAST:
                   1446:                case SO_REUSEADDR:
1.15      mycroft  1447:                case SO_REUSEPORT:
1.1       cgd      1448:                case SO_OOBINLINE:
1.26      thorpej  1449:                case SO_TIMESTAMP:
1.36      perry    1450:                        if (m == NULL || m->m_len < sizeof(int)) {
1.1       cgd      1451:                                error = EINVAL;
                   1452:                                goto bad;
                   1453:                        }
                   1454:                        if (*mtod(m, int *))
                   1455:                                so->so_options |= optname;
                   1456:                        else
                   1457:                                so->so_options &= ~optname;
                   1458:                        break;
                   1459:
                   1460:                case SO_SNDBUF:
                   1461:                case SO_RCVBUF:
                   1462:                case SO_SNDLOWAT:
                   1463:                case SO_RCVLOWAT:
1.28      thorpej  1464:                    {
                   1465:                        int optval;
                   1466:
1.36      perry    1467:                        if (m == NULL || m->m_len < sizeof(int)) {
1.1       cgd      1468:                                error = EINVAL;
                   1469:                                goto bad;
                   1470:                        }
1.28      thorpej  1471:
                   1472:                        /*
                   1473:                         * Values < 1 make no sense for any of these
                   1474:                         * options, so disallow them.
                   1475:                         */
                   1476:                        optval = *mtod(m, int *);
                   1477:                        if (optval < 1) {
                   1478:                                error = EINVAL;
                   1479:                                goto bad;
                   1480:                        }
                   1481:
1.1       cgd      1482:                        switch (optname) {
                   1483:
                   1484:                        case SO_SNDBUF:
                   1485:                        case SO_RCVBUF:
                   1486:                                if (sbreserve(optname == SO_SNDBUF ?
                   1487:                                    &so->so_snd : &so->so_rcv,
1.98      christos 1488:                                    (u_long) optval, so) == 0) {
1.1       cgd      1489:                                        error = ENOBUFS;
                   1490:                                        goto bad;
                   1491:                                }
                   1492:                                break;
                   1493:
1.28      thorpej  1494:                        /*
                   1495:                         * Make sure the low-water is never greater than
                   1496:                         * the high-water.
                   1497:                         */
1.1       cgd      1498:                        case SO_SNDLOWAT:
1.28      thorpej  1499:                                so->so_snd.sb_lowat =
                   1500:                                    (optval > so->so_snd.sb_hiwat) ?
                   1501:                                    so->so_snd.sb_hiwat : optval;
1.1       cgd      1502:                                break;
                   1503:                        case SO_RCVLOWAT:
1.28      thorpej  1504:                                so->so_rcv.sb_lowat =
                   1505:                                    (optval > so->so_rcv.sb_hiwat) ?
                   1506:                                    so->so_rcv.sb_hiwat : optval;
1.1       cgd      1507:                                break;
                   1508:                        }
                   1509:                        break;
1.28      thorpej  1510:                    }
1.1       cgd      1511:
                   1512:                case SO_SNDTIMEO:
                   1513:                case SO_RCVTIMEO:
                   1514:                    {
                   1515:                        struct timeval *tv;
1.104     yamt     1516:                        int val;
1.1       cgd      1517:
1.36      perry    1518:                        if (m == NULL || m->m_len < sizeof(*tv)) {
1.1       cgd      1519:                                error = EINVAL;
                   1520:                                goto bad;
                   1521:                        }
                   1522:                        tv = mtod(m, struct timeval *);
1.104     yamt     1523:                        if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) {
1.1       cgd      1524:                                error = EDOM;
                   1525:                                goto bad;
                   1526:                        }
                   1527:                        val = tv->tv_sec * hz + tv->tv_usec / tick;
1.74      itojun   1528:                        if (val == 0 && tv->tv_usec != 0)
                   1529:                                val = 1;
1.1       cgd      1530:
                   1531:                        switch (optname) {
                   1532:
                   1533:                        case SO_SNDTIMEO:
                   1534:                                so->so_snd.sb_timeo = val;
                   1535:                                break;
                   1536:                        case SO_RCVTIMEO:
                   1537:                                so->so_rcv.sb_timeo = val;
                   1538:                                break;
                   1539:                        }
                   1540:                        break;
                   1541:                    }
                   1542:
                   1543:                default:
                   1544:                        error = ENOPROTOOPT;
                   1545:                        break;
                   1546:                }
1.15      mycroft  1547:                if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
                   1548:                        (void) ((*so->so_proto->pr_ctloutput)
                   1549:                                  (PRCO_SETOPT, so, level, optname, &m0));
                   1550:                        m = NULL;       /* freed by protocol */
                   1551:                }
1.1       cgd      1552:        }
1.54      lukem    1553:  bad:
1.1       cgd      1554:        if (m)
                   1555:                (void) m_free(m);
                   1556:        return (error);
                   1557: }
                   1558:
1.14      mycroft  1559: int
1.54      lukem    1560: sogetopt(struct socket *so, int level, int optname, struct mbuf **mp)
1.1       cgd      1561: {
1.54      lukem    1562:        struct mbuf     *m;
1.1       cgd      1563:
                   1564:        if (level != SOL_SOCKET) {
                   1565:                if (so->so_proto && so->so_proto->pr_ctloutput) {
                   1566:                        return ((*so->so_proto->pr_ctloutput)
                   1567:                                  (PRCO_GETOPT, so, level, optname, mp));
                   1568:                } else
                   1569:                        return (ENOPROTOOPT);
                   1570:        } else {
                   1571:                m = m_get(M_WAIT, MT_SOOPTS);
1.36      perry    1572:                m->m_len = sizeof(int);
1.1       cgd      1573:
                   1574:                switch (optname) {
                   1575:
                   1576:                case SO_LINGER:
1.36      perry    1577:                        m->m_len = sizeof(struct linger);
1.1       cgd      1578:                        mtod(m, struct linger *)->l_onoff =
1.131     christos 1579:                            (so->so_options & SO_LINGER) ? 1 : 0;
1.1       cgd      1580:                        mtod(m, struct linger *)->l_linger = so->so_linger;
                   1581:                        break;
                   1582:
                   1583:                case SO_USELOOPBACK:
                   1584:                case SO_DONTROUTE:
                   1585:                case SO_DEBUG:
                   1586:                case SO_KEEPALIVE:
                   1587:                case SO_REUSEADDR:
1.15      mycroft  1588:                case SO_REUSEPORT:
1.1       cgd      1589:                case SO_BROADCAST:
                   1590:                case SO_OOBINLINE:
1.26      thorpej  1591:                case SO_TIMESTAMP:
1.131     christos 1592:                        *mtod(m, int *) = (so->so_options & optname) ? 1 : 0;
1.1       cgd      1593:                        break;
                   1594:
                   1595:                case SO_TYPE:
                   1596:                        *mtod(m, int *) = so->so_type;
                   1597:                        break;
                   1598:
                   1599:                case SO_ERROR:
                   1600:                        *mtod(m, int *) = so->so_error;
                   1601:                        so->so_error = 0;
                   1602:                        break;
                   1603:
                   1604:                case SO_SNDBUF:
                   1605:                        *mtod(m, int *) = so->so_snd.sb_hiwat;
                   1606:                        break;
                   1607:
                   1608:                case SO_RCVBUF:
                   1609:                        *mtod(m, int *) = so->so_rcv.sb_hiwat;
                   1610:                        break;
                   1611:
                   1612:                case SO_SNDLOWAT:
                   1613:                        *mtod(m, int *) = so->so_snd.sb_lowat;
                   1614:                        break;
                   1615:
                   1616:                case SO_RCVLOWAT:
                   1617:                        *mtod(m, int *) = so->so_rcv.sb_lowat;
                   1618:                        break;
                   1619:
                   1620:                case SO_SNDTIMEO:
                   1621:                case SO_RCVTIMEO:
                   1622:                    {
                   1623:                        int val = (optname == SO_SNDTIMEO ?
                   1624:                             so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
                   1625:
                   1626:                        m->m_len = sizeof(struct timeval);
                   1627:                        mtod(m, struct timeval *)->tv_sec = val / hz;
                   1628:                        mtod(m, struct timeval *)->tv_usec =
1.27      kleink   1629:                            (val % hz) * tick;
1.1       cgd      1630:                        break;
                   1631:                    }
                   1632:
1.107     darrenr  1633:                case SO_OVERFLOWED:
                   1634:                        *mtod(m, int *) = so->so_rcv.sb_overflowed;
                   1635:                        break;
                   1636:
1.1       cgd      1637:                default:
                   1638:                        (void)m_free(m);
                   1639:                        return (ENOPROTOOPT);
                   1640:                }
                   1641:                *mp = m;
                   1642:                return (0);
                   1643:        }
                   1644: }
                   1645:
1.14      mycroft  1646: void
1.54      lukem    1647: sohasoutofband(struct socket *so)
1.1       cgd      1648: {
1.90      christos 1649:        fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so);
1.2       cgd      1650:        selwakeup(&so->so_rcv.sb_sel);
1.1       cgd      1651: }
1.72      jdolecek 1652:
                   1653: static void
                   1654: filt_sordetach(struct knote *kn)
                   1655: {
                   1656:        struct socket   *so;
                   1657:
                   1658:        so = (struct socket *)kn->kn_fp->f_data;
1.73      christos 1659:        SLIST_REMOVE(&so->so_rcv.sb_sel.sel_klist, kn, knote, kn_selnext);
                   1660:        if (SLIST_EMPTY(&so->so_rcv.sb_sel.sel_klist))
1.72      jdolecek 1661:                so->so_rcv.sb_flags &= ~SB_KNOTE;
                   1662: }
                   1663:
                   1664: /*ARGSUSED*/
                   1665: static int
1.129     yamt     1666: filt_soread(struct knote *kn, long hint)
1.72      jdolecek 1667: {
                   1668:        struct socket   *so;
                   1669:
                   1670:        so = (struct socket *)kn->kn_fp->f_data;
                   1671:        kn->kn_data = so->so_rcv.sb_cc;
                   1672:        if (so->so_state & SS_CANTRCVMORE) {
1.108     perry    1673:                kn->kn_flags |= EV_EOF;
1.72      jdolecek 1674:                kn->kn_fflags = so->so_error;
                   1675:                return (1);
                   1676:        }
                   1677:        if (so->so_error)       /* temporary udp error */
                   1678:                return (1);
                   1679:        if (kn->kn_sfflags & NOTE_LOWAT)
                   1680:                return (kn->kn_data >= kn->kn_sdata);
                   1681:        return (kn->kn_data >= so->so_rcv.sb_lowat);
                   1682: }
                   1683:
                   1684: static void
                   1685: filt_sowdetach(struct knote *kn)
                   1686: {
                   1687:        struct socket   *so;
                   1688:
                   1689:        so = (struct socket *)kn->kn_fp->f_data;
1.73      christos 1690:        SLIST_REMOVE(&so->so_snd.sb_sel.sel_klist, kn, knote, kn_selnext);
                   1691:        if (SLIST_EMPTY(&so->so_snd.sb_sel.sel_klist))
1.72      jdolecek 1692:                so->so_snd.sb_flags &= ~SB_KNOTE;
                   1693: }
                   1694:
                   1695: /*ARGSUSED*/
                   1696: static int
1.129     yamt     1697: filt_sowrite(struct knote *kn, long hint)
1.72      jdolecek 1698: {
                   1699:        struct socket   *so;
                   1700:
                   1701:        so = (struct socket *)kn->kn_fp->f_data;
                   1702:        kn->kn_data = sbspace(&so->so_snd);
                   1703:        if (so->so_state & SS_CANTSENDMORE) {
1.108     perry    1704:                kn->kn_flags |= EV_EOF;
1.72      jdolecek 1705:                kn->kn_fflags = so->so_error;
                   1706:                return (1);
                   1707:        }
                   1708:        if (so->so_error)       /* temporary udp error */
                   1709:                return (1);
                   1710:        if (((so->so_state & SS_ISCONNECTED) == 0) &&
                   1711:            (so->so_proto->pr_flags & PR_CONNREQUIRED))
                   1712:                return (0);
                   1713:        if (kn->kn_sfflags & NOTE_LOWAT)
                   1714:                return (kn->kn_data >= kn->kn_sdata);
                   1715:        return (kn->kn_data >= so->so_snd.sb_lowat);
                   1716: }
                   1717:
                   1718: /*ARGSUSED*/
                   1719: static int
1.129     yamt     1720: filt_solisten(struct knote *kn, long hint)
1.72      jdolecek 1721: {
                   1722:        struct socket   *so;
                   1723:
                   1724:        so = (struct socket *)kn->kn_fp->f_data;
                   1725:
                   1726:        /*
                   1727:         * Set kn_data to number of incoming connections, not
                   1728:         * counting partial (incomplete) connections.
1.108     perry    1729:         */
1.72      jdolecek 1730:        kn->kn_data = so->so_qlen;
                   1731:        return (kn->kn_data > 0);
                   1732: }
                   1733:
                   1734: static const struct filterops solisten_filtops =
                   1735:        { 1, NULL, filt_sordetach, filt_solisten };
                   1736: static const struct filterops soread_filtops =
                   1737:        { 1, NULL, filt_sordetach, filt_soread };
                   1738: static const struct filterops sowrite_filtops =
                   1739:        { 1, NULL, filt_sowdetach, filt_sowrite };
                   1740:
                   1741: int
1.129     yamt     1742: soo_kqfilter(struct file *fp, struct knote *kn)
1.72      jdolecek 1743: {
                   1744:        struct socket   *so;
                   1745:        struct sockbuf  *sb;
                   1746:
                   1747:        so = (struct socket *)kn->kn_fp->f_data;
                   1748:        switch (kn->kn_filter) {
                   1749:        case EVFILT_READ:
                   1750:                if (so->so_options & SO_ACCEPTCONN)
                   1751:                        kn->kn_fop = &solisten_filtops;
                   1752:                else
                   1753:                        kn->kn_fop = &soread_filtops;
                   1754:                sb = &so->so_rcv;
                   1755:                break;
                   1756:        case EVFILT_WRITE:
                   1757:                kn->kn_fop = &sowrite_filtops;
                   1758:                sb = &so->so_snd;
                   1759:                break;
                   1760:        default:
                   1761:                return (1);
                   1762:        }
1.73      christos 1763:        SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext);
1.72      jdolecek 1764:        sb->sb_flags |= SB_KNOTE;
                   1765:        return (0);
                   1766: }
                   1767:
1.94      yamt     1768: #include <sys/sysctl.h>
                   1769:
                   1770: static int sysctl_kern_somaxkva(SYSCTLFN_PROTO);
                   1771:
                   1772: /*
                   1773:  * sysctl helper routine for kern.somaxkva.  ensures that the given
                   1774:  * value is not too small.
                   1775:  * (XXX should we maybe make sure it's not too large as well?)
                   1776:  */
                   1777: static int
                   1778: sysctl_kern_somaxkva(SYSCTLFN_ARGS)
                   1779: {
                   1780:        int error, new_somaxkva;
                   1781:        struct sysctlnode node;
                   1782:
                   1783:        new_somaxkva = somaxkva;
                   1784:        node = *rnode;
                   1785:        node.sysctl_data = &new_somaxkva;
                   1786:        error = sysctl_lookup(SYSCTLFN_CALL(&node));
                   1787:        if (error || newp == NULL)
                   1788:                return (error);
                   1789:
                   1790:        if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */
                   1791:                return (EINVAL);
                   1792:
1.136     ad       1793:        mutex_enter(&so_pendfree_lock);
1.94      yamt     1794:        somaxkva = new_somaxkva;
1.136     ad       1795:        cv_broadcast(&socurkva_cv);
                   1796:        mutex_exit(&so_pendfree_lock);
1.94      yamt     1797:
                   1798:        return (error);
                   1799: }
                   1800:
                   1801: SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup")
                   1802: {
                   1803:
1.97      atatat   1804:        sysctl_createv(clog, 0, NULL, NULL,
                   1805:                       CTLFLAG_PERMANENT,
                   1806:                       CTLTYPE_NODE, "kern", NULL,
                   1807:                       NULL, 0, NULL, 0,
                   1808:                       CTL_KERN, CTL_EOL);
                   1809:
                   1810:        sysctl_createv(clog, 0, NULL, NULL,
                   1811:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.103     atatat   1812:                       CTLTYPE_INT, "somaxkva",
                   1813:                       SYSCTL_DESCR("Maximum amount of kernel memory to be "
                   1814:                                    "used for socket buffers"),
1.94      yamt     1815:                       sysctl_kern_somaxkva, 0, NULL, 0,
                   1816:                       CTL_KERN, KERN_SOMAXKVA, CTL_EOL);
                   1817: }

CVSweb <webmaster@jp.NetBSD.org>