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

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

CVSweb <webmaster@jp.NetBSD.org>