blob: fe197704da84df4e81cc08f46027d6d2baa05b53 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * RADIUS authentication server
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003 * Copyright (c) 2005-2009, 2011, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10#include <net/if.h>
11
12#include "common.h"
13#include "radius.h"
14#include "eloop.h"
15#include "eap_server/eap.h"
16#include "radius_server.h"
17
18/**
19 * RADIUS_SESSION_TIMEOUT - Session timeout in seconds
20 */
21#define RADIUS_SESSION_TIMEOUT 60
22
23/**
24 * RADIUS_MAX_SESSION - Maximum number of active sessions
25 */
26#define RADIUS_MAX_SESSION 100
27
28/**
29 * RADIUS_MAX_MSG_LEN - Maximum message length for incoming RADIUS messages
30 */
31#define RADIUS_MAX_MSG_LEN 3000
32
33static struct eapol_callbacks radius_server_eapol_cb;
34
35struct radius_client;
36struct radius_server_data;
37
38/**
39 * struct radius_server_counters - RADIUS server statistics counters
40 */
41struct radius_server_counters {
42 u32 access_requests;
43 u32 invalid_requests;
44 u32 dup_access_requests;
45 u32 access_accepts;
46 u32 access_rejects;
47 u32 access_challenges;
48 u32 malformed_access_requests;
49 u32 bad_authenticators;
50 u32 packets_dropped;
51 u32 unknown_types;
52};
53
54/**
55 * struct radius_session - Internal RADIUS server data for a session
56 */
57struct radius_session {
58 struct radius_session *next;
59 struct radius_client *client;
60 struct radius_server_data *server;
61 unsigned int sess_id;
62 struct eap_sm *eap;
63 struct eap_eapol_interface *eap_if;
64
65 struct radius_msg *last_msg;
66 char *last_from_addr;
67 int last_from_port;
68 struct sockaddr_storage last_from;
69 socklen_t last_fromlen;
70 u8 last_identifier;
71 struct radius_msg *last_reply;
72 u8 last_authenticator[16];
73};
74
75/**
76 * struct radius_client - Internal RADIUS server data for a client
77 */
78struct radius_client {
79 struct radius_client *next;
80 struct in_addr addr;
81 struct in_addr mask;
82#ifdef CONFIG_IPV6
83 struct in6_addr addr6;
84 struct in6_addr mask6;
85#endif /* CONFIG_IPV6 */
86 char *shared_secret;
87 int shared_secret_len;
88 struct radius_session *sessions;
89 struct radius_server_counters counters;
90};
91
92/**
93 * struct radius_server_data - Internal RADIUS server data
94 */
95struct radius_server_data {
96 /**
97 * auth_sock - Socket for RADIUS authentication messages
98 */
99 int auth_sock;
100
101 /**
102 * clients - List of authorized RADIUS clients
103 */
104 struct radius_client *clients;
105
106 /**
107 * next_sess_id - Next session identifier
108 */
109 unsigned int next_sess_id;
110
111 /**
112 * conf_ctx - Context pointer for callbacks
113 *
114 * This is used as the ctx argument in get_eap_user() calls.
115 */
116 void *conf_ctx;
117
118 /**
119 * num_sess - Number of active sessions
120 */
121 int num_sess;
122
123 /**
124 * eap_sim_db_priv - EAP-SIM/AKA database context
125 *
126 * This is passed to the EAP-SIM/AKA server implementation as a
127 * callback context.
128 */
129 void *eap_sim_db_priv;
130
131 /**
132 * ssl_ctx - TLS context
133 *
134 * This is passed to the EAP server implementation as a callback
135 * context for TLS operations.
136 */
137 void *ssl_ctx;
138
139 /**
140 * pac_opaque_encr_key - PAC-Opaque encryption key for EAP-FAST
141 *
142 * This parameter is used to set a key for EAP-FAST to encrypt the
143 * PAC-Opaque data. It can be set to %NULL if EAP-FAST is not used. If
144 * set, must point to a 16-octet key.
145 */
146 u8 *pac_opaque_encr_key;
147
148 /**
149 * eap_fast_a_id - EAP-FAST authority identity (A-ID)
150 *
151 * If EAP-FAST is not used, this can be set to %NULL. In theory, this
152 * is a variable length field, but due to some existing implementations
153 * requiring A-ID to be 16 octets in length, it is recommended to use
154 * that length for the field to provide interoperability with deployed
155 * peer implementations.
156 */
157 u8 *eap_fast_a_id;
158
159 /**
160 * eap_fast_a_id_len - Length of eap_fast_a_id buffer in octets
161 */
162 size_t eap_fast_a_id_len;
163
164 /**
165 * eap_fast_a_id_info - EAP-FAST authority identifier information
166 *
167 * This A-ID-Info contains a user-friendly name for the A-ID. For
168 * example, this could be the enterprise and server names in
169 * human-readable format. This field is encoded as UTF-8. If EAP-FAST
170 * is not used, this can be set to %NULL.
171 */
172 char *eap_fast_a_id_info;
173
174 /**
175 * eap_fast_prov - EAP-FAST provisioning modes
176 *
177 * 0 = provisioning disabled, 1 = only anonymous provisioning allowed,
178 * 2 = only authenticated provisioning allowed, 3 = both provisioning
179 * modes allowed.
180 */
181 int eap_fast_prov;
182
183 /**
184 * pac_key_lifetime - EAP-FAST PAC-Key lifetime in seconds
185 *
186 * This is the hard limit on how long a provisioned PAC-Key can be
187 * used.
188 */
189 int pac_key_lifetime;
190
191 /**
192 * pac_key_refresh_time - EAP-FAST PAC-Key refresh time in seconds
193 *
194 * This is a soft limit on the PAC-Key. The server will automatically
195 * generate a new PAC-Key when this number of seconds (or fewer) of the
196 * lifetime remains.
197 */
198 int pac_key_refresh_time;
199
200 /**
201 * eap_sim_aka_result_ind - EAP-SIM/AKA protected success indication
202 *
203 * This controls whether the protected success/failure indication
204 * (AT_RESULT_IND) is used with EAP-SIM and EAP-AKA.
205 */
206 int eap_sim_aka_result_ind;
207
208 /**
209 * tnc - Trusted Network Connect (TNC)
210 *
211 * This controls whether TNC is enabled and will be required before the
212 * peer is allowed to connect. Note: This is only used with EAP-TTLS
213 * and EAP-FAST. If any other EAP method is enabled, the peer will be
214 * allowed to connect without TNC.
215 */
216 int tnc;
217
218 /**
219 * pwd_group - The D-H group assigned for EAP-pwd
220 *
221 * If EAP-pwd is not used it can be set to zero.
222 */
223 u16 pwd_group;
224
225 /**
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700226 * server_id - Server identity
227 */
228 const char *server_id;
229
230 /**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 * wps - Wi-Fi Protected Setup context
232 *
233 * If WPS is used with an external RADIUS server (which is quite
234 * unlikely configuration), this is used to provide a pointer to WPS
235 * context data. Normally, this can be set to %NULL.
236 */
237 struct wps_context *wps;
238
239 /**
240 * ipv6 - Whether to enable IPv6 support in the RADIUS server
241 */
242 int ipv6;
243
244 /**
245 * start_time - Timestamp of server start
246 */
247 struct os_time start_time;
248
249 /**
250 * counters - Statistics counters for server operations
251 *
252 * These counters are the sum over all clients.
253 */
254 struct radius_server_counters counters;
255
256 /**
257 * get_eap_user - Callback for fetching EAP user information
258 * @ctx: Context data from conf_ctx
259 * @identity: User identity
260 * @identity_len: identity buffer length in octets
261 * @phase2: Whether this is for Phase 2 identity
262 * @user: Data structure for filling in the user information
263 * Returns: 0 on success, -1 on failure
264 *
265 * This is used to fetch information from user database. The callback
266 * will fill in information about allowed EAP methods and the user
267 * password. The password field will be an allocated copy of the
268 * password data and RADIUS server will free it after use.
269 */
270 int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
271 int phase2, struct eap_user *user);
272
273 /**
274 * eap_req_id_text - Optional data for EAP-Request/Identity
275 *
276 * This can be used to configure an optional, displayable message that
277 * will be sent in EAP-Request/Identity. This string can contain an
278 * ASCII-0 character (nul) to separate network infromation per RFC
279 * 4284. The actual string length is explicit provided in
280 * eap_req_id_text_len since nul character will not be used as a string
281 * terminator.
282 */
283 char *eap_req_id_text;
284
285 /**
286 * eap_req_id_text_len - Length of eap_req_id_text buffer in octets
287 */
288 size_t eap_req_id_text_len;
289
290 /*
291 * msg_ctx - Context data for wpa_msg() calls
292 */
293 void *msg_ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800294
295#ifdef CONFIG_RADIUS_TEST
296 char *dump_msk_file;
297#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298};
299
300
301extern int wpa_debug_level;
302
303#define RADIUS_DEBUG(args...) \
304wpa_printf(MSG_DEBUG, "RADIUS SRV: " args)
305#define RADIUS_ERROR(args...) \
306wpa_printf(MSG_ERROR, "RADIUS SRV: " args)
307#define RADIUS_DUMP(args...) \
308wpa_hexdump(MSG_MSGDUMP, "RADIUS SRV: " args)
309#define RADIUS_DUMP_ASCII(args...) \
310wpa_hexdump_ascii(MSG_MSGDUMP, "RADIUS SRV: " args)
311
312
313static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx);
314static void radius_server_session_remove_timeout(void *eloop_ctx,
315 void *timeout_ctx);
316
317
318static struct radius_client *
319radius_server_get_client(struct radius_server_data *data, struct in_addr *addr,
320 int ipv6)
321{
322 struct radius_client *client = data->clients;
323
324 while (client) {
325#ifdef CONFIG_IPV6
326 if (ipv6) {
327 struct in6_addr *addr6;
328 int i;
329
330 addr6 = (struct in6_addr *) addr;
331 for (i = 0; i < 16; i++) {
332 if ((addr6->s6_addr[i] &
333 client->mask6.s6_addr[i]) !=
334 (client->addr6.s6_addr[i] &
335 client->mask6.s6_addr[i])) {
336 i = 17;
337 break;
338 }
339 }
340 if (i == 16) {
341 break;
342 }
343 }
344#endif /* CONFIG_IPV6 */
345 if (!ipv6 && (client->addr.s_addr & client->mask.s_addr) ==
346 (addr->s_addr & client->mask.s_addr)) {
347 break;
348 }
349
350 client = client->next;
351 }
352
353 return client;
354}
355
356
357static struct radius_session *
358radius_server_get_session(struct radius_client *client, unsigned int sess_id)
359{
360 struct radius_session *sess = client->sessions;
361
362 while (sess) {
363 if (sess->sess_id == sess_id) {
364 break;
365 }
366 sess = sess->next;
367 }
368
369 return sess;
370}
371
372
373static void radius_server_session_free(struct radius_server_data *data,
374 struct radius_session *sess)
375{
376 eloop_cancel_timeout(radius_server_session_timeout, data, sess);
377 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
378 eap_server_sm_deinit(sess->eap);
379 radius_msg_free(sess->last_msg);
380 os_free(sess->last_from_addr);
381 radius_msg_free(sess->last_reply);
382 os_free(sess);
383 data->num_sess--;
384}
385
386
387static void radius_server_session_remove(struct radius_server_data *data,
388 struct radius_session *sess)
389{
390 struct radius_client *client = sess->client;
391 struct radius_session *session, *prev;
392
393 eloop_cancel_timeout(radius_server_session_remove_timeout, data, sess);
394
395 prev = NULL;
396 session = client->sessions;
397 while (session) {
398 if (session == sess) {
399 if (prev == NULL) {
400 client->sessions = sess->next;
401 } else {
402 prev->next = sess->next;
403 }
404 radius_server_session_free(data, sess);
405 break;
406 }
407 prev = session;
408 session = session->next;
409 }
410}
411
412
413static void radius_server_session_remove_timeout(void *eloop_ctx,
414 void *timeout_ctx)
415{
416 struct radius_server_data *data = eloop_ctx;
417 struct radius_session *sess = timeout_ctx;
418 RADIUS_DEBUG("Removing completed session 0x%x", sess->sess_id);
419 radius_server_session_remove(data, sess);
420}
421
422
423static void radius_server_session_timeout(void *eloop_ctx, void *timeout_ctx)
424{
425 struct radius_server_data *data = eloop_ctx;
426 struct radius_session *sess = timeout_ctx;
427
428 RADIUS_DEBUG("Timing out authentication session 0x%x", sess->sess_id);
429 radius_server_session_remove(data, sess);
430}
431
432
433static struct radius_session *
434radius_server_new_session(struct radius_server_data *data,
435 struct radius_client *client)
436{
437 struct radius_session *sess;
438
439 if (data->num_sess >= RADIUS_MAX_SESSION) {
440 RADIUS_DEBUG("Maximum number of existing session - no room "
441 "for a new session");
442 return NULL;
443 }
444
445 sess = os_zalloc(sizeof(*sess));
446 if (sess == NULL)
447 return NULL;
448
449 sess->server = data;
450 sess->client = client;
451 sess->sess_id = data->next_sess_id++;
452 sess->next = client->sessions;
453 client->sessions = sess;
454 eloop_register_timeout(RADIUS_SESSION_TIMEOUT, 0,
455 radius_server_session_timeout, data, sess);
456 data->num_sess++;
457 return sess;
458}
459
460
461static struct radius_session *
462radius_server_get_new_session(struct radius_server_data *data,
463 struct radius_client *client,
464 struct radius_msg *msg)
465{
466 u8 *user;
467 size_t user_len;
468 int res;
469 struct radius_session *sess;
470 struct eap_config eap_conf;
471
472 RADIUS_DEBUG("Creating a new session");
473
474 user = os_malloc(256);
475 if (user == NULL) {
476 return NULL;
477 }
478 res = radius_msg_get_attr(msg, RADIUS_ATTR_USER_NAME, user, 256);
479 if (res < 0 || res > 256) {
480 RADIUS_DEBUG("Could not get User-Name");
481 os_free(user);
482 return NULL;
483 }
484 user_len = res;
485 RADIUS_DUMP_ASCII("User-Name", user, user_len);
486
487 res = data->get_eap_user(data->conf_ctx, user, user_len, 0, NULL);
488 os_free(user);
489
490 if (res == 0) {
491 RADIUS_DEBUG("Matching user entry found");
492 sess = radius_server_new_session(data, client);
493 if (sess == NULL) {
494 RADIUS_DEBUG("Failed to create a new session");
495 return NULL;
496 }
497 } else {
498 RADIUS_DEBUG("User-Name not found from user database");
499 return NULL;
500 }
501
502 os_memset(&eap_conf, 0, sizeof(eap_conf));
503 eap_conf.ssl_ctx = data->ssl_ctx;
504 eap_conf.msg_ctx = data->msg_ctx;
505 eap_conf.eap_sim_db_priv = data->eap_sim_db_priv;
506 eap_conf.backend_auth = TRUE;
507 eap_conf.eap_server = 1;
508 eap_conf.pac_opaque_encr_key = data->pac_opaque_encr_key;
509 eap_conf.eap_fast_a_id = data->eap_fast_a_id;
510 eap_conf.eap_fast_a_id_len = data->eap_fast_a_id_len;
511 eap_conf.eap_fast_a_id_info = data->eap_fast_a_id_info;
512 eap_conf.eap_fast_prov = data->eap_fast_prov;
513 eap_conf.pac_key_lifetime = data->pac_key_lifetime;
514 eap_conf.pac_key_refresh_time = data->pac_key_refresh_time;
515 eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind;
516 eap_conf.tnc = data->tnc;
517 eap_conf.wps = data->wps;
518 eap_conf.pwd_group = data->pwd_group;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700519 eap_conf.server_id = (const u8 *) data->server_id;
520 eap_conf.server_id_len = os_strlen(data->server_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521 sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb,
522 &eap_conf);
523 if (sess->eap == NULL) {
524 RADIUS_DEBUG("Failed to initialize EAP state machine for the "
525 "new session");
526 radius_server_session_free(data, sess);
527 return NULL;
528 }
529 sess->eap_if = eap_get_interface(sess->eap);
530 sess->eap_if->eapRestart = TRUE;
531 sess->eap_if->portEnabled = TRUE;
532
533 RADIUS_DEBUG("New session 0x%x initialized", sess->sess_id);
534
535 return sess;
536}
537
538
539static struct radius_msg *
540radius_server_encapsulate_eap(struct radius_server_data *data,
541 struct radius_client *client,
542 struct radius_session *sess,
543 struct radius_msg *request)
544{
545 struct radius_msg *msg;
546 int code;
547 unsigned int sess_id;
548 struct radius_hdr *hdr = radius_msg_get_hdr(request);
549
550 if (sess->eap_if->eapFail) {
551 sess->eap_if->eapFail = FALSE;
552 code = RADIUS_CODE_ACCESS_REJECT;
553 } else if (sess->eap_if->eapSuccess) {
554 sess->eap_if->eapSuccess = FALSE;
555 code = RADIUS_CODE_ACCESS_ACCEPT;
556 } else {
557 sess->eap_if->eapReq = FALSE;
558 code = RADIUS_CODE_ACCESS_CHALLENGE;
559 }
560
561 msg = radius_msg_new(code, hdr->identifier);
562 if (msg == NULL) {
563 RADIUS_DEBUG("Failed to allocate reply message");
564 return NULL;
565 }
566
567 sess_id = htonl(sess->sess_id);
568 if (code == RADIUS_CODE_ACCESS_CHALLENGE &&
569 !radius_msg_add_attr(msg, RADIUS_ATTR_STATE,
570 (u8 *) &sess_id, sizeof(sess_id))) {
571 RADIUS_DEBUG("Failed to add State attribute");
572 }
573
574 if (sess->eap_if->eapReqData &&
575 !radius_msg_add_eap(msg, wpabuf_head(sess->eap_if->eapReqData),
576 wpabuf_len(sess->eap_if->eapReqData))) {
577 RADIUS_DEBUG("Failed to add EAP-Message attribute");
578 }
579
580 if (code == RADIUS_CODE_ACCESS_ACCEPT && sess->eap_if->eapKeyData) {
581 int len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582#ifdef CONFIG_RADIUS_TEST
583 if (data->dump_msk_file) {
584 FILE *f;
585 char buf[2 * 64 + 1];
586 f = fopen(data->dump_msk_file, "a");
587 if (f) {
588 len = sess->eap_if->eapKeyDataLen;
589 if (len > 64)
590 len = 64;
591 len = wpa_snprintf_hex(
592 buf, sizeof(buf),
593 sess->eap_if->eapKeyData, len);
594 buf[len] = '\0';
595 fprintf(f, "%s\n", buf);
596 fclose(f);
597 }
598 }
599#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 if (sess->eap_if->eapKeyDataLen > 64) {
601 len = 32;
602 } else {
603 len = sess->eap_if->eapKeyDataLen / 2;
604 }
605 if (!radius_msg_add_mppe_keys(msg, hdr->authenticator,
606 (u8 *) client->shared_secret,
607 client->shared_secret_len,
608 sess->eap_if->eapKeyData + len,
609 len, sess->eap_if->eapKeyData,
610 len)) {
611 RADIUS_DEBUG("Failed to add MPPE key attributes");
612 }
613 }
614
615 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
616 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
617 radius_msg_free(msg);
618 return NULL;
619 }
620
621 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
622 client->shared_secret_len,
623 hdr->authenticator) < 0) {
624 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
625 }
626
627 return msg;
628}
629
630
631static int radius_server_reject(struct radius_server_data *data,
632 struct radius_client *client,
633 struct radius_msg *request,
634 struct sockaddr *from, socklen_t fromlen,
635 const char *from_addr, int from_port)
636{
637 struct radius_msg *msg;
638 int ret = 0;
639 struct eap_hdr eapfail;
640 struct wpabuf *buf;
641 struct radius_hdr *hdr = radius_msg_get_hdr(request);
642
643 RADIUS_DEBUG("Reject invalid request from %s:%d",
644 from_addr, from_port);
645
646 msg = radius_msg_new(RADIUS_CODE_ACCESS_REJECT, hdr->identifier);
647 if (msg == NULL) {
648 return -1;
649 }
650
651 os_memset(&eapfail, 0, sizeof(eapfail));
652 eapfail.code = EAP_CODE_FAILURE;
653 eapfail.identifier = 0;
654 eapfail.length = host_to_be16(sizeof(eapfail));
655
656 if (!radius_msg_add_eap(msg, (u8 *) &eapfail, sizeof(eapfail))) {
657 RADIUS_DEBUG("Failed to add EAP-Message attribute");
658 }
659
660 if (radius_msg_copy_attr(msg, request, RADIUS_ATTR_PROXY_STATE) < 0) {
661 RADIUS_DEBUG("Failed to copy Proxy-State attribute(s)");
662 radius_msg_free(msg);
663 return -1;
664 }
665
666 if (radius_msg_finish_srv(msg, (u8 *) client->shared_secret,
667 client->shared_secret_len,
668 hdr->authenticator) <
669 0) {
670 RADIUS_DEBUG("Failed to add Message-Authenticator attribute");
671 }
672
673 if (wpa_debug_level <= MSG_MSGDUMP) {
674 radius_msg_dump(msg);
675 }
676
677 data->counters.access_rejects++;
678 client->counters.access_rejects++;
679 buf = radius_msg_get_buf(msg);
680 if (sendto(data->auth_sock, wpabuf_head(buf), wpabuf_len(buf), 0,
681 (struct sockaddr *) from, sizeof(*from)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800682 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 ret = -1;
684 }
685
686 radius_msg_free(msg);
687
688 return ret;
689}
690
691
692static int radius_server_request(struct radius_server_data *data,
693 struct radius_msg *msg,
694 struct sockaddr *from, socklen_t fromlen,
695 struct radius_client *client,
696 const char *from_addr, int from_port,
697 struct radius_session *force_sess)
698{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700699 struct wpabuf *eap = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 int res, state_included = 0;
701 u8 statebuf[4];
702 unsigned int state;
703 struct radius_session *sess;
704 struct radius_msg *reply;
705 int is_complete = 0;
706
707 if (force_sess)
708 sess = force_sess;
709 else {
710 res = radius_msg_get_attr(msg, RADIUS_ATTR_STATE, statebuf,
711 sizeof(statebuf));
712 state_included = res >= 0;
713 if (res == sizeof(statebuf)) {
714 state = WPA_GET_BE32(statebuf);
715 sess = radius_server_get_session(client, state);
716 } else {
717 sess = NULL;
718 }
719 }
720
721 if (sess) {
722 RADIUS_DEBUG("Request for session 0x%x", sess->sess_id);
723 } else if (state_included) {
724 RADIUS_DEBUG("State attribute included but no session found");
725 radius_server_reject(data, client, msg, from, fromlen,
726 from_addr, from_port);
727 return -1;
728 } else {
729 sess = radius_server_get_new_session(data, client, msg);
730 if (sess == NULL) {
731 RADIUS_DEBUG("Could not create a new session");
732 radius_server_reject(data, client, msg, from, fromlen,
733 from_addr, from_port);
734 return -1;
735 }
736 }
737
738 if (sess->last_from_port == from_port &&
739 sess->last_identifier == radius_msg_get_hdr(msg)->identifier &&
740 os_memcmp(sess->last_authenticator,
741 radius_msg_get_hdr(msg)->authenticator, 16) == 0) {
742 RADIUS_DEBUG("Duplicate message from %s", from_addr);
743 data->counters.dup_access_requests++;
744 client->counters.dup_access_requests++;
745
746 if (sess->last_reply) {
747 struct wpabuf *buf;
748 buf = radius_msg_get_buf(sess->last_reply);
749 res = sendto(data->auth_sock, wpabuf_head(buf),
750 wpabuf_len(buf), 0,
751 (struct sockaddr *) from, fromlen);
752 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800753 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
754 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 }
756 return 0;
757 }
758
759 RADIUS_DEBUG("No previous reply available for duplicate "
760 "message");
761 return -1;
762 }
763
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700764 eap = radius_msg_get_eap(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765 if (eap == NULL) {
766 RADIUS_DEBUG("No EAP-Message in RADIUS packet from %s",
767 from_addr);
768 data->counters.packets_dropped++;
769 client->counters.packets_dropped++;
770 return -1;
771 }
772
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700773 RADIUS_DUMP("Received EAP data", wpabuf_head(eap), wpabuf_len(eap));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774
775 /* FIX: if Code is Request, Success, or Failure, send Access-Reject;
776 * RFC3579 Sect. 2.6.2.
777 * Include EAP-Response/Nak with no preferred method if
778 * code == request.
779 * If code is not 1-4, discard the packet silently.
780 * Or is this already done by the EAP state machine? */
781
782 wpabuf_free(sess->eap_if->eapRespData);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700783 sess->eap_if->eapRespData = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 sess->eap_if->eapResp = TRUE;
785 eap_server_sm_step(sess->eap);
786
787 if ((sess->eap_if->eapReq || sess->eap_if->eapSuccess ||
788 sess->eap_if->eapFail) && sess->eap_if->eapReqData) {
789 RADIUS_DUMP("EAP data from the state machine",
790 wpabuf_head(sess->eap_if->eapReqData),
791 wpabuf_len(sess->eap_if->eapReqData));
792 } else if (sess->eap_if->eapFail) {
793 RADIUS_DEBUG("No EAP data from the state machine, but eapFail "
794 "set");
795 } else if (eap_sm_method_pending(sess->eap)) {
796 radius_msg_free(sess->last_msg);
797 sess->last_msg = msg;
798 sess->last_from_port = from_port;
799 os_free(sess->last_from_addr);
800 sess->last_from_addr = os_strdup(from_addr);
801 sess->last_fromlen = fromlen;
802 os_memcpy(&sess->last_from, from, fromlen);
803 return -2;
804 } else {
805 RADIUS_DEBUG("No EAP data from the state machine - ignore this"
806 " Access-Request silently (assuming it was a "
807 "duplicate)");
808 data->counters.packets_dropped++;
809 client->counters.packets_dropped++;
810 return -1;
811 }
812
813 if (sess->eap_if->eapSuccess || sess->eap_if->eapFail)
814 is_complete = 1;
815
816 reply = radius_server_encapsulate_eap(data, client, sess, msg);
817
818 if (reply) {
819 struct wpabuf *buf;
820 struct radius_hdr *hdr;
821
822 RADIUS_DEBUG("Reply to %s:%d", from_addr, from_port);
823 if (wpa_debug_level <= MSG_MSGDUMP) {
824 radius_msg_dump(reply);
825 }
826
827 switch (radius_msg_get_hdr(reply)->code) {
828 case RADIUS_CODE_ACCESS_ACCEPT:
829 data->counters.access_accepts++;
830 client->counters.access_accepts++;
831 break;
832 case RADIUS_CODE_ACCESS_REJECT:
833 data->counters.access_rejects++;
834 client->counters.access_rejects++;
835 break;
836 case RADIUS_CODE_ACCESS_CHALLENGE:
837 data->counters.access_challenges++;
838 client->counters.access_challenges++;
839 break;
840 }
841 buf = radius_msg_get_buf(reply);
842 res = sendto(data->auth_sock, wpabuf_head(buf),
843 wpabuf_len(buf), 0,
844 (struct sockaddr *) from, fromlen);
845 if (res < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800846 wpa_printf(MSG_INFO, "sendto[RADIUS SRV]: %s",
847 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 }
849 radius_msg_free(sess->last_reply);
850 sess->last_reply = reply;
851 sess->last_from_port = from_port;
852 hdr = radius_msg_get_hdr(msg);
853 sess->last_identifier = hdr->identifier;
854 os_memcpy(sess->last_authenticator, hdr->authenticator, 16);
855 } else {
856 data->counters.packets_dropped++;
857 client->counters.packets_dropped++;
858 }
859
860 if (is_complete) {
861 RADIUS_DEBUG("Removing completed session 0x%x after timeout",
862 sess->sess_id);
863 eloop_cancel_timeout(radius_server_session_remove_timeout,
864 data, sess);
865 eloop_register_timeout(10, 0,
866 radius_server_session_remove_timeout,
867 data, sess);
868 }
869
870 return 0;
871}
872
873
874static void radius_server_receive_auth(int sock, void *eloop_ctx,
875 void *sock_ctx)
876{
877 struct radius_server_data *data = eloop_ctx;
878 u8 *buf = NULL;
879 union {
880 struct sockaddr_storage ss;
881 struct sockaddr_in sin;
882#ifdef CONFIG_IPV6
883 struct sockaddr_in6 sin6;
884#endif /* CONFIG_IPV6 */
885 } from;
886 socklen_t fromlen;
887 int len;
888 struct radius_client *client = NULL;
889 struct radius_msg *msg = NULL;
890 char abuf[50];
891 int from_port = 0;
892
893 buf = os_malloc(RADIUS_MAX_MSG_LEN);
894 if (buf == NULL) {
895 goto fail;
896 }
897
898 fromlen = sizeof(from);
899 len = recvfrom(sock, buf, RADIUS_MAX_MSG_LEN, 0,
900 (struct sockaddr *) &from.ss, &fromlen);
901 if (len < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800902 wpa_printf(MSG_INFO, "recvfrom[radius_server]: %s",
903 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 goto fail;
905 }
906
907#ifdef CONFIG_IPV6
908 if (data->ipv6) {
909 if (inet_ntop(AF_INET6, &from.sin6.sin6_addr, abuf,
910 sizeof(abuf)) == NULL)
911 abuf[0] = '\0';
912 from_port = ntohs(from.sin6.sin6_port);
913 RADIUS_DEBUG("Received %d bytes from %s:%d",
914 len, abuf, from_port);
915
916 client = radius_server_get_client(data,
917 (struct in_addr *)
918 &from.sin6.sin6_addr, 1);
919 }
920#endif /* CONFIG_IPV6 */
921
922 if (!data->ipv6) {
923 os_strlcpy(abuf, inet_ntoa(from.sin.sin_addr), sizeof(abuf));
924 from_port = ntohs(from.sin.sin_port);
925 RADIUS_DEBUG("Received %d bytes from %s:%d",
926 len, abuf, from_port);
927
928 client = radius_server_get_client(data, &from.sin.sin_addr, 0);
929 }
930
931 RADIUS_DUMP("Received data", buf, len);
932
933 if (client == NULL) {
934 RADIUS_DEBUG("Unknown client %s - packet ignored", abuf);
935 data->counters.invalid_requests++;
936 goto fail;
937 }
938
939 msg = radius_msg_parse(buf, len);
940 if (msg == NULL) {
941 RADIUS_DEBUG("Parsing incoming RADIUS frame failed");
942 data->counters.malformed_access_requests++;
943 client->counters.malformed_access_requests++;
944 goto fail;
945 }
946
947 os_free(buf);
948 buf = NULL;
949
950 if (wpa_debug_level <= MSG_MSGDUMP) {
951 radius_msg_dump(msg);
952 }
953
954 if (radius_msg_get_hdr(msg)->code != RADIUS_CODE_ACCESS_REQUEST) {
955 RADIUS_DEBUG("Unexpected RADIUS code %d",
956 radius_msg_get_hdr(msg)->code);
957 data->counters.unknown_types++;
958 client->counters.unknown_types++;
959 goto fail;
960 }
961
962 data->counters.access_requests++;
963 client->counters.access_requests++;
964
965 if (radius_msg_verify_msg_auth(msg, (u8 *) client->shared_secret,
966 client->shared_secret_len, NULL)) {
967 RADIUS_DEBUG("Invalid Message-Authenticator from %s", abuf);
968 data->counters.bad_authenticators++;
969 client->counters.bad_authenticators++;
970 goto fail;
971 }
972
973 if (radius_server_request(data, msg, (struct sockaddr *) &from,
974 fromlen, client, abuf, from_port, NULL) ==
975 -2)
976 return; /* msg was stored with the session */
977
978fail:
979 radius_msg_free(msg);
980 os_free(buf);
981}
982
983
984static int radius_server_disable_pmtu_discovery(int s)
985{
986 int r = -1;
987#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
988 /* Turn off Path MTU discovery on IPv4/UDP sockets. */
989 int action = IP_PMTUDISC_DONT;
990 r = setsockopt(s, IPPROTO_IP, IP_MTU_DISCOVER, &action,
991 sizeof(action));
992 if (r == -1)
993 wpa_printf(MSG_ERROR, "Failed to set IP_MTU_DISCOVER: "
994 "%s", strerror(errno));
995#endif
996 return r;
997}
998
999
1000static int radius_server_open_socket(int port)
1001{
1002 int s;
1003 struct sockaddr_in addr;
1004
1005 s = socket(PF_INET, SOCK_DGRAM, 0);
1006 if (s < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001007 wpa_printf(MSG_INFO, "RADIUS: socket: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 return -1;
1009 }
1010
1011 radius_server_disable_pmtu_discovery(s);
1012
1013 os_memset(&addr, 0, sizeof(addr));
1014 addr.sin_family = AF_INET;
1015 addr.sin_port = htons(port);
1016 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001017 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 close(s);
1019 return -1;
1020 }
1021
1022 return s;
1023}
1024
1025
1026#ifdef CONFIG_IPV6
1027static int radius_server_open_socket6(int port)
1028{
1029 int s;
1030 struct sockaddr_in6 addr;
1031
1032 s = socket(PF_INET6, SOCK_DGRAM, 0);
1033 if (s < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001034 wpa_printf(MSG_INFO, "RADIUS: socket[IPv6]: %s",
1035 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 return -1;
1037 }
1038
1039 os_memset(&addr, 0, sizeof(addr));
1040 addr.sin6_family = AF_INET6;
1041 os_memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
1042 addr.sin6_port = htons(port);
1043 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001044 wpa_printf(MSG_INFO, "RADIUS: bind: %s", strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 close(s);
1046 return -1;
1047 }
1048
1049 return s;
1050}
1051#endif /* CONFIG_IPV6 */
1052
1053
1054static void radius_server_free_sessions(struct radius_server_data *data,
1055 struct radius_session *sessions)
1056{
1057 struct radius_session *session, *prev;
1058
1059 session = sessions;
1060 while (session) {
1061 prev = session;
1062 session = session->next;
1063 radius_server_session_free(data, prev);
1064 }
1065}
1066
1067
1068static void radius_server_free_clients(struct radius_server_data *data,
1069 struct radius_client *clients)
1070{
1071 struct radius_client *client, *prev;
1072
1073 client = clients;
1074 while (client) {
1075 prev = client;
1076 client = client->next;
1077
1078 radius_server_free_sessions(data, prev->sessions);
1079 os_free(prev->shared_secret);
1080 os_free(prev);
1081 }
1082}
1083
1084
1085static struct radius_client *
1086radius_server_read_clients(const char *client_file, int ipv6)
1087{
1088 FILE *f;
1089 const int buf_size = 1024;
1090 char *buf, *pos;
1091 struct radius_client *clients, *tail, *entry;
1092 int line = 0, mask, failed = 0, i;
1093 struct in_addr addr;
1094#ifdef CONFIG_IPV6
1095 struct in6_addr addr6;
1096#endif /* CONFIG_IPV6 */
1097 unsigned int val;
1098
1099 f = fopen(client_file, "r");
1100 if (f == NULL) {
1101 RADIUS_ERROR("Could not open client file '%s'", client_file);
1102 return NULL;
1103 }
1104
1105 buf = os_malloc(buf_size);
1106 if (buf == NULL) {
1107 fclose(f);
1108 return NULL;
1109 }
1110
1111 clients = tail = NULL;
1112 while (fgets(buf, buf_size, f)) {
1113 /* Configuration file format:
1114 * 192.168.1.0/24 secret
1115 * 192.168.1.2 secret
1116 * fe80::211:22ff:fe33:4455/64 secretipv6
1117 */
1118 line++;
1119 buf[buf_size - 1] = '\0';
1120 pos = buf;
1121 while (*pos != '\0' && *pos != '\n')
1122 pos++;
1123 if (*pos == '\n')
1124 *pos = '\0';
1125 if (*buf == '\0' || *buf == '#')
1126 continue;
1127
1128 pos = buf;
1129 while ((*pos >= '0' && *pos <= '9') || *pos == '.' ||
1130 (*pos >= 'a' && *pos <= 'f') || *pos == ':' ||
1131 (*pos >= 'A' && *pos <= 'F')) {
1132 pos++;
1133 }
1134
1135 if (*pos == '\0') {
1136 failed = 1;
1137 break;
1138 }
1139
1140 if (*pos == '/') {
1141 char *end;
1142 *pos++ = '\0';
1143 mask = strtol(pos, &end, 10);
1144 if ((pos == end) ||
1145 (mask < 0 || mask > (ipv6 ? 128 : 32))) {
1146 failed = 1;
1147 break;
1148 }
1149 pos = end;
1150 } else {
1151 mask = ipv6 ? 128 : 32;
1152 *pos++ = '\0';
1153 }
1154
1155 if (!ipv6 && inet_aton(buf, &addr) == 0) {
1156 failed = 1;
1157 break;
1158 }
1159#ifdef CONFIG_IPV6
1160 if (ipv6 && inet_pton(AF_INET6, buf, &addr6) <= 0) {
1161 if (inet_pton(AF_INET, buf, &addr) <= 0) {
1162 failed = 1;
1163 break;
1164 }
1165 /* Convert IPv4 address to IPv6 */
1166 if (mask <= 32)
1167 mask += (128 - 32);
1168 os_memset(addr6.s6_addr, 0, 10);
1169 addr6.s6_addr[10] = 0xff;
1170 addr6.s6_addr[11] = 0xff;
1171 os_memcpy(addr6.s6_addr + 12, (char *) &addr.s_addr,
1172 4);
1173 }
1174#endif /* CONFIG_IPV6 */
1175
1176 while (*pos == ' ' || *pos == '\t') {
1177 pos++;
1178 }
1179
1180 if (*pos == '\0') {
1181 failed = 1;
1182 break;
1183 }
1184
1185 entry = os_zalloc(sizeof(*entry));
1186 if (entry == NULL) {
1187 failed = 1;
1188 break;
1189 }
1190 entry->shared_secret = os_strdup(pos);
1191 if (entry->shared_secret == NULL) {
1192 failed = 1;
1193 os_free(entry);
1194 break;
1195 }
1196 entry->shared_secret_len = os_strlen(entry->shared_secret);
1197 entry->addr.s_addr = addr.s_addr;
1198 if (!ipv6) {
1199 val = 0;
1200 for (i = 0; i < mask; i++)
1201 val |= 1 << (31 - i);
1202 entry->mask.s_addr = htonl(val);
1203 }
1204#ifdef CONFIG_IPV6
1205 if (ipv6) {
1206 int offset = mask / 8;
1207
1208 os_memcpy(entry->addr6.s6_addr, addr6.s6_addr, 16);
1209 os_memset(entry->mask6.s6_addr, 0xff, offset);
1210 val = 0;
1211 for (i = 0; i < (mask % 8); i++)
1212 val |= 1 << (7 - i);
1213 if (offset < 16)
1214 entry->mask6.s6_addr[offset] = val;
1215 }
1216#endif /* CONFIG_IPV6 */
1217
1218 if (tail == NULL) {
1219 clients = tail = entry;
1220 } else {
1221 tail->next = entry;
1222 tail = entry;
1223 }
1224 }
1225
1226 if (failed) {
1227 RADIUS_ERROR("Invalid line %d in '%s'", line, client_file);
1228 radius_server_free_clients(NULL, clients);
1229 clients = NULL;
1230 }
1231
1232 os_free(buf);
1233 fclose(f);
1234
1235 return clients;
1236}
1237
1238
1239/**
1240 * radius_server_init - Initialize RADIUS server
1241 * @conf: Configuration for the RADIUS server
1242 * Returns: Pointer to private RADIUS server context or %NULL on failure
1243 *
1244 * This initializes a RADIUS server instance and returns a context pointer that
1245 * will be used in other calls to the RADIUS server module. The server can be
1246 * deinitialize by calling radius_server_deinit().
1247 */
1248struct radius_server_data *
1249radius_server_init(struct radius_server_conf *conf)
1250{
1251 struct radius_server_data *data;
1252
1253#ifndef CONFIG_IPV6
1254 if (conf->ipv6) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001255 wpa_printf(MSG_ERROR, "RADIUS server compiled without IPv6 support");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 return NULL;
1257 }
1258#endif /* CONFIG_IPV6 */
1259
1260 data = os_zalloc(sizeof(*data));
1261 if (data == NULL)
1262 return NULL;
1263
1264 os_get_time(&data->start_time);
1265 data->conf_ctx = conf->conf_ctx;
1266 data->eap_sim_db_priv = conf->eap_sim_db_priv;
1267 data->ssl_ctx = conf->ssl_ctx;
1268 data->msg_ctx = conf->msg_ctx;
1269 data->ipv6 = conf->ipv6;
1270 if (conf->pac_opaque_encr_key) {
1271 data->pac_opaque_encr_key = os_malloc(16);
1272 os_memcpy(data->pac_opaque_encr_key, conf->pac_opaque_encr_key,
1273 16);
1274 }
1275 if (conf->eap_fast_a_id) {
1276 data->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1277 if (data->eap_fast_a_id) {
1278 os_memcpy(data->eap_fast_a_id, conf->eap_fast_a_id,
1279 conf->eap_fast_a_id_len);
1280 data->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1281 }
1282 }
1283 if (conf->eap_fast_a_id_info)
1284 data->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1285 data->eap_fast_prov = conf->eap_fast_prov;
1286 data->pac_key_lifetime = conf->pac_key_lifetime;
1287 data->pac_key_refresh_time = conf->pac_key_refresh_time;
1288 data->get_eap_user = conf->get_eap_user;
1289 data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1290 data->tnc = conf->tnc;
1291 data->wps = conf->wps;
1292 data->pwd_group = conf->pwd_group;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001293 data->server_id = conf->server_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 if (conf->eap_req_id_text) {
1295 data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len);
1296 if (data->eap_req_id_text) {
1297 os_memcpy(data->eap_req_id_text, conf->eap_req_id_text,
1298 conf->eap_req_id_text_len);
1299 data->eap_req_id_text_len = conf->eap_req_id_text_len;
1300 }
1301 }
1302
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001303#ifdef CONFIG_RADIUS_TEST
1304 if (conf->dump_msk_file)
1305 data->dump_msk_file = os_strdup(conf->dump_msk_file);
1306#endif /* CONFIG_RADIUS_TEST */
1307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 data->clients = radius_server_read_clients(conf->client_file,
1309 conf->ipv6);
1310 if (data->clients == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001311 wpa_printf(MSG_ERROR, "No RADIUS clients configured");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 radius_server_deinit(data);
1313 return NULL;
1314 }
1315
1316#ifdef CONFIG_IPV6
1317 if (conf->ipv6)
1318 data->auth_sock = radius_server_open_socket6(conf->auth_port);
1319 else
1320#endif /* CONFIG_IPV6 */
1321 data->auth_sock = radius_server_open_socket(conf->auth_port);
1322 if (data->auth_sock < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001323 wpa_printf(MSG_ERROR, "Failed to open UDP socket for RADIUS authentication server");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 radius_server_deinit(data);
1325 return NULL;
1326 }
1327 if (eloop_register_read_sock(data->auth_sock,
1328 radius_server_receive_auth,
1329 data, NULL)) {
1330 radius_server_deinit(data);
1331 return NULL;
1332 }
1333
1334 return data;
1335}
1336
1337
1338/**
1339 * radius_server_deinit - Deinitialize RADIUS server
1340 * @data: RADIUS server context from radius_server_init()
1341 */
1342void radius_server_deinit(struct radius_server_data *data)
1343{
1344 if (data == NULL)
1345 return;
1346
1347 if (data->auth_sock >= 0) {
1348 eloop_unregister_read_sock(data->auth_sock);
1349 close(data->auth_sock);
1350 }
1351
1352 radius_server_free_clients(data, data->clients);
1353
1354 os_free(data->pac_opaque_encr_key);
1355 os_free(data->eap_fast_a_id);
1356 os_free(data->eap_fast_a_id_info);
1357 os_free(data->eap_req_id_text);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001358#ifdef CONFIG_RADIUS_TEST
1359 os_free(data->dump_msk_file);
1360#endif /* CONFIG_RADIUS_TEST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 os_free(data);
1362}
1363
1364
1365/**
1366 * radius_server_get_mib - Get RADIUS server MIB information
1367 * @data: RADIUS server context from radius_server_init()
1368 * @buf: Buffer for returning the MIB data in text format
1369 * @buflen: buf length in octets
1370 * Returns: Number of octets written into buf
1371 */
1372int radius_server_get_mib(struct radius_server_data *data, char *buf,
1373 size_t buflen)
1374{
1375 int ret, uptime;
1376 unsigned int idx;
1377 char *end, *pos;
1378 struct os_time now;
1379 struct radius_client *cli;
1380
1381 /* RFC 2619 - RADIUS Authentication Server MIB */
1382
1383 if (data == NULL || buflen == 0)
1384 return 0;
1385
1386 pos = buf;
1387 end = buf + buflen;
1388
1389 os_get_time(&now);
1390 uptime = (now.sec - data->start_time.sec) * 100 +
1391 ((now.usec - data->start_time.usec) / 10000) % 100;
1392 ret = os_snprintf(pos, end - pos,
1393 "RADIUS-AUTH-SERVER-MIB\n"
1394 "radiusAuthServIdent=hostapd\n"
1395 "radiusAuthServUpTime=%d\n"
1396 "radiusAuthServResetTime=0\n"
1397 "radiusAuthServConfigReset=4\n",
1398 uptime);
1399 if (ret < 0 || ret >= end - pos) {
1400 *pos = '\0';
1401 return pos - buf;
1402 }
1403 pos += ret;
1404
1405 ret = os_snprintf(pos, end - pos,
1406 "radiusAuthServTotalAccessRequests=%u\n"
1407 "radiusAuthServTotalInvalidRequests=%u\n"
1408 "radiusAuthServTotalDupAccessRequests=%u\n"
1409 "radiusAuthServTotalAccessAccepts=%u\n"
1410 "radiusAuthServTotalAccessRejects=%u\n"
1411 "radiusAuthServTotalAccessChallenges=%u\n"
1412 "radiusAuthServTotalMalformedAccessRequests=%u\n"
1413 "radiusAuthServTotalBadAuthenticators=%u\n"
1414 "radiusAuthServTotalPacketsDropped=%u\n"
1415 "radiusAuthServTotalUnknownTypes=%u\n",
1416 data->counters.access_requests,
1417 data->counters.invalid_requests,
1418 data->counters.dup_access_requests,
1419 data->counters.access_accepts,
1420 data->counters.access_rejects,
1421 data->counters.access_challenges,
1422 data->counters.malformed_access_requests,
1423 data->counters.bad_authenticators,
1424 data->counters.packets_dropped,
1425 data->counters.unknown_types);
1426 if (ret < 0 || ret >= end - pos) {
1427 *pos = '\0';
1428 return pos - buf;
1429 }
1430 pos += ret;
1431
1432 for (cli = data->clients, idx = 0; cli; cli = cli->next, idx++) {
1433 char abuf[50], mbuf[50];
1434#ifdef CONFIG_IPV6
1435 if (data->ipv6) {
1436 if (inet_ntop(AF_INET6, &cli->addr6, abuf,
1437 sizeof(abuf)) == NULL)
1438 abuf[0] = '\0';
1439 if (inet_ntop(AF_INET6, &cli->mask6, abuf,
1440 sizeof(mbuf)) == NULL)
1441 mbuf[0] = '\0';
1442 }
1443#endif /* CONFIG_IPV6 */
1444 if (!data->ipv6) {
1445 os_strlcpy(abuf, inet_ntoa(cli->addr), sizeof(abuf));
1446 os_strlcpy(mbuf, inet_ntoa(cli->mask), sizeof(mbuf));
1447 }
1448
1449 ret = os_snprintf(pos, end - pos,
1450 "radiusAuthClientIndex=%u\n"
1451 "radiusAuthClientAddress=%s/%s\n"
1452 "radiusAuthServAccessRequests=%u\n"
1453 "radiusAuthServDupAccessRequests=%u\n"
1454 "radiusAuthServAccessAccepts=%u\n"
1455 "radiusAuthServAccessRejects=%u\n"
1456 "radiusAuthServAccessChallenges=%u\n"
1457 "radiusAuthServMalformedAccessRequests=%u\n"
1458 "radiusAuthServBadAuthenticators=%u\n"
1459 "radiusAuthServPacketsDropped=%u\n"
1460 "radiusAuthServUnknownTypes=%u\n",
1461 idx,
1462 abuf, mbuf,
1463 cli->counters.access_requests,
1464 cli->counters.dup_access_requests,
1465 cli->counters.access_accepts,
1466 cli->counters.access_rejects,
1467 cli->counters.access_challenges,
1468 cli->counters.malformed_access_requests,
1469 cli->counters.bad_authenticators,
1470 cli->counters.packets_dropped,
1471 cli->counters.unknown_types);
1472 if (ret < 0 || ret >= end - pos) {
1473 *pos = '\0';
1474 return pos - buf;
1475 }
1476 pos += ret;
1477 }
1478
1479 return pos - buf;
1480}
1481
1482
1483static int radius_server_get_eap_user(void *ctx, const u8 *identity,
1484 size_t identity_len, int phase2,
1485 struct eap_user *user)
1486{
1487 struct radius_session *sess = ctx;
1488 struct radius_server_data *data = sess->server;
1489
1490 return data->get_eap_user(data->conf_ctx, identity, identity_len,
1491 phase2, user);
1492}
1493
1494
1495static const char * radius_server_get_eap_req_id_text(void *ctx, size_t *len)
1496{
1497 struct radius_session *sess = ctx;
1498 struct radius_server_data *data = sess->server;
1499 *len = data->eap_req_id_text_len;
1500 return data->eap_req_id_text;
1501}
1502
1503
1504static struct eapol_callbacks radius_server_eapol_cb =
1505{
1506 .get_eap_user = radius_server_get_eap_user,
1507 .get_eap_req_id_text = radius_server_get_eap_req_id_text,
1508};
1509
1510
1511/**
1512 * radius_server_eap_pending_cb - Pending EAP data notification
1513 * @data: RADIUS server context from radius_server_init()
1514 * @ctx: Pending EAP context pointer
1515 *
1516 * This function is used to notify EAP server module that a pending operation
1517 * has been completed and processing of the EAP session can proceed.
1518 */
1519void radius_server_eap_pending_cb(struct radius_server_data *data, void *ctx)
1520{
1521 struct radius_client *cli;
1522 struct radius_session *s, *sess = NULL;
1523 struct radius_msg *msg;
1524
1525 if (data == NULL)
1526 return;
1527
1528 for (cli = data->clients; cli; cli = cli->next) {
1529 for (s = cli->sessions; s; s = s->next) {
1530 if (s->eap == ctx && s->last_msg) {
1531 sess = s;
1532 break;
1533 }
1534 if (sess)
1535 break;
1536 }
1537 if (sess)
1538 break;
1539 }
1540
1541 if (sess == NULL) {
1542 RADIUS_DEBUG("No session matched callback ctx");
1543 return;
1544 }
1545
1546 msg = sess->last_msg;
1547 sess->last_msg = NULL;
1548 eap_sm_pending_cb(sess->eap);
1549 if (radius_server_request(data, msg,
1550 (struct sockaddr *) &sess->last_from,
1551 sess->last_fromlen, cli,
1552 sess->last_from_addr,
1553 sess->last_from_port, sess) == -2)
1554 return; /* msg was stored with the session */
1555
1556 radius_msg_free(msg);
1557}