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

1.54.2.3! nathanw     1: /*     $NetBSD: uipc_socket.c,v 1.54.2.2 2001/04/09 01:57:58 nathanw Exp $     */
1.16      cgd         2:
1.1       cgd         3: /*
1.15      mycroft     4:  * Copyright (c) 1982, 1986, 1988, 1990, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  *
1.32      fvdl       35:  *     @(#)uipc_socket.c       8.6 (Berkeley) 5/2/95
1.1       cgd        36:  */
1.35      thorpej    37:
                     38: #include "opt_compat_sunos.h"
1.1       cgd        39:
1.9       mycroft    40: #include <sys/param.h>
                     41: #include <sys/systm.h>
1.54.2.1  nathanw    42: #include <sys/lwp.h>
1.9       mycroft    43: #include <sys/proc.h>
                     44: #include <sys/file.h>
                     45: #include <sys/malloc.h>
                     46: #include <sys/mbuf.h>
                     47: #include <sys/domain.h>
                     48: #include <sys/kernel.h>
                     49: #include <sys/protosw.h>
                     50: #include <sys/socket.h>
                     51: #include <sys/socketvar.h>
1.21      christos   52: #include <sys/signalvar.h>
1.9       mycroft    53: #include <sys/resourcevar.h>
1.37      thorpej    54: #include <sys/pool.h>
                     55:
1.54      lukem      56: struct pool    socket_pool;
1.37      thorpej    57:
1.54      lukem      58: extern int     somaxconn;                      /* patchable (XXX sysctl) */
                     59: int            somaxconn = SOMAXCONN;
1.49      jonathan   60:
1.37      thorpej    61: void
1.54      lukem      62: soinit(void)
1.37      thorpej    63: {
                     64:
                     65:        pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0,
                     66:            "sockpl", 0, NULL, NULL, M_SOCKET);
                     67: }
1.1       cgd        68:
                     69: /*
                     70:  * Socket operation routines.
                     71:  * These routines are called by the routines in
                     72:  * sys_socket.c or from a system process, and
                     73:  * implement the semantics of socket operations by
                     74:  * switching out to the protocol specific routines.
                     75:  */
                     76: /*ARGSUSED*/
1.3       andrew     77: int
1.54      lukem      78: socreate(int dom, struct socket **aso, int type, int proto)
1.1       cgd        79: {
1.54      lukem      80:        struct proc     *p;
                     81:        struct protosw  *prp;
                     82:        struct socket   *so;
                     83:        int             error, s;
1.1       cgd        84:
1.54.2.1  nathanw    85:        p = curproc->l_proc;    /* XXX */
1.1       cgd        86:        if (proto)
                     87:                prp = pffindproto(dom, proto, type);
                     88:        else
                     89:                prp = pffindtype(dom, type);
1.15      mycroft    90:        if (prp == 0 || prp->pr_usrreq == 0)
1.1       cgd        91:                return (EPROTONOSUPPORT);
                     92:        if (prp->pr_type != type)
                     93:                return (EPROTOTYPE);
1.39      matt       94:        s = splsoftnet();
1.37      thorpej    95:        so = pool_get(&socket_pool, PR_WAITOK);
1.38      perry      96:        memset((caddr_t)so, 0, sizeof(*so));
1.31      thorpej    97:        TAILQ_INIT(&so->so_q0);
                     98:        TAILQ_INIT(&so->so_q);
1.1       cgd        99:        so->so_type = type;
                    100:        so->so_proto = prp;
1.33      matt      101:        so->so_send = sosend;
                    102:        so->so_receive = soreceive;
1.44      lukem     103:        if (p != 0)
                    104:                so->so_uid = p->p_ucred->cr_uid;
1.22      mycroft   105:        error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
                    106:            (struct mbuf *)(long)proto, (struct mbuf *)0, p);
1.1       cgd       107:        if (error) {
                    108:                so->so_state |= SS_NOFDREF;
                    109:                sofree(so);
1.39      matt      110:                splx(s);
1.1       cgd       111:                return (error);
                    112:        }
1.10      deraadt   113: #ifdef COMPAT_SUNOS
1.18      christos  114:        {
                    115:                extern struct emul emul_sunos;
                    116:                if (p->p_emul == &emul_sunos && type == SOCK_DGRAM)
                    117:                        so->so_options |= SO_BROADCAST;
                    118:        }
1.10      deraadt   119: #endif
1.39      matt      120:        splx(s);
1.1       cgd       121:        *aso = so;
                    122:        return (0);
                    123: }
                    124:
1.3       andrew    125: int
1.54      lukem     126: sobind(struct socket *so, struct mbuf *nam, struct proc *p)
1.1       cgd       127: {
1.54      lukem     128:        int     s, error;
1.1       cgd       129:
1.54      lukem     130:        s = splsoftnet();
1.22      mycroft   131:        error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0,
                    132:            nam, (struct mbuf *)0, p);
1.1       cgd       133:        splx(s);
                    134:        return (error);
                    135: }
                    136:
1.3       andrew    137: int
1.54      lukem     138: solisten(struct socket *so, int backlog)
1.1       cgd       139: {
1.54      lukem     140:        int     s, error;
1.1       cgd       141:
1.54      lukem     142:        s = splsoftnet();
1.22      mycroft   143:        error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0,
                    144:            (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1       cgd       145:        if (error) {
                    146:                splx(s);
                    147:                return (error);
                    148:        }
1.31      thorpej   149:        if (so->so_q.tqh_first == NULL)
1.1       cgd       150:                so->so_options |= SO_ACCEPTCONN;
                    151:        if (backlog < 0)
                    152:                backlog = 0;
1.49      jonathan  153:        so->so_qlimit = min(backlog, somaxconn);
1.1       cgd       154:        splx(s);
                    155:        return (0);
                    156: }
                    157:
1.21      christos  158: void
1.54      lukem     159: sofree(struct socket *so)
1.1       cgd       160: {
                    161:
1.43      mycroft   162:        if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
1.1       cgd       163:                return;
1.43      mycroft   164:        if (so->so_head) {
                    165:                /*
                    166:                 * We must not decommission a socket that's on the accept(2)
                    167:                 * queue.  If we do, then accept(2) may hang after select(2)
                    168:                 * indicated that the listening socket was ready.
                    169:                 */
                    170:                if (!soqremque(so, 0))
                    171:                        return;
                    172:        }
1.1       cgd       173:        sbrelease(&so->so_snd);
                    174:        sorflush(so);
1.37      thorpej   175:        pool_put(&socket_pool, so);
1.1       cgd       176: }
                    177:
                    178: /*
                    179:  * Close a socket on last file table reference removal.
                    180:  * Initiate disconnect if connected.
                    181:  * Free socket when disconnect complete.
                    182:  */
1.3       andrew    183: int
1.54      lukem     184: soclose(struct socket *so)
1.1       cgd       185: {
1.54      lukem     186:        struct socket   *so2;
                    187:        int             s, error;
1.1       cgd       188:
1.54      lukem     189:        error = 0;
                    190:        s = splsoftnet();               /* conservative */
1.1       cgd       191:        if (so->so_options & SO_ACCEPTCONN) {
1.41      mycroft   192:                while ((so2 = so->so_q0.tqh_first) != 0) {
1.42      mycroft   193:                        (void) soqremque(so2, 0);
1.41      mycroft   194:                        (void) soabort(so2);
                    195:                }
                    196:                while ((so2 = so->so_q.tqh_first) != 0) {
1.42      mycroft   197:                        (void) soqremque(so2, 1);
1.41      mycroft   198:                        (void) soabort(so2);
                    199:                }
1.1       cgd       200:        }
                    201:        if (so->so_pcb == 0)
                    202:                goto discard;
                    203:        if (so->so_state & SS_ISCONNECTED) {
                    204:                if ((so->so_state & SS_ISDISCONNECTING) == 0) {
                    205:                        error = sodisconnect(so);
                    206:                        if (error)
                    207:                                goto drop;
                    208:                }
                    209:                if (so->so_options & SO_LINGER) {
                    210:                        if ((so->so_state & SS_ISDISCONNECTING) &&
                    211:                            (so->so_state & SS_NBIO))
                    212:                                goto drop;
1.21      christos  213:                        while (so->so_state & SS_ISCONNECTED) {
                    214:                                error = tsleep((caddr_t)&so->so_timeo,
                    215:                                               PSOCK | PCATCH, netcls,
1.30      thorpej   216:                                               so->so_linger * hz);
1.21      christos  217:                                if (error)
1.1       cgd       218:                                        break;
1.21      christos  219:                        }
1.1       cgd       220:                }
                    221:        }
1.54      lukem     222:  drop:
1.1       cgd       223:        if (so->so_pcb) {
1.22      mycroft   224:                int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
                    225:                    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
                    226:                    (struct proc *)0);
1.1       cgd       227:                if (error == 0)
                    228:                        error = error2;
                    229:        }
1.54      lukem     230:  discard:
1.1       cgd       231:        if (so->so_state & SS_NOFDREF)
                    232:                panic("soclose: NOFDREF");
                    233:        so->so_state |= SS_NOFDREF;
                    234:        sofree(so);
                    235:        splx(s);
                    236:        return (error);
                    237: }
                    238:
                    239: /*
1.20      mycroft   240:  * Must be called at splsoftnet...
1.1       cgd       241:  */
1.3       andrew    242: int
1.54      lukem     243: soabort(struct socket *so)
1.1       cgd       244: {
                    245:
1.22      mycroft   246:        return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0,
                    247:            (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1       cgd       248: }
                    249:
1.3       andrew    250: int
1.54      lukem     251: soaccept(struct socket *so, struct mbuf *nam)
1.1       cgd       252: {
1.54      lukem     253:        int     s, error;
1.1       cgd       254:
1.54      lukem     255:        error = 0;
                    256:        s = splsoftnet();
1.1       cgd       257:        if ((so->so_state & SS_NOFDREF) == 0)
                    258:                panic("soaccept: !NOFDREF");
                    259:        so->so_state &= ~SS_NOFDREF;
1.54.2.2  nathanw   260:        if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
                    261:            (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
1.41      mycroft   262:                error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
                    263:                    (struct mbuf *)0, nam, (struct mbuf *)0, (struct proc *)0);
                    264:        else
1.53      itojun    265:                error = ECONNABORTED;
1.52      itojun    266:
1.1       cgd       267:        splx(s);
                    268:        return (error);
                    269: }
                    270:
1.3       andrew    271: int
1.54      lukem     272: soconnect(struct socket *so, struct mbuf *nam)
1.1       cgd       273: {
1.54      lukem     274:        struct proc     *p;
                    275:        int             s, error;
1.1       cgd       276:
1.54.2.1  nathanw   277:        p = curproc->l_proc;            /* XXX */
1.1       cgd       278:        if (so->so_options & SO_ACCEPTCONN)
                    279:                return (EOPNOTSUPP);
1.20      mycroft   280:        s = splsoftnet();
1.1       cgd       281:        /*
                    282:         * If protocol is connection-based, can only connect once.
                    283:         * Otherwise, if connected, try to disconnect first.
                    284:         * This allows user to disconnect by connecting to, e.g.,
                    285:         * a null address.
                    286:         */
                    287:        if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
                    288:            ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
                    289:            (error = sodisconnect(so))))
                    290:                error = EISCONN;
                    291:        else
                    292:                error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
1.23      mycroft   293:                    (struct mbuf *)0, nam, (struct mbuf *)0, p);
1.1       cgd       294:        splx(s);
                    295:        return (error);
                    296: }
                    297:
1.3       andrew    298: int
1.54      lukem     299: soconnect2(struct socket *so1, struct socket *so2)
1.1       cgd       300: {
1.54      lukem     301:        int     s, error;
1.1       cgd       302:
1.54      lukem     303:        s = splsoftnet();
1.22      mycroft   304:        error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
                    305:            (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0,
                    306:            (struct proc *)0);
1.1       cgd       307:        splx(s);
                    308:        return (error);
                    309: }
                    310:
1.3       andrew    311: int
1.54      lukem     312: sodisconnect(struct socket *so)
1.1       cgd       313: {
1.54      lukem     314:        int     s, error;
1.1       cgd       315:
1.54      lukem     316:        s = splsoftnet();
1.1       cgd       317:        if ((so->so_state & SS_ISCONNECTED) == 0) {
                    318:                error = ENOTCONN;
                    319:                goto bad;
                    320:        }
                    321:        if (so->so_state & SS_ISDISCONNECTING) {
                    322:                error = EALREADY;
                    323:                goto bad;
                    324:        }
1.22      mycroft   325:        error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
                    326:            (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
                    327:            (struct proc *)0);
1.54      lukem     328:  bad:
1.1       cgd       329:        splx(s);
                    330:        return (error);
                    331: }
                    332:
1.15      mycroft   333: #define        SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
1.1       cgd       334: /*
                    335:  * Send on a socket.
                    336:  * If send must go all at once and message is larger than
                    337:  * send buffering, then hard error.
                    338:  * Lock against other senders.
                    339:  * If must go all at once and not enough room now, then
                    340:  * inform user that this would block and do nothing.
                    341:  * Otherwise, if nonblocking, send as much as possible.
                    342:  * The data to be sent is described by "uio" if nonzero,
                    343:  * otherwise by the mbuf chain "top" (which must be null
                    344:  * if uio is not).  Data provided in mbuf chain must be small
                    345:  * enough to send all at once.
                    346:  *
                    347:  * Returns nonzero on error, timeout or signal; callers
                    348:  * must check for short counts if EINTR/ERESTART are returned.
                    349:  * Data and control buffers are freed on return.
                    350:  */
1.3       andrew    351: int
1.54      lukem     352: sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
                    353:        struct mbuf *control, int flags)
1.1       cgd       354: {
1.54      lukem     355:        struct proc     *p;
                    356:        struct mbuf     **mp, *m;
                    357:        long            space, len, resid;
                    358:        int             clen, error, s, dontroute, mlen, atomic;
                    359:
1.54.2.1  nathanw   360:        p = curproc->l_proc;            /* XXX */
1.54      lukem     361:        clen = 0;
                    362:        atomic = sosendallatonce(so) || top;
1.1       cgd       363:        if (uio)
                    364:                resid = uio->uio_resid;
                    365:        else
                    366:                resid = top->m_pkthdr.len;
1.7       cgd       367:        /*
                    368:         * In theory resid should be unsigned.
                    369:         * However, space must be signed, as it might be less than 0
                    370:         * if we over-committed, and we must use a signed comparison
                    371:         * of space and resid.  On the other hand, a negative resid
                    372:         * causes us to loop sending 0-length segments to the protocol.
                    373:         */
1.29      mycroft   374:        if (resid < 0) {
                    375:                error = EINVAL;
                    376:                goto out;
                    377:        }
1.1       cgd       378:        dontroute =
                    379:            (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
                    380:            (so->so_proto->pr_flags & PR_ATOMIC);
1.12      mycroft   381:        p->p_stats->p_ru.ru_msgsnd++;
1.1       cgd       382:        if (control)
                    383:                clen = control->m_len;
                    384: #define        snderr(errno)   { error = errno; splx(s); goto release; }
                    385:
1.54      lukem     386:  restart:
1.21      christos  387:        if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
1.1       cgd       388:                goto out;
                    389:        do {
1.20      mycroft   390:                s = splsoftnet();
1.1       cgd       391:                if (so->so_state & SS_CANTSENDMORE)
                    392:                        snderr(EPIPE);
1.48      thorpej   393:                if (so->so_error) {
                    394:                        error = so->so_error;
                    395:                        so->so_error = 0;
                    396:                        splx(s);
                    397:                        goto release;
                    398:                }
1.1       cgd       399:                if ((so->so_state & SS_ISCONNECTED) == 0) {
                    400:                        if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
                    401:                                if ((so->so_state & SS_ISCONFIRMING) == 0 &&
                    402:                                    !(resid == 0 && clen != 0))
                    403:                                        snderr(ENOTCONN);
                    404:                        } else if (addr == 0)
                    405:                                snderr(EDESTADDRREQ);
                    406:                }
                    407:                space = sbspace(&so->so_snd);
                    408:                if (flags & MSG_OOB)
                    409:                        space += 1024;
1.21      christos  410:                if ((atomic && resid > so->so_snd.sb_hiwat) ||
1.11      mycroft   411:                    clen > so->so_snd.sb_hiwat)
                    412:                        snderr(EMSGSIZE);
                    413:                if (space < resid + clen && uio &&
1.1       cgd       414:                    (atomic || space < so->so_snd.sb_lowat || space < clen)) {
                    415:                        if (so->so_state & SS_NBIO)
                    416:                                snderr(EWOULDBLOCK);
                    417:                        sbunlock(&so->so_snd);
                    418:                        error = sbwait(&so->so_snd);
                    419:                        splx(s);
                    420:                        if (error)
                    421:                                goto out;
                    422:                        goto restart;
                    423:                }
                    424:                splx(s);
                    425:                mp = &top;
                    426:                space -= clen;
                    427:                do {
1.45      tv        428:                        if (uio == NULL) {
                    429:                                /*
                    430:                                 * Data is prepackaged in "top".
                    431:                                 */
                    432:                                resid = 0;
                    433:                                if (flags & MSG_EOR)
                    434:                                        top->m_flags |= M_EOR;
                    435:                        } else do {
                    436:                                if (top == 0) {
                    437:                                        MGETHDR(m, M_WAIT, MT_DATA);
                    438:                                        mlen = MHLEN;
                    439:                                        m->m_pkthdr.len = 0;
                    440:                                        m->m_pkthdr.rcvif = (struct ifnet *)0;
                    441:                                } else {
                    442:                                        MGET(m, M_WAIT, MT_DATA);
                    443:                                        mlen = MLEN;
                    444:                                }
                    445:                                if (resid >= MINCLSIZE && space >= MCLBYTES) {
                    446:                                        MCLGET(m, M_WAIT);
                    447:                                        if ((m->m_flags & M_EXT) == 0)
                    448:                                                goto nopages;
                    449:                                        mlen = MCLBYTES;
1.15      mycroft   450: #ifdef MAPPED_MBUFS
1.45      tv        451:                                        len = min(MCLBYTES, resid);
1.15      mycroft   452: #else
1.45      tv        453:                                        if (atomic && top == 0) {
1.54      lukem     454:                                                len = min(MCLBYTES - max_hdr,
                    455:                                                    resid);
1.45      tv        456:                                                m->m_data += max_hdr;
                    457:                                        } else
                    458:                                                len = min(MCLBYTES, resid);
1.15      mycroft   459: #endif
1.45      tv        460:                                        space -= len;
                    461:                                } else {
1.1       cgd       462: nopages:
1.45      tv        463:                                        len = min(min(mlen, resid), space);
                    464:                                        space -= len;
                    465:                                        /*
                    466:                                         * For datagram protocols, leave room
                    467:                                         * for protocol headers in first mbuf.
                    468:                                         */
                    469:                                        if (atomic && top == 0 && len < mlen)
                    470:                                                MH_ALIGN(m, len);
                    471:                                }
1.54      lukem     472:                                error = uiomove(mtod(m, caddr_t), (int)len,
                    473:                                    uio);
1.45      tv        474:                                resid = uio->uio_resid;
                    475:                                m->m_len = len;
                    476:                                *mp = m;
                    477:                                top->m_pkthdr.len += len;
                    478:                                if (error)
                    479:                                        goto release;
                    480:                                mp = &m->m_next;
                    481:                                if (resid <= 0) {
                    482:                                        if (flags & MSG_EOR)
                    483:                                                top->m_flags |= M_EOR;
                    484:                                        break;
                    485:                                }
                    486:                        } while (space > 0 && atomic);
1.46      sommerfe  487:
                    488:                        s = splsoftnet();
                    489:
                    490:                        if (so->so_state & SS_CANTSENDMORE)
                    491:                                snderr(EPIPE);
1.45      tv        492:
                    493:                        if (dontroute)
                    494:                                so->so_options |= SO_DONTROUTE;
                    495:                        if (resid > 0)
                    496:                                so->so_state |= SS_MORETOCOME;
1.46      sommerfe  497:                        error = (*so->so_proto->pr_usrreq)(so,
                    498:                            (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
                    499:                            top, addr, control, p);
1.45      tv        500:                        if (dontroute)
                    501:                                so->so_options &= ~SO_DONTROUTE;
                    502:                        if (resid > 0)
                    503:                                so->so_state &= ~SS_MORETOCOME;
1.46      sommerfe  504:                        splx(s);
                    505:
1.45      tv        506:                        clen = 0;
                    507:                        control = 0;
                    508:                        top = 0;
                    509:                        mp = &top;
1.1       cgd       510:                        if (error)
                    511:                                goto release;
                    512:                } while (resid && space > 0);
                    513:        } while (resid);
                    514:
1.54      lukem     515:  release:
1.1       cgd       516:        sbunlock(&so->so_snd);
1.54      lukem     517:  out:
1.1       cgd       518:        if (top)
                    519:                m_freem(top);
                    520:        if (control)
                    521:                m_freem(control);
                    522:        return (error);
                    523: }
                    524:
                    525: /*
                    526:  * Implement receive operations on a socket.
                    527:  * We depend on the way that records are added to the sockbuf
                    528:  * by sbappend*.  In particular, each record (mbufs linked through m_next)
                    529:  * must begin with an address if the protocol so specifies,
                    530:  * followed by an optional mbuf or mbufs containing ancillary data,
                    531:  * and then zero or more mbufs of data.
                    532:  * In order to avoid blocking network interrupts for the entire time here,
                    533:  * we splx() while doing the actual copy to user space.
                    534:  * Although the sockbuf is locked, new data may still be appended,
                    535:  * and thus we must maintain consistency of the sockbuf during that time.
                    536:  *
                    537:  * The caller may receive the data as a single mbuf chain by supplying
                    538:  * an mbuf **mp0 for use in returning the chain.  The uio is then used
                    539:  * only for the count in uio_resid.
                    540:  */
1.3       andrew    541: int
1.54      lukem     542: soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
                    543:        struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1.1       cgd       544: {
1.54      lukem     545:        struct mbuf     *m, **mp;
                    546:        int             flags, len, error, s, offset, moff, type, orig_resid;
                    547:        struct protosw  *pr;
                    548:        struct mbuf     *nextrecord;
1.1       cgd       549:
1.54      lukem     550:        pr = so->so_proto;
1.1       cgd       551:        mp = mp0;
1.54      lukem     552:        type = 0;
                    553:        orig_resid = uio->uio_resid;
1.1       cgd       554:        if (paddr)
                    555:                *paddr = 0;
                    556:        if (controlp)
                    557:                *controlp = 0;
                    558:        if (flagsp)
                    559:                flags = *flagsp &~ MSG_EOR;
                    560:        else
                    561:                flags = 0;
                    562:        if (flags & MSG_OOB) {
                    563:                m = m_get(M_WAIT, MT_DATA);
1.17      cgd       564:                error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
1.22      mycroft   565:                    (struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0,
                    566:                    (struct proc *)0);
1.1       cgd       567:                if (error)
                    568:                        goto bad;
                    569:                do {
                    570:                        error = uiomove(mtod(m, caddr_t),
                    571:                            (int) min(uio->uio_resid, m->m_len), uio);
                    572:                        m = m_free(m);
                    573:                } while (uio->uio_resid && error == 0 && m);
1.54      lukem     574:  bad:
1.1       cgd       575:                if (m)
                    576:                        m_freem(m);
                    577:                return (error);
                    578:        }
                    579:        if (mp)
                    580:                *mp = (struct mbuf *)0;
                    581:        if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
1.22      mycroft   582:                (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
                    583:                    (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1       cgd       584:
1.54      lukem     585:  restart:
1.21      christos  586:        if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
1.1       cgd       587:                return (error);
1.20      mycroft   588:        s = splsoftnet();
1.1       cgd       589:
                    590:        m = so->so_rcv.sb_mb;
                    591:        /*
                    592:         * If we have less data than requested, block awaiting more
                    593:         * (subject to any timeout) if:
1.15      mycroft   594:         *   1. the current count is less than the low water mark,
1.1       cgd       595:         *   2. MSG_WAITALL is set, and it is possible to do the entire
1.15      mycroft   596:         *      receive operation at once if we block (resid <= hiwat), or
                    597:         *   3. MSG_DONTWAIT is not set.
1.1       cgd       598:         * If MSG_WAITALL is set but resid is larger than the receive buffer,
                    599:         * we have to do the receive in sections, and thus risk returning
                    600:         * a short count if a timeout or signal occurs after we start.
                    601:         */
1.21      christos  602:        if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
1.15      mycroft   603:            so->so_rcv.sb_cc < uio->uio_resid) &&
1.1       cgd       604:            (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
                    605:            ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1.21      christos  606:            m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1.1       cgd       607: #ifdef DIAGNOSTIC
                    608:                if (m == 0 && so->so_rcv.sb_cc)
                    609:                        panic("receive 1");
                    610: #endif
                    611:                if (so->so_error) {
                    612:                        if (m)
1.15      mycroft   613:                                goto dontblock;
1.1       cgd       614:                        error = so->so_error;
                    615:                        if ((flags & MSG_PEEK) == 0)
                    616:                                so->so_error = 0;
                    617:                        goto release;
                    618:                }
                    619:                if (so->so_state & SS_CANTRCVMORE) {
                    620:                        if (m)
1.15      mycroft   621:                                goto dontblock;
1.1       cgd       622:                        else
                    623:                                goto release;
                    624:                }
                    625:                for (; m; m = m->m_next)
                    626:                        if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
                    627:                                m = so->so_rcv.sb_mb;
                    628:                                goto dontblock;
                    629:                        }
                    630:                if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
                    631:                    (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
                    632:                        error = ENOTCONN;
                    633:                        goto release;
                    634:                }
                    635:                if (uio->uio_resid == 0)
                    636:                        goto release;
1.15      mycroft   637:                if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
1.1       cgd       638:                        error = EWOULDBLOCK;
                    639:                        goto release;
                    640:                }
                    641:                sbunlock(&so->so_rcv);
                    642:                error = sbwait(&so->so_rcv);
                    643:                splx(s);
                    644:                if (error)
                    645:                        return (error);
                    646:                goto restart;
                    647: &nb