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

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

CVSweb <webmaster@jp.NetBSD.org>