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