2 * Copyright 2005, Gleb Smirnoff <glebius@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/ctype.h>
35 #include <sys/errno.h>
36 #include <sys/socket.h>
37 #include <sys/syslog.h>
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/in_var.h>
44 #include <netinet/ip_fw.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_var.h>
48 #include <netgraph/ng_message.h>
49 #include <netgraph/ng_parse.h>
50 #include <netgraph/ng_ipfw.h>
51 #include <netgraph/netgraph.h>
53 static int ng_ipfw_mod_event(module_t mod, int event, void *data);
54 static ng_constructor_t ng_ipfw_constructor;
55 static ng_shutdown_t ng_ipfw_shutdown;
56 static ng_newhook_t ng_ipfw_newhook;
57 static ng_connect_t ng_ipfw_connect;
58 static ng_findhook_t ng_ipfw_findhook;
59 static ng_rcvdata_t ng_ipfw_rcvdata;
60 static ng_disconnect_t ng_ipfw_disconnect;
62 static hook_p ng_ipfw_findhook1(node_p, u_int16_t );
63 static int ng_ipfw_input(struct mbuf **, int, struct ip_fw_args *,
66 /* We have only one node */
67 static node_p fw_node;
69 /* Netgraph node type descriptor */
70 static struct ng_type ng_ipfw_typestruct = {
71 .version = NG_ABI_VERSION,
72 .name = NG_IPFW_NODE_TYPE,
73 .mod_event = ng_ipfw_mod_event,
74 .constructor = ng_ipfw_constructor,
75 .shutdown = ng_ipfw_shutdown,
76 .newhook = ng_ipfw_newhook,
77 .connect = ng_ipfw_connect,
78 .findhook = ng_ipfw_findhook,
79 .rcvdata = ng_ipfw_rcvdata,
80 .disconnect = ng_ipfw_disconnect,
82 NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
83 MODULE_DEPEND(ng_ipfw, ipfw, 2, 2, 2);
85 /* Information we store for each hook */
86 struct ng_ipfw_hook_priv {
90 typedef struct ng_ipfw_hook_priv *hpriv_p;
93 ng_ipfw_mod_event(module_t mod, int event, void *data)
100 if (ng_ipfw_input_p != NULL) {
105 /* Setup node without any private data */
106 if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
108 log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
112 /* Try to name node */
113 if (ng_name_node(fw_node, "ipfw") != 0)
114 log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
118 ng_ipfw_input_p = ng_ipfw_input;
123 * This won't happen if a node exists.
124 * ng_ipfw_input_p is already cleared.
137 ng_ipfw_constructor(node_p node)
139 return (EINVAL); /* Only one node */
143 ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
150 /* Protect from leading zero */
151 if (name[0] == '0' && name[1] != '\0')
154 /* Check that name contains only digits */
155 for (cp = name; *cp != '\0'; cp++)
159 /* Convert it to integer */
160 rulenum = (u_int16_t)strtol(name, &endptr, 10);
164 /* Allocate memory for this hook's private data */
165 MALLOC(hpriv, hpriv_p, sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
170 hpriv->rulenum = rulenum;
172 NG_HOOK_SET_PRIVATE(hook, hpriv);
178 * Set hooks into queueing mode, to avoid recursion between
179 * netgraph layer and ip_{input,output}.
182 ng_ipfw_connect(hook_p hook)
184 NG_HOOK_FORCE_QUEUE(hook);
188 /* Look up hook by name */
190 ng_ipfw_findhook(node_p node, const char *name)
192 u_int16_t n; /* numeric representation of hook */
195 n = (u_int16_t)strtol(name, &endptr, 10);
198 return ng_ipfw_findhook1(node, n);
201 /* Look up hook by rule number */
203 ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
208 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
209 hpriv = NG_HOOK_PRIVATE(hook);
210 if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum))
219 ng_ipfw_rcvdata(hook_p hook, item_p item)
221 struct ng_ipfw_tag *ngit;
227 if ((ngit = (struct ng_ipfw_tag *)m_tag_locate(m, NGM_IPFW_COOKIE, 0,
230 return (EINVAL); /* XXX: find smth better */
238 if (m->m_len < sizeof(struct ip) &&
239 (m = m_pullup(m, sizeof(struct ip))) == NULL)
242 ip = mtod(m, struct ip *);
244 ip->ip_len = ntohs(ip->ip_len);
245 ip->ip_off = ntohs(ip->ip_off);
247 return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
253 panic("ng_ipfw_rcvdata: bad dir %u", ngit->dir);
261 ng_ipfw_input(struct mbuf **m0, int dir, struct ip_fw_args *fwa, int tee)
264 struct ng_ipfw_tag *ngit;
270 * Node must be loaded and corresponding hook must be present.
272 if (fw_node == NULL ||
273 (hook = ng_ipfw_findhook1(fw_node, fwa->cookie)) == NULL) {
276 return (ESRCH); /* no hook associated with this rule */
280 * We have two modes: in normal mode we add a tag to packet, which is
281 * important to return packet back to IP stack. In tee mode we make
282 * a copy of a packet and forward it into netgraph without a tag.
286 *m0 = NULL; /* it belongs now to netgraph */
288 if ((ngit = (struct ng_ipfw_tag *)m_tag_alloc(NGM_IPFW_COOKIE,
289 0, TAGSIZ, M_NOWAIT|M_ZERO)) == NULL) {
293 ngit->rule = fwa->rule;
295 ngit->ifp = fwa->oif;
296 m_tag_prepend(m, &ngit->mt);
299 if ((m = m_dup(*m0, M_DONTWAIT)) == NULL)
300 return (ENOMEM); /* which is ignored */
302 if (m->m_len < sizeof(struct ip) &&
303 (m = m_pullup(m, sizeof(struct ip))) == NULL)
306 ip = mtod(m, struct ip *);
307 ip->ip_len = htons(ip->ip_len);
308 ip->ip_off = htons(ip->ip_off);
310 NG_SEND_DATA_ONLY(error, hook, m);
316 ng_ipfw_shutdown(node_p node)
320 * After our single node has been removed,
321 * the only thing that can be done is
322 * 'kldunload ng_ipfw.ko'
324 ng_ipfw_input_p = NULL;
330 ng_ipfw_disconnect(hook_p hook)
332 const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
334 FREE(hpriv, M_NETGRAPH);
335 NG_HOOK_SET_PRIVATE(hook, NULL);