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