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 /*-
    2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/10.3/sys/netinet/in_gif.c 284072 2015-06-06 13:26:13Z ae $");
   34 
   35 #include "opt_inet.h"
   36 #include "opt_inet6.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/lock.h>
   40 #include <sys/rmlock.h>
   41 #include <sys/systm.h>
   42 #include <sys/socket.h>
   43 #include <sys/sockio.h>
   44 #include <sys/mbuf.h>
   45 #include <sys/errno.h>
   46 #include <sys/kernel.h>
   47 #include <sys/sysctl.h>
   48 #include <sys/protosw.h>
   49 #include <sys/malloc.h>
   50 
   51 #include <net/if.h>
   52 #include <net/if_var.h>
   53 #include <net/route.h>
   54 #include <net/vnet.h>
   55 
   56 #include <netinet/in.h>
   57 #include <netinet/in_systm.h>
   58 #include <netinet/ip.h>
   59 #include <netinet/ip_var.h>
   60 #include <netinet/in_var.h>
   61 #include <netinet/ip_encap.h>
   62 #include <netinet/ip_ecn.h>
   63 
   64 #ifdef INET6
   65 #include <netinet/ip6.h>
   66 #endif
   67 
   68 #include <net/if_gif.h>
   69 
   70 static int gif_validate4(const struct ip *, struct gif_softc *,
   71         struct ifnet *);
   72 static int in_gif_input(struct mbuf **, int *, int);
   73 
   74 static void in_gif_input10(struct mbuf *, int);
   75 extern  struct domain inetdomain;
   76 static struct protosw in_gif_protosw = {
   77         .pr_type =              SOCK_RAW,
   78         .pr_domain =            &inetdomain,
   79         .pr_protocol =          0/* IPPROTO_IPV[46] */,
   80         .pr_flags =             PR_ATOMIC|PR_ADDR,
   81         .pr_input =             in_gif_input10,
   82         .pr_output =            (pr_output_t*)rip_output,
   83         .pr_ctloutput =         rip_ctloutput,
   84         .pr_usrreqs =           &rip_usrreqs
   85 };
   86 
   87 #define GIF_TTL         30
   88 static VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
   89 #define V_ip_gif_ttl            VNET(ip_gif_ttl)
   90 SYSCTL_VNET_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_RW,
   91         &VNET_NAME(ip_gif_ttl), 0, "");
   92 
   93 int
   94 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
   95 {
   96         GIF_RLOCK_TRACKER;
   97         struct gif_softc *sc = ifp->if_softc;
   98         struct ip *ip;
   99         int len;
  100 
  101         /* prepend new IP header */
  102         len = sizeof(struct ip);
  103 #ifndef __NO_STRICT_ALIGNMENT
  104         if (proto == IPPROTO_ETHERIP)
  105                 len += ETHERIP_ALIGN;
  106 #endif
  107         M_PREPEND(m, len, M_NOWAIT);
  108         if (m == NULL)
  109                 return (ENOBUFS);
  110 #ifndef __NO_STRICT_ALIGNMENT
  111         if (proto == IPPROTO_ETHERIP) {
  112                 len = mtod(m, vm_offset_t) & 3;
  113                 KASSERT(len == 0 || len == ETHERIP_ALIGN,
  114                     ("in_gif_output: unexpected misalignment"));
  115                 m->m_data += len;
  116                 m->m_len -= ETHERIP_ALIGN;
  117         }
  118 #endif
  119         ip = mtod(m, struct ip *);
  120         GIF_RLOCK(sc);
  121         if (sc->gif_family != AF_INET) {
  122                 m_freem(m);
  123                 GIF_RUNLOCK(sc);
  124                 return (ENETDOWN);
  125         }
  126         bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
  127         GIF_RUNLOCK(sc);
  128 
  129         ip->ip_p = proto;
  130         /* version will be set in ip_output() */
  131         ip->ip_ttl = V_ip_gif_ttl;
  132         ip->ip_len = htons(m->m_pkthdr.len);
  133         ip->ip_tos = ecn;
  134 
  135         return (ip_output(m, NULL, NULL, 0, NULL, NULL));
  136 }
  137 
  138 static void
  139 in_gif_input10(struct mbuf *m, int off)
  140 {
  141         int proto;
  142 
  143         proto = (mtod(m, struct ip *))->ip_p;
  144         in_gif_input(&m, &off, proto);
  145 }
  146 
  147 static int
  148 in_gif_input(struct mbuf **mp, int *offp, int proto)
  149 {
  150         struct mbuf *m = *mp;
  151         struct gif_softc *sc;
  152         struct ifnet *gifp;
  153         struct ip *ip;
  154         uint8_t ecn;
  155 
  156         sc = encap_getarg(m);
  157         if (sc == NULL) {
  158                 m_freem(m);
  159                 KMOD_IPSTAT_INC(ips_nogif);
  160                 return (IPPROTO_DONE);
  161         }
  162         gifp = GIF2IFP(sc);
  163         if ((gifp->if_flags & IFF_UP) != 0) {
  164                 ip = mtod(m, struct ip *);
  165                 ecn = ip->ip_tos;
  166                 m_adj(m, *offp);
  167                 gif_input(m, gifp, proto, ecn);
  168         } else {
  169                 m_freem(m);
  170                 KMOD_IPSTAT_INC(ips_nogif);
  171         }
  172         return (IPPROTO_DONE);
  173 }
  174 
  175 /*
  176  * validate outer address.
  177  */
  178 static int
  179 gif_validate4(const struct ip *ip, struct gif_softc *sc, struct ifnet *ifp)
  180 {
  181         int ret;
  182 
  183         GIF_RLOCK_ASSERT(sc);
  184 
  185         /* check for address match */
  186         if (sc->gif_iphdr->ip_src.s_addr != ip->ip_dst.s_addr)
  187                 return (0);
  188         ret = 32;
  189         if (sc->gif_iphdr->ip_dst.s_addr != ip->ip_src.s_addr) {
  190                 if ((sc->gif_options & GIF_IGNORE_SOURCE) == 0)
  191                         return (0);
  192         } else
  193                 ret += 32;
  194 
  195         /* martian filters on outer source - NOT done in ip_input! */
  196         if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)))
  197                 return (0);
  198         switch ((ntohl(ip->ip_src.s_addr) & 0xff000000) >> 24) {
  199         case 0:
  200         case 127:
  201         case 255:
  202                 return (0);
  203         }
  204 
  205         /* ingress filters on outer source */
  206         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0 && ifp) {
  207                 struct sockaddr_in sin;
  208                 struct rtentry *rt;
  209 
  210                 bzero(&sin, sizeof(sin));
  211                 sin.sin_family = AF_INET;
  212                 sin.sin_len = sizeof(struct sockaddr_in);
  213                 sin.sin_addr = ip->ip_src;
  214                 /* XXX MRT  check for the interface we would use on output */
  215                 rt = in_rtalloc1((struct sockaddr *)&sin, 0,
  216                     0UL, sc->gif_fibnum);
  217                 if (!rt || rt->rt_ifp != ifp) {
  218                         if (rt)
  219                                 RTFREE_LOCKED(rt);
  220                         return (0);
  221                 }
  222                 RTFREE_LOCKED(rt);
  223         }
  224         return (ret);
  225 }
  226 
  227 /*
  228  * we know that we are in IFF_UP, outer address available, and outer family
  229  * matched the physical addr family.  see gif_encapcheck().
  230  */
  231 int
  232 in_gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
  233 {
  234         struct ip ip;
  235         struct gif_softc *sc;
  236         struct ifnet *ifp;
  237 
  238         /* sanity check done in caller */
  239         sc = (struct gif_softc *)arg;
  240         GIF_RLOCK_ASSERT(sc);
  241 
  242         m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
  243         ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
  244         return (gif_validate4(&ip, sc, ifp));
  245 }
  246 
  247 int
  248 in_gif_attach(struct gif_softc *sc)
  249 {
  250 
  251         KASSERT(sc->gif_ecookie == NULL, ("gif_ecookie isn't NULL"));
  252         sc->gif_ecookie = encap_attach_func(AF_INET, -1, gif_encapcheck,
  253             &in_gif_protosw, sc);
  254         if (sc->gif_ecookie == NULL)
  255                 return (EEXIST);
  256         return (0);
  257 }

Cache object: 9ed16c170389f7d7e3db17c7d790a7f1


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