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>
34 #include <sys/malloc.h>
35 #include <sys/ctype.h>
36 #include <sys/errno.h>
37 #include <sys/rwlock.h>
38 #include <sys/socket.h>
39 #include <sys/syslog.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/in_var.h>
46 #include <netinet/ip_fw.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_var.h>
50 #include <netgraph/ng_message.h>
51 #include <netgraph/ng_parse.h>
52 #include <netgraph/ng_ipfw.h>
53 #include <netgraph/netgraph.h>
55 static int ng_ipfw_mod_event(module_t mod, int event, void *data);
56 static ng_constructor_t ng_ipfw_constructor;
57 static ng_shutdown_t ng_ipfw_shutdown;
58 static ng_newhook_t ng_ipfw_newhook;
59 static ng_connect_t ng_ipfw_connect;
60 static ng_findhook_t ng_ipfw_findhook;
61 static ng_rcvdata_t ng_ipfw_rcvdata;
62 static ng_disconnect_t ng_ipfw_disconnect;
64 static hook_p ng_ipfw_findhook1(node_p, u_int16_t );
65 static int ng_ipfw_input(struct mbuf **, int, struct ip_fw_args *,
68 /* We have only one node */
69 static node_p fw_node;
71 /* Netgraph node type descriptor */
72 static struct ng_type ng_ipfw_typestruct = {
73 .version = NG_ABI_VERSION,
74 .name = NG_IPFW_NODE_TYPE,
75 .mod_event = ng_ipfw_mod_event,
76 .constructor = ng_ipfw_constructor,
77 .shutdown = ng_ipfw_shutdown,
78 .newhook = ng_ipfw_newhook,
79 .connect = ng_ipfw_connect,
80 .findhook = ng_ipfw_findhook,
81 .rcvdata = ng_ipfw_rcvdata,
82 .disconnect = ng_ipfw_disconnect,
84 NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
85 MODULE_DEPEND(ng_ipfw, ipfw, 2, 2, 2);
87 /* Information we store for each hook */
88 struct ng_ipfw_hook_priv {
92 typedef struct ng_ipfw_hook_priv *hpriv_p;
95 ng_ipfw_mod_event(module_t mod, int event, void *data)
102 if (ng_ipfw_input_p != NULL) {
107 /* Setup node without any private data */
108 if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
110 log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
114 /* Try to name node */
115 if (ng_name_node(fw_node, "ipfw") != 0)
116 log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
120 ng_ipfw_input_p = ng_ipfw_input;
125 * This won't happen if a node exists.
126 * ng_ipfw_input_p is already cleared.
139 ng_ipfw_constructor(node_p node)
141 return (EINVAL); /* Only one node */
145 ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
152 /* Protect from leading zero */
153 if (name[0] == '0' && name[1] != '\0')
156 /* Check that name contains only digits */
157 for (cp = name; *cp != '\0'; cp++)
161 /* Convert it to integer */
162 rulenum = (u_int16_t)strtol(name, &endptr, 10);
166 /* Allocate memory for this hook's private data */
167 hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
172 hpriv->rulenum = rulenum;
174 NG_HOOK_SET_PRIVATE(hook, hpriv);
180 * Set hooks into queueing mode, to avoid recursion between
181 * netgraph layer and ip_{input,output}.
184 ng_ipfw_connect(hook_p hook)
186 NG_HOOK_FORCE_QUEUE(hook);
190 /* Look up hook by name */
192 ng_ipfw_findhook(node_p node, const char *name)
194 u_int16_t n; /* numeric representation of hook */
197 n = (u_int16_t)strtol(name, &endptr, 10);
200 return ng_ipfw_findhook1(node, n);
203 /* Look up hook by rule number */
205 ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
210 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
211 hpriv = NG_HOOK_PRIVATE(hook);
212 if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum))
221 ng_ipfw_rcvdata(hook_p hook, item_p item)
223 struct ng_ipfw_tag *ngit;
229 if ((ngit = (struct ng_ipfw_tag *)m_tag_locate(m, NGM_IPFW_COOKIE, 0,
232 return (EINVAL); /* XXX: find smth better */
240 if (m->m_len < sizeof(struct ip) &&
241 (m = m_pullup(m, sizeof(struct ip))) == NULL)
244 ip = mtod(m, struct ip *);
246 ip->ip_len = ntohs(ip->ip_len);
247 ip->ip_off = ntohs(ip->ip_off);
249 return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
255 panic("ng_ipfw_rcvdata: bad dir %u", ngit->dir);
263 ng_ipfw_input(struct mbuf **m0, int dir, struct ip_fw_args *fwa, int tee)
266 struct ng_ipfw_tag *ngit;
272 * Node must be loaded and corresponding hook must be present.
274 if (fw_node == NULL ||
275 (hook = ng_ipfw_findhook1(fw_node, fwa->cookie)) == NULL) {
278 return (ESRCH); /* no hook associated with this rule */
282 * We have two modes: in normal mode we add a tag to packet, which is
283 * important to return packet back to IP stack. In tee mode we make
284 * a copy of a packet and forward it into netgraph without a tag.
288 *m0 = NULL; /* it belongs now to netgraph */
290 if ((ngit = (struct ng_ipfw_tag *)m_tag_alloc(NGM_IPFW_COOKIE,
291 0, TAGSIZ, M_NOWAIT|M_ZERO)) == NULL) {
295 ngit->rule = fwa->rule;
296 ngit->rule_id = fwa->rule_id;
297 ngit->chain_id = fwa->chain_id;
299 ngit->ifp = fwa->oif;
300 m_tag_prepend(m, &ngit->mt);
303 if ((m = m_dup(*m0, M_DONTWAIT)) == NULL)
304 return (ENOMEM); /* which is ignored */
306 if (m->m_len < sizeof(struct ip) &&
307 (m = m_pullup(m, sizeof(struct ip))) == NULL)
310 ip = mtod(m, struct ip *);
311 ip->ip_len = htons(ip->ip_len);
312 ip->ip_off = htons(ip->ip_off);
314 NG_SEND_DATA_ONLY(error, hook, m);
320 ng_ipfw_shutdown(node_p node)
324 * After our single node has been removed,
325 * the only thing that can be done is
326 * 'kldunload ng_ipfw.ko'
328 ng_ipfw_input_p = NULL;
334 ng_ipfw_disconnect(hook_p hook)
336 const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
338 free(hpriv, M_NETGRAPH);
339 NG_HOOK_SET_PRIVATE(hook, NULL);