2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright 2005, Gleb Smirnoff <glebius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "opt_inet6.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.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>
45 #include <net/if_var.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/ip_fw.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
56 #include <netpfil/ipfw/ip_fw_private.h>
58 #include <netgraph/ng_message.h>
59 #include <netgraph/ng_parse.h>
60 #include <netgraph/ng_ipfw.h>
61 #include <netgraph/netgraph.h>
63 static int ng_ipfw_mod_event(module_t mod, int event, void *data);
64 static ng_constructor_t ng_ipfw_constructor;
65 static ng_shutdown_t ng_ipfw_shutdown;
66 static ng_newhook_t ng_ipfw_newhook;
67 static ng_connect_t ng_ipfw_connect;
68 static ng_findhook_t ng_ipfw_findhook;
69 static ng_rcvdata_t ng_ipfw_rcvdata;
70 static ng_disconnect_t ng_ipfw_disconnect;
72 static hook_p ng_ipfw_findhook1(node_p, u_int16_t );
73 static int ng_ipfw_input(struct mbuf **, struct ip_fw_args *, bool);
75 /* We have only one node */
76 static node_p fw_node;
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,
91 NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
92 MODULE_DEPEND(ng_ipfw, ipfw, 3, 3, 3);
94 /* Information we store for each hook */
95 struct ng_ipfw_hook_priv {
99 typedef struct ng_ipfw_hook_priv *hpriv_p;
102 ng_ipfw_mod_event(module_t mod, int event, void *data)
109 if (ng_ipfw_input_p != NULL) {
114 /* Setup node without any private data */
115 if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
117 log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
121 /* Try to name node */
122 if (ng_name_node(fw_node, "ipfw") != 0)
123 log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
127 ng_ipfw_input_p = ng_ipfw_input;
132 * This won't happen if a node exists.
133 * ng_ipfw_input_p is already cleared.
146 ng_ipfw_constructor(node_p node)
148 return (EINVAL); /* Only one node */
152 ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
159 /* Protect from leading zero */
160 if (name[0] == '0' && name[1] != '\0')
163 /* Check that name contains only digits */
164 for (cp = name; *cp != '\0'; cp++)
168 /* Convert it to integer */
169 rulenum = (u_int16_t)strtol(name, &endptr, 10);
173 /* Allocate memory for this hook's private data */
174 hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
179 hpriv->rulenum = rulenum;
181 NG_HOOK_SET_PRIVATE(hook, hpriv);
187 * Set hooks into queueing mode, to avoid recursion between
188 * netgraph layer and ip_{input,output}.
191 ng_ipfw_connect(hook_p hook)
193 NG_HOOK_FORCE_QUEUE(hook);
197 /* Look up hook by name */
199 ng_ipfw_findhook(node_p node, const char *name)
201 u_int16_t n; /* numeric representation of hook */
204 n = (u_int16_t)strtol(name, &endptr, 10);
207 return ng_ipfw_findhook1(node, n);
210 /* Look up hook by rule number */
212 ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
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))
227 ng_ipfw_rcvdata(hook_p hook, item_p item)
230 struct ipfw_rule_ref *r;
237 tag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
240 return (EINVAL); /* XXX: find smth better */
243 if (m->m_len < sizeof(struct ip) &&
244 (m = m_pullup(m, sizeof(struct ip))) == NULL)
247 ip = mtod(m, struct ip *);
249 r = (struct ipfw_rule_ref *)(tag + 1);
250 if (r->info & IPFW_INFO_IN) {
258 case IPV6_VERSION >> 4:
267 return (ip_output(m, NULL, NULL, IP_FORWARDING,
271 case IPV6_VERSION >> 4:
272 return (ip6_output(m, NULL, NULL, 0, NULL,
278 /* unknown IP protocol version */
280 return (EPROTONOSUPPORT);
284 ng_ipfw_input(struct mbuf **m0, struct ip_fw_args *fwa, bool tee)
291 * Node must be loaded and corresponding hook must be present.
293 if (fw_node == NULL ||
294 (hook = ng_ipfw_findhook1(fw_node, fwa->rule.info)) == NULL)
295 return (ESRCH); /* no hook associated with this rule */
298 * We have two modes: in normal mode we add a tag to packet, which is
299 * important to return packet back to IP stack. In tee mode we make
300 * a copy of a packet and forward it into netgraph without a tag.
304 struct ipfw_rule_ref *r;
306 *m0 = NULL; /* it belongs now to netgraph */
308 tag = m_tag_alloc(MTAG_IPFW_RULE, 0, sizeof(*r),
314 r = (struct ipfw_rule_ref *)(tag + 1);
316 r->info &= IPFW_ONEPASS; /* keep this info */
317 r->info |= (fwa->flags & IPFW_ARGS_IN) ?
318 IPFW_INFO_IN : IPFW_INFO_OUT;
319 m_tag_prepend(m, tag);
322 if ((m = m_dup(*m0, M_NOWAIT)) == NULL)
323 return (ENOMEM); /* which is ignored */
325 if (m->m_len < sizeof(struct ip) &&
326 (m = m_pullup(m, sizeof(struct ip))) == NULL)
329 NG_SEND_DATA_ONLY(error, hook, m);
335 ng_ipfw_shutdown(node_p node)
339 * After our single node has been removed,
340 * the only thing that can be done is
341 * 'kldunload ng_ipfw.ko'
343 ng_ipfw_input_p = NULL;
349 ng_ipfw_disconnect(hook_p hook)
351 const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
353 free(hpriv, M_NETGRAPH);
354 NG_HOOK_SET_PRIVATE(hook, NULL);