d09bef36cd0bc3d4d3722b0d40a6c63d36352a52
[dragonfly.git] / sys / net / pfil.c
1 /*      $NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $  */
2 /* $DragonFly: src/sys/net/pfil.c,v 1.8 2008/09/14 04:34:26 sephe 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/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(struct pfil_head *,
45     int (*)(void *, struct mbuf **, struct ifnet *, int), void *, int);
46
47 static int pfil_list_remove(struct pfil_head *,
48     int (*)(void *, struct mbuf **, struct ifnet *, int), void *, int);
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 = *mp;
62         pfil_list_t *list;
63         int rv = 0;
64
65         if (dir == PFIL_IN)
66                 list = &ph->ph_in;
67         else if (dir == PFIL_OUT)
68                 list = &ph->ph_out;
69         else
70                 return 0; /* XXX panic? */
71
72         TAILQ_FOREACH(pfh, list, pfil_link) {
73                 if (pfh->pfil_func != NULL) {
74                         rv = (*pfh->pfil_func)(pfh->pfil_arg, &m, ifp, dir);
75                         if (rv != 0 || m == NULL)
76                                 break;
77                 }
78         }
79
80         *mp = m;
81         return (rv);
82 }
83
84 /*
85  * pfil_head_register() registers a pfil_head with the packet filter
86  * hook mechanism.
87  */
88 int
89 pfil_head_register(struct pfil_head *ph)
90 {
91         struct pfil_head *lph;
92
93         LIST_FOREACH(lph, &pfil_head_list, ph_list) {
94                 if (ph->ph_type == lph->ph_type &&
95                     ph->ph_un.phu_val == lph->ph_un.phu_val)
96                         return EEXIST;
97         }
98
99         TAILQ_INIT(&ph->ph_in);
100         TAILQ_INIT(&ph->ph_out);
101         ph->ph_hashooks = 0;
102
103         LIST_INSERT_HEAD(&pfil_head_list, ph, ph_list);
104
105         return (0);
106 }
107
108 /*
109  * pfil_head_unregister() removes a pfil_head from the packet filter
110  * hook mechanism.
111  */
112 int
113 pfil_head_unregister(struct pfil_head *pfh)
114 {
115         LIST_REMOVE(pfh, ph_list);
116         return (0);
117 }
118
119 /*
120  * pfil_head_get() returns the pfil_head for a given key/dlt.
121  */
122 struct pfil_head *
123 pfil_head_get(int type, u_long val)
124 {
125         struct pfil_head *ph;
126
127         LIST_FOREACH(ph, &pfil_head_list, ph_list) {
128                 if (ph->ph_type == type && ph->ph_un.phu_val == val)
129                         break;
130         }
131         return (ph);
132 }
133
134 /*
135  * pfil_add_hook() adds a function to the packet filter hook.  the
136  * flags are:
137  *      PFIL_IN         call me on incoming packets
138  *      PFIL_OUT        call me on outgoing packets
139  *      PFIL_ALL        call me on all of the above
140  *      PFIL_WAITOK     OK to call kmalloc with M_WAITOK.
141  */
142 int
143 pfil_add_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
144     void *arg, int flags, struct pfil_head *ph)
145 {
146         int err = 0;
147
148         if (flags & PFIL_IN) {
149                 err = pfil_list_add(ph, func, arg, flags & ~PFIL_OUT);
150                 if (err)
151                         return err;
152         }
153         if (flags & PFIL_OUT) {
154                 err = pfil_list_add(ph, func, arg, flags & ~PFIL_IN);
155                 if (err) {
156                         if (flags & PFIL_IN)
157                                 pfil_list_remove(ph, func, arg, PFIL_IN);
158                         return err;
159                 }
160         }
161         return 0;
162 }
163
164 static int
165 pfil_list_add(struct pfil_head *ph,
166     int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg,
167     int flags)
168 {
169         struct packet_filter_hook *pfh;
170         pfil_list_t *list;
171
172         list = (flags & PFIL_IN) ? &ph->ph_in : &ph->ph_out;
173
174         /*
175          * First make sure the hook is not already there.
176          */
177         TAILQ_FOREACH(pfh, list, pfil_link) {
178                 if (pfh->pfil_func == func && pfh->pfil_arg == arg)
179                         return EEXIST;
180         }
181
182         pfh = (struct packet_filter_hook *)kmalloc(sizeof(*pfh), M_IFADDR,
183             (flags & PFIL_WAITOK) ? M_WAITOK : M_NOWAIT);
184         if (pfh == NULL)
185                 return ENOMEM;
186
187         pfh->pfil_func = func;
188         pfh->pfil_arg  = arg;
189
190         /*
191          * insert the input list in reverse order of the output list
192          * so that the same path is followed in or out of the kernel.
193          */
194         if (flags & PFIL_IN)
195                 TAILQ_INSERT_HEAD(list, pfh, pfil_link);
196         else
197                 TAILQ_INSERT_TAIL(list, pfh, pfil_link);
198         ph->ph_hashooks = 1;
199         return (0);
200 }
201
202 /*
203  * pfil_remove_hook removes a specific function from the packet filter
204  * hook list.
205  */
206 int
207 pfil_remove_hook(int (*func)(void *, struct mbuf **, struct ifnet *, int),
208     void *arg, int flags, struct pfil_head *ph)
209 {
210         int err = 0;
211
212         if (flags & PFIL_IN)
213                 err = pfil_list_remove(ph, func, arg, PFIL_IN);
214         if ((err == 0) && (flags & PFIL_OUT))
215                 err = pfil_list_remove(ph, func, arg, PFIL_OUT);
216         return err;
217 }
218
219 /*
220  * pfil_list_remove is an internal function that takes a function off the
221  * specified list.  Clear ph_hashooks if no functions remain on any list.
222  */
223 static int
224 pfil_list_remove(struct pfil_head *ph,
225     int (*func)(void *, struct mbuf **, struct ifnet *, int), void *arg,
226     int flags)
227 {
228         struct packet_filter_hook *pfh;
229         pfil_list_t *list;
230
231         list = (flags & PFIL_IN) ? &ph->ph_in : &ph->ph_out;
232
233         TAILQ_FOREACH(pfh, list, pfil_link) {
234                 if (pfh->pfil_func == func && pfh->pfil_arg == arg) {
235                         TAILQ_REMOVE(list, pfh, pfil_link);
236                         kfree(pfh, M_IFADDR);
237                         if (TAILQ_EMPTY(&ph->ph_in) && TAILQ_EMPTY(&ph->ph_out))
238                                 ph->ph_hashooks = 0;
239                         return 0;
240                 }
241         }
242         return ENOENT;
243 }