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