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>
35 #include <sys/malloc.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/systm.h>
39 #include <sys/condvar.h>
41 #include <sys/mutex.h>
43 #include <sys/queue.h>
48 static struct mtx pfil_global_lock;
50 MTX_SYSINIT(pfil_heads_lock, &pfil_global_lock, "pfil_head_list lock", MTX_DEF);
52 static int pfil_list_add(pfil_list_t *, struct packet_filter_hook *, int);
54 static int pfil_list_remove(pfil_list_t *,
55 int (*)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *);
57 LIST_HEAD(, pfil_head) pfil_head_list =
58 LIST_HEAD_INITIALIZER(&pfil_head_list);
61 PFIL_RLOCK(struct pfil_head *ph)
63 mtx_lock(&ph->ph_mtx);
65 mtx_unlock(&ph->ph_mtx);
69 PFIL_RUNLOCK(struct pfil_head *ph)
71 mtx_lock(&ph->ph_mtx);
73 if (ph->ph_busy_count == 0 && ph->ph_want_write)
74 cv_signal(&ph->ph_cv);
75 mtx_unlock(&ph->ph_mtx);
79 PFIL_WLOCK(struct pfil_head *ph)
81 mtx_lock(&ph->ph_mtx);
82 ph->ph_want_write = 1;
83 while (ph->ph_busy_count > 0)
84 cv_wait(&ph->ph_cv, &ph->ph_mtx);
88 PFIL_TRY_WLOCK(struct pfil_head *ph)
90 mtx_lock(&ph->ph_mtx);
91 ph->ph_want_write = 1;
92 if (ph->ph_busy_count > 0) {
93 ph->ph_want_write = 0;
94 mtx_unlock(&ph->ph_mtx);
101 PFIL_WUNLOCK(struct pfil_head *ph)
103 ph->ph_want_write = 0;
104 cv_signal(&ph->ph_cv);
105 mtx_unlock(&ph->ph_mtx);
108 #define PFIL_LIST_LOCK() mtx_lock(&pfil_global_lock)
109 #define PFIL_LIST_UNLOCK() mtx_unlock(&pfil_global_lock)
112 * pfil_run_hooks() runs the specified packet filter hooks.
115 pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
116 int dir, struct inpcb *inp)
118 struct packet_filter_hook *pfh;
119 struct mbuf *m = *mp;
122 if (ph->ph_busy_count == -1)
125 * Prevent packet filtering from starving the modification of
126 * the packet filters. We would prefer a reader/writer locking
127 * mechanism with guaranteed ordering, though.
129 if (ph->ph_want_write) {
136 for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
137 pfh = TAILQ_NEXT(pfh, pfil_link)) {
138 if (pfh->pfil_func != NULL) {
139 rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir, inp);
140 if (rv != 0 || m == NULL)
151 * pfil_head_register() registers a pfil_head with the packet filter
155 pfil_head_register(struct pfil_head *ph)
157 struct pfil_head *lph;
160 LIST_FOREACH(lph, &pfil_head_list, ph_list)
161 if (ph->ph_type == lph->ph_type &&
162 ph->ph_un.phu_val == lph->ph_un.phu_val) {
168 if (mtx_initialized(&ph->ph_mtx)) { /* should not happen */
169 KASSERT((0), ("%s: allready initialized!", __func__));
172 ph->ph_busy_count = -1;
173 ph->ph_want_write = 1;
174 mtx_init(&ph->ph_mtx, "pfil_head_mtx", NULL, MTX_DEF);
175 cv_init(&ph->ph_cv, "pfil_head_cv");
176 mtx_lock(&ph->ph_mtx); /* XXX: race? */
179 TAILQ_INIT(&ph->ph_in);
180 TAILQ_INIT(&ph->ph_out);
183 LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
192 * pfil_head_unregister() removes a pfil_head from the packet filter
196 pfil_head_unregister(struct pfil_head *ph)
198 struct packet_filter_hook *pfh, *pfnext;
202 * LIST_REMOVE is safe for unlocked pfil_heads in ph_list.
203 * No need to WLOCK all of them.
205 LIST_REMOVE(ph, ph_list);
208 PFIL_WLOCK(ph); /* XXX: may sleep (cv_wait)! */
210 TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_link, pfnext)
212 TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_link, pfnext)
214 cv_destroy(&ph->ph_cv);
215 mtx_destroy(&ph->ph_mtx);
221 * pfil_head_get() returns the pfil_head for a given key/dlt.
224 pfil_head_get(int type, u_long val)
226 struct pfil_head *ph;
229 LIST_FOREACH(ph, &pfil_head_list, ph_list)
230 if (ph->ph_type == type && ph->ph_un.phu_val == val)
238 * pfil_add_hook() adds a function to the packet filter hook. the
240 * PFIL_IN call me on incoming packets
241 * PFIL_OUT call me on outgoing packets
242 * PFIL_ALL call me on all of the above
243 * PFIL_WAITOK OK to call malloc with M_WAITOK.
246 pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
247 void *arg, int flags, struct pfil_head *ph)
249 struct packet_filter_hook *pfh1 = NULL;
250 struct packet_filter_hook *pfh2 = NULL;
254 if (flags & PFIL_IN) {
255 pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
256 M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
262 if (flags & PFIL_OUT) {
263 pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
264 M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
272 if (flags & PFIL_WAITOK)
275 err = PFIL_TRY_WLOCK(ph);
281 if (flags & PFIL_IN) {
282 pfh1->pfil_func = func;
283 pfh1->pfil_arg = arg;
284 err = pfil_list_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
288 if (flags & PFIL_OUT) {
289 pfh2->pfil_func = func;
290 pfh2->pfil_arg = arg;
291 err = pfil_list_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
294 pfil_list_remove(&ph->ph_in, func, arg);
299 ph->ph_busy_count = 0;
307 free(pfh1, M_IFADDR);
309 free(pfh2, M_IFADDR);
314 * pfil_remove_hook removes a specific function from the packet filter
318 pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
319 void *arg, int flags, struct pfil_head *ph)
323 if (flags & PFIL_WAITOK)
326 err = PFIL_TRY_WLOCK(ph);
332 err = pfil_list_remove(&ph->ph_in, func, arg);
333 if ((err == 0) && (flags & PFIL_OUT))
334 err = pfil_list_remove(&ph->ph_out, func, arg);
336 if (TAILQ_EMPTY(&ph->ph_in) && TAILQ_EMPTY(&ph->ph_out))
337 ph->ph_busy_count = -1;
345 pfil_list_add(pfil_list_t *list, struct packet_filter_hook *pfh1, int flags)
347 struct packet_filter_hook *pfh;
350 * First make sure the hook is not already there.
352 TAILQ_FOREACH(pfh, list, pfil_link)
353 if (pfh->pfil_func == pfh1->pfil_func &&
354 pfh->pfil_arg == pfh1->pfil_arg)
357 * insert the input list in reverse order of the output list
358 * so that the same path is followed in or out of the kernel.
361 TAILQ_INSERT_HEAD(list, pfh1, pfil_link);
363 TAILQ_INSERT_TAIL(list, pfh1, pfil_link);
369 * pfil_list_remove is an internal function that takes a function off the
373 pfil_list_remove(pfil_list_t *list,
374 int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *arg)
376 struct packet_filter_hook *pfh;
378 TAILQ_FOREACH(pfh, list, pfil_link)
379 if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
380 TAILQ_REMOVE(list, pfh, pfil_link);