Home | History | Annotate | Line # | Download | only in net80211
ieee80211_acl.c revision 1.9.54.3
      1  1.9.54.3      phil /*	$NetBSD: ieee80211_acl.c,v 1.9.54.3 2018/07/16 20:11:11 phil 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.3      phil #if __FreeBSD__
     32  1.9.54.1      phil __FBSDID("$FreeBSD$");
     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.1      phil #include "opt_wlan.h"
     47  1.9.54.1      phil 
     48       1.1    dyoung #include <sys/param.h>
     49       1.1    dyoung #include <sys/kernel.h>
     50       1.1    dyoung #include <sys/systm.h>
     51  1.9.54.1      phil #include <sys/malloc.h>
     52       1.1    dyoung #include <sys/mbuf.h>
     53  1.9.54.1      phil #include <sys/module.h>
     54       1.1    dyoung #include <sys/queue.h>
     55       1.1    dyoung 
     56       1.1    dyoung #include <sys/socket.h>
     57       1.1    dyoung 
     58       1.1    dyoung #include <net/if.h>
     59       1.1    dyoung #include <net/if_media.h>
     60  1.9.54.3      phil #if __FreeBSD__
     61  1.9.54.1      phil #include <net/ethernet.h>
     62  1.9.54.3      phil #elif __NetBSD__
     63  1.9.54.3      phil #include <net/if_ether.h>
     64  1.9.54.2      phil #endif
     65       1.1    dyoung #include <net/route.h>
     66       1.1    dyoung 
     67       1.1    dyoung #include <net80211/ieee80211_var.h>
     68       1.1    dyoung 
     69  1.9.54.2      phil #ifdef __NetBSD__
     70  1.9.54.2      phil #undef KASSERT
     71  1.9.54.2      phil #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     72  1.9.54.2      phil #endif
     73  1.9.54.2      phil 
     74       1.1    dyoung enum {
     75       1.1    dyoung 	ACL_POLICY_OPEN		= 0,	/* open, don't check ACL's */
     76       1.1    dyoung 	ACL_POLICY_ALLOW	= 1,	/* allow traffic from MAC */
     77       1.1    dyoung 	ACL_POLICY_DENY		= 2,	/* deny traffic from MAC */
     78  1.9.54.1      phil 	/*
     79  1.9.54.1      phil 	 * NB: ACL_POLICY_RADIUS must be the same value as
     80  1.9.54.1      phil 	 *     IEEE80211_MACCMD_POLICY_RADIUS because of the way
     81  1.9.54.1      phil 	 *     acl_getpolicy() works.
     82  1.9.54.1      phil 	 */
     83  1.9.54.1      phil 	ACL_POLICY_RADIUS	= 7,	/* defer to RADIUS ACL server */
     84       1.1    dyoung };
     85       1.1    dyoung 
     86       1.1    dyoung #define	ACL_HASHSIZE	32
     87       1.1    dyoung 
     88       1.1    dyoung struct acl {
     89       1.1    dyoung 	TAILQ_ENTRY(acl)	acl_list;
     90       1.1    dyoung 	LIST_ENTRY(acl)		acl_hash;
     91  1.9.54.1      phil 	uint8_t			acl_macaddr[IEEE80211_ADDR_LEN];
     92       1.1    dyoung };
     93       1.1    dyoung struct aclstate {
     94       1.1    dyoung 	acl_lock_t		as_lock;
     95       1.1    dyoung 	int			as_policy;
     96       1.9  christos 	uint32_t		as_nacls;
     97       1.1    dyoung 	TAILQ_HEAD(, acl)	as_list;	/* list of all ACL's */
     98       1.1    dyoung 	LIST_HEAD(, acl)	as_hash[ACL_HASHSIZE];
     99  1.9.54.1      phil 	struct ieee80211vap	*as_vap;
    100       1.1    dyoung };
    101       1.1    dyoung 
    102       1.1    dyoung /* simple hash is enough for variation of macaddr */
    103       1.1    dyoung #define	ACL_HASH(addr)	\
    104  1.9.54.1      phil 	(((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
    105       1.1    dyoung 
    106  1.9.54.3      phil #if __FreeBSD__
    107  1.9.54.1      phil static MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl");
    108  1.9.54.2      phil #endif
    109       1.1    dyoung 
    110  1.9.54.1      phil static	int acl_free_all(struct ieee80211vap *);
    111  1.9.54.1      phil 
    112  1.9.54.1      phil /* number of references from net80211 layer */
    113  1.9.54.1      phil static	int nrefs = 0;
    114       1.1    dyoung 
    115       1.1    dyoung static int
    116  1.9.54.1      phil acl_attach(struct ieee80211vap *vap)
    117       1.1    dyoung {
    118       1.1    dyoung 	struct aclstate *as;
    119       1.1    dyoung 
    120  1.9.54.1      phil 	as = (struct aclstate *) IEEE80211_MALLOC(sizeof(struct aclstate),
    121  1.9.54.1      phil 		M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    122       1.1    dyoung 	if (as == NULL)
    123       1.1    dyoung 		return 0;
    124       1.1    dyoung 	ACL_LOCK_INIT(as, "acl");
    125       1.1    dyoung 	TAILQ_INIT(&as->as_list);
    126       1.1    dyoung 	as->as_policy = ACL_POLICY_OPEN;
    127  1.9.54.1      phil 	as->as_vap = vap;
    128  1.9.54.1      phil 	vap->iv_as = as;
    129  1.9.54.1      phil 	nrefs++;			/* NB: we assume caller locking */
    130       1.1    dyoung 	return 1;
    131       1.1    dyoung }
    132       1.1    dyoung 
    133       1.1    dyoung static void
    134  1.9.54.1      phil acl_detach(struct ieee80211vap *vap)
    135       1.1    dyoung {
    136  1.9.54.1      phil 	struct aclstate *as = vap->iv_as;
    137  1.9.54.1      phil 
    138  1.9.54.1      phil 	KASSERT(nrefs > 0, ("imbalanced attach/detach"));
    139  1.9.54.1      phil 	nrefs--;			/* NB: we assume caller locking */
    140       1.1    dyoung 
    141  1.9.54.1      phil 	acl_free_all(vap);
    142  1.9.54.1      phil 	vap->iv_as = NULL;
    143       1.1    dyoung 	ACL_LOCK_DESTROY(as);
    144  1.9.54.1      phil 	IEEE80211_FREE(as, M_80211_ACL);
    145       1.1    dyoung }
    146       1.1    dyoung 
    147       1.1    dyoung static __inline struct acl *
    148  1.9.54.1      phil _find_acl(struct aclstate *as, const uint8_t *macaddr)
    149       1.1    dyoung {
    150       1.1    dyoung 	struct acl *acl;
    151       1.1    dyoung 	int hash;
    152       1.1    dyoung 
    153       1.1    dyoung 	hash = ACL_HASH(macaddr);
    154       1.1    dyoung 	LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
    155       1.1    dyoung 		if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
    156       1.1    dyoung 			return acl;
    157       1.1    dyoung 	}
    158       1.1    dyoung 	return NULL;
    159       1.1    dyoung }
    160       1.1    dyoung 
    161       1.1    dyoung static void
    162       1.1    dyoung _acl_free(struct aclstate *as, struct acl *acl)
    163       1.1    dyoung {
    164       1.1    dyoung 	ACL_LOCK_ASSERT(as);
    165       1.1    dyoung 
    166       1.1    dyoung 	TAILQ_REMOVE(&as->as_list, acl, acl_list);
    167       1.1    dyoung 	LIST_REMOVE(acl, acl_hash);
    168  1.9.54.1      phil 	IEEE80211_FREE(acl, M_80211_ACL);
    169       1.4     skrll 	as->as_nacls--;
    170       1.1    dyoung }
    171       1.1    dyoung 
    172       1.1    dyoung static int
    173  1.9.54.1      phil acl_check(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
    174       1.1    dyoung {
    175  1.9.54.1      phil 	struct aclstate *as = vap->iv_as;
    176       1.1    dyoung 
    177       1.1    dyoung 	switch (as->as_policy) {
    178       1.1    dyoung 	case ACL_POLICY_OPEN:
    179  1.9.54.1      phil 	case ACL_POLICY_RADIUS:
    180       1.1    dyoung 		return 1;
    181       1.1    dyoung 	case ACL_POLICY_ALLOW:
    182  1.9.54.1      phil 		return _find_acl(as, wh->i_addr2) != NULL;
    183       1.1    dyoung 	case ACL_POLICY_DENY:
    184  1.9.54.1      phil 		return _find_acl(as, wh->i_addr2) == NULL;
    185       1.1    dyoung 	}
    186       1.1    dyoung 	return 0;		/* should not happen */
    187       1.1    dyoung }
    188       1.1    dyoung 
    189       1.1    dyoung static int
    190  1.9.54.1      phil acl_add(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
    191       1.1    dyoung {
    192  1.9.54.1      phil 	struct aclstate *as = vap->iv_as;
    193       1.1    dyoung 	struct acl *acl, *new;
    194       1.1    dyoung 	int hash;
    195       1.1    dyoung 
    196  1.9.54.1      phil 	new = (struct acl *) IEEE80211_MALLOC(sizeof(struct acl),
    197  1.9.54.1      phil 	    M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    198       1.1    dyoung 	if (new == NULL) {
    199  1.9.54.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    200       1.1    dyoung 			"ACL: add %s failed, no memory\n", ether_sprintf(mac));
    201       1.1    dyoung 		/* XXX statistic */
    202       1.1    dyoung 		return ENOMEM;
    203       1.1    dyoung 	}
    204       1.1    dyoung 
    205       1.1    dyoung 	ACL_LOCK(as);
    206       1.1    dyoung 	hash = ACL_HASH(mac);
    207       1.1    dyoung 	LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
    208       1.1    dyoung 		if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) {
    209       1.1    dyoung 			ACL_UNLOCK(as);
    210  1.9.54.1      phil 			IEEE80211_FREE(new, M_80211_ACL);
    211  1.9.54.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    212       1.1    dyoung 				"ACL: add %s failed, already present\n",
    213       1.1    dyoung 				ether_sprintf(mac));
    214       1.1    dyoung 			return EEXIST;
    215       1.1    dyoung 		}
    216       1.1    dyoung 	}
    217       1.1    dyoung 	IEEE80211_ADDR_COPY(new->acl_macaddr, mac);
    218       1.1    dyoung 	TAILQ_INSERT_TAIL(&as->as_list, new, acl_list);
    219       1.1    dyoung 	LIST_INSERT_HEAD(&as->as_hash[hash], new, acl_hash);
    220       1.4     skrll 	as->as_nacls++;
    221       1.1    dyoung 	ACL_UNLOCK(as);
    222       1.1    dyoung 
    223  1.9.54.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    224       1.1    dyoung 		"ACL: add %s\n", ether_sprintf(mac));
    225       1.1    dyoung 	return 0;
    226       1.1    dyoung }
    227       1.1    dyoung 
    228       1.1    dyoung static int
    229  1.9.54.1      phil acl_remove(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
    230       1.1    dyoung {
    231  1.9.54.1      phil 	struct aclstate *as = vap->iv_as;
    232       1.1    dyoung 	struct acl *acl;
    233       1.1    dyoung 
    234       1.1    dyoung 	ACL_LOCK(as);
    235       1.1    dyoung 	acl = _find_acl(as, mac);
    236       1.1    dyoung 	if (acl != NULL)
    237       1.1    dyoung 		_acl_free(as, acl);
    238       1.1    dyoung 	ACL_UNLOCK(as);
    239       1.1    dyoung 
    240  1.9.54.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    241       1.1    dyoung 		"ACL: remove %s%s\n", ether_sprintf(mac),
    242       1.1    dyoung 		acl == NULL ? ", not present" : "");
    243       1.1    dyoung 
    244       1.1    dyoung 	return (acl == NULL ? ENOENT : 0);
    245       1.1    dyoung }
    246       1.1    dyoung 
    247       1.1    dyoung static int
    248  1.9.54.1      phil acl_free_all(struct ieee80211vap *vap)
    249       1.1    dyoung {
    250  1.9.54.1      phil 	struct aclstate *as = vap->iv_as;
    251       1.1    dyoung 	struct acl *acl;
    252       1.1    dyoung 
    253  1.9.54.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
    254       1.1    dyoung 
    255       1.1    dyoung 	ACL_LOCK(as);
    256       1.1    dyoung 	while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
    257       1.1    dyoung 		_acl_free(as, acl);
    258       1.1    dyoung 	ACL_UNLOCK(as);
    259       1.1    dyoung 
    260       1.1    dyoung 	return 0;
    261       1.1    dyoung }
    262       1.1    dyoung 
    263       1.1    dyoung static int
    264  1.9.54.1      phil acl_setpolicy(struct ieee80211vap *vap, int policy)
    265       1.1    dyoung {
    266  1.9.54.1      phil 	struct aclstate *as = vap->iv_as;
    267       1.1    dyoung 
    268  1.9.54.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
    269       1.1    dyoung 		"ACL: set policy to %u\n", policy);
    270       1.1    dyoung 
    271       1.1    dyoung 	switch (policy) {
    272       1.1    dyoung 	case IEEE80211_MACCMD_POLICY_OPEN:
    273       1.1    dyoung 		as->as_policy = ACL_POLICY_OPEN;
    274       1.1    dyoung 		break;
    275       1.1    dyoung 	case IEEE80211_MACCMD_POLICY_ALLOW:
    276       1.1    dyoung 		as->as_policy = ACL_POLICY_ALLOW;
    277       1.1    dyoung 		break;
    278       1.1    dyoung 	case IEEE80211_MACCMD_POLICY_DENY:
    279       1.1    dyoung 		as->as_policy = ACL_POLICY_DENY;
    280       1.1    dyoung 		break;
    281  1.9.54.1      phil 	case IEEE80211_MACCMD_POLICY_RADIUS:
    282  1.9.54.1      phil 		as->as_policy = ACL_POLICY_RADIUS;
    283  1.9.54.1      phil 		break;
    284       1.1    dyoung 	default:
    285       1.1    dyoung 		return EINVAL;
    286       1.1    dyoung 	}
    287       1.1    dyoung 	return 0;
    288       1.1    dyoung }
    289       1.1    dyoung 
    290       1.1    dyoung static int
    291  1.9.54.1      phil acl_getpolicy(struct ieee80211vap *vap)
    292       1.1    dyoung {
    293  1.9.54.1      phil 	struct aclstate *as = vap->iv_as;
    294       1.1    dyoung 
    295       1.1    dyoung 	return as->as_policy;
    296       1.1    dyoung }
    297       1.1    dyoung 
    298       1.4     skrll static int
    299  1.9.54.1      phil acl_setioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
    300       1.4     skrll {
    301       1.4     skrll 
    302       1.4     skrll 	return EINVAL;
    303       1.4     skrll }
    304       1.4     skrll 
    305       1.4     skrll static int
    306  1.9.54.1      phil acl_getioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
    307       1.4     skrll {
    308  1.9.54.1      phil 	struct aclstate *as = vap->iv_as;
    309       1.4     skrll 	struct acl *acl;
    310       1.4     skrll 	struct ieee80211req_maclist *ap;
    311       1.9  christos 	int error;
    312       1.9  christos 	uint32_t i, space;
    313       1.4     skrll 
    314       1.4     skrll 	switch (ireq->i_val) {
    315       1.4     skrll 	case IEEE80211_MACCMD_POLICY:
    316       1.4     skrll 		ireq->i_val = as->as_policy;
    317       1.4     skrll 		return 0;
    318       1.4     skrll 	case IEEE80211_MACCMD_LIST:
    319       1.4     skrll 		space = as->as_nacls * IEEE80211_ADDR_LEN;
    320       1.4     skrll 		if (ireq->i_len == 0) {
    321       1.4     skrll 			ireq->i_len = space;	/* return required space */
    322       1.4     skrll 			return 0;		/* NB: must not error */
    323       1.4     skrll 		}
    324  1.9.54.1      phil 		ap = (struct ieee80211req_maclist *) IEEE80211_MALLOC(space,
    325  1.9.54.1      phil 		    M_TEMP, IEEE80211_M_NOWAIT);
    326       1.4     skrll 		if (ap == NULL)
    327       1.4     skrll 			return ENOMEM;
    328       1.4     skrll 		i = 0;
    329       1.4     skrll 		ACL_LOCK(as);
    330       1.4     skrll 		TAILQ_FOREACH(acl, &as->as_list, acl_list) {
    331       1.4     skrll 			IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr);
    332       1.4     skrll 			i++;
    333       1.4     skrll 		}
    334       1.4     skrll 		ACL_UNLOCK(as);
    335       1.4     skrll 		if (ireq->i_len >= space) {
    336       1.4     skrll 			error = copyout(ap, ireq->i_data, space);
    337       1.4     skrll 			ireq->i_len = space;
    338       1.4     skrll 		} else
    339       1.4     skrll 			error = copyout(ap, ireq->i_data, ireq->i_len);
    340  1.9.54.1      phil 		IEEE80211_FREE(ap, M_TEMP);
    341       1.4     skrll 		return error;
    342       1.4     skrll 	}
    343       1.4     skrll 	return EINVAL;
    344       1.4     skrll }
    345       1.4     skrll 
    346       1.1    dyoung static const struct ieee80211_aclator mac = {
    347       1.1    dyoung 	.iac_name	= "mac",
    348       1.1    dyoung 	.iac_attach	= acl_attach,
    349       1.1    dyoung 	.iac_detach	= acl_detach,
    350       1.1    dyoung 	.iac_check	= acl_check,
    351       1.1    dyoung 	.iac_add	= acl_add,
    352       1.1    dyoung 	.iac_remove	= acl_remove,
    353       1.1    dyoung 	.iac_flush	= acl_free_all,
    354       1.1    dyoung 	.iac_setpolicy	= acl_setpolicy,
    355       1.1    dyoung 	.iac_getpolicy	= acl_getpolicy,
    356       1.4     skrll 	.iac_setioctl	= acl_setioctl,
    357       1.4     skrll 	.iac_getioctl	= acl_getioctl,
    358       1.1    dyoung };
    359  1.9.54.1      phil IEEE80211_ACL_MODULE(wlan_acl, mac, 1);
    360