Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / src / radius / radius_server.c
blobf8780a69294763d6659ef58675a4a3424167b718
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;
285 * msg_ctx - Context data for wpa_msg() calls
287 void *msg_ctx;
291 extern int wpa_debug_level;
293 #define RADIUS_DEBUG(args...) \
294 wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
295 #define RADIUS_ERROR(args...) \
296 wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
297 #define RADIUS_DUMP(args...) \
298 wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
299 #define RADIUS_DUMP_ASCII(args...) \
300 wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
303 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
304 static void radius_server_session_remove_timeout(void *eloop_ctx,
305 void *timeout_ctx);
308 static struct radius_client *
309 radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
310 int ipv6)
312 struct radius_client *client = data->clients;
314 while (client) {
315 #ifdef CONFIG_IPV6
316 if (ipv6) {
317 struct in6_addr *addr6;
318 int i;
320 addr6 = (struct in6_addr *) addr;
321 for (i = 0; i < 16; i++) {
322 if ((addr6->s6_addr[i] &
323 client->mask6.s6_addr[i]) !=
324 (client->addr6.s6_addr[i] &
325 client->mask6.s6_addr[i])) {
326 i = 17;
327 break;
330 if (i == 16) {
331 break;
334 #endif /* CONFIG_IPV6 */
335 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
336 (addr->s_addr & client->mask.s_addr)) {
337 break;
340 client = client->next;
343 return client;
347 static struct radius_session *
348 radius_server_get_session(struct radius_client *client, unsigned int sess_id)
350 struct radius_session *sess = client->sessions;
352 while (sess) {
353 if (sess->sess_id == sess_id) {
354 break;
356 sess = sess->next;
359 return sess;
363 static void radius_server_session_free(struct radius_server_data *data,
364 struct radius_session *sess)
366 eloop_cancel_timeout(radius_server_session_timeout, data, sess);
367 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
368 eap_server_sm_deinit(sess->eap);
369 radius_msg_free(sess->last_msg);
370 os_free(sess->last_from_addr);
371 radius_msg_free(sess->last_reply);
372 os_free(sess);
373 data->num_sess--;
377 static void radius_server_session_remove(struct radius_server_data *data,
378 struct radius_session *sess)
380 struct radius_client *client = sess->client;
381 struct radius_session *session, *prev;
383 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
385 prev = NULL;
386 session = client->sessions;
387 while (session) {
388 if (session == sess) {
389 if (prev == NULL) {
390 client->sessions = sess->next;
391 } else {
392 prev->next = sess->next;
394 radius_server_session_free(data, sess);
395 break;
397 prev = session;
398 session = session->next;
403 static void radius_server_session_remove_timeout(void *eloop_ctx,
404 void *timeout_ctx)
406 struct radius_server_data *data = eloop_ctx;
407 struct radius_session *sess = timeout_ctx;
408 RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
409 radius_server_session_remove(data, sess);
413 static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
415 struct radius_server_data *data = eloop_ctx;
416 struct radius_session *sess = timeout_ctx;
418 RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
419 radius_server_session_remove(data, sess);
423 static struct radius_session *
424 radius_server_new_session(struct radius_server_data *data,
425 struct radius_client *client)
427 struct radius_session *sess;
429 if (data->num_sess >= RADIUS_MAX_SESSION) {
430 RADIUS_DEBUG("Maximum number of existing session - no room "
431 "for a new session");
432 return NULL;
435 sess = os_zalloc(sizeof(*sess));
436 if (sess == NULL)
437 return NULL;
439 sess->server = data;
440 sess->client = client;
441 sess->sess_id = data->next_sess_id++;
442 sess->next = client->sessions;
443 client->sessions = sess;
444 eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
445 radius_server_session_timeout, data, sess);
446 data->num_sess++;
447 return sess;
451 static struct radius_session *
452 radius_server_get_new_session(struct radius_server_data *data,
453 struct radius_client *client,
454 struct radius_msg *msg)
456 u8 *user;
457 size_t user_len;
458 int res;
459 struct radius_session *sess;
460 struct eap_config eap_conf;
462 RADIUS_DEBUG("Creating a new session");
464 user = os_malloc(256);
465 if (user == NULL) {
466 return NULL;
468 res = radius_msg_get_attr(msg, RADIUS_ATTR_USER_NAME, user, 256);
469 if (res < 0 || res > 256) {
470 RADIUS_DEBUG("Could not get User-Name");
471 os_free(user);
472 return NULL;
474 user_len = res;
475 RADIUS_DUMP_ASCII("User-Name", user, user_len);
477 res = data->get_eap_user(data->conf_ctx, user, user_len, 0, NULL);
478 os_free(user);
480 if (res == 0) {
481 RADIUS_DEBUG("Matching user entry found");
482 sess = radius_server_new_session(data, client);
483 if (sess == NULL) {
484 RADIUS_DEBUG("Failed to create a new session");
485 return NULL;
487 } else {
488 RADIUS_DEBUG("User-Name not found from user database");
489 return NULL;
492 os_memset(&eap_conf, 0, sizeof(eap_conf));
493 eap_conf.ssl_ctx = data->ssl_ctx;
494 eap_conf.msg_ctx = data->msg_ctx;
495 eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
496 eap_conf.backend_auth = TRUE;
497 eap_conf.eap_server = 1;
498 eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
499 eap_conf.eap_fast_a_id = data->eap_fast_a_id;
500 eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
501 eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
502 eap_conf.eap_fast_prov = data->eap_fast_prov;
503 eap_conf.pac_key_lifetime = data->pac_key_lifetime;
504 eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
505 eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
506 eap_conf.tnc = data->tnc;
507 eap_conf.wps = data->wps;
508 sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
509 &eap_conf);
510 if (sess->eap == NULL) {
511 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
512 "new session");
513 radius_server_session_free(data, sess);
514 return NULL;
516 sess->eap_if = eap_get_interface(sess->eap);
517 sess->eap_if->eapRestart = TRUE;
518 sess->eap_if->portEnabled = TRUE;
520 RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
522 return sess;
526 static struct radius_msg *
527 radius_server_encapsulate_eap(struct radius_server_data *data,
528 struct radius_client *client,
529 struct radius_session *sess,
530 struct radius_msg *request)
532 struct radius_msg *msg;
533 int code;
534 unsigned int sess_id;
535 struct radius_hdr *hdr = radius_msg_get_hdr(request);
537 if (sess->eap_if->eapFail) {
538 sess->eap_if->eapFail = FALSE;
539 code = RADIUS_CODE_ACCESS_REJECT;
540 } else if (sess->eap_if->eapSuccess) {
541 sess->eap_if->eapSuccess = FALSE;
542 code = RADIUS_CODE_ACCESS_ACCEPT;
543 } else {
544 sess->eap_if->eapReq = FALSE;
545 code = RADIUS_CODE_ACCESS_CHALLENGE;
548 msg = radius_msg_new(code, hdr->identifier);
549 if (msg == NULL) {
550 RADIUS_DEBUG("Failed to allocate reply message");
551 return NULL;
554 sess_id = htonl(sess->sess_id);
555 if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
556 !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
557 (u8 *) &sess_id, sizeof(sess_id))) {
558 RADIUS_DEBUG("Failed to add State attribute");
561 if (sess->eap_if->eapReqData &&
562 !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
563 wpabuf_len(sess->eap_if->eapReqData))) {
564 RADIUS_DEBUG("Failed to add EAP-Message attribute");
567 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
568 int len;
569 if (sess->eap_if->eapKeyDataLen > 64) {
570 len = 32;
571 } else {
572 len = sess->eap_if->eapKeyDataLen / 2;
574 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
575 (u8 *) client->shared_secret,
576 client->shared_secret_len,
577 sess->eap_if->eapKeyData + len,
578 len, sess->eap_if->eapKeyData,
579 len)) {
580 RADIUS_DEBUG("Failed to add MPPE key attributes");
584 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
585 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
586 radius_msg_free(msg);
587 return NULL;
590 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
591 client->shared_secret_len,
592 hdr->authenticator) < 0) {
593 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
596 return msg;
600 static int radius_server_reject(struct radius_server_data *data,
601 struct radius_client *client,
602 struct radius_msg *request,
603 struct sockaddr *from, socklen_t fromlen,
604 const char *from_addr, int from_port)
606 struct radius_msg *msg;
607 int ret = 0;
608 struct eap_hdr eapfail;
609 struct wpabuf *buf;
610 struct radius_hdr *hdr = radius_msg_get_hdr(request);
612 RADIUS_DEBUG("Reject invalid request from %s:%d",
613 from_addr, from_port);
615 msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
616 if (msg == NULL) {
617 return -1;
620 os_memset(&eapfail, 0, sizeof(eapfail));
621 eapfail.code = EAP_CODE_FAILURE;
622 eapfail.identifier = 0;
623 eapfail.length = host_to_be16(sizeof(eapfail));
625 if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
626 RADIUS_DEBUG("Failed to add EAP-Message attribute");
629 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
630 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
631 radius_msg_free(msg);
632 return -1;
635 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
636 client->shared_secret_len,
637 hdr->authenticator) <
638 0) {
639 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
642 if (wpa_debug_level <= MSG_MSGDUMP) {
643 radius_msg_dump(msg);
646 data->counters.access_rejects++;
647 client->counters.access_rejects++;
648 buf = radius_msg_get_buf(msg);
649 if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
650 (struct sockaddr *) from, sizeof(*from)) < 0) {
651 perror("sendto[RADIUS SRV]");
652 ret = -1;
655 radius_msg_free(msg);
657 return ret;
661 static int radius_server_request(struct radius_server_data *data,
662 struct radius_msg *msg,
663 struct sockaddr *from, socklen_t fromlen,
664 struct radius_client *client,
665 const char *from_addr, int from_port,
666 struct radius_session *force_sess)
668 u8 *eap = NULL;
669 size_t eap_len;
670 int res, state_included = 0;
671 u8 statebuf[4];
672 unsigned int state;
673 struct radius_session *sess;
674 struct radius_msg *reply;
675 int is_complete = 0;
677 if (force_sess)
678 sess = force_sess;
679 else {
680 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
681 sizeof(statebuf));
682 state_included = res >= 0;
683 if (res == sizeof(statebuf)) {
684 state = WPA_GET_BE32(statebuf);
685 sess = radius_server_get_session(client, state);
686 } else {
687 sess = NULL;
691 if (sess) {
692 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
693 } else if (state_included) {
694 RADIUS_DEBUG("State attribute included but no session found");
695 radius_server_reject(data, client, msg, from, fromlen,
696 from_addr, from_port);
697 return -1;
698 } else {
699 sess = radius_server_get_new_session(data, client, msg);
700 if (sess == NULL) {
701 RADIUS_DEBUG("Could not create a new session");
702 radius_server_reject(data, client, msg, from, fromlen,
703 from_addr, from_port);
704 return -1;
708 if (sess->last_from_port == from_port &&
709 sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
710 os_memcmp(sess->last_authenticator,
711 radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
712 RADIUS_DEBUG("Duplicate message from %s", from_addr);
713 data->counters.dup_access_requests++;
714 client->counters.dup_access_requests++;
716 if (sess->last_reply) {
717 struct wpabuf *buf;
718 buf = radius_msg_get_buf(sess->last_reply);
719 res = sendto(data->auth_sock, wpabuf_head(buf),
720 wpabuf_len(buf), 0,
721 (struct sockaddr *) from, fromlen);
722 if (res < 0) {
723 perror("sendto[RADIUS SRV]");
725 return 0;
728 RADIUS_DEBUG("No previous reply available for duplicate "
729 "message");
730 return -1;
733 eap = radius_msg_get_eap(msg, &eap_len);
734 if (eap == NULL) {
735 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
736 from_addr);
737 data->counters.packets_dropped++;
738 client->counters.packets_dropped++;
739 return -1;
742 RADIUS_DUMP("Received EAP data", eap, eap_len);
744 /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
745 * RFC3579 Sect. 2.6.2.
746 * Include EAP-Response/Nak with no preferred method if
747 * code == request.
748 * If code is not 1-4, discard the packet silently.
749 * Or is this already done by the EAP state machine? */
751 wpabuf_free(sess->eap_if->eapRespData);
752 sess->eap_if->eapRespData = wpabuf_alloc_ext_data(eap, eap_len);
753 if (sess->eap_if->eapRespData == NULL)
754 os_free(eap);
755 eap = NULL;
756 sess->eap_if->eapResp = TRUE;
757 eap_server_sm_step(sess->eap);
759 if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
760 sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
761 RADIUS_DUMP("EAP data from the state machine",
762 wpabuf_head(sess->eap_if->eapReqData),
763 wpabuf_len(sess->eap_if->eapReqData));
764 } else if (sess->eap_if->eapFail) {
765 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
766 "set");
767 } else if (eap_sm_method_pending(sess->eap)) {
768 radius_msg_free(sess->last_msg);
769 sess->last_msg = msg;
770 sess->last_from_port = from_port;
771 os_free(sess->last_from_addr);
772 sess->last_from_addr = os_strdup(from_addr);
773 sess->last_fromlen = fromlen;
774 os_memcpy(&sess->last_from, from, fromlen);
775 return -2;
776 } else {
777 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
778 " Access-Request silently (assuming it was a "
779 "duplicate)");
780 data->counters.packets_dropped++;
781 client->counters.packets_dropped++;
782 return -1;
785 if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
786 is_complete = 1;
788 reply = radius_server_encapsulate_eap(data, client, sess, msg);
790 if (reply) {
791 struct wpabuf *buf;
792 struct radius_hdr *hdr;
794 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
795 if (wpa_debug_level <= MSG_MSGDUMP) {
796 radius_msg_dump(reply);
799 switch (radius_msg_get_hdr(reply)->code) {
800 case RADIUS_CODE_ACCESS_ACCEPT:
801 data->counters.access_accepts++;
802 client->counters.access_accepts++;
803 break;
804 case RADIUS_CODE_ACCESS_REJECT:
805 data->counters.access_rejects++;
806 client->counters.access_rejects++;
807 break;
808 case RADIUS_CODE_ACCESS_CHALLENGE:
809 data->counters.access_challenges++;
810 client->counters.access_challenges++;
811 break;
813 buf = radius_msg_get_buf(reply);
814 res = sendto(data->auth_sock, wpabuf_head(buf),
815 wpabuf_len(buf), 0,
816 (struct sockaddr *) from, fromlen);
817 if (res < 0) {
818 perror("sendto[RADIUS SRV]");
820 radius_msg_free(sess->last_reply);
821 sess->last_reply = reply;
822 sess->last_from_port = from_port;
823 hdr = radius_msg_get_hdr(msg);
824 sess->last_identifier = hdr->identifier;
825 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
826 } else {
827 data->counters.packets_dropped++;
828 client->counters.packets_dropped++;
831 if (is_complete) {
832 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
833 sess->sess_id);
834 eloop_cancel_timeout(radius_server_session_remove_timeout,
835 data, sess);
836 eloop_register_timeout(10, 0,
837 radius_server_session_remove_timeout,
838 data, sess);
841 return 0;
845 static void radius_server_receive_auth(int sock, void *eloop_ctx,
846 void *sock_ctx)
848 struct radius_server_data *data = eloop_ctx;
849 u8 *buf = NULL;
850 union {
851 struct sockaddr_storage ss;
852 struct sockaddr_in sin;
853 #ifdef CONFIG_IPV6
854 struct sockaddr_in6 sin6;
855 #endif /* CONFIG_IPV6 */
856 } from;
857 socklen_t fromlen;
858 int len;
859 struct radius_client *client = NULL;
860 struct radius_msg *msg = NULL;
861 char abuf[50];
862 int from_port = 0;
864 buf = os_malloc(RADIUS_MAX_MSG_LEN);
865 if (buf == NULL) {
866 goto fail;
869 fromlen = sizeof(from);
870 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
871 (struct sockaddr *) &from.ss, &fromlen);
872 if (len < 0) {
873 perror("recvfrom[radius_server]");
874 goto fail;
877 #ifdef CONFIG_IPV6
878 if (data->ipv6) {
879 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
880 sizeof(abuf)) == NULL)
881 abuf[0] = '\0';
882 from_port = ntohs(from.sin6.sin6_port);
883 RADIUS_DEBUG("Received %d bytes from %s:%d",
884 len, abuf, from_port);
886 client = radius_server_get_client(data,
887 (struct in_addr *)
888 &from.sin6.sin6_addr, 1);
890 #endif /* CONFIG_IPV6 */
892 if (!data->ipv6) {
893 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
894 from_port = ntohs(from.sin.sin_port);
895 RADIUS_DEBUG("Received %d bytes from %s:%d",
896 len, abuf, from_port);
898 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
901 RADIUS_DUMP("Received data", buf, len);
903 if (client == NULL) {
904 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
905 data->counters.invalid_requests++;
906 goto fail;
909 msg = radius_msg_parse(buf, len);
910 if (msg == NULL) {
911 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
912 data->counters.malformed_access_requests++;
913 client->counters.malformed_access_requests++;
914 goto fail;
917 os_free(buf);
918 buf = NULL;
920 if (wpa_debug_level <= MSG_MSGDUMP) {
921 radius_msg_dump(msg);
924 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
925 RADIUS_DEBUG("Unexpected RADIUS code %d",
926 radius_msg_get_hdr(msg)->code);
927 data->counters.unknown_types++;
928 client->counters.unknown_types++;
929 goto fail;
932 data->counters.access_requests++;
933 client->counters.access_requests++;
935 if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
936 client->shared_secret_len, NULL)) {
937 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
938 data->counters.bad_authenticators++;
939 client->counters.bad_authenticators++;
940 goto fail;
943 if (radius_server_request(data, msg, (struct sockaddr *) &from,
944 fromlen, client, abuf, from_port, NULL) ==
946 return; /* msg was stored with the session */
948 fail:
949 radius_msg_free(msg);
950 os_free(buf);
954 static int radius_server_disable_pmtu_discovery(int s)
956 int r = -1;
957 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
958 /* Turn off Path MTU discovery on IPv4/UDP sockets. */
959 int action = IP_PMTUDISC_DONT;
960 r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
961 sizeof(action));
962 if (r == -1)
963 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
964 "%s", strerror(errno));
965 #endif
966 return r;
970 static int radius_server_open_socket(int port)
972 int s;
973 struct sockaddr_in addr;
975 s = socket(PF_INET, SOCK_DGRAM, 0);
976 if (s < 0) {
977 perror("socket");
978 return -1;
981 radius_server_disable_pmtu_discovery(s);
983 os_memset(&addr, 0, sizeof(addr));
984 addr.sin_family = AF_INET;
985 addr.sin_port = htons(port);
986 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
987 perror("bind");
988 close(s);
989 return -1;
992 return s;
996 #ifdef CONFIG_IPV6
997 static int radius_server_open_socket6(int port)
999 int s;
1000 struct sockaddr_in6 addr;
1002 s = socket(PF_INET6, SOCK_DGRAM, 0);
1003 if (s < 0) {
1004 perror("socket[IPv6]");
1005 return -1;
1008 os_memset(&addr, 0, sizeof(addr));
1009 addr.sin6_family = AF_INET6;
1010 os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
1011 addr.sin6_port = htons(port);
1012 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1013 perror("bind");
1014 close(s);
1015 return -1;
1018 return s;
1020 #endif /* CONFIG_IPV6 */
1023 static void radius_server_free_sessions(struct radius_server_data *data,
1024 struct radius_session *sessions)
1026 struct radius_session *session, *prev;
1028 session = sessions;
1029 while (session) {
1030 prev = session;
1031 session = session->next;
1032 radius_server_session_free(data, prev);
1037 static void radius_server_free_clients(struct radius_server_data *data,
1038 struct radius_client *clients)
1040 struct radius_client *client, *prev;
1042 client = clients;
1043 while (client) {
1044 prev = client;
1045 client = client->next;
1047 radius_server_free_sessions(data, prev->sessions);
1048 os_free(prev->shared_secret);
1049 os_free(prev);
1054 static struct radius_client *
1055 radius_server_read_clients(const char *client_file, int ipv6)
1057 FILE *f;
1058 const int buf_size = 1024;
1059 char *buf, *pos;
1060 struct radius_client *clients, *tail, *entry;
1061 int line = 0, mask, failed = 0, i;
1062 struct in_addr addr;
1063 #ifdef CONFIG_IPV6
1064 struct in6_addr addr6;
1065 #endif /* CONFIG_IPV6 */
1066 unsigned int val;
1068 f = fopen(client_file, "r");
1069 if (f == NULL) {
1070 RADIUS_ERROR("Could not open client file '%s'", client_file);
1071 return NULL;
1074 buf = os_malloc(buf_size);
1075 if (buf == NULL) {
1076 fclose(f);
1077 return NULL;
1080 clients = tail = NULL;
1081 while (fgets(buf, buf_size, f)) {
1082 /* Configuration file format:
1083 * 192.168.1.0/24 secret
1084 * 192.168.1.2 secret
1085 * fe80::211:22ff:fe33:4455/64 secretipv6
1087 line++;
1088 buf[buf_size - 1] = '\0';
1089 pos = buf;
1090 while (*pos != '\0' && *pos != '\n')
1091 pos++;
1092 if (*pos == '\n')
1093 *pos = '\0';
1094 if (*buf == '\0' || *buf == '#')
1095 continue;
1097 pos = buf;
1098 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
1099 (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
1100 (*pos >= 'A' && *pos <= 'F')) {
1101 pos++;
1104 if (*pos == '\0') {
1105 failed = 1;
1106 break;
1109 if (*pos == '/') {
1110 char *end;
1111 *pos++ = '\0';
1112 mask = strtol(pos, &end, 10);
1113 if ((pos == end) ||
1114 (mask < 0 || mask > (ipv6 ? 128 : 32))) {
1115 failed = 1;
1116 break;
1118 pos = end;
1119 } else {
1120 mask = ipv6 ? 128 : 32;
1121 *pos++ = '\0';
1124 if (!ipv6 && inet_aton(buf, &addr) == 0) {
1125 failed = 1;
1126 break;
1128 #ifdef CONFIG_IPV6
1129 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
1130 if (inet_pton(AF_INET, buf, &addr) <= 0) {
1131 failed = 1;
1132 break;
1134 /* Convert IPv4 address to IPv6 */
1135 if (mask <= 32)
1136 mask += (128 - 32);
1137 os_memset(addr6.s6_addr, 0, 10);
1138 addr6.s6_addr[10] = 0xff;
1139 addr6.s6_addr[11] = 0xff;
1140 os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
1143 #endif /* CONFIG_IPV6 */
1145 while (*pos == ' ' || *pos == '\t') {
1146 pos++;
1149 if (*pos == '\0') {
1150 failed = 1;
1151 break;
1154 entry = os_zalloc(sizeof(*entry));
1155 if (entry == NULL) {
1156 failed = 1;
1157 break;
1159 entry->shared_secret = os_strdup(pos);
1160 if (entry->shared_secret == NULL) {
1161 failed = 1;
1162 os_free(entry);
1163 break;
1165 entry->shared_secret_len = os_strlen(entry->shared_secret);
1166 entry->addr.s_addr = addr.s_addr;
1167 if (!ipv6) {
1168 val = 0;
1169 for (i = 0; i < mask; i++)
1170 val |= 1 << (31 - i);
1171 entry->mask.s_addr = htonl(val);
1173 #ifdef CONFIG_IPV6
1174 if (ipv6) {
1175 int offset = mask / 8;
1177 os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
1178 os_memset(entry->mask6.s6_addr, 0xff, offset);
1179 val = 0;
1180 for (i = 0; i < (mask % 8); i++)
1181 val |= 1 << (7 - i);
1182 if (offset < 16)
1183 entry->mask6.s6_addr[offset] = val;
1185 #endif /* CONFIG_IPV6 */
1187 if (tail == NULL) {
1188 clients = tail = entry;
1189 } else {
1190 tail->next = entry;
1191 tail = entry;
1195 if (failed) {
1196 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
1197 radius_server_free_clients(NULL, clients);
1198 clients = NULL;
1201 os_free(buf);
1202 fclose(f);
1204 return clients;
1209 * radius_server_init - Initialize RADIUS server
1210 * @conf: Configuration for the RADIUS server
1211 * Returns: Pointer to private RADIUS server context or %NULL on failure
1213 * This initializes a RADIUS server instance and returns a context pointer that
1214 * will be used in other calls to the RADIUS server module. The server can be
1215 * deinitialize by calling radius_server_deinit().
1217 struct radius_server_data *
1218 radius_server_init(struct radius_server_conf *conf)
1220 struct radius_server_data *data;
1222 #ifndef CONFIG_IPV6
1223 if (conf->ipv6) {
1224 fprintf(stderr, "RADIUS server compiled without IPv6 "
1225 "support.\n");
1226 return NULL;
1228 #endif /* CONFIG_IPV6 */
1230 data = os_zalloc(sizeof(*data));
1231 if (data == NULL)
1232 return NULL;
1234 os_get_time(&data->start_time);
1235 data->conf_ctx = conf->conf_ctx;
1236 data->eap_sim_db_priv = conf->eap_sim_db_priv;
1237 data->ssl_ctx = conf->ssl_ctx;
1238 data->msg_ctx = conf->msg_ctx;
1239 data->ipv6 = conf->ipv6;
1240 if (conf->pac_opaque_encr_key) {
1241 data->pac_opaque_encr_key = os_malloc(16);
1242 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1243 16);
1245 if (conf->eap_fast_a_id) {
1246 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1247 if (data->eap_fast_a_id) {
1248 os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1249 conf->eap_fast_a_id_len);
1250 data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1253 if (conf->eap_fast_a_id_info)
1254 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1255 data->eap_fast_prov = conf->eap_fast_prov;
1256 data->pac_key_lifetime = conf->pac_key_lifetime;
1257 data->pac_key_refresh_time = conf->pac_key_refresh_time;
1258 data->get_eap_user = conf->get_eap_user;
1259 data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1260 data->tnc = conf->tnc;
1261 data->wps = conf->wps;
1262 if (conf->eap_req_id_text) {
1263 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
1264 if (data->eap_req_id_text) {
1265 os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
1266 conf->eap_req_id_text_len);
1267 data->eap_req_id_text_len = conf->eap_req_id_text_len;
1271 data->clients = radius_server_read_clients(conf->client_file,
1272 conf->ipv6);
1273 if (data->clients == NULL) {
1274 printf("No RADIUS clients configured.\n");
1275 radius_server_deinit(data);
1276 return NULL;
1279 #ifdef CONFIG_IPV6
1280 if (conf->ipv6)
1281 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1282 else
1283 #endif /* CONFIG_IPV6 */
1284 data->auth_sock = radius_server_open_socket(conf->auth_port);
1285 if (data->auth_sock < 0) {
1286 printf("Failed to open UDP socket for RADIUS authentication "
1287 "server\n");
1288 radius_server_deinit(data);
1289 return NULL;
1291 if (eloop_register_read_sock(data->auth_sock,
1292 radius_server_receive_auth,
1293 data, NULL)) {
1294 radius_server_deinit(data);
1295 return NULL;
1298 return data;
1303 * radius_server_deinit - Deinitialize RADIUS server
1304 * @data: RADIUS server context from radius_server_init()
1306 void radius_server_deinit(struct radius_server_data *data)
1308 if (data == NULL)
1309 return;
1311 if (data->auth_sock >= 0) {
1312 eloop_unregister_read_sock(data->auth_sock);
1313 close(data->auth_sock);
1316 radius_server_free_clients(data, data->clients);
1318 os_free(data->pac_opaque_encr_key);
1319 os_free(data->eap_fast_a_id);
1320 os_free(data->eap_fast_a_id_info);
1321 os_free(data->eap_req_id_text);
1322 os_free(data);
1327 * radius_server_get_mib - Get RADIUS server MIB information
1328 * @data: RADIUS server context from radius_server_init()
1329 * @buf: Buffer for returning the MIB data in text format
1330 * @buflen: buf length in octets
1331 * Returns: Number of octets written into buf
1333 int radius_server_get_mib(struct radius_server_data *data, char *buf,
1334 size_t buflen)
1336 int ret, uptime;
1337 unsigned int idx;
1338 char *end, *pos;
1339 struct os_time now;
1340 struct radius_client *cli;
1342 /* RFC 2619 - RADIUS Authentication Server MIB */
1344 if (data == NULL || buflen == 0)
1345 return 0;
1347 pos = buf;
1348 end = buf + buflen;
1350 os_get_time(&now);
1351 uptime = (now.sec - data->start_time.sec) * 100 +
1352 ((now.usec - data->start_time.usec) / 10000) % 100;
1353 ret = os_snprintf(pos, end - pos,
1354 "RADIUS-AUTH-SERVER-MIB\n"
1355 "radiusAuthServIdent=hostapd\n"
1356 "radiusAuthServUpTime=%d\n"
1357 "radiusAuthServResetTime=0\n"
1358 "radiusAuthServConfigReset=4\n",
1359 uptime);
1360 if (ret < 0 || ret >= end - pos) {
1361 *pos = '\0';
1362 return pos - buf;
1364 pos += ret;
1366 ret = os_snprintf(pos, end - pos,
1367 "radiusAuthServTotalAccessRequests=%u\n"
1368 "radiusAuthServTotalInvalidRequests=%u\n"
1369 "radiusAuthServTotalDupAccessRequests=%u\n"
1370 "radiusAuthServTotalAccessAccepts=%u\n"
1371 "radiusAuthServTotalAccessRejects=%u\n"
1372 "radiusAuthServTotalAccessChallenges=%u\n"
1373 "radiusAuthServTotalMalformedAccessRequests=%u\n"
1374 "radiusAuthServTotalBadAuthenticators=%u\n"
1375 "radiusAuthServTotalPacketsDropped=%u\n"
1376 "radiusAuthServTotalUnknownTypes=%u\n",
1377 data->counters.access_requests,
1378 data->counters.invalid_requests,
1379 data->counters.dup_access_requests,
1380 data->counters.access_accepts,
1381 data->counters.access_rejects,
1382 data->counters.access_challenges,
1383 data->counters.malformed_access_requests,
1384 data->counters.bad_authenticators,
1385 data->counters.packets_dropped,
1386 data->counters.unknown_types);
1387 if (ret < 0 || ret >= end - pos) {
1388 *pos = '\0';
1389 return pos - buf;
1391 pos += ret;
1393 for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1394 char abuf[50], mbuf[50];
1395 #ifdef CONFIG_IPV6
1396 if (data->ipv6) {
1397 if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1398 sizeof(abuf)) == NULL)
1399 abuf[0] = '\0';
1400 if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1401 sizeof(mbuf)) == NULL)
1402 mbuf[0] = '\0';
1404 #endif /* CONFIG_IPV6 */
1405 if (!data->ipv6) {
1406 os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1407 os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1410 ret = os_snprintf(pos, end - pos,
1411 "radiusAuthClientIndex=%u\n"
1412 "radiusAuthClientAddress=%s/%s\n"
1413 "radiusAuthServAccessRequests=%u\n"
1414 "radiusAuthServDupAccessRequests=%u\n"
1415 "radiusAuthServAccessAccepts=%u\n"
1416 "radiusAuthServAccessRejects=%u\n"
1417 "radiusAuthServAccessChallenges=%u\n"
1418 "radiusAuthServMalformedAccessRequests=%u\n"
1419 "radiusAuthServBadAuthenticators=%u\n"
1420 "radiusAuthServPacketsDropped=%u\n"
1421 "radiusAuthServUnknownTypes=%u\n",
1422 idx,
1423 abuf, mbuf,
1424 cli->counters.access_requests,
1425 cli->counters.dup_access_requests,
1426 cli->counters.access_accepts,
1427 cli->counters.access_rejects,
1428 cli->counters.access_challenges,
1429 cli->counters.malformed_access_requests,
1430 cli->counters.bad_authenticators,
1431 cli->counters.packets_dropped,
1432 cli->counters.unknown_types);
1433 if (ret < 0 || ret >= end - pos) {
1434 *pos = '\0';
1435 return pos - buf;
1437 pos += ret;
1440 return pos - buf;
1444 static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1445 size_t identity_len, int phase2,
1446 struct eap_user *user)
1448 struct radius_session *sess = ctx;
1449 struct radius_server_data *data = sess->server;
1451 return data->get_eap_user(data->conf_ctx, identity, identity_len,
1452 phase2, user);
1456 static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
1458 struct radius_session *sess = ctx;
1459 struct radius_server_data *data = sess->server;
1460 *len = data->eap_req_id_text_len;
1461 return data->eap_req_id_text;
1465 static struct eapol_callbacks radius_server_eapol_cb =
1467 .get_eap_user = radius_server_get_eap_user,
1468 .get_eap_req_id_text = radius_server_get_eap_req_id_text,
1473 * radius_server_eap_pending_cb - Pending EAP data notification
1474 * @data: RADIUS server context from radius_server_init()
1475 * @ctx: Pending EAP context pointer
1477 * This function is used to notify EAP server module that a pending operation
1478 * has been completed and processing of the EAP session can proceed.
1480 void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1482 struct radius_client *cli;
1483 struct radius_session *s, *sess = NULL;
1484 struct radius_msg *msg;
1486 if (data == NULL)
1487 return;
1489 for (cli = data->clients; cli; cli = cli->next) {
1490 for (s = cli->sessions; s; s = s->next) {
1491 if (s->eap == ctx && s->last_msg) {
1492 sess = s;
1493 break;
1495 if (sess)
1496 break;
1498 if (sess)
1499 break;
1502 if (sess == NULL) {
1503 RADIUS_DEBUG("No session matched callback ctx");
1504 return;
1507 msg = sess->last_msg;
1508 sess->last_msg = NULL;
1509 eap_sm_pending_cb(sess->eap);
1510 if (radius_server_request(data, msg,
1511 (struct sockaddr *) &sess->last_from,
1512 sess->last_fromlen, cli,
1513 sess->last_from_addr,
1514 sess->last_from_port, sess) == -2)
1515 return; /* msg was stored with the session */
1517 radius_msg_free(msg);