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 /*      $NetBSD: if_gre.c,v 1.49 2003/12/11 00:22:29 itojun Exp $ */
    2 /*       $FreeBSD$ */
    3 
    4 /*-
    5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
    6  * All rights reserved.
    7  *
    8  * This code is derived from software contributed to The NetBSD Foundation
    9  * by Heiko W.Rupp <hwr@pilhuhn.de>
   10  *
   11  * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  * 3. All advertising materials mentioning features or use of this software
   22  *    must display the following acknowledgement:
   23  *        This product includes software developed by the NetBSD
   24  *        Foundation, Inc. and its contributors.
   25  * 4. Neither the name of The NetBSD Foundation nor the names of its
   26  *    contributors may be used to endorse or promote products derived
   27  *    from this software without specific prior written permission.
   28  *
   29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   39  * POSSIBILITY OF SUCH DAMAGE.
   40  */
   41 
   42 /*
   43  * Encapsulate L3 protocols into IP
   44  * See RFC 2784 (successor of RFC 1701 and 1702) for more details.
   45  * If_gre is compatible with Cisco GRE tunnels, so you can
   46  * have a NetBSD box as the other end of a tunnel interface of a Cisco
   47  * router. See gre(4) for more details.
   48  * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
   49  */
   50 
   51 #include "opt_atalk.h"
   52 #include "opt_inet.h"
   53 #include "opt_inet6.h"
   54 
   55 #include <sys/param.h>
   56 #include <sys/kernel.h>
   57 #include <sys/malloc.h>
   58 #include <sys/module.h>
   59 #include <sys/mbuf.h>
   60 #include <sys/protosw.h>
   61 #include <sys/socket.h>
   62 #include <sys/sockio.h>
   63 #include <sys/sysctl.h>
   64 #include <sys/systm.h>
   65 
   66 #include <net/ethernet.h>
   67 #include <net/if.h>
   68 #include <net/if_clone.h>
   69 #include <net/if_types.h>
   70 #include <net/route.h>
   71 
   72 #ifdef INET
   73 #include <netinet/in.h>
   74 #include <netinet/in_systm.h>
   75 #include <netinet/in_var.h>
   76 #include <netinet/ip.h>
   77 #include <netinet/ip_gre.h>
   78 #include <netinet/ip_var.h>
   79 #include <netinet/ip_encap.h>
   80 #else
   81 #error "Huh? if_gre without inet?"
   82 #endif
   83 
   84 #include <net/bpf.h>
   85 
   86 #include <net/net_osdep.h>
   87 #include <net/if_gre.h>
   88 
   89 /*
   90  * It is not easy to calculate the right value for a GRE MTU.
   91  * We leave this task to the admin and use the same default that
   92  * other vendors use.
   93  */
   94 #define GREMTU  1476
   95 
   96 #define GRENAME "gre"
   97 
   98 /*
   99  * gre_mtx protects all global variables in if_gre.c.
  100  * XXX: gre_softc data not protected yet.
  101  */
  102 struct mtx gre_mtx;
  103 static MALLOC_DEFINE(M_GRE, GRENAME, "Generic Routing Encapsulation");
  104 
  105 struct gre_softc_head gre_softc_list;
  106 
  107 static int      gre_clone_create(struct if_clone *, int);
  108 static void     gre_clone_destroy(struct ifnet *);
  109 static int      gre_ioctl(struct ifnet *, u_long, caddr_t);
  110 static int      gre_output(struct ifnet *, struct mbuf *, struct sockaddr *,
  111                     struct rtentry *rt);
  112 
  113 IFC_SIMPLE_DECLARE(gre, 0);
  114 
  115 static int gre_compute_route(struct gre_softc *sc);
  116 
  117 static void     greattach(void);
  118 
  119 #ifdef INET
  120 extern struct domain inetdomain;
  121 static const struct protosw in_gre_protosw =
  122 { SOCK_RAW,     &inetdomain,    IPPROTO_GRE,    PR_ATOMIC|PR_ADDR,
  123   gre_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
  124   0,
  125   0,            0,              0,              0,
  126   &rip_usrreqs
  127 };
  128 static const struct protosw in_mobile_protosw =
  129 { SOCK_RAW,     &inetdomain,    IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR,
  130   gre_mobile_input, (pr_output_t*)rip_output, rip_ctlinput, rip_ctloutput,
  131   0,
  132   0,            0,              0,              0,
  133   &rip_usrreqs
  134 };
  135 #endif
  136 
  137 SYSCTL_DECL(_net_link);
  138 SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
  139     "Generic Routing Encapsulation");
  140 #ifndef MAX_GRE_NEST
  141 /*
  142  * This macro controls the default upper limitation on nesting of gre tunnels.
  143  * Since, setting a large value to this macro with a careless configuration
  144  * may introduce system crash, we don't allow any nestings by default.
  145  * If you need to configure nested gre tunnels, you can define this macro
  146  * in your kernel configuration file.  However, if you do so, please be
  147  * careful to configure the tunnels so that it won't make a loop.
  148  */
  149 #define MAX_GRE_NEST 1
  150 #endif
  151 static int max_gre_nesting = MAX_GRE_NEST;
  152 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
  153     &max_gre_nesting, 0, "Max nested tunnels");
  154 
  155 /* ARGSUSED */
  156 static void
  157 greattach(void)
  158 {
  159 
  160         mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
  161         LIST_INIT(&gre_softc_list);
  162         if_clone_attach(&gre_cloner);
  163 }
  164 
  165 static int
  166 gre_clone_create(ifc, unit)
  167         struct if_clone *ifc;
  168         int unit;
  169 {
  170         struct gre_softc *sc;
  171 
  172         sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
  173 
  174         if_initname(&sc->sc_if, ifc->ifc_name, unit);
  175         sc->sc_if.if_softc = sc;
  176         sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
  177         sc->sc_if.if_type = IFT_TUNNEL;
  178         sc->sc_if.if_addrlen = 0;
  179         sc->sc_if.if_hdrlen = 24; /* IP + GRE */
  180         sc->sc_if.if_mtu = GREMTU;
  181         sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
  182         sc->sc_if.if_output = gre_output;
  183         sc->sc_if.if_ioctl = gre_ioctl;
  184         sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
  185         sc->g_proto = IPPROTO_GRE;
  186         sc->sc_if.if_flags |= IFF_LINK0;
  187         sc->encap = NULL;
  188         sc->called = 0;
  189         sc->wccp_ver = WCCP_V1;
  190         if_attach(&sc->sc_if);
  191         bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
  192         mtx_lock(&gre_mtx);
  193         LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
  194         mtx_unlock(&gre_mtx);
  195         return (0);
  196 }
  197 
  198 static void
  199 gre_destroy(struct gre_softc *sc)
  200 {
  201 
  202 #ifdef INET
  203         if (sc->encap != NULL)
  204                 encap_detach(sc->encap);
  205 #endif
  206         bpfdetach(&sc->sc_if);
  207         if_detach(&sc->sc_if);
  208         free(sc, M_GRE);
  209 }
  210 
  211 static void
  212 gre_clone_destroy(ifp)
  213         struct ifnet *ifp;
  214 {
  215         struct gre_softc *sc = ifp->if_softc;
  216 
  217         mtx_lock(&gre_mtx);
  218         LIST_REMOVE(sc, sc_list);
  219         mtx_unlock(&gre_mtx);
  220         gre_destroy(sc);
  221 }
  222 
  223 /*
  224  * The output routine. Takes a packet and encapsulates it in the protocol
  225  * given by sc->g_proto. See also RFC 1701 and RFC 2004
  226  */
  227 static int
  228 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
  229            struct rtentry *rt)
  230 {
  231         int error = 0;
  232         struct gre_softc *sc = ifp->if_softc;
  233         struct greip *gh;
  234         struct ip *ip;
  235         u_short ip_id = 0;
  236         uint8_t ip_tos = 0;
  237         u_int16_t etype = 0;
  238         struct mobile_h mob_h;
  239 
  240         /*
  241          * gre may cause infinite recursion calls when misconfigured.
  242          * We'll prevent this by introducing upper limit.
  243          */
  244         if (++(sc->called) > max_gre_nesting) {
  245                 printf("%s: gre_output: recursively called too many "
  246                        "times(%d)\n", if_name(&sc->sc_if), sc->called);
  247                 m_freem(m);
  248                 error = EIO;    /* is there better errno? */
  249                 goto end;
  250         }
  251 
  252         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 0 ||
  253             sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
  254                 m_freem(m);
  255                 error = ENETDOWN;
  256                 goto end;
  257         }
  258 
  259         gh = NULL;
  260         ip = NULL;
  261 
  262         if (ifp->if_bpf) {
  263                 u_int32_t af = dst->sa_family;
  264                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
  265         }
  266 
  267         m->m_flags &= ~(M_BCAST|M_MCAST);
  268 
  269         if (sc->g_proto == IPPROTO_MOBILE) {
  270                 if (dst->sa_family == AF_INET) {
  271                         struct mbuf *m0;
  272                         int msiz;
  273 
  274                         ip = mtod(m, struct ip *);
  275 
  276                         /*
  277                          * RFC2004 specifies that fragmented diagrams shouldn't
  278                          * be encapsulated.
  279                          */
  280                         if ((ip->ip_off & IP_MF) != 0) {
  281                                 _IF_DROP(&ifp->if_snd);
  282                                 m_freem(m);
  283                                 error = EINVAL;    /* is there better errno? */
  284                                 goto end;
  285                         }
  286                         memset(&mob_h, 0, MOB_H_SIZ_L);
  287                         mob_h.proto = (ip->ip_p) << 8;
  288                         mob_h.odst = ip->ip_dst.s_addr;
  289                         ip->ip_dst.s_addr = sc->g_dst.s_addr;
  290 
  291                         /*
  292                          * If the packet comes from our host, we only change
  293                          * the destination address in the IP header.
  294                          * Else we also need to save and change the source
  295                          */
  296                         if (in_hosteq(ip->ip_src, sc->g_src)) {
  297                                 msiz = MOB_H_SIZ_S;
  298                         } else {
  299                                 mob_h.proto |= MOB_H_SBIT;
  300                                 mob_h.osrc = ip->ip_src.s_addr;
  301                                 ip->ip_src.s_addr = sc->g_src.s_addr;
  302                                 msiz = MOB_H_SIZ_L;
  303                         }
  304                         mob_h.proto = htons(mob_h.proto);
  305                         mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
  306 
  307                         if ((m->m_data - msiz) < m->m_pktdat) {
  308                                 /* need new mbuf */
  309                                 MGETHDR(m0, M_DONTWAIT, MT_HEADER);
  310                                 if (m0 == NULL) {
  311                                         _IF_DROP(&ifp->if_snd);
  312                                         m_freem(m);
  313                                         error = ENOBUFS;
  314                                         goto end;
  315                                 }
  316                                 m0->m_next = m;
  317                                 m->m_data += sizeof(struct ip);
  318                                 m->m_len -= sizeof(struct ip);
  319                                 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
  320                                 m0->m_len = msiz + sizeof(struct ip);
  321                                 m0->m_data += max_linkhdr;
  322                                 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
  323                                        sizeof(struct ip));
  324                                 m = m0;
  325                         } else {  /* we have some space left in the old one */
  326                                 m->m_data -= msiz;
  327                                 m->m_len += msiz;
  328                                 m->m_pkthdr.len += msiz;
  329                                 bcopy(ip, mtod(m, caddr_t),
  330                                         sizeof(struct ip));
  331                         }
  332                         ip = mtod(m, struct ip *);
  333                         memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
  334                         ip->ip_len = ntohs(ip->ip_len) + msiz;
  335                 } else {  /* AF_INET */
  336                         _IF_DROP(&ifp->if_snd);
  337                         m_freem(m);
  338                         error = EINVAL;
  339                         goto end;
  340                 }
  341         } else if (sc->g_proto == IPPROTO_GRE) {
  342                 switch (dst->sa_family) {
  343                 case AF_INET:
  344                         ip = mtod(m, struct ip *);
  345                         ip_tos = ip->ip_tos;
  346                         ip_id = ip->ip_id;
  347                         etype = ETHERTYPE_IP;
  348                         break;
  349 #ifdef INET6
  350                 case AF_INET6:
  351                         ip_id = ip_newid();
  352                         etype = ETHERTYPE_IPV6;
  353                         break;
  354 #endif
  355 #ifdef NETATALK
  356                 case AF_APPLETALK:
  357                         etype = ETHERTYPE_ATALK;
  358                         break;
  359 #endif
  360                 default:
  361                         _IF_DROP(&ifp->if_snd);
  362                         m_freem(m);
  363                         error = EAFNOSUPPORT;
  364                         goto end;
  365                 }
  366                 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
  367         } else {
  368                 _IF_DROP(&ifp->if_snd);
  369                 m_freem(m);
  370                 error = EINVAL;
  371                 goto end;
  372         }
  373 
  374         if (m == NULL) {        /* mbuf allocation failed */
  375                 _IF_DROP(&ifp->if_snd);
  376                 error = ENOBUFS;
  377                 goto end;
  378         }
  379 
  380         gh = mtod(m, struct greip *);
  381         if (sc->g_proto == IPPROTO_GRE) {
  382                 /* we don't have any GRE flags for now */
  383                 memset((void *)gh, 0, sizeof(struct greip));
  384                 gh->gi_ptype = htons(etype);
  385         }
  386 
  387         gh->gi_pr = sc->g_proto;
  388         if (sc->g_proto != IPPROTO_MOBILE) {
  389                 gh->gi_src = sc->g_src;
  390                 gh->gi_dst = sc->g_dst;
  391                 ((struct ip*)gh)->ip_v = IPPROTO_IPV4;
  392                 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
  393                 ((struct ip*)gh)->ip_ttl = GRE_TTL;
  394                 ((struct ip*)gh)->ip_tos = ip_tos;
  395                 ((struct ip*)gh)->ip_id = ip_id;
  396                 gh->gi_len = m->m_pkthdr.len;
  397         }
  398 
  399         ifp->if_opackets++;
  400         ifp->if_obytes += m->m_pkthdr.len;
  401         /*
  402          * Send it off and with IP_FORWARD flag to prevent it from
  403          * overwriting the ip_id again.  ip_id is already set to the
  404          * ip_id of the encapsulated packet.
  405          */
  406         error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
  407             (struct ip_moptions *)NULL, (struct inpcb *)NULL);
  408   end:
  409         sc->called = 0;
  410         if (error)
  411                 ifp->if_oerrors++;
  412         return (error);
  413 }
  414 
  415 static int
  416 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  417 {
  418         struct ifreq *ifr = (struct ifreq *)data;
  419         struct if_laddrreq *lifr = (struct if_laddrreq *)data;
  420         struct in_aliasreq *aifr = (struct in_aliasreq *)data;
  421         struct gre_softc *sc = ifp->if_softc;
  422         int s;
  423         struct sockaddr_in si;
  424         struct sockaddr *sa = NULL;
  425         int error;
  426         struct sockaddr_in sp, sm, dp, dm;
  427 
  428         error = 0;
  429 
  430         s = splnet();
  431         switch (cmd) {
  432         case SIOCSIFADDR:
  433                 ifp->if_flags |= IFF_UP;
  434                 break;
  435         case SIOCSIFDSTADDR:
  436                 break;
  437         case SIOCSIFFLAGS:
  438                 if ((error = suser(curthread)) != 0)
  439                         break;
  440                 if ((ifr->ifr_flags & IFF_LINK0) != 0)
  441                         sc->g_proto = IPPROTO_GRE;
  442                 else
  443                         sc->g_proto = IPPROTO_MOBILE;
  444                 if ((ifr->ifr_flags & IFF_LINK2) != 0)
  445                         sc->wccp_ver = WCCP_V2;
  446                 else
  447                         sc->wccp_ver = WCCP_V1;
  448                 goto recompute;
  449         case SIOCSIFMTU:
  450                 if ((error = suser(curthread)) != 0)
  451                         break;
  452                 if (ifr->ifr_mtu < 576) {
  453                         error = EINVAL;
  454                         break;
  455                 }
  456                 ifp->if_mtu = ifr->ifr_mtu;
  457                 break;
  458         case SIOCGIFMTU:
  459                 ifr->ifr_mtu = sc->sc_if.if_mtu;
  460                 break;
  461         case SIOCADDMULTI:
  462         case SIOCDELMULTI:
  463                 if ((error = suser(curthread)) != 0)
  464                         break;
  465                 if (ifr == 0) {
  466                         error = EAFNOSUPPORT;
  467                         break;
  468                 }
  469                 switch (ifr->ifr_addr.sa_family) {
  470 #ifdef INET
  471                 case AF_INET:
  472                         break;
  473 #endif
  474 #ifdef INET6
  475                 case AF_INET6:
  476                         break;
  477 #endif
  478                 default:
  479                         error = EAFNOSUPPORT;
  480                         break;
  481                 }
  482                 break;
  483         case GRESPROTO:
  484                 if ((error = suser(curthread)) != 0)
  485                         break;
  486                 sc->g_proto = ifr->ifr_flags;
  487                 switch (sc->g_proto) {
  488                 case IPPROTO_GRE:
  489                         ifp->if_flags |= IFF_LINK0;
  490                         break;
  491                 case IPPROTO_MOBILE:
  492                         ifp->if_flags &= ~IFF_LINK0;
  493                         break;
  494                 default:
  495                         error = EPROTONOSUPPORT;
  496                         break;
  497                 }
  498                 goto recompute;
  499         case GREGPROTO:
  500                 ifr->ifr_flags = sc->g_proto;
  501                 break;
  502         case GRESADDRS:
  503         case GRESADDRD:
  504                 if ((error = suser(curthread)) != 0)
  505                         break;
  506                 /*
  507                  * set tunnel endpoints, compute a less specific route
  508                  * to the remote end and mark if as up
  509                  */
  510                 sa = &ifr->ifr_addr;
  511                 if (cmd == GRESADDRS)
  512                         sc->g_src = (satosin(sa))->sin_addr;
  513                 if (cmd == GRESADDRD)
  514                         sc->g_dst = (satosin(sa))->sin_addr;
  515         recompute:
  516 #ifdef INET
  517                 if (sc->encap != NULL) {
  518                         encap_detach(sc->encap);
  519                         sc->encap = NULL;
  520                 }
  521 #endif
  522                 if ((sc->g_src.s_addr != INADDR_ANY) &&
  523                     (sc->g_dst.s_addr != INADDR_ANY)) {
  524                         bzero(&sp, sizeof(sp));
  525                         bzero(&sm, sizeof(sm));
  526                         bzero(&dp, sizeof(dp));
  527                         bzero(&dm, sizeof(dm));
  528                         sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
  529                             sizeof(struct sockaddr_in);
  530                         sp.sin_family = sm.sin_family = dp.sin_family =
  531                             dm.sin_family = AF_INET;
  532                         sp.sin_addr = sc->g_src;
  533                         dp.sin_addr = sc->g_dst;
  534                         sm.sin_addr.s_addr = dm.sin_addr.s_addr =
  535                             INADDR_BROADCAST;
  536 #ifdef INET
  537                         sc->encap = encap_attach(AF_INET, sc->g_proto,
  538                             sintosa(&sp), sintosa(&sm), sintosa(&dp),
  539                             sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
  540                                 &in_gre_protosw : &in_mobile_protosw, sc);
  541                         if (sc->encap == NULL)
  542                                 printf("%s: unable to attach encap\n",
  543                                     if_name(&sc->sc_if));
  544 #endif
  545                         if (sc->route.ro_rt != 0) /* free old route */
  546                                 RTFREE(sc->route.ro_rt);
  547                         if (gre_compute_route(sc) == 0)
  548                                 ifp->if_flags |= IFF_RUNNING;
  549                         else
  550                                 ifp->if_flags &= ~IFF_RUNNING;
  551                 }
  552                 break;
  553         case GREGADDRS:
  554                 memset(&si, 0, sizeof(si));
  555                 si.sin_family = AF_INET;
  556                 si.sin_len = sizeof(struct sockaddr_in);
  557                 si.sin_addr.s_addr = sc->g_src.s_addr;
  558                 sa = sintosa(&si);
  559                 ifr->ifr_addr = *sa;
  560                 break;
  561         case GREGADDRD:
  562                 memset(&si, 0, sizeof(si));
  563                 si.sin_family = AF_INET;
  564                 si.sin_len = sizeof(struct sockaddr_in);
  565                 si.sin_addr.s_addr = sc->g_dst.s_addr;
  566                 sa = sintosa(&si);
  567                 ifr->ifr_addr = *sa;
  568                 break;
  569         case SIOCSIFPHYADDR:
  570                 if ((error = suser(curthread)) != 0)
  571                         break;
  572                 if (aifr->ifra_addr.sin_family != AF_INET ||
  573                     aifr->ifra_dstaddr.sin_family != AF_INET) {
  574                         error = EAFNOSUPPORT;
  575                         break;
  576                 }
  577                 if (aifr->ifra_addr.sin_len != sizeof(si) ||
  578                     aifr->ifra_dstaddr.sin_len != sizeof(si)) {
  579                         error = EINVAL;
  580                         break;
  581                 }
  582                 sc->g_src = aifr->ifra_addr.sin_addr;
  583                 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
  584                 goto recompute;
  585         case SIOCSLIFPHYADDR:
  586                 if ((error = suser(curthread)) != 0)
  587                         break;
  588                 if (lifr->addr.ss_family != AF_INET ||
  589                     lifr->dstaddr.ss_family != AF_INET) {
  590                         error = EAFNOSUPPORT;
  591                         break;
  592                 }
  593                 if (lifr->addr.ss_len != sizeof(si) ||
  594                     lifr->dstaddr.ss_len != sizeof(si)) {
  595                         error = EINVAL;
  596                         break;
  597                 }
  598                 sc->g_src = (satosin(&lifr->addr))->sin_addr;
  599                 sc->g_dst =
  600                     (satosin(&lifr->dstaddr))->sin_addr;
  601                 goto recompute;
  602         case SIOCDIFPHYADDR:
  603                 if ((error = suser(curthread)) != 0)
  604                         break;
  605                 sc->g_src.s_addr = INADDR_ANY;
  606                 sc->g_dst.s_addr = INADDR_ANY;
  607                 goto recompute;
  608         case SIOCGLIFPHYADDR:
  609                 if (sc->g_src.s_addr == INADDR_ANY ||
  610                     sc->g_dst.s_addr == INADDR_ANY) {
  611                         error = EADDRNOTAVAIL;
  612                         break;
  613                 }
  614                 memset(&si, 0, sizeof(si));
  615                 si.sin_family = AF_INET;
  616                 si.sin_len = sizeof(struct sockaddr_in);
  617                 si.sin_addr.s_addr = sc->g_src.s_addr;
  618                 memcpy(&lifr->addr, &si, sizeof(si));
  619                 si.sin_addr.s_addr = sc->g_dst.s_addr;
  620                 memcpy(&lifr->dstaddr, &si, sizeof(si));
  621                 break;
  622         case SIOCGIFPSRCADDR:
  623 #ifdef INET6
  624         case SIOCGIFPSRCADDR_IN6:
  625 #endif
  626                 if (sc->g_src.s_addr == INADDR_ANY) {
  627                         error = EADDRNOTAVAIL;
  628                         break;
  629                 }
  630                 memset(&si, 0, sizeof(si));
  631                 si.sin_family = AF_INET;
  632                 si.sin_len = sizeof(struct sockaddr_in);
  633                 si.sin_addr.s_addr = sc->g_src.s_addr;
  634                 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
  635                 break;
  636         case SIOCGIFPDSTADDR:
  637 #ifdef INET6
  638         case SIOCGIFPDSTADDR_IN6:
  639 #endif
  640                 if (sc->g_dst.s_addr == INADDR_ANY) {
  641                         error = EADDRNOTAVAIL;
  642                         break;
  643                 }
  644                 memset(&si, 0, sizeof(si));
  645                 si.sin_family = AF_INET;
  646                 si.sin_len = sizeof(struct sockaddr_in);
  647                 si.sin_addr.s_addr = sc->g_dst.s_addr;
  648                 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
  649                 break;
  650         default:
  651                 error = EINVAL;
  652                 break;
  653         }
  654 
  655         splx(s);
  656         return (error);
  657 }
  658 
  659 /*
  660  * computes a route to our destination that is not the one
  661  * which would be taken by ip_output(), as this one will loop back to
  662  * us. If the interface is p2p as  a--->b, then a routing entry exists
  663  * If we now send a packet to b (e.g. ping b), this will come down here
  664  * gets src=a, dst=b tacked on and would from ip_output() sent back to
  665  * if_gre.
  666  * Goal here is to compute a route to b that is less specific than
  667  * a-->b. We know that this one exists as in normal operation we have
  668  * at least a default route which matches.
  669  */
  670 static int
  671 gre_compute_route(struct gre_softc *sc)
  672 {
  673         struct route *ro;
  674         u_int32_t a, b, c;
  675 
  676         ro = &sc->route;
  677 
  678         memset(ro, 0, sizeof(struct route));
  679         ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
  680         ro->ro_dst.sa_family = AF_INET;
  681         ro->ro_dst.sa_len = sizeof(ro->ro_dst);
  682 
  683         /*
  684          * toggle last bit, so our interface is not found, but a less
  685          * specific route. I'd rather like to specify a shorter mask,
  686          * but this is not possible. Should work though. XXX
  687          * there is a simpler way ...
  688          */
  689         if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
  690                 a = ntohl(sc->g_dst.s_addr);
  691                 b = a & 0x01;
  692                 c = a & 0xfffffffe;
  693                 b = b ^ 0x01;
  694                 a = b | c;
  695                 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
  696                     = htonl(a);
  697         }
  698 
  699 #ifdef DIAGNOSTIC
  700         printf("%s: searching for a route to %s", if_name(&sc->sc_if),
  701             inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
  702 #endif
  703 
  704         rtalloc(ro);
  705 
  706         /*
  707          * check if this returned a route at all and this route is no
  708          * recursion to ourself
  709          */
  710         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
  711 #ifdef DIAGNOSTIC
  712                 if (ro->ro_rt == NULL)
  713                         printf(" - no route found!\n");
  714                 else
  715                         printf(" - route loops back to ourself!\n");
  716 #endif
  717                 return EADDRNOTAVAIL;
  718         }
  719 
  720         /*
  721          * now change it back - else ip_output will just drop
  722          * the route and search one to this interface ...
  723          */
  724         if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
  725                 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
  726 
  727 #ifdef DIAGNOSTIC
  728         printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
  729             inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
  730         printf("\n");
  731 #endif
  732 
  733         return 0;
  734 }
  735 
  736 /*
  737  * do a checksum of a buffer - much like in_cksum, which operates on
  738  * mbufs.
  739  */
  740 u_int16_t
  741 gre_in_cksum(u_int16_t *p, u_int len)
  742 {
  743         u_int32_t sum = 0;
  744         int nwords = len >> 1;
  745 
  746         while (nwords-- != 0)
  747                 sum += *p++;
  748 
  749         if (len & 1) {
  750                 union {
  751                         u_short w;
  752                         u_char c[2];
  753                 } u;
  754                 u.c[0] = *(u_char *)p;
  755                 u.c[1] = 0;
  756                 sum += u.w;
  757         }
  758 
  759         /* end-around-carry */
  760         sum = (sum >> 16) + (sum & 0xffff);
  761         sum += (sum >> 16);
  762         return (~sum);
  763 }
  764 
  765 static int
  766 gremodevent(module_t mod, int type, void *data)
  767 {
  768         struct gre_softc *sc;
  769 
  770         switch (type) {
  771         case MOD_LOAD:
  772                 greattach();
  773                 break;
  774         case MOD_UNLOAD:
  775                 if_clone_detach(&gre_cloner);
  776 
  777                 mtx_lock(&gre_mtx);
  778                 while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
  779                         LIST_REMOVE(sc, sc_list);
  780                         mtx_unlock(&gre_mtx);
  781                         gre_destroy(sc);
  782                         mtx_lock(&gre_mtx);
  783                 }
  784                 mtx_unlock(&gre_mtx);
  785                 mtx_destroy(&gre_mtx);
  786                 break;
  787         default:
  788                 return EOPNOTSUPP;
  789         }
  790         return 0;
  791 }
  792 
  793 static moduledata_t gre_mod = {
  794         "if_gre",
  795         gremodevent,
  796         0
  797 };
  798 
  799 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  800 MODULE_VERSION(if_gre, 1);

Cache object: b81021ec7a5e4e108223f3379dc6470b


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