ipfw2 - Rename FreeBSD ipfw port to ipfw3 (2)
[dragonfly.git] / sys / net / pfil.c
CommitLineData
9e99d0a2 1/* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $ */
77e0b69a 2/* $DragonFly: src/sys/net/pfil.c,v 1.14 2008/09/20 06:08:13 sephe Exp $ */
9e99d0a2
JR
3
4/*
5 * Copyright (c) 1996 Matthew R. Green
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/param.h>
33#include <sys/errno.h>
34#include <sys/malloc.h>
35#include <sys/socket.h>
36#include <sys/socketvar.h>
37#include <sys/systm.h>
38#include <sys/proc.h>
39#include <sys/queue.h>
40
41#include <net/if.h>
42#include <net/pfil.h>
f0fad8e1 43#include <net/netmsg2.h>
5337421c 44#include <net/netisr2.h>
684a93c4 45#include <sys/mplock2.h>
f0fad8e1 46
ec7f7fc8 47#define PFIL_CFGPORT netisr_cpuport(0)
77e0b69a
SZ
48
49#define PFIL_GETMPLOCK(pfh) \
50do { \
51 if (((pfh)->pfil_flags & PFIL_MPSAFE) == 0) \
52 get_mplock(); \
53} while (0)
54
55#define PFIL_RELMPLOCK(pfh) \
56do { \
57 if (((pfh)->pfil_flags & PFIL_MPSAFE) == 0) \
58 rel_mplock(); \
59} while (0)
60
caa0af9f
SZ
61/*
62 * The packet filter hooks are designed for anything to call them to
63 * possibly intercept the packet.
64 */
65struct packet_filter_hook {
66 TAILQ_ENTRY(packet_filter_hook) pfil_link;
67 pfil_func_t pfil_func;
68 void *pfil_arg;
69 int pfil_flags;
70};
71
f0fad8e1 72struct netmsg_pfil {
002c1265 73 struct netmsg_base base;
f0fad8e1
SZ
74 pfil_func_t pfil_func;
75 void *pfil_arg;
76 int pfil_flags;
77 struct pfil_head *pfil_ph;
78};
9e99d0a2 79
d66da3ad
SZ
80static LIST_HEAD(, pfil_head) pfil_head_list =
81 LIST_HEAD_INITIALIZER(&pfil_head_list);
9e99d0a2 82
d66da3ad
SZ
83static pfil_list_t *pfil_list_alloc(void);
84static void pfil_list_free(pfil_list_t *);
85static void pfil_list_dup(const pfil_list_t *, pfil_list_t *,
86 const struct packet_filter_hook *);
87static void pfil_list_add(pfil_list_t *, pfil_func_t, void *, int);
88static struct packet_filter_hook *
89 pfil_list_find(const pfil_list_t *, pfil_func_t,
90 const void *);
91
002c1265
MD
92static void pfil_remove_hook_dispatch(netmsg_t);
93static void pfil_add_hook_dispatch(netmsg_t);
9e99d0a2
JR
94
95/*
96 * pfil_run_hooks() runs the specified packet filter hooks.
97 */
98int
99pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
100 int dir)
101{
102 struct packet_filter_hook *pfh;
103 struct mbuf *m = *mp;
7f8565be 104 pfil_list_t *list;
9e99d0a2
JR
105 int rv = 0;
106
7f8565be 107 if (dir == PFIL_IN)
d66da3ad 108 list = ph->ph_in;
7f8565be 109 else if (dir == PFIL_OUT)
d66da3ad 110 list = ph->ph_out;
7f8565be
SZ
111 else
112 return 0; /* XXX panic? */
113
114 TAILQ_FOREACH(pfh, list, pfil_link) {
9e99d0a2 115 if (pfh->pfil_func != NULL) {
77e0b69a
SZ
116 PFIL_GETMPLOCK(pfh);
117 rv = pfh->pfil_func(pfh->pfil_arg, &m, ifp, dir);
118 PFIL_RELMPLOCK(pfh);
119
9e99d0a2
JR
120 if (rv != 0 || m == NULL)
121 break;
122 }
123 }
124
125 *mp = m;
126 return (rv);
127}
128
129/*
130 * pfil_head_register() registers a pfil_head with the packet filter
131 * hook mechanism.
132 */
133int
134pfil_head_register(struct pfil_head *ph)
135{
136 struct pfil_head *lph;
137
94171836 138 LIST_FOREACH(lph, &pfil_head_list, ph_list) {
9e99d0a2
JR
139 if (ph->ph_type == lph->ph_type &&
140 ph->ph_un.phu_val == lph->ph_un.phu_val)
141 return EEXIST;
142 }
143
d66da3ad
SZ
144 ph->ph_in = pfil_list_alloc();
145 ph->ph_out = pfil_list_alloc();
afabe90c 146 ph->ph_hashooks = 0;
9e99d0a2
JR
147
148 LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
149
150 return (0);
151}
152
153/*
154 * pfil_head_unregister() removes a pfil_head from the packet filter
155 * hook mechanism.
156 */
157int
158pfil_head_unregister(struct pfil_head *pfh)
159{
9e99d0a2
JR
160 LIST_REMOVE(pfh, ph_list);
161 return (0);
162}
163
164/*
165 * pfil_head_get() returns the pfil_head for a given key/dlt.
166 */
167struct pfil_head *
168pfil_head_get(int type, u_long val)
169{
170 struct pfil_head *ph;
171
94171836
SZ
172 LIST_FOREACH(ph, &pfil_head_list, ph_list) {
173 if (ph->ph_type == type && ph->ph_un.phu_val == val)
9e99d0a2
JR
174 break;
175 }
9e99d0a2
JR
176 return (ph);
177}
178
f0fad8e1 179static void
002c1265 180pfil_add_hook_dispatch(netmsg_t nmsg)
9e99d0a2 181{
f0fad8e1
SZ
182 struct netmsg_pfil *pfilmsg = (struct netmsg_pfil *)nmsg;
183 pfil_func_t func = pfilmsg->pfil_func;
184 void *arg = pfilmsg->pfil_arg;
185 int flags = pfilmsg->pfil_flags;
186 struct pfil_head *ph = pfilmsg->pfil_ph;
d66da3ad
SZ
187 const struct packet_filter_hook *pfh;
188 pfil_list_t *list_in = NULL, *list_out = NULL;
189 pfil_list_t *old_list_in = NULL, *old_list_out = NULL;
9e99d0a2
JR
190 int err = 0;
191
d66da3ad
SZ
192 /* This probably should not happen ... */
193 if ((flags & (PFIL_IN | PFIL_OUT)) == 0)
194 goto reply; /* XXX panic? */
195
196 /*
197 * If pfil hooks exist on any of the requested lists,
198 * then bail out.
199 */
9e99d0a2 200 if (flags & PFIL_IN) {
d66da3ad
SZ
201 pfh = pfil_list_find(ph->ph_in, func, arg);
202 if (pfh != NULL) {
203 err = EEXIST;
f0fad8e1 204 goto reply;
d66da3ad 205 }
9e99d0a2
JR
206 }
207 if (flags & PFIL_OUT) {
d66da3ad
SZ
208 pfh = pfil_list_find(ph->ph_out, func, arg);
209 if (pfh != NULL) {
210 err = EEXIST;
211 goto reply;
9e99d0a2
JR
212 }
213 }
d66da3ad
SZ
214
215 /*
216 * Duplicate the requested lists, install new hooks
217 * on the duplication
218 */
219 if (flags & PFIL_IN) {
220 list_in = pfil_list_alloc();
221 pfil_list_dup(ph->ph_in, list_in, NULL);
222 pfil_list_add(list_in, func, arg, flags & ~PFIL_OUT);
223 }
224 if (flags & PFIL_OUT) {
225 list_out = pfil_list_alloc();
226 pfil_list_dup(ph->ph_out, list_out, NULL);
227 pfil_list_add(list_out, func, arg, flags & ~PFIL_IN);
228 }
229
230 /*
231 * Switch list pointers, but keep the old ones
232 */
233 if (list_in != NULL) {
234 old_list_in = ph->ph_in;
235 ph->ph_in = list_in;
236 }
237 if (list_out != NULL) {
238 old_list_out = ph->ph_out;
239 ph->ph_out = list_out;
240 }
241
242 /*
243 * Wait until everyone has finished the old lists iteration
244 */
245 netmsg_service_sync();
246 ph->ph_hashooks = 1;
247
248 /*
249 * Now it is safe to free the old lists, since no one sees it
250 */
251 if (old_list_in != NULL)
252 pfil_list_free(old_list_in);
253 if (old_list_out != NULL)
254 pfil_list_free(old_list_out);
f0fad8e1 255reply:
002c1265 256 lwkt_replymsg(&nmsg->base.lmsg, err);
f0fad8e1
SZ
257}
258
259/*
260 * pfil_add_hook() adds a function to the packet filter hook. the
261 * flags are:
262 * PFIL_IN call me on incoming packets
263 * PFIL_OUT call me on outgoing packets
264 * PFIL_ALL call me on all of the above
77e0b69a 265 * PFIL_MPSAFE call me without BGL
f0fad8e1
SZ
266 */
267int
268pfil_add_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
269{
270 struct netmsg_pfil pfilmsg;
002c1265
MD
271 netmsg_base_t nmsg;
272 int error;
f0fad8e1 273
002c1265 274 nmsg = &pfilmsg.base;
48e7b118
MD
275 netmsg_init(nmsg, NULL, &curthread->td_msgport,
276 0, pfil_add_hook_dispatch);
f0fad8e1
SZ
277 pfilmsg.pfil_func = func;
278 pfilmsg.pfil_arg = arg;
279 pfilmsg.pfil_flags = flags;
280 pfilmsg.pfil_ph = ph;
281
002c1265
MD
282 error = lwkt_domsg(PFIL_CFGPORT, &nmsg->lmsg, 0);
283 return error;
9e99d0a2
JR
284}
285
d66da3ad 286static void
002c1265 287pfil_remove_hook_dispatch(netmsg_t nmsg)
9e99d0a2 288{
d66da3ad
SZ
289 struct netmsg_pfil *pfilmsg = (struct netmsg_pfil *)nmsg;
290 pfil_func_t func = pfilmsg->pfil_func;
291 void *arg = pfilmsg->pfil_arg;
292 int flags = pfilmsg->pfil_flags;
293 struct pfil_head *ph = pfilmsg->pfil_ph;
294 struct packet_filter_hook *skip_in = NULL, *skip_out = NULL;
295 pfil_list_t *list_in = NULL, *list_out = NULL;
296 pfil_list_t *old_list_in = NULL, *old_list_out = NULL;
297 int err = 0;
f0fad8e1 298
d66da3ad
SZ
299 /* This probably should not happen ... */
300 if ((flags & (PFIL_IN | PFIL_OUT)) == 0)
301 goto reply; /* XXX panic? */
9e99d0a2
JR
302
303 /*
d66da3ad
SZ
304 * The pfil hook should exist on all requested lists,
305 * if not just bail out
9e99d0a2 306 */
d66da3ad
SZ
307 if (flags & PFIL_IN) {
308 skip_in = pfil_list_find(ph->ph_in, func, arg);
309 if (!skip_in) {
310 err = ENOENT;
311 goto reply;
312 }
313 }
314 if (flags & PFIL_OUT) {
315 skip_out = pfil_list_find(ph->ph_out, func, arg);
316 if (!skip_out) {
317 err = ENOENT;
318 goto reply;
319 }
9e99d0a2
JR
320 }
321
d66da3ad
SZ
322 /*
323 * Duplicate the requested lists, but the pfil hook to
324 * be deleted is not copied
325 */
326 if (flags & PFIL_IN) {
327 KKASSERT(skip_in != NULL);
328 list_in = pfil_list_alloc();
329 pfil_list_dup(ph->ph_in, list_in, skip_in);
330 }
331 if (flags & PFIL_OUT) {
332 KKASSERT(skip_out != NULL);
333 list_out = pfil_list_alloc();
334 pfil_list_dup(ph->ph_out, list_out, skip_out);
335 }
9e99d0a2
JR
336
337 /*
d66da3ad 338 * Switch list pointers, but keep the old ones
9e99d0a2 339 */
d66da3ad
SZ
340 if (list_in != NULL) {
341 old_list_in = ph->ph_in;
342 ph->ph_in = list_in;
343 }
344 if (list_out != NULL) {
345 old_list_out = ph->ph_out;
346 ph->ph_out = list_out;
347 }
9e99d0a2 348
d66da3ad
SZ
349 /*
350 * Wait until everyone has finished the old lists iteration
351 */
352 if (TAILQ_EMPTY(ph->ph_in) && TAILQ_EMPTY(ph->ph_out))
353 ph->ph_hashooks = 0;
354 netmsg_service_sync();
f0fad8e1 355
d66da3ad
SZ
356 /*
357 * Now it is safe to free the old lists, since no one sees it
358 */
359 if (old_list_in != NULL)
360 pfil_list_free(old_list_in);
361 if (old_list_out != NULL)
362 pfil_list_free(old_list_out);
363reply:
002c1265 364 lwkt_replymsg(&nmsg->base.lmsg, err);
f0fad8e1
SZ
365}
366
9e99d0a2
JR
367/*
368 * pfil_remove_hook removes a specific function from the packet filter
369 * hook list.
370 */
371int
3bc01464 372pfil_remove_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
9e99d0a2 373{
f0fad8e1 374 struct netmsg_pfil pfilmsg;
002c1265 375 netmsg_base_t nmsg;
9e99d0a2 376
002c1265 377 nmsg = &pfilmsg.base;
48e7b118
MD
378 netmsg_init(nmsg, NULL, &curthread->td_msgport,
379 0, pfil_remove_hook_dispatch);
f0fad8e1
SZ
380 pfilmsg.pfil_func = func;
381 pfilmsg.pfil_arg = arg;
382 pfilmsg.pfil_flags = flags;
383 pfilmsg.pfil_ph = ph;
384
002c1265 385 return lwkt_domsg(PFIL_CFGPORT, &nmsg->lmsg, 0);
9e99d0a2
JR
386}
387
d66da3ad
SZ
388static void
389pfil_list_add(pfil_list_t *list, pfil_func_t func, void *arg, int flags)
9e99d0a2
JR
390{
391 struct packet_filter_hook *pfh;
afabe90c 392
f0fad8e1
SZ
393 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
394
d66da3ad
SZ
395 pfh = kmalloc(sizeof(*pfh), M_IFADDR, M_WAITOK);
396
397 pfh->pfil_func = func;
398 pfh->pfil_arg = arg;
77d9cd11 399 pfh->pfil_flags = flags;
d66da3ad
SZ
400
401 /*
402 * Insert the input list in reverse order of the output list
403 * so that the same path is followed in or out of the kernel.
404 */
405 if (flags & PFIL_IN)
406 TAILQ_INSERT_HEAD(list, pfh, pfil_link);
407 else
408 TAILQ_INSERT_TAIL(list, pfh, pfil_link);
409}
410
411static void
412pfil_list_dup(const pfil_list_t *from, pfil_list_t *to,
413 const struct packet_filter_hook *skip)
414{
415 struct packet_filter_hook *pfh_to, *pfh_from;
416
417 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
418 KKASSERT(TAILQ_EMPTY(to));
419
420 TAILQ_FOREACH(pfh_from, from, pfil_link) {
421 if (pfh_from == skip)
422 continue;
423
424 pfh_to = kmalloc(sizeof(*pfh_to), M_IFADDR, M_WAITOK);
425 bcopy(pfh_from, pfh_to, sizeof(*pfh_to));
426
427 TAILQ_INSERT_TAIL(to, pfh_to, pfil_link);
428 }
429}
430
431static pfil_list_t *
432pfil_list_alloc(void)
433{
434 pfil_list_t *list;
435
436 list = kmalloc(sizeof(*list), M_IFADDR, M_WAITOK);
437 TAILQ_INIT(list);
438 return list;
439}
440
441static void
442pfil_list_free(pfil_list_t *list)
443{
444 struct packet_filter_hook *pfh;
445
446 while ((pfh = TAILQ_FIRST(list)) != NULL) {
447 TAILQ_REMOVE(list, pfh, pfil_link);
448 kfree(pfh, M_IFADDR);
449 }
450 kfree(list, M_IFADDR);
451}
452
453static struct packet_filter_hook *
454pfil_list_find(const pfil_list_t *list, pfil_func_t func, const void *arg)
455{
456 struct packet_filter_hook *pfh;
457
458 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
9e99d0a2 459
94171836 460 TAILQ_FOREACH(pfh, list, pfil_link) {
d66da3ad
SZ
461 if (pfh->pfil_func == func && pfh->pfil_arg == arg)
462 return pfh;
9e99d0a2 463 }
d66da3ad 464 return NULL;
9e99d0a2 465}