1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004-2008 Sam Leffler, Errno Consulting 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * IEEE 802.11 MAC ACL support. 33 * 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). 41 */ 42 #include "opt_wlan.h" 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/systm.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/module.h> 50 #include <sys/queue.h> 51 52 #include <sys/socket.h> 53 54 #include <net/if.h> 55 #include <net/if_media.h> 56 #include <net/ethernet.h> 57 #include <net/route.h> 58 59 #include <net80211/ieee80211_var.h> 60 61 enum { 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 */ 65 /* 66 * NB: ACL_POLICY_RADIUS must be the same value as 67 * IEEE80211_MACCMD_POLICY_RADIUS because of the way 68 * acl_getpolicy() works. 69 */ 70 ACL_POLICY_RADIUS = 7, /* defer to RADIUS ACL server */ 71 }; 72 73 #define ACL_HASHSIZE 32 74 75 struct acl { 76 TAILQ_ENTRY(acl) acl_list; 77 LIST_ENTRY(acl) acl_hash; 78 uint8_t acl_macaddr[IEEE80211_ADDR_LEN]; 79 }; 80 struct aclstate { 81 acl_lock_t as_lock; 82 int as_policy; 83 uint32_t as_nacls; 84 TAILQ_HEAD(, acl) as_list; /* list of all ACL's */ 85 LIST_HEAD(, acl) as_hash[ACL_HASHSIZE]; 86 struct ieee80211vap *as_vap; 87 }; 88 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) 92 93 static MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl"); 94 95 static int acl_free_all(struct ieee80211vap *); 96 97 /* number of references from net80211 layer */ 98 static int nrefs = 0; 99 100 static int 101 acl_attach(struct ieee80211vap *vap) 102 { 103 struct aclstate *as; 104 105 as = (struct aclstate *) IEEE80211_MALLOC(sizeof(struct aclstate), 106 M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 107 if (as == NULL) 108 return 0; 109 ACL_LOCK_INIT(as, "acl"); 110 TAILQ_INIT(&as->as_list); 111 as->as_policy = ACL_POLICY_OPEN; 112 as->as_vap = vap; 113 vap->iv_as = as; 114 nrefs++; /* NB: we assume caller locking */ 115 return 1; 116 } 117 118 static void 119 acl_detach(struct ieee80211vap *vap) 120 { 121 struct aclstate *as = vap->iv_as; 122 123 KASSERT(nrefs > 0, ("imbalanced attach/detach")); 124 nrefs--; /* NB: we assume caller locking */ 125 126 acl_free_all(vap); 127 vap->iv_as = NULL; 128 ACL_LOCK_DESTROY(as); 129 IEEE80211_FREE(as, M_80211_ACL); 130 } 131 132 static __inline struct acl * 133 _find_acl(struct aclstate *as, const uint8_t *macaddr) 134 { 135 struct acl *acl; 136 int hash; 137 138 hash = ACL_HASH(macaddr); 139 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) { 140 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr)) 141 return acl; 142 } 143 return NULL; 144 } 145 146 static void 147 _acl_free(struct aclstate *as, struct acl *acl) 148 { 149 ACL_LOCK_ASSERT(as); 150 151 TAILQ_REMOVE(&as->as_list, acl, acl_list); 152 LIST_REMOVE(acl, acl_hash); 153 IEEE80211_FREE(acl, M_80211_ACL); 154 as->as_nacls--; 155 } 156 157 static int 158 acl_check(struct ieee80211vap *vap, const struct ieee80211_frame *wh) 159 { 160 struct aclstate *as = vap->iv_as; 161 162 switch (as->as_policy) { 163 case ACL_POLICY_OPEN: 164 case ACL_POLICY_RADIUS: 165 return 1; 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; 170 } 171 return 0; /* should not happen */ 172 } 173 174 static int 175 acl_add(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 176 { 177 struct aclstate *as = vap->iv_as; 178 struct acl *acl, *new; 179 int hash; 180 181 new = (struct acl *) IEEE80211_MALLOC(sizeof(struct acl), 182 M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO); 183 if (new == NULL) { 184 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, 185 "ACL: add %s failed, no memory\n", ether_sprintf(mac)); 186 /* XXX statistic */ 187 return ENOMEM; 188 } 189 190 ACL_LOCK(as); 191 hash = ACL_HASH(mac); 192 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) { 193 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) { 194 ACL_UNLOCK(as); 195 IEEE80211_FREE(new, M_80211_ACL); 196 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, 197 "ACL: add %s failed, already present\n", 198 ether_sprintf(mac)); 199 return EEXIST; 200 } 201 } 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); 205 as->as_nacls++; 206 ACL_UNLOCK(as); 207 208 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, 209 "ACL: add %s\n", ether_sprintf(mac)); 210 return 0; 211 } 212 213 static int 214 acl_remove(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 215 { 216 struct aclstate *as = vap->iv_as; 217 struct acl *acl; 218 219 ACL_LOCK(as); 220 acl = _find_acl(as, mac); 221 if (acl != NULL) 222 _acl_free(as, acl); 223 ACL_UNLOCK(as); 224 225 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, 226 "ACL: remove %s%s\n", ether_sprintf(mac), 227 acl == NULL ? ", not present" : ""); 228 229 return (acl == NULL ? ENOENT : 0); 230 } 231 232 static int 233 acl_free_all(struct ieee80211vap *vap) 234 { 235 struct aclstate *as = vap->iv_as; 236 struct acl *acl; 237 238 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, "ACL: %s\n", "free all"); 239 240 ACL_LOCK(as); 241 while ((acl = TAILQ_FIRST(&as->as_list)) != NULL) 242 _acl_free(as, acl); 243 ACL_UNLOCK(as); 244 245 return 0; 246 } 247 248 static int 249 acl_setpolicy(struct ieee80211vap *vap, int policy) 250 { 251 struct aclstate *as = vap->iv_as; 252 253 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, 254 "ACL: set policy to %u\n", policy); 255 256 switch (policy) { 257 case IEEE80211_MACCMD_POLICY_OPEN: 258 as->as_policy = ACL_POLICY_OPEN; 259 break; 260 case IEEE80211_MACCMD_POLICY_ALLOW: 261 as->as_policy = ACL_POLICY_ALLOW; 262 break; 263 case IEEE80211_MACCMD_POLICY_DENY: 264 as->as_policy = ACL_POLICY_DENY; 265 break; 266 case IEEE80211_MACCMD_POLICY_RADIUS: 267 as->as_policy = ACL_POLICY_RADIUS; 268 break; 269 default: 270 return EINVAL; 271 } 272 return 0; 273 } 274 275 static int 276 acl_getpolicy(struct ieee80211vap *vap) 277 { 278 struct aclstate *as = vap->iv_as; 279 280 return as->as_policy; 281 } 282 283 static int 284 acl_setioctl(struct ieee80211vap *vap, struct ieee80211req *ireq) 285 { 286 287 return EINVAL; 288 } 289 290 static int 291 acl_getioctl(struct ieee80211vap *vap, struct ieee80211req *ireq) 292 { 293 struct aclstate *as = vap->iv_as; 294 struct acl *acl; 295 struct ieee80211req_maclist *ap; 296 int error; 297 uint32_t i, space; 298 299 switch (ireq->i_val) { 300 case IEEE80211_MACCMD_POLICY: 301 ireq->i_val = as->as_policy; 302 return 0; 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 */ 308 } 309 ap = (struct ieee80211req_maclist *) IEEE80211_MALLOC(space, 310 M_TEMP, IEEE80211_M_NOWAIT); 311 if (ap == NULL) 312 return ENOMEM; 313 i = 0; 314 ACL_LOCK(as); 315 TAILQ_FOREACH(acl, &as->as_list, acl_list) { 316 IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr); 317 i++; 318 } 319 ACL_UNLOCK(as); 320 if (ireq->i_len >= space) { 321 error = copyout(ap, ireq->i_data, space); 322 ireq->i_len = space; 323 } else 324 error = copyout(ap, ireq->i_data, ireq->i_len); 325 IEEE80211_FREE(ap, M_TEMP); 326 return error; 327 } 328 return EINVAL; 329 } 330 331 static const struct ieee80211_aclator mac = { 332 .iac_name = "mac", 333 .iac_attach = acl_attach, 334 .iac_detach = acl_detach, 335 .iac_check = acl_check, 336 .iac_add = acl_add, 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, 343 }; 344 IEEE80211_ACL_MODULE(wlan_acl, mac, 1); 345