pfil.c revision 1.29 1 1.29 christos /* $NetBSD: pfil.c,v 1.29 2016/12/26 23:21:49 christos Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.28 rmind * Copyright (c) 2013 Mindaugas Rasiukevicius <rmind at NetBSD org>
5 1.1 mrg * Copyright (c) 1996 Matthew R. Green
6 1.1 mrg * All rights reserved.
7 1.1 mrg *
8 1.1 mrg * Redistribution and use in source and binary forms, with or without
9 1.1 mrg * modification, are permitted provided that the following conditions
10 1.1 mrg * are met:
11 1.1 mrg * 1. Redistributions of source code must retain the above copyright
12 1.1 mrg * notice, this list of conditions and the following disclaimer.
13 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer in the
15 1.1 mrg * documentation and/or other materials provided with the distribution.
16 1.1 mrg *
17 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 mrg * SUCH DAMAGE.
28 1.1 mrg */
29 1.20 lukem
30 1.20 lukem #include <sys/cdefs.h>
31 1.29 christos __KERNEL_RCSID(0, "$NetBSD: pfil.c,v 1.29 2016/12/26 23:21:49 christos Exp $");
32 1.1 mrg
33 1.1 mrg #include <sys/param.h>
34 1.1 mrg #include <sys/systm.h>
35 1.1 mrg #include <sys/queue.h>
36 1.28 rmind #include <sys/kmem.h>
37 1.1 mrg
38 1.1 mrg #include <net/if.h>
39 1.1 mrg #include <net/pfil.h>
40 1.1 mrg
41 1.28 rmind #define MAX_HOOKS 8
42 1.16 thorpej
43 1.29 christos /* Func is either pfil_func_t or pfil_ifunc_t. */
44 1.29 christos typedef void (*pfil_polyfunc_t)(void);
45 1.29 christos
46 1.28 rmind typedef struct {
47 1.29 christos pfil_polyfunc_t pfil_func;
48 1.28 rmind void * pfil_arg;
49 1.28 rmind } pfil_hook_t;
50 1.28 rmind
51 1.28 rmind typedef struct {
52 1.28 rmind pfil_hook_t hooks[MAX_HOOKS];
53 1.28 rmind u_int nhooks;
54 1.28 rmind } pfil_list_t;
55 1.28 rmind
56 1.29 christos CTASSERT(PFIL_IN == 1);
57 1.29 christos CTASSERT(PFIL_OUT == 2);
58 1.29 christos
59 1.28 rmind struct pfil_head {
60 1.29 christos pfil_list_t ph_inout[2];
61 1.28 rmind pfil_list_t ph_ifaddr;
62 1.28 rmind pfil_list_t ph_ifevent;
63 1.28 rmind int ph_type;
64 1.28 rmind void * ph_key;
65 1.28 rmind LIST_ENTRY(pfil_head) ph_list;
66 1.28 rmind };
67 1.28 rmind
68 1.28 rmind static const int pfil_flag_cases[] = {
69 1.28 rmind PFIL_IN, PFIL_OUT, PFIL_IFADDR, PFIL_IFNET
70 1.28 rmind };
71 1.16 thorpej
72 1.28 rmind static LIST_HEAD(, pfil_head) pfil_head_list __read_mostly =
73 1.16 thorpej LIST_HEAD_INITIALIZER(&pfil_head_list);
74 1.16 thorpej
75 1.16 thorpej /*
76 1.28 rmind * pfil_head_create: create and register a packet filter head.
77 1.16 thorpej */
78 1.28 rmind pfil_head_t *
79 1.28 rmind pfil_head_create(int type, void *key)
80 1.16 thorpej {
81 1.28 rmind pfil_head_t *ph;
82 1.1 mrg
83 1.28 rmind if (pfil_head_get(type, key)) {
84 1.28 rmind return NULL;
85 1.16 thorpej }
86 1.28 rmind ph = kmem_zalloc(sizeof(pfil_head_t), KM_SLEEP);
87 1.28 rmind ph->ph_type = type;
88 1.28 rmind ph->ph_key = key;
89 1.16 thorpej
90 1.16 thorpej LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
91 1.28 rmind return ph;
92 1.16 thorpej }
93 1.16 thorpej
94 1.16 thorpej /*
95 1.28 rmind * pfil_head_destroy: remove and destroy a packet filter head.
96 1.16 thorpej */
97 1.28 rmind void
98 1.28 rmind pfil_head_destroy(pfil_head_t *pfh)
99 1.16 thorpej {
100 1.16 thorpej LIST_REMOVE(pfh, ph_list);
101 1.28 rmind kmem_free(pfh, sizeof(pfil_head_t));
102 1.16 thorpej }
103 1.16 thorpej
104 1.16 thorpej /*
105 1.28 rmind * pfil_head_get: returns the packer filter head for a given key.
106 1.16 thorpej */
107 1.28 rmind pfil_head_t *
108 1.28 rmind pfil_head_get(int type, void *key)
109 1.16 thorpej {
110 1.28 rmind pfil_head_t *ph;
111 1.16 thorpej
112 1.27 dyoung LIST_FOREACH(ph, &pfil_head_list, ph_list) {
113 1.28 rmind if (ph->ph_type == type && ph->ph_key == key)
114 1.16 thorpej break;
115 1.16 thorpej }
116 1.28 rmind return ph;
117 1.1 mrg }
118 1.1 mrg
119 1.28 rmind static pfil_list_t *
120 1.28 rmind pfil_hook_get(int dir, pfil_head_t *ph)
121 1.1 mrg {
122 1.28 rmind switch (dir) {
123 1.28 rmind case PFIL_IN:
124 1.28 rmind case PFIL_OUT:
125 1.29 christos return &ph->ph_inout[dir];
126 1.28 rmind case PFIL_IFADDR:
127 1.28 rmind return &ph->ph_ifaddr;
128 1.28 rmind case PFIL_IFNET:
129 1.28 rmind return &ph->ph_ifevent;
130 1.21 itojun }
131 1.28 rmind return NULL;
132 1.1 mrg }
133 1.1 mrg
134 1.13 darrenr static int
135 1.29 christos pfil_list_add(pfil_list_t *phlist, pfil_polyfunc_t func, void *arg, int flags)
136 1.1 mrg {
137 1.28 rmind const u_int nhooks = phlist->nhooks;
138 1.28 rmind pfil_hook_t *pfh;
139 1.1 mrg
140 1.28 rmind /* Check if we have a free slot. */
141 1.28 rmind if (nhooks == MAX_HOOKS) {
142 1.28 rmind return ENOSPC;
143 1.28 rmind }
144 1.28 rmind KASSERT(nhooks < MAX_HOOKS);
145 1.28 rmind
146 1.28 rmind /* Make sure the hook is not already added. */
147 1.28 rmind for (u_int i = 0; i < nhooks; i++) {
148 1.28 rmind pfh = &phlist->hooks[i];
149 1.26 dyoung if (pfh->pfil_func == func && pfh->pfil_arg == arg)
150 1.16 thorpej return EEXIST;
151 1.16 thorpej }
152 1.16 thorpej
153 1.28 rmind /*
154 1.28 rmind * Finally, add the hook. Note: for PFIL_IN we insert the hooks in
155 1.28 rmind * reverse order of the PFIL_OUT so that the same path is followed
156 1.28 rmind * in or out of the kernel.
157 1.28 rmind */
158 1.28 rmind if (flags & PFIL_IN) {
159 1.28 rmind /* XXX: May want to revisit this later; */
160 1.28 rmind size_t len = sizeof(pfil_hook_t) * nhooks;
161 1.28 rmind pfh = &phlist->hooks[0];
162 1.28 rmind memmove(&phlist->hooks[1], pfh, len);
163 1.28 rmind } else {
164 1.28 rmind pfh = &phlist->hooks[nhooks];
165 1.28 rmind }
166 1.28 rmind phlist->nhooks++;
167 1.16 thorpej
168 1.1 mrg pfh->pfil_func = func;
169 1.16 thorpej pfh->pfil_arg = arg;
170 1.13 darrenr return 0;
171 1.1 mrg }
172 1.1 mrg
173 1.1 mrg /*
174 1.28 rmind * pfil_add_hook: add a function (hook) to the packet filter head.
175 1.28 rmind * The possible flags are:
176 1.28 rmind *
177 1.28 rmind * PFIL_IN call on incoming packets
178 1.28 rmind * PFIL_OUT call on outgoing packets
179 1.28 rmind * PFIL_ALL call on all of the above
180 1.1 mrg */
181 1.13 darrenr int
182 1.28 rmind pfil_add_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
183 1.1 mrg {
184 1.28 rmind int error = 0;
185 1.28 rmind
186 1.28 rmind KASSERT(func != NULL);
187 1.29 christos KASSERT((flags & ~PFIL_ALL) == 0);
188 1.28 rmind
189 1.28 rmind for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
190 1.28 rmind const int fcase = pfil_flag_cases[i];
191 1.28 rmind pfil_list_t *phlist;
192 1.1 mrg
193 1.28 rmind if ((flags & fcase) == 0) {
194 1.28 rmind continue;
195 1.28 rmind }
196 1.28 rmind phlist = pfil_hook_get(fcase, ph);
197 1.29 christos error = pfil_list_add(phlist, (pfil_polyfunc_t)func, arg, flags);
198 1.29 christos if (error) {
199 1.28 rmind break;
200 1.28 rmind }
201 1.28 rmind }
202 1.28 rmind if (error) {
203 1.28 rmind pfil_remove_hook(func, arg, flags, ph);
204 1.28 rmind }
205 1.28 rmind return error;
206 1.1 mrg }
207 1.1 mrg
208 1.1 mrg /*
209 1.29 christos * pfil_add_hook: add an interface-event function (hook) to the packet
210 1.29 christos * filter head. The possible flags are:
211 1.29 christos *
212 1.29 christos * PFIL_IFADDR call on interface reconfig (mbuf is ioctl #)
213 1.29 christos * PFIL_IFNET call on interface attach/detach (mbuf is PFIL_IFNET_*)
214 1.29 christos */
215 1.29 christos int
216 1.29 christos pfil_add_ihook(pfil_ifunc_t func, void *arg, int flags, pfil_head_t *ph)
217 1.29 christos {
218 1.29 christos pfil_list_t *phlist;
219 1.29 christos
220 1.29 christos KASSERT(flags == PFIL_IFADDR || flags == PFIL_IFNET);
221 1.29 christos phlist = pfil_hook_get(flags, ph);
222 1.29 christos return pfil_list_add(phlist, (pfil_polyfunc_t)func, arg, flags);
223 1.29 christos }
224 1.29 christos
225 1.29 christos /*
226 1.28 rmind * pfil_list_remove: remove the hook from a specified list.
227 1.1 mrg */
228 1.13 darrenr static int
229 1.29 christos pfil_list_remove(pfil_list_t *phlist, pfil_polyfunc_t func, void *arg)
230 1.1 mrg {
231 1.28 rmind const u_int nhooks = phlist->nhooks;
232 1.1 mrg
233 1.28 rmind for (u_int i = 0; i < nhooks; i++) {
234 1.28 rmind pfil_hook_t *last, *pfh = &phlist->hooks[i];
235 1.28 rmind
236 1.28 rmind if (pfh->pfil_func != func || pfh->pfil_arg != arg) {
237 1.28 rmind continue;
238 1.28 rmind }
239 1.28 rmind if ((last = &phlist->hooks[nhooks - 1]) != pfh) {
240 1.28 rmind memcpy(pfh, last, sizeof(pfil_hook_t));
241 1.1 mrg }
242 1.28 rmind phlist->nhooks--;
243 1.28 rmind return 0;
244 1.16 thorpej }
245 1.13 darrenr return ENOENT;
246 1.1 mrg }
247 1.28 rmind
248 1.28 rmind /*
249 1.28 rmind * pfil_remove_hook: remove the hook from the packet filter head.
250 1.28 rmind */
251 1.28 rmind int
252 1.28 rmind pfil_remove_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
253 1.28 rmind {
254 1.28 rmind for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
255 1.28 rmind const int fcase = pfil_flag_cases[i];
256 1.28 rmind pfil_list_t *pflist;
257 1.28 rmind
258 1.28 rmind if ((flags & fcase) == 0) {
259 1.28 rmind continue;
260 1.28 rmind }
261 1.28 rmind pflist = pfil_hook_get(fcase, ph);
262 1.29 christos (void)pfil_list_remove(pflist, (pfil_polyfunc_t)func, arg);
263 1.28 rmind }
264 1.28 rmind return 0;
265 1.28 rmind }
266 1.28 rmind
267 1.29 christos int
268 1.29 christos pfil_remove_ihook(pfil_ifunc_t func, void *arg, int flags, pfil_head_t *ph)
269 1.29 christos {
270 1.29 christos pfil_list_t *pflist;
271 1.29 christos
272 1.29 christos KASSERT(flags == PFIL_IFADDR || flags == PFIL_IFNET);
273 1.29 christos pflist = pfil_hook_get(flags, ph);
274 1.29 christos (void)pfil_list_remove(pflist, (pfil_polyfunc_t)func, arg);
275 1.29 christos return 0;
276 1.29 christos }
277 1.29 christos
278 1.28 rmind /*
279 1.28 rmind * pfil_run_hooks: run the specified packet filter hooks.
280 1.28 rmind */
281 1.28 rmind int
282 1.28 rmind pfil_run_hooks(pfil_head_t *ph, struct mbuf **mp, ifnet_t *ifp, int dir)
283 1.28 rmind {
284 1.29 christos struct mbuf *m = mp ? *mp : NULL;
285 1.28 rmind pfil_list_t *phlist;
286 1.28 rmind int ret = 0;
287 1.28 rmind
288 1.29 christos KASSERT((dir & ~PFIL_ALL) == 0);
289 1.29 christos if (__predict_false((phlist = &ph->ph_inout[dir]) == NULL)) {
290 1.28 rmind return ret;
291 1.28 rmind }
292 1.28 rmind
293 1.28 rmind for (u_int i = 0; i < phlist->nhooks; i++) {
294 1.28 rmind pfil_hook_t *pfh = &phlist->hooks[i];
295 1.29 christos pfil_func_t func = (pfil_func_t)pfh->pfil_func;
296 1.28 rmind
297 1.29 christos ret = (*func)(pfh->pfil_arg, &m, ifp, dir);
298 1.29 christos if (m == NULL || ret)
299 1.28 rmind break;
300 1.28 rmind }
301 1.28 rmind
302 1.29 christos if (mp) {
303 1.28 rmind *mp = m;
304 1.28 rmind }
305 1.28 rmind return ret;
306 1.28 rmind }
307 1.29 christos
308 1.29 christos void
309 1.29 christos pfil_run_addrhooks(pfil_head_t *ph, u_long cmd, struct ifaddr *ifa)
310 1.29 christos {
311 1.29 christos pfil_list_t *phlist = &ph->ph_ifaddr;
312 1.29 christos
313 1.29 christos for (u_int i = 0; i < phlist->nhooks; i++) {
314 1.29 christos pfil_hook_t *pfh = &phlist->hooks[i];
315 1.29 christos pfil_ifunc_t func = (pfil_ifunc_t)pfh->pfil_func;
316 1.29 christos (*func)(pfh->pfil_arg, cmd, (void *)ifa);
317 1.29 christos }
318 1.29 christos }
319 1.29 christos
320 1.29 christos void
321 1.29 christos pfil_run_ifhooks(pfil_head_t *ph, u_long cmd, struct ifnet *ifp)
322 1.29 christos {
323 1.29 christos pfil_list_t *phlist = &ph->ph_ifevent;
324 1.29 christos
325 1.29 christos for (u_int i = 0; i < phlist->nhooks; i++) {
326 1.29 christos pfil_hook_t *pfh = &phlist->hooks[i];
327 1.29 christos pfil_ifunc_t func = (pfil_ifunc_t)pfh->pfil_func;
328 1.29 christos (*func)(pfh->pfil_arg, cmd, (void *)ifp);
329 1.29 christos }
330 1.29 christos }
331