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

1.105   ! thorpej     1: /*     $NetBSD: raw_ip.c,v 1.104 2008/04/07 06:31:28 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.105   ! thorpej    64: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.104 2008/04/07 06:31:28 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>
                    100: #endif /*IPSEC*/
                    101:
1.72      jonathan  102: #ifdef FAST_IPSEC
                    103: #include <netipsec/ipsec.h>
1.80      jonathan  104: #include <netipsec/ipsec_var.h>                        /* XXX ipsecstat namespace */
1.72      jonathan  105: #endif /* FAST_IPSEC*/
                    106:
1.20      mycroft   107: struct inpcbtable rawcbtable;
1.30      pk        108:
1.82      perry     109: int     rip_pcbnotify(struct inpcbtable *, struct in_addr,
                    110:     struct in_addr, int, int, void (*)(struct inpcb *, int));
                    111: int     rip_bind(struct inpcb *, struct mbuf *);
                    112: int     rip_connect(struct inpcb *, struct mbuf *);
                    113: void    rip_disconnect(struct inpcb *);
1.13      mycroft   114:
                    115: /*
                    116:  * Nominal space allocated to a raw ip socket.
                    117:  */
                    118: #define        RIPSNDQ         8192
                    119: #define        RIPRCVQ         8192
1.1       cgd       120:
                    121: /*
                    122:  * Raw interface to IP protocol.
                    123:  */
1.13      mycroft   124:
                    125: /*
                    126:  * Initialize raw connection block q.
                    127:  */
                    128: void
1.83      perry     129: rip_init(void)
1.13      mycroft   130: {
                    131:
1.33      mycroft   132:        in_pcbinit(&rawcbtable, 1, 1);
1.13      mycroft   133: }
                    134:
1.100     dyoung    135: static void
                    136: rip_sbappendaddr(struct inpcb *last, struct ip *ip, const struct sockaddr *sa,
                    137:     int hlen, struct mbuf *opts, struct mbuf *n)
                    138: {
                    139:        if (last->inp_flags & INP_NOHEADER)
                    140:                m_adj(n, hlen);
                    141:        if (last->inp_flags & INP_CONTROLOPTS ||
                    142:            last->inp_socket->so_options & SO_TIMESTAMP)
                    143:                ip_savecontrol(last, &opts, ip, n);
                    144:        if (sbappendaddr(&last->inp_socket->so_rcv, sa, n, opts) == 0) {
                    145:                /* should notify about lost packet */
                    146:                m_freem(n);
                    147:                if (opts)
                    148:                        m_freem(opts);
                    149:        } else
                    150:                sorwakeup(last->inp_socket);
                    151: }
                    152:
1.1       cgd       153: /*
                    154:  * Setup generic address and protocol structures
                    155:  * for raw_input routine, then pass them along with
                    156:  * mbuf chain.
                    157:  */
1.9       mycroft   158: void
1.24      christos  159: rip_input(struct mbuf *m, ...)
1.1       cgd       160: {
1.100     dyoung    161:        int hlen, proto;
1.53      augustss  162:        struct ip *ip = mtod(m, struct ip *);
1.75      itojun    163:        struct inpcb_hdr *inph;
1.53      augustss  164:        struct inpcb *inp;
1.97      dyoung    165:        struct inpcb *last = NULL;
                    166:        struct mbuf *n, *opts = NULL;
1.32      mycroft   167:        struct sockaddr_in ripsrc;
1.43      itojun    168:        va_list ap;
                    169:
                    170:        va_start(ap, m);
1.64      simonb    171:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.43      itojun    172:        proto = va_arg(ap, int);
                    173:        va_end(ap);
1.1       cgd       174:
1.97      dyoung    175:        sockaddr_in_init(&ripsrc, &ip->ip_src, 0);
1.42      thorpej   176:
                    177:        /*
                    178:         * XXX Compatibility: programs using raw IP expect ip_len
1.62      itojun    179:         * XXX to have the header length subtracted, and in host order.
                    180:         * XXX ip_off is also expected to be host order.
1.42      thorpej   181:         */
1.100     dyoung    182:        hlen = ip->ip_hl << 2;
                    183:        ip->ip_len = ntohs(ip->ip_len) - hlen;
1.62      itojun    184:        NTOHS(ip->ip_off);
1.32      mycroft   185:
1.75      itojun    186:        CIRCLEQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
                    187:                inp = (struct inpcb *)inph;
                    188:                if (inp->inp_af != AF_INET)
                    189:                        continue;
1.43      itojun    190:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
1.13      mycroft   191:                        continue;
1.32      mycroft   192:                if (!in_nullhost(inp->inp_laddr) &&
                    193:                    !in_hosteq(inp->inp_laddr, ip->ip_dst))
1.13      mycroft   194:                        continue;
1.32      mycroft   195:                if (!in_nullhost(inp->inp_faddr) &&
                    196:                    !in_hosteq(inp->inp_faddr, ip->ip_src))
1.13      mycroft   197:                        continue;
1.97      dyoung    198:                if (last == NULL)
                    199:                        ;
1.72      jonathan  200: #if defined(IPSEC) || defined(FAST_IPSEC)
1.97      dyoung    201:                /* check AH/ESP integrity. */
                    202:                else if (ipsec4_in_reject_so(m, last->inp_socket)) {
                    203:                        ipsecstat.in_polvio++;
                    204:                        /* do not inject data to pcb */
                    205:                }
                    206: #endif /*IPSEC*/
1.99      dyoung    207:                else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.100     dyoung    208:                        rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
                    209:                            n);
1.97      dyoung    210:                        opts = NULL;
1.13      mycroft   211:                }
1.36      thorpej   212:                last = inp;
1.13      mycroft   213:        }
1.72      jonathan  214: #if defined(IPSEC) || defined(FAST_IPSEC)
1.55      itojun    215:        /* check AH/ESP integrity. */
1.97      dyoung    216:        if (last != NULL && ipsec4_in_reject_so(m, last->inp_socket)) {
1.55      itojun    217:                m_freem(m);
                    218:                ipsecstat.in_polvio++;
1.105   ! thorpej   219:                IP_STATDEC(IP_STAT_DELIVERED);
1.55      itojun    220:                /* do not inject data to pcb */
                    221:        } else
                    222: #endif /*IPSEC*/
1.100     dyoung    223:        if (last != NULL)
                    224:                rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
                    225:        else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
1.105   ! thorpej   226:                uint64_t *ips;
        !           227:
1.97      dyoung    228:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
                    229:                    0, 0);
1.105   ! thorpej   230:                ips = IP_STAT_GETREF();
        !           231:                ips[IP_STAT_NOPROTO]++;
        !           232:                ips[IP_STAT_DELIVERED]--;
        !           233:                IP_STAT_PUTREF();
1.97      dyoung    234:        } else
                    235:                m_freem(m);
1.43      itojun    236:        return;
1.60      itojun    237: }
                    238:
                    239: int
1.83      perry     240: rip_pcbnotify(struct inpcbtable *table,
                    241:     struct in_addr faddr, struct in_addr laddr, int proto, int errno,
                    242:     void (*notify)(struct inpcb *, int))
1.60      itojun    243: {
                    244:        struct inpcb *inp, *ninp;
                    245:        int nmatch;
                    246:
                    247:        nmatch = 0;
1.75      itojun    248:        for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
1.60      itojun    249:            inp != (struct inpcb *)&table->inpt_queue;
                    250:            inp = ninp) {
1.75      itojun    251:                ninp = (struct inpcb *)inp->inp_queue.cqe_next;
                    252:                if (inp->inp_af != AF_INET)
                    253:                        continue;
1.60      itojun    254:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
                    255:                        continue;
                    256:                if (in_hosteq(inp->inp_faddr, faddr) &&
                    257:                    in_hosteq(inp->inp_laddr, laddr)) {
                    258:                        (*notify)(inp, errno);
                    259:                        nmatch++;
                    260:                }
                    261:        }
                    262:
                    263:        return nmatch;
                    264: }
                    265:
                    266: void *
1.95      dyoung    267: rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.60      itojun    268: {
                    269:        struct ip *ip = v;
1.82      perry     270:        void (*notify)(struct inpcb *, int) = in_rtchange;
1.60      itojun    271:        int errno;
                    272:
                    273:        if (sa->sa_family != AF_INET ||
                    274:            sa->sa_len != sizeof(struct sockaddr_in))
                    275:                return NULL;
                    276:        if ((unsigned)cmd >= PRC_NCMDS)
                    277:                return NULL;
                    278:        errno = inetctlerrmap[cmd];
                    279:        if (PRC_IS_REDIRECT(cmd))
                    280:                notify = in_rtchange, ip = 0;
                    281:        else if (cmd == PRC_HOSTDEAD)
                    282:                ip = 0;
                    283:        else if (errno == 0)
                    284:                return NULL;
                    285:        if (ip) {
1.95      dyoung    286:                rip_pcbnotify(&rawcbtable, satocsin(sa)->sin_addr,
1.60      itojun    287:                    ip->ip_src, ip->ip_p, errno, notify);
                    288:
                    289:                /* XXX mapped address case */
                    290:        } else
1.95      dyoung    291:                in_pcbnotifyall(&rawcbtable, satocsin(sa)->sin_addr, errno,
1.60      itojun    292:                    notify);
                    293:        return NULL;
1.1       cgd       294: }
                    295:
                    296: /*
                    297:  * Generate IP header and pass packet to ip_output.
                    298:  * Tack on options user may have setup with control call.
                    299:  */
1.9       mycroft   300: int
1.24      christos  301: rip_output(struct mbuf *m, ...)
                    302: {
1.53      augustss  303:        struct inpcb *inp;
                    304:        struct ip *ip;
1.10      mycroft   305:        struct mbuf *opts;
1.24      christos  306:        int flags;
                    307:        va_list ap;
                    308:
                    309:        va_start(ap, m);
1.27      mycroft   310:        inp = va_arg(ap, struct inpcb *);
1.24      christos  311:        va_end(ap);
                    312:
1.27      mycroft   313:        flags =
1.37      matt      314:            (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
                    315:            | IP_RETURNMTU;
1.1       cgd       316:
                    317:        /*
                    318:         * If the user handed us a complete IP packet, use it.
                    319:         * Otherwise, allocate an mbuf for a header and fill it in.
                    320:         */
1.13      mycroft   321:        if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.35      thorpej   322:                if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
                    323:                        m_freem(m);
                    324:                        return (EMSGSIZE);
                    325:                }
1.68      itojun    326:                M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
                    327:                if (!m)
                    328:                        return (ENOBUFS);
1.1       cgd       329:                ip = mtod(m, struct ip *);
                    330:                ip->ip_tos = 0;
1.62      itojun    331:                ip->ip_off = htons(0);
1.13      mycroft   332:                ip->ip_p = inp->inp_ip.ip_p;
1.62      itojun    333:                ip->ip_len = htons(m->m_pkthdr.len);
1.13      mycroft   334:                ip->ip_src = inp->inp_laddr;
1.27      mycroft   335:                ip->ip_dst = inp->inp_faddr;
1.1       cgd       336:                ip->ip_ttl = MAXTTL;
1.13      mycroft   337:                opts = inp->inp_options;
                    338:        } else {
1.35      thorpej   339:                if (m->m_pkthdr.len > IP_MAXPACKET) {
                    340:                        m_freem(m);
                    341:                        return (EMSGSIZE);
                    342:                }
1.13      mycroft   343:                ip = mtod(m, struct ip *);
1.65      thorpej   344:
                    345:                /*
                    346:                 * If the mbuf is read-only, we need to allocate
                    347:                 * a new mbuf for the header, since we need to
                    348:                 * modify the header.
                    349:                 */
                    350:                if (M_READONLY(m)) {
                    351:                        int hlen = ip->ip_hl << 2;
                    352:
                    353:                        m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
                    354:                        if (m == NULL)
                    355:                                return (ENOMEM);        /* XXX */
                    356:                        ip = mtod(m, struct ip *);
                    357:                }
                    358:
1.62      itojun    359:                /* XXX userland passes ip_len and ip_off in host order */
1.38      mycroft   360:                if (m->m_pkthdr.len != ip->ip_len) {
                    361:                        m_freem(m);
                    362:                        return (EINVAL);
                    363:                }
1.62      itojun    364:                HTONS(ip->ip_len);
                    365:                HTONS(ip->ip_off);
1.103     matt      366:                if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
                    367:                        flags |= IP_NOIPNEWID;
1.13      mycroft   368:                opts = NULL;
                    369:                /* XXX prevent ip_output from overwriting header fields */
                    370:                flags |= IP_RAWOUTPUT;
1.105   ! thorpej   371:                IP_STATINC(IP_STAT_RAWOUT);
1.1       cgd       372:        }
1.62      itojun    373:        return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
1.73      itojun    374:             inp->inp_socket, &inp->inp_errormtu));
1.1       cgd       375: }
                    376:
                    377: /*
                    378:  * Raw IP socket option processing.
                    379:  */
1.9       mycroft   380: int
1.83      perry     381: rip_ctloutput(int op, struct socket *so, int level, int optname,
                    382:     struct mbuf **m)
1.1       cgd       383: {
1.53      augustss  384:        struct inpcb *inp = sotoinpcb(so);
1.31      mycroft   385:        int error = 0;
1.1       cgd       386:
1.100     dyoung    387:        if (level == SOL_SOCKET && optname == SO_NOHEADER) {
                    388:                if (op == PRCO_GETOPT) {
                    389:                        *m = m_intopt(so,
                    390:                            (inp->inp_flags & INP_NOHEADER) ? 1 : 0);
                    391:                        return 0;
1.101     christos  392:                } else if (*m == NULL || (*m)->m_len != sizeof(int))
1.100     dyoung    393:                        error = EINVAL;
                    394:                else if (*mtod(*m, int *)) {
                    395:                        inp->inp_flags &= ~INP_HDRINCL;
                    396:                        inp->inp_flags |= INP_NOHEADER;
                    397:                } else
                    398:                        inp->inp_flags &= ~INP_NOHEADER;
                    399:                goto free_m;
                    400:        } else if (level != IPPROTO_IP)
                    401:                return ip_ctloutput(op, so, level, optname, m);
                    402:
                    403:        switch (op) {
1.31      mycroft   404:
                    405:        case PRCO_SETOPT:
                    406:                switch (optname) {
                    407:                case IP_HDRINCL:
1.101     christos  408:                        if (*m == NULL || (*m)->m_len != sizeof(int))
1.31      mycroft   409:                                error = EINVAL;
1.100     dyoung    410:                        else if (*mtod(*m, int *))
                    411:                                inp->inp_flags |= INP_HDRINCL;
                    412:                        else
                    413:                                inp->inp_flags &= ~INP_HDRINCL;
                    414:                        goto free_m;
1.31      mycroft   415:
                    416: #ifdef MROUTING
                    417:                case MRT_INIT:
                    418:                case MRT_DONE:
                    419:                case MRT_ADD_VIF:
                    420:                case MRT_DEL_VIF:
                    421:                case MRT_ADD_MFC:
                    422:                case MRT_DEL_MFC:
                    423:                case MRT_ASSERT:
1.81      manu      424:                case MRT_API_CONFIG:
                    425:                case MRT_ADD_BW_UPCALL:
                    426:                case MRT_DEL_BW_UPCALL:
1.31      mycroft   427:                        error = ip_mrouter_set(so, optname, m);
                    428:                        break;
                    429: #endif
                    430:
                    431:                default:
                    432:                        error = ip_ctloutput(op, so, level, optname, m);
                    433:                        break;
1.13      mycroft   434:                }
                    435:                break;
1.1       cgd       436:
1.31      mycroft   437:        case PRCO_GETOPT:
                    438:                switch (optname) {
                    439:                case IP_HDRINCL:
1.100     dyoung    440:                        *m = m_intopt(so, inp->inp_flags & INP_HDRINCL ? 1 : 0);
1.31      mycroft   441:                        break;
                    442:
1.6       hpeyerl   443: #ifdef MROUTING
1.31      mycroft   444:                case MRT_VERSION:
                    445:                case MRT_ASSERT:
1.81      manu      446:                case MRT_API_SUPPORT:
                    447:                case MRT_API_CONFIG:
1.31      mycroft   448:                        error = ip_mrouter_get(so, optname, m);
1.18      mycroft   449:                        break;
1.31      mycroft   450: #endif
                    451:
1.18      mycroft   452:                default:
1.31      mycroft   453:                        error = ip_ctloutput(op, so, level, optname, m);
1.18      mycroft   454:                        break;
                    455:                }
1.31      mycroft   456:                break;
1.1       cgd       457:        }
1.100     dyoung    458:        return error;
                    459: free_m:
                    460:        if (op == PRCO_SETOPT && *m != NULL)
                    461:                (void)m_free(*m);
                    462:        return error;
1.1       cgd       463: }
                    464:
1.27      mycroft   465: int
1.83      perry     466: rip_bind(struct inpcb *inp, struct mbuf *nam)
1.29      mycroft   467: {
                    468:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    469:
                    470:        if (nam->m_len != sizeof(*addr))
                    471:                return (EINVAL);
1.58      matt      472:        if (TAILQ_FIRST(&ifnet) == 0)
1.29      mycroft   473:                return (EADDRNOTAVAIL);
                    474:        if (addr->sin_family != AF_INET &&
                    475:            addr->sin_family != AF_IMPLINK)
                    476:                return (EAFNOSUPPORT);
1.32      mycroft   477:        if (!in_nullhost(addr->sin_addr) &&
1.29      mycroft   478:            ifa_ifwithaddr(sintosa(addr)) == 0)
                    479:                return (EADDRNOTAVAIL);
                    480:        inp->inp_laddr = addr->sin_addr;
                    481:        return (0);
                    482: }
                    483:
                    484: int
1.83      perry     485: rip_connect(struct inpcb *inp, struct mbuf *nam)
1.27      mycroft   486: {
                    487:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    488:
                    489:        if (nam->m_len != sizeof(*addr))
                    490:                return (EINVAL);
1.58      matt      491:        if (TAILQ_FIRST(&ifnet) == 0)
1.27      mycroft   492:                return (EADDRNOTAVAIL);
                    493:        if (addr->sin_family != AF_INET &&
                    494:            addr->sin_family != AF_IMPLINK)
                    495:                return (EAFNOSUPPORT);
                    496:        inp->inp_faddr = addr->sin_addr;
                    497:        return (0);
                    498: }
                    499:
                    500: void
1.83      perry     501: rip_disconnect(struct inpcb *inp)
1.27      mycroft   502: {
                    503:
1.32      mycroft   504:        inp->inp_faddr = zeroin_addr;
1.27      mycroft   505: }
                    506:
1.13      mycroft   507: u_long rip_sendspace = RIPSNDQ;
                    508: u_long rip_recvspace = RIPRCVQ;
                    509:
1.1       cgd       510: /*ARGSUSED*/
1.9       mycroft   511: int
1.83      perry     512: rip_usrreq(struct socket *so, int req,
1.88      christos  513:     struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
1.1       cgd       514: {
1.53      augustss  515:        struct inpcb *inp;
1.27      mycroft   516:        int s;
1.53      augustss  517:        int error = 0;
1.13      mycroft   518: #ifdef MROUTING
1.6       hpeyerl   519:        extern struct socket *ip_mrouter;
                    520: #endif
1.27      mycroft   521:
1.22      pk        522:        if (req == PRU_CONTROL)
1.96      christos  523:                return (in_control(so, (long)m, (void *)nam,
1.90      ad        524:                    (struct ifnet *)control, l));
1.49      thorpej   525:
1.93      tls       526:        s = splsoftnet();
                    527:
1.50      thorpej   528:        if (req == PRU_PURGEIF) {
1.56      itojun    529:                in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50      thorpej   530:                in_purgeif((struct ifnet *)control);
                    531:                in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.93      tls       532:                splx(s);
1.49      thorpej   533:                return (0);
                    534:        }
1.22      pk        535:
1.27      mycroft   536:        inp = sotoinpcb(so);
1.28      mycroft   537: #ifdef DIAGNOSTIC
                    538:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    539:                panic("rip_usrreq: unexpected control mbuf");
                    540: #endif
1.27      mycroft   541:        if (inp == 0 && req != PRU_ATTACH) {
1.22      pk        542:                error = EINVAL;
                    543:                goto release;
                    544:        }
                    545:
1.1       cgd       546:        switch (req) {
                    547:
                    548:        case PRU_ATTACH:
1.27      mycroft   549:                if (inp != 0) {
                    550:                        error = EISCONN;
                    551:                        break;
                    552:                }
1.94      elad      553:
                    554:                if (l == NULL) {
1.13      mycroft   555:                        error = EACCES;
                    556:                        break;
                    557:                }
1.94      elad      558:
                    559:                /* XXX: raw socket permissions are checked in socreate() */
                    560:
1.27      mycroft   561:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    562:                        error = soreserve(so, rip_sendspace, rip_recvspace);
                    563:                        if (error)
                    564:                                break;
                    565:                }
                    566:                error = in_pcballoc(so, &rawcbtable);
                    567:                if (error)
1.13      mycroft   568:                        break;
1.27      mycroft   569:                inp = sotoinpcb(so);
1.17      cgd       570:                inp->inp_ip.ip_p = (long)nam;
1.1       cgd       571:                break;
                    572:
                    573:        case PRU_DETACH:
1.13      mycroft   574: #ifdef MROUTING
1.6       hpeyerl   575:                if (so == ip_mrouter)
                    576:                        ip_mrouter_done();
                    577: #endif
1.13      mycroft   578:                in_pcbdetach(inp);
1.1       cgd       579:                break;
                    580:
                    581:        case PRU_BIND:
1.29      mycroft   582:                error = rip_bind(inp, nam);
1.27      mycroft   583:                break;
                    584:
                    585:        case PRU_LISTEN:
                    586:                error = EOPNOTSUPP;
                    587:                break;
                    588:
                    589:        case PRU_CONNECT:
                    590:                error = rip_connect(inp, nam);
                    591:                if (error)
1.13      mycroft   592:                        break;
1.1       cgd       593:                soisconnected(so);
1.13      mycroft   594:                break;
                    595:
                    596:        case PRU_CONNECT2:
                    597:                error = EOPNOTSUPP;
                    598:                break;
                    599:
1.27      mycroft   600:        case PRU_DISCONNECT:
                    601:                soisdisconnected(so);
                    602:                rip_disconnect(inp);
                    603:                break;
                    604:
1.13      mycroft   605:        /*
                    606:         * Mark the connection as being incapable of further input.
                    607:         */
                    608:        case PRU_SHUTDOWN:
                    609:                socantsendmore(so);
                    610:                break;
                    611:
1.27      mycroft   612:        case PRU_RCVD:
                    613:                error = EOPNOTSUPP;
                    614:                break;
                    615:
1.13      mycroft   616:        /*
                    617:         * Ship a packet out.  The appropriate raw output
                    618:         * routine handles any massaging necessary.
                    619:         */
                    620:        case PRU_SEND:
1.28      mycroft   621:                if (control && control->m_len) {
                    622:                        m_freem(control);
                    623:                        m_freem(m);
                    624:                        error = EINVAL;
                    625:                        break;
                    626:                }
                    627:        {
1.27      mycroft   628:                if (nam) {
                    629:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13      mycroft   630:                                error = EISCONN;
1.28      mycroft   631:                                goto die;
1.13      mycroft   632:                        }
1.27      mycroft   633:                        error = rip_connect(inp, nam);
                    634:                        if (error) {
1.28      mycroft   635:                        die:
1.27      mycroft   636:                                m_freem(m);
                    637:                                break;
                    638:                        }
1.13      mycroft   639:                } else {
1.27      mycroft   640:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13      mycroft   641:                                error = ENOTCONN;
1.28      mycroft   642:                                goto die;
1.13      mycroft   643:                        }
                    644:                }
1.27      mycroft   645:                error = rip_output(m, inp);
                    646:                if (nam)
                    647:                        rip_disconnect(inp);
1.28      mycroft   648:        }
1.13      mycroft   649:                break;
                    650:
                    651:        case PRU_SENSE:
                    652:                /*
                    653:                 * stat: don't bother with a blocksize.
                    654:                 */
1.27      mycroft   655:                splx(s);
1.1       cgd       656:                return (0);
1.13      mycroft   657:
                    658:        case PRU_RCVOOB:
1.27      mycroft   659:                error = EOPNOTSUPP;
                    660:                break;
                    661:
1.13      mycroft   662:        case PRU_SENDOOB:
1.28      mycroft   663:                m_freem(control);
1.27      mycroft   664:                m_freem(m);
1.13      mycroft   665:                error = EOPNOTSUPP;
                    666:                break;
                    667:
                    668:        case PRU_SOCKADDR:
                    669:                in_setsockaddr(inp, nam);
                    670:                break;
                    671:
                    672:        case PRU_PEERADDR:
                    673:                in_setpeeraddr(inp, nam);
                    674:                break;
                    675:
                    676:        default:
                    677:                panic("rip_usrreq");
1.1       cgd       678:        }
1.27      mycroft   679:
1.22      pk        680: release:
1.27      mycroft   681:        splx(s);
1.1       cgd       682:        return (error);
                    683: }
1.84      atatat    684:
                    685: SYSCTL_SETUP(sysctl_net_inet_raw_setup, "sysctl net.inet.raw subtree setup")
                    686: {
                    687:
                    688:        sysctl_createv(clog, 0, NULL, NULL,
                    689:                       CTLFLAG_PERMANENT,
                    690:                       CTLTYPE_NODE, "net", NULL,
                    691:                       NULL, 0, NULL, 0,
                    692:                       CTL_NET, CTL_EOL);
                    693:        sysctl_createv(clog, 0, NULL, NULL,
                    694:                       CTLFLAG_PERMANENT,
                    695:                       CTLTYPE_NODE, "inet", NULL,
                    696:                       NULL, 0, NULL, 0,
                    697:                       CTL_NET, PF_INET, CTL_EOL);
                    698:        sysctl_createv(clog, 0, NULL, NULL,
                    699:                       CTLFLAG_PERMANENT,
                    700:                       CTLTYPE_NODE, "raw",
                    701:                       SYSCTL_DESCR("Raw IPv4 settings"),
                    702:                       NULL, 0, NULL, 0,
                    703:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
                    704:
                    705:        sysctl_createv(clog, 0, NULL, NULL,
                    706:                       CTLFLAG_PERMANENT,
1.86      atatat    707:                       CTLTYPE_STRUCT, "pcblist",
1.84      atatat    708:                       SYSCTL_DESCR("Raw IPv4 control block list"),
                    709:                       sysctl_inpcblist, 0, &rawcbtable, 0,
                    710:                       CTL_NET, PF_INET, IPPROTO_RAW,
                    711:                       CTL_CREATE, CTL_EOL);
                    712: }

CVSweb <webmaster@jp.NetBSD.org>