2 * hostapd / RADIUS authentication server
3 * Copyright (c) 2005-2008, 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.
22 #include "eap_server/eap.h"
23 #include "radius_server.h"
25 #define RADIUS_SESSION_TIMEOUT 60
26 #define RADIUS_MAX_SESSION 100
27 #define RADIUS_MAX_MSG_LEN 3000
29 static struct eapol_callbacks radius_server_eapol_cb
;
32 struct radius_server_data
;
34 struct radius_server_counters
{
37 u32 dup_access_requests
;
40 u32 access_challenges
;
41 u32 malformed_access_requests
;
42 u32 bad_authenticators
;
47 struct radius_session
{
48 struct radius_session
*next
;
49 struct radius_client
*client
;
50 struct radius_server_data
*server
;
53 struct eap_eapol_interface
*eap_if
;
55 struct radius_msg
*last_msg
;
58 struct sockaddr_storage last_from
;
59 socklen_t last_fromlen
;
61 struct radius_msg
*last_reply
;
62 u8 last_authenticator
[16];
65 struct radius_client
{
66 struct radius_client
*next
;
70 struct in6_addr addr6
;
71 struct in6_addr mask6
;
72 #endif /* CONFIG_IPV6 */
74 int shared_secret_len
;
75 struct radius_session
*sessions
;
76 struct radius_server_counters counters
;
79 struct radius_server_data
{
81 struct radius_client
*clients
;
82 unsigned int next_sess_id
;
85 void *eap_sim_db_priv
;
87 u8
*pac_opaque_encr_key
;
89 int eap_sim_aka_result_ind
;
92 struct os_time start_time
;
93 struct radius_server_counters counters
;
94 int (*get_eap_user
)(void *ctx
, const u8
*identity
, size_t identity_len
,
95 int phase2
, struct eap_user
*user
);
99 extern int wpa_debug_level
;
101 #define RADIUS_DEBUG(args...) \
102 wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
103 #define RADIUS_ERROR(args...) \
104 wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
105 #define RADIUS_DUMP(args...) \
106 wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
107 #define RADIUS_DUMP_ASCII(args...) \
108 wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
111 static void radius_server_session_timeout(void *eloop_ctx
, void *timeout_ctx
);
115 static struct radius_client
*
116 radius_server_get_client(struct radius_server_data
*data
, struct in_addr
*addr
,
119 struct radius_client
*client
= data
->clients
;
124 struct in6_addr
*addr6
;
127 addr6
= (struct in6_addr
*) addr
;
128 for (i
= 0; i
< 16; i
++) {
129 if ((addr6
->s6_addr
[i
] &
130 client
->mask6
.s6_addr
[i
]) !=
131 (client
->addr6
.s6_addr
[i
] &
132 client
->mask6
.s6_addr
[i
])) {
141 #endif /* CONFIG_IPV6 */
142 if (!ipv6
&& (client
->addr
.s_addr
& client
->mask
.s_addr
) ==
143 (addr
->s_addr
& client
->mask
.s_addr
)) {
147 client
= client
->next
;
154 static struct radius_session
*
155 radius_server_get_session(struct radius_client
*client
, unsigned int sess_id
)
157 struct radius_session
*sess
= client
->sessions
;
160 if (sess
->sess_id
== sess_id
) {
170 static void radius_server_session_free(struct radius_server_data
*data
,
171 struct radius_session
*sess
)
173 eloop_cancel_timeout(radius_server_session_timeout
, data
, sess
);
174 eap_server_sm_deinit(sess
->eap
);
175 if (sess
->last_msg
) {
176 radius_msg_free(sess
->last_msg
);
177 os_free(sess
->last_msg
);
179 os_free(sess
->last_from_addr
);
180 if (sess
->last_reply
) {
181 radius_msg_free(sess
->last_reply
);
182 os_free(sess
->last_reply
);
189 static void radius_server_session_remove_timeout(void *eloop_ctx
,
192 static void radius_server_session_remove(struct radius_server_data
*data
,
193 struct radius_session
*sess
)
195 struct radius_client
*client
= sess
->client
;
196 struct radius_session
*session
, *prev
;
198 eloop_cancel_timeout(radius_server_session_remove_timeout
, data
, sess
);
201 session
= client
->sessions
;
203 if (session
== sess
) {
205 client
->sessions
= sess
->next
;
207 prev
->next
= sess
->next
;
209 radius_server_session_free(data
, sess
);
213 session
= session
->next
;
218 static void radius_server_session_remove_timeout(void *eloop_ctx
,
221 struct radius_server_data
*data
= eloop_ctx
;
222 struct radius_session
*sess
= timeout_ctx
;
223 RADIUS_DEBUG("Removing completed session 0x%x", sess
->sess_id
);
224 radius_server_session_remove(data
, sess
);
228 static void radius_server_session_timeout(void *eloop_ctx
, void *timeout_ctx
)
230 struct radius_server_data
*data
= eloop_ctx
;
231 struct radius_session
*sess
= timeout_ctx
;
233 RADIUS_DEBUG("Timing out authentication session 0x%x", sess
->sess_id
);
234 radius_server_session_remove(data
, sess
);
238 static struct radius_session
*
239 radius_server_new_session(struct radius_server_data
*data
,
240 struct radius_client
*client
)
242 struct radius_session
*sess
;
244 if (data
->num_sess
>= RADIUS_MAX_SESSION
) {
245 RADIUS_DEBUG("Maximum number of existing session - no room "
246 "for a new session");
250 sess
= os_zalloc(sizeof(*sess
));
255 sess
->client
= client
;
256 sess
->sess_id
= data
->next_sess_id
++;
257 sess
->next
= client
->sessions
;
258 client
->sessions
= sess
;
259 eloop_register_timeout(RADIUS_SESSION_TIMEOUT
, 0,
260 radius_server_session_timeout
, data
, sess
);
266 static struct radius_session
*
267 radius_server_get_new_session(struct radius_server_data
*data
,
268 struct radius_client
*client
,
269 struct radius_msg
*msg
)
274 struct radius_session
*sess
;
275 struct eap_config eap_conf
;
277 RADIUS_DEBUG("Creating a new session");
279 user
= os_malloc(256);
283 res
= radius_msg_get_attr(msg
, RADIUS_ATTR_USER_NAME
, user
, 256);
284 if (res
< 0 || res
> 256) {
285 RADIUS_DEBUG("Could not get User-Name");
290 RADIUS_DUMP_ASCII("User-Name", user
, user_len
);
292 res
= data
->get_eap_user(data
->conf_ctx
, user
, user_len
, 0, NULL
);
296 RADIUS_DEBUG("Matching user entry found");
297 sess
= radius_server_new_session(data
, client
);
299 RADIUS_DEBUG("Failed to create a new session");
303 RADIUS_DEBUG("User-Name not found from user database");
307 os_memset(&eap_conf
, 0, sizeof(eap_conf
));
308 eap_conf
.ssl_ctx
= data
->ssl_ctx
;
309 eap_conf
.eap_sim_db_priv
= data
->eap_sim_db_priv
;
310 eap_conf
.backend_auth
= TRUE
;
311 eap_conf
.eap_server
= 1;
312 eap_conf
.pac_opaque_encr_key
= data
->pac_opaque_encr_key
;
313 eap_conf
.eap_fast_a_id
= data
->eap_fast_a_id
;
314 eap_conf
.eap_sim_aka_result_ind
= data
->eap_sim_aka_result_ind
;
315 eap_conf
.tnc
= data
->tnc
;
316 sess
->eap
= eap_server_sm_init(sess
, &radius_server_eapol_cb
,
318 if (sess
->eap
== NULL
) {
319 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
321 radius_server_session_free(data
, sess
);
324 sess
->eap_if
= eap_get_interface(sess
->eap
);
325 sess
->eap_if
->eapRestart
= TRUE
;
326 sess
->eap_if
->portEnabled
= TRUE
;
328 RADIUS_DEBUG("New session 0x%x initialized", sess
->sess_id
);
334 static struct radius_msg
*
335 radius_server_encapsulate_eap(struct radius_server_data
*data
,
336 struct radius_client
*client
,
337 struct radius_session
*sess
,
338 struct radius_msg
*request
)
340 struct radius_msg
*msg
;
342 unsigned int sess_id
;
344 if (sess
->eap_if
->eapFail
) {
345 sess
->eap_if
->eapFail
= FALSE
;
346 code
= RADIUS_CODE_ACCESS_REJECT
;
347 } else if (sess
->eap_if
->eapSuccess
) {
348 sess
->eap_if
->eapSuccess
= FALSE
;
349 code
= RADIUS_CODE_ACCESS_ACCEPT
;
351 sess
->eap_if
->eapReq
= FALSE
;
352 code
= RADIUS_CODE_ACCESS_CHALLENGE
;
355 msg
= radius_msg_new(code
, request
->hdr
->identifier
);
357 RADIUS_DEBUG("Failed to allocate reply message");
361 sess_id
= htonl(sess
->sess_id
);
362 if (code
== RADIUS_CODE_ACCESS_CHALLENGE
&&
363 !radius_msg_add_attr(msg
, RADIUS_ATTR_STATE
,
364 (u8
*) &sess_id
, sizeof(sess_id
))) {
365 RADIUS_DEBUG("Failed to add State attribute");
368 if (sess
->eap_if
->eapReqData
&&
369 !radius_msg_add_eap(msg
, wpabuf_head(sess
->eap_if
->eapReqData
),
370 wpabuf_len(sess
->eap_if
->eapReqData
))) {
371 RADIUS_DEBUG("Failed to add EAP-Message attribute");
374 if (code
== RADIUS_CODE_ACCESS_ACCEPT
&& sess
->eap_if
->eapKeyData
) {
376 if (sess
->eap_if
->eapKeyDataLen
> 64) {
379 len
= sess
->eap_if
->eapKeyDataLen
/ 2;
381 if (!radius_msg_add_mppe_keys(msg
, request
->hdr
->authenticator
,
382 (u8
*) client
->shared_secret
,
383 client
->shared_secret_len
,
384 sess
->eap_if
->eapKeyData
+ len
,
385 len
, sess
->eap_if
->eapKeyData
,
387 RADIUS_DEBUG("Failed to add MPPE key attributes");
391 if (radius_msg_copy_attr(msg
, request
, RADIUS_ATTR_PROXY_STATE
) < 0) {
392 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
393 radius_msg_free(msg
);
398 if (radius_msg_finish_srv(msg
, (u8
*) client
->shared_secret
,
399 client
->shared_secret_len
,
400 request
->hdr
->authenticator
) < 0) {
401 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
408 static int radius_server_reject(struct radius_server_data
*data
,
409 struct radius_client
*client
,
410 struct radius_msg
*request
,
411 struct sockaddr
*from
, socklen_t fromlen
,
412 const char *from_addr
, int from_port
)
414 struct radius_msg
*msg
;
416 struct eap_hdr eapfail
;
418 RADIUS_DEBUG("Reject invalid request from %s:%d",
419 from_addr
, from_port
);
421 msg
= radius_msg_new(RADIUS_CODE_ACCESS_REJECT
,
422 request
->hdr
->identifier
);
427 os_memset(&eapfail
, 0, sizeof(eapfail
));
428 eapfail
.code
= EAP_CODE_FAILURE
;
429 eapfail
.identifier
= 0;
430 eapfail
.length
= host_to_be16(sizeof(eapfail
));
432 if (!radius_msg_add_eap(msg
, (u8
*) &eapfail
, sizeof(eapfail
))) {
433 RADIUS_DEBUG("Failed to add EAP-Message attribute");
436 if (radius_msg_copy_attr(msg
, request
, RADIUS_ATTR_PROXY_STATE
) < 0) {
437 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
438 radius_msg_free(msg
);
443 if (radius_msg_finish_srv(msg
, (u8
*) client
->shared_secret
,
444 client
->shared_secret_len
,
445 request
->hdr
->authenticator
) < 0) {
446 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
449 if (wpa_debug_level
<= MSG_MSGDUMP
) {
450 radius_msg_dump(msg
);
453 data
->counters
.access_rejects
++;
454 client
->counters
.access_rejects
++;
455 if (sendto(data
->auth_sock
, msg
->buf
, msg
->buf_used
, 0,
456 (struct sockaddr
*) from
, sizeof(*from
)) < 0) {
457 perror("sendto[RADIUS SRV]");
461 radius_msg_free(msg
);
468 static int radius_server_request(struct radius_server_data
*data
,
469 struct radius_msg
*msg
,
470 struct sockaddr
*from
, socklen_t fromlen
,
471 struct radius_client
*client
,
472 const char *from_addr
, int from_port
,
473 struct radius_session
*force_sess
)
477 int res
, state_included
= 0;
480 struct radius_session
*sess
;
481 struct radius_msg
*reply
;
486 res
= radius_msg_get_attr(msg
, RADIUS_ATTR_STATE
, statebuf
,
488 state_included
= res
>= 0;
489 if (res
== sizeof(statebuf
)) {
490 state
= WPA_GET_BE32(statebuf
);
491 sess
= radius_server_get_session(client
, state
);
498 RADIUS_DEBUG("Request for session 0x%x", sess
->sess_id
);
499 } else if (state_included
) {
500 RADIUS_DEBUG("State attribute included but no session found");
501 radius_server_reject(data
, client
, msg
, from
, fromlen
,
502 from_addr
, from_port
);
505 sess
= radius_server_get_new_session(data
, client
, msg
);
507 RADIUS_DEBUG("Could not create a new session");
508 radius_server_reject(data
, client
, msg
, from
, fromlen
,
509 from_addr
, from_port
);
514 if (sess
->last_from_port
== from_port
&&
515 sess
->last_identifier
== msg
->hdr
->identifier
&&
516 os_memcmp(sess
->last_authenticator
, msg
->hdr
->authenticator
, 16) ==
518 RADIUS_DEBUG("Duplicate message from %s", from_addr
);
519 data
->counters
.dup_access_requests
++;
520 client
->counters
.dup_access_requests
++;
522 if (sess
->last_reply
) {
523 res
= sendto(data
->auth_sock
, sess
->last_reply
->buf
,
524 sess
->last_reply
->buf_used
, 0,
525 (struct sockaddr
*) from
, fromlen
);
527 perror("sendto[RADIUS SRV]");
532 RADIUS_DEBUG("No previous reply available for duplicate "
537 eap
= radius_msg_get_eap(msg
, &eap_len
);
539 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
541 data
->counters
.packets_dropped
++;
542 client
->counters
.packets_dropped
++;
546 RADIUS_DUMP("Received EAP data", eap
, eap_len
);
548 /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
549 * RFC3579 Sect. 2.6.2.
550 * Include EAP-Response/Nak with no preferred method if
552 * If code is not 1-4, discard the packet silently.
553 * Or is this already done by the EAP state machine? */
555 wpabuf_free(sess
->eap_if
->eapRespData
);
556 sess
->eap_if
->eapRespData
= wpabuf_alloc_ext_data(eap
, eap_len
);
557 if (sess
->eap_if
->eapRespData
== NULL
)
560 sess
->eap_if
->eapResp
= TRUE
;
561 eap_server_sm_step(sess
->eap
);
563 if ((sess
->eap_if
->eapReq
|| sess
->eap_if
->eapSuccess
||
564 sess
->eap_if
->eapFail
) && sess
->eap_if
->eapReqData
) {
565 RADIUS_DUMP("EAP data from the state machine",
566 wpabuf_head(sess
->eap_if
->eapReqData
),
567 wpabuf_len(sess
->eap_if
->eapReqData
));
568 } else if (sess
->eap_if
->eapFail
) {
569 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
571 } else if (eap_sm_method_pending(sess
->eap
)) {
572 if (sess
->last_msg
) {
573 radius_msg_free(sess
->last_msg
);
574 os_free(sess
->last_msg
);
576 sess
->last_msg
= msg
;
577 sess
->last_from_port
= from_port
;
578 os_free(sess
->last_from_addr
);
579 sess
->last_from_addr
= os_strdup(from_addr
);
580 sess
->last_fromlen
= fromlen
;
581 os_memcpy(&sess
->last_from
, from
, fromlen
);
584 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
585 " Access-Request silently (assuming it was a "
587 data
->counters
.packets_dropped
++;
588 client
->counters
.packets_dropped
++;
592 reply
= radius_server_encapsulate_eap(data
, client
, sess
, msg
);
595 RADIUS_DEBUG("Reply to %s:%d", from_addr
, from_port
);
596 if (wpa_debug_level
<= MSG_MSGDUMP
) {
597 radius_msg_dump(reply
);
600 switch (reply
->hdr
->code
) {
601 case RADIUS_CODE_ACCESS_ACCEPT
:
602 data
->counters
.access_accepts
++;
603 client
->counters
.access_accepts
++;
605 case RADIUS_CODE_ACCESS_REJECT
:
606 data
->counters
.access_rejects
++;
607 client
->counters
.access_rejects
++;
609 case RADIUS_CODE_ACCESS_CHALLENGE
:
610 data
->counters
.access_challenges
++;
611 client
->counters
.access_challenges
++;
614 res
= sendto(data
->auth_sock
, reply
->buf
, reply
->buf_used
, 0,
615 (struct sockaddr
*) from
, fromlen
);
617 perror("sendto[RADIUS SRV]");
619 if (sess
->last_reply
) {
620 radius_msg_free(sess
->last_reply
);
621 os_free(sess
->last_reply
);
623 sess
->last_reply
= reply
;
624 sess
->last_from_port
= from_port
;
625 sess
->last_identifier
= msg
->hdr
->identifier
;
626 os_memcpy(sess
->last_authenticator
, msg
->hdr
->authenticator
,
629 data
->counters
.packets_dropped
++;
630 client
->counters
.packets_dropped
++;
633 if (sess
->eap_if
->eapSuccess
|| sess
->eap_if
->eapFail
) {
634 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
636 eloop_cancel_timeout(radius_server_session_remove_timeout
,
638 eloop_register_timeout(10, 0,
639 radius_server_session_remove_timeout
,
647 static void radius_server_receive_auth(int sock
, void *eloop_ctx
,
650 struct radius_server_data
*data
= eloop_ctx
;
652 struct sockaddr_storage from
;
655 struct radius_client
*client
= NULL
;
656 struct radius_msg
*msg
= NULL
;
660 buf
= os_malloc(RADIUS_MAX_MSG_LEN
);
665 fromlen
= sizeof(from
);
666 len
= recvfrom(sock
, buf
, RADIUS_MAX_MSG_LEN
, 0,
667 (struct sockaddr
*) &from
, &fromlen
);
669 perror("recvfrom[radius_server]");
675 struct sockaddr_in6
*from6
= (struct sockaddr_in6
*) &from
;
676 if (inet_ntop(AF_INET6
, &from6
->sin6_addr
, abuf
, sizeof(abuf
))
679 from_port
= ntohs(from6
->sin6_port
);
680 RADIUS_DEBUG("Received %d bytes from %s:%d",
681 len
, abuf
, from_port
);
683 client
= radius_server_get_client(data
,
685 &from6
->sin6_addr
, 1);
687 #endif /* CONFIG_IPV6 */
690 struct sockaddr_in
*from4
= (struct sockaddr_in
*) &from
;
691 os_strlcpy(abuf
, inet_ntoa(from4
->sin_addr
), sizeof(abuf
));
692 from_port
= ntohs(from4
->sin_port
);
693 RADIUS_DEBUG("Received %d bytes from %s:%d",
694 len
, abuf
, from_port
);
696 client
= radius_server_get_client(data
, &from4
->sin_addr
, 0);
699 RADIUS_DUMP("Received data", buf
, len
);
701 if (client
== NULL
) {
702 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf
);
703 data
->counters
.invalid_requests
++;
707 msg
= radius_msg_parse(buf
, len
);
709 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
710 data
->counters
.malformed_access_requests
++;
711 client
->counters
.malformed_access_requests
++;
718 if (wpa_debug_level
<= MSG_MSGDUMP
) {
719 radius_msg_dump(msg
);
722 if (msg
->hdr
->code
!= RADIUS_CODE_ACCESS_REQUEST
) {
723 RADIUS_DEBUG("Unexpected RADIUS code %d", msg
->hdr
->code
);
724 data
->counters
.unknown_types
++;
725 client
->counters
.unknown_types
++;
729 data
->counters
.access_requests
++;
730 client
->counters
.access_requests
++;
732 if (radius_msg_verify_msg_auth(msg
, (u8
*) client
->shared_secret
,
733 client
->shared_secret_len
, NULL
)) {
734 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf
);
735 data
->counters
.bad_authenticators
++;
736 client
->counters
.bad_authenticators
++;
740 if (radius_server_request(data
, msg
, (struct sockaddr
*) &from
,
741 fromlen
, client
, abuf
, from_port
, NULL
) ==
743 return; /* msg was stored with the session */
747 radius_msg_free(msg
);
754 static int radius_server_open_socket(int port
)
757 struct sockaddr_in addr
;
759 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
765 os_memset(&addr
, 0, sizeof(addr
));
766 addr
.sin_family
= AF_INET
;
767 addr
.sin_port
= htons(port
);
768 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
779 static int radius_server_open_socket6(int port
)
782 struct sockaddr_in6 addr
;
784 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
786 perror("socket[IPv6]");
790 os_memset(&addr
, 0, sizeof(addr
));
791 addr
.sin6_family
= AF_INET6
;
792 os_memcpy(&addr
.sin6_addr
, &in6addr_any
, sizeof(in6addr_any
));
793 addr
.sin6_port
= htons(port
);
794 if (bind(s
, (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
802 #endif /* CONFIG_IPV6 */
805 static void radius_server_free_sessions(struct radius_server_data
*data
,
806 struct radius_session
*sessions
)
808 struct radius_session
*session
, *prev
;
813 session
= session
->next
;
814 radius_server_session_free(data
, prev
);
819 static void radius_server_free_clients(struct radius_server_data
*data
,
820 struct radius_client
*clients
)
822 struct radius_client
*client
, *prev
;
827 client
= client
->next
;
829 radius_server_free_sessions(data
, prev
->sessions
);
830 os_free(prev
->shared_secret
);
836 static struct radius_client
*
837 radius_server_read_clients(const char *client_file
, int ipv6
)
840 const int buf_size
= 1024;
842 struct radius_client
*clients
, *tail
, *entry
;
843 int line
= 0, mask
, failed
= 0, i
;
846 struct in6_addr addr6
;
847 #endif /* CONFIG_IPV6 */
850 f
= fopen(client_file
, "r");
852 RADIUS_ERROR("Could not open client file '%s'", client_file
);
856 buf
= os_malloc(buf_size
);
862 clients
= tail
= NULL
;
863 while (fgets(buf
, buf_size
, f
)) {
864 /* Configuration file format:
865 * 192.168.1.0/24 secret
867 * fe80::211:22ff:fe33:4455/64 secretipv6
870 buf
[buf_size
- 1] = '\0';
872 while (*pos
!= '\0' && *pos
!= '\n')
876 if (*buf
== '\0' || *buf
== '#')
880 while ((*pos
>= '0' && *pos
<= '9') || *pos
== '.' ||
881 (*pos
>= 'a' && *pos
<= 'f') || *pos
== ':' ||
882 (*pos
>= 'A' && *pos
<= 'F')) {
894 mask
= strtol(pos
, &end
, 10);
896 (mask
< 0 || mask
> (ipv6
? 128 : 32))) {
902 mask
= ipv6
? 128 : 32;
906 if (!ipv6
&& inet_aton(buf
, &addr
) == 0) {
911 if (ipv6
&& inet_pton(AF_INET6
, buf
, &addr6
) <= 0) {
912 if (inet_pton(AF_INET
, buf
, &addr
) <= 0) {
916 /* Convert IPv4 address to IPv6 */
919 os_memset(addr6
.s6_addr
, 0, 10);
920 addr6
.s6_addr
[10] = 0xff;
921 addr6
.s6_addr
[11] = 0xff;
922 os_memcpy(addr6
.s6_addr
+ 12, (char *) &addr
.s_addr
,
925 #endif /* CONFIG_IPV6 */
927 while (*pos
== ' ' || *pos
== '\t') {
936 entry
= os_zalloc(sizeof(*entry
));
941 entry
->shared_secret
= os_strdup(pos
);
942 if (entry
->shared_secret
== NULL
) {
947 entry
->shared_secret_len
= os_strlen(entry
->shared_secret
);
948 entry
->addr
.s_addr
= addr
.s_addr
;
951 for (i
= 0; i
< mask
; i
++)
952 val
|= 1 << (31 - i
);
953 entry
->mask
.s_addr
= htonl(val
);
957 int offset
= mask
/ 8;
959 os_memcpy(entry
->addr6
.s6_addr
, addr6
.s6_addr
, 16);
960 os_memset(entry
->mask6
.s6_addr
, 0xff, offset
);
962 for (i
= 0; i
< (mask
% 8); i
++)
965 entry
->mask6
.s6_addr
[offset
] = val
;
967 #endif /* CONFIG_IPV6 */
970 clients
= tail
= entry
;
978 RADIUS_ERROR("Invalid line %d in '%s'", line
, client_file
);
979 radius_server_free_clients(NULL
, clients
);
990 struct radius_server_data
*
991 radius_server_init(struct radius_server_conf
*conf
)
993 struct radius_server_data
*data
;
997 fprintf(stderr
, "RADIUS server compiled without IPv6 "
1001 #endif /* CONFIG_IPV6 */
1003 data
= os_zalloc(sizeof(*data
));
1007 os_get_time(&data
->start_time
);
1008 data
->conf_ctx
= conf
->conf_ctx
;
1009 data
->eap_sim_db_priv
= conf
->eap_sim_db_priv
;
1010 data
->ssl_ctx
= conf
->ssl_ctx
;
1011 data
->ipv6
= conf
->ipv6
;
1012 if (conf
->pac_opaque_encr_key
) {
1013 data
->pac_opaque_encr_key
= os_malloc(16);
1014 os_memcpy(data
->pac_opaque_encr_key
, conf
->pac_opaque_encr_key
,
1017 if (conf
->eap_fast_a_id
)
1018 data
->eap_fast_a_id
= os_strdup(conf
->eap_fast_a_id
);
1019 data
->get_eap_user
= conf
->get_eap_user
;
1020 data
->eap_sim_aka_result_ind
= conf
->eap_sim_aka_result_ind
;
1021 data
->tnc
= conf
->tnc
;
1023 data
->clients
= radius_server_read_clients(conf
->client_file
,
1025 if (data
->clients
== NULL
) {
1026 printf("No RADIUS clients configured.\n");
1027 radius_server_deinit(data
);
1033 data
->auth_sock
= radius_server_open_socket6(conf
->auth_port
);
1035 #endif /* CONFIG_IPV6 */
1036 data
->auth_sock
= radius_server_open_socket(conf
->auth_port
);
1037 if (data
->auth_sock
< 0) {
1038 printf("Failed to open UDP socket for RADIUS authentication "
1040 radius_server_deinit(data
);
1043 if (eloop_register_read_sock(data
->auth_sock
,
1044 radius_server_receive_auth
,
1046 radius_server_deinit(data
);
1054 void radius_server_deinit(struct radius_server_data
*data
)
1059 if (data
->auth_sock
>= 0) {
1060 eloop_unregister_read_sock(data
->auth_sock
);
1061 close(data
->auth_sock
);
1064 radius_server_free_clients(data
, data
->clients
);
1066 os_free(data
->pac_opaque_encr_key
);
1067 os_free(data
->eap_fast_a_id
);
1072 int radius_server_get_mib(struct radius_server_data
*data
, char *buf
,
1079 struct radius_client
*cli
;
1081 /* RFC 2619 - RADIUS Authentication Server MIB */
1083 if (data
== NULL
|| buflen
== 0)
1090 uptime
= (now
.sec
- data
->start_time
.sec
) * 100 +
1091 ((now
.usec
- data
->start_time
.usec
) / 10000) % 100;
1092 ret
= os_snprintf(pos
, end
- pos
,
1093 "RADIUS-AUTH-SERVER-MIB\n"
1094 "radiusAuthServIdent=hostapd\n"
1095 "radiusAuthServUpTime=%d\n"
1096 "radiusAuthServResetTime=0\n"
1097 "radiusAuthServConfigReset=4\n",
1099 if (ret
< 0 || ret
>= end
- pos
) {
1105 ret
= os_snprintf(pos
, end
- pos
,
1106 "radiusAuthServTotalAccessRequests=%u\n"
1107 "radiusAuthServTotalInvalidRequests=%u\n"
1108 "radiusAuthServTotalDupAccessRequests=%u\n"
1109 "radiusAuthServTotalAccessAccepts=%u\n"
1110 "radiusAuthServTotalAccessRejects=%u\n"
1111 "radiusAuthServTotalAccessChallenges=%u\n"
1112 "radiusAuthServTotalMalformedAccessRequests=%u\n"
1113 "radiusAuthServTotalBadAuthenticators=%u\n"
1114 "radiusAuthServTotalPacketsDropped=%u\n"
1115 "radiusAuthServTotalUnknownTypes=%u\n",
1116 data
->counters
.access_requests
,
1117 data
->counters
.invalid_requests
,
1118 data
->counters
.dup_access_requests
,
1119 data
->counters
.access_accepts
,
1120 data
->counters
.access_rejects
,
1121 data
->counters
.access_challenges
,
1122 data
->counters
.malformed_access_requests
,
1123 data
->counters
.bad_authenticators
,
1124 data
->counters
.packets_dropped
,
1125 data
->counters
.unknown_types
);
1126 if (ret
< 0 || ret
>= end
- pos
) {
1132 for (cli
= data
->clients
, idx
= 0; cli
; cli
= cli
->next
, idx
++) {
1133 char abuf
[50], mbuf
[50];
1136 if (inet_ntop(AF_INET6
, &cli
->addr6
, abuf
,
1137 sizeof(abuf
)) == NULL
)
1139 if (inet_ntop(AF_INET6
, &cli
->mask6
, abuf
,
1140 sizeof(mbuf
)) == NULL
)
1143 #endif /* CONFIG_IPV6 */
1145 os_strlcpy(abuf
, inet_ntoa(cli
->addr
), sizeof(abuf
));
1146 os_strlcpy(mbuf
, inet_ntoa(cli
->mask
), sizeof(mbuf
));
1149 ret
= os_snprintf(pos
, end
- pos
,
1150 "radiusAuthClientIndex=%u\n"
1151 "radiusAuthClientAddress=%s/%s\n"
1152 "radiusAuthServAccessRequests=%u\n"
1153 "radiusAuthServDupAccessRequests=%u\n"
1154 "radiusAuthServAccessAccepts=%u\n"
1155 "radiusAuthServAccessRejects=%u\n"
1156 "radiusAuthServAccessChallenges=%u\n"
1157 "radiusAuthServMalformedAccessRequests=%u\n"
1158 "radiusAuthServBadAuthenticators=%u\n"
1159 "radiusAuthServPacketsDropped=%u\n"
1160 "radiusAuthServUnknownTypes=%u\n",
1163 cli
->counters
.access_requests
,
1164 cli
->counters
.dup_access_requests
,
1165 cli
->counters
.access_accepts
,
1166 cli
->counters
.access_rejects
,
1167 cli
->counters
.access_challenges
,
1168 cli
->counters
.malformed_access_requests
,
1169 cli
->counters
.bad_authenticators
,
1170 cli
->counters
.packets_dropped
,
1171 cli
->counters
.unknown_types
);
1172 if (ret
< 0 || ret
>= end
- pos
) {
1183 static int radius_server_get_eap_user(void *ctx
, const u8
*identity
,
1184 size_t identity_len
, int phase2
,
1185 struct eap_user
*user
)
1187 struct radius_session
*sess
= ctx
;
1188 struct radius_server_data
*data
= sess
->server
;
1190 return data
->get_eap_user(data
->conf_ctx
, identity
, identity_len
,
1195 static struct eapol_callbacks radius_server_eapol_cb
=
1197 .get_eap_user
= radius_server_get_eap_user
,
1201 void radius_server_eap_pending_cb(struct radius_server_data
*data
, void *ctx
)
1203 struct radius_client
*cli
;
1204 struct radius_session
*s
, *sess
= NULL
;
1205 struct radius_msg
*msg
;
1210 for (cli
= data
->clients
; cli
; cli
= cli
->next
) {
1211 for (s
= cli
->sessions
; s
; s
= s
->next
) {
1212 if (s
->eap
== ctx
&& s
->last_msg
) {
1224 RADIUS_DEBUG("No session matched callback ctx");
1228 msg
= sess
->last_msg
;
1229 sess
->last_msg
= NULL
;
1230 eap_sm_pending_cb(sess
->eap
);
1231 if (radius_server_request(data
, msg
,
1232 (struct sockaddr
*) &sess
->last_from
,
1233 sess
->last_fromlen
, cli
,
1234 sess
->last_from_addr
,
1235 sess
->last_from_port
, sess
) == -2)
1236 return; /* msg was stored with the session */
1238 radius_msg_free(msg
);