2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/ctype.h>
41 #include <sys/errno.h>
42 #include <sys/rwlock.h>
43 #include <sys/socket.h>
44 #include <sys/syslog.h>
47 #include <net/if_var.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/in_var.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/ip_fw.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip6.h>
56 #include <netinet6/ip6_var.h>
58 #include <netpfil/ipfw/ip_fw_private.h>
60 #include <netgraph/ng_message.h>
61 #include <netgraph/ng_parse.h>
62 #include <netgraph/ng_ipfw.h>
63 #include <netgraph/netgraph.h>
65 static int ng_ipfw_mod_event(module_t mod, int event, void *data);
66 static ng_constructor_t ng_ipfw_constructor;
67 static ng_shutdown_t ng_ipfw_shutdown;
68 static ng_newhook_t ng_ipfw_newhook;
69 static ng_connect_t ng_ipfw_connect;
70 static ng_findhook_t ng_ipfw_findhook;
71 static ng_rcvdata_t ng_ipfw_rcvdata;
72 static ng_disconnect_t ng_ipfw_disconnect;
74 static hook_p ng_ipfw_findhook1(node_p, u_int16_t );
75 static int ng_ipfw_input(struct mbuf **, struct ip_fw_args *, bool);
77 /* We have only one node */
78 static node_p fw_node;
80 /* Netgraph node type descriptor */
81 static struct ng_type ng_ipfw_typestruct = {
82 .version = NG_ABI_VERSION,
83 .name = NG_IPFW_NODE_TYPE,
84 .mod_event = ng_ipfw_mod_event,
85 .constructor = ng_ipfw_constructor,
86 .shutdown = ng_ipfw_shutdown,
87 .newhook = ng_ipfw_newhook,
88 .connect = ng_ipfw_connect,
89 .findhook = ng_ipfw_findhook,
90 .rcvdata = ng_ipfw_rcvdata,
91 .disconnect = ng_ipfw_disconnect,
93 NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
94 MODULE_DEPEND(ng_ipfw, ipfw, 3, 3, 3);
96 /* Information we store for each hook */
97 struct ng_ipfw_hook_priv {
101 typedef struct ng_ipfw_hook_priv *hpriv_p;
104 ng_ipfw_mod_event(module_t mod, int event, void *data)
111 if (ng_ipfw_input_p != NULL) {
116 /* Setup node without any private data */
117 if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
119 log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
123 /* Try to name node */
124 if (ng_name_node(fw_node, "ipfw") != 0)
125 log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
129 ng_ipfw_input_p = ng_ipfw_input;
134 * This won't happen if a node exists.
135 * ng_ipfw_input_p is already cleared.
148 ng_ipfw_constructor(node_p node)
150 return (EINVAL); /* Only one node */
154 ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
161 /* Protect from leading zero */
162 if (name[0] == '0' && name[1] != '\0')
165 /* Check that name contains only digits */
166 for (cp = name; *cp != '\0'; cp++)
170 /* Convert it to integer */
171 rulenum = (u_int16_t)strtol(name, &endptr, 10);
175 /* Allocate memory for this hook's private data */
176 hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
181 hpriv->rulenum = rulenum;
183 NG_HOOK_SET_PRIVATE(hook, hpriv);
189 * Set hooks into queueing mode, to avoid recursion between
190 * netgraph layer and ip_{input,output}.
193 ng_ipfw_connect(hook_p hook)
195 NG_HOOK_FORCE_QUEUE(hook);
199 /* Look up hook by name */
201 ng_ipfw_findhook(node_p node, const char *name)
203 u_int16_t n; /* numeric representation of hook */
206 n = (u_int16_t)strtol(name, &endptr, 10);
209 return ng_ipfw_findhook1(node, n);
212 /* Look up hook by rule number */
214 ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
219 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
220 hpriv = NG_HOOK_PRIVATE(hook);
221 if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum))
229 ng_ipfw_rcvdata(hook_p hook, item_p item)
232 struct ipfw_rule_ref *r;
239 tag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
242 return (EINVAL); /* XXX: find smth better */
245 if (m->m_len < sizeof(struct ip) &&
246 (m = m_pullup(m, sizeof(struct ip))) == NULL)
249 ip = mtod(m, struct ip *);
251 r = (struct ipfw_rule_ref *)(tag + 1);
252 if (r->info & IPFW_INFO_IN) {
260 case IPV6_VERSION >> 4:
269 return (ip_output(m, NULL, NULL, IP_FORWARDING,
273 case IPV6_VERSION >> 4:
274 return (ip6_output(m, NULL, NULL, 0, NULL,
280 /* unknown IP protocol version */
282 return (EPROTONOSUPPORT);
286 ng_ipfw_input(struct mbuf **m0, struct ip_fw_args *fwa, bool tee)
293 * Node must be loaded and corresponding hook must be present.
295 if (fw_node == NULL ||
296 (hook = ng_ipfw_findhook1(fw_node, fwa->rule.info)) == NULL)
297 return (ESRCH); /* no hook associated with this rule */
300 * We have two modes: in normal mode we add a tag to packet, which is
301 * important to return packet back to IP stack. In tee mode we make
302 * a copy of a packet and forward it into netgraph without a tag.
306 struct ipfw_rule_ref *r;
308 *m0 = NULL; /* it belongs now to netgraph */
310 tag = m_tag_alloc(MTAG_IPFW_RULE, 0, sizeof(*r),
316 r = (struct ipfw_rule_ref *)(tag + 1);
318 r->info &= IPFW_ONEPASS; /* keep this info */
319 r->info |= (fwa->flags & IPFW_ARGS_IN) ?
320 IPFW_INFO_IN : IPFW_INFO_OUT;
321 m_tag_prepend(m, tag);
324 if ((m = m_dup(*m0, M_NOWAIT)) == NULL)
325 return (ENOMEM); /* which is ignored */
327 if (m->m_len < sizeof(struct ip) &&
328 (m = m_pullup(m, sizeof(struct ip))) == NULL)
331 NG_SEND_DATA_ONLY(error, hook, m);
337 ng_ipfw_shutdown(node_p node)
341 * After our single node has been removed,
342 * the only thing that can be done is
343 * 'kldunload ng_ipfw.ko'
345 ng_ipfw_input_p = NULL;
351 ng_ipfw_disconnect(hook_p hook)
353 const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
355 free(hpriv, M_NETGRAPH);
356 NG_HOOK_SET_PRIVATE(hook, NULL);