2 * hostapd / IEEE 802.11 authentication (ACL)
3 * Copyright (c) 2003-2006, 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.
17 #ifndef CONFIG_NATIVE_WINDOWS
20 #include "ieee802_11.h"
21 #include "ieee802_11_auth.h"
23 #include "radius_client.h"
26 #define RADIUS_ACL_TIMEOUT 30
29 struct hostapd_cached_radius_acl
{
32 int accepted
; /* HOSTAPD_ACL_* */
33 struct hostapd_cached_radius_acl
*next
;
35 u32 acct_interim_interval
;
40 struct hostapd_acl_query_data
{
44 u8
*auth_msg
; /* IEEE 802.11 authentication frame from station */
46 struct hostapd_acl_query_data
*next
;
50 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl
*acl_cache
)
52 struct hostapd_cached_radius_acl
*prev
;
56 acl_cache
= acl_cache
->next
;
62 static int hostapd_acl_cache_get(struct hostapd_data
*hapd
, const u8
*addr
,
64 u32
*acct_interim_interval
, int *vlan_id
)
66 struct hostapd_cached_radius_acl
*entry
;
70 entry
= hapd
->acl_cache
;
73 if (memcmp(entry
->addr
, addr
, ETH_ALEN
) == 0) {
74 if (now
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
)
75 return -1; /* entry has expired */
76 if (entry
->accepted
== HOSTAPD_ACL_ACCEPT_TIMEOUT
)
77 *session_timeout
= entry
->session_timeout
;
78 *acct_interim_interval
= entry
->acct_interim_interval
;
80 *vlan_id
= entry
->vlan_id
;
81 return entry
->accepted
;
91 static void hostapd_acl_query_free(struct hostapd_acl_query_data
*query
)
95 free(query
->auth_msg
);
100 static int hostapd_radius_acl_query(struct hostapd_data
*hapd
, const u8
*addr
,
101 struct hostapd_acl_query_data
*query
)
103 struct radius_msg
*msg
;
106 query
->radius_id
= radius_client_get_id(hapd
->radius
);
107 msg
= radius_msg_new(RADIUS_CODE_ACCESS_REQUEST
, query
->radius_id
);
111 radius_msg_make_authenticator(msg
, addr
, ETH_ALEN
);
113 snprintf(buf
, sizeof(buf
), RADIUS_ADDR_FORMAT
, MAC2STR(addr
));
114 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_USER_NAME
, (u8
*) buf
,
116 printf("Could not add User-Name\n");
120 if (!radius_msg_add_attr_user_password(
121 msg
, (u8
*) buf
, strlen(buf
),
122 hapd
->conf
->radius
->auth_server
->shared_secret
,
123 hapd
->conf
->radius
->auth_server
->shared_secret_len
)) {
124 printf("Could not add User-Password\n");
128 if (hapd
->conf
->own_ip_addr
.af
== AF_INET
&&
129 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IP_ADDRESS
,
130 (u8
*) &hapd
->conf
->own_ip_addr
.u
.v4
, 4)) {
131 printf("Could not add NAS-IP-Address\n");
136 if (hapd
->conf
->own_ip_addr
.af
== AF_INET6
&&
137 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IPV6_ADDRESS
,
138 (u8
*) &hapd
->conf
->own_ip_addr
.u
.v6
, 16)) {
139 printf("Could not add NAS-IPv6-Address\n");
142 #endif /* CONFIG_IPV6 */
144 if (hapd
->conf
->nas_identifier
&&
145 !radius_msg_add_attr(msg
, RADIUS_ATTR_NAS_IDENTIFIER
,
146 (u8
*) hapd
->conf
->nas_identifier
,
147 strlen(hapd
->conf
->nas_identifier
))) {
148 printf("Could not add NAS-Identifier\n");
152 snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
":%s",
153 MAC2STR(hapd
->own_addr
), hapd
->conf
->ssid
.ssid
);
154 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CALLED_STATION_ID
,
155 (u8
*) buf
, strlen(buf
))) {
156 printf("Could not add Called-Station-Id\n");
160 snprintf(buf
, sizeof(buf
), RADIUS_802_1X_ADDR_FORMAT
,
162 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CALLING_STATION_ID
,
163 (u8
*) buf
, strlen(buf
))) {
164 printf("Could not add Calling-Station-Id\n");
168 if (!radius_msg_add_attr_int32(msg
, RADIUS_ATTR_NAS_PORT_TYPE
,
169 RADIUS_NAS_PORT_TYPE_IEEE_802_11
)) {
170 printf("Could not add NAS-Port-Type\n");
174 snprintf(buf
, sizeof(buf
), "CONNECT 11Mbps 802.11b");
175 if (!radius_msg_add_attr(msg
, RADIUS_ATTR_CONNECT_INFO
,
176 (u8
*) buf
, strlen(buf
))) {
177 printf("Could not add Connect-Info\n");
181 radius_client_send(hapd
->radius
, msg
, RADIUS_AUTH
, addr
);
185 radius_msg_free(msg
);
191 int hostapd_allowed_address(struct hostapd_data
*hapd
, const u8
*addr
,
192 const u8
*msg
, size_t len
, u32
*session_timeout
,
193 u32
*acct_interim_interval
, int *vlan_id
)
195 *session_timeout
= 0;
196 *acct_interim_interval
= 0;
200 if (hostapd_maclist_found(hapd
->conf
->accept_mac
,
201 hapd
->conf
->num_accept_mac
, addr
))
202 return HOSTAPD_ACL_ACCEPT
;
204 if (hostapd_maclist_found(hapd
->conf
->deny_mac
,
205 hapd
->conf
->num_deny_mac
, addr
))
206 return HOSTAPD_ACL_REJECT
;
208 if (hapd
->conf
->macaddr_acl
== ACCEPT_UNLESS_DENIED
)
209 return HOSTAPD_ACL_ACCEPT
;
210 if (hapd
->conf
->macaddr_acl
== DENY_UNLESS_ACCEPTED
)
211 return HOSTAPD_ACL_REJECT
;
213 if (hapd
->conf
->macaddr_acl
== USE_EXTERNAL_RADIUS_AUTH
) {
214 struct hostapd_acl_query_data
*query
;
216 /* Check whether ACL cache has an entry for this station */
217 int res
= hostapd_acl_cache_get(hapd
, addr
, session_timeout
,
218 acct_interim_interval
,
220 if (res
== HOSTAPD_ACL_ACCEPT
||
221 res
== HOSTAPD_ACL_ACCEPT_TIMEOUT
)
223 if (res
== HOSTAPD_ACL_REJECT
)
224 return HOSTAPD_ACL_REJECT
;
226 query
= hapd
->acl_queries
;
228 if (memcmp(query
->addr
, addr
, ETH_ALEN
) == 0) {
229 /* pending query in RADIUS retransmit queue;
230 * do not generate a new one */
231 return HOSTAPD_ACL_PENDING
;
236 if (!hapd
->conf
->radius
->auth_server
)
237 return HOSTAPD_ACL_REJECT
;
239 /* No entry in the cache - query external RADIUS server */
240 query
= wpa_zalloc(sizeof(*query
));
242 printf("malloc for query data failed\n");
243 return HOSTAPD_ACL_REJECT
;
245 time(&query
->timestamp
);
246 memcpy(query
->addr
, addr
, ETH_ALEN
);
247 if (hostapd_radius_acl_query(hapd
, addr
, query
)) {
248 printf("Failed to send Access-Request for ACL "
250 hostapd_acl_query_free(query
);
251 return HOSTAPD_ACL_REJECT
;
254 query
->auth_msg
= malloc(len
);
255 if (query
->auth_msg
== NULL
) {
256 printf("Failed to allocate memory for auth frame.\n");
257 hostapd_acl_query_free(query
);
258 return HOSTAPD_ACL_REJECT
;
260 memcpy(query
->auth_msg
, msg
, len
);
261 query
->auth_msg_len
= len
;
262 query
->next
= hapd
->acl_queries
;
263 hapd
->acl_queries
= query
;
265 /* Queued data will be processed in hostapd_acl_recv_radius()
266 * when RADIUS server replies to the sent Access-Request. */
267 return HOSTAPD_ACL_PENDING
;
270 return HOSTAPD_ACL_REJECT
;
274 static void hostapd_acl_expire_cache(struct hostapd_data
*hapd
, time_t now
)
276 struct hostapd_cached_radius_acl
*prev
, *entry
, *tmp
;
279 entry
= hapd
->acl_cache
;
282 if (now
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
) {
283 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL
,
284 "Cached ACL entry for " MACSTR
285 " has expired.\n", MAC2STR(entry
->addr
));
287 prev
->next
= entry
->next
;
289 hapd
->acl_cache
= entry
->next
;
303 static void hostapd_acl_expire_queries(struct hostapd_data
*hapd
, time_t now
)
305 struct hostapd_acl_query_data
*prev
, *entry
, *tmp
;
308 entry
= hapd
->acl_queries
;
311 if (now
- entry
->timestamp
> RADIUS_ACL_TIMEOUT
) {
312 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL
,
313 "ACL query for " MACSTR
314 " has expired.\n", MAC2STR(entry
->addr
));
316 prev
->next
= entry
->next
;
318 hapd
->acl_queries
= entry
->next
;
322 hostapd_acl_query_free(tmp
);
332 static void hostapd_acl_expire(void *eloop_ctx
, void *timeout_ctx
)
334 struct hostapd_data
*hapd
= eloop_ctx
;
338 hostapd_acl_expire_cache(hapd
, now
);
339 hostapd_acl_expire_queries(hapd
, now
);
341 eloop_register_timeout(10, 0, hostapd_acl_expire
, hapd
, NULL
);
345 /* Return 0 if RADIUS message was a reply to ACL query (and was processed here)
347 static RadiusRxResult
348 hostapd_acl_recv_radius(struct radius_msg
*msg
, struct radius_msg
*req
,
349 u8
*shared_secret
, size_t shared_secret_len
,
352 struct hostapd_data
*hapd
= data
;
353 struct hostapd_acl_query_data
*query
, *prev
;
354 struct hostapd_cached_radius_acl
*cache
;
356 query
= hapd
->acl_queries
;
359 if (query
->radius_id
== msg
->hdr
->identifier
)
365 return RADIUS_RX_UNKNOWN
;
367 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL
, "Found matching Access-Request "
368 "for RADIUS message (id=%d)\n", query
->radius_id
);
370 if (radius_msg_verify(msg
, shared_secret
, shared_secret_len
, req
, 0)) {
371 printf("Incoming RADIUS packet did not have correct "
372 "authenticator - dropped\n");
373 return RADIUS_RX_INVALID_AUTHENTICATOR
;
376 if (msg
->hdr
->code
!= RADIUS_CODE_ACCESS_ACCEPT
&&
377 msg
->hdr
->code
!= RADIUS_CODE_ACCESS_REJECT
) {
378 printf("Unknown RADIUS message code %d to ACL query\n",
380 return RADIUS_RX_UNKNOWN
;
383 /* Insert Accept/Reject info into ACL cache */
384 cache
= wpa_zalloc(sizeof(*cache
));
386 printf("Failed to add ACL cache entry\n");
389 time(&cache
->timestamp
);
390 memcpy(cache
->addr
, query
->addr
, sizeof(cache
->addr
));
391 if (msg
->hdr
->code
== RADIUS_CODE_ACCESS_ACCEPT
) {
392 if (radius_msg_get_attr_int32(msg
, RADIUS_ATTR_SESSION_TIMEOUT
,
393 &cache
->session_timeout
) == 0)
394 cache
->accepted
= HOSTAPD_ACL_ACCEPT_TIMEOUT
;
396 cache
->accepted
= HOSTAPD_ACL_ACCEPT
;
398 if (radius_msg_get_attr_int32(
399 msg
, RADIUS_ATTR_ACCT_INTERIM_INTERVAL
,
400 &cache
->acct_interim_interval
) == 0 &&
401 cache
->acct_interim_interval
< 60) {
402 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL
, "Ignored too "
403 "small Acct-Interim-Interval %d for "
405 cache
->acct_interim_interval
,
406 MAC2STR(query
->addr
));
407 cache
->acct_interim_interval
= 0;
410 cache
->vlan_id
= radius_msg_get_vlanid(msg
);
412 cache
->accepted
= HOSTAPD_ACL_REJECT
;
413 cache
->next
= hapd
->acl_cache
;
414 hapd
->acl_cache
= cache
;
416 /* Re-send original authentication frame for 802.11 processing */
417 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL
, "Re-sending authentication frame "
418 "after successful RADIUS ACL query\n");
419 ieee802_11_mgmt(hapd
, query
->auth_msg
, query
->auth_msg_len
,
420 WLAN_FC_STYPE_AUTH
, NULL
);
424 hapd
->acl_queries
= query
->next
;
426 prev
->next
= query
->next
;
428 hostapd_acl_query_free(query
);
430 return RADIUS_RX_PROCESSED
;
434 int hostapd_acl_init(struct hostapd_data
*hapd
)
436 if (radius_client_register(hapd
->radius
, RADIUS_AUTH
,
437 hostapd_acl_recv_radius
, hapd
))
440 eloop_register_timeout(10, 0, hostapd_acl_expire
, hapd
, NULL
);
446 void hostapd_acl_deinit(struct hostapd_data
*hapd
)
448 struct hostapd_acl_query_data
*query
, *prev
;
450 eloop_cancel_timeout(hostapd_acl_expire
, hapd
, NULL
);
452 hostapd_acl_cache_free(hapd
->acl_cache
);
454 query
= hapd
->acl_queries
;
458 hostapd_acl_query_free(prev
);
463 int hostapd_acl_reconfig(struct hostapd_data
*hapd
,
464 struct hostapd_config
*oldconf
)
466 if (!hapd
->radius_client_reconfigured
)
469 hostapd_acl_deinit(hapd
);
470 return hostapd_acl_init(hapd
);
473 #endif /* CONFIG_NATIVE_WINDOWS */