[ 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  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  3  -  10 

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

Cache object: 7575a602a2fa089f81667b63abe418a2


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