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/netgraph/ng_ipfw.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 2005, Gleb Smirnoff <glebius@FreeBSD.org>
    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  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD: releng/9.2/sys/netgraph/ng_ipfw.c 243401 2012-11-22 12:11:32Z glebius $
   27  */
   28 
   29 #include "opt_inet.h"
   30 #include "opt_inet6.h"
   31 
   32 #include <sys/param.h>
   33 #include <sys/systm.h>
   34 #include <sys/kernel.h>
   35 #include <sys/lock.h>
   36 #include <sys/mbuf.h>
   37 #include <sys/malloc.h>
   38 #include <sys/ctype.h>
   39 #include <sys/errno.h>
   40 #include <sys/rwlock.h>
   41 #include <sys/socket.h>
   42 #include <sys/syslog.h>
   43 
   44 #include <net/if.h>
   45 
   46 #include <netinet/in.h>
   47 #include <netinet/in_systm.h>
   48 #include <netinet/in_var.h>
   49 #include <netinet/ip_var.h>
   50 #include <netinet/ip_fw.h>
   51 #include <netinet/ip.h>
   52 #include <netinet/ip6.h>
   53 #include <netinet6/ip6_var.h>
   54 
   55 #include <netpfil/ipfw/ip_fw_private.h>
   56 
   57 #include <netgraph/ng_message.h>
   58 #include <netgraph/ng_parse.h>
   59 #include <netgraph/ng_ipfw.h>
   60 #include <netgraph/netgraph.h>
   61 
   62 static int              ng_ipfw_mod_event(module_t mod, int event, void *data);
   63 static ng_constructor_t ng_ipfw_constructor;
   64 static ng_shutdown_t    ng_ipfw_shutdown;
   65 static ng_newhook_t     ng_ipfw_newhook;
   66 static ng_connect_t     ng_ipfw_connect;
   67 static ng_findhook_t    ng_ipfw_findhook;
   68 static ng_rcvdata_t     ng_ipfw_rcvdata;
   69 static ng_disconnect_t  ng_ipfw_disconnect;
   70 
   71 static hook_p           ng_ipfw_findhook1(node_p, u_int16_t );
   72 static int              ng_ipfw_input(struct mbuf **, int, struct ip_fw_args *,
   73                             int);
   74 
   75 /* We have only one node */
   76 static node_p   fw_node;
   77 
   78 /* Netgraph node type descriptor */
   79 static struct ng_type ng_ipfw_typestruct = {
   80         .version =      NG_ABI_VERSION,
   81         .name =         NG_IPFW_NODE_TYPE,
   82         .mod_event =    ng_ipfw_mod_event,
   83         .constructor =  ng_ipfw_constructor,
   84         .shutdown =     ng_ipfw_shutdown,
   85         .newhook =      ng_ipfw_newhook,
   86         .connect =      ng_ipfw_connect,
   87         .findhook =     ng_ipfw_findhook,
   88         .rcvdata =      ng_ipfw_rcvdata,
   89         .disconnect =   ng_ipfw_disconnect,
   90 };
   91 NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
   92 MODULE_DEPEND(ng_ipfw, ipfw, 2, 2, 2);
   93 
   94 /* Information we store for each hook */
   95 struct ng_ipfw_hook_priv {
   96         hook_p          hook;
   97         u_int16_t       rulenum;
   98 };
   99 typedef struct ng_ipfw_hook_priv *hpriv_p;
  100 
  101 static int
  102 ng_ipfw_mod_event(module_t mod, int event, void *data)
  103 {
  104         int error = 0;
  105 
  106         switch (event) {
  107         case MOD_LOAD:
  108 
  109                 if (ng_ipfw_input_p != NULL) {
  110                         error = EEXIST;
  111                         break;
  112                 }
  113 
  114                 /* Setup node without any private data */
  115                 if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
  116                     != 0) {
  117                         log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
  118                         break;
  119                 };
  120 
  121                 /* Try to name node */
  122                 if (ng_name_node(fw_node, "ipfw") != 0)
  123                         log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
  124                             __func__);
  125 
  126                 /* Register hook */
  127                 ng_ipfw_input_p = ng_ipfw_input;
  128                 break;
  129 
  130         case MOD_UNLOAD:
  131                  /*
  132                   * This won't happen if a node exists.
  133                   * ng_ipfw_input_p is already cleared.
  134                   */
  135                 break;
  136 
  137         default:
  138                 error = EOPNOTSUPP;
  139                 break;
  140         }
  141 
  142         return (error);
  143 }
  144 
  145 static int
  146 ng_ipfw_constructor(node_p node)
  147 {
  148         return (EINVAL);        /* Only one node */
  149 }
  150 
  151 static int
  152 ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
  153 {
  154         hpriv_p hpriv;
  155         u_int16_t rulenum;
  156         const char *cp;
  157         char *endptr;
  158 
  159         /* Protect from leading zero */
  160         if (name[0] == '' && name[1] != '\0')
  161                 return (EINVAL);
  162 
  163         /* Check that name contains only digits */
  164         for (cp = name; *cp != '\0'; cp++)
  165                 if (!isdigit(*cp))
  166                         return (EINVAL);
  167 
  168         /* Convert it to integer */
  169         rulenum = (u_int16_t)strtol(name, &endptr, 10);
  170         if (*endptr != '\0')
  171                 return (EINVAL);
  172 
  173         /* Allocate memory for this hook's private data */
  174         hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
  175         if (hpriv== NULL)
  176                 return (ENOMEM);
  177 
  178         hpriv->hook = hook;
  179         hpriv->rulenum = rulenum;
  180 
  181         NG_HOOK_SET_PRIVATE(hook, hpriv);
  182 
  183         return(0);
  184 }
  185 
  186 /*
  187  * Set hooks into queueing mode, to avoid recursion between
  188  * netgraph layer and ip_{input,output}.
  189  */
  190 static int
  191 ng_ipfw_connect(hook_p hook)
  192 {
  193         NG_HOOK_FORCE_QUEUE(hook);
  194         return (0);
  195 }
  196 
  197 /* Look up hook by name */
  198 hook_p
  199 ng_ipfw_findhook(node_p node, const char *name)
  200 {
  201         u_int16_t n;    /* numeric representation of hook */
  202         char *endptr;
  203 
  204         n = (u_int16_t)strtol(name, &endptr, 10);
  205         if (*endptr != '\0')
  206                 return NULL;
  207         return ng_ipfw_findhook1(node, n);
  208 }
  209 
  210 /* Look up hook by rule number */
  211 static hook_p
  212 ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
  213 {
  214         hook_p  hook;
  215         hpriv_p hpriv;
  216 
  217         LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
  218                 hpriv = NG_HOOK_PRIVATE(hook);
  219                 if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum))
  220                         return (hook);
  221         }
  222 
  223         return (NULL);
  224 }
  225 
  226 
  227 static int
  228 ng_ipfw_rcvdata(hook_p hook, item_p item)
  229 {
  230         struct m_tag *tag;
  231         struct ipfw_rule_ref *r;
  232         struct mbuf *m;
  233         struct ip *ip;
  234 
  235         NGI_GET_M(item, m);
  236         NG_FREE_ITEM(item);
  237 
  238         tag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
  239         if (tag == NULL) {
  240                 NG_FREE_M(m);
  241                 return (EINVAL);        /* XXX: find smth better */
  242         };
  243 
  244         if (m->m_len < sizeof(struct ip) &&
  245             (m = m_pullup(m, sizeof(struct ip))) == NULL)
  246                 return (EINVAL);
  247 
  248         ip = mtod(m, struct ip *);
  249 
  250         r = (struct ipfw_rule_ref *)(tag + 1);
  251         if (r->info & IPFW_INFO_IN) {
  252                 switch (ip->ip_v) {
  253 #ifdef INET
  254                 case IPVERSION:
  255                         ip_input(m);
  256                         break;
  257 #endif
  258 #ifdef INET6
  259                 case IPV6_VERSION >> 4:
  260                         ip6_input(m);
  261                         break;
  262 #endif
  263                 default:
  264                         NG_FREE_M(m);
  265                         return (EINVAL);
  266                 }
  267                 return (0);
  268         } else {
  269                 switch (ip->ip_v) {
  270 #ifdef INET
  271                 case IPVERSION:
  272                         SET_HOST_IPLEN(ip);
  273                         return (ip_output(m, NULL, NULL, IP_FORWARDING,
  274                             NULL, NULL));
  275 #endif
  276 #ifdef INET6
  277                 case IPV6_VERSION >> 4:
  278                         return (ip6_output(m, NULL, NULL, 0, NULL,
  279                             NULL, NULL));
  280 #endif
  281                 default:
  282                         return (EINVAL);
  283                 }
  284         }
  285 }
  286 
  287 static int
  288 ng_ipfw_input(struct mbuf **m0, int dir, struct ip_fw_args *fwa, int tee)
  289 {
  290         struct mbuf *m;
  291         struct ip *ip;
  292         hook_p  hook;
  293         int error = 0;
  294 
  295         /*
  296          * Node must be loaded and corresponding hook must be present.
  297          */
  298         if (fw_node == NULL || 
  299            (hook = ng_ipfw_findhook1(fw_node, fwa->rule.info)) == NULL)
  300                 return (ESRCH);         /* no hook associated with this rule */
  301 
  302         /*
  303          * We have two modes: in normal mode we add a tag to packet, which is
  304          * important to return packet back to IP stack. In tee mode we make
  305          * a copy of a packet and forward it into netgraph without a tag.
  306          */
  307         if (tee == 0) {
  308                 struct m_tag *tag;
  309                 struct ipfw_rule_ref *r;
  310                 m = *m0;
  311                 *m0 = NULL;     /* it belongs now to netgraph */
  312 
  313                 tag = m_tag_alloc(MTAG_IPFW_RULE, 0, sizeof(*r),
  314                         M_NOWAIT|M_ZERO);
  315                 if (tag == NULL) {
  316                         m_freem(m);
  317                         return (ENOMEM);
  318                 }
  319                 r = (struct ipfw_rule_ref *)(tag + 1);
  320                 *r = fwa->rule;
  321                 r->info &= IPFW_ONEPASS;  /* keep this info */
  322                 r->info |= dir ? IPFW_INFO_IN : IPFW_INFO_OUT;
  323                 m_tag_prepend(m, tag);
  324 
  325         } else
  326                 if ((m = m_dup(*m0, M_DONTWAIT)) == NULL)
  327                         return (ENOMEM);        /* which is ignored */
  328 
  329         if (m->m_len < sizeof(struct ip) &&
  330             (m = m_pullup(m, sizeof(struct ip))) == NULL)
  331                 return (EINVAL);
  332 
  333         ip = mtod(m, struct ip *);
  334 
  335         NG_SEND_DATA_ONLY(error, hook, m);
  336 
  337         return (error);
  338 }
  339 
  340 static int
  341 ng_ipfw_shutdown(node_p node)
  342 {
  343 
  344         /*
  345          * After our single node has been removed,
  346          * the only thing that can be done is
  347          * 'kldunload ng_ipfw.ko'
  348          */
  349         ng_ipfw_input_p = NULL;
  350         NG_NODE_UNREF(node);
  351         return (0);
  352 }
  353 
  354 static int
  355 ng_ipfw_disconnect(hook_p hook)
  356 {
  357         const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
  358 
  359         free(hpriv, M_NETGRAPH);
  360         NG_HOOK_SET_PRIVATE(hook, NULL);
  361 
  362         return (0);
  363 }

Cache object: 2dc4bb7ac62204d17e2fb87e56185bee


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