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

1.24    ! christos    1: /*     $NetBSD: raw_ip.c,v 1.23 1996/01/31 03:49:31 mycroft Exp $      */
1.14      cgd         2:
1.1       cgd         3: /*
1.13      mycroft     4:  * Copyright (c) 1982, 1986, 1988, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  *
1.14      cgd        35:  *     @(#)raw_ip.c    8.2 (Berkeley) 1/4/94
1.1       cgd        36:  */
                     37:
1.7       mycroft    38: #include <sys/param.h>
                     39: #include <sys/malloc.h>
                     40: #include <sys/mbuf.h>
                     41: #include <sys/socket.h>
                     42: #include <sys/protosw.h>
                     43: #include <sys/socketvar.h>
                     44: #include <sys/errno.h>
1.13      mycroft    45: #include <sys/systm.h>
1.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.1       cgd       207:
1.15      mycroft   208:        if (level != IPPROTO_IP) {
                    209:                if (m != 0 && *m != 0)
                    210:                        (void)m_free(*m);
1.13      mycroft   211:                return (EINVAL);
1.15      mycroft   212:        }
1.1       cgd       213:
1.13      mycroft   214:        switch (optname) {
1.1       cgd       215:
1.13      mycroft   216:        case IP_HDRINCL:
                    217:                if (op == PRCO_SETOPT || op == PRCO_GETOPT) {
                    218:                        if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
                    219:                                return (EINVAL);
                    220:                        if (op == PRCO_SETOPT) {
                    221:                                if (*mtod(*m, int *))
                    222:                                        inp->inp_flags |= INP_HDRINCL;
                    223:                                else
                    224:                                        inp->inp_flags &= ~INP_HDRINCL;
                    225:                                (void)m_free(*m);
                    226:                        } else {
                    227:                                (*m)->m_len = sizeof (int);
                    228:                                *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
                    229:                        }
                    230:                        return (0);
                    231:                }
                    232:                break;
1.1       cgd       233:
1.18      mycroft   234:        case MRT_INIT:
                    235:        case MRT_DONE:
                    236:        case MRT_ADD_VIF:
                    237:        case MRT_DEL_VIF:
                    238:        case MRT_ADD_MFC:
                    239:        case MRT_DEL_MFC:
                    240:        case MRT_VERSION:
                    241:        case MRT_ASSERT:
1.6       hpeyerl   242: #ifdef MROUTING
1.18      mycroft   243:                switch (op) {
                    244:                case PRCO_SETOPT:
                    245:                        error = ip_mrouter_set(optname, so, m);
                    246:                        break;
                    247:                case PRCO_GETOPT:
                    248:                        error = ip_mrouter_get(optname, so, m);
                    249:                        break;
                    250:                default:
1.13      mycroft   251:                        error = EINVAL;
1.18      mycroft   252:                        break;
                    253:                }
1.13      mycroft   254:                return (error);
1.6       hpeyerl   255: #else
1.13      mycroft   256:                if (op == PRCO_SETOPT && *m)
1.18      mycroft   257:                        m_free(*m);
1.13      mycroft   258:                return (EOPNOTSUPP);
1.6       hpeyerl   259: #endif
1.1       cgd       260:        }
1.13      mycroft   261:        return (ip_ctloutput(op, so, level, optname, m));
1.1       cgd       262: }
                    263:
1.13      mycroft   264: u_long rip_sendspace = RIPSNDQ;
                    265: u_long rip_recvspace = RIPRCVQ;
                    266:
1.1       cgd       267: /*ARGSUSED*/
1.9       mycroft   268: int
1.2       cgd       269: rip_usrreq(so, req, m, nam, control)
1.1       cgd       270:        register struct socket *so;
                    271:        int req;
1.2       cgd       272:        struct mbuf *m, *nam, *control;
1.1       cgd       273: {
                    274:        register int error = 0;
1.13      mycroft   275:        register struct inpcb *inp = sotoinpcb(so);
                    276: #ifdef MROUTING
1.6       hpeyerl   277:        extern struct socket *ip_mrouter;
                    278: #endif
1.22      pk        279:        if (req == PRU_CONTROL)
                    280:                return (in_control(so, (long)m, (caddr_t)nam,
                    281:                        (struct ifnet *)control));
                    282:
                    283:        if (inp == NULL && req != PRU_ATTACH) {
                    284:                error = EINVAL;
                    285:                goto release;
                    286:        }
                    287:
1.1       cgd       288:        switch (req) {
                    289:
                    290:        case PRU_ATTACH:
1.13      mycroft   291:                if (inp)
1.1       cgd       292:                        panic("rip_attach");
1.13      mycroft   293:                if ((so->so_state & SS_PRIV) == 0) {
                    294:                        error = EACCES;
                    295:                        break;
                    296:                }
                    297:                if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
1.20      mycroft   298:                    (error = in_pcballoc(so, &rawcbtable)))
1.13      mycroft   299:                        break;
                    300:                inp = (struct inpcb *)so->so_pcb;
1.17      cgd       301:                inp->inp_ip.ip_p = (long)nam;
1.1       cgd       302:                break;
                    303:
1.13      mycroft   304:        case PRU_DISCONNECT:
                    305:                if ((so->so_state & SS_ISCONNECTED) == 0) {
                    306:                        error = ENOTCONN;
                    307:                        break;
                    308:                }
                    309:                /* FALLTHROUGH */
                    310:        case PRU_ABORT:
                    311:                soisdisconnected(so);
                    312:                /* FALLTHROUGH */
1.1       cgd       313:        case PRU_DETACH:
1.13      mycroft   314:                if (inp == 0)
1.1       cgd       315:                        panic("rip_detach");
1.13      mycroft   316: #ifdef MROUTING
1.6       hpeyerl   317:                if (so == ip_mrouter)
                    318:                        ip_mrouter_done();
                    319: #endif
1.13      mycroft   320:                in_pcbdetach(inp);
1.1       cgd       321:                break;
                    322:
                    323:        case PRU_BIND:
                    324:            {
                    325:                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    326:
1.13      mycroft   327:                if (nam->m_len != sizeof(*addr)) {
                    328:                        error = EINVAL;
                    329:                        break;
                    330:                }
1.20      mycroft   331:                if ((ifnet.tqh_first == 0) ||
1.1       cgd       332:                    ((addr->sin_family != AF_INET) &&
                    333:                     (addr->sin_family != AF_IMPLINK)) ||
                    334:                    (addr->sin_addr.s_addr &&
1.19      mycroft   335:                     ifa_ifwithaddr(sintosa(addr)) == 0)) {
1.13      mycroft   336:                        error = EADDRNOTAVAIL;
                    337:                        break;
                    338:                }
                    339:                inp->inp_laddr = addr->sin_addr;
                    340:                break;
1.1       cgd       341:            }
                    342:        case PRU_CONNECT:
                    343:            {
                    344:                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    345:
1.13      mycroft   346:                if (nam->m_len != sizeof(*addr)) {
                    347:                        error = EINVAL;
                    348:                        break;
                    349:                }
1.20      mycroft   350:                if (ifnet.tqh_first == 0) {
1.13      mycroft   351:                        error = EADDRNOTAVAIL;
                    352:                        break;
                    353:                }
1.1       cgd       354:                if ((addr->sin_family != AF_INET) &&
1.13      mycroft   355:                     (addr->sin_family != AF_IMPLINK)) {
                    356:                        error = EAFNOSUPPORT;
                    357:                        break;
                    358:                }
                    359:                inp->inp_faddr = addr->sin_addr;
1.1       cgd       360:                soisconnected(so);
1.13      mycroft   361:                break;
                    362:            }
                    363:
                    364:        case PRU_CONNECT2:
                    365:                error = EOPNOTSUPP;
                    366:                break;
                    367:
                    368:        /*
                    369:         * Mark the connection as being incapable of further input.
                    370:         */
                    371:        case PRU_SHUTDOWN:
                    372:                socantsendmore(so);
                    373:                break;
                    374:
                    375:        /*
                    376:         * Ship a packet out.  The appropriate raw output
                    377:         * routine handles any massaging necessary.
                    378:         */
                    379:        case PRU_SEND:
                    380:            {
1.17      cgd       381:                register u_int32_t dst;
1.13      mycroft   382:
                    383:                if (so->so_state & SS_ISCONNECTED) {
                    384:                        if (nam) {
                    385:                                error = EISCONN;
                    386:                                break;
                    387:                        }
                    388:                        dst = inp->inp_faddr.s_addr;
                    389:                } else {
                    390:                        if (nam == NULL) {
                    391:                                error = ENOTCONN;
                    392:                                break;
                    393:                        }
                    394:                        dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
                    395:                }
                    396:                error = rip_output(m, so, dst);
                    397:                m = NULL;
                    398:                break;
                    399:            }
                    400:
                    401:        case PRU_SENSE:
                    402:                /*
                    403:                 * stat: don't bother with a blocksize.
                    404:                 */
1.1       cgd       405:                return (0);
1.13      mycroft   406:
                    407:        /*
                    408:         * Not supported.
                    409:         */
                    410:        case PRU_RCVOOB:
                    411:        case PRU_RCVD:
                    412:        case PRU_LISTEN:
                    413:        case PRU_ACCEPT:
                    414:        case PRU_SENDOOB:
                    415:                error = EOPNOTSUPP;
                    416:                break;
                    417:
                    418:        case PRU_SOCKADDR:
                    419:                in_setsockaddr(inp, nam);
                    420:                break;
                    421:
                    422:        case PRU_PEERADDR:
                    423:                in_setpeeraddr(inp, nam);
                    424:                break;
                    425:
                    426:        default:
                    427:                panic("rip_usrreq");
1.1       cgd       428:        }
1.22      pk        429: release:
1.13      mycroft   430:        if (m != NULL)
                    431:                m_freem(m);
1.1       cgd       432:        return (error);
                    433: }

CVSweb <webmaster@jp.NetBSD.org>