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

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

CVSweb <webmaster@jp.NetBSD.org>