xref: /freebsd/sys/net/pfil.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*	$FreeBSD$ */
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 
50 static struct mtx pfil_global_lock;
51 
52 MTX_SYSINIT(pfil_heads_lock, &pfil_global_lock, "pfil_head_list lock", MTX_DEF);
53 
54 static int pfil_list_add(pfil_list_t *, struct packet_filter_hook *, int);
55 
56 static int pfil_list_remove(pfil_list_t *,
57     int (*)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *);
58 
59 LIST_HEAD(, pfil_head) pfil_head_list =
60     LIST_HEAD_INITIALIZER(&pfil_head_list);
61 
62 /*
63  * pfil_run_hooks() runs the specified packet filter hooks.
64  */
65 int
66 pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
67     int dir, struct inpcb *inp)
68 {
69 	struct rm_priotracker rmpt;
70 	struct packet_filter_hook *pfh;
71 	struct mbuf *m = *mp;
72 	int rv = 0;
73 
74 	PFIL_RLOCK(ph, &rmpt);
75 	KASSERT(ph->ph_nhooks >= 0, ("Pfil hook count dropped < 0"));
76 	for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
77 	     pfh = TAILQ_NEXT(pfh, pfil_link)) {
78 		if (pfh->pfil_func != NULL) {
79 			rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir, inp);
80 			if (rv != 0 || m == NULL)
81 				break;
82 		}
83 	}
84 	PFIL_RUNLOCK(ph, &rmpt);
85 
86 	*mp = m;
87 	return (rv);
88 }
89 
90 /*
91  * pfil_head_register() registers a pfil_head with the packet filter
92  * hook mechanism.
93  */
94 int
95 pfil_head_register(struct pfil_head *ph)
96 {
97 	struct pfil_head *lph;
98 
99 	PFIL_LIST_LOCK();
100 	LIST_FOREACH(lph, &pfil_head_list, ph_list)
101 		if (ph->ph_type == lph->ph_type &&
102 		    ph->ph_un.phu_val == lph->ph_un.phu_val) {
103 			PFIL_LIST_UNLOCK();
104 			return EEXIST;
105 		}
106 	PFIL_LIST_UNLOCK();
107 
108 	PFIL_LOCK_INIT(ph);
109 	PFIL_WLOCK(ph);
110 	ph->ph_nhooks = 0;
111 
112 	TAILQ_INIT(&ph->ph_in);
113 	TAILQ_INIT(&ph->ph_out);
114 
115 	PFIL_LIST_LOCK();
116 	LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
117 	PFIL_LIST_UNLOCK();
118 
119 	PFIL_WUNLOCK(ph);
120 
121 	return (0);
122 }
123 
124 /*
125  * pfil_head_unregister() removes a pfil_head from the packet filter
126  * hook mechanism.
127  */
128 int
129 pfil_head_unregister(struct pfil_head *ph)
130 {
131 	struct packet_filter_hook *pfh, *pfnext;
132 
133 	PFIL_LIST_LOCK();
134 	/*
135 	 * LIST_REMOVE is safe for unlocked pfil_heads in ph_list.
136 	 * No need to WLOCK all of them.
137 	 */
138 	LIST_REMOVE(ph, ph_list);
139 	PFIL_LIST_UNLOCK();
140 
141 	PFIL_WLOCK(ph);
142 
143 	TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_link, pfnext)
144 		free(pfh, M_IFADDR);
145 	TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_link, pfnext)
146 		free(pfh, M_IFADDR);
147 	PFIL_LOCK_DESTROY(ph);
148 
149 	return (0);
150 }
151 
152 /*
153  * pfil_head_get() returns the pfil_head for a given key/dlt.
154  */
155 struct pfil_head *
156 pfil_head_get(int type, u_long val)
157 {
158 	struct pfil_head *ph;
159 
160 	PFIL_LIST_LOCK();
161 	LIST_FOREACH(ph, &pfil_head_list, ph_list)
162 		if (ph->ph_type == type && ph->ph_un.phu_val == val)
163 			break;
164 	PFIL_LIST_UNLOCK();
165 
166 	return (ph);
167 }
168 
169 /*
170  * pfil_add_hook() adds a function to the packet filter hook.  the
171  * flags are:
172  *	PFIL_IN		call me on incoming packets
173  *	PFIL_OUT	call me on outgoing packets
174  *	PFIL_ALL	call me on all of the above
175  *	PFIL_WAITOK	OK to call malloc with M_WAITOK.
176  */
177 int
178 pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
179     void *arg, int flags, struct pfil_head *ph)
180 {
181 	struct packet_filter_hook *pfh1 = NULL;
182 	struct packet_filter_hook *pfh2 = NULL;
183 	int err;
184 
185 	/* Get memory */
186 	if (flags & PFIL_IN) {
187 		pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
188 		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
189 		if (pfh1 == NULL) {
190 			err = ENOMEM;
191 			goto error;
192 		}
193 	}
194 	if (flags & PFIL_OUT) {
195 		pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
196 		    M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
197 		if (pfh2 == NULL) {
198 			err = ENOMEM;
199 			goto error;
200 		}
201 	}
202 
203 	/* Lock */
204 	PFIL_WLOCK(ph);
205 
206 	/* Add */
207 	if (flags & PFIL_IN) {
208 		pfh1->pfil_func = func;
209 		pfh1->pfil_arg = arg;
210 		err = pfil_list_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
211 		if (err)
212 			goto done;
213 		ph->ph_nhooks++;
214 	}
215 	if (flags & PFIL_OUT) {
216 		pfh2->pfil_func = func;
217 		pfh2->pfil_arg = arg;
218 		err = pfil_list_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
219 		if (err) {
220 			if (flags & PFIL_IN)
221 				pfil_list_remove(&ph->ph_in, func, arg);
222 			goto done;
223 		}
224 		ph->ph_nhooks++;
225 	}
226 
227 	PFIL_WUNLOCK(ph);
228 
229 	return 0;
230 done:
231 	PFIL_WUNLOCK(ph);
232 error:
233 	if (pfh1 != NULL)
234 		free(pfh1, M_IFADDR);
235 	if (pfh2 != NULL)
236 		free(pfh2, M_IFADDR);
237 	return err;
238 }
239 
240 /*
241  * pfil_remove_hook removes a specific function from the packet filter
242  * hook list.
243  */
244 int
245 pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
246     void *arg, int flags, struct pfil_head *ph)
247 {
248 	int err = 0;
249 
250 	PFIL_WLOCK(ph);
251 
252 	if (flags & PFIL_IN) {
253 		err = pfil_list_remove(&ph->ph_in, func, arg);
254 		if (err == 0)
255 			ph->ph_nhooks--;
256 	}
257 	if ((err == 0) && (flags & PFIL_OUT)) {
258 		err = pfil_list_remove(&ph->ph_out, func, arg);
259 		if (err == 0)
260 			ph->ph_nhooks--;
261 	}
262 	PFIL_WUNLOCK(ph);
263 
264 	return err;
265 }
266 
267 static int
268 pfil_list_add(pfil_list_t *list, struct packet_filter_hook *pfh1, int flags)
269 {
270 	struct packet_filter_hook *pfh;
271 
272 	/*
273 	 * First make sure the hook is not already there.
274 	 */
275 	TAILQ_FOREACH(pfh, list, pfil_link)
276 		if (pfh->pfil_func == pfh1->pfil_func &&
277 		    pfh->pfil_arg == pfh1->pfil_arg)
278 			return EEXIST;
279 	/*
280 	 * insert the input list in reverse order of the output list
281 	 * so that the same path is followed in or out of the kernel.
282 	 */
283 	if (flags & PFIL_IN)
284 		TAILQ_INSERT_HEAD(list, pfh1, pfil_link);
285 	else
286 		TAILQ_INSERT_TAIL(list, pfh1, pfil_link);
287 
288 	return 0;
289 }
290 
291 /*
292  * pfil_list_remove is an internal function that takes a function off the
293  * specified list.
294  */
295 static int
296 pfil_list_remove(pfil_list_t *list,
297     int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *), void *arg)
298 {
299 	struct packet_filter_hook *pfh;
300 
301 	TAILQ_FOREACH(pfh, list, pfil_link)
302 		if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
303 			TAILQ_REMOVE(list, pfh, pfil_link);
304 			free(pfh, M_IFADDR);
305 			return 0;
306 		}
307 	return ENOENT;
308 }
309