pfil.c revision 1.21 1 1.21 itojun /* $NetBSD: pfil.c,v 1.21 2004/06/22 12:50:41 itojun Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1996 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.6 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.20 lukem
31 1.20 lukem #include <sys/cdefs.h>
32 1.21 itojun __KERNEL_RCSID(0, "$NetBSD: pfil.c,v 1.21 2004/06/22 12:50:41 itojun Exp $");
33 1.1 mrg
34 1.1 mrg #include <sys/param.h>
35 1.1 mrg #include <sys/errno.h>
36 1.1 mrg #include <sys/malloc.h>
37 1.1 mrg #include <sys/socket.h>
38 1.1 mrg #include <sys/socketvar.h>
39 1.1 mrg #include <sys/systm.h>
40 1.1 mrg #include <sys/proc.h>
41 1.1 mrg #include <sys/queue.h>
42 1.1 mrg
43 1.1 mrg #include <net/if.h>
44 1.1 mrg #include <net/pfil.h>
45 1.1 mrg
46 1.13 darrenr static int pfil_list_add(pfil_list_t *,
47 1.16 thorpej int (*)(void *, struct mbuf **, struct ifnet *, int), void *, int);
48 1.16 thorpej
49 1.13 darrenr static int pfil_list_remove(pfil_list_t *,
50 1.16 thorpej int (*)(void *, struct mbuf **, struct ifnet *, int), void *);
51 1.16 thorpej
52 1.16 thorpej LIST_HEAD(, pfil_head) pfil_head_list =
53 1.16 thorpej LIST_HEAD_INITIALIZER(&pfil_head_list);
54 1.16 thorpej
55 1.16 thorpej /*
56 1.16 thorpej * pfil_run_hooks() runs the specified packet filter hooks.
57 1.16 thorpej */
58 1.16 thorpej int
59 1.16 thorpej pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
60 1.16 thorpej int dir)
61 1.16 thorpej {
62 1.16 thorpej struct packet_filter_hook *pfh;
63 1.21 itojun struct mbuf *m = NULL;
64 1.16 thorpej int rv = 0;
65 1.16 thorpej
66 1.21 itojun if (mp)
67 1.21 itojun m = *mp;
68 1.16 thorpej for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
69 1.16 thorpej pfh = TAILQ_NEXT(pfh, pfil_link)) {
70 1.16 thorpej if (pfh->pfil_func != NULL) {
71 1.21 itojun if (pfh->pfil_flags & PFIL_ALL) {
72 1.21 itojun rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp,
73 1.21 itojun dir);
74 1.21 itojun if (rv != 0 || m == NULL)
75 1.21 itojun break;
76 1.21 itojun } else {
77 1.21 itojun rv = (*pfh->pfil_func)(pfh->pfil_arg, mp, ifp,
78 1.21 itojun dir);
79 1.21 itojun if (rv != 0)
80 1.21 itojun break;
81 1.21 itojun }
82 1.16 thorpej }
83 1.16 thorpej }
84 1.16 thorpej
85 1.21 itojun if (mp)
86 1.21 itojun *mp = m;
87 1.16 thorpej return (rv);
88 1.16 thorpej }
89 1.1 mrg
90 1.16 thorpej /*
91 1.16 thorpej * pfil_head_register() registers a pfil_head with the packet filter
92 1.16 thorpej * hook mechanism.
93 1.16 thorpej */
94 1.16 thorpej int
95 1.16 thorpej pfil_head_register(struct pfil_head *ph)
96 1.1 mrg {
97 1.16 thorpej struct pfil_head *lph;
98 1.16 thorpej
99 1.16 thorpej for (lph = LIST_FIRST(&pfil_head_list); lph != NULL;
100 1.16 thorpej lph = LIST_NEXT(lph, ph_list)) {
101 1.19 thorpej if (ph->ph_type == lph->ph_type &&
102 1.19 thorpej ph->ph_un.phu_val == lph->ph_un.phu_val)
103 1.16 thorpej return EEXIST;
104 1.16 thorpej }
105 1.7 mrg
106 1.10 darrenr TAILQ_INIT(&ph->ph_in);
107 1.10 darrenr TAILQ_INIT(&ph->ph_out);
108 1.21 itojun TAILQ_INIT(&ph->ph_ifaddr);
109 1.21 itojun TAILQ_INIT(&ph->ph_newif);
110 1.16 thorpej
111 1.16 thorpej LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
112 1.16 thorpej
113 1.16 thorpej return (0);
114 1.16 thorpej }
115 1.16 thorpej
116 1.16 thorpej /*
117 1.16 thorpej * pfil_head_unregister() removes a pfil_head from the packet filter
118 1.16 thorpej * hook mechanism.
119 1.16 thorpej */
120 1.16 thorpej int
121 1.16 thorpej pfil_head_unregister(struct pfil_head *pfh)
122 1.16 thorpej {
123 1.16 thorpej
124 1.16 thorpej LIST_REMOVE(pfh, ph_list);
125 1.16 thorpej return (0);
126 1.16 thorpej }
127 1.16 thorpej
128 1.16 thorpej /*
129 1.16 thorpej * pfil_head_get() returns the pfil_head for a given key/dlt.
130 1.16 thorpej */
131 1.16 thorpej struct pfil_head *
132 1.19 thorpej pfil_head_get(int type, u_long val)
133 1.16 thorpej {
134 1.16 thorpej struct pfil_head *ph;
135 1.16 thorpej
136 1.16 thorpej for (ph = LIST_FIRST(&pfil_head_list); ph != NULL;
137 1.16 thorpej ph = LIST_NEXT(ph, ph_list)) {
138 1.19 thorpej if (ph->ph_type == type &&
139 1.19 thorpej ph->ph_un.phu_val == val)
140 1.16 thorpej break;
141 1.16 thorpej }
142 1.16 thorpej
143 1.16 thorpej return (ph);
144 1.1 mrg }
145 1.1 mrg
146 1.1 mrg /*
147 1.1 mrg * pfil_add_hook() adds a function to the packet filter hook. the
148 1.1 mrg * flags are:
149 1.1 mrg * PFIL_IN call me on incoming packets
150 1.1 mrg * PFIL_OUT call me on outgoing packets
151 1.1 mrg * PFIL_ALL call me on all of the above
152 1.21 itojun * PFIL_IFADDR call me on interface reconfig (mbuf ** is ioctl #)
153 1.21 itojun * PFIL_NEWIF call me on interface creation (mbuf ** is ignored)
154 1.1 mrg * PFIL_WAITOK OK to call malloc with M_WAITOK.
155 1.1 mrg */
156 1.13 darrenr int
157 1.16 thorpej pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
158 1.16 thorpej void *arg, int flags, struct pfil_head *ph)
159 1.1 mrg {
160 1.13 darrenr int err = 0;
161 1.1 mrg
162 1.16 thorpej if (flags & PFIL_IN) {
163 1.16 thorpej err = pfil_list_add(&ph->ph_in, func, arg, flags & ~PFIL_OUT);
164 1.16 thorpej if (err)
165 1.16 thorpej return err;
166 1.16 thorpej }
167 1.16 thorpej if (flags & PFIL_OUT) {
168 1.16 thorpej err = pfil_list_add(&ph->ph_out, func, arg, flags & ~PFIL_IN);
169 1.16 thorpej if (err) {
170 1.16 thorpej if (flags & PFIL_IN)
171 1.16 thorpej pfil_list_remove(&ph->ph_in, func, arg);
172 1.16 thorpej return err;
173 1.16 thorpej }
174 1.15 mycroft }
175 1.21 itojun if (flags & PFIL_IFADDR) {
176 1.21 itojun err = pfil_list_add(&ph->ph_ifaddr, func, arg,
177 1.21 itojun flags & ~PFIL_IFADDR);
178 1.21 itojun if (err) {
179 1.21 itojun if (flags & PFIL_IN)
180 1.21 itojun pfil_list_remove(&ph->ph_in, func, arg);
181 1.21 itojun if (flags & PFIL_OUT)
182 1.21 itojun pfil_list_remove(&ph->ph_out, func, arg);
183 1.21 itojun return err;
184 1.21 itojun }
185 1.21 itojun }
186 1.21 itojun if (flags & PFIL_NEWIF) {
187 1.21 itojun err = pfil_list_add(&ph->ph_newif, func, arg,
188 1.21 itojun flags & ~PFIL_NEWIF);
189 1.21 itojun if (err) {
190 1.21 itojun if (flags & PFIL_IN)
191 1.21 itojun pfil_list_remove(&ph->ph_in, func, arg);
192 1.21 itojun if (flags & PFIL_OUT)
193 1.21 itojun pfil_list_remove(&ph->ph_out, func, arg);
194 1.21 itojun if (flags & PFIL_IFADDR)
195 1.21 itojun pfil_list_remove(&ph->ph_ifaddr, func, arg);
196 1.21 itojun return err;
197 1.21 itojun }
198 1.21 itojun }
199 1.15 mycroft return 0;
200 1.1 mrg }
201 1.1 mrg
202 1.13 darrenr static int
203 1.16 thorpej pfil_list_add(pfil_list_t *list,
204 1.16 thorpej int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg,
205 1.16 thorpej int flags)
206 1.1 mrg {
207 1.1 mrg struct packet_filter_hook *pfh;
208 1.1 mrg
209 1.16 thorpej /*
210 1.16 thorpej * First make sure the hook is not already there.
211 1.16 thorpej */
212 1.16 thorpej for (pfh = TAILQ_FIRST(list); pfh != NULL;
213 1.16 thorpej pfh = TAILQ_NEXT(pfh, pfil_link)) {
214 1.16 thorpej if (pfh->pfil_func == func &&
215 1.16 thorpej pfh->pfil_arg == arg)
216 1.16 thorpej return EEXIST;
217 1.16 thorpej }
218 1.16 thorpej
219 1.1 mrg pfh = (struct packet_filter_hook *)malloc(sizeof(*pfh), M_IFADDR,
220 1.16 thorpej (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
221 1.1 mrg if (pfh == NULL)
222 1.13 darrenr return ENOMEM;
223 1.16 thorpej
224 1.1 mrg pfh->pfil_func = func;
225 1.16 thorpej pfh->pfil_arg = arg;
226 1.21 itojun pfh->pfil_flags = flags;
227 1.16 thorpej
228 1.7 mrg /*
229 1.7 mrg * insert the input list in reverse order of the output list
230 1.7 mrg * so that the same path is followed in or out of the kernel.
231 1.7 mrg */
232 1.12 darrenr if (flags & PFIL_IN)
233 1.12 darrenr TAILQ_INSERT_HEAD(list, pfh, pfil_link);
234 1.12 darrenr else
235 1.12 darrenr TAILQ_INSERT_TAIL(list, pfh, pfil_link);
236 1.16 thorpej
237 1.13 darrenr return 0;
238 1.1 mrg }
239 1.1 mrg
240 1.1 mrg /*
241 1.1 mrg * pfil_remove_hook removes a specific function from the packet filter
242 1.1 mrg * hook list.
243 1.1 mrg */
244 1.13 darrenr int
245 1.16 thorpej pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
246 1.16 thorpej void *arg, int flags, struct pfil_head *ph)
247 1.1 mrg {
248 1.13 darrenr int err = 0;
249 1.1 mrg
250 1.1 mrg if (flags & PFIL_IN)
251 1.16 thorpej err = pfil_list_remove(&ph->ph_in, func, arg);
252 1.13 darrenr if ((err == 0) && (flags & PFIL_OUT))
253 1.16 thorpej err = pfil_list_remove(&ph->ph_out, func, arg);
254 1.21 itojun if ((err == 0) && (flags & PFIL_IFADDR))
255 1.21 itojun err = pfil_list_remove(&ph->ph_ifaddr, func, arg);
256 1.21 itojun if ((err == 0) && (flags & PFIL_NEWIF))
257 1.21 itojun err = pfil_list_remove(&ph->ph_newif, func, arg);
258 1.13 darrenr return err;
259 1.1 mrg }
260 1.1 mrg
261 1.1 mrg /*
262 1.1 mrg * pfil_list_remove is an internal function that takes a function off the
263 1.1 mrg * specified list.
264 1.1 mrg */
265 1.13 darrenr static int
266 1.16 thorpej pfil_list_remove(pfil_list_t *list,
267 1.16 thorpej int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg)
268 1.1 mrg {
269 1.1 mrg struct packet_filter_hook *pfh;
270 1.1 mrg
271 1.16 thorpej for (pfh = TAILQ_FIRST(list); pfh != NULL;
272 1.16 thorpej pfh = TAILQ_NEXT(pfh, pfil_link)) {
273 1.16 thorpej if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
274 1.9 mrg TAILQ_REMOVE(list, pfh, pfil_link);
275 1.1 mrg free(pfh, M_IFADDR);
276 1.13 darrenr return 0;
277 1.1 mrg }
278 1.16 thorpej }
279 1.13 darrenr return ENOENT;
280 1.1 mrg }
281