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"
30 #ifdef CONFIG_DRIVER_RADIUS_ACL
32 #endif /* CONFIG_DRIVER_RADIUS_ACL */
34 #define RADIUS_ACL_TIMEOUT 30
37 struct hostapd_cached_radius_acl {
40 int accepted; /* HOSTAPD_ACL_* */
41 struct hostapd_cached_radius_acl *next;
43 u32 acct_interim_interval;
48 struct hostapd_acl_query_data {
52 u8 *auth_msg; /* IEEE 802.11 authentication frame from station */
54 struct hostapd_acl_query_data *next;
58 #ifndef CONFIG_NO_RADIUS
59 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
61 struct hostapd_cached_radius_acl *prev;
65 acl_cache = acl_cache->next;
71 static int hostapd_acl_cache_get(struct hostapd_data *hapd, const u8 *addr,
73 u32 *acct_interim_interval, int *vlan_id)
75 struct hostapd_cached_radius_acl *entry;
79 entry = hapd->acl_cache;
82 if (os_memcmp(entry->addr, addr, ETH_ALEN) == 0) {
83 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT)
84 return -1; /* entry has expired */
85 if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
88 entry->session_timeout;
89 if (acct_interim_interval)
90 *acct_interim_interval =
91 entry->acct_interim_interval;
93 *vlan_id = entry->vlan_id;
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);
204 #endif /* CONFIG_NO_RADIUS */
208 * hostapd_allowed_address - Check whether a specified STA can be authenticated
209 * @hapd: hostapd BSS data
210 * @addr: MAC address of the STA
211 * @msg: Authentication message
212 * @len: Length of msg in octets
213 * @session_timeout: Buffer for returning session timeout (from RADIUS)
214 * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
215 * @vlan_id: Buffer for returning VLAN ID
216 * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
218 int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
219 const u8 *msg, size_t len, u32 *session_timeout,
220 u32 *acct_interim_interval, int *vlan_id)
223 *session_timeout = 0;
224 if (acct_interim_interval)
225 *acct_interim_interval = 0;
229 if (hostapd_maclist_found(hapd->conf->accept_mac,
230 hapd->conf->num_accept_mac, addr, vlan_id))
231 return HOSTAPD_ACL_ACCEPT;
233 if (hostapd_maclist_found(hapd->conf->deny_mac,
234 hapd->conf->num_deny_mac, addr, vlan_id))
235 return HOSTAPD_ACL_REJECT;
237 if (hapd->conf->macaddr_acl == ACCEPT_UNLESS_DENIED)
238 return HOSTAPD_ACL_ACCEPT;
239 if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED)
240 return HOSTAPD_ACL_REJECT;
242 if (hapd->conf->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH) {
243 #ifdef CONFIG_NO_RADIUS
244 return HOSTAPD_ACL_REJECT;
245 #else /* CONFIG_NO_RADIUS */
246 struct hostapd_acl_query_data *query;
248 /* Check whether ACL cache has an entry for this station */
249 int res = hostapd_acl_cache_get(hapd, addr, session_timeout,
250 acct_interim_interval,
252 if (res == HOSTAPD_ACL_ACCEPT ||
253 res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
255 if (res == HOSTAPD_ACL_REJECT)
256 return HOSTAPD_ACL_REJECT;
258 query = hapd->acl_queries;
260 if (os_memcmp(query->addr, addr, ETH_ALEN) == 0) {
261 /* pending query in RADIUS retransmit queue;
262 * do not generate a new one */
263 return HOSTAPD_ACL_PENDING;
268 if (!hapd->conf->radius->auth_server)
269 return HOSTAPD_ACL_REJECT;
271 /* No entry in the cache - query external RADIUS server */
272 query = os_zalloc(sizeof(*query));
274 wpa_printf(MSG_ERROR, "malloc for query data failed");
275 return HOSTAPD_ACL_REJECT;
277 time(&query->timestamp);
278 os_memcpy(query->addr, addr, ETH_ALEN);
279 if (hostapd_radius_acl_query(hapd, addr, query)) {
280 wpa_printf(MSG_DEBUG, "Failed to send Access-Request "
282 hostapd_acl_query_free(query);
283 return HOSTAPD_ACL_REJECT;
286 query->auth_msg = os_malloc(len);
287 if (query->auth_msg == NULL) {
288 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
290 hostapd_acl_query_free(query);
291 return HOSTAPD_ACL_REJECT;
293 os_memcpy(query->auth_msg, msg, len);
294 query->auth_msg_len = len;
295 query->next = hapd->acl_queries;
296 hapd->acl_queries = query;
298 /* Queued data will be processed in hostapd_acl_recv_radius()
299 * when RADIUS server replies to the sent Access-Request. */
300 return HOSTAPD_ACL_PENDING;
301 #endif /* CONFIG_NO_RADIUS */
304 return HOSTAPD_ACL_REJECT;
308 #ifndef CONFIG_NO_RADIUS
309 static void hostapd_acl_expire_cache(struct hostapd_data *hapd, time_t now)
311 struct hostapd_cached_radius_acl *prev, *entry, *tmp;
314 entry = hapd->acl_cache;
317 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
318 wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
319 " has expired.", MAC2STR(entry->addr));
321 prev->next = entry->next;
323 hapd->acl_cache = entry->next;
324 #ifdef CONFIG_DRIVER_RADIUS_ACL
325 hostapd_set_radius_acl_expire(hapd, entry->addr);
326 #endif /* CONFIG_DRIVER_RADIUS_ACL */
339 static void hostapd_acl_expire_queries(struct hostapd_data *hapd, time_t now)
341 struct hostapd_acl_query_data *prev, *entry, *tmp;
344 entry = hapd->acl_queries;
347 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
348 wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
349 " has expired.", MAC2STR(entry->addr));
351 prev->next = entry->next;
353 hapd->acl_queries = entry->next;
357 hostapd_acl_query_free(tmp);
368 * hostapd_acl_expire - ACL cache expiration callback
369 * @eloop_ctx: struct hostapd_data *
370 * @timeout_ctx: Not used
372 static void hostapd_acl_expire(void *eloop_ctx, void *timeout_ctx)
374 struct hostapd_data *hapd = eloop_ctx;
378 hostapd_acl_expire_cache(hapd, now);
379 hostapd_acl_expire_queries(hapd, now);
381 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
386 * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
387 * @msg: RADIUS response message
388 * @req: RADIUS request message
389 * @shared_secret: RADIUS shared secret
390 * @shared_secret_len: Length of shared_secret in octets
391 * @data: Context data (struct hostapd_data *)
392 * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
393 * was processed here) or RADIUS_RX_UNKNOWN if not.
395 static RadiusRxResult
396 hostapd_acl_recv_radius(struct radius_msg *msg, struct radius_msg *req,
397 const u8 *shared_secret, size_t shared_secret_len,
400 struct hostapd_data *hapd = data;
401 struct hostapd_acl_query_data *query, *prev;
402 struct hostapd_cached_radius_acl *cache;
404 query = hapd->acl_queries;
407 if (query->radius_id == msg->hdr->identifier)
413 return RADIUS_RX_UNKNOWN;
415 wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
416 "message (id=%d)", query->radius_id);
418 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
419 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
420 "correct authenticator - dropped\n");
421 return RADIUS_RX_INVALID_AUTHENTICATOR;
424 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
425 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT) {
426 wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
427 "query", msg->hdr->code);
428 return RADIUS_RX_UNKNOWN;
431 /* Insert Accept/Reject info into ACL cache */
432 cache = os_zalloc(sizeof(*cache));
434 wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
437 time(&cache->timestamp);
438 os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
439 if (msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
440 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
441 &cache->session_timeout) == 0)
442 cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
444 cache->accepted = HOSTAPD_ACL_ACCEPT;
446 if (radius_msg_get_attr_int32(
447 msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
448 &cache->acct_interim_interval) == 0 &&
449 cache->acct_interim_interval < 60) {
450 wpa_printf(MSG_DEBUG, "Ignored too small "
451 "Acct-Interim-Interval %d for STA " MACSTR,
452 cache->acct_interim_interval,
453 MAC2STR(query->addr));
454 cache->acct_interim_interval = 0;
457 cache->vlan_id = radius_msg_get_vlanid(msg);
459 cache->accepted = HOSTAPD_ACL_REJECT;
460 cache->next = hapd->acl_cache;
461 hapd->acl_cache = cache;
463 #ifdef CONFIG_DRIVER_RADIUS_ACL
464 hostapd_set_radius_acl_auth(hapd, query->addr, cache->accepted,
465 cache->session_timeout);
466 #else /* CONFIG_DRIVER_RADIUS_ACL */
468 /* Re-send original authentication frame for 802.11 processing */
469 wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
470 "successful RADIUS ACL query");
471 ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len,
472 WLAN_FC_STYPE_AUTH, NULL);
473 #endif /* NEED_MLME */
474 #endif /* CONFIG_DRIVER_RADIUS_ACL */
478 hapd->acl_queries = query->next;
480 prev->next = query->next;
482 hostapd_acl_query_free(query);
484 return RADIUS_RX_PROCESSED;
486 #endif /* CONFIG_NO_RADIUS */
490 * hostapd_acl_init: Initialize IEEE 802.11 ACL
491 * @hapd: hostapd BSS data
492 * Returns: 0 on success, -1 on failure
494 int hostapd_acl_init(struct hostapd_data *hapd)
496 #ifndef CONFIG_NO_RADIUS
497 if (radius_client_register(hapd->radius, RADIUS_AUTH,
498 hostapd_acl_recv_radius, hapd))
501 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
502 #endif /* CONFIG_NO_RADIUS */
509 * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
510 * @hapd: hostapd BSS data
512 void hostapd_acl_deinit(struct hostapd_data *hapd)
514 struct hostapd_acl_query_data *query, *prev;
516 #ifndef CONFIG_NO_RADIUS
517 eloop_cancel_timeout(hostapd_acl_expire, hapd, NULL);
519 hostapd_acl_cache_free(hapd->acl_cache);
520 #endif /* CONFIG_NO_RADIUS */
522 query = hapd->acl_queries;
526 hostapd_acl_query_free(prev);
531 int hostapd_acl_reconfig(struct hostapd_data *hapd,
532 struct hostapd_config *oldconf)
534 if (!hapd->radius_client_reconfigured)
537 hostapd_acl_deinit(hapd);
538 return hostapd_acl_init(hapd);
541 #endif /* CONFIG_NATIVE_WINDOWS */