2 * hostapd / IEEE 802.11 authentication (ACL)
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * Access control list for IEEE 802.11 authentication can uses statically
15 * configured ACL from configuration files or an external RADIUS server.
16 * Results from external RADIUS queries are cached to allow faster
17 * authentication frame processing.
22 #ifndef CONFIG_NATIVE_WINDOWS
25 #include "ieee802_11.h"
26 #include "ieee802_11_auth.h"
27 #include "radius/radius.h"
28 #include "radius/radius_client.h"
31 #define RADIUS_ACL_TIMEOUT 30
34 struct hostapd_cached_radius_acl {
37 int accepted; /* HOSTAPD_ACL_* */
38 struct hostapd_cached_radius_acl *next;
40 u32 acct_interim_interval;
45 struct hostapd_acl_query_data {
49 u8 *auth_msg; /* IEEE 802.11 authentication frame from station */
51 struct hostapd_acl_query_data *next;
55 #ifndef CONFIG_NO_RADIUS
56 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
58 struct hostapd_cached_radius_acl *prev;
62 acl_cache = acl_cache->next;
68 static int hostapd_acl_cache_get(struct hostapd_data *hapd, const u8 *addr,
70 u32 *acct_interim_interval, int *vlan_id)
72 struct hostapd_cached_radius_acl *entry;
76 entry = hapd->acl_cache;
79 if (os_memcmp(entry->addr, addr, ETH_ALEN) == 0) {
80 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT)
81 return -1; /* entry has expired */
82 if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
85 entry->session_timeout;
86 if (acct_interim_interval)
87 *acct_interim_interval =
88 entry->acct_interim_interval;
90 *vlan_id = entry->vlan_id;
91 return entry->accepted;
99 #endif /* CONFIG_NO_RADIUS */
102 static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
106 os_free(query->auth_msg);
111 #ifndef CONFIG_NO_RADIUS
112 static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
113 struct hostapd_acl_query_data *query)
115 struct radius_msg *msg;
118 query->radius_id = radius_client_get_id(hapd->radius);
119 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
123 radius_msg_make_authenticator(msg, addr, ETH_ALEN);
125 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
126 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
128 wpa_printf(MSG_DEBUG, "Could not add User-Name");
132 if (!radius_msg_add_attr_user_password(
133 msg, (u8 *) buf, os_strlen(buf),
134 hapd->conf->radius->auth_server->shared_secret,
135 hapd->conf->radius->auth_server->shared_secret_len)) {
136 wpa_printf(MSG_DEBUG, "Could not add User-Password");
140 if (hapd->conf->own_ip_addr.af == AF_INET &&
141 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
142 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
143 wpa_printf(MSG_DEBUG, "Could not add NAS-IP-Address");
148 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
149 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
150 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
151 wpa_printf(MSG_DEBUG, "Could not add NAS-IPv6-Address");
154 #endif /* CONFIG_IPV6 */
156 if (hapd->conf->nas_identifier &&
157 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
158 (u8 *) hapd->conf->nas_identifier,
159 os_strlen(hapd->conf->nas_identifier))) {
160 wpa_printf(MSG_DEBUG, "Could not add NAS-Identifier");
164 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
165 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
166 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
167 (u8 *) buf, os_strlen(buf))) {
168 wpa_printf(MSG_DEBUG, "Could not add Called-Station-Id");
172 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
174 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
175 (u8 *) buf, os_strlen(buf))) {
176 wpa_printf(MSG_DEBUG, "Could not add Calling-Station-Id");
180 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
181 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
182 wpa_printf(MSG_DEBUG, "Could not add NAS-Port-Type");
186 os_snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
187 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
188 (u8 *) buf, os_strlen(buf))) {
189 wpa_printf(MSG_DEBUG, "Could not add Connect-Info");
193 radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr);
197 radius_msg_free(msg);
201 #endif /* CONFIG_NO_RADIUS */
205 * hostapd_allowed_address - Check whether a specified STA can be authenticated
206 * @hapd: hostapd BSS data
207 * @addr: MAC address of the STA
208 * @msg: Authentication message
209 * @len: Length of msg in octets
210 * @session_timeout: Buffer for returning session timeout (from RADIUS)
211 * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
212 * @vlan_id: Buffer for returning VLAN ID
213 * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
215 int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
216 const u8 *msg, size_t len, u32 *session_timeout,
217 u32 *acct_interim_interval, int *vlan_id)
220 *session_timeout = 0;
221 if (acct_interim_interval)
222 *acct_interim_interval = 0;
226 if (hostapd_maclist_found(hapd->conf->accept_mac,
227 hapd->conf->num_accept_mac, addr, vlan_id))
228 return HOSTAPD_ACL_ACCEPT;
230 if (hostapd_maclist_found(hapd->conf->deny_mac,
231 hapd->conf->num_deny_mac, addr, vlan_id))
232 return HOSTAPD_ACL_REJECT;
234 if (hapd->conf->macaddr_acl == ACCEPT_UNLESS_DENIED)
235 return HOSTAPD_ACL_ACCEPT;
236 if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED)
237 return HOSTAPD_ACL_REJECT;
239 if (hapd->conf->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH) {
240 #ifdef CONFIG_NO_RADIUS
241 return HOSTAPD_ACL_REJECT;
242 #else /* CONFIG_NO_RADIUS */
243 struct hostapd_acl_query_data *query;
245 /* Check whether ACL cache has an entry for this station */
246 int res = hostapd_acl_cache_get(hapd, addr, session_timeout,
247 acct_interim_interval,
249 if (res == HOSTAPD_ACL_ACCEPT ||
250 res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
252 if (res == HOSTAPD_ACL_REJECT)
253 return HOSTAPD_ACL_REJECT;
255 query = hapd->acl_queries;
257 if (os_memcmp(query->addr, addr, ETH_ALEN) == 0) {
258 /* pending query in RADIUS retransmit queue;
259 * do not generate a new one */
260 return HOSTAPD_ACL_PENDING;
265 if (!hapd->conf->radius->auth_server)
266 return HOSTAPD_ACL_REJECT;
268 /* No entry in the cache - query external RADIUS server */
269 query = os_zalloc(sizeof(*query));
271 wpa_printf(MSG_ERROR, "malloc for query data failed");
272 return HOSTAPD_ACL_REJECT;
274 time(&query->timestamp);
275 os_memcpy(query->addr, addr, ETH_ALEN);
276 if (hostapd_radius_acl_query(hapd, addr, query)) {
277 wpa_printf(MSG_DEBUG, "Failed to send Access-Request "
279 hostapd_acl_query_free(query);
280 return HOSTAPD_ACL_REJECT;
283 query->auth_msg = os_malloc(len);
284 if (query->auth_msg == NULL) {
285 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
287 hostapd_acl_query_free(query);
288 return HOSTAPD_ACL_REJECT;
290 os_memcpy(query->auth_msg, msg, len);
291 query->auth_msg_len = len;
292 query->next = hapd->acl_queries;
293 hapd->acl_queries = query;
295 /* Queued data will be processed in hostapd_acl_recv_radius()
296 * when RADIUS server replies to the sent Access-Request. */
297 return HOSTAPD_ACL_PENDING;
298 #endif /* CONFIG_NO_RADIUS */
301 return HOSTAPD_ACL_REJECT;
305 #ifndef CONFIG_NO_RADIUS
306 static void hostapd_acl_expire_cache(struct hostapd_data *hapd, time_t now)
308 struct hostapd_cached_radius_acl *prev, *entry, *tmp;
311 entry = hapd->acl_cache;
314 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
315 wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
316 " has expired.", MAC2STR(entry->addr));
318 prev->next = entry->next;
320 hapd->acl_cache = entry->next;
321 #ifdef CONFIG_DRIVER_RADIUS_ACL
322 hostapd_set_radius_acl_expire(hapd, entry->addr);
323 #endif /* CONFIG_DRIVER_RADIUS_ACL */
336 static void hostapd_acl_expire_queries(struct hostapd_data *hapd, time_t now)
338 struct hostapd_acl_query_data *prev, *entry, *tmp;
341 entry = hapd->acl_queries;
344 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
345 wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
346 " has expired.", MAC2STR(entry->addr));
348 prev->next = entry->next;
350 hapd->acl_queries = entry->next;
354 hostapd_acl_query_free(tmp);
365 * hostapd_acl_expire - ACL cache expiration callback
366 * @eloop_ctx: struct hostapd_data *
367 * @timeout_ctx: Not used
369 static void hostapd_acl_expire(void *eloop_ctx, void *timeout_ctx)
371 struct hostapd_data *hapd = eloop_ctx;
375 hostapd_acl_expire_cache(hapd, now);
376 hostapd_acl_expire_queries(hapd, now);
378 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
383 * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
384 * @msg: RADIUS response message
385 * @req: RADIUS request message
386 * @shared_secret: RADIUS shared secret
387 * @shared_secret_len: Length of shared_secret in octets
388 * @data: Context data (struct hostapd_data *)
389 * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
390 * was processed here) or RADIUS_RX_UNKNOWN if not.
392 static RadiusRxResult
393 hostapd_acl_recv_radius(struct radius_msg *msg, struct radius_msg *req,
394 const u8 *shared_secret, size_t shared_secret_len,
397 struct hostapd_data *hapd = data;
398 struct hostapd_acl_query_data *query, *prev;
399 struct hostapd_cached_radius_acl *cache;
401 query = hapd->acl_queries;
404 if (query->radius_id == msg->hdr->identifier)
410 return RADIUS_RX_UNKNOWN;
412 wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
413 "message (id=%d)", query->radius_id);
415 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
416 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
417 "correct authenticator - dropped\n");
418 return RADIUS_RX_INVALID_AUTHENTICATOR;
421 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
422 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT) {
423 wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
424 "query", msg->hdr->code);
425 return RADIUS_RX_UNKNOWN;
428 /* Insert Accept/Reject info into ACL cache */
429 cache = os_zalloc(sizeof(*cache));
431 wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
434 time(&cache->timestamp);
435 os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
436 if (msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
437 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
438 &cache->session_timeout) == 0)
439 cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
441 cache->accepted = HOSTAPD_ACL_ACCEPT;
443 if (radius_msg_get_attr_int32(
444 msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
445 &cache->acct_interim_interval) == 0 &&
446 cache->acct_interim_interval < 60) {
447 wpa_printf(MSG_DEBUG, "Ignored too small "
448 "Acct-Interim-Interval %d for STA " MACSTR,
449 cache->acct_interim_interval,
450 MAC2STR(query->addr));
451 cache->acct_interim_interval = 0;
454 cache->vlan_id = radius_msg_get_vlanid(msg);
456 cache->accepted = HOSTAPD_ACL_REJECT;
457 cache->next = hapd->acl_cache;
458 hapd->acl_cache = cache;
460 #ifdef CONFIG_DRIVER_RADIUS_ACL
461 hostapd_set_radius_acl_auth(hapd, query->addr, cache->accepted,
462 cache->session_timeout);
463 #else /* CONFIG_DRIVER_RADIUS_ACL */
465 /* Re-send original authentication frame for 802.11 processing */
466 wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
467 "successful RADIUS ACL query");
468 ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len,
469 WLAN_FC_STYPE_AUTH, NULL);
470 #endif /* NEED_MLME */
471 #endif /* CONFIG_DRIVER_RADIUS_ACL */
475 hapd->acl_queries = query->next;
477 prev->next = query->next;
479 hostapd_acl_query_free(query);
481 return RADIUS_RX_PROCESSED;
483 #endif /* CONFIG_NO_RADIUS */
487 * hostapd_acl_init: Initialize IEEE 802.11 ACL
488 * @hapd: hostapd BSS data
489 * Returns: 0 on success, -1 on failure
491 int hostapd_acl_init(struct hostapd_data *hapd)
493 #ifndef CONFIG_NO_RADIUS
494 if (radius_client_register(hapd->radius, RADIUS_AUTH,
495 hostapd_acl_recv_radius, hapd))
498 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
499 #endif /* CONFIG_NO_RADIUS */
506 * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
507 * @hapd: hostapd BSS data
509 void hostapd_acl_deinit(struct hostapd_data *hapd)
511 struct hostapd_acl_query_data *query, *prev;
513 #ifndef CONFIG_NO_RADIUS
514 eloop_cancel_timeout(hostapd_acl_expire, hapd, NULL);
516 hostapd_acl_cache_free(hapd->acl_cache);
517 #endif /* CONFIG_NO_RADIUS */
519 query = hapd->acl_queries;
523 hostapd_acl_query_free(prev);
528 int hostapd_acl_reconfig(struct hostapd_data *hapd,
529 struct hostapd_config *oldconf)
531 if (!hapd->radius_client_reconfigured)
534 hostapd_acl_deinit(hapd);
535 return hostapd_acl_init(hapd);
538 #endif /* CONFIG_NATIVE_WINDOWS */