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

1.81.6.2! yamt        1: /*     $NetBSD: raw_ip.c,v 1.81.6.1 2005/02/12 18:17:54 yamt 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.81.6.2! yamt       64: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.81.6.1 2005/02/12 18:17:54 yamt 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.81.6.2! yamt       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.1       cgd        80:
1.7       mycroft    81: #include <net/if.h>
                     82: #include <net/route.h>
1.1       cgd        83:
1.7       mycroft    84: #include <netinet/in.h>
                     85: #include <netinet/in_systm.h>
                     86: #include <netinet/ip.h>
                     87: #include <netinet/ip_var.h>
1.13      mycroft    88: #include <netinet/ip_mroute.h>
1.44      darrenr    89: #include <netinet/ip_icmp.h>
1.7       mycroft    90: #include <netinet/in_pcb.h>
1.24      christos   91: #include <netinet/in_var.h>
                     92:
                     93: #include <machine/stdarg.h>
1.13      mycroft    94:
1.43      itojun     95: #ifdef IPSEC
                     96: #include <netinet6/ipsec.h>
                     97: #endif /*IPSEC*/
                     98:
1.72      jonathan   99: #ifdef FAST_IPSEC
                    100: #include <netipsec/ipsec.h>
1.80      jonathan  101: #include <netipsec/ipsec_var.h>                        /* XXX ipsecstat namespace */
1.72      jonathan  102: #endif /* FAST_IPSEC*/
                    103:
1.20      mycroft   104: struct inpcbtable rawcbtable;
1.30      pk        105:
1.81.6.1  yamt      106: int     rip_pcbnotify(struct inpcbtable *, struct in_addr,
                    107:     struct in_addr, int, int, void (*)(struct inpcb *, int));
                    108: int     rip_bind(struct inpcb *, struct mbuf *);
                    109: int     rip_connect(struct inpcb *, struct mbuf *);
                    110: void    rip_disconnect(struct inpcb *);
1.13      mycroft   111:
                    112: /*
                    113:  * Nominal space allocated to a raw ip socket.
                    114:  */
                    115: #define        RIPSNDQ         8192
                    116: #define        RIPRCVQ         8192
1.1       cgd       117:
                    118: /*
                    119:  * Raw interface to IP protocol.
                    120:  */
1.13      mycroft   121:
                    122: /*
                    123:  * Initialize raw connection block q.
                    124:  */
                    125: void
1.81.6.1  yamt      126: rip_init(void)
1.13      mycroft   127: {
                    128:
1.33      mycroft   129:        in_pcbinit(&rawcbtable, 1, 1);
1.13      mycroft   130: }
                    131:
1.1       cgd       132: /*
                    133:  * Setup generic address and protocol structures
                    134:  * for raw_input routine, then pass them along with
                    135:  * mbuf chain.
                    136:  */
1.9       mycroft   137: void
1.24      christos  138: rip_input(struct mbuf *m, ...)
1.1       cgd       139: {
1.63      simonb    140:        int proto;
1.53      augustss  141:        struct ip *ip = mtod(m, struct ip *);
1.75      itojun    142:        struct inpcb_hdr *inph;
1.53      augustss  143:        struct inpcb *inp;
1.36      thorpej   144:        struct inpcb *last = 0;
                    145:        struct mbuf *opts = 0;
1.32      mycroft   146:        struct sockaddr_in ripsrc;
1.43      itojun    147:        va_list ap;
                    148:
                    149:        va_start(ap, m);
1.64      simonb    150:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.43      itojun    151:        proto = va_arg(ap, int);
                    152:        va_end(ap);
1.1       cgd       153:
1.32      mycroft   154:        ripsrc.sin_family = AF_INET;
                    155:        ripsrc.sin_len = sizeof(struct sockaddr_in);
1.1       cgd       156:        ripsrc.sin_addr = ip->ip_src;
1.34      mycroft   157:        ripsrc.sin_port = 0;
                    158:        bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero));
1.42      thorpej   159:
                    160:        /*
                    161:         * XXX Compatibility: programs using raw IP expect ip_len
1.62      itojun    162:         * XXX to have the header length subtracted, and in host order.
                    163:         * XXX ip_off is also expected to be host order.
1.42      thorpej   164:         */
1.62      itojun    165:        ip->ip_len = ntohs(ip->ip_len) - (ip->ip_hl << 2);
                    166:        NTOHS(ip->ip_off);
1.32      mycroft   167:
1.75      itojun    168:        CIRCLEQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
                    169:                inp = (struct inpcb *)inph;
                    170:                if (inp->inp_af != AF_INET)
                    171:                        continue;
1.43      itojun    172:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
1.13      mycroft   173:                        continue;
1.32      mycroft   174:                if (!in_nullhost(inp->inp_laddr) &&
                    175:                    !in_hosteq(inp->inp_laddr, ip->ip_dst))
1.13      mycroft   176:                        continue;
1.32      mycroft   177:                if (!in_nullhost(inp->inp_faddr) &&
                    178:                    !in_hosteq(inp->inp_faddr, ip->ip_src))
1.13      mycroft   179:                        continue;
                    180:                if (last) {
                    181:                        struct mbuf *n;
1.55      itojun    182:
1.72      jonathan  183: #if defined(IPSEC) || defined(FAST_IPSEC)
1.55      itojun    184:                        /* check AH/ESP integrity. */
                    185:                        if (ipsec4_in_reject_so(m, last->inp_socket)) {
                    186:                                ipsecstat.in_polvio++;
                    187:                                /* do not inject data to pcb */
                    188:                        } else
                    189: #endif /*IPSEC*/
1.24      christos  190:                        if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1.36      thorpej   191:                                if (last->inp_flags & INP_CONTROLOPTS ||
                    192:                                    last->inp_socket->so_options & SO_TIMESTAMP)
                    193:                                        ip_savecontrol(last, &opts, ip, n);
                    194:                                if (sbappendaddr(&last->inp_socket->so_rcv,
                    195:                                    sintosa(&ripsrc), n, opts) == 0) {
1.13      mycroft   196:                                        /* should notify about lost packet */
                    197:                                        m_freem(n);
1.36      thorpej   198:                                        if (opts)
                    199:                                                m_freem(opts);
                    200:                                } else
                    201:                                        sorwakeup(last->inp_socket);
1.41      thorpej   202:                                opts = NULL;
1.13      mycroft   203:                        }
                    204:                }
1.36      thorpej   205:                last = inp;
1.13      mycroft   206:        }
1.72      jonathan  207: #if defined(IPSEC) || defined(FAST_IPSEC)
1.55      itojun    208:        /* check AH/ESP integrity. */
                    209:        if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
                    210:                m_freem(m);
                    211:                ipsecstat.in_polvio++;
                    212:                ipstat.ips_delivered--;
                    213:                /* do not inject data to pcb */
                    214:        } else
                    215: #endif /*IPSEC*/
1.13      mycroft   216:        if (last) {
1.36      thorpej   217:                if (last->inp_flags & INP_CONTROLOPTS ||
                    218:                    last->inp_socket->so_options & SO_TIMESTAMP)
                    219:                        ip_savecontrol(last, &opts, ip, m);
                    220:                if (sbappendaddr(&last->inp_socket->so_rcv,
                    221:                    sintosa(&ripsrc), m, opts) == 0) {
1.13      mycroft   222:                        m_freem(m);
1.36      thorpej   223:                        if (opts)
                    224:                                m_freem(opts);
                    225:                } else
                    226:                        sorwakeup(last->inp_socket);
1.13      mycroft   227:        } else {
1.44      darrenr   228:                if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
1.47      itojun    229:                        icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
                    230:                            0, 0);
1.44      darrenr   231:                        ipstat.ips_noproto++;
                    232:                        ipstat.ips_delivered--;
                    233:                } else
                    234:                        m_freem(m);
1.1       cgd       235:        }
1.43      itojun    236:        return;
1.60      itojun    237: }
                    238:
                    239: int
1.81.6.1  yamt      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.81.6.1  yamt      267: rip_ctlinput(int cmd, struct sockaddr *sa, void *v)
1.60      itojun    268: {
                    269:        struct ip *ip = v;
1.81.6.1  yamt      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) {
                    286:                rip_pcbnotify(&rawcbtable, satosin(sa)->sin_addr,
                    287:                    ip->ip_src, ip->ip_p, errno, notify);
                    288:
                    289:                /* XXX mapped address case */
                    290:        } else
                    291:                in_pcbnotifyall(&rawcbtable, satosin(sa)->sin_addr, errno,
                    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.13      mycroft   366:                if (ip->ip_id == 0)
1.77      jonathan  367:                        ip->ip_id = ip_newid();
1.13      mycroft   368:                opts = NULL;
                    369:                /* XXX prevent ip_output from overwriting header fields */
                    370:                flags |= IP_RAWOUTPUT;
                    371:                ipstat.ips_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.81.6.1  yamt      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.15      mycroft   387:        if (level != IPPROTO_IP) {
1.31      mycroft   388:                error = ENOPROTOOPT;
                    389:                if (op == PRCO_SETOPT && *m != 0)
                    390:                        (void) m_free(*m);
                    391:        } else switch (op) {
                    392:
                    393:        case PRCO_SETOPT:
                    394:                switch (optname) {
                    395:                case IP_HDRINCL:
                    396:                        if (*m == 0 || (*m)->m_len < sizeof (int))
                    397:                                error = EINVAL;
                    398:                        else {
1.13      mycroft   399:                                if (*mtod(*m, int *))
                    400:                                        inp->inp_flags |= INP_HDRINCL;
                    401:                                else
                    402:                                        inp->inp_flags &= ~INP_HDRINCL;
                    403:                        }
1.31      mycroft   404:                        if (*m != 0)
                    405:                                (void) m_free(*m);
                    406:                        break;
                    407:
                    408: #ifdef MROUTING
                    409:                case MRT_INIT:
                    410:                case MRT_DONE:
                    411:                case MRT_ADD_VIF:
                    412:                case MRT_DEL_VIF:
                    413:                case MRT_ADD_MFC:
                    414:                case MRT_DEL_MFC:
                    415:                case MRT_ASSERT:
1.81      manu      416:                case MRT_API_CONFIG:
                    417:                case MRT_ADD_BW_UPCALL:
                    418:                case MRT_DEL_BW_UPCALL:
1.31      mycroft   419:                        error = ip_mrouter_set(so, optname, m);
                    420:                        break;
                    421: #endif
                    422:
                    423:                default:
                    424:                        error = ip_ctloutput(op, so, level, optname, m);
                    425:                        break;
1.13      mycroft   426:                }
                    427:                break;
1.1       cgd       428:
1.31      mycroft   429:        case PRCO_GETOPT:
                    430:                switch (optname) {
                    431:                case IP_HDRINCL:
1.66      thorpej   432:                        *m = m_get(M_WAIT, MT_SOOPTS);
1.67      matt      433:                        MCLAIM((*m), so->so_mowner);
1.31      mycroft   434:                        (*m)->m_len = sizeof (int);
                    435:                        *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
                    436:                        break;
                    437:
1.6       hpeyerl   438: #ifdef MROUTING
1.31      mycroft   439:                case MRT_VERSION:
                    440:                case MRT_ASSERT:
1.81      manu      441:                case MRT_API_SUPPORT:
                    442:                case MRT_API_CONFIG:
1.31      mycroft   443:                        error = ip_mrouter_get(so, optname, m);
1.18      mycroft   444:                        break;
1.31      mycroft   445: #endif
                    446:
1.18      mycroft   447:                default:
1.31      mycroft   448:                        error = ip_ctloutput(op, so, level, optname, m);
1.18      mycroft   449:                        break;
                    450:                }
1.31      mycroft   451:                break;
1.1       cgd       452:        }
1.31      mycroft   453:        return (error);
1.1       cgd       454: }
                    455:
1.27      mycroft   456: int
1.81.6.1  yamt      457: rip_bind(struct inpcb *inp, struct mbuf *nam)
1.29      mycroft   458: {
                    459:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    460:
                    461:        if (nam->m_len != sizeof(*addr))
                    462:                return (EINVAL);
1.58      matt      463:        if (TAILQ_FIRST(&ifnet) == 0)
1.29      mycroft   464:                return (EADDRNOTAVAIL);
                    465:        if (addr->sin_family != AF_INET &&
                    466:            addr->sin_family != AF_IMPLINK)
                    467:                return (EAFNOSUPPORT);
1.32      mycroft   468:        if (!in_nullhost(addr->sin_addr) &&
1.29      mycroft   469:            ifa_ifwithaddr(sintosa(addr)) == 0)
                    470:                return (EADDRNOTAVAIL);
                    471:        inp->inp_laddr = addr->sin_addr;
                    472:        return (0);
                    473: }
                    474:
                    475: int
1.81.6.1  yamt      476: rip_connect(struct inpcb *inp, struct mbuf *nam)
1.27      mycroft   477: {
                    478:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    479:
                    480:        if (nam->m_len != sizeof(*addr))
                    481:                return (EINVAL);
1.58      matt      482:        if (TAILQ_FIRST(&ifnet) == 0)
1.27      mycroft   483:                return (EADDRNOTAVAIL);
                    484:        if (addr->sin_family != AF_INET &&
                    485:            addr->sin_family != AF_IMPLINK)
                    486:                return (EAFNOSUPPORT);
                    487:        inp->inp_faddr = addr->sin_addr;
                    488:        return (0);
                    489: }
                    490:
                    491: void
1.81.6.1  yamt      492: rip_disconnect(struct inpcb *inp)
1.27      mycroft   493: {
                    494:
1.32      mycroft   495:        inp->inp_faddr = zeroin_addr;
1.27      mycroft   496: }
                    497:
1.13      mycroft   498: u_long rip_sendspace = RIPSNDQ;
                    499: u_long rip_recvspace = RIPRCVQ;
                    500:
1.1       cgd       501: /*ARGSUSED*/
1.9       mycroft   502: int
1.81.6.1  yamt      503: rip_usrreq(struct socket *so, int req,
                    504:     struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct proc *p)
1.1       cgd       505: {
1.53      augustss  506:        struct inpcb *inp;
1.27      mycroft   507:        int s;
1.53      augustss  508:        int error = 0;
1.13      mycroft   509: #ifdef MROUTING
1.6       hpeyerl   510:        extern struct socket *ip_mrouter;
                    511: #endif
1.27      mycroft   512:
1.22      pk        513:        if (req == PRU_CONTROL)
                    514:                return (in_control(so, (long)m, (caddr_t)nam,
1.26      mycroft   515:                    (struct ifnet *)control, p));
1.49      thorpej   516:
1.50      thorpej   517:        if (req == PRU_PURGEIF) {
1.56      itojun    518:                in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50      thorpej   519:                in_purgeif((struct ifnet *)control);
                    520:                in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.49      thorpej   521:                return (0);
                    522:        }
1.22      pk        523:
1.27      mycroft   524:        s = splsoftnet();
                    525:        inp = sotoinpcb(so);
1.28      mycroft   526: #ifdef DIAGNOSTIC
                    527:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    528:                panic("rip_usrreq: unexpected control mbuf");
                    529: #endif
1.27      mycroft   530:        if (inp == 0 && req != PRU_ATTACH) {
1.22      pk        531:                error = EINVAL;
                    532:                goto release;
                    533:        }
                    534:
1.1       cgd       535:        switch (req) {
                    536:
                    537:        case PRU_ATTACH:
1.27      mycroft   538:                if (inp != 0) {
                    539:                        error = EISCONN;
                    540:                        break;
                    541:                }
1.26      mycroft   542:                if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
1.13      mycroft   543:                        error = EACCES;
                    544:                        break;
                    545:                }
1.27      mycroft   546:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    547:                        error = soreserve(so, rip_sendspace, rip_recvspace);
                    548:                        if (error)
                    549:                                break;
                    550:                }
                    551:                error = in_pcballoc(so, &rawcbtable);
                    552:                if (error)
1.13      mycroft   553:                        break;
1.27      mycroft   554:                inp = sotoinpcb(so);
1.17      cgd       555:                inp->inp_ip.ip_p = (long)nam;
1.1       cgd       556:                break;
                    557:
                    558:        case PRU_DETACH:
1.13      mycroft   559: #ifdef MROUTING
1.6       hpeyerl   560:                if (so == ip_mrouter)
                    561:                        ip_mrouter_done();
                    562: #endif
1.13      mycroft   563:                in_pcbdetach(inp);
1.1       cgd       564:                break;
                    565:
                    566:        case PRU_BIND:
1.29      mycroft   567:                error = rip_bind(inp, nam);
1.27      mycroft   568:                break;
                    569:
                    570:        case PRU_LISTEN:
                    571:                error = EOPNOTSUPP;
                    572:                break;
                    573:
                    574:        case PRU_CONNECT:
                    575:                error = rip_connect(inp, nam);
                    576:                if (error)
1.13      mycroft   577:                        break;
1.1       cgd       578:                soisconnected(so);
1.13      mycroft   579:                break;
                    580:
                    581:        case PRU_CONNECT2:
                    582:                error = EOPNOTSUPP;
                    583:                break;
                    584:
1.27      mycroft   585:        case PRU_DISCONNECT:
                    586:                soisdisconnected(so);
                    587:                rip_disconnect(inp);
                    588:                break;
                    589:
1.13      mycroft   590:        /*
                    591:         * Mark the connection as being incapable of further input.
                    592:         */
                    593:        case PRU_SHUTDOWN:
                    594:                socantsendmore(so);
                    595:                break;
                    596:
1.27      mycroft   597:        case PRU_RCVD:
                    598:                error = EOPNOTSUPP;
                    599:                break;
                    600:
1.13      mycroft   601:        /*
                    602:         * Ship a packet out.  The appropriate raw output
                    603:         * routine handles any massaging necessary.
                    604:         */
                    605:        case PRU_SEND:
1.28      mycroft   606:                if (control && control->m_len) {
                    607:                        m_freem(control);
                    608:                        m_freem(m);
                    609:                        error = EINVAL;
                    610:                        break;
                    611:                }
                    612:        {
1.27      mycroft   613:                if (nam) {
                    614:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13      mycroft   615:                                error = EISCONN;
1.28      mycroft   616:                                goto die;
1.13      mycroft   617:                        }
1.27      mycroft   618:                        error = rip_connect(inp, nam);
                    619:                        if (error) {
1.28      mycroft   620:                        die:
1.27      mycroft   621:                                m_freem(m);
                    622:                                break;
                    623:                        }
1.13      mycroft   624:                } else {
1.27      mycroft   625:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13      mycroft   626:                                error = ENOTCONN;
1.28      mycroft   627:                                goto die;
1.13      mycroft   628:                        }
                    629:                }
1.27      mycroft   630:                error = rip_output(m, inp);
                    631:                if (nam)
                    632:                        rip_disconnect(inp);
1.28      mycroft   633:        }
1.13      mycroft   634:                break;
                    635:
                    636:        case PRU_SENSE:
                    637:                /*
                    638:                 * stat: don't bother with a blocksize.
                    639:                 */
1.27      mycroft   640:                splx(s);
1.1       cgd       641:                return (0);
1.13      mycroft   642:
                    643:        case PRU_RCVOOB:
1.27      mycroft   644:                error = EOPNOTSUPP;
                    645:                break;
                    646:
1.13      mycroft   647:        case PRU_SENDOOB:
1.28      mycroft   648:                m_freem(control);
1.27      mycroft   649:                m_freem(m);
1.13      mycroft   650:                error = EOPNOTSUPP;
                    651:                break;
                    652:
                    653:        case PRU_SOCKADDR:
                    654:                in_setsockaddr(inp, nam);
                    655:                break;
                    656:
                    657:        case PRU_PEERADDR:
                    658:                in_setpeeraddr(inp, nam);
                    659:                break;
                    660:
                    661:        default:
                    662:                panic("rip_usrreq");
1.1       cgd       663:        }
1.27      mycroft   664:
1.22      pk        665: release:
1.27      mycroft   666:        splx(s);
1.1       cgd       667:        return (error);
                    668: }
1.81.6.2! yamt      669:
        !           670: SYSCTL_SETUP(sysctl_net_inet_raw_setup, "sysctl net.inet.raw subtree setup")
        !           671: {
        !           672:
        !           673:        sysctl_createv(clog, 0, NULL, NULL,
        !           674:                       CTLFLAG_PERMANENT,
        !           675:                       CTLTYPE_NODE, "net", NULL,
        !           676:                       NULL, 0, NULL, 0,
        !           677:                       CTL_NET, CTL_EOL);
        !           678:        sysctl_createv(clog, 0, NULL, NULL,
        !           679:                       CTLFLAG_PERMANENT,
        !           680:                       CTLTYPE_NODE, "inet", NULL,
        !           681:                       NULL, 0, NULL, 0,
        !           682:                       CTL_NET, PF_INET, CTL_EOL);
        !           683:        sysctl_createv(clog, 0, NULL, NULL,
        !           684:                       CTLFLAG_PERMANENT,
        !           685:                       CTLTYPE_NODE, "raw",
        !           686:                       SYSCTL_DESCR("Raw IPv4 settings"),
        !           687:                       NULL, 0, NULL, 0,
        !           688:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
        !           689:
        !           690:        sysctl_createv(clog, 0, NULL, NULL,
        !           691:                       CTLFLAG_PERMANENT,
        !           692:                       CTLTYPE_STRUCT, "pcblist",
        !           693:                       SYSCTL_DESCR("Raw IPv4 control block list"),
        !           694:                       sysctl_inpcblist, 0, &rawcbtable, 0,
        !           695:                       CTL_NET, PF_INET, IPPROTO_RAW,
        !           696:                       CTL_CREATE, CTL_EOL);
        !           697: }

CVSweb <webmaster@jp.NetBSD.org>