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