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

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

CVSweb <webmaster@jp.NetBSD.org>