]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/ieee802_11_auth.c
acdd527e1adcb6d3379d8b326a64a648dc75af03
[thirdparty/hostap.git] / src / ap / ieee802_11_auth.c
1 /*
2 * hostapd / IEEE 802.11 authentication (ACL)
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 *
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.
12 */
13
14 #include "utils/includes.h"
15
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"
21 #include "hostapd.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"
27
28 #define RADIUS_ACL_TIMEOUT 30
29
30
31 struct hostapd_cached_radius_acl {
32 os_time_t timestamp;
33 macaddr addr;
34 int accepted; /* HOSTAPD_ACL_* */
35 struct hostapd_cached_radius_acl *next;
36 u32 session_timeout;
37 u32 acct_interim_interval;
38 int vlan_id;
39 struct hostapd_sta_wpa_psk_short *psk;
40 char *identity;
41 char *radius_cui;
42 };
43
44
45 struct hostapd_acl_query_data {
46 os_time_t timestamp;
47 u8 radius_id;
48 macaddr addr;
49 u8 *auth_msg; /* IEEE 802.11 authentication frame from station */
50 size_t auth_msg_len;
51 struct hostapd_acl_query_data *next;
52 };
53
54
55 #ifndef CONFIG_NO_RADIUS
56 static void hostapd_acl_cache_free_entry(struct hostapd_cached_radius_acl *e)
57 {
58 struct hostapd_sta_wpa_psk_short *psk = e->psk;
59 os_free(e->identity);
60 os_free(e->radius_cui);
61 while (psk) {
62 struct hostapd_sta_wpa_psk_short *prev = psk;
63 psk = psk->next;
64 os_free(prev);
65 }
66 os_free(e);
67 }
68
69
70 static void hostapd_acl_cache_free(struct hostapd_cached_radius_acl *acl_cache)
71 {
72 struct hostapd_cached_radius_acl *prev;
73
74 while (acl_cache) {
75 prev = acl_cache;
76 acl_cache = acl_cache->next;
77 hostapd_acl_cache_free_entry(prev);
78 }
79 }
80
81
82 static void copy_psk_list(struct hostapd_sta_wpa_psk_short **psk,
83 struct hostapd_sta_wpa_psk_short *src)
84 {
85 struct hostapd_sta_wpa_psk_short **copy_to;
86 struct hostapd_sta_wpa_psk_short *copy_from;
87
88 /* Copy PSK linked list */
89 copy_to = psk;
90 copy_from = src;
91 while (copy_from && copy_to) {
92 *copy_to = os_zalloc(sizeof(struct hostapd_sta_wpa_psk_short));
93 if (*copy_to == NULL)
94 break;
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);
99 }
100 if (copy_to)
101 *copy_to = NULL;
102 }
103
104
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)
110 {
111 struct hostapd_cached_radius_acl *entry;
112 struct os_time now;
113
114 os_get_time(&now);
115
116 for (entry = hapd->acl_cache; entry; entry = entry->next) {
117 if (os_memcmp(entry->addr, addr, ETH_ALEN) != 0)
118 continue;
119
120 if (now.sec - entry->timestamp > RADIUS_ACL_TIMEOUT)
121 return -1; /* entry has expired */
122 if (entry->accepted == HOSTAPD_ACL_ACCEPT_TIMEOUT)
123 if (session_timeout)
124 *session_timeout = entry->session_timeout;
125 if (acct_interim_interval)
126 *acct_interim_interval =
127 entry->acct_interim_interval;
128 if (vlan_id)
129 *vlan_id = entry->vlan_id;
130 copy_psk_list(psk, entry->psk);
131 if (identity) {
132 if (entry->identity)
133 *identity = os_strdup(entry->identity);
134 else
135 *identity = NULL;
136 }
137 if (radius_cui) {
138 if (entry->radius_cui)
139 *radius_cui = os_strdup(entry->radius_cui);
140 else
141 *radius_cui = NULL;
142 }
143 return entry->accepted;
144 }
145
146 return -1;
147 }
148 #endif /* CONFIG_NO_RADIUS */
149
150
151 static void hostapd_acl_query_free(struct hostapd_acl_query_data *query)
152 {
153 if (query == NULL)
154 return;
155 os_free(query->auth_msg);
156 os_free(query);
157 }
158
159
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)
163 {
164 struct radius_msg *msg;
165 char buf[128];
166
167 query->radius_id = radius_client_get_id(hapd->radius);
168 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST, query->radius_id);
169 if (msg == NULL)
170 return -1;
171
172 radius_msg_make_authenticator(msg, addr, ETH_ALEN);
173
174 os_snprintf(buf, sizeof(buf), RADIUS_ADDR_FORMAT, MAC2STR(addr));
175 if (!radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME, (u8 *) buf,
176 os_strlen(buf))) {
177 wpa_printf(MSG_DEBUG, "Could not add User-Name");
178 goto fail;
179 }
180
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");
186 goto fail;
187 }
188
189 if (add_common_radius_attr(hapd, hapd->conf->radius_auth_req_attr,
190 NULL, msg) < 0)
191 goto fail;
192
193 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
194 MAC2STR(addr));
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");
198 goto fail;
199 }
200
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");
205 goto fail;
206 }
207
208 if (radius_client_send(hapd->radius, msg, RADIUS_AUTH, addr) < 0)
209 goto fail;
210 return 0;
211
212 fail:
213 radius_msg_free(msg);
214 return -1;
215 }
216 #endif /* CONFIG_NO_RADIUS */
217
218
219 /**
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
232 *
233 * The caller is responsible for freeing the returned *identity and *radius_cui
234 * values with os_free().
235 */
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)
241 {
242 if (session_timeout)
243 *session_timeout = 0;
244 if (acct_interim_interval)
245 *acct_interim_interval = 0;
246 if (vlan_id)
247 *vlan_id = 0;
248 if (psk)
249 *psk = NULL;
250 if (identity)
251 *identity = NULL;
252 if (radius_cui)
253 *radius_cui = NULL;
254
255 if (hostapd_maclist_found(hapd->conf->accept_mac,
256 hapd->conf->num_accept_mac, addr, vlan_id))
257 return HOSTAPD_ACL_ACCEPT;
258
259 if (hostapd_maclist_found(hapd->conf->deny_mac,
260 hapd->conf->num_deny_mac, addr, vlan_id))
261 return HOSTAPD_ACL_REJECT;
262
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;
267
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;
273 struct os_time t;
274
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,
278 vlan_id, psk,
279 identity, radius_cui);
280 if (res == HOSTAPD_ACL_ACCEPT ||
281 res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
282 return res;
283 if (res == HOSTAPD_ACL_REJECT)
284 return HOSTAPD_ACL_REJECT;
285
286 query = hapd->acl_queries;
287 while (query) {
288 if (os_memcmp(query->addr, addr, ETH_ALEN) == 0) {
289 /* pending query in RADIUS retransmit queue;
290 * do not generate a new one */
291 if (identity) {
292 os_free(*identity);
293 *identity = NULL;
294 }
295 if (radius_cui) {
296 os_free(*radius_cui);
297 *radius_cui = NULL;
298 }
299 return HOSTAPD_ACL_PENDING;
300 }
301 query = query->next;
302 }
303
304 if (!hapd->conf->radius->auth_server)
305 return HOSTAPD_ACL_REJECT;
306
307 /* No entry in the cache - query external RADIUS server */
308 query = os_zalloc(sizeof(*query));
309 if (query == NULL) {
310 wpa_printf(MSG_ERROR, "malloc for query data failed");
311 return HOSTAPD_ACL_REJECT;
312 }
313 os_get_time(&t);
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 "
318 "for ACL query.");
319 hostapd_acl_query_free(query);
320 return HOSTAPD_ACL_REJECT;
321 }
322
323 query->auth_msg = os_malloc(len);
324 if (query->auth_msg == NULL) {
325 wpa_printf(MSG_ERROR, "Failed to allocate memory for "
326 "auth frame.");
327 hostapd_acl_query_free(query);
328 return HOSTAPD_ACL_REJECT;
329 }
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;
334
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 */
339 }
340
341 return HOSTAPD_ACL_REJECT;
342 }
343
344
345 #ifndef CONFIG_NO_RADIUS
346 static void hostapd_acl_expire_cache(struct hostapd_data *hapd, os_time_t now)
347 {
348 struct hostapd_cached_radius_acl *prev, *entry, *tmp;
349
350 prev = NULL;
351 entry = hapd->acl_cache;
352
353 while (entry) {
354 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
355 wpa_printf(MSG_DEBUG, "Cached ACL entry for " MACSTR
356 " has expired.", MAC2STR(entry->addr));
357 if (prev)
358 prev->next = entry->next;
359 else
360 hapd->acl_cache = entry->next;
361 hostapd_drv_set_radius_acl_expire(hapd, entry->addr);
362 tmp = entry;
363 entry = entry->next;
364 hostapd_acl_cache_free_entry(tmp);
365 continue;
366 }
367
368 prev = entry;
369 entry = entry->next;
370 }
371 }
372
373
374 static void hostapd_acl_expire_queries(struct hostapd_data *hapd,
375 os_time_t now)
376 {
377 struct hostapd_acl_query_data *prev, *entry, *tmp;
378
379 prev = NULL;
380 entry = hapd->acl_queries;
381
382 while (entry) {
383 if (now - entry->timestamp > RADIUS_ACL_TIMEOUT) {
384 wpa_printf(MSG_DEBUG, "ACL query for " MACSTR
385 " has expired.", MAC2STR(entry->addr));
386 if (prev)
387 prev->next = entry->next;
388 else
389 hapd->acl_queries = entry->next;
390
391 tmp = entry;
392 entry = entry->next;
393 hostapd_acl_query_free(tmp);
394 continue;
395 }
396
397 prev = entry;
398 entry = entry->next;
399 }
400 }
401
402
403 /**
404 * hostapd_acl_expire - ACL cache expiration callback
405 * @eloop_ctx: struct hostapd_data *
406 * @timeout_ctx: Not used
407 */
408 static void hostapd_acl_expire(void *eloop_ctx, void *timeout_ctx)
409 {
410 struct hostapd_data *hapd = eloop_ctx;
411 struct os_time now;
412
413 os_get_time(&now);
414 hostapd_acl_expire_cache(hapd, now.sec);
415 hostapd_acl_expire_queries(hapd, now.sec);
416
417 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
418 }
419
420
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)
427 {
428 int passphraselen;
429 char *passphrase, *strpassphrase;
430 size_t i;
431 struct hostapd_sta_wpa_psk_short *psk;
432
433 /*
434 * Decode all tunnel passwords as PSK and save them into a linked list.
435 */
436 for (i = 0; ; i++) {
437 passphrase = radius_msg_get_tunnel_password(
438 msg, &passphraselen, shared_secret, shared_secret_len,
439 req, i);
440 /*
441 * Passphrase is NULL iff there is no i-th Tunnel-Password
442 * attribute in msg.
443 */
444 if (passphrase == NULL)
445 break;
446 /*
447 * passphrase does not contain the NULL termination.
448 * Add it here as pbkdf2_sha1() requires it.
449 */
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,
457 psk->psk, PMK_LEN);
458 psk->next = cache->psk;
459 cache->psk = psk;
460 psk = NULL;
461 }
462 os_free(strpassphrase);
463 os_free(psk);
464 os_free(passphrase);
465 }
466 }
467
468
469 /**
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.
478 */
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,
482 void *data)
483 {
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);
488 struct os_time t;
489
490 query = hapd->acl_queries;
491 prev = NULL;
492 while (query) {
493 if (query->radius_id == hdr->identifier)
494 break;
495 prev = query;
496 query = query->next;
497 }
498 if (query == NULL)
499 return RADIUS_RX_UNKNOWN;
500
501 wpa_printf(MSG_DEBUG, "Found matching Access-Request for RADIUS "
502 "message (id=%d)", query->radius_id);
503
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;
508 }
509
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 "
513 "query", hdr->code);
514 return RADIUS_RX_UNKNOWN;
515 }
516
517 /* Insert Accept/Reject info into ACL cache */
518 cache = os_zalloc(sizeof(*cache));
519 if (cache == NULL) {
520 wpa_printf(MSG_DEBUG, "Failed to add ACL cache entry");
521 goto done;
522 }
523 os_get_time(&t);
524 cache->timestamp = t.sec;
525 os_memcpy(cache->addr, query->addr, sizeof(cache->addr));
526 if (hdr->code == RADIUS_CODE_ACCESS_ACCEPT) {
527 u8 *buf;
528 size_t len;
529
530 if (radius_msg_get_attr_int32(msg, RADIUS_ATTR_SESSION_TIMEOUT,
531 &cache->session_timeout) == 0)
532 cache->accepted = HOSTAPD_ACL_ACCEPT_TIMEOUT;
533 else
534 cache->accepted = HOSTAPD_ACL_ACCEPT;
535
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;
545 }
546
547 cache->vlan_id = radius_msg_get_vlanid(msg);
548
549 decode_tunnel_passwords(hapd, shared_secret, shared_secret_len,
550 msg, req, cache);
551
552 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME,
553 &buf, &len, NULL) == 0) {
554 cache->identity = os_zalloc(len + 1);
555 if (cache->identity)
556 os_memcpy(cache->identity, buf, len);
557 }
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);
564 }
565
566 if (hapd->conf->wpa_psk_radius == PSK_RADIUS_REQUIRED &&
567 !cache->psk)
568 cache->accepted = HOSTAPD_ACL_REJECT;
569 } else
570 cache->accepted = HOSTAPD_ACL_REJECT;
571 cache->next = hapd->acl_cache;
572 hapd->acl_cache = cache;
573
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 */
578 #ifdef NEED_AP_MLME
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 */
585
586 done:
587 if (prev == NULL)
588 hapd->acl_queries = query->next;
589 else
590 prev->next = query->next;
591
592 hostapd_acl_query_free(query);
593
594 return RADIUS_RX_PROCESSED;
595 }
596 #endif /* CONFIG_NO_RADIUS */
597
598
599 /**
600 * hostapd_acl_init: Initialize IEEE 802.11 ACL
601 * @hapd: hostapd BSS data
602 * Returns: 0 on success, -1 on failure
603 */
604 int hostapd_acl_init(struct hostapd_data *hapd)
605 {
606 #ifndef CONFIG_NO_RADIUS
607 if (radius_client_register(hapd->radius, RADIUS_AUTH,
608 hostapd_acl_recv_radius, hapd))
609 return -1;
610
611 eloop_register_timeout(10, 0, hostapd_acl_expire, hapd, NULL);
612 #endif /* CONFIG_NO_RADIUS */
613
614 return 0;
615 }
616
617
618 /**
619 * hostapd_acl_deinit - Deinitialize IEEE 802.11 ACL
620 * @hapd: hostapd BSS data
621 */
622 void hostapd_acl_deinit(struct hostapd_data *hapd)
623 {
624 struct hostapd_acl_query_data *query, *prev;
625
626 #ifndef CONFIG_NO_RADIUS
627 eloop_cancel_timeout(hostapd_acl_expire, hapd, NULL);
628
629 hostapd_acl_cache_free(hapd->acl_cache);
630 #endif /* CONFIG_NO_RADIUS */
631
632 query = hapd->acl_queries;
633 while (query) {
634 prev = query;
635 query = query->next;
636 hostapd_acl_query_free(prev);
637 }
638 }