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

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

CVSweb <webmaster@jp.NetBSD.org>