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

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

CVSweb <webmaster@jp.NetBSD.org>