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