]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/net/pfil.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / net / pfil.c
1 /*      $FreeBSD$ */
2 /*      $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $  */
3
4 /*-
5  * Copyright (c) 1996 Matthew R. Green
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/errno.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/rmlock.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/systm.h>
41 #include <sys/condvar.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/queue.h>
46
47 #include <net/if.h>
48 #include <net/pfil.h>
49
50 static struct mtx pfil_global_lock;
51
52 MTX_SYSINIT(pfil_heads_lock, &pfil_global_lock, "pfil_head_list lock",
53   MTX_DEF);
54
55 static int pfil_list_add(pfil_list_t *, struct packet_filter_hook *, int);
56
57 static int pfil_list_remove(pfil_list_t *,
58     int (*)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
59     void *);
60
61 LIST_HEAD(pfilheadhead, pfil_head);
62 VNET_DEFINE(struct pfilheadhead, pfil_head_list);
63 #define V_pfil_head_list        VNET(pfil_head_list)
64
65 /*
66  * pfil_run_hooks() runs the specified packet filter hooks.
67  */
68 int
69 pfil_run_hooks(struct pfil_head *ph, struct mbuf **mp, struct ifnet *ifp,
70     int dir, struct inpcb *inp)
71 {
72         struct rm_priotracker rmpt;
73         struct packet_filter_hook *pfh;
74         struct mbuf *m = *mp;
75         int rv = 0;
76
77         PFIL_RLOCK(ph, &rmpt);
78         KASSERT(ph->ph_nhooks >= 0, ("Pfil hook count dropped < 0"));
79         for (pfh = pfil_hook_get(dir, ph); pfh != NULL;
80              pfh = TAILQ_NEXT(pfh, pfil_link)) {
81                 if (pfh->pfil_func != NULL) {
82                         rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir,
83                             inp);
84                         if (rv != 0 || m == NULL)
85                                 break;
86                 }
87         }
88         PFIL_RUNLOCK(ph, &rmpt);
89         *mp = m;
90         return (rv);
91 }
92
93 /*
94  * pfil_head_register() registers a pfil_head with the packet filter hook
95  * mechanism.
96  */
97 int
98 pfil_head_register(struct pfil_head *ph)
99 {
100         struct pfil_head *lph;
101
102         PFIL_LIST_LOCK();
103         LIST_FOREACH(lph, &V_pfil_head_list, ph_list) {
104                 if (ph->ph_type == lph->ph_type &&
105                     ph->ph_un.phu_val == lph->ph_un.phu_val) {
106                         PFIL_LIST_UNLOCK();
107                         return (EEXIST);
108                 }
109         }
110         PFIL_LOCK_INIT(ph);
111         ph->ph_nhooks = 0;
112         TAILQ_INIT(&ph->ph_in);
113         TAILQ_INIT(&ph->ph_out);
114         LIST_INSERT_HEAD(&V_pfil_head_list, ph, ph_list);
115         PFIL_LIST_UNLOCK();
116         return (0);
117 }
118
119 /*
120  * pfil_head_unregister() removes a pfil_head from the packet filter hook
121  * mechanism.  The producer of the hook promises that all outstanding
122  * invocations of the hook have completed before it unregisters the hook.
123  */
124 int
125 pfil_head_unregister(struct pfil_head *ph)
126 {
127         struct packet_filter_hook *pfh, *pfnext;
128                 
129         PFIL_LIST_LOCK();
130         LIST_REMOVE(ph, ph_list);
131         PFIL_LIST_UNLOCK();
132         TAILQ_FOREACH_SAFE(pfh, &ph->ph_in, pfil_link, pfnext)
133                 free(pfh, M_IFADDR);
134         TAILQ_FOREACH_SAFE(pfh, &ph->ph_out, pfil_link, pfnext)
135                 free(pfh, M_IFADDR);
136         PFIL_LOCK_DESTROY(ph);
137         return (0);
138 }
139
140 /*
141  * pfil_head_get() returns the pfil_head for a given key/dlt.
142  */
143 struct pfil_head *
144 pfil_head_get(int type, u_long val)
145 {
146         struct pfil_head *ph;
147
148         PFIL_LIST_LOCK();
149         LIST_FOREACH(ph, &V_pfil_head_list, ph_list)
150                 if (ph->ph_type == type && ph->ph_un.phu_val == val)
151                         break;
152         PFIL_LIST_UNLOCK();
153         return (ph);
154 }
155
156 /*
157  * pfil_add_hook() adds a function to the packet filter hook.  the
158  * flags are:
159  *      PFIL_IN         call me on incoming packets
160  *      PFIL_OUT        call me on outgoing packets
161  *      PFIL_ALL        call me on all of the above
162  *      PFIL_WAITOK     OK to call malloc with M_WAITOK.
163  */
164 int
165 pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int,
166   struct inpcb *), void *arg, int flags, struct pfil_head *ph)
167 {
168         struct packet_filter_hook *pfh1 = NULL;
169         struct packet_filter_hook *pfh2 = NULL;
170         int err;
171
172         if (flags & PFIL_IN) {
173                 pfh1 = (struct packet_filter_hook *)malloc(sizeof(*pfh1), 
174                     M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
175                 if (pfh1 == NULL) {
176                         err = ENOMEM;
177                         goto error;
178                 }
179         }
180         if (flags & PFIL_OUT) {
181                 pfh2 = (struct packet_filter_hook *)malloc(sizeof(*pfh1),
182                     M_IFADDR, (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
183                 if (pfh2 == NULL) {
184                         err = ENOMEM;
185                         goto error;
186                 }
187         }
188         PFIL_WLOCK(ph);
189         if (flags & PFIL_IN) {
190                 pfh1->pfil_func = func;
191                 pfh1->pfil_arg = arg;
192                 err = pfil_list_add(&ph->ph_in, pfh1, flags & ~PFIL_OUT);
193                 if (err)
194                         goto locked_error;
195                 ph->ph_nhooks++;
196         }
197         if (flags & PFIL_OUT) {
198                 pfh2->pfil_func = func;
199                 pfh2->pfil_arg = arg;
200                 err = pfil_list_add(&ph->ph_out, pfh2, flags & ~PFIL_IN);
201                 if (err) {
202                         if (flags & PFIL_IN)
203                                 pfil_list_remove(&ph->ph_in, func, arg);
204                         goto locked_error;
205                 }
206                 ph->ph_nhooks++;
207         }
208         PFIL_WUNLOCK(ph);
209         return (0);
210 locked_error:
211         PFIL_WUNLOCK(ph);
212 error:
213         if (pfh1 != NULL)
214                 free(pfh1, M_IFADDR);
215         if (pfh2 != NULL)
216                 free(pfh2, M_IFADDR);
217         return (err);
218 }
219
220 /*
221  * pfil_remove_hook removes a specific function from the packet filter hook
222  * list.
223  */
224 int
225 pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int,
226     struct inpcb *), void *arg, int flags, struct pfil_head *ph)
227 {
228         int err = 0;
229
230         PFIL_WLOCK(ph);
231         if (flags & PFIL_IN) {
232                 err = pfil_list_remove(&ph->ph_in, func, arg);
233                 if (err == 0)
234                         ph->ph_nhooks--;
235         }
236         if ((err == 0) && (flags & PFIL_OUT)) {
237                 err = pfil_list_remove(&ph->ph_out, func, arg);
238                 if (err == 0)
239                         ph->ph_nhooks--;
240         }
241         PFIL_WUNLOCK(ph);
242         return (err);
243 }
244
245 static int
246 pfil_list_add(pfil_list_t *list, struct packet_filter_hook *pfh1, int flags)
247 {
248         struct packet_filter_hook *pfh;
249
250         /*
251          * First make sure the hook is not already there.
252          */
253         TAILQ_FOREACH(pfh, list, pfil_link)
254                 if (pfh->pfil_func == pfh1->pfil_func &&
255                     pfh->pfil_arg == pfh1->pfil_arg)
256                         return (EEXIST);
257
258         /*
259          * Insert the input list in reverse order of the output list so that
260          * the same path is followed in or out of the kernel.
261          */
262         if (flags & PFIL_IN)
263                 TAILQ_INSERT_HEAD(list, pfh1, pfil_link);
264         else
265                 TAILQ_INSERT_TAIL(list, pfh1, pfil_link);
266         return (0);
267 }
268
269 /*
270  * pfil_list_remove is an internal function that takes a function off the
271  * specified list.
272  */
273 static int
274 pfil_list_remove(pfil_list_t *list,
275     int (*func)(void *, struct mbuf **, struct ifnet *, int, struct inpcb *),
276     void *arg)
277 {
278         struct packet_filter_hook *pfh;
279
280         TAILQ_FOREACH(pfh, list, pfil_link)
281                 if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
282                         TAILQ_REMOVE(list, pfh, pfil_link);
283                         free(pfh, M_IFADDR);
284                         return (0);
285                 }
286         return (ENOENT);
287 }
288
289 /*
290  * Stuff that must be initialized for every instance (including the first of
291  * course).
292  */
293 static int
294 vnet_pfil_init(const void *unused)
295 {
296
297         LIST_INIT(&V_pfil_head_list);
298         return (0);
299 }
300
301 /*
302  * Called for the removal of each instance.
303  */
304 static int
305 vnet_pfil_uninit(const void *unused)
306 {
307
308         /*  XXX should panic if list is not empty */
309         return (0);
310 }
311
312 /* Define startup order. */
313 #define PFIL_SYSINIT_ORDER      SI_SUB_PROTO_BEGIN
314 #define PFIL_MODEVENT_ORDER     (SI_ORDER_FIRST) /* On boot slot in here. */
315 #define PFIL_VNET_ORDER         (PFIL_MODEVENT_ORDER + 2) /* Later still. */
316
317 /*
318  * Starting up.
319  *
320  * VNET_SYSINIT is called for each existing vnet and each new vnet.
321  */
322 VNET_SYSINIT(vnet_pfil_init, PFIL_SYSINIT_ORDER, PFIL_VNET_ORDER,
323     vnet_pfil_init, NULL);
324  
325 /*
326  * Closing up shop.  These are done in REVERSE ORDER.  Not called on reboot.
327  *
328  * VNET_SYSUNINIT is called for each exiting vnet as it exits.
329  */
330 VNET_SYSUNINIT(vnet_pfil_uninit, PFIL_SYSINIT_ORDER, PFIL_VNET_ORDER,
331     vnet_pfil_uninit, NULL);