2 * Copyright (c) 2004-2008 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 auth mgt
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).
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/queue.h>
50 #include <sys/socket.h>
53 #include <net/if_media.h>
54 #include <net/ethernet.h>
55 #include <net/route.h>
57 #include <net80211/ieee80211_var.h>
60 ACL_POLICY_OPEN = 0, /* open, don't check ACL's */
61 ACL_POLICY_ALLOW = 1, /* allow traffic from MAC */
62 ACL_POLICY_DENY = 2, /* deny traffic from MAC */
64 * NB: ACL_POLICY_RADIUS must be the same value as
65 * IEEE80211_MACCMD_POLICY_RADIUS because of the way
66 * acl_getpolicy() works.
68 ACL_POLICY_RADIUS = 7, /* defer to RADIUS ACL server */
71 #define ACL_HASHSIZE 32
74 TAILQ_ENTRY(acl) acl_list;
75 LIST_ENTRY(acl) acl_hash;
76 uint8_t acl_macaddr[IEEE80211_ADDR_LEN];
82 TAILQ_HEAD(, acl) as_list; /* list of all ACL's */
83 LIST_HEAD(, acl) as_hash[ACL_HASHSIZE];
84 struct ieee80211vap *as_vap;
87 /* simple hash is enough for variation of macaddr */
88 #define ACL_HASH(addr) \
89 (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
91 static MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl");
93 static int acl_free_all(struct ieee80211vap *);
95 /* number of references from net80211 layer */
99 acl_attach(struct ieee80211vap *vap)
103 as = (struct aclstate *) IEEE80211_MALLOC(sizeof(struct aclstate),
104 M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
107 ACL_LOCK_INIT(as, "acl");
108 TAILQ_INIT(&as->as_list);
109 as->as_policy = ACL_POLICY_OPEN;
112 nrefs++; /* NB: we assume caller locking */
117 acl_detach(struct ieee80211vap *vap)
119 struct aclstate *as = vap->iv_as;
121 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
122 nrefs--; /* NB: we assume caller locking */
126 ACL_LOCK_DESTROY(as);
127 IEEE80211_FREE(as, M_80211_ACL);
130 static __inline struct acl *
131 _find_acl(struct aclstate *as, const uint8_t *macaddr)
136 hash = ACL_HASH(macaddr);
137 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
138 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
145 _acl_free(struct aclstate *as, struct acl *acl)
149 TAILQ_REMOVE(&as->as_list, acl, acl_list);
150 LIST_REMOVE(acl, acl_hash);
151 IEEE80211_FREE(acl, M_80211_ACL);
156 acl_check(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
158 struct aclstate *as = vap->iv_as;
160 switch (as->as_policy) {
161 case ACL_POLICY_OPEN:
162 case ACL_POLICY_RADIUS:
164 case ACL_POLICY_ALLOW:
165 return _find_acl(as, wh->i_addr2) != NULL;
166 case ACL_POLICY_DENY:
167 return _find_acl(as, wh->i_addr2) == NULL;
169 return 0; /* should not happen */
173 acl_add(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
175 struct aclstate *as = vap->iv_as;
176 struct acl *acl, *new;
179 new = (struct acl *) IEEE80211_MALLOC(sizeof(struct acl),
180 M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
182 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
183 "ACL: add %s failed, no memory\n", ether_sprintf(mac));
189 hash = ACL_HASH(mac);
190 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
191 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) {
193 IEEE80211_FREE(new, M_80211_ACL);
194 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
195 "ACL: add %s failed, already present\n",
200 IEEE80211_ADDR_COPY(new->acl_macaddr, mac);
201 TAILQ_INSERT_TAIL(&as->as_list, new, acl_list);
202 LIST_INSERT_HEAD(&as->as_hash[hash], new, acl_hash);
206 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
207 "ACL: add %s\n", ether_sprintf(mac));
212 acl_remove(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
214 struct aclstate *as = vap->iv_as;
218 acl = _find_acl(as, mac);
223 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
224 "ACL: remove %s%s\n", ether_sprintf(mac),
225 acl == NULL ? ", not present" : "");
227 return (acl == NULL ? ENOENT : 0);
231 acl_free_all(struct ieee80211vap *vap)
233 struct aclstate *as = vap->iv_as;
236 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
239 while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
247 acl_setpolicy(struct ieee80211vap *vap, int policy)
249 struct aclstate *as = vap->iv_as;
251 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
252 "ACL: set policy to %u\n", policy);
255 case IEEE80211_MACCMD_POLICY_OPEN:
256 as->as_policy = ACL_POLICY_OPEN;
258 case IEEE80211_MACCMD_POLICY_ALLOW:
259 as->as_policy = ACL_POLICY_ALLOW;
261 case IEEE80211_MACCMD_POLICY_DENY:
262 as->as_policy = ACL_POLICY_DENY;
264 case IEEE80211_MACCMD_POLICY_RADIUS:
265 as->as_policy = ACL_POLICY_RADIUS;
274 acl_getpolicy(struct ieee80211vap *vap)
276 struct aclstate *as = vap->iv_as;
278 return as->as_policy;
282 acl_setioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
289 acl_getioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
291 struct aclstate *as = vap->iv_as;
293 struct ieee80211req_maclist *ap;
297 switch (ireq->i_val) {
298 case IEEE80211_MACCMD_POLICY:
299 ireq->i_val = as->as_policy;
301 case IEEE80211_MACCMD_LIST:
302 space = as->as_nacls * IEEE80211_ADDR_LEN;
303 if (ireq->i_len == 0) {
304 ireq->i_len = space; /* return required space */
305 return 0; /* NB: must not error */
307 ap = (struct ieee80211req_maclist *) IEEE80211_MALLOC(space,
308 M_TEMP, IEEE80211_M_NOWAIT);
313 TAILQ_FOREACH(acl, &as->as_list, acl_list) {
314 IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr);
318 if (ireq->i_len >= space) {
319 error = copyout(ap, ireq->i_data, space);
322 error = copyout(ap, ireq->i_data, ireq->i_len);
323 IEEE80211_FREE(ap, M_TEMP);
329 static const struct ieee80211_aclator mac = {
331 .iac_attach = acl_attach,
332 .iac_detach = acl_detach,
333 .iac_check = acl_check,
335 .iac_remove = acl_remove,
336 .iac_flush = acl_free_all,
337 .iac_setpolicy = acl_setpolicy,
338 .iac_getpolicy = acl_getpolicy,
339 .iac_setioctl = acl_setioctl,
340 .iac_getioctl = acl_getioctl,
342 IEEE80211_ACL_MODULE(wlan_acl, mac, 1);