pfil.c revision 1.28 1 1.28 rmind /* $NetBSD: pfil.c,v 1.28 2013/06/29 21:06:58 rmind 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.28 rmind __KERNEL_RCSID(0, "$NetBSD: pfil.c,v 1.28 2013/06/29 21:06:58 rmind 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.28 rmind typedef struct {
44 1.28 rmind pfil_func_t pfil_func;
45 1.28 rmind void * pfil_arg;
46 1.28 rmind } pfil_hook_t;
47 1.28 rmind
48 1.28 rmind typedef struct {
49 1.28 rmind pfil_hook_t hooks[MAX_HOOKS];
50 1.28 rmind u_int nhooks;
51 1.28 rmind } pfil_list_t;
52 1.28 rmind
53 1.28 rmind struct pfil_head {
54 1.28 rmind pfil_list_t ph_in;
55 1.28 rmind pfil_list_t ph_out;
56 1.28 rmind pfil_list_t ph_ifaddr;
57 1.28 rmind pfil_list_t ph_ifevent;
58 1.28 rmind int ph_type;
59 1.28 rmind void * ph_key;
60 1.28 rmind LIST_ENTRY(pfil_head) ph_list;
61 1.28 rmind };
62 1.28 rmind
63 1.28 rmind static const int pfil_flag_cases[] = {
64 1.28 rmind PFIL_IN, PFIL_OUT, PFIL_IFADDR, PFIL_IFNET
65 1.28 rmind };
66 1.16 thorpej
67 1.28 rmind static LIST_HEAD(, pfil_head) pfil_head_list __read_mostly =
68 1.16 thorpej LIST_HEAD_INITIALIZER(&pfil_head_list);
69 1.16 thorpej
70 1.16 thorpej /*
71 1.28 rmind * pfil_head_create: create and register a packet filter head.
72 1.16 thorpej */
73 1.28 rmind pfil_head_t *
74 1.28 rmind pfil_head_create(int type, void *key)
75 1.16 thorpej {
76 1.28 rmind pfil_head_t *ph;
77 1.1 mrg
78 1.28 rmind if (pfil_head_get(type, key)) {
79 1.28 rmind return NULL;
80 1.16 thorpej }
81 1.28 rmind ph = kmem_zalloc(sizeof(pfil_head_t), KM_SLEEP);
82 1.28 rmind ph->ph_type = type;
83 1.28 rmind ph->ph_key = key;
84 1.16 thorpej
85 1.16 thorpej LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
86 1.28 rmind return ph;
87 1.16 thorpej }
88 1.16 thorpej
89 1.16 thorpej /*
90 1.28 rmind * pfil_head_destroy: remove and destroy a packet filter head.
91 1.16 thorpej */
92 1.28 rmind void
93 1.28 rmind pfil_head_destroy(pfil_head_t *pfh)
94 1.16 thorpej {
95 1.16 thorpej LIST_REMOVE(pfh, ph_list);
96 1.28 rmind kmem_free(pfh, sizeof(pfil_head_t));
97 1.16 thorpej }
98 1.16 thorpej
99 1.16 thorpej /*
100 1.28 rmind * pfil_head_get: returns the packer filter head for a given key.
101 1.16 thorpej */
102 1.28 rmind pfil_head_t *
103 1.28 rmind pfil_head_get(int type, void *key)
104 1.16 thorpej {
105 1.28 rmind pfil_head_t *ph;
106 1.16 thorpej
107 1.27 dyoung LIST_FOREACH(ph, &pfil_head_list, ph_list) {
108 1.28 rmind if (ph->ph_type == type && ph->ph_key == key)
109 1.16 thorpej break;
110 1.16 thorpej }
111 1.28 rmind return ph;
112 1.1 mrg }
113 1.1 mrg
114 1.28 rmind static pfil_list_t *
115 1.28 rmind pfil_hook_get(int dir, pfil_head_t *ph)
116 1.1 mrg {
117 1.28 rmind switch (dir) {
118 1.28 rmind case PFIL_IN:
119 1.28 rmind return &ph->ph_in;
120 1.28 rmind case PFIL_OUT:
121 1.28 rmind return &ph->ph_out;
122 1.28 rmind case PFIL_IFADDR:
123 1.28 rmind return &ph->ph_ifaddr;
124 1.28 rmind case PFIL_IFNET:
125 1.28 rmind return &ph->ph_ifevent;
126 1.21 itojun }
127 1.28 rmind return NULL;
128 1.1 mrg }
129 1.1 mrg
130 1.13 darrenr static int
131 1.28 rmind pfil_list_add(pfil_list_t *phlist, pfil_func_t func, void *arg, int flags)
132 1.1 mrg {
133 1.28 rmind const u_int nhooks = phlist->nhooks;
134 1.28 rmind pfil_hook_t *pfh;
135 1.1 mrg
136 1.28 rmind /* Check if we have a free slot. */
137 1.28 rmind if (nhooks == MAX_HOOKS) {
138 1.28 rmind return ENOSPC;
139 1.28 rmind }
140 1.28 rmind KASSERT(nhooks < MAX_HOOKS);
141 1.28 rmind
142 1.28 rmind /* Make sure the hook is not already added. */
143 1.28 rmind for (u_int i = 0; i < nhooks; i++) {
144 1.28 rmind pfh = &phlist->hooks[i];
145 1.26 dyoung if (pfh->pfil_func == func && pfh->pfil_arg == arg)
146 1.16 thorpej return EEXIST;
147 1.16 thorpej }
148 1.16 thorpej
149 1.28 rmind /*
150 1.28 rmind * Finally, add the hook. Note: for PFIL_IN we insert the hooks in
151 1.28 rmind * reverse order of the PFIL_OUT so that the same path is followed
152 1.28 rmind * in or out of the kernel.
153 1.28 rmind */
154 1.28 rmind if (flags & PFIL_IN) {
155 1.28 rmind /* XXX: May want to revisit this later; */
156 1.28 rmind size_t len = sizeof(pfil_hook_t) * nhooks;
157 1.28 rmind pfh = &phlist->hooks[0];
158 1.28 rmind memmove(&phlist->hooks[1], pfh, len);
159 1.28 rmind } else {
160 1.28 rmind pfh = &phlist->hooks[nhooks];
161 1.28 rmind }
162 1.28 rmind phlist->nhooks++;
163 1.16 thorpej
164 1.1 mrg pfh->pfil_func = func;
165 1.16 thorpej pfh->pfil_arg = arg;
166 1.13 darrenr return 0;
167 1.1 mrg }
168 1.1 mrg
169 1.1 mrg /*
170 1.28 rmind * pfil_add_hook: add a function (hook) to the packet filter head.
171 1.28 rmind * The possible flags are:
172 1.28 rmind *
173 1.28 rmind * PFIL_IN call on incoming packets
174 1.28 rmind * PFIL_OUT call on outgoing packets
175 1.28 rmind * PFIL_ALL call on all of the above
176 1.28 rmind * PFIL_IFADDR call on interface reconfig (mbuf is ioctl #)
177 1.28 rmind * PFIL_IFNET call on interface attach/detach (mbuf is PFIL_IFNET_*)
178 1.1 mrg */
179 1.13 darrenr int
180 1.28 rmind pfil_add_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
181 1.1 mrg {
182 1.28 rmind int error = 0;
183 1.28 rmind
184 1.28 rmind KASSERT(func != NULL);
185 1.28 rmind
186 1.28 rmind for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
187 1.28 rmind const int fcase = pfil_flag_cases[i];
188 1.28 rmind pfil_list_t *phlist;
189 1.1 mrg
190 1.28 rmind if ((flags & fcase) == 0) {
191 1.28 rmind continue;
192 1.28 rmind }
193 1.28 rmind phlist = pfil_hook_get(fcase, ph);
194 1.28 rmind if ((error = pfil_list_add(phlist, func, arg, flags)) != 0) {
195 1.28 rmind break;
196 1.28 rmind }
197 1.28 rmind }
198 1.28 rmind if (error) {
199 1.28 rmind pfil_remove_hook(func, arg, flags, ph);
200 1.28 rmind }
201 1.28 rmind return error;
202 1.1 mrg }
203 1.1 mrg
204 1.1 mrg /*
205 1.28 rmind * pfil_list_remove: remove the hook from a specified list.
206 1.1 mrg */
207 1.13 darrenr static int
208 1.28 rmind pfil_list_remove(pfil_list_t *phlist, pfil_func_t func, void *arg)
209 1.1 mrg {
210 1.28 rmind const u_int nhooks = phlist->nhooks;
211 1.1 mrg
212 1.28 rmind for (u_int i = 0; i < nhooks; i++) {
213 1.28 rmind pfil_hook_t *last, *pfh = &phlist->hooks[i];
214 1.28 rmind
215 1.28 rmind if (pfh->pfil_func != func || pfh->pfil_arg != arg) {
216 1.28 rmind continue;
217 1.28 rmind }
218 1.28 rmind if ((last = &phlist->hooks[nhooks - 1]) != pfh) {
219 1.28 rmind memcpy(pfh, last, sizeof(pfil_hook_t));
220 1.1 mrg }
221 1.28 rmind phlist->nhooks--;
222 1.28 rmind return 0;
223 1.16 thorpej }
224 1.13 darrenr return ENOENT;
225 1.1 mrg }
226 1.28 rmind
227 1.28 rmind /*
228 1.28 rmind * pfil_remove_hook: remove the hook from the packet filter head.
229 1.28 rmind */
230 1.28 rmind int
231 1.28 rmind pfil_remove_hook(pfil_func_t func, void *arg, int flags, pfil_head_t *ph)
232 1.28 rmind {
233 1.28 rmind for (u_int i = 0; i < __arraycount(pfil_flag_cases); i++) {
234 1.28 rmind const int fcase = pfil_flag_cases[i];
235 1.28 rmind pfil_list_t *pflist;
236 1.28 rmind
237 1.28 rmind if ((flags & fcase) == 0) {
238 1.28 rmind continue;
239 1.28 rmind }
240 1.28 rmind pflist = pfil_hook_get(fcase, ph);
241 1.28 rmind (void)pfil_list_remove(pflist, func, arg);
242 1.28 rmind }
243 1.28 rmind return 0;
244 1.28 rmind }
245 1.28 rmind
246 1.28 rmind /*
247 1.28 rmind * pfil_run_hooks: run the specified packet filter hooks.
248 1.28 rmind */
249 1.28 rmind int
250 1.28 rmind pfil_run_hooks(pfil_head_t *ph, struct mbuf **mp, ifnet_t *ifp, int dir)
251 1.28 rmind {
252 1.28 rmind const bool pass_mbuf = (dir & PFIL_ALL) != 0 && mp;
253 1.28 rmind struct mbuf *m = pass_mbuf ? *mp : NULL;
254 1.28 rmind pfil_list_t *phlist;
255 1.28 rmind int ret = 0;
256 1.28 rmind
257 1.28 rmind if ((phlist = pfil_hook_get(dir, ph)) == NULL) {
258 1.28 rmind return ret;
259 1.28 rmind }
260 1.28 rmind
261 1.28 rmind for (u_int i = 0; i < phlist->nhooks; i++) {
262 1.28 rmind pfil_hook_t *pfh = &phlist->hooks[i];
263 1.28 rmind pfil_func_t func = pfh->pfil_func;
264 1.28 rmind
265 1.28 rmind if (__predict_true(dir & PFIL_ALL)) {
266 1.28 rmind ret = (*func)(pfh->pfil_arg, &m, ifp, dir);
267 1.28 rmind if (m == NULL)
268 1.28 rmind break;
269 1.28 rmind } else {
270 1.28 rmind ret = (*func)(pfh->pfil_arg, mp, ifp, dir);
271 1.28 rmind }
272 1.28 rmind if (ret)
273 1.28 rmind break;
274 1.28 rmind }
275 1.28 rmind
276 1.28 rmind if (pass_mbuf) {
277 1.28 rmind *mp = m;
278 1.28 rmind }
279 1.28 rmind return ret;
280 1.28 rmind }
281