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