2 * Copyright (c) 2004-2007 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 MAC ACL support.
32 * When this module is loaded the sender address of each received
33 * frame is passed to the iac_check method and the module indicates
34 * if the frame should be accepted or rejected. If the policy is
35 * set to ACL_POLICY_OPEN then all frames are accepted w/o checking
36 * the address. Otherwise, the address is looked up in the database
37 * and if found the frame is either accepted (ACL_POLICY_ALLOW)
38 * or rejected (ACL_POLICY_DENT).
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
44 #include <sys/module.h>
45 #include <sys/queue.h>
47 #include <sys/socket.h>
50 #include <net/if_media.h>
51 #include <net/ethernet.h>
52 #include <net/route.h>
54 #include <net80211/ieee80211_var.h>
57 ACL_POLICY_OPEN = 0, /* open, don't check ACL's */
58 ACL_POLICY_ALLOW = 1, /* allow traffic from MAC */
59 ACL_POLICY_DENY = 2, /* deny traffic from MAC */
62 #define ACL_HASHSIZE 32
65 TAILQ_ENTRY(acl) acl_list;
66 LIST_ENTRY(acl) acl_hash;
67 uint8_t acl_macaddr[IEEE80211_ADDR_LEN];
73 TAILQ_HEAD(, acl) as_list; /* list of all ACL's */
74 LIST_HEAD(, acl) as_hash[ACL_HASHSIZE];
75 struct ieee80211com *as_ic;
78 /* simple hash is enough for variation of macaddr */
79 #define ACL_HASH(addr) \
80 (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
82 MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl");
84 static int acl_free_all(struct ieee80211com *);
87 acl_attach(struct ieee80211com *ic)
91 MALLOC(as, struct aclstate *, sizeof(struct aclstate),
92 M_80211_ACL, M_NOWAIT | M_ZERO);
95 ACL_LOCK_INIT(as, "acl");
96 TAILQ_INIT(&as->as_list);
97 as->as_policy = ACL_POLICY_OPEN;
104 acl_detach(struct ieee80211com *ic)
106 struct aclstate *as = ic->ic_as;
110 ACL_LOCK_DESTROY(as);
114 static __inline struct acl *
115 _find_acl(struct aclstate *as, const uint8_t *macaddr)
120 hash = ACL_HASH(macaddr);
121 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
122 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
129 _acl_free(struct aclstate *as, struct acl *acl)
133 TAILQ_REMOVE(&as->as_list, acl, acl_list);
134 LIST_REMOVE(acl, acl_hash);
135 FREE(acl, M_80211_ACL);
140 acl_check(struct ieee80211com *ic, const uint8_t mac[IEEE80211_ADDR_LEN])
142 struct aclstate *as = ic->ic_as;
144 switch (as->as_policy) {
145 case ACL_POLICY_OPEN:
147 case ACL_POLICY_ALLOW:
148 return _find_acl(as, mac) != NULL;
149 case ACL_POLICY_DENY:
150 return _find_acl(as, mac) == NULL;
152 return 0; /* should not happen */
156 acl_add(struct ieee80211com *ic, const uint8_t mac[IEEE80211_ADDR_LEN])
158 struct aclstate *as = ic->ic_as;
159 struct acl *acl, *new;
162 MALLOC(new, struct acl *, sizeof(struct acl), M_80211_ACL, M_NOWAIT | M_ZERO);
164 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
165 "ACL: add %s failed, no memory\n", ether_sprintf(mac));
171 hash = ACL_HASH(mac);
172 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
173 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) {
175 FREE(new, M_80211_ACL);
176 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
177 "ACL: add %s failed, already present\n",
182 IEEE80211_ADDR_COPY(new->acl_macaddr, mac);
183 TAILQ_INSERT_TAIL(&as->as_list, new, acl_list);
184 LIST_INSERT_HEAD(&as->as_hash[hash], new, acl_hash);
188 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
189 "ACL: add %s\n", ether_sprintf(mac));
194 acl_remove(struct ieee80211com *ic, const uint8_t mac[IEEE80211_ADDR_LEN])
196 struct aclstate *as = ic->ic_as;
200 acl = _find_acl(as, mac);
205 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
206 "ACL: remove %s%s\n", ether_sprintf(mac),
207 acl == NULL ? ", not present" : "");
209 return (acl == NULL ? ENOENT : 0);
213 acl_free_all(struct ieee80211com *ic)
215 struct aclstate *as = ic->ic_as;
218 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
221 while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
229 acl_setpolicy(struct ieee80211com *ic, int policy)
231 struct aclstate *as = ic->ic_as;
233 IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
234 "ACL: set policy to %u\n", policy);
237 case IEEE80211_MACCMD_POLICY_OPEN:
238 as->as_policy = ACL_POLICY_OPEN;
240 case IEEE80211_MACCMD_POLICY_ALLOW:
241 as->as_policy = ACL_POLICY_ALLOW;
243 case IEEE80211_MACCMD_POLICY_DENY:
244 as->as_policy = ACL_POLICY_DENY;
253 acl_getpolicy(struct ieee80211com *ic)
255 struct aclstate *as = ic->ic_as;
257 return as->as_policy;
261 acl_setioctl(struct ieee80211com *ic, struct ieee80211req *ireq)
268 acl_getioctl(struct ieee80211com *ic, struct ieee80211req *ireq)
270 struct aclstate *as = ic->ic_as;
272 struct ieee80211req_maclist *ap;
275 switch (ireq->i_val) {
276 case IEEE80211_MACCMD_POLICY:
277 ireq->i_val = as->as_policy;
279 case IEEE80211_MACCMD_LIST:
280 space = as->as_nacls * IEEE80211_ADDR_LEN;
281 if (ireq->i_len == 0) {
282 ireq->i_len = space; /* return required space */
283 return 0; /* NB: must not error */
285 MALLOC(ap, struct ieee80211req_maclist *, space,
291 TAILQ_FOREACH(acl, &as->as_list, acl_list) {
292 IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr);
296 if (ireq->i_len >= space) {
297 error = copyout(ap, ireq->i_data, space);
300 error = copyout(ap, ireq->i_data, ireq->i_len);
307 static const struct ieee80211_aclator mac = {
309 .iac_attach = acl_attach,
310 .iac_detach = acl_detach,
311 .iac_check = acl_check,
313 .iac_remove = acl_remove,
314 .iac_flush = acl_free_all,
315 .iac_setpolicy = acl_setpolicy,
316 .iac_getpolicy = acl_getpolicy,
317 .iac_setioctl = acl_setioctl,
318 .iac_getioctl = acl_getioctl,
325 wlan_acl_modevent(module_t mod, int type, void *unused)
330 printf("wlan: <802.11 MAC ACL support>\n");
331 ieee80211_aclator_register(&mac);
334 ieee80211_aclator_unregister(&mac);
340 static moduledata_t wlan_acl_mod = {
345 DECLARE_MODULE(wlan_acl, wlan_acl_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
346 MODULE_VERSION(wlan_acl, 1);
347 MODULE_DEPEND(wlan_acl, wlan, 1, 1, 1);