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

1.145.2.1! martin      1: /*     $NetBSD: raw_ip.c,v 1.145 2014/08/09 05:33:01 rtr 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:
1.121     rmind      63: /*
                     64:  * Raw interface to IP protocol.
                     65:  */
                     66:
1.59      lukem      67: #include <sys/cdefs.h>
1.145.2.1! martin     68: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.145 2014/08/09 05:33:01 rtr Exp $");
1.40      scottr     69:
1.78      jonathan   70: #include "opt_inet.h"
1.109     christos   71: #include "opt_compat_netbsd.h"
1.45      thorpej    72: #include "opt_ipsec.h"
1.40      scottr     73: #include "opt_mrouting.h"
1.1       cgd        74:
1.7       mycroft    75: #include <sys/param.h>
1.84      atatat     76: #include <sys/sysctl.h>
1.7       mycroft    77: #include <sys/mbuf.h>
                     78: #include <sys/socket.h>
                     79: #include <sys/protosw.h>
                     80: #include <sys/socketvar.h>
                     81: #include <sys/errno.h>
1.13      mycroft    82: #include <sys/systm.h>
1.26      mycroft    83: #include <sys/proc.h>
1.89      elad       84: #include <sys/kauth.h>
1.1       cgd        85:
1.7       mycroft    86: #include <net/if.h>
                     87: #include <net/route.h>
1.1       cgd        88:
1.7       mycroft    89: #include <netinet/in.h>
                     90: #include <netinet/in_systm.h>
                     91: #include <netinet/ip.h>
                     92: #include <netinet/ip_var.h>
1.105     thorpej    93: #include <netinet/ip_private.h>
1.13      mycroft    94: #include <netinet/ip_mroute.h>
1.44      darrenr    95: #include <netinet/ip_icmp.h>
1.7       mycroft    96: #include <netinet/in_pcb.h>
1.87      yamt       97: #include <netinet/in_proto.h>
1.24      christos   98: #include <netinet/in_var.h>
                     99:
1.116     christos  100: #ifdef IPSEC
1.72      jonathan  101: #include <netipsec/ipsec.h>
1.106     thorpej   102: #include <netipsec/ipsec_var.h>
                    103: #include <netipsec/ipsec_private.h>
1.116     christos  104: #endif /* IPSEC */
1.72      jonathan  105:
1.109     christos  106: #ifdef COMPAT_50
                    107: #include <compat/sys/socket.h>
                    108: #endif
                    109:
1.20      mycroft   110: struct inpcbtable rawcbtable;
1.30      pk        111:
1.82      perry     112: int     rip_pcbnotify(struct inpcbtable *, struct in_addr,
                    113:     struct in_addr, int, int, void (*)(struct inpcb *, int));
1.137     rtr       114: int     rip_connect_pcb(struct inpcb *, struct mbuf *);
1.139     rtr       115: static void     rip_disconnect1(struct inpcb *);
1.13      mycroft   116:
1.110     pooka     117: static void sysctl_net_inet_raw_setup(struct sysctllog **);
                    118:
1.13      mycroft   119: /*
                    120:  * Nominal space allocated to a raw ip socket.
                    121:  */
                    122: #define        RIPSNDQ         8192
                    123: #define        RIPRCVQ         8192
1.1       cgd       124:
1.121     rmind     125: static u_long          rip_sendspace = RIPSNDQ;
                    126: static u_long          rip_recvspace = RIPRCVQ;
                    127:
1.1       cgd       128: /*
                    129:  * Raw interface to IP protocol.
                    130:  */
1.13      mycroft   131:
                    132: /*
                    133:  * Initialize raw connection block q.
                    134:  */
                    135: void
1.83      perry     136: rip_init(void)
1.13      mycroft   137: {
                    138:
1.110     pooka     139:        sysctl_net_inet_raw_setup(NULL);
1.33      mycroft   140:        in_pcbinit(&rawcbtable, 1, 1);
1.13      mycroft   141: }
                    142:
1.100     dyoung    143: static void
                    144: rip_sbappendaddr(struct inpcb *last, struct ip *ip, const struct sockaddr *sa,
                    145:     int hlen, struct mbuf *opts, struct mbuf *n)
                    146: {
                    147:        if (last->inp_flags & INP_NOHEADER)
                    148:                m_adj(n, hlen);
1.109     christos  149:        if (last->inp_flags & INP_CONTROLOPTS
                    150: #ifdef SO_OTIMESTAMP
                    151:            || last->inp_socket->so_options & SO_OTIMESTAMP
                    152: #endif
                    153:            || last->inp_socket->so_options & SO_TIMESTAMP)
1.100     dyoung    154:                ip_savecontrol(last, &opts, ip, n);
                    155:        if (sbappendaddr(&last->inp_socket->so_rcv, sa, n, opts) == 0) {
                    156:                /* should notify about lost packet */
                    157:                m_freem(n);
                    158:                if (opts)
                    159:                        m_freem(opts);
                    160:        } else
                    161:                sorwakeup(last->inp_socket);
                    162: }
                    163:
1.1       cgd       164: /*
                    165:  * Setup generic address and protocol structures
                    166:  * for raw_input routine, then pass them along with
                    167:  * mbuf chain.
                    168:  */
1.9       mycroft   169: void
1.24      christos  170: rip_input(struct mbuf *m, ...)
1.1       cgd       171: {
1.100     dyoung    172:        int hlen, proto;
1.53      augustss  173:        struct ip *ip = mtod(m, struct ip *);
1.75      itojun    174:        struct inpcb_hdr *inph;
1.53      augustss  175:        struct inpcb *inp;
1.97      dyoung    176:        struct inpcb *last = NULL;
                    177:        struct mbuf *n, *opts = NULL;
1.32      mycroft   178:        struct sockaddr_in ripsrc;
1.43      itojun    179:        va_list ap;
                    180:
                    181:        va_start(ap, m);
1.64      simonb    182:        (void)va_arg(ap, int);          /* ignore value, advance ap */
1.43      itojun    183:        proto = va_arg(ap, int);
                    184:        va_end(ap);
1.1       cgd       185:
1.97      dyoung    186:        sockaddr_in_init(&ripsrc, &ip->ip_src, 0);
1.42      thorpej   187:
                    188:        /*
                    189:         * XXX Compatibility: programs using raw IP expect ip_len
1.62      itojun    190:         * XXX to have the header length subtracted, and in host order.
                    191:         * XXX ip_off is also expected to be host order.
1.42      thorpej   192:         */
1.100     dyoung    193:        hlen = ip->ip_hl << 2;
                    194:        ip->ip_len = ntohs(ip->ip_len) - hlen;
1.62      itojun    195:        NTOHS(ip->ip_off);
1.32      mycroft   196:
1.117     christos  197:        TAILQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
1.75      itojun    198:                inp = (struct inpcb *)inph;
                    199:                if (inp->inp_af != AF_INET)
                    200:                        continue;
1.43      itojun    201:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
1.13      mycroft   202:                        continue;
1.32      mycroft   203:                if (!in_nullhost(inp->inp_laddr) &&
                    204:                    !in_hosteq(inp->inp_laddr, ip->ip_dst))
1.13      mycroft   205:                        continue;
1.32      mycroft   206:                if (!in_nullhost(inp->inp_faddr) &&
                    207:                    !in_hosteq(inp->inp_faddr, ip->ip_src))
1.13      mycroft   208:                        continue;
1.97      dyoung    209:                if (last == NULL)
                    210:                        ;
1.116     christos  211: #if defined(IPSEC)
1.97      dyoung    212:                /* check AH/ESP integrity. */
1.124     christos  213:                else if (ipsec_used &&
                    214:                    ipsec4_in_reject_so(m, last->inp_socket)) {
1.106     thorpej   215:                        IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.97      dyoung    216:                        /* do not inject data to pcb */
                    217:                }
                    218: #endif /*IPSEC*/
1.99      dyoung    219:                else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.100     dyoung    220:                        rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
                    221:                            n);
1.97      dyoung    222:                        opts = NULL;
1.13      mycroft   223:                }
1.36      thorpej   224:                last = inp;
1.13      mycroft   225:        }
1.116     christos  226: #if defined(IPSEC)
1.55      itojun    227:        /* check AH/ESP integrity. */
1.124     christos  228:        if (ipsec_used && last != NULL
                    229:            && ipsec4_in_reject_so(m, last->inp_socket)) {
1.55      itojun    230:                m_freem(m);
1.106     thorpej   231:                IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.105     thorpej   232:                IP_STATDEC(IP_STAT_DELIVERED);
1.55      itojun    233:                /* do not inject data to pcb */
                    234:        } else
                    235: #endif /*IPSEC*/
1.100     dyoung    236:        if (last != NULL)
                    237:                rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
                    238:        else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
1.105     thorpej   239:                uint64_t *ips;
                    240:
1.97      dyoung    241:                icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
                    242:                    0, 0);
1.105     thorpej   243:                ips = IP_STAT_GETREF();
                    244:                ips[IP_STAT_NOPROTO]++;
                    245:                ips[IP_STAT_DELIVERED]--;
                    246:                IP_STAT_PUTREF();
1.97      dyoung    247:        } else
                    248:                m_freem(m);
1.43      itojun    249:        return;
1.60      itojun    250: }
                    251:
                    252: int
1.83      perry     253: rip_pcbnotify(struct inpcbtable *table,
                    254:     struct in_addr faddr, struct in_addr laddr, int proto, int errno,
                    255:     void (*notify)(struct inpcb *, int))
1.60      itojun    256: {
1.117     christos  257:        struct inpcb_hdr *inph, *ninph;
1.60      itojun    258:        int nmatch;
                    259:
                    260:        nmatch = 0;
1.117     christos  261:        TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
                    262:                struct inpcb *inp = (struct inpcb *)inph;
1.75      itojun    263:                if (inp->inp_af != AF_INET)
                    264:                        continue;
1.60      itojun    265:                if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
                    266:                        continue;
                    267:                if (in_hosteq(inp->inp_faddr, faddr) &&
                    268:                    in_hosteq(inp->inp_laddr, laddr)) {
                    269:                        (*notify)(inp, errno);
                    270:                        nmatch++;
                    271:                }
                    272:        }
                    273:
                    274:        return nmatch;
                    275: }
                    276:
                    277: void *
1.95      dyoung    278: rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.60      itojun    279: {
                    280:        struct ip *ip = v;
1.82      perry     281:        void (*notify)(struct inpcb *, int) = in_rtchange;
1.60      itojun    282:        int errno;
                    283:
                    284:        if (sa->sa_family != AF_INET ||
                    285:            sa->sa_len != sizeof(struct sockaddr_in))
                    286:                return NULL;
                    287:        if ((unsigned)cmd >= PRC_NCMDS)
                    288:                return NULL;
                    289:        errno = inetctlerrmap[cmd];
                    290:        if (PRC_IS_REDIRECT(cmd))
                    291:                notify = in_rtchange, ip = 0;
                    292:        else if (cmd == PRC_HOSTDEAD)
                    293:                ip = 0;
                    294:        else if (errno == 0)
                    295:                return NULL;
                    296:        if (ip) {
1.95      dyoung    297:                rip_pcbnotify(&rawcbtable, satocsin(sa)->sin_addr,
1.60      itojun    298:                    ip->ip_src, ip->ip_p, errno, notify);
                    299:
                    300:                /* XXX mapped address case */
                    301:        } else
1.95      dyoung    302:                in_pcbnotifyall(&rawcbtable, satocsin(sa)->sin_addr, errno,
1.60      itojun    303:                    notify);
                    304:        return NULL;
1.1       cgd       305: }
                    306:
                    307: /*
                    308:  * Generate IP header and pass packet to ip_output.
                    309:  * Tack on options user may have setup with control call.
                    310:  */
1.9       mycroft   311: int
1.24      christos  312: rip_output(struct mbuf *m, ...)
                    313: {
1.53      augustss  314:        struct inpcb *inp;
                    315:        struct ip *ip;
1.10      mycroft   316:        struct mbuf *opts;
1.24      christos  317:        int flags;
                    318:        va_list ap;
                    319:
                    320:        va_start(ap, m);
1.27      mycroft   321:        inp = va_arg(ap, struct inpcb *);
1.24      christos  322:        va_end(ap);
                    323:
1.27      mycroft   324:        flags =
1.37      matt      325:            (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
                    326:            | IP_RETURNMTU;
1.1       cgd       327:
                    328:        /*
                    329:         * If the user handed us a complete IP packet, use it.
                    330:         * Otherwise, allocate an mbuf for a header and fill it in.
                    331:         */
1.13      mycroft   332:        if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.35      thorpej   333:                if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
                    334:                        m_freem(m);
                    335:                        return (EMSGSIZE);
                    336:                }
1.68      itojun    337:                M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
                    338:                if (!m)
                    339:                        return (ENOBUFS);
1.1       cgd       340:                ip = mtod(m, struct ip *);
                    341:                ip->ip_tos = 0;
1.62      itojun    342:                ip->ip_off = htons(0);
1.13      mycroft   343:                ip->ip_p = inp->inp_ip.ip_p;
1.62      itojun    344:                ip->ip_len = htons(m->m_pkthdr.len);
1.13      mycroft   345:                ip->ip_src = inp->inp_laddr;
1.27      mycroft   346:                ip->ip_dst = inp->inp_faddr;
1.1       cgd       347:                ip->ip_ttl = MAXTTL;
1.13      mycroft   348:                opts = inp->inp_options;
                    349:        } else {
1.35      thorpej   350:                if (m->m_pkthdr.len > IP_MAXPACKET) {
                    351:                        m_freem(m);
                    352:                        return (EMSGSIZE);
                    353:                }
1.13      mycroft   354:                ip = mtod(m, struct ip *);
1.65      thorpej   355:
                    356:                /*
                    357:                 * If the mbuf is read-only, we need to allocate
                    358:                 * a new mbuf for the header, since we need to
                    359:                 * modify the header.
                    360:                 */
                    361:                if (M_READONLY(m)) {
                    362:                        int hlen = ip->ip_hl << 2;
                    363:
                    364:                        m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
                    365:                        if (m == NULL)
                    366:                                return (ENOMEM);        /* XXX */
                    367:                        ip = mtod(m, struct ip *);
                    368:                }
                    369:
1.62      itojun    370:                /* XXX userland passes ip_len and ip_off in host order */
1.38      mycroft   371:                if (m->m_pkthdr.len != ip->ip_len) {
                    372:                        m_freem(m);
                    373:                        return (EINVAL);
                    374:                }
1.62      itojun    375:                HTONS(ip->ip_len);
                    376:                HTONS(ip->ip_off);
1.103     matt      377:                if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
                    378:                        flags |= IP_NOIPNEWID;
1.13      mycroft   379:                opts = NULL;
                    380:                /* XXX prevent ip_output from overwriting header fields */
                    381:                flags |= IP_RAWOUTPUT;
1.105     thorpej   382:                IP_STATINC(IP_STAT_RAWOUT);
1.1       cgd       383:        }
1.123     rmind     384:
                    385:        /*
                    386:         * IP output.  Note: if IP_RETURNMTU flag is set, the MTU size
                    387:         * will be stored in inp_errormtu.
                    388:         */
                    389:        return ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
                    390:             inp->inp_socket);
1.1       cgd       391: }
                    392:
                    393: /*
                    394:  * Raw IP socket option processing.
                    395:  */
1.9       mycroft   396: int
1.108     plunky    397: rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.1       cgd       398: {
1.53      augustss  399:        struct inpcb *inp = sotoinpcb(so);
1.31      mycroft   400:        int error = 0;
1.108     plunky    401:        int optval;
1.1       cgd       402:
1.108     plunky    403:        if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
1.100     dyoung    404:                if (op == PRCO_GETOPT) {
1.108     plunky    405:                        optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
                    406:                        error = sockopt_set(sopt, &optval, sizeof(optval));
                    407:                } else if (op == PRCO_SETOPT) {
                    408:                        error = sockopt_getint(sopt, &optval);
                    409:                        if (error)
                    410:                                goto out;
                    411:                        if (optval) {
                    412:                                inp->inp_flags &= ~INP_HDRINCL;
                    413:                                inp->inp_flags |= INP_NOHEADER;
                    414:                        } else
                    415:                                inp->inp_flags &= ~INP_NOHEADER;
                    416:                }
                    417:                goto out;
                    418:        } else if (sopt->sopt_level != IPPROTO_IP)
                    419:                return ip_ctloutput(op, so, sopt);
1.100     dyoung    420:
                    421:        switch (op) {
1.31      mycroft   422:
                    423:        case PRCO_SETOPT:
1.108     plunky    424:                switch (sopt->sopt_name) {
1.31      mycroft   425:                case IP_HDRINCL:
1.108     plunky    426:                        error = sockopt_getint(sopt, &optval);
                    427:                        if (error)
                    428:                                break;
                    429:                        if (optval)
1.100     dyoung    430:                                inp->inp_flags |= INP_HDRINCL;
                    431:                        else
                    432:                                inp->inp_flags &= ~INP_HDRINCL;
1.108     plunky    433:                        break;
1.31      mycroft   434:
                    435: #ifdef MROUTING
                    436:                case MRT_INIT:
                    437:                case MRT_DONE:
                    438:                case MRT_ADD_VIF:
                    439:                case MRT_DEL_VIF:
                    440:                case MRT_ADD_MFC:
                    441:                case MRT_DEL_MFC:
                    442:                case MRT_ASSERT:
1.81      manu      443:                case MRT_API_CONFIG:
                    444:                case MRT_ADD_BW_UPCALL:
                    445:                case MRT_DEL_BW_UPCALL:
1.108     plunky    446:                        error = ip_mrouter_set(so, sopt);
1.31      mycroft   447:                        break;
                    448: #endif
                    449:
                    450:                default:
1.108     plunky    451:                        error = ip_ctloutput(op, so, sopt);
1.31      mycroft   452:                        break;
1.13      mycroft   453:                }
                    454:                break;
1.1       cgd       455:
1.31      mycroft   456:        case PRCO_GETOPT:
1.108     plunky    457:                switch (sopt->sopt_name) {
1.31      mycroft   458:                case IP_HDRINCL:
1.108     plunky    459:                        optval = inp->inp_flags & INP_HDRINCL;
                    460:                        error = sockopt_set(sopt, &optval, sizeof(optval));
1.31      mycroft   461:                        break;
                    462:
1.6       hpeyerl   463: #ifdef MROUTING
1.31      mycroft   464:                case MRT_VERSION:
                    465:                case MRT_ASSERT:
1.81      manu      466:                case MRT_API_SUPPORT:
                    467:                case MRT_API_CONFIG:
1.108     plunky    468:                        error = ip_mrouter_get(so, sopt);
1.18      mycroft   469:                        break;
1.31      mycroft   470: #endif
                    471:
1.18      mycroft   472:                default:
1.108     plunky    473:                        error = ip_ctloutput(op, so, sopt);
1.18      mycroft   474:                        break;
                    475:                }
1.31      mycroft   476:                break;
1.1       cgd       477:        }
1.108     plunky    478:  out:
1.100     dyoung    479:        return error;
1.1       cgd       480: }
                    481:
1.27      mycroft   482: int
1.137     rtr       483: rip_connect_pcb(struct inpcb *inp, struct mbuf *nam)
1.27      mycroft   484: {
                    485:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    486:
                    487:        if (nam->m_len != sizeof(*addr))
                    488:                return (EINVAL);
1.138     ozaki-r   489:        if (IFNET_EMPTY())
1.27      mycroft   490:                return (EADDRNOTAVAIL);
1.115     joerg     491:        if (addr->sin_family != AF_INET)
1.27      mycroft   492:                return (EAFNOSUPPORT);
                    493:        inp->inp_faddr = addr->sin_addr;
                    494:        return (0);
                    495: }
                    496:
1.139     rtr       497: static void
                    498: rip_disconnect1(struct inpcb *inp)
1.27      mycroft   499: {
                    500:
1.32      mycroft   501:        inp->inp_faddr = zeroin_addr;
1.27      mycroft   502: }
                    503:
1.121     rmind     504: static int
                    505: rip_attach(struct socket *so, int proto)
                    506: {
                    507:        struct inpcb *inp;
                    508:        int error;
                    509:
                    510:        KASSERT(sotoinpcb(so) == NULL);
                    511:        sosetlock(so);
                    512:
                    513:        if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    514:                error = soreserve(so, rip_sendspace, rip_recvspace);
                    515:                if (error) {
                    516:                        return error;
                    517:                }
                    518:        }
                    519:
                    520:        error = in_pcballoc(so, &rawcbtable);
                    521:        if (error) {
                    522:                return error;
                    523:        }
                    524:        inp = sotoinpcb(so);
                    525:        inp->inp_ip.ip_p = proto;
                    526:        KASSERT(solocked(so));
                    527:
                    528:        return 0;
                    529: }
1.13      mycroft   530:
1.121     rmind     531: static void
                    532: rip_detach(struct socket *so)
1.1       cgd       533: {
1.53      augustss  534:        struct inpcb *inp;
1.121     rmind     535:
                    536:        KASSERT(solocked(so));
                    537:        inp = sotoinpcb(so);
                    538:        KASSERT(inp != NULL);
                    539:
1.13      mycroft   540: #ifdef MROUTING
1.6       hpeyerl   541:        extern struct socket *ip_mrouter;
1.121     rmind     542:        if (so == ip_mrouter) {
                    543:                ip_mrouter_done();
                    544:        }
1.6       hpeyerl   545: #endif
1.121     rmind     546:        in_pcbdetach(inp);
                    547: }
                    548:
1.125     rtr       549: static int
1.133     rtr       550: rip_accept(struct socket *so, struct mbuf *nam)
                    551: {
                    552:        KASSERT(solocked(so));
                    553:
                    554:        panic("rip_accept");
1.136     rtr       555:
                    556:        return EOPNOTSUPP;
                    557: }
                    558:
                    559: static int
1.142     rtr       560: rip_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
1.136     rtr       561: {
                    562:        struct inpcb *inp = sotoinpcb(so);
                    563:        struct sockaddr_in *addr;
                    564:        int error = 0;
                    565:        int s;
                    566:
                    567:        KASSERT(solocked(so));
                    568:        KASSERT(inp != NULL);
                    569:        KASSERT(nam != NULL);
                    570:
                    571:        s = splsoftnet();
                    572:        addr = mtod(nam, struct sockaddr_in *);
                    573:        if (nam->m_len != sizeof(*addr)) {
                    574:                error = EINVAL;
                    575:                goto release;
                    576:        }
1.138     ozaki-r   577:        if (IFNET_EMPTY()) {
1.136     rtr       578:                error = EADDRNOTAVAIL;
                    579:                goto release;
                    580:        }
                    581:        if (addr->sin_family != AF_INET) {
                    582:                error = EAFNOSUPPORT;
                    583:                goto release;
                    584:        }
                    585:        if (!in_nullhost(addr->sin_addr) &&
                    586:            ifa_ifwithaddr(sintosa(addr)) == 0) {
                    587:                error = EADDRNOTAVAIL;
                    588:                goto release;
                    589:        }
                    590:        inp->inp_laddr = addr->sin_addr;
                    591:
                    592: release:
                    593:        splx(s);
                    594:        return error;
                    595: }
                    596:
                    597: static int
1.142     rtr       598: rip_listen(struct socket *so, struct lwp *l)
1.136     rtr       599: {
                    600:        KASSERT(solocked(so));
                    601:
1.133     rtr       602:        return EOPNOTSUPP;
                    603: }
                    604:
                    605: static int
1.142     rtr       606: rip_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
1.137     rtr       607: {
                    608:        struct inpcb *inp = sotoinpcb(so);
                    609:        int error = 0;
                    610:        int s;
                    611:
                    612:        KASSERT(solocked(so));
                    613:        KASSERT(inp != NULL);
                    614:        KASSERT(nam != NULL);
                    615:
                    616:        s = splsoftnet();
                    617:        error = rip_connect_pcb(inp, nam);
                    618:        if (! error)
                    619:                soisconnected(so);
1.140     rtr       620:        splx(s);
1.137     rtr       621:
                    622:        return error;
                    623: }
                    624:
1.139     rtr       625: static int
1.145     rtr       626: rip_connect2(struct socket *so, struct socket *so2)
                    627: {
                    628:        KASSERT(solocked(so));
                    629:
                    630:        return EOPNOTSUPP;
                    631: }
                    632:
                    633: static int
1.139     rtr       634: rip_disconnect(struct socket *so)
                    635: {
                    636:        struct inpcb *inp = sotoinpcb(so);
1.140     rtr       637:        int s;
1.139     rtr       638:
                    639:        KASSERT(solocked(so));
                    640:        KASSERT(inp != NULL);
                    641:
1.140     rtr       642:        s = splsoftnet();
1.139     rtr       643:        soisdisconnected(so);
                    644:        rip_disconnect1(inp);
1.140     rtr       645:        splx(s);
                    646:
1.139     rtr       647:        return 0;
                    648: }
                    649:
                    650: static int
                    651: rip_shutdown(struct socket *so)
                    652: {
1.140     rtr       653:        int s;
                    654:
1.139     rtr       655:        KASSERT(solocked(so));
                    656:
                    657:        /*
                    658:         * Mark the connection as being incapable of further input.
                    659:         */
1.140     rtr       660:        s = splsoftnet();
1.139     rtr       661:        socantsendmore(so);
1.140     rtr       662:        splx(s);
                    663:
1.139     rtr       664:        return 0;
                    665: }
                    666:
                    667: static int
                    668: rip_abort(struct socket *so)
                    669: {
                    670:        KASSERT(solocked(so));
                    671:
                    672:        panic("rip_abort");
                    673:
                    674:        return EOPNOTSUPP;
                    675: }
1.137     rtr       676:
                    677: static int
1.127     rtr       678: rip_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
1.125     rtr       679: {
1.127     rtr       680:        return in_control(so, cmd, nam, ifp);
1.125     rtr       681: }
                    682:
1.128     rtr       683: static int
                    684: rip_stat(struct socket *so, struct stat *ub)
                    685: {
1.131     rtr       686:        KASSERT(solocked(so));
                    687:
1.130     rtr       688:        /* stat: don't bother with a blocksize. */
                    689:        return 0;
1.128     rtr       690: }
                    691:
1.132     rtr       692: static int
                    693: rip_peeraddr(struct socket *so, struct mbuf *nam)
                    694: {
1.140     rtr       695:        int s;
                    696:
1.134     rtr       697:        KASSERT(solocked(so));
1.132     rtr       698:        KASSERT(sotoinpcb(so) != NULL);
                    699:        KASSERT(nam != NULL);
                    700:
1.140     rtr       701:        s = splsoftnet();
1.132     rtr       702:        in_setpeeraddr(sotoinpcb(so), nam);
1.140     rtr       703:        splx(s);
                    704:
1.132     rtr       705:        return 0;
                    706: }
                    707:
                    708: static int
                    709: rip_sockaddr(struct socket *so, struct mbuf *nam)
                    710: {
1.140     rtr       711:        int s;
                    712:
1.134     rtr       713:        KASSERT(solocked(so));
1.132     rtr       714:        KASSERT(sotoinpcb(so) != NULL);
                    715:        KASSERT(nam != NULL);
                    716:
1.140     rtr       717:        s = splsoftnet();
1.132     rtr       718:        in_setsockaddr(sotoinpcb(so), nam);
1.140     rtr       719:        splx(s);
                    720:
1.132     rtr       721:        return 0;
                    722: }
                    723:
1.135     rtr       724: static int
1.144     rtr       725: rip_rcvd(struct socket *so, int flags, struct lwp *l)
                    726: {
                    727:        KASSERT(solocked(so));
                    728:
                    729:        return EOPNOTSUPP;
                    730: }
                    731:
                    732: static int
1.135     rtr       733: rip_recvoob(struct socket *so, struct mbuf *m, int flags)
                    734: {
                    735:        KASSERT(solocked(so));
                    736:
                    737:        return EOPNOTSUPP;
                    738: }
                    739:
                    740: static int
1.143     rtr       741: rip_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
                    742:     struct mbuf *control, struct lwp *l)
                    743: {
                    744:        struct inpcb *inp = sotoinpcb(so);
                    745:        int error = 0;
                    746:        int s;
                    747:
                    748:        KASSERT(solocked(so));
                    749:        KASSERT(inp != NULL);
                    750:        KASSERT(m != NULL);
                    751:
                    752:        /*
                    753:         * Ship a packet out.  The appropriate raw output
                    754:         * routine handles any massaging necessary.
                    755:         */
                    756:        if (control && control->m_len) {
                    757:                m_freem(control);
                    758:                m_freem(m);
                    759:                return EINVAL;
                    760:        }
                    761:
                    762:        s = splsoftnet();
                    763:        if (nam) {
                    764:                if ((so->so_state & SS_ISCONNECTED) != 0) {
                    765:                        error = EISCONN;
                    766:                        goto die;
                    767:                }
                    768:                error = rip_connect_pcb(inp, nam);
                    769:                if (error) {
                    770:                die:
                    771:                        m_freem(m);
                    772:                        splx(s);
                    773:                        return error;
                    774:                }
                    775:        } else {
                    776:                if ((so->so_state & SS_ISCONNECTED) == 0) {
                    777:                        error = ENOTCONN;
                    778:                        goto die;
                    779:                }
                    780:        }
                    781:        error = rip_output(m, inp);
                    782:        if (nam)
                    783:                rip_disconnect1(inp);
                    784:
                    785:        splx(s);
                    786:        return error;
                    787: }
                    788:
                    789: static int
1.135     rtr       790: rip_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
                    791: {
                    792:        KASSERT(solocked(so));
                    793:
                    794:        m_freem(m);
                    795:        m_freem(control);
                    796:
                    797:        return EOPNOTSUPP;
                    798: }
                    799:
1.145     rtr       800: static int
                    801: rip_purgeif(struct socket *so, struct ifnet *ifp)
                    802: {
                    803:        int s;
                    804:
                    805:        s = splsoftnet();
                    806:        mutex_enter(softnet_lock);
                    807:        in_pcbpurgeif0(&rawcbtable, ifp);
                    808:        in_purgeif(ifp);
                    809:        in_pcbpurgeif(&rawcbtable, ifp);
                    810:        mutex_exit(softnet_lock);
                    811:        splx(s);
                    812:
                    813:        return 0;
                    814: }
                    815:
1.121     rmind     816: int
                    817: rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
                    818:     struct mbuf *control, struct lwp *l)
                    819: {
                    820:        KASSERT(req != PRU_ATTACH);
                    821:        KASSERT(req != PRU_DETACH);
1.133     rtr       822:        KASSERT(req != PRU_ACCEPT);
1.136     rtr       823:        KASSERT(req != PRU_BIND);
                    824:        KASSERT(req != PRU_LISTEN);
1.137     rtr       825:        KASSERT(req != PRU_CONNECT);
1.145     rtr       826:        KASSERT(req != PRU_CONNECT2);
1.139     rtr       827:        KASSERT(req != PRU_DISCONNECT);
                    828:        KASSERT(req != PRU_SHUTDOWN);
                    829:        KASSERT(req != PRU_ABORT);
1.125     rtr       830:        KASSERT(req != PRU_CONTROL);
1.128     rtr       831:        KASSERT(req != PRU_SENSE);
1.132     rtr       832:        KASSERT(req != PRU_PEERADDR);
                    833:        KASSERT(req != PRU_SOCKADDR);
1.144     rtr       834:        KASSERT(req != PRU_RCVD);
1.135     rtr       835:        KASSERT(req != PRU_RCVOOB);
1.143     rtr       836:        KASSERT(req != PRU_SEND);
1.135     rtr       837:        KASSERT(req != PRU_SENDOOB);
1.145     rtr       838:        KASSERT(req != PRU_PURGEIF);
1.22      pk        839:
1.121     rmind     840:        KASSERT(solocked(so));
                    841:
1.145     rtr       842:        if (sotoinpcb(so) == NULL)
1.121     rmind     843:                return EINVAL;
1.22      pk        844:
1.145     rtr       845:        panic("rip_usrreq");
1.1       cgd       846:
1.145     rtr       847:        return 0;
1.1       cgd       848: }
1.84      atatat    849:
1.122     rmind     850: PR_WRAP_USRREQS(rip)
                    851: #define        rip_attach      rip_attach_wrapper
                    852: #define        rip_detach      rip_detach_wrapper
1.133     rtr       853: #define        rip_accept      rip_accept_wrapper
1.136     rtr       854: #define        rip_bind        rip_bind_wrapper
                    855: #define        rip_listen      rip_listen_wrapper
1.137     rtr       856: #define        rip_connect     rip_connect_wrapper
1.145     rtr       857: #define        rip_connect2    rip_connect2_wrapper
1.139     rtr       858: #define        rip_disconnect  rip_disconnect_wrapper
                    859: #define        rip_shutdown    rip_shutdown_wrapper
                    860: #define        rip_abort       rip_abort_wrapper
1.125     rtr       861: #define        rip_ioctl       rip_ioctl_wrapper
1.128     rtr       862: #define        rip_stat        rip_stat_wrapper
1.132     rtr       863: #define        rip_peeraddr    rip_peeraddr_wrapper
                    864: #define        rip_sockaddr    rip_sockaddr_wrapper
1.144     rtr       865: #define        rip_rcvd        rip_rcvd_wrapper
1.135     rtr       866: #define        rip_recvoob     rip_recvoob_wrapper
1.143     rtr       867: #define        rip_send        rip_send_wrapper
1.135     rtr       868: #define        rip_sendoob     rip_sendoob_wrapper
1.145     rtr       869: #define        rip_purgeif     rip_purgeif_wrapper
1.120     rmind     870: #define        rip_usrreq      rip_usrreq_wrapper
                    871:
                    872: const struct pr_usrreqs rip_usrreqs = {
1.121     rmind     873:        .pr_attach      = rip_attach,
                    874:        .pr_detach      = rip_detach,
1.133     rtr       875:        .pr_accept      = rip_accept,
1.136     rtr       876:        .pr_bind        = rip_bind,
                    877:        .pr_listen      = rip_listen,
1.137     rtr       878:        .pr_connect     = rip_connect,
1.145     rtr       879:        .pr_connect2    = rip_connect2,
1.139     rtr       880:        .pr_disconnect  = rip_disconnect,
                    881:        .pr_shutdown    = rip_shutdown,
                    882:        .pr_abort       = rip_abort,
1.125     rtr       883:        .pr_ioctl       = rip_ioctl,
1.128     rtr       884:        .pr_stat        = rip_stat,
1.132     rtr       885:        .pr_peeraddr    = rip_peeraddr,
                    886:        .pr_sockaddr    = rip_sockaddr,
1.144     rtr       887:        .pr_rcvd        = rip_rcvd,
1.135     rtr       888:        .pr_recvoob     = rip_recvoob,
1.143     rtr       889:        .pr_send        = rip_send,
1.135     rtr       890:        .pr_sendoob     = rip_sendoob,
1.145     rtr       891:        .pr_purgeif     = rip_purgeif,
1.120     rmind     892:        .pr_generic     = rip_usrreq,
                    893: };
                    894:
1.110     pooka     895: static void
                    896: sysctl_net_inet_raw_setup(struct sysctllog **clog)
1.84      atatat    897: {
                    898:
                    899:        sysctl_createv(clog, 0, NULL, NULL,
                    900:                       CTLFLAG_PERMANENT,
                    901:                       CTLTYPE_NODE, "inet", NULL,
                    902:                       NULL, 0, NULL, 0,
                    903:                       CTL_NET, PF_INET, CTL_EOL);
                    904:        sysctl_createv(clog, 0, NULL, NULL,
                    905:                       CTLFLAG_PERMANENT,
                    906:                       CTLTYPE_NODE, "raw",
                    907:                       SYSCTL_DESCR("Raw IPv4 settings"),
                    908:                       NULL, 0, NULL, 0,
                    909:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
                    910:
                    911:        sysctl_createv(clog, 0, NULL, NULL,
                    912:                       CTLFLAG_PERMANENT,
1.86      atatat    913:                       CTLTYPE_STRUCT, "pcblist",
1.84      atatat    914:                       SYSCTL_DESCR("Raw IPv4 control block list"),
                    915:                       sysctl_inpcblist, 0, &rawcbtable, 0,
                    916:                       CTL_NET, PF_INET, IPPROTO_RAW,
                    917:                       CTL_CREATE, CTL_EOL);
                    918: }

CVSweb <webmaster@jp.NetBSD.org>