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