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/netinet/in_gif.c

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

    1 /*      $FreeBSD$       */
    2 /*      $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $ */
    3 
    4 /*-
    5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include "opt_mrouting.h"
   34 #include "opt_inet.h"
   35 #include "opt_inet6.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/socket.h>
   40 #include <sys/sockio.h>
   41 #include <sys/mbuf.h>
   42 #include <sys/errno.h>
   43 #include <sys/kernel.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/protosw.h>
   46 
   47 #include <sys/malloc.h>
   48 
   49 #include <net/if.h>
   50 #include <net/route.h>
   51 
   52 #include <netinet/in.h>
   53 #include <netinet/in_systm.h>
   54 #include <netinet/ip.h>
   55 #include <netinet/ip_var.h>
   56 #include <netinet/in_gif.h>
   57 #include <netinet/in_var.h>
   58 #include <netinet/ip_encap.h>
   59 #include <netinet/ip_ecn.h>
   60 
   61 #ifdef INET6
   62 #include <netinet/ip6.h>
   63 #endif
   64 
   65 #ifdef MROUTING
   66 #include <netinet/ip_mroute.h>
   67 #endif /* MROUTING */
   68 
   69 #include <net/if_gif.h> 
   70 
   71 #include <net/net_osdep.h>
   72 
   73 static int gif_validate4(const struct ip *, struct gif_softc *,
   74         struct ifnet *);
   75 
   76 extern  struct domain inetdomain;
   77 struct protosw in_gif_protosw = {
   78         .pr_type =              SOCK_RAW,
   79         .pr_domain =            &inetdomain,
   80         .pr_protocol =          0/* IPPROTO_IPV[46] */,
   81         .pr_flags =             PR_ATOMIC|PR_ADDR,
   82         .pr_input =             in_gif_input,
   83         .pr_output =            (pr_output_t*)rip_output,
   84         .pr_ctloutput =         rip_ctloutput,
   85         .pr_usrreqs =           &rip_usrreqs
   86 };
   87 
   88 static int ip_gif_ttl = GIF_TTL;
   89 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
   90         &ip_gif_ttl,    0, "");
   91 
   92 int
   93 in_gif_output(ifp, family, m)
   94         struct ifnet    *ifp;
   95         int             family;
   96         struct mbuf     *m;
   97 {
   98         struct gif_softc *sc = ifp->if_softc;
   99         struct sockaddr_in *dst = (struct sockaddr_in *)&sc->gif_ro.ro_dst;
  100         struct sockaddr_in *sin_src = (struct sockaddr_in *)sc->gif_psrc;
  101         struct sockaddr_in *sin_dst = (struct sockaddr_in *)sc->gif_pdst;
  102         struct ip iphdr;        /* capsule IP header, host byte ordered */
  103         struct etherip_header eiphdr;
  104         int error, len, proto;
  105         u_int8_t tos;
  106 
  107         GIF_LOCK_ASSERT(sc);
  108 
  109         if (sin_src == NULL || sin_dst == NULL ||
  110             sin_src->sin_family != AF_INET ||
  111             sin_dst->sin_family != AF_INET) {
  112                 m_freem(m);
  113                 return EAFNOSUPPORT;
  114         }
  115 
  116         switch (family) {
  117 #ifdef INET
  118         case AF_INET:
  119             {
  120                 struct ip *ip;
  121 
  122                 proto = IPPROTO_IPV4;
  123                 if (m->m_len < sizeof(*ip)) {
  124                         m = m_pullup(m, sizeof(*ip));
  125                         if (!m)
  126                                 return ENOBUFS;
  127                 }
  128                 ip = mtod(m, struct ip *);
  129                 tos = ip->ip_tos;
  130                 break;
  131             }
  132 #endif /* INET */
  133 #ifdef INET6
  134         case AF_INET6:
  135             {
  136                 struct ip6_hdr *ip6;
  137                 proto = IPPROTO_IPV6;
  138                 if (m->m_len < sizeof(*ip6)) {
  139                         m = m_pullup(m, sizeof(*ip6));
  140                         if (!m)
  141                                 return ENOBUFS;
  142                 }
  143                 ip6 = mtod(m, struct ip6_hdr *);
  144                 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
  145                 break;
  146             }
  147 #endif /* INET6 */
  148         case AF_LINK:
  149                 proto = IPPROTO_ETHERIP;
  150                 eiphdr.eip_ver = ETHERIP_VERSION & ETHERIP_VER_VERS_MASK;
  151                 eiphdr.eip_pad = 0;
  152                 /* prepend Ethernet-in-IP header */
  153                 M_PREPEND(m, sizeof(struct etherip_header), M_DONTWAIT);
  154                 if (m && m->m_len < sizeof(struct etherip_header))
  155                         m = m_pullup(m, sizeof(struct etherip_header));
  156                 if (m == NULL)
  157                         return ENOBUFS;
  158                 bcopy(&eiphdr, mtod(m, struct etherip_header *),
  159                     sizeof(struct etherip_header));
  160                 break;
  161 
  162         default:
  163 #ifdef DEBUG
  164                 printf("in_gif_output: warning: unknown family %d passed\n",
  165                         family);
  166 #endif
  167                 m_freem(m);
  168                 return EAFNOSUPPORT;
  169         }
  170 
  171         bzero(&iphdr, sizeof(iphdr));
  172         iphdr.ip_src = sin_src->sin_addr;
  173         /* bidirectional configured tunnel mode */
  174         if (sin_dst->sin_addr.s_addr != INADDR_ANY)
  175                 iphdr.ip_dst = sin_dst->sin_addr;
  176         else {
  177                 m_freem(m);
  178                 return ENETUNREACH;
  179         }
  180         iphdr.ip_p = proto;
  181         /* version will be set in ip_output() */
  182         iphdr.ip_ttl = ip_gif_ttl;
  183         iphdr.ip_len = m->m_pkthdr.len + sizeof(struct ip);
  184         ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED : ECN_NOCARE,
  185                        &iphdr.ip_tos, &tos);
  186 
  187         /* prepend new IP header */
  188         len = sizeof(struct ip);
  189 #ifndef __NO_STRICT_ALIGNMENT
  190         if (family == AF_LINK)
  191                 len += ETHERIP_ALIGN;
  192 #endif
  193         M_PREPEND(m, len, M_DONTWAIT);
  194         if (m != NULL && m->m_len < len)
  195                 m = m_pullup(m, len);
  196         if (m == NULL) {
  197                 printf("ENOBUFS in in_gif_output %d\n", __LINE__);
  198                 return ENOBUFS;
  199         }
  200 #ifndef __NO_STRICT_ALIGNMENT
  201         if (family == AF_LINK) {
  202                 len = mtod(m, vm_offset_t) & 3;
  203                 KASSERT(len == 0 || len == ETHERIP_ALIGN,
  204                     ("in_gif_output: unexpected misalignment"));
  205                 m->m_data += len;
  206                 m->m_len -= ETHERIP_ALIGN;
  207         }
  208 #endif
  209         bcopy(&iphdr, mtod(m, struct ip *), sizeof(struct ip));
  210 
  211         if (dst->sin_family != sin_dst->sin_family ||
  212             dst->sin_addr.s_addr != sin_dst->sin_addr.s_addr) {
  213                 /* cache route doesn't match */
  214                 bzero(dst, sizeof(*dst));
  215                 dst->sin_family = sin_dst->sin_family;
  216                 dst->sin_len = sizeof(struct sockaddr_in);
  217                 dst->sin_addr = sin_dst->sin_addr;
  218                 if (sc->gif_ro.ro_rt) {
  219                         RTFREE(sc->gif_ro.ro_rt);
  220                         sc->gif_ro.ro_rt = NULL;
  221                 }
  222 #if 0
  223                 GIF2IFP(sc)->if_mtu = GIF_MTU;
  224 #endif
  225         }
  226 
  227         if (sc->gif_ro.ro_rt == NULL) {
  228                 rtalloc_ign(&sc->gif_ro, 0);
  229                 if (sc->gif_ro.ro_rt == NULL) {
  230                         m_freem(m);
  231                         return ENETUNREACH;
  232                 }
  233 
  234                 /* if it constitutes infinite encapsulation, punt. */
  235                 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
  236                         m_freem(m);
  237                         return ENETUNREACH;     /* XXX */
  238                 }
  239 #if 0
  240                 ifp->if_mtu = sc->gif_ro.ro_rt->rt_ifp->if_mtu
  241                         - sizeof(struct ip);
  242 #endif
  243         }
  244 
  245         error = ip_output(m, NULL, &sc->gif_ro, 0, NULL, NULL);
  246 
  247         if (!(GIF2IFP(sc)->if_flags & IFF_LINK0) &&
  248             sc->gif_ro.ro_rt != NULL) {
  249                 RTFREE(sc->gif_ro.ro_rt);
  250                 sc->gif_ro.ro_rt = NULL;
  251         }
  252 
  253         return (error);
  254 }
  255 
  256 void
  257 in_gif_input(m, off)
  258         struct mbuf *m;
  259         int off;
  260 {
  261         struct ifnet *gifp = NULL;
  262         struct gif_softc *sc;
  263         struct ip *ip;
  264         int af;
  265         u_int8_t otos;
  266         int proto;
  267 
  268         ip = mtod(m, struct ip *);
  269         proto = ip->ip_p;
  270 
  271         sc = (struct gif_softc *)encap_getarg(m);
  272         if (sc == NULL) {
  273                 m_freem(m);
  274                 ipstat.ips_nogif++;
  275                 return;
  276         }
  277 
  278         gifp = GIF2IFP(sc);
  279         if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
  280                 m_freem(m);
  281                 ipstat.ips_nogif++;
  282                 return;
  283         }
  284 
  285         otos = ip->ip_tos;
  286         m_adj(m, off);
  287 
  288         switch (proto) {
  289 #ifdef INET
  290         case IPPROTO_IPV4:
  291             {
  292                 struct ip *ip;
  293                 af = AF_INET;
  294                 if (m->m_len < sizeof(*ip)) {
  295                         m = m_pullup(m, sizeof(*ip));
  296                         if (!m)
  297                                 return;
  298                 }
  299                 ip = mtod(m, struct ip *);
  300                 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
  301                                   ECN_ALLOWED : ECN_NOCARE,
  302                                   &otos, &ip->ip_tos) == 0) {
  303                         m_freem(m);
  304                         return;
  305                 }
  306                 break;
  307             }
  308 #endif
  309 #ifdef INET6
  310         case IPPROTO_IPV6:
  311             {
  312                 struct ip6_hdr *ip6;
  313                 u_int8_t itos, oitos;
  314 
  315                 af = AF_INET6;
  316                 if (m->m_len < sizeof(*ip6)) {
  317                         m = m_pullup(m, sizeof(*ip6));
  318                         if (!m)
  319                                 return;
  320                 }
  321                 ip6 = mtod(m, struct ip6_hdr *);
  322                 itos = oitos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
  323                 if (ip_ecn_egress((gifp->if_flags & IFF_LINK1) ?
  324                                   ECN_ALLOWED : ECN_NOCARE,
  325                                   &otos, &itos) == 0) {
  326                         m_freem(m);
  327                         return;
  328                 }
  329                 if (itos != oitos) {
  330                         ip6->ip6_flow &= ~htonl(0xff << 20);
  331                         ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
  332                 }
  333                 break;
  334             }
  335 #endif /* INET6 */
  336         case IPPROTO_ETHERIP:
  337                 af = AF_LINK;
  338                 break;  
  339 
  340         default:
  341                 ipstat.ips_nogif++;
  342                 m_freem(m);
  343                 return;
  344         }
  345         gif_input(m, af, gifp);
  346         return;
  347 }
  348 
  349 /*
  350  * validate outer address.
  351  */
  352 static int
  353 gif_validate4(ip, sc, ifp)
  354         const struct ip *ip;
  355         struct gif_softc *sc;
  356         struct ifnet *ifp;
  357 {
  358         struct sockaddr_in *src, *dst;
  359         struct in_ifaddr *ia4;
  360 
  361         src = (struct sockaddr_in *)sc->gif_psrc;
  362         dst = (struct sockaddr_in *)sc->gif_pdst;
  363 
  364         /* check for address match */
  365         if (src->sin_addr.s_addr != ip->ip_dst.s_addr ||
  366             dst->sin_addr.s_addr != ip->ip_src.s_addr)
  367                 return 0;
  368 
  369         /* martian filters on outer source - NOT done in ip_input! */
  370         if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
  371                 return 0;
  372         switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
  373         case 0: case 127: case 255:
  374                 return 0;
  375         }
  376         /* reject packets with broadcast on source */
  377         TAILQ_FOREACH(ia4, &in_ifaddrhead, ia_link) {
  378                 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
  379                         continue;
  380                 if (ip->ip_src.s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
  381                         return 0;
  382         }
  383 
  384         /* ingress filters on outer source */
  385         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
  386                 struct sockaddr_in sin;
  387                 struct rtentry *rt;
  388 
  389                 bzero(&sin, sizeof(sin));
  390                 sin.sin_family = AF_INET;
  391                 sin.sin_len = sizeof(struct sockaddr_in);
  392                 sin.sin_addr = ip->ip_src;
  393                 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
  394                 if (!rt || rt->rt_ifp != ifp) {
  395 #if 0
  396                         log(LOG_WARNING, "%s: packet from 0x%x dropped "
  397                             "due to ingress filter\n", if_name(GIF2IFP(sc)),
  398                             (u_int32_t)ntohl(sin.sin_addr.s_addr));
  399 #endif
  400                         if (rt)
  401                                 RTFREE_LOCKED(rt);
  402                         return 0;
  403                 }
  404                 RTFREE_LOCKED(rt);
  405         }
  406 
  407         return 32 * 2;
  408 }
  409 
  410 /*
  411  * we know that we are in IFF_UP, outer address available, and outer family
  412  * matched the physical addr family.  see gif_encapcheck().
  413  */
  414 int
  415 gif_encapcheck4(m, off, proto, arg)
  416         const struct mbuf *m;
  417         int off;
  418         int proto;
  419         void *arg;
  420 {
  421         struct ip ip;
  422         struct gif_softc *sc;
  423         struct ifnet *ifp;
  424 
  425         /* sanity check done in caller */
  426         sc = (struct gif_softc *)arg;
  427 
  428         /* LINTED const cast */
  429         m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
  430         ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
  431 
  432         return gif_validate4(&ip, sc, ifp);
  433 }
  434 
  435 int
  436 in_gif_attach(sc)
  437         struct gif_softc *sc;
  438 {
  439         sc->encap_cookie4 = encap_attach_func(AF_INET, -1, gif_encapcheck,
  440             &in_gif_protosw, sc);
  441         if (sc->encap_cookie4 == NULL)
  442                 return EEXIST;
  443         return 0;
  444 }
  445 
  446 int
  447 in_gif_detach(sc)
  448         struct gif_softc *sc;
  449 {
  450         int error;
  451 
  452         error = encap_detach(sc->encap_cookie4);
  453         if (error == 0)
  454                 sc->encap_cookie4 = NULL;
  455         return error;
  456 }

Cache object: 1926e7ddae3bd73292be5f52cd983f5a


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