kernel - Refactor vnode_free_list, vnode reuse algorithm
[dragonfly.git] / sys / netgraph7 / ng_ipfw.c
blob0c94d4fa721cc597a1c595605eebca4887397e08
1 /*-
2 * Copyright 2005, Gleb Smirnoff <glebius@FreeBSD.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/netgraph/ng_ipfw.c,v 1.9 2006/02/14 15:22:24 ru Exp $
27 * $DragonFly: src/sys/netgraph7/ng_ipfw.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/mbuf.h>
34 #include <sys/malloc.h>
35 #include <sys/ctype.h>
36 #include <sys/errno.h>
37 #include <sys/socket.h>
38 #include <sys/syslog.h>
40 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/in_var.h>
45 #include <netinet/ip_fw.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_var.h>
49 #include "ng_message.h"
50 #include "ng_parse.h"
51 #include "ng_ipfw.h"
52 #include "netgraph.h"
54 static int ng_ipfw_mod_event(module_t mod, int event, void *data);
55 static ng_constructor_t ng_ipfw_constructor;
56 static ng_shutdown_t ng_ipfw_shutdown;
57 static ng_newhook_t ng_ipfw_newhook;
58 static ng_connect_t ng_ipfw_connect;
59 static ng_findhook_t ng_ipfw_findhook;
60 static ng_rcvdata_t ng_ipfw_rcvdata;
61 static ng_disconnect_t ng_ipfw_disconnect;
63 static hook_p ng_ipfw_findhook1(node_p, u_int16_t );
64 static int ng_ipfw_input(struct mbuf **, int, struct ip_fw_args *,
65 int);
67 /* We have only one node */
68 static node_p fw_node;
70 /* Netgraph node type descriptor */
71 static struct ng_type ng_ipfw_typestruct = {
72 .version = NG_ABI_VERSION,
73 .name = NG_IPFW_NODE_TYPE,
74 .mod_event = ng_ipfw_mod_event,
75 .constructor = ng_ipfw_constructor,
76 .shutdown = ng_ipfw_shutdown,
77 .newhook = ng_ipfw_newhook,
78 .connect = ng_ipfw_connect,
79 .findhook = ng_ipfw_findhook,
80 .rcvdata = ng_ipfw_rcvdata,
81 .disconnect = ng_ipfw_disconnect,
83 NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
84 MODULE_DEPEND(ng_ipfw, ipfw, 2, 2, 2);
86 /* Information we store for each hook */
87 struct ng_ipfw_hook_priv {
88 hook_p hook;
89 u_int16_t rulenum;
91 typedef struct ng_ipfw_hook_priv *hpriv_p;
93 static int
94 ng_ipfw_mod_event(module_t mod, int event, void *data)
96 int error = 0;
98 switch (event) {
99 case MOD_LOAD:
101 if (ng_ipfw_input_p != NULL) {
102 error = EEXIST;
103 break;
106 /* Setup node without any private data */
107 if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
108 != 0) {
109 log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
110 break;
113 /* Try to name node */
114 if (ng_name_node(fw_node, "ipfw") != 0)
115 log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
116 __func__);
118 /* Register hook */
119 ng_ipfw_input_p = ng_ipfw_input;
120 break;
122 case MOD_UNLOAD:
124 * This won't happen if a node exists.
125 * ng_ipfw_input_p is already cleared.
127 break;
129 default:
130 error = EOPNOTSUPP;
131 break;
134 return (error);
137 static int
138 ng_ipfw_constructor(node_p node)
140 return (EINVAL); /* Only one node */
143 static int
144 ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
146 hpriv_p hpriv;
147 u_int16_t rulenum;
148 const char *cp;
149 char *endptr;
151 /* Protect from leading zero */
152 if (name[0] == '0' && name[1] != '\0')
153 return (EINVAL);
155 /* Check that name contains only digits */
156 for (cp = name; *cp != '\0'; cp++)
157 if (!isdigit(*cp))
158 return (EINVAL);
160 /* Convert it to integer */
161 rulenum = (u_int16_t)strtol(name, &endptr, 10);
162 if (*endptr != '\0')
163 return (EINVAL);
165 /* Allocate memory for this hook's private data */
166 MALLOC(hpriv, hpriv_p, sizeof(*hpriv), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
167 if (hpriv== NULL)
168 return (ENOMEM);
170 hpriv->hook = hook;
171 hpriv->rulenum = rulenum;
173 NG_HOOK_SET_PRIVATE(hook, hpriv);
175 return(0);
179 * Set hooks into queueing mode, to avoid recursion between
180 * netgraph layer and ip_{input,output}.
182 static int
183 ng_ipfw_connect(hook_p hook)
185 NG_HOOK_FORCE_QUEUE(hook);
186 return (0);
189 /* Look up hook by name */
190 hook_p
191 ng_ipfw_findhook(node_p node, const char *name)
193 u_int16_t n; /* numeric representation of hook */
194 char *endptr;
196 n = (u_int16_t)strtol(name, &endptr, 10);
197 if (*endptr != '\0')
198 return NULL;
199 return ng_ipfw_findhook1(node, n);
202 /* Look up hook by rule number */
203 static hook_p
204 ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
206 hook_p hook;
207 hpriv_p hpriv;
209 LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
210 hpriv = NG_HOOK_PRIVATE(hook);
211 if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum))
212 return (hook);
215 return (NULL);
219 static int
220 ng_ipfw_rcvdata(hook_p hook, item_p item)
222 struct ng_ipfw_tag *ngit;
223 struct mbuf *m;
225 NGI_GET_M(item, m);
226 NG_FREE_ITEM(item);
228 if ((ngit = (struct ng_ipfw_tag *)m_tag_locate(m, NGM_IPFW_COOKIE, 0,
229 NULL)) == NULL) {
230 NG_FREE_M(m);
231 return (EINVAL); /* XXX: find smth better */
234 switch (ngit->dir) {
235 case NG_IPFW_OUT:
237 struct ip *ip;
239 if (m->m_len < sizeof(struct ip) &&
240 (m = m_pullup(m, sizeof(struct ip))) == NULL)
241 return (EINVAL);
243 ip = mtod(m, struct ip *);
245 ip->ip_len = ntohs(ip->ip_len);
246 ip->ip_off = ntohs(ip->ip_off);
248 return ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
250 case NG_IPFW_IN:
251 ip_input(m);
252 return (0);
253 default:
254 panic("ng_ipfw_rcvdata: bad dir %u", ngit->dir);
257 /* not reached */
258 return (0);
261 static int
262 ng_ipfw_input(struct mbuf **m0, int dir, struct ip_fw_args *fwa, int tee)
264 struct mbuf *m;
265 struct ng_ipfw_tag *ngit;
266 struct ip *ip;
267 hook_p hook;
268 int error = 0;
271 * Node must be loaded and corresponding hook must be present.
273 if (fw_node == NULL ||
274 (hook = ng_ipfw_findhook1(fw_node, fwa->cookie)) == NULL) {
275 if (tee == 0)
276 m_freem(*m0);
277 return (ESRCH); /* no hook associated with this rule */
281 * We have two modes: in normal mode we add a tag to packet, which is
282 * important to return packet back to IP stack. In tee mode we make
283 * a copy of a packet and forward it into netgraph without a tag.
285 if (tee == 0) {
286 m = *m0;
287 *m0 = NULL; /* it belongs now to netgraph */
289 if ((ngit = (struct ng_ipfw_tag *)m_tag_alloc(NGM_IPFW_COOKIE,
290 0, TAGSIZ, MB_DONTWAIT)) == NULL) {
291 m_freem(m);
292 return (ENOMEM);
294 ngit->rule = fwa->rule;
295 ngit->dir = dir;
296 ngit->ifp = fwa->oif;
297 m_tag_prepend(m, &ngit->mt);
299 } else
300 if ((m = m_dup(*m0, MB_DONTWAIT)) == NULL)
301 return (ENOMEM); /* which is ignored */
303 if (m->m_len < sizeof(struct ip) &&
304 (m = m_pullup(m, sizeof(struct ip))) == NULL)
305 return (EINVAL);
307 ip = mtod(m, struct ip *);
308 ip->ip_len = htons(ip->ip_len);
309 ip->ip_off = htons(ip->ip_off);
311 NG_SEND_DATA_ONLY(error, hook, m);
313 return (error);
316 static int
317 ng_ipfw_shutdown(node_p node)
321 * After our single node has been removed,
322 * the only thing that can be done is
323 * 'kldunload ng_ipfw.ko'
325 ng_ipfw_input_p = NULL;
326 NG_NODE_UNREF(node);
327 return (0);
330 static int
331 ng_ipfw_disconnect(hook_p hook)
333 const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
335 FREE(hpriv, M_NETGRAPH);
336 NG_HOOK_SET_PRIVATE(hook, NULL);
338 return (0);