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

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

CVSweb <webmaster@jp.NetBSD.org>