pfil.c revision 197952
1/*	$FreeBSD: head/sys/net/pfil.c 197952 2009-10-11 05:59:43Z julian $ */
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", MTX_DEF);
53
54static int pfil_list_add(pfil_list_t *, struct packet_filter_hook *, int);
55
56static int pfil_list_remove(pfil_list_t *,
57    int (*)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *);
58
59LIST_HEAD(pfilheadhead, pfil_head);
60VNET_DEFINE(struct pfilheadhead, pfil_head_list);
61#define	V_pfil_head_list	VNET(pfil_head_list)
62
63/*
64 * pfil_run_hooks() runs the specified packet filter hooks.
65 */
66int
67pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
68    int dir, struct inpcb *inp)
69{
70	struct rm_priotracker rmpt;
71	struct packet_filter_hook *pfh;
72	struct mbuf *m = *mp;
73	int rv = 0;
74
75	PFIL_RLOCK(ph, &rmpt);
76	KASSERT(ph->ph_nhooks >= 0, ("Pfil hook count dropped < 0"));
77	for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
78	     pfh = TAILQ_NEXT(pfh, pfil_link)) {
79		if (pfh->pfil_func != NULL) {
80			rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir, inp);
81			if (rv != 0 || m == NULL)
82				break;
83		}
84	}
85	PFIL_RUNLOCK(ph, &rmpt);
86
87	*mp = m;
88	return (rv);
89}
90
91/*
92 * pfil_head_register() registers a pfil_head with the packet filter
93 * hook mechanism.
94 */
95int
96pfil_head_register(struct pfil_head *ph)
97{
98	struct pfil_head *lph;
99
100	PFIL_LIST_LOCK();
101	LIST_FOREACH(lph, &V_pfil_head_list, ph_list) {
102		if (ph->ph_type == lph->ph_type &&
103		    ph->ph_un.phu_val == lph->ph_un.phu_val) {
104			PFIL_LIST_UNLOCK();
105			return EEXIST;
106		}
107	}
108	PFIL_LOCK_INIT(ph);
109	ph->ph_nhooks = 0;
110	TAILQ_INIT(&ph->ph_in);
111	TAILQ_INIT(&ph->ph_out);
112	LIST_INSERT_HEAD(&V_pfil_head_list, ph, ph_list);
113	PFIL_LIST_UNLOCK();
114	return (0);
115}
116
117/*
118 * pfil_head_unregister() removes a pfil_head from the packet filter hook
119 * mechanism.  The producer of the hook promises that all outstanding
120 * invocations of the hook have completed before it unregisters the hook.
121 */
122int
123pfil_head_unregister(struct pfil_head *ph)
124{
125	struct packet_filter_hook *pfh, *pfnext;
126
127	PFIL_LIST_LOCK();
128	LIST_REMOVE(ph, ph_list);
129	PFIL_LIST_UNLOCK();
130	TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_link, pfnext)
131		free(pfh, M_IFADDR);
132	TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_link, pfnext)
133		free(pfh, M_IFADDR);
134	PFIL_LOCK_DESTROY(ph);
135	return (0);
136}
137
138/*
139 * pfil_head_get() returns the pfil_head for a given key/dlt.
140 */
141struct pfil_head *
142pfil_head_get(int type, u_long val)
143{
144	struct pfil_head *ph;
145
146	PFIL_LIST_LOCK();
147	LIST_FOREACH(ph, &V_pfil_head_list, ph_list)
148		if (ph->ph_type == type && ph->ph_un.phu_val == val)
149			break;
150	PFIL_LIST_UNLOCK();
151
152	return (ph);
153}
154
155/*
156 * pfil_add_hook() adds a function to the packet filter hook.  the
157 * flags are:
158 *	PFIL_IN		call me on incoming packets
159 *	PFIL_OUT	call me on outgoing packets
160 *	PFIL_ALL	call me on all of the above
161 *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
162 */
163int
164pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int,
165  struct inpcb *), void *arg, int flags, struct pfil_head *ph)
166{
167	struct packet_filter_hook *pfh1 = NULL;
168	struct packet_filter_hook *pfh2 = NULL;
169	int err;
170
171	if (flags & PFIL_IN) {
172		pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
173		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
174		if (pfh1 == NULL) {
175			err = ENOMEM;
176			goto error;
177		}
178	}
179	if (flags & PFIL_OUT) {
180		pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
181		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
182		if (pfh2 == NULL) {
183			err = ENOMEM;
184			goto error;
185		}
186	}
187	PFIL_WLOCK(ph);
188	if (flags & PFIL_IN) {
189		pfh1->pfil_func = func;
190		pfh1->pfil_arg = arg;
191		err = pfil_list_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
192		if (err)
193			goto locked_error;
194		ph->ph_nhooks++;
195	}
196	if (flags & PFIL_OUT) {
197		pfh2->pfil_func = func;
198		pfh2->pfil_arg = arg;
199		err = pfil_list_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
200		if (err) {
201			if (flags & PFIL_IN)
202				pfil_list_remove(&ph->ph_in, func, arg);
203			goto locked_error;
204		}
205		ph->ph_nhooks++;
206	}
207	PFIL_WUNLOCK(ph);
208	return 0;
209locked_error:
210	PFIL_WUNLOCK(ph);
211error:
212	if (pfh1 != NULL)
213		free(pfh1, M_IFADDR);
214	if (pfh2 != NULL)
215		free(pfh2, M_IFADDR);
216	return err;
217}
218
219/*
220 * pfil_remove_hook removes a specific function from the packet filter
221 * hook list.
222 */
223int
224pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
225    void *arg, int flags, struct pfil_head *ph)
226{
227	int err = 0;
228
229	PFIL_WLOCK(ph);
230
231	if (flags & PFIL_IN) {
232		err = pfil_list_remove(&ph->ph_in, func, arg);
233		if (err == 0)
234			ph->ph_nhooks--;
235	}
236	if ((err == 0) && (flags & PFIL_OUT)) {
237		err = pfil_list_remove(&ph->ph_out, func, arg);
238		if (err == 0)
239			ph->ph_nhooks--;
240	}
241	PFIL_WUNLOCK(ph);
242
243	return err;
244}
245
246static int
247pfil_list_add(pfil_list_t *list, struct packet_filter_hook *pfh1, int flags)
248{
249	struct packet_filter_hook *pfh;
250
251	/*
252	 * First make sure the hook is not already there.
253	 */
254	TAILQ_FOREACH(pfh, list, pfil_link)
255		if (pfh->pfil_func == pfh1->pfil_func &&
256		    pfh->pfil_arg == pfh1->pfil_arg)
257			return EEXIST;
258	/*
259	 * insert the input list in reverse order of the output list
260	 * so that the same path is followed in or out of the kernel.
261	 */
262	if (flags & PFIL_IN)
263		TAILQ_INSERT_HEAD(list, pfh1, pfil_link);
264	else
265		TAILQ_INSERT_TAIL(list, pfh1, pfil_link);
266
267	return 0;
268}
269
270/*
271 * pfil_list_remove is an internal function that takes a function off the
272 * specified list.
273 */
274static int
275pfil_list_remove(pfil_list_t *list,
276    int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *arg)
277{
278	struct packet_filter_hook *pfh;
279
280	TAILQ_FOREACH(pfh, list, pfil_link)
281		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
282			TAILQ_REMOVE(list, pfh, pfil_link);
283			free(pfh, M_IFADDR);
284			return 0;
285		}
286	return ENOENT;
287}
288
289/****************
290 * Stuff that must be initialized for every instance
291 * (including the first of course).
292 */
293static int
294vnet_pfil_init(const void *unused)
295{
296	LIST_INIT(&V_pfil_head_list);
297	return (0);
298}
299
300/***********************
301 * Called for the removal of each instance.
302 */
303static int
304vnet_pfil_uninit(const void *unused)
305{
306	/*  XXX should panic if list is not empty */
307	return 0;
308}
309
310/* Define startup order. */
311#define	PFIL_SYSINIT_ORDER	SI_SUB_PROTO_BEGIN
312#define	PFIL_MODEVENT_ORDER	(SI_ORDER_FIRST) /* On boot slot in here. */
313#define	PFIL_VNET_ORDER		(PFIL_MODEVENT_ORDER + 2) /* Later still. */
314
315/*
316 * Starting up.
317 * VNET_SYSINIT is called for each existing vnet and each new vnet.
318 */
319VNET_SYSINIT(vnet_pfil_init, PFIL_SYSINIT_ORDER, PFIL_VNET_ORDER,
320	    vnet_pfil_init, NULL);
321
322/*
323 * Closing up shop. These are done in REVERSE ORDER,
324 * Not called on reboot.
325 * VNET_SYSUNINIT is called for each exiting vnet as it exits.
326 */
327VNET_SYSUNINIT(vnet_pfil_uninit, PFIL_SYSINIT_ORDER, PFIL_VNET_ORDER,
328	    vnet_pfil_uninit, NULL);
329
330