2 * hostapd / IEEE 802.11 authentication (ACL)
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * Access control list for IEEE 802.11 authentication can uses statically
9 * configured ACL from configuration files or an external RADIUS server.
10 * Results from external RADIUS queries are cached to allow faster
11 * authentication frame processing.
14 #include "utils/includes.h"
16 #include "utils/common.h"
17 #include "utils/eloop.h"
18 #include "crypto/sha1.h"
19 #include "radius/radius.h"
20 #include "radius/radius_client.h"
22 #include "ap_config.h"
23 #include "ap_drv_ops.h"
24 #include "ieee802_11.h"
25 #include "ieee802_1x.h"
26 #include "ieee802_11_auth.h"
28 #define RADIUS_ACL_TIMEOUT 30
31 struct hostapd_cached_radius_acl {
34 int accepted; /* HOSTAPD_ACL_* */
35 struct hostapd_cached_radius_acl *next;
37 u32 acct_interim_interval;
39 struct hostapd_sta_wpa_psk_short *psk;
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_entry(struct hostapd_cached_radius_acl *e)
59 os_free(e->radius_cui);
60 hostapd_free_psk_list(e->psk);
65 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
67 struct hostapd_cached_radius_acl *prev;
71 acl_cache = acl_cache->next;
72 hostapd_acl_cache_free_entry(prev);
77 static void copy_psk_list(struct hostapd_sta_wpa_psk_short **psk,
78 struct hostapd_sta_wpa_psk_short *src)
80 struct hostapd_sta_wpa_psk_short **copy_to;
81 struct hostapd_sta_wpa_psk_short *copy_from;
83 /* Copy PSK linked list */
86 while (copy_from && copy_to) {
87 *copy_to = os_zalloc(sizeof(struct hostapd_sta_wpa_psk_short));
90 os_memcpy(*copy_to, copy_from,
91 sizeof(struct hostapd_sta_wpa_psk_short));
92 copy_from = copy_from->next;
93 copy_to = &((*copy_to)->next);
100 static int hostapd_acl_cache_get(struct hostapd_data *hapd, const u8 *addr,
101 u32 *session_timeout,
102 u32 *acct_interim_interval, int *vlan_id,
103 struct hostapd_sta_wpa_psk_short **psk,
104 char **identity, char **radius_cui)
106 struct hostapd_cached_radius_acl *entry;
111 for (entry = hapd->acl_cache; entry; entry = entry->next) {
112 if (os_memcmp(entry->addr, addr, ETH_ALEN) != 0)
115 if (now.sec - entry->timestamp > RADIUS_ACL_TIMEOUT)
116 return -1; /* entry has expired */
117 if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
119 *session_timeout = entry->session_timeout;
120 if (acct_interim_interval)
121 *acct_interim_interval =
122 entry->acct_interim_interval;
124 *vlan_id = entry->vlan_id;
125 copy_psk_list(psk, entry->psk);
128 *identity = os_strdup(entry->identity);
133 if (entry->radius_cui)
134 *radius_cui = os_strdup(entry->radius_cui);
138 return entry->accepted;
143 #endif /* CONFIG_NO_RADIUS */
146 static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
150 os_free(query->auth_msg);
155 #ifndef CONFIG_NO_RADIUS
156 static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
157 struct hostapd_acl_query_data *query)
159 struct radius_msg *msg;
162 query->radius_id = radius_client_get_id(hapd->radius);
163 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
167 radius_msg_make_authenticator(msg, addr, ETH_ALEN);
169 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
170 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
172 wpa_printf(MSG_DEBUG, "Could not add User-Name");
176 if (!radius_msg_add_attr_user_password(
177 msg, (u8 *) buf, os_strlen(buf),
178 hapd->conf->radius->auth_server->shared_secret,
179 hapd->conf->radius->auth_server->shared_secret_len)) {
180 wpa_printf(MSG_DEBUG, "Could not add User-Password");
184 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr,
188 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
190 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
191 (u8 *) buf, os_strlen(buf))) {
192 wpa_printf(MSG_DEBUG, "Could not add Calling-Station-Id");
196 os_snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
197 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
198 (u8 *) buf, os_strlen(buf))) {
199 wpa_printf(MSG_DEBUG, "Could not add Connect-Info");
203 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr) < 0)
208 radius_msg_free(msg);
211 #endif /* CONFIG_NO_RADIUS */
215 * hostapd_allowed_address - Check whether a specified STA can be authenticated
216 * @hapd: hostapd BSS data
217 * @addr: MAC address of the STA
218 * @msg: Authentication message
219 * @len: Length of msg in octets
220 * @session_timeout: Buffer for returning session timeout (from RADIUS)
221 * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
222 * @vlan_id: Buffer for returning VLAN ID
223 * @psk: Linked list buffer for returning WPA PSK
224 * @identity: Buffer for returning identity (from RADIUS)
225 * @radius_cui: Buffer for returning CUI (from RADIUS)
226 * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
228 * The caller is responsible for freeing the returned *identity and *radius_cui
229 * values with os_free().
231 int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
232 const u8 *msg, size_t len, u32 *session_timeout,
233 u32 *acct_interim_interval, int *vlan_id,
234 struct hostapd_sta_wpa_psk_short **psk,
235 char **identity, char **radius_cui)
238 *session_timeout = 0;
239 if (acct_interim_interval)
240 *acct_interim_interval = 0;
250 if (hostapd_maclist_found(hapd->conf->accept_mac,
251 hapd->conf->num_accept_mac, addr, vlan_id))
252 return HOSTAPD_ACL_ACCEPT;
254 if (hostapd_maclist_found(hapd->conf->deny_mac,
255 hapd->conf->num_deny_mac, addr, vlan_id))
256 return HOSTAPD_ACL_REJECT;
258 if (hapd->conf->macaddr_acl == ACCEPT_UNLESS_DENIED)
259 return HOSTAPD_ACL_ACCEPT;
260 if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED)
261 return HOSTAPD_ACL_REJECT;
263 if (hapd->conf->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH) {
264 #ifdef CONFIG_NO_RADIUS
265 return HOSTAPD_ACL_REJECT;
266 #else /* CONFIG_NO_RADIUS */
267 struct hostapd_acl_query_data *query;
270 /* Check whether ACL cache has an entry for this station */
271 int res = hostapd_acl_cache_get(hapd, addr, session_timeout,
272 acct_interim_interval,
274 identity, radius_cui);
275 if (res == HOSTAPD_ACL_ACCEPT ||
276 res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
278 if (res == HOSTAPD_ACL_REJECT)
279 return HOSTAPD_ACL_REJECT;
281 query = hapd->acl_queries;
283 if (os_memcmp(query->addr, addr, ETH_ALEN) == 0) {
284 /* pending query in RADIUS retransmit queue;
285 * do not generate a new one */
291 os_free(*radius_cui);
294 return HOSTAPD_ACL_PENDING;
299 if (!hapd->conf->radius->auth_server)
300 return HOSTAPD_ACL_REJECT;
302 /* No entry in the cache - query external RADIUS server */
303 query = os_zalloc(sizeof(*query));
305 wpa_printf(MSG_ERROR, "malloc for query data failed");
306 return HOSTAPD_ACL_REJECT;
309 query->timestamp = t.sec;
310 os_memcpy(query->addr, addr, ETH_ALEN);
311 if (hostapd_radius_acl_query(hapd, addr, query)) {
312 wpa_printf(MSG_DEBUG, "Failed to send Access-Request "
314 hostapd_acl_query_free(query);
315 return HOSTAPD_ACL_REJECT;
318 query->auth_msg = os_malloc(len);
319 if (query->auth_msg == NULL) {
320 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
322 hostapd_acl_query_free(query);
323 return HOSTAPD_ACL_REJECT;
325 os_memcpy(query->auth_msg, msg, len);
326 query->auth_msg_len = len;
327 query->next = hapd->acl_queries;
328 hapd->acl_queries = query;
330 /* Queued data will be processed in hostapd_acl_recv_radius()
331 * when RADIUS server replies to the sent Access-Request. */
332 return HOSTAPD_ACL_PENDING;
333 #endif /* CONFIG_NO_RADIUS */
336 return HOSTAPD_ACL_REJECT;
340 #ifndef CONFIG_NO_RADIUS
341 static void hostapd_acl_expire_cache(struct hostapd_data *hapd, os_time_t now)
343 struct hostapd_cached_radius_acl *prev, *entry, *tmp;
346 entry = hapd->acl_cache;
349 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
350 wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
351 " has expired.", MAC2STR(entry->addr));
353 prev->next = entry->next;
355 hapd->acl_cache = entry->next;
356 hostapd_drv_set_radius_acl_expire(hapd, entry->addr);
359 hostapd_acl_cache_free_entry(tmp);
369 static void hostapd_acl_expire_queries(struct hostapd_data *hapd,
372 struct hostapd_acl_query_data *prev, *entry, *tmp;
375 entry = hapd->acl_queries;
378 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
379 wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
380 " has expired.", MAC2STR(entry->addr));
382 prev->next = entry->next;
384 hapd->acl_queries = entry->next;
388 hostapd_acl_query_free(tmp);
399 * hostapd_acl_expire - ACL cache expiration callback
400 * @eloop_ctx: struct hostapd_data *
401 * @timeout_ctx: Not used
403 static void hostapd_acl_expire(void *eloop_ctx, void *timeout_ctx)
405 struct hostapd_data *hapd = eloop_ctx;
409 hostapd_acl_expire_cache(hapd, now.sec);
410 hostapd_acl_expire_queries(hapd, now.sec);
412 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
416 static void decode_tunnel_passwords(struct hostapd_data *hapd,
417 const u8 *shared_secret,
418 size_t shared_secret_len,
419 struct radius_msg *msg,
420 struct radius_msg *req,
421 struct hostapd_cached_radius_acl *cache)
424 char *passphrase, *strpassphrase;
426 struct hostapd_sta_wpa_psk_short *psk;
429 * Decode all tunnel passwords as PSK and save them into a linked list.
432 passphrase = radius_msg_get_tunnel_password(
433 msg, &passphraselen, shared_secret, shared_secret_len,
436 * Passphrase is NULL iff there is no i-th Tunnel-Password
439 if (passphrase == NULL)
442 * passphrase does not contain the NULL termination.
443 * Add it here as pbkdf2_sha1() requires it.
445 strpassphrase = os_zalloc(passphraselen + 1);
446 psk = os_zalloc(sizeof(struct hostapd_sta_wpa_psk_short));
447 if (strpassphrase && psk) {
448 os_memcpy(strpassphrase, passphrase, passphraselen);
449 pbkdf2_sha1(strpassphrase,
450 hapd->conf->ssid.ssid,
451 hapd->conf->ssid.ssid_len, 4096,
453 psk->next = cache->psk;
457 os_free(strpassphrase);
465 * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
466 * @msg: RADIUS response message
467 * @req: RADIUS request message
468 * @shared_secret: RADIUS shared secret
469 * @shared_secret_len: Length of shared_secret in octets
470 * @data: Context data (struct hostapd_data *)
471 * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
472 * was processed here) or RADIUS_RX_UNKNOWN if not.
474 static RadiusRxResult
475 hostapd_acl_recv_radius(struct radius_msg *msg, struct radius_msg *req,
476 const u8 *shared_secret, size_t shared_secret_len,
479 struct hostapd_data *hapd = data;
480 struct hostapd_acl_query_data *query, *prev;
481 struct hostapd_cached_radius_acl *cache;
482 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
485 query = hapd->acl_queries;
488 if (query->radius_id == hdr->identifier)
494 return RADIUS_RX_UNKNOWN;
496 wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
497 "message (id=%d)", query->radius_id);
499 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
500 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
501 "correct authenticator - dropped\n");
502 return RADIUS_RX_INVALID_AUTHENTICATOR;
505 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
506 hdr->code != RADIUS_CODE_ACCESS_REJECT) {
507 wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
509 return RADIUS_RX_UNKNOWN;
512 /* Insert Accept/Reject info into ACL cache */
513 cache = os_zalloc(sizeof(*cache));
515 wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
519 cache->timestamp = t.sec;
520 os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
521 if (hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
525 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
526 &cache->session_timeout) == 0)
527 cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
529 cache->accepted = HOSTAPD_ACL_ACCEPT;
531 if (radius_msg_get_attr_int32(
532 msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
533 &cache->acct_interim_interval) == 0 &&
534 cache->acct_interim_interval < 60) {
535 wpa_printf(MSG_DEBUG, "Ignored too small "
536 "Acct-Interim-Interval %d for STA " MACSTR,
537 cache->acct_interim_interval,
538 MAC2STR(query->addr));
539 cache->acct_interim_interval = 0;
542 cache->vlan_id = radius_msg_get_vlanid(msg);
544 decode_tunnel_passwords(hapd, shared_secret, shared_secret_len,
547 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME,
548 &buf, &len, NULL) == 0) {
549 cache->identity = os_zalloc(len + 1);
551 os_memcpy(cache->identity, buf, len);
553 if (radius_msg_get_attr_ptr(
554 msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
555 &buf, &len, NULL) == 0) {
556 cache->radius_cui = os_zalloc(len + 1);
557 if (cache->radius_cui)
558 os_memcpy(cache->radius_cui, buf, len);
561 if (hapd->conf->wpa_psk_radius == PSK_RADIUS_REQUIRED &&
563 cache->accepted = HOSTAPD_ACL_REJECT;
565 cache->accepted = HOSTAPD_ACL_REJECT;
566 cache->next = hapd->acl_cache;
567 hapd->acl_cache = cache;
569 #ifdef CONFIG_DRIVER_RADIUS_ACL
570 hostapd_drv_set_radius_acl_auth(hapd, query->addr, cache->accepted,
571 cache->session_timeout);
572 #else /* CONFIG_DRIVER_RADIUS_ACL */
574 /* Re-send original authentication frame for 802.11 processing */
575 wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
576 "successful RADIUS ACL query");
577 ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len, NULL);
578 #endif /* NEED_AP_MLME */
579 #endif /* CONFIG_DRIVER_RADIUS_ACL */
583 hapd->acl_queries = query->next;
585 prev->next = query->next;
587 hostapd_acl_query_free(query);
589 return RADIUS_RX_PROCESSED;
591 #endif /* CONFIG_NO_RADIUS */
595 * hostapd_acl_init: Initialize IEEE 802.11 ACL
596 * @hapd: hostapd BSS data
597 * Returns: 0 on success, -1 on failure
599 int hostapd_acl_init(struct hostapd_data *hapd)
601 #ifndef CONFIG_NO_RADIUS
602 if (radius_client_register(hapd->radius, RADIUS_AUTH,
603 hostapd_acl_recv_radius, hapd))
606 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
607 #endif /* CONFIG_NO_RADIUS */
614 * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
615 * @hapd: hostapd BSS data
617 void hostapd_acl_deinit(struct hostapd_data *hapd)
619 struct hostapd_acl_query_data *query, *prev;
621 #ifndef CONFIG_NO_RADIUS
622 eloop_cancel_timeout(hostapd_acl_expire, hapd, NULL);
624 hostapd_acl_cache_free(hapd->acl_cache);
625 #endif /* CONFIG_NO_RADIUS */
627 query = hapd->acl_queries;
631 hostapd_acl_query_free(prev);
636 void hostapd_free_psk_list(struct hostapd_sta_wpa_psk_short *psk)
639 struct hostapd_sta_wpa_psk_short *prev = psk;