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

1.54.2.5! nathanw     1: /*     $NetBSD: uipc_socket.c,v 1.54.2.4 2001/09/21 22:36:27 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;
1.54.2.5! nathanw   357:        long            space, len, resid, clen, mlen;
        !           358:        int             error, s, dontroute, atomic;
1.54      lukem     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.54.2.5! nathanw   451:                                        len = lmin(MCLBYTES, resid);
1.15      mycroft   452: #else
1.45      tv        453:                                        if (atomic && top == 0) {
1.54.2.5! nathanw   454:                                                len = lmin(MCLBYTES - max_hdr,
1.54      lukem     455:                                                    resid);
1.45      tv        456:                                                m->m_data += max_hdr;
                    457:                                        } else
1.54.2.5! nathanw   458:                                                len = lmin(MCLBYTES, resid);
1.15      mycroft   459: #endif
1.45      tv        460:                                        space -= len;
                    461:                                } else {
1.1       cgd       462: nopages:
1.54.2.5! nathanw   463:                                        len = lmin(lmin(mlen, resid), space);
1.45      tv        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:        }
1.54      lukem     648:  dontblock:
1.15      mycroft   649: #ifdef notyet /* XXXX */
                    650:        if (uio->uio_procp)
                    651:                uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
                    652: #endif
1.1       cgd       653:        nextrecord = m->m_nextpkt;
                    654:        if (pr->pr_flags & PR_ADDR) {
                    655: #ifdef DIAGNOSTIC
                    656:                if (m->m_type != MT_SONAME)
                    657:                        panic("receive 1a");
                    658: #endif
1.3       andrew    659:                orig_resid = 0;
1.1       cgd       660:                if (flags & MSG_PEEK) {
                    661:                        if (paddr)
                    662:                                *paddr = m_copy(m, 0, m->m_len);
                    663:                        m = m->m_next;
                    664:                } else {
                    665:                        sbfree(&so->so_rcv, m);
                    666:                        if (paddr) {
                    667:                                *paddr = m;
                    668:                                so->so_rcv.sb_mb = m->m_next;
                    669:                                m->m_next = 0;
                    670:                                m = so->so_rcv.sb_mb;
                    671:                        } else {
                    672:                                MFREE(m, so->so_rcv.sb_mb);
                    673:                                m = so->so_rcv.sb_mb;
                    674:                        }
                    675:                }
                    676:        }
                    677:        while (m && m->m_type == MT_CONTROL && error == 0) {
                    678:                if (flags & MSG_PEEK) {
                    679:                        if (controlp)
                    680:                                *controlp = m_copy(m, 0, m->m_len);
                    681:                        m = m->m_next;
                    682:                } else {
                    683:                        sbfree(&so->so_rcv, m);
                    684:                        if (controlp) {
                    685:                                if (pr->pr_domain->dom_externalize &&
                    686:                                    mtod(m, struct cmsghdr *)->cmsg_type ==
                    687:                                    SCM_RIGHTS)
1.45      tv        688:                                        error = (*pr->pr_domain->dom_externalize)(m);
1.1       cgd       689:                                *controlp = m;
                    690:                                so->so_rcv.sb_mb = m->m_next;
                    691:                                m->m_next = 0;
                    692:                                m = so->so_rcv.sb_mb;
                    693:                        } else {
                    694:                                MFREE(m, so->so_rcv.sb_mb);
                    695:                                m = so->so_rcv.sb_mb;
                    696:                        }
                    697:                }
1.3       andrew    698:                if (controlp) {
                    699:                        orig_resid = 0;
1.1       cgd       700:                        controlp = &(*controlp)->m_next;
1.3       andrew    701:                }
1.1       cgd       702:        }
                    703:        if (m) {
                    704:                if ((flags & MSG_PEEK) == 0)
                    705:                        m->m_nextpkt = nextrecord;
                    706:                type = m->m_type;
                    707:                if (type == MT_OOBDATA)
                    708:                        flags |= MSG_OOB;
                    709:        }
                    710:        moff = 0;
                    711:        offset = 0;
                    712:        while (m && uio->uio_resid > 0 && error == 0) {
                    713:                if (m->m_type == MT_OOBDATA) {
                    714:                        if (type != MT_OOBDATA)
                    715:                                break;
                    716:                } else if (type == MT_OOBDATA)
                    717:                        break;
                    718: #ifdef DIAGNOSTIC
                    719:                else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
                    720:                        panic("receive 3");
                    721: #endif
                    722:                so->so_state &= ~SS_RCVATMARK;
                    723:                len = uio->uio_resid;
                    724:                if (so->so_oobmark && len > so->so_oobmark - offset)
                    725:                        len = so->so_oobmark - offset;
                    726:                if (len > m->m_len - moff)
                    727:                        len = m->m_len - moff;
                    728:                /*
                    729:                 * If mp is set, just pass back the mbufs.
                    730:                 * Otherwise copy them out via the uio, then free.
                    731:                 * Sockbuf must be consistent here (points to current mbuf,
                    732:                 * it points to next record) when we drop priority;
                    733:                 * we must note any additions to the sockbuf when we
                    734:                 * block interrupts again.
                    735:                 */
                    736:                if (mp == 0) {
                    737:                        splx(s);
                    738:                        error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
1.20      mycroft   739:                        s = splsoftnet();
1.54.2.4  nathanw   740:                        if (error)
                    741:                                goto release;
1.1       cgd       742:                } else
                    743:                        uio->uio_resid -= len;
                    744:                if (len == m->m_len - moff) {
                    745:                        if (m->m_flags & M_EOR)
                    746:                                flags |= MSG_EOR;
                    747:                        if (flags & MSG_PEEK) {
                    748:                                m = m->m_next;
                    749:                                moff = 0;
                    750:                        } else {
                    751:                                nextrecord = m->m_nextpkt;
                    752:                                sbfree(&so->so_rcv, m);
                    753:                                if (mp) {
                    754:                                        *mp = m;
                    755:                                        mp = &m->m_next;
                    756:                                        so->so_rcv.sb_mb = m = m->m_next;
                    757:                                        *mp = (struct mbuf *)0;
                    758:                                } else {
                    759:                                        MFREE(m, so->so_rcv.sb_mb);
                    760:                                        m = so->so_rcv.sb_mb;
                    761:                                }
                    762:                                if (m)
                    763:                                        m->m_nextpkt = nextrecord;
                    764:                        }
                    765:                } else {
                    766:                        if (flags & MSG_PEEK)
                    767:                                moff += len;
                    768:                        else {
                    769:                                if (mp)
                    770:                                        *mp = m_copym(m, 0, len, M_WAIT);
                    771:                                m->m_data += len;
                    772:                                m->m_len -= len;
                    773:                                so->so_rcv.sb_cc -= len;
                    774:                        }
                    775:                }
                    776:                if (so->so_oobmark) {
                    777:                        if ((flags & MSG_PEEK) == 0) {
                    778:                                so->so_oobmark -= len;
                    779:                                if (so->so_oobmark == 0) {
                    780:                                        so->so_state |= SS_RCVATMARK;
                    781:                                        break;
                    782:                                }
1.7       cgd       783:                        } else {
1.1       cgd       784:                                offset += len;
1.7       cgd       785:                                if (offset == so->so_oobmark)
                    786:                                        break;
                    787:                        }
1.1       cgd       788:                }
                    789:                if (flags & MSG_EOR)
                    790:                        break;
                    791:                /*
                    792:                 * If the MSG_WAITALL flag is set (for non-atomic socket),
                    793:                 * we must not quit until "uio->uio_resid == 0" or an error
                    794:                 * termination.  If a signal/timeout occurs, return
                    795:                 * with a short count but without error.
                    796:                 * Keep sockbuf locked against other readers.
                    797:                 */
                    798:                while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
1.3       andrew    799:                    !sosendallatonce(so) && !nextrecord) {
1.1       cgd       800:                        if (so->so_error || so->so_state & SS_CANTRCVMORE)
                    801:                                break;
                    802:                        error = sbwait(&so->so_rcv);
                    803:                        if (error) {
                    804:                                sbunlock(&so->so_rcv);
                    805:                                splx(s);
                    806:                                return (0);
                    807:                        }
1.21      christos  808:                        if ((m = so->so_rcv.sb_mb) != NULL)
1.1       cgd       809:                                nextrecord = m->m_nextpkt;
                    810:                }
                    811:        }
1.3       andrew    812:
                    813:        if (m && pr->pr_flags & PR_ATOMIC) {
                    814:                flags |= MSG_TRUNC;
                    815:                if ((flags & MSG_PEEK) == 0)
                    816:                        (void) sbdroprecord(&so->so_rcv);
                    817:        }
1.1       cgd       818:        if ((flags & MSG_PEEK) == 0) {
                    819:                if (m == 0)
                    820:                        so->so_rcv.sb_mb = nextrecord;
                    821:                if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1.22      mycroft   822:                        (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
                    823:                            (struct mbuf *)(long)flags, (struct mbuf *)0,
                    824:                            (struct proc *)0);
1.1       cgd       825:        }
1.3       andrew    826:        if (orig_resid == uio->uio_resid && orig_resid &&
                    827:            (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
                    828:                sbunlock(&so->so_rcv);
                    829:                splx(s);
                    830:                goto restart;
                    831:        }
                    832:
1.1       cgd       833:        if (flagsp)
                    834:                *flagsp |= flags;
1.54      lukem     835:  release:
1.1       cgd       836:        sbunlock(&so->so_rcv);
                    837:        splx(s);
                    838:        return (error);
                    839: }
                    840:
1.14      mycroft   841: int
1.54      lukem     842: soshutdown(struct socket *so, int how)
1.1       cgd       843: {
1.54      lukem     844:        struct protosw  *pr;
1.34      kleink    845:
1.54      lukem     846:        pr = so->so_proto;
1.34      kleink    847:        if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
                    848:                return (EINVAL);
1.1       cgd       849:
1.34      kleink    850:        if (how == SHUT_RD || how == SHUT_RDWR)
1.1       cgd       851:                sorflush(so);
1.34      kleink    852:        if (how == SHUT_WR || how == SHUT_RDWR)
1.22      mycroft   853:                return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
                    854:                    (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1.1       cgd       855:        return (0);
                    856: }
                    857:
1.14      mycroft   858: void
1.54      lukem     859: sorflush(struct socket *so)
1.1       cgd       860: {
1.54      lukem     861:        struct sockbuf  *sb, asb;
                    862:        struct protosw  *pr;
                    863:        int             s;
1.1       cgd       864:
1.54      lukem     865:        sb = &so->so_rcv;
                    866:        pr = so->so_proto;
1.1       cgd       867:        sb->sb_flags |= SB_NOINTR;
1.15      mycroft   868:        (void) sblock(sb, M_WAITOK);
1.54.2.3  nathanw   869:        s = splnet();
1.1       cgd       870:        socantrcvmore(so);
                    871:        sbunlock(sb);
                    872:        asb = *sb;
1.38      perry     873:        memset((caddr_t)sb, 0, sizeof(*sb));
1.1       cgd       874:        splx(s);
                    875:        if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
                    876:                (*pr->pr_domain->dom_dispose)(asb.sb_mb);
                    877:        sbrelease(&asb);
                    878: }
                    879:
1.14      mycroft   880: int
1.54      lukem     881: sosetopt(struct socket *so, int level, int optname, struct mbuf *m0)
1.1       cgd       882: {
1.54      lukem     883:        int             error;
                    884:        struct mbuf     *m;
1.1       cgd       885:
1.54      lukem     886:        error = 0;
                    887:        m = m0;
1.1       cgd       888:        if (level != SOL_SOCKET) {
                    889:                if (so->so_proto && so->so_proto->pr_ctloutput)
                    890:                        return ((*so->so_proto->pr_ctloutput)
                    891:                                  (PRCO_SETOPT, so, level, optname, &m0));
                    892:                error = ENOPROTOOPT;
                    893:        } else {
                    894:                switch (optname) {
                    895:
                    896:                case SO_LINGER:
1.36      perry     897:                        if (m == NULL || m->m_len != sizeof(struct linger)) {
1.1       cgd       898:                                error = EINVAL;
                    899:                                goto bad;
                    900:                        }
                    901:                        so->so_linger = mtod(m, struct linger *)->l_linger;
                    902:                        /* fall thru... */
                    903:
                    904:                case SO_DEBUG:
                    905:                case SO_KEEPALIVE:
                    906:                case SO_DONTROUTE:
                    907:                case SO_USELOOPBACK:
                    908:                case SO_BROADCAST:
                    909:                case SO_REUSEADDR:
1.15      mycroft   910:                case SO_REUSEPORT:
1.1       cgd       911:                case SO_OOBINLINE:
1.26      thorpej   912:                case SO_TIMESTAMP:
1.36      perry     913:                        if (m == NULL || m->m_len < sizeof(int)) {
1.1       cgd       914:                                error = EINVAL;
                    915:                                goto bad;
                    916:                        }
                    917:                        if (*mtod(m, int *))
                    918:                                so->so_options |= optname;
                    919:                        else
                    920:                                so->so_options &= ~optname;
                    921:                        break;
                    922:
                    923:                case SO_SNDBUF:
                    924:                case SO_RCVBUF:
                    925:                case SO_SNDLOWAT:
                    926:                case SO_RCVLOWAT:
1.28      thorpej   927:                    {
                    928:                        int optval;
                    929:
1.36      perry     930:                        if (m == NULL || m->m_len < sizeof(int)) {
1.1       cgd       931:                                error = EINVAL;
                    932:                                goto bad;
                    933:                        }
1.28      thorpej   934:
                    935:                        /*
                    936:                         * Values < 1 make no sense for any of these
                    937:                         * options, so disallow them.
                    938:                         */
                    939:                        optval = *mtod(m, int *);
                    940:                        if (optval < 1) {
                    941:                                error = EINVAL;
                    942:                                goto bad;
                    943:                        }
                    944:
1.1       cgd       945:                        switch (optname) {
                    946:
                    947:                        case SO_SNDBUF:
                    948:                        case SO_RCVBUF:
                    949:                                if (sbreserve(optname == SO_SNDBUF ?
                    950:                                    &so->so_snd : &so->so_rcv,
1.28      thorpej   951:                                    (u_long) optval) == 0) {
1.1       cgd       952:                                        error = ENOBUFS;
                    953:                                        goto bad;
                    954:                                }
                    955:                                break;
                    956:
1.28      thorpej   957:                        /*
                    958:                         * Make sure the low-water is never greater than
                    959:                         * the high-water.
                    960:                         */
1.1       cgd       961:                        case SO_SNDLOWAT:
1.28      thorpej   962:                                so->so_snd.sb_lowat =
                    963:                                    (optval > so->so_snd.sb_hiwat) ?
                    964:                                    so->so_snd.sb_hiwat : optval;
1.1       cgd       965:                                break;
                    966:                        case SO_RCVLOWAT:
1.28      thorpej   967:                                so->so_rcv.sb_lowat =
                    968:                                    (optval > so->so_rcv.sb_hiwat) ?
                    969:                                    so->so_rcv.sb_hiwat : optval;
1.1       cgd       970:                                break;
                    971:                        }
                    972:                        break;
1.28      thorpej   973:                    }
1.1       cgd       974:
                    975:                case SO_SNDTIMEO:
                    976:                case SO_RCVTIMEO:
                    977:                    {
                    978:                        struct timeval *tv;
                    979:                        short val;
                    980:
1.36      perry     981:                        if (m == NULL || m->m_len < sizeof(*tv)) {
1.1       cgd       982:                                error = EINVAL;
                    983:                                goto bad;
                    984:                        }
                    985:                        tv = mtod(m, struct timeval *);
1.19      cgd       986:                        if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) {
1.1       cgd       987:                                error = EDOM;
                    988:                                goto bad;
                    989:                        }
                    990:                        val = tv->tv_sec * hz + tv->tv_usec / tick;
                    991:
                    992:                        switch (optname) {
                    993:
                    994:                        case SO_SNDTIMEO:
                    995:                                so->so_snd.sb_timeo = val;
                    996:                                break;
                    997:                        case SO_RCVTIMEO:
                    998:                                so->so_rcv.sb_timeo = val;
                    999:                                break;
                   1000:                        }
                   1001:                        break;
                   1002:                    }
                   1003:
                   1004:                default:
                   1005:                        error = ENOPROTOOPT;
                   1006:                        break;
                   1007:                }
1.15      mycroft  1008:                if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
                   1009:                        (void) ((*so->so_proto->pr_ctloutput)
                   1010:                                  (PRCO_SETOPT, so, level, optname, &m0));
                   1011:                        m = NULL;       /* freed by protocol */
                   1012:                }
1.1       cgd      1013:        }
1.54      lukem    1014:  bad:
1.1       cgd      1015:        if (m)
                   1016:                (void) m_free(m);
                   1017:        return (error);
                   1018: }
                   1019:
1.14      mycroft  1020: int
1.54      lukem    1021: sogetopt(struct socket *so, int level, int optname, struct mbuf **mp)
1.1       cgd      1022: {
1.54      lukem    1023:        struct mbuf     *m;
1.1       cgd      1024:
                   1025:        if (level != SOL_SOCKET) {
                   1026:                if (so->so_proto && so->so_proto->pr_ctloutput) {
                   1027:                        return ((*so->so_proto->pr_ctloutput)
                   1028:                                  (PRCO_GETOPT, so, level, optname, mp));
                   1029:                } else
                   1030:                        return (ENOPROTOOPT);
                   1031:        } else {
                   1032:                m = m_get(M_WAIT, MT_SOOPTS);
1.36      perry    1033:                m->m_len = sizeof(int);
1.1       cgd      1034:
                   1035:                switch (optname) {
                   1036:
                   1037:                case SO_LINGER:
1.36      perry    1038:                        m->m_len = sizeof(struct linger);
1.1       cgd      1039:                        mtod(m, struct linger *)->l_onoff =
                   1040:                                so->so_options & SO_LINGER;
                   1041:                        mtod(m, struct linger *)->l_linger = so->so_linger;
                   1042:                        break;
                   1043:
                   1044:                case SO_USELOOPBACK:
                   1045:                case SO_DONTROUTE:
                   1046:                case SO_DEBUG:
                   1047:                case SO_KEEPALIVE:
                   1048:                case SO_REUSEADDR:
1.15      mycroft  1049:                case SO_REUSEPORT:
1.1       cgd      1050:                case SO_BROADCAST:
                   1051:                case SO_OOBINLINE:
1.26      thorpej  1052:                case SO_TIMESTAMP:
1.1       cgd      1053:                        *mtod(m, int *) = so->so_options & optname;
                   1054:                        break;
                   1055:
                   1056:                case SO_TYPE:
                   1057:                        *mtod(m, int *) = so->so_type;
                   1058:                        break;
                   1059:
                   1060:                case SO_ERROR:
                   1061:                        *mtod(m, int *) = so->so_error;
                   1062:                        so->so_error = 0;
                   1063:                        break;
                   1064:
                   1065:                case SO_SNDBUF:
                   1066:                        *mtod(m, int *) = so->so_snd.sb_hiwat;
                   1067:                        break;
                   1068:
                   1069:                case SO_RCVBUF:
                   1070:                        *mtod(m, int *) = so->so_rcv.sb_hiwat;
                   1071:                        break;
                   1072:
                   1073:                case SO_SNDLOWAT:
                   1074:                        *mtod(m, int *) = so->so_snd.sb_lowat;
                   1075:                        break;
                   1076:
                   1077:                case SO_RCVLOWAT:
                   1078:                        *mtod(m, int *) = so->so_rcv.sb_lowat;
                   1079:                        break;
                   1080:
                   1081:                case SO_SNDTIMEO:
                   1082:                case SO_RCVTIMEO:
                   1083:                    {
                   1084:                        int val = (optname == SO_SNDTIMEO ?
                   1085:                             so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
                   1086:
                   1087:                        m->m_len = sizeof(struct timeval);
                   1088:                        mtod(m, struct timeval *)->tv_sec = val / hz;
                   1089:                        mtod(m, struct timeval *)->tv_usec =
1.27      kleink   1090:                            (val % hz) * tick;
1.1       cgd      1091:                        break;
                   1092:                    }
                   1093:
                   1094:                default:
                   1095:                        (void)m_free(m);
                   1096:                        return (ENOPROTOOPT);
                   1097:                }
                   1098:                *mp = m;
                   1099:                return (0);
                   1100:        }
                   1101: }
                   1102:
1.14      mycroft  1103: void
1.54      lukem    1104: sohasoutofband(struct socket *so)
1.1       cgd      1105: {
                   1106:        struct proc *p;
                   1107:
                   1108:        if (so->so_pgid < 0)
                   1109:                gsignal(-so->so_pgid, SIGURG);
                   1110:        else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
                   1111:                psignal(p, SIGURG);
1.2       cgd      1112:        selwakeup(&so->so_rcv.sb_sel);
1.1       cgd      1113: }

CVSweb <webmaster@jp.NetBSD.org>