2 * Copyright (c) 2004-2005 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") version 2 as published by the Free
18 * Software Foundation.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $FreeBSD: src/sys/net80211/ieee80211_acl.c,v 1.3.2.1 2005/09/03 22:40:02 sam Exp $
32 * $DragonFly: src/sys/netproto/802_11/wlan_acl/ieee80211_acl.c,v 1.3 2006/12/22 23:57:53 swildner Exp $
36 * IEEE 802.11 MAC ACL support.
38 * When this module is loaded the sender address of each received
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).
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
50 #include <sys/module.h>
51 #include <sys/queue.h>
52 #include <sys/serialize.h>
54 #include <sys/socket.h>
57 #include <net/if_arp.h>
58 #include <net/if_media.h>
59 #include <net/ethernet.h>
60 #include <net/route.h>
62 #include <netproto/802_11/ieee80211_var.h>
65 ACL_POLICY_OPEN
= 0, /* open, don't check ACL's */
66 ACL_POLICY_ALLOW
= 1, /* allow traffic from MAC */
67 ACL_POLICY_DENY
= 2, /* deny traffic from MAC */
70 #define ACL_HASHSIZE 32
73 TAILQ_ENTRY(acl
) acl_list
;
74 LIST_ENTRY(acl
) acl_hash
;
75 uint8_t acl_macaddr
[IEEE80211_ADDR_LEN
];
83 TAILQ_HEAD(, acl
) as_list
; /* list of all ACL's */
84 LIST_HEAD(, acl
) as_hash
[ACL_HASHSIZE
];
85 struct ieee80211com
*as_ic
;
88 /* simple hash is enough for variation of macaddr */
89 #define ACL_HASH(addr) \
90 (((const uint8_t *)(addr))[IEEE80211_ADDR_LEN - 1] % ACL_HASHSIZE)
92 MALLOC_DEFINE(M_80211_ACL
, "acl", "802.11 station acl");
94 static int acl_free_all(struct ieee80211com
*);
97 acl_attach(struct ieee80211com
*ic
)
101 as
= kmalloc(sizeof(struct aclstate
), M_80211_ACL
, M_NOWAIT
| M_ZERO
);
104 TAILQ_INIT(&as
->as_list
);
105 as
->as_policy
= ACL_POLICY_OPEN
;
112 acl_detach(struct ieee80211com
*ic
)
114 struct aclstate
*as
= ic
->ic_as
;
121 static __inline
struct acl
*
122 _find_acl(struct aclstate
*as
, const uint8_t *macaddr
)
127 hash
= ACL_HASH(macaddr
);
128 LIST_FOREACH(acl
, &as
->as_hash
[hash
], acl_hash
) {
129 if (IEEE80211_ADDR_EQ(acl
->acl_macaddr
, macaddr
))
136 _acl_free(struct aclstate
*as
, struct acl
*acl
)
138 ASSERT_SERIALIZED(as
->as_ic
->ic_ifp
->if_serializer
);
140 TAILQ_REMOVE(&as
->as_list
, acl
, acl_list
);
141 LIST_REMOVE(acl
, acl_hash
);
142 kfree(acl
, M_80211_ACL
);
147 acl_check(struct ieee80211com
*ic
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
149 struct aclstate
*as
= ic
->ic_as
;
151 switch (as
->as_policy
) {
152 case ACL_POLICY_OPEN
:
154 case ACL_POLICY_ALLOW
:
155 return _find_acl(as
, mac
) != NULL
;
156 case ACL_POLICY_DENY
:
157 return _find_acl(as
, mac
) == NULL
;
159 return 0; /* should not happen */
163 acl_add(struct ieee80211com
*ic
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
165 struct aclstate
*as
= ic
->ic_as
;
166 struct acl
*acl
, *new;
169 ASSERT_SERIALIZED(ic
->ic_ifp
->if_serializer
);
171 new = kmalloc(sizeof(struct acl
), M_80211_ACL
, M_NOWAIT
| M_ZERO
);
173 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
174 "ACL: add %6D failed, no memory\n", mac
, ":");
179 hash
= ACL_HASH(mac
);
180 LIST_FOREACH(acl
, &as
->as_hash
[hash
], acl_hash
) {
181 if (IEEE80211_ADDR_EQ(acl
->acl_macaddr
, mac
)) {
182 FREE(new, M_80211_ACL
);
183 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
184 "ACL: add %6D failed, already present\n",
189 IEEE80211_ADDR_COPY(new->acl_macaddr
, mac
);
190 TAILQ_INSERT_TAIL(&as
->as_list
, new, acl_list
);
191 LIST_INSERT_HEAD(&as
->as_hash
[hash
], new, acl_hash
);
194 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
195 "ACL: add %6D\n", mac
, ":");
200 acl_remove(struct ieee80211com
*ic
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
202 struct aclstate
*as
= ic
->ic_as
;
205 ASSERT_SERIALIZED(ic
->ic_ifp
->if_serializer
);
207 acl
= _find_acl(as
, mac
);
211 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
212 "ACL: remove %6D%s\n", mac
, ":",
213 acl
== NULL
? ", not present" : "");
215 return (acl
== NULL
? ENOENT
: 0);
219 acl_free_all(struct ieee80211com
*ic
)
221 struct aclstate
*as
= ic
->ic_as
;
224 ASSERT_SERIALIZED(ic
->ic_ifp
->if_serializer
);
226 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
, "ACL: %s\n", "free all");
228 while ((acl
= TAILQ_FIRST(&as
->as_list
)) != NULL
)
235 acl_setpolicy(struct ieee80211com
*ic
, int policy
)
237 struct aclstate
*as
= ic
->ic_as
;
239 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
240 "ACL: set policy to %u\n", policy
);
243 case IEEE80211_MACCMD_POLICY_OPEN
:
244 as
->as_policy
= ACL_POLICY_OPEN
;
246 case IEEE80211_MACCMD_POLICY_ALLOW
:
247 as
->as_policy
= ACL_POLICY_ALLOW
;
249 case IEEE80211_MACCMD_POLICY_DENY
:
250 as
->as_policy
= ACL_POLICY_DENY
;
259 acl_getpolicy(struct ieee80211com
*ic
)
261 return ((struct aclstate
*)(ic
->ic_as
))->as_policy
;
265 acl_setioctl(struct ieee80211com
*ic
, struct ieee80211req
*ireq
)
271 acl_getioctl(struct ieee80211com
*ic
, struct ieee80211req
*ireq
)
273 struct aclstate
*as
= ic
->ic_as
;
275 struct ieee80211req_maclist
*ap
;
278 ASSERT_SERIALIZED(ic
->ic_ifp
->if_serializer
);
280 switch (ireq
->i_val
) {
281 case IEEE80211_MACCMD_POLICY
:
282 ireq
->i_val
= as
->as_policy
;
284 case IEEE80211_MACCMD_LIST
:
285 space
= as
->as_nacls
* IEEE80211_ADDR_LEN
;
286 if (ireq
->i_len
== 0) {
287 ireq
->i_len
= space
; /* return required space */
288 return 0; /* NB: must not error */
290 ap
= kmalloc(space
, M_TEMP
, M_NOWAIT
);
294 TAILQ_FOREACH(acl
, &as
->as_list
, acl_list
) {
295 IEEE80211_ADDR_COPY(ap
[i
].ml_macaddr
, acl
->acl_macaddr
);
298 if (ireq
->i_len
>= space
) {
299 error
= copyout(ap
, ireq
->i_data
, space
);
302 error
= copyout(ap
, ireq
->i_data
, ireq
->i_len
);
309 static const struct ieee80211_aclator mac
= {
311 .iac_attach
= acl_attach
,
312 .iac_detach
= acl_detach
,
313 .iac_check
= acl_check
,
315 .iac_remove
= acl_remove
,
316 .iac_flush
= acl_free_all
,
317 .iac_setpolicy
= acl_setpolicy
,
318 .iac_getpolicy
= acl_getpolicy
,
319 .iac_setioctl
= acl_setioctl
,
320 .iac_getioctl
= acl_getioctl
,
327 wlan_acl_modevent(module_t mod
, int type
, void *unused
)
332 kprintf("wlan: <802.11 MAC ACL support>\n");
333 ieee80211_aclator_register(&mac
);
336 ieee80211_aclator_unregister(&mac
);
342 static moduledata_t wlan_acl_mod
= {
347 DECLARE_MODULE(wlan_acl
, wlan_acl_mod
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
);
348 MODULE_VERSION(wlan_acl
, 1);
349 MODULE_DEPEND(wlan_acl
, wlan
, 1, 1, 1);