2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2008 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * IEEE 802.11 MAC ACL support.
34 * When this module is loaded the sender address of each auth mgt
35 * frame is passed to the iac_check method and the module indicates
36 * if the frame should be accepted or rejected. If the policy is
37 * set to ACL_POLICY_OPEN then all frames are accepted w/o checking
38 * the address. Otherwise, the address is looked up in the database
39 * and if found the frame is either accepted (ACL_POLICY_ALLOW)
40 * or rejected (ACL_POLICY_DENT).
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/queue.h>
52 #include <sys/socket.h>
55 #include <net/if_media.h>
56 #include <net/ethernet.h>
57 #include <net/route.h>
59 #include <net80211/ieee80211_var.h>
62 ACL_POLICY_OPEN = 0, /* open, don't check ACL's */
63 ACL_POLICY_ALLOW = 1, /* allow traffic from MAC */
64 ACL_POLICY_DENY = 2, /* deny traffic from MAC */
66 * NB: ACL_POLICY_RADIUS must be the same value as
67 * IEEE80211_MACCMD_POLICY_RADIUS because of the way
68 * acl_getpolicy() works.
70 ACL_POLICY_RADIUS = 7, /* defer to RADIUS ACL server */
73 #define ACL_HASHSIZE 32
76 TAILQ_ENTRY(acl) acl_list;
77 LIST_ENTRY(acl) acl_hash;
78 uint8_t acl_macaddr[IEEE80211_ADDR_LEN];
84 TAILQ_HEAD(, acl) as_list; /* list of all ACL's */
85 LIST_HEAD(, acl) as_hash[ACL_HASHSIZE];
86 struct ieee80211vap *as_vap;
89 /* simple hash is enough for variation of macaddr */
90 #define ACL_HASH(addr) \
91 (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
93 static MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl");
95 static int acl_free_all(struct ieee80211vap *);
97 /* number of references from net80211 layer */
101 acl_attach(struct ieee80211vap *vap)
105 as = (struct aclstate *) IEEE80211_MALLOC(sizeof(struct aclstate),
106 M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
109 ACL_LOCK_INIT(as, "acl");
110 TAILQ_INIT(&as->as_list);
111 as->as_policy = ACL_POLICY_OPEN;
114 nrefs++; /* NB: we assume caller locking */
119 acl_detach(struct ieee80211vap *vap)
121 struct aclstate *as = vap->iv_as;
123 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
124 nrefs--; /* NB: we assume caller locking */
128 ACL_LOCK_DESTROY(as);
129 IEEE80211_FREE(as, M_80211_ACL);
132 static __inline struct acl *
133 _find_acl(struct aclstate *as, const uint8_t *macaddr)
138 hash = ACL_HASH(macaddr);
139 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
140 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
147 _acl_free(struct aclstate *as, struct acl *acl)
151 TAILQ_REMOVE(&as->as_list, acl, acl_list);
152 LIST_REMOVE(acl, acl_hash);
153 IEEE80211_FREE(acl, M_80211_ACL);
158 acl_check(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
160 struct aclstate *as = vap->iv_as;
162 switch (as->as_policy) {
163 case ACL_POLICY_OPEN:
164 case ACL_POLICY_RADIUS:
166 case ACL_POLICY_ALLOW:
167 return _find_acl(as, wh->i_addr2) != NULL;
168 case ACL_POLICY_DENY:
169 return _find_acl(as, wh->i_addr2) == NULL;
171 return 0; /* should not happen */
175 acl_add(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
177 struct aclstate *as = vap->iv_as;
178 struct acl *acl, *new;
181 new = (struct acl *) IEEE80211_MALLOC(sizeof(struct acl),
182 M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
184 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
185 "ACL: add %s failed, no memory\n", ether_sprintf(mac));
191 hash = ACL_HASH(mac);
192 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
193 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) {
195 IEEE80211_FREE(new, M_80211_ACL);
196 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
197 "ACL: add %s failed, already present\n",
202 IEEE80211_ADDR_COPY(new->acl_macaddr, mac);
203 TAILQ_INSERT_TAIL(&as->as_list, new, acl_list);
204 LIST_INSERT_HEAD(&as->as_hash[hash], new, acl_hash);
208 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
209 "ACL: add %s\n", ether_sprintf(mac));
214 acl_remove(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
216 struct aclstate *as = vap->iv_as;
220 acl = _find_acl(as, mac);
225 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
226 "ACL: remove %s%s\n", ether_sprintf(mac),
227 acl == NULL ? ", not present" : "");
229 return (acl == NULL ? ENOENT : 0);
233 acl_free_all(struct ieee80211vap *vap)
235 struct aclstate *as = vap->iv_as;
238 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
241 while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
249 acl_setpolicy(struct ieee80211vap *vap, int policy)
251 struct aclstate *as = vap->iv_as;
253 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
254 "ACL: set policy to %u\n", policy);
257 case IEEE80211_MACCMD_POLICY_OPEN:
258 as->as_policy = ACL_POLICY_OPEN;
260 case IEEE80211_MACCMD_POLICY_ALLOW:
261 as->as_policy = ACL_POLICY_ALLOW;
263 case IEEE80211_MACCMD_POLICY_DENY:
264 as->as_policy = ACL_POLICY_DENY;
266 case IEEE80211_MACCMD_POLICY_RADIUS:
267 as->as_policy = ACL_POLICY_RADIUS;
276 acl_getpolicy(struct ieee80211vap *vap)
278 struct aclstate *as = vap->iv_as;
280 return as->as_policy;
284 acl_setioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
291 acl_getioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
293 struct aclstate *as = vap->iv_as;
295 struct ieee80211req_maclist *ap;
299 switch (ireq->i_val) {
300 case IEEE80211_MACCMD_POLICY:
301 ireq->i_val = as->as_policy;
303 case IEEE80211_MACCMD_LIST:
304 space = as->as_nacls * IEEE80211_ADDR_LEN;
305 if (ireq->i_len == 0) {
306 ireq->i_len = space; /* return required space */
307 return 0; /* NB: must not error */
309 ap = (struct ieee80211req_maclist *) IEEE80211_MALLOC(space,
310 M_TEMP, IEEE80211_M_NOWAIT);
315 TAILQ_FOREACH(acl, &as->as_list, acl_list) {
316 IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr);
320 if (ireq->i_len >= space) {
321 error = copyout(ap, ireq->i_data, space);
324 error = copyout(ap, ireq->i_data, ireq->i_len);
325 IEEE80211_FREE(ap, M_TEMP);
331 static const struct ieee80211_aclator mac = {
333 .iac_attach = acl_attach,
334 .iac_detach = acl_detach,
335 .iac_check = acl_check,
337 .iac_remove = acl_remove,
338 .iac_flush = acl_free_all,
339 .iac_setpolicy = acl_setpolicy,
340 .iac_getpolicy = acl_getpolicy,
341 .iac_setioctl = acl_setioctl,
342 .iac_getioctl = acl_getioctl,
344 IEEE80211_ACL_MODULE(wlan_acl, mac, 1);