Home | History | Annotate | Line # | Download | only in net80211
ieee80211_acl.c revision 1.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.1  dyoung __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_acl.c,v 1.3 2004/12/31 22:42:38 sam Exp $");
     34  1.1  dyoung 
     35  1.1  dyoung /*
     36  1.1  dyoung  * IEEE 802.11 MAC ACL support.
     37  1.1  dyoung  *
     38  1.1  dyoung  * When this module is loaded the sender address of each received
     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.1  dyoung #include <sys/param.h>
     47  1.1  dyoung #include <sys/kernel.h>
     48  1.1  dyoung #include <sys/systm.h>
     49  1.1  dyoung #include <sys/mbuf.h>
     50  1.1  dyoung #include <sys/module.h>
     51  1.1  dyoung #include <sys/queue.h>
     52  1.1  dyoung 
     53  1.1  dyoung #include <sys/socket.h>
     54  1.1  dyoung 
     55  1.1  dyoung #include <net/if.h>
     56  1.1  dyoung #include <net/if_media.h>
     57  1.1  dyoung #include <net/ethernet.h>
     58  1.1  dyoung #include <net/route.h>
     59  1.1  dyoung 
     60  1.1  dyoung #include <net80211/ieee80211_var.h>
     61  1.1  dyoung 
     62  1.1  dyoung enum {
     63  1.1  dyoung 	ACL_POLICY_OPEN		= 0,	/* open, don't check ACL's */
     64  1.1  dyoung 	ACL_POLICY_ALLOW	= 1,	/* allow traffic from MAC */
     65  1.1  dyoung 	ACL_POLICY_DENY		= 2,	/* deny traffic from MAC */
     66  1.1  dyoung };
     67  1.1  dyoung 
     68  1.1  dyoung #define	ACL_HASHSIZE	32
     69  1.1  dyoung 
     70  1.1  dyoung struct acl {
     71  1.1  dyoung 	TAILQ_ENTRY(acl)	acl_list;
     72  1.1  dyoung 	LIST_ENTRY(acl)		acl_hash;
     73  1.1  dyoung 	u_int8_t		acl_macaddr[IEEE80211_ADDR_LEN];
     74  1.1  dyoung };
     75  1.1  dyoung struct aclstate {
     76  1.1  dyoung 	acl_lock_t		as_lock;
     77  1.1  dyoung 	int			as_policy;
     78  1.1  dyoung 	TAILQ_HEAD(, acl)	as_list;	/* list of all ACL's */
     79  1.1  dyoung 	LIST_HEAD(, acl)	as_hash[ACL_HASHSIZE];
     80  1.1  dyoung 	struct ieee80211com	*as_ic;
     81  1.1  dyoung };
     82  1.1  dyoung 
     83  1.1  dyoung /* simple hash is enough for variation of macaddr */
     84  1.1  dyoung #define	ACL_HASH(addr)	\
     85  1.1  dyoung 	(((const u_int8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
     86  1.1  dyoung 
     87  1.1  dyoung MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl");
     88  1.1  dyoung 
     89  1.1  dyoung static	int acl_free_all(struct ieee80211com *);
     90  1.1  dyoung 
     91  1.1  dyoung static int
     92  1.1  dyoung acl_attach(struct ieee80211com *ic)
     93  1.1  dyoung {
     94  1.1  dyoung 	struct aclstate *as;
     95  1.1  dyoung 
     96  1.1  dyoung 	MALLOC(as, struct aclstate *, sizeof(struct aclstate),
     97  1.1  dyoung 		M_DEVBUF, M_NOWAIT | M_ZERO);
     98  1.1  dyoung 	if (as == NULL)
     99  1.1  dyoung 		return 0;
    100  1.1  dyoung 	ACL_LOCK_INIT(as, "acl");
    101  1.1  dyoung 	TAILQ_INIT(&as->as_list);
    102  1.1  dyoung 	as->as_policy = ACL_POLICY_OPEN;
    103  1.1  dyoung 	as->as_ic = ic;
    104  1.1  dyoung 	ic->ic_as = as;
    105  1.1  dyoung 	return 1;
    106  1.1  dyoung }
    107  1.1  dyoung 
    108  1.1  dyoung static void
    109  1.1  dyoung acl_detach(struct ieee80211com *ic)
    110  1.1  dyoung {
    111  1.1  dyoung 	struct aclstate *as = ic->ic_as;
    112  1.1  dyoung 
    113  1.1  dyoung 	acl_free_all(ic);
    114  1.1  dyoung 	ic->ic_as = NULL;
    115  1.1  dyoung 	ACL_LOCK_DESTROY(as);
    116  1.1  dyoung 	FREE(as, M_DEVBUF);
    117  1.1  dyoung }
    118  1.1  dyoung 
    119  1.1  dyoung static __inline struct acl *
    120  1.1  dyoung _find_acl(struct aclstate *as, const u_int8_t *macaddr)
    121  1.1  dyoung {
    122  1.1  dyoung 	struct acl *acl;
    123  1.1  dyoung 	int hash;
    124  1.1  dyoung 
    125  1.1  dyoung 	hash = ACL_HASH(macaddr);
    126  1.1  dyoung 	LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
    127  1.1  dyoung 		if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
    128  1.1  dyoung 			return acl;
    129  1.1  dyoung 	}
    130  1.1  dyoung 	return NULL;
    131  1.1  dyoung }
    132  1.1  dyoung 
    133  1.1  dyoung static void
    134  1.1  dyoung _acl_free(struct aclstate *as, struct acl *acl)
    135  1.1  dyoung {
    136  1.1  dyoung 	ACL_LOCK_ASSERT(as);
    137  1.1  dyoung 
    138  1.1  dyoung 	TAILQ_REMOVE(&as->as_list, acl, acl_list);
    139  1.1  dyoung 	LIST_REMOVE(acl, acl_hash);
    140  1.1  dyoung 	FREE(acl, M_80211_ACL);
    141  1.1  dyoung }
    142  1.1  dyoung 
    143  1.1  dyoung static int
    144  1.1  dyoung acl_check(struct ieee80211com *ic, const u_int8_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.1  dyoung acl_add(struct ieee80211com *ic, const u_int8_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.1  dyoung 	MALLOC(new, struct acl *, 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.1  dyoung 	ACL_UNLOCK(as);
    190  1.1  dyoung 
    191  1.1  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
    192  1.1  dyoung 		"ACL: add %s\n", ether_sprintf(mac));
    193  1.1  dyoung 	return 0;
    194  1.1  dyoung }
    195  1.1  dyoung 
    196  1.1  dyoung static int
    197  1.1  dyoung acl_remove(struct ieee80211com *ic, const u_int8_t mac[IEEE80211_ADDR_LEN])
    198  1.1  dyoung {
    199  1.1  dyoung 	struct aclstate *as = ic->ic_as;
    200  1.1  dyoung 	struct acl *acl;
    201  1.1  dyoung 
    202  1.1  dyoung 	ACL_LOCK(as);
    203  1.1  dyoung 	acl = _find_acl(as, mac);
    204  1.1  dyoung 	if (acl != NULL)
    205  1.1  dyoung 		_acl_free(as, acl);
    206  1.1  dyoung 	ACL_UNLOCK(as);
    207  1.1  dyoung 
    208  1.1  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
    209  1.1  dyoung 		"ACL: remove %s%s\n", ether_sprintf(mac),
    210  1.1  dyoung 		acl == NULL ? ", not present" : "");
    211  1.1  dyoung 
    212  1.1  dyoung 	return (acl == NULL ? ENOENT : 0);
    213  1.1  dyoung }
    214  1.1  dyoung 
    215  1.1  dyoung static int
    216  1.1  dyoung acl_free_all(struct ieee80211com *ic)
    217  1.1  dyoung {
    218  1.1  dyoung 	struct aclstate *as = ic->ic_as;
    219  1.1  dyoung 	struct acl *acl;
    220  1.1  dyoung 
    221  1.1  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
    222  1.1  dyoung 
    223  1.1  dyoung 	ACL_LOCK(as);
    224  1.1  dyoung 	while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
    225  1.1  dyoung 		_acl_free(as, acl);
    226  1.1  dyoung 	ACL_UNLOCK(as);
    227  1.1  dyoung 
    228  1.1  dyoung 	return 0;
    229  1.1  dyoung }
    230  1.1  dyoung 
    231  1.1  dyoung static int
    232  1.1  dyoung acl_setpolicy(struct ieee80211com *ic, int policy)
    233  1.1  dyoung {
    234  1.1  dyoung 	struct aclstate *as = ic->ic_as;
    235  1.1  dyoung 
    236  1.1  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
    237  1.1  dyoung 		"ACL: set policy to %u\n", policy);
    238  1.1  dyoung 
    239  1.1  dyoung 	switch (policy) {
    240  1.1  dyoung 	case IEEE80211_MACCMD_POLICY_OPEN:
    241  1.1  dyoung 		as->as_policy = ACL_POLICY_OPEN;
    242  1.1  dyoung 		break;
    243  1.1  dyoung 	case IEEE80211_MACCMD_POLICY_ALLOW:
    244  1.1  dyoung 		as->as_policy = ACL_POLICY_ALLOW;
    245  1.1  dyoung 		break;
    246  1.1  dyoung 	case IEEE80211_MACCMD_POLICY_DENY:
    247  1.1  dyoung 		as->as_policy = ACL_POLICY_DENY;
    248  1.1  dyoung 		break;
    249  1.1  dyoung 	default:
    250  1.1  dyoung 		return EINVAL;
    251  1.1  dyoung 	}
    252  1.1  dyoung 	return 0;
    253  1.1  dyoung }
    254  1.1  dyoung 
    255  1.1  dyoung static int
    256  1.1  dyoung acl_getpolicy(struct ieee80211com *ic)
    257  1.1  dyoung {
    258  1.1  dyoung 	struct aclstate *as = ic->ic_as;
    259  1.1  dyoung 
    260  1.1  dyoung 	return as->as_policy;
    261  1.1  dyoung }
    262  1.1  dyoung 
    263  1.1  dyoung static const struct ieee80211_aclator mac = {
    264  1.1  dyoung 	.iac_name	= "mac",
    265  1.1  dyoung 	.iac_attach	= acl_attach,
    266  1.1  dyoung 	.iac_detach	= acl_detach,
    267  1.1  dyoung 	.iac_check	= acl_check,
    268  1.1  dyoung 	.iac_add	= acl_add,
    269  1.1  dyoung 	.iac_remove	= acl_remove,
    270  1.1  dyoung 	.iac_flush	= acl_free_all,
    271  1.1  dyoung 	.iac_setpolicy	= acl_setpolicy,
    272  1.1  dyoung 	.iac_getpolicy	= acl_getpolicy,
    273  1.1  dyoung };
    274  1.1  dyoung 
    275  1.1  dyoung /*
    276  1.1  dyoung  * Module glue.
    277  1.1  dyoung  */
    278  1.1  dyoung static int
    279  1.1  dyoung wlan_acl_modevent(module_t mod, int type, void *unused)
    280  1.1  dyoung {
    281  1.1  dyoung 	switch (type) {
    282  1.1  dyoung 	case MOD_LOAD:
    283  1.1  dyoung 		if (bootverbose)
    284  1.1  dyoung 			printf("wlan: <802.11 MAC ACL support>\n");
    285  1.1  dyoung 		ieee80211_aclator_register(&mac);
    286  1.1  dyoung 		return 0;
    287  1.1  dyoung 	case MOD_UNLOAD:
    288  1.1  dyoung 		ieee80211_aclator_unregister(&mac);
    289  1.1  dyoung 		return 0;
    290  1.1  dyoung 	}
    291  1.1  dyoung 	return EINVAL;
    292  1.1  dyoung }
    293  1.1  dyoung 
    294  1.1  dyoung static moduledata_t wlan_acl_mod = {
    295  1.1  dyoung 	"wlan_acl",
    296  1.1  dyoung 	wlan_acl_modevent,
    297  1.1  dyoung 	0
    298  1.1  dyoung };
    299  1.1  dyoung DECLARE_MODULE(wlan_acl, wlan_acl_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
    300  1.1  dyoung MODULE_VERSION(wlan_acl, 1);
    301  1.1  dyoung MODULE_DEPEND(wlan_acl, wlan, 1, 1, 1);
    302