Merge branch 'vendor/LESS' into less_update
[dragonfly.git] / sys / netproto / 802_11 / wlan_acl / ieee80211_acl.c
1 /*
2  * Copyright (c) 2004-2005 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") version 2 as published by the Free
18  * Software Foundation.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sys/net80211/ieee80211_acl.c,v 1.3.2.1 2005/09/03 22:40:02 sam Exp $
32  * $DragonFly: src/sys/netproto/802_11/wlan_acl/ieee80211_acl.c,v 1.3 2006/12/22 23:57:53 swildner Exp $
33  */
34
35 /*
36  * IEEE 802.11 MAC ACL support.
37  *
38  * When this module is loaded the sender address of each received
39  * frame is passed to the iac_check method and the module indicates
40  * if the frame should be accepted or rejected.  If the policy is
41  * set to ACL_POLICY_OPEN then all frames are accepted w/o checking
42  * the address.  Otherwise, the address is looked up in the database
43  * and if found the frame is either accepted (ACL_POLICY_ALLOW)
44  * or rejected (ACL_POLICY_DENT).
45  */
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h> 
49 #include <sys/mbuf.h>   
50 #include <sys/module.h>
51 #include <sys/queue.h>
52 #include <sys/serialize.h>
53
54 #include <sys/socket.h>
55
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_media.h>
59 #include <net/ethernet.h>
60 #include <net/route.h>
61
62 #include <netproto/802_11/ieee80211_var.h>
63
64 enum {
65         ACL_POLICY_OPEN         = 0,    /* open, don't check ACL's */
66         ACL_POLICY_ALLOW        = 1,    /* allow traffic from MAC */
67         ACL_POLICY_DENY         = 2,    /* deny traffic from MAC */
68 };
69
70 #define ACL_HASHSIZE    32
71
72 struct acl {
73         TAILQ_ENTRY(acl)        acl_list;
74         LIST_ENTRY(acl)         acl_hash;
75         uint8_t                 acl_macaddr[IEEE80211_ADDR_LEN];
76 };
77 struct aclstate {
78 #if 0
79         acl_lock_t              as_lock;
80 #endif
81         int                     as_policy;
82         int                     as_nacls;
83         TAILQ_HEAD(, acl)       as_list;        /* list of all ACL's */
84         LIST_HEAD(, acl)        as_hash[ACL_HASHSIZE];
85         struct ieee80211com     *as_ic;
86 };
87
88 /* simple hash is enough for variation of macaddr */
89 #define ACL_HASH(addr)  \
90         (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
91
92 MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl");
93
94 static  int acl_free_all(struct ieee80211com *);
95
96 static int
97 acl_attach(struct ieee80211com *ic)
98 {
99         struct aclstate *as;
100
101         as = kmalloc(sizeof(struct aclstate), M_80211_ACL, M_NOWAIT | M_ZERO);
102         if (as == NULL)
103                 return 0;
104         TAILQ_INIT(&as->as_list);
105         as->as_policy = ACL_POLICY_OPEN;
106         as->as_ic = ic;
107         ic->ic_as = as;
108         return 1;
109 }
110
111 static void
112 acl_detach(struct ieee80211com *ic)
113 {
114         struct aclstate *as = ic->ic_as;
115
116         acl_free_all(ic);
117         ic->ic_as = NULL;
118         kfree(as, M_DEVBUF);
119 }
120
121 static __inline struct acl *
122 _find_acl(struct aclstate *as, const uint8_t *macaddr)
123 {
124         struct acl *acl;
125         int hash;
126
127         hash = ACL_HASH(macaddr);
128         LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
129                 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
130                         return acl;
131         }
132         return NULL;
133 }
134
135 static void
136 _acl_free(struct aclstate *as, struct acl *acl)
137 {
138         ASSERT_SERIALIZED(as->as_ic->ic_ifp->if_serializer);
139
140         TAILQ_REMOVE(&as->as_list, acl, acl_list);
141         LIST_REMOVE(acl, acl_hash);
142         kfree(acl, M_80211_ACL);
143         as->as_nacls--;
144 }
145
146 static int
147 acl_check(struct ieee80211com *ic, const uint8_t mac[IEEE80211_ADDR_LEN])
148 {
149         struct aclstate *as = ic->ic_as;
150
151         switch (as->as_policy) {
152         case ACL_POLICY_OPEN:
153                 return 1;
154         case ACL_POLICY_ALLOW:
155                 return _find_acl(as, mac) != NULL;
156         case ACL_POLICY_DENY:
157                 return _find_acl(as, mac) == NULL;
158         }
159         return 0;               /* should not happen */
160 }
161
162 static int
163 acl_add(struct ieee80211com *ic, const uint8_t mac[IEEE80211_ADDR_LEN])
164 {
165         struct aclstate *as = ic->ic_as;
166         struct acl *acl, *new;
167         int hash;
168
169         ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
170
171         new = kmalloc(sizeof(struct acl), M_80211_ACL, M_NOWAIT | M_ZERO);
172         if (new == NULL) {
173                 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
174                         "ACL: add %6D failed, no memory\n", mac, ":");
175                 /* XXX statistic */
176                 return ENOMEM;
177         }
178
179         hash = ACL_HASH(mac);
180         LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
181                 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) {
182                         FREE(new, M_80211_ACL);
183                         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
184                                 "ACL: add %6D failed, already present\n",
185                                 mac, ":");
186                         return EEXIST;
187                 }
188         }
189         IEEE80211_ADDR_COPY(new->acl_macaddr, mac);
190         TAILQ_INSERT_TAIL(&as->as_list, new, acl_list);
191         LIST_INSERT_HEAD(&as->as_hash[hash], new, acl_hash);
192         as->as_nacls++;
193
194         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
195                 "ACL: add %6D\n", mac, ":");
196         return 0;
197 }
198
199 static int
200 acl_remove(struct ieee80211com *ic, const uint8_t mac[IEEE80211_ADDR_LEN])
201 {
202         struct aclstate *as = ic->ic_as;
203         struct acl *acl;
204
205         ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
206
207         acl = _find_acl(as, mac);
208         if (acl != NULL)
209                 _acl_free(as, acl);
210
211         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
212                 "ACL: remove %6D%s\n", mac, ":",
213                 acl == NULL ? ", not present" : "");
214
215         return (acl == NULL ? ENOENT : 0);
216 }
217
218 static int
219 acl_free_all(struct ieee80211com *ic)
220 {
221         struct aclstate *as = ic->ic_as;
222         struct acl *acl;
223
224         ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
225
226         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
227
228         while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
229                 _acl_free(as, acl);
230
231         return 0;
232 }
233
234 static int
235 acl_setpolicy(struct ieee80211com *ic, int policy)
236 {
237         struct aclstate *as = ic->ic_as;
238
239         IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
240                 "ACL: set policy to %u\n", policy);
241
242         switch (policy) {
243         case IEEE80211_MACCMD_POLICY_OPEN:
244                 as->as_policy = ACL_POLICY_OPEN;
245                 break;
246         case IEEE80211_MACCMD_POLICY_ALLOW:
247                 as->as_policy = ACL_POLICY_ALLOW;
248                 break;
249         case IEEE80211_MACCMD_POLICY_DENY:
250                 as->as_policy = ACL_POLICY_DENY;
251                 break;
252         default:
253                 return EINVAL;
254         }
255         return 0;
256 }
257
258 static int
259 acl_getpolicy(struct ieee80211com *ic)
260 {
261         return ((struct aclstate *)(ic->ic_as))->as_policy;
262 }
263
264 static int
265 acl_setioctl(struct ieee80211com *ic, struct ieee80211req *ireq)
266 {
267         return EINVAL;
268 }
269
270 static int
271 acl_getioctl(struct ieee80211com *ic, struct ieee80211req *ireq)
272 {
273         struct aclstate *as = ic->ic_as;
274         struct acl *acl;
275         struct ieee80211req_maclist *ap;
276         int error, space, i;
277
278         ASSERT_SERIALIZED(ic->ic_ifp->if_serializer);
279
280         switch (ireq->i_val) {
281         case IEEE80211_MACCMD_POLICY:
282                 ireq->i_val = as->as_policy;
283                 return 0;
284         case IEEE80211_MACCMD_LIST:
285                 space = as->as_nacls * IEEE80211_ADDR_LEN;
286                 if (ireq->i_len == 0) {
287                         ireq->i_len = space;    /* return required space */
288                         return 0;               /* NB: must not error */
289                 }
290                 ap = kmalloc(space, M_TEMP, M_NOWAIT);
291                 if (ap == NULL)
292                         return ENOMEM;
293                 i = 0;
294                 TAILQ_FOREACH(acl, &as->as_list, acl_list) {
295                         IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr);
296                         i++;
297                 }
298                 if (ireq->i_len >= space) {
299                         error = copyout(ap, ireq->i_data, space);
300                         ireq->i_len = space;
301                 } else
302                         error = copyout(ap, ireq->i_data, ireq->i_len);
303                 kfree(ap, M_TEMP);
304                 return error;
305         }
306         return EINVAL;
307 }
308
309 static const struct ieee80211_aclator mac = {
310         .iac_name       = "mac",
311         .iac_attach     = acl_attach,
312         .iac_detach     = acl_detach,
313         .iac_check      = acl_check,
314         .iac_add        = acl_add,
315         .iac_remove     = acl_remove,
316         .iac_flush      = acl_free_all,
317         .iac_setpolicy  = acl_setpolicy,
318         .iac_getpolicy  = acl_getpolicy,
319         .iac_setioctl   = acl_setioctl,
320         .iac_getioctl   = acl_getioctl,
321 };
322
323 /*
324  * Module glue.
325  */
326 static int
327 wlan_acl_modevent(module_t mod, int type, void *unused)
328 {
329         switch (type) {
330         case MOD_LOAD:
331                 if (bootverbose)
332                         kprintf("wlan: <802.11 MAC ACL support>\n");
333                 ieee80211_aclator_register(&mac);
334                 return 0;
335         case MOD_UNLOAD:
336                 ieee80211_aclator_unregister(&mac);
337                 return 0;
338         }
339         return EINVAL;
340 }
341
342 static moduledata_t wlan_acl_mod = {
343         "wlan_acl",
344         wlan_acl_modevent,
345         0
346 };
347 DECLARE_MODULE(wlan_acl, wlan_acl_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
348 MODULE_VERSION(wlan_acl, 1);
349 MODULE_DEPEND(wlan_acl, wlan, 1, 1, 1);