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 **, int, struct ip_fw_args *,
78 /* We have only one node */
79 static node_p fw_node;
81 /* Netgraph node type descriptor */
82 static struct ng_type ng_ipfw_typestruct = {
83 .version = NG_ABI_VERSION,
84 .name = NG_IPFW_NODE_TYPE,
85 .mod_event = ng_ipfw_mod_event,
86 .constructor = ng_ipfw_constructor,
87 .shutdown = ng_ipfw_shutdown,
88 .newhook = ng_ipfw_newhook,
89 .connect = ng_ipfw_connect,
90 .findhook = ng_ipfw_findhook,
91 .rcvdata = ng_ipfw_rcvdata,
92 .disconnect = ng_ipfw_disconnect,
94 NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
95 MODULE_DEPEND(ng_ipfw, ipfw, 3, 3, 3);
97 /* Information we store for each hook */
98 struct ng_ipfw_hook_priv {
102 typedef struct ng_ipfw_hook_priv *hpriv_p;
105 ng_ipfw_mod_event(module_t mod, int event, void *data)
112 if (ng_ipfw_input_p != NULL) {
117 /* Setup node without any private data */
118 if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
120 log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
124 /* Try to name node */
125 if (ng_name_node(fw_node, "ipfw") != 0)
126 log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
130 ng_ipfw_input_p = ng_ipfw_input;
135 * This won't happen if a node exists.
136 * ng_ipfw_input_p is already cleared.
149 ng_ipfw_constructor(node_p node)
151 return (EINVAL); /* Only one node */
155 ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
162 /* Protect from leading zero */
163 if (name[0] == '0' && name[1] != '\0')
166 /* Check that name contains only digits */
167 for (cp = name; *cp != '\0'; cp++)
171 /* Convert it to integer */
172 rulenum = (u_int16_t)strtol(name, &endptr, 10);
176 /* Allocate memory for this hook's private data */
177 hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
182 hpriv->rulenum = rulenum;
184 NG_HOOK_SET_PRIVATE(hook, hpriv);
190 * Set hooks into queueing mode, to avoid recursion between
191 * netgraph layer and ip_{input,output}.
194 ng_ipfw_connect(hook_p hook)
196 NG_HOOK_FORCE_QUEUE(hook);
200 /* Look up hook by name */
202 ng_ipfw_findhook(node_p node, const char *name)
204 u_int16_t n; /* numeric representation of hook */
207 n = (u_int16_t)strtol(name, &endptr, 10);
210 return ng_ipfw_findhook1(node, n);
213 /* Look up hook by rule number */
215 ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
220 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
221 hpriv = NG_HOOK_PRIVATE(hook);
222 if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum))
231 ng_ipfw_rcvdata(hook_p hook, item_p item)
234 struct ipfw_rule_ref *r;
241 tag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
244 return (EINVAL); /* XXX: find smth better */
247 if (m->m_len < sizeof(struct ip) &&
248 (m = m_pullup(m, sizeof(struct ip))) == NULL)
251 ip = mtod(m, struct ip *);
253 r = (struct ipfw_rule_ref *)(tag + 1);
254 if (r->info & IPFW_INFO_IN) {
262 case IPV6_VERSION >> 4:
271 return (ip_output(m, NULL, NULL, IP_FORWARDING,
275 case IPV6_VERSION >> 4:
276 return (ip6_output(m, NULL, NULL, 0, NULL,
282 /* unknown IP protocol version */
284 return (EPROTONOSUPPORT);
288 ng_ipfw_input(struct mbuf **m0, int dir, struct ip_fw_args *fwa, int tee)
296 * Node must be loaded and corresponding hook must be present.
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 */
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.
309 struct ipfw_rule_ref *r;
311 *m0 = NULL; /* it belongs now to netgraph */
313 tag = m_tag_alloc(MTAG_IPFW_RULE, 0, sizeof(*r),
319 r = (struct ipfw_rule_ref *)(tag + 1);
321 r->info &= IPFW_ONEPASS; /* keep this info */
322 r->info |= dir ? IPFW_INFO_IN : IPFW_INFO_OUT;
323 m_tag_prepend(m, tag);
326 if ((m = m_dup(*m0, M_NOWAIT)) == NULL)
327 return (ENOMEM); /* which is ignored */
329 if (m->m_len < sizeof(struct ip) &&
330 (m = m_pullup(m, sizeof(struct ip))) == NULL)
333 ip = mtod(m, struct ip *);
335 NG_SEND_DATA_ONLY(error, hook, m);
341 ng_ipfw_shutdown(node_p node)
345 * After our single node has been removed,
346 * the only thing that can be done is
347 * 'kldunload ng_ipfw.ko'
349 ng_ipfw_input_p = NULL;
355 ng_ipfw_disconnect(hook_p hook)
357 const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
359 free(hpriv, M_NETGRAPH);
360 NG_HOOK_SET_PRIVATE(hook, NULL);