vkernel - Improve memory image file startup
[dragonfly.git] / sys / net / pfil.c
CommitLineData
9e99d0a2 1/* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $ */
77e0b69a 2/* $DragonFly: src/sys/net/pfil.c,v 1.14 2008/09/20 06:08:13 sephe Exp $ */
9e99d0a2
JR
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/errno.h>
34#include <sys/malloc.h>
35#include <sys/socket.h>
36#include <sys/socketvar.h>
37#include <sys/systm.h>
38#include <sys/proc.h>
39#include <sys/queue.h>
40
41#include <net/if.h>
42#include <net/pfil.h>
f0fad8e1 43#include <net/netmsg2.h>
684a93c4 44#include <sys/mplock2.h>
f0fad8e1 45
77e0b69a
SZ
46#define PFIL_CFGPORT cpu_portfn(0)
47
48#define PFIL_GETMPLOCK(pfh) \
49do { \
50 if (((pfh)->pfil_flags & PFIL_MPSAFE) == 0) \
51 get_mplock(); \
52} while (0)
53
54#define PFIL_RELMPLOCK(pfh) \
55do { \
56 if (((pfh)->pfil_flags & PFIL_MPSAFE) == 0) \
57 rel_mplock(); \
58} while (0)
59
caa0af9f
SZ
60/*
61 * The packet filter hooks are designed for anything to call them to
62 * possibly intercept the packet.
63 */
64struct packet_filter_hook {
65 TAILQ_ENTRY(packet_filter_hook) pfil_link;
66 pfil_func_t pfil_func;
67 void *pfil_arg;
68 int pfil_flags;
69};
70
f0fad8e1
SZ
71struct netmsg_pfil {
72 struct netmsg pfil_nmsg;
73 pfil_func_t pfil_func;
74 void *pfil_arg;
75 int pfil_flags;
76 struct pfil_head *pfil_ph;
77};
9e99d0a2 78
d66da3ad
SZ
79static LIST_HEAD(, pfil_head) pfil_head_list =
80 LIST_HEAD_INITIALIZER(&pfil_head_list);
9e99d0a2 81
d66da3ad
SZ
82static pfil_list_t *pfil_list_alloc(void);
83static void pfil_list_free(pfil_list_t *);
84static void pfil_list_dup(const pfil_list_t *, pfil_list_t *,
85 const struct packet_filter_hook *);
86static void pfil_list_add(pfil_list_t *, pfil_func_t, void *, int);
87static struct packet_filter_hook *
88 pfil_list_find(const pfil_list_t *, pfil_func_t,
89 const void *);
90
91static void pfil_remove_hook_dispatch(struct netmsg *);
92static void pfil_add_hook_dispatch(struct netmsg *);
9e99d0a2
JR
93
94/*
95 * pfil_run_hooks() runs the specified packet filter hooks.
96 */
97int
98pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
99 int dir)
100{
101 struct packet_filter_hook *pfh;
102 struct mbuf *m = *mp;
7f8565be 103 pfil_list_t *list;
9e99d0a2
JR
104 int rv = 0;
105
7f8565be 106 if (dir == PFIL_IN)
d66da3ad 107 list = ph->ph_in;
7f8565be 108 else if (dir == PFIL_OUT)
d66da3ad 109 list = ph->ph_out;
7f8565be
SZ
110 else
111 return 0; /* XXX panic? */
112
113 TAILQ_FOREACH(pfh, list, pfil_link) {
9e99d0a2 114 if (pfh->pfil_func != NULL) {
77e0b69a
SZ
115 PFIL_GETMPLOCK(pfh);
116 rv = pfh->pfil_func(pfh->pfil_arg, &m, ifp, dir);
117 PFIL_RELMPLOCK(pfh);
118
9e99d0a2
JR
119 if (rv != 0 || m == NULL)
120 break;
121 }
122 }
123
124 *mp = m;
125 return (rv);
126}
127
128/*
129 * pfil_head_register() registers a pfil_head with the packet filter
130 * hook mechanism.
131 */
132int
133pfil_head_register(struct pfil_head *ph)
134{
135 struct pfil_head *lph;
136
94171836 137 LIST_FOREACH(lph, &pfil_head_list, ph_list) {
9e99d0a2
JR
138 if (ph->ph_type == lph->ph_type &&
139 ph->ph_un.phu_val == lph->ph_un.phu_val)
140 return EEXIST;
141 }
142
d66da3ad
SZ
143 ph->ph_in = pfil_list_alloc();
144 ph->ph_out = pfil_list_alloc();
afabe90c 145 ph->ph_hashooks = 0;
9e99d0a2
JR
146
147 LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
148
149 return (0);
150}
151
152/*
153 * pfil_head_unregister() removes a pfil_head from the packet filter
154 * hook mechanism.
155 */
156int
157pfil_head_unregister(struct pfil_head *pfh)
158{
9e99d0a2
JR
159 LIST_REMOVE(pfh, ph_list);
160 return (0);
161}
162
163/*
164 * pfil_head_get() returns the pfil_head for a given key/dlt.
165 */
166struct pfil_head *
167pfil_head_get(int type, u_long val)
168{
169 struct pfil_head *ph;
170
94171836
SZ
171 LIST_FOREACH(ph, &pfil_head_list, ph_list) {
172 if (ph->ph_type == type && ph->ph_un.phu_val == val)
9e99d0a2
JR
173 break;
174 }
9e99d0a2
JR
175 return (ph);
176}
177
f0fad8e1
SZ
178static void
179pfil_add_hook_dispatch(struct netmsg *nmsg)
9e99d0a2 180{
f0fad8e1
SZ
181 struct netmsg_pfil *pfilmsg = (struct netmsg_pfil *)nmsg;
182 pfil_func_t func = pfilmsg->pfil_func;
183 void *arg = pfilmsg->pfil_arg;
184 int flags = pfilmsg->pfil_flags;
185 struct pfil_head *ph = pfilmsg->pfil_ph;
d66da3ad
SZ
186 const struct packet_filter_hook *pfh;
187 pfil_list_t *list_in = NULL, *list_out = NULL;
188 pfil_list_t *old_list_in = NULL, *old_list_out = NULL;
9e99d0a2
JR
189 int err = 0;
190
d66da3ad
SZ
191 /* This probably should not happen ... */
192 if ((flags & (PFIL_IN | PFIL_OUT)) == 0)
193 goto reply; /* XXX panic? */
194
195 /*
196 * If pfil hooks exist on any of the requested lists,
197 * then bail out.
198 */
9e99d0a2 199 if (flags & PFIL_IN) {
d66da3ad
SZ
200 pfh = pfil_list_find(ph->ph_in, func, arg);
201 if (pfh != NULL) {
202 err = EEXIST;
f0fad8e1 203 goto reply;
d66da3ad 204 }
9e99d0a2
JR
205 }
206 if (flags & PFIL_OUT) {
d66da3ad
SZ
207 pfh = pfil_list_find(ph->ph_out, func, arg);
208 if (pfh != NULL) {
209 err = EEXIST;
210 goto reply;
9e99d0a2
JR
211 }
212 }
d66da3ad
SZ
213
214 /*
215 * Duplicate the requested lists, install new hooks
216 * on the duplication
217 */
218 if (flags & PFIL_IN) {
219 list_in = pfil_list_alloc();
220 pfil_list_dup(ph->ph_in, list_in, NULL);
221 pfil_list_add(list_in, func, arg, flags & ~PFIL_OUT);
222 }
223 if (flags & PFIL_OUT) {
224 list_out = pfil_list_alloc();
225 pfil_list_dup(ph->ph_out, list_out, NULL);
226 pfil_list_add(list_out, func, arg, flags & ~PFIL_IN);
227 }
228
229 /*
230 * Switch list pointers, but keep the old ones
231 */
232 if (list_in != NULL) {
233 old_list_in = ph->ph_in;
234 ph->ph_in = list_in;
235 }
236 if (list_out != NULL) {
237 old_list_out = ph->ph_out;
238 ph->ph_out = list_out;
239 }
240
241 /*
242 * Wait until everyone has finished the old lists iteration
243 */
244 netmsg_service_sync();
245 ph->ph_hashooks = 1;
246
247 /*
248 * Now it is safe to free the old lists, since no one sees it
249 */
250 if (old_list_in != NULL)
251 pfil_list_free(old_list_in);
252 if (old_list_out != NULL)
253 pfil_list_free(old_list_out);
f0fad8e1
SZ
254reply:
255 lwkt_replymsg(&nmsg->nm_lmsg, err);
256}
257
258/*
259 * pfil_add_hook() adds a function to the packet filter hook. the
260 * flags are:
261 * PFIL_IN call me on incoming packets
262 * PFIL_OUT call me on outgoing packets
263 * PFIL_ALL call me on all of the above
77e0b69a 264 * PFIL_MPSAFE call me without BGL
f0fad8e1
SZ
265 */
266int
267pfil_add_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
268{
269 struct netmsg_pfil pfilmsg;
270 struct netmsg *nmsg;
271
272 nmsg = &pfilmsg.pfil_nmsg;
48e7b118
MD
273 netmsg_init(nmsg, NULL, &curthread->td_msgport,
274 0, pfil_add_hook_dispatch);
f0fad8e1
SZ
275 pfilmsg.pfil_func = func;
276 pfilmsg.pfil_arg = arg;
277 pfilmsg.pfil_flags = flags;
278 pfilmsg.pfil_ph = ph;
279
280 return lwkt_domsg(PFIL_CFGPORT, &nmsg->nm_lmsg, 0);
9e99d0a2
JR
281}
282
d66da3ad
SZ
283static void
284pfil_remove_hook_dispatch(struct netmsg *nmsg)
9e99d0a2 285{
d66da3ad
SZ
286 struct netmsg_pfil *pfilmsg = (struct netmsg_pfil *)nmsg;
287 pfil_func_t func = pfilmsg->pfil_func;
288 void *arg = pfilmsg->pfil_arg;
289 int flags = pfilmsg->pfil_flags;
290 struct pfil_head *ph = pfilmsg->pfil_ph;
291 struct packet_filter_hook *skip_in = NULL, *skip_out = NULL;
292 pfil_list_t *list_in = NULL, *list_out = NULL;
293 pfil_list_t *old_list_in = NULL, *old_list_out = NULL;
294 int err = 0;
f0fad8e1 295
d66da3ad
SZ
296 /* This probably should not happen ... */
297 if ((flags & (PFIL_IN | PFIL_OUT)) == 0)
298 goto reply; /* XXX panic? */
9e99d0a2
JR
299
300 /*
d66da3ad
SZ
301 * The pfil hook should exist on all requested lists,
302 * if not just bail out
9e99d0a2 303 */
d66da3ad
SZ
304 if (flags & PFIL_IN) {
305 skip_in = pfil_list_find(ph->ph_in, func, arg);
306 if (!skip_in) {
307 err = ENOENT;
308 goto reply;
309 }
310 }
311 if (flags & PFIL_OUT) {
312 skip_out = pfil_list_find(ph->ph_out, func, arg);
313 if (!skip_out) {
314 err = ENOENT;
315 goto reply;
316 }
9e99d0a2
JR
317 }
318
d66da3ad
SZ
319 /*
320 * Duplicate the requested lists, but the pfil hook to
321 * be deleted is not copied
322 */
323 if (flags & PFIL_IN) {
324 KKASSERT(skip_in != NULL);
325 list_in = pfil_list_alloc();
326 pfil_list_dup(ph->ph_in, list_in, skip_in);
327 }
328 if (flags & PFIL_OUT) {
329 KKASSERT(skip_out != NULL);
330 list_out = pfil_list_alloc();
331 pfil_list_dup(ph->ph_out, list_out, skip_out);
332 }
9e99d0a2
JR
333
334 /*
d66da3ad 335 * Switch list pointers, but keep the old ones
9e99d0a2 336 */
d66da3ad
SZ
337 if (list_in != NULL) {
338 old_list_in = ph->ph_in;
339 ph->ph_in = list_in;
340 }
341 if (list_out != NULL) {
342 old_list_out = ph->ph_out;
343 ph->ph_out = list_out;
344 }
9e99d0a2 345
d66da3ad
SZ
346 /*
347 * Wait until everyone has finished the old lists iteration
348 */
349 if (TAILQ_EMPTY(ph->ph_in) && TAILQ_EMPTY(ph->ph_out))
350 ph->ph_hashooks = 0;
351 netmsg_service_sync();
f0fad8e1 352
d66da3ad
SZ
353 /*
354 * Now it is safe to free the old lists, since no one sees it
355 */
356 if (old_list_in != NULL)
357 pfil_list_free(old_list_in);
358 if (old_list_out != NULL)
359 pfil_list_free(old_list_out);
360reply:
f0fad8e1
SZ
361 lwkt_replymsg(&nmsg->nm_lmsg, err);
362}
363
9e99d0a2
JR
364/*
365 * pfil_remove_hook removes a specific function from the packet filter
366 * hook list.
367 */
368int
3bc01464 369pfil_remove_hook(pfil_func_t func, void *arg, int flags, struct pfil_head *ph)
9e99d0a2 370{
f0fad8e1
SZ
371 struct netmsg_pfil pfilmsg;
372 struct netmsg *nmsg;
9e99d0a2 373
f0fad8e1 374 nmsg = &pfilmsg.pfil_nmsg;
48e7b118
MD
375 netmsg_init(nmsg, NULL, &curthread->td_msgport,
376 0, pfil_remove_hook_dispatch);
f0fad8e1
SZ
377 pfilmsg.pfil_func = func;
378 pfilmsg.pfil_arg = arg;
379 pfilmsg.pfil_flags = flags;
380 pfilmsg.pfil_ph = ph;
381
382 return lwkt_domsg(PFIL_CFGPORT, &nmsg->nm_lmsg, 0);
9e99d0a2
JR
383}
384
d66da3ad
SZ
385static void
386pfil_list_add(pfil_list_t *list, pfil_func_t func, void *arg, int flags)
9e99d0a2
JR
387{
388 struct packet_filter_hook *pfh;
afabe90c 389
f0fad8e1
SZ
390 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
391
d66da3ad
SZ
392 pfh = kmalloc(sizeof(*pfh), M_IFADDR, M_WAITOK);
393
394 pfh->pfil_func = func;
395 pfh->pfil_arg = arg;
77d9cd11 396 pfh->pfil_flags = flags;
d66da3ad
SZ
397
398 /*
399 * Insert the input list in reverse order of the output list
400 * so that the same path is followed in or out of the kernel.
401 */
402 if (flags & PFIL_IN)
403 TAILQ_INSERT_HEAD(list, pfh, pfil_link);
404 else
405 TAILQ_INSERT_TAIL(list, pfh, pfil_link);
406}
407
408static void
409pfil_list_dup(const pfil_list_t *from, pfil_list_t *to,
410 const struct packet_filter_hook *skip)
411{
412 struct packet_filter_hook *pfh_to, *pfh_from;
413
414 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
415 KKASSERT(TAILQ_EMPTY(to));
416
417 TAILQ_FOREACH(pfh_from, from, pfil_link) {
418 if (pfh_from == skip)
419 continue;
420
421 pfh_to = kmalloc(sizeof(*pfh_to), M_IFADDR, M_WAITOK);
422 bcopy(pfh_from, pfh_to, sizeof(*pfh_to));
423
424 TAILQ_INSERT_TAIL(to, pfh_to, pfil_link);
425 }
426}
427
428static pfil_list_t *
429pfil_list_alloc(void)
430{
431 pfil_list_t *list;
432
433 list = kmalloc(sizeof(*list), M_IFADDR, M_WAITOK);
434 TAILQ_INIT(list);
435 return list;
436}
437
438static void
439pfil_list_free(pfil_list_t *list)
440{
441 struct packet_filter_hook *pfh;
442
443 while ((pfh = TAILQ_FIRST(list)) != NULL) {
444 TAILQ_REMOVE(list, pfh, pfil_link);
445 kfree(pfh, M_IFADDR);
446 }
447 kfree(list, M_IFADDR);
448}
449
450static struct packet_filter_hook *
451pfil_list_find(const pfil_list_t *list, pfil_func_t func, const void *arg)
452{
453 struct packet_filter_hook *pfh;
454
455 KKASSERT(&curthread->td_msgport == PFIL_CFGPORT);
9e99d0a2 456
94171836 457 TAILQ_FOREACH(pfh, list, pfil_link) {
d66da3ad
SZ
458 if (pfh->pfil_func == func && pfh->pfil_arg == arg)
459 return pfh;
9e99d0a2 460 }
d66da3ad 461 return NULL;
9e99d0a2 462}