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

1.116.2.1! rmind       1: /*     $NetBSD: raw_ip.c,v 1.116 2013/06/05 19:01:26 christos 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:
1.116.2.1! rmind      63: /*
        !            64:  * Raw interface to IP protocol.
        !            65:  */
        !            66:
1.59      lukem      67: #include <sys/cdefs.h>
1.116.2.1! rmind      68: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.116 2013/06/05 19:01:26 christos Exp $");
1.40      scottr     69:
1.78      jonathan   70: #include "opt_inet.h"
1.109     christos   71: #include "opt_compat_netbsd.h"
1.45      thorpej    72: #include "opt_ipsec.h"
1.40      scottr     73: #include "opt_mrouting.h"
1.1       cgd        74:
1.7       mycroft    75: #include <sys/param.h>
1.84      atatat     76: #include <sys/sysctl.h>
1.7       mycroft    77: #include <sys/mbuf.h>
                     78: #include <sys/socket.h>
                     79: #include <sys/protosw.h>
                     80: #include <sys/socketvar.h>
1.13      mycroft    81: #include <sys/systm.h>
1.89      elad       82: #include <sys/kauth.h>
1.1       cgd        83:
1.7       mycroft    84: #include <net/if.h>
                     85: #include <net/route.h>
1.1       cgd        86:
1.7       mycroft    87: #include <netinet/in.h>
                     88: #include <netinet/in_systm.h>
                     89: #include <netinet/ip.h>
                     90: #include <netinet/ip_var.h>
1.105     thorpej    91: #include <netinet/ip_private.h>
1.13      mycroft    92: #include <netinet/ip_mroute.h>
1.44      darrenr    93: #include <netinet/ip_icmp.h>
1.7       mycroft    94: #include <netinet/in_pcb.h>
1.87      yamt       95: #include <netinet/in_proto.h>
1.24      christos   96: #include <netinet/in_var.h>
                     97:
1.116     christos   98: #ifdef IPSEC
1.72      jonathan   99: #include <netipsec/ipsec.h>
1.106     thorpej   100: #include <netipsec/ipsec_var.h>
                    101: #include <netipsec/ipsec_private.h>
1.116.2.1! rmind     102: #endif
1.72      jonathan  103:
1.109     christos  104: #ifdef COMPAT_50
                    105: #include <compat/sys/socket.h>
                    106: #endif
                    107:
1.116.2.1! rmind     108: static inpcbtable_t *  rawcbtable __read_mostly;
1.13      mycroft   109:
1.116.2.1! rmind     110: static void            sysctl_net_inet_raw_setup(struct sysctllog **);
1.110     pooka     111:
1.13      mycroft   112: /*
                    113:  * Nominal space allocated to a raw ip socket.
                    114:  */
                    115: #define        RIPSNDQ         8192
                    116: #define        RIPRCVQ         8192
1.1       cgd       117:
1.116.2.1! rmind     118: static u_long          rip_sendspace = RIPSNDQ;
        !           119: static u_long          rip_recvspace = RIPRCVQ;
        !           120:
        !           121: struct rip_input_ctx {
        !           122:        struct mbuf *           mbuf;
        !           123:        struct ip *             ip;
        !           124:        struct sockaddr_in      src;
        !           125:        unsigned                hlen;
        !           126:        unsigned                nfound;
        !           127: };
        !           128:
        !           129: struct rip_ctlinput_ctx {
        !           130:        struct ip *             ip;
        !           131:        struct in_addr          addr;
        !           132:        int                     errno;
        !           133: };
1.13      mycroft   134:
                    135: void
1.83      perry     136: rip_init(void)
1.13      mycroft   137: {
1.116.2.1! rmind     138:        rawcbtable = inpcb_init(1, 1, 0);
1.110     pooka     139:        sysctl_net_inet_raw_setup(NULL);
1.13      mycroft   140: }
                    141:
1.116.2.1! rmind     142: /*
        !           143:  * rip_append: pass the received datagram to the process.
        !           144:  */
1.100     dyoung    145: static void
1.116.2.1! rmind     146: rip_append(inpcb_t *inp, struct rip_input_ctx *rctx)
1.100     dyoung    147: {
1.116.2.1! rmind     148:        struct socket *so = inpcb_get_socket(inp);
        !           149:        int inpflags = inpcb_get_flags(inp);
        !           150:        struct mbuf *n, *opts = NULL;
        !           151:
        !           152:        /* XXX: Might optimise this, but not with a silly loop! */
        !           153:        if ((n = m_copypacket(rctx->mbuf, M_DONTWAIT)) == NULL) {
        !           154:                return;
        !           155:        }
        !           156:
        !           157:        if (inpflags & INP_NOHEADER) {
        !           158:                m_adj(n, rctx->hlen);
        !           159:        }
        !           160:
        !           161:        if ((inpflags & INP_CONTROLOPTS) != 0
1.109     christos  162: #ifdef SO_OTIMESTAMP
1.116.2.1! rmind     163:            || (so->so_options & SO_OTIMESTAMP) != 0
1.109     christos  164: #endif
1.116.2.1! rmind     165:            || (so->so_options & SO_TIMESTAMP) != 0) {
        !           166:                struct ip *ip = rctx->ip;
        !           167:                ip_savecontrol(inp, &opts, ip, n);
        !           168:        }
        !           169:
        !           170:        if (sbappendaddr(&so->so_rcv, sintosa(&rctx->src), n, opts) == 0) {
        !           171:                /* Should notify about lost packet. */
        !           172:                if (opts) {
1.100     dyoung    173:                        m_freem(opts);
1.116.2.1! rmind     174:                }
        !           175:                m_freem(n);
        !           176:        } else {
        !           177:                sorwakeup(so);
        !           178:        }
        !           179: }
        !           180:
        !           181: static int
        !           182: rip_pcb_process(inpcb_t *inp, void *arg)
        !           183: {
        !           184:        struct rip_input_ctx *rctx = arg;
        !           185:        const struct ip *ip = rctx->ip;
        !           186:        struct ip *inp_ip = in_getiphdr(inp);
        !           187:        struct in_addr laddr, faddr;
        !           188:
        !           189:        if (inp_ip->ip_p && inp_ip->ip_p != ip->ip_p) {
        !           190:                return 0;
        !           191:        }
        !           192:        inpcb_get_addrs(inp, &laddr, &faddr);
        !           193:
        !           194:        if (!in_nullhost(laddr) && !in_hosteq(laddr, ip->ip_dst)) {
        !           195:                return 0;
        !           196:        }
        !           197:        if (!in_nullhost(faddr) && !in_hosteq(faddr, ip->ip_src)) {
        !           198:                return 0;
        !           199:        }
        !           200:
        !           201: #if defined(IPSEC)
        !           202:        /* Check AH/ESP integrity. */
        !           203:        if (ipsec4_in_reject_so(rctx->mbuf, inpcb_get_socket(inp))) {
        !           204:                /* Do not inject data into PCB. */
        !           205:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
        !           206:                return 0;
        !           207:        }
        !           208: #endif
        !           209:        rip_append(inp, rctx);
        !           210:        rctx->nfound++;
        !           211:        return 0;
1.100     dyoung    212: }
                    213:
1.9       mycroft   214: void
1.24      christos  215: rip_input(struct mbuf *m, ...)
1.1       cgd       216: {
1.53      augustss  217:        struct ip *ip = mtod(m, struct ip *);
1.116.2.1! rmind     218:        int error, hlen, proto;
1.43      itojun    219:        va_list ap;
                    220:
                    221:        va_start(ap, m);
1.64      simonb    222:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.43      itojun    223:        proto = va_arg(ap, int);
                    224:        va_end(ap);
1.1       cgd       225:
1.116.2.1! rmind     226:        KASSERTMSG((proto == ip->ip_p), "%s: protocol mismatch", __func__);
1.42      thorpej   227:
                    228:        /*
1.116.2.1! rmind     229:         * Compatibility: programs using raw IP expect ip_len field to have
        !           230:         * the header length subtracted.  Also, ip_len and ip_off fields are
        !           231:         * expected to be in host order.
1.42      thorpej   232:         */
1.100     dyoung    233:        hlen = ip->ip_hl << 2;
                    234:        ip->ip_len = ntohs(ip->ip_len) - hlen;
1.62      itojun    235:        NTOHS(ip->ip_off);
1.32      mycroft   236:
1.116.2.1! rmind     237:        /* Save some context for the iterator. */
        !           238:        struct rip_input_ctx rctx = {
        !           239:                .mbuf = m, .ip = ip, .hlen = hlen, .nfound = 0
        !           240:        };
        !           241:        sockaddr_in_init(&rctx.src, &ip->ip_src, 0);
        !           242:
        !           243:        /* Scan all raw IP PCBs for matching entries. */
        !           244:        error = inpcb_foreach(rawcbtable, AF_INET, rip_pcb_process, &rctx);
        !           245:        KASSERT(error == 0);
        !           246:
        !           247:        /* Done, if found any. */
        !           248:        if (rctx.nfound) {
        !           249:                return;
1.13      mycroft   250:        }
1.116.2.1! rmind     251:
        !           252:        if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
1.105     thorpej   253:                uint64_t *ips;
                    254:
1.116.2.1! rmind     255:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
1.105     thorpej   256:                ips = IP_STAT_GETREF();
                    257:                ips[IP_STAT_NOPROTO]++;
                    258:                ips[IP_STAT_DELIVERED]--;
                    259:                IP_STAT_PUTREF();
1.116.2.1! rmind     260:        } else {
1.97      dyoung    261:                m_freem(m);
1.116.2.1! rmind     262:        }
1.60      itojun    263: }
                    264:
1.116.2.1! rmind     265: static int
        !           266: rip_pcbnotify(inpcb_t *inp, void *arg)
        !           267: {
        !           268:        struct rip_ctlinput_ctx *rctx = arg;
        !           269:        const struct ip *ip = rctx->ip;
        !           270:        struct ip *inp_ip = in_getiphdr(inp);
        !           271:        struct in_addr laddr, faddr;
        !           272:
        !           273:        if (inp_ip->ip_p && inp_ip->ip_p != ip->ip_p) {
        !           274:                return 0;
1.60      itojun    275:        }
1.116.2.1! rmind     276:        inpcb_get_addrs(inp, &laddr, &faddr);
1.60      itojun    277:
1.116.2.1! rmind     278:        if (in_hosteq(faddr, rctx->addr) && in_hosteq(laddr, ip->ip_src)) {
        !           279:                inpcb_rtchange(inp, rctx->errno);
        !           280:        }
        !           281:        return 0;
1.60      itojun    282: }
                    283:
                    284: void *
1.95      dyoung    285: rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.60      itojun    286: {
                    287:        struct ip *ip = v;
                    288:        int errno;
                    289:
                    290:        if (sa->sa_family != AF_INET ||
                    291:            sa->sa_len != sizeof(struct sockaddr_in))
                    292:                return NULL;
                    293:        if ((unsigned)cmd >= PRC_NCMDS)
                    294:                return NULL;
                    295:        errno = inetctlerrmap[cmd];
1.116.2.1! rmind     296:
        !           297:        if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD || ip == NULL) {
        !           298:                inpcb_notifyall(rawcbtable, satocsin(sa)->sin_addr,
        !           299:                    errno, inpcb_rtchange);
1.60      itojun    300:                return NULL;
1.116.2.1! rmind     301:        } else if (errno == 0) {
        !           302:                return NULL;
        !           303:        }
        !           304:
        !           305:        /* Note: mapped address case. */
        !           306:        struct rip_ctlinput_ctx rctx = {
        !           307:                .ip = ip, .addr = satocsin(sa)->sin_addr, .errno = errno
        !           308:        };
        !           309:        (void)inpcb_foreach(rawcbtable, AF_INET, rip_pcbnotify, &rctx);
        !           310:
1.60      itojun    311:        return NULL;
1.1       cgd       312: }
                    313:
                    314: /*
1.116.2.1! rmind     315:  * Generate IP header and pass packet to the IP output routine.
1.1       cgd       316:  * Tack on options user may have setup with control call.
                    317:  */
1.9       mycroft   318: int
1.24      christos  319: rip_output(struct mbuf *m, ...)
                    320: {
1.116.2.1! rmind     321:        inpcb_t *inp;
        !           322:        struct socket *so;
1.53      augustss  323:        struct ip *ip;
1.10      mycroft   324:        struct mbuf *opts;
1.116.2.1! rmind     325:        int flags, inpflags;
1.24      christos  326:        va_list ap;
                    327:
                    328:        va_start(ap, m);
1.116.2.1! rmind     329:        inp = va_arg(ap, inpcb_t *);
1.24      christos  330:        va_end(ap);
                    331:
1.116.2.1! rmind     332:        so = inpcb_get_socket(inp);
        !           333:        KASSERT(solocked(so));
        !           334:
        !           335:        flags = (so->so_options & SO_DONTROUTE) |
        !           336:            IP_ALLOWBROADCAST | IP_RETURNMTU;
        !           337:        inpflags = inpcb_get_flags(inp);
1.1       cgd       338:
                    339:        /*
                    340:         * If the user handed us a complete IP packet, use it.
                    341:         * Otherwise, allocate an mbuf for a header and fill it in.
                    342:         */
1.116.2.1! rmind     343:        if ((inpflags & INP_HDRINCL) == 0) {
        !           344:                struct ip *inp_ip = in_getiphdr(inp);
        !           345:
1.35      thorpej   346:                if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
                    347:                        m_freem(m);
1.116.2.1! rmind     348:                        return EMSGSIZE;
1.35      thorpej   349:                }
1.68      itojun    350:                M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
1.116.2.1! rmind     351:                if (m == NULL) {
        !           352:                        return ENOBUFS;
        !           353:                }
1.1       cgd       354:                ip = mtod(m, struct ip *);
                    355:                ip->ip_tos = 0;
1.62      itojun    356:                ip->ip_off = htons(0);
1.116.2.1! rmind     357:                ip->ip_p = inp_ip->ip_p;
1.62      itojun    358:                ip->ip_len = htons(m->m_pkthdr.len);
1.116.2.1! rmind     359:                inpcb_get_addrs(inp, &ip->ip_src, &ip->ip_dst);
        !           360:
1.1       cgd       361:                ip->ip_ttl = MAXTTL;
1.116.2.1! rmind     362:                opts = inpcb_get_options(inp);
1.13      mycroft   363:        } else {
1.35      thorpej   364:                if (m->m_pkthdr.len > IP_MAXPACKET) {
                    365:                        m_freem(m);
1.116.2.1! rmind     366:                        return EMSGSIZE;
1.35      thorpej   367:                }
1.13      mycroft   368:                ip = mtod(m, struct ip *);
1.65      thorpej   369:
                    370:                /*
1.116.2.1! rmind     371:                 * If the mbuf is read-only, we need to allocate a new mbuf
        !           372:                 * for the header, since we need to modify the header.
1.65      thorpej   373:                 */
                    374:                if (M_READONLY(m)) {
1.116.2.1! rmind     375:                        const int hlen = ip->ip_hl << 2;
1.65      thorpej   376:
                    377:                        m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
1.116.2.1! rmind     378:                        if (m == NULL) {
        !           379:                                return ENOMEM;  /* XXX */
        !           380:                        }
1.65      thorpej   381:                        ip = mtod(m, struct ip *);
                    382:                }
                    383:
1.116.2.1! rmind     384:                /*
        !           385:                 * Applications on raw sockets pass us packets
        !           386:                 * in host byte order.
        !           387:                 */
1.38      mycroft   388:                if (m->m_pkthdr.len != ip->ip_len) {
                    389:                        m_freem(m);
                    390:                        return (EINVAL);
                    391:                }
1.62      itojun    392:                HTONS(ip->ip_len);
                    393:                HTONS(ip->ip_off);
1.116.2.1! rmind     394:                if (ip->ip_id || m->m_pkthdr.len < IP_MINFRAGSIZE) {
1.103     matt      395:                        flags |= IP_NOIPNEWID;
1.116.2.1! rmind     396:                }
1.13      mycroft   397:                opts = NULL;
1.116.2.1! rmind     398:
        !           399:                /*
        !           400:                 * Note: prevent IP output from overwriting header fields.
        !           401:                 */
1.13      mycroft   402:                flags |= IP_RAWOUTPUT;
1.105     thorpej   403:                IP_STATINC(IP_STAT_RAWOUT);
1.1       cgd       404:        }
1.116.2.1! rmind     405:
        !           406:        return ip_output(m, opts, inpcb_get_route(inp), flags,
        !           407:            inpcb_get_moptions(inp), so);
1.1       cgd       408: }
                    409:
                    410: /*
                    411:  * Raw IP socket option processing.
                    412:  */
1.9       mycroft   413: int
1.108     plunky    414: rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.1       cgd       415: {
1.116.2.1! rmind     416:        inpcb_t *inp = sotoinpcb(so);
        !           417:        int inpflags = inpcb_get_flags(inp);
        !           418:        int error = 0, optval;
        !           419:
        !           420:        KASSERT(solocked(so));
1.1       cgd       421:
1.108     plunky    422:        if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
1.100     dyoung    423:                if (op == PRCO_GETOPT) {
1.116.2.1! rmind     424:                        optval = (inpflags & INP_NOHEADER) ? 1 : 0;
1.108     plunky    425:                        error = sockopt_set(sopt, &optval, sizeof(optval));
                    426:                } else if (op == PRCO_SETOPT) {
                    427:                        error = sockopt_getint(sopt, &optval);
                    428:                        if (error)
                    429:                                goto out;
                    430:                        if (optval) {
1.116.2.1! rmind     431:                                inpflags &= ~INP_HDRINCL;
        !           432:                                inpflags |= INP_NOHEADER;
1.108     plunky    433:                        } else
1.116.2.1! rmind     434:                                inpflags &= ~INP_NOHEADER;
1.108     plunky    435:                }
                    436:                goto out;
1.116.2.1! rmind     437:        }
        !           438:
        !           439:        if (sopt->sopt_level != IPPROTO_IP) {
1.108     plunky    440:                return ip_ctloutput(op, so, sopt);
1.116.2.1! rmind     441:        }
1.100     dyoung    442:
                    443:        switch (op) {
1.31      mycroft   444:        case PRCO_SETOPT:
1.108     plunky    445:                switch (sopt->sopt_name) {
1.31      mycroft   446:                case IP_HDRINCL:
1.108     plunky    447:                        error = sockopt_getint(sopt, &optval);
                    448:                        if (error)
                    449:                                break;
                    450:                        if (optval)
1.116.2.1! rmind     451:                                inpflags |= INP_HDRINCL;
1.100     dyoung    452:                        else
1.116.2.1! rmind     453:                                inpflags &= ~INP_HDRINCL;
1.108     plunky    454:                        break;
1.31      mycroft   455:
                    456: #ifdef MROUTING
                    457:                case MRT_INIT:
                    458:                case MRT_DONE:
                    459:                case MRT_ADD_VIF:
                    460:                case MRT_DEL_VIF:
                    461:                case MRT_ADD_MFC:
                    462:                case MRT_DEL_MFC:
                    463:                case MRT_ASSERT:
1.81      manu      464:                case MRT_API_CONFIG:
                    465:                case MRT_ADD_BW_UPCALL:
                    466:                case MRT_DEL_BW_UPCALL:
1.108     plunky    467:                        error = ip_mrouter_set(so, sopt);
1.31      mycroft   468:                        break;
                    469: #endif
                    470:
                    471:                default:
1.108     plunky    472:                        error = ip_ctloutput(op, so, sopt);
1.31      mycroft   473:                        break;
1.13      mycroft   474:                }
                    475:                break;
1.1       cgd       476:
1.31      mycroft   477:        case PRCO_GETOPT:
1.108     plunky    478:                switch (sopt->sopt_name) {
1.31      mycroft   479:                case IP_HDRINCL:
1.116.2.1! rmind     480:                        optval = inpflags & INP_HDRINCL;
1.108     plunky    481:                        error = sockopt_set(sopt, &optval, sizeof(optval));
1.31      mycroft   482:                        break;
                    483:
1.6       hpeyerl   484: #ifdef MROUTING
1.31      mycroft   485:                case MRT_VERSION:
                    486:                case MRT_ASSERT:
1.81      manu      487:                case MRT_API_SUPPORT:
                    488:                case MRT_API_CONFIG:
1.108     plunky    489:                        error = ip_mrouter_get(so, sopt);
1.18      mycroft   490:                        break;
1.31      mycroft   491: #endif
                    492:
1.18      mycroft   493:                default:
1.108     plunky    494:                        error = ip_ctloutput(op, so, sopt);
1.18      mycroft   495:                        break;
                    496:                }
1.31      mycroft   497:                break;
1.1       cgd       498:        }
1.108     plunky    499:  out:
1.116.2.1! rmind     500:        if (!error) {
        !           501:                inpcb_set_flags(inp, inpflags);
        !           502:        }
1.100     dyoung    503:        return error;
1.1       cgd       504: }
                    505:
1.116.2.1! rmind     506: static int
        !           507: rip_bind(inpcb_t *inp, struct mbuf *nam)
1.29      mycroft   508: {
                    509:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    510:
                    511:        if (nam->m_len != sizeof(*addr))
1.116.2.1! rmind     512:                return EINVAL;
        !           513:        if (!IFNET_FIRST())
        !           514:                return EADDRNOTAVAIL;
1.115     joerg     515:        if (addr->sin_family != AF_INET)
1.116.2.1! rmind     516:                return EAFNOSUPPORT;
        !           517:        if (!in_nullhost(addr->sin_addr) && !ifa_ifwithaddr(sintosa(addr)))
        !           518:                return EADDRNOTAVAIL;
        !           519:
        !           520:        inpcb_set_addrs(inp, &addr->sin_addr, NULL);
        !           521:        return 0;
1.29      mycroft   522: }
                    523:
1.116.2.1! rmind     524: static int
        !           525: rip_connect(inpcb_t *inp, struct mbuf *nam)
1.27      mycroft   526: {
                    527:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    528:
                    529:        if (nam->m_len != sizeof(*addr))
1.116.2.1! rmind     530:                return EINVAL;
        !           531:        if (!IFNET_FIRST())
        !           532:                return EADDRNOTAVAIL;
1.115     joerg     533:        if (addr->sin_family != AF_INET)
1.116.2.1! rmind     534:                return EAFNOSUPPORT;
        !           535:
        !           536:        inpcb_set_addrs(inp, NULL, &addr->sin_addr);
        !           537:        return 0;
1.27      mycroft   538: }
                    539:
1.116.2.1! rmind     540: static void
        !           541: rip_disconnect(inpcb_t *inp)
1.27      mycroft   542: {
1.116.2.1! rmind     543:        inpcb_set_addrs(inp, NULL, &zeroin_addr);
1.27      mycroft   544: }
                    545:
1.9       mycroft   546: int
1.116.2.1! rmind     547: rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
        !           548:     struct mbuf *control, struct lwp *l)
1.1       cgd       549: {
1.116.2.1! rmind     550:        inpcb_t *inp;
        !           551:        struct ip *ip;
1.53      augustss  552:        int error = 0;
1.13      mycroft   553: #ifdef MROUTING
1.6       hpeyerl   554:        extern struct socket *ip_mrouter;
                    555: #endif
1.27      mycroft   556:
1.116.2.1! rmind     557:        if (req == PRU_CONTROL) {
        !           558:                return in_control(so, (long)m, nam, (ifnet_t *)control, l);
        !           559:        }
1.50      thorpej   560:        if (req == PRU_PURGEIF) {
1.116.2.1! rmind     561:                int s = splsoftnet();
1.107     ad        562:                mutex_enter(softnet_lock);
1.116.2.1! rmind     563:                inpcb_purgeif0(rawcbtable, (ifnet_t *)control);
        !           564:                in_purgeif((ifnet_t *)control);
        !           565:                inpcb_purgeif(rawcbtable, (ifnet_t *)control);
1.107     ad        566:                mutex_exit(softnet_lock);
1.93      tls       567:                splx(s);
1.116.2.1! rmind     568:                return 0;
1.49      thorpej   569:        }
1.22      pk        570:
1.116.2.1! rmind     571:        KASSERT(req == PRU_ATTACH || solocked(so));
1.27      mycroft   572:        inp = sotoinpcb(so);
1.116.2.1! rmind     573:
        !           574:        KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
1.111     dyoung    575:        if (inp == NULL && req != PRU_ATTACH) {
1.116.2.1! rmind     576:                return EINVAL;
1.22      pk        577:        }
                    578:
1.1       cgd       579:        switch (req) {
                    580:        case PRU_ATTACH:
1.107     ad        581:                sosetlock(so);
1.116.2.1! rmind     582:                if (inp) {
1.27      mycroft   583:                        error = EISCONN;
                    584:                        break;
                    585:                }
1.94      elad      586:
                    587:                /* XXX: raw socket permissions are checked in socreate() */
                    588:
1.27      mycroft   589:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    590:                        error = soreserve(so, rip_sendspace, rip_recvspace);
                    591:                        if (error)
                    592:                                break;
                    593:                }
1.116.2.1! rmind     594:                error = inpcb_create(so, rawcbtable);
1.27      mycroft   595:                if (error)
1.13      mycroft   596:                        break;
1.27      mycroft   597:                inp = sotoinpcb(so);
1.116.2.1! rmind     598:                ip = in_getiphdr(inp);
        !           599:                ip->ip_p = (long)nam;
1.1       cgd       600:                break;
                    601:
                    602:        case PRU_DETACH:
1.13      mycroft   603: #ifdef MROUTING
1.6       hpeyerl   604:                if (so == ip_mrouter)
                    605:                        ip_mrouter_done();
                    606: #endif
1.116.2.1! rmind     607:                inpcb_destroy(inp);
1.1       cgd       608:                break;
                    609:
                    610:        case PRU_BIND:
1.29      mycroft   611:                error = rip_bind(inp, nam);
1.27      mycroft   612:                break;
                    613:
                    614:        case PRU_LISTEN:
                    615:                error = EOPNOTSUPP;
                    616:                break;
                    617:
                    618:        case PRU_CONNECT:
                    619:                error = rip_connect(inp, nam);
                    620:                if (error)
1.13      mycroft   621:                        break;
1.1       cgd       622:                soisconnected(so);
1.13      mycroft   623:                break;
                    624:
                    625:        case PRU_CONNECT2:
                    626:                error = EOPNOTSUPP;
                    627:                break;
                    628:
1.27      mycroft   629:        case PRU_DISCONNECT:
                    630:                soisdisconnected(so);
                    631:                rip_disconnect(inp);
                    632:                break;
                    633:
1.13      mycroft   634:        /*
                    635:         * Mark the connection as being incapable of further input.
                    636:         */
                    637:        case PRU_SHUTDOWN:
                    638:                socantsendmore(so);
                    639:                break;
                    640:
1.27      mycroft   641:        case PRU_RCVD:
                    642:                error = EOPNOTSUPP;
                    643:                break;
                    644:
1.13      mycroft   645:        /*
                    646:         * Ship a packet out.  The appropriate raw output
                    647:         * routine handles any massaging necessary.
                    648:         */
                    649:        case PRU_SEND:
1.28      mycroft   650:                if (control && control->m_len) {
                    651:                        m_freem(control);
                    652:                        m_freem(m);
                    653:                        error = EINVAL;
                    654:                        break;
                    655:                }
1.116.2.1! rmind     656:                if ((so->so_state & SS_ISCONNECTED) != 0) {
        !           657:                        error = nam ? EISCONN : ENOTCONN;
        !           658:                        m_freem(m);
        !           659:                        break;
        !           660:                }
        !           661:                if (nam && (error = rip_connect(inp, nam)) != 0) {
        !           662:                        m_freem(m);
        !           663:                        break;
1.13      mycroft   664:                }
1.27      mycroft   665:                error = rip_output(m, inp);
1.116.2.1! rmind     666:                if (nam) {
1.27      mycroft   667:                        rip_disconnect(inp);
1.116.2.1! rmind     668:                }
1.13      mycroft   669:                break;
                    670:
                    671:        case PRU_SENSE:
                    672:                /*
1.116.2.1! rmind     673:                 * Stat: do not bother with a blocksize.
1.13      mycroft   674:                 */
1.116.2.1! rmind     675:                return 0;
1.13      mycroft   676:
                    677:        case PRU_RCVOOB:
1.27      mycroft   678:                error = EOPNOTSUPP;
                    679:                break;
                    680:
1.13      mycroft   681:        case PRU_SENDOOB:
1.28      mycroft   682:                m_freem(control);
1.27      mycroft   683:                m_freem(m);
1.13      mycroft   684:                error = EOPNOTSUPP;
                    685:                break;
                    686:
                    687:        case PRU_SOCKADDR:
1.116.2.1! rmind     688:                inpcb_fetch_sockaddr(inp, nam);
1.13      mycroft   689:                break;
                    690:
                    691:        case PRU_PEERADDR:
1.116.2.1! rmind     692:                inpcb_fetch_peeraddr(inp, nam);
1.13      mycroft   693:                break;
                    694:
                    695:        default:
1.116.2.1! rmind     696:                KASSERT(false);
1.1       cgd       697:        }
1.27      mycroft   698:
1.116.2.1! rmind     699:        return error;
1.1       cgd       700: }
1.84      atatat    701:
1.110     pooka     702: static void
                    703: sysctl_net_inet_raw_setup(struct sysctllog **clog)
1.84      atatat    704: {
                    705:        sysctl_createv(clog, 0, NULL, NULL,
                    706:                       CTLFLAG_PERMANENT,
                    707:                       CTLTYPE_NODE, "net", NULL,
                    708:                       NULL, 0, NULL, 0,
                    709:                       CTL_NET, CTL_EOL);
                    710:        sysctl_createv(clog, 0, NULL, NULL,
                    711:                       CTLFLAG_PERMANENT,
                    712:                       CTLTYPE_NODE, "inet", NULL,
                    713:                       NULL, 0, NULL, 0,
                    714:                       CTL_NET, PF_INET, CTL_EOL);
                    715:        sysctl_createv(clog, 0, NULL, NULL,
                    716:                       CTLFLAG_PERMANENT,
                    717:                       CTLTYPE_NODE, "raw",
                    718:                       SYSCTL_DESCR("Raw IPv4 settings"),
                    719:                       NULL, 0, NULL, 0,
                    720:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
                    721:        sysctl_createv(clog, 0, NULL, NULL,
                    722:                       CTLFLAG_PERMANENT,
1.86      atatat    723:                       CTLTYPE_STRUCT, "pcblist",
1.84      atatat    724:                       SYSCTL_DESCR("Raw IPv4 control block list"),
1.116.2.1! rmind     725:                       sysctl_inpcblist, 0, rawcbtable, 0,
1.84      atatat    726:                       CTL_NET, PF_INET, IPPROTO_RAW,
                    727:                       CTL_CREATE, CTL_EOL);
                    728: }

CVSweb <webmaster@jp.NetBSD.org>