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

1.107.4.1! wrstuden    1: /*     $NetBSD: raw_ip.c,v 1.107 2008/04/24 11:38:38 ad Exp $  */
1.43      itojun      2:
                      3: /*
                      4:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      5:  * All rights reserved.
1.61      itojun      6:  *
1.43      itojun      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. Neither the name of the project nor the names of its contributors
                     16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
1.61      itojun     18:  *
1.43      itojun     19:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
1.14      cgd        31:
1.1       cgd        32: /*
1.13      mycroft    33:  * Copyright (c) 1982, 1986, 1988, 1993
                     34:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
1.71      agc        44:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.39      thorpej    60:  *     @(#)raw_ip.c    8.7 (Berkeley) 5/15/95
1.1       cgd        61:  */
1.59      lukem      62:
                     63: #include <sys/cdefs.h>
1.107.4.1! wrstuden   64: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.107 2008/04/24 11:38:38 ad Exp $");
1.40      scottr     65:
1.78      jonathan   66: #include "opt_inet.h"
1.45      thorpej    67: #include "opt_ipsec.h"
1.40      scottr     68: #include "opt_mrouting.h"
1.1       cgd        69:
1.7       mycroft    70: #include <sys/param.h>
1.84      atatat     71: #include <sys/sysctl.h>
1.7       mycroft    72: #include <sys/malloc.h>
                     73: #include <sys/mbuf.h>
                     74: #include <sys/socket.h>
                     75: #include <sys/protosw.h>
                     76: #include <sys/socketvar.h>
                     77: #include <sys/errno.h>
1.13      mycroft    78: #include <sys/systm.h>
1.26      mycroft    79: #include <sys/proc.h>
1.89      elad       80: #include <sys/kauth.h>
1.1       cgd        81:
1.7       mycroft    82: #include <net/if.h>
                     83: #include <net/route.h>
1.1       cgd        84:
1.7       mycroft    85: #include <netinet/in.h>
                     86: #include <netinet/in_systm.h>
                     87: #include <netinet/ip.h>
                     88: #include <netinet/ip_var.h>
1.105     thorpej    89: #include <netinet/ip_private.h>
1.13      mycroft    90: #include <netinet/ip_mroute.h>
1.44      darrenr    91: #include <netinet/ip_icmp.h>
1.7       mycroft    92: #include <netinet/in_pcb.h>
1.87      yamt       93: #include <netinet/in_proto.h>
1.24      christos   94: #include <netinet/in_var.h>
                     95:
                     96: #include <machine/stdarg.h>
1.13      mycroft    97:
1.43      itojun     98: #ifdef IPSEC
                     99: #include <netinet6/ipsec.h>
1.106     thorpej   100: #include <netinet6/ipsec_private.h>
                    101: #endif /* IPSEC */
1.43      itojun    102:
1.72      jonathan  103: #ifdef FAST_IPSEC
                    104: #include <netipsec/ipsec.h>
1.106     thorpej   105: #include <netipsec/ipsec_var.h>
                    106: #include <netipsec/ipsec_private.h>
                    107: #endif /* FAST_IPSEC */
1.72      jonathan  108:
1.20      mycroft   109: struct inpcbtable rawcbtable;
1.30      pk        110:
1.82      perry     111: int     rip_pcbnotify(struct inpcbtable *, struct in_addr,
                    112:     struct in_addr, int, int, void (*)(struct inpcb *, int));
                    113: int     rip_bind(struct inpcb *, struct mbuf *);
                    114: int     rip_connect(struct inpcb *, struct mbuf *);
                    115: void    rip_disconnect(struct inpcb *);
1.13      mycroft   116:
                    117: /*
                    118:  * Nominal space allocated to a raw ip socket.
                    119:  */
                    120: #define        RIPSNDQ         8192
                    121: #define        RIPRCVQ         8192
1.1       cgd       122:
                    123: /*
                    124:  * Raw interface to IP protocol.
                    125:  */
1.13      mycroft   126:
                    127: /*
                    128:  * Initialize raw connection block q.
                    129:  */
                    130: void
1.83      perry     131: rip_init(void)
1.13      mycroft   132: {
                    133:
1.33      mycroft   134:        in_pcbinit(&rawcbtable, 1, 1);
1.13      mycroft   135: }
                    136:
1.100     dyoung    137: static void
                    138: rip_sbappendaddr(struct inpcb *last, struct ip *ip, const struct sockaddr *sa,
                    139:     int hlen, struct mbuf *opts, struct mbuf *n)
                    140: {
                    141:        if (last->inp_flags & INP_NOHEADER)
                    142:                m_adj(n, hlen);
                    143:        if (last->inp_flags & INP_CONTROLOPTS ||
                    144:            last->inp_socket->so_options & SO_TIMESTAMP)
                    145:                ip_savecontrol(last, &opts, ip, n);
                    146:        if (sbappendaddr(&last->inp_socket->so_rcv, sa, n, opts) == 0) {
                    147:                /* should notify about lost packet */
                    148:                m_freem(n);
                    149:                if (opts)
                    150:                        m_freem(opts);
                    151:        } else
                    152:                sorwakeup(last->inp_socket);
                    153: }
                    154:
1.1       cgd       155: /*
                    156:  * Setup generic address and protocol structures
                    157:  * for raw_input routine, then pass them along with
                    158:  * mbuf chain.
                    159:  */
1.9       mycroft   160: void
1.24      christos  161: rip_input(struct mbuf *m, ...)
1.1       cgd       162: {
1.100     dyoung    163:        int hlen, proto;
1.53      augustss  164:        struct ip *ip = mtod(m, struct ip *);
1.75      itojun    165:        struct inpcb_hdr *inph;
1.53      augustss  166:        struct inpcb *inp;
1.97      dyoung    167:        struct inpcb *last = NULL;
                    168:        struct mbuf *n, *opts = NULL;
1.32      mycroft   169:        struct sockaddr_in ripsrc;
1.43      itojun    170:        va_list ap;
                    171:
                    172:        va_start(ap, m);
1.64      simonb    173:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.43      itojun    174:        proto = va_arg(ap, int);
                    175:        va_end(ap);
1.1       cgd       176:
1.97      dyoung    177:        sockaddr_in_init(&ripsrc, &ip->ip_src, 0);
1.42      thorpej   178:
                    179:        /*
                    180:         * XXX Compatibility: programs using raw IP expect ip_len
1.62      itojun    181:         * XXX to have the header length subtracted, and in host order.
                    182:         * XXX ip_off is also expected to be host order.
1.42      thorpej   183:         */
1.100     dyoung    184:        hlen = ip->ip_hl << 2;
                    185:        ip->ip_len = ntohs(ip->ip_len) - hlen;
1.62      itojun    186:        NTOHS(ip->ip_off);
1.32      mycroft   187:
1.75      itojun    188:        CIRCLEQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
                    189:                inp = (struct inpcb *)inph;
                    190:                if (inp->inp_af != AF_INET)
                    191:                        continue;
1.43      itojun    192:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
1.13      mycroft   193:                        continue;
1.32      mycroft   194:                if (!in_nullhost(inp->inp_laddr) &&
                    195:                    !in_hosteq(inp->inp_laddr, ip->ip_dst))
1.13      mycroft   196:                        continue;
1.32      mycroft   197:                if (!in_nullhost(inp->inp_faddr) &&
                    198:                    !in_hosteq(inp->inp_faddr, ip->ip_src))
1.13      mycroft   199:                        continue;
1.97      dyoung    200:                if (last == NULL)
                    201:                        ;
1.72      jonathan  202: #if defined(IPSEC) || defined(FAST_IPSEC)
1.97      dyoung    203:                /* check AH/ESP integrity. */
                    204:                else if (ipsec4_in_reject_so(m, last->inp_socket)) {
1.106     thorpej   205:                        IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.97      dyoung    206:                        /* do not inject data to pcb */
                    207:                }
                    208: #endif /*IPSEC*/
1.99      dyoung    209:                else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.100     dyoung    210:                        rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
                    211:                            n);
1.97      dyoung    212:                        opts = NULL;
1.13      mycroft   213:                }
1.36      thorpej   214:                last = inp;
1.13      mycroft   215:        }
1.72      jonathan  216: #if defined(IPSEC) || defined(FAST_IPSEC)
1.55      itojun    217:        /* check AH/ESP integrity. */
1.97      dyoung    218:        if (last != NULL && ipsec4_in_reject_so(m, last->inp_socket)) {
1.55      itojun    219:                m_freem(m);
1.106     thorpej   220:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.105     thorpej   221:                IP_STATDEC(IP_STAT_DELIVERED);
1.55      itojun    222:                /* do not inject data to pcb */
                    223:        } else
                    224: #endif /*IPSEC*/
1.100     dyoung    225:        if (last != NULL)
                    226:                rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
                    227:        else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
1.105     thorpej   228:                uint64_t *ips;
                    229:
1.97      dyoung    230:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
                    231:                    0, 0);
1.105     thorpej   232:                ips = IP_STAT_GETREF();
                    233:                ips[IP_STAT_NOPROTO]++;
                    234:                ips[IP_STAT_DELIVERED]--;
                    235:                IP_STAT_PUTREF();
1.97      dyoung    236:        } else
                    237:                m_freem(m);
1.43      itojun    238:        return;
1.60      itojun    239: }
                    240:
                    241: int
1.83      perry     242: rip_pcbnotify(struct inpcbtable *table,
                    243:     struct in_addr faddr, struct in_addr laddr, int proto, int errno,
                    244:     void (*notify)(struct inpcb *, int))
1.60      itojun    245: {
                    246:        struct inpcb *inp, *ninp;
                    247:        int nmatch;
                    248:
                    249:        nmatch = 0;
1.75      itojun    250:        for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
1.60      itojun    251:            inp != (struct inpcb *)&table->inpt_queue;
                    252:            inp = ninp) {
1.75      itojun    253:                ninp = (struct inpcb *)inp->inp_queue.cqe_next;
                    254:                if (inp->inp_af != AF_INET)
                    255:                        continue;
1.60      itojun    256:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
                    257:                        continue;
                    258:                if (in_hosteq(inp->inp_faddr, faddr) &&
                    259:                    in_hosteq(inp->inp_laddr, laddr)) {
                    260:                        (*notify)(inp, errno);
                    261:                        nmatch++;
                    262:                }
                    263:        }
                    264:
                    265:        return nmatch;
                    266: }
                    267:
                    268: void *
1.95      dyoung    269: rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.60      itojun    270: {
                    271:        struct ip *ip = v;
1.82      perry     272:        void (*notify)(struct inpcb *, int) = in_rtchange;
1.60      itojun    273:        int errno;
                    274:
                    275:        if (sa->sa_family != AF_INET ||
                    276:            sa->sa_len != sizeof(struct sockaddr_in))
                    277:                return NULL;
                    278:        if ((unsigned)cmd >= PRC_NCMDS)
                    279:                return NULL;
                    280:        errno = inetctlerrmap[cmd];
                    281:        if (PRC_IS_REDIRECT(cmd))
                    282:                notify = in_rtchange, ip = 0;
                    283:        else if (cmd == PRC_HOSTDEAD)
                    284:                ip = 0;
                    285:        else if (errno == 0)
                    286:                return NULL;
                    287:        if (ip) {
1.95      dyoung    288:                rip_pcbnotify(&rawcbtable, satocsin(sa)->sin_addr,
1.60      itojun    289:                    ip->ip_src, ip->ip_p, errno, notify);
                    290:
                    291:                /* XXX mapped address case */
                    292:        } else
1.95      dyoung    293:                in_pcbnotifyall(&rawcbtable, satocsin(sa)->sin_addr, errno,
1.60      itojun    294:                    notify);
                    295:        return NULL;
1.1       cgd       296: }
                    297:
                    298: /*
                    299:  * Generate IP header and pass packet to ip_output.
                    300:  * Tack on options user may have setup with control call.
                    301:  */
1.9       mycroft   302: int
1.24      christos  303: rip_output(struct mbuf *m, ...)
                    304: {
1.53      augustss  305:        struct inpcb *inp;
                    306:        struct ip *ip;
1.10      mycroft   307:        struct mbuf *opts;
1.24      christos  308:        int flags;
                    309:        va_list ap;
                    310:
                    311:        va_start(ap, m);
1.27      mycroft   312:        inp = va_arg(ap, struct inpcb *);
1.24      christos  313:        va_end(ap);
                    314:
1.27      mycroft   315:        flags =
1.37      matt      316:            (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
                    317:            | IP_RETURNMTU;
1.1       cgd       318:
                    319:        /*
                    320:         * If the user handed us a complete IP packet, use it.
                    321:         * Otherwise, allocate an mbuf for a header and fill it in.
                    322:         */
1.13      mycroft   323:        if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.35      thorpej   324:                if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
                    325:                        m_freem(m);
                    326:                        return (EMSGSIZE);
                    327:                }
1.68      itojun    328:                M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
                    329:                if (!m)
                    330:                        return (ENOBUFS);
1.1       cgd       331:                ip = mtod(m, struct ip *);
                    332:                ip->ip_tos = 0;
1.62      itojun    333:                ip->ip_off = htons(0);
1.13      mycroft   334:                ip->ip_p = inp->inp_ip.ip_p;
1.62      itojun    335:                ip->ip_len = htons(m->m_pkthdr.len);
1.13      mycroft   336:                ip->ip_src = inp->inp_laddr;
1.27      mycroft   337:                ip->ip_dst = inp->inp_faddr;
1.1       cgd       338:                ip->ip_ttl = MAXTTL;
1.13      mycroft   339:                opts = inp->inp_options;
                    340:        } else {
1.35      thorpej   341:                if (m->m_pkthdr.len > IP_MAXPACKET) {
                    342:                        m_freem(m);
                    343:                        return (EMSGSIZE);
                    344:                }
1.13      mycroft   345:                ip = mtod(m, struct ip *);
1.65      thorpej   346:
                    347:                /*
                    348:                 * If the mbuf is read-only, we need to allocate
                    349:                 * a new mbuf for the header, since we need to
                    350:                 * modify the header.
                    351:                 */
                    352:                if (M_READONLY(m)) {
                    353:                        int hlen = ip->ip_hl << 2;
                    354:
                    355:                        m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
                    356:                        if (m == NULL)
                    357:                                return (ENOMEM);        /* XXX */
                    358:                        ip = mtod(m, struct ip *);
                    359:                }
                    360:
1.62      itojun    361:                /* XXX userland passes ip_len and ip_off in host order */
1.38      mycroft   362:                if (m->m_pkthdr.len != ip->ip_len) {
                    363:                        m_freem(m);
                    364:                        return (EINVAL);
                    365:                }
1.62      itojun    366:                HTONS(ip->ip_len);
                    367:                HTONS(ip->ip_off);
1.103     matt      368:                if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
                    369:                        flags |= IP_NOIPNEWID;
1.13      mycroft   370:                opts = NULL;
                    371:                /* XXX prevent ip_output from overwriting header fields */
                    372:                flags |= IP_RAWOUTPUT;
1.105     thorpej   373:                IP_STATINC(IP_STAT_RAWOUT);
1.1       cgd       374:        }
1.62      itojun    375:        return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
1.73      itojun    376:             inp->inp_socket, &inp->inp_errormtu));
1.1       cgd       377: }
                    378:
                    379: /*
                    380:  * Raw IP socket option processing.
                    381:  */
1.9       mycroft   382: int
1.107.4.1! wrstuden  383: rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.1       cgd       384: {
1.53      augustss  385:        struct inpcb *inp = sotoinpcb(so);
1.31      mycroft   386:        int error = 0;
1.107.4.1! wrstuden  387:        int optval;
1.1       cgd       388:
1.107.4.1! wrstuden  389:        if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
1.100     dyoung    390:                if (op == PRCO_GETOPT) {
1.107.4.1! wrstuden  391:                        optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
        !           392:                        error = sockopt_set(sopt, &optval, sizeof(optval));
        !           393:                } else if (op == PRCO_SETOPT) {
        !           394:                        error = sockopt_getint(sopt, &optval);
        !           395:                        if (error)
        !           396:                                goto out;
        !           397:                        if (optval) {
        !           398:                                inp->inp_flags &= ~INP_HDRINCL;
        !           399:                                inp->inp_flags |= INP_NOHEADER;
        !           400:                        } else
        !           401:                                inp->inp_flags &= ~INP_NOHEADER;
        !           402:                }
        !           403:                goto out;
        !           404:        } else if (sopt->sopt_level != IPPROTO_IP)
        !           405:                return ip_ctloutput(op, so, sopt);
1.100     dyoung    406:
                    407:        switch (op) {
1.31      mycroft   408:
                    409:        case PRCO_SETOPT:
1.107.4.1! wrstuden  410:                switch (sopt->sopt_name) {
1.31      mycroft   411:                case IP_HDRINCL:
1.107.4.1! wrstuden  412:                        error = sockopt_getint(sopt, &optval);
        !           413:                        if (error)
        !           414:                                break;
        !           415:                        if (optval)
1.100     dyoung    416:                                inp->inp_flags |= INP_HDRINCL;
                    417:                        else
                    418:                                inp->inp_flags &= ~INP_HDRINCL;
1.107.4.1! wrstuden  419:                        break;
1.31      mycroft   420:
                    421: #ifdef MROUTING
                    422:                case MRT_INIT:
                    423:                case MRT_DONE:
                    424:                case MRT_ADD_VIF:
                    425:                case MRT_DEL_VIF:
                    426:                case MRT_ADD_MFC:
                    427:                case MRT_DEL_MFC:
                    428:                case MRT_ASSERT:
1.81      manu      429:                case MRT_API_CONFIG:
                    430:                case MRT_ADD_BW_UPCALL:
                    431:                case MRT_DEL_BW_UPCALL:
1.107.4.1! wrstuden  432:                        error = ip_mrouter_set(so, sopt);
1.31      mycroft   433:                        break;
                    434: #endif
                    435:
                    436:                default:
1.107.4.1! wrstuden  437:                        error = ip_ctloutput(op, so, sopt);
1.31      mycroft   438:                        break;
1.13      mycroft   439:                }
                    440:                break;
1.1       cgd       441:
1.31      mycroft   442:        case PRCO_GETOPT:
1.107.4.1! wrstuden  443:                switch (sopt->sopt_name) {
1.31      mycroft   444:                case IP_HDRINCL:
1.107.4.1! wrstuden  445:                        optval = inp->inp_flags & INP_HDRINCL;
        !           446:                        error = sockopt_set(sopt, &optval, sizeof(optval));
1.31      mycroft   447:                        break;
                    448:
1.6       hpeyerl   449: #ifdef MROUTING
1.31      mycroft   450:                case MRT_VERSION:
                    451:                case MRT_ASSERT:
1.81      manu      452:                case MRT_API_SUPPORT:
                    453:                case MRT_API_CONFIG:
1.107.4.1! wrstuden  454:                        error = ip_mrouter_get(so, sopt);
1.18      mycroft   455:                        break;
1.31      mycroft   456: #endif
                    457:
1.18      mycroft   458:                default:
1.107.4.1! wrstuden  459:                        error = ip_ctloutput(op, so, sopt);
1.18      mycroft   460:                        break;
                    461:                }
1.31      mycroft   462:                break;
1.1       cgd       463:        }
1.107.4.1! wrstuden  464:  out:
1.100     dyoung    465:        return error;
1.1       cgd       466: }
                    467:
1.27      mycroft   468: int
1.83      perry     469: rip_bind(struct inpcb *inp, struct mbuf *nam)
1.29      mycroft   470: {
                    471:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    472:
                    473:        if (nam->m_len != sizeof(*addr))
                    474:                return (EINVAL);
1.58      matt      475:        if (TAILQ_FIRST(&ifnet) == 0)
1.29      mycroft   476:                return (EADDRNOTAVAIL);
                    477:        if (addr->sin_family != AF_INET &&
                    478:            addr->sin_family != AF_IMPLINK)
                    479:                return (EAFNOSUPPORT);
1.32      mycroft   480:        if (!in_nullhost(addr->sin_addr) &&
1.29      mycroft   481:            ifa_ifwithaddr(sintosa(addr)) == 0)
                    482:                return (EADDRNOTAVAIL);
                    483:        inp->inp_laddr = addr->sin_addr;
                    484:        return (0);
                    485: }
                    486:
                    487: int
1.83      perry     488: rip_connect(struct inpcb *inp, struct mbuf *nam)
1.27      mycroft   489: {
                    490:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    491:
                    492:        if (nam->m_len != sizeof(*addr))
                    493:                return (EINVAL);
1.58      matt      494:        if (TAILQ_FIRST(&ifnet) == 0)
1.27      mycroft   495:                return (EADDRNOTAVAIL);
                    496:        if (addr->sin_family != AF_INET &&
                    497:            addr->sin_family != AF_IMPLINK)
                    498:                return (EAFNOSUPPORT);
                    499:        inp->inp_faddr = addr->sin_addr;
                    500:        return (0);
                    501: }
                    502:
                    503: void
1.83      perry     504: rip_disconnect(struct inpcb *inp)
1.27      mycroft   505: {
                    506:
1.32      mycroft   507:        inp->inp_faddr = zeroin_addr;
1.27      mycroft   508: }
                    509:
1.13      mycroft   510: u_long rip_sendspace = RIPSNDQ;
                    511: u_long rip_recvspace = RIPRCVQ;
                    512:
1.1       cgd       513: /*ARGSUSED*/
1.9       mycroft   514: int
1.83      perry     515: rip_usrreq(struct socket *so, int req,
1.88      christos  516:     struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
1.1       cgd       517: {
1.53      augustss  518:        struct inpcb *inp;
1.27      mycroft   519:        int s;
1.53      augustss  520:        int error = 0;
1.13      mycroft   521: #ifdef MROUTING
1.6       hpeyerl   522:        extern struct socket *ip_mrouter;
                    523: #endif
1.27      mycroft   524:
1.22      pk        525:        if (req == PRU_CONTROL)
1.96      christos  526:                return (in_control(so, (long)m, (void *)nam,
1.90      ad        527:                    (struct ifnet *)control, l));
1.49      thorpej   528:
1.93      tls       529:        s = splsoftnet();
                    530:
1.50      thorpej   531:        if (req == PRU_PURGEIF) {
1.107     ad        532:                mutex_enter(softnet_lock);
1.56      itojun    533:                in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50      thorpej   534:                in_purgeif((struct ifnet *)control);
                    535:                in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.107     ad        536:                mutex_exit(softnet_lock);
1.93      tls       537:                splx(s);
1.49      thorpej   538:                return (0);
                    539:        }
1.22      pk        540:
1.27      mycroft   541:        inp = sotoinpcb(so);
1.28      mycroft   542: #ifdef DIAGNOSTIC
                    543:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    544:                panic("rip_usrreq: unexpected control mbuf");
                    545: #endif
1.27      mycroft   546:        if (inp == 0 && req != PRU_ATTACH) {
1.22      pk        547:                error = EINVAL;
                    548:                goto release;
                    549:        }
                    550:
1.1       cgd       551:        switch (req) {
                    552:
                    553:        case PRU_ATTACH:
1.107     ad        554:                sosetlock(so);
1.27      mycroft   555:                if (inp != 0) {
                    556:                        error = EISCONN;
                    557:                        break;
                    558:                }
1.94      elad      559:
                    560:                if (l == NULL) {
1.13      mycroft   561:                        error = EACCES;
                    562:                        break;
                    563:                }
1.94      elad      564:
                    565:                /* XXX: raw socket permissions are checked in socreate() */
                    566:
1.27      mycroft   567:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    568:                        error = soreserve(so, rip_sendspace, rip_recvspace);
                    569:                        if (error)
                    570:                                break;
                    571:                }
                    572:                error = in_pcballoc(so, &rawcbtable);
                    573:                if (error)
1.13      mycroft   574:                        break;
1.27      mycroft   575:                inp = sotoinpcb(so);
1.17      cgd       576:                inp->inp_ip.ip_p = (long)nam;
1.1       cgd       577:                break;
                    578:
                    579:        case PRU_DETACH:
1.13      mycroft   580: #ifdef MROUTING
1.6       hpeyerl   581:                if (so == ip_mrouter)
                    582:                        ip_mrouter_done();
                    583: #endif
1.13      mycroft   584:                in_pcbdetach(inp);
1.1       cgd       585:                break;
                    586:
                    587:        case PRU_BIND:
1.29      mycroft   588:                error = rip_bind(inp, nam);
1.27      mycroft   589:                break;
                    590:
                    591:        case PRU_LISTEN:
                    592:                error = EOPNOTSUPP;
                    593:                break;
                    594:
                    595:        case PRU_CONNECT:
                    596:                error = rip_connect(inp, nam);
                    597:                if (error)
1.13      mycroft   598:                        break;
1.1       cgd       599:                soisconnected(so);
1.13      mycroft   600:                break;
                    601:
                    602:        case PRU_CONNECT2:
                    603:                error = EOPNOTSUPP;
                    604:                break;
                    605:
1.27      mycroft   606:        case PRU_DISCONNECT:
                    607:                soisdisconnected(so);
                    608:                rip_disconnect(inp);
                    609:                break;
                    610:
1.13      mycroft   611:        /*
                    612:         * Mark the connection as being incapable of further input.
                    613:         */
                    614:        case PRU_SHUTDOWN:
                    615:                socantsendmore(so);
                    616:                break;
                    617:
1.27      mycroft   618:        case PRU_RCVD:
                    619:                error = EOPNOTSUPP;
                    620:                break;
                    621:
1.13      mycroft   622:        /*
                    623:         * Ship a packet out.  The appropriate raw output
                    624:         * routine handles any massaging necessary.
                    625:         */
                    626:        case PRU_SEND:
1.28      mycroft   627:                if (control && control->m_len) {
                    628:                        m_freem(control);
                    629:                        m_freem(m);
                    630:                        error = EINVAL;
                    631:                        break;
                    632:                }
                    633:        {
1.27      mycroft   634:                if (nam) {
                    635:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13      mycroft   636:                                error = EISCONN;
1.28      mycroft   637:                                goto die;
1.13      mycroft   638:                        }
1.27      mycroft   639:                        error = rip_connect(inp, nam);
                    640:                        if (error) {
1.28      mycroft   641:                        die:
1.27      mycroft   642:                                m_freem(m);
                    643:                                break;
                    644:                        }
1.13      mycroft   645:                } else {
1.27      mycroft   646:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13      mycroft   647:                                error = ENOTCONN;
1.28      mycroft   648:                                goto die;
1.13      mycroft   649:                        }
                    650:                }
1.27      mycroft   651:                error = rip_output(m, inp);
                    652:                if (nam)
                    653:                        rip_disconnect(inp);
1.28      mycroft   654:        }
1.13      mycroft   655:                break;
                    656:
                    657:        case PRU_SENSE:
                    658:                /*
                    659:                 * stat: don't bother with a blocksize.
                    660:                 */
1.27      mycroft   661:                splx(s);
1.1       cgd       662:                return (0);
1.13      mycroft   663:
                    664:        case PRU_RCVOOB:
1.27      mycroft   665:                error = EOPNOTSUPP;
                    666:                break;
                    667:
1.13      mycroft   668:        case PRU_SENDOOB:
1.28      mycroft   669:                m_freem(control);
1.27      mycroft   670:                m_freem(m);
1.13      mycroft   671:                error = EOPNOTSUPP;
                    672:                break;
                    673:
                    674:        case PRU_SOCKADDR:
                    675:                in_setsockaddr(inp, nam);
                    676:                break;
                    677:
                    678:        case PRU_PEERADDR:
                    679:                in_setpeeraddr(inp, nam);
                    680:                break;
                    681:
                    682:        default:
                    683:                panic("rip_usrreq");
1.1       cgd       684:        }
1.27      mycroft   685:
1.22      pk        686: release:
1.27      mycroft   687:        splx(s);
1.1       cgd       688:        return (error);
                    689: }
1.84      atatat    690:
                    691: SYSCTL_SETUP(sysctl_net_inet_raw_setup, "sysctl net.inet.raw subtree setup")
                    692: {
                    693:
                    694:        sysctl_createv(clog, 0, NULL, NULL,
                    695:                       CTLFLAG_PERMANENT,
                    696:                       CTLTYPE_NODE, "net", NULL,
                    697:                       NULL, 0, NULL, 0,
                    698:                       CTL_NET, CTL_EOL);
                    699:        sysctl_createv(clog, 0, NULL, NULL,
                    700:                       CTLFLAG_PERMANENT,
                    701:                       CTLTYPE_NODE, "inet", NULL,
                    702:                       NULL, 0, NULL, 0,
                    703:                       CTL_NET, PF_INET, CTL_EOL);
                    704:        sysctl_createv(clog, 0, NULL, NULL,
                    705:                       CTLFLAG_PERMANENT,
                    706:                       CTLTYPE_NODE, "raw",
                    707:                       SYSCTL_DESCR("Raw IPv4 settings"),
                    708:                       NULL, 0, NULL, 0,
                    709:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
                    710:
                    711:        sysctl_createv(clog, 0, NULL, NULL,
                    712:                       CTLFLAG_PERMANENT,
1.86      atatat    713:                       CTLTYPE_STRUCT, "pcblist",
1.84      atatat    714:                       SYSCTL_DESCR("Raw IPv4 control block list"),
                    715:                       sysctl_inpcblist, 0, &rawcbtable, 0,
                    716:                       CTL_NET, PF_INET, IPPROTO_RAW,
                    717:                       CTL_CREATE, CTL_EOL);
                    718: }

CVSweb <webmaster@jp.NetBSD.org>