xref: /freebsd/sys/netgraph/ng_ipfw.c (revision 8c6f96299610db898ba77b41f97d075bd0d82a5f)
1670742a1SGleb Smirnoff /*-
2670742a1SGleb Smirnoff  * Copyright 2005, Gleb Smirnoff <glebius@FreeBSD.org>
3670742a1SGleb Smirnoff  * All rights reserved.
4670742a1SGleb Smirnoff  *
5670742a1SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
6670742a1SGleb Smirnoff  * modification, are permitted provided that the following conditions
7670742a1SGleb Smirnoff  * are met:
8670742a1SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
9670742a1SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
10670742a1SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
11670742a1SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
12670742a1SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
13670742a1SGleb Smirnoff  *
14670742a1SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15670742a1SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16670742a1SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17670742a1SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18670742a1SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19670742a1SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20670742a1SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21670742a1SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22670742a1SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23670742a1SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24670742a1SGleb Smirnoff  * SUCH DAMAGE.
25670742a1SGleb Smirnoff  *
26670742a1SGleb Smirnoff  * $FreeBSD$
27670742a1SGleb Smirnoff  */
28670742a1SGleb Smirnoff 
29670742a1SGleb Smirnoff #include <sys/param.h>
30670742a1SGleb Smirnoff #include <sys/systm.h>
31670742a1SGleb Smirnoff #include <sys/kernel.h>
32670742a1SGleb Smirnoff #include <sys/mbuf.h>
33670742a1SGleb Smirnoff #include <sys/malloc.h>
34670742a1SGleb Smirnoff #include <sys/ctype.h>
35670742a1SGleb Smirnoff #include <sys/errno.h>
36670742a1SGleb Smirnoff #include <sys/socket.h>
37670742a1SGleb Smirnoff #include <sys/syslog.h>
38670742a1SGleb Smirnoff 
39670742a1SGleb Smirnoff #include <net/if.h>
40670742a1SGleb Smirnoff 
41670742a1SGleb Smirnoff #include <netinet/in.h>
42670742a1SGleb Smirnoff #include <netinet/in_systm.h>
43670742a1SGleb Smirnoff #include <netinet/in_var.h>
44670742a1SGleb Smirnoff #include <netinet/ip_fw.h>
45670742a1SGleb Smirnoff #include <netinet/ip.h>
46670742a1SGleb Smirnoff #include <netinet/ip_var.h>
47670742a1SGleb Smirnoff 
48670742a1SGleb Smirnoff #include <netgraph/ng_message.h>
49670742a1SGleb Smirnoff #include <netgraph/ng_parse.h>
50670742a1SGleb Smirnoff #include <netgraph/ng_ipfw.h>
51670742a1SGleb Smirnoff #include <netgraph/netgraph.h>
52670742a1SGleb Smirnoff 
53670742a1SGleb Smirnoff static int		ng_ipfw_mod_event(module_t mod, int event, void *data);
54670742a1SGleb Smirnoff static ng_constructor_t	ng_ipfw_constructor;
55670742a1SGleb Smirnoff static ng_shutdown_t	ng_ipfw_shutdown;
56670742a1SGleb Smirnoff static ng_newhook_t	ng_ipfw_newhook;
57670742a1SGleb Smirnoff static ng_connect_t	ng_ipfw_connect;
58670742a1SGleb Smirnoff static ng_findhook_t	ng_ipfw_findhook;
59670742a1SGleb Smirnoff static ng_rcvdata_t	ng_ipfw_rcvdata;
60670742a1SGleb Smirnoff static ng_disconnect_t	ng_ipfw_disconnect;
61670742a1SGleb Smirnoff 
62670742a1SGleb Smirnoff static hook_p		ng_ipfw_findhook1(node_p, u_int16_t );
63670742a1SGleb Smirnoff static int		ng_ipfw_input(struct mbuf **, int, struct ip_fw_args *,
64670742a1SGleb Smirnoff 			    int);
65670742a1SGleb Smirnoff 
66670742a1SGleb Smirnoff /* We have only one node */
67670742a1SGleb Smirnoff static node_p	fw_node;
68670742a1SGleb Smirnoff 
69670742a1SGleb Smirnoff /* Netgraph node type descriptor */
70670742a1SGleb Smirnoff static struct ng_type ng_ipfw_typestruct = {
71670742a1SGleb Smirnoff 	.version =	NG_ABI_VERSION,
72670742a1SGleb Smirnoff 	.name =		NG_IPFW_NODE_TYPE,
73670742a1SGleb Smirnoff 	.mod_event =	ng_ipfw_mod_event,
74670742a1SGleb Smirnoff 	.constructor =	ng_ipfw_constructor,
75670742a1SGleb Smirnoff 	.shutdown =	ng_ipfw_shutdown,
76670742a1SGleb Smirnoff 	.newhook =	ng_ipfw_newhook,
77670742a1SGleb Smirnoff 	.connect =	ng_ipfw_connect,
78670742a1SGleb Smirnoff 	.findhook =	ng_ipfw_findhook,
79670742a1SGleb Smirnoff 	.rcvdata =	ng_ipfw_rcvdata,
80670742a1SGleb Smirnoff 	.disconnect =	ng_ipfw_disconnect,
81670742a1SGleb Smirnoff };
82670742a1SGleb Smirnoff NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
83670742a1SGleb Smirnoff MODULE_DEPEND(ng_ipfw, ipfw, 2, 2, 2);
84670742a1SGleb Smirnoff 
85670742a1SGleb Smirnoff /* Information we store for each hook */
86670742a1SGleb Smirnoff struct ng_ipfw_hook_priv {
87670742a1SGleb Smirnoff         hook_p		hook;
88670742a1SGleb Smirnoff 	u_int16_t	rulenum;
89670742a1SGleb Smirnoff };
90670742a1SGleb Smirnoff typedef struct ng_ipfw_hook_priv *hpriv_p;
91670742a1SGleb Smirnoff 
92670742a1SGleb Smirnoff static int
93670742a1SGleb Smirnoff ng_ipfw_mod_event(module_t mod, int event, void *data)
94670742a1SGleb Smirnoff {
95670742a1SGleb Smirnoff 	int error = 0;
96670742a1SGleb Smirnoff 
97670742a1SGleb Smirnoff 	switch (event) {
98670742a1SGleb Smirnoff 	case MOD_LOAD:
99670742a1SGleb Smirnoff 
100670742a1SGleb Smirnoff 		if (ng_ipfw_input_p != NULL) {
101670742a1SGleb Smirnoff 			error = EEXIST;
102670742a1SGleb Smirnoff 			break;
103670742a1SGleb Smirnoff 		}
104670742a1SGleb Smirnoff 
105670742a1SGleb Smirnoff 		/* Setup node without any private data */
106670742a1SGleb Smirnoff 		if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
107670742a1SGleb Smirnoff 		    != 0) {
108670742a1SGleb Smirnoff 			log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
109670742a1SGleb Smirnoff                 	break;
110670742a1SGleb Smirnoff 		};
111670742a1SGleb Smirnoff 
112670742a1SGleb Smirnoff 		/* Try to name node */
113670742a1SGleb Smirnoff 		if (ng_name_node(fw_node, "ipfw") != 0)
114670742a1SGleb Smirnoff 			log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
115670742a1SGleb Smirnoff 			    __func__);
116670742a1SGleb Smirnoff 
117670742a1SGleb Smirnoff 		/* Register hook */
118670742a1SGleb Smirnoff 		ng_ipfw_input_p = ng_ipfw_input;
119670742a1SGleb Smirnoff 		break;
120670742a1SGleb Smirnoff 
121670742a1SGleb Smirnoff 	case MOD_UNLOAD:
122670742a1SGleb Smirnoff 		 /*
123670742a1SGleb Smirnoff 		  * This won't happen if a node exists.
124670742a1SGleb Smirnoff 		  * ng_ipfw_input_p is already cleared.
125670742a1SGleb Smirnoff 		  */
126670742a1SGleb Smirnoff 		break;
127670742a1SGleb Smirnoff 
128670742a1SGleb Smirnoff 	default:
129670742a1SGleb Smirnoff 		error = EOPNOTSUPP;
130670742a1SGleb Smirnoff 		break;
131670742a1SGleb Smirnoff 	}
132670742a1SGleb Smirnoff 
133670742a1SGleb Smirnoff 	return (error);
134670742a1SGleb Smirnoff }
135670742a1SGleb Smirnoff 
136670742a1SGleb Smirnoff static int
137670742a1SGleb Smirnoff ng_ipfw_constructor(node_p node)
138670742a1SGleb Smirnoff {
139670742a1SGleb Smirnoff 	return (EINVAL);	/* Only one node */
140670742a1SGleb Smirnoff }
141670742a1SGleb Smirnoff 
142670742a1SGleb Smirnoff static int
143670742a1SGleb Smirnoff ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
144670742a1SGleb Smirnoff {
145670742a1SGleb Smirnoff 	hpriv_p	hpriv;
146670742a1SGleb Smirnoff 	u_int16_t rulenum;
147670742a1SGleb Smirnoff 	const char *cp;
148ad1376ccSGleb Smirnoff 	char *endptr;
149670742a1SGleb Smirnoff 
150670742a1SGleb Smirnoff 	/* Check that name contains only digits */
151ad1376ccSGleb Smirnoff 	for (cp = name; *cp != '\0'; cp++)
152ad1376ccSGleb Smirnoff 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
153670742a1SGleb Smirnoff 			return (EINVAL);
154670742a1SGleb Smirnoff 
155670742a1SGleb Smirnoff 	/* Convert it to integer */
156ad1376ccSGleb Smirnoff 	rulenum = (u_int16_t)strtol(name, &endptr, 10);
157ad1376ccSGleb Smirnoff 	if (*endptr != '\0')
158670742a1SGleb Smirnoff 		return (EINVAL);
159670742a1SGleb Smirnoff 
160670742a1SGleb Smirnoff 	/* Allocate memory for this hook's private data */
161670742a1SGleb Smirnoff 	MALLOC(hpriv, hpriv_p, sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
162670742a1SGleb Smirnoff 	if (hpriv== NULL)
163670742a1SGleb Smirnoff 		return (ENOMEM);
164670742a1SGleb Smirnoff 
165670742a1SGleb Smirnoff 	hpriv->hook = hook;
166670742a1SGleb Smirnoff 	hpriv->rulenum = rulenum;
167670742a1SGleb Smirnoff 
168670742a1SGleb Smirnoff 	NG_HOOK_SET_PRIVATE(hook, hpriv);
169670742a1SGleb Smirnoff 
170670742a1SGleb Smirnoff 	return(0);
171670742a1SGleb Smirnoff }
172670742a1SGleb Smirnoff 
173670742a1SGleb Smirnoff /*
174670742a1SGleb Smirnoff  * Set hooks into queueing mode, to avoid recursion between
175670742a1SGleb Smirnoff  * netgraph layer and ip_{input,output}.
176670742a1SGleb Smirnoff  */
177670742a1SGleb Smirnoff static int
178670742a1SGleb Smirnoff ng_ipfw_connect(hook_p hook)
179670742a1SGleb Smirnoff {
180670742a1SGleb Smirnoff 	NG_HOOK_FORCE_QUEUE(hook);
181670742a1SGleb Smirnoff 	return (0);
182670742a1SGleb Smirnoff }
183670742a1SGleb Smirnoff 
184670742a1SGleb Smirnoff /* Look up hook by name */
185670742a1SGleb Smirnoff hook_p
186670742a1SGleb Smirnoff ng_ipfw_findhook(node_p node, const char *name)
187670742a1SGleb Smirnoff {
188670742a1SGleb Smirnoff 	u_int16_t n;	/* numeric representation of hook */
189ad1376ccSGleb Smirnoff 	char *endptr;
190670742a1SGleb Smirnoff 
191ad1376ccSGleb Smirnoff 	n = (u_int16_t)strtol(name, &endptr, 10);
192ad1376ccSGleb Smirnoff 	if (*endptr != '\0')
193670742a1SGleb Smirnoff 		return NULL;
194670742a1SGleb Smirnoff 	return ng_ipfw_findhook1(node, n);
195670742a1SGleb Smirnoff }
196670742a1SGleb Smirnoff 
197670742a1SGleb Smirnoff /* Look up hook by rule number */
198670742a1SGleb Smirnoff static hook_p
199670742a1SGleb Smirnoff ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
200670742a1SGleb Smirnoff {
201670742a1SGleb Smirnoff 	hook_p	hook;
202670742a1SGleb Smirnoff 	hpriv_p	hpriv;
203670742a1SGleb Smirnoff 
204670742a1SGleb Smirnoff 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
205670742a1SGleb Smirnoff 		hpriv = NG_HOOK_PRIVATE(hook);
206670742a1SGleb Smirnoff 		if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum))
207670742a1SGleb Smirnoff                         return (hook);
208670742a1SGleb Smirnoff 	}
209670742a1SGleb Smirnoff 
210670742a1SGleb Smirnoff 	return (NULL);
211670742a1SGleb Smirnoff }
212670742a1SGleb Smirnoff 
213670742a1SGleb Smirnoff 
214670742a1SGleb Smirnoff static int
215670742a1SGleb Smirnoff ng_ipfw_rcvdata(hook_p hook, item_p item)
216670742a1SGleb Smirnoff {
217670742a1SGleb Smirnoff 	struct ng_ipfw_tag	*ngit;
218670742a1SGleb Smirnoff 	struct mbuf *m;
219670742a1SGleb Smirnoff 
220670742a1SGleb Smirnoff 	NGI_GET_M(item, m);
221670742a1SGleb Smirnoff 	NG_FREE_ITEM(item);
222670742a1SGleb Smirnoff 
223670742a1SGleb Smirnoff 	if ((ngit = (struct ng_ipfw_tag *)m_tag_locate(m, NGM_IPFW_COOKIE, 0,
224670742a1SGleb Smirnoff 	    NULL)) == NULL) {
225670742a1SGleb Smirnoff 		NG_FREE_M(m);
226670742a1SGleb Smirnoff 		return (EINVAL);	/* XXX: find smth better */
227670742a1SGleb Smirnoff 	};
228670742a1SGleb Smirnoff 
229670742a1SGleb Smirnoff 	switch (ngit->dir) {
230670742a1SGleb Smirnoff 	case NG_IPFW_OUT:
231670742a1SGleb Smirnoff 	    {
2320a1a279eSGleb Smirnoff 		struct ip *ip;
2330a1a279eSGleb Smirnoff 
2340a1a279eSGleb Smirnoff 		if (m->m_len < sizeof(struct ip) &&
2350a1a279eSGleb Smirnoff 			(m = m_pullup(m, sizeof(struct ip))) == NULL)
2360a1a279eSGleb Smirnoff 				return(EINVAL);
2370a1a279eSGleb Smirnoff 
2380a1a279eSGleb Smirnoff 		ip = mtod(m, struct ip *);
239670742a1SGleb Smirnoff 
24019b55776SGleb Smirnoff 		ip->ip_len = ntohs(ip->ip_len);
24119b55776SGleb Smirnoff 		ip->ip_off = ntohs(ip->ip_off);
242670742a1SGleb Smirnoff 
24319b55776SGleb Smirnoff 		return ip_output(m, NULL, NULL, ngit->flags, NULL, NULL);
244670742a1SGleb Smirnoff 	    }
24519b55776SGleb Smirnoff 	case NG_IPFW_IN:
24619b55776SGleb Smirnoff 		ip_input(m);
24719b55776SGleb Smirnoff 		return (0);
248670742a1SGleb Smirnoff 	default:
249670742a1SGleb Smirnoff 		panic("ng_ipfw_rcvdata: bad dir %u", ngit->dir);
250670742a1SGleb Smirnoff 	}
251670742a1SGleb Smirnoff 
252670742a1SGleb Smirnoff 	/* not reached */
253670742a1SGleb Smirnoff 	return (0);
254670742a1SGleb Smirnoff }
255670742a1SGleb Smirnoff 
256670742a1SGleb Smirnoff static int
257670742a1SGleb Smirnoff ng_ipfw_input(struct mbuf **m0, int dir, struct ip_fw_args *fwa, int tee)
258670742a1SGleb Smirnoff {
259670742a1SGleb Smirnoff 	struct mbuf *m;
260670742a1SGleb Smirnoff 	struct ng_ipfw_tag *ngit;
26119b55776SGleb Smirnoff 	struct ip *ip;
262670742a1SGleb Smirnoff 	hook_p	hook;
263670742a1SGleb Smirnoff 	int error = 0;
264670742a1SGleb Smirnoff 
265670742a1SGleb Smirnoff 	/*
266670742a1SGleb Smirnoff 	 * Node must be loaded and corresponding hook must be present.
267670742a1SGleb Smirnoff 	 */
268670742a1SGleb Smirnoff 	if (fw_node == NULL ||
269670742a1SGleb Smirnoff 	   (hook = ng_ipfw_findhook1(fw_node, fwa->cookie)) == NULL) {
270670742a1SGleb Smirnoff 		if (tee == 0)
271670742a1SGleb Smirnoff 			m_freem(*m0);
272670742a1SGleb Smirnoff 		return (ESRCH);		/* no hook associated with this rule */
273670742a1SGleb Smirnoff 	}
274670742a1SGleb Smirnoff 
275670742a1SGleb Smirnoff 	/*
276670742a1SGleb Smirnoff 	 * We have two modes: in normal mode we add a tag to packet, which is
277670742a1SGleb Smirnoff 	 * important to return packet back to IP stack. In tee mode we make
278670742a1SGleb Smirnoff 	 * a copy of a packet and forward it into netgraph without a tag.
279670742a1SGleb Smirnoff 	 */
280670742a1SGleb Smirnoff 	if (tee == 0) {
281670742a1SGleb Smirnoff 		m = *m0;
282670742a1SGleb Smirnoff 		*m0 = NULL;	/* it belongs now to netgraph */
283670742a1SGleb Smirnoff 
284670742a1SGleb Smirnoff 		if ((ngit = (struct ng_ipfw_tag *)m_tag_alloc(NGM_IPFW_COOKIE,
285670742a1SGleb Smirnoff 		    0, TAGSIZ, M_NOWAIT|M_ZERO)) == NULL) {
286670742a1SGleb Smirnoff 			m_freem(m);
287670742a1SGleb Smirnoff 			return (ENOMEM);
288670742a1SGleb Smirnoff 		}
289670742a1SGleb Smirnoff 		ngit->rule = fwa->rule;
290670742a1SGleb Smirnoff 		ngit->dir = dir;
291670742a1SGleb Smirnoff 		ngit->ifp = fwa->oif;
292670742a1SGleb Smirnoff 		if (dir == NG_IPFW_OUT)
293670742a1SGleb Smirnoff 			ngit->flags = fwa->flags;
294670742a1SGleb Smirnoff 		m_tag_prepend(m, &ngit->mt);
295670742a1SGleb Smirnoff 
296670742a1SGleb Smirnoff 	} else
2978c6f9629SGleb Smirnoff 		if ((m = m_dup(*m0, M_DONTWAIT)) == NULL)
298670742a1SGleb Smirnoff 			return (ENOMEM);	/* which is ignored */
299670742a1SGleb Smirnoff 
3008c6f9629SGleb Smirnoff 	if (m->m_len < sizeof(struct ip) &&
3018c6f9629SGleb Smirnoff 		(m = m_pullup(m, sizeof(struct ip))) == NULL)
3028c6f9629SGleb Smirnoff 			return(EINVAL);
3038c6f9629SGleb Smirnoff 
30419b55776SGleb Smirnoff 	ip = mtod(m, struct ip *);
30519b55776SGleb Smirnoff 	ip->ip_len = htons(ip->ip_len);
30619b55776SGleb Smirnoff 	ip->ip_off = htons(ip->ip_off);
30719b55776SGleb Smirnoff 
308670742a1SGleb Smirnoff 	NG_SEND_DATA_ONLY(error, hook, m);
309670742a1SGleb Smirnoff 
310670742a1SGleb Smirnoff 	return (error);
311670742a1SGleb Smirnoff }
312670742a1SGleb Smirnoff 
313670742a1SGleb Smirnoff static int
314670742a1SGleb Smirnoff ng_ipfw_shutdown(node_p node)
315670742a1SGleb Smirnoff {
316670742a1SGleb Smirnoff 
317670742a1SGleb Smirnoff 	/*
318670742a1SGleb Smirnoff 	 * After our single node has been removed,
319670742a1SGleb Smirnoff 	 * the only thing that can be done is
320670742a1SGleb Smirnoff 	 * 'kldunload ng_ipfw.ko'
321670742a1SGleb Smirnoff 	 */
322670742a1SGleb Smirnoff 	ng_ipfw_input_p = NULL;
323670742a1SGleb Smirnoff 	NG_NODE_UNREF(node);
324670742a1SGleb Smirnoff 	return (0);
325670742a1SGleb Smirnoff }
326670742a1SGleb Smirnoff 
327670742a1SGleb Smirnoff static int
328670742a1SGleb Smirnoff ng_ipfw_disconnect(hook_p hook)
329670742a1SGleb Smirnoff {
330670742a1SGleb Smirnoff 	const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
331670742a1SGleb Smirnoff 
332670742a1SGleb Smirnoff 	FREE(hpriv, M_NETGRAPH);
333670742a1SGleb Smirnoff 	NG_HOOK_SET_PRIVATE(hook, NULL);
334670742a1SGleb Smirnoff 
335670742a1SGleb Smirnoff 	return (0);
336670742a1SGleb Smirnoff }
337