[BACK]Return to raw_ip.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet

Annotation of src/sys/netinet/raw_ip.c, Revision 1.171.2.1

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

CVSweb <webmaster@jp.NetBSD.org>