pfil.c revision 135920
1/*	$FreeBSD: head/sys/net/pfil.c 135920 2004-09-29 04:54:33Z mlaier $ */
2/*	$NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $	*/
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/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>
40#include <sys/lock.h>
41#include <sys/mutex.h>
42#include <sys/proc.h>
43#include <sys/queue.h>
44
45#include <net/if.h>
46#include <net/pfil.h>
47
48static struct mtx pfil_global_lock;
49
50MTX_SYSINIT(pfil_heads_lock, &pfil_global_lock, "pfil_head_list lock", MTX_DEF);
51
52static int pfil_list_add(pfil_list_t *, struct packet_filter_hook *, int);
53
54static int pfil_list_remove(pfil_list_t *,
55    int (*)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *);
56
57LIST_HEAD(, pfil_head) pfil_head_list =
58    LIST_HEAD_INITIALIZER(&pfil_head_list);
59
60static __inline void
61PFIL_RLOCK(struct pfil_head *ph)
62{
63	mtx_lock(&ph->ph_mtx);
64	ph->ph_busy_count++;
65	mtx_unlock(&ph->ph_mtx);
66}
67
68static __inline void
69PFIL_RUNLOCK(struct pfil_head *ph)
70{
71	mtx_lock(&ph->ph_mtx);
72	ph->ph_busy_count--;
73	if (ph->ph_busy_count == 0 && ph->ph_want_write)
74		cv_signal(&ph->ph_cv);
75	mtx_unlock(&ph->ph_mtx);
76}
77
78static __inline void
79PFIL_WLOCK(struct pfil_head *ph)
80{
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);
85}
86
87static __inline int
88PFIL_TRY_WLOCK(struct pfil_head *ph)
89{
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);
95		return EBUSY;
96	}
97	return 0;
98}
99
100static __inline void
101PFIL_WUNLOCK(struct pfil_head *ph)
102{
103	ph->ph_want_write = 0;
104	cv_signal(&ph->ph_cv);
105	mtx_unlock(&ph->ph_mtx);
106}
107
108#define PFIL_LIST_LOCK() mtx_lock(&pfil_global_lock)
109#define PFIL_LIST_UNLOCK() mtx_unlock(&pfil_global_lock)
110
111/*
112 * pfil_run_hooks() runs the specified packet filter hooks.
113 */
114int
115pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
116    int dir, struct inpcb *inp)
117{
118	struct packet_filter_hook *pfh;
119	struct mbuf *m = *mp;
120	int rv = 0;
121
122	if (ph->ph_busy_count == -1 || ph->ph_want_write)
123		return (0);
124
125	PFIL_RLOCK(ph);
126	for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
127	     pfh = TAILQ_NEXT(pfh, pfil_link)) {
128		if (pfh->pfil_func != NULL) {
129			rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir, inp);
130			if (rv != 0 || m == NULL)
131				break;
132		}
133	}
134	PFIL_RUNLOCK(ph);
135
136	*mp = m;
137	return (rv);
138}
139
140/*
141 * pfil_head_register() registers a pfil_head with the packet filter
142 * hook mechanism.
143 */
144int
145pfil_head_register(struct pfil_head *ph)
146{
147	struct pfil_head *lph;
148
149	PFIL_LIST_LOCK();
150	LIST_FOREACH(lph, &pfil_head_list, ph_list)
151		if (ph->ph_type == lph->ph_type &&
152		    ph->ph_un.phu_val == lph->ph_un.phu_val) {
153			PFIL_LIST_UNLOCK();
154			return EEXIST;
155		}
156	PFIL_LIST_UNLOCK();
157
158	if (mtx_initialized(&ph->ph_mtx)) {	/* should not happen */
159		KASSERT((0), ("%s: allready initialized!", __func__));
160		return EBUSY;
161	} else {
162		ph->ph_busy_count = -1;
163		ph->ph_want_write = 1;
164		mtx_init(&ph->ph_mtx, "pfil_head_mtx", NULL, MTX_DEF);
165		cv_init(&ph->ph_cv, "pfil_head_cv");
166		mtx_lock(&ph->ph_mtx);			/* XXX: race? */
167	}
168
169	TAILQ_INIT(&ph->ph_in);
170	TAILQ_INIT(&ph->ph_out);
171
172	PFIL_LIST_LOCK();
173	LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
174	PFIL_LIST_UNLOCK();
175
176	PFIL_WUNLOCK(ph);
177
178	return (0);
179}
180
181/*
182 * pfil_head_unregister() removes a pfil_head from the packet filter
183 * hook mechanism.
184 */
185int
186pfil_head_unregister(struct pfil_head *ph)
187{
188	struct packet_filter_hook *pfh, *pfnext;
189
190	PFIL_LIST_LOCK();
191	/*
192	 * LIST_REMOVE is safe for unlocked pfil_heads in ph_list.
193	 * No need to WLOCK all of them.
194	 */
195	LIST_REMOVE(ph, ph_list);
196	PFIL_LIST_UNLOCK();
197
198	PFIL_WLOCK(ph);			/* XXX: may sleep (cv_wait)! */
199
200	TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_link, pfnext)
201		free(pfh, M_IFADDR);
202	TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_link, pfnext)
203		free(pfh, M_IFADDR);
204	cv_destroy(&ph->ph_cv);
205	mtx_destroy(&ph->ph_mtx);
206
207	return (0);
208}
209
210/*
211 * pfil_head_get() returns the pfil_head for a given key/dlt.
212 */
213struct pfil_head *
214pfil_head_get(int type, u_long val)
215{
216	struct pfil_head *ph;
217
218	PFIL_LIST_LOCK();
219	LIST_FOREACH(ph, &pfil_head_list, ph_list)
220		if (ph->ph_type == type && ph->ph_un.phu_val == val)
221			break;
222	PFIL_LIST_UNLOCK();
223
224	return (ph);
225}
226
227/*
228 * pfil_add_hook() adds a function to the packet filter hook.  the
229 * flags are:
230 *	PFIL_IN		call me on incoming packets
231 *	PFIL_OUT	call me on outgoing packets
232 *	PFIL_ALL	call me on all of the above
233 *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
234 */
235int
236pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
237    void *arg, int flags, struct pfil_head *ph)
238{
239	struct packet_filter_hook *pfh1 = NULL;
240	struct packet_filter_hook *pfh2 = NULL;
241	int err;
242
243	/* Get memory */
244	if (flags & PFIL_IN) {
245		pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
246		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
247		if (pfh1 == NULL) {
248			err = ENOMEM;
249			goto error;
250		}
251	}
252	if (flags & PFIL_OUT) {
253		pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
254		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
255		if (pfh2 == NULL) {
256			err = ENOMEM;
257			goto error;
258		}
259	}
260
261	/* Lock */
262	if (flags & PFIL_WAITOK)
263		PFIL_WLOCK(ph);
264	else {
265		err = PFIL_TRY_WLOCK(ph);
266		if (err)
267			goto error;
268	}
269
270	/* Add */
271	if (flags & PFIL_IN) {
272		pfh1->pfil_func = func;
273		pfh1->pfil_arg = arg;
274		err = pfil_list_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
275		if (err)
276			goto done;
277	}
278	if (flags & PFIL_OUT) {
279		pfh2->pfil_func = func;
280		pfh2->pfil_arg = arg;
281		err = pfil_list_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
282		if (err) {
283			if (flags & PFIL_IN)
284				pfil_list_remove(&ph->ph_in, func, arg);
285			goto done;
286		}
287	}
288
289	ph->ph_busy_count = 0;
290	PFIL_WUNLOCK(ph);
291
292	return 0;
293done:
294	PFIL_WUNLOCK(ph);
295error:
296	if (pfh1 != NULL)
297		free(pfh1, M_IFADDR);
298	if (pfh2 != NULL)
299		free(pfh2, M_IFADDR);
300	return err;
301}
302
303/*
304 * pfil_remove_hook removes a specific function from the packet filter
305 * hook list.
306 */
307int
308pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
309    void *arg, int flags, struct pfil_head *ph)
310{
311	int err = 0;
312
313	if (flags & PFIL_WAITOK)
314		PFIL_WLOCK(ph);
315	else {
316		err = PFIL_TRY_WLOCK(ph);
317		if (err)
318			return err;
319	}
320
321	if (flags & PFIL_IN)
322		err = pfil_list_remove(&ph->ph_in, func, arg);
323	if ((err == 0) && (flags & PFIL_OUT))
324		err = pfil_list_remove(&ph->ph_out, func, arg);
325
326	if (TAILQ_EMPTY(&ph->ph_in) && TAILQ_EMPTY(&ph->ph_out))
327		ph->ph_busy_count = -1;
328
329	PFIL_WUNLOCK(ph);
330
331	return err;
332}
333
334static int
335pfil_list_add(pfil_list_t *list, struct packet_filter_hook *pfh1, int flags)
336{
337	struct packet_filter_hook *pfh;
338
339	/*
340	 * First make sure the hook is not already there.
341	 */
342	TAILQ_FOREACH(pfh, list, pfil_link)
343		if (pfh->pfil_func == pfh1->pfil_func &&
344		    pfh->pfil_arg == pfh1->pfil_arg)
345			return EEXIST;
346	/*
347	 * insert the input list in reverse order of the output list
348	 * so that the same path is followed in or out of the kernel.
349	 */
350	if (flags & PFIL_IN)
351		TAILQ_INSERT_HEAD(list, pfh1, pfil_link);
352	else
353		TAILQ_INSERT_TAIL(list, pfh1, pfil_link);
354
355	return 0;
356}
357
358/*
359 * pfil_list_remove is an internal function that takes a function off the
360 * specified list.
361 */
362static int
363pfil_list_remove(pfil_list_t *list,
364    int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *arg)
365{
366	struct packet_filter_hook *pfh;
367
368	TAILQ_FOREACH(pfh, list, pfil_link)
369		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
370			TAILQ_REMOVE(list, pfh, pfil_link);
371			free(pfh, M_IFADDR);
372			return 0;
373		}
374	return ENOENT;
375}
376