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

1.54.2.12! nathanw     1: /*     $NetBSD: uipc_socket.c,v 1.54.2.11 2002/06/20 03:47:25 nathanw Exp $    */
1.54.2.11  nathanw     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.
                     51:  * 3. All advertising materials mentioning features or use of this software
                     52:  *    must display the following acknowledgement:
                     53:  *     This product includes software developed by the University of
                     54:  *     California, Berkeley and its contributors.
                     55:  * 4. Neither the name of the University nor the names of its contributors
                     56:  *    may be used to endorse or promote products derived from this software
                     57:  *    without specific prior written permission.
                     58:  *
                     59:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     60:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     61:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     62:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     63:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     64:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     65:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     66:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     67:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     68:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     69:  * SUCH DAMAGE.
                     70:  *
1.32      fvdl       71:  *     @(#)uipc_socket.c       8.6 (Berkeley) 5/2/95
1.1       cgd        72:  */
1.54.2.6  nathanw    73:
                     74: #include <sys/cdefs.h>
1.54.2.12! nathanw    75: __KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.54.2.11 2002/06/20 03:47:25 nathanw Exp $");
1.54.2.11  nathanw    76:
                     77: #include "opt_sock_counters.h"
                     78: #include "opt_sosend_loan.h"
1.1       cgd        79:
1.9       mycroft    80: #include <sys/param.h>
                     81: #include <sys/systm.h>
1.54.2.1  nathanw    82: #include <sys/lwp.h>
1.9       mycroft    83: #include <sys/proc.h>
                     84: #include <sys/file.h>
                     85: #include <sys/malloc.h>
                     86: #include <sys/mbuf.h>
                     87: #include <sys/domain.h>
                     88: #include <sys/kernel.h>
                     89: #include <sys/protosw.h>
                     90: #include <sys/socket.h>
                     91: #include <sys/socketvar.h>
1.21      christos   92: #include <sys/signalvar.h>
1.9       mycroft    93: #include <sys/resourcevar.h>
1.37      thorpej    94: #include <sys/pool.h>
                     95:
1.54.2.11  nathanw    96: #include <uvm/uvm.h>
                     97:
1.54      lukem      98: struct pool    socket_pool;
1.37      thorpej    99:
1.54      lukem     100: extern int     somaxconn;                      /* patchable (XXX sysctl) */
                    101: int            somaxconn = SOMAXCONN;
1.49      jonathan  102:
1.54.2.11  nathanw   103: #ifdef SOSEND_COUNTERS
                    104: #include <sys/device.h>
                    105:
                    106: struct evcnt sosend_loan_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    107:     NULL, "sosend", "loan big");
                    108: struct evcnt sosend_copy_big = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    109:     NULL, "sosend", "copy big");
                    110: struct evcnt sosend_copy_small = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    111:     NULL, "sosend", "copy small");
                    112: struct evcnt sosend_kvalimit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
                    113:     NULL, "sosend", "kva limit");
                    114:
                    115: #define        SOSEND_COUNTER_INCR(ev)         (ev)->ev_count++
                    116:
                    117: #else
                    118:
                    119: #define        SOSEND_COUNTER_INCR(ev)         /* nothing */
                    120:
                    121: #endif /* SOSEND_COUNTERS */
                    122:
1.37      thorpej   123: void
1.54      lukem     124: soinit(void)
1.37      thorpej   125: {
                    126:
                    127:        pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0,
1.54.2.9  nathanw   128:            "sockpl", NULL);
1.54.2.11  nathanw   129:
                    130: #ifdef SOSEND_COUNTERS
                    131:        evcnt_attach_static(&sosend_loan_big);
                    132:        evcnt_attach_static(&sosend_copy_big);
                    133:        evcnt_attach_static(&sosend_copy_small);
                    134:        evcnt_attach_static(&sosend_kvalimit);
                    135: #endif /* SOSEND_COUNTERS */
                    136: }
                    137:
                    138: #ifdef SOSEND_LOAN
                    139: int use_sosend_loan = 1;
                    140: #else
                    141: int use_sosend_loan = 0;
                    142: #endif
                    143:
                    144: struct mbuf *so_pendfree;
                    145:
                    146: int somaxkva = 16 * 1024 * 1024;
                    147: int socurkva;
                    148: int sokvawaiters;
                    149:
                    150: #define        SOCK_LOAN_THRESH        4096
                    151: #define        SOCK_LOAN_CHUNK         65536
                    152:
                    153: static void
                    154: sodoloanfree(caddr_t buf, u_int size)
                    155: {
                    156:        struct vm_page **pgs;
                    157:        vaddr_t va, sva, eva;
                    158:        vsize_t len;
                    159:        paddr_t pa;
                    160:        int i, npgs;
                    161:
                    162:        eva = round_page((vaddr_t) buf + size);
                    163:        sva = trunc_page((vaddr_t) buf);
                    164:        len = eva - sva;
                    165:        npgs = len >> PAGE_SHIFT;
                    166:
                    167:        pgs = alloca(npgs * sizeof(*pgs));
                    168:
                    169:        for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) {
                    170:                if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
                    171:                        panic("sodoloanfree: va 0x%lx not mapped", va);
                    172:                pgs[i] = PHYS_TO_VM_PAGE(pa);
                    173:        }
                    174:
                    175:        pmap_kremove(sva, len);
                    176:        pmap_update(pmap_kernel());
                    177:        uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE);
                    178:        uvm_km_free(kernel_map, sva, len);
                    179:        socurkva -= len;
                    180:        if (sokvawaiters)
                    181:                wakeup(&socurkva);
                    182: }
                    183:
                    184: static size_t
                    185: sodopendfree(struct socket *so)
                    186: {
                    187:        struct mbuf *m;
                    188:        size_t rv = 0;
                    189:        int s;
                    190:
                    191:        s = splvm();
                    192:
                    193:        for (;;) {
                    194:                m = so_pendfree;
                    195:                if (m == NULL)
                    196:                        break;
                    197:                so_pendfree = m->m_next;
                    198:                splx(s);
                    199:
                    200:                rv += m->m_ext.ext_size;
                    201:                sodoloanfree(m->m_ext.ext_buf, m->m_ext.ext_size);
                    202:                s = splvm();
                    203:                pool_cache_put(&mbpool_cache, m);
                    204:        }
                    205:
                    206:        for (;;) {
                    207:                m = so->so_pendfree;
                    208:                if (m == NULL)
                    209:                        break;
                    210:                so->so_pendfree = m->m_next;
                    211:                splx(s);
                    212:
                    213:                rv += m->m_ext.ext_size;
                    214:                sodoloanfree(m->m_ext.ext_buf, m->m_ext.ext_size);
                    215:                s = splvm();
                    216:                pool_cache_put(&mbpool_cache, m);
                    217:        }
                    218:
                    219:        splx(s);
                    220:        return (rv);
                    221: }
                    222:
                    223: static void
                    224: soloanfree(struct mbuf *m, caddr_t buf, u_int size, void *arg)
                    225: {
                    226:        struct socket *so = arg;
                    227:        int s;
                    228:
                    229:        if (m == NULL) {
                    230:                sodoloanfree(buf, size);
                    231:                return;
                    232:        }
                    233:
                    234:        s = splvm();
                    235:        m->m_next = so->so_pendfree;
                    236:        so->so_pendfree = m;
                    237:        splx(s);
                    238:        if (sokvawaiters)
                    239:                wakeup(&socurkva);
                    240: }
                    241:
                    242: static long
                    243: sosend_loan(struct socket *so, struct uio *uio, struct mbuf *m, long space)
                    244: {
                    245:        struct iovec *iov = uio->uio_iov;
                    246:        vaddr_t sva, eva;
                    247:        vsize_t len;
                    248:        struct vm_page **pgs;
                    249:        vaddr_t lva, va;
                    250:        int npgs, s, i, error;
                    251:
                    252:        if (uio->uio_segflg != UIO_USERSPACE)
                    253:                return (0);
                    254:
                    255:        if (iov->iov_len < (size_t) space)
                    256:                space = iov->iov_len;
                    257:        if (space > SOCK_LOAN_CHUNK)
                    258:                space = SOCK_LOAN_CHUNK;
                    259:
                    260:        eva = round_page((vaddr_t) iov->iov_base + space);
                    261:        sva = trunc_page((vaddr_t) iov->iov_base);
                    262:        len = eva - sva;
                    263:        npgs = len >> PAGE_SHIFT;
                    264:
                    265:        while (socurkva + len > somaxkva) {
                    266:                if (sodopendfree(so))
                    267:                        continue;
                    268:                SOSEND_COUNTER_INCR(&sosend_kvalimit);
                    269:                s = splvm();
                    270:                sokvawaiters++;
                    271:                (void) tsleep(&socurkva, PVM, "sokva", 0);
                    272:                sokvawaiters--;
                    273:                splx(s);
                    274:        }
                    275:
                    276:        lva = uvm_km_valloc_wait(kernel_map, len);
                    277:        if (lva == 0)
                    278:                return (0);
                    279:        socurkva += len;
                    280:
                    281:        pgs = alloca(npgs * sizeof(*pgs));
                    282:
                    283:        error = uvm_loan(&uio->uio_procp->p_vmspace->vm_map, sva, len,
                    284:            pgs, UVM_LOAN_TOPAGE);
                    285:        if (error) {
                    286:                uvm_km_free(kernel_map, lva, len);
                    287:                socurkva -= len;
                    288:                return (0);
                    289:        }
                    290:
                    291:        for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE)
                    292:                pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pgs[i]), VM_PROT_READ);
                    293:        pmap_update(pmap_kernel());
                    294:
                    295:        lva += (vaddr_t) iov->iov_base & PAGE_MASK;
                    296:
                    297:        MEXTADD(m, (caddr_t) lva, space, M_MBUF, soloanfree, so);
                    298:
                    299:        uio->uio_resid -= space;
                    300:        /* uio_offset not updated, not set/used for write(2) */
                    301:        uio->uio_iov->iov_base = (caddr_t) uio->uio_iov->iov_base + space;
                    302:        uio->uio_iov->iov_len -= space;
                    303:        if (uio->uio_iov->iov_len == 0) {
                    304:                uio->uio_iov++;
                    305:                uio->uio_iovcnt--;
                    306:        }
                    307:
                    308:        return (space);
1.37      thorpej   309: }
1.1       cgd       310:
                    311: /*
                    312:  * Socket operation routines.
                    313:  * These routines are called by the routines in
                    314:  * sys_socket.c or from a system process, and
                    315:  * implement the semantics of socket operations by
                    316:  * switching out to the protocol specific routines.
                    317:  */
                    318: /*ARGSUSED*/
1.3       andrew    319: int
1.54      lukem     320: socreate(int dom, struct socket **aso, int type, int proto)
1.1       cgd       321: {
1.54      lukem     322:        struct proc     *p;
                    323:        struct protosw  *prp;
                    324:        struct socket   *so;
                    325:        int             error, s;
1.1       cgd       326:
1.54.2.12! nathanw   327:        p = curproc;    /* XXX */
1.1       cgd       328:        if (proto)
                    329:                prp = pffindproto(dom, proto, type);
                    330:        else
                    331:                prp = pffindtype(dom, type);
1.15      mycroft   332:        if (prp == 0 || prp->pr_usrreq == 0)
1.1       cgd       333:                return (EPROTONOSUPPORT);
                    334:        if (prp->pr_type != type)
                    335:                return (EPROTOTYPE);
1.39      matt      336:        s = splsoftnet();
1.37      thorpej   337:        so = pool_get(&socket_pool, PR_WAITOK);
1.38      perry     338:        memset((caddr_t)so, 0, sizeof(*so));
1.31      thorpej   339:        TAILQ_INIT(&so->so_q0);
                    340:        TAILQ_INIT(&so->so_q);
1.1       cgd       341:        so->so_type = type;
                    342:        so->so_proto = prp;
1.33      matt      343:        so->so_send = sosend;
                    344:        so->so_receive = soreceive;
1.44      lukem     345:        if (p != 0)
                    346:                so->so_uid = p->p_ucred->cr_uid;
1.22      mycroft   347:        error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
                    348:            (struct mbuf *)(long)proto, (struct mbuf *)0, p);
1.1       cgd       349:        if (error) {
                    350:                so->so_state |= SS_NOFDREF;
                    351:                sofree(so);
1.39      matt      352:                splx(s);
1.1       cgd       353:                return (error);
                    354:        }
1.39      matt      355:        splx(s);
1.1       cgd       356:        *aso = so;
                    357:        return (0);
                    358: }
                    359:
1.3       andrew    360: int
1.54      lukem     361: sobind(struct socket *so, struct mbuf *nam, struct proc *p)
1.1       cgd       362: {
1.54      lukem     363:        int     s, error;
1.1       cgd       364:
1.54      lukem     365:        s = splsoftnet();
1.22      mycroft   366:        error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0,
                    367:            nam, (struct mbuf *)0, p);
1.1       cgd       368:        splx(s);
                    369:        return (error);
                    370: }
                    371:
1.3       andrew    372: int
1.54      lukem     373: solisten(struct socket *so, int backlog)
1.1       cgd       374: {
1.54      lukem     375:        int     s, error;
1.1       cgd       376:
1.54      lukem     377:        s = splsoftnet();
1.22      mycroft   378:        error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0,
                    379:            (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1       cgd       380:        if (error) {
                    381:                splx(s);
                    382:                return (error);
                    383:        }
1.54.2.10  nathanw   384:        if (TAILQ_EMPTY(&so->so_q))
1.1       cgd       385:                so->so_options |= SO_ACCEPTCONN;
                    386:        if (backlog < 0)
                    387:                backlog = 0;
1.49      jonathan  388:        so->so_qlimit = min(backlog, somaxconn);
1.1       cgd       389:        splx(s);
                    390:        return (0);
                    391: }
                    392:
1.21      christos  393: void
1.54      lukem     394: sofree(struct socket *so)
1.1       cgd       395: {
1.54.2.11  nathanw   396:        struct mbuf *m;
1.1       cgd       397:
1.43      mycroft   398:        if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
1.1       cgd       399:                return;
1.43      mycroft   400:        if (so->so_head) {
                    401:                /*
                    402:                 * We must not decommission a socket that's on the accept(2)
                    403:                 * queue.  If we do, then accept(2) may hang after select(2)
                    404:                 * indicated that the listening socket was ready.
                    405:                 */
                    406:                if (!soqremque(so, 0))
                    407:                        return;
                    408:        }
1.1       cgd       409:        sbrelease(&so->so_snd);
                    410:        sorflush(so);
1.54.2.11  nathanw   411:        while ((m = so->so_pendfree) != NULL) {
                    412:                so->so_pendfree = m->m_next;
                    413:                m->m_next = so_pendfree;
                    414:                so_pendfree = m;
                    415:        }
1.37      thorpej   416:        pool_put(&socket_pool, so);
1.1       cgd       417: }
                    418:
                    419: /*
                    420:  * Close a socket on last file table reference removal.
                    421:  * Initiate disconnect if connected.
                    422:  * Free socket when disconnect complete.
                    423:  */
1.3       andrew    424: int
1.54      lukem     425: soclose(struct socket *so)
1.1       cgd       426: {
1.54      lukem     427:        struct socket   *so2;
                    428:        int             s, error;
1.1       cgd       429:
1.54      lukem     430:        error = 0;
                    431:        s = splsoftnet();               /* conservative */
1.1       cgd       432:        if (so->so_options & SO_ACCEPTCONN) {
1.54.2.10  nathanw   433:                while ((so2 = TAILQ_FIRST(&so->so_q0)) != 0) {
1.42      mycroft   434:                        (void) soqremque(so2, 0);
1.41      mycroft   435:                        (void) soabort(so2);
                    436:                }
1.54.2.10  nathanw   437:                while ((so2 = TAILQ_FIRST(&so->so_q)) != 0) {
1.42      mycroft   438:                        (void) soqremque(so2, 1);
1.41      mycroft   439:                        (void) soabort(so2);
                    440:                }
1.1       cgd       441:        }
                    442:        if (so->so_pcb == 0)
                    443:                goto discard;
                    444:        if (so->so_state & SS_ISCONNECTED) {
                    445:                if ((so->so_state & SS_ISDISCONNECTING) == 0) {
                    446:                        error = sodisconnect(so);
                    447:                        if (error)
                    448:                                goto drop;
                    449:                }
                    450:                if (so->so_options & SO_LINGER) {
                    451:                        if ((so->so_state & SS_ISDISCONNECTING) &&
                    452:                            (so->so_state & SS_NBIO))
                    453:                                goto drop;
1.21      christos  454:                        while (so->so_state & SS_ISCONNECTED) {
                    455:                                error = tsleep((caddr_t)&so->so_timeo,
                    456:                                               PSOCK | PCATCH, netcls,
1.30      thorpej   457:                                               so->so_linger * hz);
1.21      christos  458:                                if (error)
1.1       cgd       459:                                        break;
1.21      christos  460:                        }
1.1       cgd       461:                }
                    462:        }
1.54      lukem     463:  drop:
1.1       cgd       464:        if (so->so_pcb) {
1.22      mycroft   465:                int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
                    466:                    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
                    467:                    (struct proc *)0);
1.1       cgd       468:                if (error == 0)
                    469:                        error = error2;
                    470:        }
1.54      lukem     471:  discard:
1.1       cgd       472:        if (so->so_state & SS_NOFDREF)
                    473:                panic("soclose: NOFDREF");
                    474:        so->so_state |= SS_NOFDREF;
                    475:        sofree(so);
                    476:        splx(s);
                    477:        return (error);
                    478: }
                    479:
                    480: /*
1.20      mycroft   481:  * Must be called at splsoftnet...
1.1       cgd       482:  */
1.3       andrew    483: int
1.54      lukem     484: soabort(struct socket *so)
1.1       cgd       485: {
                    486:
1.22      mycroft   487:        return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0,
                    488:            (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1       cgd       489: }
                    490:
1.3       andrew    491: int
1.54      lukem     492: soaccept(struct socket *so, struct mbuf *nam)
1.1       cgd       493: {
1.54      lukem     494:        int     s, error;
1.1       cgd       495:
1.54      lukem     496:        error = 0;
                    497:        s = splsoftnet();
1.1       cgd       498:        if ((so->so_state & SS_NOFDREF) == 0)
                    499:                panic("soaccept: !NOFDREF");
                    500:        so->so_state &= ~SS_NOFDREF;
1.54.2.2  nathanw   501:        if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
                    502:            (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
1.41      mycroft   503:                error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
                    504:                    (struct mbuf *)0, nam, (struct mbuf *)0, (struct proc *)0);
                    505:        else
1.53      itojun    506:                error = ECONNABORTED;
1.52      itojun    507:
1.1       cgd       508:        splx(s);
                    509:        return (error);
                    510: }
                    511:
1.3       andrew    512: int
1.54      lukem     513: soconnect(struct socket *so, struct mbuf *nam)
1.1       cgd       514: {
1.54      lukem     515:        struct proc     *p;
                    516:        int             s, error;
1.1       cgd       517:
1.54.2.12! nathanw   518:        p = curproc;            /* XXX */
1.1       cgd       519:        if (so->so_options & SO_ACCEPTCONN)
                    520:                return (EOPNOTSUPP);
1.20      mycroft   521:        s = splsoftnet();
1.1       cgd       522:        /*
                    523:         * If protocol is connection-based, can only connect once.
                    524:         * Otherwise, if connected, try to disconnect first.
                    525:         * This allows user to disconnect by connecting to, e.g.,
                    526:         * a null address.
                    527:         */
                    528:        if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
                    529:            ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
                    530:            (error = sodisconnect(so))))
                    531:                error = EISCONN;
                    532:        else
                    533:                error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
1.23      mycroft   534:                    (struct mbuf *)0, nam, (struct mbuf *)0, p);
1.1       cgd       535:        splx(s);
                    536:        return (error);
                    537: }
                    538:
1.3       andrew    539: int
1.54      lukem     540: soconnect2(struct socket *so1, struct socket *so2)
1.1       cgd       541: {
1.54      lukem     542:        int     s, error;
1.1       cgd       543:
1.54      lukem     544:        s = splsoftnet();
1.22      mycroft   545:        error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
                    546:            (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0,
                    547:            (struct proc *)0);
1.1       cgd       548:        splx(s);
                    549:        return (error);
                    550: }
                    551:
1.3       andrew    552: int
1.54      lukem     553: sodisconnect(struct socket *so)
1.1       cgd       554: {
1.54      lukem     555:        int     s, error;
1.1       cgd       556:
1.54      lukem     557:        s = splsoftnet();
1.1       cgd       558:        if ((so->so_state & SS_ISCONNECTED) == 0) {
                    559:                error = ENOTCONN;
                    560:                goto bad;
                    561:        }
                    562:        if (so->so_state & SS_ISDISCONNECTING) {
                    563:                error = EALREADY;
                    564:                goto bad;
                    565:        }
1.22      mycroft   566:        error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
                    567:            (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
                    568:            (struct proc *)0);
1.54      lukem     569:  bad:
1.1       cgd       570:        splx(s);
1.54.2.11  nathanw   571:        sodopendfree(so);
1.1       cgd       572:        return (error);
                    573: }
                    574:
1.15      mycroft   575: #define        SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
1.1       cgd       576: /*
                    577:  * Send on a socket.
                    578:  * If send must go all at once and message is larger than
                    579:  * send buffering, then hard error.
                    580:  * Lock against other senders.
                    581:  * If must go all at once and not enough room now, then
                    582:  * inform user that this would block and do nothing.
                    583:  * Otherwise, if nonblocking, send as much as possible.
                    584:  * The data to be sent is described by "uio" if nonzero,
                    585:  * otherwise by the mbuf chain "top" (which must be null
                    586:  * if uio is not).  Data provided in mbuf chain must be small
                    587:  * enough to send all at once.
                    588:  *
                    589:  * Returns nonzero on error, timeout or signal; callers
                    590:  * must check for short counts if EINTR/ERESTART are returned.
                    591:  * Data and control buffers are freed on return.
                    592:  */
1.3       andrew    593: int
1.54      lukem     594: sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
                    595:        struct mbuf *control, int flags)
1.1       cgd       596: {
1.54      lukem     597:        struct proc     *p;
                    598:        struct mbuf     **mp, *m;
1.54.2.5  nathanw   599:        long            space, len, resid, clen, mlen;
                    600:        int             error, s, dontroute, atomic;
1.54      lukem     601:
1.54.2.11  nathanw   602:        sodopendfree(so);
                    603:
1.54.2.12! nathanw   604:        p = curproc;            /* XXX */
1.54      lukem     605:        clen = 0;
                    606:        atomic = sosendallatonce(so) || top;
1.1       cgd       607:        if (uio)
                    608:                resid = uio->uio_resid;
                    609:        else
                    610:                resid = top->m_pkthdr.len;
1.7       cgd       611:        /*
                    612:         * In theory resid should be unsigned.
                    613:         * However, space must be signed, as it might be less than 0
                    614:         * if we over-committed, and we must use a signed comparison
                    615:         * of space and resid.  On the other hand, a negative resid
                    616:         * causes us to loop sending 0-length segments to the protocol.
                    617:         */
1.29      mycroft   618:        if (resid < 0) {
                    619:                error = EINVAL;
                    620:                goto out;
                    621:        }
1.1       cgd       622:        dontroute =
                    623:            (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
                    624:            (so->so_proto->pr_flags & PR_ATOMIC);
1.12      mycroft   625:        p->p_stats->p_ru.ru_msgsnd++;
1.1       cgd       626:        if (control)
                    627:                clen = control->m_len;
                    628: #define        snderr(errno)   { error = errno; splx(s); goto release; }
                    629:
1.54      lukem     630:  restart:
1.21      christos  631:        if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
1.1       cgd       632:                goto out;
                    633:        do {
1.20      mycroft   634:                s = splsoftnet();
1.1       cgd       635:                if (so->so_state & SS_CANTSENDMORE)
                    636:                        snderr(EPIPE);
1.48      thorpej   637:                if (so->so_error) {
                    638:                        error = so->so_error;
                    639:                        so->so_error = 0;
                    640:                        splx(s);
                    641:                        goto release;
                    642:                }
1.1       cgd       643:                if ((so->so_state & SS_ISCONNECTED) == 0) {
                    644:                        if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
                    645:                                if ((so->so_state & SS_ISCONFIRMING) == 0 &&
                    646:                                    !(resid == 0 && clen != 0))
                    647:                                        snderr(ENOTCONN);
                    648:                        } else if (addr == 0)
                    649:                                snderr(EDESTADDRREQ);
                    650:                }
                    651:                space = sbspace(&so->so_snd);
                    652:                if (flags & MSG_OOB)
                    653:                        space += 1024;
1.21      christos  654:                if ((atomic && resid > so->so_snd.sb_hiwat) ||
1.11      mycroft   655:                    clen > so->so_snd.sb_hiwat)
                    656:                        snderr(EMSGSIZE);
                    657:                if (space < resid + clen && uio &&
1.1       cgd       658:                    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
                    659:                        if (so->so_state & SS_NBIO)
                    660:                                snderr(EWOULDBLOCK);
                    661:                        sbunlock(&so->so_snd);
                    662:                        error = sbwait(&so->so_snd);
                    663:                        splx(s);
                    664:                        if (error)
                    665:                                goto out;
                    666:                        goto restart;
                    667:                }
                    668:                splx(s);
                    669:                mp = &top;
                    670:                space -= clen;
                    671:                do {
1.45      tv        672:                        if (uio == NULL) {
                    673:                                /*
                    674:                                 * Data is prepackaged in "top".
                    675:                                 */
                    676:                                resid = 0;
                    677:                                if (flags & MSG_EOR)
                    678:                                        top->m_flags |= M_EOR;
                    679:                        } else do {
                    680:                                if (top == 0) {
                    681:                                        MGETHDR(m, M_WAIT, MT_DATA);
                    682:                                        mlen = MHLEN;
                    683:                                        m->m_pkthdr.len = 0;
                    684:                                        m->m_pkthdr.rcvif = (struct ifnet *)0;
                    685:                                } else {
                    686:                                        MGET(m, M_WAIT, MT_DATA);
                    687:                                        mlen = MLEN;
                    688:                                }
1.54.2.11  nathanw   689:                                if (use_sosend_loan &&
                    690:                                    uio->uio_iov->iov_len >= SOCK_LOAN_THRESH &&
                    691:                                    space >= SOCK_LOAN_THRESH &&
                    692:                                    (len = sosend_loan(so, uio, m,
                    693:                                                       space)) != 0) {
                    694:                                        SOSEND_COUNTER_INCR(&sosend_loan_big);
                    695:                                        space -= len;
                    696:                                        goto have_data;
                    697:                                }
1.45      tv        698:                                if (resid >= MINCLSIZE && space >= MCLBYTES) {
1.54.2.11  nathanw   699:                                        SOSEND_COUNTER_INCR(&sosend_copy_big);
1.45      tv        700:                                        MCLGET(m, M_WAIT);
                    701:                                        if ((m->m_flags & M_EXT) == 0)
                    702:                                                goto nopages;
                    703:                                        mlen = MCLBYTES;
                    704:                                        if (atomic && top == 0) {
1.54.2.5  nathanw   705:                                                len = lmin(MCLBYTES - max_hdr,
1.54      lukem     706:                                                    resid);
1.45      tv        707:                                                m->m_data += max_hdr;
                    708:                                        } else
1.54.2.5  nathanw   709:                                                len = lmin(MCLBYTES, resid);
1.45      tv        710:                                        space -= len;
                    711:                                } else {
1.54.2.11  nathanw   712:  nopages:
                    713:                                        SOSEND_COUNTER_INCR(&sosend_copy_small);
1.54.2.5  nathanw   714:                                        len = lmin(lmin(mlen, resid), space);
1.45      tv        715:                                        space -= len;
                    716:                                        /*
                    717:                                         * For datagram protocols, leave room
                    718:                                         * for protocol headers in first mbuf.
                    719:                                         */
                    720:                                        if (atomic && top == 0 && len < mlen)
                    721:                                                MH_ALIGN(m, len);
                    722:                                }
1.54      lukem     723:                                error = uiomove(mtod(m, caddr_t), (int)len,
                    724:                                    uio);
1.54.2.11  nathanw   725:  have_data:
1.45      tv        726:                                resid = uio->uio_resid;
                    727:                                m->m_len = len;
                    728:                                *mp = m;
                    729:                                top->m_pkthdr.len += len;
                    730:                                if (error)
                    731:                                        goto release;
                    732:                                mp = &m->m_next;
                    733:                                if (resid <= 0) {
                    734:                                        if (flags & MSG_EOR)
                    735:                                                top->m_flags |= M_EOR;
                    736:                                        break;
                    737:                                }
                    738:                        } while (space > 0 && atomic);
1.46      sommerfe  739:
                    740:                        s = splsoftnet();
                    741:
                    742:                        if (so->so_state & SS_CANTSENDMORE)
                    743:                                snderr(EPIPE);
1.45      tv        744:
                    745:                        if (dontroute)
                    746:                                so->so_options |= SO_DONTROUTE;
                    747:                        if (resid > 0)
                    748:                                so->so_state |= SS_MORETOCOME;
1.46      sommerfe  749:                        error = (*so->so_proto->pr_usrreq)(so,
                    750:                            (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
                    751:                            top, addr, control, p);
1.45      tv        752:                        if (dontroute)
                    753:                                so->so_options &= ~SO_DONTROUTE;
                    754:                        if (resid > 0)
                    755:                                so->so_state &= ~SS_MORETOCOME;
1.46      sommerfe  756:                        splx(s);
                    757:
1.45      tv        758:                        clen = 0;
                    759:                        control = 0;
                    760:                        top = 0;
                    761:                        mp = &top;
1.1       cgd       762:                        if (error)
                    763:                                goto release;
                    764:                } while (resid && space > 0);
                    765:        } while (resid);
                    766:
1.54      lukem     767:  release:
1.1       cgd       768:        sbunlock(&so->so_snd);
1.54      lukem     769:  out:
1.1       cgd       770:        if (top)
                    771:                m_freem(top);
                    772:        if (control)
                    773:                m_freem(control);
                    774:        return (error);
                    775: }
                    776:
                    777: /*
                    778:  * Implement receive operations on a socket.
                    779:  * We depend on the way that records are added to the sockbuf
                    780:  * by sbappend*.  In particular, each record (mbufs linked through m_next)
                    781:  * must begin with an address if the protocol so specifies,
                    782:  * followed by an optional mbuf or mbufs containing ancillary data,
                    783:  * and then zero or more mbufs of data.
                    784:  * In order to avoid blocking network interrupts for the entire time here,
                    785:  * we splx() while doing the actual copy to user space.
                    786:  * Although the sockbuf is locked, new data may still be appended,
                    787:  * and thus we must maintain consistency of the sockbuf during that time.
                    788:  *
                    789:  * The caller may receive the data as a single mbuf chain by supplying
                    790:  * an mbuf **mp0 for use in returning the chain.  The uio is then used
                    791:  * only for the count in uio_resid.
                    792:  */
1.3       andrew    793: int
1.54      lukem     794: soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
                    795:        struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1.1       cgd       796: {
1.54      lukem     797:        struct mbuf     *m, **mp;
                    798:        int             flags, len, error, s, offset, moff, type, orig_resid;
                    799:        struct protosw  *pr;
                    800:        struct mbuf     *nextrecord;
1.54.2.11  nathanw   801:        int             mbuf_removed = 0;
1.1       cgd       802:
1.54      lukem     803:        pr = so->so_proto;
1.1       cgd       804:        mp = mp0;
1.54      lukem     805:        type = 0;
                    806:        orig_resid = uio->uio_resid;
1.1       cgd       807:        if (paddr)
                    808:                *paddr = 0;
                    809:        if (controlp)
                    810:                *controlp = 0;
                    811:        if (flagsp)
                    812:                flags = *flagsp &~ MSG_EOR;
                    813:        else
                    814:                flags = 0;
1.54.2.11  nathanw   815:
                    816:        if ((flags & MSG_DONTWAIT) == 0)
                    817:                sodopendfree(so);
                    818:
1.1       cgd       819:        if (flags & MSG_OOB) {
                    820:                m = m_get(M_WAIT, MT_DATA);
1.17      cgd       821:                error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.22      mycroft   822:                    (struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0,
                    823:                    (struct proc *)0);
1.1       cgd       824:                if (error)
                    825:                        goto bad;
                    826:                do {
                    827:                        error = uiomove(mtod(m, caddr_t),
                    828:                            (int) min(uio->uio_resid, m->m_len), uio);
                    829:                        m = m_free(m);
                    830:                } while (uio->uio_resid && error == 0 && m);
1.54      lukem     831:  bad:
1.1       cgd       832:                if (m)
                    833:                        m_freem(m);
                    834:                return (error);
                    835:        }
                    836:        if (mp)
                    837:                *mp = (struct mbuf *)0;
                    838:        if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.22      mycroft   839:                (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
                    840:                    (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1       cgd       841:
1.54      lukem     842:  restart:
1.21      christos  843:        if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
1.1       cgd       844:                return (error);
1.20      mycroft   845:        s = splsoftnet();
1.1       cgd       846:
                    847:        m = so->so_rcv.sb_mb;
                    848:        /*
                    849:         * If we have less data than requested, block awaiting more
                    850:         * (subject to any timeout) if:
1.15      mycroft   851:         *   1. the current count is less than the low water mark,
1.1       cgd       852:         *   2. MSG_WAITALL is set, and it is possible to do the entire
1.15      mycroft   853:         *      receive operation at once if we block (resid <= hiwat), or
                    854:         *   3. MSG_DONTWAIT is not set.
1.1       cgd       855:         * If MSG_WAITALL is set but resid is larger than the receive buffer,
                    856:         * we have to do the receive in sections, and thus risk returning
                    857:         * a short count if a timeout or signal occurs after we start.
                    858:         */
1.21      christos  859:        if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
1.15      mycroft   860:            so->so_rcv.sb_cc < uio->uio_resid) &&
1.1       cgd       861:            (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
                    862:            ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.21      christos  863:            m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1.1       cgd       864: #ifdef DIAGNOSTIC
                    865:                if (m == 0 && so->so_rcv.sb_cc)
                    866:                        panic("receive 1");
                    867: #endif
                    868:                if (so->so_error) {
                    869:                        if (m)
1.15      mycroft   870:                                goto dontblock;
1.1       cgd       871:                        error = so->so_error;
                    872:                        if ((flags & MSG_PEEK) == 0)
                    873:                                so->so_error = 0;
                    874:                        goto release;
                    875:                }
                    876:                if (so->so_state & SS_CANTRCVMORE) {
                    877:                        if (m)
1.15      mycroft   878:                                goto dontblock;
1.1       cgd       879:                        else
                    880:                                goto release;
                    881:                }
                    882:                for (; m; m = m->m_next)
                    883:                        if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
                    884:                                m = so->so_rcv.sb_mb;
                    885:                                goto dontblock;
                    886:                        }
                    887:                if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
                    888:                    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
                    889:                        error = ENOTCONN;
                    890:                        goto release;
                    891:                }
                    892:                if (uio->uio_resid == 0)
                    893:                        goto release;
1.15      mycroft   894:                if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
1.1       cgd       895:                        error = EWOULDBLOCK;
                    896:                        goto release;
                    897:                }
                    898:                sbunlock(&so->so_rcv);
                    899:                error = sbwait(&so->so_rcv);
                    900:                splx(s);
                    901:                if (error)
                    902:                        return (error);
                    903:                goto restart;
                    904:        }
1.54      lukem     905:  dontblock:
1.15      mycroft   906: #ifdef notyet /* XXXX */
                    907:        if (uio->uio_procp)
                    908:                uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
                    909: #endif
1.1       cgd       910:        nextrecord = m->m_nextpkt;
                    911:        if (pr->pr_flags & PR_ADDR) {
                    912: #ifdef DIAGNOSTIC
                    913:                if (m->m_type != MT_SONAME)
                    914:                        panic("receive 1a");
                    915: #endif
1.3       andrew    916:                orig_resid = 0;
1.1       cgd       917:                if (flags & MSG_PEEK) {
                    918:                        if (paddr)
                    919:                                *paddr = m_copy(m, 0, m->m_len);
                    920:                        m = m->m_next;
                    921:                } else {
                    922:                        sbfree(&so->so_rcv, m);
1.54.2.11  nathanw   923:                        mbuf_removed = 1;
1.1       cgd       924:                        if (paddr) {
                    925:                                *paddr = m;
                    926:                                so->so_rcv.sb_mb = m->m_next;
                    927:                                m->m_next = 0;
                    928:                                m = so->so_rcv.sb_mb;
                    929:                        } else {
                    930:                                MFREE(m, so->so_rcv.sb_mb);
                    931:                                m = so->so_rcv.sb_mb;
                    932:                        }
                    933:                }
                    934:        }
                    935:        while (m && m->m_type == MT_CONTROL && error == 0) {
                    936:                if (flags & MSG_PEEK) {
                    937:                        if (controlp)
                    938:                                *controlp = m_copy(m, 0, m->m_len);
                    939:                        m = m->m_next;
                    940:                } else {
                    941:                        sbfree(&so->so_rcv, m);
1.54.2.11  nathanw   942:                        mbuf_removed = 1;
1.1       cgd       943:                        if (controlp) {
                    944:                                if (pr->pr_domain->dom_externalize &&
                    945:                                    mtod(m, struct cmsghdr *)->cmsg_type ==
                    946:                                    SCM_RIGHTS)
1.45      tv        947:                                        error = (*pr->pr_domain->dom_externalize)(m);
1.1       cgd       948:                                *controlp = m;
                    949:                                so->so_rcv.sb_mb = m->m_next;
                    950:                                m->m_next = 0;
                    951:                                m = so->so_rcv.sb_mb;
                    952:                        } else {
                    953:                                MFREE(m, so->so_rcv.sb_mb);
                    954:                                m = so->so_rcv.sb_mb;
                    955:                        }
                    956:                }
1.3       andrew    957:                if (controlp) {
                    958:                        orig_resid = 0;
1.1       cgd       959:                        controlp = &(*controlp)->m_next;
1.3       andrew    960:                }
1.1       cgd       961:        }
                    962:        if (m) {
                    963:                if ((flags & MSG_PEEK) == 0)
                    964:                        m->m_nextpkt = nextrecord;
                    965:                type = m->m_type;
                    966:                if (type == MT_OOBDATA)
                    967:                        flags |= MSG_OOB;
                    968:        }
                    969:        moff = 0;
                    970:        offset = 0;
                    971:        while (m && uio->uio_resid > 0 && error == 0) {
                    972:                if (m->m_type == MT_OOBDATA) {
                    973:                        if (type != MT_OOBDATA)
                    974:                                break;
                    975:                } else if (type == MT_OOBDATA)
                    976:                        break;
                    977: #ifdef DIAGNOSTIC
                    978:                else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
                    979:                        panic("receive 3");
                    980: #endif
                    981:                so->so_state &= ~SS_RCVATMARK;
                    982:                len = uio->uio_resid;
                    983:                if (so->so_oobmark && len > so->so_oobmark - offset)
                    984:                        len = so->so_oobmark - offset;
                    985:                if (len > m->m_len - moff)
                    986:                        len = m->m_len - moff;
                    987:                /*
                    988:                 * If mp is set, just pass back the mbufs.
                    989:                 * Otherwise copy them out via the uio, then free.
                    990:                 * Sockbuf must be consistent here (points to current mbuf,
                    991:                 * it points to next record) when we drop priority;
                    992:                 * we must note any additions to the sockbuf when we
                    993:                 * block interrupts again.
                    994:                 */
                    995:                if (mp == 0) {
                    996:                        splx(s);
                    997:                        error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
1.20      mycroft   998:                        s = splsoftnet();
1.54.2.11  nathanw   999:                        if (error) {
                   1000:                                /*
                   1001:                                 * If any part of the record has been removed
                   1002:                                 * (such as the MT_SONAME mbuf, which will
                   1003:                                 * happen when PR_ADDR, and thus also
                   1004:                                 * PR_ATOMIC, is set), then drop the entire
                   1005:                                 * record to maintain the atomicity of the
                   1006:                                 * receive operation.
                   1007:                                 *
                   1008:                                 * This avoids a later panic("receive 1a")
                   1009:                                 * when compiled with DIAGNOSTIC.
                   1010:                                 */
                   1011:                                if (m && mbuf_removed
                   1012:                                    && (pr->pr_flags & PR_ATOMIC))
                   1013:                                        (void) sbdroprecord(&so->so_rcv);
                   1014:
1.54.2.4  nathanw  1015:                                goto release;
1.54.2.11  nathanw  1016:                        }
1.1       cgd      1017:                } else
                   1018:                        uio->uio_resid -= len;
                   1019:                if (len == m->m_len - moff) {
                   1020:                        if (m->m_flags & M_EOR)
                   1021:                                flags |= MSG_EOR;
                   1022:                        if (flags & MSG_PEEK) {
                   1023:                                m = m->m_next;
                   1024:                                moff = 0;
                   1025:                        } else {
                   1026:                                nextrecord = m->m_nextpkt;
                   1027:                                sbfree(&so->so_rcv, m);
                   1028:                                if (mp) {
                   1029:                                        *mp = m;
                   1030:                                        mp = &m->m_next;
                   1031:                                        so->so_rcv.sb_mb = m = m->m_next;
                   1032:                                        *mp = (struct mbuf *)0;
                   1033:                                } else {
                   1034:                                        MFREE(m, so->so_rcv.sb_mb);
                   1035:                                        m = so->so_rcv.sb_mb;
                   1036:                                }
                   1037:                                if (m)
                   1038:                                        m->m_nextpkt = nextrecord;
                   1039:                        }
                   1040:                } else {
                   1041:                        if (flags & MSG_PEEK)
                   1042:                                moff += len;
                   1043:                        else {
                   1044:                                if (mp)
                   1045:                                        *mp = m_copym(m, 0, len, M_WAIT);
                   1046:                                m->m_data += len;
                   1047:                                m->m_len -= len;
                   1048:                                so->so_rcv.sb_cc -= len;
                   1049:                        }
                   1050:                }
                   1051:                if (so->so_oobmark) {
                   1052:                        if ((flags & MSG_PEEK) == 0) {
                   1053:                                so->so_oobmark -= len;
                   1054:                                if (so->so_oobmark == 0) {
                   1055:                                        so->so_state |= SS_RCVATMARK;
                   1056:                                        break;
                   1057:                                }
1.7       cgd      1058:                        } else {
1.1       cgd      1059:                                offset += len;
1.7       cgd      1060:                                if (offset == so->so_oobmark)
                   1061:                                        break;
                   1062:                        }
1.1       cgd      1063:                }
                   1064:                if (flags & MSG_EOR)
                   1065:                        break;
                   1066:                /*
                   1067:                 * If the MSG_WAITALL flag is set (for non-atomic socket),
                   1068:                 * we must not quit until "uio->uio_resid == 0" or an error
                   1069:                 * termination.  If a signal/timeout occurs, return
                   1070:                 * with a short count but without error.
                   1071:                 * Keep sockbuf locked against other readers.
                   1072:                 */
                   1073:                while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
1.3       andrew   1074:                    !sosendallatonce(so) && !nextrecord) {
1.1       cgd      1075:                        if (so->so_error || so->so_state & SS_CANTRCVMORE)
                   1076:                                break;
1.54.2.11  nathanw  1077:                        /*
                   1078:                         * If we are peeking and the socket receive buffer is
                   1079:                         * full, stop since we can't get more data to peek at.
                   1080:                         */
                   1081:                        if ((flags & MSG_PEEK) && sbspace(&so->so_rcv) <= 0)
                   1082:                                break;
                   1083:                        /*
                   1084:                         * If we've drained the socket buffer, tell the
                   1085:                         * protocol in case it needs to do something to
                   1086:                         * get it filled again.
                   1087:                         */
                   1088:                        if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
                   1089:                                (*pr->pr_usrreq)(so, PRU_RCVD,
                   1090:                                    (struct mbuf *)0,
                   1091:                                    (struct mbuf *)(long)flags,
                   1092:                                    (struct mbuf *)0,
                   1093:                                    (struct proc *)0);
1.1       cgd      1094:                        error = sbwait(&so->so_rcv);
                   1095:                        if (error) {
                   1096:                                sbunlock(&so->so_rcv);
                   1097:                                splx(s);
                   1098:                                return (0);
                   1099:                        }
1.21      christos 1100:                        if ((m = so->so_rcv.sb_mb) != NULL)
1.1       cgd      1101:                                nextrecord = m->m_nextpkt;
                   1102:                }
                   1103:        }
1.3       andrew   1104:
                   1105:        if (m && pr->pr_flags & PR_ATOMIC) {
                   1106:                flags |= MSG_TRUNC;
                   1107:                if ((flags & MSG_PEEK) == 0)
                   1108:                        (void) sbdroprecord(&so->so_rcv);
                   1109:        }
1.1       cgd      1110:        if ((flags & MSG_PEEK) == 0) {
                   1111:                if (m == 0)
                   1112:                        so->so_rcv.sb_mb = nextrecord;
                   1113:                if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.22      mycroft  1114:                        (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
                   1115:                            (struct mbuf *)(long)flags, (struct mbuf *)0,
                   1116:                            (struct proc *)0);
1.1       cgd      1117:        }
1.3       andrew   1118:        if (orig_resid == uio->uio_resid && orig_resid &&
                   1119:            (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
                   1120:                sbunlock(&so->so_rcv);
                   1121:                splx(s);
                   1122:                goto restart;
                   1123:        }
                   1124:
1.1       cgd      1125:        if (flagsp)
                   1126:                *flagsp |= flags;
1.54      lukem    1127:  release:
1.1       cgd      1128:        sbunlock(&so->so_rcv);
                   1129:        splx(s);
                   1130:        return (error);
                   1131: }
                   1132:
1.14      mycroft  1133: int
1.54      lukem    1134: soshutdown(struct socket *so, int how)
1.1       cgd      1135: {
1.54      lukem    1136:        struct protosw  *pr;
1.34      kleink   1137:
1.54      lukem    1138:        pr = so->so_proto;
1.34      kleink   1139:        if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
                   1140:                return (EINVAL);
1.1       cgd      1141:
1.34      kleink   1142:        if (how == SHUT_RD || how == SHUT_RDWR)
1.1       cgd      1143:                sorflush(so);
1.34      kleink   1144:        if (how == SHUT_WR || how == SHUT_RDWR)
1.22      mycroft  1145:                return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
                   1146:                    (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1       cgd      1147:        return (0);
                   1148: }
                   1149:
1.14      mycroft  1150: void
1.54      lukem    1151: sorflush(struct socket *so)
1.1       cgd      1152: {
1.54      lukem    1153:        struct sockbuf  *sb, asb;
                   1154:        struct protosw  *pr;
                   1155:        int             s;
1.1       cgd      1156:
1.54      lukem    1157:        sb = &so->so_rcv;
                   1158:        pr = so->so_proto;
1.1       cgd      1159:        sb->sb_flags |= SB_NOINTR;
1.15      mycroft  1160:        (void) sblock(sb, M_WAITOK);
1.54.2.3  nathanw  1161:        s = splnet();
1.1       cgd      1162:        socantrcvmore(so);
                   1163:        sbunlock(sb);
                   1164:        asb = *sb;
1.38      perry    1165:        memset((caddr_t)sb, 0, sizeof(*sb));
1.1       cgd      1166:        splx(s);
                   1167:        if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
                   1168:                (*pr->pr_domain->dom_dispose)(asb.sb_mb);
                   1169:        sbrelease(&asb);
                   1170: }
                   1171:
1.14      mycroft  1172: int
1.54      lukem    1173: sosetopt(struct socket *so, int level, int optname, struct mbuf *m0)
1.1       cgd      1174: {
1.54      lukem    1175:        int             error;
                   1176:        struct mbuf     *m;
1.1       cgd      1177:
1.54      lukem    1178:        error = 0;
                   1179:        m = m0;
1.1       cgd      1180:        if (level != SOL_SOCKET) {
                   1181:                if (so->so_proto && so->so_proto->pr_ctloutput)
                   1182:                        return ((*so->so_proto->pr_ctloutput)
                   1183:                                  (PRCO_SETOPT, so, level, optname, &m0));
                   1184:                error = ENOPROTOOPT;
                   1185:        } else {
                   1186:                switch (optname) {
                   1187:
                   1188:                case SO_LINGER:
1.36      perry    1189:                        if (m == NULL || m->m_len != sizeof(struct linger)) {
1.1       cgd      1190:                                error = EINVAL;
                   1191:                                goto bad;
                   1192:                        }
                   1193:                        so->so_linger = mtod(m, struct linger *)->l_linger;
                   1194:                        /* fall thru... */
                   1195:
                   1196:                case SO_DEBUG:
                   1197:                case SO_KEEPALIVE:
                   1198:                case SO_DONTROUTE:
                   1199:                case SO_USELOOPBACK:
                   1200:                case SO_BROADCAST:
                   1201:                case SO_REUSEADDR:
1.15      mycroft  1202:                case SO_REUSEPORT:
1.1       cgd      1203:                case SO_OOBINLINE:
1.26      thorpej  1204:                case SO_TIMESTAMP:
1.36      perry    1205:                        if (m == NULL || m->m_len < sizeof(int)) {
1.1       cgd      1206:                                error = EINVAL;
                   1207:                                goto bad;
                   1208:                        }
                   1209:                        if (*mtod(m, int *))
                   1210:                                so->so_options |= optname;
                   1211:                        else
                   1212:                                so->so_options &= ~optname;
                   1213:                        break;
                   1214:
                   1215:                case SO_SNDBUF:
                   1216:                case SO_RCVBUF:
                   1217:                case SO_SNDLOWAT:
                   1218:                case SO_RCVLOWAT:
1.28      thorpej  1219:                    {
                   1220:                        int optval;
                   1221:
1.36      perry    1222:                        if (m == NULL || m->m_len < sizeof(int)) {
1.1       cgd      1223:                                error = EINVAL;
                   1224:                                goto bad;
                   1225:                        }
1.28      thorpej  1226:
                   1227:                        /*
                   1228:                         * Values < 1 make no sense for any of these
                   1229:                         * options, so disallow them.
                   1230:                         */
                   1231:                        optval = *mtod(m, int *);
                   1232:                        if (optval < 1) {
                   1233:                                error = EINVAL;
                   1234:                                goto bad;
                   1235:                        }
                   1236:
1.1       cgd      1237:                        switch (optname) {
                   1238:
                   1239:                        case SO_SNDBUF:
                   1240:                        case SO_RCVBUF:
                   1241:                                if (sbreserve(optname == SO_SNDBUF ?
                   1242:                                    &so->so_snd : &so->so_rcv,
1.28      thorpej  1243:                                    (u_long) optval) == 0) {
1.1       cgd      1244:                                        error = ENOBUFS;
                   1245:                                        goto bad;
                   1246:                                }
                   1247:                                break;
                   1248:
1.28      thorpej  1249:                        /*
                   1250:                         * Make sure the low-water is never greater than
                   1251:                         * the high-water.
                   1252:                         */
1.1       cgd      1253:                        case SO_SNDLOWAT:
1.28      thorpej  1254:                                so->so_snd.sb_lowat =
                   1255:                                    (optval > so->so_snd.sb_hiwat) ?
                   1256:                                    so->so_snd.sb_hiwat : optval;
1.1       cgd      1257:                                break;
                   1258:                        case SO_RCVLOWAT:
1.28      thorpej  1259:                                so->so_rcv.sb_lowat =
                   1260:                                    (optval > so->so_rcv.sb_hiwat) ?
                   1261:                                    so->so_rcv.sb_hiwat : optval;
1.1       cgd      1262:                                break;
                   1263:                        }
                   1264:                        break;
1.28      thorpej  1265:                    }
1.1       cgd      1266:
                   1267:                case SO_SNDTIMEO:
                   1268:                case SO_RCVTIMEO:
                   1269:                    {
                   1270:                        struct timeval *tv;
                   1271:                        short val;
                   1272:
1.36      perry    1273:                        if (m == NULL || m->m_len < sizeof(*tv)) {
1.1       cgd      1274:                                error = EINVAL;
                   1275:                                goto bad;
                   1276:                        }
                   1277:                        tv = mtod(m, struct timeval *);
1.19      cgd      1278:                        if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) {
1.1       cgd      1279:                                error = EDOM;
                   1280:                                goto bad;
                   1281:                        }
                   1282:                        val = tv->tv_sec * hz + tv->tv_usec / tick;
                   1283:
                   1284:                        switch (optname) {
                   1285:
                   1286:                        case SO_SNDTIMEO:
                   1287:                                so->so_snd.sb_timeo = val;
                   1288:                                break;
                   1289:                        case SO_RCVTIMEO:
                   1290:                                so->so_rcv.sb_timeo = val;
                   1291:                                break;
                   1292:                        }
                   1293:                        break;
                   1294:                    }
                   1295:
                   1296:                default:
                   1297:                        error = ENOPROTOOPT;
                   1298:                        break;
                   1299:                }
1.15      mycroft  1300:                if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
                   1301:                        (void) ((*so->so_proto->pr_ctloutput)
                   1302:                                  (PRCO_SETOPT, so, level, optname, &m0));
                   1303:                        m = NULL;       /* freed by protocol */
                   1304:                }
1.1       cgd      1305:        }
1.54      lukem    1306:  bad:
1.1       cgd      1307:        if (m)
                   1308:                (void) m_free(m);
                   1309:        return (error);
                   1310: }
                   1311:
1.14      mycroft  1312: int
1.54      lukem    1313: sogetopt(struct socket *so, int level, int optname, struct mbuf **mp)
1.1       cgd      1314: {
1.54      lukem    1315:        struct mbuf     *m;
1.1       cgd      1316:
                   1317:        if (level != SOL_SOCKET) {
                   1318:                if (so->so_proto && so->so_proto->pr_ctloutput) {
                   1319:                        return ((*so->so_proto->pr_ctloutput)
                   1320:                                  (PRCO_GETOPT, so, level, optname, mp));
                   1321:                } else
                   1322:                        return (ENOPROTOOPT);
                   1323:        } else {
                   1324:                m = m_get(M_WAIT, MT_SOOPTS);
1.36      perry    1325:                m->m_len = sizeof(int);
1.1       cgd      1326:
                   1327:                switch (optname) {
                   1328:
                   1329:                case SO_LINGER:
1.36      perry    1330:                        m->m_len = sizeof(struct linger);
1.1       cgd      1331:                        mtod(m, struct linger *)->l_onoff =
                   1332:                                so->so_options & SO_LINGER;
                   1333:                        mtod(m, struct linger *)->l_linger = so->so_linger;
                   1334:                        break;
                   1335:
                   1336:                case SO_USELOOPBACK:
                   1337:                case SO_DONTROUTE:
                   1338:                case SO_DEBUG:
                   1339:                case SO_KEEPALIVE:
                   1340:                case SO_REUSEADDR:
1.15      mycroft  1341:                case SO_REUSEPORT:
1.1       cgd      1342:                case SO_BROADCAST:
                   1343:                case SO_OOBINLINE:
1.26      thorpej  1344:                case SO_TIMESTAMP:
1.1       cgd      1345:                        *mtod(m, int *) = so->so_options & optname;
                   1346:                        break;
                   1347:
                   1348:                case SO_TYPE:
                   1349:                        *mtod(m, int *) = so->so_type;
                   1350:                        break;
                   1351:
                   1352:                case SO_ERROR:
                   1353:                        *mtod(m, int *) = so->so_error;
                   1354:                        so->so_error = 0;
                   1355:                        break;
                   1356:
                   1357:                case SO_SNDBUF:
                   1358:                        *mtod(m, int *) = so->so_snd.sb_hiwat;
                   1359:                        break;
                   1360:
                   1361:                case SO_RCVBUF:
                   1362:                        *mtod(m, int *) = so->so_rcv.sb_hiwat;
                   1363:                        break;
                   1364:
                   1365:                case SO_SNDLOWAT:
                   1366:                        *mtod(m, int *) = so->so_snd.sb_lowat;
                   1367:                        break;
                   1368:
                   1369:                case SO_RCVLOWAT:
                   1370:                        *mtod(m, int *) = so->so_rcv.sb_lowat;
                   1371:                        break;
                   1372:
                   1373:                case SO_SNDTIMEO:
                   1374:                case SO_RCVTIMEO:
                   1375:                    {
                   1376:                        int val = (optname == SO_SNDTIMEO ?
                   1377:                             so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
                   1378:
                   1379:                        m->m_len = sizeof(struct timeval);
                   1380:                        mtod(m, struct timeval *)->tv_sec = val / hz;
                   1381:                        mtod(m, struct timeval *)->tv_usec =
1.27      kleink   1382:                            (val % hz) * tick;
1.1       cgd      1383:                        break;
                   1384:                    }
                   1385:
                   1386:                default:
                   1387:                        (void)m_free(m);
                   1388:                        return (ENOPROTOOPT);
                   1389:                }
                   1390:                *mp = m;
                   1391:                return (0);
                   1392:        }
                   1393: }
                   1394:
1.14      mycroft  1395: void
1.54      lukem    1396: sohasoutofband(struct socket *so)
1.1       cgd      1397: {
                   1398:        struct proc *p;
                   1399:
                   1400:        if (so->so_pgid < 0)
                   1401:                gsignal(-so->so_pgid, SIGURG);
                   1402:        else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
                   1403:                psignal(p, SIGURG);
1.2       cgd      1404:        selwakeup(&so->so_rcv.sb_sel);
1.1       cgd      1405: }

CVSweb <webmaster@jp.NetBSD.org>