1 /* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $ */
2 /* $DragonFly: src/sys/net/pfil.c,v 1.14 2008/09/20 06:08:13 sephe Exp $ */
5 * Copyright (c) 1996 Matthew R. Green
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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>
39 #include <sys/queue.h>
43 #include <net/netmsg2.h>
44 #include <net/netisr2.h>
45 #include <sys/mplock2.h>
47 #define PFIL_CFGPORT netisr_cpuport(0)
49 #define PFIL_GETMPLOCK(pfh) \
51 if (((pfh)->pfil_flags & PFIL_MPSAFE) == 0) \
55 #define PFIL_RELMPLOCK(pfh) \
57 if (((pfh)->pfil_flags & PFIL_MPSAFE) == 0) \
62 * The packet filter hooks are designed for anything to call them to
63 * possibly intercept the packet.
65 struct packet_filter_hook {
66 TAILQ_ENTRY(packet_filter_hook) pfil_link;
67 pfil_func_t pfil_func;
73 struct netmsg_base base;
74 pfil_func_t pfil_func;
77 struct pfil_head *pfil_ph;
80 static LIST_HEAD(, pfil_head) pfil_head_list =
81 LIST_HEAD_INITIALIZER(&pfil_head_list);
83 static pfil_list_t *pfil_list_alloc(void);
84 static void pfil_list_free(pfil_list_t *);
85 static void pfil_list_dup(const pfil_list_t *, pfil_list_t *,
86 const struct packet_filter_hook *);
87 static void pfil_list_add(pfil_list_t *, pfil_func_t, void *, int);
88 static struct packet_filter_hook *
89 pfil_list_find(const pfil_list_t *, pfil_func_t,
92 static void pfil_remove_hook_dispatch(netmsg_t);
93 static void pfil_add_hook_dispatch(netmsg_t);
96 * pfil_run_hooks() runs the specified packet filter hooks.
99 pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
102 struct packet_filter_hook *pfh;
103 struct mbuf *m = *mp;
109 else if (dir == PFIL_OUT)
112 return 0; /* XXX panic? */
114 TAILQ_FOREACH(pfh, list, pfil_link) {
115 if (pfh->pfil_func != NULL) {
117 rv = pfh->pfil_func(pfh->pfil_arg, &m, ifp, dir);
120 if (rv != 0 || m == NULL)
130 * pfil_head_register() registers a pfil_head with the packet filter
134 pfil_head_register(struct pfil_head *ph)
136 struct pfil_head *lph;
138 LIST_FOREACH(lph, &pfil_head_list, ph_list) {
139 if (ph->ph_type == lph->ph_type &&
140 ph->ph_un.phu_val == lph->ph_un.phu_val)
144 ph->ph_in = pfil_list_alloc();
145 ph->ph_out = pfil_list_alloc();
148 LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
154 * pfil_head_unregister() removes a pfil_head from the packet filter
158 pfil_head_unregister(struct pfil_head *pfh)
160 LIST_REMOVE(pfh, ph_list);
165 * pfil_head_get() returns the pfil_head for a given key/dlt.
168 pfil_head_get(int type, u_long val)
170 struct pfil_head *ph;
172 LIST_FOREACH(ph, &pfil_head_list, ph_list) {
173 if (ph->ph_type == type && ph->ph_un.phu_val == val)
180 pfil_add_hook_dispatch(netmsg_t nmsg)
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;
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;
192 /* This probably should not happen ... */
193 if ((flags & (PFIL_IN | PFIL_OUT)) == 0)
194 goto reply; /* XXX panic? */
197 * If pfil hooks exist on any of the requested lists,
200 if (flags & PFIL_IN) {
201 pfh = pfil_list_find(ph->ph_in, func, arg);
207 if (flags & PFIL_OUT) {
208 pfh = pfil_list_find(ph->ph_out, func, arg);
216 * Duplicate the requested lists, install new hooks
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);
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);
231 * Switch list pointers, but keep the old ones
233 if (list_in != NULL) {
234 old_list_in = ph->ph_in;
237 if (list_out != NULL) {
238 old_list_out = ph->ph_out;
239 ph->ph_out = list_out;
243 * Wait until everyone has finished the old lists iteration
245 netmsg_service_sync();
249 * Now it is safe to free the old lists, since no one sees it
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);
256 lwkt_replymsg(&nmsg->base.lmsg, err);
260 * pfil_add_hook() adds a function to the packet filter hook. the
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
265 * PFIL_MPSAFE call me without BGL
268 pfil_add_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
270 struct netmsg_pfil pfilmsg;
274 nmsg = &pfilmsg.base;
275 netmsg_init(nmsg, NULL, &curthread->td_msgport,
276 0, pfil_add_hook_dispatch);
277 pfilmsg.pfil_func = func;
278 pfilmsg.pfil_arg = arg;
279 pfilmsg.pfil_flags = flags;
280 pfilmsg.pfil_ph = ph;
282 error = lwkt_domsg(PFIL_CFGPORT, &nmsg->lmsg, 0);
287 pfil_remove_hook_dispatch(netmsg_t nmsg)
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;
299 /* This probably should not happen ... */
300 if ((flags & (PFIL_IN | PFIL_OUT)) == 0)
301 goto reply; /* XXX panic? */
304 * The pfil hook should exist on all requested lists,
305 * if not just bail out
307 if (flags & PFIL_IN) {
308 skip_in = pfil_list_find(ph->ph_in, func, arg);
314 if (flags & PFIL_OUT) {
315 skip_out = pfil_list_find(ph->ph_out, func, arg);
323 * Duplicate the requested lists, but the pfil hook to
324 * be deleted is not copied
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);
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);
338 * Switch list pointers, but keep the old ones
340 if (list_in != NULL) {
341 old_list_in = ph->ph_in;
344 if (list_out != NULL) {
345 old_list_out = ph->ph_out;
346 ph->ph_out = list_out;
350 * Wait until everyone has finished the old lists iteration
352 if (TAILQ_EMPTY(ph->ph_in) && TAILQ_EMPTY(ph->ph_out))
354 netmsg_service_sync();
357 * Now it is safe to free the old lists, since no one sees it
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);
364 lwkt_replymsg(&nmsg->base.lmsg, err);
368 * pfil_remove_hook removes a specific function from the packet filter
372 pfil_remove_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
374 struct netmsg_pfil pfilmsg;
377 nmsg = &pfilmsg.base;
378 netmsg_init(nmsg, NULL, &curthread->td_msgport,
379 0, pfil_remove_hook_dispatch);
380 pfilmsg.pfil_func = func;
381 pfilmsg.pfil_arg = arg;
382 pfilmsg.pfil_flags = flags;
383 pfilmsg.pfil_ph = ph;
385 return lwkt_domsg(PFIL_CFGPORT, &nmsg->lmsg, 0);
389 pfil_list_add(pfil_list_t *list, pfil_func_t func, void *arg, int flags)
391 struct packet_filter_hook *pfh;
393 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
395 pfh = kmalloc(sizeof(*pfh), M_IFADDR, M_WAITOK);
397 pfh->pfil_func = func;
399 pfh->pfil_flags = flags;
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.
406 TAILQ_INSERT_HEAD(list, pfh, pfil_link);
408 TAILQ_INSERT_TAIL(list, pfh, pfil_link);
412 pfil_list_dup(const pfil_list_t *from, pfil_list_t *to,
413 const struct packet_filter_hook *skip)
415 struct packet_filter_hook *pfh_to, *pfh_from;
417 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
418 KKASSERT(TAILQ_EMPTY(to));
420 TAILQ_FOREACH(pfh_from, from, pfil_link) {
421 if (pfh_from == skip)
424 pfh_to = kmalloc(sizeof(*pfh_to), M_IFADDR, M_WAITOK);
425 bcopy(pfh_from, pfh_to, sizeof(*pfh_to));
427 TAILQ_INSERT_TAIL(to, pfh_to, pfil_link);
432 pfil_list_alloc(void)
436 list = kmalloc(sizeof(*list), M_IFADDR, M_WAITOK);
442 pfil_list_free(pfil_list_t *list)
444 struct packet_filter_hook *pfh;
446 while ((pfh = TAILQ_FIRST(list)) != NULL) {
447 TAILQ_REMOVE(list, pfh, pfil_link);
448 kfree(pfh, M_IFADDR);
450 kfree(list, M_IFADDR);
453 static struct packet_filter_hook *
454 pfil_list_find(const pfil_list_t *list, pfil_func_t func, const void *arg)
456 struct packet_filter_hook *pfh;
458 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
460 TAILQ_FOREACH(pfh, list, pfil_link) {
461 if (pfh->pfil_func == func && pfh->pfil_arg == arg)