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