2 /* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1996 Matthew R. Green
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/errno.h>
38 #include <sys/malloc.h>
39 #include <sys/rmlock.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/systm.h>
43 #include <sys/condvar.h>
45 #include <sys/mutex.h>
47 #include <sys/queue.h>
50 #include <net/if_var.h>
53 static struct mtx pfil_global_lock;
55 MTX_SYSINIT(pfil_heads_lock, &pfil_global_lock, "pfil_head_list lock",
58 static struct packet_filter_hook *pfil_chain_get(int, struct pfil_head *);
59 static int pfil_chain_add(pfil_chain_t *, struct packet_filter_hook *, int);
60 static int pfil_chain_remove(pfil_chain_t *, void *, void *);
61 static int pfil_add_hook_priv(void *, void *, int, struct pfil_head *, bool);
63 LIST_HEAD(pfilheadhead, pfil_head);
64 VNET_DEFINE(struct pfilheadhead, pfil_head_list);
65 #define V_pfil_head_list VNET(pfil_head_list)
66 VNET_DEFINE(struct rmlock, pfil_lock);
68 #define PFIL_LOCK_INIT_REAL(l, t) \
69 rm_init_flags(l, "PFil " t " rmlock", RM_RECURSE)
70 #define PFIL_LOCK_DESTROY_REAL(l) \
72 #define PFIL_LOCK_INIT(p) do { \
73 if ((p)->flags & PFIL_FLAG_PRIVATE_LOCK) { \
74 PFIL_LOCK_INIT_REAL(&(p)->ph_lock, "private"); \
75 (p)->ph_plock = &(p)->ph_lock; \
77 (p)->ph_plock = &V_pfil_lock; \
79 #define PFIL_LOCK_DESTROY(p) do { \
80 if ((p)->flags & PFIL_FLAG_PRIVATE_LOCK) \
81 PFIL_LOCK_DESTROY_REAL((p)->ph_plock); \
84 #define PFIL_TRY_RLOCK(p, t) rm_try_rlock((p)->ph_plock, (t))
85 #define PFIL_RLOCK(p, t) rm_rlock((p)->ph_plock, (t))
86 #define PFIL_WLOCK(p) rm_wlock((p)->ph_plock)
87 #define PFIL_RUNLOCK(p, t) rm_runlock((p)->ph_plock, (t))
88 #define PFIL_WUNLOCK(p) rm_wunlock((p)->ph_plock)
89 #define PFIL_WOWNED(p) rm_wowned((p)->ph_plock)
91 #define PFIL_HEADLIST_LOCK() mtx_lock(&pfil_global_lock)
92 #define PFIL_HEADLIST_UNLOCK() mtx_unlock(&pfil_global_lock)
95 * pfil_run_hooks() runs the specified packet filter hook chain.
98 pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
99 int dir, int flags, struct inpcb *inp)
101 struct rm_priotracker rmpt;
102 struct packet_filter_hook *pfh;
103 struct mbuf *m = *mp;
106 PFIL_RLOCK(ph, &rmpt);
107 KASSERT(ph->ph_nhooks >= 0, ("Pfil hook count dropped < 0"));
108 for (pfh = pfil_chain_get(dir, ph); pfh != NULL;
109 pfh = TAILQ_NEXT(pfh, pfil_chain)) {
110 if (pfh->pfil_func_flags != NULL) {
111 rv = (*pfh->pfil_func_flags)(pfh->pfil_arg, &m, ifp,
113 if (rv != 0 || m == NULL)
116 if (pfh->pfil_func != NULL) {
117 rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir,
119 if (rv != 0 || m == NULL)
123 PFIL_RUNLOCK(ph, &rmpt);
128 static struct packet_filter_hook *
129 pfil_chain_get(int dir, struct pfil_head *ph)
133 return (TAILQ_FIRST(&ph->ph_in));
134 else if (dir == PFIL_OUT)
135 return (TAILQ_FIRST(&ph->ph_out));
141 * pfil_try_rlock() acquires rm reader lock for specified head
142 * if this is immediately possible.
145 pfil_try_rlock(struct pfil_head *ph, struct rm_priotracker *tracker)
148 return (PFIL_TRY_RLOCK(ph, tracker));
152 * pfil_rlock() acquires rm reader lock for specified head.
155 pfil_rlock(struct pfil_head *ph, struct rm_priotracker *tracker)
158 PFIL_RLOCK(ph, tracker);
162 * pfil_runlock() releases reader lock for specified head.
165 pfil_runlock(struct pfil_head *ph, struct rm_priotracker *tracker)
168 PFIL_RUNLOCK(ph, tracker);
172 * pfil_wlock() acquires writer lock for specified head.
175 pfil_wlock(struct pfil_head *ph)
182 * pfil_wunlock() releases writer lock for specified head.
185 pfil_wunlock(struct pfil_head *ph)
192 * pfil_wowned() returns a non-zero value if the current thread owns
196 pfil_wowned(struct pfil_head *ph)
199 return (PFIL_WOWNED(ph));
203 * pfil_head_register() registers a pfil_head with the packet filter hook
207 pfil_head_register(struct pfil_head *ph)
209 struct pfil_head *lph;
211 PFIL_HEADLIST_LOCK();
212 LIST_FOREACH(lph, &V_pfil_head_list, ph_list) {
213 if (ph->ph_type == lph->ph_type &&
214 ph->ph_un.phu_val == lph->ph_un.phu_val) {
215 PFIL_HEADLIST_UNLOCK();
221 TAILQ_INIT(&ph->ph_in);
222 TAILQ_INIT(&ph->ph_out);
223 LIST_INSERT_HEAD(&V_pfil_head_list, ph, ph_list);
224 PFIL_HEADLIST_UNLOCK();
229 * pfil_head_unregister() removes a pfil_head from the packet filter hook
230 * mechanism. The producer of the hook promises that all outstanding
231 * invocations of the hook have completed before it unregisters the hook.
234 pfil_head_unregister(struct pfil_head *ph)
236 struct packet_filter_hook *pfh, *pfnext;
238 PFIL_HEADLIST_LOCK();
239 LIST_REMOVE(ph, ph_list);
240 PFIL_HEADLIST_UNLOCK();
241 TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_chain, pfnext)
243 TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_chain, pfnext)
245 PFIL_LOCK_DESTROY(ph);
250 * pfil_head_get() returns the pfil_head for a given key/dlt.
253 pfil_head_get(int type, u_long val)
255 struct pfil_head *ph;
257 PFIL_HEADLIST_LOCK();
258 LIST_FOREACH(ph, &V_pfil_head_list, ph_list)
259 if (ph->ph_type == type && ph->ph_un.phu_val == val)
261 PFIL_HEADLIST_UNLOCK();
266 * pfil_add_hook_flags() adds a function to the packet filter hook. the
268 * PFIL_IN call me on incoming packets
269 * PFIL_OUT call me on outgoing packets
270 * PFIL_ALL call me on all of the above
271 * PFIL_WAITOK OK to call malloc with M_WAITOK.
274 pfil_add_hook_flags(pfil_func_flags_t func, void *arg, int flags,
275 struct pfil_head *ph)
277 return (pfil_add_hook_priv(func, arg, flags, ph, true));
281 * pfil_add_hook() adds a function to the packet filter hook. the
283 * PFIL_IN call me on incoming packets
284 * PFIL_OUT call me on outgoing packets
285 * PFIL_ALL call me on all of the above
286 * PFIL_WAITOK OK to call malloc with M_WAITOK.
289 pfil_add_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
291 return (pfil_add_hook_priv(func, arg, flags, ph, false));
295 pfil_add_hook_priv(void *func, void *arg, int flags,
296 struct pfil_head *ph, bool hasflags)
298 struct packet_filter_hook *pfh1 = NULL;
299 struct packet_filter_hook *pfh2 = NULL;
302 if (flags & PFIL_IN) {
303 pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
304 M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
310 if (flags & PFIL_OUT) {
311 pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
312 M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
319 if (flags & PFIL_IN) {
320 pfh1->pfil_func_flags = hasflags ? func : NULL;
321 pfh1->pfil_func = hasflags ? NULL : func;
322 pfh1->pfil_arg = arg;
323 err = pfil_chain_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
328 if (flags & PFIL_OUT) {
329 pfh2->pfil_func_flags = hasflags ? func : NULL;
330 pfh2->pfil_func = hasflags ? NULL : func;
331 pfh2->pfil_arg = arg;
332 err = pfil_chain_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
335 pfil_chain_remove(&ph->ph_in, func, arg);
346 free(pfh1, M_IFADDR);
348 free(pfh2, M_IFADDR);
353 * pfil_remove_hook_flags removes a specific function from the packet filter hook
357 pfil_remove_hook_flags(pfil_func_flags_t func, void *arg, int flags,
358 struct pfil_head *ph)
360 return (pfil_remove_hook((pfil_func_t)func, arg, flags, ph));
364 * pfil_remove_hook removes a specific function from the packet filter hook
368 pfil_remove_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
373 if (flags & PFIL_IN) {
374 err = pfil_chain_remove(&ph->ph_in, func, arg);
378 if ((err == 0) && (flags & PFIL_OUT)) {
379 err = pfil_chain_remove(&ph->ph_out, func, arg);
388 * Internal: Add a new pfil hook into a hook chain.
391 pfil_chain_add(pfil_chain_t *chain, struct packet_filter_hook *pfh1, int flags)
393 struct packet_filter_hook *pfh;
396 * First make sure the hook is not already there.
398 TAILQ_FOREACH(pfh, chain, pfil_chain)
399 if (((pfh->pfil_func != NULL && pfh->pfil_func == pfh1->pfil_func) ||
400 (pfh->pfil_func_flags != NULL &&
401 pfh->pfil_func_flags == pfh1->pfil_func_flags)) &&
402 pfh->pfil_arg == pfh1->pfil_arg)
406 * Insert the input list in reverse order of the output list so that
407 * the same path is followed in or out of the kernel.
410 TAILQ_INSERT_HEAD(chain, pfh1, pfil_chain);
412 TAILQ_INSERT_TAIL(chain, pfh1, pfil_chain);
417 * Internal: Remove a pfil hook from a hook chain.
420 pfil_chain_remove(pfil_chain_t *chain, void *func, void *arg)
422 struct packet_filter_hook *pfh;
424 TAILQ_FOREACH(pfh, chain, pfil_chain)
425 if ((pfh->pfil_func == func || pfh->pfil_func_flags == func) &&
426 pfh->pfil_arg == arg) {
427 TAILQ_REMOVE(chain, pfh, pfil_chain);
435 * Stuff that must be initialized for every instance (including the first of
439 vnet_pfil_init(const void *unused __unused)
442 LIST_INIT(&V_pfil_head_list);
443 PFIL_LOCK_INIT_REAL(&V_pfil_lock, "shared");
447 * Called for the removal of each instance.
450 vnet_pfil_uninit(const void *unused __unused)
453 KASSERT(LIST_EMPTY(&V_pfil_head_list),
454 ("%s: pfil_head_list %p not empty", __func__, &V_pfil_head_list));
455 PFIL_LOCK_DESTROY_REAL(&V_pfil_lock);
461 * VNET_SYSINIT is called for each existing vnet and each new vnet.
462 * Make sure the pfil bits are first before any possible subsystem which
463 * might piggyback on the SI_SUB_PROTO_PFIL.
465 VNET_SYSINIT(vnet_pfil_init, SI_SUB_PROTO_PFIL, SI_ORDER_FIRST,
466 vnet_pfil_init, NULL);
469 * Closing up shop. These are done in REVERSE ORDER. Not called on reboot.
471 * VNET_SYSUNINIT is called for each exiting vnet as it exits.
473 VNET_SYSUNINIT(vnet_pfil_uninit, SI_SUB_PROTO_PFIL, SI_ORDER_FIRST,
474 vnet_pfil_uninit, NULL);