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