pfil.c revision 136258
1/*	$FreeBSD: head/sys/net/pfil.c 136258 2004-10-08 12:07:20Z 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	/*
123	 * Prevent packet filtering from starving the modification of
124	 * the packet filters. We would prefer a reader/writer locking
125	 * mechanism with guaranteed ordering, though.
126	 */
127	if (ph->ph_busy_count == -1 || ph->ph_want_write) {
128		m_freem(*mp);
129		*mp = NULL;
130		return (ENOBUFS);
131	}
132
133	PFIL_RLOCK(ph);
134	for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
135	     pfh = TAILQ_NEXT(pfh, pfil_link)) {
136		if (pfh->pfil_func != NULL) {
137			rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir, inp);
138			if (rv != 0 || m == NULL)
139				break;
140		}
141	}
142	PFIL_RUNLOCK(ph);
143
144	*mp = m;
145	return (rv);
146}
147
148/*
149 * pfil_head_register() registers a pfil_head with the packet filter
150 * hook mechanism.
151 */
152int
153pfil_head_register(struct pfil_head *ph)
154{
155	struct pfil_head *lph;
156
157	PFIL_LIST_LOCK();
158	LIST_FOREACH(lph, &pfil_head_list, ph_list)
159		if (ph->ph_type == lph->ph_type &&
160		    ph->ph_un.phu_val == lph->ph_un.phu_val) {
161			PFIL_LIST_UNLOCK();
162			return EEXIST;
163		}
164	PFIL_LIST_UNLOCK();
165
166	if (mtx_initialized(&ph->ph_mtx)) {	/* should not happen */
167		KASSERT((0), ("%s: allready initialized!", __func__));
168		return EBUSY;
169	} else {
170		ph->ph_busy_count = -1;
171		ph->ph_want_write = 1;
172		mtx_init(&ph->ph_mtx, "pfil_head_mtx", NULL, MTX_DEF);
173		cv_init(&ph->ph_cv, "pfil_head_cv");
174		mtx_lock(&ph->ph_mtx);			/* XXX: race? */
175	}
176
177	TAILQ_INIT(&ph->ph_in);
178	TAILQ_INIT(&ph->ph_out);
179
180	PFIL_LIST_LOCK();
181	LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
182	PFIL_LIST_UNLOCK();
183
184	PFIL_WUNLOCK(ph);
185
186	return (0);
187}
188
189/*
190 * pfil_head_unregister() removes a pfil_head from the packet filter
191 * hook mechanism.
192 */
193int
194pfil_head_unregister(struct pfil_head *ph)
195{
196	struct packet_filter_hook *pfh, *pfnext;
197
198	PFIL_LIST_LOCK();
199	/*
200	 * LIST_REMOVE is safe for unlocked pfil_heads in ph_list.
201	 * No need to WLOCK all of them.
202	 */
203	LIST_REMOVE(ph, ph_list);
204	PFIL_LIST_UNLOCK();
205
206	PFIL_WLOCK(ph);			/* XXX: may sleep (cv_wait)! */
207
208	TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_link, pfnext)
209		free(pfh, M_IFADDR);
210	TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_link, pfnext)
211		free(pfh, M_IFADDR);
212	cv_destroy(&ph->ph_cv);
213	mtx_destroy(&ph->ph_mtx);
214
215	return (0);
216}
217
218/*
219 * pfil_head_get() returns the pfil_head for a given key/dlt.
220 */
221struct pfil_head *
222pfil_head_get(int type, u_long val)
223{
224	struct pfil_head *ph;
225
226	PFIL_LIST_LOCK();
227	LIST_FOREACH(ph, &pfil_head_list, ph_list)
228		if (ph->ph_type == type && ph->ph_un.phu_val == val)
229			break;
230	PFIL_LIST_UNLOCK();
231
232	return (ph);
233}
234
235/*
236 * pfil_add_hook() adds a function to the packet filter hook.  the
237 * flags are:
238 *	PFIL_IN		call me on incoming packets
239 *	PFIL_OUT	call me on outgoing packets
240 *	PFIL_ALL	call me on all of the above
241 *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
242 */
243int
244pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
245    void *arg, int flags, struct pfil_head *ph)
246{
247	struct packet_filter_hook *pfh1 = NULL;
248	struct packet_filter_hook *pfh2 = NULL;
249	int err;
250
251	/* Get memory */
252	if (flags & PFIL_IN) {
253		pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
254		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
255		if (pfh1 == NULL) {
256			err = ENOMEM;
257			goto error;
258		}
259	}
260	if (flags & PFIL_OUT) {
261		pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
262		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
263		if (pfh2 == NULL) {
264			err = ENOMEM;
265			goto error;
266		}
267	}
268
269	/* Lock */
270	if (flags & PFIL_WAITOK)
271		PFIL_WLOCK(ph);
272	else {
273		err = PFIL_TRY_WLOCK(ph);
274		if (err)
275			goto error;
276	}
277
278	/* Add */
279	if (flags & PFIL_IN) {
280		pfh1->pfil_func = func;
281		pfh1->pfil_arg = arg;
282		err = pfil_list_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
283		if (err)
284			goto done;
285	}
286	if (flags & PFIL_OUT) {
287		pfh2->pfil_func = func;
288		pfh2->pfil_arg = arg;
289		err = pfil_list_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
290		if (err) {
291			if (flags & PFIL_IN)
292				pfil_list_remove(&ph->ph_in, func, arg);
293			goto done;
294		}
295	}
296
297	ph->ph_busy_count = 0;
298	PFIL_WUNLOCK(ph);
299
300	return 0;
301done:
302	PFIL_WUNLOCK(ph);
303error:
304	if (pfh1 != NULL)
305		free(pfh1, M_IFADDR);
306	if (pfh2 != NULL)
307		free(pfh2, M_IFADDR);
308	return err;
309}
310
311/*
312 * pfil_remove_hook removes a specific function from the packet filter
313 * hook list.
314 */
315int
316pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
317    void *arg, int flags, struct pfil_head *ph)
318{
319	int err = 0;
320
321	if (flags & PFIL_WAITOK)
322		PFIL_WLOCK(ph);
323	else {
324		err = PFIL_TRY_WLOCK(ph);
325		if (err)
326			return err;
327	}
328
329	if (flags & PFIL_IN)
330		err = pfil_list_remove(&ph->ph_in, func, arg);
331	if ((err == 0) && (flags & PFIL_OUT))
332		err = pfil_list_remove(&ph->ph_out, func, arg);
333
334	if (TAILQ_EMPTY(&ph->ph_in) && TAILQ_EMPTY(&ph->ph_out))
335		ph->ph_busy_count = -1;
336
337	PFIL_WUNLOCK(ph);
338
339	return err;
340}
341
342static int
343pfil_list_add(pfil_list_t *list, struct packet_filter_hook *pfh1, int flags)
344{
345	struct packet_filter_hook *pfh;
346
347	/*
348	 * First make sure the hook is not already there.
349	 */
350	TAILQ_FOREACH(pfh, list, pfil_link)
351		if (pfh->pfil_func == pfh1->pfil_func &&
352		    pfh->pfil_arg == pfh1->pfil_arg)
353			return EEXIST;
354	/*
355	 * insert the input list in reverse order of the output list
356	 * so that the same path is followed in or out of the kernel.
357	 */
358	if (flags & PFIL_IN)
359		TAILQ_INSERT_HEAD(list, pfh1, pfil_link);
360	else
361		TAILQ_INSERT_TAIL(list, pfh1, pfil_link);
362
363	return 0;
364}
365
366/*
367 * pfil_list_remove is an internal function that takes a function off the
368 * specified list.
369 */
370static int
371pfil_list_remove(pfil_list_t *list,
372    int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *arg)
373{
374	struct packet_filter_hook *pfh;
375
376	TAILQ_FOREACH(pfh, list, pfil_link)
377		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
378			TAILQ_REMOVE(list, pfh, pfil_link);
379			free(pfh, M_IFADDR);
380			return 0;
381		}
382	return ENOENT;
383}
384