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

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

CVSweb <webmaster@jp.NetBSD.org>