ieee80211_acl.c revision 1.9.54.3 1 /* $NetBSD: ieee80211_acl.c,v 1.9.54.3 2018/07/16 20:11:11 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 #if __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 #if __FreeBSD__
61 #include <net/ethernet.h>
62 #elif __NetBSD__
63 #include <net/if_ether.h>
64 #endif
65 #include <net/route.h>
66
67 #include <net80211/ieee80211_var.h>
68
69 #ifdef __NetBSD__
70 #undef KASSERT
71 #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
72 #endif
73
74 enum {
75 ACL_POLICY_OPEN = 0, /* open, don't check ACL's */
76 ACL_POLICY_ALLOW = 1, /* allow traffic from MAC */
77 ACL_POLICY_DENY = 2, /* deny traffic from MAC */
78 /*
79 * NB: ACL_POLICY_RADIUS must be the same value as
80 * IEEE80211_MACCMD_POLICY_RADIUS because of the way
81 * acl_getpolicy() works.
82 */
83 ACL_POLICY_RADIUS = 7, /* defer to RADIUS ACL server */
84 };
85
86 #define ACL_HASHSIZE 32
87
88 struct acl {
89 TAILQ_ENTRY(acl) acl_list;
90 LIST_ENTRY(acl) acl_hash;
91 uint8_t acl_macaddr[IEEE80211_ADDR_LEN];
92 };
93 struct aclstate {
94 acl_lock_t as_lock;
95 int as_policy;
96 uint32_t as_nacls;
97 TAILQ_HEAD(, acl) as_list; /* list of all ACL's */
98 LIST_HEAD(, acl) as_hash[ACL_HASHSIZE];
99 struct ieee80211vap *as_vap;
100 };
101
102 /* simple hash is enough for variation of macaddr */
103 #define ACL_HASH(addr) \
104 (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
105
106 #if __FreeBSD__
107 static MALLOC_DEFINE(M_80211_ACL, "acl", "802.11 station acl");
108 #endif
109
110 static int acl_free_all(struct ieee80211vap *);
111
112 /* number of references from net80211 layer */
113 static int nrefs = 0;
114
115 static int
116 acl_attach(struct ieee80211vap *vap)
117 {
118 struct aclstate *as;
119
120 as = (struct aclstate *) IEEE80211_MALLOC(sizeof(struct aclstate),
121 M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
122 if (as == NULL)
123 return 0;
124 ACL_LOCK_INIT(as, "acl");
125 TAILQ_INIT(&as->as_list);
126 as->as_policy = ACL_POLICY_OPEN;
127 as->as_vap = vap;
128 vap->iv_as = as;
129 nrefs++; /* NB: we assume caller locking */
130 return 1;
131 }
132
133 static void
134 acl_detach(struct ieee80211vap *vap)
135 {
136 struct aclstate *as = vap->iv_as;
137
138 KASSERT(nrefs > 0, ("imbalanced attach/detach"));
139 nrefs--; /* NB: we assume caller locking */
140
141 acl_free_all(vap);
142 vap->iv_as = NULL;
143 ACL_LOCK_DESTROY(as);
144 IEEE80211_FREE(as, M_80211_ACL);
145 }
146
147 static __inline struct acl *
148 _find_acl(struct aclstate *as, const uint8_t *macaddr)
149 {
150 struct acl *acl;
151 int hash;
152
153 hash = ACL_HASH(macaddr);
154 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
155 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, macaddr))
156 return acl;
157 }
158 return NULL;
159 }
160
161 static void
162 _acl_free(struct aclstate *as, struct acl *acl)
163 {
164 ACL_LOCK_ASSERT(as);
165
166 TAILQ_REMOVE(&as->as_list, acl, acl_list);
167 LIST_REMOVE(acl, acl_hash);
168 IEEE80211_FREE(acl, M_80211_ACL);
169 as->as_nacls--;
170 }
171
172 static int
173 acl_check(struct ieee80211vap *vap, const struct ieee80211_frame *wh)
174 {
175 struct aclstate *as = vap->iv_as;
176
177 switch (as->as_policy) {
178 case ACL_POLICY_OPEN:
179 case ACL_POLICY_RADIUS:
180 return 1;
181 case ACL_POLICY_ALLOW:
182 return _find_acl(as, wh->i_addr2) != NULL;
183 case ACL_POLICY_DENY:
184 return _find_acl(as, wh->i_addr2) == NULL;
185 }
186 return 0; /* should not happen */
187 }
188
189 static int
190 acl_add(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
191 {
192 struct aclstate *as = vap->iv_as;
193 struct acl *acl, *new;
194 int hash;
195
196 new = (struct acl *) IEEE80211_MALLOC(sizeof(struct acl),
197 M_80211_ACL, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
198 if (new == NULL) {
199 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
200 "ACL: add %s failed, no memory\n", ether_sprintf(mac));
201 /* XXX statistic */
202 return ENOMEM;
203 }
204
205 ACL_LOCK(as);
206 hash = ACL_HASH(mac);
207 LIST_FOREACH(acl, &as->as_hash[hash], acl_hash) {
208 if (IEEE80211_ADDR_EQ(acl->acl_macaddr, mac)) {
209 ACL_UNLOCK(as);
210 IEEE80211_FREE(new, M_80211_ACL);
211 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
212 "ACL: add %s failed, already present\n",
213 ether_sprintf(mac));
214 return EEXIST;
215 }
216 }
217 IEEE80211_ADDR_COPY(new->acl_macaddr, mac);
218 TAILQ_INSERT_TAIL(&as->as_list, new, acl_list);
219 LIST_INSERT_HEAD(&as->as_hash[hash], new, acl_hash);
220 as->as_nacls++;
221 ACL_UNLOCK(as);
222
223 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
224 "ACL: add %s\n", ether_sprintf(mac));
225 return 0;
226 }
227
228 static int
229 acl_remove(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
230 {
231 struct aclstate *as = vap->iv_as;
232 struct acl *acl;
233
234 ACL_LOCK(as);
235 acl = _find_acl(as, mac);
236 if (acl != NULL)
237 _acl_free(as, acl);
238 ACL_UNLOCK(as);
239
240 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
241 "ACL: remove %s%s\n", ether_sprintf(mac),
242 acl == NULL ? ", not present" : "");
243
244 return (acl == NULL ? ENOENT : 0);
245 }
246
247 static int
248 acl_free_all(struct ieee80211vap *vap)
249 {
250 struct aclstate *as = vap->iv_as;
251 struct acl *acl;
252
253 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL, "ACL: %s\n", "free all");
254
255 ACL_LOCK(as);
256 while ((acl = TAILQ_FIRST(&as->as_list)) != NULL)
257 _acl_free(as, acl);
258 ACL_UNLOCK(as);
259
260 return 0;
261 }
262
263 static int
264 acl_setpolicy(struct ieee80211vap *vap, int policy)
265 {
266 struct aclstate *as = vap->iv_as;
267
268 IEEE80211_DPRINTF(vap, IEEE80211_MSG_ACL,
269 "ACL: set policy to %u\n", policy);
270
271 switch (policy) {
272 case IEEE80211_MACCMD_POLICY_OPEN:
273 as->as_policy = ACL_POLICY_OPEN;
274 break;
275 case IEEE80211_MACCMD_POLICY_ALLOW:
276 as->as_policy = ACL_POLICY_ALLOW;
277 break;
278 case IEEE80211_MACCMD_POLICY_DENY:
279 as->as_policy = ACL_POLICY_DENY;
280 break;
281 case IEEE80211_MACCMD_POLICY_RADIUS:
282 as->as_policy = ACL_POLICY_RADIUS;
283 break;
284 default:
285 return EINVAL;
286 }
287 return 0;
288 }
289
290 static int
291 acl_getpolicy(struct ieee80211vap *vap)
292 {
293 struct aclstate *as = vap->iv_as;
294
295 return as->as_policy;
296 }
297
298 static int
299 acl_setioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
300 {
301
302 return EINVAL;
303 }
304
305 static int
306 acl_getioctl(struct ieee80211vap *vap, struct ieee80211req *ireq)
307 {
308 struct aclstate *as = vap->iv_as;
309 struct acl *acl;
310 struct ieee80211req_maclist *ap;
311 int error;
312 uint32_t i, space;
313
314 switch (ireq->i_val) {
315 case IEEE80211_MACCMD_POLICY:
316 ireq->i_val = as->as_policy;
317 return 0;
318 case IEEE80211_MACCMD_LIST:
319 space = as->as_nacls * IEEE80211_ADDR_LEN;
320 if (ireq->i_len == 0) {
321 ireq->i_len = space; /* return required space */
322 return 0; /* NB: must not error */
323 }
324 ap = (struct ieee80211req_maclist *) IEEE80211_MALLOC(space,
325 M_TEMP, IEEE80211_M_NOWAIT);
326 if (ap == NULL)
327 return ENOMEM;
328 i = 0;
329 ACL_LOCK(as);
330 TAILQ_FOREACH(acl, &as->as_list, acl_list) {
331 IEEE80211_ADDR_COPY(ap[i].ml_macaddr, acl->acl_macaddr);
332 i++;
333 }
334 ACL_UNLOCK(as);
335 if (ireq->i_len >= space) {
336 error = copyout(ap, ireq->i_data, space);
337 ireq->i_len = space;
338 } else
339 error = copyout(ap, ireq->i_data, ireq->i_len);
340 IEEE80211_FREE(ap, M_TEMP);
341 return error;
342 }
343 return EINVAL;
344 }
345
346 static const struct ieee80211_aclator mac = {
347 .iac_name = "mac",
348 .iac_attach = acl_attach,
349 .iac_detach = acl_detach,
350 .iac_check = acl_check,
351 .iac_add = acl_add,
352 .iac_remove = acl_remove,
353 .iac_flush = acl_free_all,
354 .iac_setpolicy = acl_setpolicy,
355 .iac_getpolicy = acl_getpolicy,
356 .iac_setioctl = acl_setioctl,
357 .iac_getioctl = acl_getioctl,
358 };
359 IEEE80211_ACL_MODULE(wlan_acl, mac, 1);
360