nl80211: Fix a typo
[hostap-gosc2009.git] / src / radius / radius_server.c
blobb1790c41d31f045eb04ecb99852f1a5843dcba57
1 /*
2 * RADIUS authentication server
3 * Copyright (c) 2005-2009, 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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <net/if.h>
18 #include "common.h"
19 #include "radius.h"
20 #include "eloop.h"
21 #include "eap_server/eap.h"
22 #include "radius_server.h"
24 /**
25 * RADIUS_SESSION_TIMEOUT - Session timeout in seconds
27 #define RADIUS_SESSION_TIMEOUT 60
29 /**
30 * RADIUS_MAX_SESSION - Maximum number of active sessions
32 #define RADIUS_MAX_SESSION 100
34 /**
35 * RADIUS_MAX_MSG_LEN - Maximum message length for incoming RADIUS messages
37 #define RADIUS_MAX_MSG_LEN 3000
39 static struct eapol_callbacks radius_server_eapol_cb;
41 struct radius_client;
42 struct radius_server_data;
44 /**
45 * struct radius_server_counters - RADIUS server statistics counters
47 struct radius_server_counters {
48 u32 access_requests;
49 u32 invalid_requests;
50 u32 dup_access_requests;
51 u32 access_accepts;
52 u32 access_rejects;
53 u32 access_challenges;
54 u32 malformed_access_requests;
55 u32 bad_authenticators;
56 u32 packets_dropped;
57 u32 unknown_types;
60 /**
61 * struct radius_session - Internal RADIUS server data for a session
63 struct radius_session {
64 struct radius_session *next;
65 struct radius_client *client;
66 struct radius_server_data *server;
67 unsigned int sess_id;
68 struct eap_sm *eap;
69 struct eap_eapol_interface *eap_if;
71 struct radius_msg *last_msg;
72 char *last_from_addr;
73 int last_from_port;
74 struct sockaddr_storage last_from;
75 socklen_t last_fromlen;
76 u8 last_identifier;
77 struct radius_msg *last_reply;
78 u8 last_authenticator[16];
81 /**
82 * struct radius_client - Internal RADIUS server data for a client
84 struct radius_client {
85 struct radius_client *next;
86 struct in_addr addr;
87 struct in_addr mask;
88 #ifdef CONFIG_IPV6
89 struct in6_addr addr6;
90 struct in6_addr mask6;
91 #endif /* CONFIG_IPV6 */
92 char *shared_secret;
93 int shared_secret_len;
94 struct radius_session *sessions;
95 struct radius_server_counters counters;
98 /**
99 * struct radius_server_data - Internal RADIUS server data
101 struct radius_server_data {
103 * auth_sock - Socket for RADIUS authentication messages
105 int auth_sock;
108 * clients - List of authorized RADIUS clients
110 struct radius_client *clients;
113 * next_sess_id - Next session identifier
115 unsigned int next_sess_id;
118 * conf_ctx - Context pointer for callbacks
120 * This is used as the ctx argument in get_eap_user() calls.
122 void *conf_ctx;
125 * num_sess - Number of active sessions
127 int num_sess;
130 * eap_sim_db_priv - EAP-SIM/AKA database context
132 * This is passed to the EAP-SIM/AKA server implementation as a
133 * callback context.
135 void *eap_sim_db_priv;
138 * ssl_ctx - TLS context
140 * This is passed to the EAP server implementation as a callback
141 * context for TLS operations.
143 void *ssl_ctx;
146 * pac_opaque_encr_key - PAC-Opaque encryption key for EAP-FAST
148 * This parameter is used to set a key for EAP-FAST to encrypt the
149 * PAC-Opaque data. It can be set to %NULL if EAP-FAST is not used. If
150 * set, must point to a 16-octet key.
152 u8 *pac_opaque_encr_key;
155 * eap_fast_a_id - EAP-FAST authority identity (A-ID)
157 * If EAP-FAST is not used, this can be set to %NULL. In theory, this
158 * is a variable length field, but due to some existing implementations
159 * requiring A-ID to be 16 octets in length, it is recommended to use
160 * that length for the field to provide interoperability with deployed
161 * peer implementations.
163 u8 *eap_fast_a_id;
166 * eap_fast_a_id_len - Length of eap_fast_a_id buffer in octets
168 size_t eap_fast_a_id_len;
171 * eap_fast_a_id_info - EAP-FAST authority identifier information
173 * This A-ID-Info contains a user-friendly name for the A-ID. For
174 * example, this could be the enterprise and server names in
175 * human-readable format. This field is encoded as UTF-8. If EAP-FAST
176 * is not used, this can be set to %NULL.
178 char *eap_fast_a_id_info;
181 * eap_fast_prov - EAP-FAST provisioning modes
183 * 0 = provisioning disabled, 1 = only anonymous provisioning allowed,
184 * 2 = only authenticated provisioning allowed, 3 = both provisioning
185 * modes allowed.
187 int eap_fast_prov;
190 * pac_key_lifetime - EAP-FAST PAC-Key lifetime in seconds
192 * This is the hard limit on how long a provisioned PAC-Key can be
193 * used.
195 int pac_key_lifetime;
198 * pac_key_refresh_time - EAP-FAST PAC-Key refresh time in seconds
200 * This is a soft limit on the PAC-Key. The server will automatically
201 * generate a new PAC-Key when this number of seconds (or fewer) of the
202 * lifetime remains.
204 int pac_key_refresh_time;
207 * eap_sim_aka_result_ind - EAP-SIM/AKA protected success indication
209 * This controls whether the protected success/failure indication
210 * (AT_RESULT_IND) is used with EAP-SIM and EAP-AKA.
212 int eap_sim_aka_result_ind;
215 * tnc - Trusted Network Connect (TNC)
217 * This controls whether TNC is enabled and will be required before the
218 * peer is allowed to connect. Note: This is only used with EAP-TTLS
219 * and EAP-FAST. If any other EAP method is enabled, the peer will be
220 * allowed to connect without TNC.
222 int tnc;
225 * wps - Wi-Fi Protected Setup context
227 * If WPS is used with an external RADIUS server (which is quite
228 * unlikely configuration), this is used to provide a pointer to WPS
229 * context data. Normally, this can be set to %NULL.
231 struct wps_context *wps;
234 * ipv6 - Whether to enable IPv6 support in the RADIUS server
236 int ipv6;
239 * start_time - Timestamp of server start
241 struct os_time start_time;
244 * counters - Statistics counters for server operations
246 * These counters are the sum over all clients.
248 struct radius_server_counters counters;
251 * get_eap_user - Callback for fetching EAP user information
252 * @ctx: Context data from conf_ctx
253 * @identity: User identity
254 * @identity_len: identity buffer length in octets
255 * @phase2: Whether this is for Phase 2 identity
256 * @user: Data structure for filling in the user information
257 * Returns: 0 on success, -1 on failure
259 * This is used to fetch information from user database. The callback
260 * will fill in information about allowed EAP methods and the user
261 * password. The password field will be an allocated copy of the
262 * password data and RADIUS server will free it after use.
264 int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
265 int phase2, struct eap_user *user);
268 * eap_req_id_text - Optional data for EAP-Request/Identity
270 * This can be used to configure an optional, displayable message that
271 * will be sent in EAP-Request/Identity. This string can contain an
272 * ASCII-0 character (nul) to separate network infromation per RFC
273 * 4284. The actual string length is explicit provided in
274 * eap_req_id_text_len since nul character will not be used as a string
275 * terminator.
277 char *eap_req_id_text;
280 * eap_req_id_text_len - Length of eap_req_id_text buffer in octets
282 size_t eap_req_id_text_len;
286 extern int wpa_debug_level;
288 #define RADIUS_DEBUG(args...) \
289 wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
290 #define RADIUS_ERROR(args...) \
291 wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
292 #define RADIUS_DUMP(args...) \
293 wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
294 #define RADIUS_DUMP_ASCII(args...) \
295 wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
298 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
299 static void radius_server_session_remove_timeout(void *eloop_ctx,
300 void *timeout_ctx);
303 static struct radius_client *
304 radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
305 int ipv6)
307 struct radius_client *client = data->clients;
309 while (client) {
310 #ifdef CONFIG_IPV6
311 if (ipv6) {
312 struct in6_addr *addr6;
313 int i;
315 addr6 = (struct in6_addr *) addr;
316 for (i = 0; i < 16; i++) {
317 if ((addr6->s6_addr[i] &
318 client->mask6.s6_addr[i]) !=
319 (client->addr6.s6_addr[i] &
320 client->mask6.s6_addr[i])) {
321 i = 17;
322 break;
325 if (i == 16) {
326 break;
329 #endif /* CONFIG_IPV6 */
330 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
331 (addr->s_addr & client->mask.s_addr)) {
332 break;
335 client = client->next;
338 return client;
342 static struct radius_session *
343 radius_server_get_session(struct radius_client *client, unsigned int sess_id)
345 struct radius_session *sess = client->sessions;
347 while (sess) {
348 if (sess->sess_id == sess_id) {
349 break;
351 sess = sess->next;
354 return sess;
358 static void radius_server_session_free(struct radius_server_data *data,
359 struct radius_session *sess)
361 eloop_cancel_timeout(radius_server_session_timeout, data, sess);
362 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
363 eap_server_sm_deinit(sess->eap);
364 radius_msg_free(sess->last_msg);
365 os_free(sess->last_from_addr);
366 radius_msg_free(sess->last_reply);
367 os_free(sess);
368 data->num_sess--;
372 static void radius_server_session_remove(struct radius_server_data *data,
373 struct radius_session *sess)
375 struct radius_client *client = sess->client;
376 struct radius_session *session, *prev;
378 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
380 prev = NULL;
381 session = client->sessions;
382 while (session) {
383 if (session == sess) {
384 if (prev == NULL) {
385 client->sessions = sess->next;
386 } else {
387 prev->next = sess->next;
389 radius_server_session_free(data, sess);
390 break;
392 prev = session;
393 session = session->next;
398 static void radius_server_session_remove_timeout(void *eloop_ctx,
399 void *timeout_ctx)
401 struct radius_server_data *data = eloop_ctx;
402 struct radius_session *sess = timeout_ctx;
403 RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
404 radius_server_session_remove(data, sess);
408 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
410 struct radius_server_data *data = eloop_ctx;
411 struct radius_session *sess = timeout_ctx;
413 RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
414 radius_server_session_remove(data, sess);
418 static struct radius_session *
419 radius_server_new_session(struct radius_server_data *data,
420 struct radius_client *client)
422 struct radius_session *sess;
424 if (data->num_sess >= RADIUS_MAX_SESSION) {
425 RADIUS_DEBUG("Maximum number of existing session - no room "
426 "for a new session");
427 return NULL;
430 sess = os_zalloc(sizeof(*sess));
431 if (sess == NULL)
432 return NULL;
434 sess->server = data;
435 sess->client = client;
436 sess->sess_id = data->next_sess_id++;
437 sess->next = client->sessions;
438 client->sessions = sess;
439 eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
440 radius_server_session_timeout, data, sess);
441 data->num_sess++;
442 return sess;
446 static struct radius_session *
447 radius_server_get_new_session(struct radius_server_data *data,
448 struct radius_client *client,
449 struct radius_msg *msg)
451 u8 *user;
452 size_t user_len;
453 int res;
454 struct radius_session *sess;
455 struct eap_config eap_conf;
457 RADIUS_DEBUG("Creating a new session");
459 user = os_malloc(256);
460 if (user == NULL) {
461 return NULL;
463 res = radius_msg_get_attr(msg, RADIUS_ATTR_USER_NAME, user, 256);
464 if (res < 0 || res > 256) {
465 RADIUS_DEBUG("Could not get User-Name");
466 os_free(user);
467 return NULL;
469 user_len = res;
470 RADIUS_DUMP_ASCII("User-Name", user, user_len);
472 res = data->get_eap_user(data->conf_ctx, user, user_len, 0, NULL);
473 os_free(user);
475 if (res == 0) {
476 RADIUS_DEBUG("Matching user entry found");
477 sess = radius_server_new_session(data, client);
478 if (sess == NULL) {
479 RADIUS_DEBUG("Failed to create a new session");
480 return NULL;
482 } else {
483 RADIUS_DEBUG("User-Name not found from user database");
484 return NULL;
487 os_memset(&eap_conf, 0, sizeof(eap_conf));
488 eap_conf.ssl_ctx = data->ssl_ctx;
489 eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
490 eap_conf.backend_auth = TRUE;
491 eap_conf.eap_server = 1;
492 eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
493 eap_conf.eap_fast_a_id = data->eap_fast_a_id;
494 eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
495 eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
496 eap_conf.eap_fast_prov = data->eap_fast_prov;
497 eap_conf.pac_key_lifetime = data->pac_key_lifetime;
498 eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
499 eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
500 eap_conf.tnc = data->tnc;
501 eap_conf.wps = data->wps;
502 sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
503 &eap_conf);
504 if (sess->eap == NULL) {
505 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
506 "new session");
507 radius_server_session_free(data, sess);
508 return NULL;
510 sess->eap_if = eap_get_interface(sess->eap);
511 sess->eap_if->eapRestart = TRUE;
512 sess->eap_if->portEnabled = TRUE;
514 RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
516 return sess;
520 static struct radius_msg *
521 radius_server_encapsulate_eap(struct radius_server_data *data,
522 struct radius_client *client,
523 struct radius_session *sess,
524 struct radius_msg *request)
526 struct radius_msg *msg;
527 int code;
528 unsigned int sess_id;
529 struct radius_hdr *hdr = radius_msg_get_hdr(request);
531 if (sess->eap_if->eapFail) {
532 sess->eap_if->eapFail = FALSE;
533 code = RADIUS_CODE_ACCESS_REJECT;
534 } else if (sess->eap_if->eapSuccess) {
535 sess->eap_if->eapSuccess = FALSE;
536 code = RADIUS_CODE_ACCESS_ACCEPT;
537 } else {
538 sess->eap_if->eapReq = FALSE;
539 code = RADIUS_CODE_ACCESS_CHALLENGE;
542 msg = radius_msg_new(code, hdr->identifier);
543 if (msg == NULL) {
544 RADIUS_DEBUG("Failed to allocate reply message");
545 return NULL;
548 sess_id = htonl(sess->sess_id);
549 if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
550 !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
551 (u8 *) &sess_id, sizeof(sess_id))) {
552 RADIUS_DEBUG("Failed to add State attribute");
555 if (sess->eap_if->eapReqData &&
556 !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
557 wpabuf_len(sess->eap_if->eapReqData))) {
558 RADIUS_DEBUG("Failed to add EAP-Message attribute");
561 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
562 int len;
563 if (sess->eap_if->eapKeyDataLen > 64) {
564 len = 32;
565 } else {
566 len = sess->eap_if->eapKeyDataLen / 2;
568 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
569 (u8 *) client->shared_secret,
570 client->shared_secret_len,
571 sess->eap_if->eapKeyData + len,
572 len, sess->eap_if->eapKeyData,
573 len)) {
574 RADIUS_DEBUG("Failed to add MPPE key attributes");
578 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
579 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
580 radius_msg_free(msg);
581 return NULL;
584 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
585 client->shared_secret_len,
586 hdr->authenticator) < 0) {
587 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
590 return msg;
594 static int radius_server_reject(struct radius_server_data *data,
595 struct radius_client *client,
596 struct radius_msg *request,
597 struct sockaddr *from, socklen_t fromlen,
598 const char *from_addr, int from_port)
600 struct radius_msg *msg;
601 int ret = 0;
602 struct eap_hdr eapfail;
603 struct wpabuf *buf;
604 struct radius_hdr *hdr = radius_msg_get_hdr(request);
606 RADIUS_DEBUG("Reject invalid request from %s:%d",
607 from_addr, from_port);
609 msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
610 if (msg == NULL) {
611 return -1;
614 os_memset(&eapfail, 0, sizeof(eapfail));
615 eapfail.code = EAP_CODE_FAILURE;
616 eapfail.identifier = 0;
617 eapfail.length = host_to_be16(sizeof(eapfail));
619 if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
620 RADIUS_DEBUG("Failed to add EAP-Message attribute");
623 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
624 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
625 radius_msg_free(msg);
626 return -1;
629 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
630 client->shared_secret_len,
631 hdr->authenticator) <
632 0) {
633 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
636 if (wpa_debug_level <= MSG_MSGDUMP) {
637 radius_msg_dump(msg);
640 data->counters.access_rejects++;
641 client->counters.access_rejects++;
642 buf = radius_msg_get_buf(msg);
643 if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
644 (struct sockaddr *) from, sizeof(*from)) < 0) {
645 perror("sendto[RADIUS SRV]");
646 ret = -1;
649 radius_msg_free(msg);
651 return ret;
655 static int radius_server_request(struct radius_server_data *data,
656 struct radius_msg *msg,
657 struct sockaddr *from, socklen_t fromlen,
658 struct radius_client *client,
659 const char *from_addr, int from_port,
660 struct radius_session *force_sess)
662 u8 *eap = NULL;
663 size_t eap_len;
664 int res, state_included = 0;
665 u8 statebuf[4];
666 unsigned int state;
667 struct radius_session *sess;
668 struct radius_msg *reply;
669 int is_complete = 0;
671 if (force_sess)
672 sess = force_sess;
673 else {
674 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
675 sizeof(statebuf));
676 state_included = res >= 0;
677 if (res == sizeof(statebuf)) {
678 state = WPA_GET_BE32(statebuf);
679 sess = radius_server_get_session(client, state);
680 } else {
681 sess = NULL;
685 if (sess) {
686 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
687 } else if (state_included) {
688 RADIUS_DEBUG("State attribute included but no session found");
689 radius_server_reject(data, client, msg, from, fromlen,
690 from_addr, from_port);
691 return -1;
692 } else {
693 sess = radius_server_get_new_session(data, client, msg);
694 if (sess == NULL) {
695 RADIUS_DEBUG("Could not create a new session");
696 radius_server_reject(data, client, msg, from, fromlen,
697 from_addr, from_port);
698 return -1;
702 if (sess->last_from_port == from_port &&
703 sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
704 os_memcmp(sess->last_authenticator,
705 radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
706 RADIUS_DEBUG("Duplicate message from %s", from_addr);
707 data->counters.dup_access_requests++;
708 client->counters.dup_access_requests++;
710 if (sess->last_reply) {
711 struct wpabuf *buf;
712 buf = radius_msg_get_buf(sess->last_reply);
713 res = sendto(data->auth_sock, wpabuf_head(buf),
714 wpabuf_len(buf), 0,
715 (struct sockaddr *) from, fromlen);
716 if (res < 0) {
717 perror("sendto[RADIUS SRV]");
719 return 0;
722 RADIUS_DEBUG("No previous reply available for duplicate "
723 "message");
724 return -1;
727 eap = radius_msg_get_eap(msg, &eap_len);
728 if (eap == NULL) {
729 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
730 from_addr);
731 data->counters.packets_dropped++;
732 client->counters.packets_dropped++;
733 return -1;
736 RADIUS_DUMP("Received EAP data", eap, eap_len);
738 /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
739 * RFC3579 Sect. 2.6.2.
740 * Include EAP-Response/Nak with no preferred method if
741 * code == request.
742 * If code is not 1-4, discard the packet silently.
743 * Or is this already done by the EAP state machine? */
745 wpabuf_free(sess->eap_if->eapRespData);
746 sess->eap_if->eapRespData = wpabuf_alloc_ext_data(eap, eap_len);
747 if (sess->eap_if->eapRespData == NULL)
748 os_free(eap);
749 eap = NULL;
750 sess->eap_if->eapResp = TRUE;
751 eap_server_sm_step(sess->eap);
753 if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
754 sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
755 RADIUS_DUMP("EAP data from the state machine",
756 wpabuf_head(sess->eap_if->eapReqData),
757 wpabuf_len(sess->eap_if->eapReqData));
758 } else if (sess->eap_if->eapFail) {
759 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
760 "set");
761 } else if (eap_sm_method_pending(sess->eap)) {
762 radius_msg_free(sess->last_msg);
763 sess->last_msg = msg;
764 sess->last_from_port = from_port;
765 os_free(sess->last_from_addr);
766 sess->last_from_addr = os_strdup(from_addr);
767 sess->last_fromlen = fromlen;
768 os_memcpy(&sess->last_from, from, fromlen);
769 return -2;
770 } else {
771 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
772 " Access-Request silently (assuming it was a "
773 "duplicate)");
774 data->counters.packets_dropped++;
775 client->counters.packets_dropped++;
776 return -1;
779 if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
780 is_complete = 1;
782 reply = radius_server_encapsulate_eap(data, client, sess, msg);
784 if (reply) {
785 struct wpabuf *buf;
786 struct radius_hdr *hdr;
788 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
789 if (wpa_debug_level <= MSG_MSGDUMP) {
790 radius_msg_dump(reply);
793 switch (radius_msg_get_hdr(reply)->code) {
794 case RADIUS_CODE_ACCESS_ACCEPT:
795 data->counters.access_accepts++;
796 client->counters.access_accepts++;
797 break;
798 case RADIUS_CODE_ACCESS_REJECT:
799 data->counters.access_rejects++;
800 client->counters.access_rejects++;
801 break;
802 case RADIUS_CODE_ACCESS_CHALLENGE:
803 data->counters.access_challenges++;
804 client->counters.access_challenges++;
805 break;
807 buf = radius_msg_get_buf(reply);
808 res = sendto(data->auth_sock, wpabuf_head(buf),
809 wpabuf_len(buf), 0,
810 (struct sockaddr *) from, fromlen);
811 if (res < 0) {
812 perror("sendto[RADIUS SRV]");
814 radius_msg_free(sess->last_reply);
815 sess->last_reply = reply;
816 sess->last_from_port = from_port;
817 hdr = radius_msg_get_hdr(msg);
818 sess->last_identifier = hdr->identifier;
819 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
820 } else {
821 data->counters.packets_dropped++;
822 client->counters.packets_dropped++;
825 if (is_complete) {
826 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
827 sess->sess_id);
828 eloop_cancel_timeout(radius_server_session_remove_timeout,
829 data, sess);
830 eloop_register_timeout(10, 0,
831 radius_server_session_remove_timeout,
832 data, sess);
835 return 0;
839 static void radius_server_receive_auth(int sock, void *eloop_ctx,
840 void *sock_ctx)
842 struct radius_server_data *data = eloop_ctx;
843 u8 *buf = NULL;
844 union {
845 struct sockaddr_storage ss;
846 struct sockaddr_in sin;
847 #ifdef CONFIG_IPV6
848 struct sockaddr_in6 sin6;
849 #endif /* CONFIG_IPV6 */
850 } from;
851 socklen_t fromlen;
852 int len;
853 struct radius_client *client = NULL;
854 struct radius_msg *msg = NULL;
855 char abuf[50];
856 int from_port = 0;
858 buf = os_malloc(RADIUS_MAX_MSG_LEN);
859 if (buf == NULL) {
860 goto fail;
863 fromlen = sizeof(from);
864 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
865 (struct sockaddr *) &from.ss, &fromlen);
866 if (len < 0) {
867 perror("recvfrom[radius_server]");
868 goto fail;
871 #ifdef CONFIG_IPV6
872 if (data->ipv6) {
873 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
874 sizeof(abuf)) == NULL)
875 abuf[0] = '\0';
876 from_port = ntohs(from.sin6.sin6_port);
877 RADIUS_DEBUG("Received %d bytes from %s:%d",
878 len, abuf, from_port);
880 client = radius_server_get_client(data,
881 (struct in_addr *)
882 &from.sin6.sin6_addr, 1);
884 #endif /* CONFIG_IPV6 */
886 if (!data->ipv6) {
887 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
888 from_port = ntohs(from.sin.sin_port);
889 RADIUS_DEBUG("Received %d bytes from %s:%d",
890 len, abuf, from_port);
892 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
895 RADIUS_DUMP("Received data", buf, len);
897 if (client == NULL) {
898 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
899 data->counters.invalid_requests++;
900 goto fail;
903 msg = radius_msg_parse(buf, len);
904 if (msg == NULL) {
905 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
906 data->counters.malformed_access_requests++;
907 client->counters.malformed_access_requests++;
908 goto fail;
911 os_free(buf);
912 buf = NULL;
914 if (wpa_debug_level <= MSG_MSGDUMP) {
915 radius_msg_dump(msg);
918 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
919 RADIUS_DEBUG("Unexpected RADIUS code %d",
920 radius_msg_get_hdr(msg)->code);
921 data->counters.unknown_types++;
922 client->counters.unknown_types++;
923 goto fail;
926 data->counters.access_requests++;
927 client->counters.access_requests++;
929 if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
930 client->shared_secret_len, NULL)) {
931 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
932 data->counters.bad_authenticators++;
933 client->counters.bad_authenticators++;
934 goto fail;
937 if (radius_server_request(data, msg, (struct sockaddr *) &from,
938 fromlen, client, abuf, from_port, NULL) ==
940 return; /* msg was stored with the session */
942 fail:
943 radius_msg_free(msg);
944 os_free(buf);
948 static int radius_server_disable_pmtu_discovery(int s)
950 int r = -1;
951 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
952 /* Turn off Path MTU discovery on IPv4/UDP sockets. */
953 int action = IP_PMTUDISC_DONT;
954 r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
955 sizeof(action));
956 if (r == -1)
957 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
958 "%s", strerror(errno));
959 #endif
960 return r;
964 static int radius_server_open_socket(int port)
966 int s;
967 struct sockaddr_in addr;
969 s = socket(PF_INET, SOCK_DGRAM, 0);
970 if (s < 0) {
971 perror("socket");
972 return -1;
975 radius_server_disable_pmtu_discovery(s);
977 os_memset(&addr, 0, sizeof(addr));
978 addr.sin_family = AF_INET;
979 addr.sin_port = htons(port);
980 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
981 perror("bind");
982 close(s);
983 return -1;
986 return s;
990 #ifdef CONFIG_IPV6
991 static int radius_server_open_socket6(int port)
993 int s;
994 struct sockaddr_in6 addr;
996 s = socket(PF_INET6, SOCK_DGRAM, 0);
997 if (s < 0) {
998 perror("socket[IPv6]");
999 return -1;
1002 os_memset(&addr, 0, sizeof(addr));
1003 addr.sin6_family = AF_INET6;
1004 os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
1005 addr.sin6_port = htons(port);
1006 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1007 perror("bind");
1008 close(s);
1009 return -1;
1012 return s;
1014 #endif /* CONFIG_IPV6 */
1017 static void radius_server_free_sessions(struct radius_server_data *data,
1018 struct radius_session *sessions)
1020 struct radius_session *session, *prev;
1022 session = sessions;
1023 while (session) {
1024 prev = session;
1025 session = session->next;
1026 radius_server_session_free(data, prev);
1031 static void radius_server_free_clients(struct radius_server_data *data,
1032 struct radius_client *clients)
1034 struct radius_client *client, *prev;
1036 client = clients;
1037 while (client) {
1038 prev = client;
1039 client = client->next;
1041 radius_server_free_sessions(data, prev->sessions);
1042 os_free(prev->shared_secret);
1043 os_free(prev);
1048 static struct radius_client *
1049 radius_server_read_clients(const char *client_file, int ipv6)
1051 FILE *f;
1052 const int buf_size = 1024;
1053 char *buf, *pos;
1054 struct radius_client *clients, *tail, *entry;
1055 int line = 0, mask, failed = 0, i;
1056 struct in_addr addr;
1057 #ifdef CONFIG_IPV6
1058 struct in6_addr addr6;
1059 #endif /* CONFIG_IPV6 */
1060 unsigned int val;
1062 f = fopen(client_file, "r");
1063 if (f == NULL) {
1064 RADIUS_ERROR("Could not open client file '%s'", client_file);
1065 return NULL;
1068 buf = os_malloc(buf_size);
1069 if (buf == NULL) {
1070 fclose(f);
1071 return NULL;
1074 clients = tail = NULL;
1075 while (fgets(buf, buf_size, f)) {
1076 /* Configuration file format:
1077 * 192.168.1.0/24 secret
1078 * 192.168.1.2 secret
1079 * fe80::211:22ff:fe33:4455/64 secretipv6
1081 line++;
1082 buf[buf_size - 1] = '\0';
1083 pos = buf;
1084 while (*pos != '\0' && *pos != '\n')
1085 pos++;
1086 if (*pos == '\n')
1087 *pos = '\0';
1088 if (*buf == '\0' || *buf == '#')
1089 continue;
1091 pos = buf;
1092 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
1093 (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
1094 (*pos >= 'A' && *pos <= 'F')) {
1095 pos++;
1098 if (*pos == '\0') {
1099 failed = 1;
1100 break;
1103 if (*pos == '/') {
1104 char *end;
1105 *pos++ = '\0';
1106 mask = strtol(pos, &end, 10);
1107 if ((pos == end) ||
1108 (mask < 0 || mask > (ipv6 ? 128 : 32))) {
1109 failed = 1;
1110 break;
1112 pos = end;
1113 } else {
1114 mask = ipv6 ? 128 : 32;
1115 *pos++ = '\0';
1118 if (!ipv6 && inet_aton(buf, &addr) == 0) {
1119 failed = 1;
1120 break;
1122 #ifdef CONFIG_IPV6
1123 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
1124 if (inet_pton(AF_INET, buf, &addr) <= 0) {
1125 failed = 1;
1126 break;
1128 /* Convert IPv4 address to IPv6 */
1129 if (mask <= 32)
1130 mask += (128 - 32);
1131 os_memset(addr6.s6_addr, 0, 10);
1132 addr6.s6_addr[10] = 0xff;
1133 addr6.s6_addr[11] = 0xff;
1134 os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
1137 #endif /* CONFIG_IPV6 */
1139 while (*pos == ' ' || *pos == '\t') {
1140 pos++;
1143 if (*pos == '\0') {
1144 failed = 1;
1145 break;
1148 entry = os_zalloc(sizeof(*entry));
1149 if (entry == NULL) {
1150 failed = 1;
1151 break;
1153 entry->shared_secret = os_strdup(pos);
1154 if (entry->shared_secret == NULL) {
1155 failed = 1;
1156 os_free(entry);
1157 break;
1159 entry->shared_secret_len = os_strlen(entry->shared_secret);
1160 entry->addr.s_addr = addr.s_addr;
1161 if (!ipv6) {
1162 val = 0;
1163 for (i = 0; i < mask; i++)
1164 val |= 1 << (31 - i);
1165 entry->mask.s_addr = htonl(val);
1167 #ifdef CONFIG_IPV6
1168 if (ipv6) {
1169 int offset = mask / 8;
1171 os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
1172 os_memset(entry->mask6.s6_addr, 0xff, offset);
1173 val = 0;
1174 for (i = 0; i < (mask % 8); i++)
1175 val |= 1 << (7 - i);
1176 if (offset < 16)
1177 entry->mask6.s6_addr[offset] = val;
1179 #endif /* CONFIG_IPV6 */
1181 if (tail == NULL) {
1182 clients = tail = entry;
1183 } else {
1184 tail->next = entry;
1185 tail = entry;
1189 if (failed) {
1190 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
1191 radius_server_free_clients(NULL, clients);
1192 clients = NULL;
1195 os_free(buf);
1196 fclose(f);
1198 return clients;
1203 * radius_server_init - Initialize RADIUS server
1204 * @conf: Configuration for the RADIUS server
1205 * Returns: Pointer to private RADIUS server context or %NULL on failure
1207 * This initializes a RADIUS server instance and returns a context pointer that
1208 * will be used in other calls to the RADIUS server module. The server can be
1209 * deinitialize by calling radius_server_deinit().
1211 struct radius_server_data *
1212 radius_server_init(struct radius_server_conf *conf)
1214 struct radius_server_data *data;
1216 #ifndef CONFIG_IPV6
1217 if (conf->ipv6) {
1218 fprintf(stderr, "RADIUS server compiled without IPv6 "
1219 "support.\n");
1220 return NULL;
1222 #endif /* CONFIG_IPV6 */
1224 data = os_zalloc(sizeof(*data));
1225 if (data == NULL)
1226 return NULL;
1228 os_get_time(&data->start_time);
1229 data->conf_ctx = conf->conf_ctx;
1230 data->eap_sim_db_priv = conf->eap_sim_db_priv;
1231 data->ssl_ctx = conf->ssl_ctx;
1232 data->ipv6 = conf->ipv6;
1233 if (conf->pac_opaque_encr_key) {
1234 data->pac_opaque_encr_key = os_malloc(16);
1235 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1236 16);
1238 if (conf->eap_fast_a_id) {
1239 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1240 if (data->eap_fast_a_id) {
1241 os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1242 conf->eap_fast_a_id_len);
1243 data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1246 if (conf->eap_fast_a_id_info)
1247 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1248 data->eap_fast_prov = conf->eap_fast_prov;
1249 data->pac_key_lifetime = conf->pac_key_lifetime;
1250 data->pac_key_refresh_time = conf->pac_key_refresh_time;
1251 data->get_eap_user = conf->get_eap_user;
1252 data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1253 data->tnc = conf->tnc;
1254 data->wps = conf->wps;
1255 if (conf->eap_req_id_text) {
1256 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
1257 if (data->eap_req_id_text) {
1258 os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
1259 conf->eap_req_id_text_len);
1260 data->eap_req_id_text_len = conf->eap_req_id_text_len;
1264 data->clients = radius_server_read_clients(conf->client_file,
1265 conf->ipv6);
1266 if (data->clients == NULL) {
1267 printf("No RADIUS clients configured.\n");
1268 radius_server_deinit(data);
1269 return NULL;
1272 #ifdef CONFIG_IPV6
1273 if (conf->ipv6)
1274 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1275 else
1276 #endif /* CONFIG_IPV6 */
1277 data->auth_sock = radius_server_open_socket(conf->auth_port);
1278 if (data->auth_sock < 0) {
1279 printf("Failed to open UDP socket for RADIUS authentication "
1280 "server\n");
1281 radius_server_deinit(data);
1282 return NULL;
1284 if (eloop_register_read_sock(data->auth_sock,
1285 radius_server_receive_auth,
1286 data, NULL)) {
1287 radius_server_deinit(data);
1288 return NULL;
1291 return data;
1296 * radius_server_deinit - Deinitialize RADIUS server
1297 * @data: RADIUS server context from radius_server_init()
1299 void radius_server_deinit(struct radius_server_data *data)
1301 if (data == NULL)
1302 return;
1304 if (data->auth_sock >= 0) {
1305 eloop_unregister_read_sock(data->auth_sock);
1306 close(data->auth_sock);
1309 radius_server_free_clients(data, data->clients);
1311 os_free(data->pac_opaque_encr_key);
1312 os_free(data->eap_fast_a_id);
1313 os_free(data->eap_fast_a_id_info);
1314 os_free(data->eap_req_id_text);
1315 os_free(data);
1320 * radius_server_get_mib - Get RADIUS server MIB information
1321 * @data: RADIUS server context from radius_server_init()
1322 * @buf: Buffer for returning the MIB data in text format
1323 * @buflen: buf length in octets
1324 * Returns: Number of octets written into buf
1326 int radius_server_get_mib(struct radius_server_data *data, char *buf,
1327 size_t buflen)
1329 int ret, uptime;
1330 unsigned int idx;
1331 char *end, *pos;
1332 struct os_time now;
1333 struct radius_client *cli;
1335 /* RFC 2619 - RADIUS Authentication Server MIB */
1337 if (data == NULL || buflen == 0)
1338 return 0;
1340 pos = buf;
1341 end = buf + buflen;
1343 os_get_time(&now);
1344 uptime = (now.sec - data->start_time.sec) * 100 +
1345 ((now.usec - data->start_time.usec) / 10000) % 100;
1346 ret = os_snprintf(pos, end - pos,
1347 "RADIUS-AUTH-SERVER-MIB\n"
1348 "radiusAuthServIdent=hostapd\n"
1349 "radiusAuthServUpTime=%d\n"
1350 "radiusAuthServResetTime=0\n"
1351 "radiusAuthServConfigReset=4\n",
1352 uptime);
1353 if (ret < 0 || ret >= end - pos) {
1354 *pos = '\0';
1355 return pos - buf;
1357 pos += ret;
1359 ret = os_snprintf(pos, end - pos,
1360 "radiusAuthServTotalAccessRequests=%u\n"
1361 "radiusAuthServTotalInvalidRequests=%u\n"
1362 "radiusAuthServTotalDupAccessRequests=%u\n"
1363 "radiusAuthServTotalAccessAccepts=%u\n"
1364 "radiusAuthServTotalAccessRejects=%u\n"
1365 "radiusAuthServTotalAccessChallenges=%u\n"
1366 "radiusAuthServTotalMalformedAccessRequests=%u\n"
1367 "radiusAuthServTotalBadAuthenticators=%u\n"
1368 "radiusAuthServTotalPacketsDropped=%u\n"
1369 "radiusAuthServTotalUnknownTypes=%u\n",
1370 data->counters.access_requests,
1371 data->counters.invalid_requests,
1372 data->counters.dup_access_requests,
1373 data->counters.access_accepts,
1374 data->counters.access_rejects,
1375 data->counters.access_challenges,
1376 data->counters.malformed_access_requests,
1377 data->counters.bad_authenticators,
1378 data->counters.packets_dropped,
1379 data->counters.unknown_types);
1380 if (ret < 0 || ret >= end - pos) {
1381 *pos = '\0';
1382 return pos - buf;
1384 pos += ret;
1386 for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1387 char abuf[50], mbuf[50];
1388 #ifdef CONFIG_IPV6
1389 if (data->ipv6) {
1390 if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1391 sizeof(abuf)) == NULL)
1392 abuf[0] = '\0';
1393 if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1394 sizeof(mbuf)) == NULL)
1395 mbuf[0] = '\0';
1397 #endif /* CONFIG_IPV6 */
1398 if (!data->ipv6) {
1399 os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1400 os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1403 ret = os_snprintf(pos, end - pos,
1404 "radiusAuthClientIndex=%u\n"
1405 "radiusAuthClientAddress=%s/%s\n"
1406 "radiusAuthServAccessRequests=%u\n"
1407 "radiusAuthServDupAccessRequests=%u\n"
1408 "radiusAuthServAccessAccepts=%u\n"
1409 "radiusAuthServAccessRejects=%u\n"
1410 "radiusAuthServAccessChallenges=%u\n"
1411 "radiusAuthServMalformedAccessRequests=%u\n"
1412 "radiusAuthServBadAuthenticators=%u\n"
1413 "radiusAuthServPacketsDropped=%u\n"
1414 "radiusAuthServUnknownTypes=%u\n",
1415 idx,
1416 abuf, mbuf,
1417 cli->counters.access_requests,
1418 cli->counters.dup_access_requests,
1419 cli->counters.access_accepts,
1420 cli->counters.access_rejects,
1421 cli->counters.access_challenges,
1422 cli->counters.malformed_access_requests,
1423 cli->counters.bad_authenticators,
1424 cli->counters.packets_dropped,
1425 cli->counters.unknown_types);
1426 if (ret < 0 || ret >= end - pos) {
1427 *pos = '\0';
1428 return pos - buf;
1430 pos += ret;
1433 return pos - buf;
1437 static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1438 size_t identity_len, int phase2,
1439 struct eap_user *user)
1441 struct radius_session *sess = ctx;
1442 struct radius_server_data *data = sess->server;
1444 return data->get_eap_user(data->conf_ctx, identity, identity_len,
1445 phase2, user);
1449 static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
1451 struct radius_session *sess = ctx;
1452 struct radius_server_data *data = sess->server;
1453 *len = data->eap_req_id_text_len;
1454 return data->eap_req_id_text;
1458 static struct eapol_callbacks radius_server_eapol_cb =
1460 .get_eap_user = radius_server_get_eap_user,
1461 .get_eap_req_id_text = radius_server_get_eap_req_id_text,
1466 * radius_server_eap_pending_cb - Pending EAP data notification
1467 * @data: RADIUS server context from radius_server_init()
1468 * @ctx: Pending EAP context pointer
1470 * This function is used to notify EAP server module that a pending operation
1471 * has been completed and processing of the EAP session can proceed.
1473 void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1475 struct radius_client *cli;
1476 struct radius_session *s, *sess = NULL;
1477 struct radius_msg *msg;
1479 if (data == NULL)
1480 return;
1482 for (cli = data->clients; cli; cli = cli->next) {
1483 for (s = cli->sessions; s; s = s->next) {
1484 if (s->eap == ctx && s->last_msg) {
1485 sess = s;
1486 break;
1488 if (sess)
1489 break;
1491 if (sess)
1492 break;
1495 if (sess == NULL) {
1496 RADIUS_DEBUG("No session matched callback ctx");
1497 return;
1500 msg = sess->last_msg;
1501 sess->last_msg = NULL;
1502 eap_sm_pending_cb(sess->eap);
1503 if (radius_server_request(data, msg,
1504 (struct sockaddr *) &sess->last_from,
1505 sess->last_fromlen, cli,
1506 sess->last_from_addr,
1507 sess->last_from_port, sess) == -2)
1508 return; /* msg was stored with the session */
1510 radius_msg_free(msg);