Home | History | Annotate | Line # | Download | only in net80211
ieee80211_acl.c revision 1.9.54.4
      1 /*	$NetBSD: ieee80211_acl.c,v 1.9.54.4 2019/06/10 22:09:46 christos Exp $ */
      2 
      3 /*-
      4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  *
      6  * Copyright (c) 2004-2008 Sam Leffler, Errno Consulting
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 #ifdef __NetBSD__
     32 __KERNEL_RCSID(0, "$NetBSD: ieee80211_acl.c,v 1.9.54.4 2019/06/10 22:09:46 christos Exp $");
     33 #endif
     34 
     35 /*
     36  * IEEE 802.11 MAC ACL support.
     37  *
     38  * When this module is loaded the sender address of each auth mgt
     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 #ifdef _KERNEL_OPT
     47 #include "opt_wlan.h"
     48 #endif
     49 
     50 #include <sys/param.h>
     51 #include <sys/kernel.h>
     52 #include <sys/systm.h>
     53 #include <sys/malloc.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/module.h>
     56 #include <sys/queue.h>
     57 
     58 #include <sys/socket.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_media.h>
     62 #if __FreeBSD__
     63 #include <net/ethernet.h>
     64 #elif __NetBSD__
     65 #include <net/if_ether.h>
     66 #endif
     67 #include <net/route.h>
     68 
     69 #include <net80211/ieee80211_var.h>
     70 
     71 #ifdef __NetBSD__
     72 #undef KASSERT
     73 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     74 #endif
     75 
     76 enum {
     77 	ACL_POLICY_OPEN		= 0,	/* open, don't check ACL's */
     78 	ACL_POLICY_ALLOW	= 1,	/* allow traffic from MAC */
     79 	ACL_POLICY_DENY		= 2,	/* deny traffic from MAC */
     80 	/*
     81 	 * NB: ACL_POLICY_RADIUS must be the same value as
     82 	 *     IEEE80211_MACCMD_POLICY_RADIUS because of the way
     83 	 *     acl_getpolicy() works.
     84 	 */
     85 	ACL_POLICY_RADIUS	= 7,	/* defer to RADIUS ACL server */
     86 };
     87 
     88 #define	ACL_HASHSIZE	32
     89 
     90 struct acl {
     91 	TAILQ_ENTRY(acl)	acl_list;
     92 	LIST_ENTRY(acl)		acl_hash;
     93 	uint8_t			acl_macaddr[IEEE80211_ADDR_LEN];
     94 };
     95 struct aclstate {
     96 	acl_lock_t		as_lock;
     97 	int			as_policy;
     98 	uint32_t		as_nacls;
     99 	TAILQ_HEAD(, acl)	as_list;	/* list of all ACL's */
    100 	LIST_HEAD(, acl)	as_hash[ACL_HASHSIZE];
    101 	struct ieee80211vap	*as_vap;
    102 };
    103 
    104 /* simple hash is enough for variation of macaddr */
    105 #define	ACL_HASH(addr)	\
    106 	(((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
    107 
    108 #if __FreeBSD__
    109 static MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl");
    110 #endif
    111 
    112 static	int acl_free_all(struct ieee80211vap *);
    113 
    114 /* number of references from net80211 layer */
    115 static	int nrefs = 0;
    116 
    117 static int
    118 acl_attach(struct ieee80211vap *vap)
    119 {
    120 	struct aclstate *as;
    121 
    122 	as = (struct aclstate *) IEEE80211_MALLOC(sizeof(struct aclstate),
    123 		M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    124 	if (as == NULL)
    125 		return 0;
    126 	ACL_LOCK_INIT(as, "acl");
    127 	TAILQ_INIT(&as->as_list);
    128 	as->as_policy = ACL_POLICY_OPEN;
    129 	as->as_vap = vap;
    130 	vap->iv_as = as;
    131 	nrefs++;			/* NB: we assume caller locking */
    132 	return 1;
    133 }
    134 
    135 static void
    136 acl_detach(struct ieee80211vap *vap)
    137 {
    138 	struct aclstate *as = vap->iv_as;
    139 
    140 	KASSERT(nrefs > 0, ("imbalanced attach/detach"));
    141 	nrefs--;			/* NB: we assume caller locking */
    142 
    143 	acl_free_all(vap);
    144 	vap->iv_as = NULL;
    145 	ACL_LOCK_DESTROY(as);
    146 	IEEE80211_FREE(as, M_80211_ACL);
    147 }
    148 
    149 static __inline struct acl *
    150 _find_acl(struct aclstate *as, const uint8_t *macaddr)
    151 {
    152 	struct acl *acl;
    153 	int hash;
    154 
    155 	hash = ACL_HASH(macaddr);
    156 	LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
    157 		if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
    158 			return acl;
    159 	}
    160 	return NULL;
    161 }
    162 
    163 static void
    164 _acl_free(struct aclstate *as, struct acl *acl)
    165 {
    166 	ACL_LOCK_ASSERT(as);
    167 
    168 	TAILQ_REMOVE(&as->as_list, acl, acl_list);
    169 	LIST_REMOVE(acl, acl_hash);
    170 	IEEE80211_FREE(acl, M_80211_ACL);
    171 	as->as_nacls--;
    172 }
    173 
    174 static int
    175 acl_check(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
    176 {
    177 	struct aclstate *as = vap->iv_as;
    178 
    179 	switch (as->as_policy) {
    180 	case ACL_POLICY_OPEN:
    181 	case ACL_POLICY_RADIUS:
    182 		return 1;
    183 	case ACL_POLICY_ALLOW:
    184 		return _find_acl(as, wh->i_addr2) != NULL;
    185 	case ACL_POLICY_DENY:
    186 		return _find_acl(as, wh->i_addr2) == NULL;
    187 	}
    188 	return 0;		/* should not happen */
    189 }
    190 
    191 static int
    192 acl_add(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
    193 {
    194 	struct aclstate *as = vap->iv_as;
    195 	struct acl *acl, *new;
    196 	int hash;
    197 
    198 	new = (struct acl *) IEEE80211_MALLOC(sizeof(struct acl),
    199 	    M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    200 	if (new == NULL) {
    201 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    202 			"ACL: add %s failed, no memory\n", ether_sprintf(mac));
    203 		/* XXX statistic */
    204 		return ENOMEM;
    205 	}
    206 
    207 	ACL_LOCK(as);
    208 	hash = ACL_HASH(mac);
    209 	LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
    210 		if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) {
    211 			ACL_UNLOCK(as);
    212 			IEEE80211_FREE(new, M_80211_ACL);
    213 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    214 				"ACL: add %s failed, already present\n",
    215 				ether_sprintf(mac));
    216 			return EEXIST;
    217 		}
    218 	}
    219 	IEEE80211_ADDR_COPY(new->acl_macaddr, mac);
    220 	TAILQ_INSERT_TAIL(&as->as_list, new, acl_list);
    221 	LIST_INSERT_HEAD(&as->as_hash[hash], new, acl_hash);
    222 	as->as_nacls++;
    223 	ACL_UNLOCK(as);
    224 
    225 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    226 		"ACL: add %s\n", ether_sprintf(mac));
    227 	return 0;
    228 }
    229 
    230 static int
    231 acl_remove(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
    232 {
    233 	struct aclstate *as = vap->iv_as;
    234 	struct acl *acl;
    235 
    236 	ACL_LOCK(as);
    237 	acl = _find_acl(as, mac);
    238 	if (acl != NULL)
    239 		_acl_free(as, acl);
    240 	ACL_UNLOCK(as);
    241 
    242 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    243 		"ACL: remove %s%s\n", ether_sprintf(mac),
    244 		acl == NULL ? ", not present" : "");
    245 
    246 	return (acl == NULL ? ENOENT : 0);
    247 }
    248 
    249 static int
    250 acl_free_all(struct ieee80211vap *vap)
    251 {
    252 	struct aclstate *as = vap->iv_as;
    253 	struct acl *acl;
    254 
    255 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
    256 
    257 	ACL_LOCK(as);
    258 	while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
    259 		_acl_free(as, acl);
    260 	ACL_UNLOCK(as);
    261 
    262 	return 0;
    263 }
    264 
    265 static int
    266 acl_setpolicy(struct ieee80211vap *vap, int policy)
    267 {
    268 	struct aclstate *as = vap->iv_as;
    269 
    270 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    271 		"ACL: set policy to %u\n", policy);
    272 
    273 	switch (policy) {
    274 	case IEEE80211_MACCMD_POLICY_OPEN:
    275 		as->as_policy = ACL_POLICY_OPEN;
    276 		break;
    277 	case IEEE80211_MACCMD_POLICY_ALLOW:
    278 		as->as_policy = ACL_POLICY_ALLOW;
    279 		break;
    280 	case IEEE80211_MACCMD_POLICY_DENY:
    281 		as->as_policy = ACL_POLICY_DENY;
    282 		break;
    283 	case IEEE80211_MACCMD_POLICY_RADIUS:
    284 		as->as_policy = ACL_POLICY_RADIUS;
    285 		break;
    286 	default:
    287 		return EINVAL;
    288 	}
    289 	return 0;
    290 }
    291 
    292 static int
    293 acl_getpolicy(struct ieee80211vap *vap)
    294 {
    295 	struct aclstate *as = vap->iv_as;
    296 
    297 	return as->as_policy;
    298 }
    299 
    300 static int
    301 acl_setioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
    302 {
    303 
    304 	return EINVAL;
    305 }
    306 
    307 static int
    308 acl_getioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
    309 {
    310 	struct aclstate *as = vap->iv_as;
    311 	struct acl *acl;
    312 	struct ieee80211req_maclist *ap;
    313 	int error;
    314 	uint32_t i, space;
    315 
    316 	switch (ireq->i_val) {
    317 	case IEEE80211_MACCMD_POLICY:
    318 		ireq->i_val = as->as_policy;
    319 		return 0;
    320 	case IEEE80211_MACCMD_LIST:
    321 		space = as->as_nacls * IEEE80211_ADDR_LEN;
    322 		if (ireq->i_len == 0) {
    323 			ireq->i_len = space;	/* return required space */
    324 			return 0;		/* NB: must not error */
    325 		}
    326 		ap = (struct ieee80211req_maclist *) IEEE80211_MALLOC(space,
    327 		    M_TEMP, IEEE80211_M_NOWAIT);
    328 		if (ap == NULL)
    329 			return ENOMEM;
    330 		i = 0;
    331 		ACL_LOCK(as);
    332 		TAILQ_FOREACH(acl, &as->as_list, acl_list) {
    333 			IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr);
    334 			i++;
    335 		}
    336 		ACL_UNLOCK(as);
    337 		if (ireq->i_len >= space) {
    338 			error = copyout(ap, ireq->i_data, space);
    339 			ireq->i_len = space;
    340 		} else
    341 			error = copyout(ap, ireq->i_data, ireq->i_len);
    342 		IEEE80211_FREE(ap, M_TEMP);
    343 		return error;
    344 	}
    345 	return EINVAL;
    346 }
    347 
    348 static const struct ieee80211_aclator mac = {
    349 	.iac_name	= "mac",
    350 	.iac_attach	= acl_attach,
    351 	.iac_detach	= acl_detach,
    352 	.iac_check	= acl_check,
    353 	.iac_add	= acl_add,
    354 	.iac_remove	= acl_remove,
    355 	.iac_flush	= acl_free_all,
    356 	.iac_setpolicy	= acl_setpolicy,
    357 	.iac_getpolicy	= acl_getpolicy,
    358 	.iac_setioctl	= acl_setioctl,
    359 	.iac_getioctl	= acl_getioctl,
    360 };
    361 IEEE80211_ACL_MODULE(wlan_acl, mac, 1);
    362