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/netinet6/in6_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 /*      $NetBSD: in6_gif.c,v 1.39.2.1 2006/01/08 15:48:45 riz Exp $     */
    2 /*      $KAME: in6_gif.c,v 1.62 2001/07/29 04:27:25 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 <sys/cdefs.h>
   34 __KERNEL_RCSID(0, "$NetBSD: in6_gif.c,v 1.39.2.1 2006/01/08 15:48:45 riz Exp $");
   35 
   36 #include "opt_inet.h"
   37 #include "opt_iso.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/systm.h>
   41 #include <sys/socket.h>
   42 #include <sys/sockio.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/errno.h>
   45 #include <sys/ioctl.h>
   46 #include <sys/queue.h>
   47 #include <sys/syslog.h>
   48 #include <sys/protosw.h>
   49 #include <sys/kernel.h>
   50 
   51 #include <net/if.h>
   52 #include <net/route.h>
   53 
   54 #include <netinet/in.h>
   55 #include <netinet/in_systm.h>
   56 #ifdef INET
   57 #include <netinet/ip.h>
   58 #endif
   59 #include <netinet/ip_encap.h>
   60 #ifdef INET6
   61 #include <netinet/ip6.h>
   62 #include <netinet6/ip6_var.h>
   63 #include <netinet6/in6_gif.h>
   64 #include <netinet6/in6_var.h>
   65 #endif
   66 #include <netinet6/ip6protosw.h>
   67 #include <netinet/ip_ecn.h>
   68 
   69 #include <net/if_gif.h>
   70 
   71 #include <net/net_osdep.h>
   72 
   73 static int gif_validate6 __P((const struct ip6_hdr *, struct gif_softc *,
   74         struct ifnet *));
   75 
   76 int     ip6_gif_hlim = GIF_HLIM;
   77 
   78 extern struct domain inet6domain;
   79 const struct ip6protosw in6_gif_protosw =
   80 { SOCK_RAW,     &inet6domain,   0/* IPPROTO_IPV[46] */, PR_ATOMIC|PR_ADDR,
   81   in6_gif_input, rip6_output,   in6_gif_ctlinput, rip6_ctloutput,
   82   rip6_usrreq,
   83   0,            0,              0,              0,
   84 };
   85 
   86 extern LIST_HEAD(, gif_softc) gif_softc_list;
   87 
   88 int
   89 in6_gif_output(ifp, family, m)
   90         struct ifnet *ifp;
   91         int family; /* family of the packet to be encapsulate. */
   92         struct mbuf *m;
   93 {
   94         struct gif_softc *sc = (struct gif_softc*)ifp;
   95         struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
   96         struct sockaddr_in6 *sin6_src = (struct sockaddr_in6 *)sc->gif_psrc;
   97         struct sockaddr_in6 *sin6_dst = (struct sockaddr_in6 *)sc->gif_pdst;
   98         struct ip6_hdr *ip6;
   99         int proto, error;
  100         u_int8_t itos, otos;
  101 
  102         if (sin6_src == NULL || sin6_dst == NULL ||
  103             sin6_src->sin6_family != AF_INET6 ||
  104             sin6_dst->sin6_family != AF_INET6) {
  105                 m_freem(m);
  106                 return EAFNOSUPPORT;
  107         }
  108 
  109         switch (family) {
  110 #ifdef INET
  111         case AF_INET:
  112             {
  113                 struct ip *ip;
  114 
  115                 proto = IPPROTO_IPV4;
  116                 if (m->m_len < sizeof(*ip)) {
  117                         m = m_pullup(m, sizeof(*ip));
  118                         if (!m)
  119                                 return ENOBUFS;
  120                 }
  121                 ip = mtod(m, struct ip *);
  122                 itos = ip->ip_tos;
  123                 break;
  124             }
  125 #endif
  126 #ifdef INET6
  127         case AF_INET6:
  128             {
  129                 struct ip6_hdr *ip6;
  130                 proto = IPPROTO_IPV6;
  131                 if (m->m_len < sizeof(*ip6)) {
  132                         m = m_pullup(m, sizeof(*ip6));
  133                         if (!m)
  134                                 return ENOBUFS;
  135                 }
  136                 ip6 = mtod(m, struct ip6_hdr *);
  137                 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
  138                 break;
  139             }
  140 #endif
  141 #ifdef ISO
  142         case AF_ISO:
  143                 proto = IPPROTO_EON;
  144                 itos = 0;
  145                 break;
  146 #endif
  147         default:
  148 #ifdef DEBUG
  149                 printf("in6_gif_output: warning: unknown family %d passed\n",
  150                         family);
  151 #endif
  152                 m_freem(m);
  153                 return EAFNOSUPPORT;
  154         }
  155 
  156         /* prepend new IP header */
  157         M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
  158         if (m && m->m_len < sizeof(struct ip6_hdr))
  159                 m = m_pullup(m, sizeof(struct ip6_hdr));
  160         if (m == NULL)
  161                 return ENOBUFS;
  162 
  163         ip6 = mtod(m, struct ip6_hdr *);
  164         ip6->ip6_flow   = 0;
  165         ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
  166         ip6->ip6_vfc    |= IPV6_VERSION;
  167 #if 0   /* ip6->ip6_plen will be filled by ip6_output */
  168         ip6->ip6_plen   = htons((u_int16_t)m->m_pkthdr.len);
  169 #endif
  170         ip6->ip6_nxt    = proto;
  171         ip6->ip6_hlim   = ip6_gif_hlim;
  172         ip6->ip6_src    = sin6_src->sin6_addr;
  173         /* bidirectional configured tunnel mode */
  174         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6_dst->sin6_addr))
  175                 ip6->ip6_dst = sin6_dst->sin6_addr;
  176         else  {
  177                 m_freem(m);
  178                 return ENETUNREACH;
  179         }
  180         if (ifp->if_flags & IFF_LINK1)
  181                 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos);
  182         else
  183                 ip_ecn_ingress(ECN_NOCARE, &otos, &itos);
  184         ip6->ip6_flow &= ~ntohl(0xff00000);
  185         ip6->ip6_flow |= htonl((u_int32_t)otos << 20);
  186 
  187         if (sc->gif_route_expire - time.tv_sec <= 0 ||
  188              dst->sin6_family != sin6_dst->sin6_family ||
  189              !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &sin6_dst->sin6_addr)) {
  190                 /* cache route doesn't match */
  191                 bzero(dst, sizeof(*dst));
  192                 dst->sin6_family = sin6_dst->sin6_family;
  193                 dst->sin6_len = sizeof(struct sockaddr_in6);
  194                 dst->sin6_addr = sin6_dst->sin6_addr;
  195                 if (sc->gif_ro6.ro_rt) {
  196                         RTFREE(sc->gif_ro6.ro_rt);
  197                         sc->gif_ro6.ro_rt = NULL;
  198                 }
  199         }
  200 
  201         if (sc->gif_ro6.ro_rt == NULL) {
  202                 rtalloc((struct route *)&sc->gif_ro6);
  203                 if (sc->gif_ro6.ro_rt == NULL) {
  204                         m_freem(m);
  205                         return ENETUNREACH;
  206                 }
  207 
  208                 /* if it constitutes infinite encapsulation, punt. */
  209                 if (sc->gif_ro.ro_rt->rt_ifp == ifp) {
  210                         m_freem(m);
  211                         return ENETUNREACH;     /* XXX */
  212                 }
  213 
  214                 sc->gif_route_expire = time.tv_sec + GIF_ROUTE_TTL;
  215         }
  216 
  217 #ifdef IPV6_MINMTU
  218         /*
  219          * force fragmentation to minimum MTU, to avoid path MTU discovery.
  220          * it is too painful to ask for resend of inner packet, to achieve
  221          * path MTU discovery for encapsulated packets.
  222          */
  223         error = ip6_output(m, 0, &sc->gif_ro6, IPV6_MINMTU,
  224                     (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL);
  225 #else
  226         error = ip6_output(m, 0, &sc->gif_ro6, 0,
  227                     (struct ip6_moptions *)NULL, (struct socket *)NULL, NULL);
  228 #endif
  229 
  230         return (error);
  231 }
  232 
  233 int in6_gif_input(mp, offp, proto)
  234         struct mbuf **mp;
  235         int *offp, proto;
  236 {
  237         struct mbuf *m = *mp;
  238         struct ifnet *gifp = NULL;
  239         struct ip6_hdr *ip6;
  240         int af = 0;
  241         u_int32_t otos;
  242 
  243         ip6 = mtod(m, struct ip6_hdr *);
  244 
  245         gifp = (struct ifnet *)encap_getarg(m);
  246 
  247         if (gifp == NULL || (gifp->if_flags & IFF_UP) == 0) {
  248                 m_freem(m);
  249                 ip6stat.ip6s_nogif++;
  250                 return IPPROTO_DONE;
  251         }
  252 #ifndef GIF_ENCAPCHECK
  253         if (!gif_validate6(ip6, (struct gif_softc *)gifp, m->m_pkthdr.rcvif)) {
  254                 m_freem(m);
  255                 ip6stat.ip6s_nogif++;
  256                 return IPPROTO_DONE;
  257         }
  258 #endif
  259 
  260         otos = ip6->ip6_flow;
  261         m_adj(m, *offp);
  262 
  263         switch (proto) {
  264 #ifdef INET
  265         case IPPROTO_IPV4:
  266             {
  267                 struct ip *ip;
  268                 u_int8_t otos8;
  269                 af = AF_INET;
  270                 otos8 = (ntohl(otos) >> 20) & 0xff;
  271                 if (m->m_len < sizeof(*ip)) {
  272                         m = m_pullup(m, sizeof(*ip));
  273                         if (!m)
  274                                 return IPPROTO_DONE;
  275                 }
  276                 ip = mtod(m, struct ip *);
  277                 if (gifp->if_flags & IFF_LINK1)
  278                         ip_ecn_egress(ECN_ALLOWED, &otos8, &ip->ip_tos);
  279                 else
  280                         ip_ecn_egress(ECN_NOCARE, &otos8, &ip->ip_tos);
  281                 break;
  282             }
  283 #endif /* INET */
  284 #ifdef INET6
  285         case IPPROTO_IPV6:
  286             {
  287                 struct ip6_hdr *ip6;
  288                 af = AF_INET6;
  289                 if (m->m_len < sizeof(*ip6)) {
  290                         m = m_pullup(m, sizeof(*ip6));
  291                         if (!m)
  292                                 return IPPROTO_DONE;
  293                 }
  294                 ip6 = mtod(m, struct ip6_hdr *);
  295                 if (gifp->if_flags & IFF_LINK1)
  296                         ip6_ecn_egress(ECN_ALLOWED, &otos, &ip6->ip6_flow);
  297                 else
  298                         ip6_ecn_egress(ECN_NOCARE, &otos, &ip6->ip6_flow);
  299                 break;
  300             }
  301 #endif
  302 #ifdef ISO
  303         case IPPROTO_EON:
  304                 af = AF_ISO;
  305                 break;
  306 #endif
  307         default:
  308                 ip6stat.ip6s_nogif++;
  309                 m_freem(m);
  310                 return IPPROTO_DONE;
  311         }
  312 
  313         gif_input(m, af, gifp);
  314         return IPPROTO_DONE;
  315 }
  316 
  317 /*
  318  * validate outer address.
  319  */
  320 static int
  321 gif_validate6(ip6, sc, ifp)
  322         const struct ip6_hdr *ip6;
  323         struct gif_softc *sc;
  324         struct ifnet *ifp;
  325 {
  326         struct sockaddr_in6 *src, *dst;
  327 
  328         src = (struct sockaddr_in6 *)sc->gif_psrc;
  329         dst = (struct sockaddr_in6 *)sc->gif_pdst;
  330 
  331         /* check for address match */
  332         if (!IN6_ARE_ADDR_EQUAL(&src->sin6_addr, &ip6->ip6_dst) ||
  333             !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_src))
  334                 return 0;
  335 
  336         /* martian filters on outer source - done in ip6_input */
  337 
  338         /* ingress filters on outer source */
  339         if ((sc->gif_if.if_flags & IFF_LINK2) == 0 && ifp) {
  340                 struct sockaddr_in6 sin6;
  341                 struct rtentry *rt;
  342 
  343                 bzero(&sin6, sizeof(sin6));
  344                 sin6.sin6_family = AF_INET6;
  345                 sin6.sin6_len = sizeof(struct sockaddr_in6);
  346                 sin6.sin6_addr = ip6->ip6_src;
  347                 /* XXX scopeid */
  348                 rt = rtalloc1((struct sockaddr *)&sin6, 0);
  349                 if (!rt || rt->rt_ifp != ifp) {
  350 #if 0
  351                         log(LOG_WARNING, "%s: packet from %s dropped "
  352                             "due to ingress filter\n", if_name(&sc->gif_if),
  353                             ip6_sprintf(&sin6.sin6_addr));
  354 #endif
  355                         if (rt)
  356                                 rtfree(rt);
  357                         return 0;
  358                 }
  359                 rtfree(rt);
  360         }
  361 
  362         return 128 * 2;
  363 }
  364 
  365 #ifdef GIF_ENCAPCHECK
  366 /*
  367  * we know that we are in IFF_UP, outer address available, and outer family
  368  * matched the physical addr family.  see gif_encapcheck().
  369  */
  370 int
  371 gif_encapcheck6(m, off, proto, arg)
  372         const struct mbuf *m;
  373         int off;
  374         int proto;
  375         void *arg;
  376 {
  377         struct ip6_hdr ip6;
  378         struct gif_softc *sc;
  379         struct ifnet *ifp;
  380 
  381         /* sanity check done in caller */
  382         sc = (struct gif_softc *)arg;
  383 
  384         /* LINTED const cast */
  385         m_copydata((struct mbuf *)m, 0, sizeof(ip6), (caddr_t)&ip6);
  386         ifp = ((m->m_flags & M_PKTHDR) != 0) ? m->m_pkthdr.rcvif : NULL;
  387 
  388         return gif_validate6(&ip6, sc, ifp);
  389 }
  390 #endif
  391 
  392 int
  393 in6_gif_attach(sc)
  394         struct gif_softc *sc;
  395 {
  396 #ifndef GIF_ENCAPCHECK
  397         struct sockaddr_in6 mask6;
  398 
  399         bzero(&mask6, sizeof(mask6));
  400         mask6.sin6_len = sizeof(struct sockaddr_in6);
  401         mask6.sin6_addr.s6_addr32[0] = mask6.sin6_addr.s6_addr32[1] =
  402             mask6.sin6_addr.s6_addr32[2] = mask6.sin6_addr.s6_addr32[3] = ~0;
  403 
  404         if (!sc->gif_psrc || !sc->gif_pdst)
  405                 return EINVAL;
  406         sc->encap_cookie6 = encap_attach(AF_INET6, -1, sc->gif_psrc,
  407             (struct sockaddr *)&mask6, sc->gif_pdst, (struct sockaddr *)&mask6,
  408             (void *)&in6_gif_protosw, sc);
  409 #else
  410         sc->encap_cookie6 = encap_attach_func(AF_INET6, -1, gif_encapcheck,
  411             (struct protosw *)&in6_gif_protosw, sc);
  412 #endif
  413         if (sc->encap_cookie6 == NULL)
  414                 return EEXIST;
  415         return 0;
  416 }
  417 
  418 int
  419 in6_gif_detach(sc)
  420         struct gif_softc *sc;
  421 {
  422         int error;
  423 
  424         error = encap_detach(sc->encap_cookie6);
  425         if (error == 0)
  426                 sc->encap_cookie6 = NULL;
  427 
  428         if (sc->gif_ro6.ro_rt) {
  429                 RTFREE(sc->gif_ro6.ro_rt);
  430                 sc->gif_ro6.ro_rt = NULL;
  431         }
  432 
  433         return error;
  434 }
  435 
  436 void
  437 in6_gif_ctlinput(cmd, sa, d)
  438         int cmd;
  439         struct sockaddr *sa;
  440         void *d;
  441 {
  442         struct gif_softc *sc;
  443         struct ip6ctlparam *ip6cp = NULL;
  444         struct ip6_hdr *ip6;
  445         struct sockaddr_in6 *dst6;
  446 
  447         if (sa->sa_family != AF_INET6 ||
  448             sa->sa_len != sizeof(struct sockaddr_in6))
  449                 return;
  450 
  451         if ((unsigned)cmd >= PRC_NCMDS)
  452                 return;
  453         if (cmd == PRC_HOSTDEAD)
  454                 d = NULL;
  455         else if (inet6ctlerrmap[cmd] == 0)
  456                 return;
  457 
  458         /* if the parameter is from icmp6, decode it. */
  459         if (d != NULL) {
  460                 ip6cp = (struct ip6ctlparam *)d;
  461                 ip6 = ip6cp->ip6c_ip6;
  462         } else {
  463                 ip6 = NULL;
  464         }
  465 
  466         if (!ip6)
  467                 return;
  468 
  469         /*
  470          * for now we don't care which type it was, just flush the route cache.
  471          * XXX slow.  sc (or sc->encap_cookie6) should be passed from
  472          * ip_encap.c.
  473          */
  474         for (sc = LIST_FIRST(&gif_softc_list); sc;
  475              sc = LIST_NEXT(sc, gif_list)) {
  476                 if ((sc->gif_if.if_flags & IFF_RUNNING) == 0)
  477                         continue;
  478                 if (sc->gif_psrc->sa_family != AF_INET6)
  479                         continue;
  480                 if (!sc->gif_ro6.ro_rt)
  481                         continue;
  482 
  483                 dst6 = (struct sockaddr_in6 *)&sc->gif_ro6.ro_dst;
  484                 /* XXX scope */
  485                 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst6->sin6_addr)) {
  486                         /* flush route cache */
  487                         RTFREE(sc->gif_ro6.ro_rt);
  488                         sc->gif_ro6.ro_rt = NULL;
  489                 }
  490         }
  491 }

Cache object: e7f39ad3984af9ba54ad010fdc54f252


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