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/net/if_gre.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) 1998 The NetBSD Foundation, Inc.
    3  * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org>
    4  * All rights reserved.
    5  *
    6  * This code is derived from software contributed to The NetBSD Foundation
    7  * by Heiko W.Rupp <hwr@pilhuhn.de>
    8  *
    9  * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30  * POSSIBILITY OF SUCH DAMAGE.
   31  *
   32  * $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include "opt_inet.h"
   39 #include "opt_inet6.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/jail.h>
   43 #include <sys/kernel.h>
   44 #include <sys/lock.h>
   45 #include <sys/libkern.h>
   46 #include <sys/malloc.h>
   47 #include <sys/module.h>
   48 #include <sys/mbuf.h>
   49 #include <sys/priv.h>
   50 #include <sys/proc.h>
   51 #include <sys/protosw.h>
   52 #include <sys/rmlock.h>
   53 #include <sys/socket.h>
   54 #include <sys/sockio.h>
   55 #include <sys/sx.h>
   56 #include <sys/sysctl.h>
   57 #include <sys/syslog.h>
   58 #include <sys/systm.h>
   59 
   60 #include <net/ethernet.h>
   61 #include <net/if.h>
   62 #include <net/if_var.h>
   63 #include <net/if_clone.h>
   64 #include <net/if_types.h>
   65 #include <net/netisr.h>
   66 #include <net/vnet.h>
   67 #include <net/route.h>
   68 
   69 #include <netinet/in.h>
   70 #ifdef INET
   71 #include <netinet/in_systm.h>
   72 #include <netinet/in_var.h>
   73 #include <netinet/ip.h>
   74 #include <netinet/ip_var.h>
   75 #endif
   76 
   77 #ifdef INET6
   78 #include <netinet/ip6.h>
   79 #include <netinet6/in6_var.h>
   80 #include <netinet6/ip6_var.h>
   81 #include <netinet6/scope6_var.h>
   82 #endif
   83 
   84 #include <netinet/ip_encap.h>
   85 #include <net/bpf.h>
   86 #include <net/if_gre.h>
   87 
   88 #include <machine/in_cksum.h>
   89 #include <security/mac/mac_framework.h>
   90 
   91 #define GREMTU                  1476
   92 static const char grename[] = "gre";
   93 static MALLOC_DEFINE(M_GRE, grename, "Generic Routing Encapsulation");
   94 static VNET_DEFINE(struct mtx, gre_mtx);
   95 #define V_gre_mtx       VNET(gre_mtx)
   96 #define GRE_LIST_LOCK_INIT(x)           mtx_init(&V_gre_mtx, "gre_mtx", NULL, \
   97                                             MTX_DEF)
   98 #define GRE_LIST_LOCK_DESTROY(x)        mtx_destroy(&V_gre_mtx)
   99 #define GRE_LIST_LOCK(x)                mtx_lock(&V_gre_mtx)
  100 #define GRE_LIST_UNLOCK(x)              mtx_unlock(&V_gre_mtx)
  101 
  102 static VNET_DEFINE(LIST_HEAD(, gre_softc), gre_softc_list);
  103 #define V_gre_softc_list        VNET(gre_softc_list)
  104 static struct sx gre_ioctl_sx;
  105 SX_SYSINIT(gre_ioctl_sx, &gre_ioctl_sx, "gre_ioctl");
  106 
  107 static int      gre_clone_create(struct if_clone *, int, caddr_t);
  108 static void     gre_clone_destroy(struct ifnet *);
  109 static VNET_DEFINE(struct if_clone *, gre_cloner);
  110 #define V_gre_cloner    VNET(gre_cloner)
  111 
  112 static void     gre_qflush(struct ifnet *);
  113 static int      gre_transmit(struct ifnet *, struct mbuf *);
  114 static int      gre_ioctl(struct ifnet *, u_long, caddr_t);
  115 static int      gre_output(struct ifnet *, struct mbuf *,
  116                     const struct sockaddr *, struct route *);
  117 
  118 static void     gre_updatehdr(struct gre_softc *);
  119 static int      gre_set_tunnel(struct ifnet *, struct sockaddr *,
  120     struct sockaddr *);
  121 static void     gre_delete_tunnel(struct ifnet *);
  122 
  123 SYSCTL_DECL(_net_link);
  124 static SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
  125     "Generic Routing Encapsulation");
  126 #ifndef MAX_GRE_NEST
  127 /*
  128  * This macro controls the default upper limitation on nesting of gre tunnels.
  129  * Since, setting a large value to this macro with a careless configuration
  130  * may introduce system crash, we don't allow any nestings by default.
  131  * If you need to configure nested gre tunnels, you can define this macro
  132  * in your kernel configuration file.  However, if you do so, please be
  133  * careful to configure the tunnels so that it won't make a loop.
  134  */
  135 #define MAX_GRE_NEST 1
  136 #endif
  137 
  138 static VNET_DEFINE(int, max_gre_nesting) = MAX_GRE_NEST;
  139 #define V_max_gre_nesting       VNET(max_gre_nesting)
  140 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW | CTLFLAG_VNET,
  141     &VNET_NAME(max_gre_nesting), 0, "Max nested tunnels");
  142 
  143 static void
  144 vnet_gre_init(const void *unused __unused)
  145 {
  146         LIST_INIT(&V_gre_softc_list);
  147         GRE_LIST_LOCK_INIT();
  148         V_gre_cloner = if_clone_simple(grename, gre_clone_create,
  149             gre_clone_destroy, 0);
  150 }
  151 VNET_SYSINIT(vnet_gre_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
  152     vnet_gre_init, NULL);
  153 
  154 static void
  155 vnet_gre_uninit(const void *unused __unused)
  156 {
  157 
  158         if_clone_detach(V_gre_cloner);
  159         GRE_LIST_LOCK_DESTROY();
  160 }
  161 VNET_SYSUNINIT(vnet_gre_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
  162     vnet_gre_uninit, NULL);
  163 
  164 static int
  165 gre_clone_create(struct if_clone *ifc, int unit, caddr_t params)
  166 {
  167         struct gre_softc *sc;
  168 
  169         sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
  170         sc->gre_fibnum = curthread->td_proc->p_fibnum;
  171         GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
  172         GRE_LOCK_INIT(sc);
  173         GRE2IFP(sc)->if_softc = sc;
  174         if_initname(GRE2IFP(sc), grename, unit);
  175 
  176         GRE2IFP(sc)->if_mtu = GREMTU;
  177         GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
  178         GRE2IFP(sc)->if_output = gre_output;
  179         GRE2IFP(sc)->if_ioctl = gre_ioctl;
  180         GRE2IFP(sc)->if_transmit = gre_transmit;
  181         GRE2IFP(sc)->if_qflush = gre_qflush;
  182         GRE2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
  183         GRE2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
  184         if_attach(GRE2IFP(sc));
  185         bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
  186         GRE_LIST_LOCK();
  187         LIST_INSERT_HEAD(&V_gre_softc_list, sc, gre_list);
  188         GRE_LIST_UNLOCK();
  189         return (0);
  190 }
  191 
  192 static void
  193 gre_clone_destroy(struct ifnet *ifp)
  194 {
  195         struct gre_softc *sc;
  196 
  197         sx_xlock(&gre_ioctl_sx);
  198         sc = ifp->if_softc;
  199         gre_delete_tunnel(ifp);
  200         GRE_LIST_LOCK();
  201         LIST_REMOVE(sc, gre_list);
  202         GRE_LIST_UNLOCK();
  203         bpfdetach(ifp);
  204         if_detach(ifp);
  205         ifp->if_softc = NULL;
  206         sx_xunlock(&gre_ioctl_sx);
  207 
  208         if_free(ifp);
  209         GRE_LOCK_DESTROY(sc);
  210         free(sc, M_GRE);
  211 }
  212 
  213 static int
  214 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  215 {
  216         GRE_RLOCK_TRACKER;
  217         struct ifreq *ifr = (struct ifreq *)data;
  218         struct sockaddr *src, *dst;
  219         struct gre_softc *sc;
  220 #ifdef INET
  221         struct sockaddr_in *sin = NULL;
  222 #endif
  223 #ifdef INET6
  224         struct sockaddr_in6 *sin6 = NULL;
  225 #endif
  226         uint32_t opt;
  227         int error;
  228 
  229         switch (cmd) {
  230         case SIOCSIFMTU:
  231                  /* XXX: */
  232                 if (ifr->ifr_mtu < 576)
  233                         return (EINVAL);
  234                 ifp->if_mtu = ifr->ifr_mtu;
  235                 return (0);
  236         case SIOCSIFADDR:
  237                 ifp->if_flags |= IFF_UP;
  238         case SIOCSIFFLAGS:
  239         case SIOCADDMULTI:
  240         case SIOCDELMULTI:
  241                 return (0);
  242         case GRESADDRS:
  243         case GRESADDRD:
  244         case GREGADDRS:
  245         case GREGADDRD:
  246         case GRESPROTO:
  247         case GREGPROTO:
  248                 return (EOPNOTSUPP);
  249         }
  250         src = dst = NULL;
  251         sx_xlock(&gre_ioctl_sx);
  252         sc = ifp->if_softc;
  253         if (sc == NULL) {
  254                 error = ENXIO;
  255                 goto end;
  256         }
  257         error = 0;
  258         switch (cmd) {
  259         case SIOCSIFPHYADDR:
  260 #ifdef INET6
  261         case SIOCSIFPHYADDR_IN6:
  262 #endif
  263                 error = EINVAL;
  264                 switch (cmd) {
  265 #ifdef INET
  266                 case SIOCSIFPHYADDR:
  267                         src = (struct sockaddr *)
  268                                 &(((struct in_aliasreq *)data)->ifra_addr);
  269                         dst = (struct sockaddr *)
  270                                 &(((struct in_aliasreq *)data)->ifra_dstaddr);
  271                         break;
  272 #endif
  273 #ifdef INET6
  274                 case SIOCSIFPHYADDR_IN6:
  275                         src = (struct sockaddr *)
  276                                 &(((struct in6_aliasreq *)data)->ifra_addr);
  277                         dst = (struct sockaddr *)
  278                                 &(((struct in6_aliasreq *)data)->ifra_dstaddr);
  279                         break;
  280 #endif
  281                 default:
  282                         error = EAFNOSUPPORT;
  283                         goto end;
  284                 }
  285                 /* sa_family must be equal */
  286                 if (src->sa_family != dst->sa_family ||
  287                     src->sa_len != dst->sa_len)
  288                         goto end;
  289 
  290                 /* validate sa_len */
  291                 switch (src->sa_family) {
  292 #ifdef INET
  293                 case AF_INET:
  294                         if (src->sa_len != sizeof(struct sockaddr_in))
  295                                 goto end;
  296                         break;
  297 #endif
  298 #ifdef INET6
  299                 case AF_INET6:
  300                         if (src->sa_len != sizeof(struct sockaddr_in6))
  301                                 goto end;
  302                         break;
  303 #endif
  304                 default:
  305                         error = EAFNOSUPPORT;
  306                         goto end;
  307                 }
  308                 /* check sa_family looks sane for the cmd */
  309                 error = EAFNOSUPPORT;
  310                 switch (cmd) {
  311 #ifdef INET
  312                 case SIOCSIFPHYADDR:
  313                         if (src->sa_family == AF_INET)
  314                                 break;
  315                         goto end;
  316 #endif
  317 #ifdef INET6
  318                 case SIOCSIFPHYADDR_IN6:
  319                         if (src->sa_family == AF_INET6)
  320                                 break;
  321                         goto end;
  322 #endif
  323                 }
  324                 error = EADDRNOTAVAIL;
  325                 switch (src->sa_family) {
  326 #ifdef INET
  327                 case AF_INET:
  328                         if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
  329                             satosin(dst)->sin_addr.s_addr == INADDR_ANY)
  330                                 goto end;
  331                         break;
  332 #endif
  333 #ifdef INET6
  334                 case AF_INET6:
  335                         if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
  336                             ||
  337                             IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
  338                                 goto end;
  339                         /*
  340                          * Check validity of the scope zone ID of the
  341                          * addresses, and convert it into the kernel
  342                          * internal form if necessary.
  343                          */
  344                         error = sa6_embedscope(satosin6(src), 0);
  345                         if (error != 0)
  346                                 goto end;
  347                         error = sa6_embedscope(satosin6(dst), 0);
  348                         if (error != 0)
  349                                 goto end;
  350 #endif
  351                 }
  352                 error = gre_set_tunnel(ifp, src, dst);
  353                 break;
  354         case SIOCDIFPHYADDR:
  355                 gre_delete_tunnel(ifp);
  356                 break;
  357         case SIOCGIFPSRCADDR:
  358         case SIOCGIFPDSTADDR:
  359 #ifdef INET6
  360         case SIOCGIFPSRCADDR_IN6:
  361         case SIOCGIFPDSTADDR_IN6:
  362 #endif
  363                 if (sc->gre_family == 0) {
  364                         error = EADDRNOTAVAIL;
  365                         break;
  366                 }
  367                 GRE_RLOCK(sc);
  368                 switch (cmd) {
  369 #ifdef INET
  370                 case SIOCGIFPSRCADDR:
  371                 case SIOCGIFPDSTADDR:
  372                         if (sc->gre_family != AF_INET) {
  373                                 error = EADDRNOTAVAIL;
  374                                 break;
  375                         }
  376                         sin = (struct sockaddr_in *)&ifr->ifr_addr;
  377                         memset(sin, 0, sizeof(*sin));
  378                         sin->sin_family = AF_INET;
  379                         sin->sin_len = sizeof(*sin);
  380                         break;
  381 #endif
  382 #ifdef INET6
  383                 case SIOCGIFPSRCADDR_IN6:
  384                 case SIOCGIFPDSTADDR_IN6:
  385                         if (sc->gre_family != AF_INET6) {
  386                                 error = EADDRNOTAVAIL;
  387                                 break;
  388                         }
  389                         sin6 = (struct sockaddr_in6 *)
  390                                 &(((struct in6_ifreq *)data)->ifr_addr);
  391                         memset(sin6, 0, sizeof(*sin6));
  392                         sin6->sin6_family = AF_INET6;
  393                         sin6->sin6_len = sizeof(*sin6);
  394                         break;
  395 #endif
  396                 }
  397                 if (error == 0) {
  398                         switch (cmd) {
  399 #ifdef INET
  400                         case SIOCGIFPSRCADDR:
  401                                 sin->sin_addr = sc->gre_oip.ip_src;
  402                                 break;
  403                         case SIOCGIFPDSTADDR:
  404                                 sin->sin_addr = sc->gre_oip.ip_dst;
  405                                 break;
  406 #endif
  407 #ifdef INET6
  408                         case SIOCGIFPSRCADDR_IN6:
  409                                 sin6->sin6_addr = sc->gre_oip6.ip6_src;
  410                                 break;
  411                         case SIOCGIFPDSTADDR_IN6:
  412                                 sin6->sin6_addr = sc->gre_oip6.ip6_dst;
  413                                 break;
  414 #endif
  415                         }
  416                 }
  417                 GRE_RUNLOCK(sc);
  418                 if (error != 0)
  419                         break;
  420                 switch (cmd) {
  421 #ifdef INET
  422                 case SIOCGIFPSRCADDR:
  423                 case SIOCGIFPDSTADDR:
  424                         error = prison_if(curthread->td_ucred,
  425                             (struct sockaddr *)sin);
  426                         if (error != 0)
  427                                 memset(sin, 0, sizeof(*sin));
  428                         break;
  429 #endif
  430 #ifdef INET6
  431                 case SIOCGIFPSRCADDR_IN6:
  432                 case SIOCGIFPDSTADDR_IN6:
  433                         error = prison_if(curthread->td_ucred,
  434                             (struct sockaddr *)sin6);
  435                         if (error == 0)
  436                                 error = sa6_recoverscope(sin6);
  437                         if (error != 0)
  438                                 memset(sin6, 0, sizeof(*sin6));
  439 #endif
  440                 }
  441                 break;
  442         case SIOCGTUNFIB:
  443                 ifr->ifr_fib = sc->gre_fibnum;
  444                 break;
  445         case SIOCSTUNFIB:
  446                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
  447                         break;
  448                 if (ifr->ifr_fib >= rt_numfibs)
  449                         error = EINVAL;
  450                 else
  451                         sc->gre_fibnum = ifr->ifr_fib;
  452                 break;
  453         case GRESKEY:
  454                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
  455                         break;
  456                 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
  457                     sizeof(opt))) != 0)
  458                         break;
  459                 if (sc->gre_key != opt) {
  460                         GRE_WLOCK(sc);
  461                         sc->gre_key = opt;
  462                         gre_updatehdr(sc);
  463                         GRE_WUNLOCK(sc);
  464                 }
  465                 break;
  466         case GREGKEY:
  467                 error = copyout(&sc->gre_key, ifr_data_get_ptr(ifr),
  468                     sizeof(sc->gre_key));
  469                 break;
  470         case GRESOPTS:
  471                 if ((error = priv_check(curthread, PRIV_NET_GRE)) != 0)
  472                         break;
  473                 if ((error = copyin(ifr_data_get_ptr(ifr), &opt,
  474                     sizeof(opt))) != 0)
  475                         break;
  476                 if (opt & ~GRE_OPTMASK)
  477                         error = EINVAL;
  478                 else {
  479                         if (sc->gre_options != opt) {
  480                                 GRE_WLOCK(sc);
  481                                 sc->gre_options = opt;
  482                                 gre_updatehdr(sc);
  483                                 GRE_WUNLOCK(sc);
  484                         }
  485                 }
  486                 break;
  487 
  488         case GREGOPTS:
  489                 error = copyout(&sc->gre_options, ifr_data_get_ptr(ifr),
  490                     sizeof(sc->gre_options));
  491                 break;
  492         default:
  493                 error = EINVAL;
  494                 break;
  495         }
  496 end:
  497         sx_xunlock(&gre_ioctl_sx);
  498         return (error);
  499 }
  500 
  501 static void
  502 gre_updatehdr(struct gre_softc *sc)
  503 {
  504         struct grehdr *gh = NULL;
  505         uint32_t *opts;
  506         uint16_t flags;
  507 
  508         GRE_WLOCK_ASSERT(sc);
  509         switch (sc->gre_family) {
  510 #ifdef INET
  511         case AF_INET:
  512                 sc->gre_hlen = sizeof(struct greip);
  513                 sc->gre_oip.ip_v = IPPROTO_IPV4;
  514                 sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
  515                 sc->gre_oip.ip_p = IPPROTO_GRE;
  516                 gh = &sc->gre_gihdr->gi_gre;
  517                 break;
  518 #endif
  519 #ifdef INET6
  520         case AF_INET6:
  521                 sc->gre_hlen = sizeof(struct greip6);
  522                 sc->gre_oip6.ip6_vfc = IPV6_VERSION;
  523                 sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
  524                 gh = &sc->gre_gi6hdr->gi6_gre;
  525                 break;
  526 #endif
  527         default:
  528                 return;
  529         }
  530         flags = 0;
  531         opts = gh->gre_opts;
  532         if (sc->gre_options & GRE_ENABLE_CSUM) {
  533                 flags |= GRE_FLAGS_CP;
  534                 sc->gre_hlen += 2 * sizeof(uint16_t);
  535                 *opts++ = 0;
  536         }
  537         if (sc->gre_key != 0) {
  538                 flags |= GRE_FLAGS_KP;
  539                 sc->gre_hlen += sizeof(uint32_t);
  540                 *opts++ = htonl(sc->gre_key);
  541         }
  542         if (sc->gre_options & GRE_ENABLE_SEQ) {
  543                 flags |= GRE_FLAGS_SP;
  544                 sc->gre_hlen += sizeof(uint32_t);
  545                 *opts++ = 0;
  546         } else
  547                 sc->gre_oseq = 0;
  548         gh->gre_flags = htons(flags);
  549 }
  550 
  551 static void
  552 gre_detach(struct gre_softc *sc)
  553 {
  554 
  555         sx_assert(&gre_ioctl_sx, SA_XLOCKED);
  556         if (sc->gre_ecookie != NULL)
  557                 encap_detach(sc->gre_ecookie);
  558         sc->gre_ecookie = NULL;
  559 }
  560 
  561 static int
  562 gre_set_tunnel(struct ifnet *ifp, struct sockaddr *src,
  563     struct sockaddr *dst)
  564 {
  565         struct gre_softc *sc, *tsc;
  566 #ifdef INET6
  567         struct ip6_hdr *ip6;
  568 #endif
  569 #ifdef INET
  570         struct ip *ip;
  571 #endif
  572         void *hdr;
  573         int error;
  574 
  575         sx_assert(&gre_ioctl_sx, SA_XLOCKED);
  576         GRE_LIST_LOCK();
  577         sc = ifp->if_softc;
  578         LIST_FOREACH(tsc, &V_gre_softc_list, gre_list) {
  579                 if (tsc == sc || tsc->gre_family != src->sa_family)
  580                         continue;
  581 #ifdef INET
  582                 if (tsc->gre_family == AF_INET &&
  583                     tsc->gre_oip.ip_src.s_addr ==
  584                     satosin(src)->sin_addr.s_addr &&
  585                     tsc->gre_oip.ip_dst.s_addr ==
  586                     satosin(dst)->sin_addr.s_addr) {
  587                         GRE_LIST_UNLOCK();
  588                         return (EADDRNOTAVAIL);
  589                 }
  590 #endif
  591 #ifdef INET6
  592                 if (tsc->gre_family == AF_INET6 &&
  593                     IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_src,
  594                     &satosin6(src)->sin6_addr) &&
  595                     IN6_ARE_ADDR_EQUAL(&tsc->gre_oip6.ip6_dst,
  596                         &satosin6(dst)->sin6_addr)) {
  597                         GRE_LIST_UNLOCK();
  598                         return (EADDRNOTAVAIL);
  599                 }
  600 #endif
  601         }
  602         GRE_LIST_UNLOCK();
  603 
  604         switch (src->sa_family) {
  605 #ifdef INET
  606         case AF_INET:
  607                 hdr = ip = malloc(sizeof(struct greip) +
  608                     3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO);
  609                 ip->ip_src = satosin(src)->sin_addr;
  610                 ip->ip_dst = satosin(dst)->sin_addr;
  611                 break;
  612 #endif
  613 #ifdef INET6
  614         case AF_INET6:
  615                 hdr = ip6 = malloc(sizeof(struct greip6) +
  616                     3 * sizeof(uint32_t), M_GRE, M_WAITOK | M_ZERO);
  617                 ip6->ip6_src = satosin6(src)->sin6_addr;
  618                 ip6->ip6_dst = satosin6(dst)->sin6_addr;
  619                 break;
  620 #endif
  621         default:
  622                 return (EAFNOSUPPORT);
  623         }
  624         if (sc->gre_family != 0)
  625                 gre_detach(sc);
  626         GRE_WLOCK(sc);
  627         if (sc->gre_family != 0)
  628                 free(sc->gre_hdr, M_GRE);
  629         sc->gre_family = src->sa_family;
  630         sc->gre_hdr = hdr;
  631         sc->gre_oseq = 0;
  632         sc->gre_iseq = UINT32_MAX;
  633         gre_updatehdr(sc);
  634         GRE_WUNLOCK(sc);
  635 
  636         error = 0;
  637         switch (src->sa_family) {
  638 #ifdef INET
  639         case AF_INET:
  640                 error = in_gre_attach(sc);
  641                 break;
  642 #endif
  643 #ifdef INET6
  644         case AF_INET6:
  645                 error = in6_gre_attach(sc);
  646                 break;
  647 #endif
  648         }
  649         if (error == 0) {
  650                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  651                 if_link_state_change(ifp, LINK_STATE_UP);
  652         }
  653         return (error);
  654 }
  655 
  656 static void
  657 gre_delete_tunnel(struct ifnet *ifp)
  658 {
  659         struct gre_softc *sc = ifp->if_softc;
  660         int family;
  661 
  662         GRE_WLOCK(sc);
  663         family = sc->gre_family;
  664         sc->gre_family = 0;
  665         GRE_WUNLOCK(sc);
  666         if (family != 0) {
  667                 gre_detach(sc);
  668                 free(sc->gre_hdr, M_GRE);
  669         }
  670         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  671         if_link_state_change(ifp, LINK_STATE_DOWN);
  672 }
  673 
  674 int
  675 gre_input(struct mbuf **mp, int *offp, int proto)
  676 {
  677         struct gre_softc *sc;
  678         struct grehdr *gh;
  679         struct ifnet *ifp;
  680         struct mbuf *m;
  681         uint32_t *opts;
  682 #ifdef notyet
  683         uint32_t key;
  684 #endif
  685         uint16_t flags;
  686         int hlen, isr, af;
  687 
  688         m = *mp;
  689         sc = encap_getarg(m);
  690         KASSERT(sc != NULL, ("encap_getarg returned NULL"));
  691 
  692         ifp = GRE2IFP(sc);
  693         hlen = *offp + sizeof(struct grehdr) + 4 * sizeof(uint32_t);
  694         if (m->m_pkthdr.len < hlen)
  695                 goto drop;
  696         if (m->m_len < hlen) {
  697                 m = m_pullup(m, hlen);
  698                 if (m == NULL)
  699                         goto drop;
  700         }
  701         gh = (struct grehdr *)mtodo(m, *offp);
  702         flags = ntohs(gh->gre_flags);
  703         if (flags & ~GRE_FLAGS_MASK)
  704                 goto drop;
  705         opts = gh->gre_opts;
  706         hlen = 2 * sizeof(uint16_t);
  707         if (flags & GRE_FLAGS_CP) {
  708                 /* reserved1 field must be zero */
  709                 if (((uint16_t *)opts)[1] != 0)
  710                         goto drop;
  711                 if (in_cksum_skip(m, m->m_pkthdr.len, *offp) != 0)
  712                         goto drop;
  713                 hlen += 2 * sizeof(uint16_t);
  714                 opts++;
  715         }
  716         if (flags & GRE_FLAGS_KP) {
  717 #ifdef notyet
  718         /* 
  719          * XXX: The current implementation uses the key only for outgoing
  720          * packets. But we can check the key value here, or even in the
  721          * encapcheck function.
  722          */
  723                 key = ntohl(*opts);
  724 #endif
  725                 hlen += sizeof(uint32_t);
  726                 opts++;
  727     }
  728 #ifdef notyet
  729         } else
  730                 key = 0;
  731 
  732         if (sc->gre_key != 0 && (key != sc->gre_key || key != 0))
  733                 goto drop;
  734 #endif
  735         if (flags & GRE_FLAGS_SP) {
  736 #ifdef notyet
  737                 seq = ntohl(*opts);
  738 #endif
  739                 hlen += sizeof(uint32_t);
  740         }
  741         switch (ntohs(gh->gre_proto)) {
  742         case ETHERTYPE_WCCP:
  743                 /*
  744                  * For WCCP skip an additional 4 bytes if after GRE header
  745                  * doesn't follow an IP header.
  746                  */
  747                 if (flags == 0 && (*(uint8_t *)gh->gre_opts & 0xF0) != 0x40)
  748                         hlen += sizeof(uint32_t);
  749                 /* FALLTHROUGH */
  750         case ETHERTYPE_IP:
  751                 isr = NETISR_IP;
  752                 af = AF_INET;
  753                 break;
  754         case ETHERTYPE_IPV6:
  755                 isr = NETISR_IPV6;
  756                 af = AF_INET6;
  757                 break;
  758         default:
  759                 goto drop;
  760         }
  761         m_adj(m, *offp + hlen);
  762         m_clrprotoflags(m);
  763         m->m_pkthdr.rcvif = ifp;
  764         M_SETFIB(m, ifp->if_fib);
  765 #ifdef MAC
  766         mac_ifnet_create_mbuf(ifp, m);
  767 #endif
  768         BPF_MTAP2(ifp, &af, sizeof(af), m);
  769         if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
  770         if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
  771         if ((ifp->if_flags & IFF_MONITOR) != 0)
  772                 m_freem(m);
  773         else
  774                 netisr_dispatch(isr, m);
  775         return (IPPROTO_DONE);
  776 drop:
  777         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
  778         m_freem(m);
  779         return (IPPROTO_DONE);
  780 }
  781 
  782 #define MTAG_GRE        1307983903
  783 static int
  784 gre_check_nesting(struct ifnet *ifp, struct mbuf *m)
  785 {
  786         struct m_tag *mtag;
  787         int count;
  788 
  789         count = 1;
  790         mtag = NULL;
  791         while ((mtag = m_tag_locate(m, MTAG_GRE, 0, mtag)) != NULL) {
  792                 if (*(struct ifnet **)(mtag + 1) == ifp) {
  793                         log(LOG_NOTICE, "%s: loop detected\n", ifp->if_xname);
  794                         return (EIO);
  795                 }
  796                 count++;
  797         }
  798         if (count > V_max_gre_nesting) {
  799                 log(LOG_NOTICE,
  800                     "%s: if_output recursively called too many times(%d)\n",
  801                     ifp->if_xname, count);
  802                 return (EIO);
  803         }
  804         mtag = m_tag_alloc(MTAG_GRE, 0, sizeof(struct ifnet *), M_NOWAIT);
  805         if (mtag == NULL)
  806                 return (ENOMEM);
  807         *(struct ifnet **)(mtag + 1) = ifp;
  808         m_tag_prepend(m, mtag);
  809         return (0);
  810 }
  811 
  812 static int
  813 gre_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
  814    struct route *ro)
  815 {
  816         uint32_t af;
  817         int error;
  818 
  819 #ifdef MAC
  820         error = mac_ifnet_check_transmit(ifp, m);
  821         if (error != 0)
  822                 goto drop;
  823 #endif
  824         if ((ifp->if_flags & IFF_MONITOR) != 0 ||
  825             (ifp->if_flags & IFF_UP) == 0) {
  826                 error = ENETDOWN;
  827                 goto drop;
  828         }
  829 
  830         error = gre_check_nesting(ifp, m);
  831         if (error != 0)
  832                 goto drop;
  833 
  834         m->m_flags &= ~(M_BCAST|M_MCAST);
  835         if (dst->sa_family == AF_UNSPEC)
  836                 bcopy(dst->sa_data, &af, sizeof(af));
  837         else
  838                 af = dst->sa_family;
  839         BPF_MTAP2(ifp, &af, sizeof(af), m);
  840         m->m_pkthdr.csum_data = af;     /* save af for if_transmit */
  841         return (ifp->if_transmit(ifp, m));
  842 drop:
  843         m_freem(m);
  844         if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  845         return (error);
  846 }
  847 
  848 static void
  849 gre_setseqn(struct grehdr *gh, uint32_t seq)
  850 {
  851         uint32_t *opts;
  852         uint16_t flags;
  853 
  854         opts = gh->gre_opts;
  855         flags = ntohs(gh->gre_flags);
  856         KASSERT((flags & GRE_FLAGS_SP) != 0,
  857             ("gre_setseqn called, but GRE_FLAGS_SP isn't set "));
  858         if (flags & GRE_FLAGS_CP)
  859                 opts++;
  860         if (flags & GRE_FLAGS_KP)
  861                 opts++;
  862         *opts = htonl(seq);
  863 }
  864 
  865 static int
  866 gre_transmit(struct ifnet *ifp, struct mbuf *m)
  867 {
  868         GRE_RLOCK_TRACKER;
  869         struct gre_softc *sc;
  870         struct grehdr *gh;
  871         uint32_t iaf, oaf, oseq;
  872         int error, hlen, olen, plen;
  873         int want_seq, want_csum;
  874 
  875         plen = 0;
  876         sc = ifp->if_softc;
  877         if (sc == NULL) {
  878                 error = ENETDOWN;
  879                 m_freem(m);
  880                 goto drop;
  881         }
  882         GRE_RLOCK(sc);
  883         if (sc->gre_family == 0) {
  884                 GRE_RUNLOCK(sc);
  885                 error = ENETDOWN;
  886                 m_freem(m);
  887                 goto drop;
  888         }
  889         iaf = m->m_pkthdr.csum_data;
  890         oaf = sc->gre_family;
  891         hlen = sc->gre_hlen;
  892         want_seq = (sc->gre_options & GRE_ENABLE_SEQ) != 0;
  893         if (want_seq)
  894                 oseq = sc->gre_oseq++; /* XXX */
  895         else
  896                 oseq = 0;               /* Make compiler happy. */
  897         want_csum = (sc->gre_options & GRE_ENABLE_CSUM) != 0;
  898         M_SETFIB(m, sc->gre_fibnum);
  899         M_PREPEND(m, hlen, M_NOWAIT);
  900         if (m == NULL) {
  901                 GRE_RUNLOCK(sc);
  902                 error = ENOBUFS;
  903                 goto drop;
  904         }
  905         bcopy(sc->gre_hdr, mtod(m, void *), hlen);
  906         GRE_RUNLOCK(sc);
  907         switch (oaf) {
  908 #ifdef INET
  909         case AF_INET:
  910                 olen = sizeof(struct ip);
  911                 break;
  912 #endif
  913 #ifdef INET6
  914         case AF_INET6:
  915                 olen = sizeof(struct ip6_hdr);
  916                 break;
  917 #endif
  918         default:
  919                 error = ENETDOWN;
  920                 goto drop;
  921         }
  922         gh = (struct grehdr *)mtodo(m, olen);
  923         switch (iaf) {
  924 #ifdef INET
  925         case AF_INET:
  926                 gh->gre_proto = htons(ETHERTYPE_IP);
  927                 break;
  928 #endif
  929 #ifdef INET6
  930         case AF_INET6:
  931                 gh->gre_proto = htons(ETHERTYPE_IPV6);
  932                 break;
  933 #endif
  934         default:
  935                 error = ENETDOWN;
  936                 goto drop;
  937         }
  938         if (want_seq)
  939                 gre_setseqn(gh, oseq);
  940         if (want_csum) {
  941                 *(uint16_t *)gh->gre_opts = in_cksum_skip(m,
  942                     m->m_pkthdr.len, olen);
  943         }
  944         plen = m->m_pkthdr.len - hlen;
  945         switch (oaf) {
  946 #ifdef INET
  947         case AF_INET:
  948                 error = in_gre_output(m, iaf, hlen);
  949                 break;
  950 #endif
  951 #ifdef INET6
  952         case AF_INET6:
  953                 error = in6_gre_output(m, iaf, hlen);
  954                 break;
  955 #endif
  956         default:
  957                 m_freem(m);
  958                 error = ENETDOWN;
  959         }
  960 drop:
  961         if (error)
  962                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
  963         else {
  964                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
  965                 if_inc_counter(ifp, IFCOUNTER_OBYTES, plen);
  966         }
  967         return (error);
  968 }
  969 
  970 static void
  971 gre_qflush(struct ifnet *ifp __unused)
  972 {
  973 
  974 }
  975 
  976 static int
  977 gremodevent(module_t mod, int type, void *data)
  978 {
  979 
  980         switch (type) {
  981         case MOD_LOAD:
  982         case MOD_UNLOAD:
  983                 break;
  984         default:
  985                 return (EOPNOTSUPP);
  986         }
  987         return (0);
  988 }
  989 
  990 static moduledata_t gre_mod = {
  991         "if_gre",
  992         gremodevent,
  993         0
  994 };
  995 
  996 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  997 MODULE_VERSION(if_gre, 1);

Cache object: cc31fff1702a52cb44a10a3a2e7a4f36


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