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

1.87    ! yamt        1: /*     $NetBSD: raw_ip.c,v 1.86 2005/03/11 06:16:16 atatat 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.87    ! yamt       64: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.86 2005/03/11 06:16:16 atatat 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.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.87    ! yamt       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.72      jonathan  100: #ifdef FAST_IPSEC
                    101: #include <netipsec/ipsec.h>
1.80      jonathan  102: #include <netipsec/ipsec_var.h>                        /* XXX ipsecstat namespace */
1.72      jonathan  103: #endif /* FAST_IPSEC*/
                    104:
1.20      mycroft   105: struct inpcbtable rawcbtable;
1.30      pk        106:
1.82      perry     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.83      perry     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.75      itojun    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.75      itojun    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.72      jonathan  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.72      jonathan  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.83      perry     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.75      itojun    249:        for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
1.60      itojun    250:            inp != (struct inpcb *)&table->inpt_queue;
                    251:            inp = ninp) {
1.75      itojun    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.83      perry     268: rip_ctlinput(int cmd, struct sockaddr *sa, void *v)
1.60      itojun    269: {
                    270:        struct ip *ip = v;
1.82      perry     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.77      jonathan  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.73      itojun    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.83      perry     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.81      manu      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.81      manu      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.83      perry     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.83      perry     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.83      perry     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.83      perry     504: rip_usrreq(struct socket *so, int req,
                    505:     struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct proc *p)
1.1       cgd       506: {
1.53      augustss  507:        struct inpcb *inp;
1.27      mycroft   508:        int s;
1.53      augustss  509:        int error = 0;
1.13      mycroft   510: #ifdef MROUTING
1.6       hpeyerl   511:        extern struct socket *ip_mrouter;
                    512: #endif
1.27      mycroft   513:
1.22      pk        514:        if (req == PRU_CONTROL)
                    515:                return (in_control(so, (long)m, (caddr_t)nam,
1.26      mycroft   516:                    (struct ifnet *)control, p));
1.49      thorpej   517:
1.50      thorpej   518:        if (req == PRU_PURGEIF) {
1.56      itojun    519:                in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50      thorpej   520:                in_purgeif((struct ifnet *)control);
                    521:                in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.49      thorpej   522:                return (0);
                    523:        }
1.22      pk        524:
1.27      mycroft   525:        s = splsoftnet();
                    526:        inp = sotoinpcb(so);
1.28      mycroft   527: #ifdef DIAGNOSTIC
                    528:        if (req != PRU_SEND && req != PRU_SENDOOB && control)
                    529:                panic("rip_usrreq: unexpected control mbuf");
                    530: #endif
1.27      mycroft   531:        if (inp == 0 && req != PRU_ATTACH) {
1.22      pk        532:                error = EINVAL;
                    533:                goto release;
                    534:        }
                    535:
1.1       cgd       536:        switch (req) {
                    537:
                    538:        case PRU_ATTACH:
1.27      mycroft   539:                if (inp != 0) {
                    540:                        error = EISCONN;
                    541:                        break;
                    542:                }
1.26      mycroft   543:                if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
1.13      mycroft   544:                        error = EACCES;
                    545:                        break;
                    546:                }
1.27      mycroft   547:                if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    548:                        error = soreserve(so, rip_sendspace, rip_recvspace);
                    549:                        if (error)
                    550:                                break;
                    551:                }
                    552:                error = in_pcballoc(so, &rawcbtable);
                    553:                if (error)
1.13      mycroft   554:                        break;
1.27      mycroft   555:                inp = sotoinpcb(so);
1.17      cgd       556:                inp->inp_ip.ip_p = (long)nam;
1.1       cgd       557:                break;
                    558:
                    559:        case PRU_DETACH:
1.13      mycroft   560: #ifdef MROUTING
1.6       hpeyerl   561:                if (so == ip_mrouter)
                    562:                        ip_mrouter_done();
                    563: #endif
1.13      mycroft   564:                in_pcbdetach(inp);
1.1       cgd       565:                break;
                    566:
                    567:        case PRU_BIND:
1.29      mycroft   568:                error = rip_bind(inp, nam);
1.27      mycroft   569:                break;
                    570:
                    571:        case PRU_LISTEN:
                    572:                error = EOPNOTSUPP;
                    573:                break;
                    574:
                    575:        case PRU_CONNECT:
                    576:                error = rip_connect(inp, nam);
                    577:                if (error)
1.13      mycroft   578:                        break;
1.1       cgd       579:                soisconnected(so);
1.13      mycroft   580:                break;
                    581:
                    582:        case PRU_CONNECT2:
                    583:                error = EOPNOTSUPP;
                    584:                break;
                    585:
1.27      mycroft   586:        case PRU_DISCONNECT:
                    587:                soisdisconnected(so);
                    588:                rip_disconnect(inp);
                    589:                break;
                    590:
1.13      mycroft   591:        /*
                    592:         * Mark the connection as being incapable of further input.
                    593:         */
                    594:        case PRU_SHUTDOWN:
                    595:                socantsendmore(so);
                    596:                break;
                    597:
1.27      mycroft   598:        case PRU_RCVD:
                    599:                error = EOPNOTSUPP;
                    600:                break;
                    601:
1.13      mycroft   602:        /*
                    603:         * Ship a packet out.  The appropriate raw output
                    604:         * routine handles any massaging necessary.
                    605:         */
                    606:        case PRU_SEND:
1.28      mycroft   607:                if (control && control->m_len) {
                    608:                        m_freem(control);
                    609:                        m_freem(m);
                    610:                        error = EINVAL;
                    611:                        break;
                    612:                }
                    613:        {
1.27      mycroft   614:                if (nam) {
                    615:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13      mycroft   616:                                error = EISCONN;
1.28      mycroft   617:                                goto die;
1.13      mycroft   618:                        }
1.27      mycroft   619:                        error = rip_connect(inp, nam);
                    620:                        if (error) {
1.28      mycroft   621:                        die:
1.27      mycroft   622:                                m_freem(m);
                    623:                                break;
                    624:                        }
1.13      mycroft   625:                } else {
1.27      mycroft   626:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13      mycroft   627:                                error = ENOTCONN;
1.28      mycroft   628:                                goto die;
1.13      mycroft   629:                        }
                    630:                }
1.27      mycroft   631:                error = rip_output(m, inp);
                    632:                if (nam)
                    633:                        rip_disconnect(inp);
1.28      mycroft   634:        }
1.13      mycroft   635:                break;
                    636:
                    637:        case PRU_SENSE:
                    638:                /*
                    639:                 * stat: don't bother with a blocksize.
                    640:                 */
1.27      mycroft   641:                splx(s);
1.1       cgd       642:                return (0);
1.13      mycroft   643:
                    644:        case PRU_RCVOOB:
1.27      mycroft   645:                error = EOPNOTSUPP;
                    646:                break;
                    647:
1.13      mycroft   648:        case PRU_SENDOOB:
1.28      mycroft   649:                m_freem(control);
1.27      mycroft   650:                m_freem(m);
1.13      mycroft   651:                error = EOPNOTSUPP;
                    652:                break;
                    653:
                    654:        case PRU_SOCKADDR:
                    655:                in_setsockaddr(inp, nam);
                    656:                break;
                    657:
                    658:        case PRU_PEERADDR:
                    659:                in_setpeeraddr(inp, nam);
                    660:                break;
                    661:
                    662:        default:
                    663:                panic("rip_usrreq");
1.1       cgd       664:        }
1.27      mycroft   665:
1.22      pk        666: release:
1.27      mycroft   667:        splx(s);
1.1       cgd       668:        return (error);
                    669: }
1.84      atatat    670:
                    671: SYSCTL_SETUP(sysctl_net_inet_raw_setup, "sysctl net.inet.raw subtree setup")
                    672: {
                    673:
                    674:        sysctl_createv(clog, 0, NULL, NULL,
                    675:                       CTLFLAG_PERMANENT,
                    676:                       CTLTYPE_NODE, "net", NULL,
                    677:                       NULL, 0, NULL, 0,
                    678:                       CTL_NET, CTL_EOL);
                    679:        sysctl_createv(clog, 0, NULL, NULL,
                    680:                       CTLFLAG_PERMANENT,
                    681:                       CTLTYPE_NODE, "inet", NULL,
                    682:                       NULL, 0, NULL, 0,
                    683:                       CTL_NET, PF_INET, CTL_EOL);
                    684:        sysctl_createv(clog, 0, NULL, NULL,
                    685:                       CTLFLAG_PERMANENT,
                    686:                       CTLTYPE_NODE, "raw",
                    687:                       SYSCTL_DESCR("Raw IPv4 settings"),
                    688:                       NULL, 0, NULL, 0,
                    689:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
                    690:
                    691:        sysctl_createv(clog, 0, NULL, NULL,
                    692:                       CTLFLAG_PERMANENT,
1.86      atatat    693:                       CTLTYPE_STRUCT, "pcblist",
1.84      atatat    694:                       SYSCTL_DESCR("Raw IPv4 control block list"),
                    695:                       sysctl_inpcblist, 0, &rawcbtable, 0,
                    696:                       CTL_NET, PF_INET, IPPROTO_RAW,
                    697:                       CTL_CREATE, CTL_EOL);
                    698: }

CVSweb <webmaster@jp.NetBSD.org>