pfil.c revision 254769
1/*	$FreeBSD: head/sys/net/pfil.c 254769 2013-08-24 10:13:59Z andre $ */
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/lock.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>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/proc.h>
45#include <sys/queue.h>
46
47#include <net/if.h>
48#include <net/pfil.h>
49
50static struct mtx pfil_global_lock;
51
52MTX_SYSINIT(pfil_heads_lock, &pfil_global_lock, "pfil_head_list lock",
53  MTX_DEF);
54
55static int pfil_list_add(pfil_list_t *, struct packet_filter_hook *, int);
56
57static int pfil_list_remove(pfil_list_t *,
58    int (*)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
59    void *);
60
61LIST_HEAD(pfilheadhead, pfil_head);
62VNET_DEFINE(struct pfilheadhead, pfil_head_list);
63#define	V_pfil_head_list	VNET(pfil_head_list)
64VNET_DEFINE(struct rmlock, pfil_lock);
65#define	V_pfil_lock	VNET(pfil_lock)
66
67/*
68 * pfil_run_hooks() runs the specified packet filter hooks.
69 */
70int
71pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
72    int dir, struct inpcb *inp)
73{
74	struct rm_priotracker rmpt;
75	struct packet_filter_hook *pfh;
76	struct mbuf *m = *mp;
77	int rv = 0;
78
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,
85			    inp);
86			if (rv != 0 || m == NULL)
87				break;
88		}
89	}
90	PFIL_RUNLOCK(ph, &rmpt);
91	*mp = m;
92	return (rv);
93}
94
95/*
96 * pfil_try_rlock() acquires rm reader lock for specified head
97 * if this is immediately possible.
98 */
99int
100pfil_try_rlock(struct pfil_head *ph, struct rm_priotracker *tracker)
101{
102
103	return (PFIL_TRY_RLOCK(ph, tracker));
104}
105
106/*
107 * pfil_rlock() acquires rm reader lock for specified head.
108 */
109void
110pfil_rlock(struct pfil_head *ph, struct rm_priotracker *tracker)
111{
112
113	PFIL_RLOCK(ph, tracker);
114}
115
116/*
117 * pfil_runlock() releases reader lock for specified head.
118 */
119void
120pfil_runlock(struct pfil_head *ph, struct rm_priotracker *tracker)
121{
122
123	PFIL_RUNLOCK(ph, tracker);
124}
125
126/*
127 * pfil_wlock() acquires writer lock for specified head.
128 */
129void
130pfil_wlock(struct pfil_head *ph)
131{
132
133	PFIL_WLOCK(ph);
134}
135
136/*
137 * pfil_wunlock() releases writer lock for specified head.
138 */
139void
140pfil_wunlock(struct pfil_head *ph)
141{
142
143	PFIL_WUNLOCK(ph);
144}
145
146/*
147 * pfil_wowned() returns a non-zero value if the current thread owns
148 * an exclusive lock.
149 */
150int
151pfil_wowned(struct pfil_head *ph)
152{
153
154	return (PFIL_WOWNED(ph));
155}
156/*
157 * pfil_head_register() registers a pfil_head with the packet filter hook
158 * mechanism.
159 */
160int
161pfil_head_register(struct pfil_head *ph)
162{
163	struct pfil_head *lph;
164
165	PFIL_LIST_LOCK();
166	LIST_FOREACH(lph, &V_pfil_head_list, ph_list) {
167		if (ph->ph_type == lph->ph_type &&
168		    ph->ph_un.phu_val == lph->ph_un.phu_val) {
169			PFIL_LIST_UNLOCK();
170			return (EEXIST);
171		}
172	}
173	PFIL_LOCK_INIT(ph);
174	ph->ph_nhooks = 0;
175	TAILQ_INIT(&ph->ph_in);
176	TAILQ_INIT(&ph->ph_out);
177	LIST_INSERT_HEAD(&V_pfil_head_list, ph, ph_list);
178	PFIL_LIST_UNLOCK();
179	return (0);
180}
181
182/*
183 * pfil_head_unregister() removes a pfil_head from the packet filter hook
184 * mechanism.  The producer of the hook promises that all outstanding
185 * invocations of the hook have completed before it unregisters the hook.
186 */
187int
188pfil_head_unregister(struct pfil_head *ph)
189{
190	struct packet_filter_hook *pfh, *pfnext;
191
192	PFIL_LIST_LOCK();
193	LIST_REMOVE(ph, ph_list);
194	PFIL_LIST_UNLOCK();
195	TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_link, pfnext)
196		free(pfh, M_IFADDR);
197	TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_link, pfnext)
198		free(pfh, M_IFADDR);
199	PFIL_LOCK_DESTROY(ph);
200	return (0);
201}
202
203/*
204 * pfil_head_get() returns the pfil_head for a given key/dlt.
205 */
206struct pfil_head *
207pfil_head_get(int type, u_long val)
208{
209	struct pfil_head *ph;
210
211	PFIL_LIST_LOCK();
212	LIST_FOREACH(ph, &V_pfil_head_list, ph_list)
213		if (ph->ph_type == type && ph->ph_un.phu_val == val)
214			break;
215	PFIL_LIST_UNLOCK();
216	return (ph);
217}
218
219/*
220 * pfil_add_hook() adds a function to the packet filter hook.  the
221 * flags are:
222 *	PFIL_IN		call me on incoming packets
223 *	PFIL_OUT	call me on outgoing packets
224 *	PFIL_ALL	call me on all of the above
225 *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
226 */
227int
228pfil_add_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
229{
230	struct packet_filter_hook *pfh1 = NULL;
231	struct packet_filter_hook *pfh2 = NULL;
232	int err;
233
234	if (flags & PFIL_IN) {
235		pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
236		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
237		if (pfh1 == NULL) {
238			err = ENOMEM;
239			goto error;
240		}
241	}
242	if (flags & PFIL_OUT) {
243		pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
244		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
245		if (pfh2 == NULL) {
246			err = ENOMEM;
247			goto error;
248		}
249	}
250	PFIL_WLOCK(ph);
251	if (flags & PFIL_IN) {
252		pfh1->pfil_func = func;
253		pfh1->pfil_arg = arg;
254		err = pfil_list_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
255		if (err)
256			goto locked_error;
257		ph->ph_nhooks++;
258	}
259	if (flags & PFIL_OUT) {
260		pfh2->pfil_func = func;
261		pfh2->pfil_arg = arg;
262		err = pfil_list_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
263		if (err) {
264			if (flags & PFIL_IN)
265				pfil_list_remove(&ph->ph_in, func, arg);
266			goto locked_error;
267		}
268		ph->ph_nhooks++;
269	}
270	PFIL_WUNLOCK(ph);
271	return (0);
272locked_error:
273	PFIL_WUNLOCK(ph);
274error:
275	if (pfh1 != NULL)
276		free(pfh1, M_IFADDR);
277	if (pfh2 != NULL)
278		free(pfh2, M_IFADDR);
279	return (err);
280}
281
282/*
283 * pfil_remove_hook removes a specific function from the packet filter hook
284 * list.
285 */
286int
287pfil_remove_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
288{
289	int err = 0;
290
291	PFIL_WLOCK(ph);
292	if (flags & PFIL_IN) {
293		err = pfil_list_remove(&ph->ph_in, func, arg);
294		if (err == 0)
295			ph->ph_nhooks--;
296	}
297	if ((err == 0) && (flags & PFIL_OUT)) {
298		err = pfil_list_remove(&ph->ph_out, func, arg);
299		if (err == 0)
300			ph->ph_nhooks--;
301	}
302	PFIL_WUNLOCK(ph);
303	return (err);
304}
305
306static int
307pfil_list_add(pfil_list_t *list, struct packet_filter_hook *pfh1, int flags)
308{
309	struct packet_filter_hook *pfh;
310
311	/*
312	 * First make sure the hook is not already there.
313	 */
314	TAILQ_FOREACH(pfh, list, pfil_link)
315		if (pfh->pfil_func == pfh1->pfil_func &&
316		    pfh->pfil_arg == pfh1->pfil_arg)
317			return (EEXIST);
318
319	/*
320	 * Insert the input list in reverse order of the output list so that
321	 * the same path is followed in or out of the kernel.
322	 */
323	if (flags & PFIL_IN)
324		TAILQ_INSERT_HEAD(list, pfh1, pfil_link);
325	else
326		TAILQ_INSERT_TAIL(list, pfh1, pfil_link);
327	return (0);
328}
329
330/*
331 * pfil_list_remove is an internal function that takes a function off the
332 * specified list.
333 */
334static int
335pfil_list_remove(pfil_list_t *list, pfil_func_t func, void *arg)
336{
337	struct packet_filter_hook *pfh;
338
339	TAILQ_FOREACH(pfh, list, pfil_link)
340		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
341			TAILQ_REMOVE(list, pfh, pfil_link);
342			free(pfh, M_IFADDR);
343			return (0);
344		}
345	return (ENOENT);
346}
347
348/*
349 * Stuff that must be initialized for every instance (including the first of
350 * course).
351 */
352static int
353vnet_pfil_init(const void *unused)
354{
355
356	LIST_INIT(&V_pfil_head_list);
357	PFIL_LOCK_INIT_REAL(&V_pfil_lock, "shared");
358	return (0);
359}
360
361/*
362 * Called for the removal of each instance.
363 */
364static int
365vnet_pfil_uninit(const void *unused)
366{
367
368	/*  XXX should panic if list is not empty */
369	PFIL_LOCK_DESTROY_REAL(&V_pfil_lock);
370	return (0);
371}
372
373/* Define startup order. */
374#define	PFIL_SYSINIT_ORDER	SI_SUB_PROTO_BEGIN
375#define	PFIL_MODEVENT_ORDER	(SI_ORDER_FIRST) /* On boot slot in here. */
376#define	PFIL_VNET_ORDER		(PFIL_MODEVENT_ORDER + 2) /* Later still. */
377
378/*
379 * Starting up.
380 *
381 * VNET_SYSINIT is called for each existing vnet and each new vnet.
382 */
383VNET_SYSINIT(vnet_pfil_init, PFIL_SYSINIT_ORDER, PFIL_VNET_ORDER,
384    vnet_pfil_init, NULL);
385
386/*
387 * Closing up shop.  These are done in REVERSE ORDER.  Not called on reboot.
388 *
389 * VNET_SYSUNINIT is called for each exiting vnet as it exits.
390 */
391VNET_SYSUNINIT(vnet_pfil_uninit, PFIL_SYSINIT_ORDER, PFIL_VNET_ORDER,
392    vnet_pfil_uninit, NULL);
393