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