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/in6_gif.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 /*-
    2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/6.4/sys/netinet6/in6_gif.c 182649 2008-09-01 22:57:56Z obrien $");
   34 
   35 #include "opt_inet.h"
   36 #include "opt_inet6.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/socket.h>
   41 #include <sys/sockio.h>
   42 #include <sys/mbuf.h>
   43 #include <sys/errno.h>
   44 #include <sys/queue.h>
   45 #include <sys/syslog.h>
   46 #include <sys/protosw.h>
   47 
   48 #include <sys/malloc.h>
   49 
   50 #include <net/if.h>
   51 #include <net/route.h>
   52 
   53 #include <netinet/in.h>
   54 #include <netinet/in_systm.h>
   55 #ifdef INET
   56 #include <netinet/ip.h>
   57 #endif
   58 #include <netinet/ip_encap.h>
   59 #ifdef INET6
   60 #include <netinet/ip6.h>
   61 #include <netinet6/ip6_var.h>
   62 #include <netinet6/in6_gif.h>
   63 #include <netinet6/in6_var.h>
   64 #endif
   65 #include <netinet6/ip6protosw.h>
   66 #include <netinet/ip_ecn.h>
   67 #ifdef INET6
   68 #include <netinet6/ip6_ecn.h>
   69 #endif
   70 
   71 #include <net/if_gif.h>
   72 
   73 #include <net/net_osdep.h>
   74 
   75 static int gif_validate6(const struct ip6_hdr *, struct gif_softc *,
   76                          struct ifnet *);
   77 
   78 extern  struct domain inet6domain;
   79 struct ip6protosw in6_gif_protosw =
   80 { SOCK_RAW,     &inet6domain,   0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR,
   81   in6_gif_input, rip6_output,   0,              rip6_ctloutput,
   82   0,
   83   0,            0,              0,              0,
   84   &rip6_usrreqs
   85 };
   86 
   87 int
   88 in6_gif_output(ifp, family, m)
   89         struct ifnet *ifp;
   90         int family; /* family of the packet to be encapsulate. */
   91         struct mbuf *m;
   92 {
   93         struct gif_softc *sc = ifp->if_softc;
   94         struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
   95         struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
   96         struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
   97         struct ip6_hdr *ip6;
   98         struct etherip_header eiphdr;
   99         int proto, error;
  100         u_int8_t itos, otos;
  101 
  102         GIF_LOCK_ASSERT(sc);
  103 
  104         if (sin6_src == NULL || sin6_dst == NULL ||
  105             sin6_src->sin6_family != AF_INET6 ||
  106             sin6_dst->sin6_family != AF_INET6) {
  107                 m_freem(m);
  108                 return EAFNOSUPPORT;
  109         }
  110 
  111         switch (family) {
  112 #ifdef INET
  113         case AF_INET:
  114             {
  115                 struct ip *ip;
  116 
  117                 proto = IPPROTO_IPV4;
  118                 if (m->m_len < sizeof(*ip)) {
  119                         m = m_pullup(m, sizeof(*ip));
  120                         if (!m)
  121                                 return ENOBUFS;
  122                 }
  123                 ip = mtod(m, struct ip *);
  124                 itos = ip->ip_tos;
  125                 break;
  126             }
  127 #endif
  128 #ifdef INET6
  129         case AF_INET6:
  130             {
  131                 struct ip6_hdr *ip6;
  132                 proto = IPPROTO_IPV6;
  133                 if (m->m_len < sizeof(*ip6)) {
  134                         m = m_pullup(m, sizeof(*ip6));
  135                         if (!m)
  136                                 return ENOBUFS;
  137                 }
  138                 ip6 = mtod(m, struct ip6_hdr *);
  139                 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
  140                 break;
  141             }
  142 #endif
  143         case AF_LINK:
  144                 proto = IPPROTO_ETHERIP;
  145                 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
  146                 eiphdr.eip_pad = 0;
  147                 /* prepend Ethernet-in-IP header */
  148                 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
  149                 if (m && m->m_len < sizeof(struct etherip_header))
  150                         m = m_pullup(m, sizeof(struct etherip_header));
  151                 if (m == NULL)
  152                         return ENOBUFS;
  153                 bcopy(&eiphdr, mtod(m, struct etherip_header *),
  154                     sizeof(struct etherip_header));
  155                 break;
  156 
  157         default:
  158 #ifdef DEBUG
  159                 printf("in6_gif_output: warning: unknown family %d passed\n",
  160                         family);
  161 #endif
  162                 m_freem(m);
  163                 return EAFNOSUPPORT;
  164         }
  165 
  166         /* prepend new IP header */
  167         M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
  168         if (m && m->m_len < sizeof(struct ip6_hdr))
  169                 m = m_pullup(m, sizeof(struct ip6_hdr));
  170         if (m == NULL) {
  171                 printf("ENOBUFS in in6_gif_output %d\n", __LINE__);
  172                 return ENOBUFS;
  173         }
  174 
  175         ip6 = mtod(m, struct ip6_hdr *);
  176         ip6->ip6_flow   = 0;
  177         ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
  178         ip6->ip6_vfc    |= IPV6_VERSION;
  179         ip6->ip6_plen   = htons((u_short)m->m_pkthdr.len);
  180         ip6->ip6_nxt    = proto;
  181         ip6->ip6_hlim   = ip6_gif_hlim;
  182         ip6->ip6_src    = sin6_src->sin6_addr;
  183         /* bidirectional configured tunnel mode */
  184         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
  185                 ip6->ip6_dst = sin6_dst->sin6_addr;
  186         else  {
  187                 m_freem(m);
  188                 return ENETUNREACH;
  189         }
  190         ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
  191                        &otos, &itos);
  192         ip6->ip6_flow &= ~htonl(0xff << 20);
  193         ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
  194 
  195         if (dst->sin6_family != sin6_dst->sin6_family ||
  196              !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
  197                 /* cache route doesn't match */
  198                 bzero(dst, sizeof(*dst));
  199                 dst->sin6_family = sin6_dst->sin6_family;
  200                 dst->sin6_len = sizeof(struct sockaddr_in6);
  201                 dst->sin6_addr = sin6_dst->sin6_addr;
  202                 if (sc->gif_ro6.ro_rt) {
  203                         RTFREE(sc->gif_ro6.ro_rt);
  204                         sc->gif_ro6.ro_rt = NULL;
  205                 }
  206 #if 0
  207                 GIF2IFP(sc)->if_mtu = GIF_MTU;
  208 #endif
  209         }
  210 
  211         if (sc->gif_ro6.ro_rt == NULL) {
  212                 rtalloc((struct route *)&sc->gif_ro6);
  213                 if (sc->gif_ro6.ro_rt == NULL) {
  214                         m_freem(m);
  215                         return ENETUNREACH;
  216                 }
  217 
  218                 /* if it constitutes infinite encapsulation, punt. */
  219                 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
  220                         m_freem(m);
  221                         return ENETUNREACH;     /*XXX*/
  222                 }
  223 #if 0
  224                 ifp->if_mtu = sc->gif_ro6.ro_rt->rt_ifp->if_mtu
  225                         - sizeof(struct ip6_hdr);
  226 #endif
  227         }
  228 
  229 #ifdef IPV6_MINMTU
  230         /*
  231          * force fragmentation to minimum MTU, to avoid path MTU discovery.
  232          * it is too painful to ask for resend of inner packet, to achieve
  233          * path MTU discovery for encapsulated packets.
  234          */
  235         error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU, 0, NULL, NULL);
  236 #else
  237         error = ip6_output(m, 0, &sc->gif_ro6, 0, 0, NULL, NULL);
  238 #endif
  239 
  240         if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
  241             sc->gif_ro6.ro_rt != NULL) {
  242                 RTFREE(sc->gif_ro6.ro_rt);
  243                 sc->gif_ro6.ro_rt = NULL;
  244         }
  245 
  246         return (error);
  247 }
  248 
  249 int
  250 in6_gif_input(mp, offp, proto)
  251         struct mbuf **mp;
  252         int *offp, proto;
  253 {
  254         struct mbuf *m = *mp;
  255         struct ifnet *gifp = NULL;
  256         struct gif_softc *sc;
  257         struct ip6_hdr *ip6;
  258         int af = 0;
  259         u_int32_t otos;
  260 
  261         ip6 = mtod(m, struct ip6_hdr *);
  262 
  263         sc = (struct gif_softc *)encap_getarg(m);
  264         if (sc == NULL) {
  265                 m_freem(m);
  266                 ip6stat.ip6s_nogif++;
  267                 return IPPROTO_DONE;
  268         }
  269 
  270         gifp = GIF2IFP(sc);
  271         if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
  272                 m_freem(m);
  273                 ip6stat.ip6s_nogif++;
  274                 return IPPROTO_DONE;
  275         }
  276 
  277         otos = ip6->ip6_flow;
  278         m_adj(m, *offp);
  279 
  280         switch (proto) {
  281 #ifdef INET
  282         case IPPROTO_IPV4:
  283             {
  284                 struct ip *ip;
  285                 u_int8_t otos8;
  286                 af = AF_INET;
  287                 otos8 = (ntohl(otos) >> 20) & 0xff;
  288                 if (m->m_len < sizeof(*ip)) {
  289                         m = m_pullup(m, sizeof(*ip));
  290                         if (!m)
  291                                 return IPPROTO_DONE;
  292                 }
  293                 ip = mtod(m, struct ip *);
  294                 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
  295                                   ECN_ALLOWED : ECN_NOCARE,
  296                                   &otos8, &ip->ip_tos) == 0) {
  297                         m_freem(m);
  298                         return IPPROTO_DONE;
  299                 }
  300                 break;
  301             }
  302 #endif /* INET */
  303 #ifdef INET6
  304         case IPPROTO_IPV6:
  305             {
  306                 struct ip6_hdr *ip6;
  307                 af = AF_INET6;
  308                 if (m->m_len < sizeof(*ip6)) {
  309                         m = m_pullup(m, sizeof(*ip6));
  310                         if (!m)
  311                                 return IPPROTO_DONE;
  312                 }
  313                 ip6 = mtod(m, struct ip6_hdr *);
  314                 if (ip6_ecn_egress((gifp->if_flags & IFF_LINK1) ?
  315                                    ECN_ALLOWED : ECN_NOCARE,
  316                                    &otos, &ip6->ip6_flow) == 0) {
  317                         m_freem(m);
  318                         return IPPROTO_DONE;
  319                 }
  320                 break;
  321             }
  322 #endif
  323         case IPPROTO_ETHERIP:
  324                 af = AF_LINK;
  325                 break;  
  326 
  327         default:
  328                 ip6stat.ip6s_nogif++;
  329                 m_freem(m);
  330                 return IPPROTO_DONE;
  331         }
  332 
  333         gif_input(m, af, gifp);
  334         return IPPROTO_DONE;
  335 }
  336 
  337 /*
  338  * validate outer address.
  339  */
  340 static int
  341 gif_validate6(ip6, sc, ifp)
  342         const struct ip6_hdr *ip6;
  343         struct gif_softc *sc;
  344         struct ifnet *ifp;
  345 {
  346         struct sockaddr_in6 *src, *dst;
  347 
  348         src = (struct sockaddr_in6 *)sc->gif_psrc;
  349         dst = (struct sockaddr_in6 *)sc->gif_pdst;
  350 
  351         /*
  352          * Check for address match.  Note that the check is for an incoming
  353          * packet.  We should compare the *source* address in our configuration
  354          * and the *destination* address of the packet, and vice versa.
  355          */
  356         if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
  357             !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
  358                 return 0;
  359 
  360         /* martian filters on outer source - done in ip6_input */
  361 
  362         /* ingress filters on outer source */
  363         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
  364                 struct sockaddr_in6 sin6;
  365                 struct rtentry *rt;
  366 
  367                 bzero(&sin6, sizeof(sin6));
  368                 sin6.sin6_family = AF_INET6;
  369                 sin6.sin6_len = sizeof(struct sockaddr_in6);
  370                 sin6.sin6_addr = ip6->ip6_src;
  371                 sin6.sin6_scope_id = 0; /* XXX */
  372 
  373                 rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
  374                 if (!rt || rt->rt_ifp != ifp) {
  375 #if 0
  376                         log(LOG_WARNING, "%s: packet from %s dropped "
  377                             "due to ingress filter\n", if_name(GIF2IFP(sc)),
  378                             ip6_sprintf(&sin6.sin6_addr));
  379 #endif
  380                         if (rt)
  381                                 rtfree(rt);
  382                         return 0;
  383                 }
  384                 rtfree(rt);
  385         }
  386 
  387         return 128 * 2;
  388 }
  389 
  390 /*
  391  * we know that we are in IFF_UP, outer address available, and outer family
  392  * matched the physical addr family.  see gif_encapcheck().
  393  * sanity check for arg should have been done in the caller.
  394  */
  395 int
  396 gif_encapcheck6(m, off, proto, arg)
  397         const struct mbuf *m;
  398         int off;
  399         int proto;
  400         void *arg;
  401 {
  402         struct ip6_hdr ip6;
  403         struct gif_softc *sc;
  404         struct ifnet *ifp;
  405 
  406         /* sanity check done in caller */
  407         sc = (struct gif_softc *)arg;
  408 
  409         /* LINTED const cast */
  410         m_copydata(m, 0, sizeof(ip6), (caddr_t)&ip6);
  411         ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
  412 
  413         return gif_validate6(&ip6, sc, ifp);
  414 }
  415 
  416 int
  417 in6_gif_attach(sc)
  418         struct gif_softc *sc;
  419 {
  420         sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
  421             (struct protosw *)&in6_gif_protosw, sc);
  422         if (sc->encap_cookie6 == NULL)
  423                 return EEXIST;
  424         return 0;
  425 }
  426 
  427 int
  428 in6_gif_detach(sc)
  429         struct gif_softc *sc;
  430 {
  431         int error;
  432 
  433         error = encap_detach(sc->encap_cookie6);
  434         if (error == 0)
  435                 sc->encap_cookie6 = NULL;
  436         return error;
  437 }

Cache object: 25cf195027fb7e38d664d7034dd578da


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