1 /* $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $ */
2 /* $DragonFly: src/sys/net/pfil.c,v 1.5 2006/10/19 18:44:00 swildner Exp $ */
5 * Copyright (c) 1996 Matthew R. Green
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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>
39 #include <sys/queue.h>
44 static int pfil_list_add(struct pfil_head
*,
45 int (*)(void *, struct mbuf
**, struct ifnet
*, int), void *, int);
47 static int pfil_list_remove(struct pfil_head
*,
48 int (*)(void *, struct mbuf
**, struct ifnet
*, int), void *, int);
50 LIST_HEAD(, pfil_head
) pfil_head_list
=
51 LIST_HEAD_INITIALIZER(&pfil_head_list
);
54 * pfil_run_hooks() runs the specified packet filter hooks.
57 pfil_run_hooks(struct pfil_head
*ph
, struct mbuf
**mp
, struct ifnet
*ifp
,
60 struct packet_filter_hook
*pfh
;
64 for (pfh
= pfil_hook_get(dir
, ph
); pfh
!= NULL
;
65 pfh
= TAILQ_NEXT(pfh
, pfil_link
)) {
66 if (pfh
->pfil_func
!= NULL
) {
67 rv
= (*pfh
->pfil_func
)(pfh
->pfil_arg
, &m
, ifp
, dir
);
68 if (rv
!= 0 || m
== NULL
)
78 * pfil_head_register() registers a pfil_head with the packet filter
82 pfil_head_register(struct pfil_head
*ph
)
84 struct pfil_head
*lph
;
86 for (lph
= LIST_FIRST(&pfil_head_list
); lph
!= NULL
;
87 lph
= LIST_NEXT(lph
, ph_list
)) {
88 if (ph
->ph_type
== lph
->ph_type
&&
89 ph
->ph_un
.phu_val
== lph
->ph_un
.phu_val
)
93 TAILQ_INIT(&ph
->ph_in
);
94 TAILQ_INIT(&ph
->ph_out
);
97 LIST_INSERT_HEAD(&pfil_head_list
, ph
, ph_list
);
103 * pfil_head_unregister() removes a pfil_head from the packet filter
107 pfil_head_unregister(struct pfil_head
*pfh
)
110 LIST_REMOVE(pfh
, ph_list
);
115 * pfil_head_get() returns the pfil_head for a given key/dlt.
118 pfil_head_get(int type
, u_long val
)
120 struct pfil_head
*ph
;
122 for (ph
= LIST_FIRST(&pfil_head_list
); ph
!= NULL
;
123 ph
= LIST_NEXT(ph
, ph_list
)) {
124 if (ph
->ph_type
== type
&&
125 ph
->ph_un
.phu_val
== val
)
133 * pfil_add_hook() adds a function to the packet filter hook. the
135 * PFIL_IN call me on incoming packets
136 * PFIL_OUT call me on outgoing packets
137 * PFIL_ALL call me on all of the above
138 * PFIL_WAITOK OK to call kmalloc with M_WAITOK.
141 pfil_add_hook(int (*func
)(void *, struct mbuf
**, struct ifnet
*, int),
142 void *arg
, int flags
, struct pfil_head
*ph
)
146 if (flags
& PFIL_IN
) {
147 err
= pfil_list_add(ph
, func
, arg
, flags
& ~PFIL_OUT
);
151 if (flags
& PFIL_OUT
) {
152 err
= pfil_list_add(ph
, func
, arg
, flags
& ~PFIL_IN
);
155 pfil_list_remove(ph
, func
, arg
, PFIL_IN
);
163 pfil_list_add(struct pfil_head
*ph
,
164 int (*func
)(void *, struct mbuf
**, struct ifnet
*, int), void *arg
,
167 struct packet_filter_hook
*pfh
;
170 list
= (flags
& PFIL_IN
) ? &ph
->ph_in
: &ph
->ph_out
;
173 * First make sure the hook is not already there.
175 for (pfh
= TAILQ_FIRST(list
); pfh
!= NULL
;
176 pfh
= TAILQ_NEXT(pfh
, pfil_link
)) {
177 if (pfh
->pfil_func
== func
&&
178 pfh
->pfil_arg
== arg
)
182 pfh
= (struct packet_filter_hook
*)kmalloc(sizeof(*pfh
), M_IFADDR
,
183 (flags
& PFIL_WAITOK
) ? M_WAITOK
: M_NOWAIT
);
187 pfh
->pfil_func
= func
;
191 * insert the input list in reverse order of the output list
192 * so that the same path is followed in or out of the kernel.
195 TAILQ_INSERT_HEAD(list
, pfh
, pfil_link
);
197 TAILQ_INSERT_TAIL(list
, pfh
, pfil_link
);
203 * pfil_remove_hook removes a specific function from the packet filter
207 pfil_remove_hook(int (*func
)(void *, struct mbuf
**, struct ifnet
*, int),
208 void *arg
, int flags
, struct pfil_head
*ph
)
213 err
= pfil_list_remove(ph
, func
, arg
, PFIL_IN
);
214 if ((err
== 0) && (flags
& PFIL_OUT
))
215 err
= pfil_list_remove(ph
, func
, arg
, PFIL_OUT
);
220 * pfil_list_remove is an internal function that takes a function off the
221 * specified list. Clear ph_hashooks if no functions remain on any list.
224 pfil_list_remove(struct pfil_head
*ph
,
225 int (*func
)(void *, struct mbuf
**, struct ifnet
*, int), void *arg
,
228 struct packet_filter_hook
*pfh
;
231 list
= (flags
& PFIL_IN
) ? &ph
->ph_in
: &ph
->ph_out
;
233 for (pfh
= TAILQ_FIRST(list
); pfh
!= NULL
;
234 pfh
= TAILQ_NEXT(pfh
, pfil_link
)) {
235 if (pfh
->pfil_func
== func
&& pfh
->pfil_arg
== arg
) {
236 TAILQ_REMOVE(list
, pfh
, pfil_link
);
237 kfree(pfh
, M_IFADDR
);
238 if (TAILQ_EMPTY(&ph
->ph_in
) && TAILQ_EMPTY(&ph
->ph_out
))