2 * hostapd / IEEE 802.11 authentication (ACL)
3 * Copyright (c) 2003-2009, 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_11_auth.h"
27 #define RADIUS_ACL_TIMEOUT 30
30 struct hostapd_cached_radius_acl
{
33 int accepted
; /* HOSTAPD_ACL_* */
34 struct hostapd_cached_radius_acl
*next
;
36 u32 acct_interim_interval
;
43 struct hostapd_acl_query_data
{
47 u8
*auth_msg
; /* IEEE 802.11 authentication frame from station */
49 struct hostapd_acl_query_data
*next
;
53 #ifndef CONFIG_NO_RADIUS
54 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl
*acl_cache
)
56 struct hostapd_cached_radius_acl
*prev
;
60 acl_cache
= acl_cache
->next
;
66 static int hostapd_acl_cache_get(struct hostapd_data
*hapd
, const u8
*addr
,
68 u32
*acct_interim_interval
, int *vlan_id
,
69 u8
*psk
, int *has_psk
)
71 struct hostapd_cached_radius_acl
*entry
;
75 entry
= hapd
->acl_cache
;
78 if (os_memcmp(entry
->addr
, addr
, ETH_ALEN
) == 0) {
79 if (now
.sec
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
)
80 return -1; /* entry has expired */
81 if (entry
->accepted
== HOSTAPD_ACL_ACCEPT_TIMEOUT
)
84 entry
->session_timeout
;
85 if (acct_interim_interval
)
86 *acct_interim_interval
=
87 entry
->acct_interim_interval
;
89 *vlan_id
= entry
->vlan_id
;
91 os_memcpy(psk
, entry
->psk
, PMK_LEN
);
93 *has_psk
= entry
->has_psk
;
94 return entry
->accepted
;
102 #endif /* CONFIG_NO_RADIUS */
105 static void hostapd_acl_query_free(struct hostapd_acl_query_data
*query
)
109 os_free(query
->auth_msg
);
114 #ifndef CONFIG_NO_RADIUS
115 static int hostapd_radius_acl_query(struct hostapd_data
*hapd
, const u8
*addr
,
116 struct hostapd_acl_query_data
*query
)
118 struct radius_msg
*msg
;
121 query
->radius_id
= radius_client_get_id(hapd
->radius
);
122 msg
= radius_msg_new(RADIUS_CODE_ACCESS_REQUEST
, query
->radius_id
);
126 radius_msg_make_authenticator(msg
, addr
, ETH_ALEN
);
128 os_snprintf(buf
, sizeof(buf
), RADIUS_ADDR_FORMAT
, MAC2STR(addr
));
129 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
, (u8
*) buf
,
131 wpa_printf(MSG_DEBUG
, "Could not add User-Name");
135 if (!radius_msg_add_attr_user_password(
136 msg
, (u8
*) buf
, os_strlen(buf
),
137 hapd
->conf
->radius
->auth_server
->shared_secret
,
138 hapd
->conf
->radius
->auth_server
->shared_secret_len
)) {
139 wpa_printf(MSG_DEBUG
, "Could not add User-Password");
143 if (hapd
->conf
->own_ip_addr
.af
== AF_INET
&&
144 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IP_ADDRESS
,
145 (u8
*) &hapd
->conf
->own_ip_addr
.u
.v4
, 4)) {
146 wpa_printf(MSG_DEBUG
, "Could not add NAS-IP-Address");
151 if (hapd
->conf
->own_ip_addr
.af
== AF_INET6
&&
152 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IPV6_ADDRESS
,
153 (u8
*) &hapd
->conf
->own_ip_addr
.u
.v6
, 16)) {
154 wpa_printf(MSG_DEBUG
, "Could not add NAS-IPv6-Address");
157 #endif /* CONFIG_IPV6 */
159 if (hapd
->conf
->nas_identifier
&&
160 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IDENTIFIER
,
161 (u8
*) hapd
->conf
->nas_identifier
,
162 os_strlen(hapd
->conf
->nas_identifier
))) {
163 wpa_printf(MSG_DEBUG
, "Could not add NAS-Identifier");
167 os_snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
":%s",
168 MAC2STR(hapd
->own_addr
), hapd
->conf
->ssid
.ssid
);
169 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CALLED_STATION_ID
,
170 (u8
*) buf
, os_strlen(buf
))) {
171 wpa_printf(MSG_DEBUG
, "Could not add Called-Station-Id");
175 os_snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
177 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CALLING_STATION_ID
,
178 (u8
*) buf
, os_strlen(buf
))) {
179 wpa_printf(MSG_DEBUG
, "Could not add Calling-Station-Id");
183 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_NAS_PORT_TYPE
,
184 RADIUS_NAS_PORT_TYPE_IEEE_802_11
)) {
185 wpa_printf(MSG_DEBUG
, "Could not add NAS-Port-Type");
189 os_snprintf(buf
, sizeof(buf
), "CONNECT 11Mbps 802.11b");
190 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CONNECT_INFO
,
191 (u8
*) buf
, os_strlen(buf
))) {
192 wpa_printf(MSG_DEBUG
, "Could not add Connect-Info");
196 radius_client_send(hapd
->radius
, msg
, RADIUS_AUTH
, addr
);
200 radius_msg_free(msg
);
203 #endif /* CONFIG_NO_RADIUS */
207 * hostapd_allowed_address - Check whether a specified STA can be authenticated
208 * @hapd: hostapd BSS data
209 * @addr: MAC address of the STA
210 * @msg: Authentication message
211 * @len: Length of msg in octets
212 * @session_timeout: Buffer for returning session timeout (from RADIUS)
213 * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
214 * @vlan_id: Buffer for returning VLAN ID
215 * @psk: Buffer for returning WPA PSK
216 * @has_psk: Buffer for indicating whether psk was filled
217 * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
219 int hostapd_allowed_address(struct hostapd_data
*hapd
, const u8
*addr
,
220 const u8
*msg
, size_t len
, u32
*session_timeout
,
221 u32
*acct_interim_interval
, int *vlan_id
,
222 u8
*psk
, int *has_psk
)
225 *session_timeout
= 0;
226 if (acct_interim_interval
)
227 *acct_interim_interval
= 0;
233 os_memset(psk
, 0, PMK_LEN
);
235 if (hostapd_maclist_found(hapd
->conf
->accept_mac
,
236 hapd
->conf
->num_accept_mac
, addr
, vlan_id
))
237 return HOSTAPD_ACL_ACCEPT
;
239 if (hostapd_maclist_found(hapd
->conf
->deny_mac
,
240 hapd
->conf
->num_deny_mac
, addr
, vlan_id
))
241 return HOSTAPD_ACL_REJECT
;
243 if (hapd
->conf
->macaddr_acl
== ACCEPT_UNLESS_DENIED
)
244 return HOSTAPD_ACL_ACCEPT
;
245 if (hapd
->conf
->macaddr_acl
== DENY_UNLESS_ACCEPTED
)
246 return HOSTAPD_ACL_REJECT
;
248 if (hapd
->conf
->macaddr_acl
== USE_EXTERNAL_RADIUS_AUTH
) {
249 #ifdef CONFIG_NO_RADIUS
250 return HOSTAPD_ACL_REJECT
;
251 #else /* CONFIG_NO_RADIUS */
252 struct hostapd_acl_query_data
*query
;
255 /* Check whether ACL cache has an entry for this station */
256 int res
= hostapd_acl_cache_get(hapd
, addr
, session_timeout
,
257 acct_interim_interval
,
258 vlan_id
, psk
, has_psk
);
259 if (res
== HOSTAPD_ACL_ACCEPT
||
260 res
== HOSTAPD_ACL_ACCEPT_TIMEOUT
)
262 if (res
== HOSTAPD_ACL_REJECT
)
263 return HOSTAPD_ACL_REJECT
;
265 query
= hapd
->acl_queries
;
267 if (os_memcmp(query
->addr
, addr
, ETH_ALEN
) == 0) {
268 /* pending query in RADIUS retransmit queue;
269 * do not generate a new one */
270 return HOSTAPD_ACL_PENDING
;
275 if (!hapd
->conf
->radius
->auth_server
)
276 return HOSTAPD_ACL_REJECT
;
278 /* No entry in the cache - query external RADIUS server */
279 query
= os_zalloc(sizeof(*query
));
281 wpa_printf(MSG_ERROR
, "malloc for query data failed");
282 return HOSTAPD_ACL_REJECT
;
285 query
->timestamp
= t
.sec
;
286 os_memcpy(query
->addr
, addr
, ETH_ALEN
);
287 if (hostapd_radius_acl_query(hapd
, addr
, query
)) {
288 wpa_printf(MSG_DEBUG
, "Failed to send Access-Request "
290 hostapd_acl_query_free(query
);
291 return HOSTAPD_ACL_REJECT
;
294 query
->auth_msg
= os_malloc(len
);
295 if (query
->auth_msg
== NULL
) {
296 wpa_printf(MSG_ERROR
, "Failed to allocate memory for "
298 hostapd_acl_query_free(query
);
299 return HOSTAPD_ACL_REJECT
;
301 os_memcpy(query
->auth_msg
, msg
, len
);
302 query
->auth_msg_len
= len
;
303 query
->next
= hapd
->acl_queries
;
304 hapd
->acl_queries
= query
;
306 /* Queued data will be processed in hostapd_acl_recv_radius()
307 * when RADIUS server replies to the sent Access-Request. */
308 return HOSTAPD_ACL_PENDING
;
309 #endif /* CONFIG_NO_RADIUS */
312 return HOSTAPD_ACL_REJECT
;
316 #ifndef CONFIG_NO_RADIUS
317 static void hostapd_acl_expire_cache(struct hostapd_data
*hapd
, os_time_t now
)
319 struct hostapd_cached_radius_acl
*prev
, *entry
, *tmp
;
322 entry
= hapd
->acl_cache
;
325 if (now
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
) {
326 wpa_printf(MSG_DEBUG
, "Cached ACL entry for " MACSTR
327 " has expired.", MAC2STR(entry
->addr
));
329 prev
->next
= entry
->next
;
331 hapd
->acl_cache
= entry
->next
;
332 hostapd_drv_set_radius_acl_expire(hapd
, entry
->addr
);
345 static void hostapd_acl_expire_queries(struct hostapd_data
*hapd
,
348 struct hostapd_acl_query_data
*prev
, *entry
, *tmp
;
351 entry
= hapd
->acl_queries
;
354 if (now
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
) {
355 wpa_printf(MSG_DEBUG
, "ACL query for " MACSTR
356 " has expired.", MAC2STR(entry
->addr
));
358 prev
->next
= entry
->next
;
360 hapd
->acl_queries
= entry
->next
;
364 hostapd_acl_query_free(tmp
);
375 * hostapd_acl_expire - ACL cache expiration callback
376 * @eloop_ctx: struct hostapd_data *
377 * @timeout_ctx: Not used
379 static void hostapd_acl_expire(void *eloop_ctx
, void *timeout_ctx
)
381 struct hostapd_data
*hapd
= eloop_ctx
;
385 hostapd_acl_expire_cache(hapd
, now
.sec
);
386 hostapd_acl_expire_queries(hapd
, now
.sec
);
388 eloop_register_timeout(10, 0, hostapd_acl_expire
, hapd
, NULL
);
393 * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
394 * @msg: RADIUS response message
395 * @req: RADIUS request message
396 * @shared_secret: RADIUS shared secret
397 * @shared_secret_len: Length of shared_secret in octets
398 * @data: Context data (struct hostapd_data *)
399 * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
400 * was processed here) or RADIUS_RX_UNKNOWN if not.
402 static RadiusRxResult
403 hostapd_acl_recv_radius(struct radius_msg
*msg
, struct radius_msg
*req
,
404 const u8
*shared_secret
, size_t shared_secret_len
,
407 struct hostapd_data
*hapd
= data
;
408 struct hostapd_acl_query_data
*query
, *prev
;
409 struct hostapd_cached_radius_acl
*cache
;
410 struct radius_hdr
*hdr
= radius_msg_get_hdr(msg
);
413 query
= hapd
->acl_queries
;
416 if (query
->radius_id
== hdr
->identifier
)
422 return RADIUS_RX_UNKNOWN
;
424 wpa_printf(MSG_DEBUG
, "Found matching Access-Request for RADIUS "
425 "message (id=%d)", query
->radius_id
);
427 if (radius_msg_verify(msg
, shared_secret
, shared_secret_len
, req
, 0)) {
428 wpa_printf(MSG_INFO
, "Incoming RADIUS packet did not have "
429 "correct authenticator - dropped\n");
430 return RADIUS_RX_INVALID_AUTHENTICATOR
;
433 if (hdr
->code
!= RADIUS_CODE_ACCESS_ACCEPT
&&
434 hdr
->code
!= RADIUS_CODE_ACCESS_REJECT
) {
435 wpa_printf(MSG_DEBUG
, "Unknown RADIUS message code %d to ACL "
437 return RADIUS_RX_UNKNOWN
;
440 /* Insert Accept/Reject info into ACL cache */
441 cache
= os_zalloc(sizeof(*cache
));
443 wpa_printf(MSG_DEBUG
, "Failed to add ACL cache entry");
447 cache
->timestamp
= t
.sec
;
448 os_memcpy(cache
->addr
, query
->addr
, sizeof(cache
->addr
));
449 if (hdr
->code
== RADIUS_CODE_ACCESS_ACCEPT
) {
453 if (radius_msg_get_attr_int32(msg
, RADIUS_ATTR_SESSION_TIMEOUT
,
454 &cache
->session_timeout
) == 0)
455 cache
->accepted
= HOSTAPD_ACL_ACCEPT_TIMEOUT
;
457 cache
->accepted
= HOSTAPD_ACL_ACCEPT
;
459 if (radius_msg_get_attr_int32(
460 msg
, RADIUS_ATTR_ACCT_INTERIM_INTERVAL
,
461 &cache
->acct_interim_interval
) == 0 &&
462 cache
->acct_interim_interval
< 60) {
463 wpa_printf(MSG_DEBUG
, "Ignored too small "
464 "Acct-Interim-Interval %d for STA " MACSTR
,
465 cache
->acct_interim_interval
,
466 MAC2STR(query
->addr
));
467 cache
->acct_interim_interval
= 0;
470 cache
->vlan_id
= radius_msg_get_vlanid(msg
);
472 passphrase
= radius_msg_get_tunnel_password(
474 hapd
->conf
->radius
->auth_server
->shared_secret
,
475 hapd
->conf
->radius
->auth_server
->shared_secret_len
,
477 cache
->has_psk
= passphrase
!= NULL
;
478 if (passphrase
!= NULL
) {
479 /* passphrase does not contain the NULL termination.
480 * Add it here as pbkdf2_sha1 requires it. */
481 char *strpassphrase
= os_zalloc(passphraselen
+ 1);
483 os_memcpy(strpassphrase
, passphrase
,
485 pbkdf2_sha1(strpassphrase
,
486 hapd
->conf
->ssid
.ssid
,
487 hapd
->conf
->ssid
.ssid_len
, 4096,
488 cache
->psk
, PMK_LEN
);
489 os_free(strpassphrase
);
494 if (hapd
->conf
->wpa_psk_radius
== PSK_RADIUS_REQUIRED
&&
496 cache
->accepted
= HOSTAPD_ACL_REJECT
;
498 cache
->accepted
= HOSTAPD_ACL_REJECT
;
499 cache
->next
= hapd
->acl_cache
;
500 hapd
->acl_cache
= cache
;
502 #ifdef CONFIG_DRIVER_RADIUS_ACL
503 hostapd_drv_set_radius_acl_auth(hapd
, query
->addr
, cache
->accepted
,
504 cache
->session_timeout
);
505 #else /* CONFIG_DRIVER_RADIUS_ACL */
507 /* Re-send original authentication frame for 802.11 processing */
508 wpa_printf(MSG_DEBUG
, "Re-sending authentication frame after "
509 "successful RADIUS ACL query");
510 ieee802_11_mgmt(hapd
, query
->auth_msg
, query
->auth_msg_len
, NULL
);
511 #endif /* NEED_AP_MLME */
512 #endif /* CONFIG_DRIVER_RADIUS_ACL */
516 hapd
->acl_queries
= query
->next
;
518 prev
->next
= query
->next
;
520 hostapd_acl_query_free(query
);
522 return RADIUS_RX_PROCESSED
;
524 #endif /* CONFIG_NO_RADIUS */
528 * hostapd_acl_init: Initialize IEEE 802.11 ACL
529 * @hapd: hostapd BSS data
530 * Returns: 0 on success, -1 on failure
532 int hostapd_acl_init(struct hostapd_data
*hapd
)
534 #ifndef CONFIG_NO_RADIUS
535 if (radius_client_register(hapd
->radius
, RADIUS_AUTH
,
536 hostapd_acl_recv_radius
, hapd
))
539 eloop_register_timeout(10, 0, hostapd_acl_expire
, hapd
, NULL
);
540 #endif /* CONFIG_NO_RADIUS */
547 * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
548 * @hapd: hostapd BSS data
550 void hostapd_acl_deinit(struct hostapd_data
*hapd
)
552 struct hostapd_acl_query_data
*query
, *prev
;
554 #ifndef CONFIG_NO_RADIUS
555 eloop_cancel_timeout(hostapd_acl_expire
, hapd
, NULL
);
557 hostapd_acl_cache_free(hapd
->acl_cache
);
558 #endif /* CONFIG_NO_RADIUS */
560 query
= hapd
->acl_queries
;
564 hostapd_acl_query_free(prev
);