The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/ip6_etherip.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: ip6_etherip.c,v 1.1.2.1 2006/12/09 11:51:47 bouyer Exp $        */
    2 
    3 /*
    4  *  Copyright (c) 2006, Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
    5  *  All rights reserved.
    6  *
    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 Hans Rosenfeld nor the names of his
   16  *     contributors may be used to endorse or promote products derived
   17  *     from this software without specific prior written permission.
   18  *
   19  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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  *
   31  *
   32  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
   33  * All rights reserved.
   34  *
   35  * Redistribution and use in source and binary forms, with or without
   36  * modification, are permitted provided that the following conditions
   37  * are met:
   38  * 1. Redistributions of source code must retain the above copyright
   39  *    notice, this list of conditions and the following disclaimer.
   40  * 2. Redistributions in binary form must reproduce the above copyright
   41  *    notice, this list of conditions and the following disclaimer in the
   42  *    documentation and/or other materials provided with the distribution.
   43  * 3. Neither the name of the project nor the names of its contributors
   44  *    may be used to endorse or promote products derived from this software
   45  *    without specific prior written permission.
   46  *
   47  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   57  * SUCH DAMAGE.
   58  */
   59 
   60 #include <sys/cdefs.h>
   61 
   62 #include "opt_inet.h"
   63 
   64 #include <sys/param.h>
   65 #include <sys/systm.h>
   66 #include <sys/socket.h>
   67 #include <sys/sockio.h>
   68 #include <sys/mbuf.h>
   69 #include <sys/device.h>
   70 #include <sys/errno.h>
   71 #include <sys/ioctl.h>
   72 #include <sys/syslog.h>
   73 #include <sys/protosw.h>
   74 #include <sys/kernel.h>
   75 
   76 #include <net/if.h>
   77 #include <net/route.h>
   78 
   79 #include <netinet/in.h>
   80 #include <netinet/in_systm.h>
   81 #ifdef INET
   82 #include <netinet/ip.h>
   83 #endif
   84 #ifdef INET6
   85 #include <netinet/ip6.h>
   86 #include <netinet6/ip6_var.h>
   87 #include <netinet6/in6_var.h>
   88 #include <netinet6/ip6_etherip.h>
   89 #endif
   90 
   91 #include <net/if_ether.h>
   92 #include <net/if_media.h>
   93 #include <net/if_etherip.h>
   94 
   95 #include <machine/stdarg.h>
   96 
   97 int
   98 ip6_etherip_output(struct ifnet *ifp, struct mbuf *m)
   99 {
  100         struct etherip_softc *sc = (struct etherip_softc *)ifp->if_softc;
  101         struct sockaddr_in6 *dst, *sin6_src, *sin6_dst;
  102         struct ip6_hdr *ip6;    /* capsule IP header, host byte ordered */
  103         struct etherip_header eiphdr;
  104         int proto, error;
  105 
  106         dst = (struct sockaddr_in6 *)&sc->sc_ro.ro_dst;
  107         sin6_src = (struct sockaddr_in6 *)sc->sc_src;
  108         sin6_dst = (struct sockaddr_in6 *)sc->sc_dst;
  109 
  110         if (sin6_src == NULL || 
  111             sin6_dst == NULL ||
  112             sin6_src->sin6_family != AF_INET6 ||
  113             sin6_dst->sin6_family != AF_INET6) {
  114                 m_freem(m);
  115                 return EAFNOSUPPORT;
  116         }
  117 
  118         /* reset broadcast/multicast flags */
  119         m->m_flags &= ~(M_BCAST|M_MCAST);
  120 
  121         m->m_flags |= M_PKTHDR;
  122         proto = IPPROTO_ETHERIP;
  123 
  124         /* fill and prepend Ethernet-in-IP header */
  125         eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
  126         eiphdr.eip_pad = 0;
  127         M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
  128         if (m == NULL)
  129                 return ENOBUFS;
  130         if (M_UNWRITABLE(m, sizeof(struct etherip_header))) {
  131                 m = m_pullup(m, sizeof(struct etherip_header));
  132                 if (m == NULL)
  133                         return ENOBUFS;
  134         }
  135         memcpy(mtod(m, struct etherip_header *), &eiphdr, 
  136                sizeof(struct etherip_header));
  137         
  138         /* prepend new IP header */
  139         M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
  140         if (m && m->m_len < sizeof(struct ip6_hdr))
  141                 m = m_pullup(m, sizeof(struct ip6_hdr));
  142         if (m == NULL)
  143                 return ENOBUFS;
  144 
  145         ip6 = mtod(m, struct ip6_hdr *);
  146         ip6->ip6_flow = 0;
  147         ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
  148         ip6->ip6_vfc |= IPV6_VERSION;
  149         ip6->ip6_nxt  = proto;
  150         ip6->ip6_hlim = ETHERIP_TTL;
  151         ip6->ip6_src  = sin6_src->sin6_addr;
  152 
  153         /* bidirectional configured tunnel mode */
  154         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
  155                 ip6->ip6_dst = sin6_dst->sin6_addr;
  156         else  {
  157                 m_freem(m);
  158                 return ENETUNREACH;
  159         }
  160 
  161         if (sc->sc_route_expire - time_second <= 0 ||
  162             dst->sin6_family != sin6_dst->sin6_family ||
  163             !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
  164                 /* cache route doesn't match */
  165                 memset(dst, 0, sizeof(*dst));
  166                 dst->sin6_family = sin6_dst->sin6_family;
  167                 dst->sin6_len    = sizeof(struct sockaddr_in6);
  168                 dst->sin6_addr   = sin6_dst->sin6_addr;
  169                 if (sc->sc_ro6.ro_rt) {
  170                         RTFREE(sc->sc_ro6.ro_rt);
  171                         sc->sc_ro6.ro_rt = NULL;
  172                 }
  173         }
  174 
  175         if (sc->sc_ro6.ro_rt == NULL) {
  176                 rtalloc((struct route *)&sc->sc_ro6);
  177                 if (sc->sc_ro6.ro_rt == NULL) {
  178                         m_freem(m);
  179                         return ENETUNREACH;
  180                 }
  181 
  182                 /* if it constitutes infinite encapsulation, punt. */
  183                 if (sc->sc_ro.ro_rt->rt_ifp == ifp) {
  184                         m_freem(m);
  185                         return ENETUNREACH;     /* XXX */
  186                 }
  187 
  188                 sc->sc_route_expire = time_second + ETHERIP_ROUTE_TTL;
  189         }
  190 
  191         /*
  192          * force fragmentation to minimum MTU, to avoid path MTU discovery.
  193          * it is too painful to ask for resend of inner packet, to achieve
  194          * path MTU discovery for encapsulated packets.
  195          */
  196         error = ip6_output(m, 0, &sc->sc_ro6, IPV6_MINMTU,
  197                            (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL);
  198 
  199         return error;
  200 }
  201 
  202 int
  203 ip6_etherip_input(struct mbuf *m, ...)
  204 {
  205         struct etherip_softc *sc;
  206         const struct ip6_hdr *ip6;
  207         struct sockaddr_in6 *src6, *dst6;
  208         struct ifnet *ifp = NULL;
  209         int off, proto;
  210         va_list ap;
  211 
  212         va_start(ap, m);
  213         off = va_arg(ap, int);
  214         proto = va_arg(ap, int);
  215         va_end(ap);
  216 
  217         if (proto != IPPROTO_ETHERIP) {
  218                 m_freem(m);
  219                 ip6stat.ip6s_nogif++;
  220                 return IPPROTO_DONE;
  221         }
  222 
  223         ip6 = mtod(m, const struct ip6_hdr *);
  224 
  225         /* find device configured for this packets src and dst */
  226         LIST_FOREACH(sc, &etherip_softc_list, etherip_list) {
  227                 if( !sc->sc_src || !sc->sc_dst)
  228                         continue;
  229                 if (sc->sc_src->sa_family != AF_INET6 ||
  230                     sc->sc_dst->sa_family != AF_INET6)
  231                         continue;
  232 
  233                 src6 = (struct sockaddr_in6 *)sc->sc_src;
  234                 dst6 = (struct sockaddr_in6 *)sc->sc_dst;
  235 
  236                 if (!IN6_ARE_ADDR_EQUAL(&src6->sin6_addr, &ip6->ip6_dst) ||
  237                     !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &ip6->ip6_src))
  238                         continue;
  239 
  240                 ifp = &sc->sc_ec.ec_if;
  241                 break;
  242         }
  243 
  244         /* no matching device found */
  245         if (!ifp) {
  246                 m_freem(m);
  247                 ip6stat.ip6s_odropped++;
  248                 return IPPROTO_DONE;
  249         }
  250 
  251         m_adj(m, off);
  252 
  253         /*
  254          * Section 4 of RFC 3378 requires that the EtherIP header of incoming
  255          * packets is verified to contain the correct values in the version and
  256          * reserved fields, and packets with wrong values be dropped.
  257          *
  258          * There is some discussion about what exactly the header should look
  259          * like, the RFC is not very clear there. To be compatible with broken
  260          * implementations, we don't check the header on incoming packets,
  261          * relying on the ethernet code to filter out garbage.
  262          *
  263          * The header we use for sending is compatible with the original
  264          * implementation in OpenBSD, which was used in former NetBSD versions
  265          * and is used in FreeBSD. One Linux implementation is known to use the
  266          * same value.
  267          */
  268         m_adj(m, sizeof(struct etherip_header));
  269         m = m_pullup(m, sizeof(struct ether_header));
  270         if (m == NULL) {
  271                 ifp->if_ierrors++;
  272                 return IPPROTO_DONE;
  273         }
  274 
  275         m->m_pkthdr.rcvif = ifp;
  276         m->m_flags &= ~(M_BCAST|M_MCAST);
  277 
  278 #if NBPFILTER > 0
  279         if (ifp->if_bpf)
  280                 bpf_mtap(ifp->if_bpf, m);
  281 #endif
  282 
  283         ifp->if_ipackets++;
  284         (ifp->if_input)(ifp, m);
  285 
  286         return IPPROTO_DONE;
  287 }

Cache object: 5b5909315828597c9e0a6511aff84f63


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.