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

1.138   ! ozaki-r     1: /*     $NetBSD: raw_ip.c,v 1.137 2014/07/30 10:04:26 rtr Exp $ */
1.43      itojun      2:
                      3: /*
                      4:  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
                      5:  * All rights reserved.
1.61      itojun      6:  *
1.43      itojun      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. Neither the name of the project nor the names of its contributors
                     16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
1.61      itojun     18:  *
1.43      itojun     19:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
1.14      cgd        31:
1.1       cgd        32: /*
1.13      mycroft    33:  * Copyright (c) 1982, 1986, 1988, 1993
                     34:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd        35:  *
                     36:  * Redistribution and use in source and binary forms, with or without
                     37:  * modification, are permitted provided that the following conditions
                     38:  * are met:
                     39:  * 1. Redistributions of source code must retain the above copyright
                     40:  *    notice, this list of conditions and the following disclaimer.
                     41:  * 2. Redistributions in binary form must reproduce the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer in the
                     43:  *    documentation and/or other materials provided with the distribution.
1.71      agc        44:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        45:  *    may be used to endorse or promote products derived from this software
                     46:  *    without specific prior written permission.
                     47:  *
                     48:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     49:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     50:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     51:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     52:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     53:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     54:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     55:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     56:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     57:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     58:  * SUCH DAMAGE.
                     59:  *
1.39      thorpej    60:  *     @(#)raw_ip.c    8.7 (Berkeley) 5/15/95
1.1       cgd        61:  */
1.59      lukem      62:
1.121     rmind      63: /*
                     64:  * Raw interface to IP protocol.
                     65:  */
                     66:
1.59      lukem      67: #include <sys/cdefs.h>
1.138   ! ozaki-r    68: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.137 2014/07/30 10:04:26 rtr Exp $");
1.40      scottr     69:
1.78      jonathan   70: #include "opt_inet.h"
1.109     christos   71: #include "opt_compat_netbsd.h"
1.45      thorpej    72: #include "opt_ipsec.h"
1.40      scottr     73: #include "opt_mrouting.h"
1.1       cgd        74:
1.7       mycroft    75: #include <sys/param.h>
1.84      atatat     76: #include <sys/sysctl.h>
1.7       mycroft    77: #include <sys/malloc.h>
                     78: #include <sys/mbuf.h>
                     79: #include <sys/socket.h>
                     80: #include <sys/protosw.h>
                     81: #include <sys/socketvar.h>
                     82: #include <sys/errno.h>
1.13      mycroft    83: #include <sys/systm.h>
1.26      mycroft    84: #include <sys/proc.h>
1.89      elad       85: #include <sys/kauth.h>
1.1       cgd        86:
1.7       mycroft    87: #include <net/if.h>
                     88: #include <net/route.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.137     rtr       115: int     rip_connect_pcb(struct inpcb *, struct mbuf *);
1.82      perry     116: void    rip_disconnect(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);
                    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.24      christos  313: rip_output(struct mbuf *m, ...)
                    314: {
1.53      augustss  315:        struct inpcb *inp;
                    316:        struct ip *ip;
1.10      mycroft   317:        struct mbuf *opts;
1.24      christos  318:        int flags;
                    319:        va_list ap;
                    320:
                    321:        va_start(ap, m);
1.27      mycroft   322:        inp = va_arg(ap, struct inpcb *);
1.24      christos  323:        va_end(ap);
                    324:
1.27      mycroft   325:        flags =
1.37      matt      326:            (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
                    327:            | IP_RETURNMTU;
1.1       cgd       328:
                    329:        /*
                    330:         * If the user handed us a complete IP packet, use it.
                    331:         * Otherwise, allocate an mbuf for a header and fill it in.
                    332:         */
1.13      mycroft   333:        if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.35      thorpej   334:                if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
                    335:                        m_freem(m);
                    336:                        return (EMSGSIZE);
                    337:                }
1.68      itojun    338:                M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
                    339:                if (!m)
                    340:                        return (ENOBUFS);
1.1       cgd       341:                ip = mtod(m, struct ip *);
                    342:                ip->ip_tos = 0;
1.62      itojun    343:                ip->ip_off = htons(0);
1.13      mycroft   344:                ip->ip_p = inp->inp_ip.ip_p;
1.62      itojun    345:                ip->ip_len = htons(m->m_pkthdr.len);
1.13      mycroft   346:                ip->ip_src = inp->inp_laddr;
1.27      mycroft   347:                ip->ip_dst = inp->inp_faddr;
1.1       cgd       348:                ip->ip_ttl = MAXTTL;
1.13      mycroft   349:                opts = inp->inp_options;
                    350:        } else {
1.35      thorpej   351:                if (m->m_pkthdr.len > IP_MAXPACKET) {
                    352:                        m_freem(m);
                    353:                        return (EMSGSIZE);
                    354:                }
1.13      mycroft   355:                ip = mtod(m, struct ip *);
1.65      thorpej   356:
                    357:                /*
                    358:                 * If the mbuf is read-only, we need to allocate
                    359:                 * a new mbuf for the header, since we need to
                    360:                 * modify the header.
                    361:                 */
                    362:                if (M_READONLY(m)) {
                    363:                        int hlen = ip->ip_hl << 2;
                    364:
                    365:                        m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
                    366:                        if (m == NULL)
                    367:                                return (ENOMEM);        /* XXX */
                    368:                        ip = mtod(m, struct ip *);
                    369:                }
                    370:
1.62      itojun    371:                /* XXX userland passes ip_len and ip_off in host order */
1.38      mycroft   372:                if (m->m_pkthdr.len != ip->ip_len) {
                    373:                        m_freem(m);
                    374:                        return (EINVAL);
                    375:                }
1.62      itojun    376:                HTONS(ip->ip_len);
                    377:                HTONS(ip->ip_off);
1.103     matt      378:                if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
                    379:                        flags |= IP_NOIPNEWID;
1.13      mycroft   380:                opts = NULL;
                    381:                /* XXX prevent ip_output from overwriting header fields */
                    382:                flags |= IP_RAWOUTPUT;
1.105     thorpej   383:                IP_STATINC(IP_STAT_RAWOUT);
1.1       cgd       384:        }
1.123     rmind     385:
                    386:        /*
                    387:         * IP output.  Note: if IP_RETURNMTU flag is set, the MTU size
                    388:         * will be stored in inp_errormtu.
                    389:         */
                    390:        return ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
                    391:             inp->inp_socket);
1.1       cgd       392: }
                    393:
                    394: /*
                    395:  * Raw IP socket option processing.
                    396:  */
1.9       mycroft   397: int
1.108     plunky    398: rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.1       cgd       399: {
1.53      augustss  400:        struct inpcb *inp = sotoinpcb(so);
1.31      mycroft   401:        int error = 0;
1.108     plunky    402:        int optval;
1.1       cgd       403:
1.108     plunky    404:        if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
1.100     dyoung    405:                if (op == PRCO_GETOPT) {
1.108     plunky    406:                        optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
                    407:                        error = sockopt_set(sopt, &optval, sizeof(optval));
                    408:                } else if (op == PRCO_SETOPT) {
                    409:                        error = sockopt_getint(sopt, &optval);
                    410:                        if (error)
                    411:                                goto out;
                    412:                        if (optval) {
                    413:                                inp->inp_flags &= ~INP_HDRINCL;
                    414:                                inp->inp_flags |= INP_NOHEADER;
                    415:                        } else
                    416:                                inp->inp_flags &= ~INP_NOHEADER;
                    417:                }
                    418:                goto out;
                    419:        } else if (sopt->sopt_level != IPPROTO_IP)
                    420:                return ip_ctloutput(op, so, sopt);
1.100     dyoung    421:
                    422:        switch (op) {
1.31      mycroft   423:
                    424:        case PRCO_SETOPT:
1.108     plunky    425:                switch (sopt->sopt_name) {
1.31      mycroft   426:                case IP_HDRINCL:
1.108     plunky    427:                        error = sockopt_getint(sopt, &optval);
                    428:                        if (error)
                    429:                                break;
                    430:                        if (optval)
1.100     dyoung    431:                                inp->inp_flags |= INP_HDRINCL;
                    432:                        else
                    433:                                inp->inp_flags &= ~INP_HDRINCL;
1.108     plunky    434:                        break;
1.31      mycroft   435:
                    436: #ifdef MROUTING
                    437:                case MRT_INIT:
                    438:                case MRT_DONE:
                    439:                case MRT_ADD_VIF:
                    440:                case MRT_DEL_VIF:
                    441:                case MRT_ADD_MFC:
                    442:                case MRT_DEL_MFC:
                    443:                case MRT_ASSERT:
1.81      manu      444:                case MRT_API_CONFIG:
                    445:                case MRT_ADD_BW_UPCALL:
                    446:                case MRT_DEL_BW_UPCALL:
1.108     plunky    447:                        error = ip_mrouter_set(so, sopt);
1.31      mycroft   448:                        break;
                    449: #endif
                    450:
                    451:                default:
1.108     plunky    452:                        error = ip_ctloutput(op, so, sopt);
1.31      mycroft   453:                        break;
1.13      mycroft   454:                }
                    455:                break;
1.1       cgd       456:
1.31      mycroft   457:        case PRCO_GETOPT:
1.108     plunky    458:                switch (sopt->sopt_name) {
1.31      mycroft   459:                case IP_HDRINCL:
1.108     plunky    460:                        optval = inp->inp_flags & INP_HDRINCL;
                    461:                        error = sockopt_set(sopt, &optval, sizeof(optval));
1.31      mycroft   462:                        break;
                    463:
1.6       hpeyerl   464: #ifdef MROUTING
1.31      mycroft   465:                case MRT_VERSION:
                    466:                case MRT_ASSERT:
1.81      manu      467:                case MRT_API_SUPPORT:
                    468:                case MRT_API_CONFIG:
1.108     plunky    469:                        error = ip_mrouter_get(so, sopt);
1.18      mycroft   470:                        break;
1.31      mycroft   471: #endif
                    472:
1.18      mycroft   473:                default:
1.108     plunky    474:                        error = ip_ctloutput(op, so, sopt);
1.18      mycroft   475:                        break;
                    476:                }
1.31      mycroft   477:                break;
1.1       cgd       478:        }
1.108     plunky    479:  out:
1.100     dyoung    480:        return error;
1.1       cgd       481: }
                    482:
1.27      mycroft   483: int
1.137     rtr       484: rip_connect_pcb(struct inpcb *inp, struct mbuf *nam)
1.27      mycroft   485: {
                    486:        struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
                    487:
                    488:        if (nam->m_len != sizeof(*addr))
                    489:                return (EINVAL);
1.138   ! ozaki-r   490:        if (IFNET_EMPTY())
1.27      mycroft   491:                return (EADDRNOTAVAIL);
1.115     joerg     492:        if (addr->sin_family != AF_INET)
1.27      mycroft   493:                return (EAFNOSUPPORT);
                    494:        inp->inp_faddr = addr->sin_addr;
                    495:        return (0);
                    496: }
                    497:
                    498: void
1.83      perry     499: rip_disconnect(struct inpcb *inp)
1.27      mycroft   500: {
                    501:
1.32      mycroft   502:        inp->inp_faddr = zeroin_addr;
1.27      mycroft   503: }
                    504:
1.121     rmind     505: static int
                    506: rip_attach(struct socket *so, int proto)
                    507: {
                    508:        struct inpcb *inp;
                    509:        int error;
                    510:
                    511:        KASSERT(sotoinpcb(so) == NULL);
                    512:        sosetlock(so);
                    513:
                    514:        if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
                    515:                error = soreserve(so, rip_sendspace, rip_recvspace);
                    516:                if (error) {
                    517:                        return error;
                    518:                }
                    519:        }
                    520:
                    521:        error = in_pcballoc(so, &rawcbtable);
                    522:        if (error) {
                    523:                return error;
                    524:        }
                    525:        inp = sotoinpcb(so);
                    526:        inp->inp_ip.ip_p = proto;
                    527:        KASSERT(solocked(so));
                    528:
                    529:        return 0;
                    530: }
1.13      mycroft   531:
1.121     rmind     532: static void
                    533: rip_detach(struct socket *so)
1.1       cgd       534: {
1.53      augustss  535:        struct inpcb *inp;
1.121     rmind     536:
                    537:        KASSERT(solocked(so));
                    538:        inp = sotoinpcb(so);
                    539:        KASSERT(inp != NULL);
                    540:
1.13      mycroft   541: #ifdef MROUTING
1.6       hpeyerl   542:        extern struct socket *ip_mrouter;
1.121     rmind     543:        if (so == ip_mrouter) {
                    544:                ip_mrouter_done();
                    545:        }
1.6       hpeyerl   546: #endif
1.121     rmind     547:        in_pcbdetach(inp);
                    548: }
                    549:
1.125     rtr       550: static int
1.133     rtr       551: rip_accept(struct socket *so, struct mbuf *nam)
                    552: {
                    553:        KASSERT(solocked(so));
                    554:
                    555:        panic("rip_accept");
1.136     rtr       556:
                    557:        return EOPNOTSUPP;
                    558: }
                    559:
                    560: static int
                    561: rip_bind(struct socket *so, struct mbuf *nam)
                    562: {
                    563:        struct inpcb *inp = sotoinpcb(so);
                    564:        struct sockaddr_in *addr;
                    565:        int error = 0;
                    566:        int s;
                    567:
                    568:        KASSERT(solocked(so));
                    569:        KASSERT(inp != NULL);
                    570:        KASSERT(nam != NULL);
                    571:
                    572:        s = splsoftnet();
                    573:        addr = mtod(nam, struct sockaddr_in *);
                    574:        if (nam->m_len != sizeof(*addr)) {
                    575:                error = EINVAL;
                    576:                goto release;
                    577:        }
1.138   ! ozaki-r   578:        if (IFNET_EMPTY()) {
1.136     rtr       579:                error = EADDRNOTAVAIL;
                    580:                goto release;
                    581:        }
                    582:        if (addr->sin_family != AF_INET) {
                    583:                error = EAFNOSUPPORT;
                    584:                goto release;
                    585:        }
                    586:        if (!in_nullhost(addr->sin_addr) &&
                    587:            ifa_ifwithaddr(sintosa(addr)) == 0) {
                    588:                error = EADDRNOTAVAIL;
                    589:                goto release;
                    590:        }
                    591:        inp->inp_laddr = addr->sin_addr;
                    592:
                    593: release:
                    594:        splx(s);
                    595:        return error;
                    596: }
                    597:
                    598: static int
                    599: rip_listen(struct socket *so)
                    600: {
                    601:        KASSERT(solocked(so));
                    602:
1.133     rtr       603:        return EOPNOTSUPP;
                    604: }
                    605:
                    606: static int
1.137     rtr       607: rip_connect(struct socket *so, struct mbuf *nam)
                    608: {
                    609:        struct inpcb *inp = sotoinpcb(so);
                    610:        int error = 0;
                    611:        int s;
                    612:
                    613:        KASSERT(solocked(so));
                    614:        KASSERT(inp != NULL);
                    615:        KASSERT(nam != NULL);
                    616:
                    617:        s = splsoftnet();
                    618:        error = rip_connect_pcb(inp, nam);
                    619:        if (! error)
                    620:                soisconnected(so);
                    621:
                    622:        splx(s);
                    623:        return error;
                    624: }
                    625:
                    626:
                    627: static int
1.127     rtr       628: rip_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
1.125     rtr       629: {
1.127     rtr       630:        return in_control(so, cmd, nam, ifp);
1.125     rtr       631: }
                    632:
1.128     rtr       633: static int
                    634: rip_stat(struct socket *so, struct stat *ub)
                    635: {
1.131     rtr       636:        KASSERT(solocked(so));
                    637:
1.130     rtr       638:        /* stat: don't bother with a blocksize. */
                    639:        return 0;
1.128     rtr       640: }
                    641:
1.132     rtr       642: static int
                    643: rip_peeraddr(struct socket *so, struct mbuf *nam)
                    644: {
1.134     rtr       645:        KASSERT(solocked(so));
1.132     rtr       646:        KASSERT(sotoinpcb(so) != NULL);
                    647:        KASSERT(nam != NULL);
                    648:
                    649:        in_setpeeraddr(sotoinpcb(so), nam);
                    650:        return 0;
                    651: }
                    652:
                    653: static int
                    654: rip_sockaddr(struct socket *so, struct mbuf *nam)
                    655: {
1.134     rtr       656:        KASSERT(solocked(so));
1.132     rtr       657:        KASSERT(sotoinpcb(so) != NULL);
                    658:        KASSERT(nam != NULL);
                    659:
                    660:        in_setsockaddr(sotoinpcb(so), nam);
                    661:        return 0;
                    662: }
                    663:
1.135     rtr       664: static int
                    665: rip_recvoob(struct socket *so, struct mbuf *m, int flags)
                    666: {
                    667:        KASSERT(solocked(so));
                    668:
                    669:        return EOPNOTSUPP;
                    670: }
                    671:
                    672: static int
                    673: rip_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
                    674: {
                    675:        KASSERT(solocked(so));
                    676:
                    677:        m_freem(m);
                    678:        m_freem(control);
                    679:
                    680:        return EOPNOTSUPP;
                    681: }
                    682:
1.121     rmind     683: int
                    684: rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
                    685:     struct mbuf *control, struct lwp *l)
                    686: {
                    687:        struct inpcb *inp;
                    688:        int s, error = 0;
1.27      mycroft   689:
1.121     rmind     690:        KASSERT(req != PRU_ATTACH);
                    691:        KASSERT(req != PRU_DETACH);
1.133     rtr       692:        KASSERT(req != PRU_ACCEPT);
1.136     rtr       693:        KASSERT(req != PRU_BIND);
                    694:        KASSERT(req != PRU_LISTEN);
1.137     rtr       695:        KASSERT(req != PRU_CONNECT);
1.125     rtr       696:        KASSERT(req != PRU_CONTROL);
1.128     rtr       697:        KASSERT(req != PRU_SENSE);
1.132     rtr       698:        KASSERT(req != PRU_PEERADDR);
                    699:        KASSERT(req != PRU_SOCKADDR);
1.135     rtr       700:        KASSERT(req != PRU_RCVOOB);
                    701:        KASSERT(req != PRU_SENDOOB);
1.49      thorpej   702:
1.93      tls       703:        s = splsoftnet();
1.50      thorpej   704:        if (req == PRU_PURGEIF) {
1.107     ad        705:                mutex_enter(softnet_lock);
1.56      itojun    706:                in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50      thorpej   707:                in_purgeif((struct ifnet *)control);
                    708:                in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.107     ad        709:                mutex_exit(softnet_lock);
1.93      tls       710:                splx(s);
1.121     rmind     711:                return 0;
1.49      thorpej   712:        }
1.22      pk        713:
1.121     rmind     714:        KASSERT(solocked(so));
1.27      mycroft   715:        inp = sotoinpcb(so);
1.121     rmind     716:
                    717:        KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
                    718:        if (inp == NULL) {
                    719:                splx(s);
                    720:                return EINVAL;
1.22      pk        721:        }
                    722:
1.1       cgd       723:        switch (req) {
                    724:
1.13      mycroft   725:        case PRU_CONNECT2:
                    726:                error = EOPNOTSUPP;
                    727:                break;
                    728:
1.27      mycroft   729:        case PRU_DISCONNECT:
                    730:                soisdisconnected(so);
                    731:                rip_disconnect(inp);
                    732:                break;
                    733:
1.13      mycroft   734:        /*
                    735:         * Mark the connection as being incapable of further input.
                    736:         */
                    737:        case PRU_SHUTDOWN:
                    738:                socantsendmore(so);
                    739:                break;
                    740:
1.27      mycroft   741:        case PRU_RCVD:
                    742:                error = EOPNOTSUPP;
                    743:                break;
                    744:
1.13      mycroft   745:        /*
                    746:         * Ship a packet out.  The appropriate raw output
                    747:         * routine handles any massaging necessary.
                    748:         */
                    749:        case PRU_SEND:
1.28      mycroft   750:                if (control && control->m_len) {
                    751:                        m_freem(control);
                    752:                        m_freem(m);
                    753:                        error = EINVAL;
                    754:                        break;
                    755:                }
                    756:        {
1.27      mycroft   757:                if (nam) {
                    758:                        if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13      mycroft   759:                                error = EISCONN;
1.28      mycroft   760:                                goto die;
1.13      mycroft   761:                        }
1.137     rtr       762:                        error = rip_connect_pcb(inp, nam);
1.27      mycroft   763:                        if (error) {
1.28      mycroft   764:                        die:
1.27      mycroft   765:                                m_freem(m);
                    766:                                break;
                    767:                        }
1.13      mycroft   768:                } else {
1.27      mycroft   769:                        if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13      mycroft   770:                                error = ENOTCONN;
1.28      mycroft   771:                                goto die;
1.13      mycroft   772:                        }
                    773:                }
1.27      mycroft   774:                error = rip_output(m, inp);
                    775:                if (nam)
                    776:                        rip_disconnect(inp);
1.28      mycroft   777:        }
1.13      mycroft   778:                break;
                    779:
                    780:        default:
                    781:                panic("rip_usrreq");
1.1       cgd       782:        }
1.121     rmind     783:        splx(s);
1.27      mycroft   784:
1.121     rmind     785:        return error;
1.1       cgd       786: }
1.84      atatat    787:
1.122     rmind     788: PR_WRAP_USRREQS(rip)
                    789: #define        rip_attach      rip_attach_wrapper
                    790: #define        rip_detach      rip_detach_wrapper
1.133     rtr       791: #define        rip_accept      rip_accept_wrapper
1.136     rtr       792: #define        rip_bind        rip_bind_wrapper
                    793: #define        rip_listen      rip_listen_wrapper
1.137     rtr       794: #define        rip_connect     rip_connect_wrapper
1.125     rtr       795: #define        rip_ioctl       rip_ioctl_wrapper
1.128     rtr       796: #define        rip_stat        rip_stat_wrapper
1.132     rtr       797: #define        rip_peeraddr    rip_peeraddr_wrapper
                    798: #define        rip_sockaddr    rip_sockaddr_wrapper
1.135     rtr       799: #define        rip_recvoob     rip_recvoob_wrapper
                    800: #define        rip_sendoob     rip_sendoob_wrapper
1.120     rmind     801: #define        rip_usrreq      rip_usrreq_wrapper
                    802:
                    803: const struct pr_usrreqs rip_usrreqs = {
1.121     rmind     804:        .pr_attach      = rip_attach,
                    805:        .pr_detach      = rip_detach,
1.133     rtr       806:        .pr_accept      = rip_accept,
1.136     rtr       807:        .pr_bind        = rip_bind,
                    808:        .pr_listen      = rip_listen,
1.137     rtr       809:        .pr_connect     = rip_connect,
1.125     rtr       810:        .pr_ioctl       = rip_ioctl,
1.128     rtr       811:        .pr_stat        = rip_stat,
1.132     rtr       812:        .pr_peeraddr    = rip_peeraddr,
                    813:        .pr_sockaddr    = rip_sockaddr,
1.135     rtr       814:        .pr_recvoob     = rip_recvoob,
                    815:        .pr_sendoob     = rip_sendoob,
1.120     rmind     816:        .pr_generic     = rip_usrreq,
                    817: };
                    818:
1.110     pooka     819: static void
                    820: sysctl_net_inet_raw_setup(struct sysctllog **clog)
1.84      atatat    821: {
                    822:
                    823:        sysctl_createv(clog, 0, NULL, NULL,
                    824:                       CTLFLAG_PERMANENT,
                    825:                       CTLTYPE_NODE, "inet", NULL,
                    826:                       NULL, 0, NULL, 0,
                    827:                       CTL_NET, PF_INET, CTL_EOL);
                    828:        sysctl_createv(clog, 0, NULL, NULL,
                    829:                       CTLFLAG_PERMANENT,
                    830:                       CTLTYPE_NODE, "raw",
                    831:                       SYSCTL_DESCR("Raw IPv4 settings"),
                    832:                       NULL, 0, NULL, 0,
                    833:                       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
                    834:
                    835:        sysctl_createv(clog, 0, NULL, NULL,
                    836:                       CTLFLAG_PERMANENT,
1.86      atatat    837:                       CTLTYPE_STRUCT, "pcblist",
1.84      atatat    838:                       SYSCTL_DESCR("Raw IPv4 control block list"),
                    839:                       sysctl_inpcblist, 0, &rawcbtable, 0,
                    840:                       CTL_NET, PF_INET, IPPROTO_RAW,
                    841:                       CTL_CREATE, CTL_EOL);
                    842: }

CVSweb <webmaster@jp.NetBSD.org>