xref: /freebsd/sys/netgraph/ng_ipfw.c (revision ce6a89e27cd190313be39bb479880aeda4778436)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2005, Gleb Smirnoff <glebius@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/mbuf.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>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 
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>
57 
58 #include <netpfil/ipfw/ip_fw_private.h>
59 
60 #include <netgraph/ng_message.h>
61 #include <netgraph/ng_parse.h>
62 #include <netgraph/ng_ipfw.h>
63 #include <netgraph/netgraph.h>
64 
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;
73 
74 static hook_p		ng_ipfw_findhook1(node_p, u_int16_t );
75 static int	ng_ipfw_input(struct mbuf **, struct ip_fw_args *, bool);
76 
77 /* We have only one node */
78 static node_p	fw_node;
79 
80 /* Netgraph node type descriptor */
81 static struct ng_type ng_ipfw_typestruct = {
82 	.version =	NG_ABI_VERSION,
83 	.name =		NG_IPFW_NODE_TYPE,
84 	.mod_event =	ng_ipfw_mod_event,
85 	.constructor =	ng_ipfw_constructor,
86 	.shutdown =	ng_ipfw_shutdown,
87 	.newhook =	ng_ipfw_newhook,
88 	.connect =	ng_ipfw_connect,
89 	.findhook =	ng_ipfw_findhook,
90 	.rcvdata =	ng_ipfw_rcvdata,
91 	.disconnect =	ng_ipfw_disconnect,
92 };
93 NETGRAPH_INIT(ipfw, &ng_ipfw_typestruct);
94 MODULE_DEPEND(ng_ipfw, ipfw, 3, 3, 3);
95 
96 /* Information we store for each hook */
97 struct ng_ipfw_hook_priv {
98         hook_p		hook;
99 	u_int16_t	rulenum;
100 };
101 typedef struct ng_ipfw_hook_priv *hpriv_p;
102 
103 static int
104 ng_ipfw_mod_event(module_t mod, int event, void *data)
105 {
106 	int error = 0;
107 
108 	switch (event) {
109 	case MOD_LOAD:
110 
111 		if (ng_ipfw_input_p != NULL) {
112 			error = EEXIST;
113 			break;
114 		}
115 
116 		/* Setup node without any private data */
117 		if ((error = ng_make_node_common(&ng_ipfw_typestruct, &fw_node))
118 		    != 0) {
119 			log(LOG_ERR, "%s: can't create ng_ipfw node", __func__);
120                 	break;
121 		}
122 
123 		/* Try to name node */
124 		if (ng_name_node(fw_node, "ipfw") != 0)
125 			log(LOG_WARNING, "%s: failed to name node \"ipfw\"",
126 			    __func__);
127 
128 		/* Register hook */
129 		ng_ipfw_input_p = ng_ipfw_input;
130 		break;
131 
132 	case MOD_UNLOAD:
133 		 /*
134 		  * This won't happen if a node exists.
135 		  * ng_ipfw_input_p is already cleared.
136 		  */
137 		break;
138 
139 	default:
140 		error = EOPNOTSUPP;
141 		break;
142 	}
143 
144 	return (error);
145 }
146 
147 static int
148 ng_ipfw_constructor(node_p node)
149 {
150 	return (EINVAL);	/* Only one node */
151 }
152 
153 static int
154 ng_ipfw_newhook(node_p node, hook_p hook, const char *name)
155 {
156 	hpriv_p	hpriv;
157 	u_int16_t rulenum;
158 	const char *cp;
159 	char *endptr;
160 
161 	/* Protect from leading zero */
162 	if (name[0] == '0' && name[1] != '\0')
163 		return (EINVAL);
164 
165 	/* Check that name contains only digits */
166 	for (cp = name; *cp != '\0'; cp++)
167 		if (!isdigit(*cp))
168 			return (EINVAL);
169 
170 	/* Convert it to integer */
171 	rulenum = (u_int16_t)strtol(name, &endptr, 10);
172 	if (*endptr != '\0')
173 		return (EINVAL);
174 
175 	/* Allocate memory for this hook's private data */
176 	hpriv = malloc(sizeof(*hpriv), M_NETGRAPH, M_NOWAIT | M_ZERO);
177 	if (hpriv== NULL)
178 		return (ENOMEM);
179 
180 	hpriv->hook = hook;
181 	hpriv->rulenum = rulenum;
182 
183 	NG_HOOK_SET_PRIVATE(hook, hpriv);
184 
185 	return(0);
186 }
187 
188 /*
189  * Set hooks into queueing mode, to avoid recursion between
190  * netgraph layer and ip_{input,output}.
191  */
192 static int
193 ng_ipfw_connect(hook_p hook)
194 {
195 	NG_HOOK_FORCE_QUEUE(hook);
196 	return (0);
197 }
198 
199 /* Look up hook by name */
200 static hook_p
201 ng_ipfw_findhook(node_p node, const char *name)
202 {
203 	u_int16_t n;	/* numeric representation of hook */
204 	char *endptr;
205 
206 	n = (u_int16_t)strtol(name, &endptr, 10);
207 	if (*endptr != '\0')
208 		return NULL;
209 	return ng_ipfw_findhook1(node, n);
210 }
211 
212 /* Look up hook by rule number */
213 static hook_p
214 ng_ipfw_findhook1(node_p node, u_int16_t rulenum)
215 {
216 	hook_p	hook;
217 	hpriv_p	hpriv;
218 
219 	LIST_FOREACH(hook, &node->nd_hooks, hk_hooks) {
220 		hpriv = NG_HOOK_PRIVATE(hook);
221 		if (NG_HOOK_IS_VALID(hook) && (hpriv->rulenum == rulenum))
222                         return (hook);
223 	}
224 
225 	return (NULL);
226 }
227 
228 
229 static int
230 ng_ipfw_rcvdata(hook_p hook, item_p item)
231 {
232 	struct m_tag *tag;
233 	struct ipfw_rule_ref *r;
234 	struct mbuf *m;
235 	struct ip *ip;
236 
237 	NGI_GET_M(item, m);
238 	NG_FREE_ITEM(item);
239 
240 	tag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
241 	if (tag == NULL) {
242 		NG_FREE_M(m);
243 		return (EINVAL);	/* XXX: find smth better */
244 	}
245 
246 	if (m->m_len < sizeof(struct ip) &&
247 	    (m = m_pullup(m, sizeof(struct ip))) == NULL)
248 		return (ENOBUFS);
249 
250 	ip = mtod(m, struct ip *);
251 
252 	r = (struct ipfw_rule_ref *)(tag + 1);
253 	if (r->info & IPFW_INFO_IN) {
254 		switch (ip->ip_v) {
255 #ifdef INET
256 		case IPVERSION:
257 			ip_input(m);
258 			return (0);
259 #endif
260 #ifdef INET6
261 		case IPV6_VERSION >> 4:
262 			ip6_input(m);
263 			return (0);
264 #endif
265 		}
266 	} else {
267 		switch (ip->ip_v) {
268 #ifdef INET
269 		case IPVERSION:
270 			return (ip_output(m, NULL, NULL, IP_FORWARDING,
271 			    NULL, NULL));
272 #endif
273 #ifdef INET6
274 		case IPV6_VERSION >> 4:
275 			return (ip6_output(m, NULL, NULL, 0, NULL,
276 			    NULL, NULL));
277 #endif
278 		}
279 	}
280 
281 	/* unknown IP protocol version */
282 	NG_FREE_M(m);
283 	return (EPROTONOSUPPORT);
284 }
285 
286 static int
287 ng_ipfw_input(struct mbuf **m0, struct ip_fw_args *fwa, bool tee)
288 {
289 	struct mbuf *m;
290 	hook_p	hook;
291 	int error = 0;
292 
293 	/*
294 	 * Node must be loaded and corresponding hook must be present.
295 	 */
296 	if (fw_node == NULL ||
297 	   (hook = ng_ipfw_findhook1(fw_node, fwa->rule.info)) == NULL)
298 		return (ESRCH);		/* no hook associated with this rule */
299 
300 	/*
301 	 * We have two modes: in normal mode we add a tag to packet, which is
302 	 * important to return packet back to IP stack. In tee mode we make
303 	 * a copy of a packet and forward it into netgraph without a tag.
304 	 */
305 	if (tee == false) {
306 		struct m_tag *tag;
307 		struct ipfw_rule_ref *r;
308 		m = *m0;
309 		*m0 = NULL;	/* it belongs now to netgraph */
310 
311 		tag = m_tag_alloc(MTAG_IPFW_RULE, 0, sizeof(*r),
312 			M_NOWAIT|M_ZERO);
313 		if (tag == NULL) {
314 			m_freem(m);
315 			return (ENOMEM);
316 		}
317 		r = (struct ipfw_rule_ref *)(tag + 1);
318 		*r = fwa->rule;
319 		r->info &= IPFW_ONEPASS;  /* keep this info */
320 		r->info |= (fwa->flags & IPFW_ARGS_IN) ?
321 		    IPFW_INFO_IN : IPFW_INFO_OUT;
322 		m_tag_prepend(m, tag);
323 
324 	} else
325 		if ((m = m_dup(*m0, M_NOWAIT)) == NULL)
326 			return (ENOMEM);	/* which is ignored */
327 
328 	if (m->m_len < sizeof(struct ip) &&
329 	    (m = m_pullup(m, sizeof(struct ip))) == NULL)
330 		return (EINVAL);
331 
332 	NG_SEND_DATA_ONLY(error, hook, m);
333 
334 	return (error);
335 }
336 
337 static int
338 ng_ipfw_shutdown(node_p node)
339 {
340 
341 	/*
342 	 * After our single node has been removed,
343 	 * the only thing that can be done is
344 	 * 'kldunload ng_ipfw.ko'
345 	 */
346 	ng_ipfw_input_p = NULL;
347 	NG_NODE_UNREF(node);
348 	return (0);
349 }
350 
351 static int
352 ng_ipfw_disconnect(hook_p hook)
353 {
354 	const hpriv_p hpriv = NG_HOOK_PRIVATE(hook);
355 
356 	free(hpriv, M_NETGRAPH);
357 	NG_HOOK_SET_PRIVATE(hook, NULL);
358 
359 	return (0);
360 }
361