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

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

CVSweb <webmaster@jp.NetBSD.org>