[BACK]Return to raw_ip.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Annotation of src/sys/netinet/raw_ip.c, Revision 1.29

1.29    ! mycroft     1: /*     $NetBSD: raw_ip.c,v 1.28 1996/05/23 17:03:27 mycroft Exp $      */
1.14      cgd         2:
1.1       cgd         3: /*
1.13      mycroft     4:  * Copyright (c) 1982, 1986, 1988, 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.14      cgd        35:  *     @(#)raw_ip.c    8.2 (Berkeley) 1/4/94
1.1       cgd        36:  */
                     37:
1.7       mycroft    38: #include <sys/param.h>
                     39: #include <sys/malloc.h>
                     40: #include <sys/mbuf.h>
                     41: #include <sys/socket.h>
                     42: #include <sys/protosw.h>
                     43: #include <sys/socketvar.h>
                     44: #include <sys/errno.h>
1.13      mycroft    45: #include <sys/systm.h>
1.26      mycroft    46: #include <sys/proc.h>
1.1       cgd        47:
1.7       mycroft    48: #include <net/if.h>
                     49: #include <net/route.h>
1.1       cgd        50:
1.7       mycroft    51: #include <netinet/in.h>
                     52: #include <netinet/in_systm.h>
                     53: #include <netinet/ip.h>
                     54: #include <netinet/ip_var.h>
1.13      mycroft    55: #include <netinet/ip_mroute.h>
1.7       mycroft    56: #include <netinet/in_pcb.h>
1.24      christos   57: #include <netinet/in_var.h>
                     58:
                     59: #include <machine/stdarg.h>
1.13      mycroft    60:
1.20      mycroft    61: struct inpcbtable rawcbtable;
1.13      mycroft    62:
                     63: /*
                     64:  * Nominal space allocated to a raw ip socket.
                     65:  */
                     66: #define        RIPSNDQ         8192
                     67: #define        RIPRCVQ         8192
1.1       cgd        68:
                     69: /*
                     70:  * Raw interface to IP protocol.
                     71:  */
1.13      mycroft    72:
                     73: /*
                     74:  * Initialize raw connection block q.
                     75:  */
                     76: void
                     77: rip_init()
                     78: {
                     79:
1.23      mycroft    80:        in_pcbinit(&rawcbtable, 1);
1.13      mycroft    81: }
                     82:
1.1       cgd        83: struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
                     84: /*
                     85:  * Setup generic address and protocol structures
                     86:  * for raw_input routine, then pass them along with
                     87:  * mbuf chain.
                     88:  */
1.9       mycroft    89: void
1.24      christos   90: #if __STDC__
                     91: rip_input(struct mbuf *m, ...)
                     92: #else
                     93: rip_input(m, va_alist)
1.1       cgd        94:        struct mbuf *m;
1.24      christos   95:        va_dcl
                     96: #endif
1.1       cgd        97: {
                     98:        register struct ip *ip = mtod(m, struct ip *);
1.13      mycroft    99:        register struct inpcb *inp;
                    100:        struct socket *last = 0;
1.1       cgd       101:
                    102:        ripsrc.sin_addr = ip->ip_src;
1.21      cgd       103:        for (inp = rawcbtable.inpt_queue.cqh_first;
                    104:            inp != (struct inpcb *)&rawcbtable.inpt_queue;
                    105:            inp = inp->inp_queue.cqe_next) {
1.13      mycroft   106:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p)
                    107:                        continue;
1.27      mycroft   108:                if (inp->inp_laddr.s_addr != INADDR_ANY &&
1.16      glass     109:                    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
1.13      mycroft   110:                        continue;
1.27      mycroft   111:                if (inp->inp_faddr.s_addr != INADDR_ANY &&
1.16      glass     112:                    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
1.13      mycroft   113:                        continue;
                    114:                if (last) {
                    115:                        struct mbuf *n;
1.24      christos  116:                        if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1.13      mycroft   117:                                if (sbappendaddr(&last->so_rcv,
1.19      mycroft   118:                                    sintosa(&ripsrc), n,
                    119:                                    (struct mbuf *)0) == 0)
1.13      mycroft   120:                                        /* should notify about lost packet */
                    121:                                        m_freem(n);
                    122:                                else
                    123:                                        sorwakeup(last);
                    124:                        }
                    125:                }
                    126:                last = inp->inp_socket;
                    127:        }
                    128:        if (last) {
1.19      mycroft   129:                if (sbappendaddr(&last->so_rcv, sintosa(&ripsrc), m,
                    130:                    (struct mbuf *)0) == 0)
1.13      mycroft   131:                        m_freem(m);
                    132:                else
                    133:                        sorwakeup(last);
                    134:        } else {
                    135:                m_freem(m);
1.1       cgd       136:                ipstat.ips_noproto++;
                    137:                ipstat.ips_delivered--;
                    138:        }
                    139: }
                    140:
                    141: /*
                    142:  * Generate IP header and pass packet to ip_output.
                    143:  * Tack on options user may have setup with control call.
                    144:  */
1.9       mycroft   145: int
1.24      christos  146: #if __STDC__
                    147: rip_output(struct mbuf *m, ...)
                    148: #else
                    149: rip_output(m, va_alist)
                    150:        struct mbuf *m;
                    151:        va_dcl
                    152: #endif
                    153: {
1.27      mycroft   154:        register struct inpcb *inp;
1.1       cgd       155:        register struct ip *ip;
1.10      mycroft   156:        struct mbuf *opts;
1.24      christos  157:        int flags;
                    158:        va_list ap;
                    159:
                    160:        va_start(ap, m);
1.27      mycroft   161:        inp = va_arg(ap, struct inpcb *);
1.24      christos  162:        va_end(ap);
                    163:
1.27      mycroft   164:        flags =
                    165:            (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
1.1       cgd       166:
                    167:        /*
                    168:         * If the user handed us a complete IP packet, use it.
                    169:         * Otherwise, allocate an mbuf for a header and fill it in.
                    170:         */
1.13      mycroft   171:        if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.1       cgd       172:                M_PREPEND(m, sizeof(struct ip), M_WAIT);
                    173:                ip = mtod(m, struct ip *);
                    174:                ip->ip_tos = 0;
                    175:                ip->ip_off = 0;
1.13      mycroft   176:                ip->ip_p = inp->inp_ip.ip_p;
1.1       cgd       177:                ip->ip_len = m->m_pkthdr.len;
1.13      mycroft   178:                ip->ip_src = inp->inp_laddr;
1.27      mycroft   179:                ip->ip_dst = inp->inp_faddr;
1.1       cgd       180:                ip->ip_ttl = MAXTTL;
1.13      mycroft   181:                opts = inp->inp_options;
                    182:        } else {
                    183:                ip = mtod(m, struct ip *);
                    184:                if (ip->ip_id == 0)
                    185:                        ip->ip_id = htons(ip_id++);
                    186:                opts = NULL;
                    187:                /* XXX prevent ip_output from overwriting header fields */
                    188:                flags |= IP_RAWOUTPUT;
                    189:                ipstat.ips_rawout++;
1.1       cgd       190:        }
1.13      mycroft   191:        return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions));
1.1       cgd       192: }
                    193:
                    194: /*
                    195:  * Raw IP socket option processing.
                    196:  */
1.9       mycroft   197: int
1.1       cgd       198: rip_ctloutput(op, so, level, optname, m)
                    199:        int op;
                    200:        struct socket *so;
                    201:        int level, optname;
                    202:        struct mbuf **m;
                    203: {
1.13      mycroft   204:        register struct inpcb *inp = sotoinpcb(so);
1.25      christos  205: #ifdef MROUTING
                    206:        int error;
                    207: #endif
1.1       cgd       208:
1.15      mycroft   209:        if (level != IPPROTO_IP) {
                    210:                if (m != 0 && *m != 0)
                    211:                        (void)m_free(*m);
1.13      mycroft   212:                return (EINVAL);
1.15      mycroft   213:        }
1.1       cgd       214:
1.13      mycroft   215:        switch (optname) {
1.1       cgd       216:
1.13      mycroft   217:        case IP_HDRINCL:
                    218:                if (op == PRCO_SETOPT || op == PRCO_GETOPT) {
                    219:                        if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
                    220:                                return (EINVAL);
                    221:                        if (op == PRCO_SETOPT) {
                    222:                                if (*mtod(*m, int *))
                    223:                                        inp->inp_flags |= INP_HDRINCL;
                    224:                                else
                    225:                                        inp->inp_flags &= ~INP_HDRINCL;
                    226:                                (void)m_free(*m);
                    227:                        } else {
                    228:                                (*m)->m_len = sizeof (int);
                    229:                                *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
                    230:                        }
                    231:                        return (0);
                    232:                }
                    233:                break;
1.1       cgd       234:
1.18      mycroft   235:        case MRT_INIT:
                    236:        case MRT_DONE:
                    237:        case MRT_ADD_VIF:
                    238:        case MRT_DEL_VIF:
                    239:        case MRT_ADD_MFC:
                    240:        case MRT_DEL_MFC:
                    241:        case MRT_VERSION:
                    242:        case MRT_ASSERT:
1.6       hpeyerl   243: #ifdef MROUTING
1.18      mycroft   244:                switch (op) {
                    245:                case PRCO_SETOPT:
                    246:                        error = ip_mrouter_set(optname, so, m);
                    247:                        break;
                    248:                case PRCO_GETOPT:
                    249:                        error = ip_mrouter_get(optname, so, m);
                    250:                        break;
                    251:                default:
1.13      mycroft   252:                        error = EINVAL;
1.18      mycroft   253:                        break;
                    254:                }
1.13      mycroft   255:                return (error);
1.6       hpeyerl   256: #else
1.13      mycroft   257:                if (op == PRCO_SETOPT && *m)
1.18      mycroft   258:                        m_free(*m);
1.13      mycroft   259:                return (EOPNOTSUPP);
1.6       hpeyerl   260: #endif
1.1       cgd       261:        }
1.13      mycroft   262:        return (ip_ctloutput(op, so, level, optname, m));
1.1       cgd       263: }
                    264:
1.27      mycroft   265: int
1.29    ! mycroft   266: rip_bind(inp, nam)
        !           267:        struct inpcb *inp;
        !           268:        struct mbuf *nam;
        !           269: {
        !           270:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
        !           271:
        !           272:        if (nam->m_len != sizeof(*addr))
        !           273:                return (EINVAL);
        !           274:        if (ifnet.tqh_first == 0)
        !           275:                return (EADDRNOTAVAIL);
        !           276:        if (addr->sin_family != AF_INET &&
        !           277:            addr->sin_family != AF_IMPLINK)
        !           278:                return (EAFNOSUPPORT);
        !           279:        if (addr->sin_addr.s_addr != INADDR_ANY &&
        !           280:            ifa_ifwithaddr(sintosa(addr)) == 0)
        !           281:                return (EADDRNOTAVAIL);
        !           282:        inp->inp_laddr = addr->sin_addr;
        !           283:        return (0);
        !           284: }
        !           285:
        !           286: int
1.27      mycroft   287: rip_connect(inp, nam)
                    288:        struct inpcb *inp;
                    289:        struct mbuf *nam;
                    290: {
                    291:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    292:
                    293:        if (nam->m_len != sizeof(*addr))
                    294:                return (EINVAL);
                    295:        if (ifnet.tqh_first == 0)
                    296:                return (EADDRNOTAVAIL);
                    297:        if (addr->sin_family != AF_INET &&
                    298:            addr->sin_family != AF_IMPLINK)
                    299:                return (EAFNOSUPPORT);
                    300:        inp->inp_faddr = addr->sin_addr;
                    301:        return (0);
                    302: }
                    303:
                    304: void
                    305: rip_disconnect(inp)
                    306:        struct inpcb *inp;
                    307: {
                    308:
                    309:        inp->inp_faddr.s_addr = INADDR_ANY;
                    310: }
                    311:
1.13      mycroft   312: u_long rip_sendspace = RIPSNDQ;
                    313: u_long rip_recvspace = RIPRCVQ;
                    314:
1.1       cgd       315: /*ARGSUSED*/
1.9       mycroft   316: int
1.26      mycroft   317: rip_usrreq(so, req, m, nam, control, p)
1.1       cgd       318:        register struct socket *so;
                    319:        int req;
1.2       cgd       320:        struct mbuf *m, *nam, *control;
1.26      mycroft   321:        struct proc *p;
1.1       cgd       322: {
1.27      mycroft   323:        register struct inpcb *inp;
                    324:        int s;
1.1       cgd       325:        register int error = 0;
1.13      mycroft   326: #ifdef MROUTING
1.6       hpeyerl   327:        extern struct socket *ip_mrouter;
                    328: #endif
1.27      mycroft   329:
1.22      pk        330:        if (req == PRU_CONTROL)
                    331:                return (in_control(so, (long)m, (caddr_t)nam,
1.26      mycroft   332:                    (struct ifnet *)control, p));
1.22      pk        333:
1.27      mycroft   334:        s = splsoftnet();
                    335:        inp = sotoinpcb(so);
1.28      mycroft   336: #ifdef DIAGNOSTIC
                    337:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    338:                panic("rip_usrreq: unexpected control mbuf");
                    339: #endif
1.27      mycroft   340:        if (inp == 0 && req != PRU_ATTACH) {
1.22      pk        341:                error = EINVAL;
                    342:                goto release;
                    343:        }
                    344:
1.1       cgd       345:        switch (req) {
                    346:
                    347:        case PRU_ATTACH:
1.27      mycroft   348:                if (inp != 0) {
                    349:                        error = EISCONN;
                    350:                        break;
                    351:                }
1.26      mycroft   352:                if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
1.13      mycroft   353:                        error = EACCES;
                    354:                        break;
                    355:                }
1.27      mycroft   356:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    357:                        error = soreserve(so, rip_sendspace, rip_recvspace);
                    358:                        if (error)
                    359:                                break;
                    360:                }
                    361:                error = in_pcballoc(so, &rawcbtable);
                    362:                if (error)
1.13      mycroft   363:                        break;
1.27      mycroft   364:                inp = sotoinpcb(so);
1.17      cgd       365:                inp->inp_ip.ip_p = (long)nam;
1.1       cgd       366:                break;
                    367:
                    368:        case PRU_DETACH:
1.13      mycroft   369: #ifdef MROUTING
1.6       hpeyerl   370:                if (so == ip_mrouter)
                    371:                        ip_mrouter_done();
                    372: #endif
1.13      mycroft   373:                in_pcbdetach(inp);
1.1       cgd       374:                break;
                    375:
                    376:        case PRU_BIND:
1.29    ! mycroft   377:                error = rip_bind(inp, nam);
1.27      mycroft   378:                break;
                    379:
                    380:        case PRU_LISTEN:
                    381:                error = EOPNOTSUPP;
                    382:                break;
                    383:
                    384:        case PRU_CONNECT:
                    385:                error = rip_connect(inp, nam);
                    386:                if (error)
1.13      mycroft   387:                        break;
1.1       cgd       388:                soisconnected(so);
1.13      mycroft   389:                break;
                    390:
                    391:        case PRU_CONNECT2:
                    392:                error = EOPNOTSUPP;
                    393:                break;
                    394:
1.27      mycroft   395:        case PRU_DISCONNECT:
                    396:                soisdisconnected(so);
                    397:                rip_disconnect(inp);
                    398:                break;
                    399:
1.13      mycroft   400:        /*
                    401:         * Mark the connection as being incapable of further input.
                    402:         */
                    403:        case PRU_SHUTDOWN:
                    404:                socantsendmore(so);
                    405:                break;
                    406:
1.27      mycroft   407:        case PRU_RCVD:
                    408:                error = EOPNOTSUPP;
                    409:                break;
                    410:
1.13      mycroft   411:        /*
                    412:         * Ship a packet out.  The appropriate raw output
                    413:         * routine handles any massaging necessary.
                    414:         */
                    415:        case PRU_SEND:
1.28      mycroft   416:                if (control && control->m_len) {
                    417:                        m_freem(control);
                    418:                        m_freem(m);
                    419:                        error = EINVAL;
                    420:                        break;
                    421:                }
                    422:        {
1.27      mycroft   423:                if (nam) {
                    424:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13      mycroft   425:                                error = EISCONN;
1.28      mycroft   426:                                goto die;
1.13      mycroft   427:                        }
1.27      mycroft   428:                        error = rip_connect(inp, nam);
                    429:                        if (error) {
1.28      mycroft   430:                        die:
1.27      mycroft   431:                                m_freem(m);
                    432:                                break;
                    433:                        }
1.13      mycroft   434:                } else {
1.27      mycroft   435:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13      mycroft   436:                                error = ENOTCONN;
1.28      mycroft   437:                                goto die;
1.13      mycroft   438:                        }
                    439:                }
1.27      mycroft   440:                error = rip_output(m, inp);
                    441:                if (nam)
                    442:                        rip_disconnect(inp);
1.28      mycroft   443:        }
1.13      mycroft   444:                break;
                    445:
                    446:        case PRU_SENSE:
                    447:                /*
                    448:                 * stat: don't bother with a blocksize.
                    449:                 */
1.27      mycroft   450:                splx(s);
1.1       cgd       451:                return (0);
1.13      mycroft   452:
                    453:        case PRU_RCVOOB:
1.27      mycroft   454:                error = EOPNOTSUPP;
                    455:                break;
                    456:
1.13      mycroft   457:        case PRU_SENDOOB:
1.28      mycroft   458:                m_freem(control);
1.27      mycroft   459:                m_freem(m);
1.13      mycroft   460:                error = EOPNOTSUPP;
                    461:                break;
                    462:
                    463:        case PRU_SOCKADDR:
                    464:                in_setsockaddr(inp, nam);
                    465:                break;
                    466:
                    467:        case PRU_PEERADDR:
                    468:                in_setpeeraddr(inp, nam);
                    469:                break;
                    470:
                    471:        default:
                    472:                panic("rip_usrreq");
1.1       cgd       473:        }
1.27      mycroft   474:
1.22      pk        475: release:
1.27      mycroft   476:        splx(s);
1.1       cgd       477:        return (error);
                    478: }

CVSweb <webmaster@jp.NetBSD.org>