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

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

CVSweb <webmaster@jp.NetBSD.org>