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 {
32 struct os_reltime timestamp;
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 {
46 struct os_reltime timestamp;
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;
107 struct os_reltime now;
109 os_get_reltime(&now);
111 for (entry = hapd->acl_cache; entry; entry = entry->next) {
112 if (os_memcmp(entry->addr, addr, ETH_ALEN) != 0)
115 if (os_reltime_expired(&now, &entry->timestamp,
117 return -1; /* entry has expired */
118 if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
120 *session_timeout = entry->session_timeout;
121 if (acct_interim_interval)
122 *acct_interim_interval =
123 entry->acct_interim_interval;
125 *vlan_id = entry->vlan_id;
126 copy_psk_list(psk, entry->psk);
129 *identity = os_strdup(entry->identity);
134 if (entry->radius_cui)
135 *radius_cui = os_strdup(entry->radius_cui);
139 return entry->accepted;
144 #endif /* CONFIG_NO_RADIUS */
147 static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
151 os_free(query->auth_msg);
156 #ifndef CONFIG_NO_RADIUS
157 static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
158 struct hostapd_acl_query_data *query)
160 struct radius_msg *msg;
163 query->radius_id = radius_client_get_id(hapd->radius);
164 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
168 radius_msg_make_authenticator(msg, addr, ETH_ALEN);
170 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
171 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
173 wpa_printf(MSG_DEBUG, "Could not add User-Name");
177 if (!radius_msg_add_attr_user_password(
178 msg, (u8 *) buf, os_strlen(buf),
179 hapd->conf->radius->auth_server->shared_secret,
180 hapd->conf->radius->auth_server->shared_secret_len)) {
181 wpa_printf(MSG_DEBUG, "Could not add User-Password");
185 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr,
189 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
191 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
192 (u8 *) buf, os_strlen(buf))) {
193 wpa_printf(MSG_DEBUG, "Could not add Calling-Station-Id");
197 os_snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
198 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
199 (u8 *) buf, os_strlen(buf))) {
200 wpa_printf(MSG_DEBUG, "Could not add Connect-Info");
204 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr) < 0)
209 radius_msg_free(msg);
212 #endif /* CONFIG_NO_RADIUS */
216 * hostapd_allowed_address - Check whether a specified STA can be authenticated
217 * @hapd: hostapd BSS data
218 * @addr: MAC address of the STA
219 * @msg: Authentication message
220 * @len: Length of msg in octets
221 * @session_timeout: Buffer for returning session timeout (from RADIUS)
222 * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
223 * @vlan_id: Buffer for returning VLAN ID
224 * @psk: Linked list buffer for returning WPA PSK
225 * @identity: Buffer for returning identity (from RADIUS)
226 * @radius_cui: Buffer for returning CUI (from RADIUS)
227 * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
229 * The caller is responsible for freeing the returned *identity and *radius_cui
230 * values with os_free().
232 int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
233 const u8 *msg, size_t len, u32 *session_timeout,
234 u32 *acct_interim_interval, int *vlan_id,
235 struct hostapd_sta_wpa_psk_short **psk,
236 char **identity, char **radius_cui)
239 *session_timeout = 0;
240 if (acct_interim_interval)
241 *acct_interim_interval = 0;
251 if (hostapd_maclist_found(hapd->conf->accept_mac,
252 hapd->conf->num_accept_mac, addr, vlan_id))
253 return HOSTAPD_ACL_ACCEPT;
255 if (hostapd_maclist_found(hapd->conf->deny_mac,
256 hapd->conf->num_deny_mac, addr, vlan_id))
257 return HOSTAPD_ACL_REJECT;
259 if (hapd->conf->macaddr_acl == ACCEPT_UNLESS_DENIED)
260 return HOSTAPD_ACL_ACCEPT;
261 if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED)
262 return HOSTAPD_ACL_REJECT;
264 if (hapd->conf->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH) {
265 #ifdef CONFIG_NO_RADIUS
266 return HOSTAPD_ACL_REJECT;
267 #else /* CONFIG_NO_RADIUS */
268 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;
308 os_get_reltime(&query->timestamp);
309 os_memcpy(query->addr, addr, ETH_ALEN);
310 if (hostapd_radius_acl_query(hapd, addr, query)) {
311 wpa_printf(MSG_DEBUG, "Failed to send Access-Request "
313 hostapd_acl_query_free(query);
314 return HOSTAPD_ACL_REJECT;
317 query->auth_msg = os_malloc(len);
318 if (query->auth_msg == NULL) {
319 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
321 hostapd_acl_query_free(query);
322 return HOSTAPD_ACL_REJECT;
324 os_memcpy(query->auth_msg, msg, len);
325 query->auth_msg_len = len;
326 query->next = hapd->acl_queries;
327 hapd->acl_queries = query;
329 /* Queued data will be processed in hostapd_acl_recv_radius()
330 * when RADIUS server replies to the sent Access-Request. */
331 return HOSTAPD_ACL_PENDING;
332 #endif /* CONFIG_NO_RADIUS */
335 return HOSTAPD_ACL_REJECT;
339 #ifndef CONFIG_NO_RADIUS
340 static void hostapd_acl_expire_cache(struct hostapd_data *hapd,
341 struct os_reltime *now)
343 struct hostapd_cached_radius_acl *prev, *entry, *tmp;
346 entry = hapd->acl_cache;
349 if (os_reltime_expired(now, &entry->timestamp,
350 RADIUS_ACL_TIMEOUT)) {
351 wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
352 " has expired.", MAC2STR(entry->addr));
354 prev->next = entry->next;
356 hapd->acl_cache = entry->next;
357 hostapd_drv_set_radius_acl_expire(hapd, entry->addr);
360 hostapd_acl_cache_free_entry(tmp);
370 static void hostapd_acl_expire_queries(struct hostapd_data *hapd,
371 struct os_reltime *now)
373 struct hostapd_acl_query_data *prev, *entry, *tmp;
376 entry = hapd->acl_queries;
379 if (os_reltime_expired(now, &entry->timestamp,
380 RADIUS_ACL_TIMEOUT)) {
381 wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
382 " has expired.", MAC2STR(entry->addr));
384 prev->next = entry->next;
386 hapd->acl_queries = entry->next;
390 hostapd_acl_query_free(tmp);
401 * hostapd_acl_expire - ACL cache expiration callback
402 * @hapd: struct hostapd_data *
404 void hostapd_acl_expire(struct hostapd_data *hapd)
406 struct os_reltime now;
408 os_get_reltime(&now);
409 hostapd_acl_expire_cache(hapd, &now);
410 hostapd_acl_expire_queries(hapd, &now);
414 static void decode_tunnel_passwords(struct hostapd_data *hapd,
415 const u8 *shared_secret,
416 size_t shared_secret_len,
417 struct radius_msg *msg,
418 struct radius_msg *req,
419 struct hostapd_cached_radius_acl *cache)
422 char *passphrase, *strpassphrase;
424 struct hostapd_sta_wpa_psk_short *psk;
427 * Decode all tunnel passwords as PSK and save them into a linked list.
430 passphrase = radius_msg_get_tunnel_password(
431 msg, &passphraselen, shared_secret, shared_secret_len,
434 * Passphrase is NULL iff there is no i-th Tunnel-Password
437 if (passphrase == NULL)
440 * passphrase does not contain the NULL termination.
441 * Add it here as pbkdf2_sha1() requires it.
443 strpassphrase = os_zalloc(passphraselen + 1);
444 psk = os_zalloc(sizeof(struct hostapd_sta_wpa_psk_short));
445 if (strpassphrase && psk) {
446 os_memcpy(strpassphrase, passphrase, passphraselen);
447 pbkdf2_sha1(strpassphrase,
448 hapd->conf->ssid.ssid,
449 hapd->conf->ssid.ssid_len, 4096,
451 psk->next = cache->psk;
455 os_free(strpassphrase);
463 * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
464 * @msg: RADIUS response message
465 * @req: RADIUS request message
466 * @shared_secret: RADIUS shared secret
467 * @shared_secret_len: Length of shared_secret in octets
468 * @data: Context data (struct hostapd_data *)
469 * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
470 * was processed here) or RADIUS_RX_UNKNOWN if not.
472 static RadiusRxResult
473 hostapd_acl_recv_radius(struct radius_msg *msg, struct radius_msg *req,
474 const u8 *shared_secret, size_t shared_secret_len,
477 struct hostapd_data *hapd = data;
478 struct hostapd_acl_query_data *query, *prev;
479 struct hostapd_cached_radius_acl *cache;
480 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
482 query = hapd->acl_queries;
485 if (query->radius_id == hdr->identifier)
491 return RADIUS_RX_UNKNOWN;
493 wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
494 "message (id=%d)", query->radius_id);
496 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
497 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
498 "correct authenticator - dropped\n");
499 return RADIUS_RX_INVALID_AUTHENTICATOR;
502 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
503 hdr->code != RADIUS_CODE_ACCESS_REJECT) {
504 wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
506 return RADIUS_RX_UNKNOWN;
509 /* Insert Accept/Reject info into ACL cache */
510 cache = os_zalloc(sizeof(*cache));
512 wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
515 os_get_reltime(&cache->timestamp);
516 os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
517 if (hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
521 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
522 &cache->session_timeout) == 0)
523 cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
525 cache->accepted = HOSTAPD_ACL_ACCEPT;
527 if (radius_msg_get_attr_int32(
528 msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
529 &cache->acct_interim_interval) == 0 &&
530 cache->acct_interim_interval < 60) {
531 wpa_printf(MSG_DEBUG, "Ignored too small "
532 "Acct-Interim-Interval %d for STA " MACSTR,
533 cache->acct_interim_interval,
534 MAC2STR(query->addr));
535 cache->acct_interim_interval = 0;
538 cache->vlan_id = radius_msg_get_vlanid(msg);
540 decode_tunnel_passwords(hapd, shared_secret, shared_secret_len,
543 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME,
544 &buf, &len, NULL) == 0) {
545 cache->identity = os_zalloc(len + 1);
547 os_memcpy(cache->identity, buf, len);
549 if (radius_msg_get_attr_ptr(
550 msg, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
551 &buf, &len, NULL) == 0) {
552 cache->radius_cui = os_zalloc(len + 1);
553 if (cache->radius_cui)
554 os_memcpy(cache->radius_cui, buf, len);
557 if (hapd->conf->wpa_psk_radius == PSK_RADIUS_REQUIRED &&
559 cache->accepted = HOSTAPD_ACL_REJECT;
561 if (cache->vlan_id &&
562 !hostapd_vlan_id_valid(hapd->conf->vlan, cache->vlan_id)) {
563 hostapd_logger(hapd, query->addr,
564 HOSTAPD_MODULE_RADIUS,
566 "Invalid VLAN ID %d received from RADIUS server",
570 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_REQUIRED &&
572 cache->accepted = HOSTAPD_ACL_REJECT;
574 cache->accepted = HOSTAPD_ACL_REJECT;
575 cache->next = hapd->acl_cache;
576 hapd->acl_cache = cache;
578 #ifdef CONFIG_DRIVER_RADIUS_ACL
579 hostapd_drv_set_radius_acl_auth(hapd, query->addr, cache->accepted,
580 cache->session_timeout);
581 #else /* CONFIG_DRIVER_RADIUS_ACL */
583 /* Re-send original authentication frame for 802.11 processing */
584 wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
585 "successful RADIUS ACL query");
586 ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len, NULL);
587 #endif /* NEED_AP_MLME */
588 #endif /* CONFIG_DRIVER_RADIUS_ACL */
592 hapd->acl_queries = query->next;
594 prev->next = query->next;
596 hostapd_acl_query_free(query);
598 return RADIUS_RX_PROCESSED;
600 #endif /* CONFIG_NO_RADIUS */
604 * hostapd_acl_init: Initialize IEEE 802.11 ACL
605 * @hapd: hostapd BSS data
606 * Returns: 0 on success, -1 on failure
608 int hostapd_acl_init(struct hostapd_data *hapd)
610 #ifndef CONFIG_NO_RADIUS
611 if (radius_client_register(hapd->radius, RADIUS_AUTH,
612 hostapd_acl_recv_radius, hapd))
614 #endif /* CONFIG_NO_RADIUS */
621 * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
622 * @hapd: hostapd BSS data
624 void hostapd_acl_deinit(struct hostapd_data *hapd)
626 struct hostapd_acl_query_data *query, *prev;
628 #ifndef CONFIG_NO_RADIUS
629 hostapd_acl_cache_free(hapd->acl_cache);
630 #endif /* CONFIG_NO_RADIUS */
632 query = hapd->acl_queries;
636 hostapd_acl_query_free(prev);
641 void hostapd_free_psk_list(struct hostapd_sta_wpa_psk_short *psk)
644 struct hostapd_sta_wpa_psk_short *prev = psk;