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/ip_encap.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 /*      $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $       */
    2 
    3 /*-
    4  * SPDX-License-Identifier: BSD-3-Clause
    5  *
    6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    7  * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
    8  * All rights reserved.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. Neither the name of the project nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  */
   34 /*
   35  * My grandfather said that there's a devil inside tunnelling technology...
   36  *
   37  * We have surprisingly many protocols that want packets with IP protocol
   38  * #4 or #41.  Here's a list of protocols that want protocol #41:
   39  *      RFC1933 configured tunnel
   40  *      RFC1933 automatic tunnel
   41  *      RFC2401 IPsec tunnel
   42  *      RFC2473 IPv6 generic packet tunnelling
   43  *      RFC2529 6over4 tunnel
   44  *      mobile-ip6 (uses RFC2473)
   45  *      RFC3056 6to4 tunnel
   46  *      isatap tunnel
   47  * Here's a list of protocol that want protocol #4:
   48  *      RFC1853 IPv4-in-IPv4 tunnelling
   49  *      RFC2003 IPv4 encapsulation within IPv4
   50  *      RFC2344 reverse tunnelling for mobile-ip4
   51  *      RFC2401 IPsec tunnel
   52  * Well, what can I say.  They impose different en/decapsulation mechanism
   53  * from each other, so they need separate protocol handler.  The only one
   54  * we can easily determine by protocol # is IPsec, which always has
   55  * AH/ESP/IPComp header right after outer IP header.
   56  *
   57  * So, clearly good old protosw does not work for protocol #4 and #41.
   58  * The code will let you match protocol via src/dst address pair.
   59  */
   60 
   61 #include <sys/cdefs.h>
   62 __FBSDID("$FreeBSD$");
   63 
   64 #include "opt_inet.h"
   65 #include "opt_inet6.h"
   66 
   67 #include <sys/param.h>
   68 #include <sys/systm.h>
   69 #include <sys/eventhandler.h>
   70 #include <sys/kernel.h>
   71 #include <sys/lock.h>
   72 #include <sys/malloc.h>
   73 #include <sys/mutex.h>
   74 #include <sys/mbuf.h>
   75 #include <sys/errno.h>
   76 #include <sys/socket.h>
   77 
   78 #include <net/if.h>
   79 #include <net/if_var.h>
   80 
   81 #include <netinet/in.h>
   82 #include <netinet/ip_var.h>
   83 #include <netinet/ip_encap.h>
   84 
   85 #ifdef INET6
   86 #include <netinet6/ip6_var.h>
   87 #endif
   88 
   89 static MALLOC_DEFINE(M_NETADDR, "encap_export_host",
   90     "Export host address structure");
   91 
   92 struct encaptab {
   93         CK_LIST_ENTRY(encaptab) chain;
   94         int             proto;
   95         int             min_length;
   96         int             exact_match;
   97         void            *arg;
   98 
   99         encap_lookup_t  lookup;
  100         encap_check_t   check;
  101         encap_input_t   input;
  102 };
  103 
  104 struct srcaddrtab {
  105         CK_LIST_ENTRY(srcaddrtab) chain;
  106 
  107         encap_srcaddr_t srcaddr;
  108         void            *arg;
  109 };
  110 
  111 CK_LIST_HEAD(encaptab_head, encaptab);
  112 CK_LIST_HEAD(srcaddrtab_head, srcaddrtab);
  113 #ifdef INET
  114 static struct encaptab_head ipv4_encaptab = CK_LIST_HEAD_INITIALIZER();
  115 static struct srcaddrtab_head ipv4_srcaddrtab = CK_LIST_HEAD_INITIALIZER();
  116 #endif
  117 #ifdef INET6
  118 static struct encaptab_head ipv6_encaptab = CK_LIST_HEAD_INITIALIZER();
  119 static struct srcaddrtab_head ipv6_srcaddrtab = CK_LIST_HEAD_INITIALIZER();
  120 #endif
  121 
  122 static struct mtx encapmtx, srcaddrmtx;
  123 MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF);
  124 MTX_SYSINIT(srcaddrmtx, &srcaddrmtx, "srcaddrmtx", MTX_DEF);
  125 #define ENCAP_WLOCK()           mtx_lock(&encapmtx)
  126 #define ENCAP_WUNLOCK()         mtx_unlock(&encapmtx)
  127 #define ENCAP_RLOCK_TRACKER     struct epoch_tracker encap_et
  128 #define ENCAP_RLOCK()           \
  129     epoch_enter_preempt(net_epoch_preempt, &encap_et)
  130 #define ENCAP_RUNLOCK()         \
  131     epoch_exit_preempt(net_epoch_preempt, &encap_et)
  132 #define ENCAP_WAIT()            epoch_wait_preempt(net_epoch_preempt)
  133 
  134 #define SRCADDR_WLOCK()         mtx_lock(&srcaddrmtx)
  135 #define SRCADDR_WUNLOCK()       mtx_unlock(&srcaddrmtx)
  136 #define SRCADDR_RLOCK_TRACKER   struct epoch_tracker srcaddr_et
  137 #define SRCADDR_RLOCK()         \
  138     epoch_enter_preempt(net_epoch_preempt, &srcaddr_et)
  139 #define SRCADDR_RUNLOCK()       \
  140     epoch_exit_preempt(net_epoch_preempt, &srcaddr_et)
  141 #define SRCADDR_WAIT()          epoch_wait_preempt(net_epoch_preempt)
  142 
  143 /*
  144  * ifaddr_event_ext handler.
  145  *
  146  * Tunnelling interfaces may request the kernel to notify when
  147  * some interface addresses appears or disappears. Usually tunnelling
  148  * interface must use an address configured on the local machine as
  149  * ingress address to be able receive datagramms and do not send
  150  * spoofed packets.
  151  */
  152 static void
  153 srcaddr_change_event(void *arg __unused, struct ifnet *ifp,
  154     struct ifaddr *ifa, int event)
  155 {
  156         SRCADDR_RLOCK_TRACKER;
  157         struct srcaddrtab_head *head;
  158         struct srcaddrtab *p;
  159 
  160         /* Support for old ifaddr_event. */
  161         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  162 
  163         switch (ifa->ifa_addr->sa_family) {
  164 #ifdef INET
  165         case AF_INET:
  166                 head = &ipv4_srcaddrtab;
  167                 break;
  168 #endif
  169 #ifdef INET6
  170         case AF_INET6:
  171                 head = &ipv6_srcaddrtab;
  172                 break;
  173 #endif
  174         default:
  175                 /* ignore event */
  176                 return;
  177         }
  178 
  179         SRCADDR_RLOCK();
  180         CK_LIST_FOREACH(p, head, chain) {
  181                 (*p->srcaddr)(p->arg, ifa->ifa_addr, event);
  182         }
  183         SRCADDR_RUNLOCK();
  184 }
  185 EVENTHANDLER_DEFINE(ifaddr_event_ext, srcaddr_change_event, NULL, 0);
  186 
  187 static struct srcaddrtab *
  188 encap_register_srcaddr(struct srcaddrtab_head *head, encap_srcaddr_t func,
  189     void *arg, int mflags)
  190 {
  191         struct srcaddrtab *p, *tmp;
  192 
  193         if (func == NULL)
  194                 return (NULL);
  195         p = malloc(sizeof(*p), M_NETADDR, mflags);
  196         if (p == NULL)
  197                 return (NULL);
  198         p->srcaddr = func;
  199         p->arg = arg;
  200 
  201         SRCADDR_WLOCK();
  202         CK_LIST_FOREACH(tmp, head, chain) {
  203                 if (func == tmp->srcaddr && arg == tmp->arg)
  204                         break;
  205         }
  206         if (tmp == NULL)
  207                 CK_LIST_INSERT_HEAD(head, p, chain);
  208         SRCADDR_WUNLOCK();
  209 
  210         if (tmp != NULL) {
  211                 free(p, M_NETADDR);
  212                 p = tmp;
  213         }
  214         return (p);
  215 }
  216 
  217 static int
  218 encap_unregister_srcaddr(struct srcaddrtab_head *head,
  219     const struct srcaddrtab *cookie)
  220 {
  221         struct srcaddrtab *p;
  222 
  223         SRCADDR_WLOCK();
  224         CK_LIST_FOREACH(p, head, chain) {
  225                 if (p == cookie) {
  226                         CK_LIST_REMOVE(p, chain);
  227                         SRCADDR_WUNLOCK();
  228                         SRCADDR_WAIT();
  229                         free(p, M_NETADDR);
  230                         return (0);
  231                 }
  232         }
  233         SRCADDR_WUNLOCK();
  234         return (EINVAL);
  235 }
  236 
  237 static struct encaptab *
  238 encap_attach(struct encaptab_head *head, const struct encap_config *cfg,
  239     void *arg, int mflags)
  240 {
  241         struct encaptab *ep, *tmp;
  242 
  243         if (cfg == NULL || cfg->input == NULL ||
  244             (cfg->check == NULL && cfg->lookup == NULL) ||
  245             (cfg->lookup != NULL && cfg->exact_match != ENCAP_DRV_LOOKUP) ||
  246             (cfg->exact_match == ENCAP_DRV_LOOKUP && cfg->lookup == NULL))
  247                 return (NULL);
  248 
  249         ep = malloc(sizeof(*ep), M_NETADDR, mflags);
  250         if (ep == NULL)
  251                 return (NULL);
  252 
  253         ep->proto = cfg->proto;
  254         ep->min_length = cfg->min_length;
  255         ep->exact_match = cfg->exact_match;
  256         ep->arg = arg;
  257         ep->lookup = cfg->exact_match == ENCAP_DRV_LOOKUP ? cfg->lookup: NULL;
  258         ep->check = cfg->exact_match != ENCAP_DRV_LOOKUP ? cfg->check: NULL;
  259         ep->input = cfg->input;
  260 
  261         ENCAP_WLOCK();
  262         CK_LIST_FOREACH(tmp, head, chain) {
  263                 if (tmp->exact_match <= ep->exact_match)
  264                         break;
  265         }
  266         if (tmp == NULL)
  267                 CK_LIST_INSERT_HEAD(head, ep, chain);
  268         else
  269                 CK_LIST_INSERT_BEFORE(tmp, ep, chain);
  270         ENCAP_WUNLOCK();
  271         return (ep);
  272 }
  273 
  274 static int
  275 encap_detach(struct encaptab_head *head, const struct encaptab *cookie)
  276 {
  277         struct encaptab *ep;
  278 
  279         ENCAP_WLOCK();
  280         CK_LIST_FOREACH(ep, head, chain) {
  281                 if (ep == cookie) {
  282                         CK_LIST_REMOVE(ep, chain);
  283                         ENCAP_WUNLOCK();
  284                         ENCAP_WAIT();
  285                         free(ep, M_NETADDR);
  286                         return (0);
  287                 }
  288         }
  289         ENCAP_WUNLOCK();
  290         return (EINVAL);
  291 }
  292 
  293 static int
  294 encap_input(struct encaptab_head *head, struct mbuf *m, int off, int proto)
  295 {
  296         ENCAP_RLOCK_TRACKER;
  297         struct encaptab *ep, *match;
  298         void *arg;
  299         int matchprio, ret;
  300 
  301         match = NULL;
  302         matchprio = 0;
  303 
  304         ENCAP_RLOCK();
  305         CK_LIST_FOREACH(ep, head, chain) {
  306                 if (ep->proto >= 0 && ep->proto != proto)
  307                         continue;
  308                 if (ep->min_length > m->m_pkthdr.len)
  309                         continue;
  310                 if (ep->exact_match == ENCAP_DRV_LOOKUP)
  311                         ret = (*ep->lookup)(m, off, proto, &arg);
  312                 else
  313                         ret = (*ep->check)(m, off, proto, ep->arg);
  314                 if (ret <= 0)
  315                         continue;
  316                 if (ret > matchprio) {
  317                         match = ep;
  318                         if (ep->exact_match != ENCAP_DRV_LOOKUP)
  319                                 arg = ep->arg;
  320                         /*
  321                          * No need to continue the search, we got the
  322                          * exact match.
  323                          */
  324                         if (ret >= ep->exact_match)
  325                                 break;
  326                         matchprio = ret;
  327                 }
  328         }
  329 
  330         if (match != NULL) {
  331                 /* found a match, "match" has the best one */
  332                 ret = (*match->input)(m, off, proto, arg);
  333                 ENCAP_RUNLOCK();
  334                 MPASS(ret == IPPROTO_DONE);
  335                 return (IPPROTO_DONE);
  336         }
  337         ENCAP_RUNLOCK();
  338         return (0);
  339 }
  340 
  341 #ifdef INET
  342 const struct srcaddrtab *
  343 ip_encap_register_srcaddr(encap_srcaddr_t func, void *arg, int mflags)
  344 {
  345 
  346         return (encap_register_srcaddr(&ipv4_srcaddrtab, func, arg, mflags));
  347 }
  348 
  349 int
  350 ip_encap_unregister_srcaddr(const struct srcaddrtab *cookie)
  351 {
  352 
  353         return (encap_unregister_srcaddr(&ipv4_srcaddrtab, cookie));
  354 }
  355 
  356 const struct encaptab *
  357 ip_encap_attach(const struct encap_config *cfg, void *arg, int mflags)
  358 {
  359 
  360         return (encap_attach(&ipv4_encaptab, cfg, arg, mflags));
  361 }
  362 
  363 int
  364 ip_encap_detach(const struct encaptab *cookie)
  365 {
  366 
  367         return (encap_detach(&ipv4_encaptab, cookie));
  368 }
  369 
  370 int
  371 encap4_input(struct mbuf **mp, int *offp, int proto)
  372 {
  373 
  374         if (encap_input(&ipv4_encaptab, *mp, *offp, proto) != IPPROTO_DONE)
  375                 return (rip_input(mp, offp, proto));
  376         return (IPPROTO_DONE);
  377 }
  378 #endif /* INET */
  379 
  380 #ifdef INET6
  381 const struct srcaddrtab *
  382 ip6_encap_register_srcaddr(encap_srcaddr_t func, void *arg, int mflags)
  383 {
  384 
  385         return (encap_register_srcaddr(&ipv6_srcaddrtab, func, arg, mflags));
  386 }
  387 
  388 int
  389 ip6_encap_unregister_srcaddr(const struct srcaddrtab *cookie)
  390 {
  391 
  392         return (encap_unregister_srcaddr(&ipv6_srcaddrtab, cookie));
  393 }
  394 
  395 const struct encaptab *
  396 ip6_encap_attach(const struct encap_config *cfg, void *arg, int mflags)
  397 {
  398 
  399         return (encap_attach(&ipv6_encaptab, cfg, arg, mflags));
  400 }
  401 
  402 int
  403 ip6_encap_detach(const struct encaptab *cookie)
  404 {
  405 
  406         return (encap_detach(&ipv6_encaptab, cookie));
  407 }
  408 
  409 int
  410 encap6_input(struct mbuf **mp, int *offp, int proto)
  411 {
  412 
  413         if (encap_input(&ipv6_encaptab, *mp, *offp, proto) != IPPROTO_DONE)
  414                 return (rip6_input(mp, offp, proto));
  415         return (IPPROTO_DONE);
  416 }
  417 #endif /* INET6 */

Cache object: 1b56edd5684a3c00a669f30826433e76


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