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.
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_acl.c,v 1.4 2005/08/13 17:31:48 sam Exp $");
37 __KERNEL_RCSID(0, "$NetBSD: ieee80211_acl.c,v 1.7 2006/11/16 01:33:40 christos Exp $");
41 * IEEE 802.11 MAC ACL support.
43 * When this module is loaded the sender address of each received
44 * frame is passed to the iac_check method and the module indicates
45 * if the frame should be accepted or rejected. If the policy is
46 * set to ACL_POLICY_OPEN then all frames are accepted w/o checking
47 * the address. Otherwise, the address is looked up in the database
48 * and if found the frame is either accepted (ACL_POLICY_ALLOW)
49 * or rejected (ACL_POLICY_DENT).
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
55 #include <sys/queue.h>
57 #include <sys/socket.h>
60 #include <net/if_media.h>
61 #include <net/if_ether.h>
62 #include <net/route.h>
64 #include <net80211/ieee80211_var.h>
67 ACL_POLICY_OPEN
= 0, /* open, don't check ACL's */
68 ACL_POLICY_ALLOW
= 1, /* allow traffic from MAC */
69 ACL_POLICY_DENY
= 2, /* deny traffic from MAC */
72 #define ACL_HASHSIZE 32
75 TAILQ_ENTRY(acl
) acl_list
;
76 LIST_ENTRY(acl
) acl_hash
;
77 u_int8_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 u_int8_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
= malloc(sizeof(struct aclstate
),
102 M_80211_ACL
, M_NOWAIT
| M_ZERO
);
105 ACL_LOCK_INIT(as
, "acl");
106 TAILQ_INIT(&as
->as_list
);
107 as
->as_policy
= ACL_POLICY_OPEN
;
114 acl_detach(struct ieee80211com
*ic
)
116 struct aclstate
*as
= ic
->ic_as
;
120 ACL_LOCK_DESTROY(as
);
124 static __inline
struct acl
*
125 _find_acl(struct aclstate
*as
, const u_int8_t
*macaddr
)
130 hash
= ACL_HASH(macaddr
);
131 LIST_FOREACH(acl
, &as
->as_hash
[hash
], acl_hash
) {
132 if (IEEE80211_ADDR_EQ(acl
->acl_macaddr
, macaddr
))
139 _acl_free(struct aclstate
*as
, struct acl
*acl
)
143 TAILQ_REMOVE(&as
->as_list
, acl
, acl_list
);
144 LIST_REMOVE(acl
, acl_hash
);
145 free(acl
, M_80211_ACL
);
150 acl_check(struct ieee80211com
*ic
, const u_int8_t mac
[IEEE80211_ADDR_LEN
])
152 struct aclstate
*as
= ic
->ic_as
;
154 switch (as
->as_policy
) {
155 case ACL_POLICY_OPEN
:
157 case ACL_POLICY_ALLOW
:
158 return _find_acl(as
, mac
) != NULL
;
159 case ACL_POLICY_DENY
:
160 return _find_acl(as
, mac
) == NULL
;
162 return 0; /* should not happen */
166 acl_add(struct ieee80211com
*ic
, const u_int8_t mac
[IEEE80211_ADDR_LEN
])
168 struct aclstate
*as
= ic
->ic_as
;
169 struct acl
*acl
, *new;
172 new = malloc(sizeof(struct acl
), M_80211_ACL
, M_NOWAIT
| M_ZERO
);
174 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
175 "ACL: add %s failed, no memory\n", ether_sprintf(mac
));
181 hash
= ACL_HASH(mac
);
182 LIST_FOREACH(acl
, &as
->as_hash
[hash
], acl_hash
) {
183 if (IEEE80211_ADDR_EQ(acl
->acl_macaddr
, mac
)) {
185 free(new, M_80211_ACL
);
186 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
187 "ACL: add %s failed, already present\n",
192 IEEE80211_ADDR_COPY(new->acl_macaddr
, mac
);
193 TAILQ_INSERT_TAIL(&as
->as_list
, new, acl_list
);
194 LIST_INSERT_HEAD(&as
->as_hash
[hash
], new, acl_hash
);
198 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
199 "ACL: add %s\n", ether_sprintf(mac
));
204 acl_remove(struct ieee80211com
*ic
, const u_int8_t mac
[IEEE80211_ADDR_LEN
])
206 struct aclstate
*as
= ic
->ic_as
;
210 acl
= _find_acl(as
, mac
);
215 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
216 "ACL: remove %s%s\n", ether_sprintf(mac
),
217 acl
== NULL
? ", not present" : "");
219 return (acl
== NULL
? ENOENT
: 0);
223 acl_free_all(struct ieee80211com
*ic
)
225 struct aclstate
*as
= ic
->ic_as
;
228 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
, "ACL: %s\n", "free all");
231 while ((acl
= TAILQ_FIRST(&as
->as_list
)) != NULL
)
239 acl_setpolicy(struct ieee80211com
*ic
, int policy
)
241 struct aclstate
*as
= ic
->ic_as
;
243 IEEE80211_DPRINTF(ic
, IEEE80211_MSG_ACL
,
244 "ACL: set policy to %u\n", policy
);
247 case IEEE80211_MACCMD_POLICY_OPEN
:
248 as
->as_policy
= ACL_POLICY_OPEN
;
250 case IEEE80211_MACCMD_POLICY_ALLOW
:
251 as
->as_policy
= ACL_POLICY_ALLOW
;
253 case IEEE80211_MACCMD_POLICY_DENY
:
254 as
->as_policy
= ACL_POLICY_DENY
;
263 acl_getpolicy(struct ieee80211com
*ic
)
265 struct aclstate
*as
= ic
->ic_as
;
267 return as
->as_policy
;
271 acl_setioctl(struct ieee80211com
*ic
,
272 struct ieee80211req
*ireq
)
279 acl_getioctl(struct ieee80211com
*ic
, struct ieee80211req
*ireq
)
281 struct aclstate
*as
= ic
->ic_as
;
283 struct ieee80211req_maclist
*ap
;
286 switch (ireq
->i_val
) {
287 case IEEE80211_MACCMD_POLICY
:
288 ireq
->i_val
= as
->as_policy
;
290 case IEEE80211_MACCMD_LIST
:
291 space
= as
->as_nacls
* IEEE80211_ADDR_LEN
;
292 if (ireq
->i_len
== 0) {
293 ireq
->i_len
= space
; /* return required space */
294 return 0; /* NB: must not error */
296 ap
= malloc(space
, M_TEMP
, M_NOWAIT
);
301 TAILQ_FOREACH(acl
, &as
->as_list
, acl_list
) {
302 IEEE80211_ADDR_COPY(ap
[i
].ml_macaddr
, acl
->acl_macaddr
);
306 if (ireq
->i_len
>= space
) {
307 error
= copyout(ap
, ireq
->i_data
, space
);
310 error
= copyout(ap
, ireq
->i_data
, ireq
->i_len
);
317 static const struct ieee80211_aclator mac
= {
319 .iac_attach
= acl_attach
,
320 .iac_detach
= acl_detach
,
321 .iac_check
= acl_check
,
323 .iac_remove
= acl_remove
,
324 .iac_flush
= acl_free_all
,
325 .iac_setpolicy
= acl_setpolicy
,
326 .iac_getpolicy
= acl_getpolicy
,
327 .iac_setioctl
= acl_setioctl
,
328 .iac_getioctl
= acl_getioctl
,