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
)
58 struct hostapd_sta_wpa_psk_short
*psk
= e
->psk
;
60 os_free(e
->radius_cui
);
62 struct hostapd_sta_wpa_psk_short
*prev
= psk
;
70 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl
*acl_cache
)
72 struct hostapd_cached_radius_acl
*prev
;
76 acl_cache
= acl_cache
->next
;
77 hostapd_acl_cache_free_entry(prev
);
82 static void copy_psk_list(struct hostapd_sta_wpa_psk_short
**psk
,
83 struct hostapd_sta_wpa_psk_short
*src
)
85 struct hostapd_sta_wpa_psk_short
**copy_to
;
86 struct hostapd_sta_wpa_psk_short
*copy_from
;
88 /* Copy PSK linked list */
91 while (copy_from
&& copy_to
) {
92 *copy_to
= os_zalloc(sizeof(struct hostapd_sta_wpa_psk_short
));
95 os_memcpy(*copy_to
, copy_from
,
96 sizeof(struct hostapd_sta_wpa_psk_short
));
97 copy_from
= copy_from
->next
;
98 copy_to
= &((*copy_to
)->next
);
105 static int hostapd_acl_cache_get(struct hostapd_data
*hapd
, const u8
*addr
,
106 u32
*session_timeout
,
107 u32
*acct_interim_interval
, int *vlan_id
,
108 struct hostapd_sta_wpa_psk_short
**psk
,
109 char **identity
, char **radius_cui
)
111 struct hostapd_cached_radius_acl
*entry
;
116 for (entry
= hapd
->acl_cache
; entry
; entry
= entry
->next
) {
117 if (os_memcmp(entry
->addr
, addr
, ETH_ALEN
) != 0)
120 if (now
.sec
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
)
121 return -1; /* entry has expired */
122 if (entry
->accepted
== HOSTAPD_ACL_ACCEPT_TIMEOUT
)
124 *session_timeout
= entry
->session_timeout
;
125 if (acct_interim_interval
)
126 *acct_interim_interval
=
127 entry
->acct_interim_interval
;
129 *vlan_id
= entry
->vlan_id
;
130 copy_psk_list(psk
, entry
->psk
);
133 *identity
= os_strdup(entry
->identity
);
138 if (entry
->radius_cui
)
139 *radius_cui
= os_strdup(entry
->radius_cui
);
143 return entry
->accepted
;
148 #endif /* CONFIG_NO_RADIUS */
151 static void hostapd_acl_query_free(struct hostapd_acl_query_data
*query
)
155 os_free(query
->auth_msg
);
160 #ifndef CONFIG_NO_RADIUS
161 static int hostapd_radius_acl_query(struct hostapd_data
*hapd
, const u8
*addr
,
162 struct hostapd_acl_query_data
*query
)
164 struct radius_msg
*msg
;
167 query
->radius_id
= radius_client_get_id(hapd
->radius
);
168 msg
= radius_msg_new(RADIUS_CODE_ACCESS_REQUEST
, query
->radius_id
);
172 radius_msg_make_authenticator(msg
, addr
, ETH_ALEN
);
174 os_snprintf(buf
, sizeof(buf
), RADIUS_ADDR_FORMAT
, MAC2STR(addr
));
175 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
, (u8
*) buf
,
177 wpa_printf(MSG_DEBUG
, "Could not add User-Name");
181 if (!radius_msg_add_attr_user_password(
182 msg
, (u8
*) buf
, os_strlen(buf
),
183 hapd
->conf
->radius
->auth_server
->shared_secret
,
184 hapd
->conf
->radius
->auth_server
->shared_secret_len
)) {
185 wpa_printf(MSG_DEBUG
, "Could not add User-Password");
189 if (add_common_radius_attr(hapd
, hapd
->conf
->radius_auth_req_attr
,
193 os_snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
195 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CALLING_STATION_ID
,
196 (u8
*) buf
, os_strlen(buf
))) {
197 wpa_printf(MSG_DEBUG
, "Could not add Calling-Station-Id");
201 os_snprintf(buf
, sizeof(buf
), "CONNECT 11Mbps 802.11b");
202 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CONNECT_INFO
,
203 (u8
*) buf
, os_strlen(buf
))) {
204 wpa_printf(MSG_DEBUG
, "Could not add Connect-Info");
208 if (radius_client_send(hapd
->radius
, msg
, RADIUS_AUTH
, addr
) < 0)
213 radius_msg_free(msg
);
216 #endif /* CONFIG_NO_RADIUS */
220 * hostapd_allowed_address - Check whether a specified STA can be authenticated
221 * @hapd: hostapd BSS data
222 * @addr: MAC address of the STA
223 * @msg: Authentication message
224 * @len: Length of msg in octets
225 * @session_timeout: Buffer for returning session timeout (from RADIUS)
226 * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
227 * @vlan_id: Buffer for returning VLAN ID
228 * @psk: Linked list buffer for returning WPA PSK
229 * @identity: Buffer for returning identity (from RADIUS)
230 * @radius_cui: Buffer for returning CUI (from RADIUS)
231 * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
233 * The caller is responsible for freeing the returned *identity and *radius_cui
234 * values with os_free().
236 int hostapd_allowed_address(struct hostapd_data
*hapd
, const u8
*addr
,
237 const u8
*msg
, size_t len
, u32
*session_timeout
,
238 u32
*acct_interim_interval
, int *vlan_id
,
239 struct hostapd_sta_wpa_psk_short
**psk
,
240 char **identity
, char **radius_cui
)
243 *session_timeout
= 0;
244 if (acct_interim_interval
)
245 *acct_interim_interval
= 0;
255 if (hostapd_maclist_found(hapd
->conf
->accept_mac
,
256 hapd
->conf
->num_accept_mac
, addr
, vlan_id
))
257 return HOSTAPD_ACL_ACCEPT
;
259 if (hostapd_maclist_found(hapd
->conf
->deny_mac
,
260 hapd
->conf
->num_deny_mac
, addr
, vlan_id
))
261 return HOSTAPD_ACL_REJECT
;
263 if (hapd
->conf
->macaddr_acl
== ACCEPT_UNLESS_DENIED
)
264 return HOSTAPD_ACL_ACCEPT
;
265 if (hapd
->conf
->macaddr_acl
== DENY_UNLESS_ACCEPTED
)
266 return HOSTAPD_ACL_REJECT
;
268 if (hapd
->conf
->macaddr_acl
== USE_EXTERNAL_RADIUS_AUTH
) {
269 #ifdef CONFIG_NO_RADIUS
270 return HOSTAPD_ACL_REJECT
;
271 #else /* CONFIG_NO_RADIUS */
272 struct hostapd_acl_query_data
*query
;
275 /* Check whether ACL cache has an entry for this station */
276 int res
= hostapd_acl_cache_get(hapd
, addr
, session_timeout
,
277 acct_interim_interval
,
279 identity
, radius_cui
);
280 if (res
== HOSTAPD_ACL_ACCEPT
||
281 res
== HOSTAPD_ACL_ACCEPT_TIMEOUT
)
283 if (res
== HOSTAPD_ACL_REJECT
)
284 return HOSTAPD_ACL_REJECT
;
286 query
= hapd
->acl_queries
;
288 if (os_memcmp(query
->addr
, addr
, ETH_ALEN
) == 0) {
289 /* pending query in RADIUS retransmit queue;
290 * do not generate a new one */
296 os_free(*radius_cui
);
299 return HOSTAPD_ACL_PENDING
;
304 if (!hapd
->conf
->radius
->auth_server
)
305 return HOSTAPD_ACL_REJECT
;
307 /* No entry in the cache - query external RADIUS server */
308 query
= os_zalloc(sizeof(*query
));
310 wpa_printf(MSG_ERROR
, "malloc for query data failed");
311 return HOSTAPD_ACL_REJECT
;
314 query
->timestamp
= t
.sec
;
315 os_memcpy(query
->addr
, addr
, ETH_ALEN
);
316 if (hostapd_radius_acl_query(hapd
, addr
, query
)) {
317 wpa_printf(MSG_DEBUG
, "Failed to send Access-Request "
319 hostapd_acl_query_free(query
);
320 return HOSTAPD_ACL_REJECT
;
323 query
->auth_msg
= os_malloc(len
);
324 if (query
->auth_msg
== NULL
) {
325 wpa_printf(MSG_ERROR
, "Failed to allocate memory for "
327 hostapd_acl_query_free(query
);
328 return HOSTAPD_ACL_REJECT
;
330 os_memcpy(query
->auth_msg
, msg
, len
);
331 query
->auth_msg_len
= len
;
332 query
->next
= hapd
->acl_queries
;
333 hapd
->acl_queries
= query
;
335 /* Queued data will be processed in hostapd_acl_recv_radius()
336 * when RADIUS server replies to the sent Access-Request. */
337 return HOSTAPD_ACL_PENDING
;
338 #endif /* CONFIG_NO_RADIUS */
341 return HOSTAPD_ACL_REJECT
;
345 #ifndef CONFIG_NO_RADIUS
346 static void hostapd_acl_expire_cache(struct hostapd_data
*hapd
, os_time_t now
)
348 struct hostapd_cached_radius_acl
*prev
, *entry
, *tmp
;
351 entry
= hapd
->acl_cache
;
354 if (now
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
) {
355 wpa_printf(MSG_DEBUG
, "Cached ACL entry for " MACSTR
356 " has expired.", MAC2STR(entry
->addr
));
358 prev
->next
= entry
->next
;
360 hapd
->acl_cache
= entry
->next
;
361 hostapd_drv_set_radius_acl_expire(hapd
, entry
->addr
);
364 hostapd_acl_cache_free_entry(tmp
);
374 static void hostapd_acl_expire_queries(struct hostapd_data
*hapd
,
377 struct hostapd_acl_query_data
*prev
, *entry
, *tmp
;
380 entry
= hapd
->acl_queries
;
383 if (now
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
) {
384 wpa_printf(MSG_DEBUG
, "ACL query for " MACSTR
385 " has expired.", MAC2STR(entry
->addr
));
387 prev
->next
= entry
->next
;
389 hapd
->acl_queries
= entry
->next
;
393 hostapd_acl_query_free(tmp
);
404 * hostapd_acl_expire - ACL cache expiration callback
405 * @eloop_ctx: struct hostapd_data *
406 * @timeout_ctx: Not used
408 static void hostapd_acl_expire(void *eloop_ctx
, void *timeout_ctx
)
410 struct hostapd_data
*hapd
= eloop_ctx
;
414 hostapd_acl_expire_cache(hapd
, now
.sec
);
415 hostapd_acl_expire_queries(hapd
, now
.sec
);
417 eloop_register_timeout(10, 0, hostapd_acl_expire
, hapd
, NULL
);
421 static void decode_tunnel_passwords(struct hostapd_data
*hapd
,
422 const u8
*shared_secret
,
423 size_t shared_secret_len
,
424 struct radius_msg
*msg
,
425 struct radius_msg
*req
,
426 struct hostapd_cached_radius_acl
*cache
)
429 char *passphrase
, *strpassphrase
;
431 struct hostapd_sta_wpa_psk_short
*psk
;
434 * Decode all tunnel passwords as PSK and save them into a linked list.
437 passphrase
= radius_msg_get_tunnel_password(
438 msg
, &passphraselen
, shared_secret
, shared_secret_len
,
441 * Passphrase is NULL iff there is no i-th Tunnel-Password
444 if (passphrase
== NULL
)
447 * passphrase does not contain the NULL termination.
448 * Add it here as pbkdf2_sha1() requires it.
450 strpassphrase
= os_zalloc(passphraselen
+ 1);
451 psk
= os_zalloc(sizeof(struct hostapd_sta_wpa_psk_short
));
452 if (strpassphrase
&& psk
) {
453 os_memcpy(strpassphrase
, passphrase
, passphraselen
);
454 pbkdf2_sha1(strpassphrase
,
455 hapd
->conf
->ssid
.ssid
,
456 hapd
->conf
->ssid
.ssid_len
, 4096,
458 psk
->next
= cache
->psk
;
462 os_free(strpassphrase
);
470 * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
471 * @msg: RADIUS response message
472 * @req: RADIUS request message
473 * @shared_secret: RADIUS shared secret
474 * @shared_secret_len: Length of shared_secret in octets
475 * @data: Context data (struct hostapd_data *)
476 * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
477 * was processed here) or RADIUS_RX_UNKNOWN if not.
479 static RadiusRxResult
480 hostapd_acl_recv_radius(struct radius_msg
*msg
, struct radius_msg
*req
,
481 const u8
*shared_secret
, size_t shared_secret_len
,
484 struct hostapd_data
*hapd
= data
;
485 struct hostapd_acl_query_data
*query
, *prev
;
486 struct hostapd_cached_radius_acl
*cache
;
487 struct radius_hdr
*hdr
= radius_msg_get_hdr(msg
);
490 query
= hapd
->acl_queries
;
493 if (query
->radius_id
== hdr
->identifier
)
499 return RADIUS_RX_UNKNOWN
;
501 wpa_printf(MSG_DEBUG
, "Found matching Access-Request for RADIUS "
502 "message (id=%d)", query
->radius_id
);
504 if (radius_msg_verify(msg
, shared_secret
, shared_secret_len
, req
, 0)) {
505 wpa_printf(MSG_INFO
, "Incoming RADIUS packet did not have "
506 "correct authenticator - dropped\n");
507 return RADIUS_RX_INVALID_AUTHENTICATOR
;
510 if (hdr
->code
!= RADIUS_CODE_ACCESS_ACCEPT
&&
511 hdr
->code
!= RADIUS_CODE_ACCESS_REJECT
) {
512 wpa_printf(MSG_DEBUG
, "Unknown RADIUS message code %d to ACL "
514 return RADIUS_RX_UNKNOWN
;
517 /* Insert Accept/Reject info into ACL cache */
518 cache
= os_zalloc(sizeof(*cache
));
520 wpa_printf(MSG_DEBUG
, "Failed to add ACL cache entry");
524 cache
->timestamp
= t
.sec
;
525 os_memcpy(cache
->addr
, query
->addr
, sizeof(cache
->addr
));
526 if (hdr
->code
== RADIUS_CODE_ACCESS_ACCEPT
) {
530 if (radius_msg_get_attr_int32(msg
, RADIUS_ATTR_SESSION_TIMEOUT
,
531 &cache
->session_timeout
) == 0)
532 cache
->accepted
= HOSTAPD_ACL_ACCEPT_TIMEOUT
;
534 cache
->accepted
= HOSTAPD_ACL_ACCEPT
;
536 if (radius_msg_get_attr_int32(
537 msg
, RADIUS_ATTR_ACCT_INTERIM_INTERVAL
,
538 &cache
->acct_interim_interval
) == 0 &&
539 cache
->acct_interim_interval
< 60) {
540 wpa_printf(MSG_DEBUG
, "Ignored too small "
541 "Acct-Interim-Interval %d for STA " MACSTR
,
542 cache
->acct_interim_interval
,
543 MAC2STR(query
->addr
));
544 cache
->acct_interim_interval
= 0;
547 cache
->vlan_id
= radius_msg_get_vlanid(msg
);
549 decode_tunnel_passwords(hapd
, shared_secret
, shared_secret_len
,
552 if (radius_msg_get_attr_ptr(msg
, RADIUS_ATTR_USER_NAME
,
553 &buf
, &len
, NULL
) == 0) {
554 cache
->identity
= os_zalloc(len
+ 1);
556 os_memcpy(cache
->identity
, buf
, len
);
558 if (radius_msg_get_attr_ptr(
559 msg
, RADIUS_ATTR_CHARGEABLE_USER_IDENTITY
,
560 &buf
, &len
, NULL
) == 0) {
561 cache
->radius_cui
= os_zalloc(len
+ 1);
562 if (cache
->radius_cui
)
563 os_memcpy(cache
->radius_cui
, buf
, len
);
566 if (hapd
->conf
->wpa_psk_radius
== PSK_RADIUS_REQUIRED
&&
568 cache
->accepted
= HOSTAPD_ACL_REJECT
;
570 cache
->accepted
= HOSTAPD_ACL_REJECT
;
571 cache
->next
= hapd
->acl_cache
;
572 hapd
->acl_cache
= cache
;
574 #ifdef CONFIG_DRIVER_RADIUS_ACL
575 hostapd_drv_set_radius_acl_auth(hapd
, query
->addr
, cache
->accepted
,
576 cache
->session_timeout
);
577 #else /* CONFIG_DRIVER_RADIUS_ACL */
579 /* Re-send original authentication frame for 802.11 processing */
580 wpa_printf(MSG_DEBUG
, "Re-sending authentication frame after "
581 "successful RADIUS ACL query");
582 ieee802_11_mgmt(hapd
, query
->auth_msg
, query
->auth_msg_len
, NULL
);
583 #endif /* NEED_AP_MLME */
584 #endif /* CONFIG_DRIVER_RADIUS_ACL */
588 hapd
->acl_queries
= query
->next
;
590 prev
->next
= query
->next
;
592 hostapd_acl_query_free(query
);
594 return RADIUS_RX_PROCESSED
;
596 #endif /* CONFIG_NO_RADIUS */
600 * hostapd_acl_init: Initialize IEEE 802.11 ACL
601 * @hapd: hostapd BSS data
602 * Returns: 0 on success, -1 on failure
604 int hostapd_acl_init(struct hostapd_data
*hapd
)
606 #ifndef CONFIG_NO_RADIUS
607 if (radius_client_register(hapd
->radius
, RADIUS_AUTH
,
608 hostapd_acl_recv_radius
, hapd
))
611 eloop_register_timeout(10, 0, hostapd_acl_expire
, hapd
, NULL
);
612 #endif /* CONFIG_NO_RADIUS */
619 * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
620 * @hapd: hostapd BSS data
622 void hostapd_acl_deinit(struct hostapd_data
*hapd
)
624 struct hostapd_acl_query_data
*query
, *prev
;
626 #ifndef CONFIG_NO_RADIUS
627 eloop_cancel_timeout(hostapd_acl_expire
, hapd
, NULL
);
629 hostapd_acl_cache_free(hapd
->acl_cache
);
630 #endif /* CONFIG_NO_RADIUS */
632 query
= hapd
->acl_queries
;
636 hostapd_acl_query_free(prev
);