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

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

CVSweb <webmaster@jp.NetBSD.org>