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