2 * RADIUS authentication server
3 * Copyright (c) 2005-2009, 2011-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #endif /* CONFIG_SQLITE */
18 #include "eap_server/eap.h"
19 #include "ap/ap_config.h"
20 #include "radius_server.h"
23 * RADIUS_SESSION_TIMEOUT - Session timeout in seconds
25 #define RADIUS_SESSION_TIMEOUT 60
28 * RADIUS_MAX_SESSION - Maximum number of active sessions
30 #define RADIUS_MAX_SESSION 100
33 * RADIUS_MAX_MSG_LEN - Maximum message length for incoming RADIUS messages
35 #define RADIUS_MAX_MSG_LEN 3000
37 static struct eapol_callbacks radius_server_eapol_cb;
40 struct radius_server_data;
43 * struct radius_server_counters - RADIUS server statistics counters
45 struct radius_server_counters {
48 u32 dup_access_requests;
51 u32 access_challenges;
52 u32 malformed_access_requests;
53 u32 bad_authenticators;
58 u32 invalid_acct_requests;
60 u32 malformed_acct_requests;
61 u32 acct_bad_authenticators;
62 u32 unknown_acct_types;
66 * struct radius_session - Internal RADIUS server data for a session
68 struct radius_session {
69 struct radius_session *next;
70 struct radius_client *client;
71 struct radius_server_data *server;
74 struct eap_eapol_interface *eap_if;
75 char *username; /* from User-Name attribute */
78 struct radius_msg *last_msg;
81 struct sockaddr_storage last_from;
82 socklen_t last_fromlen;
84 struct radius_msg *last_reply;
85 u8 last_authenticator[16];
87 unsigned int remediation:1;
89 struct hostapd_radius_attr *accept_attr;
93 * struct radius_client - Internal RADIUS server data for a client
95 struct radius_client {
96 struct radius_client *next;
100 struct in6_addr addr6;
101 struct in6_addr mask6;
102 #endif /* CONFIG_IPV6 */
104 int shared_secret_len;
105 struct radius_session *sessions;
106 struct radius_server_counters counters;
110 * struct radius_server_data - Internal RADIUS server data
112 struct radius_server_data {
114 * auth_sock - Socket for RADIUS authentication messages
119 * acct_sock - Socket for RADIUS accounting messages
124 * clients - List of authorized RADIUS clients
126 struct radius_client *clients;
129 * next_sess_id - Next session identifier
131 unsigned int next_sess_id;
134 * conf_ctx - Context pointer for callbacks
136 * This is used as the ctx argument in get_eap_user() calls.
141 * num_sess - Number of active sessions
146 * eap_sim_db_priv - EAP-SIM/AKA database context
148 * This is passed to the EAP-SIM/AKA server implementation as a
151 void *eap_sim_db_priv;
154 * ssl_ctx - TLS context
156 * This is passed to the EAP server implementation as a callback
157 * context for TLS operations.
162 * pac_opaque_encr_key - PAC-Opaque encryption key for EAP-FAST
164 * This parameter is used to set a key for EAP-FAST to encrypt the
165 * PAC-Opaque data. It can be set to %NULL if EAP-FAST is not used. If
166 * set, must point to a 16-octet key.
168 u8 *pac_opaque_encr_key;
171 * eap_fast_a_id - EAP-FAST authority identity (A-ID)
173 * If EAP-FAST is not used, this can be set to %NULL. In theory, this
174 * is a variable length field, but due to some existing implementations
175 * requiring A-ID to be 16 octets in length, it is recommended to use
176 * that length for the field to provide interoperability with deployed
177 * peer implementations.
182 * eap_fast_a_id_len - Length of eap_fast_a_id buffer in octets
184 size_t eap_fast_a_id_len;
187 * eap_fast_a_id_info - EAP-FAST authority identifier information
189 * This A-ID-Info contains a user-friendly name for the A-ID. For
190 * example, this could be the enterprise and server names in
191 * human-readable format. This field is encoded as UTF-8. If EAP-FAST
192 * is not used, this can be set to %NULL.
194 char *eap_fast_a_id_info;
197 * eap_fast_prov - EAP-FAST provisioning modes
199 * 0 = provisioning disabled, 1 = only anonymous provisioning allowed,
200 * 2 = only authenticated provisioning allowed, 3 = both provisioning
206 * pac_key_lifetime - EAP-FAST PAC-Key lifetime in seconds
208 * This is the hard limit on how long a provisioned PAC-Key can be
211 int pac_key_lifetime;
214 * pac_key_refresh_time - EAP-FAST PAC-Key refresh time in seconds
216 * This is a soft limit on the PAC-Key. The server will automatically
217 * generate a new PAC-Key when this number of seconds (or fewer) of the
220 int pac_key_refresh_time;
223 * eap_sim_aka_result_ind - EAP-SIM/AKA protected success indication
225 * This controls whether the protected success/failure indication
226 * (AT_RESULT_IND) is used with EAP-SIM and EAP-AKA.
228 int eap_sim_aka_result_ind;
231 * tnc - Trusted Network Connect (TNC)
233 * This controls whether TNC is enabled and will be required before the
234 * peer is allowed to connect. Note: This is only used with EAP-TTLS
235 * and EAP-FAST. If any other EAP method is enabled, the peer will be
236 * allowed to connect without TNC.
241 * pwd_group - The D-H group assigned for EAP-pwd
243 * If EAP-pwd is not used it can be set to zero.
248 * server_id - Server identity
250 const char *server_id;
253 * wps - Wi-Fi Protected Setup context
255 * If WPS is used with an external RADIUS server (which is quite
256 * unlikely configuration), this is used to provide a pointer to WPS
257 * context data. Normally, this can be set to %NULL.
259 struct wps_context *wps;
262 * ipv6 - Whether to enable IPv6 support in the RADIUS server
267 * start_time - Timestamp of server start
269 struct os_reltime start_time;
272 * counters - Statistics counters for server operations
274 * These counters are the sum over all clients.
276 struct radius_server_counters counters;
279 * get_eap_user - Callback for fetching EAP user information
280 * @ctx: Context data from conf_ctx
281 * @identity: User identity
282 * @identity_len: identity buffer length in octets
283 * @phase2: Whether this is for Phase 2 identity
284 * @user: Data structure for filling in the user information
285 * Returns: 0 on success, -1 on failure
287 * This is used to fetch information from user database. The callback
288 * will fill in information about allowed EAP methods and the user
289 * password. The password field will be an allocated copy of the
290 * password data and RADIUS server will free it after use.
292 int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
293 int phase2, struct eap_user *user);
296 * eap_req_id_text - Optional data for EAP-Request/Identity
298 * This can be used to configure an optional, displayable message that
299 * will be sent in EAP-Request/Identity. This string can contain an
300 * ASCII-0 character (nul) to separate network infromation per RFC
301 * 4284. The actual string length is explicit provided in
302 * eap_req_id_text_len since nul character will not be used as a string
305 char *eap_req_id_text;
308 * eap_req_id_text_len - Length of eap_req_id_text buffer in octets
310 size_t eap_req_id_text_len;
313 * msg_ctx - Context data for wpa_msg() calls
317 #ifdef CONFIG_RADIUS_TEST
319 #endif /* CONFIG_RADIUS_TEST */
321 char *subscr_remediation_url;
322 u8 subscr_remediation_method;
326 #endif /* CONFIG_SQLITE */
330 #define RADIUS_DEBUG(args...) \
331 wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
332 #define RADIUS_ERROR(args...) \
333 wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
334 #define RADIUS_DUMP(args...) \
335 wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
336 #define RADIUS_DUMP_ASCII(args...) \
337 wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
340 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
341 static void radius_server_session_remove_timeout(void *eloop_ctx,
344 void srv_log(struct radius_session *sess, const char *fmt, ...)
347 void srv_log(struct radius_session *sess, const char *fmt, ...)
354 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
357 buf = os_malloc(buflen);
361 vsnprintf(buf, buflen, fmt, ap);
364 RADIUS_DEBUG("[0x%x %s] %s", sess->sess_id, sess->nas_ip, buf);
367 if (sess->server->db) {
369 sql = sqlite3_mprintf("INSERT INTO authlog"
370 "(timestamp,session,nas_ip,username,note)"
372 "strftime('%%Y-%%m-%%d %%H:%%M:%%f',"
373 "'now'),%u,%Q,%Q,%Q)",
374 sess->sess_id, sess->nas_ip,
375 sess->username, buf);
377 if (sqlite3_exec(sess->server->db, sql, NULL, NULL,
378 NULL) != SQLITE_OK) {
379 RADIUS_ERROR("Failed to add authlog entry into sqlite database: %s",
380 sqlite3_errmsg(sess->server->db));
385 #endif /* CONFIG_SQLITE */
391 static struct radius_client *
392 radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
395 struct radius_client *client = data->clients;
400 struct in6_addr *addr6;
403 addr6 = (struct in6_addr *) addr;
404 for (i = 0; i < 16; i++) {
405 if ((addr6->s6_addr[i] &
406 client->mask6.s6_addr[i]) !=
407 (client->addr6.s6_addr[i] &
408 client->mask6.s6_addr[i])) {
417 #endif /* CONFIG_IPV6 */
418 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
419 (addr->s_addr & client->mask.s_addr)) {
423 client = client->next;
430 static struct radius_session *
431 radius_server_get_session(struct radius_client *client, unsigned int sess_id)
433 struct radius_session *sess = client->sessions;
436 if (sess->sess_id == sess_id) {
446 static void radius_server_session_free(struct radius_server_data *data,
447 struct radius_session *sess)
449 eloop_cancel_timeout(radius_server_session_timeout, data, sess);
450 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
451 eap_server_sm_deinit(sess->eap);
452 radius_msg_free(sess->last_msg);
453 os_free(sess->last_from_addr);
454 radius_msg_free(sess->last_reply);
455 os_free(sess->username);
456 os_free(sess->nas_ip);
462 static void radius_server_session_remove(struct radius_server_data *data,
463 struct radius_session *sess)
465 struct radius_client *client = sess->client;
466 struct radius_session *session, *prev;
468 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
471 session = client->sessions;
473 if (session == sess) {
475 client->sessions = sess->next;
477 prev->next = sess->next;
479 radius_server_session_free(data, sess);
483 session = session->next;
488 static void radius_server_session_remove_timeout(void *eloop_ctx,
491 struct radius_server_data *data = eloop_ctx;
492 struct radius_session *sess = timeout_ctx;
493 RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
494 radius_server_session_remove(data, sess);
498 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
500 struct radius_server_data *data = eloop_ctx;
501 struct radius_session *sess = timeout_ctx;
503 RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
504 radius_server_session_remove(data, sess);
508 static struct radius_session *
509 radius_server_new_session(struct radius_server_data *data,
510 struct radius_client *client)
512 struct radius_session *sess;
514 if (data->num_sess >= RADIUS_MAX_SESSION) {
515 RADIUS_DEBUG("Maximum number of existing session - no room "
516 "for a new session");
520 sess = os_zalloc(sizeof(*sess));
525 sess->client = client;
526 sess->sess_id = data->next_sess_id++;
527 sess->next = client->sessions;
528 client->sessions = sess;
529 eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
530 radius_server_session_timeout, data, sess);
536 static struct radius_session *
537 radius_server_get_new_session(struct radius_server_data *data,
538 struct radius_client *client,
539 struct radius_msg *msg, const char *from_addr)
544 struct radius_session *sess;
545 struct eap_config eap_conf;
548 RADIUS_DEBUG("Creating a new session");
550 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_USER_NAME, &user,
551 &user_len, NULL) < 0) {
552 RADIUS_DEBUG("Could not get User-Name");
555 RADIUS_DUMP_ASCII("User-Name", user, user_len);
557 os_memset(&tmp, 0, sizeof(tmp));
558 res = data->get_eap_user(data->conf_ctx, user, user_len, 0, &tmp);
559 os_free(tmp.password);
562 RADIUS_DEBUG("User-Name not found from user database");
566 RADIUS_DEBUG("Matching user entry found");
567 sess = radius_server_new_session(data, client);
569 RADIUS_DEBUG("Failed to create a new session");
572 sess->accept_attr = tmp.accept_attr;
574 sess->username = os_malloc(user_len * 2 + 1);
575 if (sess->username == NULL) {
576 radius_server_session_free(data, sess);
579 printf_encode(sess->username, user_len * 2 + 1, user, user_len);
581 sess->nas_ip = os_strdup(from_addr);
582 if (sess->nas_ip == NULL) {
583 radius_server_session_free(data, sess);
587 srv_log(sess, "New session created");
589 os_memset(&eap_conf, 0, sizeof(eap_conf));
590 eap_conf.ssl_ctx = data->ssl_ctx;
591 eap_conf.msg_ctx = data->msg_ctx;
592 eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
593 eap_conf.backend_auth = TRUE;
594 eap_conf.eap_server = 1;
595 eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
596 eap_conf.eap_fast_a_id = data->eap_fast_a_id;
597 eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
598 eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
599 eap_conf.eap_fast_prov = data->eap_fast_prov;
600 eap_conf.pac_key_lifetime = data->pac_key_lifetime;
601 eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
602 eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
603 eap_conf.tnc = data->tnc;
604 eap_conf.wps = data->wps;
605 eap_conf.pwd_group = data->pwd_group;
606 eap_conf.server_id = (const u8 *) data->server_id;
607 eap_conf.server_id_len = os_strlen(data->server_id);
608 sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
610 if (sess->eap == NULL) {
611 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
613 radius_server_session_free(data, sess);
616 sess->eap_if = eap_get_interface(sess->eap);
617 sess->eap_if->eapRestart = TRUE;
618 sess->eap_if->portEnabled = TRUE;
620 RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
626 static struct radius_msg *
627 radius_server_encapsulate_eap(struct radius_server_data *data,
628 struct radius_client *client,
629 struct radius_session *sess,
630 struct radius_msg *request)
632 struct radius_msg *msg;
634 unsigned int sess_id;
635 struct radius_hdr *hdr = radius_msg_get_hdr(request);
637 if (sess->eap_if->eapFail) {
638 sess->eap_if->eapFail = FALSE;
639 code = RADIUS_CODE_ACCESS_REJECT;
640 } else if (sess->eap_if->eapSuccess) {
641 sess->eap_if->eapSuccess = FALSE;
642 code = RADIUS_CODE_ACCESS_ACCEPT;
644 sess->eap_if->eapReq = FALSE;
645 code = RADIUS_CODE_ACCESS_CHALLENGE;
648 msg = radius_msg_new(code, hdr->identifier);
650 RADIUS_DEBUG("Failed to allocate reply message");
654 sess_id = htonl(sess->sess_id);
655 if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
656 !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
657 (u8 *) &sess_id, sizeof(sess_id))) {
658 RADIUS_DEBUG("Failed to add State attribute");
661 if (sess->eap_if->eapReqData &&
662 !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
663 wpabuf_len(sess->eap_if->eapReqData))) {
664 RADIUS_DEBUG("Failed to add EAP-Message attribute");
667 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
669 #ifdef CONFIG_RADIUS_TEST
670 if (data->dump_msk_file) {
672 char buf[2 * 64 + 1];
673 f = fopen(data->dump_msk_file, "a");
675 len = sess->eap_if->eapKeyDataLen;
678 len = wpa_snprintf_hex(
680 sess->eap_if->eapKeyData, len);
682 fprintf(f, "%s\n", buf);
686 #endif /* CONFIG_RADIUS_TEST */
687 if (sess->eap_if->eapKeyDataLen > 64) {
690 len = sess->eap_if->eapKeyDataLen / 2;
692 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
693 (u8 *) client->shared_secret,
694 client->shared_secret_len,
695 sess->eap_if->eapKeyData + len,
696 len, sess->eap_if->eapKeyData,
698 RADIUS_DEBUG("Failed to add MPPE key attributes");
703 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->remediation &&
704 data->subscr_remediation_url) {
706 size_t url_len = os_strlen(data->subscr_remediation_url);
707 buf = os_malloc(1 + url_len);
709 radius_msg_free(msg);
712 buf[0] = data->subscr_remediation_method;
713 os_memcpy(&buf[1], data->subscr_remediation_url, url_len);
714 if (!radius_msg_add_wfa(
715 msg, RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION,
717 RADIUS_DEBUG("Failed to add WFA-HS20-SubscrRem");
720 } else if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->remediation) {
722 if (!radius_msg_add_wfa(
723 msg, RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION,
725 RADIUS_DEBUG("Failed to add WFA-HS20-SubscrRem");
728 #endif /* CONFIG_HS20 */
730 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
731 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
732 radius_msg_free(msg);
736 if (code == RADIUS_CODE_ACCESS_ACCEPT) {
737 struct hostapd_radius_attr *attr;
738 for (attr = sess->accept_attr; attr; attr = attr->next) {
739 if (!radius_msg_add_attr(msg, attr->type,
740 wpabuf_head(attr->val),
741 wpabuf_len(attr->val))) {
742 wpa_printf(MSG_ERROR, "Could not add RADIUS attribute");
743 radius_msg_free(msg);
749 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
750 client->shared_secret_len,
751 hdr->authenticator) < 0) {
752 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
759 static int radius_server_reject(struct radius_server_data *data,
760 struct radius_client *client,
761 struct radius_msg *request,
762 struct sockaddr *from, socklen_t fromlen,
763 const char *from_addr, int from_port)
765 struct radius_msg *msg;
767 struct eap_hdr eapfail;
769 struct radius_hdr *hdr = radius_msg_get_hdr(request);
771 RADIUS_DEBUG("Reject invalid request from %s:%d",
772 from_addr, from_port);
774 msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
779 os_memset(&eapfail, 0, sizeof(eapfail));
780 eapfail.code = EAP_CODE_FAILURE;
781 eapfail.identifier = 0;
782 eapfail.length = host_to_be16(sizeof(eapfail));
784 if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
785 RADIUS_DEBUG("Failed to add EAP-Message attribute");
788 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
789 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
790 radius_msg_free(msg);
794 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
795 client->shared_secret_len,
796 hdr->authenticator) <
798 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
801 if (wpa_debug_level <= MSG_MSGDUMP) {
802 radius_msg_dump(msg);
805 data->counters.access_rejects++;
806 client->counters.access_rejects++;
807 buf = radius_msg_get_buf(msg);
808 if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
809 (struct sockaddr *) from, sizeof(*from)) < 0) {
810 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", strerror(errno));
814 radius_msg_free(msg);
820 static int radius_server_request(struct radius_server_data *data,
821 struct radius_msg *msg,
822 struct sockaddr *from, socklen_t fromlen,
823 struct radius_client *client,
824 const char *from_addr, int from_port,
825 struct radius_session *force_sess)
827 struct wpabuf *eap = NULL;
828 int res, state_included = 0;
831 struct radius_session *sess;
832 struct radius_msg *reply;
838 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
840 state_included = res >= 0;
841 if (res == sizeof(statebuf)) {
842 state = WPA_GET_BE32(statebuf);
843 sess = radius_server_get_session(client, state);
850 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
851 } else if (state_included) {
852 RADIUS_DEBUG("State attribute included but no session found");
853 radius_server_reject(data, client, msg, from, fromlen,
854 from_addr, from_port);
857 sess = radius_server_get_new_session(data, client, msg,
860 RADIUS_DEBUG("Could not create a new session");
861 radius_server_reject(data, client, msg, from, fromlen,
862 from_addr, from_port);
867 if (sess->last_from_port == from_port &&
868 sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
869 os_memcmp(sess->last_authenticator,
870 radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
871 RADIUS_DEBUG("Duplicate message from %s", from_addr);
872 data->counters.dup_access_requests++;
873 client->counters.dup_access_requests++;
875 if (sess->last_reply) {
877 buf = radius_msg_get_buf(sess->last_reply);
878 res = sendto(data->auth_sock, wpabuf_head(buf),
880 (struct sockaddr *) from, fromlen);
882 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
888 RADIUS_DEBUG("No previous reply available for duplicate "
893 eap = radius_msg_get_eap(msg);
895 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
897 data->counters.packets_dropped++;
898 client->counters.packets_dropped++;
902 RADIUS_DUMP("Received EAP data", wpabuf_head(eap), wpabuf_len(eap));
904 /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
905 * RFC3579 Sect. 2.6.2.
906 * Include EAP-Response/Nak with no preferred method if
908 * If code is not 1-4, discard the packet silently.
909 * Or is this already done by the EAP state machine? */
911 wpabuf_free(sess->eap_if->eapRespData);
912 sess->eap_if->eapRespData = eap;
913 sess->eap_if->eapResp = TRUE;
914 eap_server_sm_step(sess->eap);
916 if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
917 sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
918 RADIUS_DUMP("EAP data from the state machine",
919 wpabuf_head(sess->eap_if->eapReqData),
920 wpabuf_len(sess->eap_if->eapReqData));
921 } else if (sess->eap_if->eapFail) {
922 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
924 } else if (eap_sm_method_pending(sess->eap)) {
925 radius_msg_free(sess->last_msg);
926 sess->last_msg = msg;
927 sess->last_from_port = from_port;
928 os_free(sess->last_from_addr);
929 sess->last_from_addr = os_strdup(from_addr);
930 sess->last_fromlen = fromlen;
931 os_memcpy(&sess->last_from, from, fromlen);
934 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
935 " Access-Request silently (assuming it was a "
937 data->counters.packets_dropped++;
938 client->counters.packets_dropped++;
942 if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
944 if (sess->eap_if->eapFail)
945 srv_log(sess, "EAP authentication failed");
946 else if (sess->eap_if->eapSuccess)
947 srv_log(sess, "EAP authentication succeeded");
949 reply = radius_server_encapsulate_eap(data, client, sess, msg);
953 struct radius_hdr *hdr;
955 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
956 if (wpa_debug_level <= MSG_MSGDUMP) {
957 radius_msg_dump(reply);
960 switch (radius_msg_get_hdr(reply)->code) {
961 case RADIUS_CODE_ACCESS_ACCEPT:
962 srv_log(sess, "Sending Access-Accept");
963 data->counters.access_accepts++;
964 client->counters.access_accepts++;
966 case RADIUS_CODE_ACCESS_REJECT:
967 srv_log(sess, "Sending Access-Reject");
968 data->counters.access_rejects++;
969 client->counters.access_rejects++;
971 case RADIUS_CODE_ACCESS_CHALLENGE:
972 data->counters.access_challenges++;
973 client->counters.access_challenges++;
976 buf = radius_msg_get_buf(reply);
977 res = sendto(data->auth_sock, wpabuf_head(buf),
979 (struct sockaddr *) from, fromlen);
981 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
984 radius_msg_free(sess->last_reply);
985 sess->last_reply = reply;
986 sess->last_from_port = from_port;
987 hdr = radius_msg_get_hdr(msg);
988 sess->last_identifier = hdr->identifier;
989 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
991 data->counters.packets_dropped++;
992 client->counters.packets_dropped++;
996 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
998 eloop_cancel_timeout(radius_server_session_remove_timeout,
1000 eloop_register_timeout(10, 0,
1001 radius_server_session_remove_timeout,
1009 static void radius_server_receive_auth(int sock, void *eloop_ctx,
1012 struct radius_server_data *data = eloop_ctx;
1015 struct sockaddr_storage ss;
1016 struct sockaddr_in sin;
1018 struct sockaddr_in6 sin6;
1019 #endif /* CONFIG_IPV6 */
1023 struct radius_client *client = NULL;
1024 struct radius_msg *msg = NULL;
1028 buf = os_malloc(RADIUS_MAX_MSG_LEN);
1033 fromlen = sizeof(from);
1034 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
1035 (struct sockaddr *) &from.ss, &fromlen);
1037 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s",
1044 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
1045 sizeof(abuf)) == NULL)
1047 from_port = ntohs(from.sin6.sin6_port);
1048 RADIUS_DEBUG("Received %d bytes from %s:%d",
1049 len, abuf, from_port);
1051 client = radius_server_get_client(data,
1053 &from.sin6.sin6_addr, 1);
1055 #endif /* CONFIG_IPV6 */
1058 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
1059 from_port = ntohs(from.sin.sin_port);
1060 RADIUS_DEBUG("Received %d bytes from %s:%d",
1061 len, abuf, from_port);
1063 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
1066 RADIUS_DUMP("Received data", buf, len);
1068 if (client == NULL) {
1069 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
1070 data->counters.invalid_requests++;
1074 msg = radius_msg_parse(buf, len);
1076 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
1077 data->counters.malformed_access_requests++;
1078 client->counters.malformed_access_requests++;
1085 if (wpa_debug_level <= MSG_MSGDUMP) {
1086 radius_msg_dump(msg);
1089 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
1090 RADIUS_DEBUG("Unexpected RADIUS code %d",
1091 radius_msg_get_hdr(msg)->code);
1092 data->counters.unknown_types++;
1093 client->counters.unknown_types++;
1097 data->counters.access_requests++;
1098 client->counters.access_requests++;
1100 if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
1101 client->shared_secret_len, NULL)) {
1102 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
1103 data->counters.bad_authenticators++;
1104 client->counters.bad_authenticators++;
1108 if (radius_server_request(data, msg, (struct sockaddr *) &from,
1109 fromlen, client, abuf, from_port, NULL) ==
1111 return; /* msg was stored with the session */
1114 radius_msg_free(msg);
1119 static void radius_server_receive_acct(int sock, void *eloop_ctx,
1122 struct radius_server_data *data = eloop_ctx;
1125 struct sockaddr_storage ss;
1126 struct sockaddr_in sin;
1128 struct sockaddr_in6 sin6;
1129 #endif /* CONFIG_IPV6 */
1133 struct radius_client *client = NULL;
1134 struct radius_msg *msg = NULL, *resp = NULL;
1137 struct radius_hdr *hdr;
1138 struct wpabuf *rbuf;
1140 buf = os_malloc(RADIUS_MAX_MSG_LEN);
1145 fromlen = sizeof(from);
1146 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
1147 (struct sockaddr *) &from.ss, &fromlen);
1149 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s",
1156 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
1157 sizeof(abuf)) == NULL)
1159 from_port = ntohs(from.sin6.sin6_port);
1160 RADIUS_DEBUG("Received %d bytes from %s:%d",
1161 len, abuf, from_port);
1163 client = radius_server_get_client(data,
1165 &from.sin6.sin6_addr, 1);
1167 #endif /* CONFIG_IPV6 */
1170 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
1171 from_port = ntohs(from.sin.sin_port);
1172 RADIUS_DEBUG("Received %d bytes from %s:%d",
1173 len, abuf, from_port);
1175 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
1178 RADIUS_DUMP("Received data", buf, len);
1180 if (client == NULL) {
1181 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
1182 data->counters.invalid_acct_requests++;
1186 msg = radius_msg_parse(buf, len);
1188 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
1189 data->counters.malformed_acct_requests++;
1190 client->counters.malformed_acct_requests++;
1197 if (wpa_debug_level <= MSG_MSGDUMP) {
1198 radius_msg_dump(msg);
1201 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCOUNTING_REQUEST) {
1202 RADIUS_DEBUG("Unexpected RADIUS code %d",
1203 radius_msg_get_hdr(msg)->code);
1204 data->counters.unknown_acct_types++;
1205 client->counters.unknown_acct_types++;
1209 data->counters.acct_requests++;
1210 client->counters.acct_requests++;
1212 if (radius_msg_verify_acct_req(msg, (u8 *) client->shared_secret,
1213 client->shared_secret_len)) {
1214 RADIUS_DEBUG("Invalid Authenticator from %s", abuf);
1215 data->counters.acct_bad_authenticators++;
1216 client->counters.acct_bad_authenticators++;
1220 /* TODO: Write accounting information to a file or database */
1222 hdr = radius_msg_get_hdr(msg);
1224 resp = radius_msg_new(RADIUS_CODE_ACCOUNTING_RESPONSE, hdr->identifier);
1228 radius_msg_finish_acct_resp(resp, (u8 *) client->shared_secret,
1229 client->shared_secret_len,
1230 hdr->authenticator);
1232 RADIUS_DEBUG("Reply to %s:%d", abuf, from_port);
1233 if (wpa_debug_level <= MSG_MSGDUMP) {
1234 radius_msg_dump(resp);
1236 rbuf = radius_msg_get_buf(resp);
1237 data->counters.acct_responses++;
1238 client->counters.acct_responses++;
1239 res = sendto(data->acct_sock, wpabuf_head(rbuf), wpabuf_len(rbuf), 0,
1240 (struct sockaddr *) &from.ss, fromlen);
1242 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
1247 radius_msg_free(resp);
1248 radius_msg_free(msg);
1253 static int radius_server_disable_pmtu_discovery(int s)
1256 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
1257 /* Turn off Path MTU discovery on IPv4/UDP sockets. */
1258 int action = IP_PMTUDISC_DONT;
1259 r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
1262 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
1263 "%s", strerror(errno));
1269 static int radius_server_open_socket(int port)
1272 struct sockaddr_in addr;
1274 s = socket(PF_INET, SOCK_DGRAM, 0);
1276 wpa_printf(MSG_INFO, "RADIUS: socket: %s", strerror(errno));
1280 radius_server_disable_pmtu_discovery(s);
1282 os_memset(&addr, 0, sizeof(addr));
1283 addr.sin_family = AF_INET;
1284 addr.sin_port = htons(port);
1285 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1286 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
1296 static int radius_server_open_socket6(int port)
1299 struct sockaddr_in6 addr;
1301 s = socket(PF_INET6, SOCK_DGRAM, 0);
1303 wpa_printf(MSG_INFO, "RADIUS: socket[IPv6]: %s",
1308 os_memset(&addr, 0, sizeof(addr));
1309 addr.sin6_family = AF_INET6;
1310 os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
1311 addr.sin6_port = htons(port);
1312 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1313 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
1320 #endif /* CONFIG_IPV6 */
1323 static void radius_server_free_sessions(struct radius_server_data *data,
1324 struct radius_session *sessions)
1326 struct radius_session *session, *prev;
1331 session = session->next;
1332 radius_server_session_free(data, prev);
1337 static void radius_server_free_clients(struct radius_server_data *data,
1338 struct radius_client *clients)
1340 struct radius_client *client, *prev;
1345 client = client->next;
1347 radius_server_free_sessions(data, prev->sessions);
1348 os_free(prev->shared_secret);
1354 static struct radius_client *
1355 radius_server_read_clients(const char *client_file, int ipv6)
1358 const int buf_size = 1024;
1360 struct radius_client *clients, *tail, *entry;
1361 int line = 0, mask, failed = 0, i;
1362 struct in_addr addr;
1364 struct in6_addr addr6;
1365 #endif /* CONFIG_IPV6 */
1368 f = fopen(client_file, "r");
1370 RADIUS_ERROR("Could not open client file '%s'", client_file);
1374 buf = os_malloc(buf_size);
1380 clients = tail = NULL;
1381 while (fgets(buf, buf_size, f)) {
1382 /* Configuration file format:
1383 * 192.168.1.0/24 secret
1384 * 192.168.1.2 secret
1385 * fe80::211:22ff:fe33:4455/64 secretipv6
1388 buf[buf_size - 1] = '\0';
1390 while (*pos != '\0' && *pos != '\n')
1394 if (*buf == '\0' || *buf == '#')
1398 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
1399 (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
1400 (*pos >= 'A' && *pos <= 'F')) {
1412 mask = strtol(pos, &end, 10);
1414 (mask < 0 || mask > (ipv6 ? 128 : 32))) {
1420 mask = ipv6 ? 128 : 32;
1424 if (!ipv6 && inet_aton(buf, &addr) == 0) {
1429 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
1430 if (inet_pton(AF_INET, buf, &addr) <= 0) {
1434 /* Convert IPv4 address to IPv6 */
1437 os_memset(addr6.s6_addr, 0, 10);
1438 addr6.s6_addr[10] = 0xff;
1439 addr6.s6_addr[11] = 0xff;
1440 os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
1443 #endif /* CONFIG_IPV6 */
1445 while (*pos == ' ' || *pos == '\t') {
1454 entry = os_zalloc(sizeof(*entry));
1455 if (entry == NULL) {
1459 entry->shared_secret = os_strdup(pos);
1460 if (entry->shared_secret == NULL) {
1465 entry->shared_secret_len = os_strlen(entry->shared_secret);
1467 entry->addr.s_addr = addr.s_addr;
1469 for (i = 0; i < mask; i++)
1470 val |= 1 << (31 - i);
1471 entry->mask.s_addr = htonl(val);
1475 int offset = mask / 8;
1477 os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
1478 os_memset(entry->mask6.s6_addr, 0xff, offset);
1480 for (i = 0; i < (mask % 8); i++)
1481 val |= 1 << (7 - i);
1483 entry->mask6.s6_addr[offset] = val;
1485 #endif /* CONFIG_IPV6 */
1488 clients = tail = entry;
1496 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
1497 radius_server_free_clients(NULL, clients);
1509 * radius_server_init - Initialize RADIUS server
1510 * @conf: Configuration for the RADIUS server
1511 * Returns: Pointer to private RADIUS server context or %NULL on failure
1513 * This initializes a RADIUS server instance and returns a context pointer that
1514 * will be used in other calls to the RADIUS server module. The server can be
1515 * deinitialize by calling radius_server_deinit().
1517 struct radius_server_data *
1518 radius_server_init(struct radius_server_conf *conf)
1520 struct radius_server_data *data;
1524 wpa_printf(MSG_ERROR, "RADIUS server compiled without IPv6 support");
1527 #endif /* CONFIG_IPV6 */
1529 data = os_zalloc(sizeof(*data));
1533 os_get_reltime(&data->start_time);
1534 data->conf_ctx = conf->conf_ctx;
1535 data->eap_sim_db_priv = conf->eap_sim_db_priv;
1536 data->ssl_ctx = conf->ssl_ctx;
1537 data->msg_ctx = conf->msg_ctx;
1538 data->ipv6 = conf->ipv6;
1539 if (conf->pac_opaque_encr_key) {
1540 data->pac_opaque_encr_key = os_malloc(16);
1541 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1544 if (conf->eap_fast_a_id) {
1545 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1546 if (data->eap_fast_a_id) {
1547 os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1548 conf->eap_fast_a_id_len);
1549 data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1552 if (conf->eap_fast_a_id_info)
1553 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1554 data->eap_fast_prov = conf->eap_fast_prov;
1555 data->pac_key_lifetime = conf->pac_key_lifetime;
1556 data->pac_key_refresh_time = conf->pac_key_refresh_time;
1557 data->get_eap_user = conf->get_eap_user;
1558 data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1559 data->tnc = conf->tnc;
1560 data->wps = conf->wps;
1561 data->pwd_group = conf->pwd_group;
1562 data->server_id = conf->server_id;
1563 if (conf->eap_req_id_text) {
1564 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
1565 if (data->eap_req_id_text) {
1566 os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
1567 conf->eap_req_id_text_len);
1568 data->eap_req_id_text_len = conf->eap_req_id_text_len;
1572 if (conf->subscr_remediation_url) {
1573 data->subscr_remediation_url =
1574 os_strdup(conf->subscr_remediation_url);
1577 #ifdef CONFIG_SQLITE
1578 if (conf->sqlite_file) {
1579 if (sqlite3_open(conf->sqlite_file, &data->db)) {
1580 RADIUS_ERROR("Could not open SQLite file '%s'",
1582 radius_server_deinit(data);
1586 #endif /* CONFIG_SQLITE */
1588 #ifdef CONFIG_RADIUS_TEST
1589 if (conf->dump_msk_file)
1590 data->dump_msk_file = os_strdup(conf->dump_msk_file);
1591 #endif /* CONFIG_RADIUS_TEST */
1593 data->clients = radius_server_read_clients(conf->client_file,
1595 if (data->clients == NULL) {
1596 wpa_printf(MSG_ERROR, "No RADIUS clients configured");
1597 radius_server_deinit(data);
1603 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1605 #endif /* CONFIG_IPV6 */
1606 data->auth_sock = radius_server_open_socket(conf->auth_port);
1607 if (data->auth_sock < 0) {
1608 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS authentication server");
1609 radius_server_deinit(data);
1612 if (eloop_register_read_sock(data->auth_sock,
1613 radius_server_receive_auth,
1615 radius_server_deinit(data);
1619 if (conf->acct_port) {
1622 data->acct_sock = radius_server_open_socket6(
1625 #endif /* CONFIG_IPV6 */
1626 data->acct_sock = radius_server_open_socket(conf->acct_port);
1627 if (data->acct_sock < 0) {
1628 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS accounting server");
1629 radius_server_deinit(data);
1632 if (eloop_register_read_sock(data->acct_sock,
1633 radius_server_receive_acct,
1635 radius_server_deinit(data);
1639 data->acct_sock = -1;
1647 * radius_server_deinit - Deinitialize RADIUS server
1648 * @data: RADIUS server context from radius_server_init()
1650 void radius_server_deinit(struct radius_server_data *data)
1655 if (data->auth_sock >= 0) {
1656 eloop_unregister_read_sock(data->auth_sock);
1657 close(data->auth_sock);
1660 if (data->acct_sock >= 0) {
1661 eloop_unregister_read_sock(data->acct_sock);
1662 close(data->acct_sock);
1665 radius_server_free_clients(data, data->clients);
1667 os_free(data->pac_opaque_encr_key);
1668 os_free(data->eap_fast_a_id);
1669 os_free(data->eap_fast_a_id_info);
1670 os_free(data->eap_req_id_text);
1671 #ifdef CONFIG_RADIUS_TEST
1672 os_free(data->dump_msk_file);
1673 #endif /* CONFIG_RADIUS_TEST */
1674 os_free(data->subscr_remediation_url);
1676 #ifdef CONFIG_SQLITE
1678 sqlite3_close(data->db);
1679 #endif /* CONFIG_SQLITE */
1686 * radius_server_get_mib - Get RADIUS server MIB information
1687 * @data: RADIUS server context from radius_server_init()
1688 * @buf: Buffer for returning the MIB data in text format
1689 * @buflen: buf length in octets
1690 * Returns: Number of octets written into buf
1692 int radius_server_get_mib(struct radius_server_data *data, char *buf,
1698 struct os_reltime now;
1699 struct radius_client *cli;
1701 /* RFC 2619 - RADIUS Authentication Server MIB */
1703 if (data == NULL || buflen == 0)
1709 os_get_reltime(&now);
1710 uptime = (now.sec - data->start_time.sec) * 100 +
1711 ((now.usec - data->start_time.usec) / 10000) % 100;
1712 ret = os_snprintf(pos, end - pos,
1713 "RADIUS-AUTH-SERVER-MIB\n"
1714 "radiusAuthServIdent=hostapd\n"
1715 "radiusAuthServUpTime=%d\n"
1716 "radiusAuthServResetTime=0\n"
1717 "radiusAuthServConfigReset=4\n",
1719 if (ret < 0 || ret >= end - pos) {
1725 ret = os_snprintf(pos, end - pos,
1726 "radiusAuthServTotalAccessRequests=%u\n"
1727 "radiusAuthServTotalInvalidRequests=%u\n"
1728 "radiusAuthServTotalDupAccessRequests=%u\n"
1729 "radiusAuthServTotalAccessAccepts=%u\n"
1730 "radiusAuthServTotalAccessRejects=%u\n"
1731 "radiusAuthServTotalAccessChallenges=%u\n"
1732 "radiusAuthServTotalMalformedAccessRequests=%u\n"
1733 "radiusAuthServTotalBadAuthenticators=%u\n"
1734 "radiusAuthServTotalPacketsDropped=%u\n"
1735 "radiusAuthServTotalUnknownTypes=%u\n"
1736 "radiusAccServTotalRequests=%u\n"
1737 "radiusAccServTotalInvalidRequests=%u\n"
1738 "radiusAccServTotalResponses=%u\n"
1739 "radiusAccServTotalMalformedRequests=%u\n"
1740 "radiusAccServTotalBadAuthenticators=%u\n"
1741 "radiusAccServTotalUnknownTypes=%u\n",
1742 data->counters.access_requests,
1743 data->counters.invalid_requests,
1744 data->counters.dup_access_requests,
1745 data->counters.access_accepts,
1746 data->counters.access_rejects,
1747 data->counters.access_challenges,
1748 data->counters.malformed_access_requests,
1749 data->counters.bad_authenticators,
1750 data->counters.packets_dropped,
1751 data->counters.unknown_types,
1752 data->counters.acct_requests,
1753 data->counters.invalid_acct_requests,
1754 data->counters.acct_responses,
1755 data->counters.malformed_acct_requests,
1756 data->counters.acct_bad_authenticators,
1757 data->counters.unknown_acct_types);
1758 if (ret < 0 || ret >= end - pos) {
1764 for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1765 char abuf[50], mbuf[50];
1768 if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1769 sizeof(abuf)) == NULL)
1771 if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1772 sizeof(mbuf)) == NULL)
1775 #endif /* CONFIG_IPV6 */
1777 os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1778 os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1781 ret = os_snprintf(pos, end - pos,
1782 "radiusAuthClientIndex=%u\n"
1783 "radiusAuthClientAddress=%s/%s\n"
1784 "radiusAuthServAccessRequests=%u\n"
1785 "radiusAuthServDupAccessRequests=%u\n"
1786 "radiusAuthServAccessAccepts=%u\n"
1787 "radiusAuthServAccessRejects=%u\n"
1788 "radiusAuthServAccessChallenges=%u\n"
1789 "radiusAuthServMalformedAccessRequests=%u\n"
1790 "radiusAuthServBadAuthenticators=%u\n"
1791 "radiusAuthServPacketsDropped=%u\n"
1792 "radiusAuthServUnknownTypes=%u\n"
1793 "radiusAccServTotalRequests=%u\n"
1794 "radiusAccServTotalInvalidRequests=%u\n"
1795 "radiusAccServTotalResponses=%u\n"
1796 "radiusAccServTotalMalformedRequests=%u\n"
1797 "radiusAccServTotalBadAuthenticators=%u\n"
1798 "radiusAccServTotalUnknownTypes=%u\n",
1801 cli->counters.access_requests,
1802 cli->counters.dup_access_requests,
1803 cli->counters.access_accepts,
1804 cli->counters.access_rejects,
1805 cli->counters.access_challenges,
1806 cli->counters.malformed_access_requests,
1807 cli->counters.bad_authenticators,
1808 cli->counters.packets_dropped,
1809 cli->counters.unknown_types,
1810 cli->counters.acct_requests,
1811 cli->counters.invalid_acct_requests,
1812 cli->counters.acct_responses,
1813 cli->counters.malformed_acct_requests,
1814 cli->counters.acct_bad_authenticators,
1815 cli->counters.unknown_acct_types);
1816 if (ret < 0 || ret >= end - pos) {
1827 static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1828 size_t identity_len, int phase2,
1829 struct eap_user *user)
1831 struct radius_session *sess = ctx;
1832 struct radius_server_data *data = sess->server;
1835 ret = data->get_eap_user(data->conf_ctx, identity, identity_len,
1837 if (ret == 0 && user) {
1838 sess->accept_attr = user->accept_attr;
1839 sess->remediation = user->remediation;
1845 static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
1847 struct radius_session *sess = ctx;
1848 struct radius_server_data *data = sess->server;
1849 *len = data->eap_req_id_text_len;
1850 return data->eap_req_id_text;
1854 static void radius_server_log_msg(void *ctx, const char *msg)
1856 struct radius_session *sess = ctx;
1857 srv_log(sess, "EAP: %s", msg);
1861 static struct eapol_callbacks radius_server_eapol_cb =
1863 .get_eap_user = radius_server_get_eap_user,
1864 .get_eap_req_id_text = radius_server_get_eap_req_id_text,
1865 .log_msg = radius_server_log_msg,
1870 * radius_server_eap_pending_cb - Pending EAP data notification
1871 * @data: RADIUS server context from radius_server_init()
1872 * @ctx: Pending EAP context pointer
1874 * This function is used to notify EAP server module that a pending operation
1875 * has been completed and processing of the EAP session can proceed.
1877 void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1879 struct radius_client *cli;
1880 struct radius_session *s, *sess = NULL;
1881 struct radius_msg *msg;
1886 for (cli = data->clients; cli; cli = cli->next) {
1887 for (s = cli->sessions; s; s = s->next) {
1888 if (s->eap == ctx && s->last_msg) {
1900 RADIUS_DEBUG("No session matched callback ctx");
1904 msg = sess->last_msg;
1905 sess->last_msg = NULL;
1906 eap_sm_pending_cb(sess->eap);
1907 if (radius_server_request(data, msg,
1908 (struct sockaddr *) &sess->last_from,
1909 sess->last_fromlen, cli,
1910 sess->last_from_addr,
1911 sess->last_from_port, sess) == -2)
1912 return; /* msg was stored with the session */
1914 radius_msg_free(msg);