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

1.106   ! thorpej     1: /*     $NetBSD: raw_ip.c,v 1.105 2008/04/12 05:58:22 thorpej 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.106   ! thorpej    64: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.105 2008/04/12 05:58:22 thorpej 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.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.56      itojun    531:                in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50      thorpej   532:                in_purgeif((struct ifnet *)control);
                    533:                in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.93      tls       534:                splx(s);
1.49      thorpej   535:                return (0);
                    536:        }
1.22      pk        537:
1.27      mycroft   538:        inp = sotoinpcb(so);
1.28      mycroft   539: #ifdef DIAGNOSTIC
                    540:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    541:                panic("rip_usrreq: unexpected control mbuf");
                    542: #endif
1.27      mycroft   543:        if (inp == 0 && req != PRU_ATTACH) {
1.22      pk        544:                error = EINVAL;
                    545:                goto release;
                    546:        }
                    547:
1.1       cgd       548:        switch (req) {
                    549:
                    550:        case PRU_ATTACH:
1.27      mycroft   551:                if (inp != 0) {
                    552:                        error = EISCONN;
                    553:                        break;
                    554:                }
1.94      elad      555:
                    556:                if (l == NULL) {
1.13      mycroft   557:                        error = EACCES;
                    558:                        break;
                    559:                }
1.94      elad      560:
                    561:                /* XXX: raw socket permissions are checked in socreate() */
                    562:
1.27      mycroft   563:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    564:                        error = soreserve(so, rip_sendspace, rip_recvspace);
                    565:                        if (error)
                    566:                                break;
                    567:                }
                    568:                error = in_pcballoc(so, &rawcbtable);
                    569:                if (error)
1.13      mycroft   570:                        break;
1.27      mycroft   571:                inp = sotoinpcb(so);
1.17      cgd       572:                inp->inp_ip.ip_p = (long)nam;
1.1       cgd       573:                break;
                    574:
                    575:        case PRU_DETACH:
1.13      mycroft   576: #ifdef MROUTING
1.6       hpeyerl   577:                if (so == ip_mrouter)
                    578:                        ip_mrouter_done();
                    579: #endif
1.13      mycroft   580:                in_pcbdetach(inp);
1.1       cgd       581:                break;
                    582:
                    583:        case PRU_BIND:
1.29      mycroft   584:                error = rip_bind(inp, nam);
1.27      mycroft   585:                break;
                    586:
                    587:        case PRU_LISTEN:
                    588:                error = EOPNOTSUPP;
                    589:                break;
                    590:
                    591:        case PRU_CONNECT:
                    592:                error = rip_connect(inp, nam);
                    593:                if (error)
1.13      mycroft   594:                        break;
1.1       cgd       595:                soisconnected(so);
1.13      mycroft   596:                break;
                    597:
                    598:        case PRU_CONNECT2:
                    599:                error = EOPNOTSUPP;
                    600:                break;
                    601:
1.27      mycroft   602:        case PRU_DISCONNECT:
                    603:                soisdisconnected(so);
                    604:                rip_disconnect(inp);
                    605:                break;
                    606:
1.13      mycroft   607:        /*
                    608:         * Mark the connection as being incapable of further input.
                    609:         */
                    610:        case PRU_SHUTDOWN:
                    611:                socantsendmore(so);
                    612:                break;
                    613:
1.27      mycroft   614:        case PRU_RCVD:
                    615:                error = EOPNOTSUPP;
                    616:                break;
                    617:
1.13      mycroft   618:        /*
                    619:         * Ship a packet out.  The appropriate raw output
                    620:         * routine handles any massaging necessary.
                    621:         */
                    622:        case PRU_SEND:
1.28      mycroft   623:                if (control && control->m_len) {
                    624:                        m_freem(control);
                    625:                        m_freem(m);
                    626:                        error = EINVAL;
                    627:                        break;
                    628:                }
                    629:        {
1.27      mycroft   630:                if (nam) {
                    631:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13      mycroft   632:                                error = EISCONN;
1.28      mycroft   633:                                goto die;
1.13      mycroft   634:                        }
1.27      mycroft   635:                        error = rip_connect(inp, nam);
                    636:                        if (error) {
1.28      mycroft   637:                        die:
1.27      mycroft   638:                                m_freem(m);
                    639:                                break;
                    640:                        }
1.13      mycroft   641:                } else {
1.27      mycroft   642:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13      mycroft   643:                                error = ENOTCONN;
1.28      mycroft   644:                                goto die;
1.13      mycroft   645:                        }
                    646:                }
1.27      mycroft   647:                error = rip_output(m, inp);
                    648:                if (nam)
                    649:                        rip_disconnect(inp);
1.28      mycroft   650:        }
1.13      mycroft   651:                break;
                    652:
                    653:        case PRU_SENSE:
                    654:                /*
                    655:                 * stat: don't bother with a blocksize.
                    656:                 */
1.27      mycroft   657:                splx(s);
1.1       cgd       658:                return (0);
1.13      mycroft   659:
                    660:        case PRU_RCVOOB:
1.27      mycroft   661:                error = EOPNOTSUPP;
                    662:                break;
                    663:
1.13      mycroft   664:        case PRU_SENDOOB:
1.28      mycroft   665:                m_freem(control);
1.27      mycroft   666:                m_freem(m);
1.13      mycroft   667:                error = EOPNOTSUPP;
                    668:                break;
                    669:
                    670:        case PRU_SOCKADDR:
                    671:                in_setsockaddr(inp, nam);
                    672:                break;
                    673:
                    674:        case PRU_PEERADDR:
                    675:                in_setpeeraddr(inp, nam);
                    676:                break;
                    677:
                    678:        default:
                    679:                panic("rip_usrreq");
1.1       cgd       680:        }
1.27      mycroft   681:
1.22      pk        682: release:
1.27      mycroft   683:        splx(s);
1.1       cgd       684:        return (error);
                    685: }
1.84      atatat    686:
                    687: SYSCTL_SETUP(sysctl_net_inet_raw_setup, "sysctl net.inet.raw subtree setup")
                    688: {
                    689:
                    690:        sysctl_createv(clog, 0, NULL, NULL,
                    691:                       CTLFLAG_PERMANENT,
                    692:                       CTLTYPE_NODE, "net", NULL,
                    693:                       NULL, 0, NULL, 0,
                    694:                       CTL_NET, CTL_EOL);
                    695:        sysctl_createv(clog, 0, NULL, NULL,
                    696:                       CTLFLAG_PERMANENT,
                    697:                       CTLTYPE_NODE, "inet", NULL,
                    698:                       NULL, 0, NULL, 0,
                    699:                       CTL_NET, PF_INET, CTL_EOL);
                    700:        sysctl_createv(clog, 0, NULL, NULL,
                    701:                       CTLFLAG_PERMANENT,
                    702:                       CTLTYPE_NODE, "raw",
                    703:                       SYSCTL_DESCR("Raw IPv4 settings"),
                    704:                       NULL, 0, NULL, 0,
                    705:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
                    706:
                    707:        sysctl_createv(clog, 0, NULL, NULL,
                    708:                       CTLFLAG_PERMANENT,
1.86      atatat    709:                       CTLTYPE_STRUCT, "pcblist",
1.84      atatat    710:                       SYSCTL_DESCR("Raw IPv4 control block list"),
                    711:                       sysctl_inpcblist, 0, &rawcbtable, 0,
                    712:                       CTL_NET, PF_INET, IPPROTO_RAW,
                    713:                       CTL_CREATE, CTL_EOL);
                    714: }

CVSweb <webmaster@jp.NetBSD.org>