2 /* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem 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/kernel.h>
34 #include <sys/errno.h>
36 #include <sys/malloc.h>
37 #include <sys/rmlock.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/systm.h>
41 #include <sys/condvar.h>
43 #include <sys/mutex.h>
45 #include <sys/queue.h>
50 static struct mtx pfil_global_lock;
52 MTX_SYSINIT(pfil_heads_lock, &pfil_global_lock, "pfil_head_list lock",
55 static int pfil_list_add(pfil_list_t *, struct packet_filter_hook *, int);
57 static int pfil_list_remove(pfil_list_t *,
58 int (*)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
61 LIST_HEAD(pfilheadhead, pfil_head);
62 VNET_DEFINE(struct pfilheadhead, pfil_head_list);
63 #define V_pfil_head_list VNET(pfil_head_list)
64 VNET_DEFINE(struct rmlock, pfil_lock);
65 #define V_pfil_lock VNET(pfil_lock)
68 * pfil_run_hooks() runs the specified packet filter hooks.
71 pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
72 int dir, struct inpcb *inp)
74 struct rm_priotracker rmpt;
75 struct packet_filter_hook *pfh;
79 PFIL_RLOCK(ph, &rmpt);
80 KASSERT(ph->ph_nhooks >= 0, ("Pfil hook count dropped < 0"));
81 for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
82 pfh = TAILQ_NEXT(pfh, pfil_link)) {
83 if (pfh->pfil_func != NULL) {
84 rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir,
86 if (rv != 0 || m == NULL)
90 PFIL_RUNLOCK(ph, &rmpt);
96 * pfil_try_rlock() acquires rm reader lock for specified head
97 * if this is immediately possible,
100 pfil_try_rlock(struct pfil_head *ph, struct rm_priotracker *tracker)
102 return PFIL_TRY_RLOCK(ph, tracker);
106 * pfil_rlock() acquires rm reader lock for specified head.
109 pfil_rlock(struct pfil_head *ph, struct rm_priotracker *tracker)
111 PFIL_RLOCK(ph, tracker);
115 * pfil_runlock() releases reader lock for specified head.
118 pfil_runlock(struct pfil_head *ph, struct rm_priotracker *tracker)
120 PFIL_RUNLOCK(ph, tracker);
124 * pfil_wlock() acquires writer lock for specified head.
127 pfil_wlock(struct pfil_head *ph)
133 * pfil_wunlock() releases writer lock for specified head.
136 pfil_wunlock(struct pfil_head *ph)
142 * pfil_wowned() releases writer lock for specified head.
145 pfil_wowned(struct pfil_head *ph)
147 return PFIL_WOWNED(ph);
150 * pfil_head_register() registers a pfil_head with the packet filter hook
154 pfil_head_register(struct pfil_head *ph)
156 struct pfil_head *lph;
159 LIST_FOREACH(lph, &V_pfil_head_list, ph_list) {
160 if (ph->ph_type == lph->ph_type &&
161 ph->ph_un.phu_val == lph->ph_un.phu_val) {
168 TAILQ_INIT(&ph->ph_in);
169 TAILQ_INIT(&ph->ph_out);
170 LIST_INSERT_HEAD(&V_pfil_head_list, ph, ph_list);
176 * pfil_head_unregister() removes a pfil_head from the packet filter hook
177 * mechanism. The producer of the hook promises that all outstanding
178 * invocations of the hook have completed before it unregisters the hook.
181 pfil_head_unregister(struct pfil_head *ph)
183 struct packet_filter_hook *pfh, *pfnext;
186 LIST_REMOVE(ph, ph_list);
188 TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_link, pfnext)
190 TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_link, pfnext)
192 PFIL_LOCK_DESTROY(ph);
197 * pfil_head_get() returns the pfil_head for a given key/dlt.
200 pfil_head_get(int type, u_long val)
202 struct pfil_head *ph;
205 LIST_FOREACH(ph, &V_pfil_head_list, ph_list)
206 if (ph->ph_type == type && ph->ph_un.phu_val == val)
213 * pfil_add_hook() adds a function to the packet filter hook. the
215 * PFIL_IN call me on incoming packets
216 * PFIL_OUT call me on outgoing packets
217 * PFIL_ALL call me on all of the above
218 * PFIL_WAITOK OK to call malloc with M_WAITOK.
221 pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int,
222 struct inpcb *), void *arg, int flags, struct pfil_head *ph)
224 struct packet_filter_hook *pfh1 = NULL;
225 struct packet_filter_hook *pfh2 = NULL;
228 if (flags & PFIL_IN) {
229 pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
230 M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
236 if (flags & PFIL_OUT) {
237 pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
238 M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
245 if (flags & PFIL_IN) {
246 pfh1->pfil_func = func;
247 pfh1->pfil_arg = arg;
248 err = pfil_list_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
253 if (flags & PFIL_OUT) {
254 pfh2->pfil_func = func;
255 pfh2->pfil_arg = arg;
256 err = pfil_list_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
259 pfil_list_remove(&ph->ph_in, func, arg);
270 free(pfh1, M_IFADDR);
272 free(pfh2, M_IFADDR);
277 * pfil_remove_hook removes a specific function from the packet filter hook
281 pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int,
282 struct inpcb *), void *arg, int flags, struct pfil_head *ph)
287 if (flags & PFIL_IN) {
288 err = pfil_list_remove(&ph->ph_in, func, arg);
292 if ((err == 0) && (flags & PFIL_OUT)) {
293 err = pfil_list_remove(&ph->ph_out, func, arg);
302 pfil_list_add(pfil_list_t *list, struct packet_filter_hook *pfh1, int flags)
304 struct packet_filter_hook *pfh;
307 * First make sure the hook is not already there.
309 TAILQ_FOREACH(pfh, list, pfil_link)
310 if (pfh->pfil_func == pfh1->pfil_func &&
311 pfh->pfil_arg == pfh1->pfil_arg)
315 * Insert the input list in reverse order of the output list so that
316 * the same path is followed in or out of the kernel.
319 TAILQ_INSERT_HEAD(list, pfh1, pfil_link);
321 TAILQ_INSERT_TAIL(list, pfh1, pfil_link);
326 * pfil_list_remove is an internal function that takes a function off the
330 pfil_list_remove(pfil_list_t *list,
331 int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
334 struct packet_filter_hook *pfh;
336 TAILQ_FOREACH(pfh, list, pfil_link)
337 if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
338 TAILQ_REMOVE(list, pfh, pfil_link);
346 * Stuff that must be initialized for every instance (including the first of
350 vnet_pfil_init(const void *unused)
353 LIST_INIT(&V_pfil_head_list);
354 PFIL_LOCK_INIT_REAL(&V_pfil_lock, "shared");
359 * Called for the removal of each instance.
362 vnet_pfil_uninit(const void *unused)
365 /* XXX should panic if list is not empty */
366 PFIL_LOCK_DESTROY_REAL(&V_pfil_lock);
370 /* Define startup order. */
371 #define PFIL_SYSINIT_ORDER SI_SUB_PROTO_BEGIN
372 #define PFIL_MODEVENT_ORDER (SI_ORDER_FIRST) /* On boot slot in here. */
373 #define PFIL_VNET_ORDER (PFIL_MODEVENT_ORDER + 2) /* Later still. */
378 * VNET_SYSINIT is called for each existing vnet and each new vnet.
380 VNET_SYSINIT(vnet_pfil_init, PFIL_SYSINIT_ORDER, PFIL_VNET_ORDER,
381 vnet_pfil_init, NULL);
384 * Closing up shop. These are done in REVERSE ORDER. Not called on reboot.
386 * VNET_SYSUNINIT is called for each exiting vnet as it exits.
388 VNET_SYSUNINIT(vnet_pfil_uninit, PFIL_SYSINIT_ORDER, PFIL_VNET_ORDER,
389 vnet_pfil_uninit, NULL);