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

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

CVSweb <webmaster@jp.NetBSD.org>