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
;
285 * msg_ctx - Context data for wpa_msg() calls
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
,
308 static struct radius_client
*
309 radius_server_get_client(struct radius_server_data
*data
, struct in_addr
*addr
,
312 struct radius_client
*client
= data
->clients
;
317 struct in6_addr
*addr6
;
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
])) {
334 #endif /* CONFIG_IPV6 */
335 if (!ipv6
&& (client
->addr
.s_addr
& client
->mask
.s_addr
) ==
336 (addr
->s_addr
& client
->mask
.s_addr
)) {
340 client
= client
->next
;
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
;
353 if (sess
->sess_id
== sess_id
) {
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
);
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
);
386 session
= client
->sessions
;
388 if (session
== sess
) {
390 client
->sessions
= sess
->next
;
392 prev
->next
= sess
->next
;
394 radius_server_session_free(data
, sess
);
398 session
= session
->next
;
403 static void radius_server_session_remove_timeout(void *eloop_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");
435 sess
= os_zalloc(sizeof(*sess
));
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
);
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
)
459 struct radius_session
*sess
;
460 struct eap_config eap_conf
;
462 RADIUS_DEBUG("Creating a new session");
464 user
= os_malloc(256);
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");
475 RADIUS_DUMP_ASCII("User-Name", user
, user_len
);
477 res
= data
->get_eap_user(data
->conf_ctx
, user
, user_len
, 0, NULL
);
481 RADIUS_DEBUG("Matching user entry found");
482 sess
= radius_server_new_session(data
, client
);
484 RADIUS_DEBUG("Failed to create a new session");
488 RADIUS_DEBUG("User-Name not found from user database");
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
,
510 if (sess
->eap
== NULL
) {
511 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
513 radius_server_session_free(data
, sess
);
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
);
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
;
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
;
544 sess
->eap_if
->eapReq
= FALSE
;
545 code
= RADIUS_CODE_ACCESS_CHALLENGE
;
548 msg
= radius_msg_new(code
, hdr
->identifier
);
550 RADIUS_DEBUG("Failed to allocate reply message");
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
) {
569 if (sess
->eap_if
->eapKeyDataLen
> 64) {
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
,
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
);
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");
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
;
608 struct eap_hdr eapfail
;
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
);
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
);
635 if (radius_msg_finish_srv(msg
, (u8
*) client
->shared_secret
,
636 client
->shared_secret_len
,
637 hdr
->authenticator
) <
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]");
655 radius_msg_free(msg
);
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
)
670 int res
, state_included
= 0;
673 struct radius_session
*sess
;
674 struct radius_msg
*reply
;
680 res
= radius_msg_get_attr(msg
, RADIUS_ATTR_STATE
, 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
);
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
);
699 sess
= radius_server_get_new_session(data
, client
, msg
);
701 RADIUS_DEBUG("Could not create a new session");
702 radius_server_reject(data
, client
, msg
, from
, fromlen
,
703 from_addr
, from_port
);
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
) {
718 buf
= radius_msg_get_buf(sess
->last_reply
);
719 res
= sendto(data
->auth_sock
, wpabuf_head(buf
),
721 (struct sockaddr
*) from
, fromlen
);
723 perror("sendto[RADIUS SRV]");
728 RADIUS_DEBUG("No previous reply available for duplicate "
733 eap
= radius_msg_get_eap(msg
, &eap_len
);
735 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
737 data
->counters
.packets_dropped
++;
738 client
->counters
.packets_dropped
++;
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
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
)
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 "
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
);
777 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
778 " Access-Request silently (assuming it was a "
780 data
->counters
.packets_dropped
++;
781 client
->counters
.packets_dropped
++;
785 if (sess
->eap_if
->eapSuccess
|| sess
->eap_if
->eapFail
)
788 reply
= radius_server_encapsulate_eap(data
, client
, sess
, msg
);
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
++;
804 case RADIUS_CODE_ACCESS_REJECT
:
805 data
->counters
.access_rejects
++;
806 client
->counters
.access_rejects
++;
808 case RADIUS_CODE_ACCESS_CHALLENGE
:
809 data
->counters
.access_challenges
++;
810 client
->counters
.access_challenges
++;
813 buf
= radius_msg_get_buf(reply
);
814 res
= sendto(data
->auth_sock
, wpabuf_head(buf
),
816 (struct sockaddr
*) from
, fromlen
);
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);
827 data
->counters
.packets_dropped
++;
828 client
->counters
.packets_dropped
++;
832 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
834 eloop_cancel_timeout(radius_server_session_remove_timeout
,
836 eloop_register_timeout(10, 0,
837 radius_server_session_remove_timeout
,
845 static void radius_server_receive_auth(int sock
, void *eloop_ctx
,
848 struct radius_server_data
*data
= eloop_ctx
;
851 struct sockaddr_storage ss
;
852 struct sockaddr_in sin
;
854 struct sockaddr_in6 sin6
;
855 #endif /* CONFIG_IPV6 */
859 struct radius_client
*client
= NULL
;
860 struct radius_msg
*msg
= NULL
;
864 buf
= os_malloc(RADIUS_MAX_MSG_LEN
);
869 fromlen
= sizeof(from
);
870 len
= recvfrom(sock
, buf
, RADIUS_MAX_MSG_LEN
, 0,
871 (struct sockaddr
*) &from
.ss
, &fromlen
);
873 perror("recvfrom[radius_server]");
879 if (inet_ntop(AF_INET6
, &from
.sin6
.sin6_addr
, abuf
,
880 sizeof(abuf
)) == NULL
)
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
,
888 &from
.sin6
.sin6_addr
, 1);
890 #endif /* CONFIG_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
++;
909 msg
= radius_msg_parse(buf
, len
);
911 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
912 data
->counters
.malformed_access_requests
++;
913 client
->counters
.malformed_access_requests
++;
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
++;
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
++;
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 */
949 radius_msg_free(msg
);
954 static int radius_server_disable_pmtu_discovery(int s
)
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
,
963 wpa_printf(MSG_ERROR
, "Failed to set IP_MTU_DISCOVER: "
964 "%s", strerror(errno
));
970 static int radius_server_open_socket(int port
)
973 struct sockaddr_in addr
;
975 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
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) {
997 static int radius_server_open_socket6(int port
)
1000 struct sockaddr_in6 addr
;
1002 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
1004 perror("socket[IPv6]");
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) {
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
;
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
;
1045 client
= client
->next
;
1047 radius_server_free_sessions(data
, prev
->sessions
);
1048 os_free(prev
->shared_secret
);
1054 static struct radius_client
*
1055 radius_server_read_clients(const char *client_file
, int ipv6
)
1058 const int buf_size
= 1024;
1060 struct radius_client
*clients
, *tail
, *entry
;
1061 int line
= 0, mask
, failed
= 0, i
;
1062 struct in_addr addr
;
1064 struct in6_addr addr6
;
1065 #endif /* CONFIG_IPV6 */
1068 f
= fopen(client_file
, "r");
1070 RADIUS_ERROR("Could not open client file '%s'", client_file
);
1074 buf
= os_malloc(buf_size
);
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
1088 buf
[buf_size
- 1] = '\0';
1090 while (*pos
!= '\0' && *pos
!= '\n')
1094 if (*buf
== '\0' || *buf
== '#')
1098 while ((*pos
>= '0' && *pos
<= '9') || *pos
== '.' ||
1099 (*pos
>= 'a' && *pos
<= 'f') || *pos
== ':' ||
1100 (*pos
>= 'A' && *pos
<= 'F')) {
1112 mask
= strtol(pos
, &end
, 10);
1114 (mask
< 0 || mask
> (ipv6
? 128 : 32))) {
1120 mask
= ipv6
? 128 : 32;
1124 if (!ipv6
&& inet_aton(buf
, &addr
) == 0) {
1129 if (ipv6
&& inet_pton(AF_INET6
, buf
, &addr6
) <= 0) {
1130 if (inet_pton(AF_INET
, buf
, &addr
) <= 0) {
1134 /* Convert IPv4 address to IPv6 */
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') {
1154 entry
= os_zalloc(sizeof(*entry
));
1155 if (entry
== NULL
) {
1159 entry
->shared_secret
= os_strdup(pos
);
1160 if (entry
->shared_secret
== NULL
) {
1165 entry
->shared_secret_len
= os_strlen(entry
->shared_secret
);
1166 entry
->addr
.s_addr
= addr
.s_addr
;
1169 for (i
= 0; i
< mask
; i
++)
1170 val
|= 1 << (31 - i
);
1171 entry
->mask
.s_addr
= htonl(val
);
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
);
1180 for (i
= 0; i
< (mask
% 8); i
++)
1181 val
|= 1 << (7 - i
);
1183 entry
->mask6
.s6_addr
[offset
] = val
;
1185 #endif /* CONFIG_IPV6 */
1188 clients
= tail
= entry
;
1196 RADIUS_ERROR("Invalid line %d in '%s'", line
, client_file
);
1197 radius_server_free_clients(NULL
, 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
;
1224 fprintf(stderr
, "RADIUS server compiled without IPv6 "
1228 #endif /* CONFIG_IPV6 */
1230 data
= os_zalloc(sizeof(*data
));
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
,
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
,
1273 if (data
->clients
== NULL
) {
1274 printf("No RADIUS clients configured.\n");
1275 radius_server_deinit(data
);
1281 data
->auth_sock
= radius_server_open_socket6(conf
->auth_port
);
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 "
1288 radius_server_deinit(data
);
1291 if (eloop_register_read_sock(data
->auth_sock
,
1292 radius_server_receive_auth
,
1294 radius_server_deinit(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
)
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
);
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
,
1340 struct radius_client
*cli
;
1342 /* RFC 2619 - RADIUS Authentication Server MIB */
1344 if (data
== NULL
|| buflen
== 0)
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",
1360 if (ret
< 0 || ret
>= end
- pos
) {
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
) {
1393 for (cli
= data
->clients
, idx
= 0; cli
; cli
= cli
->next
, idx
++) {
1394 char abuf
[50], mbuf
[50];
1397 if (inet_ntop(AF_INET6
, &cli
->addr6
, abuf
,
1398 sizeof(abuf
)) == NULL
)
1400 if (inet_ntop(AF_INET6
, &cli
->mask6
, abuf
,
1401 sizeof(mbuf
)) == NULL
)
1404 #endif /* CONFIG_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",
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
) {
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
,
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
;
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
) {
1503 RADIUS_DEBUG("No session matched callback ctx");
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
);