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
12 * See README and COPYING for more details.
21 #include "eap_server/eap.h"
22 #include "radius_server.h"
25 * RADIUS_SESSION_TIMEOUT - Session timeout in seconds
27 #define RADIUS_SESSION_TIMEOUT 60
30 * RADIUS_MAX_SESSION - Maximum number of active sessions
32 #define RADIUS_MAX_SESSION 100
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
;
42 struct radius_server_data
;
45 * struct radius_server_counters - RADIUS server statistics counters
47 struct radius_server_counters
{
50 u32 dup_access_requests
;
53 u32 access_challenges
;
54 u32 malformed_access_requests
;
55 u32 bad_authenticators
;
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
;
69 struct eap_eapol_interface
*eap_if
;
71 struct radius_msg
*last_msg
;
74 struct sockaddr_storage last_from
;
75 socklen_t last_fromlen
;
77 struct radius_msg
*last_reply
;
78 u8 last_authenticator
[16];
82 * struct radius_client - Internal RADIUS server data for a client
84 struct radius_client
{
85 struct radius_client
*next
;
89 struct in6_addr addr6
;
90 struct in6_addr mask6
;
91 #endif /* CONFIG_IPV6 */
93 int shared_secret_len
;
94 struct radius_session
*sessions
;
95 struct radius_server_counters counters
;
99 * struct radius_server_data - Internal RADIUS server data
101 struct radius_server_data
{
103 * auth_sock - Socket for RADIUS authentication messages
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.
125 * num_sess - Number of active sessions
130 * eap_sim_db_priv - EAP-SIM/AKA database context
132 * This is passed to the EAP-SIM/AKA server implementation as a
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.
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.
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
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
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
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.
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
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
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
,
303 static struct radius_client
*
304 radius_server_get_client(struct radius_server_data
*data
, struct in_addr
*addr
,
307 struct radius_client
*client
= data
->clients
;
312 struct in6_addr
*addr6
;
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
])) {
329 #endif /* CONFIG_IPV6 */
330 if (!ipv6
&& (client
->addr
.s_addr
& client
->mask
.s_addr
) ==
331 (addr
->s_addr
& client
->mask
.s_addr
)) {
335 client
= client
->next
;
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
;
348 if (sess
->sess_id
== sess_id
) {
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
);
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
);
381 session
= client
->sessions
;
383 if (session
== sess
) {
385 client
->sessions
= sess
->next
;
387 prev
->next
= sess
->next
;
389 radius_server_session_free(data
, sess
);
393 session
= session
->next
;
398 static void radius_server_session_remove_timeout(void *eloop_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");
430 sess
= os_zalloc(sizeof(*sess
));
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
);
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
)
454 struct radius_session
*sess
;
455 struct eap_config eap_conf
;
457 RADIUS_DEBUG("Creating a new session");
459 user
= os_malloc(256);
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");
470 RADIUS_DUMP_ASCII("User-Name", user
, user_len
);
472 res
= data
->get_eap_user(data
->conf_ctx
, user
, user_len
, 0, NULL
);
476 RADIUS_DEBUG("Matching user entry found");
477 sess
= radius_server_new_session(data
, client
);
479 RADIUS_DEBUG("Failed to create a new session");
483 RADIUS_DEBUG("User-Name not found from user database");
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
,
504 if (sess
->eap
== NULL
) {
505 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
507 radius_server_session_free(data
, sess
);
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
);
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
;
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
;
538 sess
->eap_if
->eapReq
= FALSE
;
539 code
= RADIUS_CODE_ACCESS_CHALLENGE
;
542 msg
= radius_msg_new(code
, hdr
->identifier
);
544 RADIUS_DEBUG("Failed to allocate reply message");
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
) {
563 if (sess
->eap_if
->eapKeyDataLen
> 64) {
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
,
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
);
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");
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
;
602 struct eap_hdr eapfail
;
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
);
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
);
629 if (radius_msg_finish_srv(msg
, (u8
*) client
->shared_secret
,
630 client
->shared_secret_len
,
631 hdr
->authenticator
) <
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]");
649 radius_msg_free(msg
);
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
)
664 int res
, state_included
= 0;
667 struct radius_session
*sess
;
668 struct radius_msg
*reply
;
674 res
= radius_msg_get_attr(msg
, RADIUS_ATTR_STATE
, 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
);
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
);
693 sess
= radius_server_get_new_session(data
, client
, msg
);
695 RADIUS_DEBUG("Could not create a new session");
696 radius_server_reject(data
, client
, msg
, from
, fromlen
,
697 from_addr
, from_port
);
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
) {
712 buf
= radius_msg_get_buf(sess
->last_reply
);
713 res
= sendto(data
->auth_sock
, wpabuf_head(buf
),
715 (struct sockaddr
*) from
, fromlen
);
717 perror("sendto[RADIUS SRV]");
722 RADIUS_DEBUG("No previous reply available for duplicate "
727 eap
= radius_msg_get_eap(msg
, &eap_len
);
729 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
731 data
->counters
.packets_dropped
++;
732 client
->counters
.packets_dropped
++;
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
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
)
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 "
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
);
771 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
772 " Access-Request silently (assuming it was a "
774 data
->counters
.packets_dropped
++;
775 client
->counters
.packets_dropped
++;
779 if (sess
->eap_if
->eapSuccess
|| sess
->eap_if
->eapFail
)
782 reply
= radius_server_encapsulate_eap(data
, client
, sess
, msg
);
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
++;
798 case RADIUS_CODE_ACCESS_REJECT
:
799 data
->counters
.access_rejects
++;
800 client
->counters
.access_rejects
++;
802 case RADIUS_CODE_ACCESS_CHALLENGE
:
803 data
->counters
.access_challenges
++;
804 client
->counters
.access_challenges
++;
807 buf
= radius_msg_get_buf(reply
);
808 res
= sendto(data
->auth_sock
, wpabuf_head(buf
),
810 (struct sockaddr
*) from
, fromlen
);
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);
821 data
->counters
.packets_dropped
++;
822 client
->counters
.packets_dropped
++;
826 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
828 eloop_cancel_timeout(radius_server_session_remove_timeout
,
830 eloop_register_timeout(10, 0,
831 radius_server_session_remove_timeout
,
839 static void radius_server_receive_auth(int sock
, void *eloop_ctx
,
842 struct radius_server_data
*data
= eloop_ctx
;
845 struct sockaddr_storage ss
;
846 struct sockaddr_in sin
;
848 struct sockaddr_in6 sin6
;
849 #endif /* CONFIG_IPV6 */
853 struct radius_client
*client
= NULL
;
854 struct radius_msg
*msg
= NULL
;
858 buf
= os_malloc(RADIUS_MAX_MSG_LEN
);
863 fromlen
= sizeof(from
);
864 len
= recvfrom(sock
, buf
, RADIUS_MAX_MSG_LEN
, 0,
865 (struct sockaddr
*) &from
.ss
, &fromlen
);
867 perror("recvfrom[radius_server]");
873 if (inet_ntop(AF_INET6
, &from
.sin6
.sin6_addr
, abuf
,
874 sizeof(abuf
)) == NULL
)
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
,
882 &from
.sin6
.sin6_addr
, 1);
884 #endif /* CONFIG_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
++;
903 msg
= radius_msg_parse(buf
, len
);
905 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
906 data
->counters
.malformed_access_requests
++;
907 client
->counters
.malformed_access_requests
++;
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
++;
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
++;
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 */
943 radius_msg_free(msg
);
948 static int radius_server_disable_pmtu_discovery(int s
)
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
,
957 wpa_printf(MSG_ERROR
, "Failed to set IP_MTU_DISCOVER: "
958 "%s", strerror(errno
));
964 static int radius_server_open_socket(int port
)
967 struct sockaddr_in addr
;
969 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
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) {
991 static int radius_server_open_socket6(int port
)
994 struct sockaddr_in6 addr
;
996 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
998 perror("socket[IPv6]");
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) {
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
;
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
;
1039 client
= client
->next
;
1041 radius_server_free_sessions(data
, prev
->sessions
);
1042 os_free(prev
->shared_secret
);
1048 static struct radius_client
*
1049 radius_server_read_clients(const char *client_file
, int ipv6
)
1052 const int buf_size
= 1024;
1054 struct radius_client
*clients
, *tail
, *entry
;
1055 int line
= 0, mask
, failed
= 0, i
;
1056 struct in_addr addr
;
1058 struct in6_addr addr6
;
1059 #endif /* CONFIG_IPV6 */
1062 f
= fopen(client_file
, "r");
1064 RADIUS_ERROR("Could not open client file '%s'", client_file
);
1068 buf
= os_malloc(buf_size
);
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
1082 buf
[buf_size
- 1] = '\0';
1084 while (*pos
!= '\0' && *pos
!= '\n')
1088 if (*buf
== '\0' || *buf
== '#')
1092 while ((*pos
>= '0' && *pos
<= '9') || *pos
== '.' ||
1093 (*pos
>= 'a' && *pos
<= 'f') || *pos
== ':' ||
1094 (*pos
>= 'A' && *pos
<= 'F')) {
1106 mask
= strtol(pos
, &end
, 10);
1108 (mask
< 0 || mask
> (ipv6
? 128 : 32))) {
1114 mask
= ipv6
? 128 : 32;
1118 if (!ipv6
&& inet_aton(buf
, &addr
) == 0) {
1123 if (ipv6
&& inet_pton(AF_INET6
, buf
, &addr6
) <= 0) {
1124 if (inet_pton(AF_INET
, buf
, &addr
) <= 0) {
1128 /* Convert IPv4 address to IPv6 */
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') {
1148 entry
= os_zalloc(sizeof(*entry
));
1149 if (entry
== NULL
) {
1153 entry
->shared_secret
= os_strdup(pos
);
1154 if (entry
->shared_secret
== NULL
) {
1159 entry
->shared_secret_len
= os_strlen(entry
->shared_secret
);
1160 entry
->addr
.s_addr
= addr
.s_addr
;
1163 for (i
= 0; i
< mask
; i
++)
1164 val
|= 1 << (31 - i
);
1165 entry
->mask
.s_addr
= htonl(val
);
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
);
1174 for (i
= 0; i
< (mask
% 8); i
++)
1175 val
|= 1 << (7 - i
);
1177 entry
->mask6
.s6_addr
[offset
] = val
;
1179 #endif /* CONFIG_IPV6 */
1182 clients
= tail
= entry
;
1190 RADIUS_ERROR("Invalid line %d in '%s'", line
, client_file
);
1191 radius_server_free_clients(NULL
, 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
;
1218 fprintf(stderr
, "RADIUS server compiled without IPv6 "
1222 #endif /* CONFIG_IPV6 */
1224 data
= os_zalloc(sizeof(*data
));
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
,
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
,
1266 if (data
->clients
== NULL
) {
1267 printf("No RADIUS clients configured.\n");
1268 radius_server_deinit(data
);
1274 data
->auth_sock
= radius_server_open_socket6(conf
->auth_port
);
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 "
1281 radius_server_deinit(data
);
1284 if (eloop_register_read_sock(data
->auth_sock
,
1285 radius_server_receive_auth
,
1287 radius_server_deinit(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
)
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
);
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
,
1333 struct radius_client
*cli
;
1335 /* RFC 2619 - RADIUS Authentication Server MIB */
1337 if (data
== NULL
|| buflen
== 0)
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",
1353 if (ret
< 0 || ret
>= end
- pos
) {
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
) {
1386 for (cli
= data
->clients
, idx
= 0; cli
; cli
= cli
->next
, idx
++) {
1387 char abuf
[50], mbuf
[50];
1390 if (inet_ntop(AF_INET6
, &cli
->addr6
, abuf
,
1391 sizeof(abuf
)) == NULL
)
1393 if (inet_ntop(AF_INET6
, &cli
->mask6
, abuf
,
1394 sizeof(mbuf
)) == NULL
)
1397 #endif /* CONFIG_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",
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
) {
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
,
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
;
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
) {
1496 RADIUS_DEBUG("No session matched callback ctx");
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
);