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

1.1       cgd         1: /*
1.13    ! mycroft     2:  * Copyright (c) 1982, 1986, 1988, 1993
        !             3:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This product includes software developed by the University of
                     16:  *     California, Berkeley and its contributors.
                     17:  * 4. Neither the name of the University nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  *
1.13    ! mycroft    33:  *     from: @(#)raw_ip.c      8.2 (Berkeley) 1/4/94
        !            34:  *     $Id: $
1.1       cgd        35:  */
                     36:
1.7       mycroft    37: #include <sys/param.h>
                     38: #include <sys/malloc.h>
                     39: #include <sys/mbuf.h>
                     40: #include <sys/socket.h>
                     41: #include <sys/protosw.h>
                     42: #include <sys/socketvar.h>
                     43: #include <sys/errno.h>
1.13    ! mycroft    44: #include <sys/systm.h>
1.1       cgd        45:
1.7       mycroft    46: #include <net/if.h>
                     47: #include <net/route.h>
1.1       cgd        48:
1.7       mycroft    49: #include <netinet/in.h>
                     50: #include <netinet/in_systm.h>
                     51: #include <netinet/ip.h>
                     52: #include <netinet/ip_var.h>
1.13    ! mycroft    53: #include <netinet/ip_mroute.h>
1.7       mycroft    54: #include <netinet/in_pcb.h>
1.13    ! mycroft    55:
        !            56: struct inpcb rawinpcb;
        !            57:
        !            58: /*
        !            59:  * Nominal space allocated to a raw ip socket.
        !            60:  */
        !            61: #define        RIPSNDQ         8192
        !            62: #define        RIPRCVQ         8192
1.1       cgd        63:
                     64: /*
                     65:  * Raw interface to IP protocol.
                     66:  */
1.13    ! mycroft    67:
        !            68: /*
        !            69:  * Initialize raw connection block q.
        !            70:  */
        !            71: void
        !            72: rip_init()
        !            73: {
        !            74:
        !            75:        rawinpcb.inp_next = rawinpcb.inp_prev = &rawinpcb;
        !            76: }
        !            77:
1.1       cgd        78: struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
                     79: /*
                     80:  * Setup generic address and protocol structures
                     81:  * for raw_input routine, then pass them along with
                     82:  * mbuf chain.
                     83:  */
1.9       mycroft    84: void
1.1       cgd        85: rip_input(m)
                     86:        struct mbuf *m;
                     87: {
                     88:        register struct ip *ip = mtod(m, struct ip *);
1.13    ! mycroft    89:        register struct inpcb *inp;
        !            90:        struct socket *last = 0;
1.1       cgd        91:
                     92:        ripsrc.sin_addr = ip->ip_src;
1.13    ! mycroft    93:        for (inp = rawinpcb.inp_next; inp != &rawinpcb; inp = inp->inp_next) {
        !            94:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p)
        !            95:                        continue;
        !            96:                if (inp->inp_laddr.s_addr &&
        !            97:                    inp->inp_laddr.s_addr == ip->ip_dst.s_addr)
        !            98:                        continue;
        !            99:                if (inp->inp_faddr.s_addr &&
        !           100:                    inp->inp_faddr.s_addr == ip->ip_src.s_addr)
        !           101:                        continue;
        !           102:                if (last) {
        !           103:                        struct mbuf *n;
        !           104:                        if (n = m_copy(m, 0, (int)M_COPYALL)) {
        !           105:                                if (sbappendaddr(&last->so_rcv,
        !           106:                                    (struct sockaddr *)&ripsrc,
        !           107:                                    n, (struct mbuf *)0) == 0)
        !           108:                                        /* should notify about lost packet */
        !           109:                                        m_freem(n);
        !           110:                                else
        !           111:                                        sorwakeup(last);
        !           112:                        }
        !           113:                }
        !           114:                last = inp->inp_socket;
        !           115:        }
        !           116:        if (last) {
        !           117:                if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&ripsrc,
        !           118:                    m, (struct mbuf *)0) == 0)
        !           119:                        m_freem(m);
        !           120:                else
        !           121:                        sorwakeup(last);
        !           122:        } else {
        !           123:                m_freem(m);
1.1       cgd       124:                ipstat.ips_noproto++;
                    125:                ipstat.ips_delivered--;
                    126:        }
                    127: }
                    128:
                    129: /*
                    130:  * Generate IP header and pass packet to ip_output.
                    131:  * Tack on options user may have setup with control call.
                    132:  */
1.9       mycroft   133: int
1.13    ! mycroft   134: rip_output(m, so, dst)
1.1       cgd       135:        register struct mbuf *m;
                    136:        struct socket *so;
1.13    ! mycroft   137:        u_long dst;
1.1       cgd       138: {
                    139:        register struct ip *ip;
1.13    ! mycroft   140:        register struct inpcb *inp = sotoinpcb(so);
1.10      mycroft   141:        struct mbuf *opts;
1.13    ! mycroft   142:        int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
1.1       cgd       143:
                    144:        /*
                    145:         * If the user handed us a complete IP packet, use it.
                    146:         * Otherwise, allocate an mbuf for a header and fill it in.
                    147:         */
1.13    ! mycroft   148:        if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.1       cgd       149:                M_PREPEND(m, sizeof(struct ip), M_WAIT);
                    150:                ip = mtod(m, struct ip *);
                    151:                ip->ip_tos = 0;
                    152:                ip->ip_off = 0;
1.13    ! mycroft   153:                ip->ip_p = inp->inp_ip.ip_p;
1.1       cgd       154:                ip->ip_len = m->m_pkthdr.len;
1.13    ! mycroft   155:                ip->ip_src = inp->inp_laddr;
        !           156:                ip->ip_dst.s_addr = dst;
1.1       cgd       157:                ip->ip_ttl = MAXTTL;
1.13    ! mycroft   158:                opts = inp->inp_options;
        !           159:        } else {
        !           160:                ip = mtod(m, struct ip *);
        !           161:                if (ip->ip_id == 0)
        !           162:                        ip->ip_id = htons(ip_id++);
        !           163:                opts = NULL;
        !           164:                /* XXX prevent ip_output from overwriting header fields */
        !           165:                flags |= IP_RAWOUTPUT;
        !           166:                ipstat.ips_rawout++;
1.1       cgd       167:        }
1.13    ! mycroft   168:        return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions));
1.1       cgd       169: }
                    170:
                    171: /*
                    172:  * Raw IP socket option processing.
                    173:  */
1.9       mycroft   174: int
1.1       cgd       175: rip_ctloutput(op, so, level, optname, m)
                    176:        int op;
                    177:        struct socket *so;
                    178:        int level, optname;
                    179:        struct mbuf **m;
                    180: {
1.13    ! mycroft   181:        register struct inpcb *inp = sotoinpcb(so);
        !           182:        register int error;
1.1       cgd       183:
                    184:        if (level != IPPROTO_IP)
1.13    ! mycroft   185:                return (EINVAL);
1.1       cgd       186:
1.13    ! mycroft   187:        switch (optname) {
1.1       cgd       188:
1.13    ! mycroft   189:        case IP_HDRINCL:
        !           190:                if (op == PRCO_SETOPT || op == PRCO_GETOPT) {
        !           191:                        if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
        !           192:                                return (EINVAL);
        !           193:                        if (op == PRCO_SETOPT) {
        !           194:                                if (*mtod(*m, int *))
        !           195:                                        inp->inp_flags |= INP_HDRINCL;
        !           196:                                else
        !           197:                                        inp->inp_flags &= ~INP_HDRINCL;
        !           198:                                (void)m_free(*m);
        !           199:                        } else {
        !           200:                                (*m)->m_len = sizeof (int);
        !           201:                                *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
        !           202:                        }
        !           203:                        return (0);
        !           204:                }
        !           205:                break;
1.1       cgd       206:
1.13    ! mycroft   207:        case DVMRP_INIT:
        !           208:        case DVMRP_DONE:
        !           209:        case DVMRP_ADD_VIF:
        !           210:        case DVMRP_DEL_VIF:
        !           211:        case DVMRP_ADD_LGRP:
        !           212:        case DVMRP_DEL_LGRP:
        !           213:        case DVMRP_ADD_MRT:
        !           214:        case DVMRP_DEL_MRT:
1.6       hpeyerl   215: #ifdef MROUTING
1.13    ! mycroft   216:                if (op == PRCO_SETOPT) {
1.6       hpeyerl   217:                        error = ip_mrouter_cmd(optname, so, *m);
1.13    ! mycroft   218:                        if (*m)
        !           219:                                (void)m_free(*m);
        !           220:                } else
        !           221:                        error = EINVAL;
        !           222:                return (error);
1.6       hpeyerl   223: #else
1.13    ! mycroft   224:                if (op == PRCO_SETOPT && *m)
        !           225:                        (void)m_free(*m);
        !           226:                return (EOPNOTSUPP);
1.6       hpeyerl   227: #endif
1.1       cgd       228:        }
1.13    ! mycroft   229:        return (ip_ctloutput(op, so, level, optname, m));
1.1       cgd       230: }
                    231:
1.13    ! mycroft   232: u_long rip_sendspace = RIPSNDQ;
        !           233: u_long rip_recvspace = RIPRCVQ;
        !           234:
1.1       cgd       235: /*ARGSUSED*/
1.9       mycroft   236: int
1.2       cgd       237: rip_usrreq(so, req, m, nam, control)
1.1       cgd       238:        register struct socket *so;
                    239:        int req;
1.2       cgd       240:        struct mbuf *m, *nam, *control;
1.1       cgd       241: {
                    242:        register int error = 0;
1.13    ! mycroft   243:        register struct inpcb *inp = sotoinpcb(so);
        !           244: #ifdef MROUTING
1.6       hpeyerl   245:        extern struct socket *ip_mrouter;
                    246: #endif
1.1       cgd       247:        switch (req) {
                    248:
                    249:        case PRU_ATTACH:
1.13    ! mycroft   250:                if (inp)
1.1       cgd       251:                        panic("rip_attach");
1.13    ! mycroft   252:                if ((so->so_state & SS_PRIV) == 0) {
        !           253:                        error = EACCES;
        !           254:                        break;
        !           255:                }
        !           256:                if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
        !           257:                    (error = in_pcballoc(so, &rawinpcb)))
        !           258:                        break;
        !           259:                inp = (struct inpcb *)so->so_pcb;
        !           260:                inp->inp_ip.ip_p = (int)nam;
1.1       cgd       261:                break;
                    262:
1.13    ! mycroft   263:        case PRU_DISCONNECT:
        !           264:                if ((so->so_state & SS_ISCONNECTED) == 0) {
        !           265:                        error = ENOTCONN;
        !           266:                        break;
        !           267:                }
        !           268:                /* FALLTHROUGH */
        !           269:        case PRU_ABORT:
        !           270:                soisdisconnected(so);
        !           271:                /* FALLTHROUGH */
1.1       cgd       272:        case PRU_DETACH:
1.13    ! mycroft   273:                if (inp == 0)
1.1       cgd       274:                        panic("rip_detach");
1.13    ! mycroft   275: #ifdef MROUTING
1.6       hpeyerl   276:                if (so == ip_mrouter)
                    277:                        ip_mrouter_done();
                    278: #endif
1.13    ! mycroft   279:                in_pcbdetach(inp);
1.1       cgd       280:                break;
                    281:
                    282:        case PRU_BIND:
                    283:            {
                    284:                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    285:
1.13    ! mycroft   286:                if (nam->m_len != sizeof(*addr)) {
        !           287:                        error = EINVAL;
        !           288:                        break;
        !           289:                }
1.1       cgd       290:                if ((ifnet == 0) ||
                    291:                    ((addr->sin_family != AF_INET) &&
                    292:                     (addr->sin_family != AF_IMPLINK)) ||
                    293:                    (addr->sin_addr.s_addr &&
1.13    ! mycroft   294:                     ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
        !           295:                        error = EADDRNOTAVAIL;
        !           296:                        break;
        !           297:                }
        !           298:                inp->inp_laddr = addr->sin_addr;
        !           299:                break;
1.1       cgd       300:            }
                    301:        case PRU_CONNECT:
                    302:            {
                    303:                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    304:
1.13    ! mycroft   305:                if (nam->m_len != sizeof(*addr)) {
        !           306:                        error = EINVAL;
        !           307:                        break;
        !           308:                }
        !           309:                if (ifnet == 0) {
        !           310:                        error = EADDRNOTAVAIL;
        !           311:                        break;
        !           312:                }
1.1       cgd       313:                if ((addr->sin_family != AF_INET) &&
1.13    ! mycroft   314:                     (addr->sin_family != AF_IMPLINK)) {
        !           315:                        error = EAFNOSUPPORT;
        !           316:                        break;
        !           317:                }
        !           318:                inp->inp_faddr = addr->sin_addr;
1.1       cgd       319:                soisconnected(so);
1.13    ! mycroft   320:                break;
        !           321:            }
        !           322:
        !           323:        case PRU_CONNECT2:
        !           324:                error = EOPNOTSUPP;
        !           325:                break;
        !           326:
        !           327:        /*
        !           328:         * Mark the connection as being incapable of further input.
        !           329:         */
        !           330:        case PRU_SHUTDOWN:
        !           331:                socantsendmore(so);
        !           332:                break;
        !           333:
        !           334:        /*
        !           335:         * Ship a packet out.  The appropriate raw output
        !           336:         * routine handles any massaging necessary.
        !           337:         */
        !           338:        case PRU_SEND:
        !           339:            {
        !           340:                register u_long dst;
        !           341:
        !           342:                if (so->so_state & SS_ISCONNECTED) {
        !           343:                        if (nam) {
        !           344:                                error = EISCONN;
        !           345:                                break;
        !           346:                        }
        !           347:                        dst = inp->inp_faddr.s_addr;
        !           348:                } else {
        !           349:                        if (nam == NULL) {
        !           350:                                error = ENOTCONN;
        !           351:                                break;
        !           352:                        }
        !           353:                        dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
        !           354:                }
        !           355:                error = rip_output(m, so, dst);
        !           356:                m = NULL;
        !           357:                break;
        !           358:            }
        !           359:
        !           360:        case PRU_SENSE:
        !           361:                /*
        !           362:                 * stat: don't bother with a blocksize.
        !           363:                 */
1.1       cgd       364:                return (0);
1.13    ! mycroft   365:
        !           366:        /*
        !           367:         * Not supported.
        !           368:         */
        !           369:        case PRU_RCVOOB:
        !           370:        case PRU_RCVD:
        !           371:        case PRU_LISTEN:
        !           372:        case PRU_ACCEPT:
        !           373:        case PRU_SENDOOB:
        !           374:                error = EOPNOTSUPP;
        !           375:                break;
        !           376:
        !           377:        case PRU_SOCKADDR:
        !           378:                in_setsockaddr(inp, nam);
        !           379:                break;
        !           380:
        !           381:        case PRU_PEERADDR:
        !           382:                in_setpeeraddr(inp, nam);
        !           383:                break;
        !           384:
        !           385:        default:
        !           386:                panic("rip_usrreq");
1.1       cgd       387:        }
1.13    ! mycroft   388:        if (m != NULL)
        !           389:                m_freem(m);
1.1       cgd       390:        return (error);
                    391: }

CVSweb <webmaster@jp.NetBSD.org>