Home | History | Annotate | Line # | Download | only in net80211
      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.10       chs __KERNEL_RCSID(0, "$NetBSD: ieee80211_acl.c,v 1.10 2019/11/10 21:16:38 chs 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.9  christos 	uint32_t		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.8    cegger 	as = malloc(sizeof(struct aclstate),
    102  1.10       chs 		M_80211_ACL, M_WAITOK | M_ZERO);
    103   1.1    dyoung 	ACL_LOCK_INIT(as, "acl");
    104   1.1    dyoung 	TAILQ_INIT(&as->as_list);
    105   1.1    dyoung 	as->as_policy = ACL_POLICY_OPEN;
    106   1.1    dyoung 	as->as_ic = ic;
    107   1.1    dyoung 	ic->ic_as = as;
    108   1.1    dyoung 	return 1;
    109   1.1    dyoung }
    110   1.1    dyoung 
    111   1.1    dyoung static void
    112   1.1    dyoung acl_detach(struct ieee80211com *ic)
    113   1.1    dyoung {
    114   1.1    dyoung 	struct aclstate *as = ic->ic_as;
    115   1.1    dyoung 
    116   1.1    dyoung 	acl_free_all(ic);
    117   1.1    dyoung 	ic->ic_as = NULL;
    118   1.1    dyoung 	ACL_LOCK_DESTROY(as);
    119   1.8    cegger 	free(as, M_DEVBUF);
    120   1.1    dyoung }
    121   1.1    dyoung 
    122   1.1    dyoung static __inline struct acl *
    123   1.1    dyoung _find_acl(struct aclstate *as, const u_int8_t *macaddr)
    124   1.1    dyoung {
    125   1.1    dyoung 	struct acl *acl;
    126   1.1    dyoung 	int hash;
    127   1.1    dyoung 
    128   1.1    dyoung 	hash = ACL_HASH(macaddr);
    129   1.1    dyoung 	LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
    130   1.1    dyoung 		if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
    131   1.1    dyoung 			return acl;
    132   1.1    dyoung 	}
    133   1.1    dyoung 	return NULL;
    134   1.1    dyoung }
    135   1.1    dyoung 
    136   1.1    dyoung static void
    137   1.1    dyoung _acl_free(struct aclstate *as, struct acl *acl)
    138   1.1    dyoung {
    139   1.1    dyoung 	ACL_LOCK_ASSERT(as);
    140   1.1    dyoung 
    141   1.1    dyoung 	TAILQ_REMOVE(&as->as_list, acl, acl_list);
    142   1.1    dyoung 	LIST_REMOVE(acl, acl_hash);
    143   1.8    cegger 	free(acl, M_80211_ACL);
    144   1.4     skrll 	as->as_nacls--;
    145   1.1    dyoung }
    146   1.1    dyoung 
    147   1.1    dyoung static int
    148   1.1    dyoung acl_check(struct ieee80211com *ic, const u_int8_t mac[IEEE80211_ADDR_LEN])
    149   1.1    dyoung {
    150   1.1    dyoung 	struct aclstate *as = ic->ic_as;
    151   1.1    dyoung 
    152   1.1    dyoung 	switch (as->as_policy) {
    153   1.1    dyoung 	case ACL_POLICY_OPEN:
    154   1.1    dyoung 		return 1;
    155   1.1    dyoung 	case ACL_POLICY_ALLOW:
    156   1.1    dyoung 		return _find_acl(as, mac) != NULL;
    157   1.1    dyoung 	case ACL_POLICY_DENY:
    158   1.1    dyoung 		return _find_acl(as, mac) == NULL;
    159   1.1    dyoung 	}
    160   1.1    dyoung 	return 0;		/* should not happen */
    161   1.1    dyoung }
    162   1.1    dyoung 
    163   1.1    dyoung static int
    164   1.1    dyoung acl_add(struct ieee80211com *ic, const u_int8_t mac[IEEE80211_ADDR_LEN])
    165   1.1    dyoung {
    166   1.1    dyoung 	struct aclstate *as = ic->ic_as;
    167   1.1    dyoung 	struct acl *acl, *new;
    168   1.1    dyoung 	int hash;
    169   1.1    dyoung 
    170  1.10       chs 	new = malloc(sizeof(struct acl), M_80211_ACL, M_WAITOK | M_ZERO);
    171   1.1    dyoung 
    172   1.1    dyoung 	ACL_LOCK(as);
    173   1.1    dyoung 	hash = ACL_HASH(mac);
    174   1.1    dyoung 	LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
    175   1.1    dyoung 		if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) {
    176   1.1    dyoung 			ACL_UNLOCK(as);
    177   1.8    cegger 			free(new, M_80211_ACL);
    178   1.1    dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
    179   1.1    dyoung 				"ACL: add %s failed, already present\n",
    180   1.1    dyoung 				ether_sprintf(mac));
    181   1.1    dyoung 			return EEXIST;
    182   1.1    dyoung 		}
    183   1.1    dyoung 	}
    184   1.1    dyoung 	IEEE80211_ADDR_COPY(new->acl_macaddr, mac);
    185   1.1    dyoung 	TAILQ_INSERT_TAIL(&as->as_list, new, acl_list);
    186   1.1    dyoung 	LIST_INSERT_HEAD(&as->as_hash[hash], new, acl_hash);
    187   1.4     skrll 	as->as_nacls++;
    188   1.1    dyoung 	ACL_UNLOCK(as);
    189   1.1    dyoung 
    190   1.1    dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
    191   1.1    dyoung 		"ACL: add %s\n", ether_sprintf(mac));
    192   1.1    dyoung 	return 0;
    193   1.1    dyoung }
    194   1.1    dyoung 
    195   1.1    dyoung static int
    196   1.1    dyoung acl_remove(struct ieee80211com *ic, const u_int8_t mac[IEEE80211_ADDR_LEN])
    197   1.1    dyoung {
    198   1.1    dyoung 	struct aclstate *as = ic->ic_as;
    199   1.1    dyoung 	struct acl *acl;
    200   1.1    dyoung 
    201   1.1    dyoung 	ACL_LOCK(as);
    202   1.1    dyoung 	acl = _find_acl(as, mac);
    203   1.1    dyoung 	if (acl != NULL)
    204   1.1    dyoung 		_acl_free(as, acl);
    205   1.1    dyoung 	ACL_UNLOCK(as);
    206   1.1    dyoung 
    207   1.1    dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
    208   1.1    dyoung 		"ACL: remove %s%s\n", ether_sprintf(mac),
    209   1.1    dyoung 		acl == NULL ? ", not present" : "");
    210   1.1    dyoung 
    211   1.1    dyoung 	return (acl == NULL ? ENOENT : 0);
    212   1.1    dyoung }
    213   1.1    dyoung 
    214   1.1    dyoung static int
    215   1.1    dyoung acl_free_all(struct ieee80211com *ic)
    216   1.1    dyoung {
    217   1.1    dyoung 	struct aclstate *as = ic->ic_as;
    218   1.1    dyoung 	struct acl *acl;
    219   1.1    dyoung 
    220   1.1    dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
    221   1.1    dyoung 
    222   1.1    dyoung 	ACL_LOCK(as);
    223   1.1    dyoung 	while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
    224   1.1    dyoung 		_acl_free(as, acl);
    225   1.1    dyoung 	ACL_UNLOCK(as);
    226   1.1    dyoung 
    227   1.1    dyoung 	return 0;
    228   1.1    dyoung }
    229   1.1    dyoung 
    230   1.1    dyoung static int
    231   1.1    dyoung acl_setpolicy(struct ieee80211com *ic, int policy)
    232   1.1    dyoung {
    233   1.1    dyoung 	struct aclstate *as = ic->ic_as;
    234   1.1    dyoung 
    235   1.1    dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_ACL,
    236   1.1    dyoung 		"ACL: set policy to %u\n", policy);
    237   1.1    dyoung 
    238   1.1    dyoung 	switch (policy) {
    239   1.1    dyoung 	case IEEE80211_MACCMD_POLICY_OPEN:
    240   1.1    dyoung 		as->as_policy = ACL_POLICY_OPEN;
    241   1.1    dyoung 		break;
    242   1.1    dyoung 	case IEEE80211_MACCMD_POLICY_ALLOW:
    243   1.1    dyoung 		as->as_policy = ACL_POLICY_ALLOW;
    244   1.1    dyoung 		break;
    245   1.1    dyoung 	case IEEE80211_MACCMD_POLICY_DENY:
    246   1.1    dyoung 		as->as_policy = ACL_POLICY_DENY;
    247   1.1    dyoung 		break;
    248   1.1    dyoung 	default:
    249   1.1    dyoung 		return EINVAL;
    250   1.1    dyoung 	}
    251   1.1    dyoung 	return 0;
    252   1.1    dyoung }
    253   1.1    dyoung 
    254   1.1    dyoung static int
    255   1.1    dyoung acl_getpolicy(struct ieee80211com *ic)
    256   1.1    dyoung {
    257   1.1    dyoung 	struct aclstate *as = ic->ic_as;
    258   1.1    dyoung 
    259   1.1    dyoung 	return as->as_policy;
    260   1.1    dyoung }
    261   1.1    dyoung 
    262   1.4     skrll static int
    263   1.7  christos acl_setioctl(struct ieee80211com *ic,
    264   1.7  christos     struct ieee80211req *ireq)
    265   1.4     skrll {
    266   1.4     skrll 
    267   1.4     skrll 	return EINVAL;
    268   1.4     skrll }
    269   1.4     skrll 
    270   1.4     skrll static int
    271   1.4     skrll acl_getioctl(struct ieee80211com *ic, struct ieee80211req *ireq)
    272   1.4     skrll {
    273   1.4     skrll 	struct aclstate *as = ic->ic_as;
    274   1.4     skrll 	struct acl *acl;
    275   1.4     skrll 	struct ieee80211req_maclist *ap;
    276   1.9  christos 	int error;
    277   1.9  christos 	uint32_t i, space;
    278   1.4     skrll 
    279   1.4     skrll 	switch (ireq->i_val) {
    280   1.4     skrll 	case IEEE80211_MACCMD_POLICY:
    281   1.4     skrll 		ireq->i_val = as->as_policy;
    282   1.4     skrll 		return 0;
    283   1.4     skrll 	case IEEE80211_MACCMD_LIST:
    284   1.4     skrll 		space = as->as_nacls * IEEE80211_ADDR_LEN;
    285   1.4     skrll 		if (ireq->i_len == 0) {
    286   1.4     skrll 			ireq->i_len = space;	/* return required space */
    287   1.4     skrll 			return 0;		/* NB: must not error */
    288   1.4     skrll 		}
    289  1.10       chs 		ap = malloc(space, M_TEMP, M_WAITOK);
    290   1.4     skrll 		i = 0;
    291   1.4     skrll 		ACL_LOCK(as);
    292   1.4     skrll 		TAILQ_FOREACH(acl, &as->as_list, acl_list) {
    293   1.4     skrll 			IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr);
    294   1.4     skrll 			i++;
    295   1.4     skrll 		}
    296   1.4     skrll 		ACL_UNLOCK(as);
    297   1.4     skrll 		if (ireq->i_len >= space) {
    298   1.4     skrll 			error = copyout(ap, ireq->i_data, space);
    299   1.4     skrll 			ireq->i_len = space;
    300   1.4     skrll 		} else
    301   1.4     skrll 			error = copyout(ap, ireq->i_data, ireq->i_len);
    302   1.8    cegger 		free(ap, M_TEMP);
    303   1.4     skrll 		return error;
    304   1.4     skrll 	}
    305   1.4     skrll 	return EINVAL;
    306   1.4     skrll }
    307   1.4     skrll 
    308   1.1    dyoung static const struct ieee80211_aclator mac = {
    309   1.1    dyoung 	.iac_name	= "mac",
    310   1.1    dyoung 	.iac_attach	= acl_attach,
    311   1.1    dyoung 	.iac_detach	= acl_detach,
    312   1.1    dyoung 	.iac_check	= acl_check,
    313   1.1    dyoung 	.iac_add	= acl_add,
    314   1.1    dyoung 	.iac_remove	= acl_remove,
    315   1.1    dyoung 	.iac_flush	= acl_free_all,
    316   1.1    dyoung 	.iac_setpolicy	= acl_setpolicy,
    317   1.1    dyoung 	.iac_getpolicy	= acl_getpolicy,
    318   1.4     skrll 	.iac_setioctl	= acl_setioctl,
    319   1.4     skrll 	.iac_getioctl	= acl_getioctl,
    320   1.1    dyoung };
    321