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         .pr_type =              SOCK_RAW,
  123         .pr_domain =            &inetdomain,
  124         .pr_protocol =          IPPROTO_GRE,
  125         .pr_flags =             PR_ATOMIC|PR_ADDR,
  126         .pr_input =             gre_input,
  127         .pr_output =            (pr_output_t *)rip_output,
  128         .pr_ctlinput =          rip_ctlinput,
  129         .pr_ctloutput =         rip_ctloutput,
  130         .pr_usrreqs =           &rip_usrreqs
  131 };
  132 static const struct protosw in_mobile_protosw = {
  133         .pr_type =              SOCK_RAW,
  134         .pr_domain =            &inetdomain,
  135         .pr_protocol =          IPPROTO_MOBILE,
  136         .pr_flags =             PR_ATOMIC|PR_ADDR,
  137         .pr_input =             gre_mobile_input,
  138         .pr_output =            (pr_output_t *)rip_output,
  139         .pr_ctlinput =          rip_ctlinput,
  140         .pr_ctloutput =         rip_ctloutput,
  141         .pr_usrreqs =           &rip_usrreqs
  142 };
  143 #endif
  144 
  145 SYSCTL_DECL(_net_link);
  146 SYSCTL_NODE(_net_link, IFT_TUNNEL, gre, CTLFLAG_RW, 0,
  147     "Generic Routing Encapsulation");
  148 #ifndef MAX_GRE_NEST
  149 /*
  150  * This macro controls the default upper limitation on nesting of gre tunnels.
  151  * Since, setting a large value to this macro with a careless configuration
  152  * may introduce system crash, we don't allow any nestings by default.
  153  * If you need to configure nested gre tunnels, you can define this macro
  154  * in your kernel configuration file.  However, if you do so, please be
  155  * careful to configure the tunnels so that it won't make a loop.
  156  */
  157 #define MAX_GRE_NEST 1
  158 #endif
  159 static int max_gre_nesting = MAX_GRE_NEST;
  160 SYSCTL_INT(_net_link_gre, OID_AUTO, max_nesting, CTLFLAG_RW,
  161     &max_gre_nesting, 0, "Max nested tunnels");
  162 
  163 /* ARGSUSED */
  164 static void
  165 greattach(void)
  166 {
  167 
  168         mtx_init(&gre_mtx, "gre_mtx", NULL, MTX_DEF);
  169         LIST_INIT(&gre_softc_list);
  170         if_clone_attach(&gre_cloner);
  171 }
  172 
  173 static int
  174 gre_clone_create(ifc, unit)
  175         struct if_clone *ifc;
  176         int unit;
  177 {
  178         struct gre_softc *sc;
  179 
  180         sc = malloc(sizeof(struct gre_softc), M_GRE, M_WAITOK | M_ZERO);
  181 
  182         GRE2IFP(sc) = if_alloc(IFT_TUNNEL);
  183         if (GRE2IFP(sc) == NULL) {
  184                 free(sc, M_GRE);
  185                 return (ENOSPC);
  186         }
  187 
  188         GRE2IFP(sc)->if_softc = sc;
  189         if_initname(GRE2IFP(sc), ifc->ifc_name, unit);
  190 
  191         GRE2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
  192         GRE2IFP(sc)->if_addrlen = 0;
  193         GRE2IFP(sc)->if_hdrlen = 24; /* IP + GRE */
  194         GRE2IFP(sc)->if_mtu = GREMTU;
  195         GRE2IFP(sc)->if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
  196         GRE2IFP(sc)->if_output = gre_output;
  197         GRE2IFP(sc)->if_ioctl = gre_ioctl;
  198         sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
  199         sc->g_proto = IPPROTO_GRE;
  200         GRE2IFP(sc)->if_flags |= IFF_LINK0;
  201         sc->encap = NULL;
  202         sc->called = 0;
  203         sc->wccp_ver = WCCP_V1;
  204         if_attach(GRE2IFP(sc));
  205         bpfattach(GRE2IFP(sc), DLT_NULL, sizeof(u_int32_t));
  206         mtx_lock(&gre_mtx);
  207         LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
  208         mtx_unlock(&gre_mtx);
  209         return (0);
  210 }
  211 
  212 static void
  213 gre_destroy(struct gre_softc *sc)
  214 {
  215 
  216 #ifdef INET
  217         if (sc->encap != NULL)
  218                 encap_detach(sc->encap);
  219 #endif
  220         bpfdetach(GRE2IFP(sc));
  221         if_detach(GRE2IFP(sc));
  222         if_free(GRE2IFP(sc));
  223         free(sc, M_GRE);
  224 }
  225 
  226 static void
  227 gre_clone_destroy(ifp)
  228         struct ifnet *ifp;
  229 {
  230         struct gre_softc *sc = ifp->if_softc;
  231 
  232         mtx_lock(&gre_mtx);
  233         LIST_REMOVE(sc, sc_list);
  234         mtx_unlock(&gre_mtx);
  235         gre_destroy(sc);
  236 }
  237 
  238 /*
  239  * The output routine. Takes a packet and encapsulates it in the protocol
  240  * given by sc->g_proto. See also RFC 1701 and RFC 2004
  241  */
  242 static int
  243 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
  244            struct rtentry *rt)
  245 {
  246         int error = 0;
  247         struct gre_softc *sc = ifp->if_softc;
  248         struct greip *gh;
  249         struct ip *ip;
  250         u_short ip_id = 0;
  251         uint8_t ip_tos = 0;
  252         u_int16_t etype = 0;
  253         struct mobile_h mob_h;
  254         u_int32_t af;
  255 
  256         /*
  257          * gre may cause infinite recursion calls when misconfigured.
  258          * We'll prevent this by introducing upper limit.
  259          */
  260         if (++(sc->called) > max_gre_nesting) {
  261                 printf("%s: gre_output: recursively called too many "
  262                        "times(%d)\n", if_name(GRE2IFP(sc)), sc->called);
  263                 m_freem(m);
  264                 error = EIO;    /* is there better errno? */
  265                 goto end;
  266         }
  267 
  268         if (!((ifp->if_flags & IFF_UP) &&
  269             (ifp->if_drv_flags & IFF_DRV_RUNNING)) ||
  270             sc->g_src.s_addr == INADDR_ANY || sc->g_dst.s_addr == INADDR_ANY) {
  271                 m_freem(m);
  272                 error = ENETDOWN;
  273                 goto end;
  274         }
  275 
  276         gh = NULL;
  277         ip = NULL;
  278 
  279         /* BPF writes need to be handled specially. */
  280         if (dst->sa_family == AF_UNSPEC) {
  281                 bcopy(dst->sa_data, &af, sizeof(af));
  282                 dst->sa_family = af;
  283         }
  284 
  285         if (bpf_peers_present(ifp->if_bpf)) {
  286                 af = dst->sa_family;
  287                 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
  288         }
  289 
  290         m->m_flags &= ~(M_BCAST|M_MCAST);
  291 
  292         if (sc->g_proto == IPPROTO_MOBILE) {
  293                 if (dst->sa_family == AF_INET) {
  294                         struct mbuf *m0;
  295                         int msiz;
  296 
  297                         ip = mtod(m, struct ip *);
  298 
  299                         /*
  300                          * RFC2004 specifies that fragmented diagrams shouldn't
  301                          * be encapsulated.
  302                          */
  303                         if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
  304                                 _IF_DROP(&ifp->if_snd);
  305                                 m_freem(m);
  306                                 error = EINVAL;    /* is there better errno? */
  307                                 goto end;
  308                         }
  309                         memset(&mob_h, 0, MOB_H_SIZ_L);
  310                         mob_h.proto = (ip->ip_p) << 8;
  311                         mob_h.odst = ip->ip_dst.s_addr;
  312                         ip->ip_dst.s_addr = sc->g_dst.s_addr;
  313 
  314                         /*
  315                          * If the packet comes from our host, we only change
  316                          * the destination address in the IP header.
  317                          * Else we also need to save and change the source
  318                          */
  319                         if (in_hosteq(ip->ip_src, sc->g_src)) {
  320                                 msiz = MOB_H_SIZ_S;
  321                         } else {
  322                                 mob_h.proto |= MOB_H_SBIT;
  323                                 mob_h.osrc = ip->ip_src.s_addr;
  324                                 ip->ip_src.s_addr = sc->g_src.s_addr;
  325                                 msiz = MOB_H_SIZ_L;
  326                         }
  327                         mob_h.proto = htons(mob_h.proto);
  328                         mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
  329 
  330                         if ((m->m_data - msiz) < m->m_pktdat) {
  331                                 /* need new mbuf */
  332                                 MGETHDR(m0, M_DONTWAIT, MT_HEADER);
  333                                 if (m0 == NULL) {
  334                                         _IF_DROP(&ifp->if_snd);
  335                                         m_freem(m);
  336                                         error = ENOBUFS;
  337                                         goto end;
  338                                 }
  339                                 m0->m_next = m;
  340                                 m->m_data += sizeof(struct ip);
  341                                 m->m_len -= sizeof(struct ip);
  342                                 m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
  343                                 m0->m_len = msiz + sizeof(struct ip);
  344                                 m0->m_data += max_linkhdr;
  345                                 memcpy(mtod(m0, caddr_t), (caddr_t)ip,
  346                                        sizeof(struct ip));
  347                                 m = m0;
  348                         } else {  /* we have some space left in the old one */
  349                                 m->m_data -= msiz;
  350                                 m->m_len += msiz;
  351                                 m->m_pkthdr.len += msiz;
  352                                 bcopy(ip, mtod(m, caddr_t),
  353                                         sizeof(struct ip));
  354                         }
  355                         ip = mtod(m, struct ip *);
  356                         memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
  357                         ip->ip_len = ntohs(ip->ip_len) + msiz;
  358                 } else {  /* AF_INET */
  359                         _IF_DROP(&ifp->if_snd);
  360                         m_freem(m);
  361                         error = EINVAL;
  362                         goto end;
  363                 }
  364         } else if (sc->g_proto == IPPROTO_GRE) {
  365                 switch (dst->sa_family) {
  366                 case AF_INET:
  367                         ip = mtod(m, struct ip *);
  368                         ip_tos = ip->ip_tos;
  369                         ip_id = ip->ip_id;
  370                         etype = ETHERTYPE_IP;
  371                         break;
  372 #ifdef INET6
  373                 case AF_INET6:
  374                         ip_id = ip_newid();
  375                         etype = ETHERTYPE_IPV6;
  376                         break;
  377 #endif
  378 #ifdef NETATALK
  379                 case AF_APPLETALK:
  380                         etype = ETHERTYPE_ATALK;
  381                         break;
  382 #endif
  383                 default:
  384                         _IF_DROP(&ifp->if_snd);
  385                         m_freem(m);
  386                         error = EAFNOSUPPORT;
  387                         goto end;
  388                 }
  389                 M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
  390         } else {
  391                 _IF_DROP(&ifp->if_snd);
  392                 m_freem(m);
  393                 error = EINVAL;
  394                 goto end;
  395         }
  396 
  397         if (m == NULL) {        /* mbuf allocation failed */
  398                 _IF_DROP(&ifp->if_snd);
  399                 error = ENOBUFS;
  400                 goto end;
  401         }
  402 
  403         gh = mtod(m, struct greip *);
  404         if (sc->g_proto == IPPROTO_GRE) {
  405                 /* we don't have any GRE flags for now */
  406                 memset((void *)gh, 0, sizeof(struct greip));
  407                 gh->gi_ptype = htons(etype);
  408         }
  409 
  410         gh->gi_pr = sc->g_proto;
  411         if (sc->g_proto != IPPROTO_MOBILE) {
  412                 gh->gi_src = sc->g_src;
  413                 gh->gi_dst = sc->g_dst;
  414                 ((struct ip*)gh)->ip_v = IPPROTO_IPV4;
  415                 ((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
  416                 ((struct ip*)gh)->ip_ttl = GRE_TTL;
  417                 ((struct ip*)gh)->ip_tos = ip_tos;
  418                 ((struct ip*)gh)->ip_id = ip_id;
  419                 gh->gi_len = m->m_pkthdr.len;
  420         }
  421 
  422         ifp->if_opackets++;
  423         ifp->if_obytes += m->m_pkthdr.len;
  424         /*
  425          * Send it off and with IP_FORWARD flag to prevent it from
  426          * overwriting the ip_id again.  ip_id is already set to the
  427          * ip_id of the encapsulated packet.
  428          */
  429         error = ip_output(m, NULL, &sc->route, IP_FORWARDING,
  430             (struct ip_moptions *)NULL, (struct inpcb *)NULL);
  431   end:
  432         sc->called = 0;
  433         if (error)
  434                 ifp->if_oerrors++;
  435         return (error);
  436 }
  437 
  438 static int
  439 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  440 {
  441         struct ifreq *ifr = (struct ifreq *)data;
  442         struct if_laddrreq *lifr = (struct if_laddrreq *)data;
  443         struct in_aliasreq *aifr = (struct in_aliasreq *)data;
  444         struct gre_softc *sc = ifp->if_softc;
  445         int s;
  446         struct sockaddr_in si;
  447         struct sockaddr *sa = NULL;
  448         int error;
  449         struct sockaddr_in sp, sm, dp, dm;
  450 
  451         error = 0;
  452 
  453         s = splnet();
  454         switch (cmd) {
  455         case SIOCSIFADDR:
  456                 ifp->if_flags |= IFF_UP;
  457                 break;
  458         case SIOCSIFDSTADDR:
  459                 break;
  460         case SIOCSIFFLAGS:
  461                 if ((error = suser(curthread)) != 0)
  462                         break;
  463                 if ((ifr->ifr_flags & IFF_LINK0) != 0)
  464                         sc->g_proto = IPPROTO_GRE;
  465                 else
  466                         sc->g_proto = IPPROTO_MOBILE;
  467                 if ((ifr->ifr_flags & IFF_LINK2) != 0)
  468                         sc->wccp_ver = WCCP_V2;
  469                 else
  470                         sc->wccp_ver = WCCP_V1;
  471                 goto recompute;
  472         case SIOCSIFMTU:
  473                 if ((error = suser(curthread)) != 0)
  474                         break;
  475                 if (ifr->ifr_mtu < 576) {
  476                         error = EINVAL;
  477                         break;
  478                 }
  479                 ifp->if_mtu = ifr->ifr_mtu;
  480                 break;
  481         case SIOCGIFMTU:
  482                 ifr->ifr_mtu = GRE2IFP(sc)->if_mtu;
  483                 break;
  484         case SIOCADDMULTI:
  485         case SIOCDELMULTI:
  486                 if ((error = suser(curthread)) != 0)
  487                         break;
  488                 if (ifr == 0) {
  489                         error = EAFNOSUPPORT;
  490                         break;
  491                 }
  492                 switch (ifr->ifr_addr.sa_family) {
  493 #ifdef INET
  494                 case AF_INET:
  495                         break;
  496 #endif
  497 #ifdef INET6
  498                 case AF_INET6:
  499                         break;
  500 #endif
  501                 default:
  502                         error = EAFNOSUPPORT;
  503                         break;
  504                 }
  505                 break;
  506         case GRESPROTO:
  507                 if ((error = suser(curthread)) != 0)
  508                         break;
  509                 sc->g_proto = ifr->ifr_flags;
  510                 switch (sc->g_proto) {
  511                 case IPPROTO_GRE:
  512                         ifp->if_flags |= IFF_LINK0;
  513                         break;
  514                 case IPPROTO_MOBILE:
  515                         ifp->if_flags &= ~IFF_LINK0;
  516                         break;
  517                 default:
  518                         error = EPROTONOSUPPORT;
  519                         break;
  520                 }
  521                 goto recompute;
  522         case GREGPROTO:
  523                 ifr->ifr_flags = sc->g_proto;
  524                 break;
  525         case GRESADDRS:
  526         case GRESADDRD:
  527                 if ((error = suser(curthread)) != 0)
  528                         break;
  529                 /*
  530                  * set tunnel endpoints, compute a less specific route
  531                  * to the remote end and mark if as up
  532                  */
  533                 sa = &ifr->ifr_addr;
  534                 if (cmd == GRESADDRS)
  535                         sc->g_src = (satosin(sa))->sin_addr;
  536                 if (cmd == GRESADDRD)
  537                         sc->g_dst = (satosin(sa))->sin_addr;
  538         recompute:
  539 #ifdef INET
  540                 if (sc->encap != NULL) {
  541                         encap_detach(sc->encap);
  542                         sc->encap = NULL;
  543                 }
  544 #endif
  545                 if ((sc->g_src.s_addr != INADDR_ANY) &&
  546                     (sc->g_dst.s_addr != INADDR_ANY)) {
  547                         bzero(&sp, sizeof(sp));
  548                         bzero(&sm, sizeof(sm));
  549                         bzero(&dp, sizeof(dp));
  550                         bzero(&dm, sizeof(dm));
  551                         sp.sin_len = sm.sin_len = dp.sin_len = dm.sin_len =
  552                             sizeof(struct sockaddr_in);
  553                         sp.sin_family = sm.sin_family = dp.sin_family =
  554                             dm.sin_family = AF_INET;
  555                         sp.sin_addr = sc->g_src;
  556                         dp.sin_addr = sc->g_dst;
  557                         sm.sin_addr.s_addr = dm.sin_addr.s_addr =
  558                             INADDR_BROADCAST;
  559 #ifdef INET
  560                         sc->encap = encap_attach(AF_INET, sc->g_proto,
  561                             sintosa(&sp), sintosa(&sm), sintosa(&dp),
  562                             sintosa(&dm), (sc->g_proto == IPPROTO_GRE) ?
  563                                 &in_gre_protosw : &in_mobile_protosw, sc);
  564                         if (sc->encap == NULL)
  565                                 printf("%s: unable to attach encap\n",
  566                                     if_name(GRE2IFP(sc)));
  567 #endif
  568                         if (sc->route.ro_rt != 0) /* free old route */
  569                                 RTFREE(sc->route.ro_rt);
  570                         if (gre_compute_route(sc) == 0)
  571                                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
  572                         else
  573                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  574                 }
  575                 break;
  576         case GREGADDRS:
  577                 memset(&si, 0, sizeof(si));
  578                 si.sin_family = AF_INET;
  579                 si.sin_len = sizeof(struct sockaddr_in);
  580                 si.sin_addr.s_addr = sc->g_src.s_addr;
  581                 sa = sintosa(&si);
  582                 ifr->ifr_addr = *sa;
  583                 break;
  584         case GREGADDRD:
  585                 memset(&si, 0, sizeof(si));
  586                 si.sin_family = AF_INET;
  587                 si.sin_len = sizeof(struct sockaddr_in);
  588                 si.sin_addr.s_addr = sc->g_dst.s_addr;
  589                 sa = sintosa(&si);
  590                 ifr->ifr_addr = *sa;
  591                 break;
  592         case SIOCSIFPHYADDR:
  593                 if ((error = suser(curthread)) != 0)
  594                         break;
  595                 if (aifr->ifra_addr.sin_family != AF_INET ||
  596                     aifr->ifra_dstaddr.sin_family != AF_INET) {
  597                         error = EAFNOSUPPORT;
  598                         break;
  599                 }
  600                 if (aifr->ifra_addr.sin_len != sizeof(si) ||
  601                     aifr->ifra_dstaddr.sin_len != sizeof(si)) {
  602                         error = EINVAL;
  603                         break;
  604                 }
  605                 sc->g_src = aifr->ifra_addr.sin_addr;
  606                 sc->g_dst = aifr->ifra_dstaddr.sin_addr;
  607                 goto recompute;
  608         case SIOCSLIFPHYADDR:
  609                 if ((error = suser(curthread)) != 0)
  610                         break;
  611                 if (lifr->addr.ss_family != AF_INET ||
  612                     lifr->dstaddr.ss_family != AF_INET) {
  613                         error = EAFNOSUPPORT;
  614                         break;
  615                 }
  616                 if (lifr->addr.ss_len != sizeof(si) ||
  617                     lifr->dstaddr.ss_len != sizeof(si)) {
  618                         error = EINVAL;
  619                         break;
  620                 }
  621                 sc->g_src = (satosin(&lifr->addr))->sin_addr;
  622                 sc->g_dst =
  623                     (satosin(&lifr->dstaddr))->sin_addr;
  624                 goto recompute;
  625         case SIOCDIFPHYADDR:
  626                 if ((error = suser(curthread)) != 0)
  627                         break;
  628                 sc->g_src.s_addr = INADDR_ANY;
  629                 sc->g_dst.s_addr = INADDR_ANY;
  630                 goto recompute;
  631         case SIOCGLIFPHYADDR:
  632                 if (sc->g_src.s_addr == INADDR_ANY ||
  633                     sc->g_dst.s_addr == INADDR_ANY) {
  634                         error = EADDRNOTAVAIL;
  635                         break;
  636                 }
  637                 memset(&si, 0, sizeof(si));
  638                 si.sin_family = AF_INET;
  639                 si.sin_len = sizeof(struct sockaddr_in);
  640                 si.sin_addr.s_addr = sc->g_src.s_addr;
  641                 memcpy(&lifr->addr, &si, sizeof(si));
  642                 si.sin_addr.s_addr = sc->g_dst.s_addr;
  643                 memcpy(&lifr->dstaddr, &si, sizeof(si));
  644                 break;
  645         case SIOCGIFPSRCADDR:
  646 #ifdef INET6
  647         case SIOCGIFPSRCADDR_IN6:
  648 #endif
  649                 if (sc->g_src.s_addr == INADDR_ANY) {
  650                         error = EADDRNOTAVAIL;
  651                         break;
  652                 }
  653                 memset(&si, 0, sizeof(si));
  654                 si.sin_family = AF_INET;
  655                 si.sin_len = sizeof(struct sockaddr_in);
  656                 si.sin_addr.s_addr = sc->g_src.s_addr;
  657                 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
  658                 break;
  659         case SIOCGIFPDSTADDR:
  660 #ifdef INET6
  661         case SIOCGIFPDSTADDR_IN6:
  662 #endif
  663                 if (sc->g_dst.s_addr == INADDR_ANY) {
  664                         error = EADDRNOTAVAIL;
  665                         break;
  666                 }
  667                 memset(&si, 0, sizeof(si));
  668                 si.sin_family = AF_INET;
  669                 si.sin_len = sizeof(struct sockaddr_in);
  670                 si.sin_addr.s_addr = sc->g_dst.s_addr;
  671                 bcopy(&si, &ifr->ifr_addr, sizeof(ifr->ifr_addr));
  672                 break;
  673         default:
  674                 error = EINVAL;
  675                 break;
  676         }
  677 
  678         splx(s);
  679         return (error);
  680 }
  681 
  682 /*
  683  * computes a route to our destination that is not the one
  684  * which would be taken by ip_output(), as this one will loop back to
  685  * us. If the interface is p2p as  a--->b, then a routing entry exists
  686  * If we now send a packet to b (e.g. ping b), this will come down here
  687  * gets src=a, dst=b tacked on and would from ip_output() sent back to
  688  * if_gre.
  689  * Goal here is to compute a route to b that is less specific than
  690  * a-->b. We know that this one exists as in normal operation we have
  691  * at least a default route which matches.
  692  */
  693 static int
  694 gre_compute_route(struct gre_softc *sc)
  695 {
  696         struct route *ro;
  697         u_int32_t a, b, c;
  698 
  699         ro = &sc->route;
  700 
  701         memset(ro, 0, sizeof(struct route));
  702         ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
  703         ro->ro_dst.sa_family = AF_INET;
  704         ro->ro_dst.sa_len = sizeof(ro->ro_dst);
  705 
  706         /*
  707          * toggle last bit, so our interface is not found, but a less
  708          * specific route. I'd rather like to specify a shorter mask,
  709          * but this is not possible. Should work though. XXX
  710          * there is a simpler way ...
  711          */
  712         if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0) {
  713                 a = ntohl(sc->g_dst.s_addr);
  714                 b = a & 0x01;
  715                 c = a & 0xfffffffe;
  716                 b = b ^ 0x01;
  717                 a = b | c;
  718                 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
  719                     = htonl(a);
  720         }
  721 
  722 #ifdef DIAGNOSTIC
  723         printf("%s: searching for a route to %s", if_name(GRE2IFP(sc)),
  724             inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
  725 #endif
  726 
  727         rtalloc(ro);
  728 
  729         /*
  730          * check if this returned a route at all and this route is no
  731          * recursion to ourself
  732          */
  733         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
  734 #ifdef DIAGNOSTIC
  735                 if (ro->ro_rt == NULL)
  736                         printf(" - no route found!\n");
  737                 else
  738                         printf(" - route loops back to ourself!\n");
  739 #endif
  740                 return EADDRNOTAVAIL;
  741         }
  742 
  743         /*
  744          * now change it back - else ip_output will just drop
  745          * the route and search one to this interface ...
  746          */
  747         if ((GRE2IFP(sc)->if_flags & IFF_LINK1) == 0)
  748                 ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
  749 
  750 #ifdef DIAGNOSTIC
  751         printf(", choosing %s with gateway %s", if_name(ro->ro_rt->rt_ifp),
  752             inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
  753         printf("\n");
  754 #endif
  755 
  756         return 0;
  757 }
  758 
  759 /*
  760  * do a checksum of a buffer - much like in_cksum, which operates on
  761  * mbufs.
  762  */
  763 u_int16_t
  764 gre_in_cksum(u_int16_t *p, u_int len)
  765 {
  766         u_int32_t sum = 0;
  767         int nwords = len >> 1;
  768 
  769         while (nwords-- != 0)
  770                 sum += *p++;
  771 
  772         if (len & 1) {
  773                 union {
  774                         u_short w;
  775                         u_char c[2];
  776                 } u;
  777                 u.c[0] = *(u_char *)p;
  778                 u.c[1] = 0;
  779                 sum += u.w;
  780         }
  781 
  782         /* end-around-carry */
  783         sum = (sum >> 16) + (sum & 0xffff);
  784         sum += (sum >> 16);
  785         return (~sum);
  786 }
  787 
  788 static int
  789 gremodevent(module_t mod, int type, void *data)
  790 {
  791         struct gre_softc *sc;
  792 
  793         switch (type) {
  794         case MOD_LOAD:
  795                 greattach();
  796                 break;
  797         case MOD_UNLOAD:
  798                 if_clone_detach(&gre_cloner);
  799 
  800                 mtx_lock(&gre_mtx);
  801                 while ((sc = LIST_FIRST(&gre_softc_list)) != NULL) {
  802                         LIST_REMOVE(sc, sc_list);
  803                         mtx_unlock(&gre_mtx);
  804                         gre_destroy(sc);
  805                         mtx_lock(&gre_mtx);
  806                 }
  807                 mtx_unlock(&gre_mtx);
  808                 mtx_destroy(&gre_mtx);
  809                 break;
  810         default:
  811                 return EOPNOTSUPP;
  812         }
  813         return 0;
  814 }
  815 
  816 static moduledata_t gre_mod = {
  817         "if_gre",
  818         gremodevent,
  819         0
  820 };
  821 
  822 DECLARE_MODULE(if_gre, gre_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
  823 MODULE_VERSION(if_gre, 1);

Cache object: b532babe820236d134a911be995ee07e


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