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"
32 #define RADIUS_ACL_TIMEOUT 30
35 struct hostapd_cached_radius_acl {
38 int accepted; /* HOSTAPD_ACL_* */
39 struct hostapd_cached_radius_acl *next;
41 u32 acct_interim_interval;
46 struct hostapd_acl_query_data {
50 u8 *auth_msg; /* IEEE 802.11 authentication frame from station */
52 struct hostapd_acl_query_data *next;
56 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
58 struct hostapd_cached_radius_acl *prev;
62 acl_cache = acl_cache->next;
68 static int hostapd_acl_cache_get(struct hostapd_data *hapd, const u8 *addr,
70 u32 *acct_interim_interval, int *vlan_id)
72 struct hostapd_cached_radius_acl *entry;
76 entry = hapd->acl_cache;
79 if (os_memcmp(entry->addr, addr, ETH_ALEN) == 0) {
80 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT)
81 return -1; /* entry has expired */
82 if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
85 entry->session_timeout;
86 if (acct_interim_interval)
87 *acct_interim_interval =
88 entry->acct_interim_interval;
90 *vlan_id = entry->vlan_id;
91 return entry->accepted;
101 static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
105 os_free(query->auth_msg);
110 static int hostapd_radius_acl_query(struct hostapd_data *hapd, const u8 *addr,
111 struct hostapd_acl_query_data *query)
113 struct radius_msg *msg;
116 query->radius_id = radius_client_get_id(hapd->radius);
117 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
121 radius_msg_make_authenticator(msg, addr, ETH_ALEN);
123 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
124 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
126 wpa_printf(MSG_DEBUG, "Could not add User-Name");
130 if (!radius_msg_add_attr_user_password(
131 msg, (u8 *) buf, os_strlen(buf),
132 hapd->conf->radius->auth_server->shared_secret,
133 hapd->conf->radius->auth_server->shared_secret_len)) {
134 wpa_printf(MSG_DEBUG, "Could not add User-Password");
138 if (hapd->conf->own_ip_addr.af == AF_INET &&
139 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
140 (u8 *) &hapd->conf->own_ip_addr.u.v4, 4)) {
141 wpa_printf(MSG_DEBUG, "Could not add NAS-IP-Address");
146 if (hapd->conf->own_ip_addr.af == AF_INET6 &&
147 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IPV6_ADDRESS,
148 (u8 *) &hapd->conf->own_ip_addr.u.v6, 16)) {
149 wpa_printf(MSG_DEBUG, "Could not add NAS-IPv6-Address");
152 #endif /* CONFIG_IPV6 */
154 if (hapd->conf->nas_identifier &&
155 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IDENTIFIER,
156 (u8 *) hapd->conf->nas_identifier,
157 os_strlen(hapd->conf->nas_identifier))) {
158 wpa_printf(MSG_DEBUG, "Could not add NAS-Identifier");
162 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT ":%s",
163 MAC2STR(hapd->own_addr), hapd->conf->ssid.ssid);
164 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLED_STATION_ID,
165 (u8 *) buf, os_strlen(buf))) {
166 wpa_printf(MSG_DEBUG, "Could not add Called-Station-Id");
170 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
172 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
173 (u8 *) buf, os_strlen(buf))) {
174 wpa_printf(MSG_DEBUG, "Could not add Calling-Station-Id");
178 if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
179 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
180 wpa_printf(MSG_DEBUG, "Could not add NAS-Port-Type");
184 os_snprintf(buf, sizeof(buf), "CONNECT 11Mbps 802.11b");
185 if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
186 (u8 *) buf, os_strlen(buf))) {
187 wpa_printf(MSG_DEBUG, "Could not add Connect-Info");
191 radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr);
195 radius_msg_free(msg);
202 * hostapd_allowed_address - Check whether a specified STA can be authenticated
203 * @hapd: hostapd BSS data
204 * @addr: MAC address of the STA
205 * @msg: Authentication message
206 * @len: Length of msg in octets
207 * @session_timeout: Buffer for returning session timeout (from RADIUS)
208 * @acct_interim_interval: Buffer for returning account interval (from RADIUS)
209 * @vlan_id: Buffer for returning VLAN ID
210 * Returns: HOSTAPD_ACL_ACCEPT, HOSTAPD_ACL_REJECT, or HOSTAPD_ACL_PENDING
212 int hostapd_allowed_address(struct hostapd_data *hapd, const u8 *addr,
213 const u8 *msg, size_t len, u32 *session_timeout,
214 u32 *acct_interim_interval, int *vlan_id)
217 *session_timeout = 0;
218 if (acct_interim_interval)
219 *acct_interim_interval = 0;
223 if (hostapd_maclist_found(hapd->conf->accept_mac,
224 hapd->conf->num_accept_mac, addr, vlan_id))
225 return HOSTAPD_ACL_ACCEPT;
227 if (hostapd_maclist_found(hapd->conf->deny_mac,
228 hapd->conf->num_deny_mac, addr, vlan_id))
229 return HOSTAPD_ACL_REJECT;
231 if (hapd->conf->macaddr_acl == ACCEPT_UNLESS_DENIED)
232 return HOSTAPD_ACL_ACCEPT;
233 if (hapd->conf->macaddr_acl == DENY_UNLESS_ACCEPTED)
234 return HOSTAPD_ACL_REJECT;
236 if (hapd->conf->macaddr_acl == USE_EXTERNAL_RADIUS_AUTH) {
237 struct hostapd_acl_query_data *query;
239 /* Check whether ACL cache has an entry for this station */
240 int res = hostapd_acl_cache_get(hapd, addr, session_timeout,
241 acct_interim_interval,
243 if (res == HOSTAPD_ACL_ACCEPT ||
244 res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
246 if (res == HOSTAPD_ACL_REJECT)
247 return HOSTAPD_ACL_REJECT;
249 query = hapd->acl_queries;
251 if (os_memcmp(query->addr, addr, ETH_ALEN) == 0) {
252 /* pending query in RADIUS retransmit queue;
253 * do not generate a new one */
254 return HOSTAPD_ACL_PENDING;
259 if (!hapd->conf->radius->auth_server)
260 return HOSTAPD_ACL_REJECT;
262 /* No entry in the cache - query external RADIUS server */
263 query = os_zalloc(sizeof(*query));
265 wpa_printf(MSG_ERROR, "malloc for query data failed");
266 return HOSTAPD_ACL_REJECT;
268 time(&query->timestamp);
269 os_memcpy(query->addr, addr, ETH_ALEN);
270 if (hostapd_radius_acl_query(hapd, addr, query)) {
271 wpa_printf(MSG_DEBUG, "Failed to send Access-Request "
273 hostapd_acl_query_free(query);
274 return HOSTAPD_ACL_REJECT;
277 query->auth_msg = os_malloc(len);
278 if (query->auth_msg == NULL) {
279 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
281 hostapd_acl_query_free(query);
282 return HOSTAPD_ACL_REJECT;
284 os_memcpy(query->auth_msg, msg, len);
285 query->auth_msg_len = len;
286 query->next = hapd->acl_queries;
287 hapd->acl_queries = query;
289 /* Queued data will be processed in hostapd_acl_recv_radius()
290 * when RADIUS server replies to the sent Access-Request. */
291 return HOSTAPD_ACL_PENDING;
294 return HOSTAPD_ACL_REJECT;
298 static void hostapd_acl_expire_cache(struct hostapd_data *hapd, time_t now)
300 struct hostapd_cached_radius_acl *prev, *entry, *tmp;
303 entry = hapd->acl_cache;
306 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
307 wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
308 " has expired.", MAC2STR(entry->addr));
310 prev->next = entry->next;
312 hapd->acl_cache = entry->next;
313 #ifdef CONFIG_DRIVER_RADIUS_ACL
314 hostapd_set_radius_acl_expire(hapd, entry->addr);
315 #endif /* CONFIG_DRIVER_RADIUS_ACL */
328 static void hostapd_acl_expire_queries(struct hostapd_data *hapd, time_t now)
330 struct hostapd_acl_query_data *prev, *entry, *tmp;
333 entry = hapd->acl_queries;
336 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
337 wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
338 " has expired.", MAC2STR(entry->addr));
340 prev->next = entry->next;
342 hapd->acl_queries = entry->next;
346 hostapd_acl_query_free(tmp);
357 * hostapd_acl_expire - ACL cache expiration callback
358 * @eloop_ctx: struct hostapd_data *
359 * @timeout_ctx: Not used
361 static void hostapd_acl_expire(void *eloop_ctx, void *timeout_ctx)
363 struct hostapd_data *hapd = eloop_ctx;
367 hostapd_acl_expire_cache(hapd, now);
368 hostapd_acl_expire_queries(hapd, now);
370 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
375 * hostapd_acl_recv_radius - Process incoming RADIUS Authentication messages
376 * @msg: RADIUS response message
377 * @req: RADIUS request message
378 * @shared_secret: RADIUS shared secret
379 * @shared_secret_len: Length of shared_secret in octets
380 * @data: Context data (struct hostapd_data *)
381 * Returns: RADIUS_RX_PROCESSED if RADIUS message was a reply to ACL query (and
382 * was processed here) or RADIUS_RX_UNKNOWN if not.
384 static RadiusRxResult
385 hostapd_acl_recv_radius(struct radius_msg *msg, struct radius_msg *req,
386 const u8 *shared_secret, size_t shared_secret_len,
389 struct hostapd_data *hapd = data;
390 struct hostapd_acl_query_data *query, *prev;
391 struct hostapd_cached_radius_acl *cache;
393 query = hapd->acl_queries;
396 if (query->radius_id == msg->hdr->identifier)
402 return RADIUS_RX_UNKNOWN;
404 wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
405 "message (id=%d)", query->radius_id);
407 if (radius_msg_verify(msg, shared_secret, shared_secret_len, req, 0)) {
408 wpa_printf(MSG_INFO, "Incoming RADIUS packet did not have "
409 "correct authenticator - dropped\n");
410 return RADIUS_RX_INVALID_AUTHENTICATOR;
413 if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
414 msg->hdr->code != RADIUS_CODE_ACCESS_REJECT) {
415 wpa_printf(MSG_DEBUG, "Unknown RADIUS message code %d to ACL "
416 "query", msg->hdr->code);
417 return RADIUS_RX_UNKNOWN;
420 /* Insert Accept/Reject info into ACL cache */
421 cache = os_zalloc(sizeof(*cache));
423 wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
426 time(&cache->timestamp);
427 os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
428 if (msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
429 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
430 &cache->session_timeout) == 0)
431 cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
433 cache->accepted = HOSTAPD_ACL_ACCEPT;
435 if (radius_msg_get_attr_int32(
436 msg, RADIUS_ATTR_ACCT_INTERIM_INTERVAL,
437 &cache->acct_interim_interval) == 0 &&
438 cache->acct_interim_interval < 60) {
439 wpa_printf(MSG_DEBUG, "Ignored too small "
440 "Acct-Interim-Interval %d for STA " MACSTR,
441 cache->acct_interim_interval,
442 MAC2STR(query->addr));
443 cache->acct_interim_interval = 0;
446 cache->vlan_id = radius_msg_get_vlanid(msg);
448 cache->accepted = HOSTAPD_ACL_REJECT;
449 cache->next = hapd->acl_cache;
450 hapd->acl_cache = cache;
452 #ifdef CONFIG_DRIVER_RADIUS_ACL
453 hostapd_set_radius_acl_auth(hapd, query->addr, cache->accepted,
454 cache->session_timeout);
455 #else /* CONFIG_DRIVER_RADIUS_ACL */
456 /* Re-send original authentication frame for 802.11 processing */
457 wpa_printf(MSG_DEBUG, "Re-sending authentication frame after "
458 "successful RADIUS ACL query");
459 ieee802_11_mgmt(hapd, query->auth_msg, query->auth_msg_len,
460 WLAN_FC_STYPE_AUTH, NULL);
461 #endif /* CONFIG_DRIVER_RADIUS_ACL */
465 hapd->acl_queries = query->next;
467 prev->next = query->next;
469 hostapd_acl_query_free(query);
471 return RADIUS_RX_PROCESSED;
476 * hostapd_acl_init: Initialize IEEE 802.11 ACL
477 * @hapd: hostapd BSS data
478 * Returns: 0 on success, -1 on failure
480 int hostapd_acl_init(struct hostapd_data *hapd)
482 if (radius_client_register(hapd->radius, RADIUS_AUTH,
483 hostapd_acl_recv_radius, hapd))
486 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
493 * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
494 * @hapd: hostapd BSS data
496 void hostapd_acl_deinit(struct hostapd_data *hapd)
498 struct hostapd_acl_query_data *query, *prev;
500 eloop_cancel_timeout(hostapd_acl_expire, hapd, NULL);
502 hostapd_acl_cache_free(hapd->acl_cache);
504 query = hapd->acl_queries;
508 hostapd_acl_query_free(prev);
513 int hostapd_acl_reconfig(struct hostapd_data *hapd,
514 struct hostapd_config *oldconf)
516 if (!hapd->radius_client_reconfigured)
519 hostapd_acl_deinit(hapd);
520 return hostapd_acl_init(hapd);
523 #endif /* CONFIG_NATIVE_WINDOWS */