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

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

CVSweb <webmaster@jp.NetBSD.org>