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

1.103.6.1! mjf         1: /*     $NetBSD$        */
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.103.6.1! mjf        64: __KERNEL_RCSID(0, "$NetBSD$");
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.103.6.1! mjf        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.103.6.1! mjf       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.103.6.1! mjf       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.103.6.1! mjf       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.103.6.1! mjf       220:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
        !           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.103.6.1! mjf       228:                uint64_t *ips;
        !           229:
1.97      dyoung    230:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
                    231:                    0, 0);
1.103.6.1! mjf       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.103.6.1! mjf       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.83      perry     383: rip_ctloutput(int op, struct socket *so, int level, int optname,
                    384:     struct mbuf **m)
1.1       cgd       385: {
1.53      augustss  386:        struct inpcb *inp = sotoinpcb(so);
1.31      mycroft   387:        int error = 0;
1.1       cgd       388:
1.100     dyoung    389:        if (level == SOL_SOCKET && optname == SO_NOHEADER) {
                    390:                if (op == PRCO_GETOPT) {
                    391:                        *m = m_intopt(so,
                    392:                            (inp->inp_flags & INP_NOHEADER) ? 1 : 0);
                    393:                        return 0;
1.101     christos  394:                } else if (*m == NULL || (*m)->m_len != sizeof(int))
1.100     dyoung    395:                        error = EINVAL;
                    396:                else if (*mtod(*m, int *)) {
                    397:                        inp->inp_flags &= ~INP_HDRINCL;
                    398:                        inp->inp_flags |= INP_NOHEADER;
                    399:                } else
                    400:                        inp->inp_flags &= ~INP_NOHEADER;
                    401:                goto free_m;
                    402:        } else if (level != IPPROTO_IP)
                    403:                return ip_ctloutput(op, so, level, optname, m);
                    404:
                    405:        switch (op) {
1.31      mycroft   406:
                    407:        case PRCO_SETOPT:
                    408:                switch (optname) {
                    409:                case IP_HDRINCL:
1.101     christos  410:                        if (*m == NULL || (*m)->m_len != sizeof(int))
1.31      mycroft   411:                                error = EINVAL;
1.100     dyoung    412:                        else if (*mtod(*m, int *))
                    413:                                inp->inp_flags |= INP_HDRINCL;
                    414:                        else
                    415:                                inp->inp_flags &= ~INP_HDRINCL;
                    416:                        goto free_m;
1.31      mycroft   417:
                    418: #ifdef MROUTING
                    419:                case MRT_INIT:
                    420:                case MRT_DONE:
                    421:                case MRT_ADD_VIF:
                    422:                case MRT_DEL_VIF:
                    423:                case MRT_ADD_MFC:
                    424:                case MRT_DEL_MFC:
                    425:                case MRT_ASSERT:
1.81      manu      426:                case MRT_API_CONFIG:
                    427:                case MRT_ADD_BW_UPCALL:
                    428:                case MRT_DEL_BW_UPCALL:
1.31      mycroft   429:                        error = ip_mrouter_set(so, optname, m);
                    430:                        break;
                    431: #endif
                    432:
                    433:                default:
                    434:                        error = ip_ctloutput(op, so, level, optname, m);
                    435:                        break;
1.13      mycroft   436:                }
                    437:                break;
1.1       cgd       438:
1.31      mycroft   439:        case PRCO_GETOPT:
                    440:                switch (optname) {
                    441:                case IP_HDRINCL:
1.100     dyoung    442:                        *m = m_intopt(so, inp->inp_flags & INP_HDRINCL ? 1 : 0);
1.31      mycroft   443:                        break;
                    444:
1.6       hpeyerl   445: #ifdef MROUTING
1.31      mycroft   446:                case MRT_VERSION:
                    447:                case MRT_ASSERT:
1.81      manu      448:                case MRT_API_SUPPORT:
                    449:                case MRT_API_CONFIG:
1.31      mycroft   450:                        error = ip_mrouter_get(so, optname, m);
1.18      mycroft   451:                        break;
1.31      mycroft   452: #endif
                    453:
1.18      mycroft   454:                default:
1.31      mycroft   455:                        error = ip_ctloutput(op, so, level, optname, m);
1.18      mycroft   456:                        break;
                    457:                }
1.31      mycroft   458:                break;
1.1       cgd       459:        }
1.100     dyoung    460:        return error;
                    461: free_m:
                    462:        if (op == PRCO_SETOPT && *m != NULL)
                    463:                (void)m_free(*m);
                    464:        return error;
1.1       cgd       465: }
                    466:
1.27      mycroft   467: int
1.83      perry     468: rip_bind(struct inpcb *inp, struct mbuf *nam)
1.29      mycroft   469: {
                    470:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    471:
                    472:        if (nam->m_len != sizeof(*addr))
                    473:                return (EINVAL);
1.58      matt      474:        if (TAILQ_FIRST(&ifnet) == 0)
1.29      mycroft   475:                return (EADDRNOTAVAIL);
                    476:        if (addr->sin_family != AF_INET &&
                    477:            addr->sin_family != AF_IMPLINK)
                    478:                return (EAFNOSUPPORT);
1.32      mycroft   479:        if (!in_nullhost(addr->sin_addr) &&
1.29      mycroft   480:            ifa_ifwithaddr(sintosa(addr)) == 0)
                    481:                return (EADDRNOTAVAIL);
                    482:        inp->inp_laddr = addr->sin_addr;
                    483:        return (0);
                    484: }
                    485:
                    486: int
1.83      perry     487: rip_connect(struct inpcb *inp, struct mbuf *nam)
1.27      mycroft   488: {
                    489:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    490:
                    491:        if (nam->m_len != sizeof(*addr))
                    492:                return (EINVAL);
1.58      matt      493:        if (TAILQ_FIRST(&ifnet) == 0)
1.27      mycroft   494:                return (EADDRNOTAVAIL);
                    495:        if (addr->sin_family != AF_INET &&
                    496:            addr->sin_family != AF_IMPLINK)
                    497:                return (EAFNOSUPPORT);
                    498:        inp->inp_faddr = addr->sin_addr;
                    499:        return (0);
                    500: }
                    501:
                    502: void
1.83      perry     503: rip_disconnect(struct inpcb *inp)
1.27      mycroft   504: {
                    505:
1.32      mycroft   506:        inp->inp_faddr = zeroin_addr;
1.27      mycroft   507: }
                    508:
1.13      mycroft   509: u_long rip_sendspace = RIPSNDQ;
                    510: u_long rip_recvspace = RIPRCVQ;
                    511:
1.1       cgd       512: /*ARGSUSED*/
1.9       mycroft   513: int
1.83      perry     514: rip_usrreq(struct socket *so, int req,
1.88      christos  515:     struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
1.1       cgd       516: {
1.53      augustss  517:        struct inpcb *inp;
1.27      mycroft   518:        int s;
1.53      augustss  519:        int error = 0;
1.13      mycroft   520: #ifdef MROUTING
1.6       hpeyerl   521:        extern struct socket *ip_mrouter;
                    522: #endif
1.27      mycroft   523:
1.22      pk        524:        if (req == PRU_CONTROL)
1.96      christos  525:                return (in_control(so, (long)m, (void *)nam,
1.90      ad        526:                    (struct ifnet *)control, l));
1.49      thorpej   527:
1.93      tls       528:        s = splsoftnet();
                    529:
1.50      thorpej   530:        if (req == PRU_PURGEIF) {
1.103.6.1! mjf       531:                mutex_enter(softnet_lock);
1.56      itojun    532:                in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50      thorpej   533:                in_purgeif((struct ifnet *)control);
                    534:                in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.103.6.1! mjf       535:                mutex_exit(softnet_lock);
1.93      tls       536:                splx(s);
1.49      thorpej   537:                return (0);
                    538:        }
1.22      pk        539:
1.27      mycroft   540:        inp = sotoinpcb(so);
1.28      mycroft   541: #ifdef DIAGNOSTIC
                    542:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    543:                panic("rip_usrreq: unexpected control mbuf");
                    544: #endif
1.27      mycroft   545:        if (inp == 0 && req != PRU_ATTACH) {
1.22      pk        546:                error = EINVAL;
                    547:                goto release;
                    548:        }
                    549:
1.1       cgd       550:        switch (req) {
                    551:
                    552:        case PRU_ATTACH:
1.103.6.1! mjf       553:                sosetlock(so);
1.27      mycroft   554:                if (inp != 0) {
                    555:                        error = EISCONN;
                    556:                        break;
                    557:                }
1.94      elad      558:
                    559:                if (l == NULL) {
1.13      mycroft   560:                        error = EACCES;
                    561:                        break;
                    562:                }
1.94      elad      563:
                    564:                /* XXX: raw socket permissions are checked in socreate() */
                    565:
1.27      mycroft   566:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    567:                        error = soreserve(so, rip_sendspace, rip_recvspace);
                    568:                        if (error)
                    569:                                break;
                    570:                }
                    571:                error = in_pcballoc(so, &rawcbtable);
                    572:                if (error)
1.13      mycroft   573:                        break;
1.27      mycroft   574:                inp = sotoinpcb(so);
1.17      cgd       575:                inp->inp_ip.ip_p = (long)nam;
1.1       cgd       576:                break;
                    577:
                    578:        case PRU_DETACH:
1.13      mycroft   579: #ifdef MROUTING
1.6       hpeyerl   580:                if (so == ip_mrouter)
                    581:                        ip_mrouter_done();
                    582: #endif
1.13      mycroft   583:                in_pcbdetach(inp);
1.1       cgd       584:                break;
                    585:
                    586:        case PRU_BIND:
1.29      mycroft   587:                error = rip_bind(inp, nam);
1.27      mycroft   588:                break;
                    589:
                    590:        case PRU_LISTEN:
                    591:                error = EOPNOTSUPP;
                    592:                break;
                    593:
                    594:        case PRU_CONNECT:
                    595:                error = rip_connect(inp, nam);
                    596:                if (error)
1.13      mycroft   597:                        break;
1.1       cgd       598:                soisconnected(so);
1.13      mycroft   599:                break;
                    600:
                    601:        case PRU_CONNECT2:
                    602:                error = EOPNOTSUPP;
                    603:                break;
                    604:
1.27      mycroft   605:        case PRU_DISCONNECT:
                    606:                soisdisconnected(so);
                    607:                rip_disconnect(inp);
                    608:                break;
                    609:
1.13      mycroft   610:        /*
                    611:         * Mark the connection as being incapable of further input.
                    612:         */
                    613:        case PRU_SHUTDOWN:
                    614:                socantsendmore(so);
                    615:                break;
                    616:
1.27      mycroft   617:        case PRU_RCVD:
                    618:                error = EOPNOTSUPP;
                    619:                break;
                    620:
1.13      mycroft   621:        /*
                    622:         * Ship a packet out.  The appropriate raw output
                    623:         * routine handles any massaging necessary.
                    624:         */
                    625:        case PRU_SEND:
1.28      mycroft   626:                if (control && control->m_len) {
                    627:                        m_freem(control);
                    628:                        m_freem(m);
                    629:                        error = EINVAL;
                    630:                        break;
                    631:                }
                    632:        {
1.27      mycroft   633:                if (nam) {
                    634:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13      mycroft   635:                                error = EISCONN;
1.28      mycroft   636:                                goto die;
1.13      mycroft   637:                        }
1.27      mycroft   638:                        error = rip_connect(inp, nam);
                    639:                        if (error) {
1.28      mycroft   640:                        die:
1.27      mycroft   641:                                m_freem(m);
                    642:                                break;
                    643:                        }
1.13      mycroft   644:                } else {
1.27      mycroft   645:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13      mycroft   646:                                error = ENOTCONN;
1.28      mycroft   647:                                goto die;
1.13      mycroft   648:                        }
                    649:                }
1.27      mycroft   650:                error = rip_output(m, inp);
                    651:                if (nam)
                    652:                        rip_disconnect(inp);
1.28      mycroft   653:        }
1.13      mycroft   654:                break;
                    655:
                    656:        case PRU_SENSE:
                    657:                /*
                    658:                 * stat: don't bother with a blocksize.
                    659:                 */
1.27      mycroft   660:                splx(s);
1.1       cgd       661:                return (0);
1.13      mycroft   662:
                    663:        case PRU_RCVOOB:
1.27      mycroft   664:                error = EOPNOTSUPP;
                    665:                break;
                    666:
1.13      mycroft   667:        case PRU_SENDOOB:
1.28      mycroft   668:                m_freem(control);
1.27      mycroft   669:                m_freem(m);
1.13      mycroft   670:                error = EOPNOTSUPP;
                    671:                break;
                    672:
                    673:        case PRU_SOCKADDR:
                    674:                in_setsockaddr(inp, nam);
                    675:                break;
                    676:
                    677:        case PRU_PEERADDR:
                    678:                in_setpeeraddr(inp, nam);
                    679:                break;
                    680:
                    681:        default:
                    682:                panic("rip_usrreq");
1.1       cgd       683:        }
1.27      mycroft   684:
1.22      pk        685: release:
1.27      mycroft   686:        splx(s);
1.1       cgd       687:        return (error);
                    688: }
1.84      atatat    689:
                    690: SYSCTL_SETUP(sysctl_net_inet_raw_setup, "sysctl net.inet.raw subtree setup")
                    691: {
                    692:
                    693:        sysctl_createv(clog, 0, NULL, NULL,
                    694:                       CTLFLAG_PERMANENT,
                    695:                       CTLTYPE_NODE, "net", NULL,
                    696:                       NULL, 0, NULL, 0,
                    697:                       CTL_NET, CTL_EOL);
                    698:        sysctl_createv(clog, 0, NULL, NULL,
                    699:                       CTLFLAG_PERMANENT,
                    700:                       CTLTYPE_NODE, "inet", NULL,
                    701:                       NULL, 0, NULL, 0,
                    702:                       CTL_NET, PF_INET, CTL_EOL);
                    703:        sysctl_createv(clog, 0, NULL, NULL,
                    704:                       CTLFLAG_PERMANENT,
                    705:                       CTLTYPE_NODE, "raw",
                    706:                       SYSCTL_DESCR("Raw IPv4 settings"),
                    707:                       NULL, 0, NULL, 0,
                    708:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
                    709:
                    710:        sysctl_createv(clog, 0, NULL, NULL,
                    711:                       CTLFLAG_PERMANENT,
1.86      atatat    712:                       CTLTYPE_STRUCT, "pcblist",
1.84      atatat    713:                       SYSCTL_DESCR("Raw IPv4 control block list"),
                    714:                       sysctl_inpcblist, 0, &rawcbtable, 0,
                    715:                       CTL_NET, PF_INET, IPPROTO_RAW,
                    716:                       CTL_CREATE, CTL_EOL);
                    717: }

CVSweb <webmaster@jp.NetBSD.org>