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